OLDataProvider.m 684 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304
  1. //
  2. // OLDataProvider.m
  3. // iSales-NPD
  4. //
  5. // Created by Ray on 2/2/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "OLDataProvider.h"
  9. #import "iSalesDB.h"
  10. #import "RAUtils.h"
  11. #import "AESCrypt.h"
  12. //#import "AppDelegate.h"
  13. #import "ZipArchive.h"
  14. #import "AppDelegate.h"
  15. #import "pdfCreator.h"
  16. #import "RASingleton.h"
  17. #import "QRCodeGenerator.h"
  18. #import "config.h"
  19. #import "ImageUtils.h"
  20. #import "RAConvertor.h"
  21. #import "ActiveViewController.h"
  22. #import "RANetwork.h"
  23. #import "RADataProvider.h"
  24. @interface OLDataProvider ()
  25. @end
  26. @implementation OLDataProvider
  27. +(NSString*) get_portfolio_price:(NSString*) contact_id item_id:(int) item_id price:(int)price_id db:(sqlite3*)db
  28. {
  29. NSString * sqlQuery = [NSString stringWithFormat:@"select decrypt(price) from model_price where item_id=%d and price_name in (select name from price where type=%d) ",item_id,price_id];
  30. NSString* ret=@"No Price.";
  31. sqlite3_stmt * statement;
  32. // int count=0;
  33. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  34. {
  35. if (sqlite3_step(statement) == SQLITE_ROW)
  36. {
  37. char *price = (char*)sqlite3_column_text(statement, 0);
  38. if(price==nil)
  39. price="";
  40. NSString *nsprice = [[NSString alloc]initWithUTF8String:price];
  41. double dp= [nsprice doubleValue];
  42. ret=[NSString stringWithFormat:@"%.2f",dp];
  43. }
  44. sqlite3_finalize(statement);
  45. }
  46. return ret;
  47. // // [iSalesDB close_db:db];
  48. //
  49. // return nil;
  50. //// if(dprice==DBL_MAX)
  51. //// ret= nil;
  52. //// else
  53. //// ret= [NSNumber numberWithDouble:dprice];
  54. //// return ret;
  55. }
  56. + (NSString *)portfolio_img:(NSString *)item_id db:(sqlite3 *)db
  57. {
  58. NSString* ret= nil;
  59. NSString *sqlQuery = nil;
  60. // sqlQuery = [NSString stringWithFormat:@"select i.url from product m LEFT join model_image i on m.product_id = i.product_id where m.item_id=%@ order by i.default_img desc, i._id asc limit 1;",item_id];// select i.url from model m
  61. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image as i where item_id = %@ and i.type<=1 order by i.default_img desc, i.type desc limit 1;",item_id];
  62. sqlite3_stmt * statement;
  63. // int count=0;
  64. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  65. {
  66. if (sqlite3_step(statement) == SQLITE_ROW)
  67. {
  68. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  69. if(imgurl==nil)
  70. imgurl="";
  71. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  72. ret=nsimgurl;
  73. }
  74. sqlite3_finalize(statement);
  75. }
  76. else
  77. {
  78. ret=@"";
  79. }
  80. // [iSalesDB close_db:db];
  81. // DebugLog(@"data string: %@",ret );
  82. return ret;
  83. }
  84. +(NSMutableDictionary*) prepare_portfolio_data:(NSMutableDictionary*) params
  85. {
  86. // assert(params[@"contact_id"]!=nil);
  87. assert(params[@"user"]!=nil);
  88. NSMutableDictionary* values=params[@"replaceValue"]; // 离线下发的参数
  89. //生成portfolio pdf需要的数据
  90. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  91. data[@"npd_url"]=COMPANY_WEB;
  92. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  93. [formatter setDateFormat:@"MMMM yyyy"];
  94. NSString* date = [formatter stringFromDate:[NSDate date]];
  95. data[@"create_month"]=date;
  96. data[@"company_name"]=COMPANY_FULL_NAME;
  97. data[@"catalog_name"]=params[@"catalog_name"];
  98. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  99. data[@"TOTAL_PAGE"]=0;
  100. data[@"CURRENT_PAGE"]=0;
  101. NSMutableDictionary* grid = [[NSMutableDictionary alloc]init];
  102. BOOL show_group_by = [[params objectForKey:@"show_group_by"] boolValue];
  103. bool show_stockout=[params[@"show_stockout"] boolValue];
  104. NSString *sql = nil;
  105. // v1.90 more color
  106. NSString *group_by = @"";
  107. if(values==nil)
  108. {
  109. if (show_group_by) {
  110. group_by = @"group by b.product_group ";
  111. }
  112. sql=[NSString stringWithFormat:@"select a.product_id,a.name,a.description,a.price,a.sheet_discount,a.qty,a.is_percentage,a.item_id,a.fashion_id,a.line_note,a.percent,a._id,b.availability,b.color,b.legcolor,b.demension,b.seat_height,b.material,b.box_dim,b.volume,b.weight,stockuom,b.load_ability,b.fabric_content,b.assembling,b.made_in,b.more_color from (select _id, product_id,name,description,ifnull(sheet_price,'null') as price,sheet_discount,ifnull(available_qty,'null') as qty,percentage as is_percentage,item_id,fashion_id,line_note,percent from offline_portfolio where product_id in(%@)) a left join (select * from product where product_id in(%@)) b on a.product_id=b.product_id %@ order by a.name",params[@"product_ids"],params[@"product_ids"],group_by];
  113. }
  114. else
  115. {
  116. if (show_group_by) {
  117. group_by = @"group by product_group ";
  118. }
  119. sql=[NSString stringWithFormat:@"select product_id,name,description,null,null,null,null,item_id,null,null,null,-1,availability,color,legcolor,demension,seat_height,material,box_dim,volume,weight,stockuom,load_ability,fabric_content,assembling,made_in,more_color from product where product_id in(%@) %@ order by name",params[@"product_ids"],group_by];
  120. }
  121. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  122. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  123. sqlite3 *db = [iSalesDB get_db];
  124. // NSDictionary *resultDic =
  125. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  126. // [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  127. int product_id = sqlite3_column_int(stmt, 0);
  128. // NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  129. NSString *name = [self textAtColumn:1 statement:stmt];
  130. NSString *description = [self textAtColumn:2 statement:stmt];
  131. double price = sqlite3_column_double(stmt, 3);
  132. NSString *s_price = [self textAtColumn:3 statement:stmt];
  133. double discount = sqlite3_column_double(stmt,4);
  134. int qty = sqlite3_column_int(stmt, 5);
  135. NSString *s_qty = [self textAtColumn:5 statement:stmt];
  136. int is_percentage = sqlite3_column_int(stmt, 6);
  137. NSString *s_is_percentage = [self textAtColumn:6 statement:stmt];
  138. int item_id = sqlite3_column_int(stmt, 7);
  139. // int fashion_id = sqlite3_column_int(stmt, 8);
  140. NSString *line_note = [self textAtColumn:9 statement:stmt];
  141. NSString *s_percent = [self textAtColumn:10 statement:stmt];
  142. double percent = sqlite3_column_double(stmt, 10);
  143. // int portfolio_id = sqlite3_column_int(stmt, 11);
  144. NSString *s_availability = [self textAtColumn:12 statement:stmt];
  145. int availability = sqlite3_column_int(stmt, 12);
  146. NSString *color = [self textAtColumn:13 statement:stmt];
  147. NSString *legcolor = [self textAtColumn:14 statement:stmt];
  148. NSString *demension = [self textAtColumn:15 statement:stmt];
  149. NSString *seat_height = [self textAtColumn:16 statement:stmt];
  150. NSString *material = [self textAtColumn:17 statement:stmt];
  151. NSString *box_dim = [self textAtColumn:18 statement:stmt];
  152. NSString *s_volume = [self textAtColumn:19 statement:stmt];
  153. double volume = sqlite3_column_double(stmt, 19);
  154. NSString *s_weight = [self textAtColumn:20 statement:stmt];
  155. double weight = sqlite3_column_double(stmt, 20);
  156. NSString *s_model_set = [self textAtColumn:21 statement:stmt];
  157. int model_set = sqlite3_column_int(stmt, 21);
  158. NSString *s_load_ability = [self textAtColumn:22 statement:stmt];
  159. double load_ability = sqlite3_column_double(stmt, 22);
  160. NSString *fabric_content = [self textAtColumn:23 statement:stmt];
  161. NSString *assembling = [self textAtColumn:24 statement:stmt];
  162. NSString *made_in = [self textAtColumn:25 statement:stmt];
  163. NSInteger more_color_int = sqlite3_column_int(stmt, 26);
  164. NSString* gprice = [self get_portfolio_price :params[@"contact_id"] item_id:item_id price:[params[@"regular_price"] intValue] db:db];
  165. float bprice=0;
  166. NSDictionary* bundle=[self model_bundle:item_id db:db compute_part:false contactID:params[@"contact_id"] user:params[@"user"]];
  167. for(int b=0;b< [bundle[@"count"] intValue];b++)
  168. {
  169. NSDictionary* bi=bundle[[NSString stringWithFormat:@"item_%d",b]];
  170. bprice+= [bi[@"modulus"] intValue] * [bi[@"unit_price"] floatValue];
  171. }
  172. if(gprice!=nil)
  173. gprice = [NSString stringWithFormat:@"%.2f",[gprice floatValue]+bprice];
  174. if(s_percent.length==0||is_percentage==0)
  175. {
  176. percent=100.0;
  177. if([s_qty isEqualToString:@"null"])
  178. qty=availability;
  179. }
  180. else
  181. {
  182. qty=availability;
  183. }
  184. qty=qty*percent/100+0.5;
  185. for(int i=0;i<[values[@"count"] intValue];i++)
  186. {
  187. NSMutableDictionary * valueitem=values[[NSString stringWithFormat:@"item_%d",i]];;
  188. if([valueitem[@"product_id"] intValue]==product_id)
  189. {
  190. line_note=valueitem[@"line_note"];
  191. qty=[valueitem[@"available_qty"] intValue];
  192. if([[valueitem allKeys] containsObject:@"available_qty"])
  193. s_qty=[NSString stringWithFormat:@"%d",qty];
  194. else
  195. s_qty=@"null";
  196. if(valueitem[@"regular_price"]!=nil)
  197. gprice=[NSString stringWithFormat:@"%.2f",[valueitem[@"regular_price"] floatValue]];
  198. s_price=valueitem[@"special_price"];
  199. price=[valueitem[@"special_price"] floatValue];
  200. discount=0;
  201. // v1.90
  202. more_color_int = [[valueitem objectForKey:@"more_color"] integerValue];
  203. }
  204. }
  205. if(is_percentage==0&&qty==0&&!show_stockout)
  206. {
  207. return;
  208. }
  209. if((is_percentage!=0||s_is_percentage.length!=0)&&qty==0&&!show_stockout)
  210. {
  211. return;
  212. }
  213. NSString* set_price=@"";
  214. if([params[@"entered_price"] boolValue])
  215. {
  216. if (s_price==nil ) {
  217. // set_price=[NSString stringWithFormat:@"Special: $ %@",gprice];
  218. }
  219. else
  220. {
  221. if(price*(1-discount/100.0)!=gprice.floatValue)
  222. set_price=[NSString stringWithFormat:@"Special: $ %.2f",price*(1-discount/100.0)];
  223. }
  224. }
  225. NSString* get_price=@"";
  226. {
  227. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  228. // DebugLog(@"price time interval");
  229. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  230. if(gprice==nil)
  231. get_price=@"Price:No Price.";
  232. else
  233. get_price=[NSString stringWithFormat:@"Price: $ %@",gprice];
  234. }
  235. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  236. // if ([qty_null isEqualToString:@"null"]) {
  237. // qty=availability;
  238. // }
  239. //
  240. // NSString *qty_percent_null = [self textAtColumn:6 statement:stmt];
  241. // if ([qty_percent_null isEqualToString:@"null"]) {
  242. // percentage=1;
  243. // }
  244. NSString * str_availability=[NSString stringWithFormat:@"<b>Availability:</b> %d",(int)qty];
  245. NSString * str_color=[NSString stringWithFormat:@"<b>Color:</b> %@",color];
  246. NSString * str_model_set=[NSString stringWithFormat:@"<b>Set:</b> Sold in QTY of %d",model_set];
  247. NSString * str_legcolor=[NSString stringWithFormat:@"<b>Leg Color:</b> %@",legcolor];
  248. NSString * str_demension=[NSString stringWithFormat:@"<b>Dimension:</b> %@",demension];
  249. NSString * str_seat_height=[NSString stringWithFormat:@"<b>Seat Height:</b> %@",seat_height];
  250. NSString * str_meterial=[NSString stringWithFormat:@"<b>Meterial:</b> %@",material];
  251. NSString * str_box_dim=[NSString stringWithFormat:@"<b>Box Dimension:</b> %@",box_dim];
  252. NSString * str_volume=[NSString stringWithFormat:@"<b>Volume:</b> %.2f",volume];
  253. NSString * str_weight=[NSString stringWithFormat:@"<b>Weight:</b> %.2f",weight];
  254. NSString * str_load_ability=[NSString stringWithFormat:@"<b>Load Ability:</b> %.2f",load_ability];
  255. NSString * str_fabric_content=[NSString stringWithFormat:@"<b>Fabric Content:</b> %@",fabric_content];
  256. NSString * str_assembling=[NSString stringWithFormat:@"<b>Assembling:</b> %@",assembling];
  257. NSString * str_made_in=[NSString stringWithFormat:@"<b>Made In:</b> %@",made_in];
  258. NSString * str_line_note=[NSString stringWithFormat:@"<b>Line Note:</b> %@",line_note];
  259. if([params[@"availability"] boolValue]==false)
  260. str_availability=@"";
  261. else
  262. {
  263. if ([s_qty isEqualToString:@"null"] && [s_availability isEqualToString:@"0"])
  264. str_availability=@"<b>Availability:</b> In Production";
  265. }
  266. if([params[@"color"] boolValue]==false || color.length==0 )
  267. str_color=@"";
  268. if([params[@"set"] boolValue]==false || s_model_set.length==0 )
  269. str_model_set=@"";
  270. if([params[@"leg_color"] boolValue]==false || legcolor.length==0 )
  271. str_legcolor=@"";
  272. if([params[@"dimension"] boolValue]==false || demension.length==0 )
  273. str_demension=@"";
  274. if([params[@"seat_height"] boolValue]==false || seat_height.length==0 )
  275. str_seat_height=@"";
  276. if([params[@"material"] boolValue]==false || material.length==0 )
  277. str_meterial=@"";
  278. if([params[@"box_dimension"] boolValue]==false || box_dim.length==0 )
  279. str_box_dim=@"";
  280. if([params[@"volume"] boolValue]==false || s_volume.length==0 )
  281. str_volume=@"";
  282. if([params[@"weight"] boolValue]==false || s_weight.length==0 )
  283. str_weight=@"";
  284. if([params[@"load_ability"] boolValue]==false || s_load_ability.length==0 )
  285. str_load_ability=@"";
  286. if([params[@"fabric_content"] boolValue]==false || fabric_content.length==0 )
  287. str_fabric_content=@"";
  288. if([params[@"assembling"] boolValue]==false || assembling.length==0 )
  289. str_assembling=@"";
  290. if([params[@"made"] boolValue]==false || made_in.length==0 )
  291. str_made_in=@"";
  292. if([params[@"show_line_note"] boolValue]==false || line_note.length==0 )
  293. str_line_note=@"";
  294. NSMutableArray* arr_detail=[[NSMutableArray alloc]init];
  295. [arr_detail addObject:str_availability];
  296. [arr_detail addObject:str_color];
  297. [arr_detail addObject:str_model_set];
  298. [arr_detail addObject:str_legcolor];
  299. [arr_detail addObject:str_demension];
  300. [arr_detail addObject:str_seat_height];
  301. [arr_detail addObject:str_meterial];
  302. [arr_detail addObject:str_box_dim];
  303. [arr_detail addObject:str_volume];
  304. [arr_detail addObject:str_weight];
  305. [arr_detail addObject:str_load_ability];
  306. [arr_detail addObject:str_fabric_content];
  307. [arr_detail addObject:str_assembling];
  308. [arr_detail addObject:str_made_in];
  309. [arr_detail addObject:str_line_note];
  310. // NSString* detail =[NSString stringWithFormat:@"<font size=0.2>%@</font>",[RAUtils arr2string:arr_detail separator:@" " trim:true]];
  311. NSString* detail =[RAConvertor arr2string:arr_detail separator:@" " trim:true];
  312. //model image;
  313. NSString* img_path = [self portfolio_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  314. // qr image
  315. NSString* qrpath=nil;
  316. if([params[@"show_barcode"] boolValue])
  317. {
  318. NSString* temp = NSTemporaryDirectory();
  319. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  320. qrpath=[temp stringByAppendingPathComponent:filename];
  321. UIImage* qrimg=[QRCodeGenerator qrImageForString:name imageSize:240];
  322. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  323. }
  324. NSString *more_color_path = [[NSBundle mainBundle] pathForResource:@"more_color_64" ofType:@"png"];
  325. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  326. item[@"img"]=img_path;
  327. item[@"img_qr"]=qrpath;
  328. item[@"special_price"]=set_price;
  329. item[@"price"]=get_price;
  330. item[@"name"]=name;
  331. item[@"description"]=description;
  332. item[@"detail"]=detail;
  333. item[@"img"]=img_path;
  334. if (show_group_by && more_color_int != 0) {
  335. item[@"more_color"] = more_color_path;
  336. }
  337. // @{
  338. // //@"linenotes": line_note,
  339. //// @"product_id": product_id_string,
  340. //// @"available_qty": @(qty),
  341. //// @"available_percent" : @(percent),
  342. //// @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  343. //// @"item_id": [NSString stringWithFormat:@"%d",item_id],
  344. //// @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  345. // @"": ,
  346. // @"": ,
  347. // @"": ,
  348. // @"": ,
  349. // @"": ,
  350. // @"": ,
  351. // @"":
  352. // }.mutableCopy;
  353. // if (percentage) {
  354. // [item removeObjectForKey:@"available_qty"];
  355. // } else {
  356. // [item removeObjectForKey:@"available_percent"];
  357. // }
  358. //
  359. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  360. // if ([qty_null isEqualToString:@"null"]) {
  361. // [item removeObjectForKey:@"available_qty"];
  362. // }
  363. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  364. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  365. }];
  366. [iSalesDB close_db:db];
  367. // [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  368. // [dic setValue:@"" forKey:@"email_content"];
  369. // [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  370. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  371. //
  372. // NSMutableDictionary* cell = [[NSMutableDictionary alloc]init];
  373. // cell[@"count"]=[NSNumber numberWithInt:10];
  374. grid[@"cell0"]=dic;
  375. data[@"grid0"]=grid;
  376. return data;
  377. }
  378. +(NSMutableDictionary*) get_pdftemplate:(NSString*) template
  379. {
  380. NSString *default_path = [[NSBundle mainBundle] pathForResource:template ofType:nil];
  381. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  382. NSString *cache_folder=[paths objectAtIndex:0];
  383. NSString *template_path = [cache_folder stringByAppendingPathComponent:[NSString stringWithFormat:@"offline_data/%@",template]];
  384. BOOL bdir=NO;
  385. NSFileManager* fileManager = [NSFileManager defaultManager];
  386. if(PDF_DEBUG)
  387. {
  388. NSData *data = [NSData dataWithContentsOfFile:default_path];
  389. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  390. return ret;
  391. }
  392. if(! [fileManager fileExistsAtPath:template_path isDirectory:&bdir] )
  393. {
  394. NSError * error=nil;
  395. if(![fileManager copyItemAtPath:default_path toPath:template_path error:&error])
  396. {
  397. return nil;
  398. }
  399. }
  400. NSData *data = [NSData dataWithContentsOfFile:template_path];
  401. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  402. return ret;
  403. }
  404. +(NSMutableDictionary*) query_login:(NSString*) user db:(sqlite3*) db
  405. {
  406. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  407. NSString *sqlQuery = [NSString stringWithFormat:@"select can_show_price,can_see_price,contact_id,user_type,can_cancel_order,can_set_cart_price,can_create_portfolio,can_delete_order,can_submit_order,can_set_tearsheet_price,can_create_order,mode,username,can_update_contact_info,first_name from offline_login where lower(username)='%@' ",user.lowercaseString ];
  408. DebugLog(@"offline_login sql:%@",sqlQuery);
  409. sqlite3_stmt * statement;
  410. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  411. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  412. {
  413. if (sqlite3_step(statement) == SQLITE_ROW)
  414. {
  415. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  416. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  417. // int can_show_price = sqlite3_column_int(statement, 0);
  418. // int can_see_price = sqlite3_column_int(statement, 1);
  419. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  420. // if(contact_id==nil)
  421. // contact_id="";
  422. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  423. // int user_type = sqlite3_column_int(statement, 3);
  424. // int can_cancel_order = sqlite3_column_int(statement, 4);
  425. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  426. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  427. // int can_delete_order = sqlite3_column_int(statement, 7);
  428. // int can_submit_order = sqlite3_column_int(statement, 8);
  429. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  430. // int can_create_order = sqlite3_column_int(statement, 10);
  431. // char *mode = (char*)sqlite3_column_text(statement, 11);
  432. // if(mode==nil)
  433. // mode="";
  434. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  435. // char *username = (char*)sqlite3_column_text(statement, 12);
  436. // if(username==nil)
  437. // username="";
  438. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  439. // int can_update_contact_info = sqlite3_column_int(statement, 13);
  440. char *first_name = (char*)sqlite3_column_text(statement, 14);
  441. if(first_name==nil)
  442. first_name="";
  443. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  444. [ret setValue:nsfirst_name forKey:@"first_name"];
  445. }
  446. sqlite3_finalize(statement);
  447. }
  448. #ifdef DEBUG
  449. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  450. #endif
  451. return ret;
  452. }
  453. +(NSMutableDictionary*) prepare_salesorder_data:(NSString*) order_code user:(NSString*) user type:(int) type
  454. {
  455. assert(user!=nil);
  456. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  457. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  458. data[@"npd_url"]=COMPANY_WEB;
  459. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  460. [formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
  461. NSString* date = [formatter stringFromDate:[NSDate date]];
  462. data[@"print_date"]=date;
  463. data[@"company_name"]=COMPANY_FULL_NAME;
  464. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  465. data[@"TOTAL_PAGE"]=0;
  466. data[@"CURRENT_PAGE"]=0;
  467. // [formatter setDateFormat:@"MM/dd/yyyy"];
  468. // date = [formatter stringFromDate:[NSDate date]];
  469. // data[@"creat_date"]=date;
  470. NSString* temp = NSTemporaryDirectory();
  471. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  472. NSString* qrpath=[temp stringByAppendingPathComponent:filename];
  473. UIImage* qrimg=[ImageUtils generateBarCode:order_code width:480 height:44];//[QRCodeGenerator qrImageForString:order_code imageSize:240];
  474. // bool bwrite=
  475. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  476. data[@"img_barcode"]=qrpath;
  477. data[@"so_id"]=order_code;
  478. data[@"company"]=COMPANY_FULL_NAME;
  479. // NSString *sql = [NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate,o.receive_name,o.receive_contact,o.receive_ext,o.sender_name,o.sender_contact,o.sender_ext,o.shipping_billto_name,o.shipping_billto_contact,o.shipping_billto_ext,o.billing_name,o.billing_contact,o.billing_ext,o.returnto_name,o.returnto_contact,o.returnto_ext,o.handling_fee_value,o.paymentsAndCredits,o.total_price,o.customer_cid,o.credit_card_first_name,o.credit_card_last_name,o.credit_card_address1,o.credit_card_address2,o.credit_card_zipcode,o.credit_card_type,decrypt(o.credit_card_number),decrypt(o.credit_card_security_code),decrypt(o.credit_card_expiration_month),o.credit_card_city,o.credit_card_state,o.shipNull,o.erpOrderStatus,decrypt(o.credit_card_expiration_year),o.sign_picpath,o.customer_contact,o.customer_email,o.customer_phone,o.customer_fax,o.editable,o.lift_null,o.handling_null,o.ship_via from (select _id,so_id,poNumber,create_time,status,customer_cid,customer_contact,logist,shipping,lift_gate_value,general_notes,internal_notes,paymentType,lift_gate,receive_name,receive_contact,receive_ext,sender_name,sender_contact,sender_ext,shipping_billto_name,shipping_billto_contact,shipping_billto_ext,billing_name,billing_contact,billing_ext,returnto_name,returnto_contact,returnto_ext,handling_fee_value,paymentsAndCredits,total_price,customer_cid,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,credit_card_number,credit_card_security_code,credit_card_expiration_month,credit_card_city,credit_card_state,ifnull(shipping,'ship_null') as shipNull,erpOrderStatus,credit_card_expiration_year,sign_picpath,customer_contact,customer_email,customer_phone,customer_fax,order_id is null as editable,lift_gate_value is null as lift_null,handling_fee_value is null as handling_null,ship_via from offline_order where so_id=%@) o left join offline_contact c on o.customer_cid=c.contact_id",order_code];
  480. NSString *sql = [NSString stringWithFormat:@"select erpOrderStatus,sender_name,sender_ext,sender_phone,sender_fax,sender_email,warehouse_name,billing_name,billing_ext,billing_phone,billing_fax,billing_email,receive_name,receive_ext,receive_phone,receive_fax,receive_email,poNumber,sales_rep,carrier,vendor_no,terms,sales_terms,port_destination,etd,sign_picpath,status,create_time,shipping,lift_gate_value,handling_fee_value,billing_contact,receive_contact,general_notes,customer_cid,discount from offline_order where so_id='%@'",order_code];
  481. sqlite3 *db = [iSalesDB get_db];
  482. data[@"print_by"]= [self query_login:user db:db][@"first_name"];
  483. sqlite3_stmt * statement;
  484. NSMutableDictionary* datasource = [[NSMutableDictionary alloc]init];
  485. NSMutableDictionary* row0 = [[NSMutableDictionary alloc]init];
  486. row0[@"count"]=[NSNumber numberWithInt:1];
  487. datasource[@"row0"]=row0;
  488. NSString* print_status=@"";
  489. NSString* order_type=@"Offline Quote";
  490. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  491. {
  492. if (sqlite3_step(statement) == SQLITE_ROW)
  493. {
  494. int erpOrderStatus = sqlite3_column_int(statement, 0);
  495. int status = sqlite3_column_int(statement, 26);
  496. if(status==2)
  497. {
  498. switch (erpOrderStatus) {
  499. case 10:
  500. // order_type=@"Quoted Order";
  501. print_status=@"Quote";
  502. break;
  503. case 11:
  504. print_status=@"Booked";
  505. break;
  506. case 12:
  507. print_status=@"Picked";
  508. break;
  509. case 13:
  510. print_status=@"Shipped";
  511. break;
  512. case 14:
  513. print_status=@"Closed";
  514. break;
  515. case 15:
  516. print_status=@"Canceled";
  517. break;
  518. case 16:
  519. print_status=@"All";
  520. break;
  521. case 18:
  522. print_status=@"Processing";
  523. break;
  524. default:
  525. break;
  526. }
  527. }
  528. else{
  529. switch (status) {
  530. case 0:
  531. {
  532. print_status=@"Quote";
  533. break;
  534. }
  535. case 1:
  536. {
  537. print_status=@"Saved";
  538. break;
  539. }
  540. case 3:
  541. {
  542. //canceled
  543. print_status=@"Canceled";
  544. break;
  545. }
  546. default:
  547. break;
  548. }
  549. }
  550. NSString* sender_name = [self textAtColumn:1 statement:statement];
  551. NSString* sender_ext = [self textAtColumn:2 statement:statement];
  552. NSString* sender_phone = [self textAtColumn:3 statement:statement];
  553. NSString* sender_fax = [self textAtColumn:4 statement:statement];
  554. NSString* sender_email = [self textAtColumn:5 statement:statement];
  555. NSString* warehouse_name = [self textAtColumn:6 statement:statement];
  556. NSString* billing_name = [self textAtColumn:7 statement:statement];
  557. NSString* billing_ext = [self textAtColumn:8 statement:statement];
  558. NSString* billing_phone = [self textAtColumn:9 statement:statement];
  559. if(billing_phone.length>0)
  560. billing_phone= [@"TEL: " stringByAppendingString:billing_phone];
  561. NSString* billing_fax = [self textAtColumn:10 statement:statement];
  562. if(billing_fax.length>0)
  563. billing_fax= [@"FAX: " stringByAppendingString:billing_fax];
  564. NSMutableArray* billto_arr0 = [[NSMutableArray alloc]init];
  565. [billto_arr0 addObject:billing_phone];
  566. [billto_arr0 addObject:billing_fax];
  567. NSString* billing_pf=[RAConvertor arr2string:billto_arr0 separator:@" " trim:true];
  568. NSString* billing_email = [self textAtColumn:11 statement:statement];
  569. if(billing_email.length>0)
  570. billing_email= [@"EMAIL: " stringByAppendingString:billing_email];
  571. NSString* billing_contact = [self textAtColumn:31 statement:statement];
  572. if(billing_contact.length>0)
  573. billing_contact= [@"ATTN: " stringByAppendingString:billing_contact];
  574. NSString* receive_name = [self textAtColumn:12 statement:statement];
  575. NSString* receive_ext = [self textAtColumn:13 statement:statement];
  576. NSString* receive_phone = [self textAtColumn:14 statement:statement];
  577. if(receive_phone.length>0)
  578. receive_phone= [@"TEL: " stringByAppendingString:receive_phone];
  579. NSString* receive_fax = [self textAtColumn:15 statement:statement];
  580. if(receive_fax.length>0)
  581. receive_fax= [@"FAX: " stringByAppendingString:receive_fax];
  582. NSMutableArray* receive_arr0 = [[NSMutableArray alloc]init];
  583. [receive_arr0 addObject:receive_phone];
  584. [receive_arr0 addObject:receive_fax];
  585. NSString* receive_email = [self textAtColumn:16 statement:statement];
  586. NSString* receive_pf=[RAConvertor arr2string:billto_arr0 separator:@" " trim:true];
  587. if(receive_email.length>0)
  588. receive_email= [@"EMAIL: " stringByAppendingString:receive_email];
  589. NSString* receive_contact = [self textAtColumn:32 statement:statement];
  590. if(receive_contact.length>0)
  591. receive_contact= [@"ATTN: " stringByAppendingString:receive_contact];
  592. NSString* poNumber = [self textAtColumn:17 statement:statement];
  593. NSString* sales_rep = [self textAtColumn:18 statement:statement];
  594. NSString* carrier = [self textAtColumn:19 statement:statement];
  595. NSString* vendor_no = [self textAtColumn:20 statement:statement];
  596. NSString* terms = [self textAtColumn:21 statement:statement];
  597. NSString* sales_terms = [self textAtColumn:22 statement:statement];
  598. NSString* port_destination = [self textAtColumn:23 statement:statement];
  599. NSString* etd = [self textAtColumn:24 statement:statement];
  600. NSString* sign_picpath = [self textAtColumn:25 statement:statement];
  601. if(sign_picpath.length==0)
  602. sign_picpath=nil;
  603. NSString* create_date = [self textAtColumn:27 statement:statement];
  604. create_date=[self changeDateTime:create_date Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy"];
  605. NSString* general_notes=[self textAtColumn:33 statement:statement];
  606. NSString *customerID = [self textAtColumn:34 statement:statement];
  607. if (customerID.length == 0) {
  608. customerID = nil;
  609. }
  610. if(general_notes.length>0)
  611. {
  612. NSMutableDictionary* row2 = [[NSMutableDictionary alloc]init];
  613. row2[@"item_0"]=@{@"g_notes":general_notes };
  614. row2[@"count"]=[NSNumber numberWithInt:1];
  615. datasource[@"row2"]=row2;
  616. }
  617. NSMutableArray* billto_arr = [[NSMutableArray alloc]init];
  618. [billto_arr addObject:billing_ext];
  619. [billto_arr addObject:billing_pf];
  620. [billto_arr addObject:billing_contact];
  621. [billto_arr addObject:billing_email];
  622. NSString* billto=[RAConvertor arr2string:billto_arr separator:@"\r\n" trim:true];
  623. NSMutableArray* receive_arr = [[NSMutableArray alloc]init];
  624. [receive_arr addObject:receive_ext];
  625. [receive_arr addObject:receive_pf];
  626. [receive_arr addObject:receive_contact];
  627. [receive_arr addObject:receive_email];
  628. NSString* receive=[RAConvertor arr2string:receive_arr separator:@"\r\n" trim:true];
  629. float shipping = sqlite3_column_double(statement, 28);
  630. float lift_gate_value = sqlite3_column_double(statement, 29);
  631. float handling_fee_value = sqlite3_column_double(statement, 30);
  632. #ifdef BUILD_CONTRAST
  633. float order_discount=sqlite3_column_double(statement, 35);
  634. #endif
  635. data[@"company"]=sender_name;
  636. data[@"shipfrom_addr"]=sender_ext;
  637. data[@"shipfrom_phone"]=sender_phone;
  638. data[@"shipfrom_fax"]=sender_fax;
  639. data[@"shipfrom_email"]=sender_email;
  640. data[@"warehouse_name"]=warehouse_name;
  641. data[@"bill_to_name"]=billing_name;
  642. data[@"bill_to_ext"]=billto;
  643. data[@"ship_to_name"]=receive_name;
  644. data[@"ship_to_ext"]=receive;
  645. data[@"po_no"]=poNumber;
  646. data[@"rep"]=sales_rep;
  647. data[@"e_schdule"]=etd;
  648. data[@"sales_terms"]=sales_terms;
  649. data[@"port_destination"]=port_destination;
  650. data[@"terms"]=terms;
  651. data[@"truck_carrier"]=carrier;
  652. data[@"vendor_no"]=vendor_no;
  653. data[@"customer_sign"]=sign_picpath;
  654. data[@"print_status"]=print_status;
  655. data[@"create_date"]=create_date;
  656. NSString *sql_cart=[ NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id,modify_time from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id ",order_code];;
  657. sqlite3_stmt * statement_cart;
  658. // NSDate *date1 = [NSDate date];
  659. int count=0;
  660. // int cart_count=0;
  661. float credits=0;
  662. int dbresult=sqlite3_prepare_v2(db, [sql_cart UTF8String], -1, &statement_cart, nil);
  663. if ( dbresult== SQLITE_OK)
  664. {
  665. NSMutableDictionary* row1 = [[NSMutableDictionary alloc]init];
  666. int row1count=0;
  667. while (sqlite3_step(statement_cart) == SQLITE_ROW)
  668. {
  669. // NSDate *row_date = [NSDate date];
  670. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  671. // int product_id = sqlite3_column_int(statement_cart, 0);
  672. char *str_price = (char*)sqlite3_column_text(statement_cart, 1);
  673. int item_id = sqlite3_column_int(statement_cart, 7);
  674. double discount = sqlite3_column_double(statement_cart, 2);
  675. NSString* Price=nil;
  676. if(str_price==nil)
  677. {
  678. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  679. // NSDate *price_date = [NSDate date];
  680. NSNumber* price = [self get_model_default_price:customerID user:user product_id:nil item_id:@(item_id) db:db];
  681. // DebugLog(@"price time interval");
  682. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  683. if(price==nil)
  684. Price=@"No Price.";
  685. else
  686. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  687. }
  688. else
  689. {
  690. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  691. }
  692. float dunitprice=0;
  693. int item_count = sqlite3_column_int(statement_cart, 3);
  694. NSString* Amount=@"";
  695. if(![Price isEqualToString:@"No Price."])
  696. {
  697. dunitprice=[Price floatValue];
  698. dunitprice=dunitprice* (1-discount/100.0);
  699. Price=[NSString stringWithFormat:@"%.2f",dunitprice];
  700. credits+=dunitprice*item_count;
  701. Amount=[NSString stringWithFormat:@"%.2f",dunitprice*item_count];
  702. }
  703. else
  704. {
  705. Price=@"";
  706. }
  707. // char *line_note = (char*)sqlite3_column_text(statement_cart, 4);
  708. // NSString *nsline_note=nil;
  709. // if(line_note!=nil)
  710. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  711. char *name = (char*)sqlite3_column_text(statement_cart, 5);
  712. NSString *nsname=nil;
  713. if(name!=nil)
  714. nsname= [[NSString alloc]initWithUTF8String:name];
  715. char *description = (char*)sqlite3_column_text(statement_cart, 6);
  716. NSString *nsdescription=nil;
  717. if(description!=nil)
  718. nsdescription= [[NSString alloc]initWithUTF8String:description];
  719. // int stockUom = sqlite3_column_int(statement_cart, 8);
  720. // int _id = sqlite3_column_int(statement_cart, 9);
  721. // NSDate *subtotal_date = [NSDate date];
  722. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  723. // DebugLog(@"subtotal_date time interval");
  724. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  725. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  726. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  727. // int carton=[bsubtotaljson[@"carton"] intValue];
  728. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  729. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  730. // NSDate *img_date = [NSDate date];
  731. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  732. // DebugLog(@"img_date time interval");
  733. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  734. itemjson[@"model"]=nsname;
  735. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  736. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  737. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  738. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  739. // itemjson[@"check"]=@"true";
  740. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  741. itemjson[@"count"]=[NSString stringWithFormat:@"%d",item_count];
  742. itemjson[@"unit_price"]=Price;
  743. itemjson[@"amount"]=Amount;
  744. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  745. itemjson[@"description"]=[NSString stringWithFormat:@"%@",nsdescription ];
  746. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  747. // itemjson[@"note"]=nsline_note;
  748. // NSDate *date2 = [NSDate date];
  749. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  750. // DebugLog(@"model_bundle time interval");
  751. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  752. itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  753. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=itemjson;
  754. row1count++;
  755. NSDictionary* combine =[self model_bundle:item_id db:db compute_part:false contactID:customerID user:user];
  756. for(int c=0;c<[combine[@"count"] intValue];c++)
  757. {
  758. NSMutableDictionary* combine_item=[combine[[NSString stringWithFormat:@"item_%d",c]] mutableCopy];
  759. combine_item[@"count"]=[NSString stringWithFormat:@"%d",[combine_item[@"modulus"] intValue]*item_count];
  760. // combine_item[@"count"]=[NSNumber numberWithInt:[combine_item[@"modulus"] intValue]*item_count];
  761. double combine_amount=[combine_item[@"modulus"] intValue]*item_count* [combine_item[@"unit_price"] floatValue];
  762. credits+=combine_amount;
  763. combine_item[@"amount"]=[NSString stringWithFormat:@"%.2f",combine_amount];
  764. combine_item[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,c+2];
  765. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=combine_item;
  766. row1count++;
  767. }
  768. count++;
  769. // DebugLog(@"row time interval");
  770. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  771. }
  772. row1[@"count"]=[NSNumber numberWithInt:row1count];
  773. datasource[@"row1"]=row1;
  774. data[@"table_model"]=datasource;
  775. sqlite3_finalize(statement_cart);
  776. }
  777. NSMutableDictionary* fee = [[NSMutableDictionary alloc]init];
  778. NSMutableDictionary* fee_row0 = [[NSMutableDictionary alloc]init];
  779. // if(credits>0)
  780. fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  781. // else
  782. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": @""};
  783. if(shipping>0)
  784. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": [NSString stringWithFormat:@"%.2f",shipping]};
  785. else
  786. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": @""};
  787. if(lift_gate_value>0)
  788. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",lift_gate_value]};
  789. else
  790. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": @""};
  791. if(handling_fee_value>0)
  792. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",handling_fee_value]};
  793. else
  794. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": @""};
  795. #ifdef BUILD_CONTRAST
  796. if(order_discount>0)
  797. fee_row0[@"item_4"]=@{@"name":@"Order discount:" ,@"value": [NSString stringWithFormat:@"-%.2f",credits*(order_discount/100.0)]};
  798. else
  799. fee_row0[@"item_4"]=@{@"name":@"Order discount:" ,@"value": @""};
  800. float total = credits*(1-order_discount/100.0)+shipping+lift_gate_value+handling_fee_value;
  801. if(total>0)
  802. fee_row0[@"item_5"]=@{@"name":@"Total:" ,@"value": [NSString stringWithFormat:@"%.2f",total]};
  803. else
  804. fee_row0[@"item_5"]=@{@"name":@"Total:" ,@"value": @""};
  805. fee_row0[@"count"]=[NSNumber numberWithInt:6];
  806. #else
  807. float total = credits+shipping+lift_gate_value+handling_fee_value;
  808. if(total>0)
  809. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": [NSString stringWithFormat:@"%.2f",total]};
  810. else
  811. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": @""};
  812. fee_row0[@"count"]=[NSNumber numberWithInt:5];
  813. #endif
  814. fee[@"row0"]=fee_row0;
  815. data[@"table_fee"]=fee;
  816. // NSString *sign_url = [self textAtColumn:51 statement:statement];
  817. // ret[@"sign_url"] = sign_url;
  818. //
  819. // customer_contact = [self textAtColumn:52 statement:statement];
  820. // customer_email = [self textAtColumn:53 statement:statement];
  821. // customer_phone = [self textAtColumn:54 statement:statement];
  822. // customer_fax = [self textAtColumn:55 statement:statement];
  823. //
  824. //
  825. // int offline_edit=sqlite3_column_int(statement, 56);
  826. //
  827. // ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  828. //
  829. // char *soid = (char*)sqlite3_column_text(statement, 1);
  830. // if(soid==nil)
  831. // soid= "";
  832. // nssoid= [[NSString alloc]initWithUTF8String:soid];
  833. // // so#
  834. // ret[@"so#"] = nssoid;
  835. //
  836. // char *poNumber = (char*)sqlite3_column_text(statement, 2);
  837. // if(poNumber==nil)
  838. // poNumber= "";
  839. // NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  840. //
  841. //
  842. //
  843. // char *create_time = (char*)sqlite3_column_text(statement, 3);
  844. // if(create_time==nil)
  845. // create_time= "";
  846. // NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  847. // nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  848. //
  849. // int status = sqlite3_column_int(statement, 4);
  850. // int erpStatus = sqlite3_column_int(statement, 49);
  851. // NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  852. //
  853. // // status
  854. // if (status > 1 && status != 3) {
  855. // status = erpStatus;
  856. // } else if (status == 3) {
  857. // status = 15;
  858. // }
  859. // ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  860. // ret[@"order_status"] = nsstatus;
  861. //
  862. //
  863. // char *company_name = (char*)sqlite3_column_text(statement, 5);
  864. // if(company_name==nil)
  865. // company_name= "";
  866. // NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  867. // // company name
  868. // ret[@"company_name"] = nscompany_name;
  869. //
  870. // char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  871. // if(customer_contact==nil)
  872. // customer_contact= "";
  873. // NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  874. //
  875. //
  876. // char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  877. // if(addr_1==nil)
  878. // addr_1="";
  879. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  880. //
  881. // char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  882. // if(addr_2==nil)
  883. // addr_2="";
  884. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  885. //
  886. //
  887. // char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  888. // if(addr_3==nil)
  889. // addr_3="";
  890. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  891. //
  892. //
  893. // char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  894. // if(addr_4==nil)
  895. // addr_4="";
  896. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  897. //
  898. // NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  899. // [arr_addr addObject:nsaddr_1];
  900. // [arr_addr addObject:nsaddr_2];
  901. // [arr_addr addObject:nsaddr_3];
  902. // [arr_addr addObject:nsaddr_4];
  903. // NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  904. //
  905. //
  906. // char *logist = (char*)sqlite3_column_text(statement, 11);
  907. // if(logist==nil)
  908. // logist= "";
  909. // NSString *DebugLogist=[[NSString alloc]initWithUTF8String:logist];
  910. // if (status == -11 || status == 10 || status == 11) {
  911. // DebugLogist = [self textAtColumn:59 statement:statement];
  912. // };
  913. //
  914. // NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  915. // if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  916. // shipping = @"Shipping To Be Quoted";
  917. // } else {
  918. // shippingFee = sqlite3_column_double(statement, 12);
  919. // }
  920. //
  921. // // Shipping
  922. // ret[@"Shipping"] = shipping;
  923. //
  924. // int have_lift_gate = sqlite3_column_int(statement, 17);
  925. // lift_gate = sqlite3_column_double(statement, 13);
  926. // // Liftgate Fee(No loading dock)
  927. // if (!have_lift_gate) {
  928. // lift_gate = 0;
  929. // }
  930. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  931. // if (sqlite3_column_int(statement, 57)) {
  932. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  933. // }
  934. //
  935. // char *general_notes = (char*)sqlite3_column_text(statement, 14);
  936. // if(general_notes==nil)
  937. // general_notes= "";
  938. // NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  939. //
  940. // char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  941. // if(internal_notes==nil)
  942. // internal_notes= "";
  943. // NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  944. //
  945. // char *payment_type = (char*)sqlite3_column_text(statement, 16);
  946. // if(payment_type==nil)
  947. // payment_type= "";
  948. // NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  949. //
  950. //
  951. //
  952. // // order info
  953. // orderinfo = [self textFileName:@"order_info.html"];
  954. //
  955. //
  956. // orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  957. // orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  958. // orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  959. // orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  960. //
  961. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  962. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  963. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  964. // orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:DebugLogist];
  965. // orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  966. // orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  967. // orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  968. // orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  969. //
  970. // NSString *payment = nil;
  971. // if([nspayment_type isEqualToString:@"Credit Card"])
  972. // {
  973. // payment = [self textFileName:@"creditcardpayment.html"];
  974. //
  975. // NSString *card_first_name = [self textAtColumn:37 statement:statement];
  976. // NSString *card_last_name = [self textAtColumn:38 statement:statement];
  977. // NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  978. // NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  979. // NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  980. // NSString *card_type = [self textAtColumn:42 statement:statement];
  981. // if (card_type.length > 0) { // 显示星号
  982. // card_type = @"****";
  983. // }
  984. // NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  985. // if (card_number.length > 0 && card_number.length > 4) {
  986. // for (int i = 0; i < card_number.length - 4; i++) {
  987. // card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  988. // }
  989. // } else {
  990. // card_number = @"";
  991. // }
  992. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  993. // NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  994. // NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  995. // NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  996. // if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  997. // card_expiration = @"****";
  998. // }
  999. //
  1000. //
  1001. // NSString *card_city = [self textAtColumn:46 statement:statement];
  1002. // NSString *card_state = [self textAtColumn:47 statement:statement];
  1003. //
  1004. // payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  1005. // payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  1006. // payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  1007. // payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  1008. // payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  1009. // payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  1010. // payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  1011. // payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  1012. // payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  1013. // payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  1014. //
  1015. //
  1016. // }
  1017. // else
  1018. // {
  1019. // payment=[self textFileName:@"normalpayment.html"];
  1020. // payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  1021. // }
  1022. // orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  1023. //
  1024. // ret[@"result"]= [NSNumber numberWithInt:2];
  1025. //
  1026. // // more info
  1027. // moreInfo = [self textFileName:@"more_info.html"];
  1028. //
  1029. //
  1030. // // ShipToCompany_or_&nbsp
  1031. // NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  1032. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  1033. // NSString *shipToName = [self textAtColumn:19 statement:statement];
  1034. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  1035. // NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  1036. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  1037. //
  1038. //
  1039. // // ShipFromCompany_or_&nbsp
  1040. // NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  1041. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  1042. // NSString *shipFromName = [self textAtColumn:22 statement:statement];
  1043. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  1044. // NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  1045. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  1046. //
  1047. //
  1048. // // FreightBillToCompany_or_&nbsp
  1049. // NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  1050. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  1051. // NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  1052. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  1053. // NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  1054. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  1055. //
  1056. //
  1057. // // MerchandiseBillToCompany_or_&nbsp
  1058. // NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  1059. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  1060. // NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  1061. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  1062. // NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  1063. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  1064. //
  1065. //
  1066. // // ReturnToCompany_or_&nbsp
  1067. // NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  1068. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  1069. // NSString *returnToName = [self textAtColumn:31 statement:statement];
  1070. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  1071. // NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  1072. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  1073. // //
  1074. //
  1075. // // DebugLog(@"more info : %@",moreInfo);
  1076. //
  1077. // // handling fee
  1078. // handlingFee = sqlite3_column_double(statement, 33);
  1079. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  1080. // if (sqlite3_column_int(statement, 58)) {
  1081. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  1082. // }
  1083. // //
  1084. //
  1085. // // customer info
  1086. // customerID = [self textAtColumn:36 statement:statement];
  1087. //
  1088. // // mode
  1089. // ret[@"mode"] = appDelegate.mode;
  1090. //
  1091. // // model_count
  1092. // ret[@"model_count"] = @(0);
  1093. }
  1094. sqlite3_finalize(statement);
  1095. }
  1096. [iSalesDB close_db:db];
  1097. data[@"order_type"]=order_type;
  1098. #ifdef DEBUG
  1099. DebugLog(@"debug sales order data:%@", [RAConvertor dict2string:data]);
  1100. #endif
  1101. return data;
  1102. }
  1103. +(NSData*) offline_request_salesorder :(NSMutableDictionary *) params
  1104. {
  1105. assert(params[@"user"]!=nil);
  1106. NSString* template_file=nil;
  1107. switch([params[@"thumb"] intValue])
  1108. {
  1109. case 0:
  1110. template_file= @"so.json";
  1111. break;
  1112. case 1:
  1113. template_file= @"so_thumb.json";
  1114. break;
  1115. default:
  1116. template_file= @"so.json";
  1117. }
  1118. NSMutableDictionary* data = [self prepare_salesorder_data:params[@"order_code"] user:params[@"user"] type:[params[@"thumb"] intValue]];
  1119. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1120. if(data[@"table_model"][@"row2"]==nil)
  1121. template[@"pages"][@"page_0"][@"contents"][@"item_0"][@"rows"][@"count"]=[NSNumber numberWithInt:1];
  1122. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1123. DebugLog(@"%@",file);
  1124. // return nil;
  1125. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1126. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1127. if (file) {
  1128. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1129. [dic setObject:file forKey:@"pdf_path"];
  1130. dic[@"isLocalFile"]=@"true";
  1131. return [RAConvertor dict2data:dic];
  1132. }
  1133. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1134. dic[@"isLocalFile"]=@"true";
  1135. return [RAConvertor dict2data:dic];
  1136. }
  1137. +(NSData*) offline_request_tearsheet :(NSMutableDictionary *) params
  1138. {
  1139. // assert(params[@"contact_id"]!=nil);
  1140. // assert(params[@"user"]!=nil);
  1141. if(params[@"user"]==nil)
  1142. {
  1143. AppDelegate *appDelegate = nil;
  1144. //some UI methods ej
  1145. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  1146. if(appDelegate.user!=nil)
  1147. [params setValue:appDelegate.user forKey:@"user"];
  1148. }
  1149. NSString* template_file=nil;
  1150. switch([params[@"pdf_style"] intValue])
  1151. {
  1152. case 0:
  1153. template_file= @"portfolio_2x3.json";
  1154. break;
  1155. case 1:
  1156. template_file= @"portfolio_3x2.json";
  1157. break;
  1158. default:
  1159. template_file= @"portfolio_3x2.json";
  1160. }
  1161. NSMutableDictionary* data = [self prepare_portfolio_data:params];
  1162. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1163. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1164. DebugLog(@"%@",file);
  1165. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1166. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1167. if (file) {
  1168. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1169. [dic setObject:file forKey:@"pdf_path"];
  1170. dic[@"isLocalFile"]=@"true";
  1171. return [RAConvertor dict2data:dic];
  1172. }
  1173. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1174. dic[@"isLocalFile"]=@"true";
  1175. return [RAConvertor dict2data:dic];
  1176. }
  1177. +(NSString*) get_offline_soid:(sqlite3*)db
  1178. {
  1179. NSString* soid=nil;
  1180. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  1181. [formatter setDateFormat:@"yyMMdd"];
  1182. NSString* date = [formatter stringFromDate:[NSDate date]];
  1183. for(int i=1;i<999;i++)
  1184. {
  1185. soid=[NSString stringWithFormat:@"OFF%@%4d",date,i];
  1186. soid= [soid stringByReplacingOccurrencesOfString:@" " withString:@"0"];
  1187. int count=[iSalesDB get_recordcount:db table:@"offline_order" where:[NSString stringWithFormat:@"so_id='%@'",soid]];
  1188. if(count==0)
  1189. return soid;
  1190. }
  1191. return nil;
  1192. }
  1193. +(NSArray*) enumOfflineOrder
  1194. {
  1195. NSMutableArray* ret = [[NSMutableArray alloc] init];
  1196. sqlite3 *db = [iSalesDB get_db];
  1197. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  1198. sqlite3_stmt * statement;
  1199. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1200. if ( dbresult== SQLITE_OK)
  1201. {
  1202. while (sqlite3_step(statement) == SQLITE_ROW)
  1203. {
  1204. char *so_id = (char*)sqlite3_column_text(statement, 0);
  1205. NSString* nsso_id=nil;
  1206. if(so_id!=nil)
  1207. {
  1208. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  1209. }
  1210. [ret addObject:nsso_id];
  1211. }
  1212. sqlite3_finalize(statement);
  1213. }
  1214. [iSalesDB close_db:db];
  1215. return ret;
  1216. }
  1217. +(void) saveScanSubmitList:(NSMutableDictionary*) submitlist
  1218. {
  1219. if(submitlist==nil)
  1220. return;
  1221. NSString *userdir = [OLDataProvider getUserPath];
  1222. NSString *submitlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1223. [RAUtils dicttofile:submitlistPath dict:submitlist];
  1224. }
  1225. +(void) saveScanOrderList:(NSMutableDictionary*) orderlist
  1226. {
  1227. if(orderlist==nil)
  1228. return;
  1229. NSString *userdir = [OLDataProvider getUserPath];
  1230. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1231. [RAUtils dicttofile:orderlistPath dict:orderlist];
  1232. }
  1233. +(NSMutableDictionary*) getScanSubmitList
  1234. {
  1235. NSString *userdir = [OLDataProvider getUserPath];
  1236. NSString *submitlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1237. NSMutableDictionary* submitlist = nil;
  1238. if (![[NSFileManager defaultManager] fileExistsAtPath:submitlistPath]) {
  1239. submitlist = [NSMutableDictionary new];
  1240. }
  1241. else
  1242. {
  1243. submitlist=[[RAUtils dictfromfile:submitlistPath] mutableCopy];
  1244. }
  1245. return submitlist;
  1246. }
  1247. //+(NSMutableDictionary*) getScanOrderList:(NSString*)userdir{
  1248. //// NSString *userdir = [self getUserPath:user];
  1249. // NSMutableDictionary* orderlist = nil;
  1250. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1251. //
  1252. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1253. //
  1254. //
  1255. // orderlist = [NSMutableDictionary new];
  1256. //
  1257. // // [orderlist addObject:appDelegate.order_code];
  1258. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1259. //
  1260. // }
  1261. // else
  1262. // {
  1263. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1264. //
  1265. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1266. //
  1267. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1268. // }
  1269. // return orderlist;
  1270. //}
  1271. +(NSMutableDictionary*) getScanOrderList{
  1272. NSString *userdir = [OLDataProvider getUserPath];
  1273. NSMutableDictionary* orderlist = nil;
  1274. NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1275. if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1276. orderlist = [NSMutableDictionary new];
  1277. // [orderlist addObject:appDelegate.order_code];
  1278. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1279. }
  1280. else
  1281. {
  1282. orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1283. // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1284. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1285. }
  1286. return orderlist;
  1287. }
  1288. +(void) save2submitScanOrder:(NSString*) soid company:(NSString*) company
  1289. {
  1290. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1291. // NSString *documents = [paths objectAtIndex:0];
  1292. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1293. // NSString *userdir = [OLDataProvider getUserPath];
  1294. NSMutableDictionary* orderlist = [self getScanOrderList];
  1295. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1296. int count = [orderlist[@"count"] intValue];
  1297. if(count==0)
  1298. {
  1299. // order list 为空,不用维护
  1300. #ifdef RA_NOTIFICATION
  1301. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1302. #else
  1303. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1304. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1305. #endif
  1306. return;
  1307. }
  1308. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1309. //
  1310. // // order list 为空,不用维护
  1311. //
  1312. //
  1313. // // [orderlist addObject:appDelegate.order_code];
  1314. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1315. //
  1316. // }
  1317. // else
  1318. // {
  1319. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1320. //
  1321. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1322. //
  1323. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1324. // }
  1325. //==================== order list 删除
  1326. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1327. NSMutableDictionary* submititem =nil;
  1328. int newcount = 0;
  1329. for(int i=0;i<count;i++)
  1330. {
  1331. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1332. if([item[@"order_code"] isEqualToString: appDelegate.order_code ])
  1333. {
  1334. submititem = item;
  1335. continue;
  1336. }
  1337. else
  1338. {
  1339. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1340. newcount++;
  1341. }
  1342. }
  1343. if(submititem==nil)
  1344. {
  1345. //order list 里没有和当前打开的order code一致的order
  1346. //这种情况往往是因为重复点提交按钮,order已关闭。
  1347. return;
  1348. }
  1349. newlist[@"count"] = @(newcount);
  1350. [self saveScanOrderList:newlist];
  1351. // [RAUtils dicttofile:orderlistPath dict:newlist];
  1352. //////////////////---=============================== submitlist 添加
  1353. // NSString *submitlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1354. NSMutableDictionary* submitlist = [OLDataProvider getScanSubmitList];
  1355. // if (![[NSFileManager defaultManager] fileExistsAtPath:submitlistPath]) {
  1356. //
  1357. // submitlist = [NSMutableDictionary new];
  1358. //
  1359. //
  1360. // }
  1361. // else
  1362. // {
  1363. // submitlist=[[RAUtils dictfromfile:submitlistPath] mutableCopy];
  1364. //
  1365. //
  1366. // }
  1367. int submitcount =[submitlist[@"count"] intValue];
  1368. submititem[@"order_status"] = soid;//@"Submitted Order";
  1369. submititem[@"customer_name"] = company;//@"Submitted Order";
  1370. submitlist[[NSString stringWithFormat:@"item_%d",submitcount]]=submititem;
  1371. submitlist[@"count"] = @(submitcount+1);
  1372. // [RAUtils dicttofile:submitlistPath dict:submitlist];
  1373. [self saveScanSubmitList:submitlist];
  1374. #ifdef RA_NOTIFICATION
  1375. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1376. #else
  1377. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1378. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1379. #endif
  1380. }
  1381. +(void) save2submitScanOrder1
  1382. {
  1383. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1384. // NSString *documents = [paths objectAtIndex:0];
  1385. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1386. // NSString *userdir = [OLDataProvider getUserPath];
  1387. NSMutableDictionary* orderlist = [self getScanOrderList];
  1388. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1389. int count = [orderlist[@"count"] intValue];
  1390. if(count==0)
  1391. {
  1392. // order list 为空,不用维护
  1393. #ifdef RA_NOTIFICATION
  1394. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1395. #else
  1396. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1397. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1398. #endif
  1399. return;
  1400. return;
  1401. }
  1402. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1403. int newcount = 0;
  1404. for(int i=0;i<count;i++)
  1405. {
  1406. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1407. if([item[@"order_code"] isEqualToString: appDelegate.order_code ])
  1408. continue;
  1409. else
  1410. {
  1411. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1412. newcount++;
  1413. }
  1414. }
  1415. newlist[@"count"] = @(newcount);
  1416. [self saveScanOrderList:newlist];
  1417. // [RAUtils dicttofile:orderlistPath dict:newlist];
  1418. #ifdef RA_NOTIFICATION
  1419. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1420. #else
  1421. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1422. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1423. #endif
  1424. }
  1425. //+(void) createScanTempFolder
  1426. //{
  1427. // return;
  1428. //// NSString* scanFilePath = [self getScanTempPath];
  1429. //// BOOL bdir=YES;
  1430. //// NSFileManager* fileManager = [NSFileManager defaultManager];
  1431. //// if(! [fileManager fileExistsAtPath:scanFilePath isDirectory:&bdir])
  1432. //// {
  1433. ////
  1434. //// NSError *error = nil;
  1435. //// bool bsuccess=[fileManager createDirectoryAtPath:scanFilePath withIntermediateDirectories:YES attributes:nil error:&error];
  1436. ////
  1437. //// if(!bsuccess)
  1438. //// DebugLog(@"Create cache folder failed");
  1439. ////
  1440. //// // if(bsuccess)
  1441. //// // {
  1442. //// // sqlite3 *db = [self get_db];
  1443. //// //
  1444. //// // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1445. //// // [iSalesDB close_db:db];
  1446. //// // }
  1447. //// }
  1448. //}
  1449. +(NSString*) getScanOrderPathByCode:(NSString*) order_code
  1450. {
  1451. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1452. NSString *documents = [paths objectAtIndex:0];
  1453. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1454. NSString* servername = addressDic[@"name"];
  1455. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1456. NSString* user = appDelegate.user;
  1457. NSString* orderpath = [documents stringByAppendingPathComponent:servername];
  1458. orderpath = [orderpath stringByAppendingPathComponent:user];
  1459. orderpath = [orderpath stringByAppendingPathComponent:order_code];
  1460. BOOL bdir=YES;
  1461. NSFileManager* fileManager = [NSFileManager defaultManager];
  1462. if(! [fileManager fileExistsAtPath:orderpath isDirectory:&bdir])
  1463. {
  1464. NSError *error = nil;
  1465. bool bsuccess=[fileManager createDirectoryAtPath:orderpath withIntermediateDirectories:YES attributes:nil error:&error];
  1466. if(!bsuccess)
  1467. DebugLog(@"Create cache folder failed");
  1468. // if(bsuccess)
  1469. // {
  1470. // sqlite3 *db = [self get_db];
  1471. //
  1472. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1473. // [iSalesDB close_db:db];
  1474. // }
  1475. }
  1476. return orderpath;
  1477. }
  1478. +(NSString*) getScanOrderPath
  1479. {
  1480. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1481. NSString *documents = [paths objectAtIndex:0];
  1482. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1483. NSString* servername = addressDic[@"name"];
  1484. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1485. NSString* user = appDelegate.user;
  1486. NSString* orderpath = [documents stringByAppendingPathComponent:servername];
  1487. orderpath = [orderpath stringByAppendingPathComponent:user];
  1488. orderpath = [orderpath stringByAppendingPathComponent:appDelegate.order_code];
  1489. BOOL bdir=YES;
  1490. NSFileManager* fileManager = [NSFileManager defaultManager];
  1491. if(! [fileManager fileExistsAtPath:orderpath isDirectory:&bdir])
  1492. {
  1493. NSError *error = nil;
  1494. bool bsuccess=[fileManager createDirectoryAtPath:orderpath withIntermediateDirectories:YES attributes:nil error:&error];
  1495. if(!bsuccess)
  1496. DebugLog(@"Create cache folder failed");
  1497. // if(bsuccess)
  1498. // {
  1499. // sqlite3 *db = [self get_db];
  1500. //
  1501. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1502. // [iSalesDB close_db:db];
  1503. // }
  1504. }
  1505. return orderpath;
  1506. }
  1507. //+(NSString*) getScanTempPath
  1508. //{
  1509. // return nil;
  1510. ////
  1511. //// if(RASingleton.sharedInstance.scan_temp_code == nil)
  1512. //// RASingleton.sharedInstance.scan_temp_code = [self createScanTempCode];
  1513. //// return [NSTemporaryDirectory() stringByAppendingPathComponent:RASingleton.sharedInstance.scan_temp_code];
  1514. //}
  1515. //+(void) moveScanTemp2Order
  1516. //{
  1517. // return;
  1518. //// NSString *orderdir = [OLDataProvider getScanOrderPath];
  1519. ////
  1520. //// NSError *error = nil;
  1521. //// NSString* tempdir = [OLDataProvider getScanTempPath];
  1522. ////
  1523. //// // NSArray* arr_files=[RAUtils allFilesAtPath:tempdir];
  1524. //// // for(int i=0;i<arr_files.count;i++)
  1525. //// // {
  1526. //// // NSString* file=arr_files[i];
  1527. //// //
  1528. //// //
  1529. //// // }
  1530. //// [ [NSFileManager defaultManager] moveItemAtPath:[tempdir stringByAppendingPathComponent:@"cart.json"] toPath:[orderdir stringByAppendingPathComponent:@"cart.json"] error:&error];
  1531. ////
  1532. //
  1533. //}
  1534. //+(NSString*) getUserPath: (NSString*) user
  1535. //{
  1536. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1537. // NSString *documents = [paths objectAtIndex:0];
  1538. //
  1539. // NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1540. //
  1541. // NSString* servername = addressDic[@"name"];
  1542. //// AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1543. //// NSString* user = appDelegate.user;
  1544. // NSString* userpath = [documents stringByAppendingPathComponent:servername];
  1545. // userpath = [userpath stringByAppendingPathComponent:user];
  1546. // return userpath;
  1547. //}
  1548. +(NSString*) getServerPath
  1549. {
  1550. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1551. NSString *documents = [paths objectAtIndex:0];
  1552. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1553. NSString* servername = addressDic[@"name"];
  1554. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1555. // NSString* user = appDelegate.user;
  1556. // NSString* user = RASingleton.sharedInstance.user;
  1557. NSString* serverpath = [documents stringByAppendingPathComponent:servername];
  1558. // userpath = [userpath stringByAppendingPathComponent:user];
  1559. NSFileManager* fileManager = [NSFileManager defaultManager];
  1560. BOOL bdir=YES;
  1561. if(! [fileManager fileExistsAtPath:serverpath isDirectory:&bdir])
  1562. {
  1563. NSError *error = nil;
  1564. [fileManager createDirectoryAtPath:serverpath withIntermediateDirectories:YES attributes:nil error:&error];
  1565. }
  1566. return serverpath;
  1567. }
  1568. +(NSString*) getUserPath
  1569. {
  1570. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1571. NSString *documents = [paths objectAtIndex:0];
  1572. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  1573. NSString* servername = addressDic[@"name"];
  1574. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1575. // NSString* user = appDelegate.user;
  1576. NSString* user = RASingleton.sharedInstance.user;
  1577. NSString* userpath = [documents stringByAppendingPathComponent:servername];
  1578. userpath = [userpath stringByAppendingPathComponent:user];
  1579. NSFileManager* fileManager = [NSFileManager defaultManager];
  1580. BOOL bdir=YES;
  1581. if(! [fileManager fileExistsAtPath:userpath isDirectory:&bdir])
  1582. {
  1583. NSError *error = nil;
  1584. [fileManager createDirectoryAtPath:userpath withIntermediateDirectories:YES attributes:nil error:&error];
  1585. }
  1586. return userpath;
  1587. }
  1588. +(bool) isSavedScanOrder:(NSString*) order_code
  1589. {
  1590. if(order_code == nil)
  1591. return false;
  1592. NSMutableDictionary * orderlist=[self getScanOrderList];
  1593. int count = [orderlist[@"count"] intValue];
  1594. for(int i=0;i<count;i++)
  1595. {
  1596. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1597. if([item[@"order_code"] isEqualToString: order_code ])
  1598. {
  1599. return true;
  1600. }
  1601. }
  1602. return false;
  1603. }
  1604. +(NSString*) getScanPath
  1605. {
  1606. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1607. if(appDelegate.order_code==nil)
  1608. {
  1609. appDelegate.order_code=[[NSUUID new] UUIDString];
  1610. }
  1611. return [self getScanOrderPath];
  1612. // else
  1613. // {
  1614. // [self createScanTempFolder];
  1615. // return [self getScanTempPath];
  1616. // }
  1617. }
  1618. +(void) scanCloseOrder
  1619. {
  1620. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1621. {
  1622. // 当前订单先关闭
  1623. // [OLDataProvider deleteScanOrder:appDelegate.order_code];
  1624. appDelegate.order_code = nil;
  1625. // RASingleton.sharedInstance.scan_temp_code = nil;
  1626. if(appDelegate.customer_type!=USER_ROLE_CUSTOMER ) //customer 角色不清空customer info
  1627. { appDelegate.customerInfo = nil;
  1628. appDelegate.contact_id = nil;
  1629. }
  1630. RASingleton.sharedInstance.scan_cart = nil;
  1631. RASingleton.sharedInstance.price_type = 1;
  1632. // [appDelegate updateScanButton:false];
  1633. [appDelegate update_count_mark];
  1634. [appDelegate closeOrder];
  1635. }
  1636. }
  1637. +(NSString*) createScanTempCode
  1638. {
  1639. return [[NSUUID new] UUIDString];
  1640. }
  1641. +(NSString*) scanTemplatePath:(NSString*)file
  1642. {
  1643. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1644. NSString *documents = [paths objectAtIndex:0];
  1645. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  1646. NSString* templatefile = [unZipTo stringByAppendingPathComponent:file];
  1647. return templatefile;
  1648. }
  1649. +(void) realguestLogin
  1650. {
  1651. NSMutableDictionary* guestjson=[self loadScanTemplate:@"guest.json"];
  1652. [RANetwork parse_logininfo:guestjson user:@"GUEST" password:@"GUEST"];
  1653. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1654. }
  1655. +(NSMutableDictionary*) loadScanTemplate:(NSString*)file
  1656. {
  1657. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1658. NSString *documents = [paths objectAtIndex:0];
  1659. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  1660. NSString* templatefile = [unZipTo stringByAppendingPathComponent:file];
  1661. NSDictionary* dict=[RAUtils dictfromfile:templatefile];
  1662. return [dict mutableCopy];
  1663. }
  1664. +(void) deleteSubmittedOrder:(NSString*) order_code
  1665. {
  1666. NSString *userdir = [OLDataProvider getUserPath];
  1667. if(order_code.length==0)
  1668. return;
  1669. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1670. // NSString *documents = [paths objectAtIndex:0];
  1671. // if([appDelegate.order_code isEqualToString:order_code])
  1672. // {
  1673. // //要删除的订单是当前打开的订单;
  1674. // appDelegate.order_code = nil;
  1675. // appDelegate.customerInfo = nil;
  1676. // RASingleton.sharedInstance.scan_cart = nil;
  1677. // appDelegate.contact_id = nil;
  1678. // // [appDelegate updateScanButton:false];
  1679. //
  1680. // [appDelegate update_count_mark];
  1681. // }
  1682. //删除订单目录
  1683. [[NSFileManager defaultManager] removeItemAtPath:[userdir stringByAppendingPathComponent:order_code] error:nil];
  1684. //维护订单列表
  1685. NSMutableDictionary* submitlist = [self getScanSubmitList];
  1686. int count = [submitlist[@"count"] intValue];
  1687. if(count==0)
  1688. {
  1689. // order list 为空,不用维护
  1690. #ifdef RA_NOTIFICATION
  1691. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1692. #else
  1693. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1694. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1695. #endif
  1696. return;
  1697. }
  1698. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"submitlist.json"];
  1699. //
  1700. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1701. //
  1702. //
  1703. //
  1704. // // [orderlist addObject:appDelegate.order_code];
  1705. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1706. //
  1707. // }
  1708. // else
  1709. // {
  1710. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1711. //
  1712. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1713. //
  1714. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1715. // }
  1716. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1717. int newcount = 0;
  1718. for(int i=0;i<count;i++)
  1719. {
  1720. NSMutableDictionary* item = submitlist[[NSString stringWithFormat:@"item_%d",i]];
  1721. if([item[@"order_code"] isEqualToString: order_code ])
  1722. continue;
  1723. else
  1724. {
  1725. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1726. newcount++;
  1727. }
  1728. }
  1729. newlist[@"count"] = @(newcount);
  1730. // [RAUtils dicttofile:orderlistPath dict:newlist];
  1731. [self saveScanSubmitList:newlist];
  1732. #ifdef RA_NOTIFICATION
  1733. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1734. #else
  1735. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1736. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1737. #endif
  1738. }
  1739. +(void) updateScanOrder:(NSString*) order_code
  1740. {
  1741. #ifdef SCANNER_ORDER
  1742. NSString *userdir = [OLDataProvider getUserPath];
  1743. if(order_code.length==0)
  1744. return;
  1745. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1746. // NSString *documents = [paths objectAtIndex:0];
  1747. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1748. NSMutableDictionary* orderlist = [self getScanOrderList];
  1749. int count = [orderlist[@"count"] intValue];
  1750. if(count==0)
  1751. {
  1752. // order list 为空,不用维护
  1753. [ActiveViewController Notify:@"ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1754. return;
  1755. }
  1756. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1757. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1758. //
  1759. //
  1760. //
  1761. // // [orderlist addObject:appDelegate.order_code];
  1762. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1763. //
  1764. // }
  1765. // else
  1766. // {
  1767. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1768. //
  1769. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1770. //
  1771. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1772. // }
  1773. //
  1774. //
  1775. // int count = [orderlist[@"count"] intValue];
  1776. // NSMutableDictionary* newlist = [NSMutableDictionary new];
  1777. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1778. NSDictionary* price_group = [RADataProvider get_price_group:appDelegate.price_group];
  1779. float p =0;
  1780. float s =0;
  1781. for(int j=0;j<[RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] intValue];j++)
  1782. {
  1783. NSDictionary* cartitem =RASingleton.sharedInstance.scan_cart[@"section_0"][[NSString stringWithFormat:@"item_%d",j]];
  1784. p+=[cartitem[@"unit_price"] doubleValue]*[cartitem[@"count"]intValue] * (1-[cartitem[@"discount"]doubleValue]/100.0);
  1785. int mcount = [cartitem[@"count"] intValue];
  1786. double msurcharge =0;
  1787. // remove net price
  1788. // if(RASingleton.sharedInstance.price_type==1)
  1789. // {
  1790. // if([cartitem[@"special_price"] boolValue])
  1791. // {
  1792. // msurcharge=([cartitem[price_group[@"price_3"][@"name"]] doubleValue]-[cartitem[price_group[@"price_2"][@"name"]] doubleValue])*mcount*(1-[cartitem[@"discount"]doubleValue]/100.0);
  1793. // }
  1794. // else
  1795. // {
  1796. // msurcharge=([cartitem[price_group[@"price_3"][@"name"]] doubleValue]-[cartitem[price_group[@"price_1"][@"name"]] doubleValue])*mcount*(1-[cartitem[@"discount"]doubleValue]/100.0);
  1797. // }
  1798. // }
  1799. s+=msurcharge;
  1800. }
  1801. NSMutableDictionary* section =RASingleton.sharedInstance.scan_cart[@"section_0"];
  1802. int order_qty=0;
  1803. for(int c=0;c<[section[@"count"] intValue];c++)
  1804. {
  1805. NSDictionary* item = section[[NSString stringWithFormat:@"item_%d",c]];
  1806. order_qty+=[item[@"count"] intValue];
  1807. }
  1808. // orderitem[@"model_count"] =[ NSString stringWithFormat:@"%@",order_qty];
  1809. // int newcount = 0;
  1810. for(int i=0;i<count;i++)
  1811. {
  1812. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1813. if([item[@"order_code"] isEqualToString: order_code ])
  1814. {
  1815. item[@"price"] = [RAConvertor currencyNumber:p+s];//[NSString stringWithFormat:@"%.2f",p+s];
  1816. item[@"model_count"] =[ NSString stringWithFormat:@"%d",order_qty];//RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] ];
  1817. orderlist[[NSString stringWithFormat:@"item_%d",i]] = item;
  1818. }
  1819. }
  1820. // newlist[@"count"] = @(newcount);
  1821. [self saveScanOrderList:orderlist];
  1822. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1823. [ActiveViewController Notify:@"ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1824. #endif
  1825. }
  1826. +(void) deleteScanOrder:(NSString*) order_code
  1827. {
  1828. NSString *userdir = [OLDataProvider getUserPath];
  1829. if(order_code.length==0)
  1830. return;
  1831. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1832. // NSString *documents = [paths objectAtIndex:0];
  1833. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1834. if([appDelegate.order_code isEqualToString:order_code])
  1835. {
  1836. //要删除的订单是当前打开的订单;
  1837. // appDelegate.order_code = nil;
  1838. // appDelegate.customerInfo = nil;
  1839. // RASingleton.sharedInstance.scan_cart = nil;
  1840. // appDelegate.contact_id = nil;
  1841. [self scanCloseOrder];
  1842. // [appDelegate updateScanButton:false];
  1843. [appDelegate update_count_mark];
  1844. }
  1845. //删除订单目录
  1846. [[NSFileManager defaultManager] removeItemAtPath:[userdir stringByAppendingPathComponent:order_code] error:nil];
  1847. //维护订单列表
  1848. NSMutableDictionary* orderlist = [self getScanOrderList];
  1849. int count = [orderlist[@"count"] intValue];
  1850. if(count==0)
  1851. {
  1852. // order list 为空,不用维护
  1853. #ifdef RA_NOTIFICATION
  1854. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1855. #else
  1856. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1857. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1858. #endif
  1859. return;
  1860. }
  1861. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1862. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1863. //
  1864. //
  1865. //
  1866. // // [orderlist addObject:appDelegate.order_code];
  1867. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1868. //
  1869. // }
  1870. // else
  1871. // {
  1872. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1873. //
  1874. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1875. //
  1876. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1877. // }
  1878. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1879. int newcount = 0;
  1880. for(int i=0;i<count;i++)
  1881. {
  1882. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1883. if([item[@"order_code"] isEqualToString: order_code ])
  1884. continue;
  1885. else
  1886. {
  1887. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1888. newcount++;
  1889. }
  1890. }
  1891. newlist[@"count"] = @(newcount);
  1892. [self saveScanOrderList:newlist];
  1893. // [RAUtils dicttofile:orderlistPath dict:newlist];
  1894. #ifdef RA_NOTIFICATION
  1895. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1896. #else
  1897. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1898. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1899. #endif
  1900. }
  1901. #ifdef SCANNER_ORDER
  1902. +(void) scanRefreshCart
  1903. {
  1904. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1905. NSMutableDictionary* cart =[[RAUtils copyDict: RASingleton.sharedInstance.scan_cart] mutableCopy];
  1906. NSMutableDictionary * section = cart[@"section_0"];
  1907. NSMutableDictionary* newsection = [NSMutableDictionary new];
  1908. int count = [section[@"count"] intValue];
  1909. int newcount = 0;
  1910. for(int i=0;i<count;i++)
  1911. {
  1912. NSMutableDictionary* item = [section[[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  1913. NSString* model = item[@"model"];
  1914. NSDictionary* modeljson=appDelegate.scan_model[model];
  1915. if(modeljson==nil)
  1916. continue;
  1917. NSMutableDictionary* cartitem = [[self scanmodel2cartitem:[modeljson mutableCopy]] mutableCopy];
  1918. cartitem[@"note"]=item[@"note"];
  1919. cartitem[@"discount"]=item[@"discount"];
  1920. // [item removeObjectForKey:@"discount"];
  1921. // [item removeObjectForKey:@"note"];
  1922. newsection[[NSString stringWithFormat:@"item_%d",newcount]]=cartitem;
  1923. newcount++;
  1924. }
  1925. newsection[@"count"]=@(newcount);
  1926. newsection[@"available"] = @(1);
  1927. newsection[@"title"] = @"Available List";
  1928. RASingleton.sharedInstance.scan_cart[@"section_0"]=newsection;
  1929. }
  1930. +(NSDictionary*) scanmodel2cartitem:(NSMutableDictionary*) _modelJson
  1931. {
  1932. // _modelJson = modelJson;
  1933. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1934. // self.labelPrice0.text = appDelegate.price0_name;
  1935. // self.labelPrice1.text = appDelegate.price1_name;
  1936. // self.labelPrice2.text = appDelegate.price2_name;
  1937. // self.labelPrice3.text = appDelegate.price3_name;
  1938. NSDictionary* price_group = [RADataProvider get_price_group:appDelegate.price_group];
  1939. // self.labelPrice0.text = price_group[@"price_0"][@"display"];//appDelegate.price0_name;
  1940. // self.labelPrice1.text = price_group[@"price_1"][@"display"];//appDelegate.price1_name;
  1941. // self.labelPrice2.text = price_group[@"price_2"][@"display"];//appDelegate.price2_name;
  1942. // self.labelPrice3.text = price_group[@"price_3"][@"display"];//appDelegate.price3_name;
  1943. #ifdef DEBUG
  1944. DebugLog( [RAConvertor dict2string:_modelJson]);
  1945. #endif
  1946. NSString * port=_modelJson[@"port"];
  1947. if(port.length==0)
  1948. port = @"N/A";
  1949. NSString * origin=_modelJson[@"origin"];
  1950. if(origin.length==0)
  1951. origin = @"N/A";
  1952. NSString * dimension=_modelJson[@"dimension"];
  1953. if(dimension.length==0)
  1954. dimension = @"N/A";
  1955. // _labelModel.text=_modelJson[@"model"];
  1956. // _labelDescription.text=_modelJson[@"description"];
  1957. // _labelDimension.text=dimension;
  1958. // _labelCuft.text=_modelJson[@"unit_cuft"];
  1959. // _labelOrigin.text=origin;
  1960. // _labelPort.text=port;
  1961. NSString* price0=_modelJson[price_group[@"price_0"][@"name"]]; //DDP
  1962. NSString* price1=_modelJson[price_group[@"price_1"][@"name"]]; //WHSE
  1963. NSString* price2=_modelJson[price_group[@"price_2"][@"name"]]; //SHOW
  1964. NSString* price3=_modelJson[price_group[@"price_3"][@"name"]]; //NET
  1965. if(![price0 isEqualToString: @"N/A"])
  1966. price0 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_0"][@"name"]] doubleValue]];
  1967. if(![price1 isEqualToString: @"N/A"])
  1968. price1 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_1"][@"name"]] doubleValue]];
  1969. if(![price2 isEqualToString: @"N/A"])
  1970. {
  1971. price2 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_2"][@"name"]] doubleValue]];
  1972. // if(RASingleton.sharedInstance.price_type==1)
  1973. _modelJson [@"special_price"] = @true;
  1974. }
  1975. if(![price3 isEqualToString: @"N/A"])
  1976. price3 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_3"][@"name"]] doubleValue]];
  1977. // if(![_modelJson [@"special_price"] boolValue]&& ![price1 isEqualToString: @"N/A"] && [price1 isEqual:price3])
  1978. if((![price1 isEqualToString: @"N/A"] && [price1 isEqual:price3])||(![price2 isEqualToString: @"N/A"] && [price2 isEqual:price3]))
  1979. {
  1980. // if(RASingleton.sharedInstance.price_type==1)
  1981. _modelJson [@"net_price"] = @true;
  1982. // remove net price
  1983. _modelJson [@"net_price"] = @false;
  1984. }
  1985. //
  1986. // if(![price1 isEqualToString:@"N/A"])
  1987. // {
  1988. // price2 = [NSString stringWithFormat:@"%.2f",[_modelJson[@"price1"] doubleValue] *1.25];
  1989. // _modelJson[@"price2"]= price2;
  1990. // }
  1991. // else
  1992. // {
  1993. // price2=@"N/A";
  1994. // }
  1995. //
  1996. // if([price0 isEqualToString:@"N/A"])
  1997. // _labelPriceCTNR.text= @"";//price0;
  1998. // else
  1999. // _labelPriceCTNR.text=[RAConvertor currencyNumber:[price0 floatValue]];
  2000. //
  2001. // if([price3 isEqualToString:@"N/A"])
  2002. // _labelPriceNCA.text= price3;
  2003. // else
  2004. // _labelPriceNCA.text=[RAConvertor currencyNumber:[price3 floatValue]];
  2005. // _labelPrice25p.text=price2;
  2006. //
  2007. // if([_modelJson [@"special_price"] boolValue])
  2008. // {
  2009. // if([_modelJson[price_group[@"price_2"][@"name"]] isEqualToString:@"N/A"])
  2010. // _labelPriceSpecial.text=@"";//_modelJson[@"price2"];
  2011. // else
  2012. // _labelPriceSpecial.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_2"][@"name"]] floatValue]];
  2013. // }
  2014. // else
  2015. // {
  2016. // _labelPriceSpecial.text=@"";//@"N/A";
  2017. // }
  2018. //
  2019. // if([_modelJson [@"net_price"] boolValue])
  2020. // {
  2021. //// if([_modelJson[@"price1"] isEqualToString:@"N/A"])
  2022. //// {
  2023. //// _labelPriceNet.text=_modelJson[@"price1"];
  2024. ////// _labelPriceNCA.text=@"N/A";
  2025. //// }
  2026. //// else
  2027. //// {
  2028. //// _labelPriceNet.text=[RAConvertor currencyNumber:[_modelJson[@"price1"] floatValue]];
  2029. ////// _labelPriceNCA.text=@"N/A";
  2030. //// }
  2031. //
  2032. // if([_modelJson[price_group[@"price_3"][@"name"]] isEqualToString:@"N/A"])
  2033. // {
  2034. // _labelPriceNet.text=@"";//_modelJson[@"price3"];
  2035. //// _labelPriceNCA.text=@"N/A";
  2036. // }
  2037. // else
  2038. // {
  2039. // _labelPriceNet.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_3"][@"name"]] floatValue]];
  2040. //// _labelPriceNCA.text=@"N/A";
  2041. // }
  2042. //
  2043. // }
  2044. // else
  2045. // {
  2046. // _labelPriceNet.text=@"";//@"N/A";
  2047. // }
  2048. //
  2049. // {
  2050. // if([_modelJson[price_group[@"price_1"][@"name"]] isEqualToString:@"N/A"])
  2051. // {
  2052. // _labelPriceNCA.text=@"";//_modelJson[@"price1"];
  2053. //// _labelPriceNCA.text=@"N/A";
  2054. // }
  2055. // else
  2056. // {
  2057. // _labelPriceNCA.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_1"][@"name"]] floatValue]];
  2058. //// _labelPriceNet.text=@"N/A";
  2059. // }
  2060. // }
  2061. NSString * available=_modelJson[@"available"];
  2062. if(available.length==0)
  2063. available = @"N/A";
  2064. // _labelAvailable.text=available;
  2065. NSString * unit_cuft=_modelJson[@"unit_cuft"];
  2066. if(unit_cuft.length==0)
  2067. unit_cuft = @"N/A";
  2068. else
  2069. {
  2070. unit_cuft = [NSString stringWithFormat:@"%.2f", [_modelJson[@"unit_cuft"] doubleValue]];
  2071. }
  2072. NSString * cell_price;
  2073. if(RASingleton.sharedInstance.price_type==1)
  2074. {
  2075. // if(_modelJson [@"net_price"])
  2076. cell_price=_modelJson[price_group[@"price_3"][@"name"]];
  2077. // else if(_modelJson [@"special_price"])
  2078. // {
  2079. // unit_price= _modelJson[@"price2"];
  2080. // }
  2081. // else
  2082. // {
  2083. // unit_price= _modelJson[@"price1"];
  2084. // }
  2085. }
  2086. else
  2087. {
  2088. cell_price=_modelJson[price_group[@"price_0"][@"name"]];
  2089. }
  2090. NSString * mpack=_modelJson[@"stockUom"];
  2091. if([mpack isEqualToString:@"N/A"])
  2092. mpack=@"1";
  2093. _modelJson[@"count"]=_modelJson[@"stockUom"];
  2094. _modelJson[@"cuft"] = @([mpack intValue]* [unit_cuft doubleValue]);
  2095. _modelJson[@"subtotal_price"] = @([mpack intValue]* [cell_price doubleValue]);
  2096. NSMutableDictionary* jitem = nil;
  2097. jitem = [_modelJson mutableCopy];
  2098. int stockUom =[jitem[@"stockUom"] intValue];
  2099. if(stockUom==0)
  2100. stockUom=1;
  2101. bool newitem = true;
  2102. //AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2103. //NSDictionary* price_group = [RADataProvider get_price_group:appDelegate.price_group];
  2104. // for(int i=0;i<count;i++)
  2105. // {
  2106. //
  2107. // NSMutableDictionary* litem = [section[[NSString stringWithFormat:@"item_%i",i]] mutableCopy];
  2108. //
  2109. // if([litem[@"product_id"] isEqualToString:jitem[@"product_id"]])
  2110. // {
  2111. //
  2112. //// int oldcount = [litem[@"stockUom"] intValue];
  2113. // newitem = false;
  2114. //
  2115. // litem[@"count"]=@([litem[@"count"] intValue] +[jitem[@"count"] intValue]);
  2116. // litem[@"cuft"]=@([litem[@"count"] intValue] * [litem[@"unit_cuft"] doubleValue]);
  2117. //
  2118. // section[[NSString stringWithFormat:@"item_%i",i]] = litem;
  2119. // break;
  2120. //
  2121. //
  2122. // }
  2123. //
  2124. //
  2125. //
  2126. // }
  2127. // if(true)
  2128. // {
  2129. jitem[@"count"]=@(stockUom);
  2130. jitem[@"check"]=@(true);
  2131. jitem[@"cart_item_id"]=[NSUUID UUID].UUIDString;
  2132. NSString * unit_price;
  2133. if(RASingleton.sharedInstance.price_type==0)
  2134. unit_price=_modelJson[price_group[@"price_0"][@"name"]];
  2135. else if(_modelJson [@"special_price"])
  2136. {
  2137. unit_price= _modelJson[price_group[@"price_2"][@"name"]];
  2138. }
  2139. else if(_modelJson [@"net_price"])
  2140. unit_price=_modelJson[price_group[@"price_3"][@"name"]];
  2141. else
  2142. {
  2143. unit_price= _modelJson[price_group[@"price_1"][@"name"]];
  2144. }
  2145. if([unit_price isEqualToString:@"N/A"])
  2146. unit_price = @"0";
  2147. else
  2148. {
  2149. unit_price = [NSString stringWithFormat:@"%.2f", [unit_price doubleValue]];
  2150. }
  2151. jitem[@"unit_price"] = unit_price;
  2152. jitem[@"erp_unit_price"] = unit_price;
  2153. // section[[NSString stringWithFormat:@"item_%i",count]] = jitem;
  2154. // section[@"count"]= @(count+1);
  2155. // count++;
  2156. // }
  2157. return jitem;
  2158. }
  2159. +(void) saveScanCart:(NSMutableDictionary*) cart
  2160. {
  2161. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2162. if(appDelegate.order_code.length<=0)
  2163. return;
  2164. if(cart==nil)
  2165. return;
  2166. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  2167. // NSString *documents = [paths objectAtIndex:0];
  2168. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2169. if(RASingleton.sharedInstance.price_type==0)
  2170. {
  2171. NSMutableDictionary * section = cart[@"section_0"];
  2172. int count = [section[@"count"] intValue];
  2173. for(int i=0;i<count;i++)
  2174. {
  2175. NSMutableDictionary* item = [section[[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  2176. [item removeObjectForKey:@"discount"];
  2177. [item removeObjectForKey:@"note"];
  2178. section[[NSString stringWithFormat:@"item_%d",i]]=item;
  2179. }
  2180. cart[@"section_0"]=section;
  2181. }
  2182. NSString *orderdir = [self getScanPath];
  2183. NSString *cartpath = [orderdir stringByAppendingPathComponent:@"cart.json"];
  2184. cart[@"price_group"] = appDelegate.price_group;
  2185. [RAUtils dicttofile:cartpath dict:cart];
  2186. }
  2187. #endif
  2188. +(NSDictionary *) prepareUpload:(NSArray*) arr_order params:(NSMutableDictionary*)add_params
  2189. {
  2190. assert(add_params[@"user"]!=nil);
  2191. // assert(add_params[@"contact_id"]!=nil);
  2192. // assert(add_params[@"password"]!=nil);
  2193. NSString* serial= [[NSUUID UUID] UUIDString];
  2194. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2195. NSString *cachefolder = [paths objectAtIndex:0];
  2196. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  2197. upfolder = [upfolder stringByAppendingPathComponent:serial];
  2198. NSFileManager* fileManager = [NSFileManager defaultManager];
  2199. BOOL bdir=YES;
  2200. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  2201. {
  2202. NSError *error = nil;
  2203. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  2204. if(!bsuccess)
  2205. DebugLog(@"Create UPLOAD folder failed");
  2206. }
  2207. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2208. ret[@"contact"]=[self prepareContact:serial];
  2209. ret[@"wishlist"]=[self prepareWishlist:serial];
  2210. ret[@"order"]=[self prepareOrder:serial soid:arr_order params:add_params];
  2211. ret[@"portfolio"] = [self preparePortfolio:serial params:add_params];
  2212. ret[@"view_portfolio"] = [self preparePDF:serial];
  2213. NSString* str= [RAConvertor dict2string:ret];
  2214. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  2215. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  2216. NSError *error=nil;
  2217. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  2218. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  2219. ZipArchive* zip = [[ZipArchive alloc] init];
  2220. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  2221. for(int i=0;i<arr_files.count;i++)
  2222. {
  2223. NSString* file=arr_files[i];
  2224. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  2225. }
  2226. if( ![zip CloseZipFile2] )
  2227. {
  2228. zippath = @"";
  2229. }
  2230. ret[@"file"]=zippath;
  2231. return ret;
  2232. }
  2233. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  2234. {
  2235. if(filename.length==0)
  2236. return false;
  2237. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2238. bool ret=false;
  2239. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2240. NSString *cachefolder = [paths objectAtIndex:0];
  2241. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  2242. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  2243. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  2244. upfolder = [upfolder stringByAppendingPathComponent:serial];
  2245. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  2246. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  2247. NSFileManager* fileManager = [NSFileManager defaultManager];
  2248. BOOL bdir=NO;
  2249. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  2250. {
  2251. NSError *error = nil;
  2252. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  2253. {
  2254. ret=false;
  2255. }
  2256. else
  2257. {
  2258. ret=true;
  2259. }
  2260. }
  2261. return ret;
  2262. }
  2263. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  2264. {
  2265. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2266. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  2267. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  2268. NSString* where=@" 1=1";
  2269. if (ver!=nil) {
  2270. where=@"is_dirty=1";
  2271. }
  2272. sqlite3 *db = [iSalesDB get_db];
  2273. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  2274. sqlite3_stmt * statement;
  2275. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2276. int count=0;
  2277. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2278. if ( dbresult== SQLITE_OK)
  2279. {
  2280. while (sqlite3_step(statement) == SQLITE_ROW)
  2281. {
  2282. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2283. int _id = sqlite3_column_int(statement, 0);
  2284. int product_id = sqlite3_column_int(statement, 1);
  2285. int item_id = sqlite3_column_int(statement, 2);
  2286. int qty = sqlite3_column_int(statement, 3);
  2287. char *create_time = (char*)sqlite3_column_text(statement, 4);
  2288. NSString* nscreate_time=nil;
  2289. if(create_time!=nil)
  2290. {
  2291. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  2292. }
  2293. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  2294. NSString* nsmodify_time=nil;
  2295. if(modify_time!=nil)
  2296. {
  2297. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  2298. }
  2299. int is_delete = sqlite3_column_int(statement, 6);
  2300. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2301. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  2302. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  2303. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  2304. itemjson[@"create_time"]=nscreate_time;
  2305. itemjson[@"modify_time"]=nsmodify_time;
  2306. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  2307. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2308. count++;
  2309. }
  2310. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2311. sqlite3_finalize(statement);
  2312. }
  2313. ret[@"count"]=[NSNumber numberWithInt:count ];
  2314. [iSalesDB close_db:db];
  2315. return ret;
  2316. }
  2317. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode params:(NSMutableDictionary*)add_params db:(sqlite3*)db
  2318. {
  2319. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2320. // UIApplication * app = [UIApplication sharedApplication];
  2321. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2322. assert(add_params[@"user"]!=nil);
  2323. // assert(add_params[@"password"]!=nil);
  2324. 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 ];
  2325. // 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 ];
  2326. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  2327. DebugLog(@"offline_login sql:%@",sqlQuery);
  2328. sqlite3_stmt * statement;
  2329. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2330. int count=0;
  2331. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2332. if ( dbresult== SQLITE_OK)
  2333. {
  2334. while (sqlite3_step(statement) == SQLITE_ROW)
  2335. {
  2336. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2337. char *customer_cid = (char *)sqlite3_column_text(statement, 10);
  2338. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  2339. int product_id = sqlite3_column_int(statement, 0);
  2340. char *str_price = (char*)sqlite3_column_text(statement, 1);
  2341. int item_id = sqlite3_column_int(statement, 7);
  2342. NSString* Price=nil;
  2343. if(str_price==nil)
  2344. {
  2345. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2346. NSNumber* price = [self get_model_default_price:customerID user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db];
  2347. if(price==nil)
  2348. Price=@"No Price.";
  2349. else
  2350. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2351. }
  2352. else
  2353. {
  2354. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  2355. }
  2356. double discount = sqlite3_column_double(statement, 2);
  2357. int item_count = sqlite3_column_int(statement, 3);
  2358. char *line_note = (char*)sqlite3_column_text(statement, 4);
  2359. NSString *nsline_note=nil;
  2360. if(line_note!=nil)
  2361. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  2362. char *name = (char*)sqlite3_column_text(statement, 5);
  2363. NSString *nsname=nil;
  2364. if(name!=nil)
  2365. nsname= [[NSString alloc]initWithUTF8String:name];
  2366. char *description = (char*)sqlite3_column_text(statement, 6);
  2367. NSString *nsdescription=nil;
  2368. if(description!=nil)
  2369. nsdescription= [[NSString alloc]initWithUTF8String:description];
  2370. // int stockUom = sqlite3_column_int(statement, 8);
  2371. // int _id = sqlite3_column_int(statement, 9);
  2372. //
  2373. //
  2374. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  2375. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  2376. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  2377. // int carton=[bsubtotaljson[@"carton"] intValue];
  2378. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2379. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  2380. itemjson[@"model"]=nsname;
  2381. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  2382. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  2383. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  2384. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  2385. // itemjson[@"check"]=@"true";
  2386. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  2387. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  2388. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  2389. itemjson[@"unit_price"]=Price;
  2390. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  2391. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  2392. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  2393. itemjson[@"note"]=nsline_note;
  2394. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:customerID user:add_params[@"user"]];
  2395. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2396. count++;
  2397. }
  2398. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2399. sqlite3_finalize(statement);
  2400. }
  2401. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2402. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  2403. return ret;
  2404. }
  2405. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid params:(NSMutableDictionary*)add_params
  2406. {
  2407. assert(add_params[@"user"]!=nil);
  2408. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2409. sqlite3 *db = [iSalesDB get_db];
  2410. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id,discount from offline_order where order_id is null";
  2411. // for(int i=0;i<arr_soid.count;i++)
  2412. sqlite3_stmt * statement;
  2413. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2414. //int count=0;
  2415. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2416. if ( dbresult== SQLITE_OK)
  2417. {
  2418. int count=0;
  2419. while (sqlite3_step(statement) == SQLITE_ROW)
  2420. {
  2421. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2422. int _id = sqlite3_column_int(statement, 0);
  2423. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  2424. NSString* nssync_data=nil;
  2425. if(sync_data!=nil)
  2426. {
  2427. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  2428. // NSMutableDictionary* sync_dic = [RAConvertor string2dict:sync_data];
  2429. // sync_dic[@"truck_carrier"] = sync_dic[@"carrier"]
  2430. }
  2431. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  2432. NSString* nsimg_1=nil;
  2433. if(img_1!=nil)
  2434. {
  2435. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2436. [self copy_upImg:serial file:nsimg_1];
  2437. }
  2438. char *so_no = (char*)sqlite3_column_text(statement, 3);
  2439. NSString* nsso_no=nil;
  2440. if(so_no!=nil)
  2441. {
  2442. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  2443. }
  2444. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  2445. itemjson[@"order_type"]=@"submit order";
  2446. else
  2447. itemjson[@"order_type"]=@"archive order";
  2448. double discount =sqlite3_column_double(statement, 4);
  2449. itemjson[@"discount"]=@(discount);
  2450. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2451. itemjson[@"json_data"]= nssync_data;
  2452. itemjson[@"order_items"]=[self preparecart:nsso_no params:add_params db:db];
  2453. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2454. count++;
  2455. }
  2456. ret[@"count"]=[NSNumber numberWithInt:count ];
  2457. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2458. }
  2459. sqlite3_finalize(statement);
  2460. [iSalesDB close_db:db];
  2461. return ret;
  2462. }
  2463. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  2464. {
  2465. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2466. sqlite3 *db = [iSalesDB get_db];
  2467. // UIApplication * app = [UIApplication sharedApplication];
  2468. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2469. 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";
  2470. sqlite3_stmt * statement;
  2471. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2472. int count=0;
  2473. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2474. if ( dbresult== SQLITE_OK)
  2475. {
  2476. while (sqlite3_step(statement) == SQLITE_ROW)
  2477. {
  2478. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2479. int _id = sqlite3_column_int(statement, 0);
  2480. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  2481. NSString* nsimg_0=nil;
  2482. if(img_0!=nil)
  2483. {
  2484. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  2485. [self copy_upImg:serial file:nsimg_0];
  2486. }
  2487. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  2488. NSString* nsimg_1=nil;
  2489. if(img_1!=nil)
  2490. {
  2491. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2492. [self copy_upImg:serial file:nsimg_1];
  2493. }
  2494. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  2495. NSString* nsimg_2=nil;
  2496. if(img_2!=nil)
  2497. {
  2498. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  2499. [self copy_upImg:serial file:nsimg_2];
  2500. }
  2501. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  2502. NSString* nssync_data=nil;
  2503. if(sync_data!=nil)
  2504. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  2505. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2506. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  2507. itemjson[@"json_data"]= nssync_data;
  2508. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2509. count++;
  2510. }
  2511. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2512. sqlite3_finalize(statement);
  2513. }
  2514. ret[@"count"]=[NSNumber numberWithInt:count ];
  2515. [iSalesDB close_db:db];
  2516. return ret;
  2517. }
  2518. +(bool) check_offlinedata
  2519. {
  2520. // UIApplication * app = [UIApplication sharedApplication];
  2521. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2522. sqlite3 *db = [iSalesDB get_db];
  2523. NSString * where=@"1=1";
  2524. // if(appDelegate.user!=nil)
  2525. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  2526. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  2527. [iSalesDB close_db:db];
  2528. if(count==0)
  2529. {
  2530. return false;
  2531. }
  2532. return true;
  2533. //
  2534. //[iSalesDB close_db:db];
  2535. }
  2536. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  2537. {
  2538. // UIApplication * app = [UIApplication sharedApplication];
  2539. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2540. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2541. sqlite3 *db = [iSalesDB get_db];
  2542. NSString* collectId=params[@"collectId"];
  2543. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  2544. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  2545. [iSalesDB execSql:sqlQuery db:db];
  2546. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  2547. [iSalesDB close_db:db];
  2548. // appDelegate.wish_count =count;
  2549. //
  2550. // [appDelegate update_count_mark];
  2551. ret[@"result"]= [NSNumber numberWithInt:2];
  2552. ret[@"wish_count"]=@(count);
  2553. return ret;
  2554. }
  2555. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  2556. //{
  2557. //
  2558. // UIApplication * app = [UIApplication sharedApplication];
  2559. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2560. //
  2561. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2562. // sqlite3 *db = [iSalesDB get_db];
  2563. // NSString* product_id=params[@"product_id"];
  2564. //
  2565. //
  2566. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  2567. //
  2568. //
  2569. // for(int i=0;i<arr.count;i++)
  2570. // {
  2571. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  2572. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  2573. // if(count==0)
  2574. // {
  2575. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  2576. // [iSalesDB execSql:sqlQuery db:db];
  2577. // }
  2578. // }
  2579. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  2580. // [iSalesDB close_db:db];
  2581. //
  2582. // appDelegate.wish_count =count;
  2583. //
  2584. // [appDelegate update_count_mark];
  2585. // ret[@"result"]= [NSNumber numberWithInt:2];
  2586. // return ret;
  2587. // //
  2588. // //return ret;
  2589. //}
  2590. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  2591. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  2592. DebugLog(@"time interval: %lf",interval);
  2593. }
  2594. //+(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  2595. //{
  2596. // UIApplication * app = [UIApplication sharedApplication];
  2597. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2598. //
  2599. // int sort = [[params objectForKey:@"sort"] intValue];
  2600. // NSString *sort_str = @"";
  2601. // switch (sort) {
  2602. // case 0:{
  2603. // sort_str = @"order by w.modify_time desc";
  2604. // }
  2605. // break;
  2606. // case 1:{
  2607. // sort_str = @"order by w.modify_time asc";
  2608. // }
  2609. // break;
  2610. // case 2:{
  2611. // sort_str = @"order by m.name asc";
  2612. // }
  2613. // break;
  2614. // case 3:{
  2615. // sort_str = @"order by m.name desc";
  2616. // }
  2617. // break;
  2618. // case 4:{
  2619. // sort_str = @"order by m.description asc";
  2620. // }
  2621. // break;
  2622. //
  2623. // default:
  2624. // break;
  2625. //
  2626. // }
  2627. //
  2628. //
  2629. //// NSString* user = appDelegate.user;
  2630. //
  2631. // sqlite3 *db = [iSalesDB get_db];
  2632. //
  2633. // // order by w.create_time
  2634. // 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];
  2635. //
  2636. //// 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];
  2637. //
  2638. //
  2639. // sqlite3_stmt * statement;
  2640. //
  2641. // NSDate *date1 = [NSDate date];
  2642. //// NSDate *date2 = nil;
  2643. //
  2644. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2645. // int count=0;
  2646. //
  2647. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2648. // {
  2649. //
  2650. //
  2651. // while (sqlite3_step(statement) == SQLITE_ROW)
  2652. // {
  2653. //
  2654. // NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  2655. //
  2656. // int product_id = sqlite3_column_double(statement, 0);
  2657. //
  2658. //
  2659. //
  2660. //
  2661. //
  2662. // char *description = (char*)sqlite3_column_text(statement, 1);
  2663. // if(description==nil)
  2664. // description= "";
  2665. // NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  2666. //
  2667. // int item_id = sqlite3_column_double(statement, 2);
  2668. //
  2669. // NSDate *date_image = [NSDate date];
  2670. //
  2671. // NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  2672. //
  2673. // printf("image : ");
  2674. // [self printTimeIntervalBetween:date_image and:[NSDate date]];
  2675. //
  2676. // // char *url = (char*)sqlite3_column_text(statement, 3);
  2677. // // if(url==nil)
  2678. // // url="";
  2679. // // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2680. //
  2681. // int qty = sqlite3_column_int(statement, 3);
  2682. //
  2683. //
  2684. // item[@"cart_count"] = [NSNumber numberWithInt:qty];
  2685. // item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  2686. // item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  2687. // item[@"description"]= nsdescription;
  2688. // item[@"img"]= nsurl;
  2689. //
  2690. //
  2691. //
  2692. // ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2693. // count++;
  2694. //
  2695. // }
  2696. // printf("total time:");
  2697. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  2698. //
  2699. // ret[@"count"]= [NSNumber numberWithInt:count];
  2700. // ret[@"total_count"]= [NSNumber numberWithInt:count];
  2701. // // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  2702. // ret[@"result"]= [NSNumber numberWithInt:2];
  2703. //
  2704. //
  2705. // appDelegate.wish_count =count;
  2706. //
  2707. // [appDelegate update_count_mark];
  2708. // sqlite3_finalize(statement);
  2709. //
  2710. //
  2711. //
  2712. //
  2713. // }
  2714. //
  2715. // [iSalesDB close_db:db];
  2716. //
  2717. // return ret;
  2718. //}
  2719. +(NSDictionary*) offline_notimpl
  2720. {
  2721. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2722. ret[@"result"]=@"8";
  2723. ret[@"err_msg"]=@"offline mode does not support this function.";
  2724. return ret;
  2725. }
  2726. +(NSDictionary*) offline_home
  2727. {
  2728. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2729. NSString *cachefolder = [paths objectAtIndex:0];
  2730. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  2731. NSData* json =nil;
  2732. json=[NSData dataWithContentsOfFile:img_cache];
  2733. if(json==nil)
  2734. return nil;
  2735. NSError *error=nil;
  2736. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  2737. return menu;
  2738. }
  2739. +(NSDictionary*) offline_category_menu
  2740. {
  2741. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2742. NSString *cachefolder = [paths objectAtIndex:0];
  2743. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  2744. NSData* json =nil;
  2745. json=[NSData dataWithContentsOfFile:img_cache];
  2746. if(json==nil)
  2747. return nil;
  2748. NSError *error=nil;
  2749. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  2750. return menu;
  2751. }
  2752. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  2753. {
  2754. NSString* offline_command=params[@"offline_Command"];
  2755. NSDictionary* ret=nil;
  2756. if([offline_command isEqualToString:@"model_NIYMAL"])
  2757. {
  2758. NSString* category = params[@"category"];
  2759. ret = [self refresh_model_NIYMAL:category];
  2760. }
  2761. return ret;
  2762. }
  2763. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  2764. {
  2765. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2766. [ret setValue:@"2" forKey:@"result"];
  2767. [ret setValue:@"Regular Mode" forKey:@"mode"];
  2768. sqlite3* db= [iSalesDB get_db];
  2769. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  2770. [iSalesDB close_db:db];
  2771. [ret setObject:detail1_section forKey:@"detail_1"];
  2772. return ret;
  2773. }
  2774. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id user:(NSString*)user islogin:(bool)blogin db:(sqlite3*)db
  2775. {
  2776. //assert(user!=nil);
  2777. // UIApplication * app = [UIApplication sharedApplication];
  2778. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2779. // NSArray* arr1 = [self get_user_all_price_type];
  2780. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  2781. // NSSet *set1 = [NSSet setWithArray:arr1];
  2782. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2783. // if(appDelegate.contact_id==nil)
  2784. // set2=[set1 mutableCopy];
  2785. // else
  2786. // [set2 intersectsSet:set1];
  2787. // NSArray *retarr = [set2 allObjects];
  2788. NSString* whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2789. // sqlite3 *db = [iSalesDB get_db];
  2790. NSString* sqlQuery = nil;
  2791. if(contact_id==nil)
  2792. {
  2793. if(!blogin)
  2794. return nil;
  2795. 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];
  2796. }
  2797. else
  2798. 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];
  2799. sqlite3_stmt * statement;
  2800. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2801. int count=0;
  2802. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2803. {
  2804. while (sqlite3_step(statement) == SQLITE_ROW)
  2805. {
  2806. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  2807. char *name = (char*)sqlite3_column_text(statement, 0);
  2808. if(name==nil)
  2809. name="";
  2810. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2811. // double price = sqlite3_column_double(statement, 1);
  2812. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  2813. // if(isnull==nil)
  2814. // item[nsname]= @"No Price";
  2815. // else
  2816. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  2817. char *price = (char*)sqlite3_column_text(statement, 1);
  2818. if(price!=nil)
  2819. {
  2820. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2821. nsprice=[AESCrypt fastdecrypt:nsprice];
  2822. if(nsprice.length>0)
  2823. {
  2824. double dp= [nsprice doubleValue];
  2825. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  2826. }
  2827. }
  2828. else
  2829. {
  2830. item[nsname]= @"No Price";
  2831. }
  2832. // int type= sqlite3_column_int(statement, 2);
  2833. //item[@"type"]=@"price";
  2834. // item[nsname]= nsprice;
  2835. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2836. count++;
  2837. }
  2838. ret[@"count"]= [NSNumber numberWithInt:count];
  2839. sqlite3_finalize(statement);
  2840. }
  2841. // [iSalesDB close_db:db];
  2842. return ret;
  2843. }
  2844. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  2845. //{
  2846. // NSArray* arr1 = [self get_user_all_price_type:db];
  2847. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2848. //
  2849. // // NSSet *set1 = [NSSet setWithArray:arr1];
  2850. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2851. // // [set2 intersectsSet:set1];
  2852. // //
  2853. // //
  2854. // // NSArray *retarr = [set2 allObjects];
  2855. //
  2856. // NSString* whereprice=nil;
  2857. // if(contact_id==nil)
  2858. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2859. // else
  2860. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2861. //
  2862. //
  2863. // // sqlite3 *db = [iSalesDB get_db];
  2864. //
  2865. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  2866. // sqlite3_stmt * statement;
  2867. //
  2868. //
  2869. // NSNumber* ret = nil;
  2870. // double dprice=DBL_MAX;
  2871. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2872. // {
  2873. //
  2874. //
  2875. // while (sqlite3_step(statement) == SQLITE_ROW)
  2876. // {
  2877. //
  2878. // // double val = sqlite3_column_double(statement, 0);
  2879. // char *price = (char*)sqlite3_column_text(statement, 0);
  2880. // if(price!=nil)
  2881. // {
  2882. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2883. // nsprice=[AESCrypt fastdecrypt:nsprice];
  2884. // if(nsprice.length>0)
  2885. // {
  2886. // double dp= [nsprice doubleValue];
  2887. // if(dp<dprice)
  2888. // dprice=dp;
  2889. // }
  2890. // }
  2891. // }
  2892. //
  2893. //
  2894. //
  2895. //
  2896. // sqlite3_finalize(statement);
  2897. //
  2898. //
  2899. //
  2900. //
  2901. // }
  2902. //
  2903. // // [iSalesDB close_db:db];
  2904. //
  2905. // if(dprice==DBL_MAX)
  2906. // ret= nil;
  2907. // else
  2908. // ret= [NSNumber numberWithDouble:dprice];
  2909. // return ret;
  2910. //}
  2911. +(NSNumber*) get_model_default_price:(NSString*) contact_id user:(NSString*)user product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  2912. {
  2913. assert(user!=nil);
  2914. NSArray* arr1 = [self get_user_all_price_type:db user:user];
  2915. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  2916. NSString* whereprice=nil;
  2917. if(contact_id==nil)
  2918. whereprice=[RAConvertor arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2919. else
  2920. whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2921. // sqlite3 *db = [iSalesDB get_db];
  2922. NSString *productIdCondition = @"1 = 1";
  2923. if (product_id) {
  2924. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  2925. }
  2926. NSString *itemIdCondition = @"";
  2927. if (item_id) {
  2928. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  2929. }
  2930. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  2931. sqlite3_stmt * statement;
  2932. NSNumber* ret = nil;
  2933. double dprice=DBL_MAX;
  2934. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2935. {
  2936. while (sqlite3_step(statement) == SQLITE_ROW)
  2937. {
  2938. // double val = sqlite3_column_double(statement, 0);
  2939. char *price = (char*)sqlite3_column_text(statement, 0);
  2940. if(price!=nil)
  2941. {
  2942. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2943. nsprice=[AESCrypt fastdecrypt:nsprice];
  2944. if(nsprice.length>0)
  2945. {
  2946. double dp= [nsprice doubleValue];
  2947. if(dp<dprice)
  2948. dprice=dp;
  2949. }
  2950. }
  2951. }
  2952. sqlite3_finalize(statement);
  2953. }
  2954. // [iSalesDB close_db:db];
  2955. if(dprice==DBL_MAX)
  2956. ret= nil;
  2957. else
  2958. ret= [NSNumber numberWithDouble:dprice];
  2959. return ret;
  2960. }
  2961. +(NSArray*) get_user_all_price_type:(sqlite3*)db user:(NSString*) user
  2962. {
  2963. assert(user!=nil);
  2964. NSArray* ret=nil;
  2965. // sqlite3 *db = [iSalesDB get_db];
  2966. // no customer assigned , use login user contact_id
  2967. // UIApplication * app = [UIApplication sharedApplication];
  2968. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2969. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",user];
  2970. sqlite3_stmt * statement;
  2971. // int count=0;
  2972. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2973. {
  2974. if (sqlite3_step(statement) == SQLITE_ROW)
  2975. {
  2976. char *val = (char*)sqlite3_column_text(statement, 0);
  2977. if(val==nil)
  2978. val="";
  2979. NSString* price = [[NSString alloc]initWithUTF8String:val];
  2980. ret=[RAConvertor string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  2981. }
  2982. sqlite3_finalize(statement);
  2983. }
  2984. // [iSalesDB close_db:db];
  2985. return ret;
  2986. }
  2987. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id user:(NSString*)user db:(sqlite3*)db
  2988. {
  2989. // sqlite3 *db = [iSalesDB get_db];
  2990. if(contact_id==nil)
  2991. {
  2992. // no customer assigned , use login user contact_id
  2993. // UIApplication * app = [UIApplication sharedApplication];
  2994. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2995. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",user];
  2996. sqlite3_stmt * statement;
  2997. // int count=0;
  2998. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2999. {
  3000. if (sqlite3_step(statement) == SQLITE_ROW)
  3001. {
  3002. char *val = (char*)sqlite3_column_text(statement, 0);
  3003. if(val==nil)
  3004. val="";
  3005. contact_id = [[NSString alloc]initWithUTF8String:val];
  3006. }
  3007. sqlite3_finalize(statement);
  3008. }
  3009. if(contact_id.length<=0)
  3010. {
  3011. // [iSalesDB close_db:db];
  3012. return nil;
  3013. }
  3014. }
  3015. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  3016. sqlite3_stmt * statement;
  3017. NSArray* ret=nil;
  3018. // int count=0;
  3019. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3020. {
  3021. if (sqlite3_step(statement) == SQLITE_ROW)
  3022. {
  3023. char *price_type = (char*)sqlite3_column_text(statement, 0);
  3024. if(price_type==nil)
  3025. price_type="";
  3026. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  3027. if(nsprice_type.length>0)
  3028. ret=[RAConvertor string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  3029. else
  3030. ret=nil;
  3031. }
  3032. sqlite3_finalize(statement);
  3033. }
  3034. // [iSalesDB close_db:db];
  3035. return ret;
  3036. }
  3037. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  3038. {
  3039. NSString* ret= nil;
  3040. // sqlite3 *db = [iSalesDB get_db];
  3041. NSString *sqlQuery = nil;
  3042. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  3043. // 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;
  3044. // 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
  3045. if(product_id==nil && model_name)
  3046. 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;
  3047. else if (product_id)
  3048. 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
  3049. sqlite3_stmt * statement;
  3050. // int count=0;
  3051. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3052. {
  3053. if (sqlite3_step(statement) == SQLITE_ROW)
  3054. {
  3055. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  3056. if(imgurl==nil)
  3057. imgurl="";
  3058. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  3059. ret=nsimgurl;
  3060. }
  3061. sqlite3_finalize(statement);
  3062. }
  3063. else
  3064. {
  3065. [ret setValue:@"8" forKey:@"result"];
  3066. }
  3067. // [iSalesDB close_db:db];
  3068. DebugLog(@"data string: %@",ret );
  3069. return ret;
  3070. }
  3071. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name upc_code:(NSString *) upc_code
  3072. {
  3073. NSString* ret= nil;
  3074. sqlite3 *db = [iSalesDB get_db];
  3075. NSString *sqlQuery = nil;
  3076. if(product_id==nil)
  3077. if(upc_code==nil)
  3078. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  3079. else
  3080. 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='%@';
  3081. else
  3082. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  3083. sqlite3_stmt * statement;
  3084. // int count=0;
  3085. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3086. {
  3087. if (sqlite3_step(statement) == SQLITE_ROW)
  3088. {
  3089. char *default_category = (char*)sqlite3_column_text(statement, 0);
  3090. if(default_category==nil)
  3091. default_category="";
  3092. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  3093. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  3094. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  3095. ret=nsdefault_category;
  3096. }
  3097. sqlite3_finalize(statement);
  3098. }
  3099. else
  3100. {
  3101. [ret setValue:@"8" forKey:@"result"];
  3102. }
  3103. [iSalesDB close_db:db];
  3104. DebugLog(@"data string: %@",ret );
  3105. return ret;
  3106. }
  3107. + (NSString *) categoryIdOfProduct:(NSString *)product_id name:(NSString *)product_name {
  3108. NSString *sql = nil;
  3109. if (product_id != nil) {
  3110. sql = [NSString stringWithFormat:@"select category from product where product_id = '%@';",product_id];
  3111. } else {
  3112. sql = [NSString stringWithFormat:@"select category from product where name = '%@';",product_name];
  3113. }
  3114. NSString *result_set = [iSalesDB jk_queryText:sql];
  3115. result_set = [result_set stringByReplacingOccurrencesOfString:@"%" withString:@""];
  3116. result_set = [result_set stringByReplacingOccurrencesOfString:@"#" withString:@""];
  3117. return result_set;
  3118. }
  3119. //+(NSDictionary*) offline_model :(NSMutableDictionary *) params
  3120. //{
  3121. //
  3122. //
  3123. // assert(params[@"user"]!=nil);
  3124. //
  3125. //
  3126. //
  3127. // NSString* model_name = [params valueForKey:@"product_name"];
  3128. //
  3129. // NSString* product_id = [params valueForKey:@"product_id"];
  3130. //
  3131. // NSString* category = [params valueForKey:@"category"];
  3132. //
  3133. // NSString *default_category_id = [self model_default_category:product_id model_name:model_name];
  3134. // if(category==nil) {
  3135. // category = default_category_id;
  3136. // } else {
  3137. //
  3138. // NSArray *arr_0 = [category componentsSeparatedByString:@","];
  3139. // // 参数重有多个category id
  3140. // if (arr_0.count > 1) {
  3141. // NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  3142. //
  3143. // // 取交集
  3144. // NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  3145. // NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  3146. // [set_0 intersectSet:set_1];
  3147. //
  3148. // if (set_0.count == 1) {
  3149. // category = (NSString *)[set_0 anyObject];
  3150. // } else {
  3151. // if ([set_0 containsObject:default_category_id]) {
  3152. // category = default_category_id;
  3153. // } else {
  3154. // category = (NSString *)[set_0 anyObject];
  3155. // }
  3156. // }
  3157. // }
  3158. // }
  3159. //
  3160. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3161. //
  3162. //
  3163. //
  3164. //
  3165. //
  3166. //
  3167. // sqlite3 *db = [iSalesDB get_db];
  3168. //
  3169. //// int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  3170. //
  3171. //
  3172. // NSString *sqlQuery = nil;
  3173. //
  3174. // if(product_id==nil)
  3175. // 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='%@';
  3176. // else
  3177. //
  3178. // 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=%@;
  3179. //
  3180. //
  3181. // sqlite3_stmt * statement;
  3182. // [ret setValue:@"2" forKey:@"result"];
  3183. // [ret setValue:@"3" forKey:@"detail_section_count"];
  3184. //
  3185. // // int count=0;
  3186. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3187. // {
  3188. //
  3189. //
  3190. // if (sqlite3_step(statement) == SQLITE_ROW)
  3191. // {
  3192. // // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3193. //
  3194. //
  3195. // char *name = (char*)sqlite3_column_text(statement, 0);
  3196. // if(name==nil)
  3197. // name="";
  3198. // NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3199. //
  3200. // char *description = (char*)sqlite3_column_text(statement, 1);
  3201. // if(description==nil)
  3202. // description="";
  3203. // NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3204. //
  3205. //
  3206. // int product_id = sqlite3_column_int(statement, 2);
  3207. //
  3208. //
  3209. // char *color = (char*)sqlite3_column_text(statement, 3);
  3210. // if(color==nil)
  3211. // color="";
  3212. // // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  3213. // //
  3214. // // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  3215. // // if(legcolor==nil)
  3216. // // legcolor="";
  3217. // // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  3218. // //
  3219. // //
  3220. // int availability = sqlite3_column_int(statement, 5);
  3221. // //
  3222. // int incoming_stock = sqlite3_column_int(statement, 6);
  3223. //
  3224. //
  3225. // char *demension = (char*)sqlite3_column_text(statement, 7);
  3226. // if(demension==nil)
  3227. // demension="";
  3228. // NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  3229. //
  3230. //
  3231. //
  3232. // // ,,,,,,,,,
  3233. //
  3234. //
  3235. // char *seat_height = (char*)sqlite3_column_text(statement, 8);
  3236. // if(seat_height==nil)
  3237. // seat_height="";
  3238. // NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  3239. //
  3240. //
  3241. // char *material = (char*)sqlite3_column_text(statement, 9);
  3242. // if(material==nil)
  3243. // material="";
  3244. // NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  3245. //
  3246. // char *box_dim = (char*)sqlite3_column_text(statement, 10);
  3247. // if(box_dim==nil)
  3248. // box_dim="";
  3249. // NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  3250. //
  3251. // char *volume = (char*)sqlite3_column_text(statement, 11);
  3252. // if(volume==nil)
  3253. // volume="";
  3254. // NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  3255. //
  3256. // double weight = sqlite3_column_double(statement, 12);
  3257. //
  3258. // char *model_set = (char*)sqlite3_column_text(statement, 13);
  3259. // if(model_set==nil)
  3260. // model_set="";
  3261. // NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  3262. //
  3263. // char *load_ability = (char*)sqlite3_column_text(statement, 14);
  3264. // if(load_ability==nil)
  3265. // load_ability="";
  3266. // NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  3267. //
  3268. //// char *default_category = (char*)sqlite3_column_text(statement, 15);
  3269. //// if(default_category==nil)
  3270. //// default_category="";
  3271. //// NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  3272. //
  3273. //
  3274. // char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  3275. // if(fabric_content==nil)
  3276. // fabric_content="";
  3277. // NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  3278. //
  3279. // char *assembling = (char*)sqlite3_column_text(statement, 17);
  3280. // if(assembling==nil)
  3281. // assembling="";
  3282. // NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  3283. //
  3284. // char *made_in = (char*)sqlite3_column_text(statement, 18);
  3285. // if(made_in==nil)
  3286. // made_in="";
  3287. // NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  3288. //
  3289. //
  3290. // char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  3291. // if(special_remarks==nil)
  3292. // special_remarks="";
  3293. // NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  3294. //
  3295. //
  3296. // int stockUcom = sqlite3_column_double(statement, 20);
  3297. //
  3298. // char *product_group = (char*)sqlite3_column_text(statement, 21);
  3299. // if(product_group==nil)
  3300. // product_group="";
  3301. // NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  3302. //
  3303. // // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  3304. // // if(fashion_selector==nil)
  3305. // // fashion_selector="";
  3306. // // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  3307. //
  3308. // char *selector_field = (char*)sqlite3_column_text(statement, 22);
  3309. // if(selector_field==nil)
  3310. // selector_field="";
  3311. // NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  3312. //
  3313. // char *property_field = (char*)sqlite3_column_text(statement, 23);
  3314. // if(property_field==nil)
  3315. // property_field="";
  3316. // NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  3317. //
  3318. //
  3319. //
  3320. // char *packaging = (char*)sqlite3_column_text(statement, 24);
  3321. // if(packaging==nil)
  3322. // packaging="";
  3323. // NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  3324. //
  3325. // [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3326. //
  3327. // NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  3328. // [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  3329. // NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  3330. //
  3331. // [img_section setValue:model_s_img forKey:@"model_s_img"];
  3332. // [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  3333. // [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  3334. // [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  3335. //
  3336. //
  3337. //
  3338. //
  3339. //
  3340. // NSString* Availability=nil;
  3341. // if(availability>0)
  3342. // Availability=[NSString stringWithFormat:@"%d",availability];
  3343. // else
  3344. // Availability = @"Out of Stock";
  3345. //
  3346. // [img_section setValue:Availability forKey:@"Availability"];
  3347. // [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  3348. //
  3349. // AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  3350. //
  3351. //
  3352. // char *eta = (char*)sqlite3_column_text(statement, 25);
  3353. // if(eta==nil)
  3354. // eta="";
  3355. // NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  3356. // if ([nseta isEqualToString:@"null"]) {
  3357. // nseta = @"";
  3358. // }
  3359. // if (availability <= 0) {
  3360. // [img_section setValue:nseta forKey:@"ETA"];
  3361. // }
  3362. //
  3363. //
  3364. // int item_id = sqlite3_column_int(statement, 26);
  3365. // NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  3366. // NSString *content_writing = [self textAtColumn:28 statement:statement];
  3367. //
  3368. // NSString* Price=nil;
  3369. // if(appDelegate.bLogin==false)
  3370. // Price=@"Must Sign in.";
  3371. // else
  3372. // {
  3373. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  3374. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  3375. // if(price==nil)
  3376. // Price=@"No Price.";
  3377. // else
  3378. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  3379. // }
  3380. //
  3381. // [img_section setObject:content_writing forKey:@"product_content_writing"];
  3382. // [img_section setValue:Price forKey:@"price"];
  3383. // [img_section setValue:nsname forKey:@"model_name"];
  3384. // [img_section setValue:nsdescription forKey:@"model_descrition"];
  3385. //
  3386. // if (appDelegate.order_code) { // 离线order code即so#
  3387. //
  3388. // 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];
  3389. // __block int cartQTY = 0;
  3390. // [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3391. //
  3392. // cartQTY = sqlite3_column_int(stmt, 0);
  3393. //
  3394. // }];
  3395. //
  3396. // if (cartQTY > 0) {
  3397. // [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  3398. // }
  3399. // }
  3400. //
  3401. // [ret setObject:img_section forKey:@"img_section"];
  3402. //
  3403. // NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  3404. // int detail0_item_count=0;
  3405. //
  3406. // [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3407. // [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3408. // [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3409. // [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3410. // [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3411. // [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3412. // [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3413. // [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3414. // [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3415. // [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3416. // [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3417. // [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3418. // [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3419. // [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3420. //
  3421. //
  3422. //// NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  3423. // NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  3424. // for(int l=0;l<[pricejson[@"count"] intValue];l++)
  3425. // {
  3426. // NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  3427. // [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  3428. // }
  3429. //
  3430. //
  3431. //
  3432. //
  3433. // [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  3434. // [detail0_section setValue:@"kv" forKey:@"type"];
  3435. // [detail0_section setValue:@"Product Information" forKey:@"title"];
  3436. //
  3437. // [ret setObject:detail0_section forKey:@"detail_0"];
  3438. //
  3439. //
  3440. // NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  3441. // // [detail1_section setValue:@"detail" forKey:@"target"];
  3442. // // [detail1_section setValue:@"popup" forKey:@"action"];
  3443. // // [detail1_section setValue:@"content" forKey:@"type"];
  3444. // // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  3445. // // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  3446. // // [detail1_section setValue:@"true" forKey:@"single_row"];
  3447. // // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  3448. // [ret setObject:detail1_section forKey:@"detail_1"];
  3449. //
  3450. //
  3451. //
  3452. //
  3453. // NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  3454. // [detail2_section setValue:@"detail" forKey:@"target"];
  3455. // [detail2_section setValue:@"popup" forKey:@"action"];
  3456. // [detail2_section setValue:@"content" forKey:@"type"];
  3457. // [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  3458. // [detail2_section setValue:@"true" forKey:@"single_row"];
  3459. // [detail2_section setValue:@"local" forKey:@"data"];
  3460. // [ret setObject:detail2_section forKey:@"detail_2"];
  3461. // }
  3462. //
  3463. //
  3464. //
  3465. //
  3466. // sqlite3_finalize(statement);
  3467. // }
  3468. // else
  3469. // {
  3470. // [ret setValue:@"8" forKey:@"result"];
  3471. // }
  3472. //// DebugLog(@"count:%d",count);
  3473. //
  3474. //
  3475. // [iSalesDB close_db:db];
  3476. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3477. //
  3478. // return ret;
  3479. //}
  3480. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  3481. // NSString* orderCode = [params valueForKey:@"orderCode"];
  3482. NSString* keyword = [params valueForKey:@"keyword"];
  3483. keyword=keyword.lowercaseString;
  3484. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  3485. int limit = [[params valueForKey:@"limit"] intValue];
  3486. int offset = [[params valueForKey:@"offset"] intValue];
  3487. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3488. NSString *limit_str = @"";
  3489. if (limited) {
  3490. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  3491. }
  3492. sqlite3 *db = [iSalesDB get_db];
  3493. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  3494. // UIApplication * app = [UIApplication sharedApplication];
  3495. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3496. NSString *sqlQuery = nil;
  3497. if(exactMatch )
  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'%@%%' 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 ;
  3499. else
  3500. 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
  3501. DebugLog(@"offline_search sql:%@",sqlQuery);
  3502. sqlite3_stmt * statement;
  3503. [ret setValue:@"2" forKey:@"result"];
  3504. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  3505. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  3506. // int count=0;
  3507. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3508. {
  3509. int i=0;
  3510. while (sqlite3_step(statement) == SQLITE_ROW)
  3511. {
  3512. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3513. // char *name = (char*)sqlite3_column_text(statement, 1);
  3514. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3515. char *name = (char*)sqlite3_column_text(statement, 0);
  3516. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3517. char *description = (char*)sqlite3_column_text(statement, 1);
  3518. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3519. int product_id = sqlite3_column_int(statement, 2);
  3520. // char *url = (char*)sqlite3_column_text(statement, 3);
  3521. // if(url==nil)
  3522. // url="";
  3523. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3524. int wid = sqlite3_column_int(statement, 3);
  3525. int closeout = sqlite3_column_int(statement, 4);
  3526. int cid = sqlite3_column_int(statement, 5);
  3527. int wisdelete = sqlite3_column_int(statement, 6);
  3528. int more_color = sqlite3_column_int(statement, 7);
  3529. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  3530. if(wid !=0 && wisdelete != 1)
  3531. [item setValue:@"true" forKey:@"wish_exists"];
  3532. else
  3533. [item setValue:@"false" forKey:@"wish_exists"];
  3534. if(closeout==0)
  3535. [item setValue:@"false" forKey:@"is_closeout"];
  3536. else
  3537. [item setValue:@"true" forKey:@"is_closeout"];
  3538. if(cid==0)
  3539. [item setValue:@"false" forKey:@"cart_exists"];
  3540. else
  3541. [item setValue:@"true" forKey:@"cart_exists"];
  3542. if (more_color == 0) {
  3543. [item setObject:@(false) forKey:@"more_color"];
  3544. } else if (more_color == 1) {
  3545. [item setObject:@(true) forKey:@"more_color"];
  3546. }
  3547. [item addEntriesFromDictionary:imgjson];
  3548. // [item setValue:nsurl forKey:@"img"];
  3549. [item setValue:nsname forKey:@"fash_name"];
  3550. [item setValue:nsdescription forKey:@"description"];
  3551. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3552. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3553. i++;
  3554. }
  3555. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3556. [ret setObject:items forKey:@"items"];
  3557. sqlite3_finalize(statement);
  3558. }
  3559. DebugLog(@"count:%d",count);
  3560. [iSalesDB close_db:db];
  3561. #ifdef DEBUG
  3562. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3563. #endif
  3564. return ret;
  3565. }
  3566. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  3567. {
  3568. return [self search:params limited:YES];
  3569. }
  3570. //+(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  3571. //{
  3572. // UIApplication * app = [UIApplication sharedApplication];
  3573. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3574. //
  3575. //// [iSalesDB disable_trigger]
  3576. // [iSalesDB disable_trigger];
  3577. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3578. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3579. //
  3580. // [iSalesDB enable_trigger];
  3581. //// [iSalesDB enable_trigger]
  3582. // //
  3583. // // NSString* user = [params valueForKey:@"user"];
  3584. // //
  3585. // // NSString* password = [params valueForKey:@"password"];
  3586. //
  3587. //
  3588. //
  3589. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3590. //
  3591. // ret[@"result"]=[NSNumber numberWithInt:2 ];
  3592. //
  3593. //
  3594. //
  3595. // appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  3596. // appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  3597. // appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  3598. //
  3599. // [appDelegate update_count_mark];
  3600. //
  3601. //
  3602. // appDelegate.can_show_price =false;
  3603. // appDelegate.can_see_price =false;
  3604. // appDelegate.can_create_portfolio =false;
  3605. // appDelegate.can_create_order =false;
  3606. //
  3607. //
  3608. // appDelegate.can_cancel_order =false;
  3609. // appDelegate.can_set_cart_price =false;
  3610. // appDelegate.can_delete_order =false;
  3611. // appDelegate.can_submit_order =false;
  3612. // appDelegate.can_set_tearsheet_price =false;
  3613. // appDelegate.can_update_contact_info = false;
  3614. //
  3615. // appDelegate.save_order_logout = false;
  3616. // appDelegate.submit_order_logout = false;
  3617. // appDelegate.alert_sold_in_quantities = false;
  3618. //
  3619. // appDelegate.ipad_perm =nil ;
  3620. // appDelegate.user_type = USER_ROLE_UNKNOWN;
  3621. // appDelegate.OrderFilter= nil;
  3622. // [appDelegate SetSo:nil];
  3623. // [appDelegate set_main_button_panel];
  3624. //
  3625. //
  3626. // // sqlite3 *db = [iSalesDB get_db];
  3627. // //
  3628. // //
  3629. // //
  3630. // //
  3631. // //
  3632. // // 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"]];
  3633. // //
  3634. // //
  3635. // //
  3636. // //
  3637. // // DebugLog(@"offline_login sql:%@",sqlQuery);
  3638. // // sqlite3_stmt * statement;
  3639. // //
  3640. // //
  3641. // // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3642. // //
  3643. // //
  3644. // // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3645. // // {
  3646. // //
  3647. // //
  3648. // // if (sqlite3_step(statement) == SQLITE_ROW)
  3649. // // {
  3650. // //
  3651. // // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3652. // // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  3653. // //
  3654. // //
  3655. // //
  3656. // // int can_show_price = sqlite3_column_int(statement, 0);
  3657. // // int can_see_price = sqlite3_column_int(statement, 1);
  3658. // //
  3659. // // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  3660. // // if(contact_id==nil)
  3661. // // contact_id="";
  3662. // // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3663. // //
  3664. // // int user_type = sqlite3_column_int(statement, 3);
  3665. // //
  3666. // // int can_cancel_order = sqlite3_column_int(statement, 4);
  3667. // // int can_set_cart_price = sqlite3_column_int(statement, 5);
  3668. // // int can_create_portfolio = sqlite3_column_int(statement, 6);
  3669. // // int can_delete_order = sqlite3_column_int(statement, 7);
  3670. // // int can_submit_order = sqlite3_column_int(statement, 8);
  3671. // // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  3672. // // int can_create_order = sqlite3_column_int(statement, 10);
  3673. // //
  3674. // //
  3675. // // char *mode = (char*)sqlite3_column_text(statement, 11);
  3676. // // if(mode==nil)
  3677. // // mode="";
  3678. // // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  3679. // //
  3680. // //
  3681. // // char *username = (char*)sqlite3_column_text(statement, 12);
  3682. // // if(username==nil)
  3683. // // username="";
  3684. // // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  3685. // //
  3686. // //
  3687. // // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  3688. // // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  3689. // // [header setValue:nscontact_id forKey:@"contact_id"];
  3690. // // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  3691. // // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  3692. // // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  3693. // // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  3694. // // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  3695. // // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  3696. // // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  3697. // // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  3698. // //
  3699. // // [header setValue:nsusername forKey:@"username"];
  3700. // //
  3701. // //
  3702. // // [ret setObject:header forKey:@"header"];
  3703. // // [ret setValue:nsmode forKey:@"mode"];
  3704. // //
  3705. // //
  3706. // // }
  3707. // //
  3708. // //
  3709. // //
  3710. // // sqlite3_finalize(statement);
  3711. // // }
  3712. // //
  3713. // //
  3714. // //
  3715. // // [iSalesDB close_db:db];
  3716. // //
  3717. // //
  3718. // //
  3719. // //
  3720. // // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3721. //
  3722. // return ret;
  3723. //}
  3724. //+(NSData*) offline_createorder :(NSMutableDictionary *) params
  3725. //{
  3726. //
  3727. // UIApplication * app = [UIApplication sharedApplication];
  3728. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3729. //
  3730. // [iSalesDB disable_trigger];
  3731. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3732. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3733. // [iSalesDB enable_trigger];
  3734. //
  3735. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3736. // NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  3737. // NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  3738. //// NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  3739. // // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  3740. // // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  3741. // // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  3742. // // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  3743. // NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  3744. // NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  3745. // // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  3746. // // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  3747. // NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  3748. // NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  3749. // NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  3750. // NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  3751. // // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  3752. // NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  3753. // // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  3754. // NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  3755. // // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  3756. // // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  3757. // NSString* create_by = [self translateSingleQuote:params[@"user"]];
  3758. //
  3759. // NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3760. //
  3761. // [arr_name addObject:customer_first_name];
  3762. // [arr_name addObject:customer_last_name];
  3763. //
  3764. // NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  3765. //
  3766. // // default ship from
  3767. // 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';";
  3768. //
  3769. // __block NSString *cid = @"";
  3770. // __block NSString *name = @"";
  3771. // __block NSString *ext = @"";
  3772. // __block NSString *contact = @"";
  3773. // __block NSString *email = @"";
  3774. // __block NSString *fax = @"";
  3775. // __block NSString *phone = @"";
  3776. //
  3777. // sqlite3 *db = [iSalesDB get_db];
  3778. //
  3779. // [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  3780. //
  3781. // cid = [self textAtColumn:0 statement:statment];
  3782. // name = [self textAtColumn:1 statement:statment];
  3783. // ext = [self textAtColumn:2 statement:statment];
  3784. // contact = [self textAtColumn:3 statement:statment];
  3785. // email = [self textAtColumn:4 statement:statment];
  3786. // fax = [self textAtColumn:5 statement:statment];
  3787. // phone = [self textAtColumn:6 statement:statment];
  3788. //
  3789. // }];
  3790. //
  3791. // NSString* so_id = [self get_offline_soid:db];
  3792. // if(so_id==nil)
  3793. // so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  3794. //
  3795. // 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];
  3796. //
  3797. //
  3798. //
  3799. // int result =[iSalesDB execSql:sql_neworder db:db];
  3800. // [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  3801. //
  3802. //
  3803. //
  3804. // //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'
  3805. // //soId
  3806. // int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  3807. //
  3808. //
  3809. //
  3810. //
  3811. //
  3812. // NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  3813. // sqlite3_stmt * statement;
  3814. //
  3815. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3816. // {
  3817. // if (sqlite3_step(statement) == SQLITE_ROW)
  3818. // {
  3819. // // char *name = (char*)sqlite3_column_text(statement, 1);
  3820. // // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3821. //
  3822. // //ret = sqlite3_column_int(statement, 0);
  3823. //
  3824. // char *soId = (char*)sqlite3_column_text(statement, 0);
  3825. // if(soId==nil)
  3826. // soId="";
  3827. // NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  3828. // [ret setValue:nssoId forKey:@"soId"];
  3829. // [ret setValue:nssoId forKey:@"orderCode"];
  3830. //
  3831. // }
  3832. // sqlite3_finalize(statement);
  3833. // }
  3834. //
  3835. // [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  3836. // [ret setValue:@"Regular Mode" forKey:@"mode"];
  3837. //
  3838. //
  3839. // [iSalesDB close_db:db];
  3840. //
  3841. // return [RAConvertor dict2data:ret];
  3842. //
  3843. //}
  3844. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  3845. {
  3846. //assert(params[@"order_code"]);
  3847. // assert(params[@"order_code"]);
  3848. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3849. NSString* orderCode = [params valueForKey:@"orderCode"];
  3850. NSString* app_order_code= params[@"appDelegate.order_code"];
  3851. // UIApplication * app = [UIApplication sharedApplication];
  3852. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3853. if(![app_order_code isEqualToString:orderCode]&& app_order_code!=0)
  3854. {
  3855. [iSalesDB disable_trigger];
  3856. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3857. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3858. [iSalesDB enable_trigger];
  3859. }
  3860. sqlite3 *db = [iSalesDB get_db];
  3861. int cart_count=[self query_ordercartcount:orderCode db:db];
  3862. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3863. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3864. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3865. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3866. [iSalesDB close_db:db];
  3867. return [RAConvertor dict2data:ret];
  3868. }
  3869. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  3870. {
  3871. assert(params[@"can_create_backorder"]!=nil);
  3872. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3873. sqlite3 *db = [iSalesDB get_db];
  3874. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  3875. int count =0;
  3876. if(params[@"count"]!=nil)
  3877. {
  3878. count = [params[@"count"] intValue];
  3879. }
  3880. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  3881. NSString* product_id=params[@"product_id"];
  3882. NSString* orderCode=params[@"orderCode"];
  3883. NSString *qty = params[@"qty"];
  3884. NSArray* arr_id=[RAConvertor string2arr:product_id separator:@","];
  3885. NSArray *qty_arr = [RAConvertor string2arr:qty separator:@","];
  3886. __block int number_of_outOfStock = 0;
  3887. for(int i=0;i<arr_id.count;i++)
  3888. {
  3889. NSInteger item_qty= count;
  3890. if (qty) {
  3891. item_qty = [qty_arr[i] integerValue];
  3892. }
  3893. if(item_qty==0)
  3894. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  3895. // 检查新加Model数量是否大于库存
  3896. if (![params[@"can_create_backorder"] boolValue]) {
  3897. __block BOOL needContinue = NO;
  3898. [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) {
  3899. int availability = sqlite3_column_int(stmt, 0);
  3900. // 库存小于购买量为缺货
  3901. if (availability < item_qty || availability <= 0) {
  3902. number_of_outOfStock++;
  3903. needContinue = YES;
  3904. }
  3905. }];
  3906. if (needContinue) {
  3907. continue;
  3908. }
  3909. }
  3910. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  3911. NSString* sql=nil;
  3912. sqlite3_stmt *stmt = nil;
  3913. BOOL shouldStep = NO;
  3914. if(_id<0)
  3915. {
  3916. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  3917. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  3918. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  3919. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],(int)[arr_id[i] length],NULL);
  3920. sqlite3_bind_text(stmt,2,[orderCode UTF8String],(int)[orderCode length],NULL);
  3921. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  3922. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  3923. shouldStep = YES;
  3924. }
  3925. else
  3926. {
  3927. if (qty) { // wish list move to cart
  3928. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  3929. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  3930. sqlite3_bind_int(stmt, 1, _id);
  3931. shouldStep = YES;
  3932. } else {
  3933. 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];
  3934. __block BOOL update = YES;
  3935. if (![params[@"can_create_backorder"] boolValue]) {
  3936. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3937. int newQTY = sqlite3_column_int(stmt, 0);
  3938. int availability = sqlite3_column_int(stmt, 1);
  3939. if (newQTY > availability) { // 库存不够
  3940. update = NO;
  3941. number_of_outOfStock++;
  3942. }
  3943. }];
  3944. }
  3945. if (update) {
  3946. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  3947. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  3948. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  3949. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",_id] length],NULL);
  3950. shouldStep = YES;
  3951. }
  3952. }
  3953. }
  3954. if (shouldStep) {
  3955. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  3956. [iSalesDB execSql:@"ROLLBACK" db:db];
  3957. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  3958. [iSalesDB close_db:db];
  3959. DebugLog(@"add to cart error");
  3960. return [RAConvertor dict2data:ret];
  3961. }
  3962. }
  3963. }
  3964. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  3965. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3966. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3967. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3968. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3969. if (![params[@"can_create_backorder"] boolValue]) {
  3970. if (number_of_outOfStock > 0) {
  3971. ret[@"result"]=[NSNumber numberWithInt:8];
  3972. ret[@"err_msg"] = @"Out of Stock.\nNo more quantity available.\nPlease try again after sync.";
  3973. /*[NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock]*/
  3974. }
  3975. }
  3976. [iSalesDB execSql:@"END TRANSACTION" db:db];
  3977. [iSalesDB close_db:db];
  3978. return [RAConvertor dict2data:ret];
  3979. }
  3980. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute contactID:(NSString *)contactID user:(NSString*)user
  3981. {
  3982. // UIApplication * app = [UIApplication sharedApplication];
  3983. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3984. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  3985. 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];
  3986. sqlite3_stmt * statement;
  3987. int count=0;
  3988. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3989. {
  3990. while (sqlite3_step(statement) == SQLITE_ROW)
  3991. {
  3992. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  3993. int bitem_id = sqlite3_column_int(statement, 0);
  3994. int bitem_qty = sqlite3_column_int(statement, 1);
  3995. char *name = (char*)sqlite3_column_text(statement, 2);
  3996. if(name==nil)
  3997. name="";
  3998. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  3999. char *description = (char*)sqlite3_column_text(statement, 3);
  4000. if(description==nil)
  4001. description="";
  4002. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  4003. double unit_price = sqlite3_column_double(statement, 4);
  4004. int use_unitprice = sqlite3_column_int(statement, 5);
  4005. if(use_unitprice!=1)
  4006. {
  4007. unit_price=[self get_model_default_price:contactID user:user product_id:nil item_id:@(bitem_id) db:db].doubleValue;
  4008. }
  4009. itemjson[@"model"]=nsname;
  4010. itemjson[@"description"]=nsdescription;
  4011. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  4012. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  4013. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  4014. if(compute)
  4015. {
  4016. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute contactID:contactID user:user];
  4017. }
  4018. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  4019. count++;
  4020. }
  4021. sqlite3_finalize(statement);
  4022. }
  4023. ret[@"count"]=@(count);
  4024. if(count==0)
  4025. return nil;
  4026. else
  4027. return ret;
  4028. }
  4029. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  4030. {
  4031. //compute: add part to subtotal;
  4032. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  4033. dict_item[@(item_id)]=@"1";
  4034. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  4035. double cuft=0;
  4036. double weight=0;
  4037. int carton=0;
  4038. int impack=0;
  4039. 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];
  4040. sqlite3_stmt * statement;
  4041. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4042. {
  4043. if (sqlite3_step(statement) == SQLITE_ROW)
  4044. {
  4045. double ulength = sqlite3_column_double(statement, 0);
  4046. double uwidth = sqlite3_column_double(statement, 1);
  4047. double uheight = sqlite3_column_double(statement, 2);
  4048. double uweight = sqlite3_column_double(statement, 3);
  4049. double mlength = sqlite3_column_double(statement, 4);
  4050. double mwidth = sqlite3_column_double(statement, 5);
  4051. double mheight = sqlite3_column_double(statement, 6);
  4052. double mweight = sqlite3_column_double(statement, 7);
  4053. double ilength = sqlite3_column_double(statement, 8);
  4054. double iwidth = sqlite3_column_double(statement, 9);
  4055. double iheight = sqlite3_column_double(statement, 10);
  4056. double iweight = sqlite3_column_double(statement, 11);
  4057. // int pcs = sqlite3_column_int(statement,12);
  4058. int mpack = sqlite3_column_int(statement, 13);
  4059. impack = mpack;
  4060. int ipack = sqlite3_column_int(statement, 14);
  4061. double ucbf = sqlite3_column_double(statement, 15);
  4062. // double icbf = sqlite3_column_double(statement, 16);
  4063. // double mcbf = sqlite3_column_double(statement, 17);
  4064. if(ipack==0)
  4065. {
  4066. carton= count/mpack ;
  4067. weight = mweight*carton;
  4068. cuft= carton*(mlength*mwidth*mheight);
  4069. int remain=count%mpack;
  4070. if(remain==0)
  4071. {
  4072. //do nothing;
  4073. }
  4074. else
  4075. {
  4076. carton++;
  4077. weight += uweight*remain;
  4078. cuft += (ulength*uwidth*uheight)*remain;
  4079. }
  4080. }
  4081. else
  4082. {
  4083. carton = count/(mpack*ipack);
  4084. weight = mweight*carton;
  4085. cuft= carton*(mlength*mwidth*mheight);
  4086. int remain=count%(mpack*ipack);
  4087. if(remain==0)
  4088. {
  4089. // do nothing;
  4090. }
  4091. else
  4092. {
  4093. carton++;
  4094. int icarton =remain/ipack;
  4095. int iremain=remain%ipack;
  4096. weight += iweight*icarton;
  4097. cuft += (ilength*iwidth*iheight)*icarton;
  4098. if(iremain==0)
  4099. {
  4100. //do nothing;
  4101. }
  4102. else
  4103. {
  4104. weight += uweight*iremain;
  4105. cuft += (ulength*uwidth*uheight)*iremain;
  4106. }
  4107. }
  4108. }
  4109. #if defined(BUILD_NPD) || defined(BUILD_USAI) || defined(BUILD_UWAVER)|| defined (BUILD_CONTRAST)
  4110. cuft=ucbf*count;
  4111. weight= uweight*count;
  4112. #endif
  4113. #if defined(BUILD_HOMER) || defined(BUILD_GATIT)
  4114. cuft=ucbf*count;
  4115. weight= uweight*count;
  4116. #endif
  4117. }
  4118. sqlite3_finalize(statement);
  4119. }
  4120. if(compute)
  4121. {
  4122. NSArray * arr_count=nil;
  4123. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  4124. for(int i=0;i<arr_bundle.count;i++)
  4125. {
  4126. dict_item[arr_bundle[i]]=@"1";
  4127. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  4128. cuft+=[bundlejson[@"cuft"] doubleValue];
  4129. weight+=[bundlejson[@"weight"] doubleValue];
  4130. carton+=[bundlejson[@"carton"] intValue];
  4131. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  4132. }
  4133. }
  4134. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  4135. ret[@"mpack"]= [NSNumber numberWithInteger:impack];
  4136. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  4137. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  4138. ret[@"items"]=dict_item;
  4139. return ret;
  4140. }
  4141. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  4142. {
  4143. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  4144. // 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 ];
  4145. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  4146. DebugLog(@"offline_login sql:%@",sqlQuery);
  4147. sqlite3_stmt * statement;
  4148. int cart_count=0;
  4149. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  4150. if ( dbresult== SQLITE_OK)
  4151. {
  4152. while (sqlite3_step(statement) == SQLITE_ROW)
  4153. {
  4154. int item_id = sqlite3_column_int(statement, 0);
  4155. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  4156. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  4157. }
  4158. sqlite3_finalize(statement);
  4159. }
  4160. return cart_count;
  4161. }
  4162. //+(NSData*) offline_requestcart :(NSMutableDictionary *) params
  4163. //{
  4164. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4165. // sqlite3 *db = [iSalesDB get_db];
  4166. // UIApplication * app = [UIApplication sharedApplication];
  4167. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4168. //
  4169. // NSString* orderCode=params[@"orderCode"];
  4170. //
  4171. // int sort = [[params objectForKey:@"sort"] intValue];
  4172. // NSString *sort_str = @"";
  4173. // switch (sort) {
  4174. // case 0:{
  4175. // sort_str = @"order by c.modify_time desc";
  4176. // }
  4177. // break;
  4178. // case 1:{
  4179. // sort_str = @"order by c.modify_time asc";
  4180. // }
  4181. // break;
  4182. // case 2:{
  4183. // sort_str = @"order by m.name asc";
  4184. // }
  4185. // break;
  4186. // case 3:{
  4187. // sort_str = @"order by m.name desc";
  4188. // }
  4189. // break;
  4190. // case 4:{
  4191. // sort_str = @"order by m.description asc";
  4192. // }
  4193. // break;
  4194. //
  4195. // default:
  4196. // break;
  4197. //
  4198. // }
  4199. //
  4200. //
  4201. //
  4202. // 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 ];
  4203. //
  4204. //
  4205. //// 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 ];
  4206. //
  4207. //
  4208. //// NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  4209. //
  4210. // DebugLog(@"offline_login sql:%@",sqlQuery);
  4211. // sqlite3_stmt * statement;
  4212. //
  4213. //
  4214. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  4215. //
  4216. // NSDate *date1 = [NSDate date];
  4217. //
  4218. // int count=0;
  4219. // int cart_count=0;
  4220. // int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  4221. // if ( dbresult== SQLITE_OK)
  4222. // {
  4223. //
  4224. //
  4225. // while (sqlite3_step(statement) == SQLITE_ROW)
  4226. // {
  4227. //// NSDate *row_date = [NSDate date];
  4228. //
  4229. //
  4230. // NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  4231. //
  4232. //
  4233. //
  4234. //
  4235. // int product_id = sqlite3_column_int(statement, 0);
  4236. //
  4237. // char *str_price = (char*)sqlite3_column_text(statement, 1);
  4238. //
  4239. // int item_id = sqlite3_column_int(statement, 7);
  4240. //
  4241. // NSString* Price=nil;
  4242. // if(str_price==nil)
  4243. // {
  4244. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  4245. //// NSDate *price_date = [NSDate date];
  4246. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  4247. //// DebugLog(@"price time interval");
  4248. //// [self printTimeIntervalBetween:price_date and:[NSDate date]];
  4249. //
  4250. // if(price==nil)
  4251. // Price=@"No Price.";
  4252. // else
  4253. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  4254. // }
  4255. // else
  4256. // {
  4257. //
  4258. // Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  4259. // }
  4260. //
  4261. //
  4262. // double discount = sqlite3_column_double(statement, 2);
  4263. // int item_count = sqlite3_column_int(statement, 3);
  4264. //
  4265. // char *line_note = (char*)sqlite3_column_text(statement, 4);
  4266. // NSString *nsline_note=nil;
  4267. // if(line_note!=nil)
  4268. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  4269. //
  4270. //
  4271. // char *name = (char*)sqlite3_column_text(statement, 5);
  4272. // NSString *nsname=nil;
  4273. // if(name!=nil)
  4274. // nsname= [[NSString alloc]initWithUTF8String:name];
  4275. //
  4276. // char *description = (char*)sqlite3_column_text(statement, 6);
  4277. // NSString *nsdescription=nil;
  4278. // if(description!=nil)
  4279. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  4280. //
  4281. //
  4282. //
  4283. // int stockUom = sqlite3_column_int(statement, 8);
  4284. // int _id = sqlite3_column_int(statement, 9);
  4285. // int availability = sqlite3_column_int(statement, 10);
  4286. //
  4287. //// NSDate *subtotal_date = [NSDate date];
  4288. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  4289. //// DebugLog(@"subtotal_date time interval");
  4290. //// [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  4291. //
  4292. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  4293. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  4294. // int carton=[bsubtotaljson[@"carton"] intValue];
  4295. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  4296. //
  4297. //// itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  4298. //// NSDate *img_date = [NSDate date];
  4299. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  4300. //// DebugLog(@"img_date time interval");
  4301. //// [self printTimeIntervalBetween:img_date and:[NSDate date]];
  4302. //
  4303. // itemjson[@"model"]=nsname;
  4304. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  4305. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  4306. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  4307. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  4308. // itemjson[@"check"]=@"true";
  4309. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  4310. // itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  4311. // itemjson[@"unit_price"]=Price;
  4312. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  4313. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  4314. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  4315. // itemjson[@"note"]=nsline_note;
  4316. // if (!appDelegate.can_create_backorder) {
  4317. // itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  4318. // }
  4319. //// NSDate *date2 = [NSDate date];
  4320. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  4321. //// DebugLog(@"model_bundle time interval");
  4322. //// [self printTimeIntervalBetween:date2 and:[NSDate date]];
  4323. //
  4324. // ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  4325. // count++;
  4326. //
  4327. //// DebugLog(@"row time interval");
  4328. //// [self printTimeIntervalBetween:row_date and:[NSDate date]];
  4329. // }
  4330. //
  4331. //
  4332. // ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  4333. // sqlite3_finalize(statement);
  4334. // }
  4335. //
  4336. //
  4337. // DebugLog(@"request cart total time interval");
  4338. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  4339. //
  4340. //
  4341. // //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  4342. //
  4343. // int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  4344. // ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  4345. // ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  4346. // ret[@"count"]=[NSNumber numberWithInt:count ];
  4347. //
  4348. // ret[@"mode"]=@"Regular Mode";
  4349. //
  4350. // [iSalesDB close_db:db];
  4351. //
  4352. // NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  4353. //
  4354. // DebugLog(@"general notes :%@",general_note);
  4355. //
  4356. // ret[@"general_note"]= general_note;
  4357. //
  4358. // return [RAConvertor dict2data:ret];
  4359. //}
  4360. +(NSData*) offline_login :(NSMutableDictionary *) params
  4361. {
  4362. NSString* user = [params valueForKey:@"user"];
  4363. NSString* password = [params valueForKey:@"password"];
  4364. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4365. sqlite3 *db = [iSalesDB get_db];
  4366. 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"]];
  4367. DebugLog(@"offline_login sql:%@",sqlQuery);
  4368. sqlite3_stmt * statement;
  4369. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  4370. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4371. {
  4372. if (sqlite3_step(statement) == SQLITE_ROW)
  4373. {
  4374. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4375. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  4376. int can_show_price = sqlite3_column_int(statement, 0);
  4377. int can_see_price = sqlite3_column_int(statement, 1);
  4378. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  4379. if(contact_id==nil)
  4380. contact_id="";
  4381. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4382. int user_type = sqlite3_column_int(statement, 3);
  4383. int can_cancel_order = sqlite3_column_int(statement, 4);
  4384. int can_set_cart_price = sqlite3_column_int(statement, 5);
  4385. int can_create_portfolio = sqlite3_column_int(statement, 6);
  4386. int can_delete_order = sqlite3_column_int(statement, 7);
  4387. int can_submit_order = sqlite3_column_int(statement, 8);
  4388. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  4389. int can_create_order = sqlite3_column_int(statement, 10);
  4390. char *mode = (char*)sqlite3_column_text(statement, 11);
  4391. if(mode==nil)
  4392. mode="";
  4393. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  4394. char *username = (char*)sqlite3_column_text(statement, 12);
  4395. if(username==nil)
  4396. username="";
  4397. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  4398. int can_update_contact_info = sqlite3_column_int(statement, 13);
  4399. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  4400. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  4401. [header setValue:nscontact_id forKey:@"contact_id"];
  4402. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  4403. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  4404. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  4405. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  4406. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  4407. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  4408. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  4409. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  4410. #ifdef BUILD_CONTRAST
  4411. [header setValue:[NSNumber numberWithBool:1] forKey:@"alert_sold_in_quantities"];
  4412. #endif
  4413. [header setObject:@(YES) forKey:@"can_create_backorder"];
  4414. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  4415. [header setValue:nsusername forKey:@"username"];
  4416. NSError* error=nil;
  4417. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  4418. [header setValue:statusFilter forKey:@"statusFilter"];
  4419. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4420. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  4421. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  4422. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  4423. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  4424. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  4425. [ret setObject:header forKey:@"header"];
  4426. [ret setValue:nsmode forKey:@"mode"];
  4427. }
  4428. sqlite3_finalize(statement);
  4429. }
  4430. [iSalesDB close_db:db];
  4431. #ifdef DEBUG
  4432. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4433. #endif
  4434. return [RAConvertor dict2data:ret];
  4435. }
  4436. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  4437. {
  4438. assert(params[@"mode"]!=nil);
  4439. NSString* contactId = [params valueForKey:@"contactId"];
  4440. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4441. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4442. sqlite3 *db = [iSalesDB get_db];
  4443. NSString *sqlQuery = nil;
  4444. {
  4445. 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];
  4446. }
  4447. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  4448. sqlite3_stmt * statement;
  4449. [ret setValue:@"2" forKey:@"result"];
  4450. int carrier = -1;
  4451. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4452. {
  4453. //int i = 0;
  4454. if (sqlite3_step(statement) == SQLITE_ROW)
  4455. {
  4456. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4457. // int editable = sqlite3_column_int(statement, 0);
  4458. char *company_name = (char*)sqlite3_column_text(statement, 1);
  4459. NSString *nscompany_name =nil;
  4460. if(company_name==nil)
  4461. nscompany_name=@"";
  4462. else
  4463. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  4464. char *country = (char*)sqlite3_column_text(statement, 2);
  4465. if(country==nil)
  4466. country="";
  4467. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  4468. // char *addr = (char*)sqlite3_column_text(statement, 3);
  4469. // if(addr==nil)
  4470. // addr="";
  4471. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  4472. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  4473. if(zipcode==nil)
  4474. zipcode="";
  4475. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  4476. char *state = (char*)sqlite3_column_text(statement, 5);
  4477. if(state==nil)
  4478. state="";
  4479. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  4480. char *city = (char*)sqlite3_column_text(statement, 6);
  4481. if(city==nil)
  4482. city="";
  4483. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  4484. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  4485. // NSString *nscontact_name = nil;
  4486. // if(contact_name==nil)
  4487. // nscontact_name=@"";
  4488. // else
  4489. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  4490. char *phone = (char*)sqlite3_column_text(statement, 8);
  4491. NSString *nsphone = nil;
  4492. if(phone==nil)
  4493. nsphone=@"";
  4494. else
  4495. nsphone= [[NSString alloc]initWithUTF8String:phone];
  4496. // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  4497. // if(contact_id==nil)
  4498. // contact_id="";
  4499. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4500. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  4501. if(addr_1==nil)
  4502. addr_1="";
  4503. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4504. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  4505. if(addr_2==nil)
  4506. addr_2="";
  4507. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4508. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  4509. if(addr_3==nil)
  4510. addr_3="";
  4511. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4512. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  4513. if(addr_4==nil)
  4514. addr_4="";
  4515. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4516. char *first_name = (char*)sqlite3_column_text(statement, 14);
  4517. if(first_name==nil)
  4518. first_name="";
  4519. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  4520. char *last_name = (char*)sqlite3_column_text(statement, 15);
  4521. if(last_name==nil)
  4522. last_name="";
  4523. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  4524. char *fax = (char*)sqlite3_column_text(statement, 16);
  4525. NSString *nsfax = nil;
  4526. if(fax==nil)
  4527. nsfax=@"";
  4528. else
  4529. nsfax= [[NSString alloc]initWithUTF8String:fax];
  4530. char *email = (char*)sqlite3_column_text(statement, 17);
  4531. NSString *nsemail = nil;
  4532. if(email==nil)
  4533. nsemail=@"";
  4534. else
  4535. nsemail= [[NSString alloc]initWithUTF8String:email];
  4536. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  4537. NSString *nsimg_0 = nil;
  4538. if(img_0==nil)
  4539. nsimg_0=@"";
  4540. else
  4541. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  4542. [self copy_bcardImg:nsimg_0];
  4543. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  4544. NSString *nsimg_1 = nil;
  4545. if(img_1==nil)
  4546. nsimg_1=@"";
  4547. else
  4548. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  4549. [self copy_bcardImg:nsimg_1];
  4550. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  4551. NSString *nsimg_2 = nil;
  4552. if(img_2==nil)
  4553. nsimg_2=@"";
  4554. else
  4555. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  4556. [self copy_bcardImg:nsimg_2];
  4557. char *price_type = (char*)sqlite3_column_text(statement, 21);
  4558. NSString *nsprice_type = nil;
  4559. if(price_type==nil)
  4560. nsprice_type=@"";
  4561. else
  4562. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  4563. char *notes = (char*)sqlite3_column_text(statement, 22);
  4564. NSString *nsnotes = nil;
  4565. if(notes==nil)
  4566. nsnotes=@"";
  4567. else
  4568. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  4569. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  4570. NSString *nssalesrep = nil;
  4571. if(salesrep==nil)
  4572. nssalesrep=@"";
  4573. else
  4574. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  4575. NSString *contact_type = [self textAtColumn:24 statement:statement];
  4576. carrier = sqlite3_column_int(statement, 25);
  4577. {
  4578. // decrypt
  4579. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  4580. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  4581. nsphone=[AESCrypt fastdecrypt:nsphone];
  4582. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4583. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  4584. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4585. }
  4586. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4587. [arr_name addObject:nsfirst_name];
  4588. [arr_name addObject:nslast_name];
  4589. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  4590. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  4591. [arr_ext addObject:nsaddr_1];
  4592. [arr_ext addObject:nsaddr_2];
  4593. [arr_ext addObject:nsaddr_3];
  4594. [arr_ext addObject:nsaddr_4];
  4595. [arr_ext addObject:@"\r\n"];
  4596. [arr_ext addObject:nscity];
  4597. [arr_ext addObject:nsstate];
  4598. [arr_ext addObject:nszipcode];
  4599. [arr_ext addObject:nscountry];
  4600. NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  4601. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  4602. [item setValue:nsimg_2 forKey:@"business_card_2"];
  4603. [item setValue:nsimg_0 forKey:@"business_card_0"];
  4604. [item setValue:nscountry forKey:@"customer_country"];
  4605. [item setValue:nsphone forKey:@"customer_phone"];
  4606. [item setValue:nsimg_1 forKey:@"business_card_1"];
  4607. [item setValue:nscompany_name forKey:@"customer_name"];
  4608. [item setValue:nsprice_type forKey:@"customer_price_type"];
  4609. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  4610. [item setValue:nsext forKey:@"customer_contact_ext"];
  4611. [item setValue:nszipcode forKey:@"customer_zipcode"];
  4612. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  4613. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  4614. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  4615. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  4616. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  4617. [item setValue:nslast_name forKey:@"customer_last_name"];
  4618. [item setValue:nscity forKey:@"customer_city"];
  4619. [item setValue:nsstate forKey:@"customer_state"];
  4620. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  4621. [item setValue:contactId forKey:@"customer_cid"];
  4622. [item setValue:nscontact_name forKey:@"customer_contact"];
  4623. [item setValue:nsfax forKey:@"customer_fax"];
  4624. [item setValue:nsemail forKey:@"customer_email"];
  4625. [item setValue:contact_type forKey:@"customer_contact_type"];
  4626. // i++;
  4627. }
  4628. // UIApplication * app = [UIApplication sharedApplication];
  4629. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4630. [ret setValue:params[@"mode"] forKey:@"mode"];
  4631. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  4632. sqlite3_finalize(statement);
  4633. }
  4634. [iSalesDB close_db:db];
  4635. NSString* scarrier = [self offline_getCarrier:carrier];
  4636. item[@"customer_truck_carrier"] = scarrier;
  4637. [ret setObject:item forKey:@"customerInfo"];
  4638. #ifdef DEBUG
  4639. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4640. #endif
  4641. return ret;
  4642. }
  4643. + (bool) copy_bcardImg:(NSString*) filename
  4644. {
  4645. if(filename.length==0)
  4646. return false;
  4647. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4648. bool ret=false;
  4649. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4650. NSString *cachefolder = [paths objectAtIndex:0];
  4651. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  4652. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  4653. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  4654. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  4655. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  4656. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  4657. //
  4658. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  4659. NSFileManager* fileManager = [NSFileManager defaultManager];
  4660. BOOL bdir=NO;
  4661. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  4662. {
  4663. NSError *error = nil;
  4664. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  4665. {
  4666. ret=false;
  4667. }
  4668. else
  4669. {
  4670. ret=true;
  4671. }
  4672. // NSError *error = nil;
  4673. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  4674. //
  4675. // if(!bsuccess)
  4676. // {
  4677. // DebugLog(@"Create offline_createimg folder failed");
  4678. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4679. // return [RAConvertor dict2data:ret];
  4680. // }
  4681. // if(bsuccess)
  4682. // {
  4683. // sqlite3 *db = [self get_db];
  4684. //
  4685. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4686. // [iSalesDB close_db:db];
  4687. // }
  4688. }
  4689. return ret;
  4690. //
  4691. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  4692. // if(bsuccess)
  4693. // {
  4694. // NSError *error = nil;
  4695. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  4696. // {
  4697. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4698. // }
  4699. // else
  4700. // {
  4701. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  4702. //
  4703. // ret[@"img_url_aname"]=filename;
  4704. // ret[@"img_url"]=savedImagePath;
  4705. // }
  4706. // }
  4707. }
  4708. +(NSData *) offline_saveBusinesscard:(NSData *) image
  4709. {
  4710. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4711. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4712. NSString *cachefolder = [paths objectAtIndex:0];
  4713. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  4714. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  4715. NSFileManager* fileManager = [NSFileManager defaultManager];
  4716. BOOL bdir=YES;
  4717. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  4718. {
  4719. NSError *error = nil;
  4720. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  4721. if(!bsuccess)
  4722. {
  4723. DebugLog(@"Create offline_createimg folder failed");
  4724. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4725. return [RAConvertor dict2data:ret];
  4726. }
  4727. // if(bsuccess)
  4728. // {
  4729. // sqlite3 *db = [self get_db];
  4730. //
  4731. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4732. // [iSalesDB close_db:db];
  4733. // }
  4734. }
  4735. if(! [fileManager fileExistsAtPath:img_cache isDirectory:&bdir])
  4736. {
  4737. NSError *error = nil;
  4738. bool bsuccess=[fileManager createDirectoryAtPath:img_cache withIntermediateDirectories:YES attributes:nil error:&error];
  4739. if(!bsuccess)
  4740. {
  4741. DebugLog(@"Create img_cache folder failed");
  4742. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4743. return [RAConvertor dict2data:ret];
  4744. }
  4745. // if(bsuccess)
  4746. // {
  4747. // sqlite3 *db = [self get_db];
  4748. //
  4749. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4750. // [iSalesDB close_db:db];
  4751. // }
  4752. }
  4753. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  4754. //JEPG格式
  4755. // NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  4756. NSString* filename =[NSString stringWithFormat:@"%@.jpg",[[NSUUID UUID] UUIDString]];
  4757. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  4758. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  4759. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  4760. if(bsuccess)
  4761. {
  4762. NSError *error = nil;
  4763. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  4764. {
  4765. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4766. }
  4767. else
  4768. {
  4769. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  4770. ret[@"img_url_aname"]=filename;
  4771. ret[@"img_url"]=filename;
  4772. }
  4773. }
  4774. else
  4775. {
  4776. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4777. }
  4778. return [RAConvertor dict2data:ret];
  4779. }
  4780. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  4781. {
  4782. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  4783. category = [category substringToIndex:3];
  4784. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4785. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  4786. params[@"category"]= category;
  4787. ret[@"params"]= params;
  4788. [ret setValue:@"detail" forKey:@"target"];
  4789. [ret setValue:@"popup" forKey:@"action"];
  4790. [ret setValue:@"content" forKey:@"type"];
  4791. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  4792. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  4793. [ret setValue:@"true" forKey:@"single_row"];
  4794. [ret setValue:@"true" forKey:@"partial_refresh"];
  4795. // sqlite3 *db = [iSalesDB get_db];
  4796. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  4797. NSString *sqlQuery =nil;
  4798. #ifdef BUILD_NPD
  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 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 ;
  4800. #else
  4801. sqlQuery=[NSString stringWithFormat:@"select m.name,m.product_id,i.url,random() as aa from product m LEFT join model_image i on m.product_id = i.product_id where m.category like'%%#%@#%%' and i.default_img = %d order by aa limit 10 ;",category,OFFLINE_IMG_TYPE_CATEGORY];
  4802. #endif
  4803. sqlite3_stmt * statement;
  4804. int count = 0;
  4805. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  4806. // int count=0;
  4807. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4808. {
  4809. int i=0;
  4810. while (sqlite3_step(statement) == SQLITE_ROW)
  4811. {
  4812. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4813. // char *name = (char*)sqlite3_column_text(statement, 1);
  4814. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4815. char *name = (char*)sqlite3_column_text(statement, 0);
  4816. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  4817. int product_id = sqlite3_column_int(statement, 1);
  4818. char *url = (char*)sqlite3_column_text(statement, 2);
  4819. if(url==nil)
  4820. url="";
  4821. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  4822. [item setValue:nsurl forKey:@"picture_path"];
  4823. [item setValue:nsname forKey:@"fash_name"];
  4824. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  4825. [item setValue:category forKey:@"category"];
  4826. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4827. i++;
  4828. }
  4829. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4830. sqlite3_finalize(statement);
  4831. }
  4832. DebugLog(@"count:%d",count);
  4833. // [iSalesDB close_db:db];
  4834. return ret;
  4835. }
  4836. +(NSDictionary*) model_priceKvItem:(NSString*) key value:(NSString*)value
  4837. {
  4838. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4839. [ret setValue:key forKey:@"key"];
  4840. [ret setValue:value forKey:@"val"];
  4841. [ret setValue:@"price" forKey:@"type"];
  4842. return ret;
  4843. }
  4844. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  4845. {
  4846. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4847. [ret setValue:key forKey:@"key"];
  4848. [ret setValue:value forKey:@"val"];
  4849. return ret;
  4850. }
  4851. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  4852. {
  4853. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4854. [ret setValue:@"0" forKey:@"img_count"];
  4855. // sqlite3 *db = [iSalesDB get_db];
  4856. 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 ;
  4857. sqlite3_stmt * statement;
  4858. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4859. {
  4860. int i=0;
  4861. if (sqlite3_step(statement) == SQLITE_ROW)
  4862. {
  4863. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4864. // char *name = (char*)sqlite3_column_text(statement, 1);
  4865. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4866. char *value = (char*)sqlite3_column_text(statement, 0);
  4867. if(value==nil)
  4868. value="";
  4869. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  4870. char *key = (char*)sqlite3_column_text(statement, 1);
  4871. if(key==nil)
  4872. key="";
  4873. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  4874. [item setValue:nsvalue forKey:@"val"];
  4875. [item setValue:nskey forKey:@"key"];
  4876. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4877. [ret setValue:@"1" forKey:@"count"];
  4878. i++;
  4879. }
  4880. sqlite3_finalize(statement);
  4881. }
  4882. // [iSalesDB close_db:db];
  4883. return ret;
  4884. }
  4885. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  4886. {
  4887. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4888. [ret setValue:@"0" forKey:@"count"];
  4889. // sqlite3 *db = [iSalesDB get_db];
  4890. 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;
  4891. sqlite3_stmt * statement;
  4892. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4893. {
  4894. int i=0;
  4895. while (sqlite3_step(statement) == SQLITE_ROW)
  4896. {
  4897. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4898. // char *name = (char*)sqlite3_column_text(statement, 1);
  4899. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4900. char *value = (char*)sqlite3_column_text(statement, 0);
  4901. if(value==nil)
  4902. value="";
  4903. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  4904. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  4905. if(selector_display==nil)
  4906. selector_display="";
  4907. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  4908. int product_id = sqlite3_column_int(statement, 2);
  4909. char *category = (char*)sqlite3_column_text(statement, 3);
  4910. if(category==nil)
  4911. category="";
  4912. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  4913. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  4914. [item setValue:nsvalue forKey:@"title"];
  4915. [item setValue:url forKey:@"pic_url"];
  4916. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  4917. [params setValue:@"2" forKey:@"count"];
  4918. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  4919. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  4920. [param0 setValue:@"product_id" forKey:@"name"];
  4921. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  4922. [param1 setValue:nscategory forKey:@"val"];
  4923. [param1 setValue:@"category" forKey:@"name"];
  4924. [params setObject:param0 forKey:@"param_0"];
  4925. [params setObject:param1 forKey:@"param_1"];
  4926. [item setObject:params forKey:@"params"];
  4927. [ret setValue:nsselector_display forKey:@"name"];
  4928. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4929. i++;
  4930. }
  4931. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4932. [ret setValue:@"switch" forKey:@"action"];
  4933. sqlite3_finalize(statement);
  4934. }
  4935. // [iSalesDB close_db:db];
  4936. return ret;
  4937. }
  4938. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  4939. {
  4940. // model 在 category search 显示的图片。
  4941. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  4942. // sqlite3 *db = [iSalesDB get_db];
  4943. 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];
  4944. sqlite3_stmt * statement;
  4945. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4946. {
  4947. while (sqlite3_step(statement) == SQLITE_ROW)
  4948. {
  4949. char *url = (char*)sqlite3_column_text(statement, 0);
  4950. if(url==nil)
  4951. url="";
  4952. int type = sqlite3_column_int(statement, 1);
  4953. if(type==0)
  4954. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  4955. else
  4956. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  4957. }
  4958. sqlite3_finalize(statement);
  4959. }
  4960. // [iSalesDB close_db:db];
  4961. return ret;
  4962. }
  4963. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  4964. {
  4965. int item_id=-1;
  4966. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  4967. sqlite3_stmt * statement;
  4968. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4969. {
  4970. if (sqlite3_step(statement) == SQLITE_ROW)
  4971. {
  4972. item_id = sqlite3_column_int(statement, 0);
  4973. }
  4974. sqlite3_finalize(statement);
  4975. }
  4976. return item_id;
  4977. }
  4978. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  4979. {
  4980. // NSString* ret = @"";
  4981. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  4982. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  4983. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  4984. sqlite3_stmt * statement;
  4985. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4986. {
  4987. while (sqlite3_step(statement) == SQLITE_ROW)
  4988. {
  4989. int bitem_id = sqlite3_column_int(statement, 0);
  4990. int bitem_qty = sqlite3_column_int(statement, 1);
  4991. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  4992. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  4993. }
  4994. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  4995. sqlite3_finalize(statement);
  4996. }
  4997. // if(ret==nil)
  4998. // ret=@"";
  4999. *count=arr_count;
  5000. return arr_bundle;
  5001. }
  5002. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  5003. {
  5004. // get default sold qty, return -1 if model not found;
  5005. int ret = -1;
  5006. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  5007. sqlite3_stmt * statement;
  5008. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5009. {
  5010. if (sqlite3_step(statement) == SQLITE_ROW)
  5011. {
  5012. ret = sqlite3_column_int(statement, 0);
  5013. }
  5014. sqlite3_finalize(statement);
  5015. }
  5016. return ret;
  5017. }
  5018. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  5019. {
  5020. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5021. [ret setValue:@"0" forKey:@"img_count"];
  5022. // sqlite3 *db = [iSalesDB get_db];
  5023. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  5024. sqlite3_stmt * statement;
  5025. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5026. {
  5027. int i=0;
  5028. while (sqlite3_step(statement) == SQLITE_ROW)
  5029. {
  5030. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5031. // char *name = (char*)sqlite3_column_text(statement, 1);
  5032. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  5033. char *url = (char*)sqlite3_column_text(statement, 0);
  5034. if(url==nil)
  5035. url="";
  5036. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  5037. [item setValue:nsurl forKey:@"s"];
  5038. [item setValue:nsurl forKey:@"l"];
  5039. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  5040. i++;
  5041. }
  5042. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  5043. sqlite3_finalize(statement);
  5044. }
  5045. // [iSalesDB close_db:db];
  5046. return ret;
  5047. }
  5048. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  5049. // UIApplication * app = [UIApplication sharedApplication];
  5050. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  5051. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  5052. sqlite3 *db = [iSalesDB get_db];
  5053. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  5054. NSString* product_id=params[@"product_id"];
  5055. NSString *item_count_str = params[@"item_count"];
  5056. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  5057. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  5058. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  5059. // NSString *sql = @"";
  5060. for(int i=0;i<arr.count;i++)
  5061. {
  5062. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  5063. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  5064. __block int cart_count = 0;
  5065. if (!item_count_str) {
  5066. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  5067. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5068. NSString *model_set = [self textAtColumn:0 statement:stmt];
  5069. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  5070. cart_count = [[model_set_components lastObject] intValue];
  5071. }];
  5072. }
  5073. if(count==0)
  5074. {
  5075. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  5076. sqlite3_stmt *stmt;
  5077. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  5078. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  5079. if (item_count_arr) {
  5080. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  5081. } else {
  5082. sqlite3_bind_int(stmt,2,cart_count);
  5083. }
  5084. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  5085. [iSalesDB execSql:@"ROLLBACK" db:db];
  5086. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  5087. [iSalesDB close_db:db];
  5088. DebugLog(@"add to wishlist error");
  5089. return ret;
  5090. }
  5091. } else {
  5092. int qty = 0;
  5093. if (item_count_arr) {
  5094. qty = [item_count_arr[i] intValue];
  5095. } else {
  5096. qty = cart_count;
  5097. }
  5098. 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]];
  5099. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  5100. [iSalesDB execSql:@"ROLLBACK" db:db];
  5101. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  5102. [iSalesDB close_db:db];
  5103. DebugLog(@"add to wishlist error");
  5104. return ret;
  5105. }
  5106. }
  5107. }
  5108. // [iSalesDB execSql:sql db:db];
  5109. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  5110. [iSalesDB execSql:@"END TRANSACTION" db:db];
  5111. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  5112. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  5113. [iSalesDB close_db:db];
  5114. ret[@"wish_count"]=@(count);
  5115. ret[@"result"]= [NSNumber numberWithInt:2];
  5116. return ret;
  5117. }
  5118. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  5119. // 0 category
  5120. // 1 search
  5121. // 2 itemsearch
  5122. NSData *ret = nil;
  5123. NSDictionary *items = nil;
  5124. switch (from) {
  5125. case 0:{
  5126. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  5127. }
  5128. break;
  5129. case 1:{
  5130. items = [[self search:params limited:NO] objectForKey:@"items"];
  5131. }
  5132. break;
  5133. case 2:{
  5134. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  5135. }
  5136. break;
  5137. default:
  5138. break;
  5139. }
  5140. if (!items) {
  5141. return ret;
  5142. }
  5143. int count = [[items objectForKey:@"count"] intValue];
  5144. NSMutableString *product_id_str = [@"" mutableCopy];
  5145. for (int i = 0; i < count; i++) {
  5146. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  5147. NSDictionary *item = [items objectForKey:key];
  5148. NSString *product_id = [item objectForKey:@"product_id"];
  5149. if (i == 0) {
  5150. [product_id_str appendString:product_id];
  5151. } else {
  5152. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  5153. }
  5154. }
  5155. NSString *add_to = [self valueInParams:params key:@"addTo"];
  5156. if ([add_to isEqualToString:@"cart"]) {
  5157. NSString *order_code = [params objectForKey:@"orderCode"];
  5158. if (order_code.length) {
  5159. NSDictionary *newParams = @{
  5160. @"product_id" : product_id_str,
  5161. @"orderCode" : order_code,
  5162. @"can_create_backorder":params[@"can_create_backorder"]
  5163. };
  5164. ret = [self offline_add2cart:newParams.mutableCopy];
  5165. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5166. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5167. ret = [RAConvertor dict2data:retDic];
  5168. }
  5169. } else if([add_to isEqualToString:@"wishlist"]) {
  5170. NSDictionary *newParams = @{
  5171. @"product_id" : product_id_str
  5172. };
  5173. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  5174. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5175. ret = [RAConvertor dict2data:retDic];
  5176. } else if([add_to isEqualToString:@"portfolio"]) {
  5177. NSDictionary *newParams = @{
  5178. @"product_id" : product_id_str
  5179. };
  5180. NSData *data = [self offline_add2Portfolio:newParams.mutableCopy];
  5181. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5182. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5183. ret = [RAConvertor dict2data:retDic];
  5184. }
  5185. return ret;
  5186. }
  5187. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  5188. {
  5189. return [self addAll:params from:0];
  5190. }
  5191. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  5192. {
  5193. return [self addAll:params from:1];
  5194. }
  5195. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  5196. {
  5197. return [self addAll:params from:2];
  5198. }
  5199. #pragma mark - Jack
  5200. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  5201. // "val_227" : {
  5202. // "check" : 1,
  5203. // "value" : "US United States",
  5204. // "value_id" : "228"
  5205. // },
  5206. if (!countryCode) {
  5207. countryCode = @"US";
  5208. }
  5209. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  5210. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  5211. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  5212. int code_id = sqlite3_column_int(stmt, 3); // id
  5213. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5214. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  5215. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  5216. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5217. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  5218. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5219. }
  5220. long n = *count;
  5221. *count = n + 1;
  5222. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  5223. [container setValue:countryDic forKey:key];
  5224. }] mutableCopy];
  5225. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5226. return ret;
  5227. }
  5228. + (NSDictionary *)offline_OrderCarrier:(NSString *)order_code db:(sqlite3 *)db {
  5229. // "val_227" : {
  5230. // "check" : 1,
  5231. // "value" : "US United States",
  5232. // "value_id" : "228"
  5233. // },
  5234. __block NSString* OrderCarrier =@"";
  5235. // sqlite3 *db = [iSalesDB get_db];
  5236. // sqlite3_stmt * statement;
  5237. [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) {
  5238. OrderCarrier = [self textAtColumn:0 statement:statment];
  5239. // sqlite3_finalize(statement);
  5240. }];
  5241. // [iSalesDB close_db:db];
  5242. __block int c=0;
  5243. 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) {
  5244. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  5245. int code_id = sqlite3_column_int(stmt, 2); // id
  5246. NSMutableDictionary *carrierDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5247. [carrierDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  5248. [carrierDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  5249. [carrierDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5250. if ([OrderCarrier isEqualToString:[NSString stringWithUTF8String:name]]) {
  5251. [carrierDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5252. }
  5253. c++;
  5254. long n = *count;
  5255. *count = n + 1;
  5256. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  5257. [container setValue:carrierDic forKey:key];
  5258. }] mutableCopy];
  5259. [ret setValue:[NSNumber numberWithLong:c] forKey:@"count"];
  5260. return ret;
  5261. }
  5262. + (NSDictionary *)offline_getAllCarrier:(NSString *)carrierName {
  5263. // "val_227" : {
  5264. // "check" : 1,
  5265. // "value" : "US United States",
  5266. // "value_id" : "228"
  5267. // },
  5268. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code_id from carrier order by name;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  5269. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  5270. int code_id = sqlite3_column_int(stmt, 2); // id
  5271. NSMutableDictionary *carrierDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5272. [carrierDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  5273. [carrierDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  5274. [carrierDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5275. if ([carrierName isEqualToString:[NSString stringWithUTF8String:name]]) {
  5276. [carrierDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5277. }
  5278. long n = *count;
  5279. *count = n + 1;
  5280. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  5281. [container setValue:carrierDic forKey:key];
  5282. }] mutableCopy];
  5283. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5284. return ret;
  5285. }
  5286. //+ (NSString *)offline_getContactCarrier:(int)contact_id {
  5287. // // "val_227" : {
  5288. // // "check" : 1,
  5289. // // "value" : "US United States",
  5290. // // "value_id" : "228"
  5291. // // },
  5292. //
  5293. // NSString *Sql = [NSString stringWithFormat: @"select carrier from offline_contact where contact_id = %d;",contact_id];
  5294. // NSString * code = [iSalesDB jk_queryText:Sql];
  5295. //
  5296. // return [self offline_getCarrier:code];
  5297. //
  5298. //}
  5299. + (NSString *)offline_getCarrier:(int)code {
  5300. // "val_227" : {
  5301. // "check" : 1,
  5302. // "value" : "US United States",
  5303. // "value_id" : "228"
  5304. // },
  5305. NSString *Sql = [NSString stringWithFormat: @"select name from carrier where code_id = %d;",code];
  5306. NSString * ret = [iSalesDB jk_queryText:Sql];
  5307. return ret;
  5308. }
  5309. + (int )offline_getCarrierCode:(NSString*)name {
  5310. // "val_227" : {
  5311. // "check" : 1,
  5312. // "value" : "US United States",
  5313. // "value_id" : "228"
  5314. // },
  5315. NSString *Sql = [NSString stringWithFormat: @"select code from carrier where name = '%@';",name];
  5316. NSString * ret = [iSalesDB jk_queryText:Sql];
  5317. return [ret intValue];
  5318. }
  5319. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  5320. countryCode = [self translateSingleQuote:countryCode];
  5321. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  5322. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5323. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  5324. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  5325. if (name == NULL) {
  5326. name = "";
  5327. }
  5328. if (code == NULL) {
  5329. code = "";
  5330. }
  5331. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5332. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5333. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  5334. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5335. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  5336. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5337. }
  5338. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5339. [container setValue:stateDic forKey:key];
  5340. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  5341. DebugLog(@"query all state error: %@",err_msg);
  5342. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5343. // [stateDic setValue:@"Other" forKey:@"value"];
  5344. // [stateDic setValue:@"" forKey:@"value_id"];
  5345. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5346. //
  5347. // if (state_code && [@"" isEqualToString:state_code]) {
  5348. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5349. // }
  5350. //
  5351. // NSString *key = [NSString stringWithFormat:@"val_0"];
  5352. // [container setValue:stateDic forKey:key];
  5353. }] mutableCopy];
  5354. [ret removeObjectForKey:@"result"];
  5355. // failure 可以不用了,一样的
  5356. if (ret.allKeys.count == 0) {
  5357. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5358. [stateDic setValue:@"Other" forKey:@"value"];
  5359. [stateDic setValue:@"" forKey:@"value_id"];
  5360. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5361. if (state_code && [@"" isEqualToString:state_code]) {
  5362. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5363. }
  5364. NSString *key = [NSString stringWithFormat:@"val_0"];
  5365. [ret setValue:stateDic forKey:key];
  5366. }
  5367. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5368. return ret;
  5369. }
  5370. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  5371. codeId = [self translateSingleQuote:codeId];
  5372. 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];
  5373. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5374. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  5375. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  5376. if (name == NULL) {
  5377. name = "";
  5378. }
  5379. if (code == NULL) {
  5380. code = "";
  5381. }
  5382. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5383. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5384. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  5385. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5386. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  5387. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5388. }
  5389. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5390. [container setValue:stateDic forKey:key];
  5391. }] mutableCopy];
  5392. [ret removeObjectForKey:@"result"];
  5393. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5394. return ret;
  5395. }
  5396. + (NSDictionary *)offline_getPrice {
  5397. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price where is_show = 1;"];
  5398. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5399. char *name = (char *) sqlite3_column_text(stmt, 1);
  5400. int type = sqlite3_column_int(stmt, 2);
  5401. int orderBy = sqlite3_column_int(stmt, 3);
  5402. if (name == NULL) {
  5403. name = "";
  5404. }
  5405. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  5406. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5407. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  5408. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5409. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5410. if (orderBy == 0) {
  5411. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5412. }
  5413. [container setValue:priceDic forKey:key];
  5414. }] mutableCopy];
  5415. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5416. return ret;
  5417. }
  5418. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  5419. NSString *sql = @"select _id,type_name from contact_type where is_show = 1";
  5420. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  5421. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  5422. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5423. // int _id = sqlite3_column_int(stmt, 0);
  5424. NSString *name = [self textAtColumn:1 statement:stmt];
  5425. NSDictionary *typeDic = @{
  5426. @"value_id" : name,
  5427. @"value" : name,
  5428. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  5429. };
  5430. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  5431. [ret setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  5432. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  5433. }];
  5434. return ret;
  5435. }
  5436. + (NSDictionary *)offline_getSalesRep:(NSString*) user{
  5437. // 首先从offline_login表中取出sales_code
  5438. // AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5439. // NSString *user = app.user;
  5440. user = [self translateSingleQuote:user];
  5441. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  5442. __block NSString *user_code = @"";
  5443. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5444. char *code = (char *)sqlite3_column_text(stmt, 0);
  5445. if (code == NULL) {
  5446. code = "";
  5447. }
  5448. user_code = [NSString stringWithUTF8String:code];
  5449. }];
  5450. // 再取所有salesRep
  5451. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  5452. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5453. // 1 name 2 code 3 salesrep_id
  5454. char *name = (char *)sqlite3_column_text(stmt, 1);
  5455. char *code = (char *)sqlite3_column_text(stmt, 2);
  5456. int salesrep_id = sqlite3_column_int(stmt, 3);
  5457. if (name == NULL) {
  5458. name = "";
  5459. }
  5460. if (code == NULL) {
  5461. code = "";
  5462. }
  5463. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  5464. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  5465. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  5466. // 比较code 相等则check
  5467. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  5468. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5469. }
  5470. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  5471. }] mutableCopy];
  5472. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5473. return ret;
  5474. }
  5475. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  5476. zipcode = [self translateSingleQuote:zipcode];
  5477. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  5478. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5479. char *country = (char *)sqlite3_column_text(stmt, 0);
  5480. char *state = (char *)sqlite3_column_text(stmt, 1);
  5481. char *city = (char *)sqlite3_column_text(stmt, 2);
  5482. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  5483. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  5484. if (country == NULL) {
  5485. country = "";
  5486. }
  5487. if (state == NULL) {
  5488. state = "";
  5489. }
  5490. if (city == NULL) {
  5491. city = "";
  5492. }
  5493. if (country_code == NULL) {
  5494. country_code = "";
  5495. }
  5496. if (state_code == NULL) {
  5497. state_code = "";
  5498. }
  5499. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  5500. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  5501. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  5502. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  5503. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  5504. }] mutableCopy];
  5505. return ret;
  5506. }
  5507. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5508. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  5509. [item setValue:value forKey:valueKey];
  5510. [dic setValue:item forKey:itemKey];
  5511. }
  5512. + (NSString *)countryCodeByid:(NSString *)code_id {
  5513. NSString *ret = nil;
  5514. code_id = [self translateSingleQuote:code_id];
  5515. sqlite3 *db = [iSalesDB get_db];
  5516. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  5517. sqlite3_stmt * statement;
  5518. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5519. while (sqlite3_step(statement) == SQLITE_ROW) {
  5520. char *code = (char *)sqlite3_column_text(statement, 0);
  5521. if (code == NULL) {
  5522. code = "";
  5523. }
  5524. ret = [NSString stringWithUTF8String:code];
  5525. }
  5526. sqlite3_finalize(statement);
  5527. }
  5528. [iSalesDB close_db:db];
  5529. return ret;
  5530. }
  5531. + (NSString *)countryCodeIdByCode:(NSString *)code {
  5532. NSString *ret = nil;
  5533. code = [self translateSingleQuote:code];
  5534. sqlite3 *db = [iSalesDB get_db];
  5535. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  5536. sqlite3_stmt * statement;
  5537. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5538. while (sqlite3_step(statement) == SQLITE_ROW) {
  5539. char *_id = (char *)sqlite3_column_text(statement, 0);
  5540. if (_id == NULL) {
  5541. _id = "";
  5542. }
  5543. ret = [NSString stringWithFormat:@"%s",_id];
  5544. }
  5545. sqlite3_finalize(statement);
  5546. }
  5547. [iSalesDB close_db:db];
  5548. return ret;
  5549. }
  5550. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  5551. NSString *name = nil;
  5552. codeId = [self translateSingleQuote:codeId];
  5553. sqlite3 *db = [iSalesDB get_db];
  5554. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  5555. sqlite3_stmt * statement;
  5556. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5557. while (sqlite3_step(statement) == SQLITE_ROW) {
  5558. char *value = (char *)sqlite3_column_text(statement, 0);
  5559. if (value == NULL) {
  5560. value = "";
  5561. }
  5562. name = [NSString stringWithUTF8String:value];
  5563. }
  5564. sqlite3_finalize(statement);
  5565. }
  5566. [iSalesDB close_db:db];
  5567. return name;
  5568. }
  5569. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  5570. NSString *ret = nil;
  5571. sqlite3 *db = [iSalesDB get_db];
  5572. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  5573. sqlite3_stmt * statement;
  5574. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5575. while (sqlite3_step(statement) == SQLITE_ROW) {
  5576. char *name = (char *)sqlite3_column_text(statement, 0);
  5577. if (name == NULL) {
  5578. name = "";
  5579. }
  5580. ret = [NSString stringWithUTF8String:name];
  5581. }
  5582. sqlite3_finalize(statement);
  5583. }
  5584. [iSalesDB close_db:db];
  5585. return ret;
  5586. }
  5587. + (NSString *)salesRepCodeById:(NSString *)_id {
  5588. NSString *ret = nil;
  5589. _id = [self translateSingleQuote:_id];
  5590. sqlite3 *db = [iSalesDB get_db];
  5591. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  5592. sqlite3_stmt * statement;
  5593. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5594. while (sqlite3_step(statement) == SQLITE_ROW) {
  5595. char *rep = (char *)sqlite3_column_text(statement, 0);
  5596. if (rep == NULL) {
  5597. rep = "";
  5598. }
  5599. ret = [NSString stringWithUTF8String:rep];
  5600. }
  5601. sqlite3_finalize(statement);
  5602. }
  5603. [iSalesDB close_db:db];
  5604. return ret;
  5605. }
  5606. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  5607. char *tx = (char *)sqlite3_column_text(stmt, col);
  5608. if (tx == NULL) {
  5609. tx = "";
  5610. }
  5611. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  5612. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  5613. if (!text) {
  5614. text = @"";
  5615. }
  5616. // 将字符全部为' '的字符串干掉
  5617. int spaceCount = 0;
  5618. for (int i = 0; i < text.length; i++) {
  5619. if ([text characterAtIndex:i] == ' ') {
  5620. spaceCount++;
  5621. }
  5622. }
  5623. if (spaceCount == text.length) {
  5624. text = @"";
  5625. }
  5626. return text;
  5627. }
  5628. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  5629. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5630. NSData *data = [NSData dataWithContentsOfFile:path];
  5631. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5632. return ret;
  5633. }
  5634. + (NSString *)textFileName:(NSString *)name {
  5635. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5636. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  5637. if (!text) {
  5638. text = @"";
  5639. }
  5640. return text;
  5641. }
  5642. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  5643. return [[dic objectForKey:key] mutableCopy];
  5644. }
  5645. + (id)translateSingleQuote:(NSString *)string {
  5646. if ([string isKindOfClass:[NSString class]])
  5647. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  5648. return string;
  5649. }
  5650. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  5651. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  5652. }
  5653. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  5654. NSString* ret= nil;
  5655. NSString *sqlQuery = nil;
  5656. // 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
  5657. 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];
  5658. sqlite3_stmt * statement;
  5659. // int count=0;
  5660. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5661. {
  5662. if (sqlite3_step(statement) == SQLITE_ROW)
  5663. {
  5664. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  5665. if(imgurl==nil)
  5666. imgurl="";
  5667. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  5668. ret=nsimgurl;
  5669. }
  5670. sqlite3_finalize(statement);
  5671. }
  5672. else
  5673. {
  5674. [ret setValue:@"8" forKey:@"result"];
  5675. }
  5676. // [iSalesDB close_db:db];
  5677. // DebugLog(@"data string: %@",ret );
  5678. return ret;
  5679. }
  5680. #pragma mark contact Advanced search
  5681. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  5682. {
  5683. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  5684. return [RAConvertor dict2data:contactAdvanceDic];
  5685. }
  5686. #pragma mark create new contact
  5687. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  5688. assert(params[@"user"]!=nil);
  5689. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  5690. NSData *data = [NSData dataWithContentsOfFile:path];
  5691. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5692. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5693. NSString *countryCode = nil;
  5694. NSString *countryCode_id = nil;
  5695. NSString *stateCode = nil;
  5696. NSString *city = nil;
  5697. NSString *zipCode = nil;
  5698. // NSString *carrierCode = nil;
  5699. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5700. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  5701. NSString *code_id = params[@"country"];
  5702. countryCode_id = code_id;
  5703. countryCode = [self countryCodeByid:code_id];
  5704. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  5705. NSString *zip_code = params[@"zipcode"];
  5706. // 剔除全部为空格
  5707. int spaceCount = 0;
  5708. for (int i = 0; i < zip_code.length; i++) {
  5709. if ([zip_code characterAtIndex:i] == ' ') {
  5710. spaceCount++;
  5711. }
  5712. }
  5713. if (spaceCount == zip_code.length) {
  5714. zip_code = @"";
  5715. }
  5716. zipCode = zip_code;
  5717. if (zipCode.length > 0) {
  5718. countryCode_id = params[@"country"];
  5719. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  5720. countryCode = [dic valueForKey:@"country_code"];
  5721. if (!countryCode) {
  5722. // countryCode = @"US";
  5723. NSString *code_id = params[@"country"];
  5724. countryCode = [self countryCodeByid:code_id];
  5725. }
  5726. stateCode = [dic valueForKey:@"state_code"];
  5727. if (!stateCode.length) {
  5728. stateCode = params[@"state"];
  5729. }
  5730. city = [dic valueForKey:@"city"];
  5731. if (!city.length) {
  5732. city = params[@"city"];
  5733. }
  5734. // zip code
  5735. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  5736. [zipDic setValue:zipCode forKey:@"value"];
  5737. [section_0 setValue:zipDic forKey:@"item_11"];
  5738. } else {
  5739. NSString *code_id = params[@"country"];
  5740. countryCode = [self countryCodeByid:code_id];
  5741. stateCode = params[@"state"];
  5742. city = params[@"city"];
  5743. }
  5744. }
  5745. } else {
  5746. // default: US United States
  5747. countryCode = @"US";
  5748. countryCode_id = @"228";
  5749. }
  5750. // country
  5751. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5752. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  5753. // state
  5754. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5755. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  5756. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  5757. // city
  5758. if (city) {
  5759. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  5760. [cityDic setValue:city forKey:@"value"];
  5761. [section_0 setValue:cityDic forKey:@"item_13"];
  5762. }
  5763. // price type
  5764. NSDictionary *priceDic = [self offline_getPrice];
  5765. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  5766. // contact type
  5767. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  5768. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  5769. // Sales Rep
  5770. NSDictionary *repDic = [self offline_getSalesRep:params[@"user"]];
  5771. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5772. #ifdef BUILD_CONTRAST
  5773. //carrier
  5774. NSDictionary *carrierDic = [self offline_getAllCarrier:params[@"carrier"]];
  5775. [self setValue:carrierDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  5776. #endif
  5777. [ret setValue:section_0 forKey:@"section_0"];
  5778. return [RAConvertor dict2data:ret];
  5779. }
  5780. #pragma mark save
  5781. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  5782. NSString *addr = nil;
  5783. NSString *contact_name = nil;
  5784. // int carrier = [params[@"carrier"] intValue];
  5785. NSString *companyName = [params objectForKey:@"company"];
  5786. if (companyName) {
  5787. companyName = [AESCrypt fastencrypt:companyName];
  5788. } else {
  5789. companyName = @"";
  5790. }
  5791. DebugLog(@"company");
  5792. companyName = [self translateSingleQuote:companyName];
  5793. NSString *addr1 = [params objectForKey:@"address"];
  5794. NSString *addr2 = [params objectForKey:@"address2"];
  5795. NSString *addr3 = [params objectForKey:@"address_3"];
  5796. NSString *addr4 = [params objectForKey:@"address_4"];
  5797. if (!addr2) {
  5798. addr2 = @"";
  5799. }
  5800. if (!addr3) {
  5801. addr3 = @"";
  5802. }
  5803. if (!addr4) {
  5804. addr4 = @"";
  5805. }
  5806. if (!addr1) {
  5807. addr1 = @"";
  5808. }
  5809. DebugLog(@"addr");
  5810. addr = [RAConvertor arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  5811. addr = [AESCrypt fastencrypt:addr];
  5812. addr = [self translateSingleQuote:addr];
  5813. if (addr1 && ![addr1 isEqualToString:@""]) {
  5814. addr1 = [AESCrypt fastencrypt:addr1];
  5815. }
  5816. addr1 = [self translateSingleQuote:addr1];
  5817. addr2 = [self translateSingleQuote:addr2];
  5818. addr3 = [self translateSingleQuote:addr3];
  5819. addr4 = [self translateSingleQuote:addr4];
  5820. NSString *country = [params objectForKey:@"country"];
  5821. if (country) {
  5822. country = [self countryNameByCountryCodeId:country];
  5823. } else {
  5824. country = @"";
  5825. }
  5826. int carrier = [params[@"carrier"] intValue];
  5827. DebugLog(@"country");
  5828. country = [self translateSingleQuote:country];
  5829. NSString *state = [params objectForKey:@"state"];
  5830. if (!state) {
  5831. state = @"";
  5832. }
  5833. DebugLog(@"state");
  5834. state = [self translateSingleQuote:state];
  5835. NSString *city = [params objectForKey:@"city"];
  5836. if (!city) {
  5837. city = @"";
  5838. }
  5839. city = [self translateSingleQuote:city];
  5840. NSString *zipcode = [params objectForKey:@"zipcode"];
  5841. if (!zipcode) {
  5842. zipcode = @"";
  5843. }
  5844. DebugLog(@"zip");
  5845. zipcode = [self translateSingleQuote:zipcode];
  5846. NSString *fistName = [params objectForKey:@"firstname"];
  5847. if (!fistName) {
  5848. fistName = @"";
  5849. }
  5850. NSString *lastName = [params objectForKey:@"lastname"];
  5851. if (!lastName) {
  5852. lastName = @"";
  5853. }
  5854. contact_name = [RAConvertor arr2string:@[fistName,lastName] separator:@" " trim:true];
  5855. DebugLog(@"contact_name");
  5856. contact_name = [self translateSingleQuote:contact_name];
  5857. fistName = [self translateSingleQuote:fistName];
  5858. lastName = [self translateSingleQuote:lastName];
  5859. NSString *phone = [params objectForKey:@"phone"];
  5860. if (phone) {
  5861. phone = [AESCrypt fastencrypt:phone];
  5862. } else {
  5863. phone = @"";
  5864. }
  5865. DebugLog(@"PHONE");
  5866. phone = [self translateSingleQuote:phone];
  5867. NSString *fax = [params objectForKey:@"fax"];
  5868. if (!fax) {
  5869. fax = @"";
  5870. }
  5871. DebugLog(@"FAX");
  5872. fax = [self translateSingleQuote:fax];
  5873. NSString *email = [params objectForKey:@"email"];
  5874. if (!email) {
  5875. email = @"";
  5876. }
  5877. DebugLog(@"EMAIL:%@",email);
  5878. email = [self translateSingleQuote:email];
  5879. NSString *notes = [params objectForKey:@"contact_notes"];
  5880. if (!notes) {
  5881. notes = @"";
  5882. }
  5883. DebugLog(@"NOTE:%@",notes);
  5884. notes = [self translateSingleQuote:notes];
  5885. NSString *price = [params objectForKey:@"price_name"];
  5886. if (price) {
  5887. price = [self priceNameByPriceId:price];
  5888. } else {
  5889. price = @"";
  5890. }
  5891. DebugLog(@"PRICE");
  5892. price = [self translateSingleQuote:price];
  5893. NSString *salesRep = [params objectForKey:@"sales_rep"];
  5894. if (salesRep) {
  5895. salesRep = [self salesRepCodeById:salesRep];
  5896. } else {
  5897. salesRep = @"";
  5898. }
  5899. salesRep = [self translateSingleQuote:salesRep];
  5900. NSString *img = [params objectForKey:@"business_card"];
  5901. NSArray *array = [img componentsSeparatedByString:@","];
  5902. NSString *img_0 = array[0];
  5903. if (!img_0) {
  5904. img_0 = @"";
  5905. }
  5906. img_0 = [self translateSingleQuote:img_0];
  5907. NSString *img_1 = array[1];
  5908. if (!img_1) {
  5909. img_1 = @"";
  5910. }
  5911. img_1 = [self translateSingleQuote:img_1];
  5912. NSString *img_2 = array[2];
  5913. if (!img_2) {
  5914. img_2 = @"";
  5915. }
  5916. img_2 = [self translateSingleQuote:img_2];
  5917. NSString *contact_id = [NSUUID UUID].UUIDString;
  5918. NSString *contact_type = [params objectForKey:@"type_name"];
  5919. if (!contact_type) {
  5920. contact_type = @"";
  5921. }
  5922. contact_type = [self translateSingleQuote:contact_type];
  5923. // 判断更新时是否为customer
  5924. if (update) {
  5925. contact_id = [params objectForKey:@"contact_id"];
  5926. if (!contact_id) {
  5927. contact_id = @"";
  5928. }
  5929. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  5930. __block int customer = 0;
  5931. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5932. customer = sqlite3_column_int(stmt, 0);
  5933. }];
  5934. isCustomer = customer ? YES : NO;
  5935. }
  5936. NSMutableDictionary *sync_dic = [params mutableCopy];
  5937. if (isCustomer) {
  5938. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  5939. } else {
  5940. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  5941. }
  5942. sync_dic[@"truck_carrier"] = [self offline_getCarrier: [sync_dic[@"carrier"] intValue]];
  5943. NSString *sync_data = nil;
  5944. NSString *sql = nil;
  5945. if (update){
  5946. contact_id = [params objectForKey:@"contact_id"];
  5947. if (!contact_id) {
  5948. contact_id = @"";
  5949. }
  5950. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5951. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5952. sync_data = [RAConvertor dict2string:sync_dic];
  5953. sync_data = [self translateSingleQuote:sync_data];
  5954. 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];
  5955. } else {
  5956. contact_id = [self translateSingleQuote:contact_id];
  5957. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5958. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5959. sync_data = [RAConvertor dict2string:sync_dic];
  5960. sync_data = [self translateSingleQuote:sync_data];
  5961. 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];
  5962. }
  5963. int result = [iSalesDB execSql:sql];
  5964. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  5965. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  5966. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  5967. }
  5968. #pragma mark save new contact
  5969. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  5970. {
  5971. return [self offline_saveContact:params update:NO isCustomer:YES];
  5972. }
  5973. #pragma mark edit contact
  5974. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  5975. {
  5976. assert(params[@"user"]!=nil);
  5977. // {
  5978. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  5979. // password = 123456;
  5980. // user = EvanK;
  5981. // }
  5982. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  5983. NSData *data = [NSData dataWithContentsOfFile:path];
  5984. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5985. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5986. NSString *countryCode = nil;
  5987. NSString *countryCode_id = nil;
  5988. NSString *stateCode = nil;
  5989. __block int carrierCode = -1;
  5990. /*------contact infor------*/
  5991. __block NSString *country = nil;
  5992. __block NSString *company_name = nil;
  5993. __block NSString *contact_id = params[@"contact_id"];
  5994. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  5995. __block NSString *zipcode = nil;
  5996. __block NSString *state = nil; // state_code
  5997. __block NSString *city = nil; //
  5998. __block NSString *firt_name,*last_name;
  5999. __block NSString *phone,*fax,*email;
  6000. __block NSString *notes,*price_type,*sales_rep;
  6001. __block NSString *img_0,*img_1,*img_2;
  6002. __block NSString *contact_type;
  6003. contact_id = [self translateSingleQuote:contact_id];
  6004. 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];
  6005. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6006. country = [self textAtColumn:0 statement:stmt]; // country name
  6007. company_name = [self textAtColumn:1 statement:stmt];
  6008. addr_1 = [self textAtColumn:2 statement:stmt];
  6009. addr_2 = [self textAtColumn:3 statement:stmt];
  6010. addr_3 = [self textAtColumn:4 statement:stmt];
  6011. addr_4 = [self textAtColumn:5 statement:stmt];
  6012. zipcode = [self textAtColumn:6 statement:stmt];
  6013. state = [self textAtColumn:7 statement:stmt]; // state code
  6014. city = [self textAtColumn:8 statement:stmt];
  6015. firt_name = [self textAtColumn:9 statement:stmt];
  6016. last_name = [self textAtColumn:10 statement:stmt];
  6017. phone = [self textAtColumn:11 statement:stmt];
  6018. fax = [self textAtColumn:12 statement:stmt];
  6019. email = [self textAtColumn:13 statement:stmt];
  6020. notes = [self textAtColumn:14 statement:stmt];
  6021. price_type = [self textAtColumn:15 statement:stmt]; // name
  6022. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  6023. img_0 = [self textAtColumn:17 statement:stmt];
  6024. img_1 = [self textAtColumn:18 statement:stmt];
  6025. img_2 = [self textAtColumn:19 statement:stmt];
  6026. contact_type = [self textAtColumn:20 statement:stmt];
  6027. carrierCode = sqlite3_column_int(stmt, 21);
  6028. }];
  6029. // decrypt
  6030. if (company_name) {
  6031. company_name = [AESCrypt fastdecrypt:company_name];
  6032. }
  6033. if (addr_1) {
  6034. addr_1 = [AESCrypt fastdecrypt:addr_1];
  6035. }
  6036. if (phone) {
  6037. phone = [AESCrypt fastdecrypt:phone];
  6038. }
  6039. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  6040. countryCode = [iSalesDB jk_queryText:countrySql];
  6041. stateCode = state;
  6042. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  6043. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  6044. NSString *code_id = params[@"country"];
  6045. countryCode_id = code_id;
  6046. countryCode = [self countryCodeByid:code_id];
  6047. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  6048. NSString *zip_code = params[@"zipcode"];
  6049. // 剔除全部为空格
  6050. int spaceCount = 0;
  6051. for (int i = 0; i < zip_code.length; i++) {
  6052. if ([zip_code characterAtIndex:i] == ' ') {
  6053. spaceCount++;
  6054. }
  6055. }
  6056. if (spaceCount == zip_code.length) {
  6057. zip_code = @"";
  6058. }
  6059. if (zipcode.length > 0) {
  6060. zipcode = zip_code;
  6061. countryCode_id = params[@"country"];
  6062. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  6063. countryCode = [dic valueForKey:@"country_code"];
  6064. if (!countryCode) {
  6065. NSString *code_id = params[@"country"];
  6066. countryCode = [self countryCodeByid:code_id];
  6067. }
  6068. stateCode = [dic valueForKey:@"state_code"];
  6069. if (!stateCode.length) {
  6070. stateCode = params[@"state"];
  6071. }
  6072. city = [dic valueForKey:@"city"];
  6073. if (!city.length) {
  6074. city = params[@"city"];
  6075. }
  6076. // zip code
  6077. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  6078. [zipDic setValue:zipcode forKey:@"value"];
  6079. [section_0 setValue:zipDic forKey:@"item_8"];
  6080. } else {
  6081. NSString *code_id = params[@"country"];
  6082. countryCode = [self countryCodeByid:code_id];
  6083. stateCode = params[@"state"];
  6084. city = params[@"city"];
  6085. }
  6086. }
  6087. }
  6088. // 0 Country
  6089. // 1 Company Name
  6090. // 2 Contact ID
  6091. // 3 Picture
  6092. // 4 Address 1
  6093. // 5 Address 2
  6094. // 6 Address 3
  6095. // 7 Address 4
  6096. // 8 Zip Code
  6097. // 9 State/Province
  6098. // 10 City
  6099. // 11 Contact First Name
  6100. // 12 Contact Last Name
  6101. // 13 Phone
  6102. // 14 Fax
  6103. // 15 Email
  6104. // 16 Contact Notes
  6105. // 17 Price Type
  6106. // 18 Contact Type
  6107. // 19 Sales Rep
  6108. // country
  6109. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  6110. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  6111. // company
  6112. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  6113. // contact_id
  6114. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  6115. // picture
  6116. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  6117. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  6118. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  6119. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  6120. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  6121. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  6122. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  6123. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  6124. // addr 1 2 3 4
  6125. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  6126. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  6127. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  6128. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  6129. // zip code
  6130. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  6131. // state
  6132. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  6133. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  6134. // city
  6135. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  6136. // first last
  6137. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  6138. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  6139. // phone fax email
  6140. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  6141. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  6142. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  6143. // notes
  6144. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  6145. // price
  6146. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  6147. for (NSString *key in priceDic.allKeys) {
  6148. if ([key containsString:@"val_"]) {
  6149. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  6150. if ([dic[@"value"] isEqualToString:price_type]) {
  6151. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6152. [priceDic setValue:dic forKey:key];
  6153. }
  6154. }
  6155. }
  6156. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  6157. // Contact Rep
  6158. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  6159. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  6160. // Sales Rep
  6161. NSMutableDictionary *repDic = [[self offline_getSalesRep:params[@"user"]] mutableCopy];
  6162. for (NSString *key in repDic.allKeys) {
  6163. if ([key containsString:@"val_"]) {
  6164. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  6165. NSString *value = dic[@"value"];
  6166. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  6167. if (code && [code isEqualToString:sales_rep]) {
  6168. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6169. [repDic setValue:dic forKey:key];
  6170. }
  6171. }
  6172. }
  6173. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  6174. #ifdef BUILD_CONTRAST
  6175. //carrier
  6176. NSString* scarrier = [self offline_getCarrier:carrierCode];
  6177. NSDictionary *carrierDic = [self offline_getAllCarrier:scarrier];
  6178. [self setValue:carrierDic forItemKey:@"item_20" valueKey:@"cadedate" inDictionary:section_0];
  6179. #endif
  6180. [ret setValue:section_0 forKey:@"section_0"];
  6181. return [RAConvertor dict2data:ret];
  6182. }
  6183. #pragma mark save contact
  6184. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  6185. {
  6186. return [self offline_saveContact:params update:YES isCustomer:YES];
  6187. }
  6188. #pragma mark category
  6189. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  6190. if (ck) {
  6191. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  6192. for (NSString *key in res.allKeys) {
  6193. if (![key isEqualToString:@"count"]) {
  6194. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  6195. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  6196. if ([val[@"value"] isEqualToString:ck]) {
  6197. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6198. }
  6199. [res setValue:val forKey:key];
  6200. }
  6201. }
  6202. [dic setValue:res forKey:valueKey];
  6203. }
  6204. }
  6205. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  6206. // NSString* orderCode = [params valueForKey:@"orderCode"];
  6207. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6208. NSString* category = [params valueForKey:@"category"];
  6209. if (!category || [category isEqualToString:@""]) {
  6210. category = @"%";
  6211. }
  6212. category = [self translateSingleQuote:category];
  6213. int limit = [[params valueForKey:@"limit"] intValue];
  6214. int offset = [[params valueForKey:@"offset"] intValue];
  6215. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  6216. NSString *limit_str = @"";
  6217. if (limited) {
  6218. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  6219. }
  6220. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  6221. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  6222. sqlite3 *db = [iSalesDB get_db];
  6223. // [iSalesDB AddExFunction:db];
  6224. int count;
  6225. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  6226. 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];
  6227. double price_min = 0;
  6228. double price_max = 0;
  6229. if ([params.allKeys containsObject:@"alert"]) {
  6230. // alert
  6231. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  6232. NSString *alert = params[@"alert"];
  6233. if ([alert isEqualToString:@"Display All"]) {
  6234. alert = [NSString stringWithFormat:@""];
  6235. } else {
  6236. alert = [self translateSingleQuote:alert];
  6237. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  6238. }
  6239. // available
  6240. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  6241. NSString *available = params[@"available"];
  6242. NSString *available_condition;
  6243. if ([available isEqualToString:@"Display All"]) {
  6244. available_condition = @"";
  6245. } else if ([available isEqualToString:@"Available Now"]) {
  6246. available_condition = @"and availability > 0";
  6247. } else {
  6248. available_condition = @"and availability == 0";
  6249. }
  6250. // best seller
  6251. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  6252. NSString *best_seller = @"";
  6253. NSString *order_best_seller = @"m.name asc";
  6254. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  6255. best_seller = @"and best_seller > 0";
  6256. order_best_seller = @"m.best_seller desc,m.name asc";
  6257. }
  6258. // price
  6259. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  6260. NSString *price = params[@"price"];
  6261. price_min = 0;
  6262. price_max = MAXFLOAT;
  6263. if (params[@"user"] && price != nil) {
  6264. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  6265. NSMutableString *priceName = [NSMutableString string];
  6266. for (int i = 0; i < priceTypeArray.count; i++) {
  6267. NSString *pricetype = priceTypeArray[i];
  6268. pricetype = [self translateSingleQuote:pricetype];
  6269. if (i == 0) {
  6270. [priceName appendFormat:@"'%@'",pricetype];
  6271. } else {
  6272. [priceName appendFormat:@",'%@'",pricetype];
  6273. }
  6274. }
  6275. if ([price isEqualToString:@"Display All"]) {
  6276. price = [NSString stringWithFormat:@""];
  6277. } else if([price containsString:@"+"]){
  6278. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  6279. price_min = [price doubleValue];
  6280. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  6281. } else {
  6282. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  6283. price_min = [[priceArray objectAtIndex:0] doubleValue];
  6284. price_max = [[priceArray objectAtIndex:1] doubleValue];
  6285. 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];
  6286. }
  6287. } else {
  6288. price = @"";
  6289. }
  6290. // sold_by_qty : Sold in quantities of %@
  6291. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  6292. NSString *qty = params[@"sold_by_qty"];
  6293. if ([qty isEqualToString:@"Display All"]) {
  6294. qty = @"";
  6295. } else {
  6296. qty = [self translateSingleQuote:qty];
  6297. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  6298. }
  6299. // model name;
  6300. NSString* modelname =params[@"modelName"];
  6301. if(modelname.length==0)
  6302. {
  6303. modelname=@"";
  6304. }
  6305. else
  6306. {
  6307. modelname = modelname.lowercaseString;
  6308. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  6309. }
  6310. //modelDescription
  6311. NSString* modelDescription =params[@"modelDescription"];
  6312. if(modelDescription.length==0)
  6313. {
  6314. modelDescription=@"";
  6315. }
  6316. else
  6317. {
  6318. modelDescription = modelDescription.lowercaseString;
  6319. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  6320. }
  6321. // cate
  6322. // category = [self translateSingleQuote:category];
  6323. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  6324. // cate mutiple selection
  6325. NSString *category_id = params[@"category"];
  6326. NSMutableArray *cate_id_array = nil;
  6327. NSMutableString *cateWhere = [NSMutableString string];
  6328. if ([category_id isEqualToString:@""] || !category_id) {
  6329. [cateWhere appendString:@"1 = 1"];
  6330. } else {
  6331. if ([category_id containsString:@","]) {
  6332. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  6333. } else {
  6334. cate_id_array = [@[category_id] mutableCopy];
  6335. }
  6336. [cateWhere appendString:@"("];
  6337. for (int i = 0; i < cate_id_array.count; i++) {
  6338. if (i == 0) {
  6339. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6340. } else {
  6341. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6342. }
  6343. }
  6344. [cateWhere appendString:@")"];
  6345. }
  6346. // where bestseller > 0 order by bestseller desc
  6347. // sql query: alert availability(int) best_seller(int) price qty
  6348. 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];
  6349. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  6350. }
  6351. DebugLog(@"offline category where: %@",where);
  6352. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  6353. if (!params[@"user"]) {
  6354. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  6355. }
  6356. [ret setValue:filter forKey:@"filter"];
  6357. DebugLog(@"offline_category sql:%@",sqlQuery);
  6358. sqlite3_stmt * statement;
  6359. [ret setValue:@"2" forKey:@"result"];
  6360. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  6361. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  6362. // int count=0;
  6363. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6364. {
  6365. int i=0;
  6366. while (sqlite3_step(statement) == SQLITE_ROW)
  6367. {
  6368. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  6369. char *name = (char*)sqlite3_column_text(statement, 0);
  6370. if(name==nil)
  6371. name="";
  6372. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  6373. char *description = (char*)sqlite3_column_text(statement, 1);
  6374. if(description==nil)
  6375. description="";
  6376. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  6377. int product_id = sqlite3_column_int(statement, 2);
  6378. int wid = sqlite3_column_int(statement, 3);
  6379. int closeout = sqlite3_column_int(statement, 4);
  6380. int cid = sqlite3_column_int(statement, 5);
  6381. int wisdelete = sqlite3_column_int(statement, 6);
  6382. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  6383. int more_color = sqlite3_column_int(statement, 8);
  6384. // Defaul Category ID
  6385. __block NSString *categoryID = nil;
  6386. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  6387. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6388. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  6389. if(default_category==nil)
  6390. default_category="";
  6391. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  6392. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  6393. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  6394. categoryID = nsdefault_category;
  6395. }];
  6396. if (!categoryID.length) {
  6397. NSString *cateIDs = params[@"category"];
  6398. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  6399. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  6400. for (NSString *cateID in requestCategoryArr) {
  6401. BOOL needBreak = NO;
  6402. for (NSString *itemCateIDBox in itemCategoryArr) {
  6403. if (itemCateIDBox.length > 4) {
  6404. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  6405. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  6406. if ([itemCategoryID isEqualToString:cateID]) {
  6407. needBreak = YES;
  6408. categoryID = itemCategoryID;
  6409. break;
  6410. }
  6411. }
  6412. }
  6413. if (needBreak) {
  6414. break;
  6415. }
  6416. }
  6417. }
  6418. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  6419. if(wid !=0 && wisdelete != 1)
  6420. [item setValue:@"true" forKey:@"wish_exists"];
  6421. else
  6422. [item setValue:@"false" forKey:@"wish_exists"];
  6423. if(closeout==0)
  6424. [item setValue:@"false" forKey:@"is_closeout"];
  6425. else
  6426. [item setValue:@"true" forKey:@"is_closeout"];
  6427. if(cid==0)
  6428. [item setValue:@"false" forKey:@"cart_exists"];
  6429. else
  6430. [item setValue:@"true" forKey:@"cart_exists"];
  6431. if (more_color == 0) {
  6432. [item setObject:@(false) forKey:@"more_color"];
  6433. } else if (more_color == 1) {
  6434. [item setObject:@(true) forKey:@"more_color"];
  6435. }
  6436. [item addEntriesFromDictionary:imgjson];
  6437. // [item setValue:nsurl forKey:@"img"];
  6438. [item setValue:nsname forKey:@"name"];
  6439. [item setValue:nsdescription forKey:@"description"];
  6440. if (categoryID) {
  6441. [item setValue:categoryID forKey:@"item_category_id"];
  6442. }
  6443. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  6444. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  6445. i++;
  6446. }
  6447. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  6448. [ret setObject:items forKey:@"items"];
  6449. sqlite3_finalize(statement);
  6450. } else {
  6451. DebugLog(@"nothing...");
  6452. }
  6453. DebugLog(@"count:%d",count);
  6454. [iSalesDB close_db:db];
  6455. #ifdef DEBUG
  6456. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  6457. #endif
  6458. return ret;
  6459. }
  6460. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  6461. {
  6462. return [self categoryList:params limited:YES];
  6463. }
  6464. # pragma mark item search
  6465. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  6466. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6467. // assert(params[@"order_code"]);
  6468. // params[@"user"]
  6469. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  6470. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  6471. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  6472. // category
  6473. NSDictionary *category_menu = [self offline_category_menu];
  6474. [filter setValue:category_menu forKey:@"category"];
  6475. NSString* where= nil;
  6476. NSString* orderby= @"m.name";
  6477. if (!filterSearch) {
  6478. int covertype = [[params valueForKey:@"covertype"] intValue];
  6479. switch (covertype) {
  6480. case 0:
  6481. {
  6482. where=@"m.category like'%%#005#%%'";
  6483. break;
  6484. }
  6485. case 1:
  6486. {
  6487. where=@"m.alert like '%QS%'";
  6488. break;
  6489. }
  6490. case 2:
  6491. {
  6492. where=@"m.availability>0";
  6493. break;
  6494. }
  6495. case 3:
  6496. {
  6497. where=@"m.best_seller>0";
  6498. orderby=@"m.best_seller desc,m.name asc";
  6499. break;
  6500. }
  6501. default:
  6502. where=@"1=1";
  6503. break;
  6504. }
  6505. }
  6506. int limit = [[params valueForKey:@"limit"] intValue];
  6507. int offset = [[params valueForKey:@"offset"] intValue];
  6508. NSString *limit_str = @"";
  6509. if (limited) {
  6510. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  6511. }
  6512. sqlite3 *db = [iSalesDB get_db];
  6513. // [iSalesDB AddExFunction:db];
  6514. int count;
  6515. NSString *sqlQuery = nil;
  6516. where = [where stringByAppendingString:@" and m.is_active = 1"];
  6517. 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];
  6518. double price_min = 0;
  6519. double price_max = 0;
  6520. if (filterSearch) {
  6521. // alert
  6522. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  6523. NSString *alert = params[@"alert"];
  6524. if ([alert isEqualToString:@"Display All"]) {
  6525. alert = [NSString stringWithFormat:@""];
  6526. } else {
  6527. alert = [self translateSingleQuote:alert];
  6528. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  6529. }
  6530. // available
  6531. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  6532. NSString *available = params[@"available"];
  6533. NSString *available_condition;
  6534. if ([available isEqualToString:@"Display All"]) {
  6535. available_condition = @"";
  6536. } else if ([available isEqualToString:@"Available Now"]) {
  6537. available_condition = @"and availability > 0";
  6538. } else {
  6539. available_condition = @"and availability == 0";
  6540. }
  6541. // best seller
  6542. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  6543. NSString *best_seller = @"";
  6544. NSString *order_best_seller = @"m.name asc";
  6545. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  6546. best_seller = @"and best_seller > 0";
  6547. order_best_seller = @"m.best_seller desc,m.name asc";
  6548. }
  6549. // price
  6550. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  6551. NSString *price = params[@"price"];
  6552. price_min = 0;
  6553. price_max = MAXFLOAT;
  6554. if (params[@"user"]) {
  6555. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  6556. NSMutableString *priceName = [NSMutableString string];
  6557. for (int i = 0; i < priceTypeArray.count; i++) {
  6558. NSString *pricetype = priceTypeArray[i];
  6559. pricetype = [self translateSingleQuote:pricetype];
  6560. if (i == 0) {
  6561. [priceName appendFormat:@"'%@'",pricetype];
  6562. } else {
  6563. [priceName appendFormat:@",'%@'",pricetype];
  6564. }
  6565. }
  6566. if ([price isEqualToString:@"Display All"]) {
  6567. price = [NSString stringWithFormat:@""];
  6568. } else if([price containsString:@"+"]){
  6569. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  6570. price_min = [price doubleValue];
  6571. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  6572. } else {
  6573. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  6574. price_min = [[priceArray objectAtIndex:0] doubleValue];
  6575. price_max = [[priceArray objectAtIndex:1] doubleValue];
  6576. 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];
  6577. }
  6578. } else {
  6579. price = @"";
  6580. }
  6581. // sold_by_qty : Sold in quantities of %@
  6582. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  6583. NSString *qty = params[@"sold_by_qty"];
  6584. if ([qty isEqualToString:@"Display All"]) {
  6585. qty = @"";
  6586. } else {
  6587. qty = [self translateSingleQuote:qty];
  6588. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  6589. }
  6590. // model name;
  6591. NSString* modelname =params[@"modelName"];
  6592. if(modelname.length==0)
  6593. {
  6594. modelname=@"";
  6595. }
  6596. else
  6597. {
  6598. modelname = modelname.lowercaseString;
  6599. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  6600. }
  6601. //modelDescription
  6602. NSString* modelDescription =params[@"modelDescription"];
  6603. if(modelDescription.length==0)
  6604. {
  6605. modelDescription=@"";
  6606. }
  6607. else
  6608. {
  6609. modelDescription = modelDescription.lowercaseString;
  6610. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  6611. }
  6612. //lower(description) like'%%%@%%'
  6613. // category
  6614. NSString *category_id = params[@"ctgId"];
  6615. NSMutableArray *cate_id_array = nil;
  6616. NSMutableString *cateWhere = [NSMutableString string];
  6617. if ([category_id isEqualToString:@""] || !category_id) {
  6618. [cateWhere appendString:@"1 = 1"];
  6619. } else {
  6620. if ([category_id containsString:@","]) {
  6621. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  6622. } else {
  6623. cate_id_array = [@[category_id] mutableCopy];
  6624. }
  6625. /*-----------*/
  6626. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  6627. [cateWhere appendString:@"("];
  6628. for (int i = 0; i < cate_id_array.count; i++) {
  6629. for (NSString *key0 in cateDic.allKeys) {
  6630. if ([key0 containsString:@"category_"]) {
  6631. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  6632. for (NSString *key1 in category0.allKeys) {
  6633. if ([key1 containsString:@"category_"]) {
  6634. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  6635. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  6636. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6637. cate_id_array[i] = [category1 objectForKey:@"id"];
  6638. if (i == 0) {
  6639. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6640. } else {
  6641. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6642. }
  6643. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6644. [category0 setValue:category1 forKey:key1];
  6645. [cateDic setValue:category0 forKey:key0];
  6646. }
  6647. } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  6648. if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6649. cate_id_array[i] = [category0 objectForKey:@"id"];
  6650. if (i == 0) {
  6651. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6652. } else {
  6653. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6654. }
  6655. [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6656. [cateDic setValue:category0 forKey:key0];
  6657. }
  6658. }
  6659. }
  6660. }
  6661. }
  6662. }
  6663. [cateWhere appendString:@")"];
  6664. [filter setValue:cateDic forKey:@"category"];
  6665. }
  6666. // where bestseller > 0 order by bestseller desc
  6667. // sql query: alert availability(int) best_seller(int) price qty
  6668. 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];
  6669. // count
  6670. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,modelname,modelDescription,price];
  6671. }
  6672. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  6673. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  6674. if (!params[@"user"]) {
  6675. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  6676. }
  6677. [ret setValue:filter forKey:@"filter"];
  6678. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  6679. sqlite3_stmt * statement;
  6680. [ret setValue:@"2" forKey:@"result"];
  6681. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  6682. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  6683. // int count=0;
  6684. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6685. {
  6686. int i=0;
  6687. while (sqlite3_step(statement) == SQLITE_ROW)
  6688. {
  6689. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  6690. char *name = (char*)sqlite3_column_text(statement, 0);
  6691. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  6692. char *description = (char*)sqlite3_column_text(statement, 1);
  6693. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  6694. int product_id = sqlite3_column_int(statement, 2);
  6695. int wid = sqlite3_column_int(statement, 3);
  6696. int closeout = sqlite3_column_int(statement, 4);
  6697. int cid = sqlite3_column_int(statement, 5);
  6698. int wisdelete = sqlite3_column_int(statement, 6);
  6699. int more_color = sqlite3_column_int(statement, 7);
  6700. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  6701. if(wid !=0 && wisdelete != 1)
  6702. [item setValue:@"true" forKey:@"wish_exists"];
  6703. else
  6704. [item setValue:@"false" forKey:@"wish_exists"];
  6705. if(closeout==0)
  6706. [item setValue:@"false" forKey:@"is_closeout"];
  6707. else
  6708. [item setValue:@"true" forKey:@"is_closeout"];
  6709. if(cid==0)
  6710. [item setValue:@"false" forKey:@"cart_exists"];
  6711. else
  6712. [item setValue:@"true" forKey:@"cart_exists"];
  6713. if (more_color == 0) {
  6714. [item setObject:@(false) forKey:@"more_color"];
  6715. } else if (more_color == 1) {
  6716. [item setObject:@(true) forKey:@"more_color"];
  6717. }
  6718. [item addEntriesFromDictionary:imgjson];
  6719. // [item setValue:nsurl forKey:@"img"];
  6720. [item setValue:nsname forKey:@"fash_name"];
  6721. [item setValue:nsdescription forKey:@"description"];
  6722. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  6723. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  6724. i++;
  6725. }
  6726. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  6727. [ret setObject:items forKey:@"items"];
  6728. sqlite3_finalize(statement);
  6729. }
  6730. [iSalesDB close_db:db];
  6731. #ifdef DEBUG
  6732. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  6733. #endif
  6734. return ret;
  6735. }
  6736. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  6737. {
  6738. return [self itemsearch:params limited:YES];
  6739. }
  6740. #pragma mark order detail
  6741. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  6742. if (str1.length == 0) {
  6743. str1 = @"&nbsp";
  6744. }
  6745. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  6746. return str;
  6747. }
  6748. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  6749. {
  6750. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  6751. [fromformatter setDateFormat:from];
  6752. NSDate *date = [fromformatter dateFromString:datetime];
  6753. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  6754. [toformatter setDateFormat:to];
  6755. NSString * ret = [toformatter stringFromDate:date];
  6756. return ret;
  6757. }
  6758. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  6759. // 把毫秒去掉
  6760. if ([dateTime containsString:@"."]) {
  6761. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  6762. }
  6763. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  6764. formatter.dateFormat = formate;
  6765. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  6766. NSDate *date = [formatter dateFromString:dateTime];
  6767. formatter.dateFormat = newFormate;
  6768. NSString *result = [formatter stringFromDate:date];
  6769. return result ? result : @"";
  6770. }
  6771. + (NSString *)rchangeDateFormate:(NSString *)dateTime {
  6772. return [self changeDateTime:dateTime Formate:@"MM/dd/yyyy" withFormate:@"yyyy-MM-dd"];
  6773. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6774. }
  6775. + (NSString *)changeDateFormate:(NSString *)dateTime {
  6776. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd" withFormate:@"MM/dd/yyyy"];
  6777. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6778. }
  6779. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  6780. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  6781. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6782. }
  6783. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  6784. {
  6785. assert(params[@"mode"]!=nil);
  6786. assert(params[@"user"]!=nil);
  6787. DebugLog(@"offline oderdetail params: %@",params);
  6788. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6789. int orderId = [params[@"orderId"] intValue];
  6790. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  6791. // decrypt card number and card security code
  6792. // 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 ];
  6793. 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];
  6794. sqlite3 *db = [iSalesDB get_db];
  6795. sqlite3_stmt * statement;
  6796. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  6797. NSString *nssoid = @"";
  6798. NSString* orderinfo = @"";
  6799. NSString *moreInfo = @"";
  6800. double handlingFee = 0;
  6801. double payments_and_credist = 0;
  6802. double totalPrice = 0;
  6803. double shippingFee = 0;
  6804. double lift_gate = 0;
  6805. NSString *customer_contact = @"";
  6806. NSString *customer_email = @"";
  6807. NSString *customer_fax = @"";
  6808. NSString *customer_phone = @"";
  6809. NSString *customer_city = @"";
  6810. NSString *customer_state = @"";
  6811. NSString *customer_zipcode = @"";
  6812. NSString *customer_country = @"";
  6813. BOOL must_call = NO;
  6814. double discount = 0;
  6815. __block NSMutableDictionary *section_0 = [NSMutableDictionary dictionary];
  6816. __block NSMutableDictionary *section_1 = [NSMutableDictionary dictionary];
  6817. __block NSMutableDictionary *section_2 = [NSMutableDictionary dictionary];
  6818. __block NSMutableDictionary *section_3 = [NSMutableDictionary dictionary];
  6819. __block NSMutableDictionary *section_4 = [NSMutableDictionary dictionary];
  6820. __block NSMutableDictionary *price_data = [NSMutableDictionary dictionary];
  6821. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  6822. {
  6823. if (sqlite3_step(statement) == SQLITE_ROW)
  6824. {
  6825. // int order_id = sqlite3_column_int(statement, 0);
  6826. NSString *sign_url = [self textAtColumn:51 statement:statement];
  6827. // ret[@"sign_url"] = sign_url;
  6828. section_1 = @{
  6829. @"data":sign_url,
  6830. @"title":@"Signature",
  6831. @"type":@"sign_url"
  6832. }.mutableCopy;
  6833. [ret setObject:section_1 forKey:@"section_1"];
  6834. customer_contact = [self textAtColumn:52 statement:statement];
  6835. customer_email = [self textAtColumn:53 statement:statement];
  6836. customer_phone = [self textAtColumn:54 statement:statement];
  6837. customer_fax = [self textAtColumn:55 statement:statement];
  6838. customer_city = [self textAtColumn:60 statement:statement];
  6839. customer_state = [self textAtColumn:61 statement:statement];
  6840. customer_zipcode = [self textAtColumn:62 statement:statement];
  6841. customer_country = [self textAtColumn:63 statement:statement];
  6842. must_call = [[self textAtColumn:64 statement:statement] boolValue];
  6843. discount =sqlite3_column_double(statement, 65);
  6844. int offline_edit=sqlite3_column_int(statement, 56);
  6845. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  6846. char *soid = (char*)sqlite3_column_text(statement, 1);
  6847. if(soid==nil)
  6848. soid= "";
  6849. nssoid= [[NSString alloc]initWithUTF8String:soid];
  6850. // so#
  6851. ret[@"so#"] = nssoid;
  6852. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  6853. if(poNumber==nil)
  6854. poNumber= "";
  6855. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  6856. char *create_time = (char*)sqlite3_column_text(statement, 3);
  6857. if(create_time==nil)
  6858. create_time= "";
  6859. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  6860. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  6861. int status = sqlite3_column_int(statement, 4);
  6862. int erpStatus = sqlite3_column_int(statement, 49);
  6863. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  6864. // status
  6865. if (status > 1 && status != 3) {
  6866. status = erpStatus;
  6867. } else if (status == 3) {
  6868. status = 15;
  6869. }
  6870. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  6871. ret[@"order_status"] = nsstatus;
  6872. char *company_name = (char*)sqlite3_column_text(statement, 5);
  6873. if(company_name==nil)
  6874. company_name= "";
  6875. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  6876. // company name
  6877. ret[@"company_name"] = nscompany_name;
  6878. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  6879. if(customer_contact==nil)
  6880. customer_contact= "";
  6881. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  6882. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  6883. if(addr_1==nil)
  6884. addr_1="";
  6885. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  6886. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  6887. if(addr_2==nil)
  6888. addr_2="";
  6889. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  6890. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  6891. if(addr_3==nil)
  6892. addr_3="";
  6893. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  6894. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  6895. if(addr_4==nil)
  6896. addr_4="";
  6897. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  6898. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  6899. [arr_addr addObject:nsaddr_1];
  6900. [arr_addr addObject:nsaddr_2];
  6901. [arr_addr addObject:nsaddr_3];
  6902. [arr_addr addObject:nsaddr_4];
  6903. [arr_addr addObject:[NSString stringWithFormat:@"%@,",customer_city]];
  6904. [arr_addr addObject:customer_state];
  6905. [arr_addr addObject:customer_zipcode];
  6906. [arr_addr addObject:customer_country];
  6907. // NSString * customer_address = [RAConvertor arr2string:arr_addr separator:@" " trim:true];
  6908. NSString *customer_address = [NSString stringWithFormat:@"%@<br/>%@, %@ %@ %@",nsaddr_1,customer_city,customer_state,customer_zipcode,customer_country];
  6909. char *logist = (char*)sqlite3_column_text(statement, 11);
  6910. if(logist==nil)
  6911. logist= "";
  6912. NSString *DebugLogist=[[NSString alloc]initWithUTF8String:logist];
  6913. if (/*status == -11 || */status == 10 || status == 11) {
  6914. DebugLogist = [self textAtColumn:59 statement:statement];
  6915. };
  6916. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  6917. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  6918. shipping = @"Shipping To Be Quoted";
  6919. } else {
  6920. shippingFee = sqlite3_column_double(statement, 12);
  6921. }
  6922. // Shipping
  6923. // ret[@"Shipping"] = shipping;
  6924. NSDictionary *shipping_item = @{
  6925. @"title":@"Shipping",
  6926. @"value":shipping
  6927. };
  6928. [price_data setObject:shipping_item forKey:@"item_1"];
  6929. int have_lift_gate = sqlite3_column_int(statement, 17);
  6930. lift_gate = sqlite3_column_double(statement, 13);
  6931. // Liftgate Fee(No loading dock)
  6932. if (!have_lift_gate) {
  6933. lift_gate = 0;
  6934. }
  6935. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  6936. NSString *liftgate_value = [NSString stringWithFormat:@"$%.2f",lift_gate];
  6937. if (sqlite3_column_int(statement, 57)) {
  6938. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  6939. liftgate_value = @"Shipping To Be Quoted";
  6940. }
  6941. NSDictionary *liftgate_item = @{
  6942. @"title":@"Liftgate Fee(No loading dock)",
  6943. @"value":liftgate_value
  6944. };
  6945. [price_data setObject:liftgate_item forKey:@"item_2"];
  6946. // if ([DebugLogist isEqualToString:@"WILL CALL"]) {
  6947. // [ret removeObjectForKey:@"Shipping"];
  6948. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  6949. // }
  6950. //
  6951. // if (have_lift_gate) {
  6952. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  6953. // }
  6954. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  6955. if(general_notes==nil)
  6956. general_notes= "";
  6957. NSString *nsgeneral_notes= [NSString stringWithFormat:@"MUST MAKE APPOINTMENT BEFORE DELIVERY = %@;<br/>%@",must_call ? @"YES" : @"NO",[[NSString alloc]initWithUTF8String:general_notes]];
  6958. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  6959. if(internal_notes==nil)
  6960. internal_notes= "";
  6961. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  6962. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  6963. if(payment_type==nil)
  6964. payment_type= "";
  6965. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  6966. // order info
  6967. orderinfo = [self textFileName:@"order_info.html"];
  6968. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  6969. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  6970. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  6971. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  6972. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  6973. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  6974. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  6975. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:DebugLogist];
  6976. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  6977. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  6978. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  6979. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  6980. NSString *payment = nil;
  6981. // id -> show
  6982. __block NSString *show_pay_type = nspayment_type;
  6983. [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) {
  6984. char *show_typ_ch = (char*)sqlite3_column_text(stmt, 0);
  6985. if (show_typ_ch != NULL) {
  6986. show_pay_type = [NSString stringWithUTF8String:show_typ_ch];
  6987. }
  6988. }];
  6989. if([nspayment_type isEqualToString:@"Credit Card"] || [nspayment_type isEqualToString:@"Visa/Master"])
  6990. {
  6991. payment = [self textFileName:@"creditcardpayment.html"];
  6992. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  6993. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  6994. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  6995. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  6996. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  6997. NSString *card_type = [self textAtColumn:42 statement:statement];
  6998. if (card_type.length > 0) { // 显示星号
  6999. card_type = @"****";
  7000. }
  7001. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  7002. if (card_number.length > 0 && card_number.length > 4) {
  7003. for (int i = 0; i < card_number.length - 4; i++) {
  7004. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  7005. }
  7006. } else {
  7007. card_number = @"";
  7008. }
  7009. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  7010. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  7011. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  7012. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  7013. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  7014. card_expiration = @"****";
  7015. }
  7016. NSString *card_city = [self textAtColumn:46 statement:statement];
  7017. NSString *card_state = [self textAtColumn:47 statement:statement];
  7018. payment = [self replaceHtml:payment String:@"Payment_Type" withString:show_pay_type];
  7019. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  7020. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  7021. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  7022. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  7023. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  7024. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  7025. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  7026. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  7027. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  7028. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  7029. }
  7030. else
  7031. {
  7032. payment=[self textFileName:@"normalpayment.html"];
  7033. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  7034. }
  7035. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  7036. ret[@"result"]= [NSNumber numberWithInt:2];
  7037. // more info
  7038. moreInfo = [self textFileName:@"more_info.html"];
  7039. /*****ship to******/
  7040. // ShipToCompany_or_&nbsp
  7041. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  7042. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  7043. NSString *shipToName = [self textAtColumn:19 statement:statement];
  7044. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  7045. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  7046. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7047. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"]; // 手动输入的可能是\r 或 \n
  7048. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7049. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  7050. /*****ship from******/
  7051. // ShipFromCompany_or_&nbsp
  7052. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  7053. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  7054. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  7055. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  7056. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  7057. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7058. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7059. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7060. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  7061. /*****freight to******/
  7062. // FreightBillToCompany_or_&nbsp
  7063. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  7064. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  7065. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  7066. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  7067. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  7068. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7069. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7070. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7071. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  7072. /*****merchandise to******/
  7073. // MerchandiseBillToCompany_or_&nbsp
  7074. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  7075. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  7076. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  7077. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  7078. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  7079. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7080. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7081. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7082. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  7083. /*****return to******/
  7084. // ReturnToCompany_or_&nbsp
  7085. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  7086. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  7087. NSString *returnToName = [self textAtColumn:31 statement:statement];
  7088. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  7089. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  7090. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7091. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7092. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7093. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  7094. //
  7095. // DebugLog(@"more info : %@",moreInfo);
  7096. // handling fee
  7097. handlingFee = sqlite3_column_double(statement, 33);
  7098. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  7099. NSString *handling_fee_value = [NSString stringWithFormat:@"$%.2f",handlingFee];
  7100. if (sqlite3_column_int(statement, 58)) {
  7101. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  7102. handling_fee_value = @"Shipping To Be Quoted";
  7103. }
  7104. NSDictionary *handling_fee_item = @{
  7105. @"title":@"Handling Fee",
  7106. @"value":handling_fee_value
  7107. };
  7108. [price_data setObject:handling_fee_item forKey:@"item_3"];
  7109. //
  7110. // customer info
  7111. customerID = [self textAtColumn:36 statement:statement];
  7112. // mode
  7113. ret[@"mode"] = params[@"mode"];
  7114. // model_count
  7115. ret[@"model_count"] = @(0);
  7116. }
  7117. sqlite3_finalize(statement);
  7118. }
  7119. [iSalesDB close_db:db];
  7120. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID,@"mode":params[@"mode"]} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  7121. // "customer_email" = "Shui Hu";
  7122. // "customer_fax" = "";
  7123. // "customer_first_name" = F;
  7124. // "customer_last_name" = L;
  7125. // "customer_name" = ",da He Xiang Dong Liu A";
  7126. // "customer_phone" = "Hey Xuan Feng";
  7127. contactInfo[@"customer_phone"] = customer_phone;
  7128. contactInfo[@"customer_fax"] = customer_fax;
  7129. contactInfo[@"customer_email"] = customer_email;
  7130. NSString *first_name = @"";
  7131. NSString *last_name = @"";
  7132. if ([customer_contact isEqualToString:@""]) {
  7133. } else if ([customer_contact containsString:@" "]) {
  7134. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  7135. first_name = [customer_contact substringToIndex:first_space_index];
  7136. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  7137. }
  7138. contactInfo[@"customer_first_name"] = first_name;
  7139. contactInfo[@"customer_last_name"] = last_name;
  7140. ret[@"customerInfo"] = contactInfo;
  7141. // models
  7142. if (nssoid) {
  7143. __block NSMutableDictionary *model_data = [NSMutableDictionary dictionary];
  7144. __block double TotalCuft = 0;
  7145. __block double TotalWeight = 0;
  7146. __block int TotalCarton = 0;
  7147. __block double allItemPrice = 0;
  7148. 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];
  7149. sqlite3 *db1 = [iSalesDB get_db];
  7150. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7151. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  7152. int product_id = sqlite3_column_int(stmt, 0);
  7153. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  7154. int item_id = sqlite3_column_int(stmt, 7);
  7155. NSString* Price=nil;
  7156. if(str_price==nil)
  7157. {
  7158. NSNumber* price = [self get_model_default_price:customerID user:params[@"user"] product_id:nil item_id:@(item_id) db:db1];
  7159. if(price==nil)
  7160. Price=@"No Price.";
  7161. else
  7162. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7163. }
  7164. else
  7165. {
  7166. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  7167. }
  7168. double discount = sqlite3_column_double(stmt, 2);
  7169. int item_count = sqlite3_column_int(stmt, 3);
  7170. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  7171. NSString *nsline_note=nil;
  7172. if(line_note!=nil)
  7173. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  7174. // char *name = (char*)sqlite3_column_text(stmt, 5);
  7175. // NSString *nsname = nil;
  7176. // if(name!=nil)
  7177. // nsname= [[NSString alloc]initWithUTF8String:name];
  7178. NSString *nsname = [self textAtColumn:5 statement:stmt];
  7179. // char *description = (char*)sqlite3_column_text(stmt, 6);
  7180. // NSString *nsdescription=nil;
  7181. // if(description!=nil)
  7182. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  7183. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  7184. // int stockUom = sqlite3_column_int(stmt, 8);
  7185. // int _id = sqlite3_column_int(stmt, 9);
  7186. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  7187. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  7188. double weight=[bsubtotaljson[@"weight"] doubleValue];
  7189. int carton=[bsubtotaljson[@"carton"] intValue];
  7190. TotalCuft += cuft;
  7191. TotalWeight += weight;
  7192. TotalCarton += carton;
  7193. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:params[@"user"]];
  7194. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  7195. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  7196. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  7197. itemjson[@"note"]=nsline_note;
  7198. itemjson[@"origin_price"] = Price;
  7199. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  7200. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  7201. itemjson[@"order_item_status"] = @""; // 暂时不处理
  7202. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  7203. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  7204. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  7205. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7206. if(itemjson[@"combine"] != nil)
  7207. {
  7208. // int citem=0;
  7209. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  7210. for(int bc=0;bc<bcount;bc++)
  7211. {
  7212. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7213. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7214. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7215. subTotal += uprice * modulus * item_count;
  7216. }
  7217. }
  7218. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  7219. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  7220. // ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  7221. // ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  7222. itemjson[@"type"] = @"order_item";
  7223. model_data[[NSString stringWithFormat:@"item_%ld",(*count)++]] = itemjson;
  7224. model_data[@"count"] = [NSNumber numberWithInteger:*count];
  7225. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  7226. allItemPrice += subTotal;
  7227. }];
  7228. section_3[@"data"] = model_data;
  7229. section_3[@"type"] = @"sub_order";
  7230. section_3[@"title"] = @"Models";
  7231. section_3[@"switch"] = @(false);
  7232. ret[@"section_3"] = section_3;
  7233. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  7234. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  7235. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  7236. // payments/Credits
  7237. // payments_and_credist = sqlite3_column_double(statement, 34);
  7238. payments_and_credist = allItemPrice;
  7239. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  7240. NSDictionary *sub_total_item = @{
  7241. @"title":@"Sub-Total",
  7242. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  7243. };
  7244. [price_data setObject:sub_total_item forKey:@"item_0"];
  7245. // // total
  7246. // totalPrice = sqlite3_column_double(statement, 35);
  7247. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  7248. } else {
  7249. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  7250. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  7251. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  7252. // payments/Credits
  7253. payments_and_credist = 0;
  7254. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  7255. NSDictionary *sub_total_item = @{
  7256. @"title":@"Sub-Total",
  7257. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  7258. };
  7259. [price_data setObject:sub_total_item forKey:@"item_0"];
  7260. }
  7261. // total
  7262. totalPrice = payments_and_credist + lift_gate + shippingFee + handlingFee;
  7263. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  7264. if(discount>0)
  7265. {
  7266. NSDictionary *total_item = @{
  7267. @"title":@"Total",
  7268. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist*(1-(discount/100.0))+ lift_gate + shippingFee + handlingFee]
  7269. };
  7270. NSDictionary *discount_item = @{
  7271. @"title":@"Order Discount",
  7272. @"value":[NSString stringWithFormat:@"%d%% off",(int)discount]
  7273. };
  7274. [price_data setObject:discount_item forKey:@"item_4"];
  7275. [price_data setObject:total_item forKey:@"item_5"];
  7276. }
  7277. else
  7278. {
  7279. NSDictionary *total_item = @{
  7280. @"title":@"Total",
  7281. @"value":[NSString stringWithFormat:@"$%.2f",totalPrice]
  7282. };
  7283. [price_data setObject:total_item forKey:@"item_4"];
  7284. }
  7285. [price_data setObject:@(price_data.allKeys.count) forKey:@"count"];
  7286. section_4 = @{
  7287. @"data":price_data,
  7288. @"title":@"Price Info",
  7289. @"type":@"price_info"
  7290. }.mutableCopy;
  7291. ret[@"section_4"] = section_4;
  7292. // ret[@"order_info"]= orderinfo;
  7293. section_0 = @{
  7294. @"data":orderinfo,
  7295. @"title":@"Order Info",
  7296. @"type":@"order_info"
  7297. }.mutableCopy;
  7298. ret[@"section_0"] = section_0;
  7299. // ret[@"more_order_info"] = moreInfo;
  7300. section_2 = @{
  7301. @"data":moreInfo,
  7302. @"title":@"More Info",
  7303. @"type":@"more_order_info"
  7304. }.mutableCopy;
  7305. ret[@"section_2"] = section_2;
  7306. ret[@"count"] = @(5);
  7307. return [RAConvertor dict2data:ret];
  7308. }
  7309. #pragma mark order list
  7310. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  7311. NSString *nsstatus = @"";
  7312. switch (status) {
  7313. case 0:
  7314. {
  7315. nsstatus=@"Temp Order";
  7316. break;
  7317. }
  7318. case 1:
  7319. {
  7320. nsstatus=@"Saved Order";
  7321. break;
  7322. }
  7323. case 2: {
  7324. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  7325. break;
  7326. }
  7327. case 3:
  7328. case 15:
  7329. {
  7330. nsstatus=@"Cancelled";
  7331. break;
  7332. }
  7333. case 10:
  7334. {
  7335. nsstatus=@"Quote Submitted";
  7336. break;
  7337. }
  7338. case 11:
  7339. {
  7340. nsstatus=@"Sales Order Submitted";
  7341. break;
  7342. }
  7343. case 12:
  7344. {
  7345. nsstatus=@"Processing";
  7346. break;
  7347. }
  7348. case 13:
  7349. {
  7350. nsstatus=@"Shipped";
  7351. break;
  7352. }
  7353. case 14:
  7354. {
  7355. nsstatus=@"Closed";
  7356. break;
  7357. }
  7358. case -11:
  7359. {
  7360. nsstatus = @"Ready For Submit";
  7361. break;
  7362. }
  7363. default:
  7364. break;
  7365. }
  7366. return nsstatus;
  7367. }
  7368. + (double) orderTotalPrice:(NSString *)so_id user:(NSString*) user db:(sqlite3 *)db1 close:(BOOL)close
  7369. {
  7370. double total = 0;
  7371. __block double payments_and_credist = 0;
  7372. __block double allItemPrice = 0;
  7373. // sqlite3 *db1 = [iSalesDB get_db];
  7374. if (so_id) {
  7375. // 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];
  7376. 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];
  7377. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7378. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7379. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  7380. int product_id = sqlite3_column_int(stmt, 0);
  7381. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  7382. int discount = sqlite3_column_int(stmt, 2);
  7383. int item_count = sqlite3_column_int(stmt, 3);
  7384. // int item_id = sqlite3_column_int(stmt, 7);
  7385. int item_id = sqlite3_column_int(stmt, 4);
  7386. char *customer_cid = (char *)sqlite3_column_text(stmt, 5);
  7387. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  7388. NSString* Price=nil;
  7389. if(str_price==nil)
  7390. {
  7391. NSNumber* price = [self get_model_default_price:customerID user:user product_id:nil item_id:@(item_id) db:db1];
  7392. if(price==nil)
  7393. Price=@"No Price.";
  7394. else
  7395. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7396. }
  7397. else
  7398. {
  7399. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  7400. }
  7401. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:user];
  7402. itemjson[@"The unit price"]=Price;
  7403. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  7404. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  7405. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7406. if(itemjson[@"combine"] != nil)
  7407. {
  7408. // int citem=0;
  7409. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  7410. for(int bc=0;bc<bcount;bc++)
  7411. {
  7412. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7413. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7414. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7415. subTotal += uprice * modulus * item_count;
  7416. }
  7417. }
  7418. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  7419. allItemPrice += subTotal;
  7420. }];
  7421. payments_and_credist = allItemPrice;
  7422. } else {
  7423. // payments/Credits
  7424. payments_and_credist = 0;
  7425. }
  7426. // lift_gate handlingFee shippingFee
  7427. __block double lift_gate = 0;
  7428. __block double handlingFee = 0;
  7429. __block double shippingFee = 0;
  7430. __block double order_discount = 0;
  7431. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping,discount from offline_order where so_id = '%@';",so_id];
  7432. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7433. int have_lift_gate = sqlite3_column_int(stmt, 0);
  7434. if (have_lift_gate) {
  7435. lift_gate = sqlite3_column_double(stmt, 1);
  7436. }
  7437. handlingFee = sqlite3_column_double(stmt, 2);
  7438. shippingFee = sqlite3_column_double(stmt, 3);
  7439. order_discount = sqlite3_column_double(stmt, 4);
  7440. }];
  7441. if(order_discount>0)
  7442. payments_and_credist= payments_and_credist*(1-order_discount/100.0);
  7443. // total
  7444. total = payments_and_credist + lift_gate + shippingFee + handlingFee;
  7445. if (close) {
  7446. [iSalesDB close_db:db1];
  7447. }
  7448. return total;
  7449. }
  7450. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  7451. {
  7452. assert(params[@"user"]!=nil);
  7453. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  7454. int limit = [[params valueForKey:@"limit"] intValue];
  7455. int offset = [[params valueForKey:@"offset"] intValue];
  7456. NSString* keyword = [params valueForKey:@"keyWord"];
  7457. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  7458. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  7459. NSString* where=@"1 = 1";
  7460. if(keyword.length>0)
  7461. 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]];
  7462. if (orderStatus.length > 0) {
  7463. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  7464. if (order_status_array.count == 1) {
  7465. int status_value = [[order_status_array firstObject] intValue];
  7466. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  7467. if (status_value == 15 || status_value == 3) {
  7468. where = [where stringByAppendingString:[NSString stringWithFormat:@" and (o.status = 3 or o.erpOrderStatus = 15)"]];
  7469. } else {
  7470. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  7471. }
  7472. } else {
  7473. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  7474. }
  7475. } else if (order_status_array.count > 1) {
  7476. for (int i = 0; i < order_status_array.count;i++) {
  7477. NSString *status = order_status_array[i];
  7478. NSString *condition = @" or";
  7479. if (i == 0) {
  7480. condition = @" and (";
  7481. }
  7482. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  7483. int status_value = [status intValue];
  7484. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  7485. if (status_value == 15 || status_value == 3) {
  7486. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ (o.status = 3 or o.erpOrderStatus = 15)",condition]];
  7487. } else {
  7488. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  7489. }
  7490. } else {
  7491. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  7492. }
  7493. }
  7494. where = [where stringByAppendingString:@" )"];
  7495. }
  7496. }
  7497. 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];
  7498. // DebugLog(@"order list sql: %@",sqlQuery);
  7499. sqlite3 *db = [iSalesDB get_db];
  7500. sqlite3_stmt * statement;
  7501. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  7502. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  7503. {
  7504. int count=0;
  7505. while (sqlite3_step(statement) == SQLITE_ROW)
  7506. {
  7507. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  7508. int order_id = sqlite3_column_double(statement, 0);
  7509. char *soid = (char*)sqlite3_column_text(statement, 1);
  7510. if(soid==nil)
  7511. soid= "";
  7512. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  7513. int status = sqlite3_column_double(statement, 2);
  7514. int erpStatus = sqlite3_column_double(statement, 9);
  7515. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  7516. if(sales_rep==nil)
  7517. sales_rep= "";
  7518. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  7519. char *create_by = (char*)sqlite3_column_text(statement, 4);
  7520. if(create_by==nil)
  7521. create_by= "";
  7522. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  7523. char *company_name = (char*)sqlite3_column_text(statement, 5);
  7524. if(company_name==nil)
  7525. company_name= "";
  7526. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  7527. char *create_time = (char*)sqlite3_column_text(statement, 6);
  7528. if(create_time==nil)
  7529. create_time= "";
  7530. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  7531. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  7532. // double total_price = sqlite3_column_double(statement, 7);
  7533. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] user:params[@"user"] db:db close:NO];
  7534. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  7535. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  7536. int offline_edit = sqlite3_column_int(statement, 10);
  7537. // ": "JH",
  7538. // "": "$8307.00",
  7539. // "": "MOB1608050001",
  7540. // "": "ArpithaT",
  7541. // "": "1st Stage Property Transformations",
  7542. // "": "JANICE SUTTON",
  7543. // "": 2255,
  7544. // "": "08/02/2016 09:49:18",
  7545. // "": 1,
  7546. // "": "Saved Order"
  7547. // "": "1470384050483",
  7548. // "model_count": "6 / 28"
  7549. item[@"sales_rep"]= nssales_rep;
  7550. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  7551. item[@"so#"]= nssoid;
  7552. item[@"create_by"]= nscreate_by;
  7553. item[@"customer_name"]= nscompany_name;
  7554. item[@"customer_contact"] = customer_contact;
  7555. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  7556. item[@"purchase_time"]= nscreate_time;
  7557. int statusCode = status;
  7558. if (statusCode == 2) {
  7559. statusCode = erpStatus;
  7560. } else if (statusCode == 3) {
  7561. statusCode = 15;
  7562. }
  7563. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",statusCode];
  7564. item[@"order_status"]= nsstatus;
  7565. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  7566. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  7567. // item[@"model_count"]
  7568. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  7569. count++;
  7570. }
  7571. ret[@"count"]= [NSNumber numberWithInt:count];
  7572. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  7573. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  7574. ret[@"result"]= [NSNumber numberWithInt:2];
  7575. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  7576. ret[@"time_zone"] = @"PST";
  7577. sqlite3_finalize(statement);
  7578. }
  7579. 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];
  7580. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  7581. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7582. [iSalesDB close_db:db];
  7583. return [RAConvertor dict2data:ret];
  7584. }
  7585. #pragma mark update gnotes
  7586. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  7587. {
  7588. DebugLog(@"params: %@",params);
  7589. // comments = Meyoyoyoyoyoyoy;
  7590. // orderCode = MOB1608110001;
  7591. // password = 123456;
  7592. // user = EvanK;
  7593. 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]];
  7594. int ret = [iSalesDB execSql:sql];
  7595. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7596. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7597. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7598. // [dic setValue:@"160409" forKey:@"min_ver"];
  7599. return [RAConvertor dict2data:dic];
  7600. }
  7601. #pragma mark move to wishlist
  7602. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  7603. {
  7604. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  7605. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7606. _id = [NSString stringWithFormat:@"(%@)",_id];
  7607. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  7608. sqlite3 *db = [iSalesDB get_db];
  7609. __block NSString *product_id = @"";
  7610. __block NSString *item_count_str = @"";
  7611. // __block NSString *item_id = nil;
  7612. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7613. // product_id = [self textAtColumn:0 statement:stmt];
  7614. int item_count = sqlite3_column_int(stmt, 1);
  7615. // item_id = [self textAtColumn:2 statement:stmt];
  7616. NSString *p_id = [self textAtColumn:0 statement:stmt];
  7617. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  7618. if (p_id.length) {
  7619. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  7620. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  7621. }
  7622. }];
  7623. [iSalesDB close_db:db];
  7624. // 去除第一个,
  7625. if (product_id.length > 1) {
  7626. product_id = [product_id substringFromIndex:1];
  7627. }
  7628. if (item_count_str.length > 1) {
  7629. item_count_str = [item_count_str substringFromIndex:1];
  7630. }
  7631. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7632. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  7633. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  7634. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  7635. sqlite3 *db1 = [iSalesDB get_db];
  7636. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  7637. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7638. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  7639. // 删除
  7640. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  7641. int ret = [iSalesDB execSql:deleteSql db:db1];
  7642. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7643. [iSalesDB close_db:db1];
  7644. return [RAConvertor dict2data:dic];
  7645. }
  7646. #pragma mark cart delete
  7647. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  7648. {
  7649. // cartItemId = 548;
  7650. // orderCode = MOB1608110001;
  7651. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  7652. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7653. _id = [NSString stringWithFormat:@"(%@)",_id];
  7654. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  7655. int ret = [iSalesDB execSql:sql];
  7656. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7657. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7658. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7659. // [dic setValue:@"160409" forKey:@"min_ver"];
  7660. return [RAConvertor dict2data:dic];
  7661. }
  7662. #pragma mark set order discount
  7663. //+(NSDictionary*) offline_cartsetallprice :(NSMutableDictionary *) params
  7664. //{
  7665. // DebugLog(@"cart set price params: %@",params);
  7666. // // "cartitem_id" = 1;
  7667. // // discount = "0.000000";
  7668. // // "item_note" = "";
  7669. // // price = "269.000000";
  7670. // NSString *order_code = [self valueInParams:params key:@"orderCode"];
  7671. // //NSString *notes = [self valueInParams:params key:@"item_note"];
  7672. // NSString *discount = [self valueInParams:params key:@"app_discount"];
  7673. //// NSString *price = [self valueInParams:params key:@"price"];
  7674. //
  7675. // // bool badd_price_changed=false;
  7676. // // sqlite3* db=[iSalesDB get_db];
  7677. // // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7678. // // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7679. // // NSRange range;
  7680. // // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7681. // //
  7682. // // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7683. // // badd_price_changed=true;
  7684. // // [iSalesDB close_db:db];
  7685. // //
  7686. // // if(badd_price_changed)
  7687. // // {
  7688. // // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7689. // // }
  7690. // //
  7691. //
  7692. //
  7693. //// price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7694. //
  7695. // NSString *sql = [NSString stringWithFormat:@"update offline_cart set discount = %f where so_no = '%@'",discount.doubleValue,order_code];
  7696. //
  7697. // int ret = [iSalesDB execSql:sql];
  7698. //
  7699. // NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7700. // [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7701. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  7702. // // [dic setValue:@"160409" forKey:@"min_ver"];
  7703. //
  7704. //// return [RAConvertor dict2data:dic];
  7705. // return dic;
  7706. //
  7707. //}
  7708. +(NSDictionary*) offline_setorderdiscount :(NSMutableDictionary *) params
  7709. {
  7710. DebugLog(@"order set discount params: %@",params);
  7711. // "cartitem_id" = 1;
  7712. // discount = "0.000000";
  7713. // "item_note" = "";
  7714. // price = "269.000000";
  7715. NSString *order_code = [self valueInParams:params key:@"orderCode"];
  7716. //NSString *notes = [self valueInParams:params key:@"item_note"];
  7717. NSString *discount = [self valueInParams:params key:@"app_discount"];
  7718. // NSString *price = [self valueInParams:params key:@"price"];
  7719. // bool badd_price_changed=false;
  7720. // sqlite3* db=[iSalesDB get_db];
  7721. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7722. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7723. // NSRange range;
  7724. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7725. //
  7726. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7727. // badd_price_changed=true;
  7728. // [iSalesDB close_db:db];
  7729. //
  7730. // if(badd_price_changed)
  7731. // {
  7732. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7733. // }
  7734. //
  7735. // sqlite3 *db = [iSalesDB get_db];
  7736. // double total_price = [self orderTotalPrice:params[@"orderCode"] user:params[@"user"] db:db close:YES];
  7737. // price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7738. //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];
  7739. NSString *sql = [NSString stringWithFormat:@"update offline_order set discount = %f where so_id = '%@'",discount.doubleValue,order_code];
  7740. int ret = [iSalesDB execSql:sql];
  7741. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7742. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7743. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7744. // [dic setValue:@"160409" forKey:@"min_ver"];
  7745. // return [RAConvertor dict2data:dic];
  7746. return dic;
  7747. }
  7748. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  7749. {
  7750. DebugLog(@"cart set price params: %@",params);
  7751. // "cartitem_id" = 1;
  7752. // discount = "0.000000";
  7753. // "item_note" = "";
  7754. // price = "269.000000";
  7755. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7756. NSString *notes = [self valueInParams:params key:@"item_note"];
  7757. NSString *discount = [self valueInParams:params key:@"discount"];
  7758. NSString *price = [self valueInParams:params key:@"price"];
  7759. // bool badd_price_changed=false;
  7760. // sqlite3* db=[iSalesDB get_db];
  7761. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7762. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7763. // NSRange range;
  7764. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7765. //
  7766. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7767. // badd_price_changed=true;
  7768. // [iSalesDB close_db:db];
  7769. //
  7770. // if(badd_price_changed)
  7771. // {
  7772. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7773. // }
  7774. //
  7775. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7776. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  7777. int ret = [iSalesDB execSql:sql];
  7778. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7779. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7780. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7781. // [dic setValue:@"160409" forKey:@"min_ver"];
  7782. return [RAConvertor dict2data:dic];
  7783. }
  7784. #pragma mark set line notes
  7785. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  7786. {
  7787. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7788. NSString *notes = [self valueInParams:params key:@"notes"];
  7789. notes = [self translateSingleQuote:notes];
  7790. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  7791. int ret = [iSalesDB execSql:sql];
  7792. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7793. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7794. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7795. // [dic setValue:@"160409" forKey:@"min_ver"];
  7796. return [RAConvertor dict2data:dic];
  7797. }
  7798. #pragma mark set qty
  7799. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  7800. {
  7801. assert(params[@"can_create_backorder"]!=nil);
  7802. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7803. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7804. int item_count = [params[@"inputInt"] intValue];
  7805. // 购买检查数量大于库存
  7806. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7807. if (!params[@"can_create_backorder"]) {
  7808. 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];
  7809. __block BOOL out_of_stock = NO;
  7810. [iSalesDB jk_query:checkSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7811. int availability = sqlite3_column_int(stmt, 0);
  7812. if (availability < item_count) {
  7813. out_of_stock = YES;
  7814. }
  7815. }];
  7816. if (out_of_stock) { // 缺货
  7817. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"result"];
  7818. [dic setObject:@"Item is out of stock" forKey:@"err_msg"];
  7819. return [RAConvertor dict2data:dic];
  7820. }
  7821. }
  7822. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  7823. int ret = [iSalesDB execSql:sql];
  7824. __block int item_id = 0;
  7825. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7826. item_id = sqlite3_column_int(stmt, 0);
  7827. }];
  7828. sqlite3 *db = [iSalesDB get_db];
  7829. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  7830. [iSalesDB close_db:db];
  7831. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7832. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7833. // [dic setValue:@"160409" forKey:@"min_ver"];
  7834. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  7835. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  7836. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  7837. return [RAConvertor dict2data:dic];
  7838. }
  7839. #pragma mark place order
  7840. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  7841. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7842. if (aname) {
  7843. [dic setValue:aname forKey:@"aname"];
  7844. }
  7845. if (control) {
  7846. [dic setValue:control forKey:@"control"];
  7847. }
  7848. if (keyboard) {
  7849. [dic setValue:keyboard forKey:@"keyboard"];
  7850. }
  7851. if (name) {
  7852. [dic setValue:name forKey:@"name"];
  7853. }
  7854. if (value) {
  7855. [dic setValue:value forKey:@"value"];
  7856. }
  7857. return dic;
  7858. }
  7859. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  7860. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7861. orderCode = [self translateSingleQuote:orderCode];
  7862. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  7863. 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];
  7864. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7865. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  7866. NSString *img0 = [self textAtColumn:1 statement:stmt];
  7867. NSString *img1 = [self textAtColumn:2 statement:stmt];
  7868. NSString *img2 = [self textAtColumn:3 statement:stmt];
  7869. NSString *company_name = [self textAtColumn:4 statement:stmt];
  7870. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  7871. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  7872. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  7873. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  7874. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  7875. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  7876. NSString *email = [self textAtColumn:11 statement:stmt];
  7877. NSString *phone = [self textAtColumn:12 statement:stmt];
  7878. NSString *fax = [self textAtColumn:13 statement:stmt];
  7879. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  7880. NSString *city = [self textAtColumn:15 statement:stmt];
  7881. NSString *state = [self textAtColumn:16 statement:stmt];
  7882. NSString *country = [self textAtColumn:17 statement:stmt];
  7883. NSString *name = [self textAtColumn:18 statement:stmt];
  7884. // contact id
  7885. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  7886. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  7887. [contact_id_dic setValue:@"text" forKey:@"control"];
  7888. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  7889. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  7890. [contact_id_dic setValue:@"true" forKey:@"required"];
  7891. [contact_id_dic setValue:contact_id forKey:@"value"];
  7892. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  7893. // business card
  7894. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  7895. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  7896. [business_card_dic setValue:@"img" forKey:@"control"];
  7897. [business_card_dic setValue:@"1" forKey:@"disable"];
  7898. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  7899. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  7900. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  7901. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  7902. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  7903. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  7904. [business_card_dic setValue:@"business_card" forKey:@"name"];
  7905. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  7906. // fax
  7907. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7908. [customer_dic setValue:fax_dic forKey:@"item_10"];
  7909. // zipcode
  7910. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  7911. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  7912. // city
  7913. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  7914. [customer_dic setValue:city_dic forKey:@"item_12"];
  7915. // state
  7916. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  7917. [customer_dic setValue:state_dic forKey:@"item_13"];
  7918. // country
  7919. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  7920. [customer_dic setValue:country_dic forKey:@"item_14"];
  7921. // company name
  7922. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  7923. [customer_dic setValue:company_dic forKey:@"item_2"];
  7924. // addr_1
  7925. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  7926. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  7927. // addr_2
  7928. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  7929. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  7930. // addr_3
  7931. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  7932. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  7933. // addr_4
  7934. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  7935. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  7936. // Contact
  7937. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  7938. [customer_dic setValue:contact_dic forKey:@"item_7"];
  7939. // email
  7940. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7941. [customer_dic setValue:email_dic forKey:@"item_8"];
  7942. // phone
  7943. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7944. [customer_dic setValue:phone_dic forKey:@"item_9"];
  7945. // title
  7946. [customer_dic setValue:@"Customer" forKey:@"title"];
  7947. // count
  7948. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  7949. }];
  7950. // setting
  7951. NSDictionary *setting = params[@"setting"];
  7952. NSNumber *hide = setting[@"CustomerHide"];
  7953. [customer_dic setValue:hide forKey:@"hide"];
  7954. return customer_dic;
  7955. }
  7956. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7957. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7958. // setting
  7959. NSDictionary *setting = params[@"setting"];
  7960. NSNumber *hide = setting[@"ShipToHide"];
  7961. [dic setValue:hide forKey:@"hide"];
  7962. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7963. orderCode = [self translateSingleQuote:orderCode];
  7964. 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];
  7965. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7966. NSString *cid = [self textAtColumn:0 statement:stmt];
  7967. NSString *name = [self textAtColumn:1 statement:stmt];
  7968. NSString *ext = [self textAtColumn:2 statement:stmt];
  7969. NSString *contact = [self textAtColumn:3 statement:stmt];
  7970. NSString *email = [self textAtColumn:4 statement:stmt];
  7971. NSString *fax = [self textAtColumn:5 statement:stmt];
  7972. NSString *phone = [self textAtColumn:6 statement:stmt];
  7973. // count
  7974. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7975. // title
  7976. [dic setValue:@"Ship To" forKey:@"title"];
  7977. // choose
  7978. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7979. [choose_dic setValue:@"choose" forKey:@"aname"];
  7980. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7981. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  7982. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7983. @"key_map" : @{@"receive_cid" : @"customer_cid",
  7984. @"receive_contact" : @"customer_contact",
  7985. @"receive_email" : @"customer_email",
  7986. @"receive_ext" : @"customer_contact_ext",
  7987. @"receive_fax" : @"customer_fax",
  7988. @"receive_name" : @"customer_name",
  7989. @"receive_phone" : @"customer_phone"},
  7990. @"refresh" : [NSNumber numberWithInteger:1],
  7991. @"type" : @"pull"};
  7992. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  7993. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  7994. @"name" : @"Add new address",
  7995. @"refresh" : [NSNumber numberWithInteger:1],
  7996. @"value" : @"new_addr"
  7997. };
  7998. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  7999. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  8000. @"key_map" : @{@"receive_cid" : @"customer_cid",
  8001. @"receive_contact" : @"customer_contact",
  8002. @"receive_email" : @"customer_email",
  8003. @"receive_ext" : @"customer_contact_ext",
  8004. @"receive_fax" : @"customer_fax",
  8005. @"receive_name" : @"customer_name",
  8006. @"receive_phone" : @"customer_phone"},
  8007. @"name" : @"select_ship_to",
  8008. @"refresh" : [NSNumber numberWithInteger:1],
  8009. @"value" : @"Sales_Order_Ship_To"};
  8010. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  8011. [dic setValue:choose_dic forKey:@"item_0"];
  8012. // contact id
  8013. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8014. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8015. [contact_id_dic setValue:@"true" forKey:@"required"];
  8016. [dic setValue:contact_id_dic forKey:@"item_1"];
  8017. // company name
  8018. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8019. [dic setValue:company_name_dic forKey:@"item_2"];
  8020. // address
  8021. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8022. NSNumber *required = setting[@"ShippingToAddressRequire"];
  8023. if ([required integerValue]) {
  8024. [ext_dic setValue:@"true" forKey:@"required"];
  8025. }
  8026. [dic setValue:ext_dic forKey:@"item_3"];
  8027. // contact
  8028. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8029. [dic setValue:contact_dic forKey:@"item_4"];
  8030. // phone
  8031. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8032. [dic setValue:phone_dic forKey:@"item_5"];
  8033. // fax
  8034. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8035. [dic setValue:fax_dic forKey:@"item_6"];
  8036. // email
  8037. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8038. [dic setValue:email_dic forKey:@"item_7"];
  8039. }];
  8040. return dic;
  8041. }
  8042. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  8043. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8044. // setting
  8045. NSDictionary *setting = params[@"setting"];
  8046. NSNumber *hide = setting[@"ShipFromHide"];
  8047. [dic setValue:hide forKey:@"hide"];
  8048. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8049. orderCode = [self translateSingleQuote:orderCode];
  8050. 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];
  8051. 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';";
  8052. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8053. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  8054. __block NSString *name = [self textAtColumn:1 statement:stmt];
  8055. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  8056. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  8057. __block NSString *email = [self textAtColumn:4 statement:stmt];
  8058. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  8059. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  8060. if (!cid.length) {
  8061. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  8062. cid = [self textAtColumn:0 statement:statment];
  8063. name = [self textAtColumn:1 statement:statment];
  8064. ext = [self textAtColumn:2 statement:statment];
  8065. contact = [self textAtColumn:3 statement:statment];
  8066. email = [self textAtColumn:4 statement:statment];
  8067. fax = [self textAtColumn:5 statement:statment];
  8068. phone = [self textAtColumn:6 statement:statment];
  8069. }];
  8070. }
  8071. // count
  8072. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8073. // title
  8074. [dic setValue:@"Ship From" forKey:@"title"];
  8075. // hide
  8076. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8077. // choose
  8078. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8079. [choose_dic setValue:@"choose" forKey:@"aname"];
  8080. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8081. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8082. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  8083. @"key_map" : @{@"sender_cid" : @"customer_cid",
  8084. @"sender_contact" : @"customer_contact",
  8085. @"sender_email" : @"customer_email",
  8086. @"sender_ext" : @"customer_contact_ext",
  8087. @"sender_fax" : @"customer_fax",
  8088. @"sender_name" : @"customer_name",
  8089. @"sender_phone" : @"customer_phone"},
  8090. @"name" : @"select_cid",
  8091. @"refresh" : [NSNumber numberWithInteger:0],
  8092. @"value" : @"Sales_Order_Ship_From"};
  8093. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  8094. [dic setValue:choose_dic forKey:@"item_0"];
  8095. // contact id
  8096. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8097. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8098. [contact_id_dic setValue:@"true" forKey:@"required"];
  8099. [dic setValue:contact_id_dic forKey:@"item_1"];
  8100. // company name
  8101. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8102. [dic setValue:company_name_dic forKey:@"item_2"];
  8103. // address
  8104. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  8105. [dic setValue:ext_dic forKey:@"item_3"];
  8106. // contact
  8107. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8108. [dic setValue:contact_dic forKey:@"item_4"];
  8109. // phone
  8110. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8111. [dic setValue:phone_dic forKey:@"item_5"];
  8112. // fax
  8113. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8114. [dic setValue:fax_dic forKey:@"item_6"];
  8115. // email
  8116. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8117. [dic setValue:email_dic forKey:@"item_7"];
  8118. }];
  8119. return dic;
  8120. }
  8121. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  8122. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8123. // setting
  8124. NSDictionary *setting = params[@"setting"];
  8125. NSNumber *hide = setting[@"FreightBillToHide"];
  8126. [dic setValue:hide forKey:@"hide"];
  8127. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8128. orderCode = [self translateSingleQuote:orderCode];
  8129. 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];
  8130. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8131. NSString *cid = [self textAtColumn:0 statement:stmt];
  8132. NSString *name = [self textAtColumn:1 statement:stmt];
  8133. NSString *ext = [self textAtColumn:2 statement:stmt];
  8134. NSString *contact = [self textAtColumn:3 statement:stmt];
  8135. NSString *email = [self textAtColumn:4 statement:stmt];
  8136. NSString *fax = [self textAtColumn:5 statement:stmt];
  8137. NSString *phone = [self textAtColumn:6 statement:stmt];
  8138. // count
  8139. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8140. // title
  8141. [dic setValue:@"Freight Bill To" forKey:@"title"];
  8142. // hide
  8143. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8144. // choose
  8145. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8146. [choose_dic setValue:@"choose" forKey:@"aname"];
  8147. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8148. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  8149. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  8150. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  8151. @"shipping_billto_contact" : @"receive_contact",
  8152. @"shipping_billto_email" : @"receive_email",
  8153. @"shipping_billto_ext" : @"receive_ext",
  8154. @"shipping_billto_fax" : @"receive_fax",
  8155. @"shipping_billto_name" : @"receive_name",
  8156. @"shipping_billto_phone" : @"receive_phone"},
  8157. @"type" : @"pull"};
  8158. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  8159. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  8160. @"type" : @"pull",
  8161. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  8162. @"shipping_billto_contact" : @"customer_contact",
  8163. @"shipping_billto_email" : @"customer_email",
  8164. @"shipping_billto_ext" : @"customer_contact_ext",
  8165. @"shipping_billto_fax" : @"customer_fax",
  8166. @"shipping_billto_name" : @"customer_name",
  8167. @"shipping_billto_phone" : @"customer_phone"}
  8168. };
  8169. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  8170. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  8171. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  8172. @"shipping_billto_contact" : @"sender_contact",
  8173. @"shipping_billto_email" : @"sender_email",
  8174. @"shipping_billto_ext" : @"sender_ext",
  8175. @"shipping_billto_fax" : @"sender_fax",
  8176. @"shipping_billto_name" : @"sender_name",
  8177. @"shipping_billto_phone" : @"sender_phone"},
  8178. @"type" : @"pull"
  8179. };
  8180. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  8181. NSDictionary *select_freight_bill_to_dic = @{
  8182. @"aname" : @"Select freight bill to",
  8183. @"name" : @"select_cid",
  8184. @"refresh" : [NSNumber numberWithInteger:0],
  8185. @"value" : @"Sales_Order_Freight_Bill_To",
  8186. @"key_map" : @{
  8187. @"shipping_billto_cid" : @"customer_cid",
  8188. @"shipping_billto_contact" : @"customer_contact",
  8189. @"shipping_billto_email" : @"customer_email",
  8190. @"shipping_billto_ext" : @"customer_contact_ext",
  8191. @"shipping_billto_fax" : @"customer_fax",
  8192. @"shipping_billto_name" : @"customer_name",
  8193. @"shipping_billto_phone" : @"customer_phone"
  8194. }
  8195. };
  8196. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  8197. [dic setValue:choose_dic forKey:@"item_0"];
  8198. // contact id
  8199. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8200. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8201. [contact_id_dic setValue:@"true" forKey:@"required"];
  8202. [dic setValue:contact_id_dic forKey:@"item_1"];
  8203. // company name
  8204. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8205. [dic setValue:company_name_dic forKey:@"item_2"];
  8206. // address
  8207. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8208. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  8209. if ([ext_required integerValue]) {
  8210. [ext_dic setValue:@"true" forKey:@"required"];
  8211. }
  8212. [dic setValue:ext_dic forKey:@"item_3"];
  8213. // contact
  8214. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8215. [dic setValue:contact_dic forKey:@"item_4"];
  8216. // phone
  8217. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8218. [dic setValue:phone_dic forKey:@"item_5"];
  8219. // fax
  8220. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8221. [dic setValue:fax_dic forKey:@"item_6"];
  8222. // email
  8223. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8224. [dic setValue:email_dic forKey:@"item_7"];
  8225. }];
  8226. return dic;
  8227. }
  8228. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  8229. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8230. // setting
  8231. NSDictionary *setting = params[@"setting"];
  8232. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  8233. [dic setValue:hide forKey:@"hide"];
  8234. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8235. orderCode = [self translateSingleQuote:orderCode];
  8236. 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];
  8237. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8238. NSString *cid = [self textAtColumn:0 statement:stmt];
  8239. NSString *name = [self textAtColumn:1 statement:stmt];
  8240. NSString *ext = [self textAtColumn:2 statement:stmt];
  8241. NSString *contact = [self textAtColumn:3 statement:stmt];
  8242. NSString *email = [self textAtColumn:4 statement:stmt];
  8243. NSString *fax = [self textAtColumn:5 statement:stmt];
  8244. NSString *phone = [self textAtColumn:6 statement:stmt];
  8245. // count
  8246. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8247. // title
  8248. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  8249. // hide
  8250. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8251. // choose
  8252. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8253. [choose_dic setValue:@"choose" forKey:@"aname"];
  8254. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8255. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8256. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  8257. @"key_map" : @{@"billing_cid" : @"receive_cid",
  8258. @"billing_contact" : @"receive_contact",
  8259. @"billing_email" : @"receive_email",
  8260. @"billing_ext" : @"receive_ext",
  8261. @"billing_fax" : @"receive_fax",
  8262. @"billing_name" : @"receive_name",
  8263. @"billing_phone" : @"receive_phone"},
  8264. @"type" : @"pull"};
  8265. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  8266. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  8267. @"type" : @"pull",
  8268. @"key_map" : @{@"billing_cid" : @"customer_cid",
  8269. @"billing_contact" : @"customer_contact",
  8270. @"billing_email" : @"customer_email",
  8271. @"billing_ext" : @"customer_contact_ext",
  8272. @"billing_fax" : @"customer_fax",
  8273. @"billing_name" : @"customer_name",
  8274. @"billing_phone" : @"customer_phone"}
  8275. };
  8276. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  8277. NSDictionary *select_bill_to_dic = @{
  8278. @"aname" : @"Select bill to",
  8279. @"name" : @"select_cid",
  8280. @"refresh" : [NSNumber numberWithInteger:0],
  8281. @"value" : @"Sales_Order_Merchandise_Bill_To",
  8282. @"key_map" : @{
  8283. @"billing_cid" : @"customer_cid",
  8284. @"billing_contact" : @"customer_contact",
  8285. @"billing_email" : @"customer_email",
  8286. @"billing_ext" : @"customer_contact_ext",
  8287. @"billing_fax" : @"customer_fax",
  8288. @"billing_name" : @"customer_name",
  8289. @"billing_phone" : @"customer_phone"
  8290. }
  8291. };
  8292. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  8293. [dic setValue:choose_dic forKey:@"item_0"];
  8294. // contact id
  8295. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8296. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8297. [contact_id_dic setValue:@"true" forKey:@"required"];
  8298. [dic setValue:contact_id_dic forKey:@"item_1"];
  8299. // company name
  8300. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8301. [dic setValue:company_name_dic forKey:@"item_2"];
  8302. // address
  8303. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8304. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  8305. if ([ext_required integerValue]) {
  8306. [ext_dic setValue:@"true" forKey:@"required"];
  8307. }
  8308. [dic setValue:ext_dic forKey:@"item_3"];
  8309. // contact
  8310. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8311. [dic setValue:contact_dic forKey:@"item_4"];
  8312. // phone
  8313. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8314. [dic setValue:phone_dic forKey:@"item_5"];
  8315. // fax
  8316. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8317. [dic setValue:fax_dic forKey:@"item_6"];
  8318. // email
  8319. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8320. [dic setValue:email_dic forKey:@"item_7"];
  8321. }];
  8322. return dic;
  8323. }
  8324. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  8325. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8326. // setting
  8327. NSDictionary *setting = params[@"setting"];
  8328. NSNumber *hide = setting[@"ReturnToHide"];
  8329. [dic setValue:hide forKey:@"hide"];
  8330. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8331. orderCode = [self translateSingleQuote:orderCode];
  8332. 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];
  8333. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8334. NSString *cid = [self textAtColumn:0 statement:stmt];
  8335. NSString *name = [self textAtColumn:1 statement:stmt];
  8336. NSString *ext = [self textAtColumn:2 statement:stmt];
  8337. NSString *contact = [self textAtColumn:3 statement:stmt];
  8338. NSString *email = [self textAtColumn:4 statement:stmt];
  8339. NSString *fax = [self textAtColumn:5 statement:stmt];
  8340. NSString *phone = [self textAtColumn:6 statement:stmt];
  8341. // count
  8342. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8343. // title
  8344. [dic setValue:@"Return To" forKey:@"title"];
  8345. // hide
  8346. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8347. // choose
  8348. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8349. [choose_dic setValue:@"choose" forKey:@"aname"];
  8350. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8351. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  8352. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  8353. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  8354. @"returnto_contact" : @"sender_contact",
  8355. @"returnto_email" : @"sender_email",
  8356. @"returnto_ext" : @"sender_ext",
  8357. @"returnto_fax" : @"sender_fax",
  8358. @"returnto_name" : @"sender_name",
  8359. @"returnto_phone" : @"sender_phone"},
  8360. @"type" : @"pull"};
  8361. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  8362. NSDictionary *select_return_to_dic = @{
  8363. @"aname" : @"Select return to",
  8364. @"name" : @"select_cid",
  8365. @"refresh" : [NSNumber numberWithInteger:0],
  8366. @"value" : @"Contact_Return_To",
  8367. @"key_map" : @{
  8368. @"returnto_cid" : @"customer_cid",
  8369. @"returnto_contact" : @"customer_contact",
  8370. @"returnto_email" : @"customer_email",
  8371. @"returnto_ext" : @"customer_contact_ext",
  8372. @"returnto_fax" : @"customer_fax",
  8373. @"returnto_name" : @"customer_name",
  8374. @"returnto_phone" : @"customer_phone"
  8375. }
  8376. };
  8377. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  8378. [dic setValue:choose_dic forKey:@"item_0"];
  8379. // contact id
  8380. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8381. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8382. [contact_id_dic setValue:@"true" forKey:@"required"];
  8383. [dic setValue:contact_id_dic forKey:@"item_1"];
  8384. // company name
  8385. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8386. [dic setValue:company_name_dic forKey:@"item_2"];
  8387. // address
  8388. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  8389. [dic setValue:ext_dic forKey:@"item_3"];
  8390. // contact
  8391. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8392. [dic setValue:contact_dic forKey:@"item_4"];
  8393. // phone
  8394. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8395. [dic setValue:phone_dic forKey:@"item_5"];
  8396. // fax
  8397. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8398. [dic setValue:fax_dic forKey:@"item_6"];
  8399. // email
  8400. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8401. [dic setValue:email_dic forKey:@"item_7"];
  8402. }];
  8403. return dic;
  8404. }
  8405. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db
  8406. {
  8407. assert(params[@"user"]!=nil);
  8408. assert(params[@"contact_id"]!=nil);
  8409. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8410. orderCode = [self translateSingleQuote:orderCode];
  8411. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8412. __block double TotalCuft = 0;
  8413. __block double TotalWeight = 0;
  8414. __block int TotalCarton = 0;
  8415. __block double payments = 0;
  8416. // setting
  8417. NSDictionary *setting = params[@"setting"];
  8418. NSNumber *hide = setting[@"ModelInformationHide"];
  8419. [dic setValue:hide forKey:@"hide"];
  8420. 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];
  8421. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8422. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8423. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  8424. // item id
  8425. int item_id = sqlite3_column_int(stmt, 0);
  8426. // count
  8427. int item_count = sqlite3_column_int(stmt, 1);
  8428. // stockUom
  8429. int stockUom = sqlite3_column_int(stmt, 2);
  8430. // unit price
  8431. NSString *str_price = [self textAtColumn:3 statement:stmt];
  8432. NSString* Price=nil;
  8433. if([str_price isEqualToString:@""])
  8434. {
  8435. NSNumber* price = [self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db];
  8436. if(price==nil)
  8437. Price=@"No Price.";
  8438. else
  8439. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  8440. }
  8441. else
  8442. {
  8443. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  8444. }
  8445. // discount
  8446. double discount = sqlite3_column_double(stmt, 4);
  8447. // name
  8448. NSString *name = [self textAtColumn:5 statement:stmt];
  8449. // description
  8450. NSString *description = [self textAtColumn:6 statement:stmt];
  8451. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  8452. // line note
  8453. NSString *line_note = [self textAtColumn:7 statement:stmt];
  8454. int avaulability = sqlite3_column_int(stmt, 8);
  8455. // img
  8456. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  8457. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false contactID:params[@"contact_id"] user:params[@"user"]];
  8458. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  8459. if(combine != nil)
  8460. {
  8461. // int citem=0;
  8462. int bcount=[[combine valueForKey:@"count"] intValue];
  8463. for(int bc=0;bc<bcount;bc++)
  8464. {
  8465. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  8466. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  8467. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  8468. subTotal += uprice * modulus * item_count;
  8469. }
  8470. }
  8471. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  8472. [model_dic setValue:@"model" forKey:@"control"];
  8473. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  8474. [model_dic setValue:description forKey:@"description"];
  8475. [model_dic setValue:line_note forKey:@"note"];
  8476. [model_dic setValue:img forKey:@"img_url"];
  8477. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  8478. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  8479. [model_dic setValue:Price forKey:@"unit_price"];
  8480. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  8481. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  8482. if (combine) {
  8483. [model_dic setValue:combine forKey:@"combine"];
  8484. }
  8485. // well,what under the row is the info for total
  8486. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  8487. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  8488. double weight=[bsubtotaljson[@"weight"] doubleValue];
  8489. int carton=[bsubtotaljson[@"carton"] intValue];
  8490. int mpack=[bsubtotaljson[@"mpack"] intValue];
  8491. model_dic[@"mpack"]=[NSNumber numberWithInt:mpack];
  8492. TotalCuft += cuft;
  8493. TotalWeight += weight;
  8494. TotalCarton += carton;
  8495. payments += subTotal;
  8496. //---------------------------
  8497. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  8498. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  8499. }];
  8500. [dic setValue:@"Model Information" forKey:@"title"];
  8501. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  8502. return dic;
  8503. }
  8504. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  8505. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8506. [dic setValue:@"Remarks Content" forKey:@"title"];
  8507. // setting
  8508. NSDictionary *setting = params[@"setting"];
  8509. NSNumber *hide = setting[@"RemarksContentHide"];
  8510. [dic setValue:hide forKey:@"hide"];
  8511. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8512. orderCode = [self translateSingleQuote:orderCode];
  8513. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes,schedule_date from offline_order where so_id = '%@';",orderCode];
  8514. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8515. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  8516. int mustCall = sqlite3_column_int(stmt, 1);
  8517. // NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  8518. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  8519. #ifdef BUILD_CONTRAST
  8520. NSString *schedule_date = [self textAtColumn:4 statement:stmt];
  8521. #endif
  8522. NSDictionary *po_dic = @{
  8523. @"aname" : @"PO#",
  8524. @"control" : @"edit",
  8525. @"keyboard" : @"text",
  8526. @"name" : @"poNumber",
  8527. @"value" : poNumber
  8528. };
  8529. #ifdef BUILD_CONTRAST
  8530. NSString* nsdate = [self changeDateFormate:schedule_date]; // 与在线统一
  8531. NSDictionary *schedule_dict = @{
  8532. @"aname" : @"Schdule Date",
  8533. @"control" : @"datepicker",
  8534. @"type": @"date",
  8535. @"required": @"true",
  8536. @"name" : @"schedule_date_str",
  8537. @"value" : nsdate
  8538. };
  8539. #endif
  8540. NSDictionary *must_call_dic = @{
  8541. @"aname" : @"MUST CALL BEFORE DELIVERY",
  8542. @"control" : @"switch",
  8543. @"name" : @"must_call",
  8544. @"value" : mustCall ? @"true" : @"false"
  8545. };
  8546. NSDictionary *general_notes_dic = @{
  8547. @"aname" : @"General notes",
  8548. @"control" : @"text_view",
  8549. @"keyboard" : @"text",
  8550. @"name" : @"comments",
  8551. @"value" : generalNotes
  8552. };
  8553. // NSDictionary *internal_notes_dic = @{
  8554. // @"aname" : @"Internal notes",
  8555. // @"control" : @"text_view",
  8556. // @"keyboard" : @"text",
  8557. // @"name" : @"internal_notes",
  8558. // @"value" : internalNotes
  8559. // };
  8560. #ifdef BUILD_CONTRAST
  8561. [dic setValue:po_dic forKey:@"item_0"];
  8562. [dic setValue:schedule_dict forKey:@"item_1"];
  8563. [dic setValue:must_call_dic forKey:@"item_2"];
  8564. [dic setValue:general_notes_dic forKey:@"item_3"];
  8565. #else
  8566. [dic setValue:po_dic forKey:@"item_0"];
  8567. [dic setValue:must_call_dic forKey:@"item_1"];
  8568. [dic setValue:general_notes_dic forKey:@"item_2"];
  8569. #endif
  8570. // [dic setValue:internal_notes_dic forKey:@"item_3"];
  8571. }];
  8572. #ifdef BUILD_CONTRAST
  8573. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  8574. #else
  8575. [dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8576. #endif
  8577. return dic;
  8578. }
  8579. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  8580. // params
  8581. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8582. orderCode = [self translateSingleQuote:orderCode];
  8583. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8584. // setting
  8585. NSDictionary *setting = params[@"setting"];
  8586. NSNumber *hide = setting[@"OrderTotalHide"];
  8587. [dic setValue:hide forKey:@"hide"];
  8588. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value,discount from offline_order where so_id = '%@';",orderCode];
  8589. __block double lift_gate_value = 0;
  8590. __block double handling_fee = 0;
  8591. __block double shipping = 0;
  8592. __block double discount = 0;
  8593. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8594. int lift_gate = sqlite3_column_int(stmt, 0);
  8595. lift_gate_value = sqlite3_column_double(stmt, 1);
  8596. shipping = sqlite3_column_double(stmt, 2);
  8597. handling_fee = sqlite3_column_double(stmt, 3);
  8598. discount = sqlite3_column_double(stmt, 4);
  8599. if (!lift_gate) {
  8600. lift_gate_value = 0;
  8601. }
  8602. }];
  8603. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  8604. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  8605. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  8606. double payments = [[total valueForKey:@"payments"] doubleValue];
  8607. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  8608. double totalPrice = payments* (1-discount/100.0);
  8609. [dic setValue:@"Order Total" forKey:@"title"];
  8610. NSDictionary *payments_dic = @{
  8611. @"align" : @"right",
  8612. @"aname" : @"Payments/Credits",
  8613. @"control" : @"text",
  8614. @"name" : @"paymentsAndCredits",
  8615. @"type" : @"price",
  8616. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  8617. };
  8618. [dic setValue:payments_dic forKey:@"item_0"];
  8619. // version 1.71 remove
  8620. // NSDictionary *handling_fee_dic = @{
  8621. // @"align" : @"right",
  8622. // @"aname" : @"Handling Fee",
  8623. // @"control" : @"text",
  8624. // @"name" : @"handling_fee_value",
  8625. // @"required" : @"true",
  8626. // @"type" : @"price",
  8627. // @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  8628. // };
  8629. NSDictionary *shipping_dic = @{
  8630. @"align" : @"right",
  8631. @"aname" : @"Shipping*",
  8632. @"control" : @"text",
  8633. @"name" : @"shipping",
  8634. @"required" : @"true",
  8635. @"type" : @"price",
  8636. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  8637. };
  8638. NSDictionary *lift_gate_dic = @{
  8639. @"align" : @"right",
  8640. @"aname" : @"Liftgate Fee(No Loading Dock)",
  8641. @"control" : @"text",
  8642. @"name" : @"lift_gate_value",
  8643. @"required" : @"true",
  8644. @"type" : @"price",
  8645. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  8646. };
  8647. int item_count = 1;
  8648. if (![[params objectForKey:@"logist"] isEqualToString:@"WILL CALL"]) {
  8649. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  8650. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  8651. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8652. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8653. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  8654. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8655. } else {
  8656. }
  8657. }
  8658. // version 1.71 remove
  8659. // [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8660. // NSString* strdiscount;
  8661. if(discount>0)
  8662. {
  8663. NSDictionary *discount_dict = @{
  8664. @"align" : @"right",
  8665. @"aname" : @"Order Discount",
  8666. @"control" : @"text",
  8667. @"name" : @"orderDiscount",
  8668. // @"type" : @"price",
  8669. @"value" : [NSString stringWithFormat:@"%d%% off",(int)discount]
  8670. };
  8671. [dic setValue:discount_dict forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8672. }
  8673. NSDictionary *total_price_dic = @{
  8674. @"align" : @"right",
  8675. @"aname" : @"Total",
  8676. @"control" : @"text",
  8677. @"name" : @"totalPrice",
  8678. @"type" : @"price",
  8679. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  8680. };
  8681. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8682. NSDictionary *total_cuft_dic = @{
  8683. @"align" : @"right",
  8684. @"aname" : @"Total Cuft",
  8685. @"control" : @"text",
  8686. @"name" : @"",
  8687. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  8688. };
  8689. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8690. NSDictionary *total_weight_dic = @{
  8691. @"align" : @"right",
  8692. @"aname" : @"Total Weight",
  8693. @"control" : @"text",
  8694. @"name" : @"",
  8695. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  8696. };
  8697. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8698. NSDictionary *total_carton_dic = @{
  8699. @"align" : @"right",
  8700. @"aname" : @"Total Carton",
  8701. @"control" : @"text",
  8702. @"name" : @"",
  8703. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  8704. };
  8705. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8706. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  8707. return dic;
  8708. }
  8709. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  8710. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8711. orderCode = [self translateSingleQuote:orderCode];
  8712. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8713. // setting
  8714. NSDictionary *setting = params[@"setting"];
  8715. NSNumber *hide = setting[@"SignatureHide"];
  8716. [dic setValue:hide forKey:@"hide"];
  8717. [dic setValue:@"Signature" forKey:@"title"];
  8718. __block NSString *pic_path = @"";
  8719. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  8720. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8721. pic_path = [self textAtColumn:0 statement:stmt];
  8722. }];
  8723. NSDictionary *pic_dic = @{
  8724. @"aname" : @"Signature",
  8725. @"avalue" :pic_path,
  8726. @"control" : @"signature",
  8727. @"name" : @"sign_picpath",
  8728. @"value" : pic_path
  8729. };
  8730. [dic setValue:pic_dic forKey:@"item_0"];
  8731. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8732. return dic;
  8733. }
  8734. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  8735. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8736. // setting
  8737. NSDictionary *setting = params[@"setting"];
  8738. NSNumber *hide = setting[@"ShippingMethodHide"];
  8739. [dic setValue:hide forKey:@"hide"];
  8740. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8741. orderCode = [self translateSingleQuote:orderCode];
  8742. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note,carrier from offline_order where so_id = '%@';",orderCode];
  8743. __block NSString *logist = @"";
  8744. __block NSString *carrier = @"";
  8745. __block NSString *lift_gate = @"";
  8746. __block int lift_gate_integer = 0;
  8747. __block NSString *logistic_note = @"";
  8748. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8749. logist = [self textAtColumn:0 statement:stmt];
  8750. lift_gate_integer = sqlite3_column_int(stmt, 1);
  8751. logistic_note = [self textAtColumn:2 statement:stmt];
  8752. carrier = [self textAtColumn:3 statement:stmt];
  8753. }];
  8754. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8755. [dic setValue:@"Shipping Method" forKey:@"title"];
  8756. // val_0
  8757. int PERSONAL_PICK_UP_check = 0;
  8758. int USE_MY_CARRIER_check = 0;
  8759. NSString *logistic_note_text = @"";
  8760. int will_call_check = 0;
  8761. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  8762. will_call_check = 1;
  8763. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  8764. PERSONAL_PICK_UP_check = 1;
  8765. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  8766. USE_MY_CARRIER_check = 1;
  8767. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  8768. logistic_note = [logistic_note_array firstObject];
  8769. if (logistic_note_array.count > 1) {
  8770. logistic_note_text = [logistic_note_array lastObject];
  8771. }
  8772. }
  8773. }
  8774. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  8775. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  8776. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  8777. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  8778. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  8779. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8780. NSDictionary *val0_subItem_item0 = @{
  8781. @"aname" : @"Option",
  8782. @"cadedate" : @{
  8783. @"count" : [NSNumber numberWithInteger:2],
  8784. @"val_0" : @{
  8785. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  8786. @"refresh" : [NSNumber numberWithInteger:0],
  8787. @"value" : @"PERSONAL PICK UP",
  8788. @"value_id" : @"PERSONAL PICK UP"
  8789. },
  8790. @"val_1" : @{
  8791. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  8792. @"refresh" : [NSNumber numberWithInteger:0],
  8793. @"sub_item" : @{
  8794. @"count" : [NSNumber numberWithInteger:1],
  8795. @"item_0" : @{
  8796. @"aname" : @"BOL",
  8797. @"control" : @"edit",
  8798. @"keyboard" : @"text",
  8799. @"name" : @"logist_note_text",
  8800. @"refresh" : [NSNumber numberWithInteger:0],
  8801. @"required" : @"false",
  8802. @"value" : logistic_note_text
  8803. }
  8804. },
  8805. @"value" : @"USE MY CARRIER",
  8806. @"value_id" : @"USE MY CARRIER"
  8807. }
  8808. },
  8809. @"control" : @"enum",
  8810. @"name" : @"logistic_note",
  8811. @"required" : @"true",
  8812. @"single_select" : @"true"
  8813. };
  8814. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  8815. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  8816. // val_1
  8817. lift_gate = [self valueInParams:params key:@"lift_gate"];
  8818. if([lift_gate isEqualToString:@""]) {
  8819. if (lift_gate_integer == 1) {
  8820. lift_gate = @"true";
  8821. } else {
  8822. lift_gate = @"false";
  8823. }
  8824. }
  8825. // carrier = [self valueInParams:params key:@"carrier"];
  8826. // if([carrier isEqualToString:@""]) {
  8827. //
  8828. // if (lift_gate_integer == 1) {
  8829. // lift_gate = @"true";
  8830. // } else {
  8831. // lift_gate = @"false";
  8832. // }
  8833. //
  8834. // }
  8835. int common_carrier_check = 0;
  8836. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  8837. common_carrier_check = 1;
  8838. [params setValue:lift_gate forKey:@"lift_gate"];
  8839. #ifdef BUILD_CONTRAST
  8840. [params setValue:carrier forKey:@"carrier"];
  8841. #endif
  8842. }
  8843. // NSString *Sql = [NSString stringWithFormat: @"select carrier from offline_contact where contact_id = %d;",contact_id];
  8844. // NSString * code = [iSalesDB jk_queryText:Sql];
  8845. // [self offline_getCarrierCode:offline_getCarrierCode];
  8846. #ifdef BUILD_CONTRAST
  8847. NSDictionary *carrierDic = [self offline_OrderCarrier:params[@"orderCode"] db:db];
  8848. #endif
  8849. NSDictionary *val_1 = @{
  8850. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  8851. @"sub_item" : @{
  8852. @"item_0" : @{
  8853. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  8854. @"control" : @"switch",
  8855. @"name" : @"lift_gate",
  8856. @"refresh" : [NSNumber numberWithInteger:1],
  8857. @"required" : @"true",
  8858. @"value" : lift_gate
  8859. },
  8860. #ifdef BUILD_CONTRAST
  8861. @"count" : [NSNumber numberWithInteger:2],
  8862. @"item_1" : @{
  8863. @"aname" : @"Carrier",
  8864. @"cadedate" :carrierDic,
  8865. @"control" : @"enum",
  8866. @"name" : @"carrier",
  8867. @"required" : @"false",
  8868. @"value" : @"",
  8869. @"single_select" : @"true"
  8870. }
  8871. #else
  8872. @"count" : [NSNumber numberWithInteger:1]
  8873. #endif
  8874. },
  8875. @"value" : @"COMMON CARRIER",
  8876. @"value_id" : @"COMMON CARRIER"
  8877. };
  8878. // cadedate
  8879. NSDictionary *cadedate = @{
  8880. @"count" : [NSNumber numberWithInteger:2],
  8881. @"val_0" : val_0,
  8882. @"val_1" : val_1
  8883. };
  8884. // item_0
  8885. NSMutableDictionary *item_0 = @{
  8886. @"aname" : @"Shipping",
  8887. @"cadedate" : cadedate,
  8888. @"control" : @"enum",
  8889. @"name" : @"logist",
  8890. @"refresh" : [NSNumber numberWithInteger:1],
  8891. @"single_select" : @"true",
  8892. }.mutableCopy;
  8893. NSNumber *required = setting[@"ShippingMethodRequire"];
  8894. if ([required integerValue]) {
  8895. [item_0 setValue:@"true" forKey:@"required"];
  8896. }
  8897. [dic setValue:item_0 forKey:@"item_0"];
  8898. if ([logist isEqualToString:@"WILL CALL"]) {
  8899. [dic removeObjectForKey:@"lift_gate"];
  8900. }
  8901. return dic;
  8902. }
  8903. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  8904. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8905. orderCode = [self translateSingleQuote:orderCode];
  8906. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  8907. __block int submit_as_integer = 0;
  8908. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8909. submit_as_integer = sqlite3_column_int(stmt, 0);
  8910. }];
  8911. int check11 = 0;
  8912. int check10 = 0;
  8913. if (submit_as_integer == 11) {
  8914. check11 = 1;
  8915. }
  8916. if (submit_as_integer == 10) {
  8917. check10 = 1;
  8918. }
  8919. // section_0
  8920. NSMutableDictionary *dic = @{
  8921. @"count" : @(1),
  8922. @"item_0" : @{
  8923. @"aname" : @"Submit Order As",
  8924. @"cadedate" : @{
  8925. @"count" : @(2),
  8926. @"val_0" : @{
  8927. @"check" : [NSNumber numberWithInteger:check11],
  8928. @"value" : @"Sales Order",
  8929. @"value_id" : @(11)
  8930. },
  8931. @"val_1" : @{
  8932. @"check" : [NSNumber numberWithInteger:check10],
  8933. @"value" : @"Quote",
  8934. @"value_id" : @(10)
  8935. }
  8936. },
  8937. @"control" : @"enum",
  8938. @"name" : @"erpOrderStatus",
  8939. @"required" : @"true",
  8940. @"single_select" : @"true"
  8941. },
  8942. @"title" : @"Order Type"
  8943. }.mutableCopy;
  8944. // setting
  8945. NSDictionary *setting = params[@"setting"];
  8946. NSNumber *hide = setting[@"OrderTypeHide"];
  8947. [dic setValue:hide forKey:@"hide"];
  8948. return dic;
  8949. }
  8950. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  8951. countryCode = [self translateSingleQuote:countryCode];
  8952. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  8953. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8954. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  8955. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  8956. if (name == NULL) {
  8957. name = "";
  8958. }
  8959. if (code == NULL) {
  8960. code = "";
  8961. }
  8962. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  8963. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  8964. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  8965. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  8966. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  8967. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  8968. }
  8969. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  8970. [container setValue:stateDic forKey:key];
  8971. }] mutableCopy];
  8972. [ret removeObjectForKey:@"result"];
  8973. // failure 可以不用了,一样的
  8974. if (ret.allKeys.count == 0) {
  8975. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  8976. [stateDic setValue:@"Other" forKey:@"value"];
  8977. [stateDic setValue:@"" forKey:@"value_id"];
  8978. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  8979. if (state_code && [@"" isEqualToString:state_code]) {
  8980. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  8981. }
  8982. NSString *key = [NSString stringWithFormat:@"val_0"];
  8983. [ret setValue:stateDic forKey:key];
  8984. }
  8985. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  8986. return ret;
  8987. }
  8988. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  8989. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8990. orderCode = [self translateSingleQuote:orderCode];
  8991. 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];
  8992. __block NSString *payType = @"";
  8993. __block NSString *firstName = @"";
  8994. __block NSString *lastName = @"";
  8995. __block NSString *addr1 = @"";
  8996. __block NSString *addr2 = @"";
  8997. __block NSString *zipcode = @"";
  8998. __block NSString *cardType = @"";
  8999. __block NSString *cardNumber = @"";
  9000. __block NSString *securityCode = @"";
  9001. __block NSString *month = @"";
  9002. __block NSString *year = @"";
  9003. __block NSString *city = @"";
  9004. __block NSString *state = @"";
  9005. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9006. payType = [self textAtColumn:0 statement:stmt];
  9007. firstName = [self textAtColumn:1 statement:stmt];
  9008. lastName = [self textAtColumn:2 statement:stmt];
  9009. addr1 = [self textAtColumn:3 statement:stmt];
  9010. addr2 = [self textAtColumn:4 statement:stmt];
  9011. zipcode = [self textAtColumn:5 statement:stmt];
  9012. cardType = [self textAtColumn:6 statement:stmt];
  9013. cardNumber = [self textAtColumn:7 statement:stmt];
  9014. securityCode = [self textAtColumn:8 statement:stmt];
  9015. month = [self textAtColumn:9 statement:stmt];
  9016. year = [self textAtColumn:10 statement:stmt];
  9017. city = [self textAtColumn:11 statement:stmt];
  9018. state = [self textAtColumn:12 statement:stmt];
  9019. }];
  9020. NSString *required = @"true";
  9021. // setting
  9022. NSDictionary *setting = params[@"setting"];
  9023. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  9024. if ([requiredNumber integerValue]) {
  9025. required = @"true";
  9026. } else {
  9027. required = @"false";
  9028. }
  9029. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  9030. // create table if not exists payment_type (_id integer primary key,type_id text,pay_type text,sort int);
  9031. NSString *pay_type_sql = @"select type_id,pay_type,sort_id from payment_type order by sort_id asc";
  9032. [iSalesDB jk_query:pay_type_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9033. NSString *type_id = [self textAtColumn:0 statement:stmt];
  9034. NSString *type = [self textAtColumn:1 statement:stmt];
  9035. NSMutableDictionary *val = @{
  9036. @"check" : [payType isEqualToString:type_id] ? @(1) : @(0),
  9037. @"value" : type,
  9038. @"value_id" : type_id
  9039. }.mutableCopy;
  9040. if ([type_id.lowercaseString isEqualToString:@"Visa/Master".lowercaseString] || [type_id.lowercaseString isEqualToString:@"Credit Card".lowercaseString]) {
  9041. NSDictionary *sub_item =nil;
  9042. #ifdef BUILD_CONTRAST
  9043. sub_item = @{
  9044. @"count" : @(3),
  9045. @"item_0" : @{
  9046. @"aname" : @"Number",
  9047. @"control" : @"edit",
  9048. @"keyboard" : @"int",
  9049. @"length" : @"16",
  9050. @"name" : @"credit_card_number",
  9051. @"required" : @"false",
  9052. @"value" : cardNumber
  9053. },
  9054. @"item_1" : @{
  9055. @"aname" : @"Expiration Date",
  9056. @"control" : @"monthpicker",
  9057. @"name" : @"credit_card_expiration",
  9058. @"required" : @"false",
  9059. @"type" : @"date",
  9060. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9061. },
  9062. @"item_2" : @{
  9063. @"aname" : @"Security Code",
  9064. @"control" : @"edit",
  9065. @"keyboard" : @"int",
  9066. @"length" : @"4",
  9067. @"name" : @"credit_card_security_code",
  9068. @"required" : @"false",
  9069. @"value" : securityCode
  9070. },
  9071. };
  9072. #else
  9073. sub_item= @{
  9074. @"count" : @(3),
  9075. @"item_0" : @{
  9076. @"aname" : @"choose",
  9077. @"control" : @"multi_action",
  9078. @"count" : @(1),
  9079. @"item_0" : @{
  9080. @"aname" : @"Same as customer",
  9081. @"key_map" : @{
  9082. @"credit_card_address1" : @"customer_address1",
  9083. @"credit_card_address2" : @"customer_address2",
  9084. @"credit_card_city" : @"customer_city",
  9085. @"credit_card_first_name" : @"customer_first_name",
  9086. @"credit_card_last_name" : @"customer_last_name",
  9087. @"credit_card_state" : @"customer_state",
  9088. @"credit_card_zipcode" : @"customer_zipcode"
  9089. },
  9090. @"type" : @"pull"
  9091. }
  9092. },
  9093. @"item_1" : @{
  9094. @"aname" : @"",
  9095. @"color" : @"red",
  9096. @"control" : @"text",
  9097. @"name" : @"",
  9098. @"value" : @"USA Credit cards only"
  9099. },
  9100. @"item_2" : @{
  9101. @"aname" : @"Fill",
  9102. @"cadedate" : @{
  9103. @"count" : @(2),
  9104. @"val_0" : @{
  9105. @"check" : @(1),
  9106. @"sub_item" : @{
  9107. @"count" : @(11),
  9108. @"item_0" : @{
  9109. @"aname" : @"Type",
  9110. @"cadedate" : @{
  9111. @"count" : @(2),
  9112. @"val_0" : @{
  9113. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  9114. @"value" : @"VISA",
  9115. @"value_id" : @"VISA"/*@(0)*/
  9116. },
  9117. @"val_1" : @{
  9118. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  9119. @"value" : @"MASTER CARD",
  9120. @"value_id" : @"MASTER CARD"/*@(1)*/
  9121. }
  9122. },
  9123. @"control" : @"enum",
  9124. @"name" : @"credit_card_type",
  9125. @"required" : @"true",
  9126. @"single_select" : @"true"
  9127. },
  9128. @"item_1" : @{
  9129. @"aname" : @"Number",
  9130. @"control" : @"edit",
  9131. @"keyboard" : @"int",
  9132. @"length" : @"16",
  9133. @"name" : @"credit_card_number",
  9134. @"required" : @"true",
  9135. @"value" : cardNumber
  9136. },
  9137. @"item_10" : @{
  9138. @"aname" : @"State",
  9139. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  9140. @"control" : @"enum",
  9141. @"enum" : @"true",
  9142. @"name" : @"credit_card_state",
  9143. @"required" : @"true",
  9144. @"single_select" : @"true"
  9145. },
  9146. @"item_2" : @{
  9147. @"aname" : @"Expiration Date",
  9148. @"control" : @"monthpicker",
  9149. @"name" : @"credit_card_expiration",
  9150. @"required" : @"true",
  9151. @"type" : @"date",
  9152. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9153. },
  9154. @"item_3" : @{
  9155. @"aname" : @"Security Code",
  9156. @"control" : @"edit",
  9157. @"keyboard" : @"int",
  9158. @"length" : @"3",
  9159. @"name" : @"credit_card_security_code",
  9160. @"required" : @"true",
  9161. @"value" : securityCode
  9162. },
  9163. @"item_4" : @{
  9164. @"aname" : @"First Name",
  9165. @"control" : @"edit",
  9166. @"keyboard" : @"text",
  9167. @"name" : @"credit_card_first_name",
  9168. @"required" : @"true",
  9169. @"value" : firstName
  9170. },
  9171. @"item_5" : @{
  9172. @"aname" : @"Last Name",
  9173. @"control" : @"edit",
  9174. @"keyboard" : @"text",
  9175. @"name" : @"credit_card_last_name",
  9176. @"required" : @"true",
  9177. @"value" : lastName
  9178. },
  9179. @"item_6" : @{
  9180. @"aname" : @"Address 1",
  9181. @"control" : @"edit",
  9182. @"keyboard" : @"text",
  9183. @"name" : @"credit_card_address1",
  9184. @"required" : @"true",
  9185. @"value" : addr1
  9186. },
  9187. @"item_7" : @{
  9188. @"aname" : @"Address 2",
  9189. @"control" : @"edit",
  9190. @"keyboard" : @"text",
  9191. @"name" : @"credit_card_address2",
  9192. @"value" : addr2
  9193. },
  9194. @"item_8" : @{
  9195. @"aname" : @"zip code",
  9196. @"control" : @"edit",
  9197. @"keyboard" : @"text",
  9198. @"name" : @"credit_card_zipcode",
  9199. @"required" : @"true",
  9200. @"value" : zipcode
  9201. },
  9202. @"item_9" : @{
  9203. @"aname" : @"City",
  9204. @"control" : @"edit",
  9205. @"keyboard" : @"text",
  9206. @"name" : @"credit_card_city",
  9207. @"required" : @"true",
  9208. @"value" : city
  9209. }
  9210. },
  9211. @"value" : @"Fill Now",
  9212. @"value_id" : @""
  9213. },
  9214. @"val_1" : @{
  9215. @"check" : @(0),
  9216. @"value" : @"Fill Later",
  9217. @"value_id" : @""
  9218. }
  9219. },
  9220. @"control" : @"enum",
  9221. @"name" : @"",
  9222. @"single_select" : @"true"
  9223. }
  9224. };
  9225. #endif
  9226. [val setObject:sub_item forKey:@"sub_item"];
  9227. }
  9228. [cadedate setObject:val forKey:[NSString stringWithFormat:@"val_%ld",(*count)]];
  9229. [cadedate setObject:[NSNumber numberWithInteger:(++*count)] forKey:@"count"];
  9230. }];
  9231. // "section_2"
  9232. NSMutableDictionary *dic = @{
  9233. @"count" : @(1),
  9234. @"item_0" : @{
  9235. @"aname" : @"Payment",
  9236. @"required" : required,
  9237. @"cadedate" : cadedate,
  9238. // @{
  9239. // @"count" : @(6),
  9240. // @"val_3" : @{
  9241. // @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  9242. // @"value" : @"Check",
  9243. // @"value_id" : @"Check"
  9244. // },
  9245. // @"val_2" : @{
  9246. // @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  9247. // @"value" : @"Cash",
  9248. // @"value_id" : @"Cash"
  9249. // },
  9250. // @"val_1" : @{
  9251. // @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  9252. // @"value" : @"NET 60",
  9253. // @"value_id" : @"NET 30"
  9254. // },
  9255. // @"val_4" : @{
  9256. // @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  9257. // @"value" : @"Wire Transfer",
  9258. // @"value_id" : @"Wire Transfer"
  9259. // },
  9260. // @"val_0" : @{
  9261. // @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  9262. // @"sub_item" : @{
  9263. // @"count" : @(3),
  9264. // @"item_0" : @{
  9265. // @"aname" : @"choose",
  9266. // @"control" : @"multi_action",
  9267. // @"count" : @(1),
  9268. // @"item_0" : @{
  9269. // @"aname" : @"Same as customer",
  9270. // @"key_map" : @{
  9271. // @"credit_card_address1" : @"customer_address1",
  9272. // @"credit_card_address2" : @"customer_address2",
  9273. // @"credit_card_city" : @"customer_city",
  9274. // @"credit_card_first_name" : @"customer_first_name",
  9275. // @"credit_card_last_name" : @"customer_last_name",
  9276. // @"credit_card_state" : @"customer_state",
  9277. // @"credit_card_zipcode" : @"customer_zipcode"
  9278. // },
  9279. // @"type" : @"pull"
  9280. // }
  9281. // },
  9282. // @"item_1" : @{
  9283. // @"aname" : @"",
  9284. // @"color" : @"red",
  9285. // @"control" : @"text",
  9286. // @"name" : @"",
  9287. // @"value" : @"USA Credit cards only"
  9288. // },
  9289. // @"item_2" : @{
  9290. // @"aname" : @"Fill",
  9291. // @"cadedate" : @{
  9292. // @"count" : @(2),
  9293. // @"val_0" : @{
  9294. // @"check" : @(1),
  9295. // @"sub_item" : @{
  9296. // @"count" : @(11),
  9297. // @"item_0" : @{
  9298. // @"aname" : @"Type",
  9299. // @"cadedate" : @{
  9300. // @"count" : @(2),
  9301. // @"val_0" : @{
  9302. // @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  9303. // @"value" : @"VISA",
  9304. // @"value_id" : @(0)
  9305. // },
  9306. // @"val_1" : @{
  9307. // @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  9308. // @"value" : @"MASTER CARD",
  9309. // @"value_id" : @(1)
  9310. // }
  9311. // },
  9312. // @"control" : @"enum",
  9313. // @"name" : @"credit_card_type",
  9314. // @"required" : @"true",
  9315. // @"single_select" : @"true"
  9316. // },
  9317. // @"item_1" : @{
  9318. // @"aname" : @"Number",
  9319. // @"control" : @"edit",
  9320. // @"keyboard" : @"int",
  9321. // @"length" : @"16",
  9322. // @"name" : @"credit_card_number",
  9323. // @"required" : @"true",
  9324. // @"value" : cardNumber
  9325. // },
  9326. // @"item_10" : @{
  9327. // @"aname" : @"State",
  9328. // @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  9329. // @"control" : @"enum",
  9330. // @"enum" : @"true",
  9331. // @"name" : @"credit_card_state",
  9332. // @"required" : @"true",
  9333. // @"single_select" : @"true"
  9334. // },
  9335. // @"item_2" : @{
  9336. // @"aname" : @"Expiration Date",
  9337. // @"control" : @"monthpicker",
  9338. // @"name" : @"credit_card_expiration",
  9339. // @"required" : @"true",
  9340. // @"type" : @"date",
  9341. // @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9342. // },
  9343. // @"item_3" : @{
  9344. // @"aname" : @"Security Code",
  9345. // @"control" : @"edit",
  9346. // @"keyboard" : @"int",
  9347. // @"length" : @"3",
  9348. // @"name" : @"credit_card_security_code",
  9349. // @"required" : @"true",
  9350. // @"value" : securityCode
  9351. // },
  9352. // @"item_4" : @{
  9353. // @"aname" : @"First Name",
  9354. // @"control" : @"edit",
  9355. // @"keyboard" : @"text",
  9356. // @"name" : @"credit_card_first_name",
  9357. // @"required" : @"true",
  9358. // @"value" : firstName
  9359. // },
  9360. // @"item_5" : @{
  9361. // @"aname" : @"Last Name",
  9362. // @"control" : @"edit",
  9363. // @"keyboard" : @"text",
  9364. // @"name" : @"credit_card_last_name",
  9365. // @"required" : @"true",
  9366. // @"value" : lastName
  9367. // },
  9368. // @"item_6" : @{
  9369. // @"aname" : @"Address 1",
  9370. // @"control" : @"edit",
  9371. // @"keyboard" : @"text",
  9372. // @"name" : @"credit_card_address1",
  9373. // @"required" : @"true",
  9374. // @"value" : addr1
  9375. // },
  9376. // @"item_7" : @{
  9377. // @"aname" : @"Address 2",
  9378. // @"control" : @"edit",
  9379. // @"keyboard" : @"text",
  9380. // @"name" : @"credit_card_address2",
  9381. // @"value" : addr2
  9382. // },
  9383. // @"item_8" : @{
  9384. // @"aname" : @"zip code",
  9385. // @"control" : @"edit",
  9386. // @"keyboard" : @"text",
  9387. // @"name" : @"credit_card_zipcode",
  9388. // @"required" : @"true",
  9389. // @"value" : zipcode
  9390. // },
  9391. // @"item_9" : @{
  9392. // @"aname" : @"City",
  9393. // @"control" : @"edit",
  9394. // @"keyboard" : @"text",
  9395. // @"name" : @"credit_card_city",
  9396. // @"required" : @"true",
  9397. // @"value" : city
  9398. // }
  9399. // },
  9400. // @"value" : @"Fill Now",
  9401. // @"value_id" : @""
  9402. // },
  9403. // @"val_1" : @{
  9404. // @"check" : @(0),
  9405. // @"value" : @"Fill Later",
  9406. // @"value_id" : @""
  9407. // }
  9408. // },
  9409. // @"control" : @"enum",
  9410. // @"name" : @"",
  9411. // @"single_select" : @"true"
  9412. // }
  9413. // },
  9414. // @"value" : @"Visa/Master",
  9415. // @"value_id" : @"Credit Card"
  9416. // },
  9417. // @"val_5" : @{
  9418. // @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  9419. // @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  9420. // @"value_id" : @"FOLLOW EXISTING"
  9421. // }
  9422. // },
  9423. @"control" : @"enum",
  9424. @"name" : @"paymentType",
  9425. @"single_select" : @"true"
  9426. },
  9427. @"title" : @"Payment Information"
  9428. }.mutableCopy;
  9429. NSNumber *hide = setting[@"PaymentInformationHide"];
  9430. [dic setValue:hide forKey:@"hide"];
  9431. return dic;
  9432. }
  9433. + (BOOL)orderOutOfStock:(NSDictionary *)params db:(sqlite3 *)db{
  9434. // params
  9435. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9436. orderCode = [self translateSingleQuote:orderCode];
  9437. // 缺货检查
  9438. 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];
  9439. __block BOOL outOfStock = NO;
  9440. sqlite3 *database = db;
  9441. if (!db) {
  9442. database = [iSalesDB get_db];
  9443. }
  9444. [iSalesDB jk_query:out_of_stock_SQL db:database close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9445. int availability = sqlite3_column_int(stmt, 0);
  9446. int item_qty = sqlite3_column_int(stmt, 1);
  9447. outOfStock = outOfStock || (availability < item_qty ? YES : NO);
  9448. }];
  9449. if (!db) {
  9450. [iSalesDB close_db:database];
  9451. }
  9452. return outOfStock;
  9453. }
  9454. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  9455. {
  9456. assert(params[@"user"]!=nil);
  9457. assert(params[@"contact_id"]!=nil);
  9458. assert(params[@"can_create_backorder"]!=nil);
  9459. sqlite3 *db = [iSalesDB get_db];
  9460. // params
  9461. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9462. orderCode = [self translateSingleQuote:orderCode];
  9463. // 缺货检查
  9464. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9465. if (![params[@"can_create_backorder"] boolValue]) {
  9466. BOOL out_of_stock = [self orderOutOfStock:params db:db];
  9467. if (out_of_stock) {
  9468. [iSalesDB close_db:db];
  9469. NSMutableDictionary *resultDic = [NSMutableDictionary dictionary];
  9470. [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"];
  9471. [resultDic setObject:[NSNumber numberWithInt:8] forKey:@"result"];
  9472. return [RAConvertor dict2data:resultDic];
  9473. }
  9474. }
  9475. // UISetting
  9476. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9477. NSString *cachefolder = [paths objectAtIndex:0];
  9478. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  9479. NSData* json =nil;
  9480. json=[NSData dataWithContentsOfFile:img_cache];
  9481. NSError *error=nil;
  9482. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  9483. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  9484. [params setObject:setting forKey:@"setting"];
  9485. int section_count = 0;
  9486. // 0 Order Type 1 Shipping Method 2 Payment Information
  9487. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  9488. // 0 Order Type
  9489. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  9490. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  9491. [ret setValue:order_type_dic forKey:key0];
  9492. // 1 Shipping Method
  9493. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  9494. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  9495. [ret setValue:shipping_method_dic forKey:key1];
  9496. // 2 Payment Information
  9497. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  9498. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  9499. [ret setValue:payment_info_dic forKey:key2];
  9500. // 3 Customer
  9501. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  9502. NSDictionary *customer_dic = [self customerDic:params db:db];
  9503. [ret setValue:customer_dic forKey:key3];
  9504. // 4 Ship To
  9505. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  9506. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  9507. [ret setValue:ship_to_dic forKey:key4];
  9508. // 5 Ship From
  9509. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  9510. if (![shipFromDisable integerValue]) {
  9511. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  9512. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  9513. [ret setValue:ship_from_dic forKey:key5];
  9514. }
  9515. // 6 Freight Bill To
  9516. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  9517. if (![freightBillToDisable integerValue]) {
  9518. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  9519. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  9520. [ret setValue:freight_bill_to forKey:key6];
  9521. }
  9522. // 7 Merchandise Bill To
  9523. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  9524. if (![merchandiseBillToDisable integerValue]) {
  9525. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  9526. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  9527. [ret setValue:merchandise_bill_to_dic forKey:key7];
  9528. }
  9529. // 8 Return To
  9530. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  9531. if (![returnToDisable integerValue]) {
  9532. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  9533. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  9534. [ret setValue:return_to_dic forKey:key8];
  9535. }
  9536. // 9 Model Information
  9537. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  9538. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  9539. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  9540. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  9541. [ret setValue:model_info_dic forKey:key9];
  9542. // 10 Remarks Content
  9543. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  9544. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  9545. [ret setValue:remarks_content_dic forKey:key10];
  9546. // 11 Order Total
  9547. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  9548. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  9549. [ret setValue:order_total_dic forKey:key11];
  9550. // 12 Signature
  9551. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  9552. NSDictionary *sign_dic = [self signatureDic:params db:db];
  9553. [ret setValue:sign_dic forKey:key12];
  9554. [ret setValue:@(section_count) forKey:@"section_count"];
  9555. [iSalesDB close_db:db];
  9556. return [RAConvertor dict2data:ret];
  9557. // return nil;
  9558. }
  9559. #pragma mark addr editor
  9560. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  9561. NSMutableDictionary *new_item = [item mutableCopy];
  9562. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  9563. return new_item;
  9564. }
  9565. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  9566. {
  9567. // "is_subaction" = true;
  9568. // orderCode = MOB1608240002;
  9569. // password = 123456;
  9570. // "subaction_tag" = 1;
  9571. // user = EvanK;
  9572. // {
  9573. // "is_subaction" = true;
  9574. // orderCode = MOB1608240002;
  9575. // password = 123456;
  9576. // "refresh_trigger" = zipcode;
  9577. // "subaction_tag" = 1;
  9578. // user = EvanK;
  9579. // }
  9580. NSString *country_code = nil;
  9581. NSString *zipCode = nil;
  9582. NSString *stateCode = nil;
  9583. NSString *city = nil;
  9584. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  9585. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  9586. NSString *code_id = params[@"country"];
  9587. country_code = [self countryCodeByid:code_id];
  9588. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  9589. NSString *zip_code = params[@"zipcode"];
  9590. // 剔除全部为空格
  9591. int spaceCount = 0;
  9592. for (int i = 0; i < zip_code.length; i++) {
  9593. if ([zip_code characterAtIndex:i] == ' ') {
  9594. spaceCount++;
  9595. }
  9596. }
  9597. if (spaceCount == zip_code.length) {
  9598. zip_code = @"";
  9599. }
  9600. zipCode = zip_code;
  9601. if (zipCode.length > 0) {
  9602. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  9603. country_code = [dic valueForKey:@"country_code"];
  9604. if (!country_code) {
  9605. // country_code = @"US";
  9606. NSString *code_id = params[@"country"];
  9607. country_code = [self countryCodeByid:code_id];
  9608. }
  9609. stateCode = [dic valueForKey:@"state_code"];
  9610. if(!stateCode.length) {
  9611. stateCode = params[@"state"];
  9612. }
  9613. city = [dic valueForKey:@"city"];
  9614. if (!city.length) {
  9615. city = params[@"city"];
  9616. }
  9617. // zip code
  9618. // NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  9619. // [zipDic setValue:zipCode forKey:@"value"];
  9620. // [section_0 setValue:zipDic forKey:@"item_11"];
  9621. } else {
  9622. NSString *code_id = params[@"country"];
  9623. country_code = [self countryCodeByid:code_id];
  9624. stateCode = params[@"state"];
  9625. city = params[@"city"];
  9626. }
  9627. }
  9628. }
  9629. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  9630. // [ret removeObjectForKey:@"up_params"];
  9631. [ret setObject:@"New Address" forKey:@"title"];
  9632. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  9633. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  9634. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  9635. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  9636. // [country_dic removeObjectForKey:@"refresh"];
  9637. // [country_dic removeObjectForKey:@"restore"];
  9638. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  9639. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  9640. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  9641. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  9642. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  9643. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  9644. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  9645. // [zip_code_dic removeObjectForKey:@"refresh"];
  9646. [zip_code_dic setValue:zipCode ? zipCode :@"" forKey:@"value"];
  9647. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  9648. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  9649. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  9650. NSDictionary *cityDic = [[new_section_0 objectForKey:@"item_8"] mutableCopy];
  9651. [cityDic setValue:city ? city : @"" forKey:@"value"];
  9652. [new_section_0 setObject:cityDic forKey:@"item_8"];
  9653. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  9654. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  9655. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  9656. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  9657. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  9658. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  9659. // country
  9660. NSString *countryCode = country_code == nil ? @"US" : country_code;
  9661. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  9662. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  9663. // state
  9664. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:stateCode] mutableCopy];
  9665. // NSDictionary *tmpDic = @{
  9666. // @"value" : @"Other",
  9667. // @"value_id" : @"",
  9668. // @"check" : [NSNumber numberWithInteger:0]
  9669. // };
  9670. //
  9671. // for (int i = 0; i < allState.allKeys.count; i++) {
  9672. // NSString *key = [NSString stringWithFormat:@"val_%d",i];
  9673. //
  9674. // NSDictionary *tmp = allState[key];
  9675. // [allState setValue:tmpDic forKey:key];
  9676. // tmpDic = tmp;
  9677. // }
  9678. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  9679. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  9680. [ret setValue:new_section_0 forKey:@"section_0"];
  9681. return [RAConvertor dict2data:ret];
  9682. }
  9683. #pragma mark save addr
  9684. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  9685. {
  9686. // NSString *companyName = [self valueInParams:params key:@"company"];
  9687. // NSString *addr1 = [self valueInParams:params key:@"address"];
  9688. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  9689. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  9690. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  9691. // NSString *countryId = [self valueInParams:params key:@"country"];
  9692. // NSString *stateCode = [self valueInParams:params key:@"state"];
  9693. // NSString *city = [self valueInParams:params key:@"city"];
  9694. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  9695. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  9696. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  9697. // NSString *phone = [self valueInParams:params key:@"phone"];
  9698. // NSString *fax = [self valueInParams:params key:@"fax"];
  9699. // NSString *email = [self valueInParams:params key:@"email"];
  9700. return [self offline_saveContact:params update:NO isCustomer:NO];
  9701. }
  9702. #pragma mark cancel order
  9703. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  9704. {
  9705. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9706. NSString *order_id = [self valueInParams:params key:@"orderId"];
  9707. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  9708. if (order_id.length) {
  9709. sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  9710. }
  9711. int ret = [iSalesDB execSql:sql];
  9712. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9713. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9714. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9715. // [dic setValue:@"160409" forKey:@"min_ver"];
  9716. return [RAConvertor dict2data:dic];
  9717. }
  9718. #pragma mark sign order
  9719. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  9720. {
  9721. //参考 offline_saveBusinesscard
  9722. DebugLog(@"sign order params: %@",params);
  9723. // orderCode = MOB1608240002;
  9724. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  9725. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9726. orderCode = [self translateSingleQuote:orderCode];
  9727. NSString *picPath = [self valueInParams:params key:@"picpath"];
  9728. picPath = [self translateSingleQuote:picPath];
  9729. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  9730. int ret = [iSalesDB execSql:sql];
  9731. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9732. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9733. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9734. // [dic setValue:@"160409" forKey:@"min_ver"];
  9735. return [RAConvertor dict2data:dic];
  9736. }
  9737. #pragma mark save order
  9738. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  9739. NSString *ret = [self valueInParams:params key:key];
  9740. if (translate) {
  9741. ret = [self translateSingleQuote:ret];
  9742. }
  9743. return ret;
  9744. }
  9745. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  9746. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  9747. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  9748. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  9749. NSString* schedule_date = [self valueInParams:param key:@"schedule_date_str"];
  9750. if (schedule_date.length==0 ) {
  9751. schedule_date = @"";
  9752. } else {
  9753. schedule_date = [self rchangeDateFormate:schedule_date];
  9754. schedule_date = [NSString stringWithFormat:@"schedule_date = '%@',",schedule_date];
  9755. }
  9756. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  9757. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  9758. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  9759. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  9760. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  9761. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  9762. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  9763. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  9764. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  9765. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  9766. if (![total_price isEqualToString:@""]) {
  9767. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  9768. } else {
  9769. total_price = @"";
  9770. }
  9771. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  9772. if ([paymentsAndCredits isEqualToString:@""]) {
  9773. paymentsAndCredits = @"";
  9774. } else {
  9775. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  9776. }
  9777. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  9778. if ([handling_fee_value isEqualToString:@""]) {
  9779. handling_fee_value = @"";
  9780. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  9781. handling_fee_value = @"";
  9782. } else {
  9783. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  9784. }
  9785. NSString *handling_fee_placeholder = handling_fee_value;
  9786. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  9787. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  9788. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  9789. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  9790. NSString *lift_gate_placeholder = @"";
  9791. if ([lift_gate_value isEqualToString:@""]) {
  9792. lift_gate_placeholder = @"";
  9793. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  9794. lift_gate_placeholder = @"";
  9795. } else {
  9796. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  9797. }
  9798. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  9799. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  9800. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  9801. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  9802. 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];
  9803. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  9804. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  9805. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  9806. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  9807. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  9808. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  9809. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  9810. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  9811. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  9812. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  9813. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  9814. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  9815. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  9816. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  9817. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  9818. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  9819. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  9820. BOOL number_nil = [credit_card_number isEqualToString:@""];
  9821. if (!number_nil) {
  9822. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  9823. }
  9824. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  9825. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  9826. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  9827. if (!security_code_nil) {
  9828. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  9829. }
  9830. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  9831. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  9832. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  9833. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  9834. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  9835. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  9836. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  9837. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  9838. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  9839. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  9840. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  9841. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  9842. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  9843. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  9844. NSString *contact_id = customer_cid;
  9845. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  9846. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  9847. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  9848. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  9849. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  9850. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  9851. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  9852. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  9853. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  9854. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  9855. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  9856. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  9857. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  9858. //
  9859. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  9860. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  9861. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  9862. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  9863. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  9864. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  9865. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  9866. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  9867. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  9868. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  9869. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  9870. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  9871. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  9872. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  9873. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  9874. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  9875. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  9876. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  9877. 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];
  9878. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  9879. if (receive_cid.length) {
  9880. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  9881. }
  9882. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  9883. if (receive_name.length) {
  9884. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  9885. }
  9886. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  9887. if (receive_ext.length) {
  9888. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  9889. }
  9890. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  9891. if (receive_contact.length) {
  9892. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  9893. }
  9894. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  9895. if (receive_phone.length) {
  9896. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  9897. }
  9898. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  9899. if (receive_email.length) {
  9900. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  9901. }
  9902. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  9903. if (receive_fax.length) {
  9904. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  9905. }
  9906. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  9907. if (sender_cid.length) {
  9908. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  9909. }
  9910. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  9911. if (sender_name.length) {
  9912. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  9913. }
  9914. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  9915. if (sender_ext.length) {
  9916. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  9917. }
  9918. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  9919. if(sender_contact.length) {
  9920. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  9921. }
  9922. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  9923. if (sender_phone.length) {
  9924. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  9925. }
  9926. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  9927. if (sender_fax.length) {
  9928. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  9929. }
  9930. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  9931. if (sender_email.length) {
  9932. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  9933. }
  9934. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  9935. if (shipping_billto_cid.length) {
  9936. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  9937. }
  9938. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  9939. if (shipping_billto_name.length) {
  9940. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  9941. }
  9942. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  9943. if (shipping_billto_ext.length) {
  9944. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  9945. }
  9946. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  9947. if (shipping_billto_contact.length) {
  9948. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  9949. }
  9950. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  9951. if (shipping_billto_phone.length) {
  9952. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  9953. }
  9954. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  9955. if (shipping_billto_fax.length) {
  9956. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  9957. }
  9958. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  9959. if (shipping_billto_email.length) {
  9960. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  9961. }
  9962. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  9963. if (billing_cid.length) {
  9964. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  9965. }
  9966. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  9967. if (billing_name.length) {
  9968. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  9969. }
  9970. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  9971. if (billing_ext.length) {
  9972. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  9973. }
  9974. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  9975. if (billing_contact.length) {
  9976. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  9977. }
  9978. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  9979. if (billing_phone.length) {
  9980. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  9981. }
  9982. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  9983. if (billing_fax.length) {
  9984. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  9985. }
  9986. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  9987. if (billing_email.length) {
  9988. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  9989. }
  9990. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  9991. if (returnto_cid.length) {
  9992. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  9993. }
  9994. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  9995. if (returnto_name.length) {
  9996. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  9997. }
  9998. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  9999. if (returnto_ext.length) {
  10000. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  10001. }
  10002. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  10003. if (returnto_contact.length) {
  10004. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  10005. }
  10006. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  10007. if (returnto_phone.length) {
  10008. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  10009. }
  10010. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  10011. if (returnto_fax.length) {
  10012. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  10013. }
  10014. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  10015. if (returnto_email.length) {
  10016. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  10017. }
  10018. NSString *order_status = @"status = 1,";
  10019. if (submit) {
  10020. order_status = @"status = -11,";
  10021. }
  10022. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  10023. NSString* carrier = [self offline_getCarrier:[param[@"carrier"] intValue]];
  10024. NSString* scarrier = [NSString stringWithFormat:@"carrier = '%@',",carrier];
  10025. NSString *sync_sql = @"";
  10026. if (submit) {
  10027. param[@"truck_carrier"]=carrier;
  10028. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  10029. [param setValue:sales_rep forKey:@"sales_rep"];
  10030. NSString *sync_data = [self translateSingleQuote:[RAConvertor dict2string:param]];
  10031. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  10032. }
  10033. 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];
  10034. DebugLog(@"save order contactSql: %@",contactSql);
  10035. DebugLog(@"save order orderSql: %@",orderSql);
  10036. // int contact_ret = [iSalesDB execSql:contactSql];
  10037. int order_ret = [iSalesDB execSql:orderSql];
  10038. int ret = order_ret;
  10039. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10040. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  10041. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10042. // [dic setValue:@"160409" forKey:@"min_ver"];
  10043. [dic setObject:so_id forKey:@"so#"];
  10044. return [RAConvertor dict2data:dic];
  10045. }
  10046. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  10047. {
  10048. // id foo = nil;
  10049. // NSMutableArray *a = @[].mutableCopy;
  10050. // [a addObject:foo];
  10051. return [self saveorder:param submit:NO];
  10052. }
  10053. #pragma mark add to cart by name
  10054. +(NSData*) offline_add2cartbymodelQR :(NSMutableDictionary *) params
  10055. {
  10056. NSString *orderCode = [params objectForKey:@"orderCode"];
  10057. NSDictionary *newParams = @{
  10058. @"product_id" : params[@"product_id_string"],
  10059. @"orderCode" : orderCode,
  10060. @"can_create_backorder":params[@"can_create_backorder"]
  10061. };
  10062. return [self offline_add2cart:[newParams mutableCopy]];
  10063. }
  10064. +(NSData*) offline_add2cartbyupc :(NSMutableDictionary *) params
  10065. {
  10066. NSString *orderCode = [params objectForKey:@"orderCode"];
  10067. NSString *upccode = [params objectForKey:@"upc_code"];
  10068. // product_name = [self translateSingleQuote:product_name];
  10069. // NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  10070. // bool search_upc = [params[@"search_upc"] boolValue];
  10071. sqlite3 *db = [iSalesDB get_db];
  10072. __block NSMutableString *product_id_string = [NSMutableString string];
  10073. // NSString *name = [product_name_array objectAtIndex:i];
  10074. NSString *sql =nil;
  10075. 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];
  10076. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10077. int product_id = sqlite3_column_int(stmt, 0);
  10078. [product_id_string appendFormat:@"%d",product_id];
  10079. }];
  10080. if (!orderCode) {
  10081. orderCode = @"";
  10082. }
  10083. NSDictionary *newParams = @{
  10084. @"product_id" : product_id_string,
  10085. @"orderCode" : orderCode,
  10086. @"can_create_backorder":params[@"can_create_backorder"]
  10087. };
  10088. [iSalesDB close_db:db];
  10089. return [self offline_add2cart:[newParams mutableCopy]];
  10090. }
  10091. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  10092. {
  10093. NSString *orderCode = [params objectForKey:@"orderCode"];
  10094. NSString *product_name = [params objectForKey:@"product_name"];
  10095. product_name = [self translateSingleQuote:product_name];
  10096. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  10097. bool search_upc = [params[@"search_upc"] boolValue];
  10098. sqlite3 *db = [iSalesDB get_db];
  10099. __block NSMutableString *product_id_string = [NSMutableString string];
  10100. for (int i = 0; i < product_name_array.count; i++) {
  10101. NSString *name = [product_name_array objectAtIndex:i];
  10102. NSString *sql =nil;
  10103. if(search_upc)
  10104. 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];
  10105. else
  10106. sql= [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  10107. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10108. int product_id = sqlite3_column_int(stmt, 0);
  10109. if (i == product_name_array.count - 1) {
  10110. [product_id_string appendFormat:@"%d",product_id];
  10111. } else {
  10112. [product_id_string appendFormat:@"%d,",product_id];
  10113. }
  10114. }];
  10115. }
  10116. if (!orderCode) {
  10117. orderCode = @"";
  10118. }
  10119. NSDictionary *newParams = @{
  10120. @"product_id" : product_id_string,
  10121. @"orderCode" : orderCode,
  10122. @"can_create_backorder":params[@"can_create_backorder"]
  10123. };
  10124. [iSalesDB close_db:db];
  10125. return [self offline_add2cart:[newParams mutableCopy]];
  10126. }
  10127. #pragma mark reset order
  10128. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  10129. {
  10130. // UIApplication * app = [UIApplication sharedApplication];
  10131. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  10132. [iSalesDB disable_trigger];
  10133. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  10134. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  10135. [iSalesDB enable_trigger];
  10136. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10137. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  10138. return [RAConvertor dict2data:dic];
  10139. }
  10140. #pragma mark submit order
  10141. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  10142. {
  10143. return [self saveorder:params submit:YES];
  10144. }
  10145. #pragma mark copy order
  10146. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  10147. {
  10148. NSMutableDictionary *ret = @{}.mutableCopy;
  10149. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  10150. sqlite3 *db = [iSalesDB get_db];
  10151. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  10152. // 首先查看联系人是否active
  10153. 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];
  10154. __block int customer_is_active = 1;
  10155. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10156. customer_is_active = sqlite3_column_int(stmt, 0);
  10157. }];
  10158. if (!customer_is_active) {
  10159. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10160. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10161. [iSalesDB close_db:db];
  10162. return [RAConvertor dict2data:ret];
  10163. }
  10164. // new order
  10165. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  10166. NSString *new_order_code = [self get_offline_soid:db];
  10167. NSString *user =params[@"user"];// ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  10168. user = [self translateSingleQuote:user];
  10169. 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
  10170. __block int result = 1;
  10171. result = [iSalesDB execSql:insert_order_sql db:db];
  10172. if (!result) {
  10173. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10174. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10175. [iSalesDB close_db:db];
  10176. return [RAConvertor dict2data:ret];
  10177. }
  10178. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  10179. // __block NSString *product_id = @"";
  10180. __weak typeof(self) weakSelf = self;
  10181. 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];
  10182. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10183. int is_active = sqlite3_column_int(stmt, 1);
  10184. if (is_active) {
  10185. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  10186. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  10187. 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];
  10188. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  10189. }
  10190. }];
  10191. // product_id = [product_id substringFromIndex:1];
  10192. //
  10193. // [self offline_add2cart:@{}.mutableCopy];
  10194. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10195. [iSalesDB close_db:db];
  10196. if (result) {
  10197. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10198. } else {
  10199. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10200. }
  10201. [ret setObject:new_order_code forKey:@"so_id"];
  10202. return [RAConvertor dict2data:ret];
  10203. }
  10204. #pragma mark move wish list to cart
  10205. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  10206. assert(params[@"can_create_backorder"]!=nil);
  10207. // cart中存在的Model在wish list move to cart之后,cart中Model数量为1
  10208. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10209. NSString *collectId = params[@"collectId"];
  10210. 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];
  10211. __block NSString *product_id = @"";
  10212. __block NSString *qty = @"";
  10213. __block int number_of_outOfStock = 0;
  10214. __block NSMutableArray *delete_collectId = [NSMutableArray array];
  10215. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10216. int productId = sqlite3_column_int(stmt, 0);
  10217. int item_qty = sqlite3_column_int(stmt, 1);
  10218. int availability = sqlite3_column_int(stmt, 2);
  10219. int _id = sqlite3_column_int(stmt, 3);
  10220. if (![params[@"can_create_backorder"] boolValue]) {
  10221. // 库存小于购买量为缺货
  10222. if (availability >= item_qty) {
  10223. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  10224. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  10225. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  10226. } else {
  10227. number_of_outOfStock++;
  10228. }
  10229. } else {
  10230. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  10231. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  10232. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  10233. }
  10234. }];
  10235. NSMutableDictionary *retDic = nil;
  10236. if (![params[@"can_create_backorder"] boolValue]) {
  10237. if (delete_collectId.count == 0) {
  10238. retDic = [NSMutableDictionary dictionary];
  10239. retDic[@"result"] = [NSNumber numberWithInteger:8];
  10240. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  10241. return [RAConvertor dict2data:retDic];
  10242. }
  10243. }
  10244. if (product_id.length > 1) {
  10245. product_id = [product_id substringFromIndex:1];
  10246. }
  10247. if (qty.length > 1) {
  10248. qty = [qty substringFromIndex:1];
  10249. }
  10250. NSString *orderCode = params[@"orderCode"];
  10251. if (!orderCode) {
  10252. orderCode = @"";
  10253. }
  10254. NSDictionary *newParams = @{
  10255. @"product_id" : product_id,
  10256. @"orderCode" : orderCode,
  10257. @"qty" : qty,
  10258. @"can_create_backorder":params[@"can_create_backorder"]
  10259. };
  10260. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  10261. retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  10262. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  10263. NSMutableDictionary* wish_return = [[self offline_deletewishlist:@{@"collectId" : [delete_collectId componentsJoinedByString:@","]}.mutableCopy] mutableCopy];
  10264. NSInteger ret = [wish_return[@"result"] intValue];
  10265. retDic[@"wish_count"]=wish_return[@"wish_count"];
  10266. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  10267. }
  10268. if (![params[@"can_create_backorder"] boolValue]) {
  10269. if (number_of_outOfStock > 0) {
  10270. retDic[@"result"] = [NSNumber numberWithInteger:8];
  10271. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  10272. }
  10273. }
  10274. return [RAConvertor dict2data:retDic];
  10275. }
  10276. #pragma mark - portfolio
  10277. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  10278. // assert(params[@"contact_id"]!=nil);
  10279. assert(params[@"user"]!=nil);
  10280. assert(params[@"can_see_price"]!=nil);
  10281. int sort = [[params valueForKey:@"sort"] intValue];
  10282. int offset = [[params valueForKey:@"offset"] intValue];
  10283. int limit = [[params valueForKey:@"limit"] intValue];
  10284. NSString *orderBy = @"";
  10285. switch (sort) {
  10286. case 0:{
  10287. orderBy = @"p.modify_time desc";
  10288. }
  10289. break;
  10290. case 1:{
  10291. orderBy = @"modify_time asc";
  10292. }
  10293. break;
  10294. case 2:{
  10295. orderBy = @"p.name asc";
  10296. }
  10297. break;
  10298. case 3:{
  10299. orderBy = @"p.name desc";
  10300. }
  10301. break;
  10302. case 4:{
  10303. orderBy = @"p.description asc";
  10304. }
  10305. break;
  10306. case 5: {
  10307. orderBy = @"m.default_category asc";
  10308. }
  10309. default:
  10310. break;
  10311. }
  10312. // 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];
  10313. 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];
  10314. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10315. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10316. sqlite3 *db = [iSalesDB get_db];
  10317. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10318. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10319. int product_id = sqlite3_column_int(stmt, 0);
  10320. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  10321. NSString *name = [self textAtColumn:1 statement:stmt];
  10322. NSString *description = [self textAtColumn:2 statement:stmt];
  10323. double price = sqlite3_column_double(stmt, 3);
  10324. double discount = sqlite3_column_double(stmt,4);
  10325. int qty = sqlite3_column_int(stmt, 5);
  10326. int percentage = sqlite3_column_int(stmt, 6);
  10327. int item_id = sqlite3_column_int(stmt, 7);
  10328. // int fashion_id = sqlite3_column_int(stmt, 8);
  10329. NSString *img_path = [self textAtColumn:9 statement:stmt];
  10330. NSString *line_note = [self textAtColumn:10 statement:stmt];
  10331. double percent = sqlite3_column_double(stmt, 11);
  10332. NSString *price_null = [self textAtColumn:3 statement:stmt];
  10333. if ([price_null isEqualToString:@"null"]) {
  10334. price = [[self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  10335. }
  10336. NSMutableDictionary *item = @{
  10337. @"linenotes": line_note,
  10338. @"check": @(1),
  10339. @"product_id": product_id_string,
  10340. @"available_qty": @(qty),
  10341. @"available_percent" : @(percent),
  10342. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  10343. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  10344. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  10345. @"img": img_path,
  10346. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  10347. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  10348. }.mutableCopy;
  10349. if (percentage) {
  10350. [item removeObjectForKey:@"available_qty"];
  10351. } else {
  10352. [item removeObjectForKey:@"available_percent"];
  10353. }
  10354. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  10355. if ([qty_null isEqualToString:@"null"]) {
  10356. [item removeObjectForKey:@"available_qty"];
  10357. }
  10358. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10359. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10360. }];
  10361. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  10362. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10363. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  10364. }
  10365. [iSalesDB close_db:db];
  10366. [dic setValue:[NSNumber numberWithBool:[params[@"can_see_price"] boolValue]] forKey:@"can_see_price"];
  10367. [dic setValue:@"" forKey:@"email_content"];
  10368. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  10369. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10370. return [RAConvertor dict2data:dic];
  10371. }
  10372. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  10373. __block int qty = 0;
  10374. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  10375. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10376. qty = sqlite3_column_int(stmt, 0);
  10377. }];
  10378. return qty;
  10379. }
  10380. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct contact_id:(NSString* ) contact_id
  10381. {
  10382. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  10383. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  10384. // NSMutableDictionary * values = params[@"replaceValue"];
  10385. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  10386. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  10387. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  10388. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  10389. NSString *pdf_path = @"";
  10390. if (direct) {
  10391. configureParams = [self valueInParams:params key:@"pdfUrl"];
  10392. } else {
  10393. pdf_path = [self valueInParams:params key:@"pdfPath"];
  10394. }
  10395. NSString *create_user = [self valueInParams:params key:@"user"];
  10396. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  10397. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  10398. // model info
  10399. // 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];
  10400. // V1.90 more color
  10401. 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];
  10402. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10403. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  10404. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10405. sqlite3 *db = [iSalesDB get_db];
  10406. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10407. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10408. int product_id = sqlite3_column_int(stmt, 0);
  10409. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  10410. double price = sqlite3_column_double(stmt, 1);
  10411. double discount = sqlite3_column_double(stmt,2);
  10412. int qty = sqlite3_column_int(stmt, 3);
  10413. int percentage = sqlite3_column_int(stmt, 4);
  10414. int item_id = sqlite3_column_int(stmt, 5);
  10415. NSString *line_note = [self textAtColumn:6 statement:stmt];
  10416. double percent = sqlite3_column_double(stmt, 7);
  10417. int more_color = sqlite3_column_int(stmt, 8);
  10418. NSString *price_null = [self textAtColumn:1 statement:stmt];
  10419. /* if ([price_null isEqualToString:@"null"]) {
  10420. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  10421. }
  10422. */
  10423. [product_ids_string appendFormat:@"%@,",product_id_string];
  10424. // Regular Price
  10425. int regular_price = [[params objectForKey:@"regular_price"] intValue];
  10426. NSString *regular_price_str = [self get_portfolio_price:contact_id item_id:item_id price:regular_price db:db];
  10427. // QTY
  10428. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  10429. if ([qty_null isEqualToString:@"null"]) {
  10430. // 查available
  10431. qty = [self model_QTY:product_id_string db:db];
  10432. }
  10433. if (percentage) {
  10434. qty = qty * percent / 100;
  10435. }
  10436. // Special Price
  10437. if ([price_null isEqualToString:@"null"]) {
  10438. // price = regular price
  10439. price = [regular_price_str doubleValue];
  10440. }
  10441. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  10442. if (![discount_null isEqualToString:@"null"]) {
  10443. price = price * (1 - discount / 100.0);
  10444. }
  10445. NSMutableDictionary *item = @{
  10446. @"line_note": line_note,
  10447. @"product_id": product_id_string,
  10448. @"available_qty": @(qty),
  10449. @"more_color":@(more_color),
  10450. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  10451. @"regular_price" : regular_price_str,
  10452. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  10453. }.mutableCopy;
  10454. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10455. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10456. }];
  10457. [iSalesDB close_db:db];
  10458. if (product_ids_string.length > 0) {
  10459. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  10460. }
  10461. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  10462. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  10463. return [RAConvertor dict2data:resultDictionary];
  10464. }
  10465. NSString *model_info = [RAConvertor dict2string:dic];
  10466. // 创建PDF
  10467. // 在preview情况下保存,则不需要新建了
  10468. if (direct) {
  10469. NSMutableDictionary *tear_sheet_params = params;
  10470. // if (tear_sheet_id) {
  10471. // tear_sheet_params = values;
  10472. // }
  10473. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  10474. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  10475. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  10476. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  10477. resultDictionary = pdfInfo.mutableCopy;
  10478. } else { // 创建PDF失败
  10479. return pdfData;
  10480. }
  10481. } else {
  10482. // pdf_path 就是本地路径
  10483. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  10484. }
  10485. // 将文件移动到PDF Cache文件夹
  10486. NSString *newPath = [pdf_path lastPathComponent];
  10487. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10488. NSString *cachefolder = [paths objectAtIndex:0];
  10489. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10490. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  10491. NSFileManager *fileManager = [NSFileManager defaultManager];
  10492. NSError *error = nil;
  10493. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  10494. if (error) { // 移动文件失败
  10495. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  10496. return [RAConvertor dict2data:resultDictionary];
  10497. }
  10498. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  10499. pdf_path = [newPath lastPathComponent];
  10500. // 保存信息
  10501. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  10502. NSString *off_params = [RAConvertor dict2string:params];
  10503. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  10504. pdf_path = [self translateSingleQuote:pdf_path];
  10505. create_user = [self translateSingleQuote:create_user];
  10506. tear_note = [self translateSingleQuote:tear_note];
  10507. tear_name = [self translateSingleQuote:tear_name];
  10508. model_info = [self translateSingleQuote:model_info];
  10509. configureParams = [self translateSingleQuote:configureParams];
  10510. off_params = [self translateSingleQuote:off_params];
  10511. 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];
  10512. if (tear_sheet_id) {
  10513. int _id = [tear_sheet_id intValue];
  10514. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  10515. }
  10516. int result = [iSalesDB execSql:save_pdf_sql];
  10517. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10518. //
  10519. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  10520. if (remove_Item) {
  10521. // portfolioId
  10522. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  10523. }
  10524. return [RAConvertor dict2data:resultDictionary];
  10525. }
  10526. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  10527. return [self offline_savePDF:params direct:YES contact_id:params[@"contact_id"]];
  10528. }
  10529. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  10530. return [self offline_savePDF:params direct:NO contact_id:params[@"contact_id"]];
  10531. }
  10532. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  10533. int offset = [[params valueForKey:@"offset"] intValue];
  10534. int limit = [[params valueForKey:@"limit"] intValue];
  10535. NSString *keyword = [params valueForKey:@"keyWord"];
  10536. NSString *where = @"where is_delete is null or is_delete = 0";
  10537. if (keyword.length) {
  10538. keyword = [self translateSingleQuote:keyword];
  10539. 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];
  10540. }
  10541. 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
  10542. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10543. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10544. NSString *cachefolder = [paths objectAtIndex:0];
  10545. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10546. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10547. NSString *name = [self textAtColumn:0 statement:stmt];
  10548. NSString *note = [self textAtColumn:1 statement:stmt];
  10549. NSString *time = [self textAtColumn:2 statement:stmt];
  10550. NSString *user = [self textAtColumn:3 statement:stmt];
  10551. NSString *path = [self textAtColumn:4 statement:stmt];
  10552. NSString *model_info = [self textAtColumn:6 statement:stmt];
  10553. NSString *off_params = [self textAtColumn:7 statement:stmt];
  10554. path = [pdfFolder stringByAppendingPathComponent:path];
  10555. BOOL bdir=NO;
  10556. NSFileManager* fileManager = [NSFileManager defaultManager];
  10557. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  10558. {
  10559. //pdf文件不存在
  10560. path=nil;
  10561. }
  10562. time = [self changeDateTimeFormate:time];
  10563. time = [time stringByAppendingString:@" PST"];
  10564. int sheet_id = sqlite3_column_int(stmt, 5);
  10565. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  10566. item[@"tearsheetsId"]=@(sheet_id);
  10567. item[@"pdf_path"]=path;
  10568. item[@"create_time"]=time;
  10569. item[@"create_user"]=user;
  10570. item[@"tear_note"]=note;
  10571. item[@"tear_name"]=name;
  10572. item[@"isLocalFile"]=[NSNumber numberWithBool:YES];
  10573. item[@"model_info"]=model_info;
  10574. item[@"off_params"]=off_params;
  10575. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10576. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10577. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10578. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10579. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  10580. }];
  10581. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10582. return [RAConvertor dict2data:dic];
  10583. }
  10584. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  10585. NSString *prodct_ids = [params objectForKey:@"product_id"];
  10586. // NSString *user = [params objectForKey:@"user"];
  10587. // 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];
  10588. 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];
  10589. int result = [iSalesDB execSql:sql];
  10590. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10591. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  10592. if (result == RESULT_TRUE) {
  10593. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  10594. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  10595. sqlite3 *db = [iSalesDB get_db];
  10596. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  10597. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  10598. [iSalesDB close_db:db];
  10599. }
  10600. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10601. return [RAConvertor dict2data:dic];
  10602. }
  10603. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  10604. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  10605. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  10606. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  10607. NSMutableDictionary *section_0 = [[dic objectForKey:@"section_0"] mutableCopy];
  10608. NSDictionary *company_item = @{
  10609. @"control": @"text",
  10610. @"keyboard": @"text",
  10611. @"name": @"company_name",
  10612. @"value": COMPANY_FULL_NAME,
  10613. @"aname": @"Company Name"
  10614. };
  10615. [section_0 setObject:company_item forKey:@"item_0"];
  10616. [dic setObject:section_0 forKey:@"section_0"];
  10617. // Regurlar Price
  10618. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  10619. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  10620. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  10621. __block long val_count = 0;
  10622. NSString *sql = @"select name,type,order_by from price order by order_by";
  10623. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10624. NSString *name = [self textAtColumn:0 statement:stmt];
  10625. int type = sqlite3_column_int(stmt, 1);
  10626. int order_by = sqlite3_column_int(stmt, 2);
  10627. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10628. NSDictionary *price_dic = @{
  10629. @"value" : name,
  10630. @"value_id" : [NSNumber numberWithInteger:type],
  10631. @"check" : order_by == 0 ? @(1) : @(0)
  10632. };
  10633. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  10634. val_count = ++(*count);
  10635. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10636. val_count = 0;
  10637. }];
  10638. [cadedate setObject:@{@"value" : @"None",
  10639. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  10640. val_count++;
  10641. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  10642. [price setObject:cadedate forKey:@"cadedate"];
  10643. [section1 setObject:price forKey:@"item_2"];
  10644. [dic setObject:section1 forKey:@"section_1"];
  10645. return [RAConvertor dict2data:dic];
  10646. }
  10647. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  10648. NSString *product_id = [params objectForKey:@"fashionId"];
  10649. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  10650. __block int result = RESULT_TRUE;
  10651. __block int qty = 0;
  10652. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10653. qty = sqlite3_column_int(stmt, 0);
  10654. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10655. result = RESULT_FALSE;
  10656. }];
  10657. NSMutableDictionary *dic = @{
  10658. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  10659. @"mode" : @"Regular Mode",
  10660. @"quantity_available" : @(qty),
  10661. @"result" : @(result),
  10662. }.mutableCopy;
  10663. return [RAConvertor dict2data:dic];
  10664. }
  10665. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  10666. NSString *item_ids = [params objectForKey:@"item_id"];
  10667. NSString *line_notes = [params objectForKey:@"notes"];
  10668. NSString *price_str = [params objectForKey:@"price"];
  10669. NSString *discount_str = [params objectForKey:@"discount"];
  10670. NSString *percent = [params objectForKey:@"available_percent"];
  10671. NSString *qty = [params objectForKey:@"available_qty"];
  10672. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  10673. int dot = 0;
  10674. if (line_notes) {
  10675. line_notes = [self translateSingleQuote:line_notes];
  10676. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  10677. sql = [sql stringByAppendingString:line_notes];
  10678. dot = 1;
  10679. } else {
  10680. line_notes = @"";
  10681. }
  10682. if (price_str) {
  10683. if (dot) {
  10684. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  10685. } else {
  10686. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  10687. dot = 1;
  10688. }
  10689. sql = [sql stringByAppendingString:price_str];
  10690. } else {
  10691. price_str = @"";
  10692. }
  10693. if (discount_str) {
  10694. if (dot) {
  10695. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  10696. } else {
  10697. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  10698. dot = 1;
  10699. }
  10700. sql = [sql stringByAppendingString:discount_str];
  10701. } else {
  10702. discount_str = @"";
  10703. }
  10704. if (percent) {
  10705. if (dot) {
  10706. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  10707. } else {
  10708. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  10709. dot = 1;
  10710. }
  10711. sql = [sql stringByAppendingString:percent];
  10712. } else {
  10713. percent = @"";
  10714. }
  10715. if (qty) {
  10716. if (dot) {
  10717. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  10718. } else {
  10719. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  10720. dot = 1;
  10721. }
  10722. sql = [sql stringByAppendingString:qty];
  10723. } else {
  10724. qty = @"";
  10725. }
  10726. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  10727. sql = [sql stringByAppendingString:where];
  10728. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10729. int result = [iSalesDB execSql:sql];
  10730. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10731. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10732. return [RAConvertor dict2data:dic];
  10733. }
  10734. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  10735. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  10736. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10737. sqlite3 *db = [iSalesDB get_db];
  10738. 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];
  10739. int result = [iSalesDB execSql:sql db:db];
  10740. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10741. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10742. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  10743. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  10744. [iSalesDB close_db:db];
  10745. return [RAConvertor dict2data:dic];
  10746. }
  10747. + (void)offline_removePDFWithName:(NSString *)name {
  10748. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10749. NSString *cachefolder = [paths objectAtIndex:0];
  10750. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10751. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  10752. NSFileManager *fileManager = [NSFileManager defaultManager];
  10753. [fileManager removeItemAtPath:path error:nil];
  10754. }
  10755. + (void)offline_clear_PDFCache {
  10756. NSFileManager *fileManager = [NSFileManager defaultManager];
  10757. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10758. NSString *cachefolder = [paths objectAtIndex:0];
  10759. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10760. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  10761. for (NSString *path in pdf_files) {
  10762. [self offline_removePDFWithName:[path lastPathComponent]];
  10763. }
  10764. }
  10765. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  10766. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10767. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  10768. NSString *user = [params objectForKey:@"user"];
  10769. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %ld;",(long)tearsheetsId]]; // tearsheets_id ---> _id
  10770. if (![create_user isEqualToString:user]) {
  10771. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  10772. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10773. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  10774. return [RAConvertor dict2data:dic];
  10775. }
  10776. 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]];
  10777. __block int is_local = 0;
  10778. __block NSString *path = @"";
  10779. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10780. is_local = sqlite3_column_int(stmt, 0);
  10781. path = [self textAtColumn:1 statement:stmt];
  10782. }];
  10783. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  10784. if (is_local == 1) {
  10785. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  10786. // 删除文件
  10787. [self offline_removePDFWithName:path];
  10788. }
  10789. int result = [iSalesDB execSql:sql];
  10790. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10791. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10792. return [RAConvertor dict2data:dic];
  10793. }
  10794. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial params:(NSMutableDictionary*) add_params
  10795. {
  10796. // assert(add_params[@"contact_id"]!=nil);
  10797. assert(add_params[@"user"]!=nil);
  10798. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10799. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  10800. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  10801. NSString* where=@"1=1";
  10802. if (ver!=nil) {
  10803. where=@"is_dirty=1";
  10804. }
  10805. 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];
  10806. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  10807. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10808. sqlite3 *db = [iSalesDB get_db];
  10809. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10810. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  10811. NSInteger _id = sqlite3_column_int(stmt, 0);
  10812. NSInteger product_id = sqlite3_column_int(stmt, 1);
  10813. NSString *name = [self textAtColumn:2 statement:stmt];
  10814. NSString *desc = [self textAtColumn:3 statement:stmt];
  10815. NSInteger item_id = sqlite3_column_int(stmt, 4);
  10816. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  10817. NSInteger qty = sqlite3_column_int(stmt, 6);
  10818. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  10819. double percent = sqlite3_column_double(stmt, 8);
  10820. double price = sqlite3_column_double(stmt, 9);
  10821. double discount = sqlite3_column_double(stmt, 10);
  10822. NSString *img = [self textAtColumn:11 statement:stmt];
  10823. NSString *line_note = [self textAtColumn:12 statement:stmt];
  10824. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  10825. NSString *create_time = [self textAtColumn:14 statement:stmt];
  10826. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  10827. NSString *price_null = [self textAtColumn:9 statement:stmt];
  10828. if ([price_null isEqualToString:@"null"]) {
  10829. price = [[self get_model_default_price:add_params[@"contact_id"] user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  10830. }
  10831. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  10832. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  10833. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  10834. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  10835. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  10836. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  10837. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  10838. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  10839. if ([qty_null isEqualToString:@"null"]) {
  10840. // [item setValue:@"null" forKey:@"available_qty"];
  10841. } else {
  10842. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  10843. }
  10844. if ([is_percent_null isEqualToString:@"null"]) {
  10845. // [item setValue:@"null" forKey:@"percentage"];
  10846. } else {
  10847. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  10848. }
  10849. if ([percent_null isEqualToString:@"null"]) {
  10850. // [item setValue:@"null" forKey:@"percent"];
  10851. } else {
  10852. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  10853. }
  10854. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  10855. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  10856. [item setValue:name forKey:@"name"];
  10857. [item setValue:desc forKey:@"description"];
  10858. [item setValue:img forKey:@"img"];
  10859. [item setValue:line_note forKey:@"line_note"];
  10860. [item setValue:create_time forKey:@"createtime"];
  10861. [item setValue:modify_time forKey:@"modifytime"];
  10862. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  10863. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10864. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  10865. }];
  10866. [iSalesDB close_db:db];
  10867. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  10868. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10869. }
  10870. return ret;
  10871. }
  10872. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  10873. {
  10874. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10875. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  10876. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  10877. NSString* where=@"1=1";
  10878. if (ver!=nil) {
  10879. where=@"is_dirty=1";
  10880. }
  10881. 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];
  10882. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  10883. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10884. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  10885. NSInteger _id = sqlite3_column_int(stmt, 0);
  10886. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  10887. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  10888. NSString *create_user = [self textAtColumn:3 statement:stmt];
  10889. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  10890. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  10891. NSString *model_info = [self textAtColumn:6 statement:stmt];
  10892. NSString *createtime = [self textAtColumn:7 statement:stmt];
  10893. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  10894. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  10895. NSString *off_params = [self textAtColumn:10 statement:stmt];
  10896. NSString *uuid = [NSUUID UUID].UUIDString;
  10897. int is_delete = sqlite3_column_int(stmt, 11);
  10898. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  10899. int is_local = sqlite3_column_int(stmt, 12);
  10900. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  10901. if (![tearsheet_id_null isEqualToString:@"null"]) {
  10902. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  10903. }
  10904. [item setObject:pdf_path forKey:@"pdf_path"];
  10905. [item setObject:create_user forKey:@"create_user"];
  10906. [item setObject:tear_note forKey:@"tear_note"];
  10907. [item setObject:tear_name forKey:@"tear_name"];
  10908. [item setObject:model_info forKey:@"model_info"];
  10909. [item setObject:createtime forKey:@"createtime"];
  10910. [item setObject:modifytime forKey:@"modifytime"];
  10911. [item setObject:urlParams forKey:@"urlParams"];
  10912. [item setObject:off_params forKey:@"off_params"];
  10913. [item setObject:uuid forKey:@"pdf_token"];
  10914. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  10915. NSDictionary *off_Params_dic = [RAConvertor string2dict:off_params];
  10916. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  10917. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  10918. } else {
  10919. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  10920. }
  10921. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10922. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  10923. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10924. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10925. }];
  10926. return ret;
  10927. }
  10928. #pragma mark 2020
  10929. +(void) offline_contactlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  10930. {
  10931. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  10932. sqlite3 *db = [iSalesDB get_db];
  10933. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  10934. if (contactType) {
  10935. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  10936. } else {
  10937. contactType = @"1 = 1";
  10938. }
  10939. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  10940. DebugLog(@"offline contact list keyword: %@",keyword);
  10941. // advanced search
  10942. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  10943. if (contact_name) {
  10944. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10945. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  10946. } else {
  10947. contact_name = @"";
  10948. }
  10949. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  10950. if (customer_phone) {
  10951. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10952. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  10953. } else {
  10954. customer_phone = @"";
  10955. }
  10956. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  10957. if (customer_fax) {
  10958. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10959. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  10960. } else {
  10961. customer_fax = @"";
  10962. }
  10963. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  10964. if (customer_zipcode) {
  10965. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10966. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  10967. } else {
  10968. customer_zipcode = @"";
  10969. }
  10970. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  10971. if (customer_sales_rep) {
  10972. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10973. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  10974. } else {
  10975. customer_sales_rep = @"";
  10976. }
  10977. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  10978. if (customer_state) {
  10979. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10980. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  10981. } else {
  10982. customer_state = @"";
  10983. }
  10984. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  10985. if (customer_name) {
  10986. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10987. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  10988. } else {
  10989. customer_name = @"";
  10990. }
  10991. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  10992. if (customer_country) {
  10993. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10994. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  10995. } else {
  10996. customer_country = @"";
  10997. }
  10998. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  10999. if (customer_cid) {
  11000. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11001. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  11002. } else {
  11003. customer_cid = @"";
  11004. }
  11005. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  11006. if (customer_city) {
  11007. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11008. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  11009. } else {
  11010. customer_city = @"";
  11011. }
  11012. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  11013. if (customer_address) {
  11014. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11015. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  11016. } else {
  11017. customer_address = @"";
  11018. }
  11019. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  11020. if (customer_email) {
  11021. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11022. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  11023. } else {
  11024. customer_email = @"";
  11025. }
  11026. NSString *price_name = [params valueForKey:@"price_name"];
  11027. if (price_name) {
  11028. if ([price_name containsString:@","]) {
  11029. // 首先从 price表中查处name
  11030. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  11031. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  11032. for (int i = 1;i < pArray.count;i++) {
  11033. NSString *p = pArray[i];
  11034. [mutablePStr appendFormat:@" or type = %@ ",p];
  11035. }
  11036. [mutablePStr appendString:@";"];
  11037. __block NSMutableArray *price_name_array = [NSMutableArray array];
  11038. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11039. char *name = (char *)sqlite3_column_text(stmt, 0);
  11040. if (!name)
  11041. name = "";
  11042. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  11043. }];
  11044. // 再根据name 拼sql
  11045. NSMutableString *mutable_price_name = [NSMutableString string];
  11046. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  11047. for (int i = 1; i < price_name_array.count; i++) {
  11048. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  11049. }
  11050. [mutable_price_name appendString:@")"];
  11051. price_name = mutable_price_name;
  11052. } else {
  11053. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  11054. if ([price_name isEqualToString:@""]) {
  11055. price_name = @"";
  11056. } else {
  11057. __block NSString *price;
  11058. price_name = [self translateSingleQuote:price_name];
  11059. [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) {
  11060. char *p = (char *)sqlite3_column_text(stmt, 0);
  11061. if (p == NULL) {
  11062. p = "";
  11063. }
  11064. price = [NSString stringWithUTF8String:p];
  11065. }];
  11066. if ([price isEqualToString:@""]) {
  11067. price_name = @"";
  11068. } else {
  11069. price = [self translateSingleQuote:price];
  11070. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  11071. }
  11072. }
  11073. }
  11074. } else {
  11075. price_name = @"";
  11076. }
  11077. int limit = [[params valueForKey:@"limit"] intValue];
  11078. int offset = [[params valueForKey:@"offset"] intValue];
  11079. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11080. 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];
  11081. 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];
  11082. // int result= [iSalesDB AddExFunction:db];
  11083. int count =0;
  11084. NSString *sqlQuery = nil;
  11085. if(keyword.length==0)
  11086. {
  11087. // 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];
  11088. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  11089. sqlQuery = sql;
  11090. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  11091. }
  11092. else
  11093. {
  11094. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  11095. keyword = keyword.lowercaseString;
  11096. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11097. 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];
  11098. 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]];
  11099. }
  11100. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  11101. sqlite3_stmt * statement;
  11102. [ret setValue:@"2" forKey:@"result"];
  11103. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  11104. // 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";
  11105. int i = 0;
  11106. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11107. {
  11108. while (sqlite3_step(statement) == SQLITE_ROW)
  11109. {
  11110. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  11111. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11112. int editable = sqlite3_column_int(statement, 0);
  11113. char *company_name = (char*)sqlite3_column_text(statement, 1);
  11114. NSString *nscompany_name =nil;
  11115. if(company_name==nil)
  11116. nscompany_name=@"";
  11117. else
  11118. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  11119. char *country = (char*)sqlite3_column_text(statement, 2);
  11120. if(country==nil)
  11121. country="";
  11122. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  11123. // char *addr = (char*)sqlite3_column_text(statement, 3);
  11124. // if(addr==nil)
  11125. // addr="";
  11126. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  11127. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  11128. if(zipcode==nil)
  11129. zipcode="";
  11130. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  11131. char *state = (char*)sqlite3_column_text(statement, 5);
  11132. if(state==nil)
  11133. state="";
  11134. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  11135. char *city = (char*)sqlite3_column_text(statement, 6);
  11136. if(city==nil)
  11137. city="";
  11138. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  11139. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  11140. // NSString *nscontact_name = nil;
  11141. // if(contact_name==nil)
  11142. // nscontact_name=@"";
  11143. // else
  11144. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  11145. char *phone = (char*)sqlite3_column_text(statement, 8);
  11146. NSString *nsphone = nil;
  11147. if(phone==nil)
  11148. nsphone=@"";
  11149. else
  11150. nsphone= [[NSString alloc]initWithUTF8String:phone];
  11151. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  11152. if(contact_id==nil)
  11153. contact_id="";
  11154. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  11155. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  11156. if(addr_1==nil)
  11157. addr_1="";
  11158. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  11159. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  11160. if(addr_2==nil)
  11161. addr_2="";
  11162. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  11163. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  11164. if(addr_3==nil)
  11165. addr_3="";
  11166. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  11167. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  11168. if(addr_4==nil)
  11169. addr_4="";
  11170. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  11171. char *first_name = (char*)sqlite3_column_text(statement, 14);
  11172. if(first_name==nil)
  11173. first_name="";
  11174. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  11175. char *last_name = (char*)sqlite3_column_text(statement, 15);
  11176. if(last_name==nil)
  11177. last_name="";
  11178. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  11179. char *fax = (char*)sqlite3_column_text(statement, 16);
  11180. NSString *nsfax = nil;
  11181. if(fax==nil)
  11182. nsfax=@"";
  11183. else
  11184. {
  11185. nsfax= [[NSString alloc]initWithUTF8String:fax];
  11186. if(nsfax.length>0)
  11187. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  11188. }
  11189. char *email = (char*)sqlite3_column_text(statement, 17);
  11190. NSString *nsemail = nil;
  11191. if(email==nil)
  11192. nsemail=@"";
  11193. else
  11194. {
  11195. nsemail= [[NSString alloc]initWithUTF8String:email];
  11196. if(nsemail.length>0)
  11197. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  11198. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  11199. }
  11200. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  11201. [arr_name addObject:nsfirst_name];
  11202. [arr_name addObject:nslast_name];
  11203. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  11204. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  11205. {
  11206. // decrypt
  11207. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  11208. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  11209. nsphone=[AESCrypt fastdecrypt:nsphone];
  11210. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11211. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  11212. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11213. }
  11214. [arr_addr addObject:nscompany_name];
  11215. [arr_addr addObject:nscontact_name];
  11216. [arr_addr addObject:@"<br>"];
  11217. [arr_addr addObject:nsaddr_1];
  11218. [arr_addr addObject:nsaddr_2];
  11219. [arr_addr addObject:nsaddr_3];
  11220. [arr_addr addObject:nsaddr_4];
  11221. //[arr_addr addObject:nsaddr];
  11222. [arr_addr addObject:nszipcode];
  11223. [arr_addr addObject:nscity];
  11224. [arr_addr addObject:nsstate];
  11225. [arr_addr addObject:nscountry];
  11226. [arr_addr addObject:@"<br>"];
  11227. [arr_addr addObject:nsphone];
  11228. [arr_addr addObject:nsfax];
  11229. [arr_addr addObject:nsemail];
  11230. NSString * name = [RAConvertor arr2string:arr_addr separator:@", " trim:true];
  11231. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  11232. [item setValue:name forKey:@"name"];
  11233. [item setValue:nscontact_id forKey:@"contact_id"];
  11234. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  11235. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11236. i++;
  11237. }
  11238. sqlite3_finalize(statement);
  11239. }
  11240. [iSalesDB close_db:db];
  11241. #ifdef DEBUG
  11242. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11243. #endif
  11244. dispatch_async(dispatch_get_main_queue(), ^{
  11245. UIApplication * app = [UIApplication sharedApplication];
  11246. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11247. [ret setValue:appDelegate.mode forKey:@"mode"];
  11248. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  11249. result(ret);
  11250. });
  11251. return ;
  11252. });
  11253. }
  11254. + (void)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11255. {
  11256. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  11257. NSString* order_code= appDelegate.order_code;
  11258. NSString* user = appDelegate.user;
  11259. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11260. NSString* category = [params valueForKey:@"category"];
  11261. if (!category || [category isEqualToString:@""]) {
  11262. category = @"%";
  11263. }
  11264. category = [self translateSingleQuote:category];
  11265. int limit = [[params valueForKey:@"limit"] intValue];
  11266. int offset = [[params valueForKey:@"offset"] intValue];
  11267. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11268. NSString *limit_str = @"";
  11269. if (limited) {
  11270. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  11271. }
  11272. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  11273. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  11274. sqlite3 *db = [iSalesDB get_db];
  11275. // [iSalesDB AddExFunction:db];
  11276. int count;
  11277. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  11278. 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];
  11279. double price_min = 0;
  11280. double price_max = 0;
  11281. if ([params.allKeys containsObject:@"alert"]) {
  11282. // alert
  11283. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  11284. NSString *alert = params[@"alert"];
  11285. if ([alert isEqualToString:@"Display All"]) {
  11286. alert = [NSString stringWithFormat:@""];
  11287. } else {
  11288. alert = [self translateSingleQuote:alert];
  11289. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  11290. }
  11291. // available
  11292. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  11293. NSString *available = params[@"available"];
  11294. NSString *available_condition;
  11295. if ([available isEqualToString:@"Display All"]) {
  11296. available_condition = @"";
  11297. } else if ([available isEqualToString:@"Available Now"]) {
  11298. available_condition = @"and availability > 0";
  11299. } else {
  11300. available_condition = @"and availability == 0";
  11301. }
  11302. // best seller
  11303. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  11304. NSString *best_seller = @"";
  11305. NSString *order_best_seller = @"m.name asc";
  11306. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  11307. best_seller = @"and best_seller > 0";
  11308. order_best_seller = @"m.best_seller desc,m.name asc";
  11309. }
  11310. // price
  11311. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  11312. NSString *price = params[@"price"];
  11313. price_min = 0;
  11314. price_max = MAXFLOAT;
  11315. if (user && price != nil) {
  11316. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  11317. NSMutableString *priceName = [NSMutableString string];
  11318. for (int i = 0; i < priceTypeArray.count; i++) {
  11319. NSString *pricetype = priceTypeArray[i];
  11320. pricetype = [self translateSingleQuote:pricetype];
  11321. if (i == 0) {
  11322. [priceName appendFormat:@"'%@'",pricetype];
  11323. } else {
  11324. [priceName appendFormat:@",'%@'",pricetype];
  11325. }
  11326. }
  11327. if ([price isEqualToString:@"Display All"]) {
  11328. price = [NSString stringWithFormat:@""];
  11329. } else if([price containsString:@"+"]){
  11330. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  11331. price_min = [price doubleValue];
  11332. 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];
  11333. } else {
  11334. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  11335. price_min = [[priceArray objectAtIndex:0] doubleValue];
  11336. price_max = [[priceArray objectAtIndex:1] doubleValue];
  11337. 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];
  11338. }
  11339. } else {
  11340. price = @"";
  11341. }
  11342. // sold_by_qty : Sold in quantities of %@
  11343. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  11344. NSString *qty = params[@"sold_by_qty"];
  11345. if ([qty isEqualToString:@"Display All"]) {
  11346. qty = @"";
  11347. } else {
  11348. qty = [self translateSingleQuote:qty];
  11349. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  11350. }
  11351. // cate
  11352. // category = [self translateSingleQuote:category];
  11353. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  11354. // cate mutiple selection
  11355. NSString *category_id = params[@"category"];
  11356. NSMutableArray *cate_id_array = nil;
  11357. NSMutableString *cateWhere = [NSMutableString string];
  11358. if ([category_id isEqualToString:@""] || !category_id) {
  11359. [cateWhere appendString:@"1 = 1"];
  11360. } else {
  11361. if ([category_id containsString:@","]) {
  11362. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  11363. } else {
  11364. cate_id_array = [@[category_id] mutableCopy];
  11365. }
  11366. [cateWhere appendString:@"("];
  11367. for (int i = 0; i < cate_id_array.count; i++) {
  11368. if (i == 0) {
  11369. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  11370. } else {
  11371. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  11372. }
  11373. }
  11374. [cateWhere appendString:@")"];
  11375. }
  11376. // where bestseller > 0 order by bestseller desc
  11377. // sql query: alert availability(int) best_seller(int) price qty
  11378. 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];
  11379. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  11380. }
  11381. DebugLog(@"offline category where: %@",where);
  11382. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  11383. if (!user) {
  11384. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  11385. }
  11386. [ret setValue:filter forKey:@"filter"];
  11387. DebugLog(@"offline_category sql:%@",sqlQuery);
  11388. sqlite3_stmt * statement;
  11389. [ret setValue:@"2" forKey:@"result"];
  11390. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11391. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11392. // int count=0;
  11393. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11394. {
  11395. int i=0;
  11396. while (sqlite3_step(statement) == SQLITE_ROW)
  11397. {
  11398. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11399. char *name = (char*)sqlite3_column_text(statement, 0);
  11400. if(name==nil)
  11401. name="";
  11402. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11403. char *description = (char*)sqlite3_column_text(statement, 1);
  11404. if(description==nil)
  11405. description="";
  11406. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11407. int product_id = sqlite3_column_int(statement, 2);
  11408. int wid = sqlite3_column_int(statement, 3);
  11409. int closeout = sqlite3_column_int(statement, 4);
  11410. int cid = sqlite3_column_int(statement, 5);
  11411. int wisdelete = sqlite3_column_int(statement, 6);
  11412. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  11413. int more_color = sqlite3_column_int(statement, 8);
  11414. // Defaul Category ID
  11415. __block NSString *categoryID = nil;
  11416. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  11417. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11418. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  11419. if(default_category==nil)
  11420. default_category="";
  11421. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  11422. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  11423. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  11424. categoryID = nsdefault_category;
  11425. }];
  11426. if (!categoryID.length) {
  11427. NSString *cateIDs = params[@"category"];
  11428. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  11429. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  11430. for (NSString *cateID in requestCategoryArr) {
  11431. BOOL needBreak = NO;
  11432. for (NSString *itemCateIDBox in itemCategoryArr) {
  11433. if (itemCateIDBox.length > 4) {
  11434. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  11435. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  11436. if ([itemCategoryID isEqualToString:cateID]) {
  11437. needBreak = YES;
  11438. categoryID = itemCategoryID;
  11439. break;
  11440. }
  11441. }
  11442. }
  11443. if (needBreak) {
  11444. break;
  11445. }
  11446. }
  11447. }
  11448. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11449. if(wid !=0 && wisdelete != 1)
  11450. [item setValue:@"true" forKey:@"wish_exists"];
  11451. else
  11452. [item setValue:@"false" forKey:@"wish_exists"];
  11453. if(closeout==0)
  11454. [item setValue:@"false" forKey:@"is_closeout"];
  11455. else
  11456. [item setValue:@"true" forKey:@"is_closeout"];
  11457. if(cid==0)
  11458. [item setValue:@"false" forKey:@"cart_exists"];
  11459. else
  11460. [item setValue:@"true" forKey:@"cart_exists"];
  11461. if (more_color == 0) {
  11462. [item setObject:@(false) forKey:@"more_color"];
  11463. } else if (more_color == 1) {
  11464. [item setObject:@(true) forKey:@"more_color"];
  11465. }
  11466. [item addEntriesFromDictionary:imgjson];
  11467. // [item setValue:nsurl forKey:@"img"];
  11468. [item setValue:nsname forKey:@"name"];
  11469. [item setValue:nsdescription forKey:@"description"];
  11470. if (categoryID) {
  11471. [item setValue:categoryID forKey:@"item_category_id"];
  11472. }
  11473. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11474. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11475. i++;
  11476. }
  11477. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11478. [ret setObject:items forKey:@"items"];
  11479. sqlite3_finalize(statement);
  11480. } else {
  11481. DebugLog(@"nothing...");
  11482. }
  11483. DebugLog(@"count:%d",count);
  11484. [iSalesDB close_db:db];
  11485. #ifdef DEBUG
  11486. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11487. #endif
  11488. dispatch_async(dispatch_get_main_queue(), ^{
  11489. // UIApplication * app = [UIApplication sharedApplication];
  11490. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11491. // [ret setValue:appDelegate.mode forKey:@"mode"];
  11492. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  11493. result(ret);
  11494. });
  11495. });
  11496. // NSString* orderCode = [params valueForKey:@"orderCode"];
  11497. }
  11498. +(void) offline_category :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11499. {
  11500. [self categoryList:params limited:YES completionHandler:result];
  11501. }
  11502. + (void) search:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11503. {
  11504. UIApplication * app = [UIApplication sharedApplication];
  11505. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11506. NSString* order_code = appDelegate.order_code;
  11507. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11508. NSString* keyword = [params valueForKey:@"keyword"];
  11509. keyword=keyword.lowercaseString;
  11510. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  11511. int limit = [[params valueForKey:@"limit"] intValue];
  11512. int offset = [[params valueForKey:@"offset"] intValue];
  11513. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11514. NSString *limit_str = @"";
  11515. if (limited) {
  11516. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  11517. }
  11518. sqlite3 *db = [iSalesDB get_db];
  11519. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  11520. NSString *sqlQuery = nil;
  11521. if(exactMatch )
  11522. 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 ;
  11523. else
  11524. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where (lower(name) like'%%%@%%' 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
  11525. DebugLog(@"offline_search sql:%@",sqlQuery);
  11526. sqlite3_stmt * statement;
  11527. [ret setValue:@"2" forKey:@"result"];
  11528. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11529. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11530. // int count=0;
  11531. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11532. {
  11533. int i=0;
  11534. while (sqlite3_step(statement) == SQLITE_ROW)
  11535. {
  11536. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11537. // char *name = (char*)sqlite3_column_text(statement, 1);
  11538. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  11539. char *name = (char*)sqlite3_column_text(statement, 0);
  11540. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11541. char *description = (char*)sqlite3_column_text(statement, 1);
  11542. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11543. int product_id = sqlite3_column_int(statement, 2);
  11544. // char *url = (char*)sqlite3_column_text(statement, 3);
  11545. // if(url==nil)
  11546. // url="";
  11547. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  11548. int wid = sqlite3_column_int(statement, 3);
  11549. int closeout = sqlite3_column_int(statement, 4);
  11550. int cid = sqlite3_column_int(statement, 5);
  11551. int wisdelete = sqlite3_column_int(statement, 6);
  11552. int more_color = sqlite3_column_int(statement, 7);
  11553. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11554. if(wid !=0 && wisdelete != 1)
  11555. [item setValue:@"true" forKey:@"wish_exists"];
  11556. else
  11557. [item setValue:@"false" forKey:@"wish_exists"];
  11558. if(closeout==0)
  11559. [item setValue:@"false" forKey:@"is_closeout"];
  11560. else
  11561. [item setValue:@"true" forKey:@"is_closeout"];
  11562. if(cid==0)
  11563. [item setValue:@"false" forKey:@"cart_exists"];
  11564. else
  11565. [item setValue:@"true" forKey:@"cart_exists"];
  11566. if (more_color == 0) {
  11567. [item setObject:@(false) forKey:@"more_color"];
  11568. } else if (more_color == 1) {
  11569. [item setObject:@(true) forKey:@"more_color"];
  11570. }
  11571. [item addEntriesFromDictionary:imgjson];
  11572. // [item setValue:nsurl forKey:@"img"];
  11573. [item setValue:nsname forKey:@"fash_name"];
  11574. [item setValue:nsdescription forKey:@"description"];
  11575. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11576. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11577. i++;
  11578. }
  11579. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11580. [ret setObject:items forKey:@"items"];
  11581. sqlite3_finalize(statement);
  11582. }
  11583. DebugLog(@"count:%d",count);
  11584. [iSalesDB close_db:db];
  11585. #ifdef DEBUG
  11586. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11587. #endif
  11588. dispatch_async(dispatch_get_main_queue(), ^{
  11589. result(ret);
  11590. });
  11591. });
  11592. }
  11593. +(void) offline_search:(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11594. {
  11595. [self search:params limited:YES completionHandler:result];
  11596. }
  11597. +(void) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11598. {
  11599. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11600. NSString* offline_command=params[@"offline_Command"];
  11601. NSMutableDictionary* ret=nil;
  11602. if([offline_command isEqualToString:@"model_NIYMAL"])
  11603. {
  11604. NSString* category = params[@"category"];
  11605. ret = [[self refresh_model_NIYMAL:category] mutableCopy];
  11606. }
  11607. dispatch_async(dispatch_get_main_queue(), ^{
  11608. result(ret);
  11609. });
  11610. });
  11611. }
  11612. +(void) offline_wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  11613. {
  11614. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11615. int sort = [[params objectForKey:@"sort"] intValue];
  11616. NSString *sort_str = @"";
  11617. switch (sort) {
  11618. case 0:{
  11619. sort_str = @"order by w.modify_time desc";
  11620. }
  11621. break;
  11622. case 1:{
  11623. sort_str = @"order by w.modify_time asc";
  11624. }
  11625. break;
  11626. case 2:{
  11627. sort_str = @"order by m.name asc";
  11628. }
  11629. break;
  11630. case 3:{
  11631. sort_str = @"order by m.name desc";
  11632. }
  11633. break;
  11634. case 4:{
  11635. sort_str = @"order by m.description asc";
  11636. }
  11637. break;
  11638. default:
  11639. break;
  11640. }
  11641. // NSString* user = appDelegate.user;
  11642. sqlite3 *db = [iSalesDB get_db];
  11643. // order by w.create_time
  11644. 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];
  11645. // 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];
  11646. sqlite3_stmt * statement;
  11647. NSDate *date1 = [NSDate date];
  11648. // NSDate *date2 = nil;
  11649. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  11650. int count=0;
  11651. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11652. {
  11653. while (sqlite3_step(statement) == SQLITE_ROW)
  11654. {
  11655. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  11656. int product_id = sqlite3_column_double(statement, 0);
  11657. char *description = (char*)sqlite3_column_text(statement, 1);
  11658. if(description==nil)
  11659. description= "";
  11660. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  11661. int item_id = sqlite3_column_double(statement, 2);
  11662. NSDate *date_image = [NSDate date];
  11663. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  11664. printf("image : ");
  11665. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  11666. // char *url = (char*)sqlite3_column_text(statement, 3);
  11667. // if(url==nil)
  11668. // url="";
  11669. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  11670. int qty = sqlite3_column_int(statement, 3);
  11671. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  11672. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  11673. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  11674. item[@"description"]= nsdescription;
  11675. item[@"img"]= nsurl;
  11676. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  11677. count++;
  11678. }
  11679. printf("total time:");
  11680. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  11681. ret[@"count"]= [NSNumber numberWithInt:count];
  11682. ret[@"total_count"]= [NSNumber numberWithInt:count];
  11683. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  11684. ret[@"result"]= [NSNumber numberWithInt:2];
  11685. sqlite3_finalize(statement);
  11686. }
  11687. [iSalesDB close_db:db];
  11688. dispatch_async(dispatch_get_main_queue(), ^{
  11689. UIApplication * app = [UIApplication sharedApplication];
  11690. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11691. appDelegate.wish_count =count;
  11692. [appDelegate update_count_mark];
  11693. result(ret);
  11694. });
  11695. });
  11696. }
  11697. +(void) offline_add2wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11698. {
  11699. // UIApplication * app = [UIApplication sharedApplication];
  11700. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11701. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11702. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  11703. sqlite3 *db = [iSalesDB get_db];
  11704. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  11705. NSString* product_id=params[@"product_id"];
  11706. NSString *item_count_str = params[@"item_count"];
  11707. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  11708. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  11709. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  11710. // NSString *sql = @"";
  11711. for(int i=0;i<arr.count;i++)
  11712. {
  11713. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  11714. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  11715. __block int cart_count = 0;
  11716. if (!item_count_str) {
  11717. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  11718. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11719. NSString *model_set = [self textAtColumn:0 statement:stmt];
  11720. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  11721. cart_count = [[model_set_components lastObject] intValue];
  11722. }];
  11723. }
  11724. if(count==0)
  11725. {
  11726. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  11727. sqlite3_stmt *stmt;
  11728. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  11729. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  11730. if (item_count_arr) {
  11731. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  11732. } else {
  11733. sqlite3_bind_int(stmt,2,cart_count);
  11734. }
  11735. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  11736. [iSalesDB execSql:@"ROLLBACK" db:db];
  11737. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  11738. [iSalesDB close_db:db];
  11739. DebugLog(@"add to wishlist error");
  11740. dispatch_async(dispatch_get_main_queue(), ^{
  11741. result(ret);
  11742. });
  11743. }
  11744. } else {
  11745. int qty = 0;
  11746. if (item_count_arr) {
  11747. qty = [item_count_arr[i] intValue];
  11748. } else {
  11749. qty = cart_count;
  11750. }
  11751. 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]];
  11752. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  11753. [iSalesDB execSql:@"ROLLBACK" db:db];
  11754. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  11755. [iSalesDB close_db:db];
  11756. DebugLog(@"add to wishlist error");
  11757. dispatch_async(dispatch_get_main_queue(), ^{
  11758. result(ret);
  11759. });
  11760. }
  11761. }
  11762. }
  11763. // [iSalesDB execSql:sql db:db];
  11764. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  11765. [iSalesDB execSql:@"END TRANSACTION" db:db];
  11766. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  11767. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  11768. [iSalesDB close_db:db];
  11769. ret[@"result"]= [NSNumber numberWithInt:2];
  11770. dispatch_async(dispatch_get_main_queue(), ^{
  11771. UIApplication * app = [UIApplication sharedApplication];
  11772. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11773. appDelegate.wish_count =count;
  11774. [appDelegate update_count_mark];
  11775. result(ret);
  11776. });
  11777. });
  11778. }
  11779. +(void) offline_model :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11780. {
  11781. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  11782. bool bLogin = appDelegate.bLogin;
  11783. __block NSString* contact_id = appDelegate.contact_id;
  11784. __block NSString* user = appDelegate.user;
  11785. __block NSString* order_code = appDelegate.order_code;
  11786. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11787. NSString* model_name = [params valueForKey:@"product_name"];
  11788. NSString* product_id = [params valueForKey:@"product_id"];
  11789. NSString* upc_code = [params valueForKey:@"upc_code"];
  11790. NSString* category = [params valueForKey:@"category"];
  11791. NSString *default_category_id = [self model_default_category:product_id model_name:model_name upc_code:upc_code];
  11792. if(category==nil) {
  11793. category = default_category_id;
  11794. } else {
  11795. NSArray *arr_0 = [category componentsSeparatedByString:@","];
  11796. // 参数重有多个category id
  11797. if (arr_0.count > 1) {
  11798. NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  11799. // 取交集
  11800. NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  11801. NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  11802. [set_0 intersectSet:set_1];
  11803. if (set_0.count == 1) {
  11804. category = (NSString *)[set_0 anyObject];
  11805. } else {
  11806. if ([set_0 containsObject:default_category_id]) {
  11807. category = default_category_id;
  11808. } else {
  11809. category = (NSString *)[set_0 anyObject];
  11810. }
  11811. }
  11812. }
  11813. }
  11814. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11815. sqlite3 *db = [iSalesDB get_db];
  11816. // int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  11817. NSString *sqlQuery = nil;
  11818. if(product_id==nil)
  11819. if(model_name==nil)
  11820. {
  11821. 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='%@';
  11822. }
  11823. else
  11824. {
  11825. 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='%@';
  11826. }
  11827. else
  11828. 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=%@;
  11829. sqlite3_stmt * statement;
  11830. [ret setValue:@"2" forKey:@"result"];
  11831. [ret setValue:@"3" forKey:@"detail_section_count"];
  11832. // int count=0;
  11833. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11834. {
  11835. if (sqlite3_step(statement) == SQLITE_ROW)
  11836. {
  11837. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11838. char *name = (char*)sqlite3_column_text(statement, 0);
  11839. if(name==nil)
  11840. name="";
  11841. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11842. char *description = (char*)sqlite3_column_text(statement, 1);
  11843. if(description==nil)
  11844. description="";
  11845. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11846. int product_id = sqlite3_column_int(statement, 2);
  11847. char *color = (char*)sqlite3_column_text(statement, 3);
  11848. if(color==nil)
  11849. color="";
  11850. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  11851. //
  11852. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  11853. // if(legcolor==nil)
  11854. // legcolor="";
  11855. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  11856. //
  11857. //
  11858. int availability = sqlite3_column_int(statement, 5);
  11859. //
  11860. int incoming_stock = sqlite3_column_int(statement, 6);
  11861. char *demension = (char*)sqlite3_column_text(statement, 7);
  11862. if(demension==nil)
  11863. demension="";
  11864. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  11865. // ,,,,,,,,,
  11866. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  11867. if(seat_height==nil)
  11868. seat_height="";
  11869. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  11870. char *material = (char*)sqlite3_column_text(statement, 9);
  11871. if(material==nil)
  11872. material="";
  11873. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  11874. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  11875. if(box_dim==nil)
  11876. box_dim="";
  11877. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  11878. char *volume = (char*)sqlite3_column_text(statement, 11);
  11879. if(volume==nil)
  11880. volume="";
  11881. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  11882. double weight = sqlite3_column_double(statement, 12);
  11883. char *model_set = (char*)sqlite3_column_text(statement, 13);
  11884. if(model_set==nil)
  11885. model_set="";
  11886. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  11887. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  11888. if(load_ability==nil)
  11889. load_ability="";
  11890. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  11891. // char *default_category = (char*)sqlite3_column_text(statement, 15);
  11892. // if(default_category==nil)
  11893. // default_category="";
  11894. // NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  11895. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  11896. if(fabric_content==nil)
  11897. fabric_content="";
  11898. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  11899. char *assembling = (char*)sqlite3_column_text(statement, 17);
  11900. if(assembling==nil)
  11901. assembling="";
  11902. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  11903. char *made_in = (char*)sqlite3_column_text(statement, 18);
  11904. if(made_in==nil)
  11905. made_in="";
  11906. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  11907. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  11908. if(special_remarks==nil)
  11909. special_remarks="";
  11910. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  11911. int stockUcom = sqlite3_column_double(statement, 20);
  11912. char *product_group = (char*)sqlite3_column_text(statement, 21);
  11913. if(product_group==nil)
  11914. product_group="";
  11915. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  11916. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  11917. // if(fashion_selector==nil)
  11918. // fashion_selector="";
  11919. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  11920. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  11921. if(selector_field==nil)
  11922. selector_field="";
  11923. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  11924. char *property_field = (char*)sqlite3_column_text(statement, 23);
  11925. if(property_field==nil)
  11926. property_field="";
  11927. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  11928. char *packaging = (char*)sqlite3_column_text(statement, 24);
  11929. if(packaging==nil)
  11930. packaging="";
  11931. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  11932. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11933. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  11934. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  11935. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  11936. [img_section setValue:model_s_img forKey:@"model_s_img"];
  11937. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  11938. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  11939. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  11940. NSString* Availability=nil;
  11941. if(availability>0)
  11942. Availability=[NSString stringWithFormat:@"%d",availability];
  11943. else
  11944. Availability = @"Out of Stock";
  11945. [img_section setValue:Availability forKey:@"Availability"];
  11946. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  11947. char *eta = (char*)sqlite3_column_text(statement, 25);
  11948. if(eta==nil)
  11949. eta="";
  11950. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  11951. if ([nseta isEqualToString:@"null"]) {
  11952. nseta = @"";
  11953. }
  11954. if (availability <= 0) {
  11955. [img_section setValue:nseta forKey:@"ETA"];
  11956. }
  11957. int item_id = sqlite3_column_int(statement, 26);
  11958. NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  11959. NSString *content_writing = [self textAtColumn:28 statement:statement];
  11960. NSString* Price=nil;
  11961. if(bLogin==false)
  11962. Price=@"Must Sign in.";
  11963. else
  11964. {
  11965. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  11966. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  11967. if(price==nil)
  11968. Price=@"No Price.";
  11969. else
  11970. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  11971. }
  11972. [img_section setObject:content_writing forKey:@"product_content_writing"];
  11973. [img_section setValue:Price forKey:@"price"];
  11974. [img_section setValue:nsname forKey:@"model_name"];
  11975. [img_section setValue:nsdescription forKey:@"model_descrition"];
  11976. if (order_code) { // 离线order code即so#
  11977. 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];
  11978. __block int cartQTY = 0;
  11979. [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11980. cartQTY = sqlite3_column_int(stmt, 0);
  11981. }];
  11982. if (cartQTY > 0) {
  11983. [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  11984. }
  11985. }
  11986. [ret setObject:img_section forKey:@"img_section"];
  11987. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  11988. int detail0_item_count=0;
  11989. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11990. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11991. [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11992. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11993. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11994. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11995. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11996. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11997. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11998. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11999. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12000. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12001. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12002. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12003. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  12004. NSDictionary* pricejson = [self get_model_all_price:contact_id item_id:item_id user:user islogin:bLogin db:db];
  12005. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  12006. {
  12007. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  12008. [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12009. }
  12010. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  12011. [detail0_section setValue:@"kv" forKey:@"type"];
  12012. [detail0_section setValue:@"Product Information" forKey:@"title"];
  12013. [ret setObject:detail0_section forKey:@"detail_0"];
  12014. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  12015. // [detail1_section setValue:@"detail" forKey:@"target"];
  12016. // [detail1_section setValue:@"popup" forKey:@"action"];
  12017. // [detail1_section setValue:@"content" forKey:@"type"];
  12018. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  12019. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  12020. // [detail1_section setValue:@"true" forKey:@"single_row"];
  12021. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  12022. [ret setObject:detail1_section forKey:@"detail_1"];
  12023. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  12024. [detail2_section setValue:@"detail" forKey:@"target"];
  12025. [detail2_section setValue:@"popup" forKey:@"action"];
  12026. [detail2_section setValue:@"content" forKey:@"type"];
  12027. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  12028. [detail2_section setValue:@"true" forKey:@"single_row"];
  12029. [detail2_section setValue:@"local" forKey:@"data"];
  12030. [ret setObject:detail2_section forKey:@"detail_2"];
  12031. }
  12032. sqlite3_finalize(statement);
  12033. }
  12034. else
  12035. {
  12036. [ret setValue:@"8" forKey:@"result"];
  12037. }
  12038. // DebugLog(@"count:%d",count);
  12039. [iSalesDB close_db:db];
  12040. #ifdef DEBUG
  12041. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12042. #endif
  12043. dispatch_async(dispatch_get_main_queue(), ^{
  12044. result(ret);
  12045. });
  12046. });
  12047. }
  12048. //+(void) offline_deletewishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12049. //{
  12050. //
  12051. // UIApplication * app = [UIApplication sharedApplication];
  12052. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12053. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12054. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  12055. // sqlite3 *db = [iSalesDB get_db];
  12056. // NSString* collectId=params[@"collectId"];
  12057. //
  12058. //
  12059. //
  12060. //
  12061. // // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  12062. // NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  12063. // [iSalesDB execSql:sqlQuery db:db];
  12064. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12065. // [iSalesDB close_db:db];
  12066. //
  12067. //
  12068. // ret[@"result"]= [NSNumber numberWithInt:2];
  12069. // dispatch_async(dispatch_get_main_queue(), ^{
  12070. //
  12071. // appDelegate.wish_count =count;
  12072. //
  12073. // [appDelegate update_count_mark];
  12074. // result(ret);
  12075. // });
  12076. //
  12077. // });
  12078. //}
  12079. +(void) offline_logout :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12080. {
  12081. UIApplication * app = [UIApplication sharedApplication];
  12082. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12083. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12084. [iSalesDB disable_trigger];
  12085. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  12086. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  12087. [iSalesDB enable_trigger];
  12088. dispatch_async(dispatch_get_main_queue(), ^{
  12089. //
  12090. // NSString* user = [params valueForKey:@"user"];
  12091. //
  12092. // NSString* password = [params valueForKey:@"password"];
  12093. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12094. ret[@"result"]=[NSNumber numberWithInt:2 ];
  12095. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  12096. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  12097. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  12098. [appDelegate update_count_mark];
  12099. appDelegate.can_show_price =false;
  12100. appDelegate.can_see_price =false;
  12101. appDelegate.can_create_portfolio =false;
  12102. appDelegate.can_create_order =false;
  12103. appDelegate.can_cancel_order =false;
  12104. appDelegate.can_set_cart_price =false;
  12105. appDelegate.can_delete_order =false;
  12106. appDelegate.can_submit_order =false;
  12107. appDelegate.can_set_tearsheet_price =false;
  12108. appDelegate.can_update_contact_info = false;
  12109. appDelegate.save_order_logout = false;
  12110. appDelegate.submit_order_logout = false;
  12111. appDelegate.alert_sold_in_quantities = false;
  12112. appDelegate.ipad_perm =nil ;
  12113. appDelegate.user_type = USER_ROLE_UNKNOWN;
  12114. appDelegate.OrderFilter= nil;
  12115. [appDelegate SetSo:nil];
  12116. [appDelegate set_main_button_panel];
  12117. result(ret);
  12118. });
  12119. });
  12120. }
  12121. +(void) offline_createorder :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12122. {
  12123. // iSalesDB.
  12124. // UIApplication * app = [UIApplication sharedApplication];
  12125. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12126. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12127. [iSalesDB disable_trigger];
  12128. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  12129. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  12130. [iSalesDB enable_trigger];
  12131. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12132. NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  12133. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  12134. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  12135. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  12136. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  12137. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  12138. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  12139. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  12140. NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  12141. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  12142. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  12143. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  12144. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  12145. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  12146. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  12147. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  12148. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  12149. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  12150. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  12151. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  12152. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  12153. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  12154. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  12155. [arr_name addObject:customer_first_name];
  12156. [arr_name addObject:customer_last_name];
  12157. NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  12158. // default ship from
  12159. 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';";
  12160. __block NSString *cid = @"";
  12161. __block NSString *name = @"";
  12162. __block NSString *ext = @"";
  12163. __block NSString *contact = @"";
  12164. __block NSString *email = @"";
  12165. __block NSString *fax = @"";
  12166. __block NSString *phone = @"";
  12167. sqlite3 *db = [iSalesDB get_db];
  12168. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  12169. cid = [self textAtColumn:0 statement:statment];
  12170. name = [self textAtColumn:1 statement:statment];
  12171. ext = [self textAtColumn:2 statement:statment];
  12172. contact = [self textAtColumn:3 statement:statment];
  12173. email = [self textAtColumn:4 statement:statment];
  12174. fax = [self textAtColumn:5 statement:statment];
  12175. phone = [self textAtColumn:6 statement:statment];
  12176. }];
  12177. // default carrier
  12178. NSString *carrier_sql = [NSString stringWithFormat:@"select name from carrier where code_id = (select carrier from offline_contact where contact_id = '%@');",customer_cid];
  12179. __block NSString *carrier = @"";
  12180. [iSalesDB jk_query:carrier_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  12181. carrier = [self textAtColumn:0 statement:statment];
  12182. }];
  12183. NSString* so_id = [self get_offline_soid:db];
  12184. if(so_id==nil)
  12185. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  12186. 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];
  12187. int result_code =[iSalesDB execSql:sql_neworder db:db];
  12188. [ret setValue:[NSNumber numberWithInt:result_code] forKey:@"result"];
  12189. //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'
  12190. //soId
  12191. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  12192. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  12193. sqlite3_stmt * statement;
  12194. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12195. {
  12196. if (sqlite3_step(statement) == SQLITE_ROW)
  12197. {
  12198. // char *name = (char*)sqlite3_column_text(statement, 1);
  12199. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  12200. //ret = sqlite3_column_int(statement, 0);
  12201. char *soId = (char*)sqlite3_column_text(statement, 0);
  12202. if(soId==nil)
  12203. soId="";
  12204. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  12205. [ret setValue:nssoId forKey:@"soId"];
  12206. [ret setValue:nssoId forKey:@"orderCode"];
  12207. }
  12208. sqlite3_finalize(statement);
  12209. }
  12210. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  12211. [ret setValue:@"Regular Mode" forKey:@"mode"];
  12212. [iSalesDB close_db:db];
  12213. dispatch_async(dispatch_get_main_queue(), ^{
  12214. result(ret);
  12215. });
  12216. });
  12217. }
  12218. +(void) offline_requestcart :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12219. {
  12220. UIApplication * app = [UIApplication sharedApplication];
  12221. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12222. bool can_create_backorder= appDelegate.can_create_backorder;
  12223. NSString* user = appDelegate.user;
  12224. NSString* contact_id = appDelegate.contact_id;
  12225. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12226. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12227. sqlite3 *db = [iSalesDB get_db];
  12228. NSString* orderCode=params[@"orderCode"];
  12229. int sort = [[params objectForKey:@"sort"] intValue];
  12230. NSString *sort_str = @"";
  12231. switch (sort) {
  12232. case 0:{
  12233. sort_str = @"order by c.modify_time desc";
  12234. }
  12235. break;
  12236. case 1:{
  12237. sort_str = @"order by c.modify_time asc";
  12238. }
  12239. break;
  12240. case 2:{
  12241. sort_str = @"order by m.name asc";
  12242. }
  12243. break;
  12244. case 3:{
  12245. sort_str = @"order by m.name desc";
  12246. }
  12247. break;
  12248. case 4:{
  12249. sort_str = @"order by m.description asc";
  12250. }
  12251. break;
  12252. default:
  12253. break;
  12254. }
  12255. NSString* sqlorder = [ NSString stringWithFormat:@"select discount from offline_order where so_id='%@'",orderCode];
  12256. sqlite3_stmt * statement1;
  12257. double orderdiscount=0;
  12258. int dbresult1=sqlite3_prepare_v2(db, [sqlorder UTF8String], -1, &statement1, nil);
  12259. if ( dbresult1== SQLITE_OK)
  12260. {
  12261. if (sqlite3_step(statement1) == SQLITE_ROW)
  12262. {
  12263. // NSDate *row_date = [NSDate date];
  12264. // NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  12265. orderdiscount = sqlite3_column_double(statement1, 0);
  12266. }
  12267. sqlite3_finalize(statement1);
  12268. }
  12269. 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 ];
  12270. // 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 ];
  12271. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  12272. DebugLog(@"offline_login sql:%@",sqlQuery);
  12273. sqlite3_stmt * statement;
  12274. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  12275. NSDate *date1 = [NSDate date];
  12276. int count=0;
  12277. int cart_count=0;
  12278. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  12279. if ( dbresult== SQLITE_OK)
  12280. {
  12281. while (sqlite3_step(statement) == SQLITE_ROW)
  12282. {
  12283. // NSDate *row_date = [NSDate date];
  12284. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  12285. int product_id = sqlite3_column_int(statement, 0);
  12286. char *str_price = (char*)sqlite3_column_text(statement, 1);
  12287. int item_id = sqlite3_column_int(statement, 7);
  12288. NSString* Price=nil;
  12289. if(str_price==nil)
  12290. {
  12291. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  12292. // NSDate *price_date = [NSDate date];
  12293. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  12294. // DebugLog(@"price time interval");
  12295. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  12296. if(price==nil)
  12297. Price=@"No Price.";
  12298. else
  12299. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  12300. }
  12301. else
  12302. {
  12303. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  12304. }
  12305. double discount = sqlite3_column_double(statement, 2);
  12306. int item_count = sqlite3_column_int(statement, 3);
  12307. char *line_note = (char*)sqlite3_column_text(statement, 4);
  12308. NSString *nsline_note=nil;
  12309. if(line_note!=nil)
  12310. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  12311. char *name = (char*)sqlite3_column_text(statement, 5);
  12312. NSString *nsname=nil;
  12313. if(name!=nil)
  12314. nsname= [[NSString alloc]initWithUTF8String:name];
  12315. char *description = (char*)sqlite3_column_text(statement, 6);
  12316. NSString *nsdescription=nil;
  12317. if(description!=nil)
  12318. nsdescription= [[NSString alloc]initWithUTF8String:description];
  12319. int stockUom = sqlite3_column_int(statement, 8);
  12320. int _id = sqlite3_column_int(statement, 9);
  12321. int availability = sqlite3_column_int(statement, 10);
  12322. // NSDate *subtotal_date = [NSDate date];
  12323. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  12324. // DebugLog(@"subtotal_date time interval");
  12325. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  12326. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  12327. double weight=[bsubtotaljson[@"weight"] doubleValue];
  12328. int carton=[bsubtotaljson[@"carton"] intValue];
  12329. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  12330. int mpack =[bsubtotaljson[@"mpack"] intValue];
  12331. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  12332. // NSDate *img_date = [NSDate date];
  12333. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  12334. // DebugLog(@"img_date time interval");
  12335. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  12336. itemjson[@"model"]=nsname;
  12337. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  12338. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  12339. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  12340. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  12341. itemjson[@"check"]=@"true";
  12342. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  12343. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  12344. itemjson[@"unit_price"]=Price;
  12345. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  12346. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  12347. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  12348. itemjson[@"mpack"]=[NSNumber numberWithInt:mpack];
  12349. itemjson[@"note"]=nsline_note;
  12350. if (!can_create_backorder) {
  12351. itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  12352. }
  12353. // NSDate *date2 = [NSDate date];
  12354. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:contact_id user:user];
  12355. // DebugLog(@"model_bundle time interval");
  12356. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  12357. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  12358. count++;
  12359. // DebugLog(@"row time interval");
  12360. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  12361. }
  12362. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  12363. sqlite3_finalize(statement);
  12364. }
  12365. DebugLog(@"request cart total time interval");
  12366. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  12367. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  12368. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12369. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  12370. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  12371. ret[@"count"]=[NSNumber numberWithInt:count ];
  12372. if(orderdiscount>0)
  12373. ret[@"orderDiscount"]=@(orderdiscount);
  12374. ret[@"mode"]=@"Regular Mode";
  12375. [iSalesDB close_db:db];
  12376. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  12377. DebugLog(@"general notes :%@",general_note);
  12378. ret[@"general_note"]= general_note;
  12379. dispatch_async(dispatch_get_main_queue(), ^{
  12380. result(ret);
  12381. });
  12382. });
  12383. }
  12384. @end