OLDataProvider.m 626 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265
  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 "UIProgressView+AFNetworking.h"
  15. //#import "AFHTTPSessionManager.h"
  16. #import "AppDelegate.h"
  17. #import "pdfCreator.h"
  18. #import "Singleton.h"
  19. #import "QRCodeGenerator.h"
  20. #import "config.h"
  21. #import "ImageUtils.h"
  22. #import "RAConvertor.h"
  23. @interface OLDataProvider ()
  24. @end
  25. @implementation OLDataProvider
  26. +(NSString*) get_portfolio_price:(NSString*) contact_id item_id:(int) item_id price:(int)price_id db:(sqlite3*)db
  27. {
  28. 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];
  29. NSString* ret=@"No Price.";
  30. sqlite3_stmt * statement;
  31. // int count=0;
  32. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  33. {
  34. if (sqlite3_step(statement) == SQLITE_ROW)
  35. {
  36. char *price = (char*)sqlite3_column_text(statement, 0);
  37. if(price==nil)
  38. price="";
  39. NSString *nsprice = [[NSString alloc]initWithUTF8String:price];
  40. double dp= [nsprice doubleValue];
  41. ret=[NSString stringWithFormat:@"%.2f",dp];
  42. }
  43. sqlite3_finalize(statement);
  44. }
  45. return ret;
  46. // // [iSalesDB close_db:db];
  47. //
  48. // return nil;
  49. //// if(dprice==DBL_MAX)
  50. //// ret= nil;
  51. //// else
  52. //// ret= [NSNumber numberWithDouble:dprice];
  53. //// return ret;
  54. }
  55. + (NSString *)portfolio_img:(NSString *)item_id db:(sqlite3 *)db
  56. {
  57. NSString* ret= nil;
  58. NSString *sqlQuery = nil;
  59. // 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
  60. 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];
  61. sqlite3_stmt * statement;
  62. // int count=0;
  63. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  64. {
  65. if (sqlite3_step(statement) == SQLITE_ROW)
  66. {
  67. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  68. if(imgurl==nil)
  69. imgurl="";
  70. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  71. ret=nsimgurl;
  72. }
  73. sqlite3_finalize(statement);
  74. }
  75. else
  76. {
  77. ret=@"";
  78. }
  79. // [iSalesDB close_db:db];
  80. // DebugLog(@"data string: %@",ret );
  81. return ret;
  82. }
  83. +(NSMutableDictionary*) prepare_portfolio_data:(NSMutableDictionary*) params
  84. {
  85. NSMutableDictionary* values=params[@"replaceValue"]; // 离线下发的参数
  86. //生成portfolio pdf需要的数据
  87. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  88. data[@"npd_url"]=COMPANY_WEB;
  89. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  90. [formatter setDateFormat:@"MMMM yyyy"];
  91. NSString* date = [formatter stringFromDate:[NSDate date]];
  92. data[@"create_month"]=date;
  93. data[@"company_name"]=COMPANY_FULL_NAME;
  94. data[@"catalog_name"]=params[@"catalog_name"];
  95. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  96. data[@"TOTAL_PAGE"]=0;
  97. data[@"CURRENT_PAGE"]=0;
  98. NSMutableDictionary* grid = [[NSMutableDictionary alloc]init];
  99. BOOL show_group_by = [[params objectForKey:@"show_group_by"] boolValue];
  100. bool show_stockout=[params[@"show_stockout"] boolValue];
  101. NSString *sql = nil;
  102. // v1.90 more color
  103. if(values==nil)
  104. {
  105. 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"]];
  106. }
  107. else
  108. {
  109. 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"]];
  110. }
  111. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  112. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  113. sqlite3 *db = [iSalesDB get_db];
  114. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  115. // [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  116. int product_id = sqlite3_column_int(stmt, 0);
  117. // NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  118. NSString *name = [self textAtColumn:1 statement:stmt];
  119. NSString *description = [self textAtColumn:2 statement:stmt];
  120. double price = sqlite3_column_double(stmt, 3);
  121. NSString *s_price = [self textAtColumn:3 statement:stmt];
  122. double discount = sqlite3_column_double(stmt,4);
  123. int qty = sqlite3_column_int(stmt, 5);
  124. NSString *s_qty = [self textAtColumn:5 statement:stmt];
  125. int is_percentage = sqlite3_column_int(stmt, 6);
  126. NSString *s_is_percentage = [self textAtColumn:6 statement:stmt];
  127. int item_id = sqlite3_column_int(stmt, 7);
  128. // int fashion_id = sqlite3_column_int(stmt, 8);
  129. NSString *line_note = [self textAtColumn:9 statement:stmt];
  130. NSString *s_percent = [self textAtColumn:10 statement:stmt];
  131. double percent = sqlite3_column_double(stmt, 10);
  132. // int portfolio_id = sqlite3_column_int(stmt, 11);
  133. NSString *s_availability = [self textAtColumn:12 statement:stmt];
  134. int availability = sqlite3_column_int(stmt, 12);
  135. NSString *color = [self textAtColumn:13 statement:stmt];
  136. NSString *legcolor = [self textAtColumn:14 statement:stmt];
  137. NSString *demension = [self textAtColumn:15 statement:stmt];
  138. NSString *seat_height = [self textAtColumn:16 statement:stmt];
  139. NSString *material = [self textAtColumn:17 statement:stmt];
  140. NSString *box_dim = [self textAtColumn:18 statement:stmt];
  141. NSString *s_volume = [self textAtColumn:19 statement:stmt];
  142. double volume = sqlite3_column_double(stmt, 19);
  143. NSString *s_weight = [self textAtColumn:20 statement:stmt];
  144. double weight = sqlite3_column_double(stmt, 20);
  145. NSString *s_model_set = [self textAtColumn:21 statement:stmt];
  146. int model_set = sqlite3_column_int(stmt, 21);
  147. NSString *s_load_ability = [self textAtColumn:22 statement:stmt];
  148. double load_ability = sqlite3_column_double(stmt, 22);
  149. NSString *fabric_content = [self textAtColumn:23 statement:stmt];
  150. NSString *assembling = [self textAtColumn:24 statement:stmt];
  151. NSString *made_in = [self textAtColumn:25 statement:stmt];
  152. NSInteger more_color_int = sqlite3_column_int(stmt, 26);
  153. NSString* gprice = [self get_portfolio_price :appDelegate.contact_id item_id:item_id price:[params[@"regular_price"] intValue] db:db];
  154. float bprice=0;
  155. NSDictionary* bundle=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  156. for(int b=0;b< [bundle[@"count"] intValue];b++)
  157. {
  158. NSDictionary* bi=bundle[[NSString stringWithFormat:@"item_%d",b]];
  159. bprice+= [bi[@"modulus"] intValue] * [bi[@"unit_price"] floatValue];
  160. }
  161. if(gprice!=nil)
  162. gprice = [NSString stringWithFormat:@"%.2f",[gprice floatValue]+bprice];
  163. if(s_percent.length==0||is_percentage==0)
  164. {
  165. percent=100.0;
  166. if([s_qty isEqualToString:@"null"])
  167. qty=availability;
  168. }
  169. else
  170. {
  171. qty=availability;
  172. }
  173. qty=qty*percent/100+0.5;
  174. for(int i=0;i<[values[@"count"] intValue];i++)
  175. {
  176. NSMutableDictionary * valueitem=values[[NSString stringWithFormat:@"item_%d",i]];;
  177. if([valueitem[@"product_id"] intValue]==product_id)
  178. {
  179. line_note=valueitem[@"line_note"];
  180. qty=[valueitem[@"available_qty"] intValue];
  181. if([[valueitem allKeys] containsObject:@"available_qty"])
  182. s_qty=[NSString stringWithFormat:@"%d",qty];
  183. else
  184. s_qty=@"null";
  185. if(valueitem[@"regular_price"]!=nil)
  186. gprice=[NSString stringWithFormat:@"%.2f",[valueitem[@"regular_price"] floatValue]];
  187. s_price=valueitem[@"special_price"];
  188. price=[valueitem[@"special_price"] floatValue];
  189. discount=0;
  190. // v1.90
  191. more_color_int = [[valueitem objectForKey:@"more_color"] integerValue];
  192. }
  193. }
  194. if(is_percentage==0&&qty==0&&!show_stockout)
  195. {
  196. return;
  197. }
  198. if((is_percentage!=0||s_is_percentage.length!=0)&&qty==0&&!show_stockout)
  199. {
  200. return;
  201. }
  202. NSString* set_price=@"";
  203. if([params[@"entered_price"] boolValue])
  204. {
  205. if (s_price==nil ) {
  206. // set_price=[NSString stringWithFormat:@"Special: $ %@",gprice];
  207. }
  208. else
  209. {
  210. if(price*(1-discount/100.0)!=gprice.floatValue)
  211. set_price=[NSString stringWithFormat:@"Special: $ %.2f",price*(1-discount/100.0)];
  212. }
  213. }
  214. NSString* get_price=@"";
  215. {
  216. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  217. // DebugLog(@"price time interval");
  218. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  219. if(gprice==nil)
  220. get_price=@"Price:No Price.";
  221. else
  222. get_price=[NSString stringWithFormat:@"Price: $ %@",gprice];
  223. }
  224. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  225. // if ([qty_null isEqualToString:@"null"]) {
  226. // qty=availability;
  227. // }
  228. //
  229. // NSString *qty_percent_null = [self textAtColumn:6 statement:stmt];
  230. // if ([qty_percent_null isEqualToString:@"null"]) {
  231. // percentage=1;
  232. // }
  233. NSString * str_availability=[NSString stringWithFormat:@"<b>Availability:</b> %d",(int)qty];
  234. NSString * str_color=[NSString stringWithFormat:@"<b>Color:</b> %@",color];
  235. NSString * str_model_set=[NSString stringWithFormat:@"<b>Set:</b> Sold in QTY of %d",model_set];
  236. NSString * str_legcolor=[NSString stringWithFormat:@"<b>Leg Color:</b> %@",legcolor];
  237. NSString * str_demension=[NSString stringWithFormat:@"<b>Dimension:</b> %@",demension];
  238. NSString * str_seat_height=[NSString stringWithFormat:@"<b>Seat Height:</b> %@",seat_height];
  239. NSString * str_meterial=[NSString stringWithFormat:@"<b>Meterial:</b> %@",material];
  240. NSString * str_box_dim=[NSString stringWithFormat:@"<b>Box Dimension:</b> %@",box_dim];
  241. NSString * str_volume=[NSString stringWithFormat:@"<b>Volume:</b> %.2f",volume];
  242. NSString * str_weight=[NSString stringWithFormat:@"<b>Weight:</b> %.2f",weight];
  243. NSString * str_load_ability=[NSString stringWithFormat:@"<b>Load Ability:</b> %.2f",load_ability];
  244. NSString * str_fabric_content=[NSString stringWithFormat:@"<b>Fabric Content:</b> %@",fabric_content];
  245. NSString * str_assembling=[NSString stringWithFormat:@"<b>Assembling:</b> %@",assembling];
  246. NSString * str_made_in=[NSString stringWithFormat:@"<b>Made In:</b> %@",made_in];
  247. NSString * str_line_note=[NSString stringWithFormat:@"<b>Line Note:</b> %@",line_note];
  248. if([params[@"availability"] boolValue]==false)
  249. str_availability=@"";
  250. else
  251. {
  252. if ([s_qty isEqualToString:@"null"] && [s_availability isEqualToString:@"0"])
  253. str_availability=@"<b>Availability:</b> In Production";
  254. }
  255. if([params[@"color"] boolValue]==false || color.length==0 )
  256. str_color=@"";
  257. if([params[@"set"] boolValue]==false || s_model_set.length==0 )
  258. str_model_set=@"";
  259. if([params[@"leg_color"] boolValue]==false || legcolor.length==0 )
  260. str_legcolor=@"";
  261. if([params[@"dimension"] boolValue]==false || demension.length==0 )
  262. str_demension=@"";
  263. if([params[@"seat_height"] boolValue]==false || seat_height.length==0 )
  264. str_seat_height=@"";
  265. if([params[@"material"] boolValue]==false || material.length==0 )
  266. str_meterial=@"";
  267. if([params[@"box_dimension"] boolValue]==false || box_dim.length==0 )
  268. str_box_dim=@"";
  269. if([params[@"volume"] boolValue]==false || s_volume.length==0 )
  270. str_volume=@"";
  271. if([params[@"weight"] boolValue]==false || s_weight.length==0 )
  272. str_weight=@"";
  273. if([params[@"load_ability"] boolValue]==false || s_load_ability.length==0 )
  274. str_load_ability=@"";
  275. if([params[@"fabric_content"] boolValue]==false || fabric_content.length==0 )
  276. str_fabric_content=@"";
  277. if([params[@"assembling"] boolValue]==false || assembling.length==0 )
  278. str_assembling=@"";
  279. if([params[@"made"] boolValue]==false || made_in.length==0 )
  280. str_made_in=@"";
  281. if([params[@"show_line_note"] boolValue]==false || line_note.length==0 )
  282. str_line_note=@"";
  283. NSMutableArray* arr_detail=[[NSMutableArray alloc]init];
  284. [arr_detail addObject:str_availability];
  285. [arr_detail addObject:str_color];
  286. [arr_detail addObject:str_model_set];
  287. [arr_detail addObject:str_legcolor];
  288. [arr_detail addObject:str_demension];
  289. [arr_detail addObject:str_seat_height];
  290. [arr_detail addObject:str_meterial];
  291. [arr_detail addObject:str_box_dim];
  292. [arr_detail addObject:str_volume];
  293. [arr_detail addObject:str_weight];
  294. [arr_detail addObject:str_load_ability];
  295. [arr_detail addObject:str_fabric_content];
  296. [arr_detail addObject:str_assembling];
  297. [arr_detail addObject:str_made_in];
  298. [arr_detail addObject:str_line_note];
  299. // NSString* detail =[NSString stringWithFormat:@"<font size=0.2>%@</font>",[RAUtils arr2string:arr_detail separator:@" " trim:true]];
  300. NSString* detail =[RAConvertor arr2string:arr_detail separator:@" " trim:true];
  301. //model image;
  302. NSString* img_path = [self portfolio_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  303. // qr image
  304. NSString* qrpath=nil;
  305. if([params[@"show_barcode"] boolValue])
  306. {
  307. NSString* temp = NSTemporaryDirectory();
  308. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  309. qrpath=[temp stringByAppendingPathComponent:filename];
  310. UIImage* qrimg=[QRCodeGenerator qrImageForString:name imageSize:240];
  311. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  312. }
  313. NSString *more_color_path = [[NSBundle mainBundle] pathForResource:@"more_color_64" ofType:@"png"];
  314. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  315. item[@"img"]=img_path;
  316. item[@"img_qr"]=qrpath;
  317. item[@"special_price"]=set_price;
  318. item[@"price"]=get_price;
  319. item[@"name"]=name;
  320. item[@"description"]=description;
  321. item[@"detail"]=detail;
  322. item[@"img"]=img_path;
  323. if (show_group_by && more_color_int != 0) {
  324. item[@"more_color"] = more_color_path;
  325. }
  326. // @{
  327. // //@"linenotes": line_note,
  328. //// @"product_id": product_id_string,
  329. //// @"available_qty": @(qty),
  330. //// @"available_percent" : @(percent),
  331. //// @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  332. //// @"item_id": [NSString stringWithFormat:@"%d",item_id],
  333. //// @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  334. // @"": ,
  335. // @"": ,
  336. // @"": ,
  337. // @"": ,
  338. // @"": ,
  339. // @"": ,
  340. // @"":
  341. // }.mutableCopy;
  342. // if (percentage) {
  343. // [item removeObjectForKey:@"available_qty"];
  344. // } else {
  345. // [item removeObjectForKey:@"available_percent"];
  346. // }
  347. //
  348. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  349. // if ([qty_null isEqualToString:@"null"]) {
  350. // [item removeObjectForKey:@"available_qty"];
  351. // }
  352. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  353. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  354. }];
  355. [iSalesDB close_db:db];
  356. // [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  357. // [dic setValue:@"" forKey:@"email_content"];
  358. // [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  359. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  360. //
  361. // NSMutableDictionary* cell = [[NSMutableDictionary alloc]init];
  362. // cell[@"count"]=[NSNumber numberWithInt:10];
  363. grid[@"cell0"]=dic;
  364. data[@"grid0"]=grid;
  365. return data;
  366. }
  367. +(NSMutableDictionary*) get_pdftemplate:(NSString*) template
  368. {
  369. NSString *default_path = [[NSBundle mainBundle] pathForResource:template ofType:nil];
  370. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  371. NSString *cache_folder=[paths objectAtIndex:0];
  372. NSString *template_path = [cache_folder stringByAppendingPathComponent:[NSString stringWithFormat:@"offline_data/%@",template]];
  373. BOOL bdir=NO;
  374. NSFileManager* fileManager = [NSFileManager defaultManager];
  375. if(PDF_DEBUG)
  376. {
  377. NSData *data = [NSData dataWithContentsOfFile:default_path];
  378. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  379. return ret;
  380. }
  381. if(! [fileManager fileExistsAtPath:template_path isDirectory:&bdir] )
  382. {
  383. NSError * error=nil;
  384. if(![fileManager copyItemAtPath:default_path toPath:template_path error:&error])
  385. {
  386. return nil;
  387. }
  388. }
  389. NSData *data = [NSData dataWithContentsOfFile:template_path];
  390. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  391. return ret;
  392. }
  393. +(NSMutableDictionary*) query_login:(NSString*) user db:(sqlite3*) db
  394. {
  395. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  396. 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 ];
  397. DebugLog(@"offline_login sql:%@",sqlQuery);
  398. sqlite3_stmt * statement;
  399. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  400. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  401. {
  402. if (sqlite3_step(statement) == SQLITE_ROW)
  403. {
  404. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  405. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  406. int can_show_price = sqlite3_column_int(statement, 0);
  407. int can_see_price = sqlite3_column_int(statement, 1);
  408. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  409. if(contact_id==nil)
  410. contact_id="";
  411. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  412. int user_type = sqlite3_column_int(statement, 3);
  413. int can_cancel_order = sqlite3_column_int(statement, 4);
  414. int can_set_cart_price = sqlite3_column_int(statement, 5);
  415. int can_create_portfolio = sqlite3_column_int(statement, 6);
  416. int can_delete_order = sqlite3_column_int(statement, 7);
  417. int can_submit_order = sqlite3_column_int(statement, 8);
  418. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  419. int can_create_order = sqlite3_column_int(statement, 10);
  420. char *mode = (char*)sqlite3_column_text(statement, 11);
  421. if(mode==nil)
  422. mode="";
  423. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  424. char *username = (char*)sqlite3_column_text(statement, 12);
  425. if(username==nil)
  426. username="";
  427. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  428. int can_update_contact_info = sqlite3_column_int(statement, 13);
  429. char *first_name = (char*)sqlite3_column_text(statement, 14);
  430. if(first_name==nil)
  431. first_name="";
  432. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  433. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  434. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  435. // [header setValue:nscontact_id forKey:@"contact_id"];
  436. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  437. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  438. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  439. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  440. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  441. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  442. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  443. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  444. //
  445. // [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  446. //
  447. // [header setValue:nsusername forKey:@"username"];
  448. //
  449. // NSError* error=nil;
  450. //
  451. // NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  452. // [header setValue:statusFilter forKey:@"statusFilter"];
  453. //
  454. //
  455. //
  456. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  457. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  458. //
  459. // // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  460. // // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  461. // int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  462. // [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  463. //
  464. // [ret setObject:header forKey:@"header"];
  465. [ret setValue:nsfirst_name forKey:@"first_name"];
  466. }
  467. sqlite3_finalize(statement);
  468. }
  469. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  470. return ret;
  471. }
  472. +(NSMutableDictionary*) prepare_salesorder_data:(NSString*) order_code type:(int) type
  473. {
  474. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  475. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  476. data[@"npd_url"]=COMPANY_WEB;
  477. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  478. [formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
  479. NSString* date = [formatter stringFromDate:[NSDate date]];
  480. data[@"print_date"]=date;
  481. data[@"company_name"]=COMPANY_FULL_NAME;
  482. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  483. data[@"TOTAL_PAGE"]=0;
  484. data[@"CURRENT_PAGE"]=0;
  485. // [formatter setDateFormat:@"MM/dd/yyyy"];
  486. // date = [formatter stringFromDate:[NSDate date]];
  487. // data[@"creat_date"]=date;
  488. NSString* temp = NSTemporaryDirectory();
  489. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  490. NSString* qrpath=[temp stringByAppendingPathComponent:filename];
  491. UIImage* qrimg=[ImageUtils generateBarCode:order_code width:480 height:44];//[QRCodeGenerator qrImageForString:order_code imageSize:240];
  492. bool bwrite=[UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  493. data[@"img_barcode"]=qrpath;
  494. data[@"so_id"]=order_code;
  495. data[@"company"]=COMPANY_FULL_NAME;
  496. // 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];
  497. 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 from offline_order where so_id='%@'",order_code];
  498. sqlite3 *db = [iSalesDB get_db];
  499. data[@"print_by"]= [self query_login:appDelegate.user db:db][@"first_name"];
  500. sqlite3_stmt * statement;
  501. NSMutableDictionary* datasource = [[NSMutableDictionary alloc]init];
  502. NSMutableDictionary* row0 = [[NSMutableDictionary alloc]init];
  503. row0[@"count"]=[NSNumber numberWithInt:1];
  504. datasource[@"row0"]=row0;
  505. NSString* print_status=@"";
  506. NSString* order_type=@"Offline Quote";
  507. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  508. {
  509. if (sqlite3_step(statement) == SQLITE_ROW)
  510. {
  511. int erpOrderStatus = sqlite3_column_int(statement, 0);
  512. int status = sqlite3_column_int(statement, 26);
  513. if(status==2)
  514. {
  515. switch (erpOrderStatus) {
  516. case 10:
  517. // order_type=@"Quoted Order";
  518. print_status=@"Quote";
  519. break;
  520. case 11:
  521. print_status=@"Booked";
  522. break;
  523. case 12:
  524. print_status=@"Picked";
  525. break;
  526. case 13:
  527. print_status=@"Shipped";
  528. break;
  529. case 14:
  530. print_status=@"Closed";
  531. break;
  532. case 15:
  533. print_status=@"Canceled";
  534. break;
  535. case 16:
  536. print_status=@"All";
  537. break;
  538. case 18:
  539. print_status=@"Processing";
  540. break;
  541. default:
  542. break;
  543. }
  544. }
  545. else{
  546. switch (status) {
  547. case 0:
  548. {
  549. print_status=@"Quote";
  550. break;
  551. }
  552. case 1:
  553. {
  554. print_status=@"Saved";
  555. break;
  556. }
  557. case 3:
  558. {
  559. //canceled
  560. print_status=@"Canceled";
  561. break;
  562. }
  563. default:
  564. break;
  565. }
  566. }
  567. NSString* sender_name = [self textAtColumn:1 statement:statement];
  568. NSString* sender_ext = [self textAtColumn:2 statement:statement];
  569. NSString* sender_phone = [self textAtColumn:3 statement:statement];
  570. NSString* sender_fax = [self textAtColumn:4 statement:statement];
  571. NSString* sender_email = [self textAtColumn:5 statement:statement];
  572. NSString* warehouse_name = [self textAtColumn:6 statement:statement];
  573. NSString* billing_name = [self textAtColumn:7 statement:statement];
  574. NSString* billing_ext = [self textAtColumn:8 statement:statement];
  575. NSString* billing_phone = [self textAtColumn:9 statement:statement];
  576. if(billing_phone.length>0)
  577. billing_phone= [@"TEL: " stringByAppendingString:billing_phone];
  578. NSString* billing_fax = [self textAtColumn:10 statement:statement];
  579. if(billing_fax.length>0)
  580. billing_fax= [@"FAX: " stringByAppendingString:billing_fax];
  581. NSMutableArray* billto_arr0 = [[NSMutableArray alloc]init];
  582. [billto_arr0 addObject:billing_phone];
  583. [billto_arr0 addObject:billing_fax];
  584. NSString* billing_pf=[RAConvertor arr2string:billto_arr0 separator:@" " trim:true];
  585. NSString* billing_email = [self textAtColumn:11 statement:statement];
  586. if(billing_email.length>0)
  587. billing_email= [@"EMAIL: " stringByAppendingString:billing_email];
  588. NSString* billing_contact = [self textAtColumn:31 statement:statement];
  589. if(billing_contact.length>0)
  590. billing_contact= [@"ATTN: " stringByAppendingString:billing_contact];
  591. NSString* receive_name = [self textAtColumn:12 statement:statement];
  592. NSString* receive_ext = [self textAtColumn:13 statement:statement];
  593. NSString* receive_phone = [self textAtColumn:14 statement:statement];
  594. if(receive_phone.length>0)
  595. receive_phone= [@"TEL: " stringByAppendingString:receive_phone];
  596. NSString* receive_fax = [self textAtColumn:15 statement:statement];
  597. if(receive_fax.length>0)
  598. receive_fax= [@"FAX: " stringByAppendingString:receive_fax];
  599. NSMutableArray* receive_arr0 = [[NSMutableArray alloc]init];
  600. [receive_arr0 addObject:receive_phone];
  601. [receive_arr0 addObject:receive_fax];
  602. NSString* receive_email = [self textAtColumn:16 statement:statement];
  603. NSString* receive_pf=[RAConvertor arr2string:billto_arr0 separator:@" " trim:true];
  604. if(receive_email.length>0)
  605. receive_email= [@"EMAIL: " stringByAppendingString:receive_email];
  606. NSString* receive_contact = [self textAtColumn:32 statement:statement];
  607. if(receive_contact.length>0)
  608. receive_contact= [@"ATTN: " stringByAppendingString:receive_contact];
  609. NSString* poNumber = [self textAtColumn:17 statement:statement];
  610. NSString* sales_rep = [self textAtColumn:18 statement:statement];
  611. NSString* carrier = [self textAtColumn:19 statement:statement];
  612. NSString* vendor_no = [self textAtColumn:20 statement:statement];
  613. NSString* terms = [self textAtColumn:21 statement:statement];
  614. NSString* sales_terms = [self textAtColumn:22 statement:statement];
  615. NSString* port_destination = [self textAtColumn:23 statement:statement];
  616. NSString* etd = [self textAtColumn:24 statement:statement];
  617. NSString* sign_picpath = [self textAtColumn:25 statement:statement];
  618. if(sign_picpath.length==0)
  619. sign_picpath=nil;
  620. NSString* create_date = [self textAtColumn:27 statement:statement];
  621. create_date=[self changeDateTime:create_date Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy"];
  622. NSString* general_notes=[self textAtColumn:33 statement:statement];
  623. NSString *customerID = [self textAtColumn:34 statement:statement];
  624. if (customerID.length == 0) {
  625. customerID = nil;
  626. }
  627. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  628. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  629. if(general_notes.length>0)
  630. {
  631. NSMutableDictionary* row2 = [[NSMutableDictionary alloc]init];
  632. row2[@"item_0"]=@{@"g_notes":general_notes };
  633. row2[@"count"]=[NSNumber numberWithInt:1];
  634. datasource[@"row2"]=row2;
  635. }
  636. NSMutableArray* billto_arr = [[NSMutableArray alloc]init];
  637. [billto_arr addObject:billing_ext];
  638. [billto_arr addObject:billing_pf];
  639. [billto_arr addObject:billing_contact];
  640. [billto_arr addObject:billing_email];
  641. NSString* billto=[RAConvertor arr2string:billto_arr separator:@"\r\n" trim:true];
  642. NSMutableArray* receive_arr = [[NSMutableArray alloc]init];
  643. [receive_arr addObject:receive_ext];
  644. [receive_arr addObject:receive_pf];
  645. [receive_arr addObject:receive_contact];
  646. [receive_arr addObject:receive_email];
  647. NSString* receive=[RAConvertor arr2string:receive_arr separator:@"\r\n" trim:true];
  648. float shipping = sqlite3_column_double(statement, 28);
  649. float lift_gate_value = sqlite3_column_double(statement, 29);
  650. float handling_fee_value = sqlite3_column_double(statement, 30);
  651. data[@"company"]=sender_name;
  652. data[@"shipfrom_addr"]=sender_ext;
  653. data[@"shipfrom_phone"]=sender_phone;
  654. data[@"shipfrom_fax"]=sender_fax;
  655. data[@"shipfrom_email"]=sender_email;
  656. data[@"warehouse_name"]=warehouse_name;
  657. data[@"bill_to_name"]=billing_name;
  658. data[@"bill_to_ext"]=billto;
  659. data[@"ship_to_name"]=receive_name;
  660. data[@"ship_to_ext"]=receive;
  661. data[@"po_no"]=poNumber;
  662. data[@"rep"]=sales_rep;
  663. data[@"e_schdule"]=etd;
  664. data[@"sales_terms"]=sales_terms;
  665. data[@"port_destination"]=port_destination;
  666. data[@"terms"]=terms;
  667. data[@"carrier"]=carrier;
  668. data[@"vendor_no"]=vendor_no;
  669. data[@"customer_sign"]=sign_picpath;
  670. data[@"print_status"]=print_status;
  671. data[@"create_date"]=create_date;
  672. 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];;
  673. sqlite3_stmt * statement_cart;
  674. // NSDate *date1 = [NSDate date];
  675. int count=0;
  676. int cart_count=0;
  677. float credits=0;
  678. int dbresult=sqlite3_prepare_v2(db, [sql_cart UTF8String], -1, &statement_cart, nil);
  679. if ( dbresult== SQLITE_OK)
  680. {
  681. NSMutableDictionary* row1 = [[NSMutableDictionary alloc]init];
  682. int row1count=0;
  683. while (sqlite3_step(statement_cart) == SQLITE_ROW)
  684. {
  685. NSDate *row_date = [NSDate date];
  686. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  687. // int product_id = sqlite3_column_int(statement_cart, 0);
  688. char *str_price = (char*)sqlite3_column_text(statement_cart, 1);
  689. int item_id = sqlite3_column_int(statement_cart, 7);
  690. double discount = sqlite3_column_double(statement_cart, 2);
  691. NSString* Price=nil;
  692. if(str_price==nil)
  693. {
  694. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  695. // NSDate *price_date = [NSDate date];
  696. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db];
  697. // DebugLog(@"price time interval");
  698. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  699. if(price==nil)
  700. Price=@"No Price.";
  701. else
  702. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  703. }
  704. else
  705. {
  706. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  707. }
  708. float dunitprice=0;
  709. int item_count = sqlite3_column_int(statement_cart, 3);
  710. NSString* Amount=@"";
  711. if(![Price isEqualToString:@"No Price."])
  712. {
  713. dunitprice=[Price floatValue];
  714. dunitprice=dunitprice* (1-discount/100.0);
  715. Price=[NSString stringWithFormat:@"%.2f",dunitprice];
  716. credits+=dunitprice*item_count;
  717. Amount=[NSString stringWithFormat:@"%.2f",dunitprice*item_count];
  718. }
  719. else
  720. {
  721. Price=@"";
  722. }
  723. // char *line_note = (char*)sqlite3_column_text(statement_cart, 4);
  724. // NSString *nsline_note=nil;
  725. // if(line_note!=nil)
  726. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  727. char *name = (char*)sqlite3_column_text(statement_cart, 5);
  728. NSString *nsname=nil;
  729. if(name!=nil)
  730. nsname= [[NSString alloc]initWithUTF8String:name];
  731. char *description = (char*)sqlite3_column_text(statement_cart, 6);
  732. NSString *nsdescription=nil;
  733. if(description!=nil)
  734. nsdescription= [[NSString alloc]initWithUTF8String:description];
  735. // int stockUom = sqlite3_column_int(statement_cart, 8);
  736. // int _id = sqlite3_column_int(statement_cart, 9);
  737. // NSDate *subtotal_date = [NSDate date];
  738. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  739. // DebugLog(@"subtotal_date time interval");
  740. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  741. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  742. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  743. // int carton=[bsubtotaljson[@"carton"] intValue];
  744. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  745. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  746. // NSDate *img_date = [NSDate date];
  747. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  748. // DebugLog(@"img_date time interval");
  749. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  750. itemjson[@"model"]=nsname;
  751. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  752. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  753. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  754. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  755. // itemjson[@"check"]=@"true";
  756. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  757. itemjson[@"count"]=[NSString stringWithFormat:@"%d",item_count];
  758. itemjson[@"unit_price"]=Price;
  759. itemjson[@"amount"]=Amount;
  760. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  761. itemjson[@"description"]=[NSString stringWithFormat:@"%@",nsdescription ];
  762. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  763. // itemjson[@"note"]=nsline_note;
  764. // NSDate *date2 = [NSDate date];
  765. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  766. // DebugLog(@"model_bundle time interval");
  767. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  768. itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  769. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=itemjson;
  770. row1count++;
  771. NSDictionary* combine =[self model_bundle:item_id db:db compute_part:false contactID:customerID];
  772. for(int c=0;c<[combine[@"count"] intValue];c++)
  773. {
  774. NSMutableDictionary* combine_item=[combine[[NSString stringWithFormat:@"item_%d",c]] mutableCopy];
  775. combine_item[@"count"]=[NSString stringWithFormat:@"%d",[combine_item[@"modulus"] intValue]*item_count];
  776. // combine_item[@"count"]=[NSNumber numberWithInt:[combine_item[@"modulus"] intValue]*item_count];
  777. double combine_amount=[combine_item[@"modulus"] intValue]*item_count* [combine_item[@"unit_price"] floatValue];
  778. credits+=combine_amount;
  779. combine_item[@"amount"]=[NSString stringWithFormat:@"%.2f",combine_amount];
  780. combine_item[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,c+2];
  781. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=combine_item;
  782. row1count++;
  783. }
  784. count++;
  785. // DebugLog(@"row time interval");
  786. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  787. }
  788. row1[@"count"]=[NSNumber numberWithInt:row1count];
  789. datasource[@"row1"]=row1;
  790. data[@"table_model"]=datasource;
  791. sqlite3_finalize(statement_cart);
  792. }
  793. NSMutableDictionary* fee = [[NSMutableDictionary alloc]init];
  794. NSMutableDictionary* fee_row0 = [[NSMutableDictionary alloc]init];
  795. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  796. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  797. // if(credits>0)
  798. fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  799. // else
  800. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": @""};
  801. if(shipping>0)
  802. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": [NSString stringWithFormat:@"%.2f",shipping]};
  803. else
  804. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": @""};
  805. if(lift_gate_value>0)
  806. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",lift_gate_value]};
  807. else
  808. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": @""};
  809. if(handling_fee_value>0)
  810. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",handling_fee_value]};
  811. else
  812. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": @""};
  813. float total = credits+shipping+lift_gate_value+handling_fee_value;
  814. if(total>0)
  815. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": [NSString stringWithFormat:@"%.2f",total]};
  816. else
  817. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": @""};
  818. fee_row0[@"count"]=[NSNumber numberWithInt:5];
  819. fee[@"row0"]=fee_row0;
  820. data[@"table_fee"]=fee;
  821. // NSString *sign_url = [self textAtColumn:51 statement:statement];
  822. // ret[@"sign_url"] = sign_url;
  823. //
  824. // customer_contact = [self textAtColumn:52 statement:statement];
  825. // customer_email = [self textAtColumn:53 statement:statement];
  826. // customer_phone = [self textAtColumn:54 statement:statement];
  827. // customer_fax = [self textAtColumn:55 statement:statement];
  828. //
  829. //
  830. // int offline_edit=sqlite3_column_int(statement, 56);
  831. //
  832. // ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  833. //
  834. // char *soid = (char*)sqlite3_column_text(statement, 1);
  835. // if(soid==nil)
  836. // soid= "";
  837. // nssoid= [[NSString alloc]initWithUTF8String:soid];
  838. // // so#
  839. // ret[@"so#"] = nssoid;
  840. //
  841. // char *poNumber = (char*)sqlite3_column_text(statement, 2);
  842. // if(poNumber==nil)
  843. // poNumber= "";
  844. // NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  845. //
  846. //
  847. //
  848. // char *create_time = (char*)sqlite3_column_text(statement, 3);
  849. // if(create_time==nil)
  850. // create_time= "";
  851. // NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  852. // nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  853. //
  854. // int status = sqlite3_column_int(statement, 4);
  855. // int erpStatus = sqlite3_column_int(statement, 49);
  856. // NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  857. //
  858. // // status
  859. // if (status > 1 && status != 3) {
  860. // status = erpStatus;
  861. // } else if (status == 3) {
  862. // status = 15;
  863. // }
  864. // ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  865. // ret[@"order_status"] = nsstatus;
  866. //
  867. //
  868. // char *company_name = (char*)sqlite3_column_text(statement, 5);
  869. // if(company_name==nil)
  870. // company_name= "";
  871. // NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  872. // // company name
  873. // ret[@"company_name"] = nscompany_name;
  874. //
  875. // char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  876. // if(customer_contact==nil)
  877. // customer_contact= "";
  878. // NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  879. //
  880. //
  881. // char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  882. // if(addr_1==nil)
  883. // addr_1="";
  884. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  885. //
  886. // char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  887. // if(addr_2==nil)
  888. // addr_2="";
  889. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  890. //
  891. //
  892. // char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  893. // if(addr_3==nil)
  894. // addr_3="";
  895. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  896. //
  897. //
  898. // char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  899. // if(addr_4==nil)
  900. // addr_4="";
  901. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  902. //
  903. // NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  904. // [arr_addr addObject:nsaddr_1];
  905. // [arr_addr addObject:nsaddr_2];
  906. // [arr_addr addObject:nsaddr_3];
  907. // [arr_addr addObject:nsaddr_4];
  908. // NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  909. //
  910. //
  911. // char *logist = (char*)sqlite3_column_text(statement, 11);
  912. // if(logist==nil)
  913. // logist= "";
  914. // NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  915. // if (status == -11 || status == 10 || status == 11) {
  916. // nslogist = [self textAtColumn:59 statement:statement];
  917. // };
  918. //
  919. // NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  920. // if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  921. // shipping = @"Shipping To Be Quoted";
  922. // } else {
  923. // shippingFee = sqlite3_column_double(statement, 12);
  924. // }
  925. //
  926. // // Shipping
  927. // ret[@"Shipping"] = shipping;
  928. //
  929. // int have_lift_gate = sqlite3_column_int(statement, 17);
  930. // lift_gate = sqlite3_column_double(statement, 13);
  931. // // Liftgate Fee(No loading dock)
  932. // if (!have_lift_gate) {
  933. // lift_gate = 0;
  934. // }
  935. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  936. // if (sqlite3_column_int(statement, 57)) {
  937. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  938. // }
  939. //
  940. // char *general_notes = (char*)sqlite3_column_text(statement, 14);
  941. // if(general_notes==nil)
  942. // general_notes= "";
  943. // NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  944. //
  945. // char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  946. // if(internal_notes==nil)
  947. // internal_notes= "";
  948. // NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  949. //
  950. // char *payment_type = (char*)sqlite3_column_text(statement, 16);
  951. // if(payment_type==nil)
  952. // payment_type= "";
  953. // NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  954. //
  955. //
  956. //
  957. // // order info
  958. // orderinfo = [self textFileName:@"order_info.html"];
  959. //
  960. //
  961. // orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  962. // orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  963. // orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  964. // orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  965. //
  966. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  967. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  968. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  969. // orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  970. // orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  971. // orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  972. // orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  973. // orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  974. //
  975. // NSString *payment = nil;
  976. // if([nspayment_type isEqualToString:@"Credit Card"])
  977. // {
  978. // payment = [self textFileName:@"creditcardpayment.html"];
  979. //
  980. // NSString *card_first_name = [self textAtColumn:37 statement:statement];
  981. // NSString *card_last_name = [self textAtColumn:38 statement:statement];
  982. // NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  983. // NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  984. // NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  985. // NSString *card_type = [self textAtColumn:42 statement:statement];
  986. // if (card_type.length > 0) { // 显示星号
  987. // card_type = @"****";
  988. // }
  989. // NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  990. // if (card_number.length > 0 && card_number.length > 4) {
  991. // for (int i = 0; i < card_number.length - 4; i++) {
  992. // card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  993. // }
  994. // } else {
  995. // card_number = @"";
  996. // }
  997. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  998. // NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  999. // NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  1000. // NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  1001. // if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  1002. // card_expiration = @"****";
  1003. // }
  1004. //
  1005. //
  1006. // NSString *card_city = [self textAtColumn:46 statement:statement];
  1007. // NSString *card_state = [self textAtColumn:47 statement:statement];
  1008. //
  1009. // payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  1010. // payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  1011. // payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  1012. // payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  1013. // payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  1014. // payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  1015. // payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  1016. // payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  1017. // payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  1018. // payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  1019. //
  1020. //
  1021. // }
  1022. // else
  1023. // {
  1024. // payment=[self textFileName:@"normalpayment.html"];
  1025. // payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  1026. // }
  1027. // orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  1028. //
  1029. // ret[@"result"]= [NSNumber numberWithInt:2];
  1030. //
  1031. // // more info
  1032. // moreInfo = [self textFileName:@"more_info.html"];
  1033. //
  1034. //
  1035. // // ShipToCompany_or_&nbsp
  1036. // NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  1037. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  1038. // NSString *shipToName = [self textAtColumn:19 statement:statement];
  1039. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  1040. // NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  1041. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  1042. //
  1043. //
  1044. // // ShipFromCompany_or_&nbsp
  1045. // NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  1046. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  1047. // NSString *shipFromName = [self textAtColumn:22 statement:statement];
  1048. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  1049. // NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  1050. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  1051. //
  1052. //
  1053. // // FreightBillToCompany_or_&nbsp
  1054. // NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  1055. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  1056. // NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  1057. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  1058. // NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  1059. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  1060. //
  1061. //
  1062. // // MerchandiseBillToCompany_or_&nbsp
  1063. // NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  1064. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  1065. // NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  1066. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  1067. // NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  1068. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  1069. //
  1070. //
  1071. // // ReturnToCompany_or_&nbsp
  1072. // NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  1073. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  1074. // NSString *returnToName = [self textAtColumn:31 statement:statement];
  1075. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  1076. // NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  1077. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  1078. // //
  1079. //
  1080. // // DebugLog(@"more info : %@",moreInfo);
  1081. //
  1082. // // handling fee
  1083. // handlingFee = sqlite3_column_double(statement, 33);
  1084. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  1085. // if (sqlite3_column_int(statement, 58)) {
  1086. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  1087. // }
  1088. // //
  1089. //
  1090. // // customer info
  1091. // customerID = [self textAtColumn:36 statement:statement];
  1092. //
  1093. // // mode
  1094. // ret[@"mode"] = appDelegate.mode;
  1095. //
  1096. // // model_count
  1097. // ret[@"model_count"] = @(0);
  1098. }
  1099. sqlite3_finalize(statement);
  1100. }
  1101. [iSalesDB close_db:db];
  1102. data[@"order_type"]=order_type;
  1103. /*
  1104. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  1105. // "customer_email" = "Shui Hu";
  1106. // "customer_fax" = "";
  1107. // "customer_first_name" = F;
  1108. // "customer_last_name" = L;
  1109. // "customer_name" = ",da He Xiang Dong Liu A";
  1110. // "customer_phone" = "Hey Xuan Feng";
  1111. contactInfo[@"customer_phone"] = customer_phone;
  1112. contactInfo[@"customer_fax"] = customer_fax;
  1113. contactInfo[@"customer_email"] = customer_email;
  1114. NSString *first_name = @"";
  1115. NSString *last_name = @"";
  1116. if ([customer_contact isEqualToString:@""]) {
  1117. } else if ([customer_contact containsString:@" "]) {
  1118. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  1119. first_name = [customer_contact substringToIndex:first_space_index];
  1120. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  1121. }
  1122. contactInfo[@"customer_first_name"] = first_name;
  1123. contactInfo[@"customer_last_name"] = last_name;
  1124. ret[@"customerInfo"] = contactInfo;
  1125. // models
  1126. if (nssoid) {
  1127. __block double TotalCuft = 0;
  1128. __block double TotalWeight = 0;
  1129. __block int TotalCarton = 0;
  1130. __block double allItemPrice = 0;
  1131. 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];
  1132. sqlite3 *db1 = [iSalesDB get_db];
  1133. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1134. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1135. int product_id = sqlite3_column_int(stmt, 0);
  1136. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  1137. int item_id = sqlite3_column_int(stmt, 7);
  1138. NSString* Price=nil;
  1139. if(str_price==nil)
  1140. {
  1141. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  1142. if(price==nil)
  1143. Price=@"No Price.";
  1144. else
  1145. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1146. }
  1147. else
  1148. {
  1149. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1150. }
  1151. double discount = sqlite3_column_double(stmt, 2);
  1152. int item_count = sqlite3_column_int(stmt, 3);
  1153. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  1154. NSString *nsline_note=nil;
  1155. if(line_note!=nil)
  1156. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1157. // char *name = (char*)sqlite3_column_text(stmt, 5);
  1158. // NSString *nsname = nil;
  1159. // if(name!=nil)
  1160. // nsname= [[NSString alloc]initWithUTF8String:name];
  1161. NSString *nsname = [self textAtColumn:5 statement:stmt];
  1162. // char *description = (char*)sqlite3_column_text(stmt, 6);
  1163. // NSString *nsdescription=nil;
  1164. // if(description!=nil)
  1165. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  1166. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  1167. // int stockUom = sqlite3_column_int(stmt, 8);
  1168. // int _id = sqlite3_column_int(stmt, 9);
  1169. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  1170. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1171. double weight=[bsubtotaljson[@"weight"] doubleValue];
  1172. int carton=[bsubtotaljson[@"carton"] intValue];
  1173. TotalCuft += cuft;
  1174. TotalWeight += weight;
  1175. TotalCarton += carton;
  1176. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  1177. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  1178. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  1179. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1180. itemjson[@"note"]=nsline_note;
  1181. itemjson[@"origin_price"] = Price;
  1182. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1183. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1184. itemjson[@"order_item_status"] = @""; // 暂时不处理
  1185. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  1186. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  1187. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  1188. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  1189. if(itemjson[@"combine"] != nil)
  1190. {
  1191. // int citem=0;
  1192. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  1193. for(int bc=0;bc<bcount;bc++)
  1194. {
  1195. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  1196. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  1197. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  1198. subTotal += uprice * modulus * item_count;
  1199. }
  1200. }
  1201. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  1202. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  1203. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  1204. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  1205. allItemPrice += subTotal;
  1206. }];
  1207. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  1208. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  1209. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  1210. // payments/Credits
  1211. // payments_and_credist = sqlite3_column_double(statement, 34);
  1212. payments_and_credist = allItemPrice;
  1213. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1214. // // total
  1215. // totalPrice = sqlite3_column_double(statement, 35);
  1216. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1217. } else {
  1218. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  1219. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  1220. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  1221. // payments/Credits
  1222. payments_and_credist = 0;
  1223. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1224. }
  1225. // total
  1226. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  1227. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1228. ret[@"order_info"]= orderinfo;
  1229. ret[@"more_order_info"] = moreInfo;
  1230. return [RAConvertor dict2data:ret];
  1231. */
  1232. DebugLog(@"debug sales order data:%@", [RAConvertor dict2string:data]);
  1233. return data;
  1234. }
  1235. +(NSData*) offline_request_salesorder :(NSMutableDictionary *) params
  1236. {
  1237. NSString* template_file=nil;
  1238. switch([params[@"thumb"] intValue])
  1239. {
  1240. case 0:
  1241. template_file= @"so.json";
  1242. break;
  1243. case 1:
  1244. template_file= @"so_thumb.json";
  1245. break;
  1246. default:
  1247. template_file= @"so.json";
  1248. }
  1249. NSMutableDictionary* data = [self prepare_salesorder_data:params[@"order_code"] type:[params[@"thumb"] intValue]];
  1250. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1251. if(data[@"table_model"][@"row2"]==nil)
  1252. template[@"pages"][@"page_0"][@"contents"][@"item_0"][@"rows"][@"count"]=[NSNumber numberWithInt:1];
  1253. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1254. DebugLog(@"%@",file);
  1255. // return nil;
  1256. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1257. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1258. if (file) {
  1259. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1260. [dic setObject:file forKey:@"pdf_path"];
  1261. dic[@"isLocalFile"]=@"true";
  1262. return [RAConvertor dict2data:dic];
  1263. }
  1264. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1265. dic[@"isLocalFile"]=@"true";
  1266. return [RAConvertor dict2data:dic];
  1267. }
  1268. +(NSData*) offline_request_tearsheet :(NSMutableDictionary *) params
  1269. {
  1270. NSString* template_file=nil;
  1271. switch([params[@"pdf_style"] intValue])
  1272. {
  1273. case 0:
  1274. template_file= @"portfolio_2x3.json";
  1275. break;
  1276. case 1:
  1277. template_file= @"portfolio_3x2.json";
  1278. break;
  1279. default:
  1280. template_file= @"portfolio_3x2.json";
  1281. }
  1282. NSMutableDictionary* data = [self prepare_portfolio_data:params];
  1283. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1284. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1285. DebugLog(@"%@",file);
  1286. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1287. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1288. if (file) {
  1289. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1290. [dic setObject:file forKey:@"pdf_path"];
  1291. dic[@"isLocalFile"]=@"true";
  1292. return [RAConvertor dict2data:dic];
  1293. }
  1294. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1295. dic[@"isLocalFile"]=@"true";
  1296. return [RAConvertor dict2data:dic];
  1297. }
  1298. +(NSString*) get_offline_soid:(sqlite3*)db
  1299. {
  1300. NSString* soid=nil;
  1301. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  1302. [formatter setDateFormat:@"yyMMdd"];
  1303. NSString* date = [formatter stringFromDate:[NSDate date]];
  1304. for(int i=1;i<999;i++)
  1305. {
  1306. soid=[NSString stringWithFormat:@"OFF%@%4d",date,i];
  1307. soid= [soid stringByReplacingOccurrencesOfString:@" " withString:@"0"];
  1308. int count=[iSalesDB get_recordcount:db table:@"offline_order" where:[NSString stringWithFormat:@"so_id='%@'",soid]];
  1309. if(count==0)
  1310. return soid;
  1311. }
  1312. return nil;
  1313. }
  1314. +(NSArray*) enumOfflineOrder
  1315. {
  1316. NSMutableArray* ret = [[NSMutableArray alloc] init];
  1317. sqlite3 *db = [iSalesDB get_db];
  1318. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  1319. sqlite3_stmt * statement;
  1320. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1321. if ( dbresult== SQLITE_OK)
  1322. {
  1323. while (sqlite3_step(statement) == SQLITE_ROW)
  1324. {
  1325. char *so_id = (char*)sqlite3_column_text(statement, 0);
  1326. NSString* nsso_id=nil;
  1327. if(so_id!=nil)
  1328. {
  1329. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  1330. }
  1331. [ret addObject:nsso_id];
  1332. }
  1333. sqlite3_finalize(statement);
  1334. }
  1335. [iSalesDB close_db:db];
  1336. return ret;
  1337. }
  1338. //+(void) uploadFile:(NSString*) file
  1339. //{
  1340. // NSData* data = [NSData dataWithContentsOfFile: file];
  1341. // UIApplication * app = [UIApplication sharedApplication];
  1342. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1343. //
  1344. //
  1345. // AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  1346. //
  1347. //
  1348. //
  1349. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  1350. //
  1351. // if(appDelegate.user!=nil)
  1352. // [params setValue:appDelegate.user forKey:@"user"];
  1353. // // if(appDelegate.contact_id!=nil)
  1354. // // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  1355. // if(appDelegate.password!=nil)
  1356. // [params setValue:appDelegate.password forKey:@"password"];
  1357. //
  1358. //
  1359. // NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_OFFLINE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  1360. // [formData appendPartWithFileData:data name:@"upfile" fileName:[file lastPathComponent] mimeType:@"application/zip"];
  1361. // } error:nil];
  1362. //
  1363. //
  1364. //
  1365. // AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  1366. //
  1367. // NSProgress *progress = nil;
  1368. //
  1369. // NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  1370. //
  1371. //
  1372. //
  1373. // if (error) {
  1374. //
  1375. // NSString* err_msg = [error localizedDescription];
  1376. // DebugLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  1377. //
  1378. //
  1379. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1380. //
  1381. // DebugLog(@"data string: %@",str);
  1382. // [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  1383. //
  1384. // } else {
  1385. // DebugLog(@"response ");
  1386. //
  1387. //
  1388. //
  1389. // NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  1390. //
  1391. // // 再将NSData转为字符串
  1392. // NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  1393. //
  1394. // // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1395. //
  1396. // DebugLog(@"data string: %@",jsonStr);
  1397. //
  1398. // NSDictionary* json = responseObject;
  1399. //
  1400. //
  1401. // if([[json valueForKey:@"result"] intValue]==2)
  1402. // {
  1403. //// NSString* img_url_down = json[@"img_url_aname"];
  1404. //// NSString* img_url_up = json[@"img_url"];
  1405. //
  1406. //
  1407. // }
  1408. // else
  1409. // {
  1410. // [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload file" controller:nil] ;
  1411. // }
  1412. //
  1413. //
  1414. // }
  1415. // // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  1416. // }];
  1417. //
  1418. //
  1419. //
  1420. //
  1421. // // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  1422. // //
  1423. //
  1424. //
  1425. //
  1426. //
  1427. //
  1428. //
  1429. // [uploadTask resume];
  1430. //
  1431. //}
  1432. +(NSDictionary *) prepareUpload:(NSArray*) arr_order
  1433. {
  1434. NSString* serial= [[NSUUID UUID] UUIDString];
  1435. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1436. NSString *cachefolder = [paths objectAtIndex:0];
  1437. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1438. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1439. NSFileManager* fileManager = [NSFileManager defaultManager];
  1440. BOOL bdir=YES;
  1441. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  1442. {
  1443. NSError *error = nil;
  1444. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  1445. if(!bsuccess)
  1446. DebugLog(@"Create UPLOAD folder failed");
  1447. }
  1448. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1449. ret[@"contact"]=[self prepareContact:serial];
  1450. ret[@"wishlist"]=[self prepareWishlist:serial];
  1451. ret[@"order"]=[self prepareOrder:serial soid:arr_order];
  1452. ret[@"portfolio"] = [self preparePortfolio:serial];
  1453. ret[@"view_portfolio"] = [self preparePDF:serial];
  1454. NSString* str= [RAConvertor dict2string:ret];
  1455. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  1456. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  1457. NSError *error=nil;
  1458. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  1459. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  1460. ZipArchive* zip = [[ZipArchive alloc] init];
  1461. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  1462. for(int i=0;i<arr_files.count;i++)
  1463. {
  1464. NSString* file=arr_files[i];
  1465. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  1466. }
  1467. if( ![zip CloseZipFile2] )
  1468. {
  1469. zippath = @"";
  1470. }
  1471. ret[@"file"]=zippath;
  1472. return ret;
  1473. }
  1474. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  1475. {
  1476. if(filename.length==0)
  1477. return false;
  1478. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1479. bool ret=false;
  1480. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1481. NSString *cachefolder = [paths objectAtIndex:0];
  1482. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  1483. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  1484. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1485. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1486. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  1487. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  1488. NSFileManager* fileManager = [NSFileManager defaultManager];
  1489. BOOL bdir=NO;
  1490. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  1491. {
  1492. NSError *error = nil;
  1493. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  1494. {
  1495. ret=false;
  1496. }
  1497. else
  1498. {
  1499. ret=true;
  1500. }
  1501. }
  1502. return ret;
  1503. }
  1504. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  1505. {
  1506. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1507. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  1508. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  1509. NSString* where=@" 1=1";
  1510. if (ver!=nil) {
  1511. where=@"is_dirty=1";
  1512. }
  1513. sqlite3 *db = [iSalesDB get_db];
  1514. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  1515. sqlite3_stmt * statement;
  1516. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1517. int count=0;
  1518. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1519. if ( dbresult== SQLITE_OK)
  1520. {
  1521. while (sqlite3_step(statement) == SQLITE_ROW)
  1522. {
  1523. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1524. int _id = sqlite3_column_int(statement, 0);
  1525. int product_id = sqlite3_column_int(statement, 1);
  1526. int item_id = sqlite3_column_int(statement, 2);
  1527. int qty = sqlite3_column_int(statement, 3);
  1528. char *create_time = (char*)sqlite3_column_text(statement, 4);
  1529. NSString* nscreate_time=nil;
  1530. if(create_time!=nil)
  1531. {
  1532. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  1533. }
  1534. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  1535. NSString* nsmodify_time=nil;
  1536. if(modify_time!=nil)
  1537. {
  1538. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  1539. }
  1540. int is_delete = sqlite3_column_int(statement, 6);
  1541. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1542. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  1543. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1544. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  1545. itemjson[@"create_time"]=nscreate_time;
  1546. itemjson[@"modify_time"]=nsmodify_time;
  1547. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  1548. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1549. count++;
  1550. }
  1551. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1552. sqlite3_finalize(statement);
  1553. }
  1554. ret[@"count"]=[NSNumber numberWithInt:count ];
  1555. [iSalesDB close_db:db];
  1556. return ret;
  1557. }
  1558. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode db:(sqlite3*)db
  1559. {
  1560. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1561. UIApplication * app = [UIApplication sharedApplication];
  1562. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1563. 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 ];
  1564. // 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 ];
  1565. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  1566. DebugLog(@"offline_login sql:%@",sqlQuery);
  1567. sqlite3_stmt * statement;
  1568. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1569. int count=0;
  1570. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1571. if ( dbresult== SQLITE_OK)
  1572. {
  1573. while (sqlite3_step(statement) == SQLITE_ROW)
  1574. {
  1575. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1576. char *customer_cid = (char *)sqlite3_column_text(statement, 10);
  1577. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  1578. int product_id = sqlite3_column_int(statement, 0);
  1579. char *str_price = (char*)sqlite3_column_text(statement, 1);
  1580. int item_id = sqlite3_column_int(statement, 7);
  1581. NSString* Price=nil;
  1582. if(str_price==nil)
  1583. {
  1584. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1585. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db];
  1586. if(price==nil)
  1587. Price=@"No Price.";
  1588. else
  1589. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1590. }
  1591. else
  1592. {
  1593. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1594. }
  1595. double discount = sqlite3_column_double(statement, 2);
  1596. int item_count = sqlite3_column_int(statement, 3);
  1597. char *line_note = (char*)sqlite3_column_text(statement, 4);
  1598. NSString *nsline_note=nil;
  1599. if(line_note!=nil)
  1600. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1601. char *name = (char*)sqlite3_column_text(statement, 5);
  1602. NSString *nsname=nil;
  1603. if(name!=nil)
  1604. nsname= [[NSString alloc]initWithUTF8String:name];
  1605. char *description = (char*)sqlite3_column_text(statement, 6);
  1606. NSString *nsdescription=nil;
  1607. if(description!=nil)
  1608. nsdescription= [[NSString alloc]initWithUTF8String:description];
  1609. // int stockUom = sqlite3_column_int(statement, 8);
  1610. // int _id = sqlite3_column_int(statement, 9);
  1611. //
  1612. //
  1613. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  1614. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1615. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  1616. // int carton=[bsubtotaljson[@"carton"] intValue];
  1617. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1618. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  1619. itemjson[@"model"]=nsname;
  1620. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  1621. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  1622. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  1623. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  1624. // itemjson[@"check"]=@"true";
  1625. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1626. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  1627. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  1628. itemjson[@"unit_price"]=Price;
  1629. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  1630. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1631. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  1632. itemjson[@"note"]=nsline_note;
  1633. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:customerID];
  1634. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1635. count++;
  1636. }
  1637. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1638. sqlite3_finalize(statement);
  1639. }
  1640. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1641. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  1642. return ret;
  1643. }
  1644. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid
  1645. {
  1646. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1647. sqlite3 *db = [iSalesDB get_db];
  1648. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  1649. // for(int i=0;i<arr_soid.count;i++)
  1650. sqlite3_stmt * statement;
  1651. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1652. //int count=0;
  1653. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1654. if ( dbresult== SQLITE_OK)
  1655. {
  1656. int count=0;
  1657. while (sqlite3_step(statement) == SQLITE_ROW)
  1658. {
  1659. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1660. int _id = sqlite3_column_int(statement, 0);
  1661. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  1662. NSString* nssync_data=nil;
  1663. if(sync_data!=nil)
  1664. {
  1665. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1666. }
  1667. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1668. NSString* nsimg_1=nil;
  1669. if(img_1!=nil)
  1670. {
  1671. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1672. [self copy_upImg:serial file:nsimg_1];
  1673. }
  1674. char *so_no = (char*)sqlite3_column_text(statement, 3);
  1675. NSString* nsso_no=nil;
  1676. if(so_no!=nil)
  1677. {
  1678. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  1679. }
  1680. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  1681. itemjson[@"order_type"]=@"submit order";
  1682. else
  1683. itemjson[@"order_type"]=@"archive order";
  1684. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1685. itemjson[@"json_data"]= nssync_data;
  1686. itemjson[@"order_items"]=[self preparecart:nsso_no db:db];
  1687. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1688. count++;
  1689. }
  1690. ret[@"count"]=[NSNumber numberWithInt:count ];
  1691. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1692. }
  1693. sqlite3_finalize(statement);
  1694. [iSalesDB close_db:db];
  1695. return ret;
  1696. }
  1697. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  1698. {
  1699. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1700. sqlite3 *db = [iSalesDB get_db];
  1701. // UIApplication * app = [UIApplication sharedApplication];
  1702. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1703. 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";
  1704. sqlite3_stmt * statement;
  1705. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1706. int count=0;
  1707. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1708. if ( dbresult== SQLITE_OK)
  1709. {
  1710. while (sqlite3_step(statement) == SQLITE_ROW)
  1711. {
  1712. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1713. int _id = sqlite3_column_int(statement, 0);
  1714. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  1715. NSString* nsimg_0=nil;
  1716. if(img_0!=nil)
  1717. {
  1718. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  1719. [self copy_upImg:serial file:nsimg_0];
  1720. }
  1721. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1722. NSString* nsimg_1=nil;
  1723. if(img_1!=nil)
  1724. {
  1725. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1726. [self copy_upImg:serial file:nsimg_1];
  1727. }
  1728. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  1729. NSString* nsimg_2=nil;
  1730. if(img_2!=nil)
  1731. {
  1732. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  1733. [self copy_upImg:serial file:nsimg_2];
  1734. }
  1735. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  1736. NSString* nssync_data=nil;
  1737. if(sync_data!=nil)
  1738. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1739. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1740. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1741. itemjson[@"json_data"]= nssync_data;
  1742. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1743. count++;
  1744. }
  1745. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1746. sqlite3_finalize(statement);
  1747. }
  1748. ret[@"count"]=[NSNumber numberWithInt:count ];
  1749. [iSalesDB close_db:db];
  1750. return ret;
  1751. }
  1752. +(bool) check_offlinedata
  1753. {
  1754. UIApplication * app = [UIApplication sharedApplication];
  1755. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1756. sqlite3 *db = [iSalesDB get_db];
  1757. NSString * where=@"1=1";
  1758. // if(appDelegate.user!=nil)
  1759. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  1760. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  1761. [iSalesDB close_db:db];
  1762. if(count==0)
  1763. {
  1764. return false;
  1765. }
  1766. return true;
  1767. //
  1768. //[iSalesDB close_db:db];
  1769. }
  1770. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  1771. {
  1772. UIApplication * app = [UIApplication sharedApplication];
  1773. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1774. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1775. sqlite3 *db = [iSalesDB get_db];
  1776. NSString* collectId=params[@"collectId"];
  1777. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  1778. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  1779. [iSalesDB execSql:sqlQuery db:db];
  1780. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  1781. [iSalesDB close_db:db];
  1782. appDelegate.wish_count =count;
  1783. [appDelegate update_count_mark];
  1784. ret[@"result"]= [NSNumber numberWithInt:2];
  1785. return ret;
  1786. }
  1787. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  1788. //{
  1789. //
  1790. // UIApplication * app = [UIApplication sharedApplication];
  1791. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1792. //
  1793. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1794. // sqlite3 *db = [iSalesDB get_db];
  1795. // NSString* product_id=params[@"product_id"];
  1796. //
  1797. //
  1798. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  1799. //
  1800. //
  1801. // for(int i=0;i<arr.count;i++)
  1802. // {
  1803. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  1804. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  1805. // if(count==0)
  1806. // {
  1807. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  1808. // [iSalesDB execSql:sqlQuery db:db];
  1809. // }
  1810. // }
  1811. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  1812. // [iSalesDB close_db:db];
  1813. //
  1814. // appDelegate.wish_count =count;
  1815. //
  1816. // [appDelegate update_count_mark];
  1817. // ret[@"result"]= [NSNumber numberWithInt:2];
  1818. // return ret;
  1819. // //
  1820. // //return ret;
  1821. //}
  1822. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  1823. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  1824. DebugLog(@"time interval: %lf",interval);
  1825. }
  1826. +(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  1827. {
  1828. UIApplication * app = [UIApplication sharedApplication];
  1829. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1830. int sort = [[params objectForKey:@"sort"] intValue];
  1831. NSString *sort_str = @"";
  1832. switch (sort) {
  1833. case 0:{
  1834. sort_str = @"order by w.modify_time desc";
  1835. }
  1836. break;
  1837. case 1:{
  1838. sort_str = @"order by w.modify_time asc";
  1839. }
  1840. break;
  1841. case 2:{
  1842. sort_str = @"order by m.name asc";
  1843. }
  1844. break;
  1845. case 3:{
  1846. sort_str = @"order by m.name desc";
  1847. }
  1848. break;
  1849. case 4:{
  1850. sort_str = @"order by m.description asc";
  1851. }
  1852. break;
  1853. default:
  1854. break;
  1855. }
  1856. NSString* user = appDelegate.user;
  1857. sqlite3 *db = [iSalesDB get_db];
  1858. // order by w.create_time
  1859. 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];
  1860. // 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];
  1861. sqlite3_stmt * statement;
  1862. NSDate *date1 = [NSDate date];
  1863. NSDate *date2 = nil;
  1864. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1865. int count=0;
  1866. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1867. {
  1868. while (sqlite3_step(statement) == SQLITE_ROW)
  1869. {
  1870. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1871. int product_id = sqlite3_column_double(statement, 0);
  1872. char *description = (char*)sqlite3_column_text(statement, 1);
  1873. if(description==nil)
  1874. description= "";
  1875. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  1876. int item_id = sqlite3_column_double(statement, 2);
  1877. NSDate *date_image = [NSDate date];
  1878. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  1879. printf("image : ");
  1880. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  1881. // char *url = (char*)sqlite3_column_text(statement, 3);
  1882. // if(url==nil)
  1883. // url="";
  1884. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1885. int qty = sqlite3_column_int(statement, 3);
  1886. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  1887. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  1888. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  1889. item[@"description"]= nsdescription;
  1890. item[@"img"]= nsurl;
  1891. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  1892. count++;
  1893. }
  1894. printf("total time:");
  1895. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  1896. ret[@"count"]= [NSNumber numberWithInt:count];
  1897. ret[@"total_count"]= [NSNumber numberWithInt:count];
  1898. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  1899. ret[@"result"]= [NSNumber numberWithInt:2];
  1900. appDelegate.wish_count =count;
  1901. [appDelegate update_count_mark];
  1902. sqlite3_finalize(statement);
  1903. }
  1904. [iSalesDB close_db:db];
  1905. return ret;
  1906. }
  1907. +(NSDictionary*) offline_notimpl
  1908. {
  1909. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1910. ret[@"result"]=@"8";
  1911. ret[@"err_msg"]=@"offline mode does not support this function.";
  1912. return ret;
  1913. }
  1914. +(NSDictionary*) offline_home
  1915. {
  1916. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1917. NSString *cachefolder = [paths objectAtIndex:0];
  1918. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  1919. NSData* json =nil;
  1920. json=[NSData dataWithContentsOfFile:img_cache];
  1921. if(json==nil)
  1922. return nil;
  1923. NSError *error=nil;
  1924. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1925. return menu;
  1926. }
  1927. +(NSDictionary*) offline_category_menu
  1928. {
  1929. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1930. NSString *cachefolder = [paths objectAtIndex:0];
  1931. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  1932. NSData* json =nil;
  1933. json=[NSData dataWithContentsOfFile:img_cache];
  1934. if(json==nil)
  1935. return nil;
  1936. NSError *error=nil;
  1937. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1938. return menu;
  1939. }
  1940. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  1941. {
  1942. NSString* offline_command=params[@"offline_Command"];
  1943. NSDictionary* ret=nil;
  1944. if([offline_command isEqualToString:@"model_NIYMAL"])
  1945. {
  1946. NSString* category = params[@"category"];
  1947. ret = [self refresh_model_NIYMAL:category];
  1948. }
  1949. return ret;
  1950. }
  1951. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  1952. {
  1953. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1954. [ret setValue:@"2" forKey:@"result"];
  1955. [ret setValue:@"Regular Mode" forKey:@"mode"];
  1956. sqlite3* db= [iSalesDB get_db];
  1957. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  1958. [iSalesDB close_db:db];
  1959. [ret setObject:detail1_section forKey:@"detail_1"];
  1960. return ret;
  1961. }
  1962. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id db:(sqlite3*)db
  1963. {
  1964. UIApplication * app = [UIApplication sharedApplication];
  1965. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1966. // NSArray* arr1 = [self get_user_all_price_type];
  1967. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  1968. // NSSet *set1 = [NSSet setWithArray:arr1];
  1969. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  1970. // if(appDelegate.contact_id==nil)
  1971. // set2=[set1 mutableCopy];
  1972. // else
  1973. // [set2 intersectsSet:set1];
  1974. // NSArray *retarr = [set2 allObjects];
  1975. NSString* whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  1976. // sqlite3 *db = [iSalesDB get_db];
  1977. NSString* sqlQuery = nil;
  1978. if(appDelegate.contact_id==nil)
  1979. {
  1980. if(!appDelegate.bLogin)
  1981. return nil;
  1982. 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];
  1983. }
  1984. else
  1985. 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];
  1986. sqlite3_stmt * statement;
  1987. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1988. int count=0;
  1989. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1990. {
  1991. while (sqlite3_step(statement) == SQLITE_ROW)
  1992. {
  1993. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1994. char *name = (char*)sqlite3_column_text(statement, 0);
  1995. if(name==nil)
  1996. name="";
  1997. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1998. // double price = sqlite3_column_double(statement, 1);
  1999. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  2000. // if(isnull==nil)
  2001. // item[nsname]= @"No Price";
  2002. // else
  2003. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  2004. char *price = (char*)sqlite3_column_text(statement, 1);
  2005. if(price!=nil)
  2006. {
  2007. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2008. nsprice=[AESCrypt fastdecrypt:nsprice];
  2009. if(nsprice.length>0)
  2010. {
  2011. double dp= [nsprice doubleValue];
  2012. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  2013. }
  2014. }
  2015. else
  2016. {
  2017. item[nsname]= @"No Price";
  2018. }
  2019. // int type= sqlite3_column_int(statement, 2);
  2020. //item[@"type"]=@"price";
  2021. // item[nsname]= nsprice;
  2022. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2023. count++;
  2024. }
  2025. ret[@"count"]= [NSNumber numberWithInt:count];
  2026. sqlite3_finalize(statement);
  2027. }
  2028. // [iSalesDB close_db:db];
  2029. return ret;
  2030. }
  2031. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  2032. //{
  2033. // NSArray* arr1 = [self get_user_all_price_type:db];
  2034. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2035. //
  2036. // // NSSet *set1 = [NSSet setWithArray:arr1];
  2037. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2038. // // [set2 intersectsSet:set1];
  2039. // //
  2040. // //
  2041. // // NSArray *retarr = [set2 allObjects];
  2042. //
  2043. // NSString* whereprice=nil;
  2044. // if(contact_id==nil)
  2045. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2046. // else
  2047. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2048. //
  2049. //
  2050. // // sqlite3 *db = [iSalesDB get_db];
  2051. //
  2052. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  2053. // sqlite3_stmt * statement;
  2054. //
  2055. //
  2056. // NSNumber* ret = nil;
  2057. // double dprice=DBL_MAX;
  2058. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2059. // {
  2060. //
  2061. //
  2062. // while (sqlite3_step(statement) == SQLITE_ROW)
  2063. // {
  2064. //
  2065. // // double val = sqlite3_column_double(statement, 0);
  2066. // char *price = (char*)sqlite3_column_text(statement, 0);
  2067. // if(price!=nil)
  2068. // {
  2069. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2070. // nsprice=[AESCrypt fastdecrypt:nsprice];
  2071. // if(nsprice.length>0)
  2072. // {
  2073. // double dp= [nsprice doubleValue];
  2074. // if(dp<dprice)
  2075. // dprice=dp;
  2076. // }
  2077. // }
  2078. // }
  2079. //
  2080. //
  2081. //
  2082. //
  2083. // sqlite3_finalize(statement);
  2084. //
  2085. //
  2086. //
  2087. //
  2088. // }
  2089. //
  2090. // // [iSalesDB close_db:db];
  2091. //
  2092. // if(dprice==DBL_MAX)
  2093. // ret= nil;
  2094. // else
  2095. // ret= [NSNumber numberWithDouble:dprice];
  2096. // return ret;
  2097. //}
  2098. +(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  2099. {
  2100. NSArray* arr1 = [self get_user_all_price_type:db];
  2101. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2102. NSString* whereprice=nil;
  2103. if(contact_id==nil)
  2104. whereprice=[RAConvertor arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2105. else
  2106. whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2107. // sqlite3 *db = [iSalesDB get_db];
  2108. NSString *productIdCondition = @"1 = 1";
  2109. if (product_id) {
  2110. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  2111. }
  2112. NSString *itemIdCondition = @"";
  2113. if (item_id) {
  2114. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  2115. }
  2116. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  2117. sqlite3_stmt * statement;
  2118. NSNumber* ret = nil;
  2119. double dprice=DBL_MAX;
  2120. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2121. {
  2122. while (sqlite3_step(statement) == SQLITE_ROW)
  2123. {
  2124. // double val = sqlite3_column_double(statement, 0);
  2125. char *price = (char*)sqlite3_column_text(statement, 0);
  2126. if(price!=nil)
  2127. {
  2128. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2129. nsprice=[AESCrypt fastdecrypt:nsprice];
  2130. if(nsprice.length>0)
  2131. {
  2132. double dp= [nsprice doubleValue];
  2133. if(dp<dprice)
  2134. dprice=dp;
  2135. }
  2136. }
  2137. }
  2138. sqlite3_finalize(statement);
  2139. }
  2140. // [iSalesDB close_db:db];
  2141. if(dprice==DBL_MAX)
  2142. ret= nil;
  2143. else
  2144. ret= [NSNumber numberWithDouble:dprice];
  2145. return ret;
  2146. }
  2147. +(NSArray*) get_user_all_price_type:(sqlite3*)db
  2148. {
  2149. NSArray* ret=nil;
  2150. // sqlite3 *db = [iSalesDB get_db];
  2151. // no customer assigned , use login user contact_id
  2152. UIApplication * app = [UIApplication sharedApplication];
  2153. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2154. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",appDelegate.user];
  2155. sqlite3_stmt * statement;
  2156. // int count=0;
  2157. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2158. {
  2159. if (sqlite3_step(statement) == SQLITE_ROW)
  2160. {
  2161. char *val = (char*)sqlite3_column_text(statement, 0);
  2162. if(val==nil)
  2163. val="";
  2164. NSString* price = [[NSString alloc]initWithUTF8String:val];
  2165. ret=[RAUtils string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  2166. }
  2167. sqlite3_finalize(statement);
  2168. }
  2169. // [iSalesDB close_db:db];
  2170. return ret;
  2171. }
  2172. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id db:(sqlite3*)db
  2173. {
  2174. // sqlite3 *db = [iSalesDB get_db];
  2175. if(contact_id==nil)
  2176. {
  2177. // no customer assigned , use login user contact_id
  2178. UIApplication * app = [UIApplication sharedApplication];
  2179. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2180. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",appDelegate.user];
  2181. sqlite3_stmt * statement;
  2182. // int count=0;
  2183. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2184. {
  2185. if (sqlite3_step(statement) == SQLITE_ROW)
  2186. {
  2187. char *val = (char*)sqlite3_column_text(statement, 0);
  2188. if(val==nil)
  2189. val="";
  2190. contact_id = [[NSString alloc]initWithUTF8String:val];
  2191. }
  2192. sqlite3_finalize(statement);
  2193. }
  2194. if(contact_id.length<=0)
  2195. {
  2196. // [iSalesDB close_db:db];
  2197. return nil;
  2198. }
  2199. }
  2200. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  2201. sqlite3_stmt * statement;
  2202. NSArray* ret=nil;
  2203. // int count=0;
  2204. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2205. {
  2206. if (sqlite3_step(statement) == SQLITE_ROW)
  2207. {
  2208. char *price_type = (char*)sqlite3_column_text(statement, 0);
  2209. if(price_type==nil)
  2210. price_type="";
  2211. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  2212. if(nsprice_type.length>0)
  2213. ret=[RAUtils string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  2214. else
  2215. ret=nil;
  2216. }
  2217. sqlite3_finalize(statement);
  2218. }
  2219. // [iSalesDB close_db:db];
  2220. return ret;
  2221. }
  2222. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  2223. {
  2224. NSString* ret= nil;
  2225. // sqlite3 *db = [iSalesDB get_db];
  2226. NSString *sqlQuery = nil;
  2227. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  2228. // 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;
  2229. // 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
  2230. if(product_id==nil && model_name)
  2231. 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;
  2232. else if (product_id)
  2233. 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
  2234. sqlite3_stmt * statement;
  2235. // int count=0;
  2236. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2237. {
  2238. if (sqlite3_step(statement) == SQLITE_ROW)
  2239. {
  2240. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  2241. if(imgurl==nil)
  2242. imgurl="";
  2243. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  2244. ret=nsimgurl;
  2245. }
  2246. sqlite3_finalize(statement);
  2247. }
  2248. else
  2249. {
  2250. [ret setValue:@"8" forKey:@"result"];
  2251. }
  2252. // [iSalesDB close_db:db];
  2253. DebugLog(@"data string: %@",ret );
  2254. return ret;
  2255. }
  2256. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name
  2257. {
  2258. NSString* ret= nil;
  2259. sqlite3 *db = [iSalesDB get_db];
  2260. NSString *sqlQuery = nil;
  2261. if(product_id==nil)
  2262. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  2263. else
  2264. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  2265. sqlite3_stmt * statement;
  2266. // int count=0;
  2267. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2268. {
  2269. if (sqlite3_step(statement) == SQLITE_ROW)
  2270. {
  2271. char *default_category = (char*)sqlite3_column_text(statement, 0);
  2272. if(default_category==nil)
  2273. default_category="";
  2274. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2275. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  2276. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  2277. ret=nsdefault_category;
  2278. }
  2279. sqlite3_finalize(statement);
  2280. }
  2281. else
  2282. {
  2283. [ret setValue:@"8" forKey:@"result"];
  2284. }
  2285. [iSalesDB close_db:db];
  2286. DebugLog(@"data string: %@",ret );
  2287. return ret;
  2288. }
  2289. + (NSString *) categoryIdOfProduct:(NSString *)product_id name:(NSString *)product_name {
  2290. NSString *sql = nil;
  2291. if (product_id != nil) {
  2292. sql = [NSString stringWithFormat:@"select category from product where product_id = '%@';",product_id];
  2293. } else {
  2294. sql = [NSString stringWithFormat:@"select category from product where name = '%@';",product_name];
  2295. }
  2296. NSString *result_set = [iSalesDB jk_queryText:sql];
  2297. result_set = [result_set stringByReplacingOccurrencesOfString:@"%" withString:@""];
  2298. result_set = [result_set stringByReplacingOccurrencesOfString:@"#" withString:@""];
  2299. return result_set;
  2300. }
  2301. +(NSDictionary*) offline_model :(NSMutableDictionary *) params
  2302. {
  2303. NSString* model_name = [params valueForKey:@"product_name"];
  2304. NSString* product_id = [params valueForKey:@"product_id"];
  2305. NSString* category = [params valueForKey:@"category"];
  2306. NSString *default_category_id = [self model_default_category:product_id model_name:model_name];
  2307. if(category==nil) {
  2308. category = default_category_id;
  2309. } else {
  2310. NSArray *arr_0 = [category componentsSeparatedByString:@","];
  2311. // 参数重有多个category id
  2312. if (arr_0.count > 1) {
  2313. NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  2314. // 取交集
  2315. NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  2316. NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  2317. [set_0 intersectSet:set_1];
  2318. if (set_0.count == 1) {
  2319. category = (NSString *)[set_0 anyObject];
  2320. } else {
  2321. if ([set_0 containsObject:default_category_id]) {
  2322. category = default_category_id;
  2323. } else {
  2324. category = (NSString *)[set_0 anyObject];
  2325. }
  2326. }
  2327. }
  2328. }
  2329. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2330. sqlite3 *db = [iSalesDB get_db];
  2331. // int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  2332. NSString *sqlQuery = nil;
  2333. if(product_id==nil)
  2334. 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='%@';
  2335. else
  2336. 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=%@;
  2337. sqlite3_stmt * statement;
  2338. [ret setValue:@"2" forKey:@"result"];
  2339. [ret setValue:@"3" forKey:@"detail_section_count"];
  2340. // int count=0;
  2341. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2342. {
  2343. if (sqlite3_step(statement) == SQLITE_ROW)
  2344. {
  2345. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2346. char *name = (char*)sqlite3_column_text(statement, 0);
  2347. if(name==nil)
  2348. name="";
  2349. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2350. char *description = (char*)sqlite3_column_text(statement, 1);
  2351. if(description==nil)
  2352. description="";
  2353. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2354. int product_id = sqlite3_column_int(statement, 2);
  2355. char *color = (char*)sqlite3_column_text(statement, 3);
  2356. if(color==nil)
  2357. color="";
  2358. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  2359. //
  2360. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  2361. // if(legcolor==nil)
  2362. // legcolor="";
  2363. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  2364. //
  2365. //
  2366. int availability = sqlite3_column_int(statement, 5);
  2367. //
  2368. int incoming_stock = sqlite3_column_int(statement, 6);
  2369. char *demension = (char*)sqlite3_column_text(statement, 7);
  2370. if(demension==nil)
  2371. demension="";
  2372. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  2373. // ,,,,,,,,,
  2374. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  2375. if(seat_height==nil)
  2376. seat_height="";
  2377. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  2378. char *material = (char*)sqlite3_column_text(statement, 9);
  2379. if(material==nil)
  2380. material="";
  2381. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  2382. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  2383. if(box_dim==nil)
  2384. box_dim="";
  2385. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  2386. char *volume = (char*)sqlite3_column_text(statement, 11);
  2387. if(volume==nil)
  2388. volume="";
  2389. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  2390. double weight = sqlite3_column_double(statement, 12);
  2391. char *model_set = (char*)sqlite3_column_text(statement, 13);
  2392. if(model_set==nil)
  2393. model_set="";
  2394. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  2395. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  2396. if(load_ability==nil)
  2397. load_ability="";
  2398. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  2399. char *default_category = (char*)sqlite3_column_text(statement, 15);
  2400. if(default_category==nil)
  2401. default_category="";
  2402. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2403. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  2404. if(fabric_content==nil)
  2405. fabric_content="";
  2406. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  2407. char *assembling = (char*)sqlite3_column_text(statement, 17);
  2408. if(assembling==nil)
  2409. assembling="";
  2410. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  2411. char *made_in = (char*)sqlite3_column_text(statement, 18);
  2412. if(made_in==nil)
  2413. made_in="";
  2414. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  2415. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  2416. if(special_remarks==nil)
  2417. special_remarks="";
  2418. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  2419. int stockUcom = sqlite3_column_double(statement, 20);
  2420. char *product_group = (char*)sqlite3_column_text(statement, 21);
  2421. if(product_group==nil)
  2422. product_group="";
  2423. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  2424. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  2425. // if(fashion_selector==nil)
  2426. // fashion_selector="";
  2427. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  2428. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  2429. if(selector_field==nil)
  2430. selector_field="";
  2431. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  2432. char *property_field = (char*)sqlite3_column_text(statement, 23);
  2433. if(property_field==nil)
  2434. property_field="";
  2435. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  2436. char *packaging = (char*)sqlite3_column_text(statement, 24);
  2437. if(packaging==nil)
  2438. packaging="";
  2439. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  2440. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2441. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  2442. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  2443. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  2444. [img_section setValue:model_s_img forKey:@"model_s_img"];
  2445. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  2446. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  2447. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  2448. NSString* Availability=nil;
  2449. if(availability>0)
  2450. Availability=[NSString stringWithFormat:@"%d",availability];
  2451. else
  2452. Availability = @"Out of Stock";
  2453. [img_section setValue:Availability forKey:@"Availability"];
  2454. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  2455. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  2456. char *eta = (char*)sqlite3_column_text(statement, 25);
  2457. if(eta==nil)
  2458. eta="";
  2459. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  2460. if ([nseta isEqualToString:@"null"]) {
  2461. nseta = @"";
  2462. }
  2463. if (availability <= 0) {
  2464. [img_section setValue:nseta forKey:@"ETA"];
  2465. }
  2466. int item_id = sqlite3_column_int(statement, 26);
  2467. NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  2468. NSString *content_writing = [self textAtColumn:28 statement:statement];
  2469. NSString* Price=nil;
  2470. if(appDelegate.bLogin==false)
  2471. Price=@"Must Sign in.";
  2472. else
  2473. {
  2474. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2475. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  2476. if(price==nil)
  2477. Price=@"No Price.";
  2478. else
  2479. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2480. }
  2481. [img_section setObject:content_writing forKey:@"product_content_writing"];
  2482. [img_section setValue:Price forKey:@"price"];
  2483. [img_section setValue:nsname forKey:@"model_name"];
  2484. [img_section setValue:nsdescription forKey:@"model_descrition"];
  2485. if (appDelegate.order_code) { // 离线order code即so#
  2486. 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];
  2487. __block int cartQTY = 0;
  2488. [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2489. cartQTY = sqlite3_column_int(stmt, 0);
  2490. }];
  2491. if (cartQTY > 0) {
  2492. [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  2493. }
  2494. }
  2495. [ret setObject:img_section forKey:@"img_section"];
  2496. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  2497. int detail0_item_count=0;
  2498. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2499. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2500. [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2501. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2502. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2503. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2504. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2505. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2506. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2507. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2508. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2509. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2510. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2511. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2512. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  2513. NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  2514. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  2515. {
  2516. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  2517. [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2518. }
  2519. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  2520. [detail0_section setValue:@"kv" forKey:@"type"];
  2521. [detail0_section setValue:@"Product Information" forKey:@"title"];
  2522. [ret setObject:detail0_section forKey:@"detail_0"];
  2523. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  2524. // [detail1_section setValue:@"detail" forKey:@"target"];
  2525. // [detail1_section setValue:@"popup" forKey:@"action"];
  2526. // [detail1_section setValue:@"content" forKey:@"type"];
  2527. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  2528. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  2529. // [detail1_section setValue:@"true" forKey:@"single_row"];
  2530. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  2531. [ret setObject:detail1_section forKey:@"detail_1"];
  2532. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  2533. [detail2_section setValue:@"detail" forKey:@"target"];
  2534. [detail2_section setValue:@"popup" forKey:@"action"];
  2535. [detail2_section setValue:@"content" forKey:@"type"];
  2536. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  2537. [detail2_section setValue:@"true" forKey:@"single_row"];
  2538. [detail2_section setValue:@"local" forKey:@"data"];
  2539. [ret setObject:detail2_section forKey:@"detail_2"];
  2540. }
  2541. sqlite3_finalize(statement);
  2542. }
  2543. else
  2544. {
  2545. [ret setValue:@"8" forKey:@"result"];
  2546. }
  2547. // DebugLog(@"count:%d",count);
  2548. [iSalesDB close_db:db];
  2549. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  2550. return ret;
  2551. }
  2552. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  2553. NSString* orderCode = [params valueForKey:@"orderCode"];
  2554. NSString* keyword = [params valueForKey:@"keyword"];
  2555. keyword=keyword.lowercaseString;
  2556. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  2557. int limit = [[params valueForKey:@"limit"] intValue];
  2558. int offset = [[params valueForKey:@"offset"] intValue];
  2559. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2560. NSString *limit_str = @"";
  2561. if (limited) {
  2562. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  2563. }
  2564. sqlite3 *db = [iSalesDB get_db];
  2565. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  2566. UIApplication * app = [UIApplication sharedApplication];
  2567. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2568. NSString *sqlQuery = nil;
  2569. if(exactMatch )
  2570. 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 %@;",appDelegate.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 ;
  2571. else
  2572. 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 %@ ;",appDelegate.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
  2573. DebugLog(@"offline_search sql:%@",sqlQuery);
  2574. sqlite3_stmt * statement;
  2575. [ret setValue:@"2" forKey:@"result"];
  2576. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  2577. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  2578. // int count=0;
  2579. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2580. {
  2581. int i=0;
  2582. while (sqlite3_step(statement) == SQLITE_ROW)
  2583. {
  2584. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2585. // char *name = (char*)sqlite3_column_text(statement, 1);
  2586. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2587. char *name = (char*)sqlite3_column_text(statement, 0);
  2588. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2589. char *description = (char*)sqlite3_column_text(statement, 1);
  2590. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2591. int product_id = sqlite3_column_int(statement, 2);
  2592. // char *url = (char*)sqlite3_column_text(statement, 3);
  2593. // if(url==nil)
  2594. // url="";
  2595. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2596. int wid = sqlite3_column_int(statement, 3);
  2597. int closeout = sqlite3_column_int(statement, 4);
  2598. int cid = sqlite3_column_int(statement, 5);
  2599. int wisdelete = sqlite3_column_int(statement, 6);
  2600. int more_color = sqlite3_column_int(statement, 7);
  2601. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  2602. if(wid !=0 && wisdelete != 1)
  2603. [item setValue:@"true" forKey:@"wish_exists"];
  2604. else
  2605. [item setValue:@"false" forKey:@"wish_exists"];
  2606. if(closeout==0)
  2607. [item setValue:@"false" forKey:@"is_closeout"];
  2608. else
  2609. [item setValue:@"true" forKey:@"is_closeout"];
  2610. if(cid==0)
  2611. [item setValue:@"false" forKey:@"cart_exists"];
  2612. else
  2613. [item setValue:@"true" forKey:@"cart_exists"];
  2614. if (more_color == 0) {
  2615. [item setObject:@(false) forKey:@"more_color"];
  2616. } else if (more_color == 1) {
  2617. [item setObject:@(true) forKey:@"more_color"];
  2618. }
  2619. [item addEntriesFromDictionary:imgjson];
  2620. // [item setValue:nsurl forKey:@"img"];
  2621. [item setValue:nsname forKey:@"fash_name"];
  2622. [item setValue:nsdescription forKey:@"description"];
  2623. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2624. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2625. i++;
  2626. }
  2627. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2628. [ret setObject:items forKey:@"items"];
  2629. sqlite3_finalize(statement);
  2630. }
  2631. DebugLog(@"count:%d",count);
  2632. [iSalesDB close_db:db];
  2633. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  2634. return ret;
  2635. }
  2636. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  2637. {
  2638. return [self search:params limited:YES];
  2639. }
  2640. +(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  2641. {
  2642. UIApplication * app = [UIApplication sharedApplication];
  2643. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2644. appDelegate.disable_trigger=true;
  2645. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  2646. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2647. appDelegate.disable_trigger=false;
  2648. //
  2649. // NSString* user = [params valueForKey:@"user"];
  2650. //
  2651. // NSString* password = [params valueForKey:@"password"];
  2652. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2653. ret[@"result"]=[NSNumber numberWithInt:2 ];
  2654. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  2655. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  2656. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  2657. [appDelegate update_count_mark];
  2658. appDelegate.can_show_price =false;
  2659. appDelegate.can_see_price =false;
  2660. appDelegate.can_create_portfolio =false;
  2661. appDelegate.can_create_order =false;
  2662. appDelegate.can_cancel_order =false;
  2663. appDelegate.can_set_cart_price =false;
  2664. appDelegate.can_delete_order =false;
  2665. appDelegate.can_submit_order =false;
  2666. appDelegate.can_set_tearsheet_price =false;
  2667. appDelegate.can_update_contact_info = false;
  2668. appDelegate.save_order_logout = false;
  2669. appDelegate.submit_order_logout = false;
  2670. appDelegate.alert_sold_in_quantities = false;
  2671. appDelegate.ipad_perm =nil ;
  2672. appDelegate.user_type = USER_ROLE_UNKNOWN;
  2673. appDelegate.OrderFilter= nil;
  2674. [appDelegate SetSo:nil];
  2675. [appDelegate set_main_button_panel];
  2676. // sqlite3 *db = [iSalesDB get_db];
  2677. //
  2678. //
  2679. //
  2680. //
  2681. //
  2682. // 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"]];
  2683. //
  2684. //
  2685. //
  2686. //
  2687. // DebugLog(@"offline_login sql:%@",sqlQuery);
  2688. // sqlite3_stmt * statement;
  2689. //
  2690. //
  2691. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2692. //
  2693. //
  2694. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2695. // {
  2696. //
  2697. //
  2698. // if (sqlite3_step(statement) == SQLITE_ROW)
  2699. // {
  2700. //
  2701. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2702. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  2703. //
  2704. //
  2705. //
  2706. // int can_show_price = sqlite3_column_int(statement, 0);
  2707. // int can_see_price = sqlite3_column_int(statement, 1);
  2708. //
  2709. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  2710. // if(contact_id==nil)
  2711. // contact_id="";
  2712. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2713. //
  2714. // int user_type = sqlite3_column_int(statement, 3);
  2715. //
  2716. // int can_cancel_order = sqlite3_column_int(statement, 4);
  2717. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  2718. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  2719. // int can_delete_order = sqlite3_column_int(statement, 7);
  2720. // int can_submit_order = sqlite3_column_int(statement, 8);
  2721. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  2722. // int can_create_order = sqlite3_column_int(statement, 10);
  2723. //
  2724. //
  2725. // char *mode = (char*)sqlite3_column_text(statement, 11);
  2726. // if(mode==nil)
  2727. // mode="";
  2728. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  2729. //
  2730. //
  2731. // char *username = (char*)sqlite3_column_text(statement, 12);
  2732. // if(username==nil)
  2733. // username="";
  2734. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  2735. //
  2736. //
  2737. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  2738. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  2739. // [header setValue:nscontact_id forKey:@"contact_id"];
  2740. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  2741. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  2742. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  2743. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  2744. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  2745. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  2746. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  2747. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  2748. //
  2749. // [header setValue:nsusername forKey:@"username"];
  2750. //
  2751. //
  2752. // [ret setObject:header forKey:@"header"];
  2753. // [ret setValue:nsmode forKey:@"mode"];
  2754. //
  2755. //
  2756. // }
  2757. //
  2758. //
  2759. //
  2760. // sqlite3_finalize(statement);
  2761. // }
  2762. //
  2763. //
  2764. //
  2765. // [iSalesDB close_db:db];
  2766. //
  2767. //
  2768. //
  2769. //
  2770. // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2771. return ret;
  2772. }
  2773. +(NSData*) offline_createorder :(NSMutableDictionary *) params
  2774. {
  2775. UIApplication * app = [UIApplication sharedApplication];
  2776. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2777. appDelegate.disable_trigger=true;
  2778. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  2779. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2780. appDelegate.disable_trigger=false;
  2781. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2782. NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  2783. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  2784. NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  2785. NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  2786. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  2787. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  2788. NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  2789. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  2790. NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  2791. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  2792. NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  2793. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  2794. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  2795. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  2796. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  2797. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  2798. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  2799. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  2800. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  2801. NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  2802. NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  2803. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  2804. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  2805. [arr_name addObject:customer_first_name];
  2806. [arr_name addObject:customer_last_name];
  2807. NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  2808. // default ship from
  2809. NSString *ship_from_default_sql = @"select contact_id,decrypt(company_name),decrypt(addr_1),first_name || ' ' || last_name,email,fax,decrypt(phone),addr_2,city,state,zipcode,country from offline_contact where contact_id = 'NPD';";
  2810. __block NSString *cid = @"";
  2811. __block NSString *name = @"";
  2812. __block NSString *ext = @"";
  2813. __block NSString *contact = @"";
  2814. __block NSString *email = @"";
  2815. __block NSString *fax = @"";
  2816. __block NSString *phone = @"";
  2817. __block NSString *addr2 = @"";
  2818. __block NSString *city = @"";
  2819. __block NSString *state = @"";
  2820. __block NSString *zipcode = @"";
  2821. __block NSString *country = @"";
  2822. sqlite3 *db = [iSalesDB get_db];
  2823. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  2824. cid = [self textAtColumn:0 statement:statment];
  2825. name = [self textAtColumn:1 statement:statment];
  2826. ext = [self textAtColumn:2 statement:statment];
  2827. contact = [self textAtColumn:3 statement:statment];
  2828. email = [self textAtColumn:4 statement:statment];
  2829. fax = [self textAtColumn:5 statement:statment];
  2830. phone = [self textAtColumn:6 statement:statment];
  2831. addr2 = [self textAtColumn:7 statement:statment];
  2832. city = [self textAtColumn:8 statement:statment];
  2833. state = [self textAtColumn:9 statement:statment];
  2834. zipcode = [self textAtColumn:10 statement:statment];
  2835. country = [self textAtColumn:11 statement:statment];
  2836. }];
  2837. NSString* so_id = [self get_offline_soid:db];
  2838. if(so_id==nil)
  2839. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  2840. 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,sender_address1,sender_address2,sender_state,sender_zipcode,sender_city,sender_country,billing_address1,billing_address2,billing_state,billing_zipcode,billing_city,billing_country) 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,ext,addr2,state,zipcode,city,country,customer_address1,customer_address2,customer_state,customer_zipcode,customer_city,customer_country];
  2841. int result =[iSalesDB execSql:sql_neworder db:db];
  2842. [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  2843. //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'
  2844. //soId
  2845. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  2846. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  2847. sqlite3_stmt * statement;
  2848. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2849. {
  2850. if (sqlite3_step(statement) == SQLITE_ROW)
  2851. {
  2852. // char *name = (char*)sqlite3_column_text(statement, 1);
  2853. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2854. //ret = sqlite3_column_int(statement, 0);
  2855. char *soId = (char*)sqlite3_column_text(statement, 0);
  2856. if(soId==nil)
  2857. soId="";
  2858. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  2859. [ret setValue:nssoId forKey:@"soId"];
  2860. [ret setValue:nssoId forKey:@"orderCode"];
  2861. }
  2862. sqlite3_finalize(statement);
  2863. }
  2864. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  2865. [ret setValue:@"Regular Mode" forKey:@"mode"];
  2866. [iSalesDB close_db:db];
  2867. return [RAConvertor dict2data:ret];
  2868. }
  2869. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  2870. {
  2871. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2872. NSString* orderCode = [params valueForKey:@"orderCode"];
  2873. UIApplication * app = [UIApplication sharedApplication];
  2874. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2875. if(![appDelegate.order_code isEqualToString:orderCode]&& appDelegate.order_code.length!=0)
  2876. {
  2877. appDelegate.disable_trigger=true;
  2878. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  2879. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2880. appDelegate.disable_trigger=false;
  2881. }
  2882. sqlite3 *db = [iSalesDB get_db];
  2883. int cart_count=[self query_ordercartcount:orderCode db:db];
  2884. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2885. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2886. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2887. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2888. [iSalesDB close_db:db];
  2889. return [RAConvertor dict2data:ret];
  2890. }
  2891. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  2892. {
  2893. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2894. sqlite3 *db = [iSalesDB get_db];
  2895. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  2896. int count =0;
  2897. if(params[@"count"]!=nil)
  2898. {
  2899. count = [params[@"count"] intValue];
  2900. }
  2901. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  2902. NSString* product_id=params[@"product_id"];
  2903. NSString* orderCode=params[@"orderCode"];
  2904. NSString *qty = params[@"qty"];
  2905. NSArray* arr_id=[RAUtils string2arr:product_id separator:@","];
  2906. NSArray *qty_arr = [RAUtils string2arr:qty separator:@","];
  2907. __block int number_of_outOfStock = 0;
  2908. for(int i=0;i<arr_id.count;i++)
  2909. {
  2910. NSInteger item_qty= count;
  2911. if (qty) {
  2912. item_qty = [qty_arr[i] integerValue];
  2913. }
  2914. if(item_qty==0)
  2915. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  2916. // 检查新加Model数量是否大于库存
  2917. if (!appDelegate.can_create_backorder) {
  2918. __block BOOL needContinue = NO;
  2919. [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) {
  2920. int availability = sqlite3_column_int(stmt, 0);
  2921. // 库存小于购买量为缺货
  2922. if (availability < item_qty || availability <= 0) {
  2923. number_of_outOfStock++;
  2924. needContinue = YES;
  2925. }
  2926. }];
  2927. if (needContinue) {
  2928. continue;
  2929. }
  2930. }
  2931. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  2932. NSString* sql=nil;
  2933. sqlite3_stmt *stmt = nil;
  2934. BOOL shouldStep = NO;
  2935. if(_id<0)
  2936. {
  2937. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  2938. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  2939. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2940. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],(int)[arr_id[i] length],NULL);
  2941. sqlite3_bind_text(stmt,2,[orderCode UTF8String],(int)[orderCode length],NULL);
  2942. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  2943. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  2944. shouldStep = YES;
  2945. }
  2946. else
  2947. {
  2948. if (qty) { // wish list move to cart
  2949. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  2950. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2951. sqlite3_bind_int(stmt, 1, _id);
  2952. shouldStep = YES;
  2953. } else {
  2954. 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];
  2955. __block BOOL update = YES;
  2956. if (!appDelegate.can_create_backorder) {
  2957. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2958. int newQTY = sqlite3_column_int(stmt, 0);
  2959. int availability = sqlite3_column_int(stmt, 1);
  2960. if (newQTY > availability) { // 库存不够
  2961. update = NO;
  2962. number_of_outOfStock++;
  2963. }
  2964. }];
  2965. }
  2966. if (update) {
  2967. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  2968. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2969. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  2970. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",_id] length],NULL);
  2971. shouldStep = YES;
  2972. }
  2973. }
  2974. }
  2975. if (shouldStep) {
  2976. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  2977. [iSalesDB execSql:@"ROLLBACK" db:db];
  2978. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  2979. [iSalesDB close_db:db];
  2980. DebugLog(@"add to cart error");
  2981. return [RAConvertor dict2data:ret];
  2982. }
  2983. }
  2984. }
  2985. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2986. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2987. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2988. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2989. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2990. if (!appDelegate.can_create_backorder) {
  2991. if (number_of_outOfStock > 0) {
  2992. ret[@"result"]=[NSNumber numberWithInt:8];
  2993. ret[@"err_msg"] = @"Out of Stock.\nNo more quantity available.\nPlease try again after sync.";
  2994. /*[NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock]*/
  2995. }
  2996. }
  2997. [iSalesDB execSql:@"END TRANSACTION" db:db];
  2998. [iSalesDB close_db:db];
  2999. return [RAConvertor dict2data:ret];
  3000. }
  3001. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute contactID:(NSString *)contactID
  3002. {
  3003. // UIApplication * app = [UIApplication sharedApplication];
  3004. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3005. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  3006. 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];
  3007. sqlite3_stmt * statement;
  3008. int count=0;
  3009. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3010. {
  3011. while (sqlite3_step(statement) == SQLITE_ROW)
  3012. {
  3013. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  3014. int bitem_id = sqlite3_column_int(statement, 0);
  3015. int bitem_qty = sqlite3_column_int(statement, 1);
  3016. char *name = (char*)sqlite3_column_text(statement, 2);
  3017. if(name==nil)
  3018. name="";
  3019. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  3020. char *description = (char*)sqlite3_column_text(statement, 3);
  3021. if(description==nil)
  3022. description="";
  3023. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  3024. double unit_price = sqlite3_column_double(statement, 4);
  3025. int use_unitprice = sqlite3_column_int(statement, 5);
  3026. if(use_unitprice!=1)
  3027. {
  3028. unit_price=[self get_model_default_price:contactID product_id:nil item_id:@(bitem_id) db:db].doubleValue;
  3029. }
  3030. itemjson[@"model"]=nsname;
  3031. itemjson[@"description"]=nsdescription;
  3032. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  3033. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  3034. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  3035. if(compute)
  3036. {
  3037. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute contactID:contactID];
  3038. }
  3039. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  3040. count++;
  3041. }
  3042. sqlite3_finalize(statement);
  3043. }
  3044. ret[@"count"]=@(count);
  3045. if(count==0)
  3046. return nil;
  3047. else
  3048. return ret;
  3049. }
  3050. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  3051. {
  3052. //compute: add part to subtotal;
  3053. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  3054. dict_item[@(item_id)]=@"1";
  3055. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  3056. double cuft=0;
  3057. double weight=0;
  3058. int carton=0;
  3059. 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];
  3060. sqlite3_stmt * statement;
  3061. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3062. {
  3063. if (sqlite3_step(statement) == SQLITE_ROW)
  3064. {
  3065. double ulength = sqlite3_column_double(statement, 0);
  3066. double uwidth = sqlite3_column_double(statement, 1);
  3067. double uheight = sqlite3_column_double(statement, 2);
  3068. double uweight = sqlite3_column_double(statement, 3);
  3069. double mlength = sqlite3_column_double(statement, 4);
  3070. double mwidth = sqlite3_column_double(statement, 5);
  3071. double mheight = sqlite3_column_double(statement, 6);
  3072. double mweight = sqlite3_column_double(statement, 7);
  3073. double ilength = sqlite3_column_double(statement, 8);
  3074. double iwidth = sqlite3_column_double(statement, 9);
  3075. double iheight = sqlite3_column_double(statement, 10);
  3076. double iweight = sqlite3_column_double(statement, 11);
  3077. int pcs = sqlite3_column_int(statement,12);
  3078. int mpack = sqlite3_column_int(statement, 13);
  3079. int ipack = sqlite3_column_int(statement, 14);
  3080. double ucbf = sqlite3_column_double(statement, 15);
  3081. double icbf = sqlite3_column_double(statement, 16);
  3082. double mcbf = sqlite3_column_double(statement, 17);
  3083. if(ipack==0)
  3084. {
  3085. carton= count/mpack ;
  3086. weight = mweight*carton;
  3087. cuft= carton*(mlength*mwidth*mheight);
  3088. int remain=count%mpack;
  3089. if(remain==0)
  3090. {
  3091. //do nothing;
  3092. }
  3093. else
  3094. {
  3095. carton++;
  3096. weight += uweight*remain;
  3097. cuft += (ulength*uwidth*uheight)*remain;
  3098. }
  3099. }
  3100. else
  3101. {
  3102. carton = count/(mpack*ipack);
  3103. weight = mweight*carton;
  3104. cuft= carton*(mlength*mwidth*mheight);
  3105. int remain=count%(mpack*ipack);
  3106. if(remain==0)
  3107. {
  3108. // do nothing;
  3109. }
  3110. else
  3111. {
  3112. carton++;
  3113. int icarton =remain/ipack;
  3114. int iremain=remain%ipack;
  3115. weight += iweight*icarton;
  3116. cuft += (ilength*iwidth*iheight)*icarton;
  3117. if(iremain==0)
  3118. {
  3119. //do nothing;
  3120. }
  3121. else
  3122. {
  3123. weight += uweight*iremain;
  3124. cuft += (ulength*uwidth*uheight)*iremain;
  3125. }
  3126. }
  3127. }
  3128. #ifdef BUILD_NPD
  3129. cuft=ucbf*count;
  3130. weight= uweight*count;
  3131. #endif
  3132. #if defined(BUILD_HOMER) || defined(BUILD_GATIT)
  3133. cuft=ucbf*count;
  3134. weight= uweight*count;
  3135. #endif
  3136. }
  3137. sqlite3_finalize(statement);
  3138. }
  3139. if(compute)
  3140. {
  3141. NSArray * arr_count=nil;
  3142. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  3143. for(int i=0;i<arr_bundle.count;i++)
  3144. {
  3145. dict_item[arr_bundle[i]]=@"1";
  3146. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  3147. cuft+=[bundlejson[@"cuft"] doubleValue];
  3148. weight+=[bundlejson[@"weight"] doubleValue];
  3149. carton+=[bundlejson[@"carton"] intValue];
  3150. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  3151. }
  3152. }
  3153. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  3154. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  3155. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  3156. ret[@"items"]=dict_item;
  3157. return ret;
  3158. }
  3159. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  3160. {
  3161. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  3162. // 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 ];
  3163. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3164. DebugLog(@"offline_login sql:%@",sqlQuery);
  3165. sqlite3_stmt * statement;
  3166. int cart_count=0;
  3167. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3168. if ( dbresult== SQLITE_OK)
  3169. {
  3170. while (sqlite3_step(statement) == SQLITE_ROW)
  3171. {
  3172. int item_id = sqlite3_column_int(statement, 0);
  3173. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  3174. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3175. }
  3176. sqlite3_finalize(statement);
  3177. }
  3178. return cart_count;
  3179. }
  3180. +(NSData*) offline_requestcart :(NSMutableDictionary *) params
  3181. {
  3182. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3183. sqlite3 *db = [iSalesDB get_db];
  3184. UIApplication * app = [UIApplication sharedApplication];
  3185. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3186. NSString* orderCode=params[@"orderCode"];
  3187. int sort = [[params objectForKey:@"sort"] intValue];
  3188. NSString *sort_str = @"";
  3189. switch (sort) {
  3190. case 0:{
  3191. sort_str = @"order by c.modify_time desc";
  3192. }
  3193. break;
  3194. case 1:{
  3195. sort_str = @"order by c.modify_time asc";
  3196. }
  3197. break;
  3198. case 2:{
  3199. sort_str = @"order by m.name asc";
  3200. }
  3201. break;
  3202. case 3:{
  3203. sort_str = @"order by m.name desc";
  3204. }
  3205. break;
  3206. case 4:{
  3207. sort_str = @"order by m.description asc";
  3208. }
  3209. break;
  3210. default:
  3211. break;
  3212. }
  3213. 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 ];
  3214. // 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 ];
  3215. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3216. DebugLog(@"offline_login sql:%@",sqlQuery);
  3217. sqlite3_stmt * statement;
  3218. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3219. NSDate *date1 = [NSDate date];
  3220. int count=0;
  3221. int cart_count=0;
  3222. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3223. if ( dbresult== SQLITE_OK)
  3224. {
  3225. while (sqlite3_step(statement) == SQLITE_ROW)
  3226. {
  3227. // NSDate *row_date = [NSDate date];
  3228. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  3229. int product_id = sqlite3_column_int(statement, 0);
  3230. char *str_price = (char*)sqlite3_column_text(statement, 1);
  3231. int item_id = sqlite3_column_int(statement, 7);
  3232. NSString* Price=nil;
  3233. if(str_price==nil)
  3234. {
  3235. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  3236. // NSDate *price_date = [NSDate date];
  3237. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  3238. // DebugLog(@"price time interval");
  3239. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  3240. if(price==nil)
  3241. Price=@"No Price.";
  3242. else
  3243. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  3244. }
  3245. else
  3246. {
  3247. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  3248. }
  3249. double discount = sqlite3_column_double(statement, 2);
  3250. int item_count = sqlite3_column_int(statement, 3);
  3251. char *line_note = (char*)sqlite3_column_text(statement, 4);
  3252. NSString *nsline_note=nil;
  3253. if(line_note!=nil)
  3254. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  3255. char *name = (char*)sqlite3_column_text(statement, 5);
  3256. NSString *nsname=nil;
  3257. if(name!=nil)
  3258. nsname= [[NSString alloc]initWithUTF8String:name];
  3259. char *description = (char*)sqlite3_column_text(statement, 6);
  3260. NSString *nsdescription=nil;
  3261. if(description!=nil)
  3262. nsdescription= [[NSString alloc]initWithUTF8String:description];
  3263. int stockUom = sqlite3_column_int(statement, 8);
  3264. int _id = sqlite3_column_int(statement, 9);
  3265. int availability = sqlite3_column_int(statement, 10);
  3266. // NSDate *subtotal_date = [NSDate date];
  3267. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  3268. // DebugLog(@"subtotal_date time interval");
  3269. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  3270. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  3271. double weight=[bsubtotaljson[@"weight"] doubleValue];
  3272. int carton=[bsubtotaljson[@"carton"] intValue];
  3273. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3274. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  3275. // NSDate *img_date = [NSDate date];
  3276. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  3277. // DebugLog(@"img_date time interval");
  3278. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  3279. itemjson[@"model"]=nsname;
  3280. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  3281. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  3282. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  3283. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  3284. itemjson[@"check"]=@"true";
  3285. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  3286. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  3287. itemjson[@"unit_price"]=Price;
  3288. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  3289. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  3290. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  3291. itemjson[@"note"]=nsline_note;
  3292. if (!appDelegate.can_create_backorder) {
  3293. itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  3294. }
  3295. // NSDate *date2 = [NSDate date];
  3296. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  3297. // DebugLog(@"model_bundle time interval");
  3298. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  3299. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  3300. count++;
  3301. // DebugLog(@"row time interval");
  3302. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  3303. }
  3304. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3305. sqlite3_finalize(statement);
  3306. }
  3307. DebugLog(@"request cart total time interval");
  3308. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  3309. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  3310. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  3311. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3312. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3313. ret[@"count"]=[NSNumber numberWithInt:count ];
  3314. ret[@"mode"]=@"Regular Mode";
  3315. [iSalesDB close_db:db];
  3316. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  3317. DebugLog(@"general notes :%@",general_note);
  3318. ret[@"general_note"]= general_note;
  3319. return [RAConvertor dict2data:ret];
  3320. }
  3321. +(NSData*) offline_login :(NSMutableDictionary *) params
  3322. {
  3323. NSString* user = [params valueForKey:@"user"];
  3324. NSString* password = [params valueForKey:@"password"];
  3325. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3326. sqlite3 *db = [iSalesDB get_db];
  3327. 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"]];
  3328. DebugLog(@"offline_login sql:%@",sqlQuery);
  3329. sqlite3_stmt * statement;
  3330. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3331. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3332. {
  3333. if (sqlite3_step(statement) == SQLITE_ROW)
  3334. {
  3335. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3336. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  3337. int can_show_price = sqlite3_column_int(statement, 0);
  3338. int can_see_price = sqlite3_column_int(statement, 1);
  3339. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  3340. if(contact_id==nil)
  3341. contact_id="";
  3342. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3343. int user_type = sqlite3_column_int(statement, 3);
  3344. int can_cancel_order = sqlite3_column_int(statement, 4);
  3345. int can_set_cart_price = sqlite3_column_int(statement, 5);
  3346. int can_create_portfolio = sqlite3_column_int(statement, 6);
  3347. int can_delete_order = sqlite3_column_int(statement, 7);
  3348. int can_submit_order = sqlite3_column_int(statement, 8);
  3349. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  3350. int can_create_order = sqlite3_column_int(statement, 10);
  3351. char *mode = (char*)sqlite3_column_text(statement, 11);
  3352. if(mode==nil)
  3353. mode="";
  3354. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  3355. char *username = (char*)sqlite3_column_text(statement, 12);
  3356. if(username==nil)
  3357. username="";
  3358. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  3359. int can_update_contact_info = sqlite3_column_int(statement, 13);
  3360. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  3361. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  3362. [header setValue:nscontact_id forKey:@"contact_id"];
  3363. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  3364. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  3365. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  3366. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  3367. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  3368. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  3369. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  3370. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  3371. [header setObject:@(YES) forKey:@"can_create_backorder"];
  3372. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  3373. [header setValue:nsusername forKey:@"username"];
  3374. NSError* error=nil;
  3375. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  3376. [header setValue:statusFilter forKey:@"statusFilter"];
  3377. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3378. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  3379. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  3380. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  3381. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  3382. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  3383. [ret setObject:header forKey:@"header"];
  3384. [ret setValue:nsmode forKey:@"mode"];
  3385. }
  3386. sqlite3_finalize(statement);
  3387. }
  3388. [iSalesDB close_db:db];
  3389. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3390. return [RAConvertor dict2data:ret];
  3391. }
  3392. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  3393. {
  3394. NSString* contactId = [params valueForKey:@"contactId"];
  3395. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3396. sqlite3 *db = [iSalesDB get_db];
  3397. NSString *sqlQuery = nil;
  3398. {
  3399. 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 from offline_contact where contact_id='%@'",contactId];
  3400. }
  3401. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  3402. sqlite3_stmt * statement;
  3403. [ret setValue:@"2" forKey:@"result"];
  3404. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3405. {
  3406. //int i = 0;
  3407. if (sqlite3_step(statement) == SQLITE_ROW)
  3408. {
  3409. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3410. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3411. // int editable = sqlite3_column_int(statement, 0);
  3412. char *company_name = (char*)sqlite3_column_text(statement, 1);
  3413. NSString *nscompany_name =nil;
  3414. if(company_name==nil)
  3415. nscompany_name=@"";
  3416. else
  3417. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  3418. char *country = (char*)sqlite3_column_text(statement, 2);
  3419. if(country==nil)
  3420. country="";
  3421. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  3422. // char *addr = (char*)sqlite3_column_text(statement, 3);
  3423. // if(addr==nil)
  3424. // addr="";
  3425. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  3426. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  3427. if(zipcode==nil)
  3428. zipcode="";
  3429. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  3430. char *state = (char*)sqlite3_column_text(statement, 5);
  3431. if(state==nil)
  3432. state="";
  3433. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  3434. char *city = (char*)sqlite3_column_text(statement, 6);
  3435. if(city==nil)
  3436. city="";
  3437. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  3438. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  3439. // NSString *nscontact_name = nil;
  3440. // if(contact_name==nil)
  3441. // nscontact_name=@"";
  3442. // else
  3443. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  3444. char *phone = (char*)sqlite3_column_text(statement, 8);
  3445. NSString *nsphone = nil;
  3446. if(phone==nil)
  3447. nsphone=@"";
  3448. else
  3449. nsphone= [[NSString alloc]initWithUTF8String:phone];
  3450. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  3451. if(contact_id==nil)
  3452. contact_id="";
  3453. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3454. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  3455. if(addr_1==nil)
  3456. addr_1="";
  3457. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  3458. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  3459. if(addr_2==nil)
  3460. addr_2="";
  3461. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  3462. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  3463. if(addr_3==nil)
  3464. addr_3="";
  3465. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  3466. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  3467. if(addr_4==nil)
  3468. addr_4="";
  3469. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  3470. char *first_name = (char*)sqlite3_column_text(statement, 14);
  3471. if(first_name==nil)
  3472. first_name="";
  3473. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  3474. char *last_name = (char*)sqlite3_column_text(statement, 15);
  3475. if(last_name==nil)
  3476. last_name="";
  3477. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  3478. char *fax = (char*)sqlite3_column_text(statement, 16);
  3479. NSString *nsfax = nil;
  3480. if(fax==nil)
  3481. nsfax=@"";
  3482. else
  3483. nsfax= [[NSString alloc]initWithUTF8String:fax];
  3484. char *email = (char*)sqlite3_column_text(statement, 17);
  3485. NSString *nsemail = nil;
  3486. if(email==nil)
  3487. nsemail=@"";
  3488. else
  3489. nsemail= [[NSString alloc]initWithUTF8String:email];
  3490. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  3491. NSString *nsimg_0 = nil;
  3492. if(img_0==nil)
  3493. nsimg_0=@"";
  3494. else
  3495. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  3496. [self copy_bcardImg:nsimg_0];
  3497. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  3498. NSString *nsimg_1 = nil;
  3499. if(img_1==nil)
  3500. nsimg_1=@"";
  3501. else
  3502. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  3503. [self copy_bcardImg:nsimg_1];
  3504. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  3505. NSString *nsimg_2 = nil;
  3506. if(img_2==nil)
  3507. nsimg_2=@"";
  3508. else
  3509. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  3510. [self copy_bcardImg:nsimg_2];
  3511. char *price_type = (char*)sqlite3_column_text(statement, 21);
  3512. NSString *nsprice_type = nil;
  3513. if(price_type==nil)
  3514. nsprice_type=@"";
  3515. else
  3516. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  3517. char *notes = (char*)sqlite3_column_text(statement, 22);
  3518. NSString *nsnotes = nil;
  3519. if(notes==nil)
  3520. nsnotes=@"";
  3521. else
  3522. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  3523. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  3524. NSString *nssalesrep = nil;
  3525. if(salesrep==nil)
  3526. nssalesrep=@"";
  3527. else
  3528. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  3529. NSString *contact_type = [self textAtColumn:24 statement:statement];
  3530. {
  3531. // decrypt
  3532. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  3533. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  3534. nsphone=[AESCrypt fastdecrypt:nsphone];
  3535. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3536. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  3537. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3538. }
  3539. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3540. [arr_name addObject:nsfirst_name];
  3541. [arr_name addObject:nslast_name];
  3542. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  3543. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  3544. [arr_ext addObject:nsaddr_1];
  3545. [arr_ext addObject:nsaddr_2];
  3546. [arr_ext addObject:nsaddr_3];
  3547. [arr_ext addObject:nsaddr_4];
  3548. [arr_ext addObject:@"\r\n"];
  3549. [arr_ext addObject:nscity];
  3550. [arr_ext addObject:nsstate];
  3551. [arr_ext addObject:nszipcode];
  3552. [arr_ext addObject:nscountry];
  3553. NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  3554. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  3555. [item setValue:nsimg_2 forKey:@"business_card_2"];
  3556. [item setValue:nsimg_0 forKey:@"business_card_0"];
  3557. [item setValue:nscountry forKey:@"customer_country"];
  3558. [item setValue:nsphone forKey:@"customer_phone"];
  3559. [item setValue:nsimg_1 forKey:@"business_card_1"];
  3560. [item setValue:nscompany_name forKey:@"customer_name"];
  3561. [item setValue:nsprice_type forKey:@"customer_price_type"];
  3562. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  3563. [item setValue:nsext forKey:@"customer_contact_ext"];
  3564. [item setValue:nszipcode forKey:@"customer_zipcode"];
  3565. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  3566. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  3567. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  3568. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  3569. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  3570. [item setValue:nslast_name forKey:@"customer_last_name"];
  3571. [item setValue:nscity forKey:@"customer_city"];
  3572. [item setValue:nsstate forKey:@"customer_state"];
  3573. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  3574. [item setValue:contactId forKey:@"customer_cid"];
  3575. [item setValue:nscontact_name forKey:@"customer_contact"];
  3576. [item setValue:nsfax forKey:@"customer_fax"];
  3577. [item setValue:nsemail forKey:@"customer_email"];
  3578. [item setValue:contact_type forKey:@"customer_contact_type"];
  3579. [ret setObject:item forKey:@"customerInfo"];
  3580. // i++;
  3581. }
  3582. UIApplication * app = [UIApplication sharedApplication];
  3583. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3584. [ret setValue:appDelegate.mode forKey:@"mode"];
  3585. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  3586. sqlite3_finalize(statement);
  3587. }
  3588. [iSalesDB close_db:db];
  3589. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3590. return ret;
  3591. }
  3592. + (bool) copy_bcardImg:(NSString*) filename
  3593. {
  3594. if(filename.length==0)
  3595. return false;
  3596. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3597. bool ret=false;
  3598. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3599. NSString *cachefolder = [paths objectAtIndex:0];
  3600. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  3601. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  3602. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  3603. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  3604. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  3605. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  3606. //
  3607. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  3608. NSFileManager* fileManager = [NSFileManager defaultManager];
  3609. BOOL bdir=NO;
  3610. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  3611. {
  3612. NSError *error = nil;
  3613. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  3614. {
  3615. ret=false;
  3616. }
  3617. else
  3618. {
  3619. ret=true;
  3620. }
  3621. // NSError *error = nil;
  3622. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  3623. //
  3624. // if(!bsuccess)
  3625. // {
  3626. // DebugLog(@"Create offline_createimg folder failed");
  3627. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3628. // return [RAConvertor dict2data:ret];
  3629. // }
  3630. // if(bsuccess)
  3631. // {
  3632. // sqlite3 *db = [self get_db];
  3633. //
  3634. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  3635. // [iSalesDB close_db:db];
  3636. // }
  3637. }
  3638. return ret;
  3639. //
  3640. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  3641. // if(bsuccess)
  3642. // {
  3643. // NSError *error = nil;
  3644. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  3645. // {
  3646. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3647. // }
  3648. // else
  3649. // {
  3650. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  3651. //
  3652. // ret[@"img_url_aname"]=filename;
  3653. // ret[@"img_url"]=savedImagePath;
  3654. // }
  3655. // }
  3656. }
  3657. +(NSData *) offline_saveBusinesscard:(NSData *) image
  3658. {
  3659. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3660. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3661. NSString *cachefolder = [paths objectAtIndex:0];
  3662. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  3663. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  3664. NSFileManager* fileManager = [NSFileManager defaultManager];
  3665. BOOL bdir=YES;
  3666. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  3667. {
  3668. NSError *error = nil;
  3669. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  3670. if(!bsuccess)
  3671. {
  3672. DebugLog(@"Create offline_createimg folder failed");
  3673. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3674. return [RAConvertor dict2data:ret];
  3675. }
  3676. // if(bsuccess)
  3677. // {
  3678. // sqlite3 *db = [self get_db];
  3679. //
  3680. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  3681. // [iSalesDB close_db:db];
  3682. // }
  3683. }
  3684. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  3685. //JEPG格式
  3686. //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  3687. NSString* filename =[NSString stringWithFormat:@"%@.png",[[NSUUID UUID] UUIDString]];
  3688. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  3689. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  3690. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  3691. if(bsuccess)
  3692. {
  3693. NSError *error = nil;
  3694. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  3695. {
  3696. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3697. }
  3698. else
  3699. {
  3700. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  3701. ret[@"img_url_aname"]=filename;
  3702. ret[@"img_url"]=filename;
  3703. }
  3704. }
  3705. else
  3706. {
  3707. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3708. }
  3709. return [RAConvertor dict2data:ret];
  3710. }
  3711. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  3712. {
  3713. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  3714. category = [category substringToIndex:3];
  3715. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3716. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  3717. params[@"category"]= category;
  3718. ret[@"params"]= params;
  3719. [ret setValue:@"detail" forKey:@"target"];
  3720. [ret setValue:@"popup" forKey:@"action"];
  3721. [ret setValue:@"content" forKey:@"type"];
  3722. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  3723. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  3724. [ret setValue:@"true" forKey:@"single_row"];
  3725. [ret setValue:@"true" forKey:@"partial_refresh"];
  3726. // sqlite3 *db = [iSalesDB get_db];
  3727. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  3728. NSString *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 ;
  3729. sqlite3_stmt * statement;
  3730. int count = 0;
  3731. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  3732. // int count=0;
  3733. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3734. {
  3735. int i=0;
  3736. while (sqlite3_step(statement) == SQLITE_ROW)
  3737. {
  3738. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3739. // char *name = (char*)sqlite3_column_text(statement, 1);
  3740. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3741. char *name = (char*)sqlite3_column_text(statement, 0);
  3742. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3743. int product_id = sqlite3_column_int(statement, 1);
  3744. char *url = (char*)sqlite3_column_text(statement, 2);
  3745. if(url==nil)
  3746. url="";
  3747. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3748. [item setValue:nsurl forKey:@"picture_path"];
  3749. [item setValue:nsname forKey:@"fash_name"];
  3750. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3751. [item setValue:category forKey:@"category"];
  3752. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3753. i++;
  3754. }
  3755. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3756. sqlite3_finalize(statement);
  3757. }
  3758. DebugLog(@"count:%d",count);
  3759. // [iSalesDB close_db:db];
  3760. return ret;
  3761. }
  3762. +(NSDictionary*) model_priceKvItem:(NSString*) key value:(NSString*)value
  3763. {
  3764. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3765. [ret setValue:key forKey:@"key"];
  3766. [ret setValue:value forKey:@"val"];
  3767. [ret setValue:@"price" forKey:@"type"];
  3768. return ret;
  3769. }
  3770. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  3771. {
  3772. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3773. [ret setValue:key forKey:@"key"];
  3774. [ret setValue:value forKey:@"val"];
  3775. return ret;
  3776. }
  3777. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  3778. {
  3779. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3780. [ret setValue:@"0" forKey:@"img_count"];
  3781. // sqlite3 *db = [iSalesDB get_db];
  3782. 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 ;
  3783. sqlite3_stmt * statement;
  3784. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3785. {
  3786. int i=0;
  3787. if (sqlite3_step(statement) == SQLITE_ROW)
  3788. {
  3789. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3790. // char *name = (char*)sqlite3_column_text(statement, 1);
  3791. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3792. char *value = (char*)sqlite3_column_text(statement, 0);
  3793. if(value==nil)
  3794. value="";
  3795. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  3796. char *key = (char*)sqlite3_column_text(statement, 1);
  3797. if(key==nil)
  3798. key="";
  3799. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  3800. [item setValue:nsvalue forKey:@"val"];
  3801. [item setValue:nskey forKey:@"key"];
  3802. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3803. [ret setValue:@"1" forKey:@"count"];
  3804. i++;
  3805. }
  3806. sqlite3_finalize(statement);
  3807. }
  3808. // [iSalesDB close_db:db];
  3809. return ret;
  3810. }
  3811. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  3812. {
  3813. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3814. [ret setValue:@"0" forKey:@"count"];
  3815. // sqlite3 *db = [iSalesDB get_db];
  3816. 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;
  3817. sqlite3_stmt * statement;
  3818. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3819. {
  3820. int i=0;
  3821. while (sqlite3_step(statement) == SQLITE_ROW)
  3822. {
  3823. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3824. // char *name = (char*)sqlite3_column_text(statement, 1);
  3825. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3826. char *value = (char*)sqlite3_column_text(statement, 0);
  3827. if(value==nil)
  3828. value="";
  3829. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  3830. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  3831. if(selector_display==nil)
  3832. selector_display="";
  3833. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  3834. int product_id = sqlite3_column_int(statement, 2);
  3835. char *category = (char*)sqlite3_column_text(statement, 3);
  3836. if(category==nil)
  3837. category="";
  3838. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  3839. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  3840. [item setValue:nsvalue forKey:@"title"];
  3841. [item setValue:url forKey:@"pic_url"];
  3842. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  3843. [params setValue:@"2" forKey:@"count"];
  3844. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  3845. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  3846. [param0 setValue:@"product_id" forKey:@"name"];
  3847. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  3848. [param1 setValue:nscategory forKey:@"val"];
  3849. [param1 setValue:@"category" forKey:@"name"];
  3850. [params setObject:param0 forKey:@"param_0"];
  3851. [params setObject:param1 forKey:@"param_1"];
  3852. [item setObject:params forKey:@"params"];
  3853. [ret setValue:nsselector_display forKey:@"name"];
  3854. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3855. i++;
  3856. }
  3857. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3858. [ret setValue:@"switch" forKey:@"action"];
  3859. sqlite3_finalize(statement);
  3860. }
  3861. // [iSalesDB close_db:db];
  3862. return ret;
  3863. }
  3864. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  3865. {
  3866. // model 在 category search 显示的图片。
  3867. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  3868. // sqlite3 *db = [iSalesDB get_db];
  3869. 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];
  3870. sqlite3_stmt * statement;
  3871. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3872. {
  3873. while (sqlite3_step(statement) == SQLITE_ROW)
  3874. {
  3875. char *url = (char*)sqlite3_column_text(statement, 0);
  3876. if(url==nil)
  3877. url="";
  3878. int type = sqlite3_column_int(statement, 1);
  3879. if(type==0)
  3880. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  3881. else
  3882. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  3883. }
  3884. sqlite3_finalize(statement);
  3885. }
  3886. // [iSalesDB close_db:db];
  3887. return ret;
  3888. }
  3889. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  3890. {
  3891. int item_id=-1;
  3892. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  3893. sqlite3_stmt * statement;
  3894. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3895. {
  3896. if (sqlite3_step(statement) == SQLITE_ROW)
  3897. {
  3898. item_id = sqlite3_column_int(statement, 0);
  3899. }
  3900. sqlite3_finalize(statement);
  3901. }
  3902. return item_id;
  3903. }
  3904. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  3905. {
  3906. // NSString* ret = @"";
  3907. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  3908. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  3909. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  3910. sqlite3_stmt * statement;
  3911. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3912. {
  3913. while (sqlite3_step(statement) == SQLITE_ROW)
  3914. {
  3915. int bitem_id = sqlite3_column_int(statement, 0);
  3916. int bitem_qty = sqlite3_column_int(statement, 1);
  3917. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  3918. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  3919. }
  3920. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  3921. sqlite3_finalize(statement);
  3922. }
  3923. // if(ret==nil)
  3924. // ret=@"";
  3925. *count=arr_count;
  3926. return arr_bundle;
  3927. }
  3928. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  3929. {
  3930. // get default sold qty, return -1 if model not found;
  3931. int ret = -1;
  3932. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  3933. sqlite3_stmt * statement;
  3934. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3935. {
  3936. if (sqlite3_step(statement) == SQLITE_ROW)
  3937. {
  3938. ret = sqlite3_column_int(statement, 0);
  3939. }
  3940. sqlite3_finalize(statement);
  3941. }
  3942. return ret;
  3943. }
  3944. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  3945. {
  3946. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3947. [ret setValue:@"0" forKey:@"img_count"];
  3948. // sqlite3 *db = [iSalesDB get_db];
  3949. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  3950. sqlite3_stmt * statement;
  3951. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3952. {
  3953. int i=0;
  3954. while (sqlite3_step(statement) == SQLITE_ROW)
  3955. {
  3956. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3957. // char *name = (char*)sqlite3_column_text(statement, 1);
  3958. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3959. char *url = (char*)sqlite3_column_text(statement, 0);
  3960. if(url==nil)
  3961. url="";
  3962. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3963. [item setValue:nsurl forKey:@"s"];
  3964. [item setValue:nsurl forKey:@"l"];
  3965. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  3966. i++;
  3967. }
  3968. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  3969. sqlite3_finalize(statement);
  3970. }
  3971. // [iSalesDB close_db:db];
  3972. return ret;
  3973. }
  3974. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  3975. UIApplication * app = [UIApplication sharedApplication];
  3976. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3977. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  3978. sqlite3 *db = [iSalesDB get_db];
  3979. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  3980. NSString* product_id=params[@"product_id"];
  3981. NSString *item_count_str = params[@"item_count"];
  3982. NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  3983. NSArray *item_count_arr = item_count_str ? [RAUtils string2arr:item_count_str separator:@","] : nil;
  3984. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  3985. // NSString *sql = @"";
  3986. for(int i=0;i<arr.count;i++)
  3987. {
  3988. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  3989. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  3990. __block int cart_count = 0;
  3991. if (!item_count_str) {
  3992. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  3993. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3994. NSString *model_set = [self textAtColumn:0 statement:stmt];
  3995. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  3996. cart_count = [[model_set_components lastObject] integerValue];
  3997. }];
  3998. }
  3999. if(count==0)
  4000. {
  4001. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  4002. sqlite3_stmt *stmt;
  4003. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  4004. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],[arr[i] length],NULL);
  4005. if (item_count_arr) {
  4006. sqlite3_bind_int(stmt,2,[item_count_arr[i] integerValue]);
  4007. } else {
  4008. sqlite3_bind_int(stmt,2,cart_count);
  4009. }
  4010. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  4011. [iSalesDB execSql:@"ROLLBACK" db:db];
  4012. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  4013. [iSalesDB close_db:db];
  4014. DebugLog(@"add to wishlist error");
  4015. return ret;
  4016. }
  4017. } else {
  4018. int qty = 0;
  4019. if (item_count_arr) {
  4020. qty = [item_count_arr[i] integerValue];
  4021. } else {
  4022. qty = cart_count;
  4023. }
  4024. 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]];
  4025. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  4026. [iSalesDB execSql:@"ROLLBACK" db:db];
  4027. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  4028. [iSalesDB close_db:db];
  4029. DebugLog(@"add to wishlist error");
  4030. return ret;
  4031. }
  4032. }
  4033. }
  4034. // [iSalesDB execSql:sql db:db];
  4035. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4036. [iSalesDB execSql:@"END TRANSACTION" db:db];
  4037. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  4038. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  4039. [iSalesDB close_db:db];
  4040. appDelegate.wish_count =count;
  4041. [appDelegate update_count_mark];
  4042. ret[@"result"]= [NSNumber numberWithInt:2];
  4043. return ret;
  4044. }
  4045. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  4046. // 0 category
  4047. // 1 search
  4048. // 2 itemsearch
  4049. NSData *ret = nil;
  4050. NSDictionary *items = nil;
  4051. switch (from) {
  4052. case 0:{
  4053. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  4054. }
  4055. break;
  4056. case 1:{
  4057. items = [[self search:params limited:NO] objectForKey:@"items"];
  4058. }
  4059. break;
  4060. case 2:{
  4061. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  4062. }
  4063. break;
  4064. default:
  4065. break;
  4066. }
  4067. if (!items) {
  4068. return ret;
  4069. }
  4070. int count = [[items objectForKey:@"count"] intValue];
  4071. NSMutableString *product_id_str = [@"" mutableCopy];
  4072. for (int i = 0; i < count; i++) {
  4073. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  4074. NSDictionary *item = [items objectForKey:key];
  4075. NSString *product_id = [item objectForKey:@"product_id"];
  4076. if (i == 0) {
  4077. [product_id_str appendString:product_id];
  4078. } else {
  4079. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  4080. }
  4081. }
  4082. NSString *add_to = [self valueInParams:params key:@"addTo"];
  4083. if ([add_to isEqualToString:@"cart"]) {
  4084. NSString *order_code = [params objectForKey:@"orderCode"];
  4085. if (order_code.length) {
  4086. NSDictionary *newParams = @{
  4087. @"product_id" : product_id_str,
  4088. @"orderCode" : order_code
  4089. };
  4090. ret = [self offline_add2cart:newParams.mutableCopy];
  4091. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4092. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4093. ret = [RAConvertor dict2data:retDic];
  4094. }
  4095. } else if([add_to isEqualToString:@"wishlist"]) {
  4096. NSDictionary *newParams = @{
  4097. @"product_id" : product_id_str
  4098. };
  4099. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  4100. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4101. ret = [RAConvertor dict2data:retDic];
  4102. } else if([add_to isEqualToString:@"portfolio"]) {
  4103. NSDictionary *newParams = @{
  4104. @"product_id" : product_id_str
  4105. };
  4106. NSData *data = [self offline_add2Portfolio:newParams.mutableCopy];
  4107. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4108. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4109. ret = [RAConvertor dict2data:retDic];
  4110. }
  4111. return ret;
  4112. }
  4113. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  4114. {
  4115. return [self addAll:params from:0];
  4116. }
  4117. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  4118. {
  4119. return [self addAll:params from:1];
  4120. }
  4121. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  4122. {
  4123. return [self addAll:params from:2];
  4124. }
  4125. #pragma mark - Jack
  4126. #warning 做SQL操作时转义!!
  4127. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  4128. // "val_227" : {
  4129. // "check" : 1,
  4130. // "value" : "US United States",
  4131. // "value_id" : "228"
  4132. // },
  4133. if (!countryCode) {
  4134. countryCode = @"US";
  4135. }
  4136. __block NSInteger _count = 0;
  4137. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  4138. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  4139. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  4140. int code_id = sqlite3_column_int(stmt, 3); // id
  4141. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4142. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  4143. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  4144. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4145. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  4146. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4147. }
  4148. long n = *count;
  4149. *count = n + 1;
  4150. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  4151. [container setValue:countryDic forKey:key];
  4152. _count = *count;
  4153. }] mutableCopy];
  4154. [ret setValue:@(_count) forKey:@"count"];
  4155. return ret;
  4156. }
  4157. + (NSDictionary *)offline_getAllCountryDefaultId:(NSString *)countryCodeId db:(sqlite3 *)db {
  4158. if (db == nil) {
  4159. return nil;
  4160. }
  4161. if (!countryCodeId) {
  4162. countryCodeId = @"228";
  4163. }
  4164. __block NSInteger _count = 0;
  4165. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4166. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  4167. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  4168. int code_id = sqlite3_column_int(stmt, 3); // id
  4169. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4170. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  4171. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  4172. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4173. if ([countryCodeId intValue] == code_id) {
  4174. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4175. }
  4176. long n = *count;
  4177. *count = n + 1;
  4178. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  4179. [container setValue:countryDic forKey:key];
  4180. _count = *count;
  4181. }] mutableCopy];
  4182. [ret setValue:@(_count) forKey:@"count"];
  4183. return ret;
  4184. }
  4185. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  4186. countryCode = [self translateSingleQuote:countryCode];
  4187. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  4188. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4189. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  4190. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  4191. if (name == NULL) {
  4192. name = "";
  4193. }
  4194. if (code == NULL) {
  4195. code = "";
  4196. }
  4197. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4198. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4199. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  4200. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4201. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  4202. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4203. }
  4204. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4205. [container setValue:stateDic forKey:key];
  4206. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  4207. DebugLog(@"query all state error: %@",err_msg);
  4208. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4209. // [stateDic setValue:@"Other" forKey:@"value"];
  4210. // [stateDic setValue:@"" forKey:@"value_id"];
  4211. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4212. //
  4213. // if (state_code && [@"" isEqualToString:state_code]) {
  4214. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4215. // }
  4216. //
  4217. // NSString *key = [NSString stringWithFormat:@"val_0"];
  4218. // [container setValue:stateDic forKey:key];
  4219. }] mutableCopy];
  4220. [ret removeObjectForKey:@"result"];
  4221. // failure 可以不用了,一样的
  4222. if (ret.allKeys.count == 0) {
  4223. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4224. [stateDic setValue:@"Other" forKey:@"value"];
  4225. [stateDic setValue:@"" forKey:@"value_id"];
  4226. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4227. if (state_code && [@"" isEqualToString:state_code]) {
  4228. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4229. }
  4230. NSString *key = [NSString stringWithFormat:@"val_0"];
  4231. [ret setValue:stateDic forKey:key];
  4232. }
  4233. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4234. return ret;
  4235. }
  4236. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  4237. codeId = [self translateSingleQuote:codeId];
  4238. 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];
  4239. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4240. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  4241. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  4242. if (name == NULL) {
  4243. name = "";
  4244. }
  4245. if (code == NULL) {
  4246. code = "";
  4247. }
  4248. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4249. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4250. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  4251. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4252. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  4253. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4254. }
  4255. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4256. [container setValue:stateDic forKey:key];
  4257. }] mutableCopy];
  4258. [ret removeObjectForKey:@"result"];
  4259. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4260. return ret;
  4261. }
  4262. + (NSDictionary *)offline_getPrice {
  4263. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price where is_show = 1;"];
  4264. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4265. char *name = (char *) sqlite3_column_text(stmt, 1);
  4266. int type = sqlite3_column_int(stmt, 2);
  4267. int orderBy = sqlite3_column_int(stmt, 3);
  4268. if (name == NULL) {
  4269. name = "";
  4270. }
  4271. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  4272. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4273. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  4274. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4275. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4276. if (orderBy == 0) {
  4277. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4278. }
  4279. [container setValue:priceDic forKey:key];
  4280. }] mutableCopy];
  4281. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4282. return ret;
  4283. }
  4284. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  4285. NSString *sql = @"select _id,type_name from contact_type where is_show = 1";
  4286. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  4287. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  4288. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4289. int _id = sqlite3_column_int(stmt, 0);
  4290. NSString *name = [self textAtColumn:1 statement:stmt];
  4291. NSDictionary *typeDic = @{
  4292. @"value_id" : name,
  4293. @"value" : name,
  4294. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  4295. };
  4296. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  4297. [ret setValue:[NSNumber numberWithInt:++(*count)] forKey:@"count"];
  4298. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  4299. }];
  4300. return ret;
  4301. }
  4302. + (NSDictionary *)offline_getSalesRep {
  4303. // 首先从offline_login表中取出sales_code
  4304. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4305. NSString *user = app.user;
  4306. user = [self translateSingleQuote:user];
  4307. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  4308. __block NSString *user_code = @"";
  4309. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4310. char *code = (char *)sqlite3_column_text(stmt, 0);
  4311. if (code == NULL) {
  4312. code = "";
  4313. }
  4314. user_code = [NSString stringWithUTF8String:code];
  4315. }];
  4316. // 再取所有salesRep
  4317. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  4318. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4319. // 1 name 2 code 3 salesrep_id
  4320. char *name = (char *)sqlite3_column_text(stmt, 1);
  4321. char *code = (char *)sqlite3_column_text(stmt, 2);
  4322. int salesrep_id = sqlite3_column_int(stmt, 3);
  4323. if (name == NULL) {
  4324. name = "";
  4325. }
  4326. if (code == NULL) {
  4327. code = "";
  4328. }
  4329. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  4330. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  4331. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  4332. // 比较code 相等则check
  4333. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  4334. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4335. }
  4336. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  4337. }] mutableCopy];
  4338. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4339. return ret;
  4340. }
  4341. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  4342. zipcode = [self translateSingleQuote:zipcode];
  4343. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  4344. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4345. char *country = (char *)sqlite3_column_text(stmt, 0);
  4346. char *state = (char *)sqlite3_column_text(stmt, 1);
  4347. char *city = (char *)sqlite3_column_text(stmt, 2);
  4348. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  4349. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  4350. if (country == NULL) {
  4351. country = "";
  4352. }
  4353. if (state == NULL) {
  4354. state = "";
  4355. }
  4356. if (city == NULL) {
  4357. city = "";
  4358. }
  4359. if (country_code == NULL) {
  4360. country_code = "";
  4361. }
  4362. if (state_code == NULL) {
  4363. state_code = "";
  4364. }
  4365. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  4366. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  4367. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  4368. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  4369. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  4370. }] mutableCopy];
  4371. return ret;
  4372. }
  4373. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  4374. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  4375. [item setValue:value forKey:valueKey];
  4376. [dic setValue:item forKey:itemKey];
  4377. }
  4378. + (NSString *)countryCodeByid:(NSString *)code_id {
  4379. NSString *ret = nil;
  4380. code_id = [self translateSingleQuote:code_id];
  4381. sqlite3 *db = [iSalesDB get_db];
  4382. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  4383. sqlite3_stmt * statement;
  4384. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4385. while (sqlite3_step(statement) == SQLITE_ROW) {
  4386. char *code = (char *)sqlite3_column_text(statement, 0);
  4387. if (code == NULL) {
  4388. code = "";
  4389. }
  4390. ret = [NSString stringWithUTF8String:code];
  4391. }
  4392. sqlite3_finalize(statement);
  4393. }
  4394. [iSalesDB close_db:db];
  4395. return ret;
  4396. }
  4397. + (NSString *)countryCodeIdByCode:(NSString *)code {
  4398. NSString *ret = nil;
  4399. code = [self translateSingleQuote:code];
  4400. sqlite3 *db = [iSalesDB get_db];
  4401. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  4402. sqlite3_stmt * statement;
  4403. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4404. while (sqlite3_step(statement) == SQLITE_ROW) {
  4405. char *_id = (char *)sqlite3_column_text(statement, 0);
  4406. if (_id == NULL) {
  4407. _id = "";
  4408. }
  4409. ret = [NSString stringWithFormat:@"%s",_id];
  4410. }
  4411. sqlite3_finalize(statement);
  4412. }
  4413. [iSalesDB close_db:db];
  4414. return ret;
  4415. }
  4416. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  4417. NSString *name = nil;
  4418. codeId = [self translateSingleQuote:codeId];
  4419. sqlite3 *db = [iSalesDB get_db];
  4420. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  4421. sqlite3_stmt * statement;
  4422. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4423. while (sqlite3_step(statement) == SQLITE_ROW) {
  4424. char *value = (char *)sqlite3_column_text(statement, 0);
  4425. if (value == NULL) {
  4426. value = "";
  4427. }
  4428. name = [NSString stringWithUTF8String:value];
  4429. }
  4430. sqlite3_finalize(statement);
  4431. }
  4432. [iSalesDB close_db:db];
  4433. return name;
  4434. }
  4435. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  4436. NSString *ret = nil;
  4437. sqlite3 *db = [iSalesDB get_db];
  4438. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  4439. sqlite3_stmt * statement;
  4440. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4441. while (sqlite3_step(statement) == SQLITE_ROW) {
  4442. char *name = (char *)sqlite3_column_text(statement, 0);
  4443. if (name == NULL) {
  4444. name = "";
  4445. }
  4446. ret = [NSString stringWithUTF8String:name];
  4447. }
  4448. sqlite3_finalize(statement);
  4449. }
  4450. [iSalesDB close_db:db];
  4451. return ret;
  4452. }
  4453. + (NSString *)salesRepCodeById:(NSString *)_id {
  4454. NSString *ret = nil;
  4455. _id = [self translateSingleQuote:_id];
  4456. sqlite3 *db = [iSalesDB get_db];
  4457. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  4458. sqlite3_stmt * statement;
  4459. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4460. while (sqlite3_step(statement) == SQLITE_ROW) {
  4461. char *rep = (char *)sqlite3_column_text(statement, 0);
  4462. if (rep == NULL) {
  4463. rep = "";
  4464. }
  4465. ret = [NSString stringWithUTF8String:rep];
  4466. }
  4467. sqlite3_finalize(statement);
  4468. }
  4469. [iSalesDB close_db:db];
  4470. return ret;
  4471. }
  4472. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  4473. char *tx = (char *)sqlite3_column_text(stmt, col);
  4474. if (tx == NULL) {
  4475. tx = "";
  4476. }
  4477. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  4478. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  4479. if (!text) {
  4480. text = @"";
  4481. }
  4482. // 将字符全部为' '的字符串干掉
  4483. int spaceCount = 0;
  4484. for (int i = 0; i < text.length; i++) {
  4485. if ([text characterAtIndex:i] == ' ') {
  4486. spaceCount++;
  4487. }
  4488. }
  4489. if (spaceCount == text.length) {
  4490. text = @"";
  4491. }
  4492. return text;
  4493. }
  4494. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  4495. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  4496. NSData *data = [NSData dataWithContentsOfFile:path];
  4497. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4498. return ret;
  4499. }
  4500. + (NSString *)textFileName:(NSString *)name {
  4501. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  4502. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  4503. if (!text) {
  4504. text = @"";
  4505. }
  4506. return text;
  4507. }
  4508. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  4509. return [[dic objectForKey:key] mutableCopy];
  4510. }
  4511. + (id)translateSingleQuote:(NSString *)string {
  4512. if ([string isKindOfClass:[NSString class]])
  4513. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4514. return string;
  4515. }
  4516. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  4517. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  4518. }
  4519. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  4520. NSString* ret= nil;
  4521. NSString *sqlQuery = nil;
  4522. // 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
  4523. 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];
  4524. sqlite3_stmt * statement;
  4525. // int count=0;
  4526. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4527. {
  4528. if (sqlite3_step(statement) == SQLITE_ROW)
  4529. {
  4530. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  4531. if(imgurl==nil)
  4532. imgurl="";
  4533. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  4534. ret=nsimgurl;
  4535. }
  4536. sqlite3_finalize(statement);
  4537. }
  4538. else
  4539. {
  4540. [ret setValue:@"8" forKey:@"result"];
  4541. }
  4542. // [iSalesDB close_db:db];
  4543. // DebugLog(@"data string: %@",ret );
  4544. return ret;
  4545. }
  4546. #pragma mark contact list
  4547. +(NSDictionary*) offline_contactlist :(NSMutableDictionary *) params
  4548. {
  4549. // contactType = "Sales_Order_Customer";
  4550. // limit = 25;
  4551. // offset = 0;
  4552. // password = 123456;
  4553. // "price_name" = 16;
  4554. // user = EvanK;
  4555. sqlite3 *db = [iSalesDB get_db];
  4556. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  4557. if (contactType) {
  4558. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  4559. } else {
  4560. contactType = @"1 = 1";
  4561. }
  4562. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  4563. DebugLog(@"offline contact list keyword: %@",keyword);
  4564. // advanced search
  4565. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  4566. if (contact_name) {
  4567. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4568. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  4569. } else {
  4570. contact_name = @"";
  4571. }
  4572. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  4573. if (customer_phone) {
  4574. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4575. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  4576. } else {
  4577. customer_phone = @"";
  4578. }
  4579. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  4580. if (customer_fax) {
  4581. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4582. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  4583. } else {
  4584. customer_fax = @"";
  4585. }
  4586. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  4587. if (customer_zipcode) {
  4588. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4589. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  4590. } else {
  4591. customer_zipcode = @"";
  4592. }
  4593. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  4594. if (customer_sales_rep) {
  4595. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4596. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  4597. } else {
  4598. customer_sales_rep = @"";
  4599. }
  4600. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  4601. if (customer_state) {
  4602. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4603. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  4604. } else {
  4605. customer_state = @"";
  4606. }
  4607. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  4608. if (customer_name) {
  4609. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4610. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  4611. } else {
  4612. customer_name = @"";
  4613. }
  4614. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  4615. if (customer_country) {
  4616. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4617. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  4618. } else {
  4619. customer_country = @"";
  4620. }
  4621. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  4622. if (customer_cid) {
  4623. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4624. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  4625. } else {
  4626. customer_cid = @"";
  4627. }
  4628. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  4629. if (customer_city) {
  4630. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4631. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  4632. } else {
  4633. customer_city = @"";
  4634. }
  4635. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  4636. if (customer_address) {
  4637. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4638. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  4639. } else {
  4640. customer_address = @"";
  4641. }
  4642. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  4643. if (customer_email) {
  4644. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4645. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  4646. } else {
  4647. customer_email = @"";
  4648. }
  4649. NSString *price_name = [params valueForKey:@"price_name"];
  4650. if (price_name) {
  4651. if ([price_name containsString:@","]) {
  4652. // 首先从 price表中查处name
  4653. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  4654. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  4655. for (int i = 1;i < pArray.count;i++) {
  4656. NSString *p = pArray[i];
  4657. [mutablePStr appendFormat:@" or type = %@ ",p];
  4658. }
  4659. [mutablePStr appendString:@";"];
  4660. __block NSMutableArray *price_name_array = [NSMutableArray array];
  4661. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4662. char *name = (char *)sqlite3_column_text(stmt, 0);
  4663. if (!name)
  4664. name = "";
  4665. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  4666. }];
  4667. // 再根据name 拼sql
  4668. NSMutableString *mutable_price_name = [NSMutableString string];
  4669. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  4670. for (int i = 1; i < price_name_array.count; i++) {
  4671. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  4672. }
  4673. [mutable_price_name appendString:@")"];
  4674. price_name = mutable_price_name;
  4675. } else {
  4676. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  4677. if ([price_name isEqualToString:@""]) {
  4678. price_name = @"";
  4679. } else {
  4680. __block NSString *price;
  4681. price_name = [self translateSingleQuote:price_name];
  4682. [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) {
  4683. char *p = (char *)sqlite3_column_text(stmt, 0);
  4684. if (p == NULL) {
  4685. p = "";
  4686. }
  4687. price = [NSString stringWithUTF8String:p];
  4688. }];
  4689. if ([price isEqualToString:@""]) {
  4690. price_name = @"";
  4691. } else {
  4692. price = [self translateSingleQuote:price];
  4693. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  4694. }
  4695. }
  4696. }
  4697. } else {
  4698. price_name = @"";
  4699. }
  4700. int limit = [[params valueForKey:@"limit"] intValue];
  4701. int offset = [[params valueForKey:@"offset"] intValue];
  4702. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4703. 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];
  4704. 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];
  4705. // int result= [iSalesDB AddExFunction:db];
  4706. int count =0;
  4707. NSString *sqlQuery = nil;
  4708. if(keyword.length==0)
  4709. {
  4710. // 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];
  4711. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  4712. sqlQuery = sql;
  4713. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  4714. }
  4715. else
  4716. {
  4717. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  4718. keyword = keyword.lowercaseString;
  4719. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4720. 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];
  4721. 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]];
  4722. }
  4723. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  4724. sqlite3_stmt * statement;
  4725. [ret setValue:@"2" forKey:@"result"];
  4726. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  4727. // 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";
  4728. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4729. {
  4730. int i = 0;
  4731. while (sqlite3_step(statement) == SQLITE_ROW)
  4732. {
  4733. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4734. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4735. int editable = sqlite3_column_int(statement, 0);
  4736. char *company_name = (char*)sqlite3_column_text(statement, 1);
  4737. NSString *nscompany_name =nil;
  4738. if(company_name==nil)
  4739. nscompany_name=@"";
  4740. else
  4741. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  4742. char *country = (char*)sqlite3_column_text(statement, 2);
  4743. if(country==nil)
  4744. country="";
  4745. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  4746. // char *addr = (char*)sqlite3_column_text(statement, 3);
  4747. // if(addr==nil)
  4748. // addr="";
  4749. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  4750. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  4751. if(zipcode==nil)
  4752. zipcode="";
  4753. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  4754. char *state = (char*)sqlite3_column_text(statement, 5);
  4755. if(state==nil)
  4756. state="";
  4757. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  4758. char *city = (char*)sqlite3_column_text(statement, 6);
  4759. if(city==nil)
  4760. city="";
  4761. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  4762. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  4763. // NSString *nscontact_name = nil;
  4764. // if(contact_name==nil)
  4765. // nscontact_name=@"";
  4766. // else
  4767. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  4768. char *phone = (char*)sqlite3_column_text(statement, 8);
  4769. NSString *nsphone = nil;
  4770. if(phone==nil)
  4771. nsphone=@"";
  4772. else
  4773. nsphone= [[NSString alloc]initWithUTF8String:phone];
  4774. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  4775. if(contact_id==nil)
  4776. contact_id="";
  4777. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4778. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  4779. if(addr_1==nil)
  4780. addr_1="";
  4781. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4782. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  4783. if(addr_2==nil)
  4784. addr_2="";
  4785. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4786. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  4787. if(addr_3==nil)
  4788. addr_3="";
  4789. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4790. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  4791. if(addr_4==nil)
  4792. addr_4="";
  4793. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4794. char *first_name = (char*)sqlite3_column_text(statement, 14);
  4795. if(first_name==nil)
  4796. first_name="";
  4797. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  4798. char *last_name = (char*)sqlite3_column_text(statement, 15);
  4799. if(last_name==nil)
  4800. last_name="";
  4801. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  4802. char *fax = (char*)sqlite3_column_text(statement, 16);
  4803. NSString *nsfax = nil;
  4804. if(fax==nil)
  4805. nsfax=@"";
  4806. else
  4807. {
  4808. nsfax= [[NSString alloc]initWithUTF8String:fax];
  4809. if(nsfax.length>0)
  4810. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  4811. }
  4812. char *email = (char*)sqlite3_column_text(statement, 17);
  4813. NSString *nsemail = nil;
  4814. if(email==nil)
  4815. nsemail=@"";
  4816. else
  4817. {
  4818. nsemail= [[NSString alloc]initWithUTF8String:email];
  4819. if(nsemail.length>0)
  4820. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  4821. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  4822. }
  4823. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4824. [arr_name addObject:nsfirst_name];
  4825. [arr_name addObject:nslast_name];
  4826. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  4827. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  4828. {
  4829. // decrypt
  4830. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  4831. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  4832. nsphone=[AESCrypt fastdecrypt:nsphone];
  4833. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4834. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  4835. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4836. }
  4837. [arr_addr addObject:nscompany_name];
  4838. [arr_addr addObject:nscontact_name];
  4839. [arr_addr addObject:@"<br>"];
  4840. [arr_addr addObject:nsaddr_1];
  4841. [arr_addr addObject:nsaddr_2];
  4842. [arr_addr addObject:nsaddr_3];
  4843. [arr_addr addObject:nsaddr_4];
  4844. //[arr_addr addObject:nsaddr];
  4845. [arr_addr addObject:nszipcode];
  4846. [arr_addr addObject:nscity];
  4847. [arr_addr addObject:nsstate];
  4848. [arr_addr addObject:nscountry];
  4849. [arr_addr addObject:@"<br>"];
  4850. [arr_addr addObject:nsphone];
  4851. [arr_addr addObject:nsfax];
  4852. [arr_addr addObject:nsemail];
  4853. NSString * name = [RAConvertor arr2string:arr_addr separator:@", " trim:true];
  4854. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  4855. [item setValue:name forKey:@"name"];
  4856. [item setValue:nscontact_id forKey:@"contact_id"];
  4857. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  4858. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4859. i++;
  4860. }
  4861. UIApplication * app = [UIApplication sharedApplication];
  4862. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4863. [ret setValue:appDelegate.mode forKey:@"mode"];
  4864. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  4865. sqlite3_finalize(statement);
  4866. }
  4867. [iSalesDB close_db:db];
  4868. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4869. return ret;
  4870. }
  4871. #pragma mark contact Advanced search
  4872. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  4873. {
  4874. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  4875. return [RAConvertor dict2data:contactAdvanceDic];
  4876. }
  4877. #pragma mark create new contact
  4878. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  4879. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  4880. NSData *data = [NSData dataWithContentsOfFile:path];
  4881. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4882. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  4883. NSString *countryCode = nil;
  4884. NSString *countryCode_id = nil;
  4885. NSString *stateCode = nil;
  4886. NSString *city = nil;
  4887. NSString *zipCode = nil;
  4888. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  4889. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  4890. NSString *code_id = params[@"country"];
  4891. countryCode_id = code_id;
  4892. countryCode = [self countryCodeByid:code_id];
  4893. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  4894. NSString *zip_code = params[@"zipcode"];
  4895. // 剔除全部为空格
  4896. int spaceCount = 0;
  4897. for (int i = 0; i < zip_code.length; i++) {
  4898. if ([zip_code characterAtIndex:i] == ' ') {
  4899. spaceCount++;
  4900. }
  4901. }
  4902. if (spaceCount == zip_code.length) {
  4903. zip_code = @"";
  4904. }
  4905. zipCode = zip_code;
  4906. if (zipCode.length > 0) {
  4907. countryCode_id = params[@"country"];
  4908. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  4909. countryCode = [dic valueForKey:@"country_code"];
  4910. if (!countryCode) {
  4911. // countryCode = @"US";
  4912. NSString *code_id = params[@"country"];
  4913. countryCode = [self countryCodeByid:code_id];
  4914. }
  4915. stateCode = [dic valueForKey:@"state_code"];
  4916. if (!stateCode.length) {
  4917. stateCode = params[@"state"];
  4918. }
  4919. city = [dic valueForKey:@"city"];
  4920. if (!city.length) {
  4921. city = params[@"city"];
  4922. }
  4923. // zip code
  4924. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  4925. [zipDic setValue:zipCode forKey:@"value"];
  4926. [section_0 setValue:zipDic forKey:@"item_11"];
  4927. } else {
  4928. NSString *code_id = params[@"country"];
  4929. countryCode = [self countryCodeByid:code_id];
  4930. stateCode = params[@"state"];
  4931. city = params[@"city"];
  4932. }
  4933. }
  4934. } else {
  4935. // default: US United States
  4936. countryCode = @"US";
  4937. countryCode_id = @"228";
  4938. }
  4939. // country
  4940. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  4941. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  4942. // state
  4943. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  4944. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  4945. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  4946. // city
  4947. if (city) {
  4948. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  4949. [cityDic setValue:city forKey:@"value"];
  4950. [section_0 setValue:cityDic forKey:@"item_13"];
  4951. }
  4952. // price type
  4953. NSDictionary *priceDic = [self offline_getPrice];
  4954. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  4955. // contact type
  4956. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  4957. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  4958. // Sales Rep
  4959. NSDictionary *repDic = [self offline_getSalesRep];
  4960. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  4961. [ret setValue:section_0 forKey:@"section_0"];
  4962. return [RAConvertor dict2data:ret];
  4963. }
  4964. #pragma mark save
  4965. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  4966. NSString *addr = nil;
  4967. NSString *contact_name = nil;
  4968. NSString *companyName = [params objectForKey:@"company"];
  4969. if (companyName) {
  4970. companyName = [AESCrypt fastencrypt:companyName];
  4971. } else {
  4972. companyName = @"";
  4973. }
  4974. DebugLog(@"company");
  4975. companyName = [self translateSingleQuote:companyName];
  4976. NSString *addr1 = [params objectForKey:@"address"];
  4977. NSString *addr2 = [params objectForKey:@"address2"];
  4978. NSString *addr3 = [params objectForKey:@"address_3"];
  4979. NSString *addr4 = [params objectForKey:@"address_4"];
  4980. if (!addr2) {
  4981. addr2 = @"";
  4982. }
  4983. if (!addr3) {
  4984. addr3 = @"";
  4985. }
  4986. if (!addr4) {
  4987. addr4 = @"";
  4988. }
  4989. if (!addr1) {
  4990. addr1 = @"";
  4991. }
  4992. DebugLog(@"addr");
  4993. addr = [RAConvertor arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  4994. addr = [AESCrypt fastencrypt:addr];
  4995. addr = [self translateSingleQuote:addr];
  4996. if (addr1 && ![addr1 isEqualToString:@""]) {
  4997. addr1 = [AESCrypt fastencrypt:addr1];
  4998. }
  4999. addr1 = [self translateSingleQuote:addr1];
  5000. addr2 = [self translateSingleQuote:addr2];
  5001. addr3 = [self translateSingleQuote:addr3];
  5002. addr4 = [self translateSingleQuote:addr4];
  5003. NSString *country = [params objectForKey:@"country"];
  5004. if (country) {
  5005. country = [self countryNameByCountryCodeId:country];
  5006. } else {
  5007. country = @"";
  5008. }
  5009. DebugLog(@"country");
  5010. country = [self translateSingleQuote:country];
  5011. NSString *state = [params objectForKey:@"state"];
  5012. if (!state) {
  5013. state = @"";
  5014. }
  5015. DebugLog(@"state");
  5016. state = [self translateSingleQuote:state];
  5017. NSString *city = [params objectForKey:@"city"];
  5018. if (!city) {
  5019. city = @"";
  5020. }
  5021. city = [self translateSingleQuote:city];
  5022. NSString *zipcode = [params objectForKey:@"zipcode"];
  5023. if (!zipcode) {
  5024. zipcode = @"";
  5025. }
  5026. DebugLog(@"zip");
  5027. zipcode = [self translateSingleQuote:zipcode];
  5028. NSString *fistName = [params objectForKey:@"firstname"];
  5029. if (!fistName) {
  5030. fistName = @"";
  5031. }
  5032. NSString *lastName = [params objectForKey:@"lastname"];
  5033. if (!lastName) {
  5034. lastName = @"";
  5035. }
  5036. contact_name = [RAConvertor arr2string:@[fistName,lastName] separator:@" " trim:true];
  5037. DebugLog(@"contact_name");
  5038. contact_name = [self translateSingleQuote:contact_name];
  5039. fistName = [self translateSingleQuote:fistName];
  5040. lastName = [self translateSingleQuote:lastName];
  5041. NSString *phone = [params objectForKey:@"phone"];
  5042. if (phone) {
  5043. phone = [AESCrypt fastencrypt:phone];
  5044. } else {
  5045. phone = @"";
  5046. }
  5047. DebugLog(@"PHONE");
  5048. phone = [self translateSingleQuote:phone];
  5049. NSString *fax = [params objectForKey:@"fax"];
  5050. if (!fax) {
  5051. fax = @"";
  5052. }
  5053. DebugLog(@"FAX");
  5054. fax = [self translateSingleQuote:fax];
  5055. NSString *email = [params objectForKey:@"email"];
  5056. if (!email) {
  5057. email = @"";
  5058. }
  5059. DebugLog(@"EMAIL:%@",email);
  5060. email = [self translateSingleQuote:email];
  5061. NSString *notes = [params objectForKey:@"contact_notes"];
  5062. if (!notes) {
  5063. notes = @"";
  5064. }
  5065. DebugLog(@"NOTE:%@",notes);
  5066. notes = [self translateSingleQuote:notes];
  5067. NSString *price = [params objectForKey:@"price_name"];
  5068. if (price) {
  5069. price = [self priceNameByPriceId:price];
  5070. } else {
  5071. price = @"";
  5072. }
  5073. DebugLog(@"PRICE");
  5074. price = [self translateSingleQuote:price];
  5075. NSString *salesRep = [params objectForKey:@"sales_rep"];
  5076. if (salesRep) {
  5077. salesRep = [self salesRepCodeById:salesRep];
  5078. } else {
  5079. salesRep = @"";
  5080. }
  5081. salesRep = [self translateSingleQuote:salesRep];
  5082. NSString *img = [params objectForKey:@"business_card"];
  5083. NSArray *array = [img componentsSeparatedByString:@","];
  5084. NSString *img_0 = array[0];
  5085. if (!img_0) {
  5086. img_0 = @"";
  5087. }
  5088. img_0 = [self translateSingleQuote:img_0];
  5089. NSString *img_1 = array[1];
  5090. if (!img_1) {
  5091. img_1 = @"";
  5092. }
  5093. img_1 = [self translateSingleQuote:img_1];
  5094. NSString *img_2 = array[2];
  5095. if (!img_2) {
  5096. img_2 = @"";
  5097. }
  5098. img_2 = [self translateSingleQuote:img_2];
  5099. NSString *contact_id = [NSUUID UUID].UUIDString;
  5100. NSString *contact_type = [params objectForKey:@"type_name"];
  5101. if (!contact_type) {
  5102. contact_type = @"";
  5103. }
  5104. contact_type = [self translateSingleQuote:contact_type];
  5105. // 判断更新时是否为customer
  5106. if (update) {
  5107. contact_id = [params objectForKey:@"contact_id"];
  5108. if (!contact_id) {
  5109. contact_id = @"";
  5110. }
  5111. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  5112. __block int customer = 0;
  5113. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5114. customer = sqlite3_column_int(stmt, 0);
  5115. }];
  5116. isCustomer = customer ? YES : NO;
  5117. }
  5118. NSMutableDictionary *sync_dic = [params mutableCopy];
  5119. if (isCustomer) {
  5120. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  5121. } else {
  5122. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  5123. }
  5124. NSString *sync_data = nil;
  5125. NSString *sql = nil;
  5126. if (update){
  5127. contact_id = [params objectForKey:@"contact_id"];
  5128. if (!contact_id) {
  5129. contact_id = @"";
  5130. }
  5131. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5132. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5133. sync_data = [RAConvertor dict2string:sync_dic];
  5134. sync_data = [self translateSingleQuote:sync_data];
  5135. 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 = '%@' 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,contact_id];
  5136. } else {
  5137. contact_id = [self translateSingleQuote:contact_id];
  5138. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5139. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5140. sync_data = [RAConvertor dict2string:sync_dic];
  5141. sync_data = [self translateSingleQuote:sync_data];
  5142. 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) values (1,'%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@',1,'%@',%d,%d,0,%d,0,1,'%@','%@','%@','%@')",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];
  5143. }
  5144. int result = [iSalesDB execSql:sql];
  5145. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  5146. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  5147. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  5148. }
  5149. #pragma mark save new contact
  5150. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  5151. {
  5152. return [self offline_saveContact:params update:NO isCustomer:YES];
  5153. }
  5154. #pragma mark edit contact
  5155. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  5156. {
  5157. // {
  5158. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  5159. // password = 123456;
  5160. // user = EvanK;
  5161. // }
  5162. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  5163. NSData *data = [NSData dataWithContentsOfFile:path];
  5164. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5165. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5166. NSString *countryCode = nil;
  5167. NSString *countryCode_id = nil;
  5168. NSString *stateCode = nil;
  5169. /*------contact infor------*/
  5170. __block NSString *country = nil;
  5171. __block NSString *company_name = nil;
  5172. __block NSString *contact_id = params[@"contact_id"];
  5173. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  5174. __block NSString *zipcode = nil;
  5175. __block NSString *state = nil; // state_code
  5176. __block NSString *city = nil; //
  5177. __block NSString *firt_name,*last_name;
  5178. __block NSString *phone,*fax,*email;
  5179. __block NSString *notes,*price_type,*sales_rep;
  5180. __block NSString *img_0,*img_1,*img_2;
  5181. __block NSString *contact_type;
  5182. contact_id = [self translateSingleQuote:contact_id];
  5183. 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 from offline_contact where contact_id = '%@';",contact_id];
  5184. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5185. country = [self textAtColumn:0 statement:stmt]; // country name
  5186. company_name = [self textAtColumn:1 statement:stmt];
  5187. addr_1 = [self textAtColumn:2 statement:stmt];
  5188. addr_2 = [self textAtColumn:3 statement:stmt];
  5189. addr_3 = [self textAtColumn:4 statement:stmt];
  5190. addr_4 = [self textAtColumn:5 statement:stmt];
  5191. zipcode = [self textAtColumn:6 statement:stmt];
  5192. state = [self textAtColumn:7 statement:stmt]; // state code
  5193. city = [self textAtColumn:8 statement:stmt];
  5194. firt_name = [self textAtColumn:9 statement:stmt];
  5195. last_name = [self textAtColumn:10 statement:stmt];
  5196. phone = [self textAtColumn:11 statement:stmt];
  5197. fax = [self textAtColumn:12 statement:stmt];
  5198. email = [self textAtColumn:13 statement:stmt];
  5199. notes = [self textAtColumn:14 statement:stmt];
  5200. price_type = [self textAtColumn:15 statement:stmt]; // name
  5201. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  5202. img_0 = [self textAtColumn:17 statement:stmt];
  5203. img_1 = [self textAtColumn:18 statement:stmt];
  5204. img_2 = [self textAtColumn:19 statement:stmt];
  5205. contact_type = [self textAtColumn:20 statement:stmt];
  5206. }];
  5207. // decrypt
  5208. if (company_name) {
  5209. company_name = [AESCrypt fastdecrypt:company_name];
  5210. }
  5211. if (addr_1) {
  5212. addr_1 = [AESCrypt fastdecrypt:addr_1];
  5213. }
  5214. if (phone) {
  5215. phone = [AESCrypt fastdecrypt:phone];
  5216. }
  5217. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  5218. countryCode = [iSalesDB jk_queryText:countrySql];
  5219. stateCode = state;
  5220. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5221. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  5222. NSString *code_id = params[@"country"];
  5223. countryCode_id = code_id;
  5224. countryCode = [self countryCodeByid:code_id];
  5225. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  5226. NSString *zip_code = params[@"zipcode"];
  5227. // 剔除全部为空格
  5228. int spaceCount = 0;
  5229. for (int i = 0; i < zip_code.length; i++) {
  5230. if ([zip_code characterAtIndex:i] == ' ') {
  5231. spaceCount++;
  5232. }
  5233. }
  5234. if (spaceCount == zip_code.length) {
  5235. zip_code = @"";
  5236. }
  5237. if (zipcode.length > 0) {
  5238. zipcode = zip_code;
  5239. countryCode_id = params[@"country"];
  5240. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  5241. countryCode = [dic valueForKey:@"country_code"];
  5242. if (!countryCode) {
  5243. NSString *code_id = params[@"country"];
  5244. countryCode = [self countryCodeByid:code_id];
  5245. }
  5246. stateCode = [dic valueForKey:@"state_code"];
  5247. if (!stateCode.length) {
  5248. stateCode = params[@"state"];
  5249. }
  5250. city = [dic valueForKey:@"city"];
  5251. if (!city.length) {
  5252. city = params[@"city"];
  5253. }
  5254. // zip code
  5255. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  5256. [zipDic setValue:zipcode forKey:@"value"];
  5257. [section_0 setValue:zipDic forKey:@"item_8"];
  5258. } else {
  5259. NSString *code_id = params[@"country"];
  5260. countryCode = [self countryCodeByid:code_id];
  5261. stateCode = params[@"state"];
  5262. city = params[@"city"];
  5263. }
  5264. }
  5265. }
  5266. // 0 Country
  5267. // 1 Company Name
  5268. // 2 Contact ID
  5269. // 3 Picture
  5270. // 4 Address 1
  5271. // 5 Address 2
  5272. // 6 Address 3
  5273. // 7 Address 4
  5274. // 8 Zip Code
  5275. // 9 State/Province
  5276. // 10 City
  5277. // 11 Contact First Name
  5278. // 12 Contact Last Name
  5279. // 13 Phone
  5280. // 14 Fax
  5281. // 15 Email
  5282. // 16 Contact Notes
  5283. // 17 Price Type
  5284. // 18 Contact Type
  5285. // 19 Sales Rep
  5286. // country
  5287. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5288. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  5289. // company
  5290. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  5291. // contact_id
  5292. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  5293. // picture
  5294. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  5295. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  5296. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  5297. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  5298. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  5299. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  5300. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  5301. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  5302. // addr 1 2 3 4
  5303. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  5304. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  5305. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  5306. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  5307. // zip code
  5308. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  5309. // state
  5310. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5311. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  5312. // city
  5313. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  5314. // first last
  5315. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  5316. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  5317. // phone fax email
  5318. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  5319. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  5320. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  5321. // notes
  5322. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  5323. // price
  5324. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  5325. for (NSString *key in priceDic.allKeys) {
  5326. if ([key containsString:@"val_"]) {
  5327. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  5328. if ([dic[@"value"] isEqualToString:price_type]) {
  5329. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5330. [priceDic setValue:dic forKey:key];
  5331. }
  5332. }
  5333. }
  5334. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  5335. // Contact Rep
  5336. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  5337. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5338. // Sales Rep
  5339. NSMutableDictionary *repDic = [[self offline_getSalesRep] mutableCopy];
  5340. for (NSString *key in repDic.allKeys) {
  5341. if ([key containsString:@"val_"]) {
  5342. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  5343. NSString *value = dic[@"value"];
  5344. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  5345. if (code && [code isEqualToString:sales_rep]) {
  5346. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5347. [repDic setValue:dic forKey:key];
  5348. }
  5349. }
  5350. }
  5351. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  5352. [ret setValue:section_0 forKey:@"section_0"];
  5353. return [RAConvertor dict2data:ret];
  5354. }
  5355. #pragma mark save contact
  5356. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  5357. {
  5358. return [self offline_saveContact:params update:YES isCustomer:YES];
  5359. }
  5360. #pragma mark category
  5361. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5362. if (ck) {
  5363. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  5364. for (NSString *key in res.allKeys) {
  5365. if (![key isEqualToString:@"count"]) {
  5366. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  5367. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5368. if ([val[@"value"] isEqualToString:ck]) {
  5369. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5370. }
  5371. [res setValue:val forKey:key];
  5372. }
  5373. }
  5374. [dic setValue:res forKey:valueKey];
  5375. }
  5376. }
  5377. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  5378. // NSString* orderCode = [params valueForKey:@"orderCode"];
  5379. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5380. NSString* category = [params valueForKey:@"category"];
  5381. if (!category || [category isEqualToString:@""]) {
  5382. category = @"%";
  5383. }
  5384. category = [self translateSingleQuote:category];
  5385. int limit = [[params valueForKey:@"limit"] intValue];
  5386. int offset = [[params valueForKey:@"offset"] intValue];
  5387. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5388. NSString *limit_str = @"";
  5389. if (limited) {
  5390. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  5391. }
  5392. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5393. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5394. sqlite3 *db = [iSalesDB get_db];
  5395. // [iSalesDB AddExFunction:db];
  5396. int count;
  5397. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  5398. 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 %@ ;",appDelegate.order_code, category,limit_str];
  5399. double price_min = 0;
  5400. double price_max = 0;
  5401. if ([params.allKeys containsObject:@"alert"]) {
  5402. // alert
  5403. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5404. NSString *alert = params[@"alert"];
  5405. if ([alert isEqualToString:@"Display All"]) {
  5406. alert = [NSString stringWithFormat:@""];
  5407. } else {
  5408. alert = [self translateSingleQuote:alert];
  5409. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5410. }
  5411. // available
  5412. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5413. NSString *available = params[@"available"];
  5414. NSString *available_condition;
  5415. if ([available isEqualToString:@"Display All"]) {
  5416. available_condition = @"";
  5417. } else if ([available isEqualToString:@"Available Now"]) {
  5418. available_condition = @"and availability > 0";
  5419. } else {
  5420. available_condition = @"and availability == 0";
  5421. }
  5422. // best seller
  5423. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5424. NSString *best_seller = @"";
  5425. NSString *order_best_seller = @"m.name asc";
  5426. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5427. best_seller = @"and best_seller > 0";
  5428. order_best_seller = @"m.best_seller desc,m.name asc";
  5429. }
  5430. // price
  5431. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5432. NSString *price = params[@"price"];
  5433. price_min = 0;
  5434. price_max = MAXFLOAT;
  5435. if (appDelegate.user && price != nil) {
  5436. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  5437. NSMutableString *priceName = [NSMutableString string];
  5438. for (int i = 0; i < priceTypeArray.count; i++) {
  5439. NSString *pricetype = priceTypeArray[i];
  5440. pricetype = [self translateSingleQuote:pricetype];
  5441. if (i == 0) {
  5442. [priceName appendFormat:@"'%@'",pricetype];
  5443. } else {
  5444. [priceName appendFormat:@",'%@'",pricetype];
  5445. }
  5446. }
  5447. if ([price isEqualToString:@"Display All"]) {
  5448. price = [NSString stringWithFormat:@""];
  5449. } else if([price containsString:@"+"]){
  5450. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5451. price_min = [price doubleValue];
  5452. 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];
  5453. } else {
  5454. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5455. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5456. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5457. 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];
  5458. }
  5459. } else {
  5460. price = @"";
  5461. }
  5462. // sold_by_qty : Sold in quantities of %@
  5463. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5464. NSString *qty = params[@"sold_by_qty"];
  5465. if ([qty isEqualToString:@"Display All"]) {
  5466. qty = @"";
  5467. } else {
  5468. qty = [self translateSingleQuote:qty];
  5469. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5470. }
  5471. // cate
  5472. // category = [self translateSingleQuote:category];
  5473. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  5474. // cate mutiple selection
  5475. NSString *category_id = params[@"category"];
  5476. NSMutableArray *cate_id_array = nil;
  5477. NSMutableString *cateWhere = [NSMutableString string];
  5478. if ([category_id isEqualToString:@""] || !category_id) {
  5479. [cateWhere appendString:@"1 = 1"];
  5480. } else {
  5481. if ([category_id containsString:@","]) {
  5482. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  5483. } else {
  5484. cate_id_array = [@[category_id] mutableCopy];
  5485. }
  5486. [cateWhere appendString:@"("];
  5487. for (int i = 0; i < cate_id_array.count; i++) {
  5488. if (i == 0) {
  5489. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5490. } else {
  5491. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  5492. }
  5493. }
  5494. [cateWhere appendString:@")"];
  5495. }
  5496. // where bestseller > 0 order by bestseller desc
  5497. // sql query: alert availability(int) best_seller(int) price qty
  5498. 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,appDelegate.order_code,order_best_seller,limit_str];
  5499. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  5500. }
  5501. DebugLog(@"offline category where: %@",where);
  5502. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  5503. if (!appDelegate.user) {
  5504. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  5505. }
  5506. [ret setValue:filter forKey:@"filter"];
  5507. DebugLog(@"offline_category sql:%@",sqlQuery);
  5508. sqlite3_stmt * statement;
  5509. [ret setValue:@"2" forKey:@"result"];
  5510. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  5511. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  5512. // int count=0;
  5513. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5514. {
  5515. int i=0;
  5516. while (sqlite3_step(statement) == SQLITE_ROW)
  5517. {
  5518. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5519. char *name = (char*)sqlite3_column_text(statement, 0);
  5520. if(name==nil)
  5521. name="";
  5522. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5523. char *description = (char*)sqlite3_column_text(statement, 1);
  5524. if(description==nil)
  5525. description="";
  5526. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  5527. int product_id = sqlite3_column_int(statement, 2);
  5528. int wid = sqlite3_column_int(statement, 3);
  5529. int closeout = sqlite3_column_int(statement, 4);
  5530. int cid = sqlite3_column_int(statement, 5);
  5531. int wisdelete = sqlite3_column_int(statement, 6);
  5532. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  5533. int more_color = sqlite3_column_int(statement, 8);
  5534. // Defaul Category ID
  5535. __block NSString *categoryID = nil;
  5536. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  5537. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5538. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  5539. if(default_category==nil)
  5540. default_category="";
  5541. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  5542. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  5543. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  5544. categoryID = nsdefault_category;
  5545. }];
  5546. if (!categoryID.length) {
  5547. NSString *cateIDs = params[@"category"];
  5548. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  5549. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  5550. for (NSString *cateID in requestCategoryArr) {
  5551. BOOL needBreak = NO;
  5552. for (NSString *itemCateIDBox in itemCategoryArr) {
  5553. if (itemCateIDBox.length > 4) {
  5554. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  5555. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  5556. if ([itemCategoryID isEqualToString:cateID]) {
  5557. needBreak = YES;
  5558. categoryID = itemCategoryID;
  5559. break;
  5560. }
  5561. }
  5562. }
  5563. if (needBreak) {
  5564. break;
  5565. }
  5566. }
  5567. }
  5568. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  5569. if(wid !=0 && wisdelete != 1)
  5570. [item setValue:@"true" forKey:@"wish_exists"];
  5571. else
  5572. [item setValue:@"false" forKey:@"wish_exists"];
  5573. if(closeout==0)
  5574. [item setValue:@"false" forKey:@"is_closeout"];
  5575. else
  5576. [item setValue:@"true" forKey:@"is_closeout"];
  5577. if(cid==0)
  5578. [item setValue:@"false" forKey:@"cart_exists"];
  5579. else
  5580. [item setValue:@"true" forKey:@"cart_exists"];
  5581. if (more_color == 0) {
  5582. [item setObject:@(false) forKey:@"more_color"];
  5583. } else if (more_color == 1) {
  5584. [item setObject:@(true) forKey:@"more_color"];
  5585. }
  5586. [item addEntriesFromDictionary:imgjson];
  5587. // [item setValue:nsurl forKey:@"img"];
  5588. [item setValue:nsname forKey:@"name"];
  5589. [item setValue:nsdescription forKey:@"description"];
  5590. if (categoryID) {
  5591. [item setValue:categoryID forKey:@"item_category_id"];
  5592. }
  5593. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5594. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5595. i++;
  5596. }
  5597. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5598. [ret setObject:items forKey:@"items"];
  5599. sqlite3_finalize(statement);
  5600. } else {
  5601. DebugLog(@"nothing...");
  5602. }
  5603. DebugLog(@"count:%d",count);
  5604. [iSalesDB close_db:db];
  5605. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  5606. return ret;
  5607. }
  5608. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  5609. {
  5610. return [self categoryList:params limited:YES];
  5611. }
  5612. # pragma mark item search
  5613. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  5614. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5615. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  5616. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5617. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5618. // category
  5619. NSDictionary *category_menu = [self offline_category_menu];
  5620. [filter setValue:category_menu forKey:@"category"];
  5621. NSString* where= nil;
  5622. NSString* orderby= @"m.name";
  5623. if (!filterSearch) {
  5624. int covertype = [[params valueForKey:@"covertype"] intValue];
  5625. switch (covertype) {
  5626. case 0:
  5627. {
  5628. where=@"m.category like'%%#005#%%'";
  5629. break;
  5630. }
  5631. case 1:
  5632. {
  5633. where=@"m.alert like '%QS%'";
  5634. break;
  5635. }
  5636. case 2:
  5637. {
  5638. where=@"m.availability>0";
  5639. break;
  5640. }
  5641. case 3:
  5642. {
  5643. where=@"m.best_seller>0";
  5644. orderby=@"m.best_seller desc,m.name asc";
  5645. break;
  5646. }
  5647. default:
  5648. where=@"1=1";
  5649. break;
  5650. }
  5651. }
  5652. int limit = [[params valueForKey:@"limit"] intValue];
  5653. int offset = [[params valueForKey:@"offset"] intValue];
  5654. NSString *limit_str = @"";
  5655. if (limited) {
  5656. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  5657. }
  5658. sqlite3 *db = [iSalesDB get_db];
  5659. // [iSalesDB AddExFunction:db];
  5660. int count;
  5661. NSString *sqlQuery = nil;
  5662. where = [where stringByAppendingString:@" and m.is_active = 1"];
  5663. 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 %@ %@;",appDelegate.order_code, where,orderby,limit_str];
  5664. double price_min = 0;
  5665. double price_max = 0;
  5666. if (filterSearch) {
  5667. // alert
  5668. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5669. NSString *alert = params[@"alert"];
  5670. if ([alert isEqualToString:@"Display All"]) {
  5671. alert = [NSString stringWithFormat:@""];
  5672. } else {
  5673. alert = [self translateSingleQuote:alert];
  5674. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5675. }
  5676. // available
  5677. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5678. NSString *available = params[@"available"];
  5679. NSString *available_condition;
  5680. if ([available isEqualToString:@"Display All"]) {
  5681. available_condition = @"";
  5682. } else if ([available isEqualToString:@"Available Now"]) {
  5683. available_condition = @"and availability > 0";
  5684. } else {
  5685. available_condition = @"and availability == 0";
  5686. }
  5687. // best seller
  5688. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5689. NSString *best_seller = @"";
  5690. NSString *order_best_seller = @"m.name asc";
  5691. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5692. best_seller = @"and best_seller > 0";
  5693. order_best_seller = @"m.best_seller desc,m.name asc";
  5694. }
  5695. // price
  5696. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5697. NSString *price = params[@"price"];
  5698. price_min = 0;
  5699. price_max = MAXFLOAT;
  5700. if (appDelegate.user) {
  5701. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  5702. NSMutableString *priceName = [NSMutableString string];
  5703. for (int i = 0; i < priceTypeArray.count; i++) {
  5704. NSString *pricetype = priceTypeArray[i];
  5705. pricetype = [self translateSingleQuote:pricetype];
  5706. if (i == 0) {
  5707. [priceName appendFormat:@"'%@'",pricetype];
  5708. } else {
  5709. [priceName appendFormat:@",'%@'",pricetype];
  5710. }
  5711. }
  5712. if ([price isEqualToString:@"Display All"]) {
  5713. price = [NSString stringWithFormat:@""];
  5714. } else if([price containsString:@"+"]){
  5715. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5716. price_min = [price doubleValue];
  5717. 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];
  5718. } else {
  5719. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5720. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5721. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5722. 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];
  5723. }
  5724. } else {
  5725. price = @"";
  5726. }
  5727. // sold_by_qty : Sold in quantities of %@
  5728. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5729. NSString *qty = params[@"sold_by_qty"];
  5730. if ([qty isEqualToString:@"Display All"]) {
  5731. qty = @"";
  5732. } else {
  5733. qty = [self translateSingleQuote:qty];
  5734. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5735. }
  5736. // category
  5737. NSString *category_id = params[@"ctgId"];
  5738. NSMutableArray *cate_id_array = nil;
  5739. NSMutableString *cateWhere = [NSMutableString string];
  5740. if ([category_id isEqualToString:@""] || !category_id) {
  5741. [cateWhere appendString:@"1 = 1"];
  5742. } else {
  5743. if ([category_id containsString:@","]) {
  5744. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  5745. } else {
  5746. cate_id_array = [@[category_id] mutableCopy];
  5747. }
  5748. /*-----------*/
  5749. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  5750. [cateWhere appendString:@"("];
  5751. for (int i = 0; i < cate_id_array.count; i++) {
  5752. for (NSString *key0 in cateDic.allKeys) {
  5753. if ([key0 containsString:@"category_"]) {
  5754. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  5755. for (NSString *key1 in category0.allKeys) {
  5756. if ([key1 containsString:@"category_"]) {
  5757. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  5758. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5759. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  5760. cate_id_array[i] = [category1 objectForKey:@"id"];
  5761. if (i == 0) {
  5762. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5763. } else {
  5764. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  5765. }
  5766. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5767. [category0 setValue:category1 forKey:key1];
  5768. [cateDic setValue:category0 forKey:key0];
  5769. }
  5770. } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  5771. if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  5772. cate_id_array[i] = [category0 objectForKey:@"id"];
  5773. if (i == 0) {
  5774. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5775. } else {
  5776. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  5777. }
  5778. [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5779. [cateDic setValue:category0 forKey:key0];
  5780. }
  5781. }
  5782. }
  5783. }
  5784. }
  5785. }
  5786. [cateWhere appendString:@")"];
  5787. [filter setValue:cateDic forKey:@"category"];
  5788. }
  5789. // where bestseller > 0 order by bestseller desc
  5790. // sql query: alert availability(int) best_seller(int) price qty
  5791. 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,price,appDelegate.order_code,order_best_seller,limit_str];
  5792. // count
  5793. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  5794. }
  5795. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  5796. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  5797. if (!appDelegate.user) {
  5798. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  5799. }
  5800. [ret setValue:filter forKey:@"filter"];
  5801. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  5802. sqlite3_stmt * statement;
  5803. [ret setValue:@"2" forKey:@"result"];
  5804. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  5805. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  5806. // int count=0;
  5807. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5808. {
  5809. int i=0;
  5810. while (sqlite3_step(statement) == SQLITE_ROW)
  5811. {
  5812. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5813. char *name = (char*)sqlite3_column_text(statement, 0);
  5814. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5815. char *description = (char*)sqlite3_column_text(statement, 1);
  5816. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  5817. int product_id = sqlite3_column_int(statement, 2);
  5818. int wid = sqlite3_column_int(statement, 3);
  5819. int closeout = sqlite3_column_int(statement, 4);
  5820. int cid = sqlite3_column_int(statement, 5);
  5821. int wisdelete = sqlite3_column_int(statement, 6);
  5822. int more_color = sqlite3_column_int(statement, 7);
  5823. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  5824. if(wid !=0 && wisdelete != 1)
  5825. [item setValue:@"true" forKey:@"wish_exists"];
  5826. else
  5827. [item setValue:@"false" forKey:@"wish_exists"];
  5828. if(closeout==0)
  5829. [item setValue:@"false" forKey:@"is_closeout"];
  5830. else
  5831. [item setValue:@"true" forKey:@"is_closeout"];
  5832. if(cid==0)
  5833. [item setValue:@"false" forKey:@"cart_exists"];
  5834. else
  5835. [item setValue:@"true" forKey:@"cart_exists"];
  5836. if (more_color == 0) {
  5837. [item setObject:@(false) forKey:@"more_color"];
  5838. } else if (more_color == 1) {
  5839. [item setObject:@(true) forKey:@"more_color"];
  5840. }
  5841. [item addEntriesFromDictionary:imgjson];
  5842. // [item setValue:nsurl forKey:@"img"];
  5843. [item setValue:nsname forKey:@"fash_name"];
  5844. [item setValue:nsdescription forKey:@"description"];
  5845. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5846. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5847. i++;
  5848. }
  5849. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5850. [ret setObject:items forKey:@"items"];
  5851. sqlite3_finalize(statement);
  5852. }
  5853. [iSalesDB close_db:db];
  5854. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  5855. return ret;
  5856. }
  5857. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  5858. {
  5859. return [self itemsearch:params limited:YES];
  5860. }
  5861. #pragma mark order detail
  5862. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  5863. if (str1.length == 0) {
  5864. str1 = @"&nbsp";
  5865. }
  5866. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  5867. return str;
  5868. }
  5869. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  5870. {
  5871. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  5872. [fromformatter setDateFormat:from];
  5873. NSDate *date = [fromformatter dateFromString:datetime];
  5874. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  5875. [toformatter setDateFormat:to];
  5876. NSString * ret = [toformatter stringFromDate:date];
  5877. return ret;
  5878. }
  5879. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  5880. // 把毫秒去掉
  5881. if ([dateTime containsString:@"."]) {
  5882. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  5883. }
  5884. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  5885. formatter.dateFormat = formate;
  5886. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  5887. NSDate *date = [formatter dateFromString:dateTime];
  5888. formatter.dateFormat = newFormate;
  5889. NSString *result = [formatter stringFromDate:date];
  5890. return result ? result : @"";
  5891. }
  5892. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  5893. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  5894. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  5895. }
  5896. + (NSString *)countryCodeByCountryCodeId:(NSString *)countryCodeId db:(sqlite3 *)db {
  5897. NSString *sql = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@;",countryCodeId];
  5898. __block NSString *countryCode = @"";
  5899. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5900. countryCode = [self textAtColumn:0 statement:stmt];
  5901. }];
  5902. return countryCode;
  5903. }
  5904. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  5905. {
  5906. DebugLog(@"offline oderdetail params: %@",params);
  5907. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5908. int orderId = [params[@"orderId"] intValue];
  5909. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  5910. // decrypt card number and card security code
  5911. // 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 ];
  5912. // 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 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 _id=%d) o left join offline_contact c on o.customer_cid=c.contact_id",orderId];
  5913. 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.receive_address1,o.receive_address2,o.receive_city,o.receive_state,o.receive_zipcode,o.receive_country,o.sender_address1,o.sender_address2,o.sender_city,o.sender_state,o.sender_zipcode,o.sender_country,o.shipping_billto_address1,o.shipping_billto_address2,o.shipping_billto_city,o.shipping_billto_state,o.shipping_billto_zipcode,o.shipping_billto_country,o.billing_address1,o.billing_address2,o.billing_city,o.billing_state,o.billing_zipcode,o.billing_country,o.returnto_address1,o.returnto_address2,o.returnto_city,o.returnto_state,o.returnto_zipcode,o.returnto_country 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,receive_address1,receive_address2,receive_city,receive_state,receive_zipcode,receive_country,sender_address1,sender_address2,sender_city,sender_state,sender_zipcode,sender_country,shipping_billto_address1,shipping_billto_address2,shipping_billto_city,shipping_billto_state,shipping_billto_zipcode,shipping_billto_country,billing_address1,billing_address2,billing_city,billing_state,billing_zipcode,billing_country,returnto_address1,returnto_address2,returnto_city,returnto_state,returnto_zipcode,returnto_country from offline_order where _id=%d) o left join offline_contact c on o.customer_cid=c.contact_id",orderId];
  5914. sqlite3 *db = [iSalesDB get_db];
  5915. sqlite3_stmt * statement;
  5916. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  5917. NSString *nssoid = @"";
  5918. NSString* orderinfo = @"";
  5919. NSString *moreInfo = @"";
  5920. double handlingFee = 0;
  5921. double payments_and_credist = 0;
  5922. double totalPrice = 0;
  5923. double shippingFee = 0;
  5924. double lift_gate = 0;
  5925. NSString *customer_contact = @"";
  5926. NSString *customer_email = @"";
  5927. NSString *customer_fax = @"";
  5928. NSString *customer_phone = @"";
  5929. NSString *customer_city = @"";
  5930. NSString *customer_state = @"";
  5931. NSString *customer_zipcode = @"";
  5932. NSString *customer_country = @"";
  5933. __block NSMutableDictionary *section_0 = [NSMutableDictionary dictionary];
  5934. __block NSMutableDictionary *section_1 = [NSMutableDictionary dictionary];
  5935. __block NSMutableDictionary *section_2 = [NSMutableDictionary dictionary];
  5936. __block NSMutableDictionary *section_3 = [NSMutableDictionary dictionary];
  5937. __block NSMutableDictionary *section_4 = [NSMutableDictionary dictionary];
  5938. __block NSMutableDictionary *price_data = [NSMutableDictionary dictionary];
  5939. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  5940. {
  5941. if (sqlite3_step(statement) == SQLITE_ROW)
  5942. {
  5943. int order_id = sqlite3_column_int(statement, 0);
  5944. NSString *sign_url = [self textAtColumn:51 statement:statement];
  5945. // ret[@"sign_url"] = sign_url;
  5946. section_1 = @{
  5947. @"data":sign_url,
  5948. @"title":@"Signature",
  5949. @"type":@"sign_url"
  5950. }.mutableCopy;
  5951. [ret setObject:section_1 forKey:@"section_1"];
  5952. customer_contact = [self textAtColumn:52 statement:statement];
  5953. customer_email = [self textAtColumn:53 statement:statement];
  5954. customer_phone = [self textAtColumn:54 statement:statement];
  5955. customer_fax = [self textAtColumn:55 statement:statement];
  5956. customer_city = [self textAtColumn:60 statement:statement];
  5957. customer_state = [self textAtColumn:61 statement:statement];
  5958. customer_zipcode = [self textAtColumn:62 statement:statement];
  5959. customer_country = [self textAtColumn:63 statement:statement];
  5960. int offline_edit=sqlite3_column_int(statement, 56);
  5961. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  5962. char *soid = (char*)sqlite3_column_text(statement, 1);
  5963. if(soid==nil)
  5964. soid= "";
  5965. nssoid= [[NSString alloc]initWithUTF8String:soid];
  5966. // so#
  5967. ret[@"so#"] = nssoid;
  5968. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  5969. if(poNumber==nil)
  5970. poNumber= "";
  5971. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  5972. char *create_time = (char*)sqlite3_column_text(statement, 3);
  5973. if(create_time==nil)
  5974. create_time= "";
  5975. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  5976. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  5977. int status = sqlite3_column_int(statement, 4);
  5978. int erpStatus = sqlite3_column_int(statement, 49);
  5979. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  5980. // status
  5981. if (status > 1 && status != 3) {
  5982. status = erpStatus;
  5983. } else if (status == 3) {
  5984. status = 15;
  5985. }
  5986. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  5987. ret[@"order_status"] = nsstatus;
  5988. char *company_name = (char*)sqlite3_column_text(statement, 5);
  5989. if(company_name==nil)
  5990. company_name= "";
  5991. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  5992. // company name
  5993. ret[@"company_name"] = nscompany_name;
  5994. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  5995. if(customer_contact==nil)
  5996. customer_contact= "";
  5997. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  5998. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  5999. if(addr_1==nil)
  6000. addr_1="";
  6001. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  6002. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  6003. if(addr_2==nil)
  6004. addr_2="";
  6005. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  6006. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  6007. if(addr_3==nil)
  6008. addr_3="";
  6009. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  6010. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  6011. if(addr_4==nil)
  6012. addr_4="";
  6013. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  6014. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  6015. [arr_addr addObject:nsaddr_1];
  6016. [arr_addr addObject:nsaddr_2];
  6017. [arr_addr addObject:nsaddr_3];
  6018. [arr_addr addObject:nsaddr_4];
  6019. [arr_addr addObject:[NSString stringWithFormat:@"%@,",customer_city]];
  6020. [arr_addr addObject:customer_state];
  6021. [arr_addr addObject:customer_zipcode];
  6022. [arr_addr addObject:customer_country];
  6023. NSString * customer_address = [RAConvertor arr2string:arr_addr separator:@" " trim:true];
  6024. char *logist = (char*)sqlite3_column_text(statement, 11);
  6025. if(logist==nil)
  6026. logist= "";
  6027. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  6028. if (/*status == -11 || */status == 10 || status == 11) {
  6029. nslogist = [self textAtColumn:59 statement:statement];
  6030. };
  6031. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  6032. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  6033. shipping = @"Shipping To Be Quoted";
  6034. } else {
  6035. shippingFee = sqlite3_column_double(statement, 12);
  6036. }
  6037. // Shipping
  6038. // ret[@"Shipping"] = shipping;
  6039. NSDictionary *shipping_item = @{
  6040. @"title":@"Shipping",
  6041. @"value":shipping
  6042. };
  6043. [price_data setObject:shipping_item forKey:@"item_1"];
  6044. int have_lift_gate = sqlite3_column_int(statement, 17);
  6045. lift_gate = sqlite3_column_double(statement, 13);
  6046. // Liftgate Fee(No loading dock)
  6047. if (!have_lift_gate) {
  6048. lift_gate = 0;
  6049. }
  6050. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  6051. NSString *liftgate_value = [NSString stringWithFormat:@"$%.2f",lift_gate];
  6052. if (sqlite3_column_int(statement, 57)) {
  6053. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  6054. liftgate_value = @"Shipping To Be Quoted";
  6055. }
  6056. NSDictionary *liftgate_item = @{
  6057. @"title":@"Liftgate Fee(No loading dock)",
  6058. @"value":liftgate_value
  6059. };
  6060. [price_data setObject:liftgate_item forKey:@"item_2"];
  6061. // if ([nslogist isEqualToString:@"WILL CALL"]) {
  6062. // [ret removeObjectForKey:@"Shipping"];
  6063. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  6064. // }
  6065. //
  6066. // if (have_lift_gate) {
  6067. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  6068. // }
  6069. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  6070. if(general_notes==nil)
  6071. general_notes= "";
  6072. NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  6073. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  6074. if(internal_notes==nil)
  6075. internal_notes= "";
  6076. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  6077. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  6078. if(payment_type==nil)
  6079. payment_type= "";
  6080. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  6081. // order info
  6082. orderinfo = [self textFileName:@"order_info.html"];
  6083. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  6084. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  6085. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  6086. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  6087. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  6088. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  6089. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  6090. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  6091. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  6092. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  6093. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  6094. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  6095. NSString *payment = nil;
  6096. // id -> show
  6097. __block NSString *show_pay_type = nspayment_type;
  6098. [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) {
  6099. char *show_typ_ch = (char*)sqlite3_column_text(stmt, 0);
  6100. if (show_typ_ch != NULL) {
  6101. show_pay_type = [NSString stringWithUTF8String:show_typ_ch];
  6102. }
  6103. }];
  6104. if([nspayment_type isEqualToString:@"Credit Card"] || [nspayment_type isEqualToString:@"Visa/Master"])
  6105. {
  6106. payment = [self textFileName:@"creditcardpayment.html"];
  6107. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  6108. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  6109. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  6110. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  6111. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  6112. NSString *card_type = [self textAtColumn:42 statement:statement];
  6113. if (card_type.length > 0) { // 显示星号
  6114. card_type = @"****";
  6115. }
  6116. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  6117. if (card_number.length > 0 && card_number.length > 4) {
  6118. for (int i = 0; i < card_number.length - 4; i++) {
  6119. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  6120. }
  6121. } else {
  6122. card_number = @"";
  6123. }
  6124. NSString *card_security_code = [self textAtColumn:44 statement:statement];
  6125. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  6126. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  6127. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  6128. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  6129. card_expiration = @"****";
  6130. }
  6131. NSString *card_city = [self textAtColumn:46 statement:statement];
  6132. NSString *card_state = [self textAtColumn:47 statement:statement];
  6133. payment = [self replaceHtml:payment String:@"Payment_Type" withString:show_pay_type];
  6134. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  6135. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  6136. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  6137. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  6138. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  6139. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  6140. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  6141. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  6142. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  6143. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  6144. }
  6145. else
  6146. {
  6147. payment=[self textFileName:@"normalpayment.html"];
  6148. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  6149. }
  6150. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  6151. ret[@"result"]= [NSNumber numberWithInt:2];
  6152. // more info
  6153. moreInfo = [self textFileName:@"more_info.html"];
  6154. /*****ship to******/
  6155. // ShipToCompany_or_&nbsp
  6156. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  6157. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  6158. NSString *shipToName = [self textAtColumn:19 statement:statement];
  6159. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  6160. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  6161. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  6162. NSString *shipToAddr1 = [self textAtColumn:64 statement:statement];
  6163. NSString *shipToAddr2 = [self textAtColumn:65 statement:statement];
  6164. NSString *shipToCity = [self textAtColumn:66 statement:statement];
  6165. NSString *shipToStateCode = [self textAtColumn:67 statement:statement]; // stateCode
  6166. NSString *shipToZipCode = [self textAtColumn:68 statement:statement];
  6167. NSString *shipToCountryCodeId = [self textAtColumn:69 statement:statement];// countryCode_Id
  6168. if (!shipToAddr1.length && !shipToAddr2.length && !shipToCity.length && !shipToStateCode.length && !shipToZipCode.length && !shipToCountryCodeId.length) {
  6169. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_1_or_" withString:shipToAddr];
  6170. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_2_or_" withString:@""];
  6171. moreInfo = [self replaceHtml:moreInfo String:@"ShipTo_City_State_ZipCode_Country_or" withString:@""];
  6172. } else {
  6173. NSString *shipToCountryCode = [self countryCodeByCountryCodeId:shipToCountryCodeId db:db];
  6174. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_1_or_" withString:shipToAddr1];
  6175. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_2_or_" withString:shipToAddr2];
  6176. if (!shipToCity.length && !shipToStateCode.length && !shipToZipCode.length && !shipToCountryCode.length) {
  6177. moreInfo = [self replaceHtml:moreInfo String:@"ShipTo_City_State_ZipCode_Country_or" withString:@""];
  6178. } else {
  6179. moreInfo = [self replaceHtml:moreInfo String:@"ShipTo_City_State_ZipCode_Country_or" withString:[NSString stringWithFormat:@"%@, %@ %@ %@",shipToCity,shipToStateCode,shipToZipCode,shipToCountryCode]];
  6180. }
  6181. }
  6182. /*****ship from******/
  6183. // ShipFromCompany_or_&nbsp
  6184. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  6185. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  6186. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  6187. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  6188. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  6189. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  6190. NSString *shipFromAddr1 = [self textAtColumn:70 statement:statement];
  6191. NSString *shipFromAddr2 = [self textAtColumn:71 statement:statement];
  6192. NSString *shipFromCity = [self textAtColumn:72 statement:statement];
  6193. NSString *shipFromStateCode = [self textAtColumn:73 statement:statement]; // stateCode
  6194. NSString *shipFromZipCode = [self textAtColumn:74 statement:statement];
  6195. NSString *shipFromCountryCodeId = [self textAtColumn:75 statement:statement];// countryCode_Id
  6196. if (!shipFromAddr1.length && !shipFromAddr2.length && !shipFromCity.length && !shipFromStateCode.length && !shipFromZipCode.length && !shipFromCountryCodeId.length) {
  6197. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_1_or_" withString:shipFromAddr];
  6198. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_2_or_" withString:@""];
  6199. moreInfo = [self replaceHtml:moreInfo String:@"ShipFrom_City_State_ZipCode_Country_or" withString:@""];
  6200. } else {
  6201. NSString *shipFromCountryCode = [self countryCodeByCountryCodeId:shipFromCountryCodeId db:db];
  6202. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_1_or_" withString:shipFromAddr1];
  6203. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_2_or_" withString:shipFromAddr2];
  6204. if (!shipFromCity.length && !shipFromStateCode.length && !shipFromZipCode.length && !shipFromCountryCode.length) {
  6205. moreInfo = [self replaceHtml:moreInfo String:@"ShipFrom_City_State_ZipCode_Country_or" withString:@""];
  6206. } else {
  6207. moreInfo = [self replaceHtml:moreInfo String:@"ShipFrom_City_State_ZipCode_Country_or" withString:[NSString stringWithFormat:@"%@, %@ %@ %@",shipFromCity,shipFromStateCode,shipFromZipCode,shipFromCountryCode]];
  6208. }
  6209. }
  6210. /*****freight to******/
  6211. // FreightBillToCompany_or_&nbsp
  6212. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  6213. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  6214. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  6215. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  6216. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  6217. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  6218. NSString *FreightBillToAddr1 = [self textAtColumn:76 statement:statement];
  6219. NSString *FreightBillToAddr2 = [self textAtColumn:77 statement:statement];
  6220. NSString *FreightBillToCity = [self textAtColumn:78 statement:statement];
  6221. NSString *FreightBillToStateCode = [self textAtColumn:79 statement:statement]; // stateCode
  6222. NSString *FreightBillToZipCode = [self textAtColumn:80 statement:statement];
  6223. NSString *FreightBillToCountryCodeId = [self textAtColumn:81 statement:statement];// countryCode_Id
  6224. if (!FreightBillToAddr1.length && !FreightBillToAddr2.length && !FreightBillToCity.length && !FreightBillToStateCode.length && !FreightBillToZipCode.length && !FreightBillToCountryCodeId.length) {
  6225. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_1_or_" withString:freightBillToAddr];
  6226. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_2_or_" withString:@""];
  6227. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillTo_City_State_ZipCode_Country_or" withString:@""];
  6228. } else {
  6229. NSString *FreightBillToCountryCode = [self countryCodeByCountryCodeId:FreightBillToCountryCodeId db:db];
  6230. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_1_or_" withString:FreightBillToAddr1];
  6231. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_2_or_" withString:FreightBillToAddr2];
  6232. if (!FreightBillToCity.length && !FreightBillToStateCode.length && !FreightBillToZipCode.length && !FreightBillToCountryCode.length) {
  6233. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillTo_City_State_ZipCode_Country_or" withString:@""];
  6234. } else {
  6235. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillTo_City_State_ZipCode_Country_or" withString:[NSString stringWithFormat:@"%@, %@ %@ %@",FreightBillToCity,FreightBillToStateCode,FreightBillToZipCode,FreightBillToCountryCode]];
  6236. }
  6237. }
  6238. /*****merchandise to******/
  6239. // MerchandiseBillToCompany_or_&nbsp
  6240. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  6241. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  6242. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  6243. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  6244. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  6245. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  6246. NSString *MerchandiseBillToAddr1 = [self textAtColumn:82 statement:statement];
  6247. NSString *MerchandiseBillToAddr2 = [self textAtColumn:83 statement:statement];
  6248. NSString *MerchandiseBillToCity = [self textAtColumn:84 statement:statement];
  6249. NSString *MerchandiseBillToStateCode = [self textAtColumn:85 statement:statement]; // stateCode
  6250. NSString *MerchandiseBillToZipCode = [self textAtColumn:86 statement:statement];
  6251. NSString *MerchandiseBillToCountryCodeId = [self textAtColumn:87 statement:statement];// countryCode_Id
  6252. if (!MerchandiseBillToAddr1.length && !MerchandiseBillToAddr2.length && !MerchandiseBillToCity.length && !MerchandiseBillToStateCode.length && !MerchandiseBillToZipCode.length && !MerchandiseBillToCountryCodeId.length) {
  6253. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_1_or_" withString:merchandiseBillToAddr];
  6254. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_2_or_" withString:@""];
  6255. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillTo_City_State_ZipCode_Country_or" withString:@""];
  6256. } else {
  6257. NSString *MerchandiseBillToCountryCode = [self countryCodeByCountryCodeId:MerchandiseBillToCountryCodeId db:db];
  6258. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_1_or_" withString:MerchandiseBillToAddr1];
  6259. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_2_or_" withString:MerchandiseBillToAddr2];
  6260. if (!MerchandiseBillToCity.length && !MerchandiseBillToStateCode.length && !MerchandiseBillToZipCode.length && !MerchandiseBillToCountryCode.length) {
  6261. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillTo_City_State_ZipCode_Country_or" withString:@""];
  6262. } else {
  6263. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillTo_City_State_ZipCode_Country_or" withString:[NSString stringWithFormat:@"%@, %@ %@ %@",MerchandiseBillToCity,MerchandiseBillToStateCode,MerchandiseBillToZipCode,MerchandiseBillToCountryCode]];
  6264. }
  6265. }
  6266. /*****return to******/
  6267. // ReturnToCompany_or_&nbsp
  6268. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  6269. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  6270. NSString *returnToName = [self textAtColumn:31 statement:statement];
  6271. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  6272. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  6273. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  6274. NSString *ReturnToAddr1 = [self textAtColumn:88 statement:statement];
  6275. NSString *ReturnToAddr2 = [self textAtColumn:89 statement:statement];
  6276. NSString *ReturnToCity = [self textAtColumn:90 statement:statement];
  6277. NSString *ReturnToStateCode = [self textAtColumn:91 statement:statement]; // stateCode
  6278. NSString *ReturnToZipCode = [self textAtColumn:92 statement:statement];
  6279. NSString *ReturnToCountryCodeId = [self textAtColumn:93 statement:statement];// countryCode_Id
  6280. if (!ReturnToAddr1.length && !ReturnToAddr2.length && !ReturnToCity.length && !ReturnToStateCode.length && !ReturnToZipCode.length && !ReturnToCountryCodeId.length) {
  6281. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_1_or_" withString:returnToAddr];
  6282. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_2_or_" withString:@""];
  6283. moreInfo = [self replaceHtml:moreInfo String:@"ReturnTo_City_State_ZipCode_Country_or" withString:@""];
  6284. } else {
  6285. NSString *ReturnToCountryCode = [self countryCodeByCountryCodeId:ReturnToCountryCodeId db:db];
  6286. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_1_or_" withString:ReturnToAddr1];
  6287. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_2_or_" withString:ReturnToAddr2];
  6288. if (!ReturnToCity.length && !ReturnToStateCode.length && !ReturnToZipCode.length && !ReturnToCountryCode.length) {
  6289. moreInfo = [self replaceHtml:moreInfo String:@"ReturnTo_City_State_ZipCode_Country_or" withString:@""];
  6290. } else {
  6291. moreInfo = [self replaceHtml:moreInfo String:@"ReturnTo_City_State_ZipCode_Country_or" withString:[NSString stringWithFormat:@"%@, %@ %@ %@",ReturnToCity,ReturnToStateCode,ReturnToZipCode,ReturnToCountryCode]];
  6292. }
  6293. }
  6294. //
  6295. // DebugLog(@"more info : %@",moreInfo);
  6296. // handling fee
  6297. handlingFee = sqlite3_column_double(statement, 33);
  6298. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  6299. NSString *handling_fee_value = [NSString stringWithFormat:@"$%.2f",handlingFee];
  6300. if (sqlite3_column_int(statement, 58)) {
  6301. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  6302. handling_fee_value = @"Shipping To Be Quoted";
  6303. }
  6304. NSDictionary *handling_fee_item = @{
  6305. @"title":@"Handling Fee",
  6306. @"value":handling_fee_value
  6307. };
  6308. [price_data setObject:handling_fee_item forKey:@"item_3"];
  6309. //
  6310. // customer info
  6311. customerID = [self textAtColumn:36 statement:statement];
  6312. // mode
  6313. ret[@"mode"] = appDelegate.mode;
  6314. // model_count
  6315. ret[@"model_count"] = @(0);
  6316. }
  6317. sqlite3_finalize(statement);
  6318. }
  6319. [iSalesDB close_db:db];
  6320. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  6321. // "customer_email" = "Shui Hu";
  6322. // "customer_fax" = "";
  6323. // "customer_first_name" = F;
  6324. // "customer_last_name" = L;
  6325. // "customer_name" = ",da He Xiang Dong Liu A";
  6326. // "customer_phone" = "Hey Xuan Feng";
  6327. contactInfo[@"customer_phone"] = customer_phone;
  6328. contactInfo[@"customer_fax"] = customer_fax;
  6329. contactInfo[@"customer_email"] = customer_email;
  6330. NSString *first_name = @"";
  6331. NSString *last_name = @"";
  6332. if ([customer_contact isEqualToString:@""]) {
  6333. } else if ([customer_contact containsString:@" "]) {
  6334. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  6335. first_name = [customer_contact substringToIndex:first_space_index];
  6336. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  6337. }
  6338. contactInfo[@"customer_first_name"] = first_name;
  6339. contactInfo[@"customer_last_name"] = last_name;
  6340. ret[@"customerInfo"] = contactInfo;
  6341. // models
  6342. if (nssoid) {
  6343. __block NSMutableDictionary *model_data = [NSMutableDictionary dictionary];
  6344. __block double TotalCuft = 0;
  6345. __block double TotalWeight = 0;
  6346. __block int TotalCarton = 0;
  6347. __block double allItemPrice = 0;
  6348. 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];
  6349. sqlite3 *db1 = [iSalesDB get_db];
  6350. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6351. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  6352. int product_id = sqlite3_column_int(stmt, 0);
  6353. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  6354. int item_id = sqlite3_column_int(stmt, 7);
  6355. NSString* Price=nil;
  6356. if(str_price==nil)
  6357. {
  6358. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db1];
  6359. if(price==nil)
  6360. Price=@"No Price.";
  6361. else
  6362. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6363. }
  6364. else
  6365. {
  6366. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  6367. }
  6368. double discount = sqlite3_column_double(stmt, 2);
  6369. int item_count = sqlite3_column_int(stmt, 3);
  6370. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  6371. NSString *nsline_note=nil;
  6372. if(line_note!=nil)
  6373. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  6374. // char *name = (char*)sqlite3_column_text(stmt, 5);
  6375. // NSString *nsname = nil;
  6376. // if(name!=nil)
  6377. // nsname= [[NSString alloc]initWithUTF8String:name];
  6378. NSString *nsname = [self textAtColumn:5 statement:stmt];
  6379. // char *description = (char*)sqlite3_column_text(stmt, 6);
  6380. // NSString *nsdescription=nil;
  6381. // if(description!=nil)
  6382. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  6383. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  6384. // int stockUom = sqlite3_column_int(stmt, 8);
  6385. // int _id = sqlite3_column_int(stmt, 9);
  6386. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  6387. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  6388. double weight=[bsubtotaljson[@"weight"] doubleValue];
  6389. int carton=[bsubtotaljson[@"carton"] intValue];
  6390. TotalCuft += cuft;
  6391. TotalWeight += weight;
  6392. TotalCarton += carton;
  6393. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID];
  6394. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  6395. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  6396. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  6397. itemjson[@"note"]=nsline_note;
  6398. itemjson[@"origin_price"] = Price;
  6399. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  6400. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  6401. itemjson[@"order_item_status"] = @""; // 暂时不处理
  6402. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  6403. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  6404. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  6405. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6406. if(itemjson[@"combine"] != nil)
  6407. {
  6408. // int citem=0;
  6409. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  6410. for(int bc=0;bc<bcount;bc++)
  6411. {
  6412. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6413. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6414. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6415. subTotal += uprice * modulus * item_count;
  6416. }
  6417. }
  6418. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  6419. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  6420. // ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  6421. // ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  6422. itemjson[@"type"] = @"order_item";
  6423. model_data[[NSString stringWithFormat:@"item_%ld",(*count)++]] = itemjson;
  6424. model_data[@"count"] = [NSNumber numberWithInteger:*count];
  6425. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  6426. allItemPrice += subTotal;
  6427. }];
  6428. section_3[@"data"] = model_data;
  6429. section_3[@"type"] = @"sub_order";
  6430. section_3[@"title"] = @"Models";
  6431. section_3[@"switch"] = @(false);
  6432. ret[@"section_3"] = section_3;
  6433. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  6434. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  6435. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  6436. // payments/Credits
  6437. // payments_and_credist = sqlite3_column_double(statement, 34);
  6438. payments_and_credist = allItemPrice;
  6439. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6440. NSDictionary *sub_total_item = @{
  6441. @"title":@"Sub-Total",
  6442. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  6443. };
  6444. [price_data setObject:sub_total_item forKey:@"item_0"];
  6445. // // total
  6446. // totalPrice = sqlite3_column_double(statement, 35);
  6447. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6448. } else {
  6449. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  6450. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  6451. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  6452. // payments/Credits
  6453. payments_and_credist = 0;
  6454. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6455. NSDictionary *sub_total_item = @{
  6456. @"title":@"Sub-Total",
  6457. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  6458. };
  6459. [price_data setObject:sub_total_item forKey:@"item_0"];
  6460. }
  6461. // total
  6462. totalPrice = payments_and_credist + lift_gate + shippingFee + handlingFee;
  6463. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6464. NSDictionary *total_item = @{
  6465. @"title":@"Total",
  6466. @"value":[NSString stringWithFormat:@"$%.2f",totalPrice]
  6467. };
  6468. [price_data setObject:total_item forKey:@"item_4"];
  6469. [price_data setObject:@(price_data.allKeys.count) forKey:@"count"];
  6470. section_4 = @{
  6471. @"data":price_data,
  6472. @"title":@"Price Info",
  6473. @"type":@"price_info"
  6474. }.mutableCopy;
  6475. ret[@"section_4"] = section_4;
  6476. // ret[@"order_info"]= orderinfo;
  6477. section_0 = @{
  6478. @"data":orderinfo,
  6479. @"title":@"Order Info",
  6480. @"type":@"order_info"
  6481. }.mutableCopy;
  6482. ret[@"section_0"] = section_0;
  6483. // ret[@"more_order_info"] = moreInfo;
  6484. section_2 = @{
  6485. @"data":moreInfo,
  6486. @"title":@"More Info",
  6487. @"type":@"more_order_info"
  6488. }.mutableCopy;
  6489. ret[@"section_2"] = section_2;
  6490. ret[@"count"] = @(5);
  6491. return [RAConvertor dict2data:ret];
  6492. }
  6493. #pragma mark order list
  6494. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  6495. NSString *nsstatus = @"";
  6496. switch (status) {
  6497. case 0:
  6498. {
  6499. nsstatus=@"Temp Order";
  6500. break;
  6501. }
  6502. case 1:
  6503. {
  6504. nsstatus=@"Saved Order";
  6505. break;
  6506. }
  6507. case 2: {
  6508. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  6509. break;
  6510. }
  6511. case 3:
  6512. case 15:
  6513. {
  6514. nsstatus=@"Cancelled";
  6515. break;
  6516. }
  6517. case 10:
  6518. {
  6519. nsstatus=@"Quote Submitted";
  6520. break;
  6521. }
  6522. case 11:
  6523. {
  6524. nsstatus=@"Sales Order Submitted";
  6525. break;
  6526. }
  6527. case 12:
  6528. {
  6529. nsstatus=@"Processing";
  6530. break;
  6531. }
  6532. case 13:
  6533. {
  6534. nsstatus=@"Shipped";
  6535. break;
  6536. }
  6537. case 14:
  6538. {
  6539. nsstatus=@"Closed";
  6540. break;
  6541. }
  6542. case -11:
  6543. {
  6544. nsstatus = @"Ready For Submit";
  6545. break;
  6546. }
  6547. default:
  6548. break;
  6549. }
  6550. return nsstatus;
  6551. }
  6552. + (double) orderTotalPrice:(NSString *)so_id db:(sqlite3 *)db1 close:(BOOL)close{
  6553. double total = 0;
  6554. __block double payments_and_credist = 0;
  6555. __block double allItemPrice = 0;
  6556. // sqlite3 *db1 = [iSalesDB get_db];
  6557. if (so_id) {
  6558. // 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];
  6559. 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];
  6560. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6561. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6562. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  6563. int product_id = sqlite3_column_int(stmt, 0);
  6564. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  6565. int discount = sqlite3_column_int(stmt, 2);
  6566. int item_count = sqlite3_column_int(stmt, 3);
  6567. // int item_id = sqlite3_column_int(stmt, 7);
  6568. int item_id = sqlite3_column_int(stmt, 4);
  6569. char *customer_cid = (char *)sqlite3_column_text(stmt, 5);
  6570. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  6571. NSString* Price=nil;
  6572. if(str_price==nil)
  6573. {
  6574. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db1];
  6575. if(price==nil)
  6576. Price=@"No Price.";
  6577. else
  6578. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6579. }
  6580. else
  6581. {
  6582. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  6583. }
  6584. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID];
  6585. itemjson[@"The unit price"]=Price;
  6586. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  6587. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  6588. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6589. if(itemjson[@"combine"] != nil)
  6590. {
  6591. // int citem=0;
  6592. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  6593. for(int bc=0;bc<bcount;bc++)
  6594. {
  6595. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6596. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6597. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6598. subTotal += uprice * modulus * item_count;
  6599. }
  6600. }
  6601. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  6602. allItemPrice += subTotal;
  6603. }];
  6604. payments_and_credist = allItemPrice;
  6605. } else {
  6606. // payments/Credits
  6607. payments_and_credist = 0;
  6608. }
  6609. // lift_gate handlingFee shippingFee
  6610. __block double lift_gate = 0;
  6611. __block double handlingFee = 0;
  6612. __block double shippingFee = 0;
  6613. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  6614. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6615. int have_lift_gate = sqlite3_column_int(stmt, 0);
  6616. if (have_lift_gate) {
  6617. lift_gate = sqlite3_column_double(stmt, 1);
  6618. }
  6619. handlingFee = sqlite3_column_double(stmt, 2);
  6620. shippingFee = sqlite3_column_double(stmt, 3);
  6621. }];
  6622. // total
  6623. total = payments_and_credist + lift_gate + shippingFee + handlingFee;
  6624. if (close) {
  6625. [iSalesDB close_db:db1];
  6626. }
  6627. return total;
  6628. }
  6629. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  6630. {
  6631. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  6632. int limit = [[params valueForKey:@"limit"] intValue];
  6633. int offset = [[params valueForKey:@"offset"] intValue];
  6634. NSString* keyword = [params valueForKey:@"keyWord"];
  6635. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  6636. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  6637. NSString* where=@"1 = 1";
  6638. if(keyword.length>0)
  6639. 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]];
  6640. if (orderStatus.length > 0) {
  6641. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  6642. if (order_status_array.count == 1) {
  6643. int status_value = [[order_status_array firstObject] integerValue];
  6644. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  6645. if (status_value == 15 || status_value == 3) {
  6646. where = [where stringByAppendingString:[NSString stringWithFormat:@" and (o.status = 3 or o.erpOrderStatus = 15)"]];
  6647. } else {
  6648. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  6649. }
  6650. } else {
  6651. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  6652. }
  6653. } else if (order_status_array.count > 1) {
  6654. for (int i = 0; i < order_status_array.count;i++) {
  6655. NSString *status = order_status_array[i];
  6656. NSString *condition = @" or";
  6657. if (i == 0) {
  6658. condition = @" and (";
  6659. }
  6660. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  6661. int status_value = [status integerValue];
  6662. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  6663. if (status_value == 15 || status_value == 3) {
  6664. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ (o.status = 3 or o.erpOrderStatus = 15)",condition]];
  6665. } else {
  6666. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  6667. }
  6668. } else {
  6669. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  6670. }
  6671. }
  6672. where = [where stringByAppendingString:@" )"];
  6673. }
  6674. }
  6675. 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];
  6676. // DebugLog(@"order list sql: %@",sqlQuery);
  6677. sqlite3 *db = [iSalesDB get_db];
  6678. sqlite3_stmt * statement;
  6679. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  6680. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6681. {
  6682. int count=0;
  6683. while (sqlite3_step(statement) == SQLITE_ROW)
  6684. {
  6685. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  6686. int order_id = sqlite3_column_double(statement, 0);
  6687. char *soid = (char*)sqlite3_column_text(statement, 1);
  6688. if(soid==nil)
  6689. soid= "";
  6690. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  6691. int status = sqlite3_column_double(statement, 2);
  6692. int erpStatus = sqlite3_column_double(statement, 9);
  6693. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  6694. if(sales_rep==nil)
  6695. sales_rep= "";
  6696. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  6697. char *create_by = (char*)sqlite3_column_text(statement, 4);
  6698. if(create_by==nil)
  6699. create_by= "";
  6700. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  6701. char *company_name = (char*)sqlite3_column_text(statement, 5);
  6702. if(company_name==nil)
  6703. company_name= "";
  6704. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  6705. char *create_time = (char*)sqlite3_column_text(statement, 6);
  6706. if(create_time==nil)
  6707. create_time= "";
  6708. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  6709. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  6710. // double total_price = sqlite3_column_double(statement, 7);
  6711. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] db:db close:NO];
  6712. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  6713. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  6714. int offline_edit = sqlite3_column_int(statement, 10);
  6715. // ": "JH",
  6716. // "": "$8307.00",
  6717. // "": "MOB1608050001",
  6718. // "": "ArpithaT",
  6719. // "": "1st Stage Property Transformations",
  6720. // "": "JANICE SUTTON",
  6721. // "": 2255,
  6722. // "": "08/02/2016 09:49:18",
  6723. // "": 1,
  6724. // "": "Saved Order"
  6725. // "": "1470384050483",
  6726. // "model_count": "6 / 28"
  6727. item[@"sales_rep"]= nssales_rep;
  6728. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  6729. item[@"so#"]= nssoid;
  6730. item[@"create_by"]= nscreate_by;
  6731. item[@"customer_name"]= nscompany_name;
  6732. item[@"customer_contact"] = customer_contact;
  6733. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  6734. item[@"purchase_time"]= nscreate_time;
  6735. int statusCode = status;
  6736. if (statusCode == 2) {
  6737. statusCode = erpStatus;
  6738. } else if (statusCode == 3) {
  6739. statusCode = 15;
  6740. }
  6741. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",statusCode];
  6742. item[@"order_status"]= nsstatus;
  6743. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  6744. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  6745. // item[@"model_count"]
  6746. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  6747. count++;
  6748. }
  6749. ret[@"count"]= [NSNumber numberWithInt:count];
  6750. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  6751. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  6752. ret[@"result"]= [NSNumber numberWithInt:2];
  6753. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  6754. ret[@"time_zone"] = @"PST";
  6755. sqlite3_finalize(statement);
  6756. }
  6757. 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];
  6758. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  6759. [iSalesDB execSql:@"END TRANSACTION" db:db];
  6760. [iSalesDB close_db:db];
  6761. return [RAConvertor dict2data:ret];
  6762. }
  6763. #pragma mark update gnotes
  6764. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  6765. {
  6766. DebugLog(@"params: %@",params);
  6767. // comments = Meyoyoyoyoyoyoy;
  6768. // orderCode = MOB1608110001;
  6769. // password = 123456;
  6770. // user = EvanK;
  6771. 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]];
  6772. int ret = [iSalesDB execSql:sql];
  6773. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6774. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6775. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6776. // [dic setValue:@"160409" forKey:@"min_ver"];
  6777. return [RAConvertor dict2data:dic];
  6778. }
  6779. #pragma mark move to wishlist
  6780. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  6781. {
  6782. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  6783. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6784. _id = [NSString stringWithFormat:@"(%@)",_id];
  6785. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  6786. sqlite3 *db = [iSalesDB get_db];
  6787. __block NSString *product_id = @"";
  6788. __block NSString *item_count_str = @"";
  6789. // __block NSString *item_id = nil;
  6790. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6791. // product_id = [self textAtColumn:0 statement:stmt];
  6792. int item_count = sqlite3_column_int(stmt, 1);
  6793. // item_id = [self textAtColumn:2 statement:stmt];
  6794. NSString *p_id = [self textAtColumn:0 statement:stmt];
  6795. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  6796. if (p_id.length) {
  6797. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  6798. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  6799. }
  6800. }];
  6801. [iSalesDB close_db:db];
  6802. // 去除第一个,
  6803. if (product_id.length > 1) {
  6804. product_id = [product_id substringFromIndex:1];
  6805. }
  6806. if (item_count_str.length > 1) {
  6807. item_count_str = [item_count_str substringFromIndex:1];
  6808. }
  6809. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6810. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  6811. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  6812. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  6813. sqlite3 *db1 = [iSalesDB get_db];
  6814. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  6815. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6816. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  6817. // 删除
  6818. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  6819. int ret = [iSalesDB execSql:deleteSql db:db1];
  6820. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6821. [iSalesDB close_db:db1];
  6822. return [RAConvertor dict2data:dic];
  6823. }
  6824. #pragma mark cart delete
  6825. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  6826. {
  6827. // cartItemId = 548;
  6828. // orderCode = MOB1608110001;
  6829. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  6830. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6831. _id = [NSString stringWithFormat:@"(%@)",_id];
  6832. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  6833. int ret = [iSalesDB execSql:sql];
  6834. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6835. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6836. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6837. // [dic setValue:@"160409" forKey:@"min_ver"];
  6838. return [RAConvertor dict2data:dic];
  6839. }
  6840. #pragma mark set price
  6841. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  6842. {
  6843. DebugLog(@"cart set price params: %@",params);
  6844. // "cartitem_id" = 1;
  6845. // discount = "0.000000";
  6846. // "item_note" = "";
  6847. // price = "269.000000";
  6848. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6849. NSString *notes = [self valueInParams:params key:@"item_note"];
  6850. NSString *discount = [self valueInParams:params key:@"discount"];
  6851. NSString *price = [self valueInParams:params key:@"price"];
  6852. // bool badd_price_changed=false;
  6853. // sqlite3* db=[iSalesDB get_db];
  6854. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6855. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  6856. // NSRange range;
  6857. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  6858. //
  6859. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  6860. // badd_price_changed=true;
  6861. // [iSalesDB close_db:db];
  6862. //
  6863. // if(badd_price_changed)
  6864. // {
  6865. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  6866. // }
  6867. //
  6868. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  6869. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  6870. int ret = [iSalesDB execSql:sql];
  6871. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6872. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6873. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6874. // [dic setValue:@"160409" forKey:@"min_ver"];
  6875. return [RAConvertor dict2data:dic];
  6876. }
  6877. #pragma mark set line notes
  6878. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  6879. {
  6880. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6881. NSString *notes = [self valueInParams:params key:@"notes"];
  6882. notes = [self translateSingleQuote:notes];
  6883. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  6884. int ret = [iSalesDB execSql:sql];
  6885. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6886. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6887. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6888. // [dic setValue:@"160409" forKey:@"min_ver"];
  6889. return [RAConvertor dict2data:dic];
  6890. }
  6891. #pragma mark set qty
  6892. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  6893. {
  6894. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6895. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6896. int item_count = [params[@"inputInt"] intValue];
  6897. // 购买检查数量大于库存
  6898. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6899. if (!appDelegate.can_create_backorder) {
  6900. 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];
  6901. __block BOOL out_of_stock = NO;
  6902. [iSalesDB jk_query:checkSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6903. int availability = sqlite3_column_int(stmt, 0);
  6904. if (availability < item_count) {
  6905. out_of_stock = YES;
  6906. }
  6907. }];
  6908. if (out_of_stock) { // 缺货
  6909. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"result"];
  6910. [dic setObject:@"Item is out of stock" forKey:@"err_msg"];
  6911. return [RAConvertor dict2data:dic];
  6912. }
  6913. }
  6914. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  6915. int ret = [iSalesDB execSql:sql];
  6916. __block int item_id = 0;
  6917. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6918. item_id = sqlite3_column_int(stmt, 0);
  6919. }];
  6920. sqlite3 *db = [iSalesDB get_db];
  6921. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  6922. [iSalesDB close_db:db];
  6923. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6924. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6925. // [dic setValue:@"160409" forKey:@"min_ver"];
  6926. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  6927. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  6928. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  6929. return [RAConvertor dict2data:dic];
  6930. }
  6931. #pragma mark place order
  6932. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  6933. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6934. if (aname) {
  6935. [dic setValue:aname forKey:@"aname"];
  6936. }
  6937. if (control) {
  6938. [dic setValue:control forKey:@"control"];
  6939. }
  6940. if (keyboard) {
  6941. [dic setValue:keyboard forKey:@"keyboard"];
  6942. }
  6943. if (name) {
  6944. [dic setValue:name forKey:@"name"];
  6945. }
  6946. if (value) {
  6947. [dic setValue:value forKey:@"value"];
  6948. }
  6949. return dic;
  6950. }
  6951. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  6952. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6953. orderCode = [self translateSingleQuote:orderCode];
  6954. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  6955. 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];
  6956. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6957. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  6958. NSString *img0 = [self textAtColumn:1 statement:stmt];
  6959. NSString *img1 = [self textAtColumn:2 statement:stmt];
  6960. NSString *img2 = [self textAtColumn:3 statement:stmt];
  6961. NSString *company_name = [self textAtColumn:4 statement:stmt];
  6962. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  6963. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  6964. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  6965. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  6966. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  6967. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  6968. NSString *email = [self textAtColumn:11 statement:stmt];
  6969. NSString *phone = [self textAtColumn:12 statement:stmt];
  6970. NSString *fax = [self textAtColumn:13 statement:stmt];
  6971. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  6972. NSString *city = [self textAtColumn:15 statement:stmt];
  6973. NSString *state = [self textAtColumn:16 statement:stmt];
  6974. NSString *country = [self textAtColumn:17 statement:stmt];
  6975. NSString *name = [self textAtColumn:18 statement:stmt];
  6976. // contact id
  6977. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  6978. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  6979. [contact_id_dic setValue:@"text" forKey:@"control"];
  6980. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  6981. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  6982. [contact_id_dic setValue:@"true" forKey:@"required"];
  6983. [contact_id_dic setValue:contact_id forKey:@"value"];
  6984. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  6985. // business card
  6986. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  6987. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  6988. [business_card_dic setValue:@"img" forKey:@"control"];
  6989. [business_card_dic setValue:@"1" forKey:@"disable"];
  6990. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  6991. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  6992. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  6993. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  6994. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  6995. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  6996. [business_card_dic setValue:@"business_card" forKey:@"name"];
  6997. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  6998. // fax
  6999. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7000. [customer_dic setValue:fax_dic forKey:@"item_10"];
  7001. // zipcode
  7002. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  7003. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  7004. // city
  7005. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  7006. [customer_dic setValue:city_dic forKey:@"item_12"];
  7007. // state
  7008. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  7009. [customer_dic setValue:state_dic forKey:@"item_13"];
  7010. // country
  7011. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  7012. [customer_dic setValue:country_dic forKey:@"item_14"];
  7013. // company name
  7014. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  7015. [customer_dic setValue:company_dic forKey:@"item_2"];
  7016. // addr_1
  7017. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  7018. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  7019. // addr_2
  7020. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  7021. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  7022. // addr_3
  7023. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  7024. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  7025. // addr_4
  7026. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  7027. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  7028. // Contact
  7029. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  7030. [customer_dic setValue:contact_dic forKey:@"item_7"];
  7031. // email
  7032. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7033. [customer_dic setValue:email_dic forKey:@"item_8"];
  7034. // phone
  7035. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7036. [customer_dic setValue:phone_dic forKey:@"item_9"];
  7037. // title
  7038. [customer_dic setValue:@"Customer" forKey:@"title"];
  7039. // count
  7040. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  7041. }];
  7042. // setting
  7043. NSDictionary *setting = params[@"setting"];
  7044. NSNumber *hide = setting[@"CustomerHide"];
  7045. [customer_dic setValue:hide forKey:@"hide"];
  7046. return customer_dic;
  7047. }
  7048. + (void)dictionary:(NSMutableDictionary *)dictionary setValue:(id)value forKey:(id)key {
  7049. if (dictionary == nil || value == nil || key == nil) {
  7050. return;
  7051. }
  7052. [dictionary setValue:value forKey:key];
  7053. }
  7054. + (NSDictionary *)placeOrderAddressRefresh:(NSDictionary *)params {
  7055. NSString *countryCode = nil;
  7056. NSString *countryCode_id = nil;
  7057. NSString *stateCode = nil;
  7058. NSString *city = nil;
  7059. NSString *zipCode = nil;
  7060. NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  7061. NSString *refresh_trigger = params[@"refresh_trigger"];
  7062. NSString *trigger = @"zipcode";
  7063. if([[refresh_trigger lowercaseString] containsString:@"country"]) {
  7064. trigger = @"country";
  7065. } else if ([[refresh_trigger lowercaseString] containsString:@"zipcode"]) {
  7066. trigger = @"zipcode";
  7067. }
  7068. NSString *country_key = [refresh_trigger stringByReplacingOccurrencesOfString:trigger withString:@"country"];
  7069. NSString *state_key = [refresh_trigger stringByReplacingOccurrencesOfString:trigger withString:@"state"];
  7070. NSString *city_key = [refresh_trigger stringByReplacingOccurrencesOfString:trigger withString:@"city"];
  7071. NSString *countryCode_key = [refresh_trigger stringByReplacingOccurrencesOfString:trigger withString:@"countryCode"];
  7072. NSString *zipcode_key = [refresh_trigger stringByReplacingOccurrencesOfString:trigger withString:@"zipcode"];
  7073. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  7074. if([[refresh_trigger lowercaseString] containsString:@"country"]) { // choose country
  7075. NSString *code_id = params[refresh_trigger];
  7076. countryCode_id = code_id;
  7077. countryCode = [self countryCodeByid:code_id];
  7078. [self dictionary:ret setValue:countryCode_id forKey:refresh_trigger];
  7079. [self dictionary:ret setValue:countryCode forKey:countryCode_key];
  7080. // 清除zipcode
  7081. [self dictionary:ret setValue:@"" forKey:zipcode_key];
  7082. [self dictionary:ret setValue:@"" forKey:city_key];
  7083. } else if ([[refresh_trigger lowercaseString] containsString:@"zipcode"]) { // zipcode
  7084. NSString *zip_code = params[refresh_trigger];
  7085. // 剔除全部为空格
  7086. int spaceCount = 0;
  7087. for (int i = 0; i < zip_code.length; i++) {
  7088. if ([zip_code characterAtIndex:i] == ' ') {
  7089. spaceCount++;
  7090. }
  7091. }
  7092. if (spaceCount == zip_code.length) {
  7093. zip_code = @"";
  7094. }
  7095. zipCode = zip_code;
  7096. [self dictionary:ret setValue:zipCode forKey:refresh_trigger];
  7097. if (zipCode.length > 0) {
  7098. countryCode_id = params[country_key];
  7099. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  7100. countryCode = [dic valueForKey:@"country_code"];
  7101. countryCode_id = [self countryCodeIdByCode:countryCode];
  7102. if (!countryCode) {
  7103. // countryCode = @"US";
  7104. NSString *code_id = params[country_key];
  7105. countryCode_id = code_id;
  7106. countryCode = [self countryCodeByid:code_id];
  7107. }
  7108. [self dictionary:ret setValue:countryCode_id forKey:country_key];
  7109. [self dictionary:ret setValue:countryCode forKey:countryCode_key];
  7110. stateCode = [dic valueForKey:@"state_code"];
  7111. if (!stateCode.length) {
  7112. stateCode = params[state_key];
  7113. }
  7114. [self dictionary:ret setValue:stateCode forKey:state_key];
  7115. city = [dic valueForKey:@"city"];
  7116. if (!city.length) {
  7117. city = params[city_key];
  7118. }
  7119. [self dictionary:ret setValue:city forKey:city_key];
  7120. // zip code
  7121. } else {
  7122. NSString *code_id = params[country_key];
  7123. countryCode_id = code_id;
  7124. countryCode = [self countryCodeByid:code_id];
  7125. stateCode = params[state_key];
  7126. city = params[city_key];
  7127. [self dictionary:ret setValue:countryCode_id forKey:country_key];
  7128. [self dictionary:ret setValue:countryCode forKey:countryCode_key];
  7129. [self dictionary:ret setValue:stateCode forKey:state_key];
  7130. [self dictionary:ret setValue:city forKey:city_key];
  7131. }
  7132. }
  7133. } else {
  7134. // default: US United States
  7135. countryCode = @"US";
  7136. countryCode_id = @"228";
  7137. [self dictionary:ret setValue:countryCode_id forKey:country_key];
  7138. [self dictionary:ret setValue:countryCode forKey:countryCode_key];
  7139. // 清除zipcode
  7140. [self dictionary:ret setValue:@"" forKey:zipcode_key];
  7141. [self dictionary:ret setValue:@"" forKey:city_key];
  7142. }
  7143. return ret;
  7144. }
  7145. + (int)placeOrderFillAddressInDictionary:(NSMutableDictionary *)dic beginIndex:(int)index withParams:(NSDictionary *)params Prefix:(NSString *)prefix Addr1:(NSString *)addr_1 Addr2:(NSString *)addr_2 City:(NSString *)city State:(NSString *)state ZipCode:(NSString *)zipcode Country:(NSString *)country DB:(sqlite3 *)db {
  7146. NSString *refresh_trigger = params[@"refresh_trigger"];
  7147. BOOL isRefresh = refresh_trigger.length > 0 ? YES : NO;
  7148. NSString *key = prefix;
  7149. key = [NSString stringWithFormat:@"%@_address1",prefix];
  7150. NSDictionary *addr1_dic = [self infoName:key aname:@"Address1" control:@"edit" keyboard:@"text" value:addr_1];
  7151. [dic setValue:addr1_dic forKey:[NSString stringWithFormat:@"item_%d",index++]];
  7152. key = [NSString stringWithFormat:@"%@_address2",prefix];
  7153. NSDictionary *addr2_dic = [self infoName:key aname:@"Address2" control:@"edit" keyboard:@"text" value:addr_2];
  7154. [dic setValue:addr2_dic forKey:[NSString stringWithFormat:@"item_%d",index++]];
  7155. // city
  7156. key = [NSString stringWithFormat:@"%@_city",prefix];
  7157. NSString *param_city = [params objectForKey:key];
  7158. if (param_city.length > 0 || isRefresh) {
  7159. city = param_city;
  7160. }
  7161. NSDictionary *city_dic = [self infoName:key aname:@"City" control:@"edit" keyboard:@"text" value:city];
  7162. [dic setValue:city_dic forKey:[NSString stringWithFormat:@"item_%d",index++]];
  7163. // state
  7164. key = [NSString stringWithFormat:@"%@_state",prefix];
  7165. NSString *param_state = [params objectForKey:key];
  7166. if (param_state.length > 0 || isRefresh) {
  7167. state = param_state;
  7168. }
  7169. key = [NSString stringWithFormat:@"%@_countryCode",prefix];
  7170. NSString *countryCode = [params objectForKey:key];
  7171. if (!countryCode.length) {
  7172. countryCode = @"US";
  7173. }
  7174. NSDictionary *state_dic = @{
  7175. @"aname" : @"State",
  7176. @"cadedate" : [self offline_getStateByCountryCode:countryCode checkedState:state db:db],
  7177. @"control" : @"enum",
  7178. @"enum" : @"true",
  7179. @"name" : [NSString stringWithFormat:@"%@_state",prefix],
  7180. @"single_select" : @"true"
  7181. };
  7182. [dic setValue:state_dic forKey:[NSString stringWithFormat:@"item_%d",index++]];
  7183. // zipcode
  7184. key = [NSString stringWithFormat:@"%@_zipcode",prefix];
  7185. NSString *param_zipcode = [params objectForKey:key];
  7186. if (param_zipcode.length > 0 || isRefresh) {
  7187. zipcode = param_zipcode;
  7188. }
  7189. NSMutableDictionary *zipcode_dic = [[self infoName:key aname:@"Zip code" control:@"edit" keyboard:@"text" value:zipcode] mutableCopy];
  7190. [zipcode_dic setValue:@(1) forKey:@"refresh"];
  7191. NSDictionary *restore = @{
  7192. @"count" : @(3),
  7193. @"item_0" : [NSString stringWithFormat:@"%@_state",prefix],
  7194. @"item_1" : [NSString stringWithFormat:@"%@_city",prefix],
  7195. @"item_2" : [NSString stringWithFormat:@"%@_country",prefix]
  7196. };
  7197. [zipcode_dic setValue:restore forKey:@"restore"];
  7198. [dic setValue:zipcode_dic forKey:[NSString stringWithFormat:@"item_%d",index++]];
  7199. // country
  7200. key = [NSString stringWithFormat:@"%@_country",prefix];
  7201. NSString *param_country = [params objectForKey:key];
  7202. if (param_country.length > 0 || isRefresh) {
  7203. country = param_country;
  7204. }
  7205. NSDictionary *country_dic = @{
  7206. @"aname" : @"Country",
  7207. @"cadedate" : [self offline_getAllCountryDefaultId:country db:db],
  7208. @"control" : @"enum",
  7209. @"name" : [NSString stringWithFormat:@"%@_country",prefix],
  7210. @"refresh" : @(1),
  7211. @"restore" : @{
  7212. @"count" : @(3),
  7213. @"item_0" : [NSString stringWithFormat:@"%@_state",prefix],
  7214. @"item_1" : [NSString stringWithFormat:@"%@_city",prefix],
  7215. @"item_2" : [NSString stringWithFormat:@"%@_zipcode",prefix]
  7216. },
  7217. @"single_select" : @"true"
  7218. };
  7219. [dic setValue:country_dic forKey:[NSString stringWithFormat:@"item_%d",index++]];
  7220. return index;
  7221. }
  7222. + (int) placeOrderFillCompanyInfoInDictionnary:(NSMutableDictionary *)dic withInfo:(NSDictionary *)info params:(NSDictionary *)params beginIndex:(int)index prefix:(NSString *)prefix DB:(sqlite3 *)db {
  7223. NSString *cid = [info objectForKey:@"cid"];
  7224. NSString *name = [info objectForKey:@"name"];
  7225. NSString *ext = [info objectForKey:@"ext"];
  7226. NSString *contact = [info objectForKey:@"contact"];
  7227. NSString *email = [info objectForKey:@"email"];
  7228. NSString *fax = [info objectForKey:@"fax"];
  7229. NSString *phone = [info objectForKey:@"phone"];
  7230. NSString *addr_1 = [info objectForKey:@"addr_1"];
  7231. NSString *addr_2 = [info objectForKey:@"addr_2"];
  7232. NSString *state = [info objectForKey:@"state"];
  7233. NSString *zipcode = [info objectForKey:@"zipcode"];
  7234. NSString *city = [info objectForKey:@"city"];
  7235. NSString *country = [info objectForKey:@"country"];
  7236. // contact id
  7237. NSMutableDictionary *contact_id_dic = [[self infoName:[NSString stringWithFormat:@"%@_cid",prefix] aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7238. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7239. [contact_id_dic setValue:@"true" forKey:@"required"];
  7240. [dic setValue:contact_id_dic forKey:[NSString stringWithFormat:@"item_%d",index++]];
  7241. // company name
  7242. NSDictionary *company_name_dic = [self infoName:[NSString stringWithFormat:@"%@_name",prefix] aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7243. [dic setValue:company_name_dic forKey:[NSString stringWithFormat:@"item_%d",index++]];
  7244. // address
  7245. // NSMutableDictionary *ext_dic = [self infoName:[NSString stringWithFormat:@"%@_ext",prefix] aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7246. // NSNumber *required = setting[@"ShippingToAddressRequire"];
  7247. // if ([required integerValue]) {
  7248. // [ext_dic setValue:@"true" forKey:@"required"];
  7249. // }
  7250. //
  7251. // [dic setValue:ext_dic forKey:@"item_3"];
  7252. index = [self placeOrderFillAddressInDictionary:dic beginIndex:index withParams:params Prefix:prefix Addr1:addr_1 Addr2:addr_2 City:city State:state ZipCode:zipcode Country:country DB:db];
  7253. // contact
  7254. NSDictionary *contact_dic = [self infoName:[NSString stringWithFormat:@"%@_contact",prefix] aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7255. [dic setValue:contact_dic forKey:[NSString stringWithFormat:@"item_%d",index++]];
  7256. // phone
  7257. NSDictionary *phone_dic = [self infoName:[NSString stringWithFormat:@"%@_phone",prefix] aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7258. [dic setValue:phone_dic forKey:[NSString stringWithFormat:@"item_%d",index++]];
  7259. // fax
  7260. NSDictionary *fax_dic = [self infoName:[NSString stringWithFormat:@"%@_fax",prefix] aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7261. [dic setValue:fax_dic forKey:[NSString stringWithFormat:@"item_%d",index++]];
  7262. // email
  7263. NSDictionary *email_dic = [self infoName:[NSString stringWithFormat:@"%@_email",prefix] aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7264. [dic setValue:email_dic forKey:[NSString stringWithFormat:@"item_%d",index++]];
  7265. return index;
  7266. }
  7267. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7268. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7269. // setting
  7270. NSDictionary *setting = params[@"setting"];
  7271. NSNumber *hide = setting[@"ShipToHide"];
  7272. [dic setValue:hide forKey:@"hide"];
  7273. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7274. orderCode = [self translateSingleQuote:orderCode];
  7275. // 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];
  7276. NSString *sql = [NSString stringWithFormat:@"select receive_cid,receive_name,receive_ext,receive_contact,receive_email,receive_fax,receive_phone,receive_address1,receive_address2,receive_state,receive_zipcode,receive_city,receive_country from offline_order where so_id = '%@';",orderCode];
  7277. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7278. NSString *cid = [self textAtColumn:0 statement:stmt];
  7279. NSString *name = [self textAtColumn:1 statement:stmt];
  7280. NSString *ext = [self textAtColumn:2 statement:stmt];
  7281. NSString *contact = [self textAtColumn:3 statement:stmt];
  7282. NSString *email = [self textAtColumn:4 statement:stmt];
  7283. NSString *fax = [self textAtColumn:5 statement:stmt];
  7284. NSString *phone = [self textAtColumn:6 statement:stmt];
  7285. NSString *addr_1 = [self textAtColumn:7 statement:stmt];
  7286. NSString *addr_2 = [self textAtColumn:8 statement:stmt];
  7287. NSString *state = [self textAtColumn:9 statement:stmt];
  7288. NSString *zipcode = [self textAtColumn:10 statement:stmt];
  7289. NSString *city = [self textAtColumn:11 statement:stmt];
  7290. NSString *country = [self textAtColumn:12 statement:stmt];
  7291. NSDictionary *info = @{
  7292. @"cid" : cid,
  7293. @"name" : name,
  7294. @"ext" : ext,
  7295. @"contact" : contact,
  7296. @"email" : email,
  7297. @"fax" : fax,
  7298. @"phone" : phone,
  7299. @"addr_1" : addr_1,
  7300. @"addr_2" : addr_2,
  7301. @"state" : state,
  7302. @"zipcode" : zipcode,
  7303. @"city" : city,
  7304. @"country" : country
  7305. };
  7306. // title
  7307. [dic setValue:@"Ship To" forKey:@"title"];
  7308. int index = 0;
  7309. // choose
  7310. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7311. [choose_dic setValue:@"choose" forKey:@"aname"];
  7312. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7313. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  7314. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7315. @"key_map" : @{@"receive_cid" : @"customer_cid",
  7316. @"receive_contact" : @"customer_contact",
  7317. @"receive_email" : @"customer_email",
  7318. @"receive_address1" : @"customer_address1",
  7319. @"receive_address2" : @"customer_address2",
  7320. @"receive_zipcode" : @"customer_zipcode",
  7321. @"receive_city" : @"customer_city",
  7322. @"receive_state" : @"customer_state",
  7323. @"receive_country" : @"customer_country",
  7324. @"receive_fax" : @"customer_fax",
  7325. @"receive_name" : @"customer_name",
  7326. @"receive_phone" : @"customer_phone"},
  7327. @"refresh" : [NSNumber numberWithInteger:1],
  7328. @"type" : @"pull"};
  7329. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  7330. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  7331. @"name" : @"Add new address",
  7332. @"refresh" : [NSNumber numberWithInteger:1],
  7333. @"value" : @"new_addr"
  7334. };
  7335. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  7336. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  7337. @"key_map" : @{@"receive_cid" : @"customer_cid",
  7338. @"receive_contact" : @"customer_contact",
  7339. @"receive_email" : @"customer_email",
  7340. @"receive_address1" : @"customer_address1",
  7341. @"receive_address2" : @"customer_address2",
  7342. @"receive_zipcode" : @"customer_zipcode",
  7343. @"receive_city" : @"customer_city",
  7344. @"receive_state" : @"customer_state",
  7345. @"receive_country" : @"customer_country",
  7346. @"receive_fax" : @"customer_fax",
  7347. @"receive_name" : @"customer_name",
  7348. @"receive_phone" : @"customer_phone"},
  7349. @"name" : @"select_ship_to",
  7350. @"refresh" : [NSNumber numberWithInteger:1],
  7351. @"value" : @"Sales_Order_Ship_To"};
  7352. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  7353. [dic setValue:choose_dic forKey:[NSString stringWithFormat:@"item_%d",index++]];
  7354. // // contact id
  7355. // NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7356. // [contact_id_dic setValue:@"1" forKey:@"disable"];
  7357. // [contact_id_dic setValue:@"true" forKey:@"required"];
  7358. //
  7359. // [dic setValue:contact_id_dic forKey:[NSString stringWithFormat:@"item_%d",index++]];
  7360. //
  7361. // // company name
  7362. // NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7363. //
  7364. // [dic setValue:company_name_dic forKey:[NSString stringWithFormat:@"item_%d",index++]];
  7365. //
  7366. // // address
  7367. //// NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7368. //// NSNumber *required = setting[@"ShippingToAddressRequire"];
  7369. //// if ([required integerValue]) {
  7370. //// [ext_dic setValue:@"true" forKey:@"required"];
  7371. //// }
  7372. ////
  7373. //// [dic setValue:ext_dic forKey:@"item_3"];
  7374. //
  7375. // index = [self placeOrderFillAddressInDictionary:dic beginIndex:index withParams:params Prefix:@"receive" Addr1:addr_1 Addr2:addr_2 City:city State:state ZipCode:zipcode Country:country DB:db];
  7376. //
  7377. //
  7378. // // contact
  7379. // NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7380. //
  7381. // [dic setValue:contact_dic forKey:[NSString stringWithFormat:@"item_%d",index++]];
  7382. //
  7383. // // phone
  7384. // NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7385. //
  7386. // [dic setValue:phone_dic forKey:[NSString stringWithFormat:@"item_%d",index++]];
  7387. //
  7388. // // fax
  7389. // NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7390. // [dic setValue:fax_dic forKey:[NSString stringWithFormat:@"item_%d",index++]];
  7391. //
  7392. // // email
  7393. // NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7394. //
  7395. // [dic setValue:email_dic forKey:[NSString stringWithFormat:@"item_%d",index++]];
  7396. index = [self placeOrderFillCompanyInfoInDictionnary:dic withInfo:info params:params beginIndex:index prefix:@"receive" DB:db];
  7397. // count
  7398. [dic setValue:[NSNumber numberWithInteger:index] forKey:@"count"];
  7399. }];
  7400. return dic;
  7401. }
  7402. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  7403. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7404. // setting
  7405. NSDictionary *setting = params[@"setting"];
  7406. NSNumber *hide = setting[@"ShipFromHide"];
  7407. [dic setValue:hide forKey:@"hide"];
  7408. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7409. orderCode = [self translateSingleQuote:orderCode];
  7410. NSString *sql = [NSString stringWithFormat:@"select sender_cid,sender_name,sender_ext,sender_contact,sender_email,sender_fax,sender_phone,sender_address1,sender_address2,sender_state,sender_zipcode,sender_city,sender_country from offline_order where so_id = '%@';",orderCode];
  7411. NSString *ship_from_default_sql = @"select contact_id,decrypt(company_name),decrypt(addr_1),first_name || ' ' || last_name,email,fax,phone,addr_2,state,zipcode,city,country from offline_contact where contact_id = 'NPD';";
  7412. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7413. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  7414. __block NSString *name = [self textAtColumn:1 statement:stmt];
  7415. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  7416. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  7417. __block NSString *email = [self textAtColumn:4 statement:stmt];
  7418. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  7419. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  7420. __block NSString *addr_1 = [self textAtColumn:7 statement:stmt];
  7421. __block NSString *addr_2 = [self textAtColumn:8 statement:stmt];
  7422. __block NSString *state = [self textAtColumn:9 statement:stmt];
  7423. __block NSString *zipcode = [self textAtColumn:10 statement:stmt];
  7424. __block NSString *city = [self textAtColumn:11 statement:stmt];
  7425. __block NSString *country = [self textAtColumn:12 statement:stmt];
  7426. if (!cid.length) {
  7427. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  7428. cid = [self textAtColumn:0 statement:statment];
  7429. name = [self textAtColumn:1 statement:statment];
  7430. ext = [self textAtColumn:2 statement:statment];
  7431. contact = [self textAtColumn:3 statement:statment];
  7432. email = [self textAtColumn:4 statement:statment];
  7433. fax = [self textAtColumn:5 statement:statment];
  7434. phone = [self textAtColumn:6 statement:statment];
  7435. addr_1 = ext;
  7436. addr_2 = [self textAtColumn:7 statement:statment];
  7437. state = [self textAtColumn:8 statement:statment];
  7438. zipcode = [self textAtColumn:9 statement:statment];
  7439. city = [self textAtColumn:10 statement:statment];
  7440. country = [self textAtColumn:11 statement:statment];
  7441. }];
  7442. }
  7443. NSDictionary *info = @{
  7444. @"cid" : cid,
  7445. @"name" : name,
  7446. @"ext" : ext,
  7447. @"contact" : contact,
  7448. @"email" : email,
  7449. @"fax" : fax,
  7450. @"phone" : phone,
  7451. @"addr_1" : addr_1,
  7452. @"addr_2" : addr_2,
  7453. @"state" : state,
  7454. @"zipcode" : zipcode,
  7455. @"city" : city,
  7456. @"country" : country
  7457. };
  7458. // // count
  7459. // [dic setValue:[NSNumber numberWithInteger:13] forKey:@"count"];
  7460. // title
  7461. [dic setValue:@"Ship From" forKey:@"title"];
  7462. // hide
  7463. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7464. int index = 0;
  7465. // choose
  7466. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7467. [choose_dic setValue:@"choose" forKey:@"aname"];
  7468. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7469. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7470. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  7471. @"key_map" : @{@"sender_cid" : @"customer_cid",
  7472. @"sender_contact" : @"customer_contact",
  7473. @"sender_email" : @"customer_email",
  7474. @"sender_address1" : @"customer_address1",
  7475. @"sender_address2" : @"customer_address2",
  7476. @"sender_zipcode" : @"customer_zipcode",
  7477. @"sender_city" : @"customer_city",
  7478. @"sender_state" : @"customer_state",
  7479. @"sender_country" : @"customer_country",
  7480. @"sender_fax" : @"customer_fax",
  7481. @"sender_name" : @"customer_name",
  7482. @"sender_phone" : @"customer_phone"},
  7483. @"name" : @"select_cid",
  7484. @"refresh" : [NSNumber numberWithInteger:0],
  7485. @"value" : @"Sales_Order_Ship_From"};
  7486. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  7487. [dic setValue:choose_dic forKey:[NSString stringWithFormat:@"item_%d",index++]];
  7488. index = [self placeOrderFillCompanyInfoInDictionnary:dic withInfo:info params:params beginIndex:index prefix:@"sender" DB:db];
  7489. // count
  7490. [dic setValue:[NSNumber numberWithInteger:index] forKey:@"count"];
  7491. // // contact id
  7492. // NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7493. // [contact_id_dic setValue:@"1" forKey:@"disable"];
  7494. // [contact_id_dic setValue:@"true" forKey:@"required"];
  7495. //
  7496. // [dic setValue:contact_id_dic forKey:@"item_1"];
  7497. //
  7498. // // company name
  7499. // NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7500. //
  7501. // [dic setValue:company_name_dic forKey:@"item_2"];
  7502. //
  7503. // // address
  7504. //// NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  7505. ////
  7506. //// [dic setValue:ext_dic forKey:@"item_3"];
  7507. //
  7508. // // new address
  7509. //
  7510. // NSDictionary *addr1_dic = [self infoName:@"sender_address1" aname:@"Address1" control:@"edit" keyboard:@"text" value:addr_1];
  7511. //
  7512. // [dic setValue:addr1_dic forKey:@"item_3"];
  7513. //
  7514. // NSDictionary *addr2_dic = [self infoName:@"sender_address2" aname:@"Address2" control:@"edit" keyboard:@"text" value:addr_2];
  7515. //
  7516. // [dic setValue:addr2_dic forKey:@"item_4"];
  7517. //
  7518. //
  7519. // // city
  7520. // NSString *param_city = [params objectForKey:@"sender_city"];
  7521. // if (param_city.length > 0) {
  7522. // city = param_city;
  7523. // }
  7524. // NSDictionary *city_dic = [self infoName:@"sender_city" aname:@"City" control:@"edit" keyboard:@"text" value:city];
  7525. //
  7526. // [dic setValue:city_dic forKey:@"item_5"];
  7527. //
  7528. // // state
  7529. // NSString *param_state = [params objectForKey:@"sender_state"];
  7530. // if (param_state.length > 0) {
  7531. // state = param_state;
  7532. // }
  7533. // NSDictionary *state_dic = @{
  7534. // @"aname" : @"State",
  7535. // @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  7536. // @"control" : @"enum",
  7537. // @"enum" : @"true",
  7538. // @"name" : @"sender_state",
  7539. // @"required" : @"true",
  7540. // @"single_select" : @"true"
  7541. // };
  7542. // // NSDictionary *state_dic = [self infoName:@"sender_state" aname:@"State" control:@"edit" keyboard:@"text" value:state];
  7543. //
  7544. // [dic setValue:state_dic forKey:@"item_6"];
  7545. //
  7546. // // zipcode
  7547. // NSString *param_zipcode = [params objectForKey:@"sender_zipcode"];
  7548. // if (param_zipcode.length > 0) {
  7549. // zipcode = param_zipcode;
  7550. // }
  7551. // NSMutableDictionary *zipcode_dic = [[self infoName:@"sender_zipcode" aname:@"Zip code" control:@"edit" keyboard:@"text" value:zipcode] mutableCopy];
  7552. // [zipcode_dic setValue:@(1) forKey:@"refresh"];
  7553. // NSDictionary *restore = @{
  7554. // @"count" : @(3),
  7555. // @"item_0" : @"sender_state",
  7556. // @"item_1" : @"sender_city",
  7557. // @"item_2" : @"sender_country"
  7558. // };
  7559. // [zipcode_dic setValue:restore forKey:@"restore"];
  7560. //
  7561. // [dic setValue:zipcode_dic forKey:@"item_7"];
  7562. //
  7563. // // country
  7564. // NSString *param_country = [params objectForKey:@"sender_country"];
  7565. // if (param_country.length > 0) {
  7566. // country = param_country;
  7567. // }
  7568. // NSDictionary *country_dic = @{
  7569. // @"aname" : @"Country",
  7570. // @"cadedate" : [self offline_getAllCountryDefaultId:country db:db],
  7571. // @"control" : @"enum",
  7572. // @"name" : @"sender_country",
  7573. // @"refresh" : @(1),
  7574. // @"restore" : @{
  7575. // @"count" : @(3),
  7576. // @"item_0" : @"sender_state",
  7577. // @"item_1" : @"sender_city",
  7578. // @"item_2" : @"sender_zipcode"
  7579. // },
  7580. // @"single_select" : @"true"
  7581. // };
  7582. //
  7583. // // NSDictionary *country_dic = [self infoName:@"sender_country" aname:@"Country" control:@"edit" keyboard:@"text" value:country];
  7584. //
  7585. // [dic setValue:country_dic forKey:@"item_8"];
  7586. //
  7587. //
  7588. // // contact
  7589. // NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7590. //
  7591. // [dic setValue:contact_dic forKey:@"item_9"];
  7592. //
  7593. // // phone
  7594. // NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7595. //
  7596. // [dic setValue:phone_dic forKey:@"item_10"];
  7597. //
  7598. // // fax
  7599. // NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7600. // [dic setValue:fax_dic forKey:@"item_11"];
  7601. //
  7602. // // email
  7603. // NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7604. //
  7605. // [dic setValue:email_dic forKey:@"item_12"];
  7606. }];
  7607. return dic;
  7608. }
  7609. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  7610. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7611. // setting
  7612. NSDictionary *setting = params[@"setting"];
  7613. NSNumber *hide = setting[@"FreightBillToHide"];
  7614. [dic setValue:hide forKey:@"hide"];
  7615. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7616. orderCode = [self translateSingleQuote:orderCode];
  7617. 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,shipping_billto_address1,shipping_billto_address2,shipping_billto_state,shipping_billto_zipcode,shipping_billto_city,shipping_billto_country from offline_order where so_id = '%@';",orderCode];
  7618. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7619. NSString *cid = [self textAtColumn:0 statement:stmt];
  7620. NSString *name = [self textAtColumn:1 statement:stmt];
  7621. NSString *ext = [self textAtColumn:2 statement:stmt];
  7622. NSString *contact = [self textAtColumn:3 statement:stmt];
  7623. NSString *email = [self textAtColumn:4 statement:stmt];
  7624. NSString *fax = [self textAtColumn:5 statement:stmt];
  7625. NSString *phone = [self textAtColumn:6 statement:stmt];
  7626. NSString *addr_1 = [self textAtColumn:7 statement:stmt];
  7627. NSString *addr_2 = [self textAtColumn:8 statement:stmt];
  7628. NSString *state = [self textAtColumn:9 statement:stmt];
  7629. NSString *zipcode = [self textAtColumn:10 statement:stmt];
  7630. NSString *city = [self textAtColumn:11 statement:stmt];
  7631. NSString *country = [self textAtColumn:12 statement:stmt];
  7632. NSDictionary *info = @{
  7633. @"cid" : cid,
  7634. @"name" : name,
  7635. @"ext" : ext,
  7636. @"contact" : contact,
  7637. @"email" : email,
  7638. @"fax" : fax,
  7639. @"phone" : phone,
  7640. @"addr_1" : addr_1,
  7641. @"addr_2" : addr_2,
  7642. @"state" : state,
  7643. @"zipcode" : zipcode,
  7644. @"city" : city,
  7645. @"country" : country
  7646. };
  7647. // // count
  7648. // [dic setValue:[NSNumber numberWithInteger:13] forKey:@"count"];
  7649. // title
  7650. [dic setValue:@"Freight Bill To" forKey:@"title"];
  7651. // hide
  7652. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7653. int index = 0;
  7654. // choose
  7655. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7656. [choose_dic setValue:@"choose" forKey:@"aname"];
  7657. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7658. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  7659. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  7660. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  7661. @"shipping_billto_contact" : @"receive_contact",
  7662. @"shipping_billto_email" : @"receive_email",
  7663. @"shipping_billto_address1" : @"receive_address1",
  7664. @"shipping_billto_address2" : @"receive_address2",
  7665. @"shipping_billto_zipcode" : @"receive_zipcode",
  7666. @"shipping_billto_city" : @"receive_city",
  7667. @"shipping_billto_state" : @"receive_state",
  7668. @"shipping_billto_country" : @"receive_country",
  7669. @"shipping_billto_fax" : @"receive_fax",
  7670. @"shipping_billto_name" : @"receive_name",
  7671. @"shipping_billto_phone" : @"receive_phone"},
  7672. @"type" : @"pull"};
  7673. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  7674. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7675. @"type" : @"pull",
  7676. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  7677. @"shipping_billto_contact" : @"customer_contact",
  7678. @"shipping_billto_email" : @"customer_email",
  7679. @"shipping_billto_address1" : @"customer_address1",
  7680. @"shipping_billto_address2" : @"customer_address2",
  7681. @"shipping_billto_zipcode" : @"customer_zipcode",
  7682. @"shipping_billto_city" : @"customer_city",
  7683. @"shipping_billto_state" : @"customer_state",
  7684. @"shipping_billto_country" : @"customer_country",
  7685. @"shipping_billto_fax" : @"customer_fax",
  7686. @"shipping_billto_name" : @"customer_name",
  7687. @"shipping_billto_phone" : @"customer_phone"}
  7688. };
  7689. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  7690. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  7691. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  7692. @"shipping_billto_contact" : @"sender_contact",
  7693. @"shipping_billto_email" : @"sender_email",
  7694. @"shipping_billto_address1" : @"sender_address1",
  7695. @"shipping_billto_address2" : @"sender_address2",
  7696. @"shipping_billto_zipcode" : @"sender_zipcode",
  7697. @"shipping_billto_city" : @"sender_city",
  7698. @"shipping_billto_state" : @"sender_state",
  7699. @"shipping_billto_country" : @"sender_country",
  7700. @"shipping_billto_fax" : @"sender_fax",
  7701. @"shipping_billto_name" : @"sender_name",
  7702. @"shipping_billto_phone" : @"sender_phone"},
  7703. @"type" : @"pull"
  7704. };
  7705. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  7706. NSDictionary *select_freight_bill_to_dic = @{
  7707. @"aname" : @"Select freight bill to",
  7708. @"name" : @"select_cid",
  7709. @"refresh" : [NSNumber numberWithInteger:0],
  7710. @"value" : @"Sales_Order_Freight_Bill_To",
  7711. @"key_map" : @{
  7712. @"shipping_billto_cid" : @"customer_cid",
  7713. @"shipping_billto_contact" : @"customer_contact",
  7714. @"shipping_billto_email" : @"customer_email",
  7715. @"shipping_billto_address1" : @"customer_address1",
  7716. @"shipping_billto_address2" : @"customer_address2",
  7717. @"shipping_billto_zipcode" : @"customer_zipcode",
  7718. @"shipping_billto_city" : @"customer_city",
  7719. @"shipping_billto_state" : @"customer_state",
  7720. @"shipping_billto_country" : @"customer_country",
  7721. @"shipping_billto_fax" : @"customer_fax",
  7722. @"shipping_billto_name" : @"customer_name",
  7723. @"shipping_billto_phone" : @"customer_phone"
  7724. }
  7725. };
  7726. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  7727. [dic setValue:choose_dic forKey:[NSString stringWithFormat:@"item_%d",index++]];
  7728. index = [self placeOrderFillCompanyInfoInDictionnary:dic withInfo:info params:params beginIndex:index prefix:@"shipping_billto" DB:db];
  7729. // count
  7730. [dic setValue:[NSNumber numberWithInteger:index] forKey:@"count"];
  7731. // // contact id
  7732. // NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7733. // [contact_id_dic setValue:@"1" forKey:@"disable"];
  7734. // [contact_id_dic setValue:@"true" forKey:@"required"];
  7735. //
  7736. // [dic setValue:contact_id_dic forKey:@"item_1"];
  7737. //
  7738. // // company name
  7739. // NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7740. //
  7741. // [dic setValue:company_name_dic forKey:@"item_2"];
  7742. //
  7743. // // address
  7744. // NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7745. // NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  7746. // if ([ext_required integerValue]) {
  7747. // [ext_dic setValue:@"true" forKey:@"required"];
  7748. // }
  7749. //
  7750. // [dic setValue:ext_dic forKey:@"item_3"];
  7751. //
  7752. // // contact
  7753. // NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7754. //
  7755. // [dic setValue:contact_dic forKey:@"item_4"];
  7756. //
  7757. // // phone
  7758. // NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7759. //
  7760. // [dic setValue:phone_dic forKey:@"item_5"];
  7761. //
  7762. // // fax
  7763. // NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7764. // [dic setValue:fax_dic forKey:@"item_6"];
  7765. //
  7766. // // email
  7767. // NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7768. //
  7769. // [dic setValue:email_dic forKey:@"item_7"];
  7770. }];
  7771. return dic;
  7772. }
  7773. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7774. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7775. // setting
  7776. NSDictionary *setting = params[@"setting"];
  7777. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  7778. [dic setValue:hide forKey:@"hide"];
  7779. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7780. orderCode = [self translateSingleQuote:orderCode];
  7781. NSString *sql = [NSString stringWithFormat:@"select billing_cid,billing_name,billing_ext,billing_contact,billing_email,billing_fax,billing_phone,billing_address1,billing_address2,billing_state,billing_zipcode,billing_city,billing_country from offline_order where so_id = '%@';",orderCode];
  7782. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7783. NSString *cid = [self textAtColumn:0 statement:stmt];
  7784. NSString *name = [self textAtColumn:1 statement:stmt];
  7785. NSString *ext = [self textAtColumn:2 statement:stmt];
  7786. NSString *contact = [self textAtColumn:3 statement:stmt];
  7787. NSString *email = [self textAtColumn:4 statement:stmt];
  7788. NSString *fax = [self textAtColumn:5 statement:stmt];
  7789. NSString *phone = [self textAtColumn:6 statement:stmt];
  7790. NSString *addr_1 = [self textAtColumn:7 statement:stmt];
  7791. NSString *addr_2 = [self textAtColumn:8 statement:stmt];
  7792. NSString *state = [self textAtColumn:9 statement:stmt];
  7793. NSString *zipcode = [self textAtColumn:10 statement:stmt];
  7794. NSString *city = [self textAtColumn:11 statement:stmt];
  7795. NSString *country = [self textAtColumn:12 statement:stmt];
  7796. NSDictionary *info = @{
  7797. @"cid" : cid,
  7798. @"name" : name,
  7799. @"ext" : ext,
  7800. @"contact" : contact,
  7801. @"email" : email,
  7802. @"fax" : fax,
  7803. @"phone" : phone,
  7804. @"addr_1" : addr_1,
  7805. @"addr_2" : addr_2,
  7806. @"state" : state,
  7807. @"zipcode" : zipcode,
  7808. @"city" : city,
  7809. @"country" : country
  7810. };
  7811. // // count
  7812. // [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7813. // title
  7814. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  7815. // hide
  7816. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7817. int index = 0;
  7818. // choose
  7819. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7820. [choose_dic setValue:@"choose" forKey:@"aname"];
  7821. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7822. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  7823. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  7824. @"key_map" : @{@"billing_cid" : @"receive_cid",
  7825. @"billing_contact" : @"receive_contact",
  7826. @"billing_email" : @"receive_email",
  7827. @"billing_address1" : @"receive_address1",
  7828. @"billing_address2" : @"receive_address2",
  7829. @"billing_zipcode" : @"receive_zipcode",
  7830. @"billing_city" : @"receive_city",
  7831. @"billing_state" : @"receive_state",
  7832. @"billing_country" : @"receive_country",
  7833. @"billing_fax" : @"receive_fax",
  7834. @"billing_name" : @"receive_name",
  7835. @"billing_phone" : @"receive_phone"},
  7836. @"type" : @"pull"};
  7837. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  7838. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7839. @"type" : @"pull",
  7840. @"key_map" : @{@"billing_cid" : @"customer_cid",
  7841. @"billing_contact" : @"customer_contact",
  7842. @"billing_email" : @"customer_email",
  7843. @"billing_address1" : @"customer_address1",
  7844. @"billing_address2" : @"customer_address2",
  7845. @"billing_zipcode" : @"customer_zipcode",
  7846. @"billing_city" : @"customer_city",
  7847. @"billing_state" : @"customer_state",
  7848. @"billing_country" : @"customer_country",
  7849. @"billing_fax" : @"customer_fax",
  7850. @"billing_name" : @"customer_name",
  7851. @"billing_phone" : @"customer_phone"}
  7852. };
  7853. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  7854. NSDictionary *select_bill_to_dic = @{
  7855. @"aname" : @"Select bill to",
  7856. @"name" : @"select_cid",
  7857. @"refresh" : [NSNumber numberWithInteger:0],
  7858. @"value" : @"Sales_Order_Merchandise_Bill_To",
  7859. @"key_map" : @{
  7860. @"billing_cid" : @"customer_cid",
  7861. @"billing_contact" : @"customer_contact",
  7862. @"billing_email" : @"customer_email",
  7863. @"billing_address1" : @"customer_address1",
  7864. @"billing_address2" : @"customer_address2",
  7865. @"billing_zipcode" : @"customer_zipcode",
  7866. @"billing_city" : @"customer_city",
  7867. @"billing_state" : @"customer_state",
  7868. @"billing_country" : @"customer_country",
  7869. @"billing_fax" : @"customer_fax",
  7870. @"billing_name" : @"customer_name",
  7871. @"billing_phone" : @"customer_phone"
  7872. }
  7873. };
  7874. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  7875. [dic setValue:choose_dic forKey:[NSString stringWithFormat:@"item_%d",index++]];
  7876. index = [self placeOrderFillCompanyInfoInDictionnary:dic withInfo:info params:params beginIndex:index prefix:@"billing" DB:db];
  7877. // count
  7878. [dic setValue:[NSNumber numberWithInteger:index] forKey:@"count"];
  7879. // // contact id
  7880. // NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7881. // [contact_id_dic setValue:@"1" forKey:@"disable"];
  7882. // [contact_id_dic setValue:@"true" forKey:@"required"];
  7883. //
  7884. // [dic setValue:contact_id_dic forKey:@"item_1"];
  7885. //
  7886. // // company name
  7887. // NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7888. //
  7889. // [dic setValue:company_name_dic forKey:@"item_2"];
  7890. //
  7891. // // address
  7892. // NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7893. // NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  7894. // if ([ext_required integerValue]) {
  7895. // [ext_dic setValue:@"true" forKey:@"required"];
  7896. // }
  7897. //
  7898. // [dic setValue:ext_dic forKey:@"item_3"];
  7899. //
  7900. // // contact
  7901. // NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7902. //
  7903. // [dic setValue:contact_dic forKey:@"item_4"];
  7904. //
  7905. // // phone
  7906. // NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7907. //
  7908. // [dic setValue:phone_dic forKey:@"item_5"];
  7909. //
  7910. // // fax
  7911. // NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7912. // [dic setValue:fax_dic forKey:@"item_6"];
  7913. //
  7914. // // email
  7915. // NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7916. //
  7917. // [dic setValue:email_dic forKey:@"item_7"];
  7918. }];
  7919. return dic;
  7920. }
  7921. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7922. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7923. // setting
  7924. NSDictionary *setting = params[@"setting"];
  7925. NSNumber *hide = setting[@"ReturnToHide"];
  7926. [dic setValue:hide forKey:@"hide"];
  7927. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7928. orderCode = [self translateSingleQuote:orderCode];
  7929. NSString *sql = [NSString stringWithFormat:@"select returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_email,returnto_fax,returnto_phone,returnto_address1,returnto_address2,returnto_state,returnto_zipcode,returnto_city,returnto_country from offline_order where so_id = '%@';",orderCode];
  7930. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7931. NSString *cid = [self textAtColumn:0 statement:stmt];
  7932. NSString *name = [self textAtColumn:1 statement:stmt];
  7933. NSString *ext = [self textAtColumn:2 statement:stmt];
  7934. NSString *contact = [self textAtColumn:3 statement:stmt];
  7935. NSString *email = [self textAtColumn:4 statement:stmt];
  7936. NSString *fax = [self textAtColumn:5 statement:stmt];
  7937. NSString *phone = [self textAtColumn:6 statement:stmt];
  7938. NSString *addr_1 = [self textAtColumn:7 statement:stmt];
  7939. NSString *addr_2 = [self textAtColumn:8 statement:stmt];
  7940. NSString *state = [self textAtColumn:9 statement:stmt];
  7941. NSString *zipcode = [self textAtColumn:10 statement:stmt];
  7942. NSString *city = [self textAtColumn:11 statement:stmt];
  7943. NSString *country = [self textAtColumn:12 statement:stmt];
  7944. NSDictionary *info = @{
  7945. @"cid" : cid,
  7946. @"name" : name,
  7947. @"ext" : ext,
  7948. @"contact" : contact,
  7949. @"email" : email,
  7950. @"fax" : fax,
  7951. @"phone" : phone,
  7952. @"addr_1" : addr_1,
  7953. @"addr_2" : addr_2,
  7954. @"state" : state,
  7955. @"zipcode" : zipcode,
  7956. @"city" : city,
  7957. @"country" : country
  7958. };
  7959. // // count
  7960. // [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7961. // title
  7962. [dic setValue:@"Return To" forKey:@"title"];
  7963. // hide
  7964. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7965. int index = 0;
  7966. // choose
  7967. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7968. [choose_dic setValue:@"choose" forKey:@"aname"];
  7969. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7970. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  7971. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  7972. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  7973. @"returnto_contact" : @"sender_contact",
  7974. @"returnto_email" : @"sender_email",
  7975. @"returnto_address1" : @"sender_address1",
  7976. @"returnto_address2" : @"sender_address2",
  7977. @"returnto_zipcode" : @"sender_zipcode",
  7978. @"returnto_city" : @"sender_city",
  7979. @"returnto_state" : @"sender_state",
  7980. @"returnto_country" : @"sender_country",
  7981. @"returnto_fax" : @"sender_fax",
  7982. @"returnto_name" : @"sender_name",
  7983. @"returnto_phone" : @"sender_phone"},
  7984. @"type" : @"pull"};
  7985. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  7986. NSDictionary *select_return_to_dic = @{
  7987. @"aname" : @"Select return to",
  7988. @"name" : @"select_cid",
  7989. @"refresh" : [NSNumber numberWithInteger:0],
  7990. @"value" : @"Contact_Return_To",
  7991. @"key_map" : @{
  7992. @"returnto_cid" : @"customer_cid",
  7993. @"returnto_contact" : @"customer_contact",
  7994. @"returnto_email" : @"customer_email",
  7995. @"returnto_address1" : @"customer_address1",
  7996. @"returnto_address2" : @"customer_address2",
  7997. @"returnto_zipcode" : @"customer_zipcode",
  7998. @"returnto_city" : @"customer_city",
  7999. @"returnto_state" : @"customer_state",
  8000. @"returnto_country" : @"customer_country",
  8001. @"returnto_fax" : @"customer_fax",
  8002. @"returnto_name" : @"customer_name",
  8003. @"returnto_phone" : @"customer_phone"
  8004. }
  8005. };
  8006. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  8007. [dic setValue:choose_dic forKey:[NSString stringWithFormat:@"item_%d",index++]];
  8008. index = [self placeOrderFillCompanyInfoInDictionnary:dic withInfo:info params:params beginIndex:index prefix:@"returnto" DB:db];
  8009. // count
  8010. [dic setValue:[NSNumber numberWithInteger:index] forKey:@"count"];
  8011. // // contact id
  8012. // NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  8013. // [contact_id_dic setValue:@"1" forKey:@"disable"];
  8014. // [contact_id_dic setValue:@"true" forKey:@"required"];
  8015. //
  8016. // [dic setValue:contact_id_dic forKey:@"item_1"];
  8017. //
  8018. // // company name
  8019. // NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  8020. //
  8021. // [dic setValue:company_name_dic forKey:@"item_2"];
  8022. //
  8023. // // address
  8024. // NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  8025. //
  8026. // [dic setValue:ext_dic forKey:@"item_3"];
  8027. //
  8028. // // contact
  8029. // NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  8030. //
  8031. // [dic setValue:contact_dic forKey:@"item_4"];
  8032. //
  8033. // // phone
  8034. // NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  8035. //
  8036. // [dic setValue:phone_dic forKey:@"item_5"];
  8037. //
  8038. // // fax
  8039. // NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  8040. // [dic setValue:fax_dic forKey:@"item_6"];
  8041. //
  8042. // // email
  8043. // NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  8044. //
  8045. // [dic setValue:email_dic forKey:@"item_7"];
  8046. }];
  8047. return dic;
  8048. }
  8049. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  8050. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8051. orderCode = [self translateSingleQuote:orderCode];
  8052. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8053. __block double TotalCuft = 0;
  8054. __block double TotalWeight = 0;
  8055. __block int TotalCarton = 0;
  8056. __block double payments = 0;
  8057. // setting
  8058. NSDictionary *setting = params[@"setting"];
  8059. NSNumber *hide = setting[@"ModelInformationHide"];
  8060. [dic setValue:hide forKey:@"hide"];
  8061. 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];
  8062. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8063. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8064. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  8065. // item id
  8066. int item_id = sqlite3_column_int(stmt, 0);
  8067. // count
  8068. int item_count = sqlite3_column_int(stmt, 1);
  8069. // stockUom
  8070. int stockUom = sqlite3_column_int(stmt, 2);
  8071. // unit price
  8072. NSString *str_price = [self textAtColumn:3 statement:stmt];
  8073. NSString* Price=nil;
  8074. if([str_price isEqualToString:@""])
  8075. {
  8076. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  8077. if(price==nil)
  8078. Price=@"No Price.";
  8079. else
  8080. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  8081. }
  8082. else
  8083. {
  8084. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  8085. }
  8086. // discount
  8087. double discount = sqlite3_column_double(stmt, 4);
  8088. // name
  8089. NSString *name = [self textAtColumn:5 statement:stmt];
  8090. // description
  8091. NSString *description = [self textAtColumn:6 statement:stmt];
  8092. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  8093. // line note
  8094. NSString *line_note = [self textAtColumn:7 statement:stmt];
  8095. int avaulability = sqlite3_column_int(stmt, 8);
  8096. // img
  8097. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  8098. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  8099. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  8100. if(combine != nil)
  8101. {
  8102. // int citem=0;
  8103. int bcount=[[combine valueForKey:@"count"] intValue];
  8104. for(int bc=0;bc<bcount;bc++)
  8105. {
  8106. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  8107. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  8108. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  8109. subTotal += uprice * modulus * item_count;
  8110. }
  8111. }
  8112. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  8113. [model_dic setValue:@"model" forKey:@"control"];
  8114. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  8115. [model_dic setValue:description forKey:@"description"];
  8116. [model_dic setValue:line_note forKey:@"note"];
  8117. [model_dic setValue:img forKey:@"img_url"];
  8118. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  8119. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  8120. [model_dic setValue:Price forKey:@"unit_price"];
  8121. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  8122. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  8123. if (combine) {
  8124. [model_dic setValue:combine forKey:@"combine"];
  8125. }
  8126. // well,what under the row is the info for total
  8127. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  8128. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  8129. double weight=[bsubtotaljson[@"weight"] doubleValue];
  8130. int carton=[bsubtotaljson[@"carton"] intValue];
  8131. TotalCuft += cuft;
  8132. TotalWeight += weight;
  8133. TotalCarton += carton;
  8134. payments += subTotal;
  8135. //---------------------------
  8136. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  8137. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  8138. }];
  8139. [dic setValue:@"Model Information" forKey:@"title"];
  8140. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  8141. return dic;
  8142. }
  8143. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  8144. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8145. [dic setValue:@"Remarks Content" forKey:@"title"];
  8146. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  8147. // setting
  8148. NSDictionary *setting = params[@"setting"];
  8149. NSNumber *hide = setting[@"RemarksContentHide"];
  8150. [dic setValue:hide forKey:@"hide"];
  8151. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8152. orderCode = [self translateSingleQuote:orderCode];
  8153. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes from offline_order where so_id = '%@';",orderCode];
  8154. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8155. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  8156. int mustCall = sqlite3_column_int(stmt, 1);
  8157. NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  8158. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  8159. NSDictionary *po_dic = @{
  8160. @"aname" : @"PO#",
  8161. @"control" : @"edit",
  8162. @"keyboard" : @"text",
  8163. @"name" : @"poNumber",
  8164. @"value" : poNumber
  8165. };
  8166. NSDictionary *must_call_dic = @{
  8167. @"aname" : @"MUST CALL BEFORE DELIVERY",
  8168. @"control" : @"switch",
  8169. @"name" : @"must_call",
  8170. @"value" : mustCall ? @"true" : @"false"
  8171. };
  8172. NSDictionary *general_notes_dic = @{
  8173. @"aname" : @"General notes",
  8174. @"control" : @"text_view",
  8175. @"keyboard" : @"text",
  8176. @"name" : @"comments",
  8177. @"value" : generalNotes
  8178. };
  8179. NSDictionary *internal_notes_dic = @{
  8180. @"aname" : @"Internal notes",
  8181. @"control" : @"text_view",
  8182. @"keyboard" : @"text",
  8183. @"name" : @"internal_notes",
  8184. @"value" : internalNotes
  8185. };
  8186. [dic setValue:po_dic forKey:@"item_0"];
  8187. [dic setValue:must_call_dic forKey:@"item_1"];
  8188. [dic setValue:general_notes_dic forKey:@"item_2"];
  8189. [dic setValue:internal_notes_dic forKey:@"item_3"];
  8190. }];
  8191. return dic;
  8192. }
  8193. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  8194. // params
  8195. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8196. orderCode = [self translateSingleQuote:orderCode];
  8197. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8198. // setting
  8199. NSDictionary *setting = params[@"setting"];
  8200. NSNumber *hide = setting[@"OrderTotalHide"];
  8201. [dic setValue:hide forKey:@"hide"];
  8202. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  8203. __block double lift_gate_value = 0;
  8204. __block double handling_fee = 0;
  8205. __block double shipping = 0;
  8206. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8207. int lift_gate = sqlite3_column_int(stmt, 0);
  8208. lift_gate_value = sqlite3_column_double(stmt, 1);
  8209. shipping = sqlite3_column_double(stmt, 2);
  8210. handling_fee = sqlite3_column_double(stmt, 3);
  8211. if (!lift_gate) {
  8212. lift_gate_value = 0;
  8213. }
  8214. }];
  8215. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  8216. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  8217. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  8218. double payments = [[total valueForKey:@"payments"] doubleValue];
  8219. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  8220. double totalPrice = payments;
  8221. [dic setValue:@"Order Total" forKey:@"title"];
  8222. NSDictionary *payments_dic = @{
  8223. @"align" : @"right",
  8224. @"aname" : @"Payments/Credits",
  8225. @"control" : @"text",
  8226. @"name" : @"paymentsAndCredits",
  8227. @"type" : @"price",
  8228. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  8229. };
  8230. [dic setValue:payments_dic forKey:@"item_0"];
  8231. // version 1.71 remove
  8232. // NSDictionary *handling_fee_dic = @{
  8233. // @"align" : @"right",
  8234. // @"aname" : @"Handling Fee",
  8235. // @"control" : @"text",
  8236. // @"name" : @"handling_fee_value",
  8237. // @"required" : @"true",
  8238. // @"type" : @"price",
  8239. // @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  8240. // };
  8241. NSDictionary *shipping_dic = @{
  8242. @"align" : @"right",
  8243. @"aname" : @"Shipping*",
  8244. @"control" : @"text",
  8245. @"name" : @"shipping",
  8246. @"required" : @"true",
  8247. @"type" : @"price",
  8248. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  8249. };
  8250. NSDictionary *lift_gate_dic = @{
  8251. @"align" : @"right",
  8252. @"aname" : @"Liftgate Fee(No Loading Dock)",
  8253. @"control" : @"text",
  8254. @"name" : @"lift_gate_value",
  8255. @"required" : @"true",
  8256. @"type" : @"price",
  8257. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  8258. };
  8259. int item_count = 1;
  8260. if (![[params objectForKey:@"logist"] isEqualToString:@"WILL CALL"]) {
  8261. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  8262. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  8263. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8264. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8265. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  8266. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8267. } else {
  8268. }
  8269. }
  8270. // version 1.71 remove
  8271. // [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8272. NSDictionary *total_price_dic = @{
  8273. @"align" : @"right",
  8274. @"aname" : @"Total",
  8275. @"control" : @"text",
  8276. @"name" : @"totalPrice",
  8277. @"type" : @"price",
  8278. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  8279. };
  8280. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8281. NSDictionary *total_cuft_dic = @{
  8282. @"align" : @"right",
  8283. @"aname" : @"Total Cuft",
  8284. @"control" : @"text",
  8285. @"name" : @"",
  8286. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  8287. };
  8288. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8289. NSDictionary *total_weight_dic = @{
  8290. @"align" : @"right",
  8291. @"aname" : @"Total Weight",
  8292. @"control" : @"text",
  8293. @"name" : @"",
  8294. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  8295. };
  8296. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8297. NSDictionary *total_carton_dic = @{
  8298. @"align" : @"right",
  8299. @"aname" : @"Total Carton",
  8300. @"control" : @"text",
  8301. @"name" : @"",
  8302. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  8303. };
  8304. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  8305. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  8306. return dic;
  8307. }
  8308. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  8309. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8310. orderCode = [self translateSingleQuote:orderCode];
  8311. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8312. // setting
  8313. NSDictionary *setting = params[@"setting"];
  8314. NSNumber *hide = setting[@"SignatureHide"];
  8315. [dic setValue:hide forKey:@"hide"];
  8316. [dic setValue:@"Signature" forKey:@"title"];
  8317. __block NSString *pic_path = @"";
  8318. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  8319. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8320. pic_path = [self textAtColumn:0 statement:stmt];
  8321. }];
  8322. NSDictionary *pic_dic = @{
  8323. @"aname" : @"Signature",
  8324. @"avalue" :pic_path,
  8325. @"control" : @"signature",
  8326. @"name" : @"sign_picpath",
  8327. @"value" : pic_path
  8328. };
  8329. [dic setValue:pic_dic forKey:@"item_0"];
  8330. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8331. return dic;
  8332. }
  8333. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  8334. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8335. // setting
  8336. NSDictionary *setting = params[@"setting"];
  8337. NSNumber *hide = setting[@"ShippingMethodHide"];
  8338. [dic setValue:hide forKey:@"hide"];
  8339. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8340. orderCode = [self translateSingleQuote:orderCode];
  8341. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  8342. __block NSString *logist = @"";
  8343. __block NSString *lift_gate = @"";
  8344. __block int lift_gate_integer = 0;
  8345. __block NSString *logistic_note = @"";
  8346. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8347. logist = [self textAtColumn:0 statement:stmt];
  8348. lift_gate_integer = sqlite3_column_int(stmt, 1);
  8349. logistic_note = [self textAtColumn:2 statement:stmt];
  8350. }];
  8351. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8352. [dic setValue:@"Shipping Method" forKey:@"title"];
  8353. // val_0
  8354. int PERSONAL_PICK_UP_check = 0;
  8355. int USE_MY_CARRIER_check = 0;
  8356. NSString *logistic_note_text = @"";
  8357. int will_call_check = 0;
  8358. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  8359. will_call_check = 1;
  8360. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  8361. PERSONAL_PICK_UP_check = 1;
  8362. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  8363. USE_MY_CARRIER_check = 1;
  8364. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  8365. logistic_note = [logistic_note_array firstObject];
  8366. if (logistic_note_array.count > 1) {
  8367. logistic_note_text = [logistic_note_array lastObject];
  8368. }
  8369. }
  8370. }
  8371. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  8372. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  8373. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  8374. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  8375. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  8376. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  8377. NSDictionary *val0_subItem_item0 = @{
  8378. @"aname" : @"Option",
  8379. @"cadedate" : @{
  8380. @"count" : [NSNumber numberWithInteger:2],
  8381. @"val_0" : @{
  8382. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  8383. @"refresh" : [NSNumber numberWithInteger:0],
  8384. @"value" : @"PERSONAL PICK UP",
  8385. @"value_id" : @"PERSONAL PICK UP"
  8386. },
  8387. @"val_1" : @{
  8388. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  8389. @"refresh" : [NSNumber numberWithInteger:0],
  8390. @"sub_item" : @{
  8391. @"count" : [NSNumber numberWithInteger:1],
  8392. @"item_0" : @{
  8393. @"aname" : @"BOL",
  8394. @"control" : @"edit",
  8395. @"keyboard" : @"text",
  8396. @"name" : @"logist_note_text",
  8397. @"refresh" : [NSNumber numberWithInteger:0],
  8398. @"required" : @"false",
  8399. @"value" : logistic_note_text
  8400. }
  8401. },
  8402. @"value" : @"USE MY CARRIER",
  8403. @"value_id" : @"USE MY CARRIER"
  8404. }
  8405. },
  8406. @"control" : @"enum",
  8407. @"name" : @"logistic_note",
  8408. @"required" : @"true",
  8409. @"single_select" : @"true"
  8410. };
  8411. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  8412. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  8413. // val_1
  8414. lift_gate = [self valueInParams:params key:@"lift_gate"];
  8415. if([lift_gate isEqualToString:@""]) {
  8416. if (lift_gate_integer == 1) {
  8417. lift_gate = @"true";
  8418. } else {
  8419. lift_gate = @"false";
  8420. }
  8421. }
  8422. int common_carrier_check = 0;
  8423. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  8424. common_carrier_check = 1;
  8425. [params setValue:lift_gate forKey:@"lift_gate"];
  8426. }
  8427. NSDictionary *val_1 = @{
  8428. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  8429. @"sub_item" : @{
  8430. @"count" : [NSNumber numberWithInteger:1],
  8431. @"item_0" : @{
  8432. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  8433. @"control" : @"switch",
  8434. @"name" : @"lift_gate",
  8435. @"refresh" : [NSNumber numberWithInteger:1],
  8436. @"required" : @"true",
  8437. @"value" : lift_gate
  8438. }
  8439. },
  8440. @"value" : @"COMMON CARRIER",
  8441. @"value_id" : @"COMMON CARRIER"
  8442. };
  8443. // cadedate
  8444. NSDictionary *cadedate = @{
  8445. @"count" : [NSNumber numberWithInteger:2],
  8446. @"val_0" : val_0,
  8447. @"val_1" : val_1
  8448. };
  8449. // item_0
  8450. NSMutableDictionary *item_0 = @{
  8451. @"aname" : @"Shipping",
  8452. @"cadedate" : cadedate,
  8453. @"control" : @"enum",
  8454. @"name" : @"logist",
  8455. @"refresh" : [NSNumber numberWithInteger:1],
  8456. @"single_select" : @"true",
  8457. }.mutableCopy;
  8458. NSNumber *required = setting[@"ShippingMethodRequire"];
  8459. if ([required integerValue]) {
  8460. [item_0 setValue:@"true" forKey:@"required"];
  8461. }
  8462. [dic setValue:item_0 forKey:@"item_0"];
  8463. if ([logist isEqualToString:@"WILL CALL"]) {
  8464. [dic removeObjectForKey:@"lift_gate"];
  8465. }
  8466. return dic;
  8467. }
  8468. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  8469. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8470. orderCode = [self translateSingleQuote:orderCode];
  8471. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  8472. __block int submit_as_integer = 0;
  8473. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8474. submit_as_integer = sqlite3_column_int(stmt, 0);
  8475. }];
  8476. int check11 = 0;
  8477. int check10 = 0;
  8478. if (submit_as_integer == 11) {
  8479. check11 = 1;
  8480. }
  8481. if (submit_as_integer == 10) {
  8482. check10 = 1;
  8483. }
  8484. // section_0
  8485. NSMutableDictionary *dic = @{
  8486. @"count" : @(1),
  8487. @"item_0" : @{
  8488. @"aname" : @"Submit Order As",
  8489. @"cadedate" : @{
  8490. @"count" : @(2),
  8491. @"val_0" : @{
  8492. @"check" : [NSNumber numberWithInteger:check11],
  8493. @"value" : @"Sales Order",
  8494. @"value_id" : @(11)
  8495. },
  8496. @"val_1" : @{
  8497. @"check" : [NSNumber numberWithInteger:check10],
  8498. @"value" : @"Quote",
  8499. @"value_id" : @(10)
  8500. }
  8501. },
  8502. @"control" : @"enum",
  8503. @"name" : @"erpOrderStatus",
  8504. @"required" : @"true",
  8505. @"single_select" : @"true"
  8506. },
  8507. @"title" : @"Order Type"
  8508. }.mutableCopy;
  8509. // setting
  8510. NSDictionary *setting = params[@"setting"];
  8511. NSNumber *hide = setting[@"OrderTypeHide"];
  8512. [dic setValue:hide forKey:@"hide"];
  8513. return dic;
  8514. }
  8515. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  8516. countryCode = [self translateSingleQuote:countryCode];
  8517. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  8518. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8519. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  8520. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  8521. if (name == NULL) {
  8522. name = "";
  8523. }
  8524. if (code == NULL) {
  8525. code = "";
  8526. }
  8527. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  8528. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  8529. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  8530. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  8531. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  8532. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  8533. }
  8534. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  8535. [container setValue:stateDic forKey:key];
  8536. }] mutableCopy];
  8537. [ret removeObjectForKey:@"result"];
  8538. // failure 可以不用了,一样的
  8539. if (ret.allKeys.count == 0) {
  8540. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  8541. [stateDic setValue:@"Other" forKey:@"value"];
  8542. [stateDic setValue:@"" forKey:@"value_id"];
  8543. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  8544. if (state_code && [@"" isEqualToString:state_code]) {
  8545. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  8546. }
  8547. NSString *key = [NSString stringWithFormat:@"val_0"];
  8548. [ret setValue:stateDic forKey:key];
  8549. }
  8550. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  8551. return ret;
  8552. }
  8553. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  8554. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8555. orderCode = [self translateSingleQuote:orderCode];
  8556. 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];
  8557. __block NSString *payType = @"";
  8558. __block NSString *firstName = @"";
  8559. __block NSString *lastName = @"";
  8560. __block NSString *addr1 = @"";
  8561. __block NSString *addr2 = @"";
  8562. __block NSString *zipcode = @"";
  8563. __block NSString *cardType = @"";
  8564. __block NSString *cardNumber = @"";
  8565. __block NSString *securityCode = @"";
  8566. __block NSString *month = @"";
  8567. __block NSString *year = @"";
  8568. __block NSString *city = @"";
  8569. __block NSString *state = @"";
  8570. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8571. payType = [self textAtColumn:0 statement:stmt];
  8572. firstName = [self textAtColumn:1 statement:stmt];
  8573. lastName = [self textAtColumn:2 statement:stmt];
  8574. addr1 = [self textAtColumn:3 statement:stmt];
  8575. addr2 = [self textAtColumn:4 statement:stmt];
  8576. zipcode = [self textAtColumn:5 statement:stmt];
  8577. cardType = [self textAtColumn:6 statement:stmt];
  8578. cardNumber = [self textAtColumn:7 statement:stmt];
  8579. securityCode = [self textAtColumn:8 statement:stmt];
  8580. month = [self textAtColumn:9 statement:stmt];
  8581. year = [self textAtColumn:10 statement:stmt];
  8582. city = [self textAtColumn:11 statement:stmt];
  8583. state = [self textAtColumn:12 statement:stmt];
  8584. }];
  8585. NSString *required = @"true";
  8586. // setting
  8587. NSDictionary *setting = params[@"setting"];
  8588. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  8589. if ([requiredNumber integerValue]) {
  8590. required = @"true";
  8591. } else {
  8592. required = @"false";
  8593. }
  8594. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  8595. // create table if not exists payment_type (_id integer primary key,type_id text,pay_type text,sort int);
  8596. NSString *pay_type_sql = @"select type_id,pay_type,sort_id from payment_type order by sort_id asc";
  8597. [iSalesDB jk_query:pay_type_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8598. NSString *type_id = [self textAtColumn:0 statement:stmt];
  8599. NSString *type = [self textAtColumn:1 statement:stmt];
  8600. NSMutableDictionary *val = @{
  8601. @"check" : [payType isEqualToString:type_id] ? @(1) : @(0),
  8602. @"value" : type,
  8603. @"value_id" : type_id
  8604. }.mutableCopy;
  8605. if ([type_id isEqualToString:@"Visa/Master"] || [type_id isEqualToString:@"Credit Card"]) {
  8606. NSDictionary *sub_item = @{
  8607. @"count" : @(3),
  8608. @"item_0" : @{
  8609. @"aname" : @"choose",
  8610. @"control" : @"multi_action",
  8611. @"count" : @(1),
  8612. @"item_0" : @{
  8613. @"aname" : @"Same as customer",
  8614. @"key_map" : @{
  8615. @"credit_card_address1" : @"customer_address1",
  8616. @"credit_card_address2" : @"customer_address2",
  8617. @"credit_card_city" : @"customer_city",
  8618. @"credit_card_first_name" : @"customer_first_name",
  8619. @"credit_card_last_name" : @"customer_last_name",
  8620. @"credit_card_state" : @"customer_state",
  8621. @"credit_card_zipcode" : @"customer_zipcode"
  8622. },
  8623. @"type" : @"pull"
  8624. }
  8625. },
  8626. @"item_1" : @{
  8627. @"aname" : @"",
  8628. @"color" : @"red",
  8629. @"control" : @"text",
  8630. @"name" : @"",
  8631. @"value" : @"USA Credit cards only"
  8632. },
  8633. @"item_2" : @{
  8634. @"aname" : @"Fill",
  8635. @"cadedate" : @{
  8636. @"count" : @(2),
  8637. @"val_0" : @{
  8638. @"check" : @(1),
  8639. @"sub_item" : @{
  8640. @"count" : @(11),
  8641. @"item_0" : @{
  8642. @"aname" : @"Type",
  8643. @"cadedate" : @{
  8644. @"count" : @(2),
  8645. @"val_0" : @{
  8646. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  8647. @"value" : @"VISA",
  8648. @"value_id" : @"VISA"/*@(0)*/
  8649. },
  8650. @"val_1" : @{
  8651. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  8652. @"value" : @"MASTER CARD",
  8653. @"value_id" : @"MASTER CARD"/*@(1)*/
  8654. }
  8655. },
  8656. @"control" : @"enum",
  8657. @"name" : @"credit_card_type",
  8658. @"required" : @"true",
  8659. @"single_select" : @"true"
  8660. },
  8661. @"item_1" : @{
  8662. @"aname" : @"Number",
  8663. @"control" : @"edit",
  8664. @"keyboard" : @"int",
  8665. @"length" : @"16",
  8666. @"name" : @"credit_card_number",
  8667. @"required" : @"true",
  8668. @"value" : cardNumber
  8669. },
  8670. @"item_10" : @{
  8671. @"aname" : @"State",
  8672. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  8673. @"control" : @"enum",
  8674. @"enum" : @"true",
  8675. @"name" : @"credit_card_state",
  8676. @"required" : @"true",
  8677. @"single_select" : @"true"
  8678. },
  8679. @"item_2" : @{
  8680. @"aname" : @"Expiration Date",
  8681. @"control" : @"monthpicker",
  8682. @"name" : @"credit_card_expiration",
  8683. @"required" : @"true",
  8684. @"type" : @"date",
  8685. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  8686. },
  8687. @"item_3" : @{
  8688. @"aname" : @"Security Code",
  8689. @"control" : @"edit",
  8690. @"keyboard" : @"int",
  8691. @"length" : @"3",
  8692. @"name" : @"credit_card_security_code",
  8693. @"required" : @"true",
  8694. @"value" : securityCode
  8695. },
  8696. @"item_4" : @{
  8697. @"aname" : @"First Name",
  8698. @"control" : @"edit",
  8699. @"keyboard" : @"text",
  8700. @"name" : @"credit_card_first_name",
  8701. @"required" : @"true",
  8702. @"value" : firstName
  8703. },
  8704. @"item_5" : @{
  8705. @"aname" : @"Last Name",
  8706. @"control" : @"edit",
  8707. @"keyboard" : @"text",
  8708. @"name" : @"credit_card_last_name",
  8709. @"required" : @"true",
  8710. @"value" : lastName
  8711. },
  8712. @"item_6" : @{
  8713. @"aname" : @"Address 1",
  8714. @"control" : @"edit",
  8715. @"keyboard" : @"text",
  8716. @"name" : @"credit_card_address1",
  8717. @"required" : @"true",
  8718. @"value" : addr1
  8719. },
  8720. @"item_7" : @{
  8721. @"aname" : @"Address 2",
  8722. @"control" : @"edit",
  8723. @"keyboard" : @"text",
  8724. @"name" : @"credit_card_address2",
  8725. @"value" : addr2
  8726. },
  8727. @"item_8" : @{
  8728. @"aname" : @"zip code",
  8729. @"control" : @"edit",
  8730. @"keyboard" : @"text",
  8731. @"name" : @"credit_card_zipcode",
  8732. @"required" : @"true",
  8733. @"value" : zipcode
  8734. },
  8735. @"item_9" : @{
  8736. @"aname" : @"City",
  8737. @"control" : @"edit",
  8738. @"keyboard" : @"text",
  8739. @"name" : @"credit_card_city",
  8740. @"required" : @"true",
  8741. @"value" : city
  8742. }
  8743. },
  8744. @"value" : @"Fill Now",
  8745. @"value_id" : @""
  8746. },
  8747. @"val_1" : @{
  8748. @"check" : @(0),
  8749. @"value" : @"Fill Later",
  8750. @"value_id" : @""
  8751. }
  8752. },
  8753. @"control" : @"enum",
  8754. @"name" : @"",
  8755. @"single_select" : @"true"
  8756. }
  8757. };
  8758. [val setObject:sub_item forKey:@"sub_item"];
  8759. }
  8760. [cadedate setObject:val forKey:[NSString stringWithFormat:@"val_%ld",(*count)]];
  8761. [cadedate setObject:[NSNumber numberWithInteger:(++*count)] forKey:@"count"];
  8762. }];
  8763. // "section_2"
  8764. NSMutableDictionary *dic = @{
  8765. @"count" : @(1),
  8766. @"item_0" : @{
  8767. @"aname" : @"Payment",
  8768. @"required" : required,
  8769. @"cadedate" : cadedate,
  8770. // @{
  8771. // @"count" : @(6),
  8772. // @"val_3" : @{
  8773. // @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  8774. // @"value" : @"Check",
  8775. // @"value_id" : @"Check"
  8776. // },
  8777. // @"val_2" : @{
  8778. // @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  8779. // @"value" : @"Cash",
  8780. // @"value_id" : @"Cash"
  8781. // },
  8782. // @"val_1" : @{
  8783. // @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  8784. // @"value" : @"NET 60",
  8785. // @"value_id" : @"NET 30"
  8786. // },
  8787. // @"val_4" : @{
  8788. // @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  8789. // @"value" : @"Wire Transfer",
  8790. // @"value_id" : @"Wire Transfer"
  8791. // },
  8792. // @"val_0" : @{
  8793. // @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  8794. // @"sub_item" : @{
  8795. // @"count" : @(3),
  8796. // @"item_0" : @{
  8797. // @"aname" : @"choose",
  8798. // @"control" : @"multi_action",
  8799. // @"count" : @(1),
  8800. // @"item_0" : @{
  8801. // @"aname" : @"Same as customer",
  8802. // @"key_map" : @{
  8803. // @"credit_card_address1" : @"customer_address1",
  8804. // @"credit_card_address2" : @"customer_address2",
  8805. // @"credit_card_city" : @"customer_city",
  8806. // @"credit_card_first_name" : @"customer_first_name",
  8807. // @"credit_card_last_name" : @"customer_last_name",
  8808. // @"credit_card_state" : @"customer_state",
  8809. // @"credit_card_zipcode" : @"customer_zipcode"
  8810. // },
  8811. // @"type" : @"pull"
  8812. // }
  8813. // },
  8814. // @"item_1" : @{
  8815. // @"aname" : @"",
  8816. // @"color" : @"red",
  8817. // @"control" : @"text",
  8818. // @"name" : @"",
  8819. // @"value" : @"USA Credit cards only"
  8820. // },
  8821. // @"item_2" : @{
  8822. // @"aname" : @"Fill",
  8823. // @"cadedate" : @{
  8824. // @"count" : @(2),
  8825. // @"val_0" : @{
  8826. // @"check" : @(1),
  8827. // @"sub_item" : @{
  8828. // @"count" : @(11),
  8829. // @"item_0" : @{
  8830. // @"aname" : @"Type",
  8831. // @"cadedate" : @{
  8832. // @"count" : @(2),
  8833. // @"val_0" : @{
  8834. // @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  8835. // @"value" : @"VISA",
  8836. // @"value_id" : @(0)
  8837. // },
  8838. // @"val_1" : @{
  8839. // @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  8840. // @"value" : @"MASTER CARD",
  8841. // @"value_id" : @(1)
  8842. // }
  8843. // },
  8844. // @"control" : @"enum",
  8845. // @"name" : @"credit_card_type",
  8846. // @"required" : @"true",
  8847. // @"single_select" : @"true"
  8848. // },
  8849. // @"item_1" : @{
  8850. // @"aname" : @"Number",
  8851. // @"control" : @"edit",
  8852. // @"keyboard" : @"int",
  8853. // @"length" : @"16",
  8854. // @"name" : @"credit_card_number",
  8855. // @"required" : @"true",
  8856. // @"value" : cardNumber
  8857. // },
  8858. // @"item_10" : @{
  8859. // @"aname" : @"State",
  8860. // @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  8861. // @"control" : @"enum",
  8862. // @"enum" : @"true",
  8863. // @"name" : @"credit_card_state",
  8864. // @"required" : @"true",
  8865. // @"single_select" : @"true"
  8866. // },
  8867. // @"item_2" : @{
  8868. // @"aname" : @"Expiration Date",
  8869. // @"control" : @"monthpicker",
  8870. // @"name" : @"credit_card_expiration",
  8871. // @"required" : @"true",
  8872. // @"type" : @"date",
  8873. // @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  8874. // },
  8875. // @"item_3" : @{
  8876. // @"aname" : @"Security Code",
  8877. // @"control" : @"edit",
  8878. // @"keyboard" : @"int",
  8879. // @"length" : @"3",
  8880. // @"name" : @"credit_card_security_code",
  8881. // @"required" : @"true",
  8882. // @"value" : securityCode
  8883. // },
  8884. // @"item_4" : @{
  8885. // @"aname" : @"First Name",
  8886. // @"control" : @"edit",
  8887. // @"keyboard" : @"text",
  8888. // @"name" : @"credit_card_first_name",
  8889. // @"required" : @"true",
  8890. // @"value" : firstName
  8891. // },
  8892. // @"item_5" : @{
  8893. // @"aname" : @"Last Name",
  8894. // @"control" : @"edit",
  8895. // @"keyboard" : @"text",
  8896. // @"name" : @"credit_card_last_name",
  8897. // @"required" : @"true",
  8898. // @"value" : lastName
  8899. // },
  8900. // @"item_6" : @{
  8901. // @"aname" : @"Address 1",
  8902. // @"control" : @"edit",
  8903. // @"keyboard" : @"text",
  8904. // @"name" : @"credit_card_address1",
  8905. // @"required" : @"true",
  8906. // @"value" : addr1
  8907. // },
  8908. // @"item_7" : @{
  8909. // @"aname" : @"Address 2",
  8910. // @"control" : @"edit",
  8911. // @"keyboard" : @"text",
  8912. // @"name" : @"credit_card_address2",
  8913. // @"value" : addr2
  8914. // },
  8915. // @"item_8" : @{
  8916. // @"aname" : @"zip code",
  8917. // @"control" : @"edit",
  8918. // @"keyboard" : @"text",
  8919. // @"name" : @"credit_card_zipcode",
  8920. // @"required" : @"true",
  8921. // @"value" : zipcode
  8922. // },
  8923. // @"item_9" : @{
  8924. // @"aname" : @"City",
  8925. // @"control" : @"edit",
  8926. // @"keyboard" : @"text",
  8927. // @"name" : @"credit_card_city",
  8928. // @"required" : @"true",
  8929. // @"value" : city
  8930. // }
  8931. // },
  8932. // @"value" : @"Fill Now",
  8933. // @"value_id" : @""
  8934. // },
  8935. // @"val_1" : @{
  8936. // @"check" : @(0),
  8937. // @"value" : @"Fill Later",
  8938. // @"value_id" : @""
  8939. // }
  8940. // },
  8941. // @"control" : @"enum",
  8942. // @"name" : @"",
  8943. // @"single_select" : @"true"
  8944. // }
  8945. // },
  8946. // @"value" : @"Visa/Master",
  8947. // @"value_id" : @"Credit Card"
  8948. // },
  8949. // @"val_5" : @{
  8950. // @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  8951. // @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  8952. // @"value_id" : @"FOLLOW EXISTING"
  8953. // }
  8954. // },
  8955. @"control" : @"enum",
  8956. @"name" : @"paymentType",
  8957. @"single_select" : @"true"
  8958. },
  8959. @"title" : @"Payment Information"
  8960. }.mutableCopy;
  8961. NSNumber *hide = setting[@"PaymentInformationHide"];
  8962. [dic setValue:hide forKey:@"hide"];
  8963. return dic;
  8964. }
  8965. + (BOOL)orderOutOfStock:(NSDictionary *)params db:(sqlite3 *)db{
  8966. // params
  8967. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8968. orderCode = [self translateSingleQuote:orderCode];
  8969. // 缺货检查
  8970. 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];
  8971. __block BOOL outOfStock = NO;
  8972. sqlite3 *database = db;
  8973. if (!db) {
  8974. database = [iSalesDB get_db];
  8975. }
  8976. [iSalesDB jk_query:out_of_stock_SQL db:database close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8977. int availability = sqlite3_column_int(stmt, 0);
  8978. int item_qty = sqlite3_column_int(stmt, 1);
  8979. outOfStock = outOfStock || (availability < item_qty ? YES : NO);
  8980. }];
  8981. if (!db) {
  8982. [iSalesDB close_db:database];
  8983. }
  8984. return outOfStock;
  8985. }
  8986. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  8987. {
  8988. // params
  8989. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8990. orderCode = [self translateSingleQuote:orderCode];
  8991. NSDictionary *addressRefreshDic = [self placeOrderAddressRefresh:params];
  8992. [params setValuesForKeysWithDictionary:addressRefreshDic];
  8993. sqlite3 *db = [iSalesDB get_db];
  8994. // 缺货检查
  8995. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8996. if (!appDelegate.can_create_backorder) {
  8997. BOOL out_of_stock = [self orderOutOfStock:params db:db];
  8998. if (out_of_stock) {
  8999. [iSalesDB close_db:db];
  9000. NSMutableDictionary *resultDic = [NSMutableDictionary dictionary];
  9001. [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"];
  9002. [resultDic setObject:[NSNumber numberWithInt:8] forKey:@"result"];
  9003. return [RAConvertor dict2data:resultDic];
  9004. }
  9005. }
  9006. // UISetting
  9007. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9008. NSString *cachefolder = [paths objectAtIndex:0];
  9009. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  9010. NSData* json =nil;
  9011. json=[NSData dataWithContentsOfFile:img_cache];
  9012. NSError *error=nil;
  9013. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  9014. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  9015. [params setObject:setting forKey:@"setting"];
  9016. int section_count = 0;
  9017. // 0 Order Type 1 Shipping Method 2 Payment Information
  9018. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  9019. // 0 Order Type
  9020. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  9021. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  9022. [ret setValue:order_type_dic forKey:key0];
  9023. // 1 Shipping Method
  9024. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  9025. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  9026. [ret setValue:shipping_method_dic forKey:key1];
  9027. // 2 Payment Information
  9028. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  9029. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  9030. [ret setValue:payment_info_dic forKey:key2];
  9031. // 3 Customer
  9032. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  9033. NSDictionary *customer_dic = [self customerDic:params db:db];
  9034. [ret setValue:customer_dic forKey:key3];
  9035. // 4 Ship To
  9036. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  9037. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  9038. [ret setValue:ship_to_dic forKey:key4];
  9039. // 5 Ship From
  9040. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  9041. if (![shipFromDisable integerValue]) {
  9042. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  9043. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  9044. [ret setValue:ship_from_dic forKey:key5];
  9045. }
  9046. // 6 Freight Bill To
  9047. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  9048. if (![freightBillToDisable integerValue]) {
  9049. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  9050. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  9051. [ret setValue:freight_bill_to forKey:key6];
  9052. }
  9053. // 7 Merchandise Bill To
  9054. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  9055. if (![merchandiseBillToDisable integerValue]) {
  9056. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  9057. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  9058. [ret setValue:merchandise_bill_to_dic forKey:key7];
  9059. }
  9060. // 8 Return To
  9061. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  9062. if (![returnToDisable integerValue]) {
  9063. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  9064. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  9065. [ret setValue:return_to_dic forKey:key8];
  9066. }
  9067. // 9 Model Information
  9068. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  9069. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  9070. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  9071. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  9072. [ret setValue:model_info_dic forKey:key9];
  9073. // 10 Remarks Content
  9074. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  9075. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  9076. [ret setValue:remarks_content_dic forKey:key10];
  9077. // 11 Order Total
  9078. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  9079. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  9080. [ret setValue:order_total_dic forKey:key11];
  9081. // 12 Signature
  9082. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  9083. NSDictionary *sign_dic = [self signatureDic:params db:db];
  9084. [ret setValue:sign_dic forKey:key12];
  9085. [ret setValue:@(section_count) forKey:@"section_count"];
  9086. [iSalesDB close_db:db];
  9087. NSDictionary *up_params = @{
  9088. @"count" : @(10),
  9089. @"val_0" : @"receive_zipcode",
  9090. @"val_1" : @"receive_country",
  9091. @"val_2" : @"sender_zipcode",
  9092. @"val_3" : @"sender_country",
  9093. @"val_4" : @"shipping_billto_zipcode",
  9094. @"val_5" : @"shipping_billto_country",
  9095. @"val_6" : @"billing_zipcode",
  9096. @"val_7" : @"billing_country",
  9097. @"val_8" : @"returnto_zipcode",
  9098. @"val_9" : @"returnto_country",
  9099. };
  9100. [ret setObject:up_params forKey:@"up_params"];
  9101. return [RAConvertor dict2data:ret];
  9102. // return nil;
  9103. }
  9104. #pragma mark addr editor
  9105. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  9106. NSMutableDictionary *new_item = [item mutableCopy];
  9107. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  9108. return new_item;
  9109. }
  9110. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  9111. {
  9112. // "is_subaction" = true;
  9113. // orderCode = MOB1608240002;
  9114. // password = 123456;
  9115. // "subaction_tag" = 1;
  9116. // user = EvanK;
  9117. // {
  9118. // "is_subaction" = true;
  9119. // orderCode = MOB1608240002;
  9120. // password = 123456;
  9121. // "refresh_trigger" = zipcode;
  9122. // "subaction_tag" = 1;
  9123. // user = EvanK;
  9124. // }
  9125. NSString *country_code = nil;
  9126. NSString *zipCode = nil;
  9127. NSString *stateCode = nil;
  9128. NSString *city = nil;
  9129. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  9130. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  9131. NSString *code_id = params[@"country"];
  9132. country_code = [self countryCodeByid:code_id];
  9133. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  9134. NSString *zip_code = params[@"zipcode"];
  9135. // 剔除全部为空格
  9136. int spaceCount = 0;
  9137. for (int i = 0; i < zip_code.length; i++) {
  9138. if ([zip_code characterAtIndex:i] == ' ') {
  9139. spaceCount++;
  9140. }
  9141. }
  9142. if (spaceCount == zip_code.length) {
  9143. zip_code = @"";
  9144. }
  9145. zipCode = zip_code;
  9146. if (zipCode.length > 0) {
  9147. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  9148. country_code = [dic valueForKey:@"country_code"];
  9149. if (!country_code) {
  9150. // country_code = @"US";
  9151. NSString *code_id = params[@"country"];
  9152. country_code = [self countryCodeByid:code_id];
  9153. }
  9154. stateCode = [dic valueForKey:@"state_code"];
  9155. if(!stateCode.length) {
  9156. stateCode = params[@"state"];
  9157. }
  9158. city = [dic valueForKey:@"city"];
  9159. if (!city.length) {
  9160. city = params[@"city"];
  9161. }
  9162. // zip code
  9163. // NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  9164. // [zipDic setValue:zipCode forKey:@"value"];
  9165. // [section_0 setValue:zipDic forKey:@"item_11"];
  9166. } else {
  9167. NSString *code_id = params[@"country"];
  9168. country_code = [self countryCodeByid:code_id];
  9169. stateCode = params[@"state"];
  9170. city = params[@"city"];
  9171. }
  9172. }
  9173. }
  9174. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  9175. // [ret removeObjectForKey:@"up_params"];
  9176. [ret setObject:@"New Address" forKey:@"title"];
  9177. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  9178. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  9179. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  9180. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  9181. // [country_dic removeObjectForKey:@"refresh"];
  9182. // [country_dic removeObjectForKey:@"restore"];
  9183. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  9184. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  9185. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  9186. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  9187. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  9188. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  9189. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  9190. // [zip_code_dic removeObjectForKey:@"refresh"];
  9191. [zip_code_dic setValue:zipCode ? zipCode :@"" forKey:@"value"];
  9192. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  9193. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  9194. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  9195. NSDictionary *cityDic = [[new_section_0 objectForKey:@"item_8"] mutableCopy];
  9196. [cityDic setValue:city ? city : @"" forKey:@"value"];
  9197. [new_section_0 setObject:cityDic forKey:@"item_8"];
  9198. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  9199. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  9200. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  9201. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  9202. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  9203. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  9204. // country
  9205. NSString *countryCode = country_code == nil ? @"US" : country_code;
  9206. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  9207. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  9208. // state
  9209. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:stateCode] mutableCopy];
  9210. // NSDictionary *tmpDic = @{
  9211. // @"value" : @"Other",
  9212. // @"value_id" : @"",
  9213. // @"check" : [NSNumber numberWithInteger:0]
  9214. // };
  9215. //
  9216. // for (int i = 0; i < allState.allKeys.count; i++) {
  9217. // NSString *key = [NSString stringWithFormat:@"val_%d",i];
  9218. //
  9219. // NSDictionary *tmp = allState[key];
  9220. // [allState setValue:tmpDic forKey:key];
  9221. // tmpDic = tmp;
  9222. // }
  9223. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  9224. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  9225. [ret setValue:new_section_0 forKey:@"section_0"];
  9226. return [RAConvertor dict2data:ret];
  9227. }
  9228. #pragma mark save addr
  9229. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  9230. {
  9231. // NSString *companyName = [self valueInParams:params key:@"company"];
  9232. // NSString *addr1 = [self valueInParams:params key:@"address"];
  9233. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  9234. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  9235. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  9236. // NSString *countryId = [self valueInParams:params key:@"country"];
  9237. // NSString *stateCode = [self valueInParams:params key:@"state"];
  9238. // NSString *city = [self valueInParams:params key:@"city"];
  9239. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  9240. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  9241. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  9242. // NSString *phone = [self valueInParams:params key:@"phone"];
  9243. // NSString *fax = [self valueInParams:params key:@"fax"];
  9244. // NSString *email = [self valueInParams:params key:@"email"];
  9245. return [self offline_saveContact:params update:NO isCustomer:NO];
  9246. }
  9247. #pragma mark cancel order
  9248. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  9249. {
  9250. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9251. NSString *order_id = [self valueInParams:params key:@"orderId"];
  9252. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  9253. if (order_id.length) {
  9254. sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  9255. }
  9256. int ret = [iSalesDB execSql:sql];
  9257. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9258. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9259. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9260. // [dic setValue:@"160409" forKey:@"min_ver"];
  9261. return [RAConvertor dict2data:dic];
  9262. }
  9263. #pragma mark sign order
  9264. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  9265. {
  9266. //参考 offline_saveBusinesscard
  9267. DebugLog(@"sign order params: %@",params);
  9268. // orderCode = MOB1608240002;
  9269. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  9270. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  9271. orderCode = [self translateSingleQuote:orderCode];
  9272. NSString *picPath = [self valueInParams:params key:@"picpath"];
  9273. picPath = [self translateSingleQuote:picPath];
  9274. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  9275. int ret = [iSalesDB execSql:sql];
  9276. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9277. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9278. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9279. // [dic setValue:@"160409" forKey:@"min_ver"];
  9280. return [RAConvertor dict2data:dic];
  9281. }
  9282. #pragma mark save order
  9283. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  9284. NSString *ret = [self valueInParams:params key:key];
  9285. if (translate) {
  9286. ret = [self translateSingleQuote:ret];
  9287. }
  9288. return ret;
  9289. }
  9290. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  9291. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  9292. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  9293. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  9294. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  9295. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  9296. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  9297. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  9298. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  9299. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  9300. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  9301. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  9302. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  9303. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  9304. if (![total_price isEqualToString:@""]) {
  9305. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  9306. } else {
  9307. total_price = @"";
  9308. }
  9309. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  9310. if ([paymentsAndCredits isEqualToString:@""]) {
  9311. paymentsAndCredits = @"";
  9312. } else {
  9313. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  9314. }
  9315. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  9316. if ([handling_fee_value isEqualToString:@""]) {
  9317. handling_fee_value = @"";
  9318. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  9319. handling_fee_value = @"";
  9320. } else {
  9321. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  9322. }
  9323. NSString *handling_fee_placeholder = handling_fee_value;
  9324. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  9325. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  9326. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  9327. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  9328. NSString *lift_gate_placeholder = @"";
  9329. if ([lift_gate_value isEqualToString:@""]) {
  9330. lift_gate_placeholder = @"";
  9331. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  9332. lift_gate_placeholder = @"";
  9333. } else {
  9334. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  9335. }
  9336. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  9337. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  9338. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  9339. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  9340. 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];
  9341. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  9342. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  9343. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  9344. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  9345. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  9346. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  9347. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  9348. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  9349. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  9350. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  9351. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  9352. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  9353. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  9354. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  9355. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  9356. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  9357. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  9358. BOOL number_nil = [credit_card_number isEqualToString:@""];
  9359. if (!number_nil) {
  9360. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  9361. }
  9362. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  9363. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  9364. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  9365. if (!security_code_nil) {
  9366. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  9367. }
  9368. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  9369. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  9370. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  9371. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  9372. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  9373. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  9374. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  9375. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  9376. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  9377. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  9378. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  9379. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  9380. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  9381. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  9382. NSString *contact_id = customer_cid;
  9383. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  9384. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  9385. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  9386. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  9387. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  9388. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  9389. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  9390. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  9391. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  9392. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  9393. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  9394. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  9395. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  9396. //
  9397. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  9398. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  9399. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  9400. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  9401. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  9402. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  9403. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  9404. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  9405. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  9406. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  9407. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  9408. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  9409. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  9410. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  9411. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  9412. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  9413. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  9414. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  9415. 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];
  9416. // Place Order Setting,对于Disable的Section保存时不做处理,即没有对应的参数时对应的column就不再重置为null
  9417. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9418. NSString *cachefolder = [paths objectAtIndex:0];
  9419. NSString *ui_path = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  9420. NSData *ui_data = [NSData dataWithContentsOfFile:ui_path];;
  9421. NSError *error=nil;
  9422. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:ui_data options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  9423. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  9424. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  9425. if (receive_cid.length) {
  9426. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  9427. } else {
  9428. receive_cid = [NSString stringWithFormat:@"receive_cid = null,"];
  9429. }
  9430. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  9431. if (receive_name.length) {
  9432. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  9433. } else {
  9434. receive_name = [NSString stringWithFormat:@"receive_name = null,"];
  9435. }
  9436. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  9437. if (receive_ext.length) {
  9438. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  9439. } else {
  9440. receive_ext = [NSString stringWithFormat:@"receive_ext = null,"];
  9441. }
  9442. NSString *receive_addr1 = [self valueInParams:param key:@"receive_address1" translateSingleQuote:YES];
  9443. if (receive_addr1.length) {
  9444. receive_addr1 = [NSString stringWithFormat:@"receive_address1 = '%@',",receive_addr1];
  9445. } else {
  9446. receive_addr1 = [NSString stringWithFormat:@"receive_address1 = null,"];
  9447. }
  9448. NSString *receive_addr2 = [self valueInParams:param key:@"receive_address2" translateSingleQuote:YES];
  9449. if (receive_addr2.length) {
  9450. receive_addr2 = [NSString stringWithFormat:@"receive_address2 = '%@',",receive_addr2];
  9451. } else {
  9452. receive_addr2 = [NSString stringWithFormat:@"receive_address2 = null,"];
  9453. }
  9454. NSString *receive_city = [self valueInParams:param key:@"receive_city" translateSingleQuote:YES];
  9455. if (receive_city.length) {
  9456. receive_city = [NSString stringWithFormat:@"receive_city = '%@',",receive_city];
  9457. } else {
  9458. receive_city = [NSString stringWithFormat:@"receive_city = null,"];
  9459. }
  9460. NSString *receive_state = [self valueInParams:param key:@"receive_state" translateSingleQuote:YES];
  9461. if (receive_state.length) {
  9462. receive_state = [NSString stringWithFormat:@"receive_state = '%@',",receive_state];
  9463. } else {
  9464. receive_state = [NSString stringWithFormat:@"receive_state = null,"];
  9465. }
  9466. NSString *receive_zipcode = [self valueInParams:param key:@"receive_zipcode" translateSingleQuote:YES];
  9467. if (receive_zipcode.length) {
  9468. receive_zipcode = [NSString stringWithFormat:@"receive_zipcode = '%@',",receive_zipcode];
  9469. } else {
  9470. receive_zipcode = [NSString stringWithFormat:@"receive_zipcode = null,"];
  9471. }
  9472. NSString *receive_country = [self valueInParams:param key:@"receive_country" translateSingleQuote:YES];
  9473. if (receive_country.length) {
  9474. receive_country = [NSString stringWithFormat:@"receive_country = '%@',",receive_country];
  9475. } else {
  9476. receive_country = [NSString stringWithFormat:@"receive_country = null,"];
  9477. }
  9478. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  9479. if (receive_contact.length) {
  9480. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  9481. } else {
  9482. receive_contact = [NSString stringWithFormat:@"receive_contact = null,"];
  9483. }
  9484. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  9485. if (receive_phone.length) {
  9486. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  9487. } else {
  9488. receive_phone = [NSString stringWithFormat:@"receive_phone = null,"];
  9489. }
  9490. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  9491. if (receive_email.length) {
  9492. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  9493. } else {
  9494. receive_email = [NSString stringWithFormat:@"receive_email = null,"];
  9495. }
  9496. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  9497. if (receive_fax.length) {
  9498. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  9499. } else {
  9500. receive_fax = [NSString stringWithFormat:@"receive_fax = null,"];
  9501. }
  9502. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  9503. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  9504. if (sender_cid.length) {
  9505. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  9506. } else {
  9507. if (![shipFromDisable integerValue]) {
  9508. sender_cid = [NSString stringWithFormat:@"sender_cid = null,"];
  9509. }
  9510. }
  9511. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  9512. if (sender_name.length) {
  9513. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  9514. } else {
  9515. if (![shipFromDisable integerValue]) {
  9516. sender_name = [NSString stringWithFormat:@"sender_name = null,"];
  9517. }
  9518. }
  9519. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  9520. if (sender_ext.length) {
  9521. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  9522. } else {
  9523. if (![shipFromDisable integerValue]) {
  9524. sender_ext = [NSString stringWithFormat:@"sender_ext = null,"];
  9525. }
  9526. }
  9527. NSString *sender_addr1 = [self valueInParams:param key:@"sender_address1" translateSingleQuote:YES];
  9528. if (sender_addr1.length) {
  9529. sender_addr1 = [NSString stringWithFormat:@"sender_address1 = '%@',",sender_addr1];
  9530. } else {
  9531. if (![shipFromDisable integerValue]) {
  9532. sender_addr1 = [NSString stringWithFormat:@"sender_address1 = null,"];
  9533. }
  9534. }
  9535. NSString *sender_addr2 = [self valueInParams:param key:@"sender_address2" translateSingleQuote:YES];
  9536. if (sender_addr2.length) {
  9537. sender_addr2 = [NSString stringWithFormat:@"sender_address2 = '%@',",sender_addr2];
  9538. } else {
  9539. if (![shipFromDisable integerValue]) {
  9540. sender_addr2 = [NSString stringWithFormat:@"sender_address2 = null,"];
  9541. }
  9542. }
  9543. NSString *sender_city = [self valueInParams:param key:@"sender_city" translateSingleQuote:YES];
  9544. if (sender_city.length) {
  9545. sender_city = [NSString stringWithFormat:@"sender_city = '%@',",sender_city];
  9546. } else {
  9547. if (![shipFromDisable integerValue]) {
  9548. sender_city = [NSString stringWithFormat:@"sender_city = null,"];
  9549. }
  9550. }
  9551. NSString *sender_state = [self valueInParams:param key:@"sender_state" translateSingleQuote:YES];
  9552. if (sender_state.length) {
  9553. sender_state = [NSString stringWithFormat:@"sender_state = '%@',",sender_state];
  9554. } else {
  9555. if (![shipFromDisable integerValue]) {
  9556. sender_state = [NSString stringWithFormat:@"sender_state = null,"];
  9557. }
  9558. }
  9559. NSString *sender_zipcode = [self valueInParams:param key:@"sender_zipcode" translateSingleQuote:YES];
  9560. if (sender_zipcode.length) {
  9561. sender_zipcode = [NSString stringWithFormat:@"sender_zipcode = '%@',",sender_zipcode];
  9562. } else {
  9563. if (![shipFromDisable integerValue]) {
  9564. sender_zipcode = [NSString stringWithFormat:@"sender_zipcode = null,"];
  9565. }
  9566. }
  9567. NSString *sender_country = [self valueInParams:param key:@"sender_country" translateSingleQuote:YES];
  9568. if (sender_country.length) {
  9569. sender_country = [NSString stringWithFormat:@"sender_country = '%@',",sender_country];
  9570. } else {
  9571. if (![shipFromDisable integerValue]) {
  9572. sender_country = [NSString stringWithFormat:@"sender_country = null,"];
  9573. }
  9574. }
  9575. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  9576. if(sender_contact.length) {
  9577. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  9578. } else {
  9579. if (![shipFromDisable integerValue]) {
  9580. sender_contact = [NSString stringWithFormat:@"sender_contact = null,"];
  9581. }
  9582. }
  9583. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  9584. if (sender_phone.length) {
  9585. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  9586. } else {
  9587. if (![shipFromDisable integerValue]) {
  9588. sender_phone = [NSString stringWithFormat:@"sender_phone = null,"];
  9589. }
  9590. }
  9591. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  9592. if (sender_fax.length) {
  9593. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  9594. } else {
  9595. if (![shipFromDisable integerValue]) {
  9596. sender_fax = [NSString stringWithFormat:@"sender_fax = null,"];
  9597. }
  9598. }
  9599. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  9600. if (sender_email.length) {
  9601. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  9602. } else {
  9603. if (![shipFromDisable integerValue]) {
  9604. sender_email = [NSString stringWithFormat:@"sender_email = null,"];
  9605. }
  9606. }
  9607. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  9608. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  9609. if (shipping_billto_cid.length) {
  9610. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  9611. } else {
  9612. if (![freightBillToDisable integerValue]) {
  9613. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = null,"];
  9614. }
  9615. }
  9616. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  9617. if (shipping_billto_name.length) {
  9618. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  9619. } else {
  9620. if (![freightBillToDisable integerValue]) {
  9621. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = null,"];
  9622. }
  9623. }
  9624. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  9625. if (shipping_billto_ext.length) {
  9626. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  9627. } else {
  9628. if (![freightBillToDisable integerValue]) {
  9629. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = null,"];
  9630. }
  9631. }
  9632. NSString *shipping_billto_addr1 = [self valueInParams:param key:@"shipping_billto_address1" translateSingleQuote:YES];
  9633. if (shipping_billto_addr1.length) {
  9634. shipping_billto_addr1 = [NSString stringWithFormat:@"shipping_billto_address1 = '%@',",shipping_billto_addr1];
  9635. } else {
  9636. if (![freightBillToDisable integerValue]) {
  9637. shipping_billto_addr1 = [NSString stringWithFormat:@"shipping_billto_address1 = null,"];
  9638. }
  9639. }
  9640. NSString *shipping_billto_addr2 = [self valueInParams:param key:@"shipping_billto_address2" translateSingleQuote:YES];
  9641. if (shipping_billto_addr2.length) {
  9642. shipping_billto_addr2 = [NSString stringWithFormat:@"shipping_billto_address2 = '%@',",shipping_billto_addr2];
  9643. } else {
  9644. if (![freightBillToDisable integerValue]) {
  9645. shipping_billto_addr2 = [NSString stringWithFormat:@"shipping_billto_address2 = null,"];
  9646. }
  9647. }
  9648. NSString *shipping_billto_city = [self valueInParams:param key:@"shipping_billto_city" translateSingleQuote:YES];
  9649. if (shipping_billto_city.length) {
  9650. shipping_billto_city = [NSString stringWithFormat:@"shipping_billto_city = '%@',",shipping_billto_city];
  9651. } else {
  9652. if (![freightBillToDisable integerValue]) {
  9653. shipping_billto_city = [NSString stringWithFormat:@"shipping_billto_city = null,"];
  9654. }
  9655. }
  9656. NSString *shipping_billto_state = [self valueInParams:param key:@"shipping_billto_state" translateSingleQuote:YES];
  9657. if (shipping_billto_state.length) {
  9658. shipping_billto_state = [NSString stringWithFormat:@"shipping_billto_state = '%@',",shipping_billto_state];
  9659. } else {
  9660. if (![freightBillToDisable integerValue]) {
  9661. shipping_billto_state = [NSString stringWithFormat:@"shipping_billto_state = null,"];
  9662. }
  9663. }
  9664. NSString *shipping_billto_zipcode = [self valueInParams:param key:@"shipping_billto_zipcode" translateSingleQuote:YES];
  9665. if (shipping_billto_zipcode.length) {
  9666. shipping_billto_zipcode = [NSString stringWithFormat:@"shipping_billto_zipcode = '%@',",shipping_billto_zipcode];
  9667. } else {
  9668. if (![freightBillToDisable integerValue]) {
  9669. shipping_billto_zipcode = [NSString stringWithFormat:@"shipping_billto_zipcode = null,"];
  9670. }
  9671. }
  9672. NSString *shipping_billto_country = [self valueInParams:param key:@"shipping_billto_country" translateSingleQuote:YES];
  9673. if (shipping_billto_country.length) {
  9674. shipping_billto_country = [NSString stringWithFormat:@"shipping_billto_country = '%@',",shipping_billto_country];
  9675. } else {
  9676. if (![freightBillToDisable integerValue]) {
  9677. shipping_billto_country = [NSString stringWithFormat:@"shipping_billto_country = null,"];
  9678. }
  9679. }
  9680. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  9681. if (shipping_billto_contact.length) {
  9682. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  9683. } else {
  9684. if (![freightBillToDisable integerValue]) {
  9685. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = null,"];
  9686. }
  9687. }
  9688. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  9689. if (shipping_billto_phone.length) {
  9690. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  9691. } else {
  9692. if (![freightBillToDisable integerValue]) {
  9693. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = null,"];
  9694. }
  9695. }
  9696. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  9697. if (shipping_billto_fax.length) {
  9698. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  9699. } else {
  9700. if (![freightBillToDisable integerValue]) {
  9701. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = null,"];
  9702. }
  9703. }
  9704. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  9705. if (shipping_billto_email.length) {
  9706. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  9707. } else {
  9708. if (![freightBillToDisable integerValue]) {
  9709. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = null,"];
  9710. }
  9711. }
  9712. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  9713. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  9714. if (billing_cid.length) {
  9715. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  9716. } else {
  9717. if (![merchandiseBillToDisable integerValue]) {
  9718. billing_cid = [NSString stringWithFormat:@"billing_cid = null,"];
  9719. }
  9720. }
  9721. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  9722. if (billing_name.length) {
  9723. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  9724. } else {
  9725. if (![merchandiseBillToDisable integerValue]) {
  9726. billing_name = [NSString stringWithFormat:@"billing_name = null,"];
  9727. }
  9728. }
  9729. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  9730. if (billing_ext.length) {
  9731. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  9732. } else {
  9733. if (![merchandiseBillToDisable integerValue]) {
  9734. billing_ext = [NSString stringWithFormat:@"billing_ext = null,"];
  9735. }
  9736. }
  9737. NSString *billing_addr1 = [self valueInParams:param key:@"billing_address1" translateSingleQuote:YES];
  9738. if (billing_addr1.length) {
  9739. billing_addr1 = [NSString stringWithFormat:@"billing_address1 = '%@',",billing_addr1];
  9740. } else {
  9741. if (![merchandiseBillToDisable integerValue]) {
  9742. billing_addr1 = [NSString stringWithFormat:@"billing_address1 = null,"];
  9743. }
  9744. }
  9745. NSString *billing_addr2 = [self valueInParams:param key:@"billing_address2" translateSingleQuote:YES];
  9746. if (billing_addr2.length) {
  9747. billing_addr2 = [NSString stringWithFormat:@"billing_address2 = '%@',",billing_addr2];
  9748. } else {
  9749. billing_addr2 = [NSString stringWithFormat:@"billing_address2 = null,"];
  9750. }
  9751. NSString *billing_city = [self valueInParams:param key:@"billing_city" translateSingleQuote:YES];
  9752. if (billing_city.length) {
  9753. billing_city = [NSString stringWithFormat:@"billing_city = '%@',",billing_city];
  9754. } else {
  9755. if (![merchandiseBillToDisable integerValue]) {
  9756. billing_city = [NSString stringWithFormat:@"billing_city = null,"];
  9757. }
  9758. }
  9759. NSString *billing_state = [self valueInParams:param key:@"billing_state" translateSingleQuote:YES];
  9760. if (billing_state.length) {
  9761. billing_state = [NSString stringWithFormat:@"billing_state = '%@',",billing_state];
  9762. } else {
  9763. if (![merchandiseBillToDisable integerValue]) {
  9764. billing_state = [NSString stringWithFormat:@"billing_state = null,"];
  9765. }
  9766. }
  9767. NSString *billing_zipcode = [self valueInParams:param key:@"billing_zipcode" translateSingleQuote:YES];
  9768. if (billing_zipcode.length) {
  9769. billing_zipcode = [NSString stringWithFormat:@"billing_zipcode = '%@',",billing_zipcode];
  9770. } else {
  9771. if (![merchandiseBillToDisable integerValue]) {
  9772. billing_zipcode = [NSString stringWithFormat:@"billing_zipcode = null,"];
  9773. }
  9774. }
  9775. NSString *billing_country = [self valueInParams:param key:@"billing_country" translateSingleQuote:YES];
  9776. if (billing_country.length) {
  9777. billing_country = [NSString stringWithFormat:@"billing_country = '%@',",billing_country];
  9778. } else {
  9779. if (![merchandiseBillToDisable integerValue]) {
  9780. billing_country = [NSString stringWithFormat:@"billing_country = null,"];
  9781. }
  9782. }
  9783. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  9784. if (billing_contact.length) {
  9785. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  9786. } else {
  9787. if (![merchandiseBillToDisable integerValue]) {
  9788. billing_contact = [NSString stringWithFormat:@"billing_contact = null,"];
  9789. }
  9790. }
  9791. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  9792. if (billing_phone.length) {
  9793. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  9794. } else {
  9795. if (![merchandiseBillToDisable integerValue]) {
  9796. billing_phone = [NSString stringWithFormat:@"billing_phone = null,"];
  9797. }
  9798. }
  9799. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  9800. if (billing_fax.length) {
  9801. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  9802. } else {
  9803. if (![merchandiseBillToDisable integerValue]) {
  9804. billing_fax = [NSString stringWithFormat:@"billing_fax = null,"];
  9805. }
  9806. }
  9807. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  9808. if (billing_email.length) {
  9809. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  9810. } else {
  9811. if (![merchandiseBillToDisable integerValue]) {
  9812. billing_email = [NSString stringWithFormat:@"billing_email = null,"];
  9813. }
  9814. }
  9815. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  9816. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  9817. if (returnto_cid.length) {
  9818. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  9819. } else {
  9820. if (![returnToDisable integerValue]) {
  9821. returnto_cid = [NSString stringWithFormat:@"returnto_cid = null,"];
  9822. }
  9823. }
  9824. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  9825. if (returnto_name.length) {
  9826. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  9827. } else {
  9828. if (![returnToDisable integerValue]) {
  9829. returnto_name = [NSString stringWithFormat:@"returnto_name = null,"];
  9830. }
  9831. }
  9832. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  9833. if (returnto_ext.length) {
  9834. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  9835. } else {
  9836. if (![returnToDisable integerValue]) {
  9837. returnto_ext = [NSString stringWithFormat:@"returnto_ext = null,"];
  9838. }
  9839. }
  9840. NSString *returnto_addr1 = [self valueInParams:param key:@"returnto_address1" translateSingleQuote:YES];
  9841. if (returnto_addr1.length) {
  9842. returnto_addr1 = [NSString stringWithFormat:@"returnto_address1 = '%@',",returnto_addr1];
  9843. } else {
  9844. if (![returnToDisable integerValue]) {
  9845. returnto_addr1 = [NSString stringWithFormat:@"returnto_address1 = null,"];
  9846. }
  9847. }
  9848. NSString *returnto_addr2 = [self valueInParams:param key:@"returnto_address2" translateSingleQuote:YES];
  9849. if (returnto_addr2.length) {
  9850. returnto_addr2 = [NSString stringWithFormat:@"returnto_address2 = '%@',",returnto_addr2];
  9851. } else {
  9852. if (![returnToDisable integerValue]) {
  9853. returnto_addr2 = [NSString stringWithFormat:@"returnto_address2 = null,"];
  9854. }
  9855. }
  9856. NSString *returnto_city = [self valueInParams:param key:@"returnto_city" translateSingleQuote:YES];
  9857. if (returnto_city.length) {
  9858. returnto_city = [NSString stringWithFormat:@"returnto_city = '%@',",returnto_city];
  9859. } else {
  9860. if (![returnToDisable integerValue]) {
  9861. returnto_city = [NSString stringWithFormat:@"returnto_city = null,"];
  9862. }
  9863. }
  9864. NSString *returnto_state = [self valueInParams:param key:@"returnto_state" translateSingleQuote:YES];
  9865. if (returnto_state.length) {
  9866. returnto_state = [NSString stringWithFormat:@"returnto_state = '%@',",returnto_state];
  9867. } else {
  9868. if (![returnToDisable integerValue]) {
  9869. returnto_state = [NSString stringWithFormat:@"returnto_state = null,"];
  9870. }
  9871. }
  9872. NSString *returnto_zipcode = [self valueInParams:param key:@"returnto_zipcode" translateSingleQuote:YES];
  9873. if (returnto_zipcode.length) {
  9874. returnto_zipcode = [NSString stringWithFormat:@"returnto_zipcode = '%@',",returnto_zipcode];
  9875. } else {
  9876. if (![returnToDisable integerValue]) {
  9877. returnto_zipcode = [NSString stringWithFormat:@"returnto_zipcode = null,"];
  9878. }
  9879. }
  9880. NSString *returnto_country = [self valueInParams:param key:@"returnto_country" translateSingleQuote:YES];
  9881. if (returnto_country.length) {
  9882. returnto_country = [NSString stringWithFormat:@"returnto_country = '%@',",returnto_country];
  9883. } else {
  9884. if (![returnToDisable integerValue]) {
  9885. returnto_country = [NSString stringWithFormat:@"returnto_country = null,"];
  9886. }
  9887. }
  9888. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  9889. if (returnto_contact.length) {
  9890. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  9891. } else {
  9892. if (![returnToDisable integerValue]) {
  9893. returnto_contact = [NSString stringWithFormat:@"returnto_contact = null,"];
  9894. }
  9895. }
  9896. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  9897. if (returnto_phone.length) {
  9898. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  9899. } else {
  9900. if (![returnToDisable integerValue]) {
  9901. returnto_phone = [NSString stringWithFormat:@"returnto_phone = null,"];
  9902. }
  9903. }
  9904. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  9905. if (returnto_fax.length) {
  9906. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  9907. } else {
  9908. if (![returnToDisable integerValue]) {
  9909. returnto_fax = [NSString stringWithFormat:@"returnto_fax = null,"];
  9910. }
  9911. }
  9912. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  9913. if (returnto_email.length) {
  9914. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  9915. } else {
  9916. if (![returnToDisable integerValue]) {
  9917. returnto_email = [NSString stringWithFormat:@"returnto_email = null,"];
  9918. }
  9919. }
  9920. NSString *order_status = @"status = 1,";
  9921. if (submit) {
  9922. order_status = @"status = -11,";
  9923. }
  9924. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  9925. NSString *sync_sql = @"";
  9926. if (submit) {
  9927. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  9928. [param setValue:sales_rep forKey:@"sales_rep"];
  9929. NSString *sync_data = [self translateSingleQuote:[RAConvertor dict2string:param]];
  9930. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  9931. }
  9932. NSString *orderSql = [NSString stringWithFormat:@"update offline_order set %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ so_id = '%@' %@ where so_id = '%@';",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,receive_addr1,receive_addr2,receive_city,receive_state,receive_zipcode,receive_country,sender_addr1,sender_addr2,sender_city,sender_state,sender_zipcode,sender_country,shipping_billto_addr1,shipping_billto_addr2,shipping_billto_city,shipping_billto_state,shipping_billto_zipcode,shipping_billto_country,billing_addr1,billing_addr2,billing_city,billing_state,billing_zipcode,billing_country,returnto_addr1,returnto_addr2,returnto_city,returnto_state,returnto_zipcode,returnto_country,so_id,sync_sql,so_id];
  9933. DebugLog(@"save order contactSql: %@",contactSql);
  9934. DebugLog(@"save order orderSql: %@",orderSql);
  9935. // int contact_ret = [iSalesDB execSql:contactSql];
  9936. int order_ret = [iSalesDB execSql:orderSql];
  9937. int ret = order_ret;
  9938. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9939. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  9940. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9941. // [dic setValue:@"160409" forKey:@"min_ver"];
  9942. [dic setObject:so_id forKey:@"so#"];
  9943. return [RAConvertor dict2data:dic];
  9944. }
  9945. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  9946. {
  9947. // id foo = nil;
  9948. // NSMutableArray *a = @[].mutableCopy;
  9949. // [a addObject:foo];
  9950. return [self saveorder:param submit:NO];
  9951. }
  9952. #pragma mark add to cart by name
  9953. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  9954. {
  9955. NSString *orderCode = [params objectForKey:@"orderCode"];
  9956. NSString *product_name = [params objectForKey:@"product_name"];
  9957. product_name = [self translateSingleQuote:product_name];
  9958. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  9959. sqlite3 *db = [iSalesDB get_db];
  9960. __block NSMutableString *product_id_string = [NSMutableString string];
  9961. for (int i = 0; i < product_name_array.count; i++) {
  9962. NSString *name = [product_name_array objectAtIndex:i];
  9963. NSString *sql = [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  9964. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9965. int product_id = sqlite3_column_int(stmt, 0);
  9966. if (i == product_name_array.count - 1) {
  9967. [product_id_string appendFormat:@"%d",product_id];
  9968. } else {
  9969. [product_id_string appendFormat:@"%d,",product_id];
  9970. }
  9971. }];
  9972. }
  9973. NSDictionary *newParams = @{
  9974. @"product_id" : product_id_string,
  9975. @"orderCode" : orderCode
  9976. };
  9977. [iSalesDB close_db:db];
  9978. return [self offline_add2cart:[newParams mutableCopy]];
  9979. }
  9980. #pragma mark reset order
  9981. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  9982. {
  9983. UIApplication * app = [UIApplication sharedApplication];
  9984. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  9985. appDelegate.disable_trigger=true;
  9986. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  9987. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  9988. appDelegate.disable_trigger=false;
  9989. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9990. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  9991. return [RAConvertor dict2data:dic];
  9992. }
  9993. #pragma mark submit order
  9994. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  9995. {
  9996. return [self saveorder:params submit:YES];
  9997. }
  9998. #pragma mark copy order
  9999. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  10000. {
  10001. NSMutableDictionary *ret = @{}.mutableCopy;
  10002. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  10003. sqlite3 *db = [iSalesDB get_db];
  10004. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  10005. // 首先查看联系人是否active
  10006. 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];
  10007. __block int customer_is_active = 1;
  10008. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10009. customer_is_active = sqlite3_column_int(stmt, 0);
  10010. }];
  10011. if (!customer_is_active) {
  10012. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10013. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10014. [iSalesDB close_db:db];
  10015. return [RAConvertor dict2data:ret];
  10016. }
  10017. // new order
  10018. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  10019. NSString *new_order_code = [self get_offline_soid:db];
  10020. NSString *user = ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  10021. user = [self translateSingleQuote:user];
  10022. 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,receive_address1,receive_address2,receive_city,receive_state,receive_zipcode,receive_country,sender_address1,sender_address2,sender_city,sender_state,sender_zipcode,sender_country,shipping_billto_address1,shipping_billto_address2,shipping_billto_city,shipping_billto_state,shipping_billto_zipcode,shipping_billto_country,billing_address1,billing_address2,billing_city,billing_state,billing_zipcode,billing_country,returnto_address1,returnto_address2,returnto_city,returnto_state,returnto_zipcode,returnto_country) 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,receive_address1,receive_address2,receive_city,receive_state,receive_zipcode,receive_country,sender_address1,sender_address2,sender_city,sender_state,sender_zipcode,sender_country,shipping_billto_address1,shipping_billto_address2,shipping_billto_city,shipping_billto_state,shipping_billto_zipcode,shipping_billto_country,billing_address1,billing_address2,billing_city,billing_state,billing_zipcode,billing_country,returnto_address1,returnto_address2,returnto_city,returnto_state,returnto_zipcode,returnto_country from offline_order where so_id = '%@';",new_order_code,user,order_code]; // ,paymentsAndCredits,shipping,lift_gate_value,handling_fee_value
  10023. __block int result = 1;
  10024. result = [iSalesDB execSql:insert_order_sql db:db];
  10025. if (!result) {
  10026. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10027. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10028. [iSalesDB close_db:db];
  10029. return [RAConvertor dict2data:ret];
  10030. }
  10031. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  10032. // __block NSString *product_id = @"";
  10033. __weak typeof(self) weakSelf = self;
  10034. 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];
  10035. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10036. int is_active = sqlite3_column_int(stmt, 1);
  10037. if (is_active) {
  10038. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  10039. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  10040. 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];
  10041. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  10042. }
  10043. }];
  10044. // product_id = [product_id substringFromIndex:1];
  10045. //
  10046. // [self offline_add2cart:@{}.mutableCopy];
  10047. [iSalesDB execSql:@"END TRANSACTION" db:db];
  10048. [iSalesDB close_db:db];
  10049. if (result) {
  10050. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10051. } else {
  10052. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10053. }
  10054. [ret setObject:new_order_code forKey:@"so_id"];
  10055. return [RAConvertor dict2data:ret];
  10056. }
  10057. #pragma mark move wish list to cart
  10058. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  10059. // cart中存在的Model在wish list move to cart之后,cart中Model数量为1
  10060. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10061. NSString *collectId = params[@"collectId"];
  10062. 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];
  10063. __block NSString *product_id = @"";
  10064. __block NSString *qty = @"";
  10065. __block int number_of_outOfStock = 0;
  10066. __block NSMutableArray *delete_collectId = [NSMutableArray array];
  10067. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10068. int productId = sqlite3_column_int(stmt, 0);
  10069. int item_qty = sqlite3_column_int(stmt, 1);
  10070. int availability = sqlite3_column_int(stmt, 2);
  10071. int _id = sqlite3_column_int(stmt, 3);
  10072. if (!appDelegate.can_create_backorder) {
  10073. // 库存小于购买量为缺货
  10074. if (availability >= item_qty) {
  10075. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  10076. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  10077. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  10078. } else {
  10079. number_of_outOfStock++;
  10080. }
  10081. } else {
  10082. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  10083. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  10084. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  10085. }
  10086. }];
  10087. NSMutableDictionary *retDic = nil;
  10088. if (!appDelegate.can_create_backorder) {
  10089. if (delete_collectId.count == 0) {
  10090. retDic = [NSMutableDictionary dictionary];
  10091. retDic[@"result"] = [NSNumber numberWithInteger:8];
  10092. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  10093. return [RAConvertor dict2data:retDic];
  10094. }
  10095. }
  10096. if (product_id.length > 1) {
  10097. product_id = [product_id substringFromIndex:1];
  10098. }
  10099. if (qty.length > 1) {
  10100. qty = [qty substringFromIndex:1];
  10101. }
  10102. NSDictionary *newParams = @{
  10103. @"product_id" : product_id,
  10104. @"orderCode" : params[@"orderCode"],
  10105. @"qty" : qty
  10106. };
  10107. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  10108. retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  10109. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  10110. NSInteger ret = [[[self offline_deletewishlist:@{@"collectId" : [delete_collectId componentsJoinedByString:@","]}.mutableCopy] valueForKey:@"result"] integerValue];
  10111. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  10112. }
  10113. if (!appDelegate.can_create_backorder) {
  10114. if (number_of_outOfStock > 0) {
  10115. retDic[@"result"] = [NSNumber numberWithInteger:8];
  10116. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  10117. }
  10118. }
  10119. return [RAConvertor dict2data:retDic];
  10120. }
  10121. #pragma mark - portfolio
  10122. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  10123. int sort = [[params valueForKey:@"sort"] intValue];
  10124. int offset = [[params valueForKey:@"offset"] intValue];
  10125. int limit = [[params valueForKey:@"limit"] intValue];
  10126. NSString *orderBy = @"";
  10127. switch (sort) {
  10128. case 0:{
  10129. orderBy = @"p.modify_time desc";
  10130. }
  10131. break;
  10132. case 1:{
  10133. orderBy = @"modify_time asc";
  10134. }
  10135. break;
  10136. case 2:{
  10137. orderBy = @"p.name asc";
  10138. }
  10139. break;
  10140. case 3:{
  10141. orderBy = @"p.name desc";
  10142. }
  10143. break;
  10144. case 4:{
  10145. orderBy = @"p.description asc";
  10146. }
  10147. break;
  10148. case 5: {
  10149. orderBy = @"m.default_category asc";
  10150. }
  10151. default:
  10152. break;
  10153. }
  10154. // 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];
  10155. 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];
  10156. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10157. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10158. sqlite3 *db = [iSalesDB get_db];
  10159. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10160. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10161. int product_id = sqlite3_column_int(stmt, 0);
  10162. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  10163. NSString *name = [self textAtColumn:1 statement:stmt];
  10164. NSString *description = [self textAtColumn:2 statement:stmt];
  10165. double price = sqlite3_column_double(stmt, 3);
  10166. double discount = sqlite3_column_double(stmt,4);
  10167. int qty = sqlite3_column_int(stmt, 5);
  10168. int percentage = sqlite3_column_int(stmt, 6);
  10169. int item_id = sqlite3_column_int(stmt, 7);
  10170. // int fashion_id = sqlite3_column_int(stmt, 8);
  10171. NSString *img_path = [self textAtColumn:9 statement:stmt];
  10172. NSString *line_note = [self textAtColumn:10 statement:stmt];
  10173. double percent = sqlite3_column_double(stmt, 11);
  10174. NSString *price_null = [self textAtColumn:3 statement:stmt];
  10175. if ([price_null isEqualToString:@"null"]) {
  10176. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  10177. }
  10178. NSMutableDictionary *item = @{
  10179. @"linenotes": line_note,
  10180. @"check": @(1),
  10181. @"product_id": product_id_string,
  10182. @"available_qty": @(qty),
  10183. @"available_percent" : @(percent),
  10184. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  10185. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  10186. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  10187. @"img": img_path,
  10188. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  10189. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  10190. }.mutableCopy;
  10191. if (percentage) {
  10192. [item removeObjectForKey:@"available_qty"];
  10193. } else {
  10194. [item removeObjectForKey:@"available_percent"];
  10195. }
  10196. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  10197. if ([qty_null isEqualToString:@"null"]) {
  10198. [item removeObjectForKey:@"available_qty"];
  10199. }
  10200. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10201. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10202. }];
  10203. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  10204. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10205. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  10206. }
  10207. [iSalesDB close_db:db];
  10208. [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  10209. [dic setValue:@"" forKey:@"email_content"];
  10210. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  10211. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10212. return [RAConvertor dict2data:dic];
  10213. }
  10214. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  10215. __block int qty = 0;
  10216. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  10217. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10218. qty = sqlite3_column_int(stmt, 0);
  10219. }];
  10220. return qty;
  10221. }
  10222. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct {
  10223. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  10224. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  10225. NSMutableDictionary * values = params[@"replaceValue"];
  10226. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  10227. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  10228. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  10229. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  10230. NSString *pdf_path = @"";
  10231. if (direct) {
  10232. configureParams = [self valueInParams:params key:@"pdfUrl"];
  10233. } else {
  10234. pdf_path = [self valueInParams:params key:@"pdfPath"];
  10235. }
  10236. NSString *create_user = [self valueInParams:params key:@"user"];
  10237. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  10238. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  10239. // model info
  10240. // 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];
  10241. // V1.90 more color
  10242. 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];
  10243. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10244. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  10245. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10246. sqlite3 *db = [iSalesDB get_db];
  10247. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10248. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10249. int product_id = sqlite3_column_int(stmt, 0);
  10250. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  10251. double price = sqlite3_column_double(stmt, 1);
  10252. double discount = sqlite3_column_double(stmt,2);
  10253. int qty = sqlite3_column_int(stmt, 3);
  10254. int percentage = sqlite3_column_int(stmt, 4);
  10255. int item_id = sqlite3_column_int(stmt, 5);
  10256. NSString *line_note = [self textAtColumn:6 statement:stmt];
  10257. double percent = sqlite3_column_double(stmt, 7);
  10258. int more_color = sqlite3_column_int(stmt, 8);
  10259. NSString *price_null = [self textAtColumn:1 statement:stmt];
  10260. /* if ([price_null isEqualToString:@"null"]) {
  10261. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  10262. }
  10263. */
  10264. [product_ids_string appendFormat:@"%@,",product_id_string];
  10265. // Regular Price
  10266. int regular_price = [[params objectForKey:@"regular_price"] integerValue];
  10267. NSString *regular_price_str = [self get_portfolio_price:appDelegate.contact_id item_id:item_id price:regular_price db:db];
  10268. // QTY
  10269. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  10270. if ([qty_null isEqualToString:@"null"]) {
  10271. // 查available
  10272. qty = [self model_QTY:product_id_string db:db];
  10273. }
  10274. if (percentage) {
  10275. qty = qty * percent / 100;
  10276. }
  10277. // Special Price
  10278. if ([price_null isEqualToString:@"null"]) {
  10279. // price = regular price
  10280. price = [regular_price_str doubleValue];
  10281. }
  10282. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  10283. if (![discount_null isEqualToString:@"null"]) {
  10284. price = price * (1 - discount / 100.0);
  10285. }
  10286. NSMutableDictionary *item = @{
  10287. @"line_note": line_note,
  10288. @"product_id": product_id_string,
  10289. @"available_qty": @(qty),
  10290. @"more_color":@(more_color),
  10291. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  10292. @"regular_price" : regular_price_str,
  10293. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  10294. }.mutableCopy;
  10295. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10296. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10297. }];
  10298. [iSalesDB close_db:db];
  10299. if (product_ids_string.length > 0) {
  10300. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  10301. }
  10302. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  10303. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  10304. return [RAConvertor dict2data:resultDictionary];
  10305. }
  10306. NSString *model_info = [RAConvertor dict2string:dic];
  10307. // 创建PDF
  10308. // 在preview情况下保存,则不需要新建了
  10309. if (direct) {
  10310. NSMutableDictionary *tear_sheet_params = params;
  10311. // if (tear_sheet_id) {
  10312. // tear_sheet_params = values;
  10313. // }
  10314. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  10315. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  10316. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  10317. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  10318. resultDictionary = pdfInfo.mutableCopy;
  10319. } else { // 创建PDF失败
  10320. return pdfData;
  10321. }
  10322. } else {
  10323. // pdf_path 就是本地路径
  10324. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  10325. }
  10326. // 将文件移动到PDF Cache文件夹
  10327. NSString *newPath = [pdf_path lastPathComponent];
  10328. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10329. NSString *cachefolder = [paths objectAtIndex:0];
  10330. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10331. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  10332. NSFileManager *fileManager = [NSFileManager defaultManager];
  10333. NSError *error = nil;
  10334. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  10335. if (error) { // 移动文件失败
  10336. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  10337. return [RAConvertor dict2data:resultDictionary];
  10338. }
  10339. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  10340. pdf_path = [newPath lastPathComponent];
  10341. // 保存信息
  10342. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  10343. NSString *off_params = [RAConvertor dict2string:params];
  10344. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  10345. pdf_path = [self translateSingleQuote:pdf_path];
  10346. create_user = [self translateSingleQuote:create_user];
  10347. tear_note = [self translateSingleQuote:tear_note];
  10348. tear_name = [self translateSingleQuote:tear_name];
  10349. model_info = [self translateSingleQuote:model_info];
  10350. configureParams = [self translateSingleQuote:configureParams];
  10351. off_params = [self translateSingleQuote:off_params];
  10352. 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];
  10353. if (tear_sheet_id) {
  10354. int _id = [tear_sheet_id integerValue];
  10355. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  10356. }
  10357. int result = [iSalesDB execSql:save_pdf_sql];
  10358. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10359. //
  10360. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  10361. if (remove_Item) {
  10362. // portfolioId
  10363. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  10364. }
  10365. return [RAConvertor dict2data:resultDictionary];
  10366. }
  10367. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  10368. return [self offline_savePDF:params direct:YES];
  10369. }
  10370. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  10371. return [self offline_savePDF:params direct:NO];
  10372. }
  10373. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  10374. int offset = [[params valueForKey:@"offset"] integerValue];
  10375. int limit = [[params valueForKey:@"limit"] integerValue];
  10376. NSString *keyword = [params valueForKey:@"keyWord"];
  10377. NSString *where = @"where is_delete is null or is_delete = 0";
  10378. if (keyword.length) {
  10379. keyword = [self translateSingleQuote:keyword];
  10380. 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];
  10381. }
  10382. 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
  10383. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10384. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10385. NSString *cachefolder = [paths objectAtIndex:0];
  10386. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10387. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10388. NSString *name = [self textAtColumn:0 statement:stmt];
  10389. NSString *note = [self textAtColumn:1 statement:stmt];
  10390. NSString *time = [self textAtColumn:2 statement:stmt];
  10391. NSString *user = [self textAtColumn:3 statement:stmt];
  10392. NSString *path = [self textAtColumn:4 statement:stmt];
  10393. NSString *model_info = [self textAtColumn:6 statement:stmt];
  10394. NSString *off_params = [self textAtColumn:7 statement:stmt];
  10395. path = [pdfFolder stringByAppendingPathComponent:path];
  10396. BOOL bdir=NO;
  10397. NSFileManager* fileManager = [NSFileManager defaultManager];
  10398. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  10399. {
  10400. //pdf文件不存在
  10401. path=nil;
  10402. }
  10403. time = [self changeDateTimeFormate:time];
  10404. time = [time stringByAppendingString:@" PST"];
  10405. int sheet_id = sqlite3_column_int(stmt, 5);
  10406. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  10407. item[@"tearsheetsId"]=@(sheet_id);
  10408. item[@"pdf_path"]=path;
  10409. item[@"create_time"]=time;
  10410. item[@"create_user"]=user;
  10411. item[@"tear_note"]=note;
  10412. item[@"tear_name"]=name;
  10413. item[@"isLocalFile"]=[NSNumber numberWithBool:YES];
  10414. item[@"model_info"]=model_info;
  10415. item[@"off_params"]=off_params;
  10416. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  10417. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  10418. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10419. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10420. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  10421. }];
  10422. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10423. return [RAConvertor dict2data:dic];
  10424. }
  10425. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  10426. NSString *prodct_ids = [params objectForKey:@"product_id"];
  10427. // NSString *user = [params objectForKey:@"user"];
  10428. // 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];
  10429. 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];
  10430. int result = [iSalesDB execSql:sql];
  10431. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10432. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  10433. if (result == RESULT_TRUE) {
  10434. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  10435. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  10436. sqlite3 *db = [iSalesDB get_db];
  10437. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  10438. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  10439. [iSalesDB close_db:db];
  10440. }
  10441. [dic setValue:@"Regular Mode" forKey:@"mode"];
  10442. return [RAConvertor dict2data:dic];
  10443. }
  10444. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  10445. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  10446. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  10447. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  10448. NSMutableDictionary *section_0 = [[dic objectForKey:@"section_0"] mutableCopy];
  10449. NSDictionary *company_item = @{
  10450. @"control": @"text",
  10451. @"keyboard": @"text",
  10452. @"name": @"company_name",
  10453. @"value": COMPANY_FULL_NAME,
  10454. @"aname": @"Company Name"
  10455. };
  10456. [section_0 setObject:company_item forKey:@"item_0"];
  10457. [dic setObject:section_0 forKey:@"section_0"];
  10458. // Regurlar Price
  10459. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  10460. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  10461. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  10462. __block long val_count = 0;
  10463. NSString *sql = @"select name,type,order_by from price order by order_by";
  10464. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10465. NSString *name = [self textAtColumn:0 statement:stmt];
  10466. int type = sqlite3_column_int(stmt, 1);
  10467. int order_by = sqlite3_column_int(stmt, 2);
  10468. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10469. NSDictionary *price_dic = @{
  10470. @"value" : name,
  10471. @"value_id" : [NSNumber numberWithInteger:type],
  10472. @"check" : order_by == 0 ? @(1) : @(0)
  10473. };
  10474. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  10475. val_count = ++(*count);
  10476. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10477. val_count = 0;
  10478. }];
  10479. [cadedate setObject:@{@"value" : @"None",
  10480. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  10481. val_count++;
  10482. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  10483. [price setObject:cadedate forKey:@"cadedate"];
  10484. [section1 setObject:price forKey:@"item_2"];
  10485. [dic setObject:section1 forKey:@"section_1"];
  10486. return [RAConvertor dict2data:dic];
  10487. }
  10488. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  10489. NSString *product_id = [params objectForKey:@"fashionId"];
  10490. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  10491. __block int result = RESULT_TRUE;
  10492. __block int qty = 0;
  10493. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10494. qty = sqlite3_column_int(stmt, 0);
  10495. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10496. result = RESULT_FALSE;
  10497. }];
  10498. NSMutableDictionary *dic = @{
  10499. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  10500. @"mode" : @"Regular Mode",
  10501. @"quantity_available" : @(qty),
  10502. @"result" : @(result),
  10503. }.mutableCopy;
  10504. return [RAConvertor dict2data:dic];
  10505. }
  10506. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  10507. NSString *item_ids = [params objectForKey:@"item_id"];
  10508. NSString *line_notes = [params objectForKey:@"notes"];
  10509. NSString *price_str = [params objectForKey:@"price"];
  10510. NSString *discount_str = [params objectForKey:@"discount"];
  10511. NSString *percent = [params objectForKey:@"available_percent"];
  10512. NSString *qty = [params objectForKey:@"available_qty"];
  10513. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  10514. int dot = 0;
  10515. if (line_notes) {
  10516. line_notes = [self translateSingleQuote:line_notes];
  10517. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  10518. sql = [sql stringByAppendingString:line_notes];
  10519. dot = 1;
  10520. } else {
  10521. line_notes = @"";
  10522. }
  10523. if (price_str) {
  10524. if (dot) {
  10525. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  10526. } else {
  10527. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  10528. dot = 1;
  10529. }
  10530. sql = [sql stringByAppendingString:price_str];
  10531. } else {
  10532. price_str = @"";
  10533. }
  10534. if (discount_str) {
  10535. if (dot) {
  10536. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  10537. } else {
  10538. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  10539. dot = 1;
  10540. }
  10541. sql = [sql stringByAppendingString:discount_str];
  10542. } else {
  10543. discount_str = @"";
  10544. }
  10545. if (percent) {
  10546. if (dot) {
  10547. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  10548. } else {
  10549. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  10550. dot = 1;
  10551. }
  10552. sql = [sql stringByAppendingString:percent];
  10553. } else {
  10554. percent = @"";
  10555. }
  10556. if (qty) {
  10557. if (dot) {
  10558. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  10559. } else {
  10560. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  10561. dot = 1;
  10562. }
  10563. sql = [sql stringByAppendingString:qty];
  10564. } else {
  10565. qty = @"";
  10566. }
  10567. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  10568. sql = [sql stringByAppendingString:where];
  10569. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10570. int result = [iSalesDB execSql:sql];
  10571. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10572. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10573. return [RAConvertor dict2data:dic];
  10574. }
  10575. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  10576. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  10577. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10578. sqlite3 *db = [iSalesDB get_db];
  10579. 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];
  10580. int result = [iSalesDB execSql:sql db:db];
  10581. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10582. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10583. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  10584. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  10585. [iSalesDB close_db:db];
  10586. return [RAConvertor dict2data:dic];
  10587. }
  10588. + (void)offline_removePDFWithName:(NSString *)name {
  10589. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10590. NSString *cachefolder = [paths objectAtIndex:0];
  10591. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10592. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  10593. NSFileManager *fileManager = [NSFileManager defaultManager];
  10594. [fileManager removeItemAtPath:path error:nil];
  10595. }
  10596. + (void)offline_clear_PDFCache {
  10597. NSFileManager *fileManager = [NSFileManager defaultManager];
  10598. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  10599. NSString *cachefolder = [paths objectAtIndex:0];
  10600. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  10601. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  10602. for (NSString *path in pdf_files) {
  10603. [self offline_removePDFWithName:[path lastPathComponent]];
  10604. }
  10605. }
  10606. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  10607. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  10608. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  10609. NSString *user = [params objectForKey:@"user"];
  10610. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %d;",tearsheetsId]]; // tearsheets_id ---> _id
  10611. if (![create_user isEqualToString:user]) {
  10612. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  10613. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10614. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  10615. return [RAConvertor dict2data:dic];
  10616. }
  10617. NSString *is_local_sql = [NSString stringWithFormat:@"select is_local,pdf_path from offline_pdf where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  10618. __block int is_local = 0;
  10619. __block NSString *path = @"";
  10620. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10621. is_local = sqlite3_column_int(stmt, 0);
  10622. path = [self textAtColumn:1 statement:stmt];
  10623. }];
  10624. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  10625. if (is_local == 1) {
  10626. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  10627. // 删除文件
  10628. [self offline_removePDFWithName:path];
  10629. }
  10630. int result = [iSalesDB execSql:sql];
  10631. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  10632. [dic setObject:@"Regular Mode" forKey:@"mode"];
  10633. return [RAConvertor dict2data:dic];
  10634. }
  10635. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial
  10636. {
  10637. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10638. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  10639. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  10640. NSString* where=@"1=1";
  10641. if (ver!=nil) {
  10642. where=@"is_dirty=1";
  10643. }
  10644. 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];
  10645. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  10646. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10647. sqlite3 *db = [iSalesDB get_db];
  10648. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10649. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  10650. NSInteger _id = sqlite3_column_int(stmt, 0);
  10651. NSInteger product_id = sqlite3_column_int(stmt, 1);
  10652. NSString *name = [self textAtColumn:2 statement:stmt];
  10653. NSString *desc = [self textAtColumn:3 statement:stmt];
  10654. NSInteger item_id = sqlite3_column_int(stmt, 4);
  10655. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  10656. NSInteger qty = sqlite3_column_int(stmt, 6);
  10657. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  10658. double percent = sqlite3_column_double(stmt, 8);
  10659. double price = sqlite3_column_double(stmt, 9);
  10660. double discount = sqlite3_column_double(stmt, 10);
  10661. NSString *img = [self textAtColumn:11 statement:stmt];
  10662. NSString *line_note = [self textAtColumn:12 statement:stmt];
  10663. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  10664. NSString *create_time = [self textAtColumn:14 statement:stmt];
  10665. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  10666. NSString *price_null = [self textAtColumn:9 statement:stmt];
  10667. if ([price_null isEqualToString:@"null"]) {
  10668. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  10669. }
  10670. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  10671. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  10672. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  10673. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  10674. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  10675. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  10676. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  10677. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  10678. if ([qty_null isEqualToString:@"null"]) {
  10679. // [item setValue:@"null" forKey:@"available_qty"];
  10680. } else {
  10681. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  10682. }
  10683. if ([is_percent_null isEqualToString:@"null"]) {
  10684. // [item setValue:@"null" forKey:@"percentage"];
  10685. } else {
  10686. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  10687. }
  10688. if ([percent_null isEqualToString:@"null"]) {
  10689. // [item setValue:@"null" forKey:@"percent"];
  10690. } else {
  10691. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  10692. }
  10693. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  10694. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  10695. [item setValue:name forKey:@"name"];
  10696. [item setValue:desc forKey:@"description"];
  10697. [item setValue:img forKey:@"img"];
  10698. [item setValue:line_note forKey:@"line_note"];
  10699. [item setValue:create_time forKey:@"createtime"];
  10700. [item setValue:modify_time forKey:@"modifytime"];
  10701. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  10702. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10703. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  10704. }];
  10705. [iSalesDB close_db:db];
  10706. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  10707. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10708. }
  10709. return ret;
  10710. }
  10711. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  10712. {
  10713. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10714. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  10715. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  10716. NSString* where=@"1=1";
  10717. if (ver!=nil) {
  10718. where=@"is_dirty=1";
  10719. }
  10720. 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];
  10721. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  10722. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10723. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  10724. NSInteger _id = sqlite3_column_int(stmt, 0);
  10725. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  10726. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  10727. NSString *create_user = [self textAtColumn:3 statement:stmt];
  10728. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  10729. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  10730. NSString *model_info = [self textAtColumn:6 statement:stmt];
  10731. NSString *createtime = [self textAtColumn:7 statement:stmt];
  10732. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  10733. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  10734. NSString *off_params = [self textAtColumn:10 statement:stmt];
  10735. NSString *uuid = [NSUUID UUID].UUIDString;
  10736. int is_delete = sqlite3_column_int(stmt, 11);
  10737. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  10738. int is_local = sqlite3_column_int(stmt, 12);
  10739. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  10740. if (![tearsheet_id_null isEqualToString:@"null"]) {
  10741. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  10742. }
  10743. [item setObject:pdf_path forKey:@"pdf_path"];
  10744. [item setObject:create_user forKey:@"create_user"];
  10745. [item setObject:tear_note forKey:@"tear_note"];
  10746. [item setObject:tear_name forKey:@"tear_name"];
  10747. [item setObject:model_info forKey:@"model_info"];
  10748. [item setObject:createtime forKey:@"createtime"];
  10749. [item setObject:modifytime forKey:@"modifytime"];
  10750. [item setObject:urlParams forKey:@"urlParams"];
  10751. [item setObject:off_params forKey:@"off_params"];
  10752. [item setObject:uuid forKey:@"pdf_token"];
  10753. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  10754. NSDictionary *off_Params_dic = [RAConvertor string2dict:off_params];
  10755. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  10756. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  10757. } else {
  10758. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  10759. }
  10760. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  10761. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  10762. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  10763. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  10764. }];
  10765. return ret;
  10766. }
  10767. @end