OLDataProvider.m 684 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309
  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. float p =0;
  1779. float s =0;
  1780. for(int j=0;j<[RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] intValue];j++)
  1781. {
  1782. NSDictionary* cartitem =RASingleton.sharedInstance.scan_cart[@"section_0"][[NSString stringWithFormat:@"item_%d",j]];
  1783. p+=[cartitem[@"unit_price"] doubleValue]*[cartitem[@"count"]intValue] * (1-[cartitem[@"discount"]doubleValue]/100.0);
  1784. // remove net price
  1785. // double msurcharge =0;
  1786. // int mcount = [cartitem[@"count"] intValue];
  1787. // NSDictionary* price_group = [RADataProvider get_price_group:appDelegate.price_group];
  1788. // if(RASingleton.sharedInstance.price_type==1)
  1789. // {
  1790. // if([cartitem[@"special_price"] boolValue])
  1791. // {
  1792. // msurcharge=([cartitem[price_group[@"price_3"][@"name"]] doubleValue]-[cartitem[price_group[@"price_2"][@"name"]] doubleValue])*mcount*(1-[cartitem[@"discount"]doubleValue]/100.0);
  1793. // }
  1794. // else
  1795. // {
  1796. // msurcharge=([cartitem[price_group[@"price_3"][@"name"]] doubleValue]-[cartitem[price_group[@"price_1"][@"name"]] doubleValue])*mcount*(1-[cartitem[@"discount"]doubleValue]/100.0);
  1797. // }
  1798. // }
  1799. // s+=msurcharge;
  1800. }
  1801. NSMutableDictionary* section =RASingleton.sharedInstance.scan_cart[@"section_0"];
  1802. int order_qty=0;
  1803. for(int c=0;c<[section[@"count"] intValue];c++)
  1804. {
  1805. NSDictionary* item = section[[NSString stringWithFormat:@"item_%d",c]];
  1806. order_qty+=[item[@"count"] intValue];
  1807. }
  1808. // orderitem[@"model_count"] =[ NSString stringWithFormat:@"%@",order_qty];
  1809. // int newcount = 0;
  1810. for(int i=0;i<count;i++)
  1811. {
  1812. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1813. if([item[@"order_code"] isEqualToString: order_code ])
  1814. {
  1815. item[@"price"] = [RAConvertor currencyNumber:p+s];//[NSString stringWithFormat:@"%.2f",p+s];
  1816. item[@"model_count"] =[ NSString stringWithFormat:@"%d",order_qty];//RASingleton.sharedInstance.scan_cart[@"section_0"][@"count"] ];
  1817. orderlist[[NSString stringWithFormat:@"item_%d",i]] = item;
  1818. }
  1819. }
  1820. // newlist[@"count"] = @(newcount);
  1821. [self saveScanOrderList:orderlist];
  1822. // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1823. [ActiveViewController Notify:@"ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1824. #endif
  1825. }
  1826. +(void) deleteScanOrder:(NSString*) order_code
  1827. {
  1828. NSString *userdir = [OLDataProvider getUserPath];
  1829. if(order_code.length==0)
  1830. return;
  1831. // orderdirNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1832. // NSString *documents = [paths objectAtIndex:0];
  1833. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1834. if([appDelegate.order_code isEqualToString:order_code])
  1835. {
  1836. //要删除的订单是当前打开的订单;
  1837. // appDelegate.order_code = nil;
  1838. // appDelegate.customerInfo = nil;
  1839. // RASingleton.sharedInstance.scan_cart = nil;
  1840. // appDelegate.contact_id = nil;
  1841. [self scanCloseOrder];
  1842. // [appDelegate updateScanButton:false];
  1843. [appDelegate update_count_mark];
  1844. }
  1845. //删除订单目录
  1846. [[NSFileManager defaultManager] removeItemAtPath:[userdir stringByAppendingPathComponent:order_code] error:nil];
  1847. //维护订单列表
  1848. NSMutableDictionary* orderlist = [self getScanOrderList];
  1849. int count = [orderlist[@"count"] intValue];
  1850. if(count==0)
  1851. {
  1852. // order list 为空,不用维护
  1853. #ifdef RA_NOTIFICATION
  1854. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1855. #else
  1856. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1857. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1858. #endif
  1859. return;
  1860. }
  1861. // NSString *orderlistPath = [userdir stringByAppendingPathComponent:@"orderlist.json"];
  1862. // if (![[NSFileManager defaultManager] fileExistsAtPath:orderlistPath]) {
  1863. //
  1864. //
  1865. //
  1866. // // [orderlist addObject:appDelegate.order_code];
  1867. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1868. //
  1869. // }
  1870. // else
  1871. // {
  1872. // orderlist=[[RAUtils dictfromfile:orderlistPath] mutableCopy];
  1873. //
  1874. // // [orderlist insertObject:appDelegate.order_code atIndex:0];
  1875. //
  1876. // // [RAUtils dicttofile:orderlistPath dict:orderlist];
  1877. // }
  1878. NSMutableDictionary* newlist = [NSMutableDictionary new];
  1879. int newcount = 0;
  1880. for(int i=0;i<count;i++)
  1881. {
  1882. NSMutableDictionary* item = orderlist[[NSString stringWithFormat:@"item_%d",i]];
  1883. if([item[@"order_code"] isEqualToString: order_code ])
  1884. continue;
  1885. else
  1886. {
  1887. newlist[[NSString stringWithFormat:@"item_%d",newcount]] = item;
  1888. newcount++;
  1889. }
  1890. }
  1891. newlist[@"count"] = @(newcount);
  1892. [self saveScanOrderList:newlist];
  1893. // [RAUtils dicttofile:orderlistPath dict:newlist];
  1894. #ifdef RA_NOTIFICATION
  1895. [ActiveViewController Notify:@"CartViewController,ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1896. #else
  1897. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1898. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  1899. #endif
  1900. }
  1901. #ifdef SCANNER_ORDER
  1902. +(void) scanRefreshCart
  1903. {
  1904. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1905. NSMutableDictionary* cart =[[RAUtils copyDict: RASingleton.sharedInstance.scan_cart] mutableCopy];
  1906. NSMutableDictionary * section = cart[@"section_0"];
  1907. NSMutableDictionary* newsection = [NSMutableDictionary new];
  1908. int count = [section[@"count"] intValue];
  1909. int newcount = 0;
  1910. for(int i=0;i<count;i++)
  1911. {
  1912. NSMutableDictionary* item = [section[[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  1913. NSString* model = item[@"model"];
  1914. NSDictionary* modeljson=appDelegate.scan_model[model];
  1915. if(modeljson==nil)
  1916. continue;
  1917. NSMutableDictionary* cartitem = [[self scanmodel2cartitem:[modeljson mutableCopy]] mutableCopy];
  1918. cartitem[@"note"]=item[@"note"];
  1919. cartitem[@"discount"]=item[@"discount"];
  1920. // [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 icbf = sqlite3_column_double(statement, 16);
  4062. // double mcbf = sqlite3_column_double(statement, 17);
  4063. if(ipack==0)
  4064. {
  4065. carton= count/mpack ;
  4066. weight = mweight*carton;
  4067. cuft= carton*(mlength*mwidth*mheight);
  4068. int remain=count%mpack;
  4069. if(remain==0)
  4070. {
  4071. //do nothing;
  4072. }
  4073. else
  4074. {
  4075. carton++;
  4076. weight += uweight*remain;
  4077. cuft += (ulength*uwidth*uheight)*remain;
  4078. }
  4079. }
  4080. else
  4081. {
  4082. carton = count/(mpack*ipack);
  4083. weight = mweight*carton;
  4084. cuft= carton*(mlength*mwidth*mheight);
  4085. int remain=count%(mpack*ipack);
  4086. if(remain==0)
  4087. {
  4088. // do nothing;
  4089. }
  4090. else
  4091. {
  4092. carton++;
  4093. int icarton =remain/ipack;
  4094. int iremain=remain%ipack;
  4095. weight += iweight*icarton;
  4096. cuft += (ilength*iwidth*iheight)*icarton;
  4097. if(iremain==0)
  4098. {
  4099. //do nothing;
  4100. }
  4101. else
  4102. {
  4103. weight += uweight*iremain;
  4104. cuft += (ulength*uwidth*uheight)*iremain;
  4105. }
  4106. }
  4107. }
  4108. #if defined(BUILD_NPD) || defined(BUILD_USAI) || defined(BUILD_UWAVER)|| defined (BUILD_CONTRAST)
  4109. double ucbf = sqlite3_column_double(statement, 15);
  4110. cuft=ucbf*count;
  4111. weight= uweight*count;
  4112. #endif
  4113. #if defined(BUILD_HOMER) || defined(BUILD_GATIT)
  4114. double ucbf = sqlite3_column_double(statement, 15);
  4115. cuft=ucbf*count;
  4116. weight= uweight*count;
  4117. #endif
  4118. }
  4119. sqlite3_finalize(statement);
  4120. }
  4121. if(compute)
  4122. {
  4123. NSArray * arr_count=nil;
  4124. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  4125. for(int i=0;i<arr_bundle.count;i++)
  4126. {
  4127. dict_item[arr_bundle[i]]=@"1";
  4128. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  4129. cuft+=[bundlejson[@"cuft"] doubleValue];
  4130. weight+=[bundlejson[@"weight"] doubleValue];
  4131. carton+=[bundlejson[@"carton"] intValue];
  4132. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  4133. }
  4134. }
  4135. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  4136. ret[@"mpack"]= [NSNumber numberWithInteger:impack];
  4137. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  4138. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  4139. ret[@"items"]=dict_item;
  4140. return ret;
  4141. }
  4142. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  4143. {
  4144. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  4145. // 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 ];
  4146. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  4147. DebugLog(@"offline_login sql:%@",sqlQuery);
  4148. sqlite3_stmt * statement;
  4149. int cart_count=0;
  4150. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  4151. if ( dbresult== SQLITE_OK)
  4152. {
  4153. while (sqlite3_step(statement) == SQLITE_ROW)
  4154. {
  4155. int item_id = sqlite3_column_int(statement, 0);
  4156. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  4157. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  4158. }
  4159. sqlite3_finalize(statement);
  4160. }
  4161. return cart_count;
  4162. }
  4163. //+(NSData*) offline_requestcart :(NSMutableDictionary *) params
  4164. //{
  4165. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4166. // sqlite3 *db = [iSalesDB get_db];
  4167. // UIApplication * app = [UIApplication sharedApplication];
  4168. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4169. //
  4170. // NSString* orderCode=params[@"orderCode"];
  4171. //
  4172. // int sort = [[params objectForKey:@"sort"] intValue];
  4173. // NSString *sort_str = @"";
  4174. // switch (sort) {
  4175. // case 0:{
  4176. // sort_str = @"order by c.modify_time desc";
  4177. // }
  4178. // break;
  4179. // case 1:{
  4180. // sort_str = @"order by c.modify_time asc";
  4181. // }
  4182. // break;
  4183. // case 2:{
  4184. // sort_str = @"order by m.name asc";
  4185. // }
  4186. // break;
  4187. // case 3:{
  4188. // sort_str = @"order by m.name desc";
  4189. // }
  4190. // break;
  4191. // case 4:{
  4192. // sort_str = @"order by m.description asc";
  4193. // }
  4194. // break;
  4195. //
  4196. // default:
  4197. // break;
  4198. //
  4199. // }
  4200. //
  4201. //
  4202. //
  4203. // 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 ];
  4204. //
  4205. //
  4206. //// 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 ];
  4207. //
  4208. //
  4209. //// NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  4210. //
  4211. // DebugLog(@"offline_login sql:%@",sqlQuery);
  4212. // sqlite3_stmt * statement;
  4213. //
  4214. //
  4215. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  4216. //
  4217. // NSDate *date1 = [NSDate date];
  4218. //
  4219. // int count=0;
  4220. // int cart_count=0;
  4221. // int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  4222. // if ( dbresult== SQLITE_OK)
  4223. // {
  4224. //
  4225. //
  4226. // while (sqlite3_step(statement) == SQLITE_ROW)
  4227. // {
  4228. //// NSDate *row_date = [NSDate date];
  4229. //
  4230. //
  4231. // NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  4232. //
  4233. //
  4234. //
  4235. //
  4236. // int product_id = sqlite3_column_int(statement, 0);
  4237. //
  4238. // char *str_price = (char*)sqlite3_column_text(statement, 1);
  4239. //
  4240. // int item_id = sqlite3_column_int(statement, 7);
  4241. //
  4242. // NSString* Price=nil;
  4243. // if(str_price==nil)
  4244. // {
  4245. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  4246. //// NSDate *price_date = [NSDate date];
  4247. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  4248. //// DebugLog(@"price time interval");
  4249. //// [self printTimeIntervalBetween:price_date and:[NSDate date]];
  4250. //
  4251. // if(price==nil)
  4252. // Price=@"No Price.";
  4253. // else
  4254. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  4255. // }
  4256. // else
  4257. // {
  4258. //
  4259. // Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  4260. // }
  4261. //
  4262. //
  4263. // double discount = sqlite3_column_double(statement, 2);
  4264. // int item_count = sqlite3_column_int(statement, 3);
  4265. //
  4266. // char *line_note = (char*)sqlite3_column_text(statement, 4);
  4267. // NSString *nsline_note=nil;
  4268. // if(line_note!=nil)
  4269. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  4270. //
  4271. //
  4272. // char *name = (char*)sqlite3_column_text(statement, 5);
  4273. // NSString *nsname=nil;
  4274. // if(name!=nil)
  4275. // nsname= [[NSString alloc]initWithUTF8String:name];
  4276. //
  4277. // char *description = (char*)sqlite3_column_text(statement, 6);
  4278. // NSString *nsdescription=nil;
  4279. // if(description!=nil)
  4280. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  4281. //
  4282. //
  4283. //
  4284. // int stockUom = sqlite3_column_int(statement, 8);
  4285. // int _id = sqlite3_column_int(statement, 9);
  4286. // int availability = sqlite3_column_int(statement, 10);
  4287. //
  4288. //// NSDate *subtotal_date = [NSDate date];
  4289. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  4290. //// DebugLog(@"subtotal_date time interval");
  4291. //// [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  4292. //
  4293. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  4294. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  4295. // int carton=[bsubtotaljson[@"carton"] intValue];
  4296. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  4297. //
  4298. //// itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  4299. //// NSDate *img_date = [NSDate date];
  4300. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  4301. //// DebugLog(@"img_date time interval");
  4302. //// [self printTimeIntervalBetween:img_date and:[NSDate date]];
  4303. //
  4304. // itemjson[@"model"]=nsname;
  4305. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  4306. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  4307. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  4308. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  4309. // itemjson[@"check"]=@"true";
  4310. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  4311. // itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  4312. // itemjson[@"unit_price"]=Price;
  4313. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  4314. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  4315. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  4316. // itemjson[@"note"]=nsline_note;
  4317. // if (!appDelegate.can_create_backorder) {
  4318. // itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  4319. // }
  4320. //// NSDate *date2 = [NSDate date];
  4321. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  4322. //// DebugLog(@"model_bundle time interval");
  4323. //// [self printTimeIntervalBetween:date2 and:[NSDate date]];
  4324. //
  4325. // ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  4326. // count++;
  4327. //
  4328. //// DebugLog(@"row time interval");
  4329. //// [self printTimeIntervalBetween:row_date and:[NSDate date]];
  4330. // }
  4331. //
  4332. //
  4333. // ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  4334. // sqlite3_finalize(statement);
  4335. // }
  4336. //
  4337. //
  4338. // DebugLog(@"request cart total time interval");
  4339. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  4340. //
  4341. //
  4342. // //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  4343. //
  4344. // int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  4345. // ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  4346. // ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  4347. // ret[@"count"]=[NSNumber numberWithInt:count ];
  4348. //
  4349. // ret[@"mode"]=@"Regular Mode";
  4350. //
  4351. // [iSalesDB close_db:db];
  4352. //
  4353. // NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  4354. //
  4355. // DebugLog(@"general notes :%@",general_note);
  4356. //
  4357. // ret[@"general_note"]= general_note;
  4358. //
  4359. // return [RAConvertor dict2data:ret];
  4360. //}
  4361. +(NSData*) offline_login :(NSMutableDictionary *) params
  4362. {
  4363. NSString* user = [params valueForKey:@"user"];
  4364. NSString* password = [params valueForKey:@"password"];
  4365. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4366. sqlite3 *db = [iSalesDB get_db];
  4367. 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"]];
  4368. DebugLog(@"offline_login sql:%@",sqlQuery);
  4369. sqlite3_stmt * statement;
  4370. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  4371. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4372. {
  4373. if (sqlite3_step(statement) == SQLITE_ROW)
  4374. {
  4375. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4376. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  4377. int can_show_price = sqlite3_column_int(statement, 0);
  4378. int can_see_price = sqlite3_column_int(statement, 1);
  4379. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  4380. if(contact_id==nil)
  4381. contact_id="";
  4382. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4383. int user_type = sqlite3_column_int(statement, 3);
  4384. int can_cancel_order = sqlite3_column_int(statement, 4);
  4385. int can_set_cart_price = sqlite3_column_int(statement, 5);
  4386. int can_create_portfolio = sqlite3_column_int(statement, 6);
  4387. int can_delete_order = sqlite3_column_int(statement, 7);
  4388. int can_submit_order = sqlite3_column_int(statement, 8);
  4389. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  4390. int can_create_order = sqlite3_column_int(statement, 10);
  4391. char *mode = (char*)sqlite3_column_text(statement, 11);
  4392. if(mode==nil)
  4393. mode="";
  4394. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  4395. char *username = (char*)sqlite3_column_text(statement, 12);
  4396. if(username==nil)
  4397. username="";
  4398. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  4399. int can_update_contact_info = sqlite3_column_int(statement, 13);
  4400. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  4401. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  4402. [header setValue:nscontact_id forKey:@"contact_id"];
  4403. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  4404. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  4405. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  4406. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  4407. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  4408. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  4409. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  4410. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  4411. #ifdef BUILD_CONTRAST
  4412. [header setValue:[NSNumber numberWithBool:1] forKey:@"alert_sold_in_quantities"];
  4413. #endif
  4414. [header setObject:@(YES) forKey:@"can_create_backorder"];
  4415. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  4416. [header setValue:nsusername forKey:@"username"];
  4417. NSError* error=nil;
  4418. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  4419. [header setValue:statusFilter forKey:@"statusFilter"];
  4420. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4421. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  4422. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  4423. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  4424. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  4425. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  4426. [ret setObject:header forKey:@"header"];
  4427. [ret setValue:nsmode forKey:@"mode"];
  4428. }
  4429. sqlite3_finalize(statement);
  4430. }
  4431. [iSalesDB close_db:db];
  4432. #ifdef DEBUG
  4433. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4434. #endif
  4435. return [RAConvertor dict2data:ret];
  4436. }
  4437. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  4438. {
  4439. assert(params[@"mode"]!=nil);
  4440. NSString* contactId = [params valueForKey:@"contactId"];
  4441. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4442. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4443. sqlite3 *db = [iSalesDB get_db];
  4444. NSString *sqlQuery = nil;
  4445. {
  4446. 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];
  4447. }
  4448. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  4449. sqlite3_stmt * statement;
  4450. [ret setValue:@"2" forKey:@"result"];
  4451. int carrier = -1;
  4452. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4453. {
  4454. //int i = 0;
  4455. if (sqlite3_step(statement) == SQLITE_ROW)
  4456. {
  4457. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4458. // int editable = sqlite3_column_int(statement, 0);
  4459. char *company_name = (char*)sqlite3_column_text(statement, 1);
  4460. NSString *nscompany_name =nil;
  4461. if(company_name==nil)
  4462. nscompany_name=@"";
  4463. else
  4464. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  4465. char *country = (char*)sqlite3_column_text(statement, 2);
  4466. if(country==nil)
  4467. country="";
  4468. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  4469. // char *addr = (char*)sqlite3_column_text(statement, 3);
  4470. // if(addr==nil)
  4471. // addr="";
  4472. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  4473. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  4474. if(zipcode==nil)
  4475. zipcode="";
  4476. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  4477. char *state = (char*)sqlite3_column_text(statement, 5);
  4478. if(state==nil)
  4479. state="";
  4480. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  4481. char *city = (char*)sqlite3_column_text(statement, 6);
  4482. if(city==nil)
  4483. city="";
  4484. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  4485. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  4486. // NSString *nscontact_name = nil;
  4487. // if(contact_name==nil)
  4488. // nscontact_name=@"";
  4489. // else
  4490. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  4491. char *phone = (char*)sqlite3_column_text(statement, 8);
  4492. NSString *nsphone = nil;
  4493. if(phone==nil)
  4494. nsphone=@"";
  4495. else
  4496. nsphone= [[NSString alloc]initWithUTF8String:phone];
  4497. // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  4498. // if(contact_id==nil)
  4499. // contact_id="";
  4500. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4501. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  4502. if(addr_1==nil)
  4503. addr_1="";
  4504. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4505. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  4506. if(addr_2==nil)
  4507. addr_2="";
  4508. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4509. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  4510. if(addr_3==nil)
  4511. addr_3="";
  4512. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4513. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  4514. if(addr_4==nil)
  4515. addr_4="";
  4516. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4517. char *first_name = (char*)sqlite3_column_text(statement, 14);
  4518. if(first_name==nil)
  4519. first_name="";
  4520. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  4521. char *last_name = (char*)sqlite3_column_text(statement, 15);
  4522. if(last_name==nil)
  4523. last_name="";
  4524. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  4525. char *fax = (char*)sqlite3_column_text(statement, 16);
  4526. NSString *nsfax = nil;
  4527. if(fax==nil)
  4528. nsfax=@"";
  4529. else
  4530. nsfax= [[NSString alloc]initWithUTF8String:fax];
  4531. char *email = (char*)sqlite3_column_text(statement, 17);
  4532. NSString *nsemail = nil;
  4533. if(email==nil)
  4534. nsemail=@"";
  4535. else
  4536. nsemail= [[NSString alloc]initWithUTF8String:email];
  4537. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  4538. NSString *nsimg_0 = nil;
  4539. if(img_0==nil)
  4540. nsimg_0=@"";
  4541. else
  4542. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  4543. [self copy_bcardImg:nsimg_0];
  4544. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  4545. NSString *nsimg_1 = nil;
  4546. if(img_1==nil)
  4547. nsimg_1=@"";
  4548. else
  4549. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  4550. [self copy_bcardImg:nsimg_1];
  4551. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  4552. NSString *nsimg_2 = nil;
  4553. if(img_2==nil)
  4554. nsimg_2=@"";
  4555. else
  4556. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  4557. [self copy_bcardImg:nsimg_2];
  4558. char *price_type = (char*)sqlite3_column_text(statement, 21);
  4559. NSString *nsprice_type = nil;
  4560. if(price_type==nil)
  4561. nsprice_type=@"";
  4562. else
  4563. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  4564. char *notes = (char*)sqlite3_column_text(statement, 22);
  4565. NSString *nsnotes = nil;
  4566. if(notes==nil)
  4567. nsnotes=@"";
  4568. else
  4569. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  4570. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  4571. NSString *nssalesrep = nil;
  4572. if(salesrep==nil)
  4573. nssalesrep=@"";
  4574. else
  4575. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  4576. NSString *contact_type = [self textAtColumn:24 statement:statement];
  4577. carrier = sqlite3_column_int(statement, 25);
  4578. {
  4579. // decrypt
  4580. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  4581. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  4582. nsphone=[AESCrypt fastdecrypt:nsphone];
  4583. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4584. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  4585. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4586. }
  4587. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4588. [arr_name addObject:nsfirst_name];
  4589. [arr_name addObject:nslast_name];
  4590. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  4591. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  4592. [arr_ext addObject:nsaddr_1];
  4593. [arr_ext addObject:nsaddr_2];
  4594. [arr_ext addObject:nsaddr_3];
  4595. [arr_ext addObject:nsaddr_4];
  4596. [arr_ext addObject:@"\r\n"];
  4597. [arr_ext addObject:nscity];
  4598. [arr_ext addObject:nsstate];
  4599. [arr_ext addObject:nszipcode];
  4600. [arr_ext addObject:nscountry];
  4601. NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  4602. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  4603. [item setValue:nsimg_2 forKey:@"business_card_2"];
  4604. [item setValue:nsimg_0 forKey:@"business_card_0"];
  4605. [item setValue:nscountry forKey:@"customer_country"];
  4606. [item setValue:nsphone forKey:@"customer_phone"];
  4607. [item setValue:nsimg_1 forKey:@"business_card_1"];
  4608. [item setValue:nscompany_name forKey:@"customer_name"];
  4609. [item setValue:nsprice_type forKey:@"customer_price_type"];
  4610. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  4611. [item setValue:nsext forKey:@"customer_contact_ext"];
  4612. [item setValue:nszipcode forKey:@"customer_zipcode"];
  4613. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  4614. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  4615. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  4616. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  4617. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  4618. [item setValue:nslast_name forKey:@"customer_last_name"];
  4619. [item setValue:nscity forKey:@"customer_city"];
  4620. [item setValue:nsstate forKey:@"customer_state"];
  4621. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  4622. [item setValue:contactId forKey:@"customer_cid"];
  4623. [item setValue:nscontact_name forKey:@"customer_contact"];
  4624. [item setValue:nsfax forKey:@"customer_fax"];
  4625. [item setValue:nsemail forKey:@"customer_email"];
  4626. [item setValue:contact_type forKey:@"customer_contact_type"];
  4627. // i++;
  4628. }
  4629. // UIApplication * app = [UIApplication sharedApplication];
  4630. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4631. [ret setValue:params[@"mode"] forKey:@"mode"];
  4632. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  4633. sqlite3_finalize(statement);
  4634. }
  4635. [iSalesDB close_db:db];
  4636. NSString* scarrier = [self offline_getCarrier:carrier];
  4637. item[@"customer_truck_carrier"] = scarrier;
  4638. [ret setObject:item forKey:@"customerInfo"];
  4639. #ifdef DEBUG
  4640. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4641. #endif
  4642. return ret;
  4643. }
  4644. + (bool) copy_bcardImg:(NSString*) filename
  4645. {
  4646. if(filename.length==0)
  4647. return false;
  4648. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4649. bool ret=false;
  4650. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4651. NSString *cachefolder = [paths objectAtIndex:0];
  4652. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  4653. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  4654. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  4655. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  4656. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  4657. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  4658. //
  4659. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  4660. NSFileManager* fileManager = [NSFileManager defaultManager];
  4661. BOOL bdir=NO;
  4662. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  4663. {
  4664. NSError *error = nil;
  4665. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  4666. {
  4667. ret=false;
  4668. }
  4669. else
  4670. {
  4671. ret=true;
  4672. }
  4673. // NSError *error = nil;
  4674. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  4675. //
  4676. // if(!bsuccess)
  4677. // {
  4678. // DebugLog(@"Create offline_createimg folder failed");
  4679. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4680. // return [RAConvertor dict2data:ret];
  4681. // }
  4682. // if(bsuccess)
  4683. // {
  4684. // sqlite3 *db = [self get_db];
  4685. //
  4686. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4687. // [iSalesDB close_db:db];
  4688. // }
  4689. }
  4690. return ret;
  4691. //
  4692. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  4693. // if(bsuccess)
  4694. // {
  4695. // NSError *error = nil;
  4696. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  4697. // {
  4698. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4699. // }
  4700. // else
  4701. // {
  4702. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  4703. //
  4704. // ret[@"img_url_aname"]=filename;
  4705. // ret[@"img_url"]=savedImagePath;
  4706. // }
  4707. // }
  4708. }
  4709. +(NSData *) offline_saveBusinesscard:(NSData *) image
  4710. {
  4711. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4712. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  4713. NSString *cachefolder = [paths objectAtIndex:0];
  4714. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  4715. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  4716. NSFileManager* fileManager = [NSFileManager defaultManager];
  4717. BOOL bdir=YES;
  4718. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  4719. {
  4720. NSError *error = nil;
  4721. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  4722. if(!bsuccess)
  4723. {
  4724. DebugLog(@"Create offline_createimg folder failed");
  4725. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4726. return [RAConvertor dict2data:ret];
  4727. }
  4728. // if(bsuccess)
  4729. // {
  4730. // sqlite3 *db = [self get_db];
  4731. //
  4732. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4733. // [iSalesDB close_db:db];
  4734. // }
  4735. }
  4736. if(! [fileManager fileExistsAtPath:img_cache isDirectory:&bdir])
  4737. {
  4738. NSError *error = nil;
  4739. bool bsuccess=[fileManager createDirectoryAtPath:img_cache withIntermediateDirectories:YES attributes:nil error:&error];
  4740. if(!bsuccess)
  4741. {
  4742. DebugLog(@"Create img_cache folder failed");
  4743. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4744. return [RAConvertor dict2data:ret];
  4745. }
  4746. // if(bsuccess)
  4747. // {
  4748. // sqlite3 *db = [self get_db];
  4749. //
  4750. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  4751. // [iSalesDB close_db:db];
  4752. // }
  4753. }
  4754. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  4755. //JEPG格式
  4756. // NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  4757. NSString* filename =[NSString stringWithFormat:@"%@.jpg",[[NSUUID UUID] UUIDString]];
  4758. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  4759. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  4760. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  4761. if(bsuccess)
  4762. {
  4763. NSError *error = nil;
  4764. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  4765. {
  4766. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4767. }
  4768. else
  4769. {
  4770. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  4771. ret[@"img_url_aname"]=filename;
  4772. ret[@"img_url"]=filename;
  4773. }
  4774. }
  4775. else
  4776. {
  4777. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4778. }
  4779. return [RAConvertor dict2data:ret];
  4780. }
  4781. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  4782. {
  4783. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  4784. category = [category substringToIndex:3];
  4785. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4786. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  4787. params[@"category"]= category;
  4788. ret[@"params"]= params;
  4789. [ret setValue:@"detail" forKey:@"target"];
  4790. [ret setValue:@"popup" forKey:@"action"];
  4791. [ret setValue:@"content" forKey:@"type"];
  4792. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  4793. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  4794. [ret setValue:@"true" forKey:@"single_row"];
  4795. [ret setValue:@"true" forKey:@"partial_refresh"];
  4796. // sqlite3 *db = [iSalesDB get_db];
  4797. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  4798. NSString *sqlQuery =nil;
  4799. #ifdef BUILD_NPD
  4800. 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 ;
  4801. #else
  4802. 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];
  4803. #endif
  4804. sqlite3_stmt * statement;
  4805. int count = 0;
  4806. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  4807. // int count=0;
  4808. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4809. {
  4810. int i=0;
  4811. while (sqlite3_step(statement) == SQLITE_ROW)
  4812. {
  4813. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4814. // char *name = (char*)sqlite3_column_text(statement, 1);
  4815. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4816. char *name = (char*)sqlite3_column_text(statement, 0);
  4817. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  4818. int product_id = sqlite3_column_int(statement, 1);
  4819. char *url = (char*)sqlite3_column_text(statement, 2);
  4820. if(url==nil)
  4821. url="";
  4822. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  4823. [item setValue:nsurl forKey:@"picture_path"];
  4824. [item setValue:nsname forKey:@"fash_name"];
  4825. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  4826. [item setValue:category forKey:@"category"];
  4827. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4828. i++;
  4829. }
  4830. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4831. sqlite3_finalize(statement);
  4832. }
  4833. DebugLog(@"count:%d",count);
  4834. // [iSalesDB close_db:db];
  4835. return ret;
  4836. }
  4837. +(NSDictionary*) model_priceKvItem:(NSString*) key value:(NSString*)value
  4838. {
  4839. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4840. [ret setValue:key forKey:@"key"];
  4841. [ret setValue:value forKey:@"val"];
  4842. [ret setValue:@"price" forKey:@"type"];
  4843. return ret;
  4844. }
  4845. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  4846. {
  4847. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4848. [ret setValue:key forKey:@"key"];
  4849. [ret setValue:value forKey:@"val"];
  4850. return ret;
  4851. }
  4852. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  4853. {
  4854. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4855. [ret setValue:@"0" forKey:@"img_count"];
  4856. // sqlite3 *db = [iSalesDB get_db];
  4857. 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 ;
  4858. sqlite3_stmt * statement;
  4859. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4860. {
  4861. int i=0;
  4862. if (sqlite3_step(statement) == SQLITE_ROW)
  4863. {
  4864. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4865. // char *name = (char*)sqlite3_column_text(statement, 1);
  4866. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4867. char *value = (char*)sqlite3_column_text(statement, 0);
  4868. if(value==nil)
  4869. value="";
  4870. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  4871. char *key = (char*)sqlite3_column_text(statement, 1);
  4872. if(key==nil)
  4873. key="";
  4874. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  4875. [item setValue:nsvalue forKey:@"val"];
  4876. [item setValue:nskey forKey:@"key"];
  4877. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4878. [ret setValue:@"1" forKey:@"count"];
  4879. i++;
  4880. }
  4881. sqlite3_finalize(statement);
  4882. }
  4883. // [iSalesDB close_db:db];
  4884. return ret;
  4885. }
  4886. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  4887. {
  4888. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4889. [ret setValue:@"0" forKey:@"count"];
  4890. // sqlite3 *db = [iSalesDB get_db];
  4891. 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;
  4892. sqlite3_stmt * statement;
  4893. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4894. {
  4895. int i=0;
  4896. while (sqlite3_step(statement) == SQLITE_ROW)
  4897. {
  4898. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4899. // char *name = (char*)sqlite3_column_text(statement, 1);
  4900. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4901. char *value = (char*)sqlite3_column_text(statement, 0);
  4902. if(value==nil)
  4903. value="";
  4904. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  4905. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  4906. if(selector_display==nil)
  4907. selector_display="";
  4908. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  4909. int product_id = sqlite3_column_int(statement, 2);
  4910. char *category = (char*)sqlite3_column_text(statement, 3);
  4911. if(category==nil)
  4912. category="";
  4913. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  4914. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  4915. [item setValue:nsvalue forKey:@"title"];
  4916. [item setValue:url forKey:@"pic_url"];
  4917. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  4918. [params setValue:@"2" forKey:@"count"];
  4919. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  4920. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  4921. [param0 setValue:@"product_id" forKey:@"name"];
  4922. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  4923. [param1 setValue:nscategory forKey:@"val"];
  4924. [param1 setValue:@"category" forKey:@"name"];
  4925. [params setObject:param0 forKey:@"param_0"];
  4926. [params setObject:param1 forKey:@"param_1"];
  4927. [item setObject:params forKey:@"params"];
  4928. [ret setValue:nsselector_display forKey:@"name"];
  4929. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4930. i++;
  4931. }
  4932. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4933. [ret setValue:@"switch" forKey:@"action"];
  4934. sqlite3_finalize(statement);
  4935. }
  4936. // [iSalesDB close_db:db];
  4937. return ret;
  4938. }
  4939. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  4940. {
  4941. // model 在 category search 显示的图片。
  4942. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  4943. // sqlite3 *db = [iSalesDB get_db];
  4944. 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];
  4945. sqlite3_stmt * statement;
  4946. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4947. {
  4948. while (sqlite3_step(statement) == SQLITE_ROW)
  4949. {
  4950. char *url = (char*)sqlite3_column_text(statement, 0);
  4951. if(url==nil)
  4952. url="";
  4953. int type = sqlite3_column_int(statement, 1);
  4954. if(type==0)
  4955. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  4956. else
  4957. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  4958. }
  4959. sqlite3_finalize(statement);
  4960. }
  4961. // [iSalesDB close_db:db];
  4962. return ret;
  4963. }
  4964. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  4965. {
  4966. int item_id=-1;
  4967. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  4968. sqlite3_stmt * statement;
  4969. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4970. {
  4971. if (sqlite3_step(statement) == SQLITE_ROW)
  4972. {
  4973. item_id = sqlite3_column_int(statement, 0);
  4974. }
  4975. sqlite3_finalize(statement);
  4976. }
  4977. return item_id;
  4978. }
  4979. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  4980. {
  4981. // NSString* ret = @"";
  4982. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  4983. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  4984. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  4985. sqlite3_stmt * statement;
  4986. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4987. {
  4988. while (sqlite3_step(statement) == SQLITE_ROW)
  4989. {
  4990. int bitem_id = sqlite3_column_int(statement, 0);
  4991. int bitem_qty = sqlite3_column_int(statement, 1);
  4992. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  4993. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  4994. }
  4995. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  4996. sqlite3_finalize(statement);
  4997. }
  4998. // if(ret==nil)
  4999. // ret=@"";
  5000. *count=arr_count;
  5001. return arr_bundle;
  5002. }
  5003. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  5004. {
  5005. // get default sold qty, return -1 if model not found;
  5006. int ret = -1;
  5007. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  5008. sqlite3_stmt * statement;
  5009. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5010. {
  5011. if (sqlite3_step(statement) == SQLITE_ROW)
  5012. {
  5013. ret = sqlite3_column_int(statement, 0);
  5014. }
  5015. sqlite3_finalize(statement);
  5016. }
  5017. return ret;
  5018. }
  5019. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  5020. {
  5021. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5022. [ret setValue:@"0" forKey:@"img_count"];
  5023. // sqlite3 *db = [iSalesDB get_db];
  5024. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  5025. sqlite3_stmt * statement;
  5026. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5027. {
  5028. int i=0;
  5029. while (sqlite3_step(statement) == SQLITE_ROW)
  5030. {
  5031. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5032. // char *name = (char*)sqlite3_column_text(statement, 1);
  5033. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  5034. char *url = (char*)sqlite3_column_text(statement, 0);
  5035. if(url==nil)
  5036. url="";
  5037. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  5038. [item setValue:nsurl forKey:@"s"];
  5039. [item setValue:nsurl forKey:@"l"];
  5040. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  5041. i++;
  5042. }
  5043. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  5044. sqlite3_finalize(statement);
  5045. }
  5046. // [iSalesDB close_db:db];
  5047. return ret;
  5048. }
  5049. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  5050. // UIApplication * app = [UIApplication sharedApplication];
  5051. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  5052. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  5053. sqlite3 *db = [iSalesDB get_db];
  5054. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  5055. NSString* product_id=params[@"product_id"];
  5056. NSString *item_count_str = params[@"item_count"];
  5057. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  5058. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  5059. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  5060. // NSString *sql = @"";
  5061. for(int i=0;i<arr.count;i++)
  5062. {
  5063. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  5064. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  5065. __block int cart_count = 0;
  5066. if (!item_count_str) {
  5067. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  5068. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5069. NSString *model_set = [self textAtColumn:0 statement:stmt];
  5070. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  5071. cart_count = [[model_set_components lastObject] intValue];
  5072. }];
  5073. }
  5074. if(count==0)
  5075. {
  5076. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  5077. sqlite3_stmt *stmt;
  5078. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  5079. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  5080. if (item_count_arr) {
  5081. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  5082. } else {
  5083. sqlite3_bind_int(stmt,2,cart_count);
  5084. }
  5085. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  5086. [iSalesDB execSql:@"ROLLBACK" db:db];
  5087. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  5088. [iSalesDB close_db:db];
  5089. DebugLog(@"add to wishlist error");
  5090. return ret;
  5091. }
  5092. } else {
  5093. int qty = 0;
  5094. if (item_count_arr) {
  5095. qty = [item_count_arr[i] intValue];
  5096. } else {
  5097. qty = cart_count;
  5098. }
  5099. 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]];
  5100. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  5101. [iSalesDB execSql:@"ROLLBACK" db:db];
  5102. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  5103. [iSalesDB close_db:db];
  5104. DebugLog(@"add to wishlist error");
  5105. return ret;
  5106. }
  5107. }
  5108. }
  5109. // [iSalesDB execSql:sql db:db];
  5110. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  5111. [iSalesDB execSql:@"END TRANSACTION" db:db];
  5112. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  5113. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  5114. [iSalesDB close_db:db];
  5115. ret[@"wish_count"]=@(count);
  5116. ret[@"result"]= [NSNumber numberWithInt:2];
  5117. return ret;
  5118. }
  5119. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  5120. // 0 category
  5121. // 1 search
  5122. // 2 itemsearch
  5123. NSData *ret = nil;
  5124. NSDictionary *items = nil;
  5125. switch (from) {
  5126. case 0:{
  5127. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  5128. }
  5129. break;
  5130. case 1:{
  5131. items = [[self search:params limited:NO] objectForKey:@"items"];
  5132. }
  5133. break;
  5134. case 2:{
  5135. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  5136. }
  5137. break;
  5138. default:
  5139. break;
  5140. }
  5141. if (!items) {
  5142. return ret;
  5143. }
  5144. int count = [[items objectForKey:@"count"] intValue];
  5145. NSMutableString *product_id_str = [@"" mutableCopy];
  5146. for (int i = 0; i < count; i++) {
  5147. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  5148. NSDictionary *item = [items objectForKey:key];
  5149. NSString *product_id = [item objectForKey:@"product_id"];
  5150. if (i == 0) {
  5151. [product_id_str appendString:product_id];
  5152. } else {
  5153. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  5154. }
  5155. }
  5156. NSString *add_to = [self valueInParams:params key:@"addTo"];
  5157. if ([add_to isEqualToString:@"cart"]) {
  5158. NSString *order_code = [params objectForKey:@"orderCode"];
  5159. if (order_code.length) {
  5160. NSDictionary *newParams = @{
  5161. @"product_id" : product_id_str,
  5162. @"orderCode" : order_code,
  5163. @"can_create_backorder":params[@"can_create_backorder"]
  5164. };
  5165. ret = [self offline_add2cart:newParams.mutableCopy];
  5166. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5167. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5168. ret = [RAConvertor dict2data:retDic];
  5169. }
  5170. } else if([add_to isEqualToString:@"wishlist"]) {
  5171. NSDictionary *newParams = @{
  5172. @"product_id" : product_id_str
  5173. };
  5174. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  5175. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5176. ret = [RAConvertor dict2data:retDic];
  5177. } else if([add_to isEqualToString:@"portfolio"]) {
  5178. NSDictionary *newParams = @{
  5179. @"product_id" : product_id_str
  5180. };
  5181. NSData *data = [self offline_add2Portfolio:newParams.mutableCopy];
  5182. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5183. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  5184. ret = [RAConvertor dict2data:retDic];
  5185. }
  5186. return ret;
  5187. }
  5188. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  5189. {
  5190. return [self addAll:params from:0];
  5191. }
  5192. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  5193. {
  5194. return [self addAll:params from:1];
  5195. }
  5196. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  5197. {
  5198. return [self addAll:params from:2];
  5199. }
  5200. #pragma mark - Jack
  5201. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  5202. // "val_227" : {
  5203. // "check" : 1,
  5204. // "value" : "US United States",
  5205. // "value_id" : "228"
  5206. // },
  5207. if (!countryCode) {
  5208. countryCode = @"US";
  5209. }
  5210. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  5211. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  5212. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  5213. int code_id = sqlite3_column_int(stmt, 3); // id
  5214. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5215. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  5216. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  5217. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5218. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  5219. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5220. }
  5221. long n = *count;
  5222. *count = n + 1;
  5223. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  5224. [container setValue:countryDic forKey:key];
  5225. }] mutableCopy];
  5226. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5227. return ret;
  5228. }
  5229. + (NSDictionary *)offline_OrderCarrier:(NSString *)order_code db:(sqlite3 *)db {
  5230. // "val_227" : {
  5231. // "check" : 1,
  5232. // "value" : "US United States",
  5233. // "value_id" : "228"
  5234. // },
  5235. __block NSString* OrderCarrier =@"";
  5236. // sqlite3 *db = [iSalesDB get_db];
  5237. // sqlite3_stmt * statement;
  5238. [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) {
  5239. OrderCarrier = [self textAtColumn:0 statement:statment];
  5240. // sqlite3_finalize(statement);
  5241. }];
  5242. // [iSalesDB close_db:db];
  5243. __block int c=0;
  5244. 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) {
  5245. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  5246. int code_id = sqlite3_column_int(stmt, 2); // id
  5247. NSMutableDictionary *carrierDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5248. [carrierDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  5249. [carrierDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  5250. [carrierDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5251. if ([OrderCarrier isEqualToString:[NSString stringWithUTF8String:name]]) {
  5252. [carrierDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5253. }
  5254. c++;
  5255. long n = *count;
  5256. *count = n + 1;
  5257. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  5258. [container setValue:carrierDic forKey:key];
  5259. }] mutableCopy];
  5260. [ret setValue:[NSNumber numberWithLong:c] forKey:@"count"];
  5261. return ret;
  5262. }
  5263. + (NSDictionary *)offline_getAllCarrier:(NSString *)carrierName {
  5264. // "val_227" : {
  5265. // "check" : 1,
  5266. // "value" : "US United States",
  5267. // "value_id" : "228"
  5268. // },
  5269. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code_id from carrier order by name;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  5270. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  5271. int code_id = sqlite3_column_int(stmt, 2); // id
  5272. NSMutableDictionary *carrierDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5273. [carrierDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  5274. [carrierDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  5275. [carrierDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5276. if ([carrierName isEqualToString:[NSString stringWithUTF8String:name]]) {
  5277. [carrierDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5278. }
  5279. long n = *count;
  5280. *count = n + 1;
  5281. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  5282. [container setValue:carrierDic forKey:key];
  5283. }] mutableCopy];
  5284. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5285. return ret;
  5286. }
  5287. //+ (NSString *)offline_getContactCarrier:(int)contact_id {
  5288. // // "val_227" : {
  5289. // // "check" : 1,
  5290. // // "value" : "US United States",
  5291. // // "value_id" : "228"
  5292. // // },
  5293. //
  5294. // NSString *Sql = [NSString stringWithFormat: @"select carrier from offline_contact where contact_id = %d;",contact_id];
  5295. // NSString * code = [iSalesDB jk_queryText:Sql];
  5296. //
  5297. // return [self offline_getCarrier:code];
  5298. //
  5299. //}
  5300. + (NSString *)offline_getCarrier:(int)code {
  5301. // "val_227" : {
  5302. // "check" : 1,
  5303. // "value" : "US United States",
  5304. // "value_id" : "228"
  5305. // },
  5306. NSString *Sql = [NSString stringWithFormat: @"select name from carrier where code_id = %d;",code];
  5307. NSString * ret = [iSalesDB jk_queryText:Sql];
  5308. return ret;
  5309. }
  5310. + (int )offline_getCarrierCode:(NSString*)name {
  5311. // "val_227" : {
  5312. // "check" : 1,
  5313. // "value" : "US United States",
  5314. // "value_id" : "228"
  5315. // },
  5316. NSString *Sql = [NSString stringWithFormat: @"select code from carrier where name = '%@';",name];
  5317. NSString * ret = [iSalesDB jk_queryText:Sql];
  5318. return [ret intValue];
  5319. }
  5320. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  5321. countryCode = [self translateSingleQuote:countryCode];
  5322. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  5323. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5324. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  5325. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  5326. if (name == NULL) {
  5327. name = "";
  5328. }
  5329. if (code == NULL) {
  5330. code = "";
  5331. }
  5332. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5333. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5334. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  5335. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5336. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  5337. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5338. }
  5339. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5340. [container setValue:stateDic forKey:key];
  5341. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  5342. DebugLog(@"query all state error: %@",err_msg);
  5343. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5344. // [stateDic setValue:@"Other" forKey:@"value"];
  5345. // [stateDic setValue:@"" forKey:@"value_id"];
  5346. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5347. //
  5348. // if (state_code && [@"" isEqualToString:state_code]) {
  5349. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5350. // }
  5351. //
  5352. // NSString *key = [NSString stringWithFormat:@"val_0"];
  5353. // [container setValue:stateDic forKey:key];
  5354. }] mutableCopy];
  5355. [ret removeObjectForKey:@"result"];
  5356. // failure 可以不用了,一样的
  5357. if (ret.allKeys.count == 0) {
  5358. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5359. [stateDic setValue:@"Other" forKey:@"value"];
  5360. [stateDic setValue:@"" forKey:@"value_id"];
  5361. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5362. if (state_code && [@"" isEqualToString:state_code]) {
  5363. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5364. }
  5365. NSString *key = [NSString stringWithFormat:@"val_0"];
  5366. [ret setValue:stateDic forKey:key];
  5367. }
  5368. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5369. return ret;
  5370. }
  5371. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  5372. codeId = [self translateSingleQuote:codeId];
  5373. 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];
  5374. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5375. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  5376. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  5377. if (name == NULL) {
  5378. name = "";
  5379. }
  5380. if (code == NULL) {
  5381. code = "";
  5382. }
  5383. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5384. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5385. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  5386. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5387. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  5388. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5389. }
  5390. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5391. [container setValue:stateDic forKey:key];
  5392. }] mutableCopy];
  5393. [ret removeObjectForKey:@"result"];
  5394. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5395. return ret;
  5396. }
  5397. + (NSDictionary *)offline_getPrice {
  5398. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price where is_show = 1;"];
  5399. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5400. char *name = (char *) sqlite3_column_text(stmt, 1);
  5401. int type = sqlite3_column_int(stmt, 2);
  5402. int orderBy = sqlite3_column_int(stmt, 3);
  5403. if (name == NULL) {
  5404. name = "";
  5405. }
  5406. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  5407. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5408. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  5409. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5410. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5411. if (orderBy == 0) {
  5412. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5413. }
  5414. [container setValue:priceDic forKey:key];
  5415. }] mutableCopy];
  5416. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5417. return ret;
  5418. }
  5419. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  5420. NSString *sql = @"select _id,type_name from contact_type where is_show = 1";
  5421. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  5422. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  5423. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5424. // int _id = sqlite3_column_int(stmt, 0);
  5425. NSString *name = [self textAtColumn:1 statement:stmt];
  5426. NSDictionary *typeDic = @{
  5427. @"value_id" : name,
  5428. @"value" : name,
  5429. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  5430. };
  5431. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  5432. [ret setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  5433. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  5434. }];
  5435. return ret;
  5436. }
  5437. + (NSDictionary *)offline_getSalesRep:(NSString*) user{
  5438. // 首先从offline_login表中取出sales_code
  5439. // AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5440. // NSString *user = app.user;
  5441. user = [self translateSingleQuote:user];
  5442. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  5443. __block NSString *user_code = @"";
  5444. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5445. char *code = (char *)sqlite3_column_text(stmt, 0);
  5446. if (code == NULL) {
  5447. code = "";
  5448. }
  5449. user_code = [NSString stringWithUTF8String:code];
  5450. }];
  5451. // 再取所有salesRep
  5452. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  5453. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5454. // 1 name 2 code 3 salesrep_id
  5455. char *name = (char *)sqlite3_column_text(stmt, 1);
  5456. char *code = (char *)sqlite3_column_text(stmt, 2);
  5457. int salesrep_id = sqlite3_column_int(stmt, 3);
  5458. if (name == NULL) {
  5459. name = "";
  5460. }
  5461. if (code == NULL) {
  5462. code = "";
  5463. }
  5464. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  5465. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  5466. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  5467. // 比较code 相等则check
  5468. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  5469. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5470. }
  5471. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  5472. }] mutableCopy];
  5473. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  5474. return ret;
  5475. }
  5476. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  5477. zipcode = [self translateSingleQuote:zipcode];
  5478. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  5479. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5480. char *country = (char *)sqlite3_column_text(stmt, 0);
  5481. char *state = (char *)sqlite3_column_text(stmt, 1);
  5482. char *city = (char *)sqlite3_column_text(stmt, 2);
  5483. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  5484. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  5485. if (country == NULL) {
  5486. country = "";
  5487. }
  5488. if (state == NULL) {
  5489. state = "";
  5490. }
  5491. if (city == NULL) {
  5492. city = "";
  5493. }
  5494. if (country_code == NULL) {
  5495. country_code = "";
  5496. }
  5497. if (state_code == NULL) {
  5498. state_code = "";
  5499. }
  5500. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  5501. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  5502. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  5503. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  5504. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  5505. }] mutableCopy];
  5506. return ret;
  5507. }
  5508. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5509. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  5510. [item setValue:value forKey:valueKey];
  5511. [dic setValue:item forKey:itemKey];
  5512. }
  5513. + (NSString *)countryCodeByid:(NSString *)code_id {
  5514. NSString *ret = nil;
  5515. code_id = [self translateSingleQuote:code_id];
  5516. sqlite3 *db = [iSalesDB get_db];
  5517. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  5518. sqlite3_stmt * statement;
  5519. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5520. while (sqlite3_step(statement) == SQLITE_ROW) {
  5521. char *code = (char *)sqlite3_column_text(statement, 0);
  5522. if (code == NULL) {
  5523. code = "";
  5524. }
  5525. ret = [NSString stringWithUTF8String:code];
  5526. }
  5527. sqlite3_finalize(statement);
  5528. }
  5529. [iSalesDB close_db:db];
  5530. return ret;
  5531. }
  5532. + (NSString *)countryCodeIdByCode:(NSString *)code {
  5533. NSString *ret = nil;
  5534. code = [self translateSingleQuote:code];
  5535. sqlite3 *db = [iSalesDB get_db];
  5536. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  5537. sqlite3_stmt * statement;
  5538. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5539. while (sqlite3_step(statement) == SQLITE_ROW) {
  5540. char *_id = (char *)sqlite3_column_text(statement, 0);
  5541. if (_id == NULL) {
  5542. _id = "";
  5543. }
  5544. ret = [NSString stringWithFormat:@"%s",_id];
  5545. }
  5546. sqlite3_finalize(statement);
  5547. }
  5548. [iSalesDB close_db:db];
  5549. return ret;
  5550. }
  5551. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  5552. NSString *name = nil;
  5553. codeId = [self translateSingleQuote:codeId];
  5554. sqlite3 *db = [iSalesDB get_db];
  5555. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  5556. sqlite3_stmt * statement;
  5557. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5558. while (sqlite3_step(statement) == SQLITE_ROW) {
  5559. char *value = (char *)sqlite3_column_text(statement, 0);
  5560. if (value == NULL) {
  5561. value = "";
  5562. }
  5563. name = [NSString stringWithUTF8String:value];
  5564. }
  5565. sqlite3_finalize(statement);
  5566. }
  5567. [iSalesDB close_db:db];
  5568. return name;
  5569. }
  5570. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  5571. NSString *ret = nil;
  5572. sqlite3 *db = [iSalesDB get_db];
  5573. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  5574. sqlite3_stmt * statement;
  5575. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5576. while (sqlite3_step(statement) == SQLITE_ROW) {
  5577. char *name = (char *)sqlite3_column_text(statement, 0);
  5578. if (name == NULL) {
  5579. name = "";
  5580. }
  5581. ret = [NSString stringWithUTF8String:name];
  5582. }
  5583. sqlite3_finalize(statement);
  5584. }
  5585. [iSalesDB close_db:db];
  5586. return ret;
  5587. }
  5588. + (NSString *)salesRepCodeById:(NSString *)_id {
  5589. NSString *ret = nil;
  5590. _id = [self translateSingleQuote:_id];
  5591. sqlite3 *db = [iSalesDB get_db];
  5592. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  5593. sqlite3_stmt * statement;
  5594. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  5595. while (sqlite3_step(statement) == SQLITE_ROW) {
  5596. char *rep = (char *)sqlite3_column_text(statement, 0);
  5597. if (rep == NULL) {
  5598. rep = "";
  5599. }
  5600. ret = [NSString stringWithUTF8String:rep];
  5601. }
  5602. sqlite3_finalize(statement);
  5603. }
  5604. [iSalesDB close_db:db];
  5605. return ret;
  5606. }
  5607. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  5608. char *tx = (char *)sqlite3_column_text(stmt, col);
  5609. if (tx == NULL) {
  5610. tx = "";
  5611. }
  5612. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  5613. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  5614. if (!text) {
  5615. text = @"";
  5616. }
  5617. // 将字符全部为' '的字符串干掉
  5618. int spaceCount = 0;
  5619. for (int i = 0; i < text.length; i++) {
  5620. if ([text characterAtIndex:i] == ' ') {
  5621. spaceCount++;
  5622. }
  5623. }
  5624. if (spaceCount == text.length) {
  5625. text = @"";
  5626. }
  5627. return text;
  5628. }
  5629. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  5630. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5631. NSData *data = [NSData dataWithContentsOfFile:path];
  5632. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5633. return ret;
  5634. }
  5635. + (NSString *)textFileName:(NSString *)name {
  5636. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  5637. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  5638. if (!text) {
  5639. text = @"";
  5640. }
  5641. return text;
  5642. }
  5643. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  5644. return [[dic objectForKey:key] mutableCopy];
  5645. }
  5646. + (id)translateSingleQuote:(NSString *)string {
  5647. if ([string isKindOfClass:[NSString class]])
  5648. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  5649. return string;
  5650. }
  5651. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  5652. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  5653. }
  5654. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  5655. NSString* ret= nil;
  5656. NSString *sqlQuery = nil;
  5657. // 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
  5658. 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];
  5659. sqlite3_stmt * statement;
  5660. // int count=0;
  5661. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5662. {
  5663. if (sqlite3_step(statement) == SQLITE_ROW)
  5664. {
  5665. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  5666. if(imgurl==nil)
  5667. imgurl="";
  5668. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  5669. ret=nsimgurl;
  5670. }
  5671. sqlite3_finalize(statement);
  5672. }
  5673. else
  5674. {
  5675. [ret setValue:@"8" forKey:@"result"];
  5676. }
  5677. // [iSalesDB close_db:db];
  5678. // DebugLog(@"data string: %@",ret );
  5679. return ret;
  5680. }
  5681. #pragma mark contact Advanced search
  5682. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  5683. {
  5684. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  5685. return [RAConvertor dict2data:contactAdvanceDic];
  5686. }
  5687. #pragma mark create new contact
  5688. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  5689. assert(params[@"user"]!=nil);
  5690. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  5691. NSData *data = [NSData dataWithContentsOfFile:path];
  5692. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5693. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5694. NSString *countryCode = nil;
  5695. NSString *countryCode_id = nil;
  5696. NSString *stateCode = nil;
  5697. NSString *city = nil;
  5698. NSString *zipCode = nil;
  5699. // NSString *carrierCode = nil;
  5700. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5701. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  5702. NSString *code_id = params[@"country"];
  5703. countryCode_id = code_id;
  5704. countryCode = [self countryCodeByid:code_id];
  5705. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  5706. NSString *zip_code = params[@"zipcode"];
  5707. // 剔除全部为空格
  5708. int spaceCount = 0;
  5709. for (int i = 0; i < zip_code.length; i++) {
  5710. if ([zip_code characterAtIndex:i] == ' ') {
  5711. spaceCount++;
  5712. }
  5713. }
  5714. if (spaceCount == zip_code.length) {
  5715. zip_code = @"";
  5716. }
  5717. zipCode = zip_code;
  5718. if (zipCode.length > 0) {
  5719. countryCode_id = params[@"country"];
  5720. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  5721. countryCode = [dic valueForKey:@"country_code"];
  5722. if (!countryCode) {
  5723. // countryCode = @"US";
  5724. NSString *code_id = params[@"country"];
  5725. countryCode = [self countryCodeByid:code_id];
  5726. }
  5727. stateCode = [dic valueForKey:@"state_code"];
  5728. if (!stateCode.length) {
  5729. stateCode = params[@"state"];
  5730. }
  5731. city = [dic valueForKey:@"city"];
  5732. if (!city.length) {
  5733. city = params[@"city"];
  5734. }
  5735. // zip code
  5736. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  5737. [zipDic setValue:zipCode forKey:@"value"];
  5738. [section_0 setValue:zipDic forKey:@"item_11"];
  5739. } else {
  5740. NSString *code_id = params[@"country"];
  5741. countryCode = [self countryCodeByid:code_id];
  5742. stateCode = params[@"state"];
  5743. city = params[@"city"];
  5744. }
  5745. }
  5746. } else {
  5747. // default: US United States
  5748. countryCode = @"US";
  5749. countryCode_id = @"228";
  5750. }
  5751. // country
  5752. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5753. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  5754. // state
  5755. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5756. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  5757. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  5758. // city
  5759. if (city) {
  5760. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  5761. [cityDic setValue:city forKey:@"value"];
  5762. [section_0 setValue:cityDic forKey:@"item_13"];
  5763. }
  5764. // price type
  5765. NSDictionary *priceDic = [self offline_getPrice];
  5766. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  5767. // contact type
  5768. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  5769. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  5770. // Sales Rep
  5771. NSDictionary *repDic = [self offline_getSalesRep:params[@"user"]];
  5772. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5773. #ifdef BUILD_CONTRAST
  5774. //carrier
  5775. NSDictionary *carrierDic = [self offline_getAllCarrier:params[@"carrier"]];
  5776. [self setValue:carrierDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  5777. #endif
  5778. [ret setValue:section_0 forKey:@"section_0"];
  5779. return [RAConvertor dict2data:ret];
  5780. }
  5781. #pragma mark save
  5782. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  5783. NSString *addr = nil;
  5784. NSString *contact_name = nil;
  5785. // int carrier = [params[@"carrier"] intValue];
  5786. NSString *companyName = [params objectForKey:@"company"];
  5787. if (companyName) {
  5788. companyName = [AESCrypt fastencrypt:companyName];
  5789. } else {
  5790. companyName = @"";
  5791. }
  5792. DebugLog(@"company");
  5793. companyName = [self translateSingleQuote:companyName];
  5794. NSString *addr1 = [params objectForKey:@"address"];
  5795. NSString *addr2 = [params objectForKey:@"address2"];
  5796. NSString *addr3 = [params objectForKey:@"address_3"];
  5797. NSString *addr4 = [params objectForKey:@"address_4"];
  5798. if (!addr2) {
  5799. addr2 = @"";
  5800. }
  5801. if (!addr3) {
  5802. addr3 = @"";
  5803. }
  5804. if (!addr4) {
  5805. addr4 = @"";
  5806. }
  5807. if (!addr1) {
  5808. addr1 = @"";
  5809. }
  5810. DebugLog(@"addr");
  5811. addr = [RAConvertor arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  5812. addr = [AESCrypt fastencrypt:addr];
  5813. addr = [self translateSingleQuote:addr];
  5814. if (addr1 && ![addr1 isEqualToString:@""]) {
  5815. addr1 = [AESCrypt fastencrypt:addr1];
  5816. }
  5817. addr1 = [self translateSingleQuote:addr1];
  5818. addr2 = [self translateSingleQuote:addr2];
  5819. addr3 = [self translateSingleQuote:addr3];
  5820. addr4 = [self translateSingleQuote:addr4];
  5821. NSString *country = [params objectForKey:@"country"];
  5822. if (country) {
  5823. country = [self countryNameByCountryCodeId:country];
  5824. } else {
  5825. country = @"";
  5826. }
  5827. int carrier = [params[@"carrier"] intValue];
  5828. DebugLog(@"country");
  5829. country = [self translateSingleQuote:country];
  5830. NSString *state = [params objectForKey:@"state"];
  5831. if (!state) {
  5832. state = @"";
  5833. }
  5834. DebugLog(@"state");
  5835. state = [self translateSingleQuote:state];
  5836. NSString *city = [params objectForKey:@"city"];
  5837. if (!city) {
  5838. city = @"";
  5839. }
  5840. city = [self translateSingleQuote:city];
  5841. NSString *zipcode = [params objectForKey:@"zipcode"];
  5842. if (!zipcode) {
  5843. zipcode = @"";
  5844. }
  5845. DebugLog(@"zip");
  5846. zipcode = [self translateSingleQuote:zipcode];
  5847. NSString *fistName = [params objectForKey:@"firstname"];
  5848. if (!fistName) {
  5849. fistName = @"";
  5850. }
  5851. NSString *lastName = [params objectForKey:@"lastname"];
  5852. if (!lastName) {
  5853. lastName = @"";
  5854. }
  5855. contact_name = [RAConvertor arr2string:@[fistName,lastName] separator:@" " trim:true];
  5856. DebugLog(@"contact_name");
  5857. contact_name = [self translateSingleQuote:contact_name];
  5858. fistName = [self translateSingleQuote:fistName];
  5859. lastName = [self translateSingleQuote:lastName];
  5860. NSString *phone = [params objectForKey:@"phone"];
  5861. if (phone) {
  5862. phone = [AESCrypt fastencrypt:phone];
  5863. } else {
  5864. phone = @"";
  5865. }
  5866. DebugLog(@"PHONE");
  5867. phone = [self translateSingleQuote:phone];
  5868. NSString *fax = [params objectForKey:@"fax"];
  5869. if (!fax) {
  5870. fax = @"";
  5871. }
  5872. DebugLog(@"FAX");
  5873. fax = [self translateSingleQuote:fax];
  5874. NSString *email = [params objectForKey:@"email"];
  5875. if (!email) {
  5876. email = @"";
  5877. }
  5878. DebugLog(@"EMAIL:%@",email);
  5879. email = [self translateSingleQuote:email];
  5880. NSString *notes = [params objectForKey:@"contact_notes"];
  5881. if (!notes) {
  5882. notes = @"";
  5883. }
  5884. DebugLog(@"NOTE:%@",notes);
  5885. notes = [self translateSingleQuote:notes];
  5886. NSString *price = [params objectForKey:@"price_name"];
  5887. if (price) {
  5888. price = [self priceNameByPriceId:price];
  5889. } else {
  5890. price = @"";
  5891. }
  5892. DebugLog(@"PRICE");
  5893. price = [self translateSingleQuote:price];
  5894. NSString *salesRep = [params objectForKey:@"sales_rep"];
  5895. if (salesRep) {
  5896. salesRep = [self salesRepCodeById:salesRep];
  5897. } else {
  5898. salesRep = @"";
  5899. }
  5900. salesRep = [self translateSingleQuote:salesRep];
  5901. NSString *img = [params objectForKey:@"business_card"];
  5902. NSArray *array = [img componentsSeparatedByString:@","];
  5903. NSString *img_0 = array[0];
  5904. if (!img_0) {
  5905. img_0 = @"";
  5906. }
  5907. img_0 = [self translateSingleQuote:img_0];
  5908. NSString *img_1 = array[1];
  5909. if (!img_1) {
  5910. img_1 = @"";
  5911. }
  5912. img_1 = [self translateSingleQuote:img_1];
  5913. NSString *img_2 = array[2];
  5914. if (!img_2) {
  5915. img_2 = @"";
  5916. }
  5917. img_2 = [self translateSingleQuote:img_2];
  5918. NSString *contact_id = [NSUUID UUID].UUIDString;
  5919. NSString *contact_type = [params objectForKey:@"type_name"];
  5920. if (!contact_type) {
  5921. contact_type = @"";
  5922. }
  5923. contact_type = [self translateSingleQuote:contact_type];
  5924. // 判断更新时是否为customer
  5925. if (update) {
  5926. contact_id = [params objectForKey:@"contact_id"];
  5927. if (!contact_id) {
  5928. contact_id = @"";
  5929. }
  5930. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  5931. __block int customer = 0;
  5932. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5933. customer = sqlite3_column_int(stmt, 0);
  5934. }];
  5935. isCustomer = customer ? YES : NO;
  5936. }
  5937. NSMutableDictionary *sync_dic = [params mutableCopy];
  5938. if (isCustomer) {
  5939. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  5940. } else {
  5941. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  5942. }
  5943. sync_dic[@"truck_carrier"] = [self offline_getCarrier: [sync_dic[@"carrier"] intValue]];
  5944. NSString *sync_data = nil;
  5945. NSString *sql = nil;
  5946. if (update){
  5947. contact_id = [params objectForKey:@"contact_id"];
  5948. if (!contact_id) {
  5949. contact_id = @"";
  5950. }
  5951. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5952. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5953. sync_data = [RAConvertor dict2string:sync_dic];
  5954. sync_data = [self translateSingleQuote:sync_data];
  5955. 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];
  5956. } else {
  5957. contact_id = [self translateSingleQuote:contact_id];
  5958. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5959. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5960. sync_data = [RAConvertor dict2string:sync_dic];
  5961. sync_data = [self translateSingleQuote:sync_data];
  5962. 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];
  5963. }
  5964. int result = [iSalesDB execSql:sql];
  5965. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  5966. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  5967. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  5968. }
  5969. #pragma mark save new contact
  5970. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  5971. {
  5972. return [self offline_saveContact:params update:NO isCustomer:YES];
  5973. }
  5974. #pragma mark edit contact
  5975. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  5976. {
  5977. assert(params[@"user"]!=nil);
  5978. // {
  5979. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  5980. // password = 123456;
  5981. // user = EvanK;
  5982. // }
  5983. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  5984. NSData *data = [NSData dataWithContentsOfFile:path];
  5985. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5986. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5987. NSString *countryCode = nil;
  5988. NSString *countryCode_id = nil;
  5989. NSString *stateCode = nil;
  5990. __block int carrierCode = -1;
  5991. /*------contact infor------*/
  5992. __block NSString *country = nil;
  5993. __block NSString *company_name = nil;
  5994. __block NSString *contact_id = params[@"contact_id"];
  5995. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  5996. __block NSString *zipcode = nil;
  5997. __block NSString *state = nil; // state_code
  5998. __block NSString *city = nil; //
  5999. __block NSString *firt_name,*last_name;
  6000. __block NSString *phone,*fax,*email;
  6001. __block NSString *notes,*price_type,*sales_rep;
  6002. __block NSString *img_0,*img_1,*img_2;
  6003. __block NSString *contact_type;
  6004. contact_id = [self translateSingleQuote:contact_id];
  6005. 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];
  6006. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6007. country = [self textAtColumn:0 statement:stmt]; // country name
  6008. company_name = [self textAtColumn:1 statement:stmt];
  6009. addr_1 = [self textAtColumn:2 statement:stmt];
  6010. addr_2 = [self textAtColumn:3 statement:stmt];
  6011. addr_3 = [self textAtColumn:4 statement:stmt];
  6012. addr_4 = [self textAtColumn:5 statement:stmt];
  6013. zipcode = [self textAtColumn:6 statement:stmt];
  6014. state = [self textAtColumn:7 statement:stmt]; // state code
  6015. city = [self textAtColumn:8 statement:stmt];
  6016. firt_name = [self textAtColumn:9 statement:stmt];
  6017. last_name = [self textAtColumn:10 statement:stmt];
  6018. phone = [self textAtColumn:11 statement:stmt];
  6019. fax = [self textAtColumn:12 statement:stmt];
  6020. email = [self textAtColumn:13 statement:stmt];
  6021. notes = [self textAtColumn:14 statement:stmt];
  6022. price_type = [self textAtColumn:15 statement:stmt]; // name
  6023. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  6024. img_0 = [self textAtColumn:17 statement:stmt];
  6025. img_1 = [self textAtColumn:18 statement:stmt];
  6026. img_2 = [self textAtColumn:19 statement:stmt];
  6027. contact_type = [self textAtColumn:20 statement:stmt];
  6028. carrierCode = sqlite3_column_int(stmt, 21);
  6029. }];
  6030. // decrypt
  6031. if (company_name) {
  6032. company_name = [AESCrypt fastdecrypt:company_name];
  6033. }
  6034. if (addr_1) {
  6035. addr_1 = [AESCrypt fastdecrypt:addr_1];
  6036. }
  6037. if (phone) {
  6038. phone = [AESCrypt fastdecrypt:phone];
  6039. }
  6040. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  6041. countryCode = [iSalesDB jk_queryText:countrySql];
  6042. stateCode = state;
  6043. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  6044. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  6045. NSString *code_id = params[@"country"];
  6046. countryCode_id = code_id;
  6047. countryCode = [self countryCodeByid:code_id];
  6048. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  6049. NSString *zip_code = params[@"zipcode"];
  6050. // 剔除全部为空格
  6051. int spaceCount = 0;
  6052. for (int i = 0; i < zip_code.length; i++) {
  6053. if ([zip_code characterAtIndex:i] == ' ') {
  6054. spaceCount++;
  6055. }
  6056. }
  6057. if (spaceCount == zip_code.length) {
  6058. zip_code = @"";
  6059. }
  6060. if (zipcode.length > 0) {
  6061. zipcode = zip_code;
  6062. countryCode_id = params[@"country"];
  6063. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  6064. countryCode = [dic valueForKey:@"country_code"];
  6065. if (!countryCode) {
  6066. NSString *code_id = params[@"country"];
  6067. countryCode = [self countryCodeByid:code_id];
  6068. }
  6069. stateCode = [dic valueForKey:@"state_code"];
  6070. if (!stateCode.length) {
  6071. stateCode = params[@"state"];
  6072. }
  6073. city = [dic valueForKey:@"city"];
  6074. if (!city.length) {
  6075. city = params[@"city"];
  6076. }
  6077. // zip code
  6078. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  6079. [zipDic setValue:zipcode forKey:@"value"];
  6080. [section_0 setValue:zipDic forKey:@"item_8"];
  6081. } else {
  6082. NSString *code_id = params[@"country"];
  6083. countryCode = [self countryCodeByid:code_id];
  6084. stateCode = params[@"state"];
  6085. city = params[@"city"];
  6086. }
  6087. }
  6088. }
  6089. // 0 Country
  6090. // 1 Company Name
  6091. // 2 Contact ID
  6092. // 3 Picture
  6093. // 4 Address 1
  6094. // 5 Address 2
  6095. // 6 Address 3
  6096. // 7 Address 4
  6097. // 8 Zip Code
  6098. // 9 State/Province
  6099. // 10 City
  6100. // 11 Contact First Name
  6101. // 12 Contact Last Name
  6102. // 13 Phone
  6103. // 14 Fax
  6104. // 15 Email
  6105. // 16 Contact Notes
  6106. // 17 Price Type
  6107. // 18 Contact Type
  6108. // 19 Sales Rep
  6109. // country
  6110. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  6111. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  6112. // company
  6113. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  6114. // contact_id
  6115. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  6116. // picture
  6117. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  6118. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  6119. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  6120. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  6121. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  6122. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  6123. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  6124. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  6125. // addr 1 2 3 4
  6126. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  6127. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  6128. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  6129. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  6130. // zip code
  6131. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  6132. // state
  6133. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  6134. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  6135. // city
  6136. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  6137. // first last
  6138. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  6139. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  6140. // phone fax email
  6141. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  6142. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  6143. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  6144. // notes
  6145. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  6146. // price
  6147. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  6148. for (NSString *key in priceDic.allKeys) {
  6149. if ([key containsString:@"val_"]) {
  6150. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  6151. if ([dic[@"value"] isEqualToString:price_type]) {
  6152. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6153. [priceDic setValue:dic forKey:key];
  6154. }
  6155. }
  6156. }
  6157. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  6158. // Contact Rep
  6159. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  6160. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  6161. // Sales Rep
  6162. NSMutableDictionary *repDic = [[self offline_getSalesRep:params[@"user"]] mutableCopy];
  6163. for (NSString *key in repDic.allKeys) {
  6164. if ([key containsString:@"val_"]) {
  6165. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  6166. NSString *value = dic[@"value"];
  6167. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  6168. if (code && [code isEqualToString:sales_rep]) {
  6169. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6170. [repDic setValue:dic forKey:key];
  6171. }
  6172. }
  6173. }
  6174. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  6175. #ifdef BUILD_CONTRAST
  6176. //carrier
  6177. NSString* scarrier = [self offline_getCarrier:carrierCode];
  6178. NSDictionary *carrierDic = [self offline_getAllCarrier:scarrier];
  6179. [self setValue:carrierDic forItemKey:@"item_20" valueKey:@"cadedate" inDictionary:section_0];
  6180. #endif
  6181. [ret setValue:section_0 forKey:@"section_0"];
  6182. return [RAConvertor dict2data:ret];
  6183. }
  6184. #pragma mark save contact
  6185. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  6186. {
  6187. return [self offline_saveContact:params update:YES isCustomer:YES];
  6188. }
  6189. #pragma mark category
  6190. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  6191. if (ck) {
  6192. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  6193. for (NSString *key in res.allKeys) {
  6194. if (![key isEqualToString:@"count"]) {
  6195. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  6196. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  6197. if ([val[@"value"] isEqualToString:ck]) {
  6198. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6199. }
  6200. [res setValue:val forKey:key];
  6201. }
  6202. }
  6203. [dic setValue:res forKey:valueKey];
  6204. }
  6205. }
  6206. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  6207. // NSString* orderCode = [params valueForKey:@"orderCode"];
  6208. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6209. NSString* category = [params valueForKey:@"category"];
  6210. if (!category || [category isEqualToString:@""]) {
  6211. category = @"%";
  6212. }
  6213. category = [self translateSingleQuote:category];
  6214. int limit = [[params valueForKey:@"limit"] intValue];
  6215. int offset = [[params valueForKey:@"offset"] intValue];
  6216. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  6217. NSString *limit_str = @"";
  6218. if (limited) {
  6219. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  6220. }
  6221. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  6222. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  6223. sqlite3 *db = [iSalesDB get_db];
  6224. // [iSalesDB AddExFunction:db];
  6225. int count;
  6226. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  6227. 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];
  6228. double price_min = 0;
  6229. double price_max = 0;
  6230. if ([params.allKeys containsObject:@"alert"]) {
  6231. // alert
  6232. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  6233. NSString *alert = params[@"alert"];
  6234. if ([alert isEqualToString:@"Display All"]) {
  6235. alert = [NSString stringWithFormat:@""];
  6236. } else {
  6237. alert = [self translateSingleQuote:alert];
  6238. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  6239. }
  6240. // available
  6241. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  6242. NSString *available = params[@"available"];
  6243. NSString *available_condition;
  6244. if ([available isEqualToString:@"Display All"]) {
  6245. available_condition = @"";
  6246. } else if ([available isEqualToString:@"Available Now"]) {
  6247. available_condition = @"and availability > 0";
  6248. } else {
  6249. available_condition = @"and availability == 0";
  6250. }
  6251. // best seller
  6252. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  6253. NSString *best_seller = @"";
  6254. NSString *order_best_seller = @"m.name asc";
  6255. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  6256. best_seller = @"and best_seller > 0";
  6257. order_best_seller = @"m.best_seller desc,m.name asc";
  6258. }
  6259. // price
  6260. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  6261. NSString *price = params[@"price"];
  6262. price_min = 0;
  6263. price_max = MAXFLOAT;
  6264. if (params[@"user"] && price != nil) {
  6265. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  6266. NSMutableString *priceName = [NSMutableString string];
  6267. for (int i = 0; i < priceTypeArray.count; i++) {
  6268. NSString *pricetype = priceTypeArray[i];
  6269. pricetype = [self translateSingleQuote:pricetype];
  6270. if (i == 0) {
  6271. [priceName appendFormat:@"'%@'",pricetype];
  6272. } else {
  6273. [priceName appendFormat:@",'%@'",pricetype];
  6274. }
  6275. }
  6276. if ([price isEqualToString:@"Display All"]) {
  6277. price = [NSString stringWithFormat:@""];
  6278. } else if([price containsString:@"+"]){
  6279. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  6280. price_min = [price doubleValue];
  6281. 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];
  6282. } else {
  6283. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  6284. price_min = [[priceArray objectAtIndex:0] doubleValue];
  6285. price_max = [[priceArray objectAtIndex:1] doubleValue];
  6286. 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];
  6287. }
  6288. } else {
  6289. price = @"";
  6290. }
  6291. // sold_by_qty : Sold in quantities of %@
  6292. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  6293. NSString *qty = params[@"sold_by_qty"];
  6294. if ([qty isEqualToString:@"Display All"]) {
  6295. qty = @"";
  6296. } else {
  6297. qty = [self translateSingleQuote:qty];
  6298. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  6299. }
  6300. // model name;
  6301. NSString* modelname =params[@"modelName"];
  6302. if(modelname.length==0)
  6303. {
  6304. modelname=@"";
  6305. }
  6306. else
  6307. {
  6308. modelname = modelname.lowercaseString;
  6309. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  6310. }
  6311. //modelDescription
  6312. NSString* modelDescription =params[@"modelDescription"];
  6313. if(modelDescription.length==0)
  6314. {
  6315. modelDescription=@"";
  6316. }
  6317. else
  6318. {
  6319. modelDescription = modelDescription.lowercaseString;
  6320. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  6321. }
  6322. // cate
  6323. // category = [self translateSingleQuote:category];
  6324. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  6325. // cate mutiple selection
  6326. NSString *category_id = params[@"category"];
  6327. NSMutableArray *cate_id_array = nil;
  6328. NSMutableString *cateWhere = [NSMutableString string];
  6329. if ([category_id isEqualToString:@""] || !category_id) {
  6330. [cateWhere appendString:@"1 = 1"];
  6331. } else {
  6332. if ([category_id containsString:@","]) {
  6333. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  6334. } else {
  6335. cate_id_array = [@[category_id] mutableCopy];
  6336. }
  6337. [cateWhere appendString:@"("];
  6338. for (int i = 0; i < cate_id_array.count; i++) {
  6339. if (i == 0) {
  6340. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6341. } else {
  6342. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6343. }
  6344. }
  6345. [cateWhere appendString:@")"];
  6346. }
  6347. // where bestseller > 0 order by bestseller desc
  6348. // sql query: alert availability(int) best_seller(int) price qty
  6349. 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];
  6350. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  6351. }
  6352. DebugLog(@"offline category where: %@",where);
  6353. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  6354. if (!params[@"user"]) {
  6355. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  6356. }
  6357. [ret setValue:filter forKey:@"filter"];
  6358. DebugLog(@"offline_category sql:%@",sqlQuery);
  6359. sqlite3_stmt * statement;
  6360. [ret setValue:@"2" forKey:@"result"];
  6361. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  6362. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  6363. // int count=0;
  6364. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6365. {
  6366. int i=0;
  6367. while (sqlite3_step(statement) == SQLITE_ROW)
  6368. {
  6369. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  6370. char *name = (char*)sqlite3_column_text(statement, 0);
  6371. if(name==nil)
  6372. name="";
  6373. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  6374. char *description = (char*)sqlite3_column_text(statement, 1);
  6375. if(description==nil)
  6376. description="";
  6377. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  6378. int product_id = sqlite3_column_int(statement, 2);
  6379. int wid = sqlite3_column_int(statement, 3);
  6380. int closeout = sqlite3_column_int(statement, 4);
  6381. int cid = sqlite3_column_int(statement, 5);
  6382. int wisdelete = sqlite3_column_int(statement, 6);
  6383. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  6384. int more_color = sqlite3_column_int(statement, 8);
  6385. // Defaul Category ID
  6386. __block NSString *categoryID = nil;
  6387. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  6388. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6389. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  6390. if(default_category==nil)
  6391. default_category="";
  6392. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  6393. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  6394. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  6395. categoryID = nsdefault_category;
  6396. }];
  6397. if (!categoryID.length) {
  6398. NSString *cateIDs = params[@"category"];
  6399. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  6400. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  6401. for (NSString *cateID in requestCategoryArr) {
  6402. BOOL needBreak = NO;
  6403. for (NSString *itemCateIDBox in itemCategoryArr) {
  6404. if (itemCateIDBox.length > 4) {
  6405. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  6406. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  6407. if ([itemCategoryID isEqualToString:cateID]) {
  6408. needBreak = YES;
  6409. categoryID = itemCategoryID;
  6410. break;
  6411. }
  6412. }
  6413. }
  6414. if (needBreak) {
  6415. break;
  6416. }
  6417. }
  6418. }
  6419. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  6420. if(wid !=0 && wisdelete != 1)
  6421. [item setValue:@"true" forKey:@"wish_exists"];
  6422. else
  6423. [item setValue:@"false" forKey:@"wish_exists"];
  6424. if(closeout==0)
  6425. [item setValue:@"false" forKey:@"is_closeout"];
  6426. else
  6427. [item setValue:@"true" forKey:@"is_closeout"];
  6428. if(cid==0)
  6429. [item setValue:@"false" forKey:@"cart_exists"];
  6430. else
  6431. [item setValue:@"true" forKey:@"cart_exists"];
  6432. if (more_color == 0) {
  6433. [item setObject:@(false) forKey:@"more_color"];
  6434. } else if (more_color == 1) {
  6435. [item setObject:@(true) forKey:@"more_color"];
  6436. }
  6437. [item addEntriesFromDictionary:imgjson];
  6438. // [item setValue:nsurl forKey:@"img"];
  6439. [item setValue:nsname forKey:@"name"];
  6440. [item setValue:nsdescription forKey:@"description"];
  6441. if (categoryID) {
  6442. [item setValue:categoryID forKey:@"item_category_id"];
  6443. }
  6444. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  6445. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  6446. i++;
  6447. }
  6448. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  6449. [ret setObject:items forKey:@"items"];
  6450. sqlite3_finalize(statement);
  6451. } else {
  6452. DebugLog(@"nothing...");
  6453. }
  6454. DebugLog(@"count:%d",count);
  6455. [iSalesDB close_db:db];
  6456. #ifdef DEBUG
  6457. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  6458. #endif
  6459. return ret;
  6460. }
  6461. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  6462. {
  6463. return [self categoryList:params limited:YES];
  6464. }
  6465. # pragma mark item search
  6466. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  6467. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6468. // assert(params[@"order_code"]);
  6469. // params[@"user"]
  6470. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  6471. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  6472. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  6473. // category
  6474. NSDictionary *category_menu = [self offline_category_menu];
  6475. [filter setValue:category_menu forKey:@"category"];
  6476. NSString* where= nil;
  6477. NSString* orderby= @"m.name";
  6478. if (!filterSearch) {
  6479. int covertype = [[params valueForKey:@"covertype"] intValue];
  6480. switch (covertype) {
  6481. case 0:
  6482. {
  6483. where=@"m.category like'%%#005#%%'";
  6484. break;
  6485. }
  6486. case 1:
  6487. {
  6488. where=@"m.alert like '%QS%'";
  6489. break;
  6490. }
  6491. case 2:
  6492. {
  6493. where=@"m.availability>0";
  6494. break;
  6495. }
  6496. case 3:
  6497. {
  6498. where=@"m.best_seller>0";
  6499. orderby=@"m.best_seller desc,m.name asc";
  6500. break;
  6501. }
  6502. default:
  6503. where=@"1=1";
  6504. break;
  6505. }
  6506. }
  6507. int limit = [[params valueForKey:@"limit"] intValue];
  6508. int offset = [[params valueForKey:@"offset"] intValue];
  6509. NSString *limit_str = @"";
  6510. if (limited) {
  6511. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  6512. }
  6513. sqlite3 *db = [iSalesDB get_db];
  6514. // [iSalesDB AddExFunction:db];
  6515. int count;
  6516. NSString *sqlQuery = nil;
  6517. where = [where stringByAppendingString:@" and m.is_active = 1"];
  6518. 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];
  6519. double price_min = 0;
  6520. double price_max = 0;
  6521. if (filterSearch) {
  6522. // alert
  6523. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  6524. NSString *alert = params[@"alert"];
  6525. if ([alert isEqualToString:@"Display All"]) {
  6526. alert = [NSString stringWithFormat:@""];
  6527. } else {
  6528. alert = [self translateSingleQuote:alert];
  6529. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  6530. }
  6531. // available
  6532. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  6533. NSString *available = params[@"available"];
  6534. NSString *available_condition;
  6535. if ([available isEqualToString:@"Display All"]) {
  6536. available_condition = @"";
  6537. } else if ([available isEqualToString:@"Available Now"]) {
  6538. available_condition = @"and availability > 0";
  6539. } else {
  6540. available_condition = @"and availability == 0";
  6541. }
  6542. // best seller
  6543. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  6544. NSString *best_seller = @"";
  6545. NSString *order_best_seller = @"m.name asc";
  6546. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  6547. best_seller = @"and best_seller > 0";
  6548. order_best_seller = @"m.best_seller desc,m.name asc";
  6549. }
  6550. // price
  6551. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  6552. NSString *price = params[@"price"];
  6553. price_min = 0;
  6554. price_max = MAXFLOAT;
  6555. if (params[@"user"]) {
  6556. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  6557. NSMutableString *priceName = [NSMutableString string];
  6558. for (int i = 0; i < priceTypeArray.count; i++) {
  6559. NSString *pricetype = priceTypeArray[i];
  6560. pricetype = [self translateSingleQuote:pricetype];
  6561. if (i == 0) {
  6562. [priceName appendFormat:@"'%@'",pricetype];
  6563. } else {
  6564. [priceName appendFormat:@",'%@'",pricetype];
  6565. }
  6566. }
  6567. if ([price isEqualToString:@"Display All"]) {
  6568. price = [NSString stringWithFormat:@""];
  6569. } else if([price containsString:@"+"]){
  6570. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  6571. price_min = [price doubleValue];
  6572. 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];
  6573. } else {
  6574. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  6575. price_min = [[priceArray objectAtIndex:0] doubleValue];
  6576. price_max = [[priceArray objectAtIndex:1] doubleValue];
  6577. 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];
  6578. }
  6579. } else {
  6580. price = @"";
  6581. }
  6582. // sold_by_qty : Sold in quantities of %@
  6583. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  6584. NSString *qty = params[@"sold_by_qty"];
  6585. if ([qty isEqualToString:@"Display All"]) {
  6586. qty = @"";
  6587. } else {
  6588. qty = [self translateSingleQuote:qty];
  6589. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  6590. }
  6591. // model name;
  6592. NSString* modelname =params[@"modelName"];
  6593. if(modelname.length==0)
  6594. {
  6595. modelname=@"";
  6596. }
  6597. else
  6598. {
  6599. modelname = modelname.lowercaseString;
  6600. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  6601. }
  6602. //modelDescription
  6603. NSString* modelDescription =params[@"modelDescription"];
  6604. if(modelDescription.length==0)
  6605. {
  6606. modelDescription=@"";
  6607. }
  6608. else
  6609. {
  6610. modelDescription = modelDescription.lowercaseString;
  6611. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  6612. }
  6613. //lower(description) like'%%%@%%'
  6614. // category
  6615. NSString *category_id = params[@"ctgId"];
  6616. NSMutableArray *cate_id_array = nil;
  6617. NSMutableString *cateWhere = [NSMutableString string];
  6618. if ([category_id isEqualToString:@""] || !category_id) {
  6619. [cateWhere appendString:@"1 = 1"];
  6620. } else {
  6621. if ([category_id containsString:@","]) {
  6622. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  6623. } else {
  6624. cate_id_array = [@[category_id] mutableCopy];
  6625. }
  6626. /*-----------*/
  6627. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  6628. [cateWhere appendString:@"("];
  6629. for (int i = 0; i < cate_id_array.count; i++) {
  6630. for (NSString *key0 in cateDic.allKeys) {
  6631. if ([key0 containsString:@"category_"]) {
  6632. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  6633. for (NSString *key1 in category0.allKeys) {
  6634. if ([key1 containsString:@"category_"]) {
  6635. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  6636. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  6637. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6638. cate_id_array[i] = [category1 objectForKey:@"id"];
  6639. if (i == 0) {
  6640. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6641. } else {
  6642. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6643. }
  6644. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6645. [category0 setValue:category1 forKey:key1];
  6646. [cateDic setValue:category0 forKey:key0];
  6647. }
  6648. } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  6649. if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  6650. cate_id_array[i] = [category0 objectForKey:@"id"];
  6651. if (i == 0) {
  6652. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  6653. } else {
  6654. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  6655. }
  6656. [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  6657. [cateDic setValue:category0 forKey:key0];
  6658. }
  6659. }
  6660. }
  6661. }
  6662. }
  6663. }
  6664. [cateWhere appendString:@")"];
  6665. [filter setValue:cateDic forKey:@"category"];
  6666. }
  6667. // where bestseller > 0 order by bestseller desc
  6668. // sql query: alert availability(int) best_seller(int) price qty
  6669. 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];
  6670. // count
  6671. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,modelname,modelDescription,price];
  6672. }
  6673. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  6674. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  6675. if (!params[@"user"]) {
  6676. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  6677. }
  6678. [ret setValue:filter forKey:@"filter"];
  6679. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  6680. sqlite3_stmt * statement;
  6681. [ret setValue:@"2" forKey:@"result"];
  6682. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  6683. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  6684. // int count=0;
  6685. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6686. {
  6687. int i=0;
  6688. while (sqlite3_step(statement) == SQLITE_ROW)
  6689. {
  6690. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  6691. char *name = (char*)sqlite3_column_text(statement, 0);
  6692. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  6693. char *description = (char*)sqlite3_column_text(statement, 1);
  6694. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  6695. int product_id = sqlite3_column_int(statement, 2);
  6696. int wid = sqlite3_column_int(statement, 3);
  6697. int closeout = sqlite3_column_int(statement, 4);
  6698. int cid = sqlite3_column_int(statement, 5);
  6699. int wisdelete = sqlite3_column_int(statement, 6);
  6700. int more_color = sqlite3_column_int(statement, 7);
  6701. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  6702. if(wid !=0 && wisdelete != 1)
  6703. [item setValue:@"true" forKey:@"wish_exists"];
  6704. else
  6705. [item setValue:@"false" forKey:@"wish_exists"];
  6706. if(closeout==0)
  6707. [item setValue:@"false" forKey:@"is_closeout"];
  6708. else
  6709. [item setValue:@"true" forKey:@"is_closeout"];
  6710. if(cid==0)
  6711. [item setValue:@"false" forKey:@"cart_exists"];
  6712. else
  6713. [item setValue:@"true" forKey:@"cart_exists"];
  6714. if (more_color == 0) {
  6715. [item setObject:@(false) forKey:@"more_color"];
  6716. } else if (more_color == 1) {
  6717. [item setObject:@(true) forKey:@"more_color"];
  6718. }
  6719. [item addEntriesFromDictionary:imgjson];
  6720. // [item setValue:nsurl forKey:@"img"];
  6721. [item setValue:nsname forKey:@"fash_name"];
  6722. [item setValue:nsdescription forKey:@"description"];
  6723. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  6724. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  6725. i++;
  6726. }
  6727. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  6728. [ret setObject:items forKey:@"items"];
  6729. sqlite3_finalize(statement);
  6730. }
  6731. [iSalesDB close_db:db];
  6732. #ifdef DEBUG
  6733. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  6734. #endif
  6735. return ret;
  6736. }
  6737. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  6738. {
  6739. return [self itemsearch:params limited:YES];
  6740. }
  6741. #pragma mark order detail
  6742. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  6743. if (str1.length == 0) {
  6744. str1 = @"&nbsp";
  6745. }
  6746. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  6747. return str;
  6748. }
  6749. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  6750. {
  6751. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  6752. [fromformatter setDateFormat:from];
  6753. NSDate *date = [fromformatter dateFromString:datetime];
  6754. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  6755. [toformatter setDateFormat:to];
  6756. NSString * ret = [toformatter stringFromDate:date];
  6757. return ret;
  6758. }
  6759. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  6760. // 把毫秒去掉
  6761. if ([dateTime containsString:@"."]) {
  6762. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  6763. }
  6764. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  6765. formatter.dateFormat = formate;
  6766. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  6767. NSDate *date = [formatter dateFromString:dateTime];
  6768. formatter.dateFormat = newFormate;
  6769. NSString *result = [formatter stringFromDate:date];
  6770. return result ? result : @"";
  6771. }
  6772. + (NSString *)rchangeDateFormate:(NSString *)dateTime {
  6773. return [self changeDateTime:dateTime Formate:@"MM/dd/yyyy" withFormate:@"yyyy-MM-dd"];
  6774. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6775. }
  6776. + (NSString *)changeDateFormate:(NSString *)dateTime {
  6777. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd" withFormate:@"MM/dd/yyyy"];
  6778. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6779. }
  6780. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  6781. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  6782. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  6783. }
  6784. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  6785. {
  6786. assert(params[@"mode"]!=nil);
  6787. assert(params[@"user"]!=nil);
  6788. DebugLog(@"offline oderdetail params: %@",params);
  6789. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6790. int orderId = [params[@"orderId"] intValue];
  6791. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  6792. // decrypt card number and card security code
  6793. // 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 ];
  6794. 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];
  6795. sqlite3 *db = [iSalesDB get_db];
  6796. sqlite3_stmt * statement;
  6797. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  6798. NSString *nssoid = @"";
  6799. NSString* orderinfo = @"";
  6800. NSString *moreInfo = @"";
  6801. double handlingFee = 0;
  6802. double payments_and_credist = 0;
  6803. double totalPrice = 0;
  6804. double shippingFee = 0;
  6805. double lift_gate = 0;
  6806. NSString *customer_contact = @"";
  6807. NSString *customer_email = @"";
  6808. NSString *customer_fax = @"";
  6809. NSString *customer_phone = @"";
  6810. NSString *customer_city = @"";
  6811. NSString *customer_state = @"";
  6812. NSString *customer_zipcode = @"";
  6813. NSString *customer_country = @"";
  6814. BOOL must_call = NO;
  6815. double discount = 0;
  6816. __block NSMutableDictionary *section_0 = [NSMutableDictionary dictionary];
  6817. __block NSMutableDictionary *section_1 = [NSMutableDictionary dictionary];
  6818. __block NSMutableDictionary *section_2 = [NSMutableDictionary dictionary];
  6819. __block NSMutableDictionary *section_3 = [NSMutableDictionary dictionary];
  6820. __block NSMutableDictionary *section_4 = [NSMutableDictionary dictionary];
  6821. __block NSMutableDictionary *price_data = [NSMutableDictionary dictionary];
  6822. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  6823. {
  6824. if (sqlite3_step(statement) == SQLITE_ROW)
  6825. {
  6826. // int order_id = sqlite3_column_int(statement, 0);
  6827. NSString *sign_url = [self textAtColumn:51 statement:statement];
  6828. // ret[@"sign_url"] = sign_url;
  6829. section_1 = @{
  6830. @"data":sign_url,
  6831. @"title":@"Signature",
  6832. @"type":@"sign_url"
  6833. }.mutableCopy;
  6834. [ret setObject:section_1 forKey:@"section_1"];
  6835. customer_contact = [self textAtColumn:52 statement:statement];
  6836. customer_email = [self textAtColumn:53 statement:statement];
  6837. customer_phone = [self textAtColumn:54 statement:statement];
  6838. customer_fax = [self textAtColumn:55 statement:statement];
  6839. customer_city = [self textAtColumn:60 statement:statement];
  6840. customer_state = [self textAtColumn:61 statement:statement];
  6841. customer_zipcode = [self textAtColumn:62 statement:statement];
  6842. customer_country = [self textAtColumn:63 statement:statement];
  6843. must_call = [[self textAtColumn:64 statement:statement] boolValue];
  6844. discount =sqlite3_column_double(statement, 65);
  6845. int offline_edit=sqlite3_column_int(statement, 56);
  6846. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  6847. char *soid = (char*)sqlite3_column_text(statement, 1);
  6848. if(soid==nil)
  6849. soid= "";
  6850. nssoid= [[NSString alloc]initWithUTF8String:soid];
  6851. // so#
  6852. ret[@"so#"] = nssoid;
  6853. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  6854. if(poNumber==nil)
  6855. poNumber= "";
  6856. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  6857. char *create_time = (char*)sqlite3_column_text(statement, 3);
  6858. if(create_time==nil)
  6859. create_time= "";
  6860. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  6861. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  6862. int status = sqlite3_column_int(statement, 4);
  6863. int erpStatus = sqlite3_column_int(statement, 49);
  6864. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  6865. // status
  6866. if (status > 1 && status != 3) {
  6867. status = erpStatus;
  6868. } else if (status == 3) {
  6869. status = 15;
  6870. }
  6871. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  6872. ret[@"order_status"] = nsstatus;
  6873. char *company_name = (char*)sqlite3_column_text(statement, 5);
  6874. if(company_name==nil)
  6875. company_name= "";
  6876. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  6877. // company name
  6878. ret[@"company_name"] = nscompany_name;
  6879. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  6880. if(customer_contact==nil)
  6881. customer_contact= "";
  6882. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  6883. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  6884. if(addr_1==nil)
  6885. addr_1="";
  6886. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  6887. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  6888. if(addr_2==nil)
  6889. addr_2="";
  6890. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  6891. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  6892. if(addr_3==nil)
  6893. addr_3="";
  6894. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  6895. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  6896. if(addr_4==nil)
  6897. addr_4="";
  6898. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  6899. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  6900. [arr_addr addObject:nsaddr_1];
  6901. [arr_addr addObject:nsaddr_2];
  6902. [arr_addr addObject:nsaddr_3];
  6903. [arr_addr addObject:nsaddr_4];
  6904. [arr_addr addObject:[NSString stringWithFormat:@"%@,",customer_city]];
  6905. [arr_addr addObject:customer_state];
  6906. [arr_addr addObject:customer_zipcode];
  6907. [arr_addr addObject:customer_country];
  6908. // NSString * customer_address = [RAConvertor arr2string:arr_addr separator:@" " trim:true];
  6909. NSString *customer_address = [NSString stringWithFormat:@"%@<br/>%@, %@ %@ %@",nsaddr_1,customer_city,customer_state,customer_zipcode,customer_country];
  6910. char *logist = (char*)sqlite3_column_text(statement, 11);
  6911. if(logist==nil)
  6912. logist= "";
  6913. NSString *DebugLogist=[[NSString alloc]initWithUTF8String:logist];
  6914. if (/*status == -11 || */status == 10 || status == 11) {
  6915. DebugLogist = [self textAtColumn:59 statement:statement];
  6916. };
  6917. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  6918. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  6919. shipping = @"Shipping To Be Quoted";
  6920. } else {
  6921. shippingFee = sqlite3_column_double(statement, 12);
  6922. }
  6923. // Shipping
  6924. // ret[@"Shipping"] = shipping;
  6925. NSDictionary *shipping_item = @{
  6926. @"title":@"Shipping",
  6927. @"value":shipping
  6928. };
  6929. [price_data setObject:shipping_item forKey:@"item_1"];
  6930. int have_lift_gate = sqlite3_column_int(statement, 17);
  6931. lift_gate = sqlite3_column_double(statement, 13);
  6932. // Liftgate Fee(No loading dock)
  6933. if (!have_lift_gate) {
  6934. lift_gate = 0;
  6935. }
  6936. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  6937. NSString *liftgate_value = [NSString stringWithFormat:@"$%.2f",lift_gate];
  6938. if (sqlite3_column_int(statement, 57)) {
  6939. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  6940. liftgate_value = @"Shipping To Be Quoted";
  6941. }
  6942. NSDictionary *liftgate_item = @{
  6943. @"title":@"Liftgate Fee(No loading dock)",
  6944. @"value":liftgate_value
  6945. };
  6946. [price_data setObject:liftgate_item forKey:@"item_2"];
  6947. // if ([DebugLogist isEqualToString:@"WILL CALL"]) {
  6948. // [ret removeObjectForKey:@"Shipping"];
  6949. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  6950. // }
  6951. //
  6952. // if (have_lift_gate) {
  6953. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  6954. // }
  6955. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  6956. if(general_notes==nil)
  6957. general_notes= "";
  6958. NSString *nsgeneral_notes= [NSString stringWithFormat:@"MUST MAKE APPOINTMENT BEFORE DELIVERY = %@;<br/>%@",must_call ? @"YES" : @"NO",[[NSString alloc]initWithUTF8String:general_notes]];
  6959. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  6960. if(internal_notes==nil)
  6961. internal_notes= "";
  6962. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  6963. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  6964. if(payment_type==nil)
  6965. payment_type= "";
  6966. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  6967. // order info
  6968. orderinfo = [self textFileName:@"order_info.html"];
  6969. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  6970. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  6971. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  6972. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  6973. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  6974. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  6975. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  6976. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:DebugLogist];
  6977. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  6978. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  6979. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  6980. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  6981. NSString *payment = nil;
  6982. // id -> show
  6983. __block NSString *show_pay_type = nspayment_type;
  6984. [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) {
  6985. char *show_typ_ch = (char*)sqlite3_column_text(stmt, 0);
  6986. if (show_typ_ch != NULL) {
  6987. show_pay_type = [NSString stringWithUTF8String:show_typ_ch];
  6988. }
  6989. }];
  6990. if([nspayment_type isEqualToString:@"Credit Card"] || [nspayment_type isEqualToString:@"Visa/Master"])
  6991. {
  6992. payment = [self textFileName:@"creditcardpayment.html"];
  6993. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  6994. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  6995. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  6996. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  6997. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  6998. NSString *card_type = [self textAtColumn:42 statement:statement];
  6999. if (card_type.length > 0) { // 显示星号
  7000. card_type = @"****";
  7001. }
  7002. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  7003. if (card_number.length > 0 && card_number.length > 4) {
  7004. for (int i = 0; i < card_number.length - 4; i++) {
  7005. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  7006. }
  7007. } else {
  7008. card_number = @"";
  7009. }
  7010. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  7011. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  7012. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  7013. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  7014. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  7015. card_expiration = @"****";
  7016. }
  7017. NSString *card_city = [self textAtColumn:46 statement:statement];
  7018. NSString *card_state = [self textAtColumn:47 statement:statement];
  7019. payment = [self replaceHtml:payment String:@"Payment_Type" withString:show_pay_type];
  7020. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  7021. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  7022. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  7023. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  7024. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  7025. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  7026. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  7027. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  7028. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  7029. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  7030. }
  7031. else
  7032. {
  7033. payment=[self textFileName:@"normalpayment.html"];
  7034. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  7035. }
  7036. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  7037. ret[@"result"]= [NSNumber numberWithInt:2];
  7038. // more info
  7039. moreInfo = [self textFileName:@"more_info.html"];
  7040. /*****ship to******/
  7041. // ShipToCompany_or_&nbsp
  7042. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  7043. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  7044. NSString *shipToName = [self textAtColumn:19 statement:statement];
  7045. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  7046. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  7047. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7048. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"]; // 手动输入的可能是\r 或 \n
  7049. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7050. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  7051. /*****ship from******/
  7052. // ShipFromCompany_or_&nbsp
  7053. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  7054. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  7055. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  7056. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  7057. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  7058. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7059. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7060. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7061. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  7062. /*****freight to******/
  7063. // FreightBillToCompany_or_&nbsp
  7064. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  7065. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  7066. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  7067. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  7068. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  7069. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7070. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7071. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7072. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  7073. /*****merchandise to******/
  7074. // MerchandiseBillToCompany_or_&nbsp
  7075. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  7076. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  7077. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  7078. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  7079. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  7080. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7081. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7082. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7083. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  7084. /*****return to******/
  7085. // ReturnToCompany_or_&nbsp
  7086. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  7087. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  7088. NSString *returnToName = [self textAtColumn:31 statement:statement];
  7089. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  7090. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  7091. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  7092. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  7093. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  7094. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  7095. //
  7096. // DebugLog(@"more info : %@",moreInfo);
  7097. // handling fee
  7098. handlingFee = sqlite3_column_double(statement, 33);
  7099. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  7100. NSString *handling_fee_value = [NSString stringWithFormat:@"$%.2f",handlingFee];
  7101. if (sqlite3_column_int(statement, 58)) {
  7102. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  7103. handling_fee_value = @"Shipping To Be Quoted";
  7104. }
  7105. NSDictionary *handling_fee_item = @{
  7106. @"title":@"Handling Fee",
  7107. @"value":handling_fee_value
  7108. };
  7109. [price_data setObject:handling_fee_item forKey:@"item_3"];
  7110. //
  7111. // customer info
  7112. customerID = [self textAtColumn:36 statement:statement];
  7113. // mode
  7114. ret[@"mode"] = params[@"mode"];
  7115. // model_count
  7116. ret[@"model_count"] = @(0);
  7117. }
  7118. sqlite3_finalize(statement);
  7119. }
  7120. [iSalesDB close_db:db];
  7121. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID,@"mode":params[@"mode"]} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  7122. // "customer_email" = "Shui Hu";
  7123. // "customer_fax" = "";
  7124. // "customer_first_name" = F;
  7125. // "customer_last_name" = L;
  7126. // "customer_name" = ",da He Xiang Dong Liu A";
  7127. // "customer_phone" = "Hey Xuan Feng";
  7128. contactInfo[@"customer_phone"] = customer_phone;
  7129. contactInfo[@"customer_fax"] = customer_fax;
  7130. contactInfo[@"customer_email"] = customer_email;
  7131. NSString *first_name = @"";
  7132. NSString *last_name = @"";
  7133. if ([customer_contact isEqualToString:@""]) {
  7134. } else if ([customer_contact containsString:@" "]) {
  7135. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  7136. first_name = [customer_contact substringToIndex:first_space_index];
  7137. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  7138. }
  7139. contactInfo[@"customer_first_name"] = first_name;
  7140. contactInfo[@"customer_last_name"] = last_name;
  7141. ret[@"customerInfo"] = contactInfo;
  7142. // models
  7143. if (nssoid) {
  7144. __block NSMutableDictionary *model_data = [NSMutableDictionary dictionary];
  7145. __block double TotalCuft = 0;
  7146. __block double TotalWeight = 0;
  7147. __block int TotalCarton = 0;
  7148. __block double allItemPrice = 0;
  7149. 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];
  7150. sqlite3 *db1 = [iSalesDB get_db];
  7151. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7152. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  7153. int product_id = sqlite3_column_int(stmt, 0);
  7154. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  7155. int item_id = sqlite3_column_int(stmt, 7);
  7156. NSString* Price=nil;
  7157. if(str_price==nil)
  7158. {
  7159. NSNumber* price = [self get_model_default_price:customerID user:params[@"user"] product_id:nil item_id:@(item_id) db:db1];
  7160. if(price==nil)
  7161. Price=@"No Price.";
  7162. else
  7163. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7164. }
  7165. else
  7166. {
  7167. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  7168. }
  7169. double discount = sqlite3_column_double(stmt, 2);
  7170. int item_count = sqlite3_column_int(stmt, 3);
  7171. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  7172. NSString *nsline_note=nil;
  7173. if(line_note!=nil)
  7174. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  7175. // char *name = (char*)sqlite3_column_text(stmt, 5);
  7176. // NSString *nsname = nil;
  7177. // if(name!=nil)
  7178. // nsname= [[NSString alloc]initWithUTF8String:name];
  7179. NSString *nsname = [self textAtColumn:5 statement:stmt];
  7180. // char *description = (char*)sqlite3_column_text(stmt, 6);
  7181. // NSString *nsdescription=nil;
  7182. // if(description!=nil)
  7183. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  7184. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  7185. // int stockUom = sqlite3_column_int(stmt, 8);
  7186. // int _id = sqlite3_column_int(stmt, 9);
  7187. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  7188. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  7189. double weight=[bsubtotaljson[@"weight"] doubleValue];
  7190. int carton=[bsubtotaljson[@"carton"] intValue];
  7191. TotalCuft += cuft;
  7192. TotalWeight += weight;
  7193. TotalCarton += carton;
  7194. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:params[@"user"]];
  7195. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  7196. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  7197. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  7198. itemjson[@"note"]=nsline_note;
  7199. itemjson[@"origin_price"] = Price;
  7200. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  7201. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  7202. itemjson[@"order_item_status"] = @""; // 暂时不处理
  7203. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  7204. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  7205. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  7206. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7207. if(itemjson[@"combine"] != nil)
  7208. {
  7209. // int citem=0;
  7210. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  7211. for(int bc=0;bc<bcount;bc++)
  7212. {
  7213. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7214. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7215. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7216. subTotal += uprice * modulus * item_count;
  7217. }
  7218. }
  7219. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  7220. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  7221. // ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  7222. // ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  7223. itemjson[@"type"] = @"order_item";
  7224. model_data[[NSString stringWithFormat:@"item_%ld",(*count)++]] = itemjson;
  7225. model_data[@"count"] = [NSNumber numberWithInteger:*count];
  7226. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  7227. allItemPrice += subTotal;
  7228. }];
  7229. section_3[@"data"] = model_data;
  7230. section_3[@"type"] = @"sub_order";
  7231. section_3[@"title"] = @"Models";
  7232. section_3[@"switch"] = @(false);
  7233. ret[@"section_3"] = section_3;
  7234. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  7235. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  7236. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  7237. // payments/Credits
  7238. // payments_and_credist = sqlite3_column_double(statement, 34);
  7239. payments_and_credist = allItemPrice;
  7240. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  7241. NSDictionary *sub_total_item = @{
  7242. @"title":@"Sub-Total",
  7243. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  7244. };
  7245. [price_data setObject:sub_total_item forKey:@"item_0"];
  7246. // // total
  7247. // totalPrice = sqlite3_column_double(statement, 35);
  7248. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  7249. } else {
  7250. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  7251. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  7252. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  7253. // payments/Credits
  7254. payments_and_credist = 0;
  7255. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  7256. NSDictionary *sub_total_item = @{
  7257. @"title":@"Sub-Total",
  7258. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  7259. };
  7260. [price_data setObject:sub_total_item forKey:@"item_0"];
  7261. }
  7262. // total
  7263. totalPrice = payments_and_credist + lift_gate + shippingFee + handlingFee;
  7264. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  7265. if(discount>0)
  7266. {
  7267. NSDictionary *total_item = @{
  7268. @"title":@"Total",
  7269. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist*(1-(discount/100.0))+ lift_gate + shippingFee + handlingFee]
  7270. };
  7271. NSDictionary *discount_item = @{
  7272. @"title":@"Order Discount",
  7273. @"value":[NSString stringWithFormat:@"%d%% off",(int)discount]
  7274. };
  7275. [price_data setObject:discount_item forKey:@"item_4"];
  7276. [price_data setObject:total_item forKey:@"item_5"];
  7277. }
  7278. else
  7279. {
  7280. NSDictionary *total_item = @{
  7281. @"title":@"Total",
  7282. @"value":[NSString stringWithFormat:@"$%.2f",totalPrice]
  7283. };
  7284. [price_data setObject:total_item forKey:@"item_4"];
  7285. }
  7286. [price_data setObject:@(price_data.allKeys.count) forKey:@"count"];
  7287. section_4 = @{
  7288. @"data":price_data,
  7289. @"title":@"Price Info",
  7290. @"type":@"price_info"
  7291. }.mutableCopy;
  7292. ret[@"section_4"] = section_4;
  7293. // ret[@"order_info"]= orderinfo;
  7294. section_0 = @{
  7295. @"data":orderinfo,
  7296. @"title":@"Order Info",
  7297. @"type":@"order_info"
  7298. }.mutableCopy;
  7299. ret[@"section_0"] = section_0;
  7300. // ret[@"more_order_info"] = moreInfo;
  7301. section_2 = @{
  7302. @"data":moreInfo,
  7303. @"title":@"More Info",
  7304. @"type":@"more_order_info"
  7305. }.mutableCopy;
  7306. ret[@"section_2"] = section_2;
  7307. ret[@"count"] = @(5);
  7308. return [RAConvertor dict2data:ret];
  7309. }
  7310. #pragma mark order list
  7311. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  7312. NSString *nsstatus = @"";
  7313. switch (status) {
  7314. case 0:
  7315. {
  7316. nsstatus=@"Temp Order";
  7317. break;
  7318. }
  7319. case 1:
  7320. {
  7321. nsstatus=@"Saved Order";
  7322. break;
  7323. }
  7324. case 2: {
  7325. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  7326. break;
  7327. }
  7328. case 3:
  7329. case 15:
  7330. {
  7331. nsstatus=@"Cancelled";
  7332. break;
  7333. }
  7334. case 10:
  7335. {
  7336. nsstatus=@"Quote Submitted";
  7337. break;
  7338. }
  7339. case 11:
  7340. {
  7341. nsstatus=@"Sales Order Submitted";
  7342. break;
  7343. }
  7344. case 12:
  7345. {
  7346. nsstatus=@"Processing";
  7347. break;
  7348. }
  7349. case 13:
  7350. {
  7351. nsstatus=@"Shipped";
  7352. break;
  7353. }
  7354. case 14:
  7355. {
  7356. nsstatus=@"Closed";
  7357. break;
  7358. }
  7359. case -11:
  7360. {
  7361. nsstatus = @"Ready For Submit";
  7362. break;
  7363. }
  7364. default:
  7365. break;
  7366. }
  7367. return nsstatus;
  7368. }
  7369. + (double) orderTotalPrice:(NSString *)so_id user:(NSString*) user db:(sqlite3 *)db1 close:(BOOL)close
  7370. {
  7371. double total = 0;
  7372. __block double payments_and_credist = 0;
  7373. __block double allItemPrice = 0;
  7374. // sqlite3 *db1 = [iSalesDB get_db];
  7375. if (so_id) {
  7376. // 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];
  7377. 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];
  7378. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7379. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7380. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  7381. int product_id = sqlite3_column_int(stmt, 0);
  7382. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  7383. int discount = sqlite3_column_int(stmt, 2);
  7384. int item_count = sqlite3_column_int(stmt, 3);
  7385. // int item_id = sqlite3_column_int(stmt, 7);
  7386. int item_id = sqlite3_column_int(stmt, 4);
  7387. char *customer_cid = (char *)sqlite3_column_text(stmt, 5);
  7388. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  7389. NSString* Price=nil;
  7390. if(str_price==nil)
  7391. {
  7392. NSNumber* price = [self get_model_default_price:customerID user:user product_id:nil item_id:@(item_id) db:db1];
  7393. if(price==nil)
  7394. Price=@"No Price.";
  7395. else
  7396. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7397. }
  7398. else
  7399. {
  7400. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  7401. }
  7402. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:user];
  7403. itemjson[@"The unit price"]=Price;
  7404. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  7405. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  7406. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7407. if(itemjson[@"combine"] != nil)
  7408. {
  7409. // int citem=0;
  7410. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  7411. for(int bc=0;bc<bcount;bc++)
  7412. {
  7413. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7414. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7415. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7416. subTotal += uprice * modulus * item_count;
  7417. }
  7418. }
  7419. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  7420. allItemPrice += subTotal;
  7421. }];
  7422. payments_and_credist = allItemPrice;
  7423. } else {
  7424. // payments/Credits
  7425. payments_and_credist = 0;
  7426. }
  7427. // lift_gate handlingFee shippingFee
  7428. __block double lift_gate = 0;
  7429. __block double handlingFee = 0;
  7430. __block double shippingFee = 0;
  7431. __block double order_discount = 0;
  7432. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping,discount from offline_order where so_id = '%@';",so_id];
  7433. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7434. int have_lift_gate = sqlite3_column_int(stmt, 0);
  7435. if (have_lift_gate) {
  7436. lift_gate = sqlite3_column_double(stmt, 1);
  7437. }
  7438. handlingFee = sqlite3_column_double(stmt, 2);
  7439. shippingFee = sqlite3_column_double(stmt, 3);
  7440. order_discount = sqlite3_column_double(stmt, 4);
  7441. }];
  7442. if(order_discount>0)
  7443. payments_and_credist= payments_and_credist*(1-order_discount/100.0);
  7444. // total
  7445. total = payments_and_credist + lift_gate + shippingFee + handlingFee;
  7446. if (close) {
  7447. [iSalesDB close_db:db1];
  7448. }
  7449. return total;
  7450. }
  7451. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  7452. {
  7453. assert(params[@"user"]!=nil);
  7454. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  7455. int limit = [[params valueForKey:@"limit"] intValue];
  7456. int offset = [[params valueForKey:@"offset"] intValue];
  7457. NSString* keyword = [params valueForKey:@"keyWord"];
  7458. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  7459. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  7460. NSString* where=@"1 = 1";
  7461. if(keyword.length>0)
  7462. 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]];
  7463. if (orderStatus.length > 0) {
  7464. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  7465. if (order_status_array.count == 1) {
  7466. int status_value = [[order_status_array firstObject] intValue];
  7467. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  7468. if (status_value == 15 || status_value == 3) {
  7469. where = [where stringByAppendingString:[NSString stringWithFormat:@" and (o.status = 3 or o.erpOrderStatus = 15)"]];
  7470. } else {
  7471. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  7472. }
  7473. } else {
  7474. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  7475. }
  7476. } else if (order_status_array.count > 1) {
  7477. for (int i = 0; i < order_status_array.count;i++) {
  7478. NSString *status = order_status_array[i];
  7479. NSString *condition = @" or";
  7480. if (i == 0) {
  7481. condition = @" and (";
  7482. }
  7483. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  7484. int status_value = [status intValue];
  7485. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  7486. if (status_value == 15 || status_value == 3) {
  7487. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ (o.status = 3 or o.erpOrderStatus = 15)",condition]];
  7488. } else {
  7489. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  7490. }
  7491. } else {
  7492. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  7493. }
  7494. }
  7495. where = [where stringByAppendingString:@" )"];
  7496. }
  7497. }
  7498. 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];
  7499. // DebugLog(@"order list sql: %@",sqlQuery);
  7500. sqlite3 *db = [iSalesDB get_db];
  7501. sqlite3_stmt * statement;
  7502. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  7503. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  7504. {
  7505. int count=0;
  7506. while (sqlite3_step(statement) == SQLITE_ROW)
  7507. {
  7508. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  7509. int order_id = sqlite3_column_double(statement, 0);
  7510. char *soid = (char*)sqlite3_column_text(statement, 1);
  7511. if(soid==nil)
  7512. soid= "";
  7513. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  7514. int status = sqlite3_column_double(statement, 2);
  7515. int erpStatus = sqlite3_column_double(statement, 9);
  7516. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  7517. if(sales_rep==nil)
  7518. sales_rep= "";
  7519. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  7520. char *create_by = (char*)sqlite3_column_text(statement, 4);
  7521. if(create_by==nil)
  7522. create_by= "";
  7523. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  7524. char *company_name = (char*)sqlite3_column_text(statement, 5);
  7525. if(company_name==nil)
  7526. company_name= "";
  7527. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  7528. char *create_time = (char*)sqlite3_column_text(statement, 6);
  7529. if(create_time==nil)
  7530. create_time= "";
  7531. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  7532. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  7533. // double total_price = sqlite3_column_double(statement, 7);
  7534. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] user:params[@"user"] db:db close:NO];
  7535. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  7536. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  7537. int offline_edit = sqlite3_column_int(statement, 10);
  7538. // ": "JH",
  7539. // "": "$8307.00",
  7540. // "": "MOB1608050001",
  7541. // "": "ArpithaT",
  7542. // "": "1st Stage Property Transformations",
  7543. // "": "JANICE SUTTON",
  7544. // "": 2255,
  7545. // "": "08/02/2016 09:49:18",
  7546. // "": 1,
  7547. // "": "Saved Order"
  7548. // "": "1470384050483",
  7549. // "model_count": "6 / 28"
  7550. item[@"sales_rep"]= nssales_rep;
  7551. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  7552. item[@"so#"]= nssoid;
  7553. item[@"create_by"]= nscreate_by;
  7554. item[@"customer_name"]= nscompany_name;
  7555. item[@"customer_contact"] = customer_contact;
  7556. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  7557. item[@"purchase_time"]= nscreate_time;
  7558. int statusCode = status;
  7559. if (statusCode == 2) {
  7560. statusCode = erpStatus;
  7561. } else if (statusCode == 3) {
  7562. statusCode = 15;
  7563. }
  7564. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",statusCode];
  7565. item[@"order_status"]= nsstatus;
  7566. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  7567. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  7568. // item[@"model_count"]
  7569. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  7570. count++;
  7571. }
  7572. ret[@"count"]= [NSNumber numberWithInt:count];
  7573. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  7574. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  7575. ret[@"result"]= [NSNumber numberWithInt:2];
  7576. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  7577. ret[@"time_zone"] = @"PST";
  7578. sqlite3_finalize(statement);
  7579. }
  7580. 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];
  7581. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  7582. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7583. [iSalesDB close_db:db];
  7584. return [RAConvertor dict2data:ret];
  7585. }
  7586. #pragma mark update gnotes
  7587. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  7588. {
  7589. DebugLog(@"params: %@",params);
  7590. // comments = Meyoyoyoyoyoyoy;
  7591. // orderCode = MOB1608110001;
  7592. // password = 123456;
  7593. // user = EvanK;
  7594. 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]];
  7595. int ret = [iSalesDB execSql:sql];
  7596. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7597. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7598. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7599. // [dic setValue:@"160409" forKey:@"min_ver"];
  7600. return [RAConvertor dict2data:dic];
  7601. }
  7602. #pragma mark move to wishlist
  7603. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  7604. {
  7605. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  7606. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7607. _id = [NSString stringWithFormat:@"(%@)",_id];
  7608. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  7609. sqlite3 *db = [iSalesDB get_db];
  7610. __block NSString *product_id = @"";
  7611. __block NSString *item_count_str = @"";
  7612. // __block NSString *item_id = nil;
  7613. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7614. // product_id = [self textAtColumn:0 statement:stmt];
  7615. int item_count = sqlite3_column_int(stmt, 1);
  7616. // item_id = [self textAtColumn:2 statement:stmt];
  7617. NSString *p_id = [self textAtColumn:0 statement:stmt];
  7618. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  7619. if (p_id.length) {
  7620. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  7621. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  7622. }
  7623. }];
  7624. [iSalesDB close_db:db];
  7625. // 去除第一个,
  7626. if (product_id.length > 1) {
  7627. product_id = [product_id substringFromIndex:1];
  7628. }
  7629. if (item_count_str.length > 1) {
  7630. item_count_str = [item_count_str substringFromIndex:1];
  7631. }
  7632. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7633. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  7634. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  7635. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  7636. sqlite3 *db1 = [iSalesDB get_db];
  7637. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  7638. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7639. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  7640. // 删除
  7641. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  7642. int ret = [iSalesDB execSql:deleteSql db:db1];
  7643. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7644. [iSalesDB close_db:db1];
  7645. return [RAConvertor dict2data:dic];
  7646. }
  7647. #pragma mark cart delete
  7648. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  7649. {
  7650. // cartItemId = 548;
  7651. // orderCode = MOB1608110001;
  7652. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  7653. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7654. _id = [NSString stringWithFormat:@"(%@)",_id];
  7655. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  7656. int ret = [iSalesDB execSql:sql];
  7657. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7658. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7659. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7660. // [dic setValue:@"160409" forKey:@"min_ver"];
  7661. return [RAConvertor dict2data:dic];
  7662. }
  7663. #pragma mark set order discount
  7664. //+(NSDictionary*) offline_cartsetallprice :(NSMutableDictionary *) params
  7665. //{
  7666. // DebugLog(@"cart set price params: %@",params);
  7667. // // "cartitem_id" = 1;
  7668. // // discount = "0.000000";
  7669. // // "item_note" = "";
  7670. // // price = "269.000000";
  7671. // NSString *order_code = [self valueInParams:params key:@"orderCode"];
  7672. // //NSString *notes = [self valueInParams:params key:@"item_note"];
  7673. // NSString *discount = [self valueInParams:params key:@"app_discount"];
  7674. //// NSString *price = [self valueInParams:params key:@"price"];
  7675. //
  7676. // // bool badd_price_changed=false;
  7677. // // sqlite3* db=[iSalesDB get_db];
  7678. // // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7679. // // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7680. // // NSRange range;
  7681. // // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7682. // //
  7683. // // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7684. // // badd_price_changed=true;
  7685. // // [iSalesDB close_db:db];
  7686. // //
  7687. // // if(badd_price_changed)
  7688. // // {
  7689. // // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7690. // // }
  7691. // //
  7692. //
  7693. //
  7694. //// price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7695. //
  7696. // NSString *sql = [NSString stringWithFormat:@"update offline_cart set discount = %f where so_no = '%@'",discount.doubleValue,order_code];
  7697. //
  7698. // int ret = [iSalesDB execSql:sql];
  7699. //
  7700. // NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7701. // [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7702. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  7703. // // [dic setValue:@"160409" forKey:@"min_ver"];
  7704. //
  7705. //// return [RAConvertor dict2data:dic];
  7706. // return dic;
  7707. //
  7708. //}
  7709. +(NSDictionary*) offline_setorderdiscount :(NSMutableDictionary *) params
  7710. {
  7711. DebugLog(@"order set discount params: %@",params);
  7712. // "cartitem_id" = 1;
  7713. // discount = "0.000000";
  7714. // "item_note" = "";
  7715. // price = "269.000000";
  7716. NSString *order_code = [self valueInParams:params key:@"orderCode"];
  7717. //NSString *notes = [self valueInParams:params key:@"item_note"];
  7718. NSString *discount = [self valueInParams:params key:@"app_discount"];
  7719. // NSString *price = [self valueInParams:params key:@"price"];
  7720. // bool badd_price_changed=false;
  7721. // sqlite3* db=[iSalesDB get_db];
  7722. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7723. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7724. // NSRange range;
  7725. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7726. //
  7727. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7728. // badd_price_changed=true;
  7729. // [iSalesDB close_db:db];
  7730. //
  7731. // if(badd_price_changed)
  7732. // {
  7733. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7734. // }
  7735. //
  7736. // sqlite3 *db = [iSalesDB get_db];
  7737. // double total_price = [self orderTotalPrice:params[@"orderCode"] user:params[@"user"] db:db close:YES];
  7738. // price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7739. //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];
  7740. NSString *sql = [NSString stringWithFormat:@"update offline_order set discount = %f where so_id = '%@'",discount.doubleValue,order_code];
  7741. int ret = [iSalesDB execSql:sql];
  7742. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7743. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7744. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7745. // [dic setValue:@"160409" forKey:@"min_ver"];
  7746. // return [RAConvertor dict2data:dic];
  7747. return dic;
  7748. }
  7749. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  7750. {
  7751. DebugLog(@"cart set price params: %@",params);
  7752. // "cartitem_id" = 1;
  7753. // discount = "0.000000";
  7754. // "item_note" = "";
  7755. // price = "269.000000";
  7756. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7757. NSString *notes = [self valueInParams:params key:@"item_note"];
  7758. NSString *discount = [self valueInParams:params key:@"discount"];
  7759. NSString *price = [self valueInParams:params key:@"price"];
  7760. // bool badd_price_changed=false;
  7761. // sqlite3* db=[iSalesDB get_db];
  7762. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7763. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  7764. // NSRange range;
  7765. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  7766. //
  7767. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  7768. // badd_price_changed=true;
  7769. // [iSalesDB close_db:db];
  7770. //
  7771. // if(badd_price_changed)
  7772. // {
  7773. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  7774. // }
  7775. //
  7776. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  7777. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  7778. int ret = [iSalesDB execSql:sql];
  7779. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7780. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7781. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7782. // [dic setValue:@"160409" forKey:@"min_ver"];
  7783. return [RAConvertor dict2data:dic];
  7784. }
  7785. #pragma mark set line notes
  7786. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  7787. {
  7788. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7789. NSString *notes = [self valueInParams:params key:@"notes"];
  7790. notes = [self translateSingleQuote:notes];
  7791. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  7792. int ret = [iSalesDB execSql:sql];
  7793. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7794. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7795. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7796. // [dic setValue:@"160409" forKey:@"min_ver"];
  7797. return [RAConvertor dict2data:dic];
  7798. }
  7799. #pragma mark set qty
  7800. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  7801. {
  7802. assert(params[@"can_create_backorder"]!=nil);
  7803. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7804. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  7805. int item_count = [params[@"inputInt"] intValue];
  7806. // 购买检查数量大于库存
  7807. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7808. if (!params[@"can_create_backorder"]) {
  7809. 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];
  7810. __block BOOL out_of_stock = NO;
  7811. [iSalesDB jk_query:checkSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7812. int availability = sqlite3_column_int(stmt, 0);
  7813. if (availability < item_count) {
  7814. out_of_stock = YES;
  7815. }
  7816. }];
  7817. if (out_of_stock) { // 缺货
  7818. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"result"];
  7819. [dic setObject:@"Item is out of stock" forKey:@"err_msg"];
  7820. return [RAConvertor dict2data:dic];
  7821. }
  7822. }
  7823. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  7824. int ret = [iSalesDB execSql:sql];
  7825. __block int item_id = 0;
  7826. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7827. item_id = sqlite3_column_int(stmt, 0);
  7828. }];
  7829. sqlite3 *db = [iSalesDB get_db];
  7830. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  7831. [iSalesDB close_db:db];
  7832. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7833. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7834. // [dic setValue:@"160409" forKey:@"min_ver"];
  7835. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  7836. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  7837. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  7838. return [RAConvertor dict2data:dic];
  7839. }
  7840. #pragma mark place order
  7841. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  7842. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7843. if (aname) {
  7844. [dic setValue:aname forKey:@"aname"];
  7845. }
  7846. if (control) {
  7847. [dic setValue:control forKey:@"control"];
  7848. }
  7849. if (keyboard) {
  7850. [dic setValue:keyboard forKey:@"keyboard"];
  7851. }
  7852. if (name) {
  7853. [dic setValue:name forKey:@"name"];
  7854. }
  7855. if (value) {
  7856. [dic setValue:value forKey:@"value"];
  7857. }
  7858. return dic;
  7859. }
  7860. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  7861. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7862. orderCode = [self translateSingleQuote:orderCode];
  7863. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  7864. 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];
  7865. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7866. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  7867. NSString *img0 = [self textAtColumn:1 statement:stmt];
  7868. NSString *img1 = [self textAtColumn:2 statement:stmt];
  7869. NSString *img2 = [self textAtColumn:3 statement:stmt];
  7870. NSString *company_name = [self textAtColumn:4 statement:stmt];
  7871. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  7872. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  7873. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  7874. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  7875. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  7876. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  7877. NSString *email = [self textAtColumn:11 statement:stmt];
  7878. NSString *phone = [self textAtColumn:12 statement:stmt];
  7879. NSString *fax = [self textAtColumn:13 statement:stmt];
  7880. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  7881. NSString *city = [self textAtColumn:15 statement:stmt];
  7882. NSString *state = [self textAtColumn:16 statement:stmt];
  7883. NSString *country = [self textAtColumn:17 statement:stmt];
  7884. NSString *name = [self textAtColumn:18 statement:stmt];
  7885. // contact id
  7886. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  7887. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  7888. [contact_id_dic setValue:@"text" forKey:@"control"];
  7889. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  7890. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  7891. [contact_id_dic setValue:@"true" forKey:@"required"];
  7892. [contact_id_dic setValue:contact_id forKey:@"value"];
  7893. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  7894. // business card
  7895. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  7896. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  7897. [business_card_dic setValue:@"img" forKey:@"control"];
  7898. [business_card_dic setValue:@"1" forKey:@"disable"];
  7899. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  7900. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  7901. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  7902. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  7903. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  7904. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  7905. [business_card_dic setValue:@"business_card" forKey:@"name"];
  7906. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  7907. // fax
  7908. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7909. [customer_dic setValue:fax_dic forKey:@"item_10"];
  7910. // zipcode
  7911. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  7912. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  7913. // city
  7914. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  7915. [customer_dic setValue:city_dic forKey:@"item_12"];
  7916. // state
  7917. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  7918. [customer_dic setValue:state_dic forKey:@"item_13"];
  7919. // country
  7920. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  7921. [customer_dic setValue:country_dic forKey:@"item_14"];
  7922. // company name
  7923. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  7924. [customer_dic setValue:company_dic forKey:@"item_2"];
  7925. // addr_1
  7926. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  7927. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  7928. // addr_2
  7929. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  7930. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  7931. // addr_3
  7932. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  7933. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  7934. // addr_4
  7935. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  7936. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  7937. // Contact
  7938. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  7939. [customer_dic setValue:contact_dic forKey:@"item_7"];
  7940. // email
  7941. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7942. [customer_dic setValue:email_dic forKey:@"item_8"];
  7943. // phone
  7944. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7945. [customer_dic setValue:phone_dic forKey:@"item_9"];
  7946. // title
  7947. [customer_dic setValue:@"Customer" forKey:@"title"];
  7948. // count
  7949. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  7950. }];
  7951. // setting
  7952. NSDictionary *setting = params[@"setting"];
  7953. NSNumber *hide = setting[@"CustomerHide"];
  7954. [customer_dic setValue:hide forKey:@"hide"];
  7955. return customer_dic;
  7956. }
  7957. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7958. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7959. // setting
  7960. NSDictionary *setting = params[@"setting"];
  7961. NSNumber *hide = setting[@"ShipToHide"];
  7962. [dic setValue:hide forKey:@"hide"];
  7963. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7964. orderCode = [self translateSingleQuote:orderCode];
  7965. 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];
  7966. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7967. NSString *cid = [self textAtColumn:0 statement:stmt];
  7968. NSString *name = [self textAtColumn:1 statement:stmt];
  7969. NSString *ext = [self textAtColumn:2 statement:stmt];
  7970. NSString *contact = [self textAtColumn:3 statement:stmt];
  7971. NSString *email = [self textAtColumn:4 statement:stmt];
  7972. NSString *fax = [self textAtColumn:5 statement:stmt];
  7973. NSString *phone = [self textAtColumn:6 statement:stmt];
  7974. // count
  7975. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7976. // title
  7977. [dic setValue:@"Ship To" forKey:@"title"];
  7978. // choose
  7979. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7980. [choose_dic setValue:@"choose" forKey:@"aname"];
  7981. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7982. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  7983. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7984. @"key_map" : @{@"receive_cid" : @"customer_cid",
  7985. @"receive_contact" : @"customer_contact",
  7986. @"receive_email" : @"customer_email",
  7987. @"receive_ext" : @"customer_contact_ext",
  7988. @"receive_fax" : @"customer_fax",
  7989. @"receive_name" : @"customer_name",
  7990. @"receive_phone" : @"customer_phone"},
  7991. @"refresh" : [NSNumber numberWithInteger:1],
  7992. @"type" : @"pull"};
  7993. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  7994. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  7995. @"name" : @"Add new address",
  7996. @"refresh" : [NSNumber numberWithInteger:1],
  7997. @"value" : @"new_addr"
  7998. };
  7999. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  8000. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  8001. @"key_map" : @{@"receive_cid" : @"customer_cid",
  8002. @"receive_contact" : @"customer_contact",
  8003. @"receive_email" : @"customer_email",
  8004. @"receive_ext" : @"customer_contact_ext",
  8005. @"receive_fax" : @"customer_fax",
  8006. @"receive_name" : @"customer_name",
  8007. @"receive_phone" : @"customer_phone"},
  8008. @"name" : @"select_ship_to",
  8009. @"refresh" : [NSNumber numberWithInteger:1],
  8010. @"value" : @"Sales_Order_Ship_To"};
  8011. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  8012. [dic setValue:choose_dic forKey:@"item_0"];
  8013. // contact id
  8014. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8015. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8016. [contact_id_dic setValue:@"true" forKey:@"required"];
  8017. [dic setValue:contact_id_dic forKey:@"item_1"];
  8018. // company name
  8019. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8020. [dic setValue:company_name_dic forKey:@"item_2"];
  8021. // address
  8022. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8023. NSNumber *required = setting[@"ShippingToAddressRequire"];
  8024. if ([required integerValue]) {
  8025. [ext_dic setValue:@"true" forKey:@"required"];
  8026. }
  8027. [dic setValue:ext_dic forKey:@"item_3"];
  8028. // contact
  8029. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8030. [dic setValue:contact_dic forKey:@"item_4"];
  8031. // phone
  8032. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8033. [dic setValue:phone_dic forKey:@"item_5"];
  8034. // fax
  8035. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8036. [dic setValue:fax_dic forKey:@"item_6"];
  8037. // email
  8038. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8039. [dic setValue:email_dic forKey:@"item_7"];
  8040. }];
  8041. return dic;
  8042. }
  8043. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  8044. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8045. // setting
  8046. NSDictionary *setting = params[@"setting"];
  8047. NSNumber *hide = setting[@"ShipFromHide"];
  8048. [dic setValue:hide forKey:@"hide"];
  8049. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8050. orderCode = [self translateSingleQuote:orderCode];
  8051. 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];
  8052. 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';";
  8053. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8054. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  8055. __block NSString *name = [self textAtColumn:1 statement:stmt];
  8056. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  8057. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  8058. __block NSString *email = [self textAtColumn:4 statement:stmt];
  8059. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  8060. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  8061. if (!cid.length) {
  8062. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  8063. cid = [self textAtColumn:0 statement:statment];
  8064. name = [self textAtColumn:1 statement:statment];
  8065. ext = [self textAtColumn:2 statement:statment];
  8066. contact = [self textAtColumn:3 statement:statment];
  8067. email = [self textAtColumn:4 statement:statment];
  8068. fax = [self textAtColumn:5 statement:statment];
  8069. phone = [self textAtColumn:6 statement:statment];
  8070. }];
  8071. }
  8072. // count
  8073. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8074. // title
  8075. [dic setValue:@"Ship From" forKey:@"title"];
  8076. // hide
  8077. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8078. // choose
  8079. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8080. [choose_dic setValue:@"choose" forKey:@"aname"];
  8081. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8082. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8083. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  8084. @"key_map" : @{@"sender_cid" : @"customer_cid",
  8085. @"sender_contact" : @"customer_contact",
  8086. @"sender_email" : @"customer_email",
  8087. @"sender_ext" : @"customer_contact_ext",
  8088. @"sender_fax" : @"customer_fax",
  8089. @"sender_name" : @"customer_name",
  8090. @"sender_phone" : @"customer_phone"},
  8091. @"name" : @"select_cid",
  8092. @"refresh" : [NSNumber numberWithInteger:0],
  8093. @"value" : @"Sales_Order_Ship_From"};
  8094. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  8095. [dic setValue:choose_dic forKey:@"item_0"];
  8096. // contact id
  8097. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8098. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8099. [contact_id_dic setValue:@"true" forKey:@"required"];
  8100. [dic setValue:contact_id_dic forKey:@"item_1"];
  8101. // company name
  8102. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8103. [dic setValue:company_name_dic forKey:@"item_2"];
  8104. // address
  8105. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  8106. [dic setValue:ext_dic forKey:@"item_3"];
  8107. // contact
  8108. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8109. [dic setValue:contact_dic forKey:@"item_4"];
  8110. // phone
  8111. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8112. [dic setValue:phone_dic forKey:@"item_5"];
  8113. // fax
  8114. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8115. [dic setValue:fax_dic forKey:@"item_6"];
  8116. // email
  8117. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8118. [dic setValue:email_dic forKey:@"item_7"];
  8119. }];
  8120. return dic;
  8121. }
  8122. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  8123. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8124. // setting
  8125. NSDictionary *setting = params[@"setting"];
  8126. NSNumber *hide = setting[@"FreightBillToHide"];
  8127. [dic setValue:hide forKey:@"hide"];
  8128. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8129. orderCode = [self translateSingleQuote:orderCode];
  8130. 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];
  8131. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8132. NSString *cid = [self textAtColumn:0 statement:stmt];
  8133. NSString *name = [self textAtColumn:1 statement:stmt];
  8134. NSString *ext = [self textAtColumn:2 statement:stmt];
  8135. NSString *contact = [self textAtColumn:3 statement:stmt];
  8136. NSString *email = [self textAtColumn:4 statement:stmt];
  8137. NSString *fax = [self textAtColumn:5 statement:stmt];
  8138. NSString *phone = [self textAtColumn:6 statement:stmt];
  8139. // count
  8140. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8141. // title
  8142. [dic setValue:@"Freight Bill To" forKey:@"title"];
  8143. // hide
  8144. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8145. // choose
  8146. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8147. [choose_dic setValue:@"choose" forKey:@"aname"];
  8148. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8149. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  8150. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  8151. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  8152. @"shipping_billto_contact" : @"receive_contact",
  8153. @"shipping_billto_email" : @"receive_email",
  8154. @"shipping_billto_ext" : @"receive_ext",
  8155. @"shipping_billto_fax" : @"receive_fax",
  8156. @"shipping_billto_name" : @"receive_name",
  8157. @"shipping_billto_phone" : @"receive_phone"},
  8158. @"type" : @"pull"};
  8159. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  8160. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  8161. @"type" : @"pull",
  8162. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  8163. @"shipping_billto_contact" : @"customer_contact",
  8164. @"shipping_billto_email" : @"customer_email",
  8165. @"shipping_billto_ext" : @"customer_contact_ext",
  8166. @"shipping_billto_fax" : @"customer_fax",
  8167. @"shipping_billto_name" : @"customer_name",
  8168. @"shipping_billto_phone" : @"customer_phone"}
  8169. };
  8170. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  8171. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  8172. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  8173. @"shipping_billto_contact" : @"sender_contact",
  8174. @"shipping_billto_email" : @"sender_email",
  8175. @"shipping_billto_ext" : @"sender_ext",
  8176. @"shipping_billto_fax" : @"sender_fax",
  8177. @"shipping_billto_name" : @"sender_name",
  8178. @"shipping_billto_phone" : @"sender_phone"},
  8179. @"type" : @"pull"
  8180. };
  8181. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  8182. NSDictionary *select_freight_bill_to_dic = @{
  8183. @"aname" : @"Select freight bill to",
  8184. @"name" : @"select_cid",
  8185. @"refresh" : [NSNumber numberWithInteger:0],
  8186. @"value" : @"Sales_Order_Freight_Bill_To",
  8187. @"key_map" : @{
  8188. @"shipping_billto_cid" : @"customer_cid",
  8189. @"shipping_billto_contact" : @"customer_contact",
  8190. @"shipping_billto_email" : @"customer_email",
  8191. @"shipping_billto_ext" : @"customer_contact_ext",
  8192. @"shipping_billto_fax" : @"customer_fax",
  8193. @"shipping_billto_name" : @"customer_name",
  8194. @"shipping_billto_phone" : @"customer_phone"
  8195. }
  8196. };
  8197. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  8198. [dic setValue:choose_dic forKey:@"item_0"];
  8199. // contact id
  8200. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8201. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8202. [contact_id_dic setValue:@"true" forKey:@"required"];
  8203. [dic setValue:contact_id_dic forKey:@"item_1"];
  8204. // company name
  8205. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8206. [dic setValue:company_name_dic forKey:@"item_2"];
  8207. // address
  8208. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8209. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  8210. if ([ext_required integerValue]) {
  8211. [ext_dic setValue:@"true" forKey:@"required"];
  8212. }
  8213. [dic setValue:ext_dic forKey:@"item_3"];
  8214. // contact
  8215. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8216. [dic setValue:contact_dic forKey:@"item_4"];
  8217. // phone
  8218. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8219. [dic setValue:phone_dic forKey:@"item_5"];
  8220. // fax
  8221. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8222. [dic setValue:fax_dic forKey:@"item_6"];
  8223. // email
  8224. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8225. [dic setValue:email_dic forKey:@"item_7"];
  8226. }];
  8227. return dic;
  8228. }
  8229. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  8230. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8231. // setting
  8232. NSDictionary *setting = params[@"setting"];
  8233. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  8234. [dic setValue:hide forKey:@"hide"];
  8235. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8236. orderCode = [self translateSingleQuote:orderCode];
  8237. 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];
  8238. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8239. NSString *cid = [self textAtColumn:0 statement:stmt];
  8240. NSString *name = [self textAtColumn:1 statement:stmt];
  8241. NSString *ext = [self textAtColumn:2 statement:stmt];
  8242. NSString *contact = [self textAtColumn:3 statement:stmt];
  8243. NSString *email = [self textAtColumn:4 statement:stmt];
  8244. NSString *fax = [self textAtColumn:5 statement:stmt];
  8245. NSString *phone = [self textAtColumn:6 statement:stmt];
  8246. // count
  8247. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8248. // title
  8249. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  8250. // hide
  8251. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8252. // choose
  8253. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8254. [choose_dic setValue:@"choose" forKey:@"aname"];
  8255. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8256. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8257. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  8258. @"key_map" : @{@"billing_cid" : @"receive_cid",
  8259. @"billing_contact" : @"receive_contact",
  8260. @"billing_email" : @"receive_email",
  8261. @"billing_ext" : @"receive_ext",
  8262. @"billing_fax" : @"receive_fax",
  8263. @"billing_name" : @"receive_name",
  8264. @"billing_phone" : @"receive_phone"},
  8265. @"type" : @"pull"};
  8266. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  8267. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  8268. @"type" : @"pull",
  8269. @"key_map" : @{@"billing_cid" : @"customer_cid",
  8270. @"billing_contact" : @"customer_contact",
  8271. @"billing_email" : @"customer_email",
  8272. @"billing_ext" : @"customer_contact_ext",
  8273. @"billing_fax" : @"customer_fax",
  8274. @"billing_name" : @"customer_name",
  8275. @"billing_phone" : @"customer_phone"}
  8276. };
  8277. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  8278. NSDictionary *select_bill_to_dic = @{
  8279. @"aname" : @"Select bill to",
  8280. @"name" : @"select_cid",
  8281. @"refresh" : [NSNumber numberWithInteger:0],
  8282. @"value" : @"Sales_Order_Merchandise_Bill_To",
  8283. @"key_map" : @{
  8284. @"billing_cid" : @"customer_cid",
  8285. @"billing_contact" : @"customer_contact",
  8286. @"billing_email" : @"customer_email",
  8287. @"billing_ext" : @"customer_contact_ext",
  8288. @"billing_fax" : @"customer_fax",
  8289. @"billing_name" : @"customer_name",
  8290. @"billing_phone" : @"customer_phone"
  8291. }
  8292. };
  8293. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  8294. [dic setValue:choose_dic forKey:@"item_0"];
  8295. // contact id
  8296. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8297. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8298. [contact_id_dic setValue:@"true" forKey:@"required"];
  8299. [dic setValue:contact_id_dic forKey:@"item_1"];
  8300. // company name
  8301. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8302. [dic setValue:company_name_dic forKey:@"item_2"];
  8303. // address
  8304. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  8305. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  8306. if ([ext_required integerValue]) {
  8307. [ext_dic setValue:@"true" forKey:@"required"];
  8308. }
  8309. [dic setValue:ext_dic forKey:@"item_3"];
  8310. // contact
  8311. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8312. [dic setValue:contact_dic forKey:@"item_4"];
  8313. // phone
  8314. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8315. [dic setValue:phone_dic forKey:@"item_5"];
  8316. // fax
  8317. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8318. [dic setValue:fax_dic forKey:@"item_6"];
  8319. // email
  8320. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8321. [dic setValue:email_dic forKey:@"item_7"];
  8322. }];
  8323. return dic;
  8324. }
  8325. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  8326. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8327. // setting
  8328. NSDictionary *setting = params[@"setting"];
  8329. NSNumber *hide = setting[@"ReturnToHide"];
  8330. [dic setValue:hide forKey:@"hide"];
  8331. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8332. orderCode = [self translateSingleQuote:orderCode];
  8333. 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];
  8334. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8335. NSString *cid = [self textAtColumn:0 statement:stmt];
  8336. NSString *name = [self textAtColumn:1 statement:stmt];
  8337. NSString *ext = [self textAtColumn:2 statement:stmt];
  8338. NSString *contact = [self textAtColumn:3 statement:stmt];
  8339. NSString *email = [self textAtColumn:4 statement:stmt];
  8340. NSString *fax = [self textAtColumn:5 statement:stmt];
  8341. NSString *phone = [self textAtColumn:6 statement:stmt];
  8342. // count
  8343. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  8344. // title
  8345. [dic setValue:@"Return To" forKey:@"title"];
  8346. // hide
  8347. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  8348. // choose
  8349. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  8350. [choose_dic setValue:@"choose" forKey:@"aname"];
  8351. [choose_dic setValue:@"multi_action" forKey:@"control"];
  8352. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  8353. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  8354. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  8355. @"returnto_contact" : @"sender_contact",
  8356. @"returnto_email" : @"sender_email",
  8357. @"returnto_ext" : @"sender_ext",
  8358. @"returnto_fax" : @"sender_fax",
  8359. @"returnto_name" : @"sender_name",
  8360. @"returnto_phone" : @"sender_phone"},
  8361. @"type" : @"pull"};
  8362. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  8363. NSDictionary *select_return_to_dic = @{
  8364. @"aname" : @"Select return to",
  8365. @"name" : @"select_cid",
  8366. @"refresh" : [NSNumber numberWithInteger:0],
  8367. @"value" : @"Contact_Return_To",
  8368. @"key_map" : @{
  8369. @"returnto_cid" : @"customer_cid",
  8370. @"returnto_contact" : @"customer_contact",
  8371. @"returnto_email" : @"customer_email",
  8372. @"returnto_ext" : @"customer_contact_ext",
  8373. @"returnto_fax" : @"customer_fax",
  8374. @"returnto_name" : @"customer_name",
  8375. @"returnto_phone" : @"customer_phone"
  8376. }
  8377. };
  8378. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  8379. [dic setValue:choose_dic forKey:@"item_0"];
  8380. // contact id
  8381. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8382. [contact_id_dic setValue:@"1" forKey:@"disable"];
  8383. [contact_id_dic setValue:@"true" forKey:@"required"];
  8384. [dic setValue:contact_id_dic forKey:@"item_1"];
  8385. // company name
  8386. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8387. [dic setValue:company_name_dic forKey:@"item_2"];
  8388. // address
  8389. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  8390. [dic setValue:ext_dic forKey:@"item_3"];
  8391. // contact
  8392. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8393. [dic setValue:contact_dic forKey:@"item_4"];
  8394. // phone
  8395. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8396. [dic setValue:phone_dic forKey:@"item_5"];
  8397. // fax
  8398. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8399. [dic setValue:fax_dic forKey:@"item_6"];
  8400. // email
  8401. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8402. [dic setValue:email_dic forKey:@"item_7"];
  8403. }];
  8404. return dic;
  8405. }
  8406. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db
  8407. {
  8408. assert(params[@"user"]!=nil);
  8409. assert(params[@"contact_id"]!=nil);
  8410. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8411. orderCode = [self translateSingleQuote:orderCode];
  8412. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8413. __block double TotalCuft = 0;
  8414. __block double TotalWeight = 0;
  8415. __block int TotalCarton = 0;
  8416. __block double payments = 0;
  8417. // setting
  8418. NSDictionary *setting = params[@"setting"];
  8419. NSNumber *hide = setting[@"ModelInformationHide"];
  8420. [dic setValue:hide forKey:@"hide"];
  8421. 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];
  8422. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8423. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8424. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  8425. // item id
  8426. int item_id = sqlite3_column_int(stmt, 0);
  8427. // count
  8428. int item_count = sqlite3_column_int(stmt, 1);
  8429. // stockUom
  8430. int stockUom = sqlite3_column_int(stmt, 2);
  8431. // unit price
  8432. NSString *str_price = [self textAtColumn:3 statement:stmt];
  8433. NSString* Price=nil;
  8434. if([str_price isEqualToString:@""])
  8435. {
  8436. NSNumber* price = [self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db];
  8437. if(price==nil)
  8438. Price=@"No Price.";
  8439. else
  8440. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  8441. }
  8442. else
  8443. {
  8444. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  8445. }
  8446. // discount
  8447. double discount = sqlite3_column_double(stmt, 4);
  8448. // name
  8449. NSString *name = [self textAtColumn:5 statement:stmt];
  8450. // description
  8451. NSString *description = [self textAtColumn:6 statement:stmt];
  8452. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  8453. // line note
  8454. NSString *line_note = [self textAtColumn:7 statement:stmt];
  8455. int avaulability = sqlite3_column_int(stmt, 8);
  8456. // img
  8457. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  8458. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false contactID:params[@"contact_id"] user:params[@"user"]];
  8459. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  8460. if(combine != nil)
  8461. {
  8462. // int citem=0;
  8463. int bcount=[[combine valueForKey:@"count"] intValue];
  8464. for(int bc=0;bc<bcount;bc++)
  8465. {
  8466. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  8467. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  8468. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  8469. subTotal += uprice * modulus * item_count;
  8470. }
  8471. }
  8472. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  8473. [model_dic setValue:@"model" forKey:@"control"];
  8474. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  8475. [model_dic setValue:description forKey:@"description"];
  8476. [model_dic setValue:line_note forKey:@"note"];
  8477. [model_dic setValue:img forKey:@"img_url"];
  8478. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  8479. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  8480. [model_dic setValue:Price forKey:@"unit_price"];
  8481. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  8482. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  8483. if (combine) {
  8484. [model_dic setValue:combine forKey:@"combine"];
  8485. }
  8486. // well,what under the row is the info for total
  8487. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  8488. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  8489. double weight=[bsubtotaljson[@"weight"] doubleValue];
  8490. int carton=[bsubtotaljson[@"carton"] intValue];
  8491. int mpack=[bsubtotaljson[@"mpack"] intValue];
  8492. model_dic[@"mpack"]=[NSNumber numberWithInt:mpack];
  8493. TotalCuft += cuft;
  8494. TotalWeight += weight;
  8495. TotalCarton += carton;
  8496. payments += subTotal;
  8497. //---------------------------
  8498. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  8499. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  8500. }];
  8501. [dic setValue:@"Model Information" forKey:@"title"];
  8502. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  8503. return dic;
  8504. }
  8505. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  8506. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8507. [dic setValue:@"Remarks Content" forKey:@"title"];
  8508. // setting
  8509. NSDictionary *setting = params[@"setting"];
  8510. NSNumber *hide = setting[@"RemarksContentHide"];
  8511. [dic setValue:hide forKey:@"hide"];
  8512. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8513. orderCode = [self translateSingleQuote:orderCode];
  8514. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes,schedule_date from offline_order where so_id = '%@';",orderCode];
  8515. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8516. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  8517. int mustCall = sqlite3_column_int(stmt, 1);
  8518. // NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  8519. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  8520. #ifdef BUILD_CONTRAST
  8521. NSString *schedule_date = [self textAtColumn:4 statement:stmt];
  8522. #endif
  8523. NSDictionary *po_dic = @{
  8524. @"aname" : @"PO#",
  8525. @"control" : @"edit",
  8526. @"keyboard" : @"text",
  8527. @"name" : @"poNumber",
  8528. @"value" : poNumber
  8529. };
  8530. #ifdef BUILD_CONTRAST
  8531. NSString* nsdate = [self changeDateFormate:schedule_date]; // 与在线统一
  8532. NSDictionary *schedule_dict = @{
  8533. @"aname" : @"Schdule Date",
  8534. @"control" : @"datepicker",
  8535. @"type": @"date",
  8536. @"required": @"true",
  8537. @"name" : @"schedule_date_str",
  8538. @"value" : nsdate
  8539. };
  8540. #endif
  8541. NSDictionary *must_call_dic = @{
  8542. @"aname" : @"MUST CALL BEFORE DELIVERY",
  8543. @"control" : @"switch",
  8544. @"name" : @"must_call",
  8545. @"value" : mustCall ? @"true" : @"false"
  8546. };
  8547. NSDictionary *general_notes_dic = @{
  8548. @"aname" : @"General notes",
  8549. @"control" : @"text_view",
  8550. @"keyboard" : @"text",
  8551. @"name" : @"comments",
  8552. @"value" : generalNotes
  8553. };
  8554. // NSDictionary *internal_notes_dic = @{
  8555. // @"aname" : @"Internal notes",
  8556. // @"control" : @"text_view",
  8557. // @"keyboard" : @"text",
  8558. // @"name" : @"internal_notes",
  8559. // @"value" : internalNotes
  8560. // };
  8561. #ifdef BUILD_CONTRAST
  8562. [dic setValue:po_dic forKey:@"item_0"];
  8563. [dic setValue:schedule_dict forKey:@"item_1"];
  8564. [dic setValue:must_call_dic forKey:@"item_2"];
  8565. [dic setValue:general_notes_dic forKey:@"item_3"];
  8566. #else
  8567. [dic setValue:po_dic forKey:@"item_0"];
  8568. [dic setValue:must_call_dic forKey:@"item_1"];
  8569. [dic setValue:general_notes_dic forKey:@"item_2"];
  8570. #endif
  8571. // [dic setValue:internal_notes_dic forKey:@"item_3"];
  8572. }];
  8573. #ifdef BUILD_CONTRAST
  8574. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  8575. #else
  8576. [dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  8577. #endif
  8578. return dic;
  8579. }
  8580. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  8581. // params
  8582. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8583. orderCode = [self translateSingleQuote:orderCode];
  8584. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8585. // setting
  8586. NSDictionary *setting = params[@"setting"];
  8587. NSNumber *hide = setting[@"OrderTotalHide"];
  8588. [dic setValue:hide forKey:@"hide"];
  8589. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value,discount from offline_order where so_id = '%@';",orderCode];
  8590. __block double lift_gate_value = 0;
  8591. __block double handling_fee = 0;
  8592. __block double shipping = 0;
  8593. __block double discount = 0;
  8594. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8595. int lift_gate = sqlite3_column_int(stmt, 0);
  8596. lift_gate_value = sqlite3_column_double(stmt, 1);
  8597. shipping = sqlite3_column_double(stmt, 2);
  8598. handling_fee = sqlite3_column_double(stmt, 3);
  8599. discount = sqlite3_column_double(stmt, 4);
  8600. if (!lift_gate) {
  8601. lift_gate_value = 0;
  8602. }
  8603. }];
  8604. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  8605. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  8606. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  8607. double payments = [[total valueForKey:@"payments"] doubleValue];
  8608. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  8609. double totalPrice = payments* (1-discount/100.0);
  8610. [dic setValue:@"Order Total" forKey:@"title"];
  8611. NSDictionary *payments_dic = @{
  8612. @"align" : @"right",
  8613. @"aname" : @"Payments/Credits",
  8614. @"control" : @"text",
  8615. @"name" : @"paymentsAndCredits",
  8616. @"type" : @"price",
  8617. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  8618. };
  8619. [dic setValue:payments_dic forKey:@"item_0"];
  8620. // version 1.71 remove
  8621. // NSDictionary *handling_fee_dic = @{
  8622. // @"align" : @"right",
  8623. // @"aname" : @"Handling Fee",
  8624. // @"control" : @"text",
  8625. // @"name" : @"handling_fee_value",
  8626. // @"required" : @"true",
  8627. // @"type" : @"price",
  8628. // @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  8629. // };
  8630. NSDictionary *shipping_dic = @{
  8631. @"align" : @"right",
  8632. @"aname" : @"Shipping*",
  8633. @"control" : @"text",
  8634. @"name" : @"shipping",
  8635. @"required" : @"true",
  8636. @"type" : @"price",
  8637. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  8638. };
  8639. NSDictionary *lift_gate_dic = @{
  8640. @"align" : @"right",
  8641. @"aname" : @"Liftgate Fee(No Loading Dock)",
  8642. @"control" : @"text",
  8643. @"name" : @"lift_gate_value",
  8644. @"required" : @"true",
  8645. @"type" : @"price",
  8646. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  8647. };
  8648. int item_count = 1;
  8649. if (![[params objectForKey:@"logist"] isEqualToString:@"WILL CALL"]) {
  8650. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  8651. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  8652. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8653. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8654. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  8655. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8656. } else {
  8657. }
  8658. }
  8659. // version 1.71 remove
  8660. // [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8661. // NSString* strdiscount;
  8662. if(discount>0)
  8663. {
  8664. NSDictionary *discount_dict = @{
  8665. @"align" : @"right",
  8666. @"aname" : @"Order Discount",
  8667. @"control" : @"text",
  8668. @"name" : @"orderDiscount",
  8669. // @"type" : @"price",
  8670. @"value" : [NSString stringWithFormat:@"%d%% off",(int)discount]
  8671. };
  8672. [dic setValue:discount_dict forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8673. }
  8674. NSDictionary *total_price_dic = @{
  8675. @"align" : @"right",
  8676. @"aname" : @"Total",
  8677. @"control" : @"text",
  8678. @"name" : @"totalPrice",
  8679. @"type" : @"price",
  8680. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  8681. };
  8682. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8683. NSDictionary *total_cuft_dic = @{
  8684. @"align" : @"right",
  8685. @"aname" : @"Total Cuft",
  8686. @"control" : @"text",
  8687. @"name" : @"",
  8688. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  8689. };
  8690. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8691. NSDictionary *total_weight_dic = @{
  8692. @"align" : @"right",
  8693. @"aname" : @"Total Weight",
  8694. @"control" : @"text",
  8695. @"name" : @"",
  8696. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  8697. };
  8698. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8699. NSDictionary *total_carton_dic = @{
  8700. @"align" : @"right",
  8701. @"aname" : @"Total Carton",
  8702. @"control" : @"text",
  8703. @"name" : @"",
  8704. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  8705. };
  8706. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8707. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  8708. return dic;
  8709. }
  8710. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  8711. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8712. orderCode = [self translateSingleQuote:orderCode];
  8713. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8714. // setting
  8715. NSDictionary *setting = params[@"setting"];
  8716. NSNumber *hide = setting[@"SignatureHide"];
  8717. [dic setValue:hide forKey:@"hide"];
  8718. [dic setValue:@"Signature" forKey:@"title"];
  8719. __block NSString *pic_path = @"";
  8720. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  8721. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8722. pic_path = [self textAtColumn:0 statement:stmt];
  8723. }];
  8724. NSDictionary *pic_dic = @{
  8725. @"aname" : @"Signature",
  8726. @"avalue" :pic_path,
  8727. @"control" : @"signature",
  8728. @"name" : @"sign_picpath",
  8729. @"value" : pic_path
  8730. };
  8731. [dic setValue:pic_dic forKey:@"item_0"];
  8732. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8733. return dic;
  8734. }
  8735. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  8736. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8737. // setting
  8738. NSDictionary *setting = params[@"setting"];
  8739. NSNumber *hide = setting[@"ShippingMethodHide"];
  8740. [dic setValue:hide forKey:@"hide"];
  8741. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8742. orderCode = [self translateSingleQuote:orderCode];
  8743. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note,carrier from offline_order where so_id = '%@';",orderCode];
  8744. __block NSString *logist = @"";
  8745. __block NSString *carrier = @"";
  8746. __block NSString *lift_gate = @"";
  8747. __block int lift_gate_integer = 0;
  8748. __block NSString *logistic_note = @"";
  8749. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8750. logist = [self textAtColumn:0 statement:stmt];
  8751. lift_gate_integer = sqlite3_column_int(stmt, 1);
  8752. logistic_note = [self textAtColumn:2 statement:stmt];
  8753. carrier = [self textAtColumn:3 statement:stmt];
  8754. }];
  8755. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8756. [dic setValue:@"Shipping Method" forKey:@"title"];
  8757. // val_0
  8758. int PERSONAL_PICK_UP_check = 0;
  8759. int USE_MY_CARRIER_check = 0;
  8760. NSString *logistic_note_text = @"";
  8761. int will_call_check = 0;
  8762. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  8763. will_call_check = 1;
  8764. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  8765. PERSONAL_PICK_UP_check = 1;
  8766. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  8767. USE_MY_CARRIER_check = 1;
  8768. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  8769. logistic_note = [logistic_note_array firstObject];
  8770. if (logistic_note_array.count > 1) {
  8771. logistic_note_text = [logistic_note_array lastObject];
  8772. }
  8773. }
  8774. }
  8775. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  8776. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  8777. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  8778. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  8779. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  8780. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8781. NSDictionary *val0_subItem_item0 = @{
  8782. @"aname" : @"Option",
  8783. @"cadedate" : @{
  8784. @"count" : [NSNumber numberWithInteger:2],
  8785. @"val_0" : @{
  8786. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  8787. @"refresh" : [NSNumber numberWithInteger:0],
  8788. @"value" : @"PERSONAL PICK UP",
  8789. @"value_id" : @"PERSONAL PICK UP"
  8790. },
  8791. @"val_1" : @{
  8792. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  8793. @"refresh" : [NSNumber numberWithInteger:0],
  8794. @"sub_item" : @{
  8795. @"count" : [NSNumber numberWithInteger:1],
  8796. @"item_0" : @{
  8797. @"aname" : @"BOL",
  8798. @"control" : @"edit",
  8799. @"keyboard" : @"text",
  8800. @"name" : @"logist_note_text",
  8801. @"refresh" : [NSNumber numberWithInteger:0],
  8802. @"required" : @"false",
  8803. @"value" : logistic_note_text
  8804. }
  8805. },
  8806. @"value" : @"USE MY CARRIER",
  8807. @"value_id" : @"USE MY CARRIER"
  8808. }
  8809. },
  8810. @"control" : @"enum",
  8811. @"name" : @"logistic_note",
  8812. @"required" : @"true",
  8813. @"single_select" : @"true"
  8814. };
  8815. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  8816. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  8817. // val_1
  8818. lift_gate = [self valueInParams:params key:@"lift_gate"];
  8819. if([lift_gate isEqualToString:@""]) {
  8820. if (lift_gate_integer == 1) {
  8821. lift_gate = @"true";
  8822. } else {
  8823. lift_gate = @"false";
  8824. }
  8825. }
  8826. // carrier = [self valueInParams:params key:@"carrier"];
  8827. // if([carrier isEqualToString:@""]) {
  8828. //
  8829. // if (lift_gate_integer == 1) {
  8830. // lift_gate = @"true";
  8831. // } else {
  8832. // lift_gate = @"false";
  8833. // }
  8834. //
  8835. // }
  8836. int common_carrier_check = 0;
  8837. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  8838. common_carrier_check = 1;
  8839. [params setValue:lift_gate forKey:@"lift_gate"];
  8840. #ifdef BUILD_CONTRAST
  8841. [params setValue:carrier forKey:@"carrier"];
  8842. #endif
  8843. }
  8844. // NSString *Sql = [NSString stringWithFormat: @"select carrier from offline_contact where contact_id = %d;",contact_id];
  8845. // NSString * code = [iSalesDB jk_queryText:Sql];
  8846. // [self offline_getCarrierCode:offline_getCarrierCode];
  8847. #ifdef BUILD_CONTRAST
  8848. NSDictionary *carrierDic = [self offline_OrderCarrier:params[@"orderCode"] db:db];
  8849. #endif
  8850. NSDictionary *val_1 = @{
  8851. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  8852. @"sub_item" : @{
  8853. @"item_0" : @{
  8854. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  8855. @"control" : @"switch",
  8856. @"name" : @"lift_gate",
  8857. @"refresh" : [NSNumber numberWithInteger:1],
  8858. @"required" : @"true",
  8859. @"value" : lift_gate
  8860. },
  8861. #ifdef BUILD_CONTRAST
  8862. @"count" : [NSNumber numberWithInteger:2],
  8863. @"item_1" : @{
  8864. @"aname" : @"Carrier",
  8865. @"cadedate" :carrierDic,
  8866. @"control" : @"enum",
  8867. @"name" : @"carrier",
  8868. @"required" : @"false",
  8869. @"value" : @"",
  8870. @"single_select" : @"true"
  8871. }
  8872. #else
  8873. @"count" : [NSNumber numberWithInteger:1]
  8874. #endif
  8875. },
  8876. @"value" : @"COMMON CARRIER",
  8877. @"value_id" : @"COMMON CARRIER"
  8878. };
  8879. // cadedate
  8880. NSDictionary *cadedate = @{
  8881. @"count" : [NSNumber numberWithInteger:2],
  8882. @"val_0" : val_0,
  8883. @"val_1" : val_1
  8884. };
  8885. // item_0
  8886. NSMutableDictionary *item_0 = @{
  8887. @"aname" : @"Shipping",
  8888. @"cadedate" : cadedate,
  8889. @"control" : @"enum",
  8890. @"name" : @"logist",
  8891. @"refresh" : [NSNumber numberWithInteger:1],
  8892. @"single_select" : @"true",
  8893. }.mutableCopy;
  8894. NSNumber *required = setting[@"ShippingMethodRequire"];
  8895. if ([required integerValue]) {
  8896. [item_0 setValue:@"true" forKey:@"required"];
  8897. }
  8898. [dic setValue:item_0 forKey:@"item_0"];
  8899. if ([logist isEqualToString:@"WILL CALL"]) {
  8900. [dic removeObjectForKey:@"lift_gate"];
  8901. }
  8902. return dic;
  8903. }
  8904. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  8905. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8906. orderCode = [self translateSingleQuote:orderCode];
  8907. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  8908. __block int submit_as_integer = 0;
  8909. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8910. submit_as_integer = sqlite3_column_int(stmt, 0);
  8911. }];
  8912. int check11 = 0;
  8913. int check10 = 0;
  8914. if (submit_as_integer == 11) {
  8915. check11 = 1;
  8916. }
  8917. if (submit_as_integer == 10) {
  8918. check10 = 1;
  8919. }
  8920. // section_0
  8921. NSMutableDictionary *dic = @{
  8922. @"count" : @(1),
  8923. @"item_0" : @{
  8924. @"aname" : @"Submit Order As",
  8925. @"cadedate" : @{
  8926. @"count" : @(2),
  8927. @"val_0" : @{
  8928. @"check" : [NSNumber numberWithInteger:check11],
  8929. @"value" : @"Sales Order",
  8930. @"value_id" : @(11)
  8931. },
  8932. @"val_1" : @{
  8933. @"check" : [NSNumber numberWithInteger:check10],
  8934. @"value" : @"Quote",
  8935. @"value_id" : @(10)
  8936. }
  8937. },
  8938. @"control" : @"enum",
  8939. @"name" : @"erpOrderStatus",
  8940. @"required" : @"true",
  8941. @"single_select" : @"true"
  8942. },
  8943. @"title" : @"Order Type"
  8944. }.mutableCopy;
  8945. // setting
  8946. NSDictionary *setting = params[@"setting"];
  8947. NSNumber *hide = setting[@"OrderTypeHide"];
  8948. [dic setValue:hide forKey:@"hide"];
  8949. return dic;
  8950. }
  8951. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  8952. countryCode = [self translateSingleQuote:countryCode];
  8953. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  8954. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8955. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  8956. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  8957. if (name == NULL) {
  8958. name = "";
  8959. }
  8960. if (code == NULL) {
  8961. code = "";
  8962. }
  8963. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  8964. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  8965. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  8966. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  8967. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  8968. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  8969. }
  8970. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  8971. [container setValue:stateDic forKey:key];
  8972. }] mutableCopy];
  8973. [ret removeObjectForKey:@"result"];
  8974. // failure 可以不用了,一样的
  8975. if (ret.allKeys.count == 0) {
  8976. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  8977. [stateDic setValue:@"Other" forKey:@"value"];
  8978. [stateDic setValue:@"" forKey:@"value_id"];
  8979. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  8980. if (state_code && [@"" isEqualToString:state_code]) {
  8981. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  8982. }
  8983. NSString *key = [NSString stringWithFormat:@"val_0"];
  8984. [ret setValue:stateDic forKey:key];
  8985. }
  8986. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  8987. return ret;
  8988. }
  8989. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  8990. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8991. orderCode = [self translateSingleQuote:orderCode];
  8992. 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];
  8993. __block NSString *payType = @"";
  8994. __block NSString *firstName = @"";
  8995. __block NSString *lastName = @"";
  8996. __block NSString *addr1 = @"";
  8997. __block NSString *addr2 = @"";
  8998. __block NSString *zipcode = @"";
  8999. __block NSString *cardType = @"";
  9000. __block NSString *cardNumber = @"";
  9001. __block NSString *securityCode = @"";
  9002. __block NSString *month = @"";
  9003. __block NSString *year = @"";
  9004. __block NSString *city = @"";
  9005. __block NSString *state = @"";
  9006. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9007. payType = [self textAtColumn:0 statement:stmt];
  9008. firstName = [self textAtColumn:1 statement:stmt];
  9009. lastName = [self textAtColumn:2 statement:stmt];
  9010. addr1 = [self textAtColumn:3 statement:stmt];
  9011. addr2 = [self textAtColumn:4 statement:stmt];
  9012. zipcode = [self textAtColumn:5 statement:stmt];
  9013. cardType = [self textAtColumn:6 statement:stmt];
  9014. cardNumber = [self textAtColumn:7 statement:stmt];
  9015. securityCode = [self textAtColumn:8 statement:stmt];
  9016. month = [self textAtColumn:9 statement:stmt];
  9017. year = [self textAtColumn:10 statement:stmt];
  9018. city = [self textAtColumn:11 statement:stmt];
  9019. state = [self textAtColumn:12 statement:stmt];
  9020. }];
  9021. NSString *required = @"true";
  9022. // setting
  9023. NSDictionary *setting = params[@"setting"];
  9024. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  9025. if ([requiredNumber integerValue]) {
  9026. required = @"true";
  9027. } else {
  9028. required = @"false";
  9029. }
  9030. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  9031. // create table if not exists payment_type (_id integer primary key,type_id text,pay_type text,sort int);
  9032. NSString *pay_type_sql = @"select type_id,pay_type,sort_id from payment_type order by sort_id asc";
  9033. [iSalesDB jk_query:pay_type_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9034. NSString *type_id = [self textAtColumn:0 statement:stmt];
  9035. NSString *type = [self textAtColumn:1 statement:stmt];
  9036. NSMutableDictionary *val = @{
  9037. @"check" : [payType isEqualToString:type_id] ? @(1) : @(0),
  9038. @"value" : type,
  9039. @"value_id" : type_id
  9040. }.mutableCopy;
  9041. if ([type_id.lowercaseString isEqualToString:@"Visa/Master".lowercaseString] || [type_id.lowercaseString isEqualToString:@"Credit Card".lowercaseString]) {
  9042. NSDictionary *sub_item =nil;
  9043. #ifdef BUILD_CONTRAST
  9044. sub_item = @{
  9045. @"count" : @(3),
  9046. @"item_0" : @{
  9047. @"aname" : @"Number",
  9048. @"control" : @"edit",
  9049. @"keyboard" : @"int",
  9050. @"length" : @"16",
  9051. @"name" : @"credit_card_number",
  9052. @"required" : @"false",
  9053. @"value" : cardNumber
  9054. },
  9055. @"item_1" : @{
  9056. @"aname" : @"Expiration Date",
  9057. @"control" : @"monthpicker",
  9058. @"name" : @"credit_card_expiration",
  9059. @"required" : @"false",
  9060. @"type" : @"date",
  9061. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9062. },
  9063. @"item_2" : @{
  9064. @"aname" : @"Security Code",
  9065. @"control" : @"edit",
  9066. @"keyboard" : @"int",
  9067. @"length" : @"4",
  9068. @"name" : @"credit_card_security_code",
  9069. @"required" : @"false",
  9070. @"value" : securityCode
  9071. },
  9072. };
  9073. #else
  9074. sub_item= @{
  9075. @"count" : @(3),
  9076. @"item_0" : @{
  9077. @"aname" : @"choose",
  9078. @"control" : @"multi_action",
  9079. @"count" : @(1),
  9080. @"item_0" : @{
  9081. @"aname" : @"Same as customer",
  9082. @"key_map" : @{
  9083. @"credit_card_address1" : @"customer_address1",
  9084. @"credit_card_address2" : @"customer_address2",
  9085. @"credit_card_city" : @"customer_city",
  9086. @"credit_card_first_name" : @"customer_first_name",
  9087. @"credit_card_last_name" : @"customer_last_name",
  9088. @"credit_card_state" : @"customer_state",
  9089. @"credit_card_zipcode" : @"customer_zipcode"
  9090. },
  9091. @"type" : @"pull"
  9092. }
  9093. },
  9094. @"item_1" : @{
  9095. @"aname" : @"",
  9096. @"color" : @"red",
  9097. @"control" : @"text",
  9098. @"name" : @"",
  9099. @"value" : @"USA Credit cards only"
  9100. },
  9101. @"item_2" : @{
  9102. @"aname" : @"Fill",
  9103. @"cadedate" : @{
  9104. @"count" : @(2),
  9105. @"val_0" : @{
  9106. @"check" : @(1),
  9107. @"sub_item" : @{
  9108. @"count" : @(11),
  9109. @"item_0" : @{
  9110. @"aname" : @"Type",
  9111. @"cadedate" : @{
  9112. @"count" : @(2),
  9113. @"val_0" : @{
  9114. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  9115. @"value" : @"VISA",
  9116. @"value_id" : @"VISA"/*@(0)*/
  9117. },
  9118. @"val_1" : @{
  9119. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  9120. @"value" : @"MASTER CARD",
  9121. @"value_id" : @"MASTER CARD"/*@(1)*/
  9122. }
  9123. },
  9124. @"control" : @"enum",
  9125. @"name" : @"credit_card_type",
  9126. @"required" : @"true",
  9127. @"single_select" : @"true"
  9128. },
  9129. @"item_1" : @{
  9130. @"aname" : @"Number",
  9131. @"control" : @"edit",
  9132. @"keyboard" : @"int",
  9133. @"length" : @"16",
  9134. @"name" : @"credit_card_number",
  9135. @"required" : @"true",
  9136. @"value" : cardNumber
  9137. },
  9138. @"item_10" : @{
  9139. @"aname" : @"State",
  9140. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  9141. @"control" : @"enum",
  9142. @"enum" : @"true",
  9143. @"name" : @"credit_card_state",
  9144. @"required" : @"true",
  9145. @"single_select" : @"true"
  9146. },
  9147. @"item_2" : @{
  9148. @"aname" : @"Expiration Date",
  9149. @"control" : @"monthpicker",
  9150. @"name" : @"credit_card_expiration",
  9151. @"required" : @"true",
  9152. @"type" : @"date",
  9153. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9154. },
  9155. @"item_3" : @{
  9156. @"aname" : @"Security Code",
  9157. @"control" : @"edit",
  9158. @"keyboard" : @"int",
  9159. @"length" : @"3",
  9160. @"name" : @"credit_card_security_code",
  9161. @"required" : @"true",
  9162. @"value" : securityCode
  9163. },
  9164. @"item_4" : @{
  9165. @"aname" : @"First Name",
  9166. @"control" : @"edit",
  9167. @"keyboard" : @"text",
  9168. @"name" : @"credit_card_first_name",
  9169. @"required" : @"true",
  9170. @"value" : firstName
  9171. },
  9172. @"item_5" : @{
  9173. @"aname" : @"Last Name",
  9174. @"control" : @"edit",
  9175. @"keyboard" : @"text",
  9176. @"name" : @"credit_card_last_name",
  9177. @"required" : @"true",
  9178. @"value" : lastName
  9179. },
  9180. @"item_6" : @{
  9181. @"aname" : @"Address 1",
  9182. @"control" : @"edit",
  9183. @"keyboard" : @"text",
  9184. @"name" : @"credit_card_address1",
  9185. @"required" : @"true",
  9186. @"value" : addr1
  9187. },
  9188. @"item_7" : @{
  9189. @"aname" : @"Address 2",
  9190. @"control" : @"edit",
  9191. @"keyboard" : @"text",
  9192. @"name" : @"credit_card_address2",
  9193. @"value" : addr2
  9194. },
  9195. @"item_8" : @{
  9196. @"aname" : @"zip code",
  9197. @"control" : @"edit",
  9198. @"keyboard" : @"text",
  9199. @"name" : @"credit_card_zipcode",
  9200. @"required" : @"true",
  9201. @"value" : zipcode
  9202. },
  9203. @"item_9" : @{
  9204. @"aname" : @"City",
  9205. @"control" : @"edit",
  9206. @"keyboard" : @"text",
  9207. @"name" : @"credit_card_city",
  9208. @"required" : @"true",
  9209. @"value" : city
  9210. }
  9211. },
  9212. @"value" : @"Fill Now",
  9213. @"value_id" : @""
  9214. },
  9215. @"val_1" : @{
  9216. @"check" : @(0),
  9217. @"value" : @"Fill Later",
  9218. @"value_id" : @""
  9219. }
  9220. },
  9221. @"control" : @"enum",
  9222. @"name" : @"",
  9223. @"single_select" : @"true"
  9224. }
  9225. };
  9226. #endif
  9227. [val setObject:sub_item forKey:@"sub_item"];
  9228. }
  9229. [cadedate setObject:val forKey:[NSString stringWithFormat:@"val_%ld",(*count)]];
  9230. [cadedate setObject:[NSNumber numberWithInteger:(++*count)] forKey:@"count"];
  9231. }];
  9232. // "section_2"
  9233. NSMutableDictionary *dic = @{
  9234. @"count" : @(1),
  9235. @"item_0" : @{
  9236. @"aname" : @"Payment",
  9237. @"required" : required,
  9238. @"cadedate" : cadedate,
  9239. // @{
  9240. // @"count" : @(6),
  9241. // @"val_3" : @{
  9242. // @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  9243. // @"value" : @"Check",
  9244. // @"value_id" : @"Check"
  9245. // },
  9246. // @"val_2" : @{
  9247. // @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  9248. // @"value" : @"Cash",
  9249. // @"value_id" : @"Cash"
  9250. // },
  9251. // @"val_1" : @{
  9252. // @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  9253. // @"value" : @"NET 60",
  9254. // @"value_id" : @"NET 30"
  9255. // },
  9256. // @"val_4" : @{
  9257. // @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  9258. // @"value" : @"Wire Transfer",
  9259. // @"value_id" : @"Wire Transfer"
  9260. // },
  9261. // @"val_0" : @{
  9262. // @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  9263. // @"sub_item" : @{
  9264. // @"count" : @(3),
  9265. // @"item_0" : @{
  9266. // @"aname" : @"choose",
  9267. // @"control" : @"multi_action",
  9268. // @"count" : @(1),
  9269. // @"item_0" : @{
  9270. // @"aname" : @"Same as customer",
  9271. // @"key_map" : @{
  9272. // @"credit_card_address1" : @"customer_address1",
  9273. // @"credit_card_address2" : @"customer_address2",
  9274. // @"credit_card_city" : @"customer_city",
  9275. // @"credit_card_first_name" : @"customer_first_name",
  9276. // @"credit_card_last_name" : @"customer_last_name",
  9277. // @"credit_card_state" : @"customer_state",
  9278. // @"credit_card_zipcode" : @"customer_zipcode"
  9279. // },
  9280. // @"type" : @"pull"
  9281. // }
  9282. // },
  9283. // @"item_1" : @{
  9284. // @"aname" : @"",
  9285. // @"color" : @"red",
  9286. // @"control" : @"text",
  9287. // @"name" : @"",
  9288. // @"value" : @"USA Credit cards only"
  9289. // },
  9290. // @"item_2" : @{
  9291. // @"aname" : @"Fill",
  9292. // @"cadedate" : @{
  9293. // @"count" : @(2),
  9294. // @"val_0" : @{
  9295. // @"check" : @(1),
  9296. // @"sub_item" : @{
  9297. // @"count" : @(11),
  9298. // @"item_0" : @{
  9299. // @"aname" : @"Type",
  9300. // @"cadedate" : @{
  9301. // @"count" : @(2),
  9302. // @"val_0" : @{
  9303. // @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  9304. // @"value" : @"VISA",
  9305. // @"value_id" : @(0)
  9306. // },
  9307. // @"val_1" : @{
  9308. // @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  9309. // @"value" : @"MASTER CARD",
  9310. // @"value_id" : @(1)
  9311. // }
  9312. // },
  9313. // @"control" : @"enum",
  9314. // @"name" : @"credit_card_type",
  9315. // @"required" : @"true",
  9316. // @"single_select" : @"true"
  9317. // },
  9318. // @"item_1" : @{
  9319. // @"aname" : @"Number",
  9320. // @"control" : @"edit",
  9321. // @"keyboard" : @"int",
  9322. // @"length" : @"16",
  9323. // @"name" : @"credit_card_number",
  9324. // @"required" : @"true",
  9325. // @"value" : cardNumber
  9326. // },
  9327. // @"item_10" : @{
  9328. // @"aname" : @"State",
  9329. // @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  9330. // @"control" : @"enum",
  9331. // @"enum" : @"true",
  9332. // @"name" : @"credit_card_state",
  9333. // @"required" : @"true",
  9334. // @"single_select" : @"true"
  9335. // },
  9336. // @"item_2" : @{
  9337. // @"aname" : @"Expiration Date",
  9338. // @"control" : @"monthpicker",
  9339. // @"name" : @"credit_card_expiration",
  9340. // @"required" : @"true",
  9341. // @"type" : @"date",
  9342. // @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  9343. // },
  9344. // @"item_3" : @{
  9345. // @"aname" : @"Security Code",
  9346. // @"control" : @"edit",
  9347. // @"keyboard" : @"int",
  9348. // @"length" : @"3",
  9349. // @"name" : @"credit_card_security_code",
  9350. // @"required" : @"true",
  9351. // @"value" : securityCode
  9352. // },
  9353. // @"item_4" : @{
  9354. // @"aname" : @"First Name",
  9355. // @"control" : @"edit",
  9356. // @"keyboard" : @"text",
  9357. // @"name" : @"credit_card_first_name",
  9358. // @"required" : @"true",
  9359. // @"value" : firstName
  9360. // },
  9361. // @"item_5" : @{
  9362. // @"aname" : @"Last Name",
  9363. // @"control" : @"edit",
  9364. // @"keyboard" : @"text",
  9365. // @"name" : @"credit_card_last_name",
  9366. // @"required" : @"true",
  9367. // @"value" : lastName
  9368. // },
  9369. // @"item_6" : @{
  9370. // @"aname" : @"Address 1",
  9371. // @"control" : @"edit",
  9372. // @"keyboard" : @"text",
  9373. // @"name" : @"credit_card_address1",
  9374. // @"required" : @"true",
  9375. // @"value" : addr1
  9376. // },
  9377. // @"item_7" : @{
  9378. // @"aname" : @"Address 2",
  9379. // @"control" : @"edit",
  9380. // @"keyboard" : @"text",
  9381. // @"name" : @"credit_card_address2",
  9382. // @"value" : addr2
  9383. // },
  9384. // @"item_8" : @{
  9385. // @"aname" : @"zip code",
  9386. // @"control" : @"edit",
  9387. // @"keyboard" : @"text",
  9388. // @"name" : @"credit_card_zipcode",
  9389. // @"required" : @"true",
  9390. // @"value" : zipcode
  9391. // },
  9392. // @"item_9" : @{
  9393. // @"aname" : @"City",
  9394. // @"control" : @"edit",
  9395. // @"keyboard" : @"text",
  9396. // @"name" : @"credit_card_city",
  9397. // @"required" : @"true",
  9398. // @"value" : city
  9399. // }
  9400. // },
  9401. // @"value" : @"Fill Now",
  9402. // @"value_id" : @""
  9403. // },
  9404. // @"val_1" : @{
  9405. // @"check" : @(0),
  9406. // @"value" : @"Fill Later",
  9407. // @"value_id" : @""
  9408. // }
  9409. // },
  9410. // @"control" : @"enum",
  9411. // @"name" : @"",
  9412. // @"single_select" : @"true"
  9413. // }
  9414. // },
  9415. // @"value" : @"Visa/Master",
  9416. // @"value_id" : @"Credit Card"
  9417. // },
  9418. // @"val_5" : @{
  9419. // @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  9420. // @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  9421. // @"value_id" : @"FOLLOW EXISTING"
  9422. // }
  9423. // },
  9424. @"control" : @"enum",
  9425. @"name" : @"paymentType",
  9426. @"single_select" : @"true"
  9427. },
  9428. @"title" : @"Payment Information"
  9429. }.mutableCopy;
  9430. NSNumber *hide = setting[@"PaymentInformationHide"];
  9431. [dic setValue:hide forKey:@"hide"];
  9432. return dic;
  9433. }
  9434. + (BOOL)orderOutOfStock:(NSDictionary *)params db:(sqlite3 *)db{
  9435. // params
  9436. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9437. orderCode = [self translateSingleQuote:orderCode];
  9438. // 缺货检查
  9439. 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];
  9440. __block BOOL outOfStock = NO;
  9441. sqlite3 *database = db;
  9442. if (!db) {
  9443. database = [iSalesDB get_db];
  9444. }
  9445. [iSalesDB jk_query:out_of_stock_SQL db:database close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9446. int availability = sqlite3_column_int(stmt, 0);
  9447. int item_qty = sqlite3_column_int(stmt, 1);
  9448. outOfStock = outOfStock || (availability < item_qty ? YES : NO);
  9449. }];
  9450. if (!db) {
  9451. [iSalesDB close_db:database];
  9452. }
  9453. return outOfStock;
  9454. }
  9455. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  9456. {
  9457. assert(params[@"user"]!=nil);
  9458. assert(params[@"contact_id"]!=nil);
  9459. assert(params[@"can_create_backorder"]!=nil);
  9460. sqlite3 *db = [iSalesDB get_db];
  9461. // params
  9462. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9463. orderCode = [self translateSingleQuote:orderCode];
  9464. // 缺货检查
  9465. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9466. if (![params[@"can_create_backorder"] boolValue]) {
  9467. BOOL out_of_stock = [self orderOutOfStock:params db:db];
  9468. if (out_of_stock) {
  9469. [iSalesDB close_db:db];
  9470. NSMutableDictionary *resultDic = [NSMutableDictionary dictionary];
  9471. [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"];
  9472. [resultDic setObject:[NSNumber numberWithInt:8] forKey:@"result"];
  9473. return [RAConvertor dict2data:resultDic];
  9474. }
  9475. }
  9476. // UISetting
  9477. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9478. NSString *cachefolder = [paths objectAtIndex:0];
  9479. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  9480. NSData* json =nil;
  9481. json=[NSData dataWithContentsOfFile:img_cache];
  9482. NSError *error=nil;
  9483. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  9484. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  9485. [params setObject:setting forKey:@"setting"];
  9486. int section_count = 0;
  9487. // 0 Order Type 1 Shipping Method 2 Payment Information
  9488. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  9489. // 0 Order Type
  9490. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  9491. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  9492. [ret setValue:order_type_dic forKey:key0];
  9493. // 1 Shipping Method
  9494. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  9495. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  9496. [ret setValue:shipping_method_dic forKey:key1];
  9497. // 2 Payment Information
  9498. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  9499. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  9500. [ret setValue:payment_info_dic forKey:key2];
  9501. // 3 Customer
  9502. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  9503. NSDictionary *customer_dic = [self customerDic:params db:db];
  9504. [ret setValue:customer_dic forKey:key3];
  9505. // 4 Ship To
  9506. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  9507. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  9508. [ret setValue:ship_to_dic forKey:key4];
  9509. // 5 Ship From
  9510. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  9511. if (![shipFromDisable integerValue]) {
  9512. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  9513. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  9514. [ret setValue:ship_from_dic forKey:key5];
  9515. }
  9516. // 6 Freight Bill To
  9517. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  9518. if (![freightBillToDisable integerValue]) {
  9519. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  9520. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  9521. [ret setValue:freight_bill_to forKey:key6];
  9522. }
  9523. // 7 Merchandise Bill To
  9524. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  9525. if (![merchandiseBillToDisable integerValue]) {
  9526. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  9527. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  9528. [ret setValue:merchandise_bill_to_dic forKey:key7];
  9529. }
  9530. // 8 Return To
  9531. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  9532. if (![returnToDisable integerValue]) {
  9533. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  9534. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  9535. [ret setValue:return_to_dic forKey:key8];
  9536. }
  9537. // 9 Model Information
  9538. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  9539. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  9540. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  9541. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  9542. [ret setValue:model_info_dic forKey:key9];
  9543. // 10 Remarks Content
  9544. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  9545. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  9546. [ret setValue:remarks_content_dic forKey:key10];
  9547. // 11 Order Total
  9548. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  9549. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  9550. [ret setValue:order_total_dic forKey:key11];
  9551. // 12 Signature
  9552. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  9553. NSDictionary *sign_dic = [self signatureDic:params db:db];
  9554. [ret setValue:sign_dic forKey:key12];
  9555. [ret setValue:@(section_count) forKey:@"section_count"];
  9556. [iSalesDB close_db:db];
  9557. return [RAConvertor dict2data:ret];
  9558. // return nil;
  9559. }
  9560. #pragma mark addr editor
  9561. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  9562. NSMutableDictionary *new_item = [item mutableCopy];
  9563. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  9564. return new_item;
  9565. }
  9566. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  9567. {
  9568. // "is_subaction" = true;
  9569. // orderCode = MOB1608240002;
  9570. // password = 123456;
  9571. // "subaction_tag" = 1;
  9572. // user = EvanK;
  9573. // {
  9574. // "is_subaction" = true;
  9575. // orderCode = MOB1608240002;
  9576. // password = 123456;
  9577. // "refresh_trigger" = zipcode;
  9578. // "subaction_tag" = 1;
  9579. // user = EvanK;
  9580. // }
  9581. NSString *country_code = nil;
  9582. NSString *zipCode = nil;
  9583. NSString *stateCode = nil;
  9584. NSString *city = nil;
  9585. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  9586. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  9587. NSString *code_id = params[@"country"];
  9588. country_code = [self countryCodeByid:code_id];
  9589. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  9590. NSString *zip_code = params[@"zipcode"];
  9591. // 剔除全部为空格
  9592. int spaceCount = 0;
  9593. for (int i = 0; i < zip_code.length; i++) {
  9594. if ([zip_code characterAtIndex:i] == ' ') {
  9595. spaceCount++;
  9596. }
  9597. }
  9598. if (spaceCount == zip_code.length) {
  9599. zip_code = @"";
  9600. }
  9601. zipCode = zip_code;
  9602. if (zipCode.length > 0) {
  9603. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  9604. country_code = [dic valueForKey:@"country_code"];
  9605. if (!country_code) {
  9606. // country_code = @"US";
  9607. NSString *code_id = params[@"country"];
  9608. country_code = [self countryCodeByid:code_id];
  9609. }
  9610. stateCode = [dic valueForKey:@"state_code"];
  9611. if(!stateCode.length) {
  9612. stateCode = params[@"state"];
  9613. }
  9614. city = [dic valueForKey:@"city"];
  9615. if (!city.length) {
  9616. city = params[@"city"];
  9617. }
  9618. // zip code
  9619. // NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  9620. // [zipDic setValue:zipCode forKey:@"value"];
  9621. // [section_0 setValue:zipDic forKey:@"item_11"];
  9622. } else {
  9623. NSString *code_id = params[@"country"];
  9624. country_code = [self countryCodeByid:code_id];
  9625. stateCode = params[@"state"];
  9626. city = params[@"city"];
  9627. }
  9628. }
  9629. }
  9630. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  9631. // [ret removeObjectForKey:@"up_params"];
  9632. [ret setObject:@"New Address" forKey:@"title"];
  9633. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  9634. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  9635. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  9636. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  9637. // [country_dic removeObjectForKey:@"refresh"];
  9638. // [country_dic removeObjectForKey:@"restore"];
  9639. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  9640. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  9641. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  9642. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  9643. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  9644. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  9645. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  9646. // [zip_code_dic removeObjectForKey:@"refresh"];
  9647. [zip_code_dic setValue:zipCode ? zipCode :@"" forKey:@"value"];
  9648. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  9649. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  9650. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  9651. NSDictionary *cityDic = [[new_section_0 objectForKey:@"item_8"] mutableCopy];
  9652. [cityDic setValue:city ? city : @"" forKey:@"value"];
  9653. [new_section_0 setObject:cityDic forKey:@"item_8"];
  9654. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  9655. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  9656. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  9657. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  9658. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  9659. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  9660. // country
  9661. NSString *countryCode = country_code == nil ? @"US" : country_code;
  9662. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  9663. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  9664. // state
  9665. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:stateCode] mutableCopy];
  9666. // NSDictionary *tmpDic = @{
  9667. // @"value" : @"Other",
  9668. // @"value_id" : @"",
  9669. // @"check" : [NSNumber numberWithInteger:0]
  9670. // };
  9671. //
  9672. // for (int i = 0; i < allState.allKeys.count; i++) {
  9673. // NSString *key = [NSString stringWithFormat:@"val_%d",i];
  9674. //
  9675. // NSDictionary *tmp = allState[key];
  9676. // [allState setValue:tmpDic forKey:key];
  9677. // tmpDic = tmp;
  9678. // }
  9679. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  9680. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  9681. [ret setValue:new_section_0 forKey:@"section_0"];
  9682. return [RAConvertor dict2data:ret];
  9683. }
  9684. #pragma mark save addr
  9685. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  9686. {
  9687. // NSString *companyName = [self valueInParams:params key:@"company"];
  9688. // NSString *addr1 = [self valueInParams:params key:@"address"];
  9689. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  9690. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  9691. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  9692. // NSString *countryId = [self valueInParams:params key:@"country"];
  9693. // NSString *stateCode = [self valueInParams:params key:@"state"];
  9694. // NSString *city = [self valueInParams:params key:@"city"];
  9695. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  9696. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  9697. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  9698. // NSString *phone = [self valueInParams:params key:@"phone"];
  9699. // NSString *fax = [self valueInParams:params key:@"fax"];
  9700. // NSString *email = [self valueInParams:params key:@"email"];
  9701. return [self offline_saveContact:params update:NO isCustomer:NO];
  9702. }
  9703. #pragma mark cancel order
  9704. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  9705. {
  9706. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9707. NSString *order_id = [self valueInParams:params key:@"orderId"];
  9708. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  9709. if (order_id.length) {
  9710. sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  9711. }
  9712. int ret = [iSalesDB execSql:sql];
  9713. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9714. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9715. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9716. // [dic setValue:@"160409" forKey:@"min_ver"];
  9717. return [RAConvertor dict2data:dic];
  9718. }
  9719. #pragma mark sign order
  9720. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  9721. {
  9722. //参考 offline_saveBusinesscard
  9723. DebugLog(@"sign order params: %@",params);
  9724. // orderCode = MOB1608240002;
  9725. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  9726. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9727. orderCode = [self translateSingleQuote:orderCode];
  9728. NSString *picPath = [self valueInParams:params key:@"picpath"];
  9729. picPath = [self translateSingleQuote:picPath];
  9730. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  9731. int ret = [iSalesDB execSql:sql];
  9732. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9733. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9734. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9735. // [dic setValue:@"160409" forKey:@"min_ver"];
  9736. return [RAConvertor dict2data:dic];
  9737. }
  9738. #pragma mark save order
  9739. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  9740. NSString *ret = [self valueInParams:params key:key];
  9741. if (translate) {
  9742. ret = [self translateSingleQuote:ret];
  9743. }
  9744. return ret;
  9745. }
  9746. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  9747. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  9748. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  9749. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  9750. NSString* schedule_date = [self valueInParams:param key:@"schedule_date_str"];
  9751. if (schedule_date.length==0 ) {
  9752. schedule_date = @"";
  9753. } else {
  9754. schedule_date = [self rchangeDateFormate:schedule_date];
  9755. schedule_date = [NSString stringWithFormat:@"schedule_date = '%@',",schedule_date];
  9756. }
  9757. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  9758. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  9759. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  9760. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  9761. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  9762. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  9763. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  9764. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  9765. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  9766. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  9767. if (![total_price isEqualToString:@""]) {
  9768. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  9769. } else {
  9770. total_price = @"";
  9771. }
  9772. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  9773. if ([paymentsAndCredits isEqualToString:@""]) {
  9774. paymentsAndCredits = @"";
  9775. } else {
  9776. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  9777. }
  9778. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  9779. if ([handling_fee_value isEqualToString:@""]) {
  9780. handling_fee_value = @"";
  9781. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  9782. handling_fee_value = @"";
  9783. } else {
  9784. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  9785. }
  9786. NSString *handling_fee_placeholder = handling_fee_value;
  9787. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  9788. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  9789. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  9790. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  9791. NSString *lift_gate_placeholder = @"";
  9792. if ([lift_gate_value isEqualToString:@""]) {
  9793. lift_gate_placeholder = @"";
  9794. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  9795. lift_gate_placeholder = @"";
  9796. } else {
  9797. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  9798. }
  9799. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  9800. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  9801. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  9802. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  9803. 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];
  9804. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  9805. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  9806. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  9807. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  9808. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  9809. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  9810. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  9811. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  9812. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  9813. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  9814. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  9815. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  9816. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  9817. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  9818. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  9819. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  9820. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  9821. BOOL number_nil = [credit_card_number isEqualToString:@""];
  9822. if (!number_nil) {
  9823. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  9824. }
  9825. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  9826. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  9827. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  9828. if (!security_code_nil) {
  9829. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  9830. }
  9831. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  9832. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  9833. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  9834. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  9835. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  9836. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  9837. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  9838. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  9839. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  9840. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  9841. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  9842. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  9843. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  9844. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  9845. NSString *contact_id = customer_cid;
  9846. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  9847. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  9848. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  9849. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  9850. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  9851. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  9852. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  9853. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  9854. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  9855. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  9856. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  9857. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  9858. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  9859. //
  9860. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  9861. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  9862. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  9863. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  9864. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  9865. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  9866. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  9867. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  9868. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  9869. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  9870. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  9871. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  9872. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  9873. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  9874. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  9875. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  9876. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  9877. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  9878. 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];
  9879. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  9880. if (receive_cid.length) {
  9881. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  9882. }
  9883. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  9884. if (receive_name.length) {
  9885. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  9886. }
  9887. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  9888. if (receive_ext.length) {
  9889. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  9890. }
  9891. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  9892. if (receive_contact.length) {
  9893. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  9894. }
  9895. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  9896. if (receive_phone.length) {
  9897. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  9898. }
  9899. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  9900. if (receive_email.length) {
  9901. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  9902. }
  9903. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  9904. if (receive_fax.length) {
  9905. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  9906. }
  9907. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  9908. if (sender_cid.length) {
  9909. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  9910. }
  9911. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  9912. if (sender_name.length) {
  9913. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  9914. }
  9915. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  9916. if (sender_ext.length) {
  9917. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  9918. }
  9919. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  9920. if(sender_contact.length) {
  9921. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  9922. }
  9923. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  9924. if (sender_phone.length) {
  9925. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  9926. }
  9927. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  9928. if (sender_fax.length) {
  9929. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  9930. }
  9931. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  9932. if (sender_email.length) {
  9933. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  9934. }
  9935. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  9936. if (shipping_billto_cid.length) {
  9937. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  9938. }
  9939. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  9940. if (shipping_billto_name.length) {
  9941. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  9942. }
  9943. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  9944. if (shipping_billto_ext.length) {
  9945. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  9946. }
  9947. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  9948. if (shipping_billto_contact.length) {
  9949. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  9950. }
  9951. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  9952. if (shipping_billto_phone.length) {
  9953. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  9954. }
  9955. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  9956. if (shipping_billto_fax.length) {
  9957. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  9958. }
  9959. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  9960. if (shipping_billto_email.length) {
  9961. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  9962. }
  9963. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  9964. if (billing_cid.length) {
  9965. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  9966. }
  9967. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  9968. if (billing_name.length) {
  9969. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  9970. }
  9971. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  9972. if (billing_ext.length) {
  9973. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  9974. }
  9975. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  9976. if (billing_contact.length) {
  9977. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  9978. }
  9979. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  9980. if (billing_phone.length) {
  9981. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  9982. }
  9983. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  9984. if (billing_fax.length) {
  9985. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  9986. }
  9987. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  9988. if (billing_email.length) {
  9989. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  9990. }
  9991. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  9992. if (returnto_cid.length) {
  9993. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  9994. }
  9995. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  9996. if (returnto_name.length) {
  9997. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  9998. }
  9999. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  10000. if (returnto_ext.length) {
  10001. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  10002. }
  10003. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  10004. if (returnto_contact.length) {
  10005. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  10006. }
  10007. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  10008. if (returnto_phone.length) {
  10009. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  10010. }
  10011. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  10012. if (returnto_fax.length) {
  10013. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  10014. }
  10015. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  10016. if (returnto_email.length) {
  10017. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  10018. }
  10019. NSString *order_status = @"status = 1,";
  10020. if (submit) {
  10021. order_status = @"status = -11,";
  10022. }
  10023. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  10024. NSString* carrier = [self offline_getCarrier:[param[@"carrier"] intValue]];
  10025. NSString* scarrier = [NSString stringWithFormat:@"carrier = '%@',",carrier];
  10026. NSString *sync_sql = @"";
  10027. if (submit) {
  10028. param[@"truck_carrier"]=carrier;
  10029. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  10030. [param setValue:sales_rep forKey:@"sales_rep"];
  10031. NSString *sync_data = [self translateSingleQuote:[RAConvertor dict2string:param]];
  10032. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  10033. }
  10034. 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];
  10035. DebugLog(@"save order contactSql: %@",contactSql);
  10036. DebugLog(@"save order orderSql: %@",orderSql);
  10037. // int contact_ret = [iSalesDB execSql:contactSql];
  10038. int order_ret = [iSalesDB execSql:orderSql];
  10039. int ret = order_ret;
  10040. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10041. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  10042. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10043. // [dic setValue:@"160409" forKey:@"min_ver"];
  10044. [dic setObject:so_id forKey:@"so#"];
  10045. return [RAConvertor dict2data:dic];
  10046. }
  10047. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  10048. {
  10049. // id foo = nil;
  10050. // NSMutableArray *a = @[].mutableCopy;
  10051. // [a addObject:foo];
  10052. return [self saveorder:param submit:NO];
  10053. }
  10054. #pragma mark add to cart by name
  10055. +(NSData*) offline_add2cartbymodelQR :(NSMutableDictionary *) params
  10056. {
  10057. NSString *orderCode = [params objectForKey:@"orderCode"];
  10058. NSDictionary *newParams = @{
  10059. @"product_id" : params[@"product_id_string"],
  10060. @"orderCode" : orderCode,
  10061. @"can_create_backorder":params[@"can_create_backorder"]
  10062. };
  10063. return [self offline_add2cart:[newParams mutableCopy]];
  10064. }
  10065. +(NSData*) offline_add2cartbyupc :(NSMutableDictionary *) params
  10066. {
  10067. NSString *orderCode = [params objectForKey:@"orderCode"];
  10068. NSString *upccode = [params objectForKey:@"upc_code"];
  10069. // product_name = [self translateSingleQuote:product_name];
  10070. // NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  10071. // bool search_upc = [params[@"search_upc"] boolValue];
  10072. sqlite3 *db = [iSalesDB get_db];
  10073. __block NSMutableString *product_id_string = [NSMutableString string];
  10074. // NSString *name = [product_name_array objectAtIndex:i];
  10075. NSString *sql =nil;
  10076. 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];
  10077. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10078. int product_id = sqlite3_column_int(stmt, 0);
  10079. [product_id_string appendFormat:@"%d",product_id];
  10080. }];
  10081. if (!orderCode) {
  10082. orderCode = @"";
  10083. }
  10084. NSDictionary *newParams = @{
  10085. @"product_id" : product_id_string,
  10086. @"orderCode" : orderCode,
  10087. @"can_create_backorder":params[@"can_create_backorder"]
  10088. };
  10089. [iSalesDB close_db:db];
  10090. return [self offline_add2cart:[newParams mutableCopy]];
  10091. }
  10092. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  10093. {
  10094. NSString *orderCode = [params objectForKey:@"orderCode"];
  10095. NSString *product_name = [params objectForKey:@"product_name"];
  10096. product_name = [self translateSingleQuote:product_name];
  10097. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  10098. bool search_upc = [params[@"search_upc"] boolValue];
  10099. sqlite3 *db = [iSalesDB get_db];
  10100. __block NSMutableString *product_id_string = [NSMutableString string];
  10101. for (int i = 0; i < product_name_array.count; i++) {
  10102. NSString *name = [product_name_array objectAtIndex:i];
  10103. NSString *sql =nil;
  10104. if(search_upc)
  10105. 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];
  10106. else
  10107. sql= [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  10108. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10109. int product_id = sqlite3_column_int(stmt, 0);
  10110. if (i == product_name_array.count - 1) {
  10111. [product_id_string appendFormat:@"%d",product_id];
  10112. } else {
  10113. [product_id_string appendFormat:@"%d,",product_id];
  10114. }
  10115. }];
  10116. }
  10117. if (!orderCode) {
  10118. orderCode = @"";
  10119. }
  10120. NSDictionary *newParams = @{
  10121. @"product_id" : product_id_string,
  10122. @"orderCode" : orderCode,
  10123. @"can_create_backorder":params[@"can_create_backorder"]
  10124. };
  10125. [iSalesDB close_db:db];
  10126. return [self offline_add2cart:[newParams mutableCopy]];
  10127. }
  10128. #pragma mark reset order
  10129. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  10130. {
  10131. // UIApplication * app = [UIApplication sharedApplication];
  10132. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  10133. [iSalesDB disable_trigger];
  10134. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  10135. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  10136. [iSalesDB enable_trigger];
  10137. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10138. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  10139. return [RAConvertor dict2data:dic];
  10140. }
  10141. #pragma mark submit order
  10142. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  10143. {
  10144. return [self saveorder:params submit:YES];
  10145. }
  10146. #pragma mark copy order
  10147. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  10148. {
  10149. NSMutableDictionary *ret = @{}.mutableCopy;
  10150. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  10151. sqlite3 *db = [iSalesDB get_db];
  10152. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  10153. // 首先查看联系人是否active
  10154. 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];
  10155. __block int customer_is_active = 1;
  10156. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10157. customer_is_active = sqlite3_column_int(stmt, 0);
  10158. }];
  10159. if (!customer_is_active) {
  10160. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10161. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10162. [iSalesDB close_db:db];
  10163. return [RAConvertor dict2data:ret];
  10164. }
  10165. // new order
  10166. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  10167. NSString *new_order_code = [self get_offline_soid:db];
  10168. NSString *user =params[@"user"];// ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  10169. user = [self translateSingleQuote:user];
  10170. 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
  10171. __block int result = 1;
  10172. result = [iSalesDB execSql:insert_order_sql db:db];
  10173. if (!result) {
  10174. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10175. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10176. [iSalesDB close_db:db];
  10177. return [RAConvertor dict2data:ret];
  10178. }
  10179. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  10180. // __block NSString *product_id = @"";
  10181. __weak typeof(self) weakSelf = self;
  10182. 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];
  10183. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10184. int is_active = sqlite3_column_int(stmt, 1);
  10185. if (is_active) {
  10186. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  10187. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  10188. 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];
  10189. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  10190. }
  10191. }];
  10192. // product_id = [product_id substringFromIndex:1];
  10193. //
  10194. // [self offline_add2cart:@{}.mutableCopy];
  10195. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10196. [iSalesDB close_db:db];
  10197. if (result) {
  10198. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10199. } else {
  10200. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10201. }
  10202. [ret setObject:new_order_code forKey:@"so_id"];
  10203. return [RAConvertor dict2data:ret];
  10204. }
  10205. #pragma mark move wish list to cart
  10206. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  10207. assert(params[@"can_create_backorder"]!=nil);
  10208. // cart中存在的Model在wish list move to cart之后,cart中Model数量为1
  10209. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10210. NSString *collectId = params[@"collectId"];
  10211. 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];
  10212. __block NSString *product_id = @"";
  10213. __block NSString *qty = @"";
  10214. __block int number_of_outOfStock = 0;
  10215. __block NSMutableArray *delete_collectId = [NSMutableArray array];
  10216. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10217. int productId = sqlite3_column_int(stmt, 0);
  10218. int item_qty = sqlite3_column_int(stmt, 1);
  10219. int availability = sqlite3_column_int(stmt, 2);
  10220. int _id = sqlite3_column_int(stmt, 3);
  10221. if (![params[@"can_create_backorder"] boolValue]) {
  10222. // 库存小于购买量为缺货
  10223. if (availability >= item_qty) {
  10224. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  10225. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  10226. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  10227. } else {
  10228. number_of_outOfStock++;
  10229. }
  10230. } else {
  10231. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  10232. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  10233. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  10234. }
  10235. }];
  10236. NSMutableDictionary *retDic = nil;
  10237. if (![params[@"can_create_backorder"] boolValue]) {
  10238. if (delete_collectId.count == 0) {
  10239. retDic = [NSMutableDictionary dictionary];
  10240. retDic[@"result"] = [NSNumber numberWithInteger:8];
  10241. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  10242. return [RAConvertor dict2data:retDic];
  10243. }
  10244. }
  10245. if (product_id.length > 1) {
  10246. product_id = [product_id substringFromIndex:1];
  10247. }
  10248. if (qty.length > 1) {
  10249. qty = [qty substringFromIndex:1];
  10250. }
  10251. NSString *orderCode = params[@"orderCode"];
  10252. if (!orderCode) {
  10253. orderCode = @"";
  10254. }
  10255. NSDictionary *newParams = @{
  10256. @"product_id" : product_id,
  10257. @"orderCode" : orderCode,
  10258. @"qty" : qty,
  10259. @"can_create_backorder":params[@"can_create_backorder"]
  10260. };
  10261. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  10262. retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  10263. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  10264. NSMutableDictionary* wish_return = [[self offline_deletewishlist:@{@"collectId" : [delete_collectId componentsJoinedByString:@","]}.mutableCopy] mutableCopy];
  10265. NSInteger ret = [wish_return[@"result"] intValue];
  10266. retDic[@"wish_count"]=wish_return[@"wish_count"];
  10267. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  10268. }
  10269. if (![params[@"can_create_backorder"] boolValue]) {
  10270. if (number_of_outOfStock > 0) {
  10271. retDic[@"result"] = [NSNumber numberWithInteger:8];
  10272. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  10273. }
  10274. }
  10275. return [RAConvertor dict2data:retDic];
  10276. }
  10277. #pragma mark - portfolio
  10278. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  10279. // assert(params[@"contact_id"]!=nil);
  10280. assert(params[@"user"]!=nil);
  10281. assert(params[@"can_see_price"]!=nil);
  10282. int sort = [[params valueForKey:@"sort"] intValue];
  10283. int offset = [[params valueForKey:@"offset"] intValue];
  10284. int limit = [[params valueForKey:@"limit"] intValue];
  10285. NSString *orderBy = @"";
  10286. switch (sort) {
  10287. case 0:{
  10288. orderBy = @"p.modify_time desc";
  10289. }
  10290. break;
  10291. case 1:{
  10292. orderBy = @"modify_time asc";
  10293. }
  10294. break;
  10295. case 2:{
  10296. orderBy = @"p.name asc";
  10297. }
  10298. break;
  10299. case 3:{
  10300. orderBy = @"p.name desc";
  10301. }
  10302. break;
  10303. case 4:{
  10304. orderBy = @"p.description asc";
  10305. }
  10306. break;
  10307. case 5: {
  10308. orderBy = @"m.default_category asc";
  10309. }
  10310. default:
  10311. break;
  10312. }
  10313. // 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];
  10314. 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];
  10315. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10316. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10317. sqlite3 *db = [iSalesDB get_db];
  10318. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10319. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10320. int product_id = sqlite3_column_int(stmt, 0);
  10321. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  10322. NSString *name = [self textAtColumn:1 statement:stmt];
  10323. NSString *description = [self textAtColumn:2 statement:stmt];
  10324. double price = sqlite3_column_double(stmt, 3);
  10325. double discount = sqlite3_column_double(stmt,4);
  10326. int qty = sqlite3_column_int(stmt, 5);
  10327. int percentage = sqlite3_column_int(stmt, 6);
  10328. int item_id = sqlite3_column_int(stmt, 7);
  10329. // int fashion_id = sqlite3_column_int(stmt, 8);
  10330. NSString *img_path = [self textAtColumn:9 statement:stmt];
  10331. NSString *line_note = [self textAtColumn:10 statement:stmt];
  10332. double percent = sqlite3_column_double(stmt, 11);
  10333. NSString *price_null = [self textAtColumn:3 statement:stmt];
  10334. if ([price_null isEqualToString:@"null"]) {
  10335. price = [[self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  10336. }
  10337. NSMutableDictionary *item = @{
  10338. @"linenotes": line_note,
  10339. @"check": @(1),
  10340. @"product_id": product_id_string,
  10341. @"available_qty": @(qty),
  10342. @"available_percent" : @(percent),
  10343. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  10344. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  10345. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  10346. @"img": img_path,
  10347. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  10348. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  10349. }.mutableCopy;
  10350. if (percentage) {
  10351. [item removeObjectForKey:@"available_qty"];
  10352. } else {
  10353. [item removeObjectForKey:@"available_percent"];
  10354. }
  10355. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  10356. if ([qty_null isEqualToString:@"null"]) {
  10357. [item removeObjectForKey:@"available_qty"];
  10358. }
  10359. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10360. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10361. }];
  10362. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  10363. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10364. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  10365. }
  10366. [iSalesDB close_db:db];
  10367. [dic setValue:[NSNumber numberWithBool:[params[@"can_see_price"] boolValue]] forKey:@"can_see_price"];
  10368. [dic setValue:@"" forKey:@"email_content"];
  10369. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  10370. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10371. return [RAConvertor dict2data:dic];
  10372. }
  10373. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  10374. __block int qty = 0;
  10375. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  10376. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10377. qty = sqlite3_column_int(stmt, 0);
  10378. }];
  10379. return qty;
  10380. }
  10381. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct contact_id:(NSString* ) contact_id
  10382. {
  10383. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  10384. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  10385. // NSMutableDictionary * values = params[@"replaceValue"];
  10386. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  10387. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  10388. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  10389. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  10390. NSString *pdf_path = @"";
  10391. if (direct) {
  10392. configureParams = [self valueInParams:params key:@"pdfUrl"];
  10393. } else {
  10394. pdf_path = [self valueInParams:params key:@"pdfPath"];
  10395. }
  10396. NSString *create_user = [self valueInParams:params key:@"user"];
  10397. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  10398. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  10399. // model info
  10400. // 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];
  10401. // V1.90 more color
  10402. 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];
  10403. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10404. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  10405. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10406. sqlite3 *db = [iSalesDB get_db];
  10407. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10408. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10409. int product_id = sqlite3_column_int(stmt, 0);
  10410. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  10411. double price = sqlite3_column_double(stmt, 1);
  10412. double discount = sqlite3_column_double(stmt,2);
  10413. int qty = sqlite3_column_int(stmt, 3);
  10414. int percentage = sqlite3_column_int(stmt, 4);
  10415. int item_id = sqlite3_column_int(stmt, 5);
  10416. NSString *line_note = [self textAtColumn:6 statement:stmt];
  10417. double percent = sqlite3_column_double(stmt, 7);
  10418. int more_color = sqlite3_column_int(stmt, 8);
  10419. NSString *price_null = [self textAtColumn:1 statement:stmt];
  10420. /* if ([price_null isEqualToString:@"null"]) {
  10421. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  10422. }
  10423. */
  10424. [product_ids_string appendFormat:@"%@,",product_id_string];
  10425. // Regular Price
  10426. int regular_price = [[params objectForKey:@"regular_price"] intValue];
  10427. NSString *regular_price_str = [self get_portfolio_price:contact_id item_id:item_id price:regular_price db:db];
  10428. // QTY
  10429. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  10430. if ([qty_null isEqualToString:@"null"]) {
  10431. // 查available
  10432. qty = [self model_QTY:product_id_string db:db];
  10433. }
  10434. if (percentage) {
  10435. qty = qty * percent / 100;
  10436. }
  10437. // Special Price
  10438. if ([price_null isEqualToString:@"null"]) {
  10439. // price = regular price
  10440. price = [regular_price_str doubleValue];
  10441. }
  10442. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  10443. if (![discount_null isEqualToString:@"null"]) {
  10444. price = price * (1 - discount / 100.0);
  10445. }
  10446. NSMutableDictionary *item = @{
  10447. @"line_note": line_note,
  10448. @"product_id": product_id_string,
  10449. @"available_qty": @(qty),
  10450. @"more_color":@(more_color),
  10451. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  10452. @"regular_price" : regular_price_str,
  10453. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  10454. }.mutableCopy;
  10455. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10456. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10457. }];
  10458. [iSalesDB close_db:db];
  10459. if (product_ids_string.length > 0) {
  10460. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  10461. }
  10462. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  10463. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  10464. return [RAConvertor dict2data:resultDictionary];
  10465. }
  10466. NSString *model_info = [RAConvertor dict2string:dic];
  10467. // 创建PDF
  10468. // 在preview情况下保存,则不需要新建了
  10469. if (direct) {
  10470. NSMutableDictionary *tear_sheet_params = params;
  10471. // if (tear_sheet_id) {
  10472. // tear_sheet_params = values;
  10473. // }
  10474. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  10475. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  10476. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  10477. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  10478. resultDictionary = pdfInfo.mutableCopy;
  10479. } else { // 创建PDF失败
  10480. return pdfData;
  10481. }
  10482. } else {
  10483. // pdf_path 就是本地路径
  10484. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  10485. }
  10486. // 将文件移动到PDF Cache文件夹
  10487. NSString *newPath = [pdf_path lastPathComponent];
  10488. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10489. NSString *cachefolder = [paths objectAtIndex:0];
  10490. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10491. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  10492. NSFileManager *fileManager = [NSFileManager defaultManager];
  10493. NSError *error = nil;
  10494. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  10495. if (error) { // 移动文件失败
  10496. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  10497. return [RAConvertor dict2data:resultDictionary];
  10498. }
  10499. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  10500. pdf_path = [newPath lastPathComponent];
  10501. // 保存信息
  10502. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  10503. NSString *off_params = [RAConvertor dict2string:params];
  10504. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  10505. pdf_path = [self translateSingleQuote:pdf_path];
  10506. create_user = [self translateSingleQuote:create_user];
  10507. tear_note = [self translateSingleQuote:tear_note];
  10508. tear_name = [self translateSingleQuote:tear_name];
  10509. model_info = [self translateSingleQuote:model_info];
  10510. configureParams = [self translateSingleQuote:configureParams];
  10511. off_params = [self translateSingleQuote:off_params];
  10512. 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];
  10513. if (tear_sheet_id) {
  10514. int _id = [tear_sheet_id intValue];
  10515. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  10516. }
  10517. int result = [iSalesDB execSql:save_pdf_sql];
  10518. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10519. //
  10520. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  10521. if (remove_Item) {
  10522. // portfolioId
  10523. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  10524. }
  10525. return [RAConvertor dict2data:resultDictionary];
  10526. }
  10527. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  10528. return [self offline_savePDF:params direct:YES contact_id:params[@"contact_id"]];
  10529. }
  10530. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  10531. return [self offline_savePDF:params direct:NO contact_id:params[@"contact_id"]];
  10532. }
  10533. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  10534. int offset = [[params valueForKey:@"offset"] intValue];
  10535. int limit = [[params valueForKey:@"limit"] intValue];
  10536. NSString *keyword = [params valueForKey:@"keyWord"];
  10537. NSString *where = @"where is_delete is null or is_delete = 0";
  10538. if (keyword.length) {
  10539. keyword = [self translateSingleQuote:keyword];
  10540. 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];
  10541. }
  10542. 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
  10543. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10544. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10545. NSString *cachefolder = [paths objectAtIndex:0];
  10546. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10547. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10548. NSString *name = [self textAtColumn:0 statement:stmt];
  10549. NSString *note = [self textAtColumn:1 statement:stmt];
  10550. NSString *time = [self textAtColumn:2 statement:stmt];
  10551. NSString *user = [self textAtColumn:3 statement:stmt];
  10552. NSString *path = [self textAtColumn:4 statement:stmt];
  10553. NSString *model_info = [self textAtColumn:6 statement:stmt];
  10554. NSString *off_params = [self textAtColumn:7 statement:stmt];
  10555. path = [pdfFolder stringByAppendingPathComponent:path];
  10556. BOOL bdir=NO;
  10557. NSFileManager* fileManager = [NSFileManager defaultManager];
  10558. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  10559. {
  10560. //pdf文件不存在
  10561. path=nil;
  10562. }
  10563. time = [self changeDateTimeFormate:time];
  10564. time = [time stringByAppendingString:@" PST"];
  10565. int sheet_id = sqlite3_column_int(stmt, 5);
  10566. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  10567. item[@"tearsheetsId"]=@(sheet_id);
  10568. item[@"pdf_path"]=path;
  10569. item[@"create_time"]=time;
  10570. item[@"create_user"]=user;
  10571. item[@"tear_note"]=note;
  10572. item[@"tear_name"]=name;
  10573. item[@"isLocalFile"]=[NSNumber numberWithBool:YES];
  10574. item[@"model_info"]=model_info;
  10575. item[@"off_params"]=off_params;
  10576. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10577. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10578. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10579. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10580. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  10581. }];
  10582. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10583. return [RAConvertor dict2data:dic];
  10584. }
  10585. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  10586. NSString *prodct_ids = [params objectForKey:@"product_id"];
  10587. // NSString *user = [params objectForKey:@"user"];
  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 (select count(0) from offline_portfolio where product_id in (%@) and (is_delete is null or is_delete = 0)) = 0);",prodct_ids,prodct_ids];
  10589. NSString *sql = [NSString stringWithFormat:@"insert into offline_portfolio (product_id,name,description,item_id) select * from (select p.product_id,p.name,p.description,p.item_id from product as p where p.product_id in (%@) and 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];
  10590. int result = [iSalesDB execSql:sql];
  10591. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10592. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  10593. if (result == RESULT_TRUE) {
  10594. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  10595. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  10596. sqlite3 *db = [iSalesDB get_db];
  10597. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  10598. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  10599. [iSalesDB close_db:db];
  10600. }
  10601. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10602. return [RAConvertor dict2data:dic];
  10603. }
  10604. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  10605. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  10606. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  10607. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  10608. NSMutableDictionary *section_0 = [[dic objectForKey:@"section_0"] mutableCopy];
  10609. NSDictionary *company_item = @{
  10610. @"control": @"text",
  10611. @"keyboard": @"text",
  10612. @"name": @"company_name",
  10613. @"value": COMPANY_FULL_NAME,
  10614. @"aname": @"Company Name"
  10615. };
  10616. [section_0 setObject:company_item forKey:@"item_0"];
  10617. [dic setObject:section_0 forKey:@"section_0"];
  10618. // Regurlar Price
  10619. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  10620. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  10621. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  10622. __block long val_count = 0;
  10623. NSString *sql = @"select name,type,order_by from price order by order_by";
  10624. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10625. NSString *name = [self textAtColumn:0 statement:stmt];
  10626. int type = sqlite3_column_int(stmt, 1);
  10627. int order_by = sqlite3_column_int(stmt, 2);
  10628. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10629. NSDictionary *price_dic = @{
  10630. @"value" : name,
  10631. @"value_id" : [NSNumber numberWithInteger:type],
  10632. @"check" : order_by == 0 ? @(1) : @(0)
  10633. };
  10634. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  10635. val_count = ++(*count);
  10636. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10637. val_count = 0;
  10638. }];
  10639. [cadedate setObject:@{@"value" : @"None",
  10640. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  10641. val_count++;
  10642. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  10643. [price setObject:cadedate forKey:@"cadedate"];
  10644. [section1 setObject:price forKey:@"item_2"];
  10645. [dic setObject:section1 forKey:@"section_1"];
  10646. return [RAConvertor dict2data:dic];
  10647. }
  10648. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  10649. NSString *product_id = [params objectForKey:@"fashionId"];
  10650. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  10651. __block int result = RESULT_TRUE;
  10652. __block int qty = 0;
  10653. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10654. qty = sqlite3_column_int(stmt, 0);
  10655. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10656. result = RESULT_FALSE;
  10657. }];
  10658. NSMutableDictionary *dic = @{
  10659. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  10660. @"mode" : @"Regular Mode",
  10661. @"quantity_available" : @(qty),
  10662. @"result" : @(result),
  10663. }.mutableCopy;
  10664. return [RAConvertor dict2data:dic];
  10665. }
  10666. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  10667. NSString *item_ids = [params objectForKey:@"item_id"];
  10668. NSString *line_notes = [params objectForKey:@"notes"];
  10669. NSString *price_str = [params objectForKey:@"price"];
  10670. NSString *discount_str = [params objectForKey:@"discount"];
  10671. NSString *percent = [params objectForKey:@"available_percent"];
  10672. NSString *qty = [params objectForKey:@"available_qty"];
  10673. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  10674. int dot = 0;
  10675. if (line_notes) {
  10676. line_notes = [self translateSingleQuote:line_notes];
  10677. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  10678. sql = [sql stringByAppendingString:line_notes];
  10679. dot = 1;
  10680. } else {
  10681. line_notes = @"";
  10682. }
  10683. if (price_str) {
  10684. if (dot) {
  10685. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  10686. } else {
  10687. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  10688. dot = 1;
  10689. }
  10690. sql = [sql stringByAppendingString:price_str];
  10691. } else {
  10692. price_str = @"";
  10693. }
  10694. if (discount_str) {
  10695. if (dot) {
  10696. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  10697. } else {
  10698. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  10699. dot = 1;
  10700. }
  10701. sql = [sql stringByAppendingString:discount_str];
  10702. } else {
  10703. discount_str = @"";
  10704. }
  10705. if (percent) {
  10706. if (dot) {
  10707. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  10708. } else {
  10709. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  10710. dot = 1;
  10711. }
  10712. sql = [sql stringByAppendingString:percent];
  10713. } else {
  10714. percent = @"";
  10715. }
  10716. if (qty) {
  10717. if (dot) {
  10718. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  10719. } else {
  10720. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  10721. dot = 1;
  10722. }
  10723. sql = [sql stringByAppendingString:qty];
  10724. } else {
  10725. qty = @"";
  10726. }
  10727. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  10728. sql = [sql stringByAppendingString:where];
  10729. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10730. int result = [iSalesDB execSql:sql];
  10731. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10732. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10733. return [RAConvertor dict2data:dic];
  10734. }
  10735. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  10736. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  10737. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10738. sqlite3 *db = [iSalesDB get_db];
  10739. 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];
  10740. int result = [iSalesDB execSql:sql db:db];
  10741. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10742. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10743. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  10744. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  10745. [iSalesDB close_db:db];
  10746. return [RAConvertor dict2data:dic];
  10747. }
  10748. + (void)offline_removePDFWithName:(NSString *)name {
  10749. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10750. NSString *cachefolder = [paths objectAtIndex:0];
  10751. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10752. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  10753. NSFileManager *fileManager = [NSFileManager defaultManager];
  10754. [fileManager removeItemAtPath:path error:nil];
  10755. }
  10756. + (void)offline_clear_PDFCache {
  10757. NSFileManager *fileManager = [NSFileManager defaultManager];
  10758. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10759. NSString *cachefolder = [paths objectAtIndex:0];
  10760. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10761. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  10762. for (NSString *path in pdf_files) {
  10763. [self offline_removePDFWithName:[path lastPathComponent]];
  10764. }
  10765. }
  10766. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  10767. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10768. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  10769. NSString *user = [params objectForKey:@"user"];
  10770. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %ld;",(long)tearsheetsId]]; // tearsheets_id ---> _id
  10771. if (![create_user isEqualToString:user]) {
  10772. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  10773. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10774. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  10775. return [RAConvertor dict2data:dic];
  10776. }
  10777. 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]];
  10778. __block int is_local = 0;
  10779. __block NSString *path = @"";
  10780. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10781. is_local = sqlite3_column_int(stmt, 0);
  10782. path = [self textAtColumn:1 statement:stmt];
  10783. }];
  10784. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  10785. if (is_local == 1) {
  10786. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  10787. // 删除文件
  10788. [self offline_removePDFWithName:path];
  10789. }
  10790. int result = [iSalesDB execSql:sql];
  10791. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10792. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10793. return [RAConvertor dict2data:dic];
  10794. }
  10795. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial params:(NSMutableDictionary*) add_params
  10796. {
  10797. // assert(add_params[@"contact_id"]!=nil);
  10798. assert(add_params[@"user"]!=nil);
  10799. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10800. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  10801. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  10802. NSString* where=@"1=1";
  10803. if (ver!=nil) {
  10804. where=@"is_dirty=1";
  10805. }
  10806. 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];
  10807. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  10808. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10809. sqlite3 *db = [iSalesDB get_db];
  10810. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10811. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  10812. NSInteger _id = sqlite3_column_int(stmt, 0);
  10813. NSInteger product_id = sqlite3_column_int(stmt, 1);
  10814. NSString *name = [self textAtColumn:2 statement:stmt];
  10815. NSString *desc = [self textAtColumn:3 statement:stmt];
  10816. NSInteger item_id = sqlite3_column_int(stmt, 4);
  10817. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  10818. NSInteger qty = sqlite3_column_int(stmt, 6);
  10819. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  10820. double percent = sqlite3_column_double(stmt, 8);
  10821. double price = sqlite3_column_double(stmt, 9);
  10822. double discount = sqlite3_column_double(stmt, 10);
  10823. NSString *img = [self textAtColumn:11 statement:stmt];
  10824. NSString *line_note = [self textAtColumn:12 statement:stmt];
  10825. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  10826. NSString *create_time = [self textAtColumn:14 statement:stmt];
  10827. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  10828. NSString *price_null = [self textAtColumn:9 statement:stmt];
  10829. if ([price_null isEqualToString:@"null"]) {
  10830. price = [[self get_model_default_price:add_params[@"contact_id"] user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  10831. }
  10832. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  10833. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  10834. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  10835. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  10836. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  10837. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  10838. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  10839. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  10840. if ([qty_null isEqualToString:@"null"]) {
  10841. // [item setValue:@"null" forKey:@"available_qty"];
  10842. } else {
  10843. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  10844. }
  10845. if ([is_percent_null isEqualToString:@"null"]) {
  10846. // [item setValue:@"null" forKey:@"percentage"];
  10847. } else {
  10848. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  10849. }
  10850. if ([percent_null isEqualToString:@"null"]) {
  10851. // [item setValue:@"null" forKey:@"percent"];
  10852. } else {
  10853. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  10854. }
  10855. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  10856. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  10857. [item setValue:name forKey:@"name"];
  10858. [item setValue:desc forKey:@"description"];
  10859. [item setValue:img forKey:@"img"];
  10860. [item setValue:line_note forKey:@"line_note"];
  10861. [item setValue:create_time forKey:@"createtime"];
  10862. [item setValue:modify_time forKey:@"modifytime"];
  10863. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  10864. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10865. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  10866. }];
  10867. [iSalesDB close_db:db];
  10868. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  10869. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10870. }
  10871. return ret;
  10872. }
  10873. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  10874. {
  10875. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10876. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  10877. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  10878. NSString* where=@"1=1";
  10879. if (ver!=nil) {
  10880. where=@"is_dirty=1";
  10881. }
  10882. 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];
  10883. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  10884. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10885. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  10886. NSInteger _id = sqlite3_column_int(stmt, 0);
  10887. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  10888. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  10889. NSString *create_user = [self textAtColumn:3 statement:stmt];
  10890. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  10891. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  10892. NSString *model_info = [self textAtColumn:6 statement:stmt];
  10893. NSString *createtime = [self textAtColumn:7 statement:stmt];
  10894. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  10895. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  10896. NSString *off_params = [self textAtColumn:10 statement:stmt];
  10897. NSString *uuid = [NSUUID UUID].UUIDString;
  10898. int is_delete = sqlite3_column_int(stmt, 11);
  10899. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  10900. int is_local = sqlite3_column_int(stmt, 12);
  10901. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  10902. if (![tearsheet_id_null isEqualToString:@"null"]) {
  10903. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  10904. }
  10905. [item setObject:pdf_path forKey:@"pdf_path"];
  10906. [item setObject:create_user forKey:@"create_user"];
  10907. [item setObject:tear_note forKey:@"tear_note"];
  10908. [item setObject:tear_name forKey:@"tear_name"];
  10909. [item setObject:model_info forKey:@"model_info"];
  10910. [item setObject:createtime forKey:@"createtime"];
  10911. [item setObject:modifytime forKey:@"modifytime"];
  10912. [item setObject:urlParams forKey:@"urlParams"];
  10913. [item setObject:off_params forKey:@"off_params"];
  10914. [item setObject:uuid forKey:@"pdf_token"];
  10915. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  10916. NSDictionary *off_Params_dic = [RAConvertor string2dict:off_params];
  10917. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  10918. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  10919. } else {
  10920. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  10921. }
  10922. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10923. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  10924. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10925. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10926. }];
  10927. return ret;
  10928. }
  10929. #pragma mark 2020
  10930. +(void) offline_contactlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  10931. {
  10932. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  10933. sqlite3 *db = [iSalesDB get_db];
  10934. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  10935. if (contactType) {
  10936. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  10937. } else {
  10938. contactType = @"1 = 1";
  10939. }
  10940. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  10941. DebugLog(@"offline contact list keyword: %@",keyword);
  10942. // advanced search
  10943. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  10944. if (contact_name) {
  10945. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10946. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  10947. } else {
  10948. contact_name = @"";
  10949. }
  10950. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  10951. if (customer_phone) {
  10952. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10953. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  10954. } else {
  10955. customer_phone = @"";
  10956. }
  10957. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  10958. if (customer_fax) {
  10959. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10960. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  10961. } else {
  10962. customer_fax = @"";
  10963. }
  10964. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  10965. if (customer_zipcode) {
  10966. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10967. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  10968. } else {
  10969. customer_zipcode = @"";
  10970. }
  10971. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  10972. if (customer_sales_rep) {
  10973. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10974. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  10975. } else {
  10976. customer_sales_rep = @"";
  10977. }
  10978. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  10979. if (customer_state) {
  10980. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10981. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  10982. } else {
  10983. customer_state = @"";
  10984. }
  10985. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  10986. if (customer_name) {
  10987. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10988. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  10989. } else {
  10990. customer_name = @"";
  10991. }
  10992. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  10993. if (customer_country) {
  10994. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  10995. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  10996. } else {
  10997. customer_country = @"";
  10998. }
  10999. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  11000. if (customer_cid) {
  11001. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11002. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  11003. } else {
  11004. customer_cid = @"";
  11005. }
  11006. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  11007. if (customer_city) {
  11008. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11009. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  11010. } else {
  11011. customer_city = @"";
  11012. }
  11013. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  11014. if (customer_address) {
  11015. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11016. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  11017. } else {
  11018. customer_address = @"";
  11019. }
  11020. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  11021. if (customer_email) {
  11022. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11023. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  11024. } else {
  11025. customer_email = @"";
  11026. }
  11027. NSString *price_name = [params valueForKey:@"price_name"];
  11028. if (price_name) {
  11029. if ([price_name containsString:@","]) {
  11030. // 首先从 price表中查处name
  11031. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  11032. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  11033. for (int i = 1;i < pArray.count;i++) {
  11034. NSString *p = pArray[i];
  11035. [mutablePStr appendFormat:@" or type = %@ ",p];
  11036. }
  11037. [mutablePStr appendString:@";"];
  11038. __block NSMutableArray *price_name_array = [NSMutableArray array];
  11039. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11040. char *name = (char *)sqlite3_column_text(stmt, 0);
  11041. if (!name)
  11042. name = "";
  11043. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  11044. }];
  11045. // 再根据name 拼sql
  11046. NSMutableString *mutable_price_name = [NSMutableString string];
  11047. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  11048. for (int i = 1; i < price_name_array.count; i++) {
  11049. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  11050. }
  11051. [mutable_price_name appendString:@")"];
  11052. price_name = mutable_price_name;
  11053. } else {
  11054. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  11055. if ([price_name isEqualToString:@""]) {
  11056. price_name = @"";
  11057. } else {
  11058. __block NSString *price;
  11059. price_name = [self translateSingleQuote:price_name];
  11060. [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) {
  11061. char *p = (char *)sqlite3_column_text(stmt, 0);
  11062. if (p == NULL) {
  11063. p = "";
  11064. }
  11065. price = [NSString stringWithUTF8String:p];
  11066. }];
  11067. if ([price isEqualToString:@""]) {
  11068. price_name = @"";
  11069. } else {
  11070. price = [self translateSingleQuote:price];
  11071. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  11072. }
  11073. }
  11074. }
  11075. } else {
  11076. price_name = @"";
  11077. }
  11078. int limit = [[params valueForKey:@"limit"] intValue];
  11079. int offset = [[params valueForKey:@"offset"] intValue];
  11080. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11081. 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];
  11082. 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];
  11083. // int result= [iSalesDB AddExFunction:db];
  11084. int count =0;
  11085. NSString *sqlQuery = nil;
  11086. if(keyword.length==0)
  11087. {
  11088. // sqlQuery=[NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email from offline_contact where %@=1 order by decrypt(company_name) limit %d offset %d",contactType , limit, offset];
  11089. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  11090. sqlQuery = sql;
  11091. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  11092. }
  11093. else
  11094. {
  11095. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  11096. keyword = keyword.lowercaseString;
  11097. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  11098. 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];
  11099. 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]];
  11100. }
  11101. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  11102. sqlite3_stmt * statement;
  11103. [ret setValue:@"2" forKey:@"result"];
  11104. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  11105. // 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";
  11106. int i = 0;
  11107. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11108. {
  11109. while (sqlite3_step(statement) == SQLITE_ROW)
  11110. {
  11111. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  11112. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11113. int editable = sqlite3_column_int(statement, 0);
  11114. char *company_name = (char*)sqlite3_column_text(statement, 1);
  11115. NSString *nscompany_name =nil;
  11116. if(company_name==nil)
  11117. nscompany_name=@"";
  11118. else
  11119. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  11120. char *country = (char*)sqlite3_column_text(statement, 2);
  11121. if(country==nil)
  11122. country="";
  11123. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  11124. // char *addr = (char*)sqlite3_column_text(statement, 3);
  11125. // if(addr==nil)
  11126. // addr="";
  11127. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  11128. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  11129. if(zipcode==nil)
  11130. zipcode="";
  11131. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  11132. char *state = (char*)sqlite3_column_text(statement, 5);
  11133. if(state==nil)
  11134. state="";
  11135. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  11136. char *city = (char*)sqlite3_column_text(statement, 6);
  11137. if(city==nil)
  11138. city="";
  11139. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  11140. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  11141. // NSString *nscontact_name = nil;
  11142. // if(contact_name==nil)
  11143. // nscontact_name=@"";
  11144. // else
  11145. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  11146. char *phone = (char*)sqlite3_column_text(statement, 8);
  11147. NSString *nsphone = nil;
  11148. if(phone==nil)
  11149. nsphone=@"";
  11150. else
  11151. nsphone= [[NSString alloc]initWithUTF8String:phone];
  11152. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  11153. if(contact_id==nil)
  11154. contact_id="";
  11155. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  11156. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  11157. if(addr_1==nil)
  11158. addr_1="";
  11159. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  11160. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  11161. if(addr_2==nil)
  11162. addr_2="";
  11163. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  11164. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  11165. if(addr_3==nil)
  11166. addr_3="";
  11167. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  11168. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  11169. if(addr_4==nil)
  11170. addr_4="";
  11171. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  11172. char *first_name = (char*)sqlite3_column_text(statement, 14);
  11173. if(first_name==nil)
  11174. first_name="";
  11175. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  11176. char *last_name = (char*)sqlite3_column_text(statement, 15);
  11177. if(last_name==nil)
  11178. last_name="";
  11179. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  11180. char *fax = (char*)sqlite3_column_text(statement, 16);
  11181. NSString *nsfax = nil;
  11182. if(fax==nil)
  11183. nsfax=@"";
  11184. else
  11185. {
  11186. nsfax= [[NSString alloc]initWithUTF8String:fax];
  11187. if(nsfax.length>0)
  11188. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  11189. }
  11190. char *email = (char*)sqlite3_column_text(statement, 17);
  11191. NSString *nsemail = nil;
  11192. if(email==nil)
  11193. nsemail=@"";
  11194. else
  11195. {
  11196. nsemail= [[NSString alloc]initWithUTF8String:email];
  11197. if(nsemail.length>0)
  11198. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  11199. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  11200. }
  11201. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  11202. [arr_name addObject:nsfirst_name];
  11203. [arr_name addObject:nslast_name];
  11204. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  11205. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  11206. {
  11207. // decrypt
  11208. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  11209. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  11210. nsphone=[AESCrypt fastdecrypt:nsphone];
  11211. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11212. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  11213. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  11214. }
  11215. [arr_addr addObject:nscompany_name];
  11216. [arr_addr addObject:nscontact_name];
  11217. [arr_addr addObject:@"<br>"];
  11218. [arr_addr addObject:nsaddr_1];
  11219. [arr_addr addObject:nsaddr_2];
  11220. [arr_addr addObject:nsaddr_3];
  11221. [arr_addr addObject:nsaddr_4];
  11222. //[arr_addr addObject:nsaddr];
  11223. [arr_addr addObject:nszipcode];
  11224. [arr_addr addObject:nscity];
  11225. [arr_addr addObject:nsstate];
  11226. [arr_addr addObject:nscountry];
  11227. [arr_addr addObject:@"<br>"];
  11228. [arr_addr addObject:nsphone];
  11229. [arr_addr addObject:nsfax];
  11230. [arr_addr addObject:nsemail];
  11231. NSString * name = [RAConvertor arr2string:arr_addr separator:@", " trim:true];
  11232. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  11233. [item setValue:name forKey:@"name"];
  11234. [item setValue:nscontact_id forKey:@"contact_id"];
  11235. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  11236. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11237. i++;
  11238. }
  11239. sqlite3_finalize(statement);
  11240. }
  11241. [iSalesDB close_db:db];
  11242. #ifdef DEBUG
  11243. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11244. #endif
  11245. dispatch_async(dispatch_get_main_queue(), ^{
  11246. UIApplication * app = [UIApplication sharedApplication];
  11247. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11248. [ret setValue:appDelegate.mode forKey:@"mode"];
  11249. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  11250. result(ret);
  11251. });
  11252. return ;
  11253. });
  11254. }
  11255. + (void)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11256. {
  11257. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  11258. NSString* order_code= appDelegate.order_code;
  11259. NSString* user = appDelegate.user;
  11260. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11261. NSString* category = [params valueForKey:@"category"];
  11262. if (!category || [category isEqualToString:@""]) {
  11263. category = @"%";
  11264. }
  11265. category = [self translateSingleQuote:category];
  11266. int limit = [[params valueForKey:@"limit"] intValue];
  11267. int offset = [[params valueForKey:@"offset"] intValue];
  11268. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11269. NSString *limit_str = @"";
  11270. if (limited) {
  11271. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  11272. }
  11273. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  11274. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  11275. sqlite3 *db = [iSalesDB get_db];
  11276. // [iSalesDB AddExFunction:db];
  11277. int count;
  11278. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  11279. 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];
  11280. double price_min = 0;
  11281. double price_max = 0;
  11282. if ([params.allKeys containsObject:@"alert"]) {
  11283. // alert
  11284. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  11285. NSString *alert = params[@"alert"];
  11286. if ([alert isEqualToString:@"Display All"]) {
  11287. alert = [NSString stringWithFormat:@""];
  11288. } else {
  11289. alert = [self translateSingleQuote:alert];
  11290. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  11291. }
  11292. // available
  11293. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  11294. NSString *available = params[@"available"];
  11295. NSString *available_condition;
  11296. if ([available isEqualToString:@"Display All"]) {
  11297. available_condition = @"";
  11298. } else if ([available isEqualToString:@"Available Now"]) {
  11299. available_condition = @"and availability > 0";
  11300. } else {
  11301. available_condition = @"and availability == 0";
  11302. }
  11303. // best seller
  11304. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  11305. NSString *best_seller = @"";
  11306. NSString *order_best_seller = @"m.name asc";
  11307. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  11308. best_seller = @"and best_seller > 0";
  11309. order_best_seller = @"m.best_seller desc,m.name asc";
  11310. }
  11311. // price
  11312. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  11313. NSString *price = params[@"price"];
  11314. price_min = 0;
  11315. price_max = MAXFLOAT;
  11316. if (user && price != nil) {
  11317. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  11318. NSMutableString *priceName = [NSMutableString string];
  11319. for (int i = 0; i < priceTypeArray.count; i++) {
  11320. NSString *pricetype = priceTypeArray[i];
  11321. pricetype = [self translateSingleQuote:pricetype];
  11322. if (i == 0) {
  11323. [priceName appendFormat:@"'%@'",pricetype];
  11324. } else {
  11325. [priceName appendFormat:@",'%@'",pricetype];
  11326. }
  11327. }
  11328. if ([price isEqualToString:@"Display All"]) {
  11329. price = [NSString stringWithFormat:@""];
  11330. } else if([price containsString:@"+"]){
  11331. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  11332. price_min = [price doubleValue];
  11333. 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];
  11334. } else {
  11335. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  11336. price_min = [[priceArray objectAtIndex:0] doubleValue];
  11337. price_max = [[priceArray objectAtIndex:1] doubleValue];
  11338. 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];
  11339. }
  11340. } else {
  11341. price = @"";
  11342. }
  11343. // sold_by_qty : Sold in quantities of %@
  11344. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  11345. NSString *qty = params[@"sold_by_qty"];
  11346. if ([qty isEqualToString:@"Display All"]) {
  11347. qty = @"";
  11348. } else {
  11349. qty = [self translateSingleQuote:qty];
  11350. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  11351. }
  11352. // cate
  11353. // category = [self translateSingleQuote:category];
  11354. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  11355. // cate mutiple selection
  11356. NSString *category_id = params[@"category"];
  11357. NSMutableArray *cate_id_array = nil;
  11358. NSMutableString *cateWhere = [NSMutableString string];
  11359. if ([category_id isEqualToString:@""] || !category_id) {
  11360. [cateWhere appendString:@"1 = 1"];
  11361. } else {
  11362. if ([category_id containsString:@","]) {
  11363. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  11364. } else {
  11365. cate_id_array = [@[category_id] mutableCopy];
  11366. }
  11367. [cateWhere appendString:@"("];
  11368. for (int i = 0; i < cate_id_array.count; i++) {
  11369. if (i == 0) {
  11370. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  11371. } else {
  11372. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  11373. }
  11374. }
  11375. [cateWhere appendString:@")"];
  11376. }
  11377. // where bestseller > 0 order by bestseller desc
  11378. // sql query: alert availability(int) best_seller(int) price qty
  11379. 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];
  11380. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  11381. }
  11382. DebugLog(@"offline category where: %@",where);
  11383. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  11384. if (!user) {
  11385. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  11386. }
  11387. [ret setValue:filter forKey:@"filter"];
  11388. DebugLog(@"offline_category sql:%@",sqlQuery);
  11389. sqlite3_stmt * statement;
  11390. [ret setValue:@"2" forKey:@"result"];
  11391. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11392. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11393. // int count=0;
  11394. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11395. {
  11396. int i=0;
  11397. while (sqlite3_step(statement) == SQLITE_ROW)
  11398. {
  11399. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11400. char *name = (char*)sqlite3_column_text(statement, 0);
  11401. if(name==nil)
  11402. name="";
  11403. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11404. char *description = (char*)sqlite3_column_text(statement, 1);
  11405. if(description==nil)
  11406. description="";
  11407. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11408. int product_id = sqlite3_column_int(statement, 2);
  11409. int wid = sqlite3_column_int(statement, 3);
  11410. int closeout = sqlite3_column_int(statement, 4);
  11411. int cid = sqlite3_column_int(statement, 5);
  11412. int wisdelete = sqlite3_column_int(statement, 6);
  11413. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  11414. int more_color = sqlite3_column_int(statement, 8);
  11415. // Defaul Category ID
  11416. __block NSString *categoryID = nil;
  11417. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  11418. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11419. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  11420. if(default_category==nil)
  11421. default_category="";
  11422. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  11423. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  11424. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  11425. categoryID = nsdefault_category;
  11426. }];
  11427. if (!categoryID.length) {
  11428. NSString *cateIDs = params[@"category"];
  11429. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  11430. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  11431. for (NSString *cateID in requestCategoryArr) {
  11432. BOOL needBreak = NO;
  11433. for (NSString *itemCateIDBox in itemCategoryArr) {
  11434. if (itemCateIDBox.length > 4) {
  11435. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  11436. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  11437. if ([itemCategoryID isEqualToString:cateID]) {
  11438. needBreak = YES;
  11439. categoryID = itemCategoryID;
  11440. break;
  11441. }
  11442. }
  11443. }
  11444. if (needBreak) {
  11445. break;
  11446. }
  11447. }
  11448. }
  11449. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11450. if(wid !=0 && wisdelete != 1)
  11451. [item setValue:@"true" forKey:@"wish_exists"];
  11452. else
  11453. [item setValue:@"false" forKey:@"wish_exists"];
  11454. if(closeout==0)
  11455. [item setValue:@"false" forKey:@"is_closeout"];
  11456. else
  11457. [item setValue:@"true" forKey:@"is_closeout"];
  11458. if(cid==0)
  11459. [item setValue:@"false" forKey:@"cart_exists"];
  11460. else
  11461. [item setValue:@"true" forKey:@"cart_exists"];
  11462. if (more_color == 0) {
  11463. [item setObject:@(false) forKey:@"more_color"];
  11464. } else if (more_color == 1) {
  11465. [item setObject:@(true) forKey:@"more_color"];
  11466. }
  11467. [item addEntriesFromDictionary:imgjson];
  11468. // [item setValue:nsurl forKey:@"img"];
  11469. [item setValue:nsname forKey:@"name"];
  11470. [item setValue:nsdescription forKey:@"description"];
  11471. if (categoryID) {
  11472. [item setValue:categoryID forKey:@"item_category_id"];
  11473. }
  11474. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11475. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11476. i++;
  11477. }
  11478. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11479. [ret setObject:items forKey:@"items"];
  11480. sqlite3_finalize(statement);
  11481. } else {
  11482. DebugLog(@"nothing...");
  11483. }
  11484. DebugLog(@"count:%d",count);
  11485. [iSalesDB close_db:db];
  11486. #ifdef DEBUG
  11487. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11488. #endif
  11489. dispatch_async(dispatch_get_main_queue(), ^{
  11490. // UIApplication * app = [UIApplication sharedApplication];
  11491. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11492. // [ret setValue:appDelegate.mode forKey:@"mode"];
  11493. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  11494. result(ret);
  11495. });
  11496. });
  11497. // NSString* orderCode = [params valueForKey:@"orderCode"];
  11498. }
  11499. +(void) offline_category :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11500. {
  11501. [self categoryList:params limited:YES completionHandler:result];
  11502. }
  11503. + (void) search:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  11504. {
  11505. UIApplication * app = [UIApplication sharedApplication];
  11506. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11507. NSString* order_code = appDelegate.order_code;
  11508. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11509. NSString* keyword = [params valueForKey:@"keyword"];
  11510. keyword=keyword.lowercaseString;
  11511. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  11512. int limit = [[params valueForKey:@"limit"] intValue];
  11513. int offset = [[params valueForKey:@"offset"] intValue];
  11514. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11515. NSString *limit_str = @"";
  11516. if (limited) {
  11517. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  11518. }
  11519. sqlite3 *db = [iSalesDB get_db];
  11520. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  11521. NSString *sqlQuery = nil;
  11522. if(exactMatch )
  11523. 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 ;
  11524. else
  11525. 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
  11526. DebugLog(@"offline_search sql:%@",sqlQuery);
  11527. sqlite3_stmt * statement;
  11528. [ret setValue:@"2" forKey:@"result"];
  11529. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11530. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11531. // int count=0;
  11532. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11533. {
  11534. int i=0;
  11535. while (sqlite3_step(statement) == SQLITE_ROW)
  11536. {
  11537. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11538. // char *name = (char*)sqlite3_column_text(statement, 1);
  11539. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  11540. char *name = (char*)sqlite3_column_text(statement, 0);
  11541. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11542. char *description = (char*)sqlite3_column_text(statement, 1);
  11543. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11544. int product_id = sqlite3_column_int(statement, 2);
  11545. // char *url = (char*)sqlite3_column_text(statement, 3);
  11546. // if(url==nil)
  11547. // url="";
  11548. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  11549. int wid = sqlite3_column_int(statement, 3);
  11550. int closeout = sqlite3_column_int(statement, 4);
  11551. int cid = sqlite3_column_int(statement, 5);
  11552. int wisdelete = sqlite3_column_int(statement, 6);
  11553. int more_color = sqlite3_column_int(statement, 7);
  11554. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11555. if(wid !=0 && wisdelete != 1)
  11556. [item setValue:@"true" forKey:@"wish_exists"];
  11557. else
  11558. [item setValue:@"false" forKey:@"wish_exists"];
  11559. if(closeout==0)
  11560. [item setValue:@"false" forKey:@"is_closeout"];
  11561. else
  11562. [item setValue:@"true" forKey:@"is_closeout"];
  11563. if(cid==0)
  11564. [item setValue:@"false" forKey:@"cart_exists"];
  11565. else
  11566. [item setValue:@"true" forKey:@"cart_exists"];
  11567. if (more_color == 0) {
  11568. [item setObject:@(false) forKey:@"more_color"];
  11569. } else if (more_color == 1) {
  11570. [item setObject:@(true) forKey:@"more_color"];
  11571. }
  11572. [item addEntriesFromDictionary:imgjson];
  11573. // [item setValue:nsurl forKey:@"img"];
  11574. [item setValue:nsname forKey:@"fash_name"];
  11575. [item setValue:nsdescription forKey:@"description"];
  11576. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11577. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11578. i++;
  11579. }
  11580. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11581. [ret setObject:items forKey:@"items"];
  11582. sqlite3_finalize(statement);
  11583. }
  11584. DebugLog(@"count:%d",count);
  11585. [iSalesDB close_db:db];
  11586. #ifdef DEBUG
  11587. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11588. #endif
  11589. dispatch_async(dispatch_get_main_queue(), ^{
  11590. result(ret);
  11591. });
  11592. });
  11593. }
  11594. +(void) offline_search:(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11595. {
  11596. [self search:params limited:YES completionHandler:result];
  11597. }
  11598. +(void) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11599. {
  11600. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11601. NSString* offline_command=params[@"offline_Command"];
  11602. NSMutableDictionary* ret=nil;
  11603. if([offline_command isEqualToString:@"model_NIYMAL"])
  11604. {
  11605. NSString* category = params[@"category"];
  11606. ret = [[self refresh_model_NIYMAL:category] mutableCopy];
  11607. }
  11608. dispatch_async(dispatch_get_main_queue(), ^{
  11609. result(ret);
  11610. });
  11611. });
  11612. }
  11613. +(void) offline_wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  11614. {
  11615. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11616. int sort = [[params objectForKey:@"sort"] intValue];
  11617. NSString *sort_str = @"";
  11618. switch (sort) {
  11619. case 0:{
  11620. sort_str = @"order by w.modify_time desc";
  11621. }
  11622. break;
  11623. case 1:{
  11624. sort_str = @"order by w.modify_time asc";
  11625. }
  11626. break;
  11627. case 2:{
  11628. sort_str = @"order by m.name asc";
  11629. }
  11630. break;
  11631. case 3:{
  11632. sort_str = @"order by m.name desc";
  11633. }
  11634. break;
  11635. case 4:{
  11636. sort_str = @"order by m.description asc";
  11637. }
  11638. break;
  11639. default:
  11640. break;
  11641. }
  11642. // NSString* user = appDelegate.user;
  11643. sqlite3 *db = [iSalesDB get_db];
  11644. // order by w.create_time
  11645. 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];
  11646. // 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];
  11647. sqlite3_stmt * statement;
  11648. NSDate *date1 = [NSDate date];
  11649. // NSDate *date2 = nil;
  11650. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  11651. int count=0;
  11652. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11653. {
  11654. while (sqlite3_step(statement) == SQLITE_ROW)
  11655. {
  11656. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  11657. int product_id = sqlite3_column_double(statement, 0);
  11658. char *description = (char*)sqlite3_column_text(statement, 1);
  11659. if(description==nil)
  11660. description= "";
  11661. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  11662. int item_id = sqlite3_column_double(statement, 2);
  11663. NSDate *date_image = [NSDate date];
  11664. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  11665. printf("image : ");
  11666. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  11667. // char *url = (char*)sqlite3_column_text(statement, 3);
  11668. // if(url==nil)
  11669. // url="";
  11670. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  11671. int qty = sqlite3_column_int(statement, 3);
  11672. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  11673. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  11674. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  11675. item[@"description"]= nsdescription;
  11676. item[@"img"]= nsurl;
  11677. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  11678. count++;
  11679. }
  11680. printf("total time:");
  11681. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  11682. ret[@"count"]= [NSNumber numberWithInt:count];
  11683. ret[@"total_count"]= [NSNumber numberWithInt:count];
  11684. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  11685. ret[@"result"]= [NSNumber numberWithInt:2];
  11686. sqlite3_finalize(statement);
  11687. }
  11688. [iSalesDB close_db:db];
  11689. dispatch_async(dispatch_get_main_queue(), ^{
  11690. UIApplication * app = [UIApplication sharedApplication];
  11691. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11692. appDelegate.wish_count =count;
  11693. [appDelegate update_count_mark];
  11694. result(ret);
  11695. });
  11696. });
  11697. }
  11698. +(void) offline_add2wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11699. {
  11700. // UIApplication * app = [UIApplication sharedApplication];
  11701. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11702. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11703. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  11704. sqlite3 *db = [iSalesDB get_db];
  11705. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  11706. NSString* product_id=params[@"product_id"];
  11707. NSString *item_count_str = params[@"item_count"];
  11708. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  11709. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  11710. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  11711. // NSString *sql = @"";
  11712. for(int i=0;i<arr.count;i++)
  11713. {
  11714. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  11715. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  11716. __block int cart_count = 0;
  11717. if (!item_count_str) {
  11718. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  11719. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11720. NSString *model_set = [self textAtColumn:0 statement:stmt];
  11721. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  11722. cart_count = [[model_set_components lastObject] intValue];
  11723. }];
  11724. }
  11725. if(count==0)
  11726. {
  11727. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  11728. sqlite3_stmt *stmt;
  11729. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  11730. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  11731. if (item_count_arr) {
  11732. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  11733. } else {
  11734. sqlite3_bind_int(stmt,2,cart_count);
  11735. }
  11736. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  11737. [iSalesDB execSql:@"ROLLBACK" db:db];
  11738. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  11739. [iSalesDB close_db:db];
  11740. DebugLog(@"add to wishlist error");
  11741. dispatch_async(dispatch_get_main_queue(), ^{
  11742. result(ret);
  11743. });
  11744. }
  11745. } else {
  11746. int qty = 0;
  11747. if (item_count_arr) {
  11748. qty = [item_count_arr[i] intValue];
  11749. } else {
  11750. qty = cart_count;
  11751. }
  11752. 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]];
  11753. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  11754. [iSalesDB execSql:@"ROLLBACK" db:db];
  11755. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  11756. [iSalesDB close_db:db];
  11757. DebugLog(@"add to wishlist error");
  11758. dispatch_async(dispatch_get_main_queue(), ^{
  11759. result(ret);
  11760. });
  11761. }
  11762. }
  11763. }
  11764. // [iSalesDB execSql:sql db:db];
  11765. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  11766. [iSalesDB execSql:@"END TRANSACTION" db:db];
  11767. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  11768. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  11769. [iSalesDB close_db:db];
  11770. ret[@"result"]= [NSNumber numberWithInt:2];
  11771. dispatch_async(dispatch_get_main_queue(), ^{
  11772. UIApplication * app = [UIApplication sharedApplication];
  11773. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11774. appDelegate.wish_count =count;
  11775. [appDelegate update_count_mark];
  11776. result(ret);
  11777. });
  11778. });
  11779. }
  11780. +(void) offline_model :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11781. {
  11782. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  11783. bool bLogin = appDelegate.bLogin;
  11784. __block NSString* contact_id = appDelegate.contact_id;
  11785. __block NSString* user = appDelegate.user;
  11786. __block NSString* order_code = appDelegate.order_code;
  11787. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11788. NSString* model_name = [params valueForKey:@"product_name"];
  11789. NSString* product_id = [params valueForKey:@"product_id"];
  11790. NSString* upc_code = [params valueForKey:@"upc_code"];
  11791. NSString* category = [params valueForKey:@"category"];
  11792. NSString *default_category_id = [self model_default_category:product_id model_name:model_name upc_code:upc_code];
  11793. if(category==nil) {
  11794. category = default_category_id;
  11795. } else {
  11796. NSArray *arr_0 = [category componentsSeparatedByString:@","];
  11797. // 参数重有多个category id
  11798. if (arr_0.count > 1) {
  11799. NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  11800. // 取交集
  11801. NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  11802. NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  11803. [set_0 intersectSet:set_1];
  11804. if (set_0.count == 1) {
  11805. category = (NSString *)[set_0 anyObject];
  11806. } else {
  11807. if ([set_0 containsObject:default_category_id]) {
  11808. category = default_category_id;
  11809. } else {
  11810. category = (NSString *)[set_0 anyObject];
  11811. }
  11812. }
  11813. }
  11814. }
  11815. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11816. sqlite3 *db = [iSalesDB get_db];
  11817. // int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  11818. NSString *sqlQuery = nil;
  11819. if(product_id==nil)
  11820. if(model_name==nil)
  11821. {
  11822. sqlQuery = [NSString stringWithFormat:@"select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging,ETA,item_id,seat_dimension,product_content_writing from product where 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='%@';
  11823. }
  11824. else
  11825. {
  11826. 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='%@';
  11827. }
  11828. else
  11829. 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=%@;
  11830. sqlite3_stmt * statement;
  11831. [ret setValue:@"2" forKey:@"result"];
  11832. [ret setValue:@"3" forKey:@"detail_section_count"];
  11833. // int count=0;
  11834. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11835. {
  11836. if (sqlite3_step(statement) == SQLITE_ROW)
  11837. {
  11838. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11839. char *name = (char*)sqlite3_column_text(statement, 0);
  11840. if(name==nil)
  11841. name="";
  11842. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11843. char *description = (char*)sqlite3_column_text(statement, 1);
  11844. if(description==nil)
  11845. description="";
  11846. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11847. int product_id = sqlite3_column_int(statement, 2);
  11848. char *color = (char*)sqlite3_column_text(statement, 3);
  11849. if(color==nil)
  11850. color="";
  11851. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  11852. //
  11853. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  11854. // if(legcolor==nil)
  11855. // legcolor="";
  11856. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  11857. //
  11858. //
  11859. int availability = sqlite3_column_int(statement, 5);
  11860. //
  11861. int incoming_stock = sqlite3_column_int(statement, 6);
  11862. char *demension = (char*)sqlite3_column_text(statement, 7);
  11863. if(demension==nil)
  11864. demension="";
  11865. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  11866. // ,,,,,,,,,
  11867. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  11868. if(seat_height==nil)
  11869. seat_height="";
  11870. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  11871. char *material = (char*)sqlite3_column_text(statement, 9);
  11872. if(material==nil)
  11873. material="";
  11874. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  11875. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  11876. if(box_dim==nil)
  11877. box_dim="";
  11878. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  11879. char *volume = (char*)sqlite3_column_text(statement, 11);
  11880. if(volume==nil)
  11881. volume="";
  11882. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  11883. double weight = sqlite3_column_double(statement, 12);
  11884. char *model_set = (char*)sqlite3_column_text(statement, 13);
  11885. if(model_set==nil)
  11886. model_set="";
  11887. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  11888. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  11889. if(load_ability==nil)
  11890. load_ability="";
  11891. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  11892. // char *default_category = (char*)sqlite3_column_text(statement, 15);
  11893. // if(default_category==nil)
  11894. // default_category="";
  11895. // NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  11896. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  11897. if(fabric_content==nil)
  11898. fabric_content="";
  11899. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  11900. char *assembling = (char*)sqlite3_column_text(statement, 17);
  11901. if(assembling==nil)
  11902. assembling="";
  11903. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  11904. char *made_in = (char*)sqlite3_column_text(statement, 18);
  11905. if(made_in==nil)
  11906. made_in="";
  11907. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  11908. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  11909. if(special_remarks==nil)
  11910. special_remarks="";
  11911. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  11912. int stockUcom = sqlite3_column_double(statement, 20);
  11913. char *product_group = (char*)sqlite3_column_text(statement, 21);
  11914. if(product_group==nil)
  11915. product_group="";
  11916. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  11917. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  11918. // if(fashion_selector==nil)
  11919. // fashion_selector="";
  11920. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  11921. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  11922. if(selector_field==nil)
  11923. selector_field="";
  11924. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  11925. char *property_field = (char*)sqlite3_column_text(statement, 23);
  11926. if(property_field==nil)
  11927. property_field="";
  11928. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  11929. char *packaging = (char*)sqlite3_column_text(statement, 24);
  11930. if(packaging==nil)
  11931. packaging="";
  11932. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  11933. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11934. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  11935. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  11936. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  11937. [img_section setValue:model_s_img forKey:@"model_s_img"];
  11938. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  11939. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  11940. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  11941. NSString* Availability=nil;
  11942. if(availability>0)
  11943. Availability=[NSString stringWithFormat:@"%d",availability];
  11944. else
  11945. Availability = @"Out of Stock";
  11946. [img_section setValue:Availability forKey:@"Availability"];
  11947. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  11948. char *eta = (char*)sqlite3_column_text(statement, 25);
  11949. if(eta==nil)
  11950. eta="";
  11951. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  11952. if ([nseta isEqualToString:@"null"]) {
  11953. nseta = @"";
  11954. }
  11955. if (availability <= 0) {
  11956. [img_section setValue:nseta forKey:@"ETA"];
  11957. }
  11958. int item_id = sqlite3_column_int(statement, 26);
  11959. NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  11960. NSString *content_writing = [self textAtColumn:28 statement:statement];
  11961. NSString* Price=nil;
  11962. if(bLogin==false)
  11963. Price=@"Must Sign in.";
  11964. else
  11965. {
  11966. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  11967. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  11968. if(price==nil)
  11969. Price=@"No Price.";
  11970. else
  11971. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  11972. }
  11973. [img_section setObject:content_writing forKey:@"product_content_writing"];
  11974. [img_section setValue:Price forKey:@"price"];
  11975. [img_section setValue:nsname forKey:@"model_name"];
  11976. [img_section setValue:nsdescription forKey:@"model_descrition"];
  11977. if (order_code) { // 离线order code即so#
  11978. 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];
  11979. __block int cartQTY = 0;
  11980. [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11981. cartQTY = sqlite3_column_int(stmt, 0);
  11982. }];
  11983. if (cartQTY > 0) {
  11984. [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  11985. }
  11986. }
  11987. [ret setObject:img_section forKey:@"img_section"];
  11988. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  11989. int detail0_item_count=0;
  11990. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11991. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11992. [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11993. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11994. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11995. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11996. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11997. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11998. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11999. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12000. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12001. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12002. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12003. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12004. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  12005. NSDictionary* pricejson = [self get_model_all_price:contact_id item_id:item_id user:user islogin:bLogin db:db];
  12006. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  12007. {
  12008. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  12009. [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  12010. }
  12011. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  12012. [detail0_section setValue:@"kv" forKey:@"type"];
  12013. [detail0_section setValue:@"Product Information" forKey:@"title"];
  12014. [ret setObject:detail0_section forKey:@"detail_0"];
  12015. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  12016. // [detail1_section setValue:@"detail" forKey:@"target"];
  12017. // [detail1_section setValue:@"popup" forKey:@"action"];
  12018. // [detail1_section setValue:@"content" forKey:@"type"];
  12019. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  12020. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  12021. // [detail1_section setValue:@"true" forKey:@"single_row"];
  12022. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  12023. [ret setObject:detail1_section forKey:@"detail_1"];
  12024. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  12025. [detail2_section setValue:@"detail" forKey:@"target"];
  12026. [detail2_section setValue:@"popup" forKey:@"action"];
  12027. [detail2_section setValue:@"content" forKey:@"type"];
  12028. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  12029. [detail2_section setValue:@"true" forKey:@"single_row"];
  12030. [detail2_section setValue:@"local" forKey:@"data"];
  12031. [ret setObject:detail2_section forKey:@"detail_2"];
  12032. }
  12033. sqlite3_finalize(statement);
  12034. }
  12035. else
  12036. {
  12037. [ret setValue:@"8" forKey:@"result"];
  12038. }
  12039. // DebugLog(@"count:%d",count);
  12040. [iSalesDB close_db:db];
  12041. #ifdef DEBUG
  12042. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  12043. #endif
  12044. dispatch_async(dispatch_get_main_queue(), ^{
  12045. result(ret);
  12046. });
  12047. });
  12048. }
  12049. //+(void) offline_deletewishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12050. //{
  12051. //
  12052. // UIApplication * app = [UIApplication sharedApplication];
  12053. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12054. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12055. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  12056. // sqlite3 *db = [iSalesDB get_db];
  12057. // NSString* collectId=params[@"collectId"];
  12058. //
  12059. //
  12060. //
  12061. //
  12062. // // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  12063. // NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  12064. // [iSalesDB execSql:sqlQuery db:db];
  12065. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12066. // [iSalesDB close_db:db];
  12067. //
  12068. //
  12069. // ret[@"result"]= [NSNumber numberWithInt:2];
  12070. // dispatch_async(dispatch_get_main_queue(), ^{
  12071. //
  12072. // appDelegate.wish_count =count;
  12073. //
  12074. // [appDelegate update_count_mark];
  12075. // result(ret);
  12076. // });
  12077. //
  12078. // });
  12079. //}
  12080. +(void) offline_logout :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12081. {
  12082. UIApplication * app = [UIApplication sharedApplication];
  12083. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12084. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12085. [iSalesDB disable_trigger];
  12086. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  12087. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  12088. [iSalesDB enable_trigger];
  12089. dispatch_async(dispatch_get_main_queue(), ^{
  12090. //
  12091. // NSString* user = [params valueForKey:@"user"];
  12092. //
  12093. // NSString* password = [params valueForKey:@"password"];
  12094. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12095. ret[@"result"]=[NSNumber numberWithInt:2 ];
  12096. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  12097. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  12098. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  12099. [appDelegate update_count_mark];
  12100. appDelegate.can_show_price =false;
  12101. appDelegate.can_see_price =false;
  12102. appDelegate.can_create_portfolio =false;
  12103. appDelegate.can_create_order =false;
  12104. appDelegate.can_cancel_order =false;
  12105. appDelegate.can_set_cart_price =false;
  12106. appDelegate.can_delete_order =false;
  12107. appDelegate.can_submit_order =false;
  12108. appDelegate.can_set_tearsheet_price =false;
  12109. appDelegate.can_update_contact_info = false;
  12110. appDelegate.save_order_logout = false;
  12111. appDelegate.submit_order_logout = false;
  12112. appDelegate.alert_sold_in_quantities = false;
  12113. appDelegate.ipad_perm =nil ;
  12114. appDelegate.user_type = USER_ROLE_UNKNOWN;
  12115. appDelegate.OrderFilter= nil;
  12116. [appDelegate SetSo:nil];
  12117. [appDelegate set_main_button_panel];
  12118. result(ret);
  12119. });
  12120. });
  12121. }
  12122. +(void) offline_createorder :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  12123. {
  12124. // iSalesDB.
  12125. // UIApplication * app = [UIApplication sharedApplication];
  12126. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12127. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12128. [iSalesDB disable_trigger];
  12129. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  12130. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  12131. [iSalesDB enable_trigger];
  12132. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12133. NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  12134. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  12135. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  12136. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  12137. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  12138. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  12139. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  12140. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  12141. NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  12142. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  12143. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  12144. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  12145. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  12146. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  12147. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  12148. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  12149. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  12150. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  12151. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  12152. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  12153. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  12154. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  12155. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  12156. [arr_name addObject:customer_first_name];
  12157. [arr_name addObject:customer_last_name];
  12158. NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  12159. // default ship from
  12160. 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';";
  12161. __block NSString *cid = @"";
  12162. __block NSString *name = @"";
  12163. __block NSString *ext = @"";
  12164. __block NSString *contact = @"";
  12165. __block NSString *email = @"";
  12166. __block NSString *fax = @"";
  12167. __block NSString *phone = @"";
  12168. sqlite3 *db = [iSalesDB get_db];
  12169. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  12170. cid = [self textAtColumn:0 statement:statment];
  12171. name = [self textAtColumn:1 statement:statment];
  12172. ext = [self textAtColumn:2 statement:statment];
  12173. contact = [self textAtColumn:3 statement:statment];
  12174. email = [self textAtColumn:4 statement:statment];
  12175. fax = [self textAtColumn:5 statement:statment];
  12176. phone = [self textAtColumn:6 statement:statment];
  12177. }];
  12178. // default carrier
  12179. NSString *carrier_sql = [NSString stringWithFormat:@"select name from carrier where code_id = (select carrier from offline_contact where contact_id = '%@');",customer_cid];
  12180. __block NSString *carrier = @"";
  12181. [iSalesDB jk_query:carrier_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  12182. carrier = [self textAtColumn:0 statement:statment];
  12183. }];
  12184. NSString* so_id = [self get_offline_soid:db];
  12185. if(so_id==nil)
  12186. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  12187. 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];
  12188. int result_code =[iSalesDB execSql:sql_neworder db:db];
  12189. [ret setValue:[NSNumber numberWithInt:result_code] forKey:@"result"];
  12190. //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'
  12191. //soId
  12192. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  12193. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  12194. sqlite3_stmt * statement;
  12195. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  12196. {
  12197. if (sqlite3_step(statement) == SQLITE_ROW)
  12198. {
  12199. // char *name = (char*)sqlite3_column_text(statement, 1);
  12200. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  12201. //ret = sqlite3_column_int(statement, 0);
  12202. char *soId = (char*)sqlite3_column_text(statement, 0);
  12203. if(soId==nil)
  12204. soId="";
  12205. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  12206. [ret setValue:nssoId forKey:@"soId"];
  12207. [ret setValue:nssoId forKey:@"orderCode"];
  12208. }
  12209. sqlite3_finalize(statement);
  12210. }
  12211. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  12212. [ret setValue:@"Regular Mode" forKey:@"mode"];
  12213. [iSalesDB close_db:db];
  12214. dispatch_async(dispatch_get_main_queue(), ^{
  12215. result(ret);
  12216. });
  12217. });
  12218. }
  12219. +(void) offline_requestcart :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  12220. {
  12221. UIApplication * app = [UIApplication sharedApplication];
  12222. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  12223. bool can_create_backorder= appDelegate.can_create_backorder;
  12224. NSString* user = appDelegate.user;
  12225. NSString* contact_id = appDelegate.contact_id;
  12226. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12227. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  12228. sqlite3 *db = [iSalesDB get_db];
  12229. NSString* orderCode=params[@"orderCode"];
  12230. int sort = [[params objectForKey:@"sort"] intValue];
  12231. NSString *sort_str = @"";
  12232. switch (sort) {
  12233. case 0:{
  12234. sort_str = @"order by c.modify_time desc";
  12235. }
  12236. break;
  12237. case 1:{
  12238. sort_str = @"order by c.modify_time asc";
  12239. }
  12240. break;
  12241. case 2:{
  12242. sort_str = @"order by m.name asc";
  12243. }
  12244. break;
  12245. case 3:{
  12246. sort_str = @"order by m.name desc";
  12247. }
  12248. break;
  12249. case 4:{
  12250. sort_str = @"order by m.description asc";
  12251. }
  12252. break;
  12253. default:
  12254. break;
  12255. }
  12256. NSString* sqlorder = [ NSString stringWithFormat:@"select discount from offline_order where so_id='%@'",orderCode];
  12257. sqlite3_stmt * statement1;
  12258. double orderdiscount=0;
  12259. int dbresult1=sqlite3_prepare_v2(db, [sqlorder UTF8String], -1, &statement1, nil);
  12260. if ( dbresult1== SQLITE_OK)
  12261. {
  12262. if (sqlite3_step(statement1) == SQLITE_ROW)
  12263. {
  12264. // NSDate *row_date = [NSDate date];
  12265. // NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  12266. orderdiscount = sqlite3_column_double(statement1, 0);
  12267. }
  12268. sqlite3_finalize(statement1);
  12269. }
  12270. 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 ];
  12271. // 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 ];
  12272. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  12273. DebugLog(@"offline_login sql:%@",sqlQuery);
  12274. sqlite3_stmt * statement;
  12275. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  12276. NSDate *date1 = [NSDate date];
  12277. int count=0;
  12278. int cart_count=0;
  12279. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  12280. if ( dbresult== SQLITE_OK)
  12281. {
  12282. while (sqlite3_step(statement) == SQLITE_ROW)
  12283. {
  12284. // NSDate *row_date = [NSDate date];
  12285. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  12286. int product_id = sqlite3_column_int(statement, 0);
  12287. char *str_price = (char*)sqlite3_column_text(statement, 1);
  12288. int item_id = sqlite3_column_int(statement, 7);
  12289. NSString* Price=nil;
  12290. if(str_price==nil)
  12291. {
  12292. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  12293. // NSDate *price_date = [NSDate date];
  12294. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  12295. // DebugLog(@"price time interval");
  12296. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  12297. if(price==nil)
  12298. Price=@"No Price.";
  12299. else
  12300. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  12301. }
  12302. else
  12303. {
  12304. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  12305. }
  12306. double discount = sqlite3_column_double(statement, 2);
  12307. int item_count = sqlite3_column_int(statement, 3);
  12308. char *line_note = (char*)sqlite3_column_text(statement, 4);
  12309. NSString *nsline_note=nil;
  12310. if(line_note!=nil)
  12311. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  12312. char *name = (char*)sqlite3_column_text(statement, 5);
  12313. NSString *nsname=nil;
  12314. if(name!=nil)
  12315. nsname= [[NSString alloc]initWithUTF8String:name];
  12316. char *description = (char*)sqlite3_column_text(statement, 6);
  12317. NSString *nsdescription=nil;
  12318. if(description!=nil)
  12319. nsdescription= [[NSString alloc]initWithUTF8String:description];
  12320. int stockUom = sqlite3_column_int(statement, 8);
  12321. int _id = sqlite3_column_int(statement, 9);
  12322. int availability = sqlite3_column_int(statement, 10);
  12323. // NSDate *subtotal_date = [NSDate date];
  12324. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  12325. // DebugLog(@"subtotal_date time interval");
  12326. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  12327. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  12328. double weight=[bsubtotaljson[@"weight"] doubleValue];
  12329. int carton=[bsubtotaljson[@"carton"] intValue];
  12330. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  12331. int mpack =[bsubtotaljson[@"mpack"] intValue];
  12332. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  12333. // NSDate *img_date = [NSDate date];
  12334. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  12335. // DebugLog(@"img_date time interval");
  12336. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  12337. itemjson[@"model"]=nsname;
  12338. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  12339. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  12340. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  12341. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  12342. itemjson[@"check"]=@"true";
  12343. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  12344. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  12345. itemjson[@"unit_price"]=Price;
  12346. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  12347. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  12348. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  12349. itemjson[@"mpack"]=[NSNumber numberWithInt:mpack];
  12350. itemjson[@"note"]=nsline_note;
  12351. if (!can_create_backorder) {
  12352. itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  12353. }
  12354. // NSDate *date2 = [NSDate date];
  12355. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:contact_id user:user];
  12356. // DebugLog(@"model_bundle time interval");
  12357. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  12358. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  12359. count++;
  12360. // DebugLog(@"row time interval");
  12361. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  12362. }
  12363. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  12364. sqlite3_finalize(statement);
  12365. }
  12366. DebugLog(@"request cart total time interval");
  12367. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  12368. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  12369. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  12370. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  12371. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  12372. ret[@"count"]=[NSNumber numberWithInt:count ];
  12373. if(orderdiscount>0)
  12374. ret[@"orderDiscount"]=@(orderdiscount);
  12375. ret[@"mode"]=@"Regular Mode";
  12376. [iSalesDB close_db:db];
  12377. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  12378. DebugLog(@"general notes :%@",general_note);
  12379. ret[@"general_note"]= general_note;
  12380. dispatch_async(dispatch_get_main_queue(), ^{
  12381. result(ret);
  12382. });
  12383. });
  12384. }
  12385. @end