OLDataProvider.m 446 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873
  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. @interface OLDataProvider ()
  21. @end
  22. @implementation OLDataProvider
  23. +(NSString*) get_portfolio_price:(NSString*) contact_id item_id:(int) item_id price:(int)price_id db:(sqlite3*)db
  24. {
  25. 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];
  26. NSString* ret=@"No Price.";
  27. sqlite3_stmt * statement;
  28. // int count=0;
  29. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  30. {
  31. if (sqlite3_step(statement) == SQLITE_ROW)
  32. {
  33. char *price = (char*)sqlite3_column_text(statement, 0);
  34. if(price==nil)
  35. price="";
  36. NSString *nsprice = [[NSString alloc]initWithUTF8String:price];
  37. double dp= [nsprice doubleValue];
  38. ret=[NSString stringWithFormat:@"%.2f",dp];
  39. }
  40. sqlite3_finalize(statement);
  41. }
  42. return ret;
  43. // // [iSalesDB close_db:db];
  44. //
  45. // return nil;
  46. //// if(dprice==DBL_MAX)
  47. //// ret= nil;
  48. //// else
  49. //// ret= [NSNumber numberWithDouble:dprice];
  50. //// return ret;
  51. }
  52. + (NSString *)portfolio_img:(NSString *)item_id db:(sqlite3 *)db
  53. {
  54. NSString* ret= nil;
  55. NSString *sqlQuery = nil;
  56. // 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
  57. 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];
  58. sqlite3_stmt * statement;
  59. // int count=0;
  60. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  61. {
  62. if (sqlite3_step(statement) == SQLITE_ROW)
  63. {
  64. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  65. if(imgurl==nil)
  66. imgurl="";
  67. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  68. ret=nsimgurl;
  69. }
  70. sqlite3_finalize(statement);
  71. }
  72. else
  73. {
  74. ret=@"";
  75. }
  76. // [iSalesDB close_db:db];
  77. // DebugLog(@"data string: %@",ret );
  78. return ret;
  79. }
  80. +(NSMutableDictionary*) prepare_portfolio_data:(NSMutableDictionary*) params
  81. {
  82. //生成portfolio pdf需要的数据
  83. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  84. data[@"npd_url"]=@"www.newpacificdirect.com";
  85. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  86. [formatter setDateFormat:@"MMMM yyyy"];
  87. NSString* date = [formatter stringFromDate:[NSDate date]];
  88. data[@"create_month"]=date;
  89. data[@"company_name"]=@"New Pacific Direct,Inc.";
  90. data[@"catalog_name"]=params[@"catalog_name"];
  91. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  92. data[@"TOTAL_PAGE"]=0;
  93. data[@"CURRENT_PAGE"]=0;
  94. NSMutableDictionary* grid = [[NSMutableDictionary alloc]init];
  95. bool show_stockout=[params[@"show_stockout"] boolValue];
  96. NSString *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 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",params[@"product_ids"],params[@"product_ids"]];
  97. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  98. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  99. sqlite3 *db = [iSalesDB get_db];
  100. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  101. // [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  102. int product_id = sqlite3_column_int(stmt, 0);
  103. // NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  104. NSString *name = [self textAtColumn:1 statement:stmt];
  105. NSString *description = [self textAtColumn:2 statement:stmt];
  106. double price = sqlite3_column_double(stmt, 3);
  107. double discount = sqlite3_column_double(stmt,4);
  108. int qty = sqlite3_column_int(stmt, 5);
  109. NSString *s_qty = [self textAtColumn:5 statement:stmt];
  110. int is_percentage = sqlite3_column_int(stmt, 6);
  111. NSString *s_is_percentage = [self textAtColumn:6 statement:stmt];
  112. int item_id = sqlite3_column_int(stmt, 7);
  113. // int fashion_id = sqlite3_column_int(stmt, 8);
  114. NSString *line_note = [self textAtColumn:9 statement:stmt];
  115. NSString *s_percent = [self textAtColumn:10 statement:stmt];
  116. double percent = sqlite3_column_double(stmt, 10);
  117. // int portfolio_id = sqlite3_column_int(stmt, 11);
  118. NSString *s_availability = [self textAtColumn:12 statement:stmt];
  119. int availability = sqlite3_column_int(stmt, 12);
  120. NSString *color = [self textAtColumn:13 statement:stmt];
  121. NSString *legcolor = [self textAtColumn:14 statement:stmt];
  122. NSString *demension = [self textAtColumn:15 statement:stmt];
  123. NSString *seat_height = [self textAtColumn:16 statement:stmt];
  124. NSString *material = [self textAtColumn:17 statement:stmt];
  125. NSString *box_dim = [self textAtColumn:18 statement:stmt];
  126. NSString *s_volume = [self textAtColumn:19 statement:stmt];
  127. double volume = sqlite3_column_double(stmt, 19);
  128. NSString *s_weight = [self textAtColumn:20 statement:stmt];
  129. double weight = sqlite3_column_double(stmt, 20);
  130. NSString *s_model_set = [self textAtColumn:21 statement:stmt];
  131. int model_set = sqlite3_column_int(stmt, 21);
  132. NSString *s_load_ability = [self textAtColumn:22 statement:stmt];
  133. double load_ability = sqlite3_column_double(stmt, 22);
  134. NSString *fabric_content = [self textAtColumn:23 statement:stmt];
  135. NSString *assembling = [self textAtColumn:24 statement:stmt];
  136. NSString *made_in = [self textAtColumn:25 statement:stmt];
  137. if(is_percentage==0&&qty==0&&!show_stockout)
  138. {
  139. return;
  140. }
  141. if((is_percentage!=0||s_is_percentage.length==0)&&availability==0&&!show_stockout)
  142. {
  143. return;
  144. }
  145. if(is_percentage)
  146. {
  147. qty=availability;
  148. }
  149. else
  150. {
  151. percent=1;
  152. }
  153. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  154. // if ([qty_null isEqualToString:@"null"]) {
  155. // qty=availability;
  156. // }
  157. //
  158. // NSString *qty_percent_null = [self textAtColumn:6 statement:stmt];
  159. // if ([qty_percent_null isEqualToString:@"null"]) {
  160. // percentage=1;
  161. // }
  162. NSString * str_availability=[NSString stringWithFormat:@"<b>Availability:</b> %d",(int)(qty*percent+0.5)];
  163. NSString * str_color=[NSString stringWithFormat:@"<b>Color:</b> %@",color];
  164. NSString * str_model_set=[NSString stringWithFormat:@"<b>Set:</b> Sold in QTY of %d",model_set];
  165. NSString * str_legcolor=[NSString stringWithFormat:@"<b>Leg Color:</b> %@",legcolor];
  166. NSString * str_demension=[NSString stringWithFormat:@"<b>Dimension:</b> %@",demension];
  167. NSString * str_seat_height=[NSString stringWithFormat:@"<b>Seat Height:</b> %@",seat_height];
  168. NSString * str_meterial=[NSString stringWithFormat:@"<b>Meterial:</b> %@",material];
  169. NSString * str_box_dim=[NSString stringWithFormat:@"<b>Box Dimension:</b> %@",box_dim];
  170. NSString * str_volume=[NSString stringWithFormat:@"<b>Volume:</b> %.2f",volume];
  171. NSString * str_weight=[NSString stringWithFormat:@"<b>Weight:</b> %.2f",weight];
  172. NSString * str_load_ability=[NSString stringWithFormat:@"<b>Load Ability:</b> %.2f",load_ability];
  173. NSString * str_fabric_content=[NSString stringWithFormat:@"<b>Fabric Content:</b> %@",fabric_content];
  174. NSString * str_assembling=[NSString stringWithFormat:@"<b>Assembling:</b> %@",assembling];
  175. NSString * str_made_in=[NSString stringWithFormat:@"<b>Made In:</b> %@",made_in];
  176. NSString * str_line_note=[NSString stringWithFormat:@"<b>Line Note:</b> %@",line_note];
  177. if([params[@"availability"] boolValue]==false || (s_qty.length==0 && s_availability.length==0))
  178. str_availability=@"";
  179. if([params[@"color"] boolValue]==false || color.length==0 )
  180. str_color=@"";
  181. if([params[@"set"] boolValue]==false || s_model_set.length==0 )
  182. str_model_set=@"";
  183. if([params[@"leg_color"] boolValue]==false || legcolor.length==0 )
  184. str_legcolor=@"";
  185. if([params[@"dimension"] boolValue]==false || demension.length==0 )
  186. str_demension=@"";
  187. if([params[@"seat_height"] boolValue]==false || seat_height.length==0 )
  188. str_seat_height=@"";
  189. if([params[@"material"] boolValue]==false || material.length==0 )
  190. str_meterial=@"";
  191. if([params[@"box_dimension"] boolValue]==false || box_dim.length==0 )
  192. str_box_dim=@"";
  193. if([params[@"volume"] boolValue]==false || s_volume.length==0 )
  194. str_volume=@"";
  195. if([params[@"weight"] boolValue]==false || s_weight.length==0 )
  196. str_weight=@"";
  197. if([params[@"load_ability"] boolValue]==false || s_load_ability.length==0 )
  198. str_load_ability=@"";
  199. if([params[@"fabric_content"] boolValue]==false || fabric_content.length==0 )
  200. str_fabric_content=@"";
  201. if([params[@"assembling"] boolValue]==false || assembling.length==0 )
  202. str_assembling=@"";
  203. if([params[@"made"] boolValue]==false || made_in.length==0 )
  204. str_made_in=@"";
  205. if([params[@"show_line_note"] boolValue]==false || line_note.length==0 )
  206. str_line_note=@"";
  207. NSMutableArray* arr_detail=[[NSMutableArray alloc]init];
  208. [arr_detail addObject:str_color];
  209. [arr_detail addObject:str_model_set];
  210. [arr_detail addObject:str_legcolor];
  211. [arr_detail addObject:str_demension];
  212. [arr_detail addObject:str_seat_height];
  213. [arr_detail addObject:str_meterial];
  214. [arr_detail addObject:str_box_dim];
  215. [arr_detail addObject:str_volume];
  216. [arr_detail addObject:str_weight];
  217. [arr_detail addObject:str_load_ability];
  218. [arr_detail addObject:str_fabric_content];
  219. [arr_detail addObject:str_assembling];
  220. [arr_detail addObject:str_made_in];
  221. [arr_detail addObject:str_line_note];
  222. // NSString* detail =[NSString stringWithFormat:@"<font size=0.2>%@</font>",[RAUtils arr2string:arr_detail separator:@" " trim:true]];
  223. NSString* detail =[RAUtils arr2string:arr_detail separator:@" " trim:true];
  224. //model image;
  225. NSString* img_path = [self portfolio_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  226. // qr image
  227. NSString* qrpath=nil;
  228. if([params[@"show_barcode"] boolValue])
  229. {
  230. NSString* temp = NSTemporaryDirectory();
  231. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  232. qrpath=[temp stringByAppendingPathComponent:filename];
  233. UIImage* qrimg=[QRCodeGenerator qrImageForString:name imageSize:240];
  234. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  235. }
  236. NSString* set_price=@"";
  237. NSString *price_null = [self textAtColumn:3 statement:stmt];
  238. if ([price_null isEqualToString:@"null"]) {
  239. }
  240. else
  241. {
  242. set_price=[NSString stringWithFormat:@"Special: $ %.2f",price*(1-discount/100.0)];
  243. }
  244. NSString* get_price=@"";
  245. {
  246. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  247. NSString* price = [self get_portfolio_price :appDelegate.contact_id item_id:item_id price:[params[@"regular_price"] intValue] db:db];
  248. // DebugLog(@"price time interval");
  249. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  250. if(price==nil)
  251. get_price=@"Price:No Price.";
  252. else
  253. get_price=[NSString stringWithFormat:@"Price: $ %@",price];
  254. }
  255. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  256. item[@"img"]=img_path;
  257. item[@"img_qr"]=qrpath;
  258. item[@"special_price"]=set_price;
  259. item[@"price"]=get_price;
  260. item[@"name"]=name;
  261. item[@"description"]=description;
  262. item[@"detail"]=detail;
  263. item[@"img"]=img_path;
  264. // @{
  265. // //@"linenotes": line_note,
  266. //// @"product_id": product_id_string,
  267. //// @"available_qty": @(qty),
  268. //// @"available_percent" : @(percent),
  269. //// @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  270. //// @"item_id": [NSString stringWithFormat:@"%d",item_id],
  271. //// @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  272. // @"": ,
  273. // @"": ,
  274. // @"": ,
  275. // @"": ,
  276. // @"": ,
  277. // @"": ,
  278. // @"":
  279. // }.mutableCopy;
  280. // if (percentage) {
  281. // [item removeObjectForKey:@"available_qty"];
  282. // } else {
  283. // [item removeObjectForKey:@"available_percent"];
  284. // }
  285. //
  286. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  287. // if ([qty_null isEqualToString:@"null"]) {
  288. // [item removeObjectForKey:@"available_qty"];
  289. // }
  290. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  291. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  292. }];
  293. [iSalesDB close_db:db];
  294. // [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  295. // [dic setValue:@"" forKey:@"email_content"];
  296. // [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  297. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  298. //
  299. // NSMutableDictionary* cell = [[NSMutableDictionary alloc]init];
  300. // cell[@"count"]=[NSNumber numberWithInt:10];
  301. grid[@"cell0"]=dic;
  302. data[@"grid0"]=grid;
  303. return data;
  304. }
  305. +(NSMutableDictionary*) get_pdftemplate:(NSString*) template
  306. {
  307. NSString *default_path = [[NSBundle mainBundle] pathForResource:template ofType:nil];
  308. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  309. NSString *cache_folder=[paths objectAtIndex:0];
  310. NSString *template_path = [cache_folder stringByAppendingPathComponent:[NSString stringWithFormat:@"offline_data/%@",template]];
  311. BOOL bdir=NO;
  312. NSFileManager* fileManager = [NSFileManager defaultManager];
  313. if(! [fileManager fileExistsAtPath:template_path isDirectory:&bdir])
  314. {
  315. NSError * error=nil;
  316. if(![fileManager copyItemAtPath:default_path toPath:template_path error:&error])
  317. {
  318. return nil;
  319. }
  320. }
  321. NSData *data = [NSData dataWithContentsOfFile:template_path];
  322. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  323. return ret;
  324. }
  325. +(NSData*) offline_request_tearsheet :(NSMutableDictionary *) params
  326. {
  327. NSString* template_file=nil;
  328. switch([params[@"pdf_style"] intValue])
  329. {
  330. case 0:
  331. template_file= @"portfolio_2x3.json";
  332. break;
  333. case 1:
  334. template_file= @"portfolio_3x2.json";
  335. break;
  336. default:
  337. template_file= @"portfolio_3x2.json";
  338. }
  339. NSMutableDictionary* data = [self prepare_portfolio_data:params];
  340. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  341. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  342. DebugLog(@"%@",file);
  343. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  344. [dic setObject:@"Regular Mode" forKey:@"mode"];
  345. if (file) {
  346. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  347. [dic setObject:file forKey:@"pdf_path"];
  348. dic[@"isLocalFile"]=@"true";
  349. return [RAUtils dict2data:dic];
  350. }
  351. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  352. dic[@"isLocalFile"]=@"true";
  353. return [RAUtils dict2data:dic];
  354. }
  355. +(NSString*) get_offline_soid:(sqlite3*)db
  356. {
  357. NSString* soid=nil;
  358. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  359. [formatter setDateFormat:@"yyMMdd"];
  360. NSString* date = [formatter stringFromDate:[NSDate date]];
  361. for(int i=1;i<999;i++)
  362. {
  363. soid=[NSString stringWithFormat:@"OFF%@%4d",date,i];
  364. soid= [soid stringByReplacingOccurrencesOfString:@" " withString:@"0"];
  365. int count=[iSalesDB get_recordcount:db table:@"offline_order" where:[NSString stringWithFormat:@"so_id='%@'",soid]];
  366. if(count==0)
  367. return soid;
  368. }
  369. return nil;
  370. }
  371. +(NSArray*) enumOfflineOrder
  372. {
  373. NSMutableArray* ret = [[NSMutableArray alloc] init];
  374. sqlite3 *db = [iSalesDB get_db];
  375. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  376. sqlite3_stmt * statement;
  377. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  378. if ( dbresult== SQLITE_OK)
  379. {
  380. while (sqlite3_step(statement) == SQLITE_ROW)
  381. {
  382. char *so_id = (char*)sqlite3_column_text(statement, 0);
  383. NSString* nsso_id=nil;
  384. if(so_id!=nil)
  385. {
  386. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  387. }
  388. [ret addObject:nsso_id];
  389. }
  390. sqlite3_finalize(statement);
  391. }
  392. [iSalesDB close_db:db];
  393. return ret;
  394. }
  395. +(void) uploadFile:(NSString*) file
  396. {
  397. NSData* data = [NSData dataWithContentsOfFile: file];
  398. UIApplication * app = [UIApplication sharedApplication];
  399. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  400. AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  401. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  402. if(appDelegate.user!=nil)
  403. [params setValue:appDelegate.user forKey:@"user"];
  404. // if(appDelegate.contact_id!=nil)
  405. // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  406. if(appDelegate.password!=nil)
  407. [params setValue:appDelegate.password forKey:@"password"];
  408. NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_OFFLINE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  409. [formData appendPartWithFileData:data name:@"upfile" fileName:[file lastPathComponent] mimeType:@"application/zip"];
  410. } error:nil];
  411. AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  412. NSProgress *progress = nil;
  413. NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  414. if (error) {
  415. NSString* err_msg = [error localizedDescription];
  416. NSLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  417. NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  418. NSLog(@"data string: %@",str);
  419. [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  420. } else {
  421. NSLog(@"response ");
  422. NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  423. // 再将NSData转为字符串
  424. NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  425. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  426. NSLog(@"data string: %@",jsonStr);
  427. NSDictionary* json = responseObject;
  428. if([[json valueForKey:@"result"] intValue]==2)
  429. {
  430. // NSString* img_url_down = json[@"img_url_aname"];
  431. // NSString* img_url_up = json[@"img_url"];
  432. }
  433. else
  434. {
  435. [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload file" controller:nil] ;
  436. }
  437. }
  438. // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  439. }];
  440. // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  441. //
  442. [uploadTask resume];
  443. }
  444. +(NSDictionary *) prepareUpload:(NSArray*) arr_order
  445. {
  446. NSString* serial= [[NSUUID UUID] UUIDString];
  447. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  448. NSString *cachefolder = [paths objectAtIndex:0];
  449. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  450. upfolder = [upfolder stringByAppendingPathComponent:serial];
  451. NSFileManager* fileManager = [NSFileManager defaultManager];
  452. BOOL bdir=YES;
  453. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  454. {
  455. NSError *error = nil;
  456. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  457. if(!bsuccess)
  458. DebugLog(@"Create UPLOAD folder failed");
  459. }
  460. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  461. ret[@"contact"]=[self prepareContact:serial];
  462. ret[@"wishlist"]=[self prepareWishlist:serial];
  463. ret[@"order"]=[self prepareOrder:serial soid:arr_order];
  464. ret[@"portfolio"] = [self preparePortfolio:serial];
  465. ret[@"view_portfolio"] = [self preparePDF:serial];
  466. NSString* str= [RAUtils dict2string:ret];
  467. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  468. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  469. NSError *error=nil;
  470. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  471. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  472. ZipArchive* zip = [[ZipArchive alloc] init];
  473. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  474. for(int i=0;i<arr_files.count;i++)
  475. {
  476. NSString* file=arr_files[i];
  477. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  478. }
  479. if( ![zip CloseZipFile2] )
  480. {
  481. zippath = @"";
  482. }
  483. ret[@"file"]=zippath;
  484. return ret;
  485. }
  486. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  487. {
  488. if(filename.length==0)
  489. return false;
  490. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  491. bool ret=false;
  492. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  493. NSString *cachefolder = [paths objectAtIndex:0];
  494. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  495. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  496. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  497. upfolder = [upfolder stringByAppendingPathComponent:serial];
  498. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  499. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  500. NSFileManager* fileManager = [NSFileManager defaultManager];
  501. BOOL bdir=NO;
  502. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  503. {
  504. NSError *error = nil;
  505. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  506. {
  507. ret=false;
  508. }
  509. else
  510. {
  511. ret=true;
  512. }
  513. }
  514. return ret;
  515. }
  516. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  517. {
  518. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  519. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  520. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  521. NSString* where=@" 1=1";
  522. if (ver!=nil) {
  523. where=@"is_dirty=1";
  524. }
  525. sqlite3 *db = [iSalesDB get_db];
  526. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  527. sqlite3_stmt * statement;
  528. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  529. int count=0;
  530. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  531. if ( dbresult== SQLITE_OK)
  532. {
  533. while (sqlite3_step(statement) == SQLITE_ROW)
  534. {
  535. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  536. int _id = sqlite3_column_int(statement, 0);
  537. int product_id = sqlite3_column_int(statement, 1);
  538. int item_id = sqlite3_column_int(statement, 2);
  539. int qty = sqlite3_column_int(statement, 3);
  540. char *create_time = (char*)sqlite3_column_text(statement, 4);
  541. NSString* nscreate_time=nil;
  542. if(create_time!=nil)
  543. {
  544. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  545. }
  546. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  547. NSString* nsmodify_time=nil;
  548. if(modify_time!=nil)
  549. {
  550. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  551. }
  552. int is_delete = sqlite3_column_int(statement, 6);
  553. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  554. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  555. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  556. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  557. itemjson[@"create_time"]=nscreate_time;
  558. itemjson[@"modify_time"]=nsmodify_time;
  559. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  560. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  561. count++;
  562. }
  563. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  564. sqlite3_finalize(statement);
  565. }
  566. ret[@"count"]=[NSNumber numberWithInt:count ];
  567. [iSalesDB close_db:db];
  568. return ret;
  569. }
  570. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode db:(sqlite3*)db
  571. {
  572. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  573. UIApplication * app = [UIApplication sharedApplication];
  574. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  575. 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 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 ];
  576. // 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 ];
  577. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  578. DebugLog(@"offline_login sql:%@",sqlQuery);
  579. sqlite3_stmt * statement;
  580. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  581. int count=0;
  582. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  583. if ( dbresult== SQLITE_OK)
  584. {
  585. while (sqlite3_step(statement) == SQLITE_ROW)
  586. {
  587. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  588. int product_id = sqlite3_column_int(statement, 0);
  589. char *str_price = (char*)sqlite3_column_text(statement, 1);
  590. int item_id = sqlite3_column_int(statement, 7);
  591. NSString* Price=nil;
  592. if(str_price==nil)
  593. {
  594. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  595. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  596. if(price==nil)
  597. Price=@"No Price.";
  598. else
  599. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  600. }
  601. else
  602. {
  603. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  604. }
  605. double discount = sqlite3_column_double(statement, 2);
  606. int item_count = sqlite3_column_int(statement, 3);
  607. char *line_note = (char*)sqlite3_column_text(statement, 4);
  608. NSString *nsline_note=nil;
  609. if(line_note!=nil)
  610. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  611. char *name = (char*)sqlite3_column_text(statement, 5);
  612. NSString *nsname=nil;
  613. if(name!=nil)
  614. nsname= [[NSString alloc]initWithUTF8String:name];
  615. char *description = (char*)sqlite3_column_text(statement, 6);
  616. NSString *nsdescription=nil;
  617. if(description!=nil)
  618. nsdescription= [[NSString alloc]initWithUTF8String:description];
  619. // int stockUom = sqlite3_column_int(statement, 8);
  620. // int _id = sqlite3_column_int(statement, 9);
  621. //
  622. //
  623. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  624. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  625. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  626. // int carton=[bsubtotaljson[@"carton"] intValue];
  627. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  628. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  629. itemjson[@"model"]=nsname;
  630. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  631. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  632. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  633. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  634. // itemjson[@"check"]=@"true";
  635. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  636. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  637. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  638. itemjson[@"unit_price"]=Price;
  639. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  640. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  641. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  642. itemjson[@"note"]=nsline_note;
  643. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  644. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  645. count++;
  646. }
  647. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  648. sqlite3_finalize(statement);
  649. }
  650. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  651. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  652. return ret;
  653. }
  654. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid
  655. {
  656. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  657. sqlite3 *db = [iSalesDB get_db];
  658. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  659. // for(int i=0;i<arr_soid.count;i++)
  660. sqlite3_stmt * statement;
  661. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  662. //int count=0;
  663. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  664. if ( dbresult== SQLITE_OK)
  665. {
  666. int count=0;
  667. while (sqlite3_step(statement) == SQLITE_ROW)
  668. {
  669. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  670. int _id = sqlite3_column_int(statement, 0);
  671. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  672. NSString* nssync_data=nil;
  673. if(sync_data!=nil)
  674. {
  675. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  676. }
  677. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  678. NSString* nsimg_1=nil;
  679. if(img_1!=nil)
  680. {
  681. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  682. [self copy_upImg:serial file:nsimg_1];
  683. }
  684. char *so_no = (char*)sqlite3_column_text(statement, 3);
  685. NSString* nsso_no=nil;
  686. if(so_no!=nil)
  687. {
  688. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  689. }
  690. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  691. itemjson[@"order_type"]=@"submit order";
  692. else
  693. itemjson[@"order_type"]=@"archive order";
  694. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  695. itemjson[@"json_data"]= nssync_data;
  696. itemjson[@"order_items"]=[self preparecart:nsso_no db:db];
  697. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  698. count++;
  699. }
  700. ret[@"count"]=[NSNumber numberWithInt:count ];
  701. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  702. }
  703. sqlite3_finalize(statement);
  704. [iSalesDB close_db:db];
  705. return ret;
  706. }
  707. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  708. {
  709. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  710. sqlite3 *db = [iSalesDB get_db];
  711. // UIApplication * app = [UIApplication sharedApplication];
  712. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  713. 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";
  714. sqlite3_stmt * statement;
  715. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  716. int count=0;
  717. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  718. if ( dbresult== SQLITE_OK)
  719. {
  720. while (sqlite3_step(statement) == SQLITE_ROW)
  721. {
  722. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  723. int _id = sqlite3_column_int(statement, 0);
  724. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  725. NSString* nsimg_0=nil;
  726. if(img_0!=nil)
  727. {
  728. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  729. [self copy_upImg:serial file:nsimg_0];
  730. }
  731. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  732. NSString* nsimg_1=nil;
  733. if(img_1!=nil)
  734. {
  735. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  736. [self copy_upImg:serial file:nsimg_1];
  737. }
  738. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  739. NSString* nsimg_2=nil;
  740. if(img_2!=nil)
  741. {
  742. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  743. [self copy_upImg:serial file:nsimg_2];
  744. }
  745. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  746. NSString* nssync_data=nil;
  747. if(sync_data!=nil)
  748. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  749. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  750. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  751. itemjson[@"json_data"]= nssync_data;
  752. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  753. count++;
  754. }
  755. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  756. sqlite3_finalize(statement);
  757. }
  758. ret[@"count"]=[NSNumber numberWithInt:count ];
  759. [iSalesDB close_db:db];
  760. return ret;
  761. }
  762. +(bool) check_offlinedata
  763. {
  764. UIApplication * app = [UIApplication sharedApplication];
  765. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  766. sqlite3 *db = [iSalesDB get_db];
  767. NSString * where=@"1=1";
  768. // if(appDelegate.user!=nil)
  769. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  770. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  771. [iSalesDB close_db:db];
  772. if(count==0)
  773. {
  774. return false;
  775. }
  776. return true;
  777. //
  778. //[iSalesDB close_db:db];
  779. }
  780. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  781. {
  782. UIApplication * app = [UIApplication sharedApplication];
  783. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  784. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  785. sqlite3 *db = [iSalesDB get_db];
  786. NSString* collectId=params[@"collectId"];
  787. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  788. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  789. [iSalesDB execSql:sqlQuery db:db];
  790. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  791. [iSalesDB close_db:db];
  792. appDelegate.wish_count =count;
  793. [appDelegate update_count_mark];
  794. ret[@"result"]= [NSNumber numberWithInt:2];
  795. return ret;
  796. }
  797. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  798. //{
  799. //
  800. // UIApplication * app = [UIApplication sharedApplication];
  801. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  802. //
  803. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  804. // sqlite3 *db = [iSalesDB get_db];
  805. // NSString* product_id=params[@"product_id"];
  806. //
  807. //
  808. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  809. //
  810. //
  811. // for(int i=0;i<arr.count;i++)
  812. // {
  813. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  814. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  815. // if(count==0)
  816. // {
  817. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  818. // [iSalesDB execSql:sqlQuery db:db];
  819. // }
  820. // }
  821. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  822. // [iSalesDB close_db:db];
  823. //
  824. // appDelegate.wish_count =count;
  825. //
  826. // [appDelegate update_count_mark];
  827. // ret[@"result"]= [NSNumber numberWithInt:2];
  828. // return ret;
  829. // //
  830. // //return ret;
  831. //}
  832. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  833. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  834. DebugLog(@"time interval: %lf",interval);
  835. }
  836. +(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  837. {
  838. UIApplication * app = [UIApplication sharedApplication];
  839. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  840. int sort = [[params objectForKey:@"sort"] intValue];
  841. NSString *sort_str = @"";
  842. switch (sort) {
  843. case 0:{
  844. sort_str = @"order by w.modify_time desc";
  845. }
  846. break;
  847. case 1:{
  848. sort_str = @"order by w.modify_time asc";
  849. }
  850. break;
  851. case 2:{
  852. sort_str = @"order by m.name asc";
  853. }
  854. break;
  855. case 3:{
  856. sort_str = @"order by m.name desc";
  857. }
  858. break;
  859. case 4:{
  860. sort_str = @"order by m.description asc";
  861. }
  862. break;
  863. default:
  864. break;
  865. }
  866. NSString* user = appDelegate.user;
  867. sqlite3 *db = [iSalesDB get_db];
  868. // order by w.create_time
  869. 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];
  870. // 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];
  871. sqlite3_stmt * statement;
  872. NSDate *date1 = [NSDate date];
  873. NSDate *date2 = nil;
  874. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  875. int count=0;
  876. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  877. {
  878. while (sqlite3_step(statement) == SQLITE_ROW)
  879. {
  880. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  881. int product_id = sqlite3_column_double(statement, 0);
  882. char *description = (char*)sqlite3_column_text(statement, 1);
  883. if(description==nil)
  884. description= "";
  885. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  886. int item_id = sqlite3_column_double(statement, 2);
  887. NSDate *date_image = [NSDate date];
  888. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  889. printf("image : ");
  890. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  891. // char *url = (char*)sqlite3_column_text(statement, 3);
  892. // if(url==nil)
  893. // url="";
  894. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  895. int qty = sqlite3_column_int(statement, 3);
  896. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  897. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  898. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  899. item[@"description"]= nsdescription;
  900. item[@"img"]= nsurl;
  901. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  902. count++;
  903. }
  904. printf("total time:");
  905. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  906. ret[@"count"]= [NSNumber numberWithInt:count];
  907. ret[@"total_count"]= [NSNumber numberWithInt:count];
  908. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  909. ret[@"result"]= [NSNumber numberWithInt:2];
  910. appDelegate.wish_count =count;
  911. [appDelegate update_count_mark];
  912. sqlite3_finalize(statement);
  913. }
  914. [iSalesDB close_db:db];
  915. return ret;
  916. }
  917. +(NSDictionary*) offline_notimpl
  918. {
  919. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  920. ret[@"result"]=@"8";
  921. ret[@"err_msg"]=@"offline mode does not support this function.";
  922. return ret;
  923. }
  924. +(NSDictionary*) offline_home
  925. {
  926. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  927. NSString *cachefolder = [paths objectAtIndex:0];
  928. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  929. NSData* json =nil;
  930. json=[NSData dataWithContentsOfFile:img_cache];
  931. NSError *error=nil;
  932. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  933. return menu;
  934. }
  935. +(NSDictionary*) offline_category_menu
  936. {
  937. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  938. NSString *cachefolder = [paths objectAtIndex:0];
  939. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  940. NSData* json =nil;
  941. json=[NSData dataWithContentsOfFile:img_cache];
  942. NSError *error=nil;
  943. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  944. return menu;
  945. }
  946. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  947. {
  948. NSString* offline_command=params[@"offline_Command"];
  949. NSDictionary* ret=nil;
  950. if([offline_command isEqualToString:@"model_NIYMAL"])
  951. {
  952. NSString* category = params[@"category"];
  953. ret = [self refresh_model_NIYMAL:category];
  954. }
  955. return ret;
  956. }
  957. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  958. {
  959. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  960. [ret setValue:@"2" forKey:@"result"];
  961. [ret setValue:@"Regular Mode" forKey:@"mode"];
  962. sqlite3* db= [iSalesDB get_db];
  963. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  964. [iSalesDB close_db:db];
  965. [ret setObject:detail1_section forKey:@"detail_1"];
  966. return ret;
  967. }
  968. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id db:(sqlite3*)db
  969. {
  970. UIApplication * app = [UIApplication sharedApplication];
  971. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  972. // NSArray* arr1 = [self get_user_all_price_type];
  973. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  974. // NSSet *set1 = [NSSet setWithArray:arr1];
  975. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  976. // if(appDelegate.contact_id==nil)
  977. // set2=[set1 mutableCopy];
  978. // else
  979. // [set2 intersectsSet:set1];
  980. // NSArray *retarr = [set2 allObjects];
  981. NSString* whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  982. // sqlite3 *db = [iSalesDB get_db];
  983. NSString* sqlQuery = nil;
  984. if(appDelegate.contact_id==nil)
  985. 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];
  986. else
  987. 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];
  988. sqlite3_stmt * statement;
  989. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  990. int count=0;
  991. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  992. {
  993. while (sqlite3_step(statement) == SQLITE_ROW)
  994. {
  995. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  996. char *name = (char*)sqlite3_column_text(statement, 0);
  997. if(name==nil)
  998. name="";
  999. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1000. // double price = sqlite3_column_double(statement, 1);
  1001. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  1002. // if(isnull==nil)
  1003. // item[nsname]= @"No Price";
  1004. // else
  1005. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  1006. char *price = (char*)sqlite3_column_text(statement, 1);
  1007. if(price!=nil)
  1008. {
  1009. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  1010. nsprice=[AESCrypt fastdecrypt:nsprice];
  1011. if(nsprice.length>0)
  1012. {
  1013. double dp= [nsprice doubleValue];
  1014. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  1015. }
  1016. }
  1017. else
  1018. {
  1019. item[nsname]= @"No Price";
  1020. }
  1021. // int type= sqlite3_column_int(statement, 2);
  1022. // item[@"type"]=[NSNumber numberWithInt:type ];
  1023. // item[nsname]= nsprice;
  1024. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  1025. count++;
  1026. }
  1027. ret[@"count"]= [NSNumber numberWithInt:count];
  1028. sqlite3_finalize(statement);
  1029. }
  1030. // [iSalesDB close_db:db];
  1031. return ret;
  1032. }
  1033. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  1034. //{
  1035. // NSArray* arr1 = [self get_user_all_price_type:db];
  1036. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  1037. //
  1038. // // NSSet *set1 = [NSSet setWithArray:arr1];
  1039. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  1040. // // [set2 intersectsSet:set1];
  1041. // //
  1042. // //
  1043. // // NSArray *retarr = [set2 allObjects];
  1044. //
  1045. // NSString* whereprice=nil;
  1046. // if(contact_id==nil)
  1047. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  1048. // else
  1049. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  1050. //
  1051. //
  1052. // // sqlite3 *db = [iSalesDB get_db];
  1053. //
  1054. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  1055. // sqlite3_stmt * statement;
  1056. //
  1057. //
  1058. // NSNumber* ret = nil;
  1059. // double dprice=DBL_MAX;
  1060. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1061. // {
  1062. //
  1063. //
  1064. // while (sqlite3_step(statement) == SQLITE_ROW)
  1065. // {
  1066. //
  1067. // // double val = sqlite3_column_double(statement, 0);
  1068. // char *price = (char*)sqlite3_column_text(statement, 0);
  1069. // if(price!=nil)
  1070. // {
  1071. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  1072. // nsprice=[AESCrypt fastdecrypt:nsprice];
  1073. // if(nsprice.length>0)
  1074. // {
  1075. // double dp= [nsprice doubleValue];
  1076. // if(dp<dprice)
  1077. // dprice=dp;
  1078. // }
  1079. // }
  1080. // }
  1081. //
  1082. //
  1083. //
  1084. //
  1085. // sqlite3_finalize(statement);
  1086. //
  1087. //
  1088. //
  1089. //
  1090. // }
  1091. //
  1092. // // [iSalesDB close_db:db];
  1093. //
  1094. // if(dprice==DBL_MAX)
  1095. // ret= nil;
  1096. // else
  1097. // ret= [NSNumber numberWithDouble:dprice];
  1098. // return ret;
  1099. //}
  1100. +(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  1101. {
  1102. NSArray* arr1 = [self get_user_all_price_type:db];
  1103. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  1104. NSString* whereprice=nil;
  1105. if(contact_id==nil)
  1106. whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  1107. else
  1108. whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  1109. // sqlite3 *db = [iSalesDB get_db];
  1110. NSString *productIdCondition = @"1 = 1";
  1111. if (product_id) {
  1112. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  1113. }
  1114. NSString *itemIdCondition = @"";
  1115. if (item_id) {
  1116. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  1117. }
  1118. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  1119. sqlite3_stmt * statement;
  1120. NSNumber* ret = nil;
  1121. double dprice=DBL_MAX;
  1122. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1123. {
  1124. while (sqlite3_step(statement) == SQLITE_ROW)
  1125. {
  1126. // double val = sqlite3_column_double(statement, 0);
  1127. char *price = (char*)sqlite3_column_text(statement, 0);
  1128. if(price!=nil)
  1129. {
  1130. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  1131. nsprice=[AESCrypt fastdecrypt:nsprice];
  1132. if(nsprice.length>0)
  1133. {
  1134. double dp= [nsprice doubleValue];
  1135. if(dp<dprice)
  1136. dprice=dp;
  1137. }
  1138. }
  1139. }
  1140. sqlite3_finalize(statement);
  1141. }
  1142. // [iSalesDB close_db:db];
  1143. if(dprice==DBL_MAX)
  1144. ret= nil;
  1145. else
  1146. ret= [NSNumber numberWithDouble:dprice];
  1147. return ret;
  1148. }
  1149. +(NSArray*) get_user_all_price_type:(sqlite3*)db
  1150. {
  1151. NSArray* ret=nil;
  1152. // sqlite3 *db = [iSalesDB get_db];
  1153. // no customer assigned , use login user contact_id
  1154. UIApplication * app = [UIApplication sharedApplication];
  1155. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1156. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",appDelegate.user];
  1157. sqlite3_stmt * statement;
  1158. // int count=0;
  1159. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1160. {
  1161. if (sqlite3_step(statement) == SQLITE_ROW)
  1162. {
  1163. char *val = (char*)sqlite3_column_text(statement, 0);
  1164. if(val==nil)
  1165. val="";
  1166. NSString* price = [[NSString alloc]initWithUTF8String:val];
  1167. ret=[RAUtils string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  1168. }
  1169. sqlite3_finalize(statement);
  1170. }
  1171. // [iSalesDB close_db:db];
  1172. return ret;
  1173. }
  1174. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id db:(sqlite3*)db
  1175. {
  1176. // sqlite3 *db = [iSalesDB get_db];
  1177. if(contact_id==nil)
  1178. {
  1179. // no customer assigned , use login user contact_id
  1180. UIApplication * app = [UIApplication sharedApplication];
  1181. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1182. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",appDelegate.user];
  1183. sqlite3_stmt * statement;
  1184. // int count=0;
  1185. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1186. {
  1187. if (sqlite3_step(statement) == SQLITE_ROW)
  1188. {
  1189. char *val = (char*)sqlite3_column_text(statement, 0);
  1190. if(val==nil)
  1191. val="";
  1192. contact_id = [[NSString alloc]initWithUTF8String:val];
  1193. }
  1194. sqlite3_finalize(statement);
  1195. }
  1196. if(contact_id.length<=0)
  1197. {
  1198. // [iSalesDB close_db:db];
  1199. return nil;
  1200. }
  1201. }
  1202. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  1203. sqlite3_stmt * statement;
  1204. NSArray* ret=nil;
  1205. // int count=0;
  1206. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1207. {
  1208. if (sqlite3_step(statement) == SQLITE_ROW)
  1209. {
  1210. char *price_type = (char*)sqlite3_column_text(statement, 0);
  1211. if(price_type==nil)
  1212. price_type="";
  1213. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  1214. if(nsprice_type.length>0)
  1215. ret=[RAUtils string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  1216. else
  1217. ret=nil;
  1218. }
  1219. sqlite3_finalize(statement);
  1220. }
  1221. // [iSalesDB close_db:db];
  1222. return ret;
  1223. }
  1224. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  1225. {
  1226. NSString* ret= nil;
  1227. // sqlite3 *db = [iSalesDB get_db];
  1228. NSString *sqlQuery = nil;
  1229. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  1230. // 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;
  1231. // 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
  1232. if(product_id==nil && model_name)
  1233. 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;
  1234. else if (product_id)
  1235. 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
  1236. sqlite3_stmt * statement;
  1237. // int count=0;
  1238. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1239. {
  1240. if (sqlite3_step(statement) == SQLITE_ROW)
  1241. {
  1242. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  1243. if(imgurl==nil)
  1244. imgurl="";
  1245. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  1246. ret=nsimgurl;
  1247. }
  1248. sqlite3_finalize(statement);
  1249. }
  1250. else
  1251. {
  1252. [ret setValue:@"8" forKey:@"result"];
  1253. }
  1254. // [iSalesDB close_db:db];
  1255. DebugLog(@"data string: %@",ret );
  1256. return ret;
  1257. }
  1258. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name
  1259. {
  1260. NSString* ret= nil;
  1261. sqlite3 *db = [iSalesDB get_db];
  1262. NSString *sqlQuery = nil;
  1263. if(product_id==nil)
  1264. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  1265. else
  1266. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  1267. sqlite3_stmt * statement;
  1268. // int count=0;
  1269. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1270. {
  1271. if (sqlite3_step(statement) == SQLITE_ROW)
  1272. {
  1273. char *default_category = (char*)sqlite3_column_text(statement, 0);
  1274. if(default_category==nil)
  1275. default_category="";
  1276. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  1277. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  1278. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  1279. ret=nsdefault_category;
  1280. }
  1281. sqlite3_finalize(statement);
  1282. }
  1283. else
  1284. {
  1285. [ret setValue:@"8" forKey:@"result"];
  1286. }
  1287. [iSalesDB close_db:db];
  1288. DebugLog(@"data string: %@",ret );
  1289. return ret;
  1290. }
  1291. +(NSDictionary*) offline_model :(NSMutableDictionary *) params
  1292. {
  1293. NSString* model_name = [params valueForKey:@"product_name"];
  1294. NSString* product_id = [params valueForKey:@"product_id"];
  1295. NSString* category = [params valueForKey:@"category"];
  1296. if(category==nil)
  1297. category = [self model_default_category:product_id model_name:model_name];
  1298. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1299. sqlite3 *db = [iSalesDB get_db];
  1300. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  1301. NSString *sqlQuery = nil;
  1302. if(product_id==nil)
  1303. 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 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='%@';
  1304. else
  1305. 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 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=%@;
  1306. sqlite3_stmt * statement;
  1307. [ret setValue:@"2" forKey:@"result"];
  1308. [ret setValue:@"3" forKey:@"detail_section_count"];
  1309. // int count=0;
  1310. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1311. {
  1312. if (sqlite3_step(statement) == SQLITE_ROW)
  1313. {
  1314. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1315. char *name = (char*)sqlite3_column_text(statement, 0);
  1316. if(name==nil)
  1317. name="";
  1318. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1319. char *description = (char*)sqlite3_column_text(statement, 1);
  1320. if(description==nil)
  1321. description="";
  1322. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  1323. int product_id = sqlite3_column_int(statement, 2);
  1324. char *color = (char*)sqlite3_column_text(statement, 3);
  1325. if(color==nil)
  1326. color="";
  1327. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  1328. //
  1329. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  1330. // if(legcolor==nil)
  1331. // legcolor="";
  1332. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  1333. //
  1334. //
  1335. int availability = sqlite3_column_int(statement, 5);
  1336. //
  1337. int incoming_stock = sqlite3_column_int(statement, 6);
  1338. char *demension = (char*)sqlite3_column_text(statement, 7);
  1339. if(demension==nil)
  1340. demension="";
  1341. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  1342. // ,,,,,,,,,
  1343. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  1344. if(seat_height==nil)
  1345. seat_height="";
  1346. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  1347. char *material = (char*)sqlite3_column_text(statement, 9);
  1348. if(material==nil)
  1349. material="";
  1350. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  1351. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  1352. if(box_dim==nil)
  1353. box_dim="";
  1354. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  1355. char *volume = (char*)sqlite3_column_text(statement, 11);
  1356. if(volume==nil)
  1357. volume="";
  1358. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  1359. double weight = sqlite3_column_double(statement, 12);
  1360. char *model_set = (char*)sqlite3_column_text(statement, 13);
  1361. if(model_set==nil)
  1362. model_set="";
  1363. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  1364. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  1365. if(load_ability==nil)
  1366. load_ability="";
  1367. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  1368. char *default_category = (char*)sqlite3_column_text(statement, 15);
  1369. if(default_category==nil)
  1370. default_category="";
  1371. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  1372. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  1373. if(fabric_content==nil)
  1374. fabric_content="";
  1375. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  1376. char *assembling = (char*)sqlite3_column_text(statement, 17);
  1377. if(assembling==nil)
  1378. assembling="";
  1379. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  1380. char *made_in = (char*)sqlite3_column_text(statement, 18);
  1381. if(made_in==nil)
  1382. made_in="";
  1383. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  1384. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  1385. if(special_remarks==nil)
  1386. special_remarks="";
  1387. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  1388. int stockUcom = sqlite3_column_double(statement, 20);
  1389. char *product_group = (char*)sqlite3_column_text(statement, 21);
  1390. if(product_group==nil)
  1391. product_group="";
  1392. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  1393. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  1394. // if(fashion_selector==nil)
  1395. // fashion_selector="";
  1396. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  1397. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  1398. if(selector_field==nil)
  1399. selector_field="";
  1400. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  1401. char *property_field = (char*)sqlite3_column_text(statement, 23);
  1402. if(property_field==nil)
  1403. property_field="";
  1404. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  1405. char *packaging = (char*)sqlite3_column_text(statement, 24);
  1406. if(packaging==nil)
  1407. packaging="";
  1408. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  1409. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  1410. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  1411. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  1412. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  1413. [img_section setValue:model_s_img forKey:@"model_s_img"];
  1414. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  1415. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  1416. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  1417. NSString* Availability=nil;
  1418. if(availability>0)
  1419. Availability=[NSString stringWithFormat:@"%d",availability];
  1420. else
  1421. Availability = @"In Production";
  1422. [img_section setValue:Availability forKey:@"Availability"];
  1423. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  1424. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  1425. char *eta = (char*)sqlite3_column_text(statement, 25);
  1426. if(eta==nil)
  1427. eta="";
  1428. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  1429. [img_section setValue:nseta forKey:@"ETA"];
  1430. int item_id = sqlite3_column_int(statement, 26);
  1431. NSString* Price=nil;
  1432. if(appDelegate.bLogin==false)
  1433. Price=@"Must Sign in.";
  1434. else
  1435. {
  1436. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1437. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  1438. if(price==nil)
  1439. Price=@"No Price.";
  1440. else
  1441. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1442. }
  1443. [img_section setValue:Price forKey:@"price"];
  1444. [img_section setValue:nsname forKey:@"model_name"];
  1445. [img_section setValue:nsdescription forKey:@"model_descrition"];
  1446. [ret setObject:img_section forKey:@"img_section"];
  1447. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  1448. int detail0_item_count=0;
  1449. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1450. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1451. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1452. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1453. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1454. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1455. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1456. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1457. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1458. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1459. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1460. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1461. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1462. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  1463. NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  1464. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  1465. {
  1466. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  1467. [detail0_section setObject:[self model_kvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1468. }
  1469. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  1470. [detail0_section setValue:@"kv" forKey:@"type"];
  1471. [detail0_section setValue:@"Product Information" forKey:@"title"];
  1472. [ret setObject:detail0_section forKey:@"detail_0"];
  1473. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  1474. // [detail1_section setValue:@"detail" forKey:@"target"];
  1475. // [detail1_section setValue:@"popup" forKey:@"action"];
  1476. // [detail1_section setValue:@"content" forKey:@"type"];
  1477. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  1478. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  1479. // [detail1_section setValue:@"true" forKey:@"single_row"];
  1480. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  1481. [ret setObject:detail1_section forKey:@"detail_1"];
  1482. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  1483. [detail2_section setValue:@"detail" forKey:@"target"];
  1484. [detail2_section setValue:@"popup" forKey:@"action"];
  1485. [detail2_section setValue:@"content" forKey:@"type"];
  1486. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  1487. [detail2_section setValue:@"true" forKey:@"single_row"];
  1488. [detail2_section setValue:@"local" forKey:@"data"];
  1489. [ret setObject:detail2_section forKey:@"detail_2"];
  1490. }
  1491. sqlite3_finalize(statement);
  1492. }
  1493. else
  1494. {
  1495. [ret setValue:@"8" forKey:@"result"];
  1496. }
  1497. NSLog(@"count:%d",count);
  1498. [iSalesDB close_db:db];
  1499. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1500. return ret;
  1501. }
  1502. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  1503. NSString* orderCode = [params valueForKey:@"orderCode"];
  1504. NSString* keyword = [params valueForKey:@"keyword"];
  1505. keyword=keyword.lowercaseString;
  1506. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue];
  1507. int limit = [[params valueForKey:@"limit"] intValue];
  1508. int offset = [[params valueForKey:@"offset"] intValue];
  1509. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1510. NSString *limit_str = @"";
  1511. if (limited) {
  1512. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  1513. }
  1514. sqlite3 *db = [iSalesDB get_db];
  1515. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  1516. UIApplication * app = [UIApplication sharedApplication];
  1517. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1518. NSString *sqlQuery = nil;
  1519. if(exactMatch )
  1520. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete 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 ;
  1521. else
  1522. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete 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'%%%@%%') and m.is_active = 1 order by m.name %@ ;",appDelegate.order_code, 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
  1523. DebugLog(@"offline_search sql:%@",sqlQuery);
  1524. sqlite3_stmt * statement;
  1525. [ret setValue:@"2" forKey:@"result"];
  1526. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  1527. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  1528. // int count=0;
  1529. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1530. {
  1531. int i=0;
  1532. while (sqlite3_step(statement) == SQLITE_ROW)
  1533. {
  1534. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1535. // char *name = (char*)sqlite3_column_text(statement, 1);
  1536. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1537. char *name = (char*)sqlite3_column_text(statement, 0);
  1538. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1539. char *description = (char*)sqlite3_column_text(statement, 1);
  1540. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  1541. int product_id = sqlite3_column_int(statement, 2);
  1542. // char *url = (char*)sqlite3_column_text(statement, 3);
  1543. // if(url==nil)
  1544. // url="";
  1545. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1546. int wid = sqlite3_column_int(statement, 3);
  1547. int closeout = sqlite3_column_int(statement, 4);
  1548. int cid = sqlite3_column_int(statement, 5);
  1549. int wisdelete = sqlite3_column_int(statement, 6);
  1550. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  1551. if(wid !=0 && wisdelete != 1)
  1552. [item setValue:@"true" forKey:@"wish_exists"];
  1553. else
  1554. [item setValue:@"false" forKey:@"wish_exists"];
  1555. if(closeout==0)
  1556. [item setValue:@"false" forKey:@"is_closeout"];
  1557. else
  1558. [item setValue:@"true" forKey:@"is_closeout"];
  1559. if(cid==0)
  1560. [item setValue:@"false" forKey:@"cart_exists"];
  1561. else
  1562. [item setValue:@"true" forKey:@"cart_exists"];
  1563. [item addEntriesFromDictionary:imgjson];
  1564. // [item setValue:nsurl forKey:@"img"];
  1565. [item setValue:nsname forKey:@"fash_name"];
  1566. [item setValue:nsdescription forKey:@"description"];
  1567. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  1568. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  1569. i++;
  1570. }
  1571. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  1572. [ret setObject:items forKey:@"items"];
  1573. sqlite3_finalize(statement);
  1574. }
  1575. NSLog(@"count:%d",count);
  1576. [iSalesDB close_db:db];
  1577. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1578. return ret;
  1579. }
  1580. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  1581. {
  1582. return [self search:params limited:YES];
  1583. }
  1584. +(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  1585. {
  1586. UIApplication * app = [UIApplication sharedApplication];
  1587. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1588. appDelegate.disable_trigger=true;
  1589. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  1590. appDelegate.disable_trigger=false;
  1591. //
  1592. // NSString* user = [params valueForKey:@"user"];
  1593. //
  1594. // NSString* password = [params valueForKey:@"password"];
  1595. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1596. ret[@"result"]=[NSNumber numberWithInt:2 ];
  1597. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  1598. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  1599. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  1600. [appDelegate update_count_mark];
  1601. appDelegate.can_show_price =false;
  1602. appDelegate.can_see_price =false;
  1603. appDelegate.can_create_portfolio =false;
  1604. appDelegate.can_create_order =false;
  1605. appDelegate.can_cancel_order =false;
  1606. appDelegate.can_set_cart_price =false;
  1607. appDelegate.can_delete_order =false;
  1608. appDelegate.can_submit_order =false;
  1609. appDelegate.can_set_tearsheet_price =false;
  1610. appDelegate.can_update_contact_info = false;
  1611. appDelegate.save_order_logout = false;
  1612. appDelegate.submit_order_logout = false;
  1613. appDelegate.alert_sold_in_quantities = false;
  1614. appDelegate.ipad_perm =nil ;
  1615. appDelegate.user_type = USER_ROLE_UNKNOWN;
  1616. appDelegate.OrderFilter= nil;
  1617. [appDelegate SetSo:nil];
  1618. [appDelegate set_main_button_panel];
  1619. // sqlite3 *db = [iSalesDB get_db];
  1620. //
  1621. //
  1622. //
  1623. //
  1624. //
  1625. // 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"]];
  1626. //
  1627. //
  1628. //
  1629. //
  1630. // DebugLog(@"offline_login sql:%@",sqlQuery);
  1631. // sqlite3_stmt * statement;
  1632. //
  1633. //
  1634. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1635. //
  1636. //
  1637. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1638. // {
  1639. //
  1640. //
  1641. // if (sqlite3_step(statement) == SQLITE_ROW)
  1642. // {
  1643. //
  1644. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  1645. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  1646. //
  1647. //
  1648. //
  1649. // int can_show_price = sqlite3_column_int(statement, 0);
  1650. // int can_see_price = sqlite3_column_int(statement, 1);
  1651. //
  1652. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  1653. // if(contact_id==nil)
  1654. // contact_id="";
  1655. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  1656. //
  1657. // int user_type = sqlite3_column_int(statement, 3);
  1658. //
  1659. // int can_cancel_order = sqlite3_column_int(statement, 4);
  1660. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  1661. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  1662. // int can_delete_order = sqlite3_column_int(statement, 7);
  1663. // int can_submit_order = sqlite3_column_int(statement, 8);
  1664. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  1665. // int can_create_order = sqlite3_column_int(statement, 10);
  1666. //
  1667. //
  1668. // char *mode = (char*)sqlite3_column_text(statement, 11);
  1669. // if(mode==nil)
  1670. // mode="";
  1671. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  1672. //
  1673. //
  1674. // char *username = (char*)sqlite3_column_text(statement, 12);
  1675. // if(username==nil)
  1676. // username="";
  1677. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  1678. //
  1679. //
  1680. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  1681. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  1682. // [header setValue:nscontact_id forKey:@"contact_id"];
  1683. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  1684. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  1685. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  1686. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  1687. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  1688. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  1689. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  1690. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  1691. //
  1692. // [header setValue:nsusername forKey:@"username"];
  1693. //
  1694. //
  1695. // [ret setObject:header forKey:@"header"];
  1696. // [ret setValue:nsmode forKey:@"mode"];
  1697. //
  1698. //
  1699. // }
  1700. //
  1701. //
  1702. //
  1703. // sqlite3_finalize(statement);
  1704. // }
  1705. //
  1706. //
  1707. //
  1708. // [iSalesDB close_db:db];
  1709. //
  1710. //
  1711. //
  1712. //
  1713. // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1714. return ret;
  1715. }
  1716. +(NSData*) offline_createorder :(NSMutableDictionary *) params
  1717. {
  1718. UIApplication * app = [UIApplication sharedApplication];
  1719. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1720. appDelegate.disable_trigger=true;
  1721. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  1722. appDelegate.disable_trigger=false;
  1723. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1724. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  1725. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  1726. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  1727. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  1728. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  1729. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  1730. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  1731. // NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  1732. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  1733. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  1734. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  1735. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  1736. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  1737. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  1738. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  1739. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  1740. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  1741. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  1742. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  1743. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  1744. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  1745. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  1746. [arr_name addObject:customer_first_name];
  1747. [arr_name addObject:customer_last_name];
  1748. NSString *contact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  1749. // default ship from
  1750. NSString *ship_from_default_sql = @"select contact_id,decrypt(company_name),decrypt(addr_1),first_name || ' ' || last_name,email,fax,decrypt(phone) from offline_contact where contact_id = 'NPD';";
  1751. __block NSString *cid = @"";
  1752. __block NSString *name = @"";
  1753. __block NSString *ext = @"";
  1754. __block NSString *contact = @"";
  1755. __block NSString *email = @"";
  1756. __block NSString *fax = @"";
  1757. __block NSString *phone = @"";
  1758. sqlite3 *db = [iSalesDB get_db];
  1759. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  1760. cid = [self textAtColumn:0 statement:statment];
  1761. name = [self textAtColumn:1 statement:statment];
  1762. ext = [self textAtColumn:2 statement:statment];
  1763. contact = [self textAtColumn:3 statement:statment];
  1764. email = [self textAtColumn:4 statement:statment];
  1765. fax = [self textAtColumn:5 statement:statment];
  1766. phone = [self textAtColumn:6 statement:statment];
  1767. }];
  1768. NSString* so_id = [self get_offline_soid:db];
  1769. if(so_id==nil)
  1770. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  1771. 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) values('%@',0,'%@','%@','%@','%@','%@','%@','%@','%@',1,'%@','%@','%@','%@','%@','%@','%@')",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];
  1772. int result =[iSalesDB execSql:sql_neworder db:db];
  1773. [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  1774. //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'
  1775. //soId
  1776. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  1777. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  1778. sqlite3_stmt * statement;
  1779. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1780. {
  1781. if (sqlite3_step(statement) == SQLITE_ROW)
  1782. {
  1783. // char *name = (char*)sqlite3_column_text(statement, 1);
  1784. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1785. //ret = sqlite3_column_int(statement, 0);
  1786. char *soId = (char*)sqlite3_column_text(statement, 0);
  1787. if(soId==nil)
  1788. soId="";
  1789. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  1790. [ret setValue:nssoId forKey:@"soId"];
  1791. [ret setValue:nssoId forKey:@"orderCode"];
  1792. }
  1793. sqlite3_finalize(statement);
  1794. }
  1795. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  1796. [ret setValue:@"Regular Mode" forKey:@"mode"];
  1797. [iSalesDB close_db:db];
  1798. return [RAUtils dict2data:ret];
  1799. }
  1800. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  1801. {
  1802. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1803. NSString* orderCode = [params valueForKey:@"orderCode"];
  1804. UIApplication * app = [UIApplication sharedApplication];
  1805. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1806. if(![appDelegate.order_code isEqualToString:orderCode]&& appDelegate.order_code.length!=0)
  1807. {
  1808. appDelegate.disable_trigger=true;
  1809. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  1810. appDelegate.disable_trigger=false;
  1811. }
  1812. sqlite3 *db = [iSalesDB get_db];
  1813. int cart_count=[self query_ordercartcount:orderCode db:db];
  1814. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  1815. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  1816. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  1817. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1818. [iSalesDB close_db:db];
  1819. return [RAUtils dict2data:ret];
  1820. }
  1821. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  1822. {
  1823. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1824. sqlite3 *db = [iSalesDB get_db];
  1825. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  1826. int count =0;
  1827. if(params[@"count"]!=nil)
  1828. {
  1829. count = [params[@"count"] intValue];
  1830. }
  1831. NSString* product_id=params[@"product_id"];
  1832. NSString* orderCode=params[@"orderCode"];
  1833. NSString *qty = params[@"qty"];
  1834. NSArray* arr_id=[RAUtils string2arr:product_id separator:@","];
  1835. NSArray *qty_arr = [RAUtils string2arr:qty separator:@","];
  1836. for(int i=0;i<arr_id.count;i++)
  1837. {
  1838. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  1839. int item_qty= count;
  1840. if (qty) {
  1841. item_qty = [qty_arr[i] integerValue];
  1842. }
  1843. if(item_qty==0)
  1844. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  1845. NSString* sql=nil;
  1846. sqlite3_stmt *stmt;
  1847. if(_id<0)
  1848. {
  1849. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  1850. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  1851. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  1852. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],[arr_id[i] length],NULL);
  1853. sqlite3_bind_text(stmt,2,[orderCode UTF8String],[orderCode length],NULL);
  1854. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%d",item_qty] UTF8String],[[NSString stringWithFormat:@"%d",item_qty] length],NULL);
  1855. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  1856. }
  1857. else
  1858. {
  1859. if (qty) {
  1860. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  1861. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  1862. sqlite3_bind_int(stmt, 1, _id);
  1863. } else {
  1864. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  1865. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  1866. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%d",item_qty] UTF8String],[[NSString stringWithFormat:@"%d",item_qty] length],NULL);
  1867. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],[[NSString stringWithFormat:@"%d",_id] length],NULL);
  1868. }
  1869. }
  1870. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  1871. [iSalesDB execSql:@"ROLLBACK" db:db];
  1872. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  1873. [iSalesDB close_db:db];
  1874. DebugLog(@"add to cart error");
  1875. return [RAUtils dict2data:ret];
  1876. }
  1877. }
  1878. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1879. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  1880. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  1881. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  1882. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1883. [iSalesDB execSql:@"END TRANSACTION" db:db];
  1884. [iSalesDB close_db:db];
  1885. return [RAUtils dict2data:ret];
  1886. }
  1887. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute
  1888. {
  1889. UIApplication * app = [UIApplication sharedApplication];
  1890. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1891. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  1892. 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];
  1893. sqlite3_stmt * statement;
  1894. int count=0;
  1895. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1896. {
  1897. while (sqlite3_step(statement) == SQLITE_ROW)
  1898. {
  1899. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  1900. int bitem_id = sqlite3_column_int(statement, 0);
  1901. int bitem_qty = sqlite3_column_int(statement, 1);
  1902. char *name = (char*)sqlite3_column_text(statement, 2);
  1903. if(name==nil)
  1904. name="";
  1905. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  1906. char *description = (char*)sqlite3_column_text(statement, 3);
  1907. if(description==nil)
  1908. description="";
  1909. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  1910. double unit_price = sqlite3_column_double(statement, 4);
  1911. int use_unitprice = sqlite3_column_int(statement, 5);
  1912. if(use_unitprice!=1)
  1913. {
  1914. unit_price=[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db].doubleValue;
  1915. }
  1916. itemjson[@"model"]=nsname;
  1917. itemjson[@"description"]=nsdescription;
  1918. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  1919. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  1920. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  1921. if(compute)
  1922. {
  1923. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute];
  1924. }
  1925. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1926. count++;
  1927. }
  1928. sqlite3_finalize(statement);
  1929. }
  1930. ret[@"count"]=@(count);
  1931. if(count==0)
  1932. return nil;
  1933. else
  1934. return ret;
  1935. }
  1936. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  1937. {
  1938. //compute: add part to subtotal;
  1939. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  1940. dict_item[@(item_id)]=@"1";
  1941. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  1942. double cuft=0;
  1943. double weight=0;
  1944. int carton=0;
  1945. 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];
  1946. sqlite3_stmt * statement;
  1947. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1948. {
  1949. if (sqlite3_step(statement) == SQLITE_ROW)
  1950. {
  1951. double ulength = sqlite3_column_double(statement, 0);
  1952. double uwidth = sqlite3_column_double(statement, 1);
  1953. double uheight = sqlite3_column_double(statement, 2);
  1954. double uweight = sqlite3_column_double(statement, 3);
  1955. double mlength = sqlite3_column_double(statement, 4);
  1956. double mwidth = sqlite3_column_double(statement, 5);
  1957. double mheight = sqlite3_column_double(statement, 6);
  1958. double mweight = sqlite3_column_double(statement, 7);
  1959. double ilength = sqlite3_column_double(statement, 8);
  1960. double iwidth = sqlite3_column_double(statement, 9);
  1961. double iheight = sqlite3_column_double(statement, 10);
  1962. double iweight = sqlite3_column_double(statement, 11);
  1963. int pcs = sqlite3_column_int(statement,12);
  1964. int mpack = sqlite3_column_int(statement, 13);
  1965. int ipack = sqlite3_column_int(statement, 14);
  1966. double ucbf = sqlite3_column_double(statement, 15);
  1967. double icbf = sqlite3_column_double(statement, 16);
  1968. double mcbf = sqlite3_column_double(statement, 17);
  1969. if(ipack==0)
  1970. {
  1971. carton= count/mpack ;
  1972. weight = mweight*carton;
  1973. cuft= carton*(mlength*mwidth*mheight);
  1974. int remain=count%mpack;
  1975. if(remain==0)
  1976. {
  1977. //do nothing;
  1978. }
  1979. else
  1980. {
  1981. carton++;
  1982. weight += uweight*remain;
  1983. cuft += (ulength*uwidth*uheight)*remain;
  1984. }
  1985. }
  1986. else
  1987. {
  1988. carton = count/(mpack*ipack);
  1989. weight = mweight*carton;
  1990. cuft= carton*(mlength*mwidth*mheight);
  1991. int remain=count%(mpack*ipack);
  1992. if(remain==0)
  1993. {
  1994. // do nothing;
  1995. }
  1996. else
  1997. {
  1998. carton++;
  1999. int icarton =remain/ipack;
  2000. int iremain=remain%ipack;
  2001. weight += iweight*icarton;
  2002. cuft += (ilength*iwidth*iheight)*icarton;
  2003. if(iremain==0)
  2004. {
  2005. //do nothing;
  2006. }
  2007. else
  2008. {
  2009. weight += uweight*iremain;
  2010. cuft += (ulength*uwidth*uheight)*iremain;
  2011. }
  2012. }
  2013. }
  2014. #ifdef BUILD_NPD
  2015. cuft=ucbf*count;
  2016. weight= uweight*count;
  2017. #endif
  2018. }
  2019. sqlite3_finalize(statement);
  2020. }
  2021. if(compute)
  2022. {
  2023. NSArray * arr_count=nil;
  2024. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  2025. for(int i=0;i<arr_bundle.count;i++)
  2026. {
  2027. dict_item[arr_bundle[i]]=@"1";
  2028. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  2029. cuft+=[bundlejson[@"cuft"] doubleValue];
  2030. weight+=[bundlejson[@"weight"] doubleValue];
  2031. carton+=[bundlejson[@"carton"] intValue];
  2032. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  2033. }
  2034. }
  2035. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  2036. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  2037. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  2038. ret[@"items"]=dict_item;
  2039. return ret;
  2040. }
  2041. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  2042. {
  2043. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  2044. // 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 ];
  2045. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  2046. DebugLog(@"offline_login sql:%@",sqlQuery);
  2047. sqlite3_stmt * statement;
  2048. int cart_count=0;
  2049. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2050. if ( dbresult== SQLITE_OK)
  2051. {
  2052. while (sqlite3_step(statement) == SQLITE_ROW)
  2053. {
  2054. int item_id = sqlite3_column_int(statement, 0);
  2055. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  2056. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  2057. }
  2058. sqlite3_finalize(statement);
  2059. }
  2060. return cart_count;
  2061. }
  2062. +(NSData*) offline_requestcart :(NSMutableDictionary *) params
  2063. {
  2064. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2065. sqlite3 *db = [iSalesDB get_db];
  2066. UIApplication * app = [UIApplication sharedApplication];
  2067. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2068. NSString* orderCode=params[@"orderCode"];
  2069. int sort = [[params objectForKey:@"sort"] intValue];
  2070. NSString *sort_str = @"";
  2071. switch (sort) {
  2072. case 0:{
  2073. sort_str = @"order by c.modify_time desc";
  2074. }
  2075. break;
  2076. case 1:{
  2077. sort_str = @"order by c.modify_time asc";
  2078. }
  2079. break;
  2080. case 2:{
  2081. sort_str = @"order by m.name asc";
  2082. }
  2083. break;
  2084. case 3:{
  2085. sort_str = @"order by m.name desc";
  2086. }
  2087. break;
  2088. case 4:{
  2089. sort_str = @"order by m.description asc";
  2090. }
  2091. break;
  2092. default:
  2093. break;
  2094. }
  2095. 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 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 ];
  2096. // 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 ];
  2097. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  2098. DebugLog(@"offline_login sql:%@",sqlQuery);
  2099. sqlite3_stmt * statement;
  2100. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2101. NSDate *date1 = [NSDate date];
  2102. int count=0;
  2103. int cart_count=0;
  2104. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2105. if ( dbresult== SQLITE_OK)
  2106. {
  2107. while (sqlite3_step(statement) == SQLITE_ROW)
  2108. {
  2109. NSDate *row_date = [NSDate date];
  2110. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2111. int product_id = sqlite3_column_int(statement, 0);
  2112. char *str_price = (char*)sqlite3_column_text(statement, 1);
  2113. int item_id = sqlite3_column_int(statement, 7);
  2114. NSString* Price=nil;
  2115. if(str_price==nil)
  2116. {
  2117. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2118. // NSDate *price_date = [NSDate date];
  2119. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  2120. // DebugLog(@"price time interval");
  2121. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  2122. if(price==nil)
  2123. Price=@"No Price.";
  2124. else
  2125. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2126. }
  2127. else
  2128. {
  2129. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  2130. }
  2131. double discount = sqlite3_column_double(statement, 2);
  2132. int item_count = sqlite3_column_int(statement, 3);
  2133. char *line_note = (char*)sqlite3_column_text(statement, 4);
  2134. NSString *nsline_note=nil;
  2135. if(line_note!=nil)
  2136. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  2137. char *name = (char*)sqlite3_column_text(statement, 5);
  2138. NSString *nsname=nil;
  2139. if(name!=nil)
  2140. nsname= [[NSString alloc]initWithUTF8String:name];
  2141. char *description = (char*)sqlite3_column_text(statement, 6);
  2142. NSString *nsdescription=nil;
  2143. if(description!=nil)
  2144. nsdescription= [[NSString alloc]initWithUTF8String:description];
  2145. int stockUom = sqlite3_column_int(statement, 8);
  2146. int _id = sqlite3_column_int(statement, 9);
  2147. // NSDate *subtotal_date = [NSDate date];
  2148. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  2149. // DebugLog(@"subtotal_date time interval");
  2150. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  2151. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  2152. double weight=[bsubtotaljson[@"weight"] doubleValue];
  2153. int carton=[bsubtotaljson[@"carton"] intValue];
  2154. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  2155. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2156. // NSDate *img_date = [NSDate date];
  2157. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  2158. // DebugLog(@"img_date time interval");
  2159. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  2160. itemjson[@"model"]=nsname;
  2161. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  2162. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  2163. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  2164. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  2165. itemjson[@"check"]=@"true";
  2166. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  2167. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  2168. itemjson[@"unit_price"]=Price;
  2169. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  2170. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  2171. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  2172. itemjson[@"note"]=nsline_note;
  2173. // NSDate *date2 = [NSDate date];
  2174. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  2175. // DebugLog(@"model_bundle time interval");
  2176. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  2177. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2178. count++;
  2179. // DebugLog(@"row time interval");
  2180. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  2181. }
  2182. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2183. sqlite3_finalize(statement);
  2184. }
  2185. DebugLog(@"request cart total time interval");
  2186. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  2187. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2188. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2189. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2190. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2191. ret[@"count"]=[NSNumber numberWithInt:count ];
  2192. ret[@"mode"]=@"Regular Mode";
  2193. [iSalesDB close_db:db];
  2194. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  2195. DebugLog(@"general notes :%@",general_note);
  2196. ret[@"general_note"]= general_note;
  2197. return [RAUtils dict2data:ret];
  2198. }
  2199. +(NSData*) offline_login :(NSMutableDictionary *) params
  2200. {
  2201. NSString* user = [params valueForKey:@"user"];
  2202. NSString* password = [params valueForKey:@"password"];
  2203. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2204. sqlite3 *db = [iSalesDB get_db];
  2205. 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"]];
  2206. DebugLog(@"offline_login sql:%@",sqlQuery);
  2207. sqlite3_stmt * statement;
  2208. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2209. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2210. {
  2211. if (sqlite3_step(statement) == SQLITE_ROW)
  2212. {
  2213. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2214. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  2215. int can_show_price = sqlite3_column_int(statement, 0);
  2216. int can_see_price = sqlite3_column_int(statement, 1);
  2217. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  2218. if(contact_id==nil)
  2219. contact_id="";
  2220. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2221. int user_type = sqlite3_column_int(statement, 3);
  2222. int can_cancel_order = sqlite3_column_int(statement, 4);
  2223. int can_set_cart_price = sqlite3_column_int(statement, 5);
  2224. int can_create_portfolio = sqlite3_column_int(statement, 6);
  2225. int can_delete_order = sqlite3_column_int(statement, 7);
  2226. int can_submit_order = sqlite3_column_int(statement, 8);
  2227. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  2228. int can_create_order = sqlite3_column_int(statement, 10);
  2229. char *mode = (char*)sqlite3_column_text(statement, 11);
  2230. if(mode==nil)
  2231. mode="";
  2232. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  2233. char *username = (char*)sqlite3_column_text(statement, 12);
  2234. if(username==nil)
  2235. username="";
  2236. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  2237. int can_update_contact_info = sqlite3_column_int(statement, 13);
  2238. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  2239. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  2240. [header setValue:nscontact_id forKey:@"contact_id"];
  2241. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  2242. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  2243. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  2244. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  2245. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  2246. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  2247. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  2248. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  2249. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  2250. [header setValue:nsusername forKey:@"username"];
  2251. NSError* error=nil;
  2252. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  2253. [header setValue:statusFilter forKey:@"statusFilter"];
  2254. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  2255. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  2256. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  2257. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  2258. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  2259. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  2260. [ret setObject:header forKey:@"header"];
  2261. [ret setValue:nsmode forKey:@"mode"];
  2262. }
  2263. sqlite3_finalize(statement);
  2264. }
  2265. [iSalesDB close_db:db];
  2266. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2267. return [RAUtils dict2data:ret];
  2268. }
  2269. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  2270. {
  2271. NSString* contactId = [params valueForKey:@"contactId"];
  2272. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2273. sqlite3 *db = [iSalesDB get_db];
  2274. NSString *sqlQuery = nil;
  2275. {
  2276. 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 from offline_contact where contact_id='%@'",contactId];
  2277. }
  2278. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  2279. sqlite3_stmt * statement;
  2280. [ret setValue:@"2" forKey:@"result"];
  2281. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2282. {
  2283. //int i = 0;
  2284. if (sqlite3_step(statement) == SQLITE_ROW)
  2285. {
  2286. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2287. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2288. // int editable = sqlite3_column_int(statement, 0);
  2289. char *company_name = (char*)sqlite3_column_text(statement, 1);
  2290. NSString *nscompany_name =nil;
  2291. if(company_name==nil)
  2292. nscompany_name=@"";
  2293. else
  2294. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  2295. char *country = (char*)sqlite3_column_text(statement, 2);
  2296. if(country==nil)
  2297. country="";
  2298. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  2299. // char *addr = (char*)sqlite3_column_text(statement, 3);
  2300. // if(addr==nil)
  2301. // addr="";
  2302. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  2303. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  2304. if(zipcode==nil)
  2305. zipcode="";
  2306. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  2307. char *state = (char*)sqlite3_column_text(statement, 5);
  2308. if(state==nil)
  2309. state="";
  2310. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  2311. char *city = (char*)sqlite3_column_text(statement, 6);
  2312. if(city==nil)
  2313. city="";
  2314. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  2315. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  2316. // NSString *nscontact_name = nil;
  2317. // if(contact_name==nil)
  2318. // nscontact_name=@"";
  2319. // else
  2320. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  2321. char *phone = (char*)sqlite3_column_text(statement, 8);
  2322. NSString *nsphone = nil;
  2323. if(phone==nil)
  2324. nsphone=@"";
  2325. else
  2326. nsphone= [[NSString alloc]initWithUTF8String:phone];
  2327. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  2328. if(contact_id==nil)
  2329. contact_id="";
  2330. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2331. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  2332. if(addr_1==nil)
  2333. addr_1="";
  2334. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  2335. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  2336. if(addr_2==nil)
  2337. addr_2="";
  2338. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  2339. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  2340. if(addr_3==nil)
  2341. addr_3="";
  2342. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  2343. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  2344. if(addr_4==nil)
  2345. addr_4="";
  2346. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  2347. char *first_name = (char*)sqlite3_column_text(statement, 14);
  2348. if(first_name==nil)
  2349. first_name="";
  2350. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  2351. char *last_name = (char*)sqlite3_column_text(statement, 15);
  2352. if(last_name==nil)
  2353. last_name="";
  2354. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  2355. char *fax = (char*)sqlite3_column_text(statement, 16);
  2356. NSString *nsfax = nil;
  2357. if(fax==nil)
  2358. nsfax=@"";
  2359. else
  2360. nsfax= [[NSString alloc]initWithUTF8String:fax];
  2361. char *email = (char*)sqlite3_column_text(statement, 17);
  2362. NSString *nsemail = nil;
  2363. if(email==nil)
  2364. nsemail=@"";
  2365. else
  2366. nsemail= [[NSString alloc]initWithUTF8String:email];
  2367. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  2368. NSString *nsimg_0 = nil;
  2369. if(img_0==nil)
  2370. nsimg_0=@"";
  2371. else
  2372. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  2373. [self copy_bcardImg:nsimg_0];
  2374. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  2375. NSString *nsimg_1 = nil;
  2376. if(img_1==nil)
  2377. nsimg_1=@"";
  2378. else
  2379. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2380. [self copy_bcardImg:nsimg_1];
  2381. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  2382. NSString *nsimg_2 = nil;
  2383. if(img_2==nil)
  2384. nsimg_2=@"";
  2385. else
  2386. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  2387. [self copy_bcardImg:nsimg_2];
  2388. char *price_type = (char*)sqlite3_column_text(statement, 21);
  2389. NSString *nsprice_type = nil;
  2390. if(price_type==nil)
  2391. nsprice_type=@"";
  2392. else
  2393. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  2394. char *notes = (char*)sqlite3_column_text(statement, 22);
  2395. NSString *nsnotes = nil;
  2396. if(notes==nil)
  2397. nsnotes=@"";
  2398. else
  2399. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  2400. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  2401. NSString *nssalesrep = nil;
  2402. if(salesrep==nil)
  2403. nssalesrep=@"";
  2404. else
  2405. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  2406. {
  2407. // decrypt
  2408. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  2409. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  2410. nsphone=[AESCrypt fastdecrypt:nsphone];
  2411. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  2412. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  2413. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  2414. }
  2415. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  2416. [arr_name addObject:nsfirst_name];
  2417. [arr_name addObject:nslast_name];
  2418. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  2419. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  2420. [arr_ext addObject:nsaddr_1];
  2421. [arr_ext addObject:nsaddr_2];
  2422. [arr_ext addObject:nsaddr_3];
  2423. [arr_ext addObject:nsaddr_4];
  2424. [arr_ext addObject:@"\r\n"];
  2425. [arr_ext addObject:nscity];
  2426. [arr_ext addObject:nsstate];
  2427. [arr_ext addObject:nszipcode];
  2428. [arr_ext addObject:nscountry];
  2429. NSString *nsext=[RAUtils arr2string:arr_ext separator:@", " trim:true];
  2430. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  2431. [item setValue:nsimg_2 forKey:@"business_card_2"];
  2432. [item setValue:nsimg_0 forKey:@"business_card_0"];
  2433. [item setValue:nscountry forKey:@"customer_country"];
  2434. [item setValue:nsphone forKey:@"customer_phone"];
  2435. [item setValue:nsimg_1 forKey:@"business_card_1"];
  2436. [item setValue:nscompany_name forKey:@"customer_name"];
  2437. [item setValue:nsprice_type forKey:@"customer_price_type"];
  2438. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  2439. [item setValue:nsext forKey:@"customer_contact_ext"];
  2440. [item setValue:nszipcode forKey:@"customer_zipcode"];
  2441. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  2442. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  2443. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  2444. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  2445. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  2446. [item setValue:nslast_name forKey:@"customer_last_name"];
  2447. [item setValue:nscity forKey:@"customer_city"];
  2448. [item setValue:nsstate forKey:@"customer_state"];
  2449. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  2450. [item setValue:contactId forKey:@"customer_cid"];
  2451. [item setValue:nscontact_name forKey:@"customer_contact"];
  2452. [item setValue:nsfax forKey:@"customer_fax"];
  2453. [item setValue:nsemail forKey:@"customer_email"];
  2454. [ret setObject:item forKey:@"customerInfo"];
  2455. // i++;
  2456. }
  2457. UIApplication * app = [UIApplication sharedApplication];
  2458. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2459. [ret setValue:appDelegate.mode forKey:@"mode"];
  2460. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  2461. sqlite3_finalize(statement);
  2462. }
  2463. [iSalesDB close_db:db];
  2464. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2465. return ret;
  2466. }
  2467. + (bool) copy_bcardImg:(NSString*) filename
  2468. {
  2469. if(filename.length==0)
  2470. return false;
  2471. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2472. bool ret=false;
  2473. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2474. NSString *cachefolder = [paths objectAtIndex:0];
  2475. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  2476. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  2477. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  2478. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  2479. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  2480. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  2481. //
  2482. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  2483. NSFileManager* fileManager = [NSFileManager defaultManager];
  2484. BOOL bdir=NO;
  2485. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  2486. {
  2487. NSError *error = nil;
  2488. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  2489. {
  2490. ret=false;
  2491. }
  2492. else
  2493. {
  2494. ret=true;
  2495. }
  2496. // NSError *error = nil;
  2497. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  2498. //
  2499. // if(!bsuccess)
  2500. // {
  2501. // DebugLog(@"Create offline_createimg folder failed");
  2502. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2503. // return [RAUtils dict2data:ret];
  2504. // }
  2505. // if(bsuccess)
  2506. // {
  2507. // sqlite3 *db = [self get_db];
  2508. //
  2509. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  2510. // [iSalesDB close_db:db];
  2511. // }
  2512. }
  2513. return ret;
  2514. //
  2515. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  2516. // if(bsuccess)
  2517. // {
  2518. // NSError *error = nil;
  2519. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  2520. // {
  2521. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2522. // }
  2523. // else
  2524. // {
  2525. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  2526. //
  2527. // ret[@"img_url_aname"]=filename;
  2528. // ret[@"img_url"]=savedImagePath;
  2529. // }
  2530. // }
  2531. }
  2532. +(NSData *) offline_saveBusinesscard:(NSData *) image
  2533. {
  2534. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2535. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2536. NSString *cachefolder = [paths objectAtIndex:0];
  2537. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  2538. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  2539. NSFileManager* fileManager = [NSFileManager defaultManager];
  2540. BOOL bdir=YES;
  2541. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  2542. {
  2543. NSError *error = nil;
  2544. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  2545. if(!bsuccess)
  2546. {
  2547. DebugLog(@"Create offline_createimg folder failed");
  2548. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2549. return [RAUtils dict2data:ret];
  2550. }
  2551. // if(bsuccess)
  2552. // {
  2553. // sqlite3 *db = [self get_db];
  2554. //
  2555. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  2556. // [iSalesDB close_db:db];
  2557. // }
  2558. }
  2559. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  2560. //JEPG格式
  2561. //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  2562. NSString* filename =[NSString stringWithFormat:@"%@.png",[[NSUUID UUID] UUIDString]];
  2563. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  2564. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  2565. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  2566. if(bsuccess)
  2567. {
  2568. NSError *error = nil;
  2569. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  2570. {
  2571. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2572. }
  2573. else
  2574. {
  2575. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  2576. ret[@"img_url_aname"]=filename;
  2577. ret[@"img_url"]=filename;
  2578. }
  2579. }
  2580. else
  2581. {
  2582. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2583. }
  2584. return [RAUtils dict2data:ret];
  2585. }
  2586. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  2587. {
  2588. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  2589. category = [category substringToIndex:3];
  2590. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2591. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  2592. params[@"category"]= category;
  2593. ret[@"params"]= params;
  2594. [ret setValue:@"detail" forKey:@"target"];
  2595. [ret setValue:@"popup" forKey:@"action"];
  2596. [ret setValue:@"content" forKey:@"type"];
  2597. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  2598. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  2599. [ret setValue:@"true" forKey:@"single_row"];
  2600. [ret setValue:@"true" forKey:@"partial_refresh"];
  2601. // sqlite3 *db = [iSalesDB get_db];
  2602. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  2603. 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 ;
  2604. sqlite3_stmt * statement;
  2605. int count = 0;
  2606. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  2607. // int count=0;
  2608. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2609. {
  2610. int i=0;
  2611. while (sqlite3_step(statement) == SQLITE_ROW)
  2612. {
  2613. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2614. // char *name = (char*)sqlite3_column_text(statement, 1);
  2615. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2616. char *name = (char*)sqlite3_column_text(statement, 0);
  2617. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2618. int product_id = sqlite3_column_int(statement, 1);
  2619. char *url = (char*)sqlite3_column_text(statement, 2);
  2620. if(url==nil)
  2621. url="";
  2622. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2623. [item setValue:nsurl forKey:@"picture_path"];
  2624. [item setValue:nsname forKey:@"fash_name"];
  2625. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2626. [item setValue:category forKey:@"category"];
  2627. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2628. i++;
  2629. }
  2630. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2631. sqlite3_finalize(statement);
  2632. }
  2633. NSLog(@"count:%d",count);
  2634. // [iSalesDB close_db:db];
  2635. return ret;
  2636. }
  2637. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  2638. {
  2639. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2640. [ret setValue:key forKey:@"key"];
  2641. [ret setValue:value forKey:@"val"];
  2642. return ret;
  2643. }
  2644. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  2645. {
  2646. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2647. [ret setValue:@"0" forKey:@"img_count"];
  2648. // sqlite3 *db = [iSalesDB get_db];
  2649. 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 ;
  2650. sqlite3_stmt * statement;
  2651. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2652. {
  2653. int i=0;
  2654. if (sqlite3_step(statement) == SQLITE_ROW)
  2655. {
  2656. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2657. // char *name = (char*)sqlite3_column_text(statement, 1);
  2658. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2659. char *value = (char*)sqlite3_column_text(statement, 0);
  2660. if(value==nil)
  2661. value="";
  2662. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  2663. char *key = (char*)sqlite3_column_text(statement, 1);
  2664. if(key==nil)
  2665. key="";
  2666. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  2667. [item setValue:nsvalue forKey:@"val"];
  2668. [item setValue:nskey forKey:@"key"];
  2669. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2670. [ret setValue:@"1" forKey:@"count"];
  2671. i++;
  2672. }
  2673. sqlite3_finalize(statement);
  2674. }
  2675. // [iSalesDB close_db:db];
  2676. return ret;
  2677. }
  2678. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  2679. {
  2680. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2681. [ret setValue:@"0" forKey:@"count"];
  2682. // sqlite3 *db = [iSalesDB get_db];
  2683. 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;
  2684. sqlite3_stmt * statement;
  2685. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2686. {
  2687. int i=0;
  2688. while (sqlite3_step(statement) == SQLITE_ROW)
  2689. {
  2690. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2691. // char *name = (char*)sqlite3_column_text(statement, 1);
  2692. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2693. char *value = (char*)sqlite3_column_text(statement, 0);
  2694. if(value==nil)
  2695. value="";
  2696. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  2697. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  2698. if(selector_display==nil)
  2699. selector_display="";
  2700. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  2701. int product_id = sqlite3_column_int(statement, 2);
  2702. char *category = (char*)sqlite3_column_text(statement, 3);
  2703. if(category==nil)
  2704. category="";
  2705. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  2706. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  2707. [item setValue:nsvalue forKey:@"title"];
  2708. [item setValue:url forKey:@"pic_url"];
  2709. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  2710. [params setValue:@"2" forKey:@"count"];
  2711. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  2712. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  2713. [param0 setValue:@"product_id" forKey:@"name"];
  2714. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  2715. [param1 setValue:nscategory forKey:@"val"];
  2716. [param1 setValue:@"category" forKey:@"name"];
  2717. [params setObject:param0 forKey:@"param_0"];
  2718. [params setObject:param1 forKey:@"param_1"];
  2719. [item setObject:params forKey:@"params"];
  2720. [ret setValue:nsselector_display forKey:@"name"];
  2721. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2722. i++;
  2723. }
  2724. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2725. [ret setValue:@"switch" forKey:@"action"];
  2726. sqlite3_finalize(statement);
  2727. }
  2728. // [iSalesDB close_db:db];
  2729. return ret;
  2730. }
  2731. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  2732. {
  2733. // model 在 category search 显示的图片。
  2734. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  2735. // sqlite3 *db = [iSalesDB get_db];
  2736. 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];
  2737. sqlite3_stmt * statement;
  2738. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2739. {
  2740. while (sqlite3_step(statement) == SQLITE_ROW)
  2741. {
  2742. char *url = (char*)sqlite3_column_text(statement, 0);
  2743. if(url==nil)
  2744. url="";
  2745. int type = sqlite3_column_int(statement, 1);
  2746. if(type==0)
  2747. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  2748. else
  2749. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  2750. }
  2751. sqlite3_finalize(statement);
  2752. }
  2753. // [iSalesDB close_db:db];
  2754. return ret;
  2755. }
  2756. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  2757. {
  2758. int item_id=-1;
  2759. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  2760. sqlite3_stmt * statement;
  2761. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2762. {
  2763. if (sqlite3_step(statement) == SQLITE_ROW)
  2764. {
  2765. item_id = sqlite3_column_int(statement, 0);
  2766. }
  2767. sqlite3_finalize(statement);
  2768. }
  2769. return item_id;
  2770. }
  2771. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  2772. {
  2773. // NSString* ret = @"";
  2774. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  2775. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  2776. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  2777. sqlite3_stmt * statement;
  2778. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2779. {
  2780. while (sqlite3_step(statement) == SQLITE_ROW)
  2781. {
  2782. int bitem_id = sqlite3_column_int(statement, 0);
  2783. int bitem_qty = sqlite3_column_int(statement, 1);
  2784. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  2785. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  2786. }
  2787. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  2788. sqlite3_finalize(statement);
  2789. }
  2790. // if(ret==nil)
  2791. // ret=@"";
  2792. *count=arr_count;
  2793. return arr_bundle;
  2794. }
  2795. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  2796. {
  2797. // get default sold qty, return -1 if model not found;
  2798. int ret = -1;
  2799. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  2800. sqlite3_stmt * statement;
  2801. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2802. {
  2803. if (sqlite3_step(statement) == SQLITE_ROW)
  2804. {
  2805. ret = sqlite3_column_int(statement, 0);
  2806. }
  2807. sqlite3_finalize(statement);
  2808. }
  2809. return ret;
  2810. }
  2811. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  2812. {
  2813. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2814. [ret setValue:@"0" forKey:@"img_count"];
  2815. // sqlite3 *db = [iSalesDB get_db];
  2816. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  2817. sqlite3_stmt * statement;
  2818. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2819. {
  2820. int i=0;
  2821. while (sqlite3_step(statement) == SQLITE_ROW)
  2822. {
  2823. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2824. // char *name = (char*)sqlite3_column_text(statement, 1);
  2825. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2826. char *url = (char*)sqlite3_column_text(statement, 0);
  2827. if(url==nil)
  2828. url="";
  2829. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2830. [item setValue:nsurl forKey:@"s"];
  2831. [item setValue:nsurl forKey:@"l"];
  2832. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  2833. i++;
  2834. }
  2835. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  2836. sqlite3_finalize(statement);
  2837. }
  2838. // [iSalesDB close_db:db];
  2839. return ret;
  2840. }
  2841. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  2842. UIApplication * app = [UIApplication sharedApplication];
  2843. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2844. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2845. sqlite3 *db = [iSalesDB get_db];
  2846. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  2847. NSString* product_id=params[@"product_id"];
  2848. NSString *item_count_str = params[@"item_count"];
  2849. NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  2850. NSArray *item_count_arr = item_count_str ? [RAUtils string2arr:item_count_str separator:@","] : nil;
  2851. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  2852. // NSString *sql = @"";
  2853. for(int i=0;i<arr.count;i++)
  2854. {
  2855. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  2856. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  2857. __block int cart_count = 0;
  2858. if (!item_count_str) {
  2859. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  2860. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2861. NSString *model_set = [self textAtColumn:0 statement:stmt];
  2862. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  2863. cart_count = [[model_set_components lastObject] integerValue];
  2864. }];
  2865. }
  2866. if(count==0)
  2867. {
  2868. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  2869. sqlite3_stmt *stmt;
  2870. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  2871. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],[arr[i] length],NULL);
  2872. if (item_count_arr) {
  2873. sqlite3_bind_int(stmt,2,[item_count_arr[i] integerValue]);
  2874. } else {
  2875. sqlite3_bind_int(stmt,2,cart_count);
  2876. }
  2877. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  2878. [iSalesDB execSql:@"ROLLBACK" db:db];
  2879. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  2880. [iSalesDB close_db:db];
  2881. DebugLog(@"add to wishlist error");
  2882. return ret;
  2883. }
  2884. } else {
  2885. int qty = 0;
  2886. if (item_count_arr) {
  2887. qty = [item_count_arr[i] integerValue];
  2888. } else {
  2889. qty = cart_count;
  2890. }
  2891. 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]];
  2892. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  2893. [iSalesDB execSql:@"ROLLBACK" db:db];
  2894. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  2895. [iSalesDB close_db:db];
  2896. DebugLog(@"add to wishlist error");
  2897. return ret;
  2898. }
  2899. }
  2900. }
  2901. // [iSalesDB execSql:sql db:db];
  2902. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  2903. [iSalesDB execSql:@"END TRANSACTION" db:db];
  2904. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  2905. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  2906. [iSalesDB close_db:db];
  2907. appDelegate.wish_count =count;
  2908. [appDelegate update_count_mark];
  2909. ret[@"result"]= [NSNumber numberWithInt:2];
  2910. return ret;
  2911. }
  2912. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  2913. // 0 category
  2914. // 1 search
  2915. // 2 itemsearch
  2916. NSData *ret = nil;
  2917. NSDictionary *items = nil;
  2918. switch (from) {
  2919. case 0:{
  2920. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  2921. }
  2922. break;
  2923. case 1:{
  2924. items = [[self search:params limited:NO] objectForKey:@"items"];
  2925. }
  2926. break;
  2927. case 2:{
  2928. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  2929. }
  2930. break;
  2931. default:
  2932. break;
  2933. }
  2934. if (!items) {
  2935. return ret;
  2936. }
  2937. int count = [[items objectForKey:@"count"] intValue];
  2938. NSMutableString *product_id_str = [@"" mutableCopy];
  2939. for (int i = 0; i < count; i++) {
  2940. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  2941. NSDictionary *item = [items objectForKey:key];
  2942. NSString *product_id = [item objectForKey:@"product_id"];
  2943. if (i == 0) {
  2944. [product_id_str appendString:product_id];
  2945. } else {
  2946. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  2947. }
  2948. }
  2949. NSString *add_to = [self valueInParams:params key:@"addTo"];
  2950. if ([add_to isEqualToString:@"cart"]) {
  2951. NSString *order_code = [params objectForKey:@"orderCode"];
  2952. if (order_code.length) {
  2953. NSDictionary *newParams = @{
  2954. @"product_id" : product_id_str,
  2955. @"orderCode" : order_code
  2956. };
  2957. ret = [self offline_add2cart:newParams.mutableCopy];
  2958. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  2959. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  2960. ret = [RAUtils dict2data:retDic];
  2961. }
  2962. } else if([add_to isEqualToString:@"wishlist"]) {
  2963. NSDictionary *newParams = @{
  2964. @"product_id" : product_id_str
  2965. };
  2966. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  2967. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  2968. ret = [RAUtils dict2data:retDic];
  2969. } else if([add_to isEqualToString:@"portfolio"]) {
  2970. }
  2971. return ret;
  2972. }
  2973. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  2974. {
  2975. return [self addAll:params from:0];
  2976. }
  2977. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  2978. {
  2979. return [self addAll:params from:1];
  2980. }
  2981. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  2982. {
  2983. return [self addAll:params from:2];
  2984. }
  2985. #pragma mark - Jack
  2986. #warning 做SQL操作时转义!!
  2987. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  2988. // "val_227" : {
  2989. // "check" : 1,
  2990. // "value" : "US United States",
  2991. // "value_id" : "228"
  2992. // },
  2993. if (!countryCode) {
  2994. countryCode = @"US";
  2995. }
  2996. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  2997. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  2998. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  2999. int code_id = sqlite3_column_int(stmt, 3); // id
  3000. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3001. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  3002. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  3003. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3004. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  3005. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3006. }
  3007. long n = *count;
  3008. *count = n + 1;
  3009. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  3010. [container setValue:countryDic forKey:key];
  3011. }] mutableCopy];
  3012. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3013. return ret;
  3014. }
  3015. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  3016. countryCode = [self translateSingleQuote:countryCode];
  3017. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  3018. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3019. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  3020. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  3021. if (name == NULL) {
  3022. name = "";
  3023. }
  3024. if (code == NULL) {
  3025. code = "";
  3026. }
  3027. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3028. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  3029. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  3030. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3031. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  3032. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3033. }
  3034. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  3035. [container setValue:stateDic forKey:key];
  3036. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  3037. DebugLog(@"query all state error: %@",err_msg);
  3038. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3039. // [stateDic setValue:@"Other" forKey:@"value"];
  3040. // [stateDic setValue:@"" forKey:@"value_id"];
  3041. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3042. //
  3043. // if (state_code && [@"" isEqualToString:state_code]) {
  3044. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3045. // }
  3046. //
  3047. // NSString *key = [NSString stringWithFormat:@"val_0"];
  3048. // [container setValue:stateDic forKey:key];
  3049. }] mutableCopy];
  3050. // failure 可以不用了,一样的
  3051. if (ret.allKeys.count == 0) {
  3052. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3053. [stateDic setValue:@"Other" forKey:@"value"];
  3054. [stateDic setValue:@"" forKey:@"value_id"];
  3055. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3056. if (state_code && [@"" isEqualToString:state_code]) {
  3057. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3058. }
  3059. NSString *key = [NSString stringWithFormat:@"val_0"];
  3060. [ret setValue:stateDic forKey:key];
  3061. }
  3062. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3063. return ret;
  3064. }
  3065. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  3066. codeId = [self translateSingleQuote:codeId];
  3067. 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];
  3068. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3069. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  3070. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  3071. if (name == NULL) {
  3072. name = "";
  3073. }
  3074. if (code == NULL) {
  3075. code = "";
  3076. }
  3077. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3078. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  3079. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  3080. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3081. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  3082. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3083. }
  3084. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  3085. [container setValue:stateDic forKey:key];
  3086. }] mutableCopy];
  3087. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3088. return ret;
  3089. }
  3090. + (NSDictionary *)offline_getPrice {
  3091. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price;"];
  3092. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3093. char *name = (char *) sqlite3_column_text(stmt, 1);
  3094. int type = sqlite3_column_int(stmt, 2);
  3095. int orderBy = sqlite3_column_int(stmt, 3);
  3096. if (name == NULL) {
  3097. name = "";
  3098. }
  3099. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  3100. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  3101. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  3102. NSString *key = [NSString stringWithFormat:@"val_%d",orderBy];
  3103. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3104. if (orderBy == 0) {
  3105. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3106. }
  3107. [container setValue:priceDic forKey:key];
  3108. }] mutableCopy];
  3109. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3110. return ret;
  3111. }
  3112. + (NSDictionary *)offline_getSalesRep {
  3113. // 首先从offline_login表中取出sales_code
  3114. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  3115. NSString *user = app.user;
  3116. user = [self translateSingleQuote:user];
  3117. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  3118. __block NSString *user_code = @"";
  3119. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3120. char *code = (char *)sqlite3_column_text(stmt, 0);
  3121. if (code == NULL) {
  3122. code = "";
  3123. }
  3124. user_code = [NSString stringWithUTF8String:code];
  3125. }];
  3126. // 再取所有salesRep
  3127. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  3128. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3129. // 1 name 2 code 3 salesrep_id
  3130. char *name = (char *)sqlite3_column_text(stmt, 1);
  3131. char *code = (char *)sqlite3_column_text(stmt, 2);
  3132. int salesrep_id = sqlite3_column_int(stmt, 3);
  3133. if (name == NULL) {
  3134. name = "";
  3135. }
  3136. if (code == NULL) {
  3137. code = "";
  3138. }
  3139. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  3140. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  3141. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  3142. // 比较code 相等则check
  3143. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  3144. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3145. }
  3146. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  3147. }] mutableCopy];
  3148. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3149. return ret;
  3150. }
  3151. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  3152. zipcode = [self translateSingleQuote:zipcode];
  3153. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  3154. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3155. char *country = (char *)sqlite3_column_text(stmt, 0);
  3156. char *state = (char *)sqlite3_column_text(stmt, 1);
  3157. char *city = (char *)sqlite3_column_text(stmt, 2);
  3158. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  3159. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  3160. if (country == NULL) {
  3161. country = "";
  3162. }
  3163. if (state == NULL) {
  3164. state = "";
  3165. }
  3166. if (city == NULL) {
  3167. city = "";
  3168. }
  3169. if (country_code == NULL) {
  3170. country_code = "";
  3171. }
  3172. if (state_code == NULL) {
  3173. state_code = "";
  3174. }
  3175. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  3176. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  3177. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  3178. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  3179. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  3180. }] mutableCopy];
  3181. return ret;
  3182. }
  3183. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  3184. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  3185. [item setValue:value forKey:valueKey];
  3186. [dic setValue:item forKey:itemKey];
  3187. }
  3188. + (NSString *)countryCodeByid:(NSString *)code_id {
  3189. NSString *ret = nil;
  3190. code_id = [self translateSingleQuote:code_id];
  3191. sqlite3 *db = [iSalesDB get_db];
  3192. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  3193. sqlite3_stmt * statement;
  3194. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3195. while (sqlite3_step(statement) == SQLITE_ROW) {
  3196. char *code = (char *)sqlite3_column_text(statement, 0);
  3197. if (code == NULL) {
  3198. code = "";
  3199. }
  3200. ret = [NSString stringWithUTF8String:code];
  3201. }
  3202. sqlite3_finalize(statement);
  3203. }
  3204. [iSalesDB close_db:db];
  3205. return ret;
  3206. }
  3207. + (NSString *)countryCodeIdByCode:(NSString *)code {
  3208. NSString *ret = nil;
  3209. code = [self translateSingleQuote:code];
  3210. sqlite3 *db = [iSalesDB get_db];
  3211. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  3212. sqlite3_stmt * statement;
  3213. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3214. while (sqlite3_step(statement) == SQLITE_ROW) {
  3215. char *_id = (char *)sqlite3_column_text(statement, 0);
  3216. if (_id == NULL) {
  3217. _id = "";
  3218. }
  3219. ret = [NSString stringWithFormat:@"%s",_id];
  3220. }
  3221. sqlite3_finalize(statement);
  3222. }
  3223. [iSalesDB close_db:db];
  3224. return ret;
  3225. }
  3226. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  3227. NSString *name = nil;
  3228. codeId = [self translateSingleQuote:codeId];
  3229. sqlite3 *db = [iSalesDB get_db];
  3230. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  3231. sqlite3_stmt * statement;
  3232. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3233. while (sqlite3_step(statement) == SQLITE_ROW) {
  3234. char *value = (char *)sqlite3_column_text(statement, 0);
  3235. if (value == NULL) {
  3236. value = "";
  3237. }
  3238. name = [NSString stringWithUTF8String:value];
  3239. }
  3240. sqlite3_finalize(statement);
  3241. }
  3242. [iSalesDB close_db:db];
  3243. return name;
  3244. }
  3245. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  3246. NSString *ret = nil;
  3247. sqlite3 *db = [iSalesDB get_db];
  3248. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  3249. sqlite3_stmt * statement;
  3250. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3251. while (sqlite3_step(statement) == SQLITE_ROW) {
  3252. char *name = (char *)sqlite3_column_text(statement, 0);
  3253. if (name == NULL) {
  3254. name = "";
  3255. }
  3256. ret = [NSString stringWithUTF8String:name];
  3257. }
  3258. sqlite3_finalize(statement);
  3259. }
  3260. [iSalesDB close_db:db];
  3261. return ret;
  3262. }
  3263. + (NSString *)salesRepCodeById:(NSString *)_id {
  3264. NSString *ret = nil;
  3265. _id = [self translateSingleQuote:_id];
  3266. sqlite3 *db = [iSalesDB get_db];
  3267. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  3268. sqlite3_stmt * statement;
  3269. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3270. while (sqlite3_step(statement) == SQLITE_ROW) {
  3271. char *rep = (char *)sqlite3_column_text(statement, 0);
  3272. if (rep == NULL) {
  3273. rep = "";
  3274. }
  3275. ret = [NSString stringWithUTF8String:rep];
  3276. }
  3277. sqlite3_finalize(statement);
  3278. }
  3279. [iSalesDB close_db:db];
  3280. return ret;
  3281. }
  3282. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  3283. char *tx = (char *)sqlite3_column_text(stmt, col);
  3284. if (tx == NULL) {
  3285. tx = "";
  3286. }
  3287. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  3288. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  3289. if (!text) {
  3290. text = @"";
  3291. }
  3292. // 将字符全部为' '的字符串干掉
  3293. int spaceCount = 0;
  3294. for (int i = 0; i < text.length; i++) {
  3295. if ([text characterAtIndex:i] == ' ') {
  3296. spaceCount++;
  3297. }
  3298. }
  3299. if (spaceCount == text.length) {
  3300. text = @"";
  3301. }
  3302. return text;
  3303. }
  3304. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  3305. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  3306. NSData *data = [NSData dataWithContentsOfFile:path];
  3307. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  3308. return ret;
  3309. }
  3310. + (NSString *)textFileName:(NSString *)name {
  3311. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  3312. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  3313. if (!text) {
  3314. text = @"";
  3315. }
  3316. return text;
  3317. }
  3318. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  3319. return [[dic objectForKey:key] mutableCopy];
  3320. }
  3321. + (id)translateSingleQuote:(NSString *)string {
  3322. if ([string isKindOfClass:[NSString class]])
  3323. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3324. return string;
  3325. }
  3326. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  3327. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  3328. }
  3329. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  3330. NSString* ret= nil;
  3331. NSString *sqlQuery = nil;
  3332. // 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
  3333. 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];
  3334. sqlite3_stmt * statement;
  3335. // int count=0;
  3336. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3337. {
  3338. if (sqlite3_step(statement) == SQLITE_ROW)
  3339. {
  3340. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  3341. if(imgurl==nil)
  3342. imgurl="";
  3343. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  3344. ret=nsimgurl;
  3345. }
  3346. sqlite3_finalize(statement);
  3347. }
  3348. else
  3349. {
  3350. [ret setValue:@"8" forKey:@"result"];
  3351. }
  3352. // [iSalesDB close_db:db];
  3353. // DebugLog(@"data string: %@",ret );
  3354. return ret;
  3355. }
  3356. #pragma mark contact list
  3357. +(NSDictionary*) offline_contactlist :(NSMutableDictionary *) params
  3358. {
  3359. // contactType = "Sales_Order_Customer";
  3360. // limit = 25;
  3361. // offset = 0;
  3362. // password = 123456;
  3363. // "price_name" = 16;
  3364. // user = EvanK;
  3365. sqlite3 *db = [iSalesDB get_db];
  3366. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  3367. if (contactType) {
  3368. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  3369. } else {
  3370. contactType = @"1 = 1";
  3371. }
  3372. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  3373. DebugLog(@"offline contact list keyword: %@",keyword);
  3374. // advanced search
  3375. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  3376. if (contact_name) {
  3377. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3378. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  3379. } else {
  3380. contact_name = @"";
  3381. }
  3382. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  3383. if (customer_phone) {
  3384. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3385. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  3386. } else {
  3387. customer_phone = @"";
  3388. }
  3389. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  3390. if (customer_fax) {
  3391. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3392. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  3393. } else {
  3394. customer_fax = @"";
  3395. }
  3396. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  3397. if (customer_zipcode) {
  3398. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3399. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  3400. } else {
  3401. customer_zipcode = @"";
  3402. }
  3403. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  3404. if (customer_sales_rep) {
  3405. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3406. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  3407. } else {
  3408. customer_sales_rep = @"";
  3409. }
  3410. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  3411. if (customer_state) {
  3412. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3413. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  3414. } else {
  3415. customer_state = @"";
  3416. }
  3417. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  3418. if (customer_name) {
  3419. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3420. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  3421. } else {
  3422. customer_name = @"";
  3423. }
  3424. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  3425. if (customer_country) {
  3426. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3427. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  3428. } else {
  3429. customer_country = @"";
  3430. }
  3431. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  3432. if (customer_cid) {
  3433. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3434. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  3435. } else {
  3436. customer_cid = @"";
  3437. }
  3438. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  3439. if (customer_city) {
  3440. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3441. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  3442. } else {
  3443. customer_city = @"";
  3444. }
  3445. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  3446. if (customer_address) {
  3447. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3448. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  3449. } else {
  3450. customer_address = @"";
  3451. }
  3452. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  3453. if (customer_email) {
  3454. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3455. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  3456. } else {
  3457. customer_email = @"";
  3458. }
  3459. NSString *price_name = [params valueForKey:@"price_name"];
  3460. if (price_name) {
  3461. if ([price_name containsString:@","]) {
  3462. // 首先从 price表中查处name
  3463. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  3464. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  3465. for (int i = 1;i < pArray.count;i++) {
  3466. NSString *p = pArray[i];
  3467. [mutablePStr appendFormat:@" or type = %@ ",p];
  3468. }
  3469. [mutablePStr appendString:@";"];
  3470. __block NSMutableArray *price_name_array = [NSMutableArray array];
  3471. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3472. char *name = (char *)sqlite3_column_text(stmt, 0);
  3473. if (!name)
  3474. name = "";
  3475. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  3476. }];
  3477. // 再根据name 拼sql
  3478. NSMutableString *mutable_price_name = [NSMutableString string];
  3479. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  3480. for (int i = 1; i < price_name_array.count; i++) {
  3481. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  3482. }
  3483. [mutable_price_name appendString:@")"];
  3484. price_name = mutable_price_name;
  3485. } else {
  3486. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  3487. if ([price_name isEqualToString:@""]) {
  3488. price_name = @"";
  3489. } else {
  3490. __block NSString *price;
  3491. price_name = [self translateSingleQuote:price_name];
  3492. [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) {
  3493. char *p = (char *)sqlite3_column_text(stmt, 0);
  3494. if (p == NULL) {
  3495. p = "";
  3496. }
  3497. price = [NSString stringWithUTF8String:p];
  3498. }];
  3499. if ([price isEqualToString:@""]) {
  3500. price_name = @"";
  3501. } else {
  3502. price = [self translateSingleQuote:price];
  3503. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  3504. }
  3505. }
  3506. }
  3507. } else {
  3508. price_name = @"";
  3509. }
  3510. int limit = [[params valueForKey:@"limit"] intValue];
  3511. int offset = [[params valueForKey:@"offset"] intValue];
  3512. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3513. 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];
  3514. 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];
  3515. // int result= [iSalesDB AddExFunction:db];
  3516. int count =0;
  3517. NSString *sqlQuery = nil;
  3518. if(keyword.length==0)
  3519. {
  3520. // 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];
  3521. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  3522. sqlQuery = sql;
  3523. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  3524. }
  3525. else
  3526. {
  3527. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  3528. keyword = keyword.lowercaseString;
  3529. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3530. 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];
  3531. 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]];
  3532. }
  3533. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  3534. sqlite3_stmt * statement;
  3535. [ret setValue:@"2" forKey:@"result"];
  3536. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  3537. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3538. {
  3539. int i = 0;
  3540. while (sqlite3_step(statement) == SQLITE_ROW)
  3541. {
  3542. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3543. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3544. int editable = sqlite3_column_int(statement, 0);
  3545. char *company_name = (char*)sqlite3_column_text(statement, 1);
  3546. NSString *nscompany_name =nil;
  3547. if(company_name==nil)
  3548. nscompany_name=@"";
  3549. else
  3550. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  3551. char *country = (char*)sqlite3_column_text(statement, 2);
  3552. if(country==nil)
  3553. country="";
  3554. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  3555. // char *addr = (char*)sqlite3_column_text(statement, 3);
  3556. // if(addr==nil)
  3557. // addr="";
  3558. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  3559. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  3560. if(zipcode==nil)
  3561. zipcode="";
  3562. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  3563. char *state = (char*)sqlite3_column_text(statement, 5);
  3564. if(state==nil)
  3565. state="";
  3566. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  3567. char *city = (char*)sqlite3_column_text(statement, 6);
  3568. if(city==nil)
  3569. city="";
  3570. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  3571. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  3572. // NSString *nscontact_name = nil;
  3573. // if(contact_name==nil)
  3574. // nscontact_name=@"";
  3575. // else
  3576. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  3577. char *phone = (char*)sqlite3_column_text(statement, 8);
  3578. NSString *nsphone = nil;
  3579. if(phone==nil)
  3580. nsphone=@"";
  3581. else
  3582. nsphone= [[NSString alloc]initWithUTF8String:phone];
  3583. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  3584. if(contact_id==nil)
  3585. contact_id="";
  3586. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3587. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  3588. if(addr_1==nil)
  3589. addr_1="";
  3590. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  3591. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  3592. if(addr_2==nil)
  3593. addr_2="";
  3594. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  3595. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  3596. if(addr_3==nil)
  3597. addr_3="";
  3598. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  3599. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  3600. if(addr_4==nil)
  3601. addr_4="";
  3602. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  3603. char *first_name = (char*)sqlite3_column_text(statement, 14);
  3604. if(first_name==nil)
  3605. first_name="";
  3606. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  3607. char *last_name = (char*)sqlite3_column_text(statement, 15);
  3608. if(last_name==nil)
  3609. last_name="";
  3610. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  3611. char *fax = (char*)sqlite3_column_text(statement, 16);
  3612. NSString *nsfax = nil;
  3613. if(fax==nil)
  3614. nsfax=@"";
  3615. else
  3616. {
  3617. nsfax= [[NSString alloc]initWithUTF8String:fax];
  3618. if(nsfax.length>0)
  3619. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  3620. }
  3621. char *email = (char*)sqlite3_column_text(statement, 17);
  3622. NSString *nsemail = nil;
  3623. if(email==nil)
  3624. nsemail=@"";
  3625. else
  3626. {
  3627. nsemail= [[NSString alloc]initWithUTF8String:email];
  3628. if(nsemail.length>0)
  3629. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  3630. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  3631. }
  3632. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3633. [arr_name addObject:nsfirst_name];
  3634. [arr_name addObject:nslast_name];
  3635. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  3636. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  3637. {
  3638. // decrypt
  3639. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  3640. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  3641. nsphone=[AESCrypt fastdecrypt:nsphone];
  3642. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3643. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  3644. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3645. }
  3646. [arr_addr addObject:nscompany_name];
  3647. [arr_addr addObject:nscontact_name];
  3648. [arr_addr addObject:@"<br>"];
  3649. [arr_addr addObject:nsaddr_1];
  3650. [arr_addr addObject:nsaddr_2];
  3651. [arr_addr addObject:nsaddr_3];
  3652. [arr_addr addObject:nsaddr_4];
  3653. //[arr_addr addObject:nsaddr];
  3654. [arr_addr addObject:nszipcode];
  3655. [arr_addr addObject:nscity];
  3656. [arr_addr addObject:nsstate];
  3657. [arr_addr addObject:nscountry];
  3658. [arr_addr addObject:@"<br>"];
  3659. [arr_addr addObject:nsphone];
  3660. [arr_addr addObject:nsfax];
  3661. [arr_addr addObject:nsemail];
  3662. NSString * name = [RAUtils arr2string:arr_addr separator:@", " trim:true];
  3663. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  3664. [item setValue:name forKey:@"name"];
  3665. [item setValue:nscontact_id forKey:@"contact_id"];
  3666. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  3667. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3668. i++;
  3669. }
  3670. UIApplication * app = [UIApplication sharedApplication];
  3671. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3672. [ret setValue:appDelegate.mode forKey:@"mode"];
  3673. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  3674. sqlite3_finalize(statement);
  3675. }
  3676. [iSalesDB close_db:db];
  3677. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3678. return ret;
  3679. }
  3680. #pragma mark contact Advanced search
  3681. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  3682. {
  3683. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  3684. return [RAUtils dict2data:contactAdvanceDic];
  3685. }
  3686. #pragma mark create new contact
  3687. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  3688. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  3689. NSData *data = [NSData dataWithContentsOfFile:path];
  3690. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  3691. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  3692. NSString *countryCode = nil;
  3693. NSString *countryCode_id = nil;
  3694. NSString *stateCode = nil;
  3695. NSString *city = nil;
  3696. NSString *zipCode = nil;
  3697. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  3698. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  3699. NSString *code_id = params[@"country"];
  3700. countryCode_id = code_id;
  3701. countryCode = [self countryCodeByid:code_id];
  3702. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  3703. NSString *zip_code = params[@"zipcode"];
  3704. // 剔除全部为空格
  3705. int spaceCount = 0;
  3706. for (int i = 0; i < zip_code.length; i++) {
  3707. if ([zip_code characterAtIndex:i] == ' ') {
  3708. spaceCount++;
  3709. }
  3710. }
  3711. if (spaceCount == zip_code.length) {
  3712. zip_code = @"";
  3713. }
  3714. zipCode = zip_code;
  3715. if (zipCode.length > 0) {
  3716. countryCode_id = params[@"country"];
  3717. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  3718. countryCode = [dic valueForKey:@"country_code"];
  3719. if (!countryCode) {
  3720. countryCode = @"US";
  3721. }
  3722. stateCode = [dic valueForKey:@"state_code"];
  3723. city = [dic valueForKey:@"city"];
  3724. // zip code
  3725. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_10"] mutableCopy];
  3726. [zipDic setValue:zipCode forKey:@"value"];
  3727. [section_0 setValue:zipDic forKey:@"item_10"];
  3728. }
  3729. }
  3730. } else {
  3731. // default: US United States
  3732. countryCode = @"US";
  3733. countryCode_id = @"228";
  3734. }
  3735. // country
  3736. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  3737. [self setValue:allCountry forItemKey:@"item_5" valueKey:@"cadedate" inDictionary:section_0];
  3738. // state
  3739. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  3740. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  3741. [self setValue:allState forItemKey:@"item_11" valueKey:@"cadedate" inDictionary:section_0];
  3742. // city
  3743. if (city) {
  3744. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_12"] mutableCopy];
  3745. [cityDic setValue:city forKey:@"value"];
  3746. [section_0 setValue:cityDic forKey:@"item_12"];
  3747. }
  3748. // price type
  3749. NSDictionary *priceDic = [self offline_getPrice];
  3750. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  3751. // Sales Rep
  3752. NSDictionary *repDic = [self offline_getSalesRep];
  3753. [self setValue:repDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  3754. [ret setValue:section_0 forKey:@"section_0"];
  3755. return [RAUtils dict2data:ret];
  3756. }
  3757. #pragma mark save
  3758. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  3759. NSString *addr = nil;
  3760. NSString *contact_name = nil;
  3761. NSString *companyName = [params objectForKey:@"company"];
  3762. if (companyName) {
  3763. companyName = [AESCrypt fastencrypt:companyName];
  3764. } else {
  3765. companyName = @"";
  3766. }
  3767. DebugLog(@"company");
  3768. companyName = [self translateSingleQuote:companyName];
  3769. NSString *addr1 = [params objectForKey:@"address"];
  3770. NSString *addr2 = [params objectForKey:@"address2"];
  3771. NSString *addr3 = [params objectForKey:@"address_3"];
  3772. NSString *addr4 = [params objectForKey:@"address_4"];
  3773. if (!addr2) {
  3774. addr2 = @"";
  3775. }
  3776. if (!addr3) {
  3777. addr3 = @"";
  3778. }
  3779. if (!addr4) {
  3780. addr4 = @"";
  3781. }
  3782. if (!addr1) {
  3783. addr1 = @"";
  3784. }
  3785. DebugLog(@"addr");
  3786. addr = [RAUtils arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  3787. addr = [AESCrypt fastencrypt:addr];
  3788. addr = [self translateSingleQuote:addr];
  3789. if (addr1 && ![addr1 isEqualToString:@""]) {
  3790. addr1 = [AESCrypt fastencrypt:addr1];
  3791. }
  3792. addr1 = [self translateSingleQuote:addr1];
  3793. addr2 = [self translateSingleQuote:addr2];
  3794. addr3 = [self translateSingleQuote:addr3];
  3795. addr4 = [self translateSingleQuote:addr4];
  3796. NSString *country = [params objectForKey:@"country"];
  3797. if (country) {
  3798. country = [self countryNameByCountryCodeId:country];
  3799. } else {
  3800. country = @"";
  3801. }
  3802. DebugLog(@"country");
  3803. country = [self translateSingleQuote:country];
  3804. NSString *state = [params objectForKey:@"state"];
  3805. if (!state) {
  3806. state = @"";
  3807. }
  3808. DebugLog(@"state");
  3809. state = [self translateSingleQuote:state];
  3810. NSString *city = [params objectForKey:@"city"];
  3811. if (!city) {
  3812. city = @"";
  3813. }
  3814. city = [self translateSingleQuote:city];
  3815. NSString *zipcode = [params objectForKey:@"zipcode"];
  3816. if (!zipcode) {
  3817. zipcode = @"";
  3818. }
  3819. DebugLog(@"zip");
  3820. zipcode = [self translateSingleQuote:zipcode];
  3821. NSString *fistName = [params objectForKey:@"firstname"];
  3822. if (!fistName) {
  3823. fistName = @"";
  3824. }
  3825. NSString *lastName = [params objectForKey:@"lastname"];
  3826. if (!lastName) {
  3827. lastName = @"";
  3828. }
  3829. contact_name = [RAUtils arr2string:@[fistName,lastName] separator:@" " trim:true];
  3830. DebugLog(@"contact_name");
  3831. contact_name = [self translateSingleQuote:contact_name];
  3832. fistName = [self translateSingleQuote:fistName];
  3833. lastName = [self translateSingleQuote:lastName];
  3834. NSString *phone = [params objectForKey:@"phone"];
  3835. if (phone) {
  3836. phone = [AESCrypt fastencrypt:phone];
  3837. } else {
  3838. phone = @"";
  3839. }
  3840. DebugLog(@"PHONE");
  3841. phone = [self translateSingleQuote:phone];
  3842. NSString *fax = [params objectForKey:@"fax"];
  3843. if (!fax) {
  3844. fax = @"";
  3845. }
  3846. DebugLog(@"FAX");
  3847. fax = [self translateSingleQuote:fax];
  3848. NSString *email = [params objectForKey:@"email"];
  3849. if (!email) {
  3850. email = @"";
  3851. }
  3852. DebugLog(@"EMAIL:%@",email);
  3853. email = [self translateSingleQuote:email];
  3854. NSString *notes = [params objectForKey:@"contact_notes"];
  3855. if (!notes) {
  3856. notes = @"";
  3857. }
  3858. DebugLog(@"NOTE:%@",notes);
  3859. notes = [self translateSingleQuote:notes];
  3860. NSString *price = [params objectForKey:@"price_name"];
  3861. if (price) {
  3862. price = [self priceNameByPriceId:price];
  3863. } else {
  3864. price = @"";
  3865. }
  3866. DebugLog(@"PRICE");
  3867. price = [self translateSingleQuote:price];
  3868. NSString *salesRep = [params objectForKey:@"sales_rep"];
  3869. if (salesRep) {
  3870. salesRep = [self salesRepCodeById:salesRep];
  3871. } else {
  3872. salesRep = @"";
  3873. }
  3874. salesRep = [self translateSingleQuote:salesRep];
  3875. NSString *img = [params objectForKey:@"business_card"];
  3876. NSArray *array = [img componentsSeparatedByString:@","];
  3877. NSString *img_0 = array[0];
  3878. if (!img_0) {
  3879. img_0 = @"";
  3880. }
  3881. img_0 = [self translateSingleQuote:img_0];
  3882. NSString *img_1 = array[1];
  3883. if (!img_1) {
  3884. img_1 = @"";
  3885. }
  3886. img_1 = [self translateSingleQuote:img_1];
  3887. NSString *img_2 = array[2];
  3888. if (!img_2) {
  3889. img_2 = @"";
  3890. }
  3891. img_2 = [self translateSingleQuote:img_2];
  3892. NSString *contact_id = [NSUUID UUID].UUIDString;
  3893. // 判断更新时是否为customer
  3894. if (update) {
  3895. contact_id = [params objectForKey:@"contact_id"];
  3896. if (!contact_id) {
  3897. contact_id = @"";
  3898. }
  3899. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  3900. __block int customer = 0;
  3901. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3902. customer = sqlite3_column_int(stmt, 0);
  3903. }];
  3904. isCustomer = customer ? YES : NO;
  3905. }
  3906. NSMutableDictionary *sync_dic = [params mutableCopy];
  3907. if (isCustomer) {
  3908. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  3909. } else {
  3910. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  3911. }
  3912. NSString *sync_data = nil;
  3913. NSString *sql = nil;
  3914. if (update){
  3915. contact_id = [params objectForKey:@"contact_id"];
  3916. if (!contact_id) {
  3917. contact_id = @"";
  3918. }
  3919. [sync_dic setValue:contact_id forKey:@"contact_id"];
  3920. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  3921. sync_data = [RAUtils dict2string:sync_dic];
  3922. sync_data = [self translateSingleQuote:sync_data];
  3923. 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 = '%@' 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_id];
  3924. } else {
  3925. contact_id = [self translateSingleQuote:contact_id];
  3926. [sync_dic setValue:contact_id forKey:@"contact_id"];
  3927. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  3928. sync_data = [RAUtils dict2string:sync_dic];
  3929. sync_data = [self translateSingleQuote:sync_data];
  3930. 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) 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];
  3931. }
  3932. int result = [iSalesDB execSql:sql];
  3933. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  3934. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  3935. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  3936. }
  3937. #pragma mark save new contact
  3938. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  3939. {
  3940. return [self offline_saveContact:params update:NO isCustomer:YES];
  3941. }
  3942. #pragma mark edit contact
  3943. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  3944. {
  3945. // {
  3946. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  3947. // password = 123456;
  3948. // user = EvanK;
  3949. // }
  3950. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  3951. NSData *data = [NSData dataWithContentsOfFile:path];
  3952. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  3953. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  3954. NSString *countryCode = nil;
  3955. NSString *countryCode_id = nil;
  3956. NSString *stateCode = nil;
  3957. /*------contact infor------*/
  3958. __block NSString *country = nil;
  3959. __block NSString *company_name = nil;
  3960. __block NSString *contact_id = params[@"contact_id"];
  3961. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  3962. __block NSString *zipcode = nil;
  3963. __block NSString *state = nil; // state_code
  3964. __block NSString *city = nil; //
  3965. __block NSString *firt_name,*last_name;
  3966. __block NSString *phone,*fax,*email;
  3967. __block NSString *notes,*price_type,*sales_rep;
  3968. __block NSString *img_0,*img_1,*img_2;
  3969. contact_id = [self translateSingleQuote:contact_id];
  3970. 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 from offline_contact where contact_id = '%@';",contact_id];
  3971. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3972. country = [self textAtColumn:0 statement:stmt]; // country name
  3973. company_name = [self textAtColumn:1 statement:stmt];
  3974. addr_1 = [self textAtColumn:2 statement:stmt];
  3975. addr_2 = [self textAtColumn:3 statement:stmt];
  3976. addr_3 = [self textAtColumn:4 statement:stmt];
  3977. addr_4 = [self textAtColumn:5 statement:stmt];
  3978. zipcode = [self textAtColumn:6 statement:stmt];
  3979. state = [self textAtColumn:7 statement:stmt]; // state code
  3980. city = [self textAtColumn:8 statement:stmt];
  3981. firt_name = [self textAtColumn:9 statement:stmt];
  3982. last_name = [self textAtColumn:10 statement:stmt];
  3983. phone = [self textAtColumn:11 statement:stmt];
  3984. fax = [self textAtColumn:12 statement:stmt];
  3985. email = [self textAtColumn:13 statement:stmt];
  3986. notes = [self textAtColumn:14 statement:stmt];
  3987. price_type = [self textAtColumn:15 statement:stmt]; // name
  3988. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  3989. img_0 = [self textAtColumn:17 statement:stmt];
  3990. img_1 = [self textAtColumn:18 statement:stmt];
  3991. img_2 = [self textAtColumn:19 statement:stmt];
  3992. }];
  3993. // decrypt
  3994. if (company_name) {
  3995. company_name = [AESCrypt fastdecrypt:company_name];
  3996. }
  3997. if (addr_1) {
  3998. addr_1 = [AESCrypt fastdecrypt:addr_1];
  3999. }
  4000. if (phone) {
  4001. phone = [AESCrypt fastdecrypt:phone];
  4002. }
  4003. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  4004. countryCode = [iSalesDB jk_queryText:countrySql];
  4005. stateCode = state;
  4006. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  4007. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  4008. NSString *code_id = params[@"country"];
  4009. countryCode_id = code_id;
  4010. countryCode = [self countryCodeByid:code_id];
  4011. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  4012. NSString *zip_code = params[@"zipcode"];
  4013. // 剔除全部为空格
  4014. int spaceCount = 0;
  4015. for (int i = 0; i < zip_code.length; i++) {
  4016. if ([zip_code characterAtIndex:i] == ' ') {
  4017. spaceCount++;
  4018. }
  4019. }
  4020. if (spaceCount == zip_code.length) {
  4021. zip_code = @"";
  4022. }
  4023. if (zipcode.length > 0) {
  4024. zipcode = zip_code;
  4025. countryCode_id = params[@"country"];
  4026. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  4027. countryCode = [dic valueForKey:@"country_code"];
  4028. stateCode = [dic valueForKey:@"state_code"];
  4029. city = [dic valueForKey:@"city"];
  4030. // zip code
  4031. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  4032. [zipDic setValue:zipcode forKey:@"value"];
  4033. [section_0 setValue:zipDic forKey:@"item_8"];
  4034. }
  4035. }
  4036. }
  4037. // 0 Country
  4038. // 1 Company Name
  4039. // 2 Contact ID
  4040. // 3 Picture
  4041. // 4 Address 1
  4042. // 5 Address 2
  4043. // 6 Address 3
  4044. // 7 Address 4
  4045. // 8 Zip Code
  4046. // 9 State/Province
  4047. // 10 City
  4048. // 11 Contact First Name
  4049. // 12 Contact Last Name
  4050. // 13 Phone
  4051. // 14 Fax
  4052. // 15 Email
  4053. // 16 Contact Notes
  4054. // 17 Price Type
  4055. // 18 Sales Rep
  4056. // country
  4057. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  4058. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  4059. // company
  4060. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  4061. // contact_id
  4062. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  4063. // picture
  4064. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  4065. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  4066. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  4067. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  4068. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  4069. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  4070. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  4071. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  4072. // addr 1 2 3 4
  4073. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  4074. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  4075. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  4076. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  4077. // zip code
  4078. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  4079. // state
  4080. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  4081. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  4082. // city
  4083. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  4084. // first last
  4085. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  4086. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  4087. // phone fax email
  4088. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  4089. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  4090. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  4091. // notes
  4092. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  4093. // price
  4094. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  4095. for (NSString *key in priceDic.allKeys) {
  4096. if ([key containsString:@"val_"]) {
  4097. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  4098. if ([dic[@"value"] isEqualToString:price_type]) {
  4099. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  4100. [priceDic setValue:dic forKey:key];
  4101. }
  4102. }
  4103. }
  4104. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  4105. // Sales Rep
  4106. NSMutableDictionary *repDic = [[self offline_getSalesRep] mutableCopy];
  4107. for (NSString *key in repDic.allKeys) {
  4108. if ([key containsString:@"val_"]) {
  4109. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  4110. NSString *value = dic[@"value"];
  4111. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  4112. if (code && [code isEqualToString:sales_rep]) {
  4113. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  4114. [repDic setValue:dic forKey:key];
  4115. }
  4116. }
  4117. }
  4118. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  4119. [ret setValue:section_0 forKey:@"section_0"];
  4120. return [RAUtils dict2data:ret];
  4121. }
  4122. #pragma mark save contact
  4123. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  4124. {
  4125. return [self offline_saveContact:params update:YES isCustomer:YES];
  4126. }
  4127. #pragma mark category
  4128. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  4129. if (ck) {
  4130. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  4131. for (NSString *key in res.allKeys) {
  4132. if (![key isEqualToString:@"count"]) {
  4133. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  4134. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  4135. if ([val[@"value"] isEqualToString:ck]) {
  4136. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  4137. }
  4138. [res setValue:val forKey:key];
  4139. }
  4140. }
  4141. [dic setValue:res forKey:valueKey];
  4142. }
  4143. }
  4144. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  4145. // NSString* orderCode = [params valueForKey:@"orderCode"];
  4146. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4147. NSString* category = [params valueForKey:@"category"];
  4148. if (!category || [category isEqualToString:@""]) {
  4149. category = @"%";
  4150. }
  4151. category = [self translateSingleQuote:category];
  4152. int limit = [[params valueForKey:@"limit"] intValue];
  4153. int offset = [[params valueForKey:@"offset"] intValue];
  4154. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4155. NSString *limit_str = @"";
  4156. if (limited) {
  4157. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  4158. }
  4159. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  4160. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  4161. sqlite3 *db = [iSalesDB get_db];
  4162. // [iSalesDB AddExFunction:db];
  4163. int count;
  4164. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  4165. NSString *sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete 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];
  4166. double price_min = 0;
  4167. double price_max = 0;
  4168. if ([params.allKeys containsObject:@"alert"]) {
  4169. // alert
  4170. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  4171. NSString *alert = params[@"alert"];
  4172. if ([alert isEqualToString:@"Display All"]) {
  4173. alert = [NSString stringWithFormat:@""];
  4174. } else {
  4175. alert = [self translateSingleQuote:alert];
  4176. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  4177. }
  4178. // available
  4179. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  4180. NSString *available = params[@"available"];
  4181. NSString *available_condition;
  4182. if ([available isEqualToString:@"Display All"]) {
  4183. available_condition = @"";
  4184. } else if ([available isEqualToString:@"Available Now"]) {
  4185. available_condition = @"and availability > 0";
  4186. } else {
  4187. available_condition = @"and availability == 0";
  4188. }
  4189. // best seller
  4190. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  4191. NSString *best_seller = @"";
  4192. NSString *order_best_seller = @"m.name asc";
  4193. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  4194. best_seller = @"and best_seller > 0";
  4195. order_best_seller = @"m.best_seller desc,m.name asc";
  4196. }
  4197. // price
  4198. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  4199. NSString *price = params[@"price"];
  4200. price_min = 0;
  4201. price_max = MAXFLOAT;
  4202. if (appDelegate.user) {
  4203. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  4204. NSMutableString *priceName = [NSMutableString string];
  4205. for (int i = 0; i < priceTypeArray.count; i++) {
  4206. NSString *pricetype = priceTypeArray[i];
  4207. pricetype = [self translateSingleQuote:pricetype];
  4208. if (i == 0) {
  4209. [priceName appendFormat:@"'%@'",pricetype];
  4210. } else {
  4211. [priceName appendFormat:@",'%@'",pricetype];
  4212. }
  4213. }
  4214. if ([price isEqualToString:@"Display All"]) {
  4215. price = [NSString stringWithFormat:@""];
  4216. } else if([price containsString:@"+"]){
  4217. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  4218. price_min = [price doubleValue];
  4219. 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];
  4220. } else {
  4221. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  4222. price_min = [[priceArray objectAtIndex:0] doubleValue];
  4223. price_max = [[priceArray objectAtIndex:1] doubleValue];
  4224. 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];
  4225. }
  4226. } else {
  4227. price = @"";
  4228. }
  4229. // sold_by_qty : Sold in quantities of %@
  4230. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  4231. NSString *qty = params[@"sold_by_qty"];
  4232. if ([qty isEqualToString:@"Display All"]) {
  4233. qty = @"";
  4234. } else {
  4235. qty = [self translateSingleQuote:qty];
  4236. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  4237. }
  4238. // cate
  4239. category = [self translateSingleQuote:category];
  4240. NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  4241. // where bestseller > 0 order by bestseller desc
  4242. // sql query: alert availability(int) best_seller(int) price qty
  4243. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete from (select name,description,product_id,closeout,best_seller 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];
  4244. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  4245. }
  4246. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  4247. if (!appDelegate.user) {
  4248. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  4249. }
  4250. [ret setValue:filter forKey:@"filter"];
  4251. DebugLog(@"offline_category sql:%@",sqlQuery);
  4252. sqlite3_stmt * statement;
  4253. [ret setValue:@"2" forKey:@"result"];
  4254. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  4255. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  4256. // int count=0;
  4257. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4258. {
  4259. int i=0;
  4260. while (sqlite3_step(statement) == SQLITE_ROW)
  4261. {
  4262. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4263. char *name = (char*)sqlite3_column_text(statement, 0);
  4264. if(name==nil)
  4265. name="";
  4266. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  4267. char *description = (char*)sqlite3_column_text(statement, 1);
  4268. if(description==nil)
  4269. description="";
  4270. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  4271. int product_id = sqlite3_column_int(statement, 2);
  4272. int wid = sqlite3_column_int(statement, 3);
  4273. int closeout = sqlite3_column_int(statement, 4);
  4274. int cid = sqlite3_column_int(statement, 5);
  4275. int wisdelete = sqlite3_column_int(statement, 6);
  4276. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  4277. if(wid !=0 && wisdelete != 1)
  4278. [item setValue:@"true" forKey:@"wish_exists"];
  4279. else
  4280. [item setValue:@"false" forKey:@"wish_exists"];
  4281. if(closeout==0)
  4282. [item setValue:@"false" forKey:@"is_closeout"];
  4283. else
  4284. [item setValue:@"true" forKey:@"is_closeout"];
  4285. if(cid==0)
  4286. [item setValue:@"false" forKey:@"cart_exists"];
  4287. else
  4288. [item setValue:@"true" forKey:@"cart_exists"];
  4289. [item addEntriesFromDictionary:imgjson];
  4290. // [item setValue:nsurl forKey:@"img"];
  4291. [item setValue:nsname forKey:@"name"];
  4292. [item setValue:nsdescription forKey:@"description"];
  4293. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  4294. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4295. i++;
  4296. }
  4297. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4298. [ret setObject:items forKey:@"items"];
  4299. sqlite3_finalize(statement);
  4300. } else {
  4301. DebugLog(@"nothing...");
  4302. }
  4303. DebugLog(@"count:%d",count);
  4304. [iSalesDB close_db:db];
  4305. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  4306. return ret;
  4307. }
  4308. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  4309. {
  4310. return [self categoryList:params limited:YES];
  4311. }
  4312. # pragma mark item search
  4313. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  4314. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4315. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  4316. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  4317. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  4318. // category
  4319. NSDictionary *category_menu = [self offline_category_menu];
  4320. [filter setValue:category_menu forKey:@"category"];
  4321. NSString* where= nil;
  4322. NSString* orderby= @"m.name";
  4323. if (!filterSearch) {
  4324. int covertype = [[params valueForKey:@"covertype"] intValue];
  4325. switch (covertype) {
  4326. case 0:
  4327. {
  4328. where=@"m.category like'%%#005#%%'";
  4329. break;
  4330. }
  4331. case 1:
  4332. {
  4333. where=@"m.alert like '%QS%'";
  4334. break;
  4335. }
  4336. case 2:
  4337. {
  4338. where=@"m.availability>0";
  4339. break;
  4340. }
  4341. case 3:
  4342. {
  4343. where=@"m.best_seller>0";
  4344. orderby=@"m.best_seller desc,m.name asc";
  4345. break;
  4346. }
  4347. default:
  4348. where=@"1=1";
  4349. break;
  4350. }
  4351. }
  4352. int limit = [[params valueForKey:@"limit"] intValue];
  4353. int offset = [[params valueForKey:@"offset"] intValue];
  4354. NSString *limit_str = @"";
  4355. if (limited) {
  4356. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  4357. }
  4358. sqlite3 *db = [iSalesDB get_db];
  4359. // [iSalesDB AddExFunction:db];
  4360. int count;
  4361. NSString *sqlQuery = nil;
  4362. where = [where stringByAppendingString:@" and m.is_active = 1"];
  4363. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete 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];
  4364. double price_min = 0;
  4365. double price_max = 0;
  4366. if (filterSearch) {
  4367. // alert
  4368. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  4369. NSString *alert = params[@"alert"];
  4370. if ([alert isEqualToString:@"Display All"]) {
  4371. alert = [NSString stringWithFormat:@""];
  4372. } else {
  4373. alert = [self translateSingleQuote:alert];
  4374. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  4375. }
  4376. // available
  4377. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  4378. NSString *available = params[@"available"];
  4379. NSString *available_condition;
  4380. if ([available isEqualToString:@"Display All"]) {
  4381. available_condition = @"";
  4382. } else if ([available isEqualToString:@"Available Now"]) {
  4383. available_condition = @"and availability > 0";
  4384. } else {
  4385. available_condition = @"and availability == 0";
  4386. }
  4387. // best seller
  4388. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  4389. NSString *best_seller = @"";
  4390. NSString *order_best_seller = @"m.name asc";
  4391. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  4392. best_seller = @"and best_seller > 0";
  4393. order_best_seller = @"m.best_seller desc,m.name asc";
  4394. }
  4395. // price
  4396. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  4397. NSString *price = params[@"price"];
  4398. price_min = 0;
  4399. price_max = MAXFLOAT;
  4400. if (appDelegate.user) {
  4401. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  4402. NSMutableString *priceName = [NSMutableString string];
  4403. for (int i = 0; i < priceTypeArray.count; i++) {
  4404. NSString *pricetype = priceTypeArray[i];
  4405. pricetype = [self translateSingleQuote:pricetype];
  4406. if (i == 0) {
  4407. [priceName appendFormat:@"'%@'",pricetype];
  4408. } else {
  4409. [priceName appendFormat:@",'%@'",pricetype];
  4410. }
  4411. }
  4412. if ([price isEqualToString:@"Display All"]) {
  4413. price = [NSString stringWithFormat:@""];
  4414. } else if([price containsString:@"+"]){
  4415. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  4416. price_min = [price doubleValue];
  4417. 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];
  4418. } else {
  4419. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  4420. price_min = [[priceArray objectAtIndex:0] doubleValue];
  4421. price_max = [[priceArray objectAtIndex:1] doubleValue];
  4422. 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];
  4423. }
  4424. } else {
  4425. price = @"";
  4426. }
  4427. // sold_by_qty : Sold in quantities of %@
  4428. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  4429. NSString *qty = params[@"sold_by_qty"];
  4430. if ([qty isEqualToString:@"Display All"]) {
  4431. qty = @"";
  4432. } else {
  4433. qty = [self translateSingleQuote:qty];
  4434. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  4435. }
  4436. // category
  4437. NSString *category_id = params[@"ctgId"];
  4438. NSMutableArray *cate_id_array = nil;
  4439. NSMutableString *cateWhere = [NSMutableString string];
  4440. if ([category_id isEqualToString:@""] || !category_id) {
  4441. [cateWhere appendString:@"1 = 1"];
  4442. } else {
  4443. if ([category_id containsString:@","]) {
  4444. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  4445. } else {
  4446. cate_id_array = [@[category_id] mutableCopy];
  4447. }
  4448. /*-----------*/
  4449. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];
  4450. for (int i = 0; i < cate_id_array.count; i++) {
  4451. for (NSString *key0 in cateDic.allKeys) {
  4452. if ([key0 containsString:@"category_"]) {
  4453. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy];
  4454. for (NSString *key1 in category0.allKeys) {
  4455. if ([key1 containsString:@"category_"]) {
  4456. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy];
  4457. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  4458. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  4459. cate_id_array[i] = [category1 objectForKey:@"id"];
  4460. if (i == 0) {
  4461. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  4462. } else {
  4463. [cateWhere appendFormat:@"or category like'%%#%@#%%'",cate_id_array[i]];
  4464. }
  4465. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  4466. [category0 setValue:category1 forKey:key1];
  4467. [cateDic setValue:category0 forKey:key0];
  4468. }
  4469. }
  4470. }
  4471. }
  4472. }
  4473. }
  4474. [filter setValue:cateDic forKey:@"category"];
  4475. }
  4476. // where bestseller > 0 order by bestseller desc
  4477. // sql query: alert availability(int) best_seller(int) price qty
  4478. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete from (select name,description,product_id,closeout,best_seller 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];
  4479. // count
  4480. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  4481. }
  4482. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  4483. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  4484. if (!appDelegate.user) {
  4485. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  4486. }
  4487. [ret setValue:filter forKey:@"filter"];
  4488. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  4489. sqlite3_stmt * statement;
  4490. [ret setValue:@"2" forKey:@"result"];
  4491. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  4492. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  4493. // int count=0;
  4494. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4495. {
  4496. int i=0;
  4497. while (sqlite3_step(statement) == SQLITE_ROW)
  4498. {
  4499. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4500. char *name = (char*)sqlite3_column_text(statement, 0);
  4501. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  4502. char *description = (char*)sqlite3_column_text(statement, 1);
  4503. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  4504. int product_id = sqlite3_column_int(statement, 2);
  4505. int wid = sqlite3_column_int(statement, 3);
  4506. int closeout = sqlite3_column_int(statement, 4);
  4507. int cid = sqlite3_column_int(statement, 5);
  4508. int wisdelete = sqlite3_column_int(statement, 6);
  4509. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  4510. if(wid !=0 && wisdelete != 1)
  4511. [item setValue:@"true" forKey:@"wish_exists"];
  4512. else
  4513. [item setValue:@"false" forKey:@"wish_exists"];
  4514. if(closeout==0)
  4515. [item setValue:@"false" forKey:@"is_closeout"];
  4516. else
  4517. [item setValue:@"true" forKey:@"is_closeout"];
  4518. if(cid==0)
  4519. [item setValue:@"false" forKey:@"cart_exists"];
  4520. else
  4521. [item setValue:@"true" forKey:@"cart_exists"];
  4522. [item addEntriesFromDictionary:imgjson];
  4523. // [item setValue:nsurl forKey:@"img"];
  4524. [item setValue:nsname forKey:@"fash_name"];
  4525. [item setValue:nsdescription forKey:@"description"];
  4526. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  4527. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4528. i++;
  4529. }
  4530. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4531. [ret setObject:items forKey:@"items"];
  4532. sqlite3_finalize(statement);
  4533. }
  4534. [iSalesDB close_db:db];
  4535. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  4536. return ret;
  4537. }
  4538. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  4539. {
  4540. return [self itemsearch:params limited:YES];
  4541. }
  4542. #pragma mark order detail
  4543. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  4544. if (str1.length == 0) {
  4545. str1 = @"&nbsp";
  4546. }
  4547. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  4548. return str;
  4549. }
  4550. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  4551. {
  4552. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  4553. [fromformatter setDateFormat:from];
  4554. NSDate *date = [fromformatter dateFromString:datetime];
  4555. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  4556. [toformatter setDateFormat:to];
  4557. NSString * ret = [toformatter stringFromDate:date];
  4558. return ret;
  4559. }
  4560. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  4561. // 把毫秒去掉
  4562. if ([dateTime containsString:@"."]) {
  4563. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  4564. }
  4565. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  4566. formatter.dateFormat = formate;
  4567. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  4568. NSDate *date = [formatter dateFromString:dateTime];
  4569. formatter.dateFormat = newFormate;
  4570. NSString *result = [formatter stringFromDate:date];
  4571. return result ? result : @"";
  4572. }
  4573. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  4574. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  4575. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  4576. }
  4577. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  4578. {
  4579. DebugLog(@"offline oderdetail params: %@",params);
  4580. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4581. int orderId = [params[@"orderId"] intValue];
  4582. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  4583. // decrypt card number and card security code
  4584. // 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 ];
  4585. 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 _id=%d) o left join offline_contact c on o.customer_cid=c.contact_id",orderId];
  4586. sqlite3 *db = [iSalesDB get_db];
  4587. sqlite3_stmt * statement;
  4588. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  4589. NSString *nssoid = @"";
  4590. NSString* orderinfo = @"";
  4591. NSString *moreInfo = @"";
  4592. double handlingFee = 0;
  4593. double payments_and_credist = 0;
  4594. double totalPrice = 0;
  4595. double shippingFee = 0;
  4596. double lift_gate = 0;
  4597. NSString *customer_contact = @"";
  4598. NSString *customer_email = @"";
  4599. NSString *customer_fax = @"";
  4600. NSString *customer_phone = @"";
  4601. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  4602. {
  4603. if (sqlite3_step(statement) == SQLITE_ROW)
  4604. {
  4605. int order_id = sqlite3_column_int(statement, 0);
  4606. NSString *sign_url = [self textAtColumn:51 statement:statement];
  4607. ret[@"sign_url"] = sign_url;
  4608. customer_contact = [self textAtColumn:52 statement:statement];
  4609. customer_email = [self textAtColumn:53 statement:statement];
  4610. customer_phone = [self textAtColumn:54 statement:statement];
  4611. customer_fax = [self textAtColumn:55 statement:statement];
  4612. int offline_edit=sqlite3_column_int(statement, 56);
  4613. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  4614. char *soid = (char*)sqlite3_column_text(statement, 1);
  4615. if(soid==nil)
  4616. soid= "";
  4617. nssoid= [[NSString alloc]initWithUTF8String:soid];
  4618. // so#
  4619. ret[@"so#"] = nssoid;
  4620. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  4621. if(poNumber==nil)
  4622. poNumber= "";
  4623. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  4624. char *create_time = (char*)sqlite3_column_text(statement, 3);
  4625. if(create_time==nil)
  4626. create_time= "";
  4627. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  4628. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  4629. int status = sqlite3_column_int(statement, 4);
  4630. int erpStatus = sqlite3_column_int(statement, 49);
  4631. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  4632. // status
  4633. if (status > 1 && status != 3) {
  4634. status = erpStatus;
  4635. } else if (status == 3) {
  4636. status = 15;
  4637. }
  4638. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  4639. ret[@"order_status"] = nsstatus;
  4640. char *company_name = (char*)sqlite3_column_text(statement, 5);
  4641. if(company_name==nil)
  4642. company_name= "";
  4643. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  4644. // company name
  4645. ret[@"company_name"] = nscompany_name;
  4646. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  4647. if(customer_contact==nil)
  4648. customer_contact= "";
  4649. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  4650. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  4651. if(addr_1==nil)
  4652. addr_1="";
  4653. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4654. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  4655. if(addr_2==nil)
  4656. addr_2="";
  4657. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4658. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  4659. if(addr_3==nil)
  4660. addr_3="";
  4661. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4662. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  4663. if(addr_4==nil)
  4664. addr_4="";
  4665. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4666. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  4667. [arr_addr addObject:nsaddr_1];
  4668. [arr_addr addObject:nsaddr_2];
  4669. [arr_addr addObject:nsaddr_3];
  4670. [arr_addr addObject:nsaddr_4];
  4671. NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  4672. char *logist = (char*)sqlite3_column_text(statement, 11);
  4673. if(logist==nil)
  4674. logist= "";
  4675. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  4676. if (status == -11 || status == 10 || status == 11) {
  4677. nslogist = [self textAtColumn:59 statement:statement];
  4678. };
  4679. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  4680. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  4681. shipping = @"Shipping To Be Quoted";
  4682. } else {
  4683. shippingFee = sqlite3_column_double(statement, 12);
  4684. }
  4685. // Shipping
  4686. ret[@"Shipping"] = shipping;
  4687. int have_lift_gate = sqlite3_column_int(statement, 17);
  4688. lift_gate = sqlite3_column_double(statement, 13);
  4689. // Liftgate Fee(No loading dock)
  4690. if (!have_lift_gate) {
  4691. lift_gate = 0;
  4692. }
  4693. ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  4694. if (sqlite3_column_int(statement, 57)) {
  4695. ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  4696. }
  4697. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  4698. if(general_notes==nil)
  4699. general_notes= "";
  4700. NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  4701. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  4702. if(internal_notes==nil)
  4703. internal_notes= "";
  4704. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  4705. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  4706. if(payment_type==nil)
  4707. payment_type= "";
  4708. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  4709. // order info
  4710. orderinfo = [self textFileName:@"order_info.html"];
  4711. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  4712. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  4713. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  4714. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  4715. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  4716. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  4717. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  4718. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  4719. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  4720. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  4721. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  4722. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  4723. NSString *payment = nil;
  4724. if([nspayment_type isEqualToString:@"Credit Card"])
  4725. {
  4726. payment = [self textFileName:@"creditcardpayment.html"];
  4727. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  4728. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  4729. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  4730. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  4731. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  4732. NSString *card_type = [self textAtColumn:42 statement:statement];
  4733. if (card_type.length > 0) { // 显示星号
  4734. card_type = @"****";
  4735. }
  4736. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  4737. if (card_number.length > 0 && card_number.length > 4) {
  4738. for (int i = 0; i < card_number.length - 4; i++) {
  4739. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  4740. }
  4741. } else {
  4742. card_number = @"";
  4743. }
  4744. NSString *card_security_code = [self textAtColumn:44 statement:statement];
  4745. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  4746. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  4747. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  4748. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  4749. card_expiration = @"****";
  4750. }
  4751. NSString *card_city = [self textAtColumn:46 statement:statement];
  4752. NSString *card_state = [self textAtColumn:47 statement:statement];
  4753. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  4754. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  4755. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  4756. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  4757. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  4758. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  4759. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  4760. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  4761. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  4762. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  4763. }
  4764. else
  4765. {
  4766. payment=[self textFileName:@"normalpayment.html"];
  4767. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  4768. }
  4769. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  4770. ret[@"result"]= [NSNumber numberWithInt:2];
  4771. // more info
  4772. moreInfo = [self textFileName:@"more_info.html"];
  4773. /*****ship to******/
  4774. // ShipToCompany_or_&nbsp
  4775. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  4776. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  4777. NSString *shipToName = [self textAtColumn:19 statement:statement];
  4778. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  4779. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  4780. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  4781. /*****ship from******/
  4782. // ShipFromCompany_or_&nbsp
  4783. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  4784. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  4785. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  4786. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  4787. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  4788. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  4789. /*****freight to******/
  4790. // FreightBillToCompany_or_&nbsp
  4791. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  4792. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  4793. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  4794. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  4795. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  4796. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  4797. /*****merchandise to******/
  4798. // MerchandiseBillToCompany_or_&nbsp
  4799. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  4800. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  4801. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  4802. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  4803. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  4804. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  4805. /*****return to******/
  4806. // ReturnToCompany_or_&nbsp
  4807. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  4808. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  4809. NSString *returnToName = [self textAtColumn:31 statement:statement];
  4810. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  4811. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  4812. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  4813. //
  4814. // DebugLog(@"more info : %@",moreInfo);
  4815. // handling fee
  4816. handlingFee = sqlite3_column_double(statement, 33);
  4817. ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  4818. if (sqlite3_column_int(statement, 58)) {
  4819. ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  4820. }
  4821. //
  4822. // customer info
  4823. customerID = [self textAtColumn:36 statement:statement];
  4824. // mode
  4825. ret[@"mode"] = appDelegate.mode;
  4826. // model_count
  4827. ret[@"model_count"] = @(0);
  4828. }
  4829. sqlite3_finalize(statement);
  4830. }
  4831. [iSalesDB close_db:db];
  4832. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  4833. // "customer_email" = "Shui Hu";
  4834. // "customer_fax" = "";
  4835. // "customer_first_name" = F;
  4836. // "customer_last_name" = L;
  4837. // "customer_name" = ",da He Xiang Dong Liu A";
  4838. // "customer_phone" = "Hey Xuan Feng";
  4839. contactInfo[@"customer_phone"] = customer_phone;
  4840. contactInfo[@"customer_fax"] = customer_fax;
  4841. contactInfo[@"customer_email"] = customer_email;
  4842. NSString *first_name = @"";
  4843. NSString *last_name = @"";
  4844. if ([customer_contact isEqualToString:@""]) {
  4845. } else if ([customer_contact containsString:@" "]) {
  4846. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  4847. first_name = [customer_contact substringToIndex:first_space_index];
  4848. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  4849. }
  4850. contactInfo[@"customer_first_name"] = first_name;
  4851. contactInfo[@"customer_last_name"] = last_name;
  4852. ret[@"customerInfo"] = contactInfo;
  4853. // models
  4854. if (nssoid) {
  4855. __block double TotalCuft = 0;
  4856. __block double TotalWeight = 0;
  4857. __block int TotalCarton = 0;
  4858. __block double allItemPrice = 0;
  4859. 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];
  4860. sqlite3 *db1 = [iSalesDB get_db];
  4861. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4862. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  4863. int product_id = sqlite3_column_int(stmt, 0);
  4864. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  4865. int item_id = sqlite3_column_int(stmt, 7);
  4866. NSString* Price=nil;
  4867. if(str_price==nil)
  4868. {
  4869. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  4870. if(price==nil)
  4871. Price=@"No Price.";
  4872. else
  4873. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  4874. }
  4875. else
  4876. {
  4877. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  4878. }
  4879. double discount = sqlite3_column_double(stmt, 2);
  4880. int item_count = sqlite3_column_int(stmt, 3);
  4881. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  4882. NSString *nsline_note=nil;
  4883. if(line_note!=nil)
  4884. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  4885. // char *name = (char*)sqlite3_column_text(stmt, 5);
  4886. // NSString *nsname = nil;
  4887. // if(name!=nil)
  4888. // nsname= [[NSString alloc]initWithUTF8String:name];
  4889. NSString *nsname = [self textAtColumn:5 statement:stmt];
  4890. // char *description = (char*)sqlite3_column_text(stmt, 6);
  4891. // NSString *nsdescription=nil;
  4892. // if(description!=nil)
  4893. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  4894. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  4895. // int stockUom = sqlite3_column_int(stmt, 8);
  4896. // int _id = sqlite3_column_int(stmt, 9);
  4897. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  4898. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  4899. double weight=[bsubtotaljson[@"weight"] doubleValue];
  4900. int carton=[bsubtotaljson[@"carton"] intValue];
  4901. TotalCuft += cuft;
  4902. TotalWeight += weight;
  4903. TotalCarton += carton;
  4904. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  4905. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  4906. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  4907. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  4908. itemjson[@"note"]=nsline_note;
  4909. itemjson[@"origin_price"] = Price;
  4910. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  4911. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  4912. itemjson[@"order_item_status"] = @""; // 暂时不处理
  4913. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  4914. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  4915. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  4916. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  4917. if(itemjson[@"combine"] != nil)
  4918. {
  4919. // int citem=0;
  4920. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  4921. for(int bc=0;bc<bcount;bc++)
  4922. {
  4923. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  4924. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  4925. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  4926. subTotal += uprice * modulus * item_count;
  4927. }
  4928. }
  4929. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  4930. // NSLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  4931. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  4932. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  4933. allItemPrice += subTotal;
  4934. }];
  4935. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  4936. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  4937. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  4938. // payments/Credits
  4939. // payments_and_credist = sqlite3_column_double(statement, 34);
  4940. payments_and_credist = allItemPrice;
  4941. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  4942. // // total
  4943. // totalPrice = sqlite3_column_double(statement, 35);
  4944. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  4945. } else {
  4946. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  4947. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  4948. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  4949. // payments/Credits
  4950. payments_and_credist = 0;
  4951. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  4952. }
  4953. // total
  4954. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  4955. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  4956. ret[@"order_info"]= orderinfo;
  4957. ret[@"more_order_info"] = moreInfo;
  4958. return [RAUtils dict2data:ret];
  4959. }
  4960. #pragma mark order list
  4961. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  4962. NSString *nsstatus = @"";
  4963. switch (status) {
  4964. case 0:
  4965. {
  4966. nsstatus=@"Temp Order";
  4967. break;
  4968. }
  4969. case 1:
  4970. {
  4971. nsstatus=@"Saved Order";
  4972. break;
  4973. }
  4974. case 2: {
  4975. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  4976. break;
  4977. }
  4978. case 3:
  4979. case 15:
  4980. {
  4981. nsstatus=@"Cancelled";
  4982. break;
  4983. }
  4984. case 10:
  4985. {
  4986. nsstatus=@"Quote Submitted";
  4987. break;
  4988. }
  4989. case 11:
  4990. {
  4991. nsstatus=@"Sales Order Submitted";
  4992. break;
  4993. }
  4994. case 12:
  4995. {
  4996. nsstatus=@"Processing";
  4997. break;
  4998. }
  4999. case 13:
  5000. {
  5001. nsstatus=@"Shipped";
  5002. break;
  5003. }
  5004. case 14:
  5005. {
  5006. nsstatus=@"Closed";
  5007. break;
  5008. }
  5009. case -11:
  5010. {
  5011. nsstatus = @"Ready For Submit";
  5012. break;
  5013. }
  5014. default:
  5015. break;
  5016. }
  5017. return nsstatus;
  5018. }
  5019. + (double) orderTotalPrice:(NSString *)so_id db:(sqlite3 *)db1 close:(BOOL)close{
  5020. double total = 0;
  5021. __block double payments_and_credist = 0;
  5022. __block double allItemPrice = 0;
  5023. // sqlite3 *db1 = [iSalesDB get_db];
  5024. if (so_id) {
  5025. // 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];
  5026. NSString *modelSql = [NSString stringWithFormat:@"select product_id,price,discount,item_count,item_id from (select product_id,decrypt(str_price) as price,discount,item_count,item_id from offline_cart where so_no='%@');",so_id];
  5027. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5028. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5029. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  5030. int product_id = sqlite3_column_int(stmt, 0);
  5031. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  5032. int discount = sqlite3_column_int(stmt, 2);
  5033. int item_count = sqlite3_column_int(stmt, 3);
  5034. // int item_id = sqlite3_column_int(stmt, 7);
  5035. int item_id = sqlite3_column_int(stmt, 4);
  5036. NSString* Price=nil;
  5037. if(str_price==nil)
  5038. {
  5039. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  5040. if(price==nil)
  5041. Price=@"No Price.";
  5042. else
  5043. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  5044. }
  5045. else
  5046. {
  5047. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  5048. }
  5049. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  5050. itemjson[@"The unit price"]=Price;
  5051. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  5052. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  5053. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  5054. if(itemjson[@"combine"] != nil)
  5055. {
  5056. // int citem=0;
  5057. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  5058. for(int bc=0;bc<bcount;bc++)
  5059. {
  5060. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  5061. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  5062. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  5063. subTotal += uprice * modulus * item_count;
  5064. }
  5065. }
  5066. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  5067. allItemPrice += subTotal;
  5068. }];
  5069. payments_and_credist = allItemPrice;
  5070. } else {
  5071. // payments/Credits
  5072. payments_and_credist = 0;
  5073. }
  5074. // lift_gate handlingFee shippingFee
  5075. __block double lift_gate = 0;
  5076. __block double handlingFee = 0;
  5077. __block double shippingFee = 0;
  5078. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  5079. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5080. int have_lift_gate = sqlite3_column_int(stmt, 0);
  5081. if (have_lift_gate) {
  5082. lift_gate = sqlite3_column_double(stmt, 1);
  5083. }
  5084. handlingFee = sqlite3_column_double(stmt, 2);
  5085. shippingFee = sqlite3_column_double(stmt, 3);
  5086. }];
  5087. // total
  5088. total = payments_and_credist + lift_gate + handlingFee + shippingFee;
  5089. if (close) {
  5090. [iSalesDB close_db:db1];
  5091. }
  5092. return total;
  5093. }
  5094. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  5095. {
  5096. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5097. int limit = [[params valueForKey:@"limit"] intValue];
  5098. int offset = [[params valueForKey:@"offset"] intValue];
  5099. NSString* keyword = [params valueForKey:@"keyWord"];
  5100. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  5101. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  5102. NSString* where=@"1 = 1";
  5103. if(keyword.length>0)
  5104. 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]];
  5105. if (orderStatus.length > 0) {
  5106. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  5107. if (order_status_array.count == 1) {
  5108. int status_value = [[order_status_array firstObject] integerValue];
  5109. if (status_value <= 1 || status_value == 3) {
  5110. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  5111. } else {
  5112. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  5113. }
  5114. } else if (order_status_array.count > 1) {
  5115. for (int i = 0; i < order_status_array.count;i++) {
  5116. NSString *status = order_status_array[i];
  5117. NSString *condition = @" or";
  5118. if (i == 0) {
  5119. condition = @" and (";
  5120. }
  5121. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  5122. int status_value = [status integerValue];
  5123. if (status_value <= 1 || status_value == 3) {
  5124. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  5125. } else {
  5126. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  5127. }
  5128. }
  5129. where = [where stringByAppendingString:@" )"];
  5130. }
  5131. }
  5132. 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.modify_time desc,o.create_time desc limit %d offset %d" ,where, limit, offset];
  5133. // DebugLog(@"order list sql: %@",sqlQuery);
  5134. sqlite3 *db = [iSalesDB get_db];
  5135. sqlite3_stmt * statement;
  5136. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  5137. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5138. {
  5139. int count=0;
  5140. while (sqlite3_step(statement) == SQLITE_ROW)
  5141. {
  5142. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  5143. int order_id = sqlite3_column_double(statement, 0);
  5144. char *soid = (char*)sqlite3_column_text(statement, 1);
  5145. if(soid==nil)
  5146. soid= "";
  5147. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  5148. int status = sqlite3_column_double(statement, 2);
  5149. int erpStatus = sqlite3_column_double(statement, 9);
  5150. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  5151. if(sales_rep==nil)
  5152. sales_rep= "";
  5153. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  5154. char *create_by = (char*)sqlite3_column_text(statement, 4);
  5155. if(create_by==nil)
  5156. create_by= "";
  5157. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  5158. char *company_name = (char*)sqlite3_column_text(statement, 5);
  5159. if(company_name==nil)
  5160. company_name= "";
  5161. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  5162. char *create_time = (char*)sqlite3_column_text(statement, 6);
  5163. if(create_time==nil)
  5164. create_time= "";
  5165. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  5166. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  5167. // double total_price = sqlite3_column_double(statement, 7);
  5168. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] db:db close:NO];
  5169. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  5170. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  5171. int offline_edit = sqlite3_column_int(statement, 10);
  5172. // ": "JH",
  5173. // "": "$8307.00",
  5174. // "": "MOB1608050001",
  5175. // "": "ArpithaT",
  5176. // "": "1st Stage Property Transformations",
  5177. // "": "JANICE SUTTON",
  5178. // "": 2255,
  5179. // "": "08/02/2016 09:49:18",
  5180. // "": 1,
  5181. // "": "Saved Order"
  5182. // "": "1470384050483",
  5183. // "model_count": "6 / 28"
  5184. item[@"sales_rep"]= nssales_rep;
  5185. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  5186. item[@"so#"]= nssoid;
  5187. item[@"create_by"]= nscreate_by;
  5188. item[@"customer_name"]= nscompany_name;
  5189. item[@"customer_contact"] = customer_contact;
  5190. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  5191. item[@"purchase_time"]= nscreate_time;
  5192. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",status];
  5193. item[@"order_status"]= nsstatus;
  5194. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  5195. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  5196. // item[@"model_count"]
  5197. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  5198. count++;
  5199. }
  5200. ret[@"count"]= [NSNumber numberWithInt:count];
  5201. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  5202. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  5203. ret[@"result"]= [NSNumber numberWithInt:2];
  5204. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  5205. ret[@"time_zone"] = @"PST";
  5206. sqlite3_finalize(statement);
  5207. }
  5208. 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];
  5209. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  5210. [iSalesDB execSql:@"END TRANSACTION" db:db];
  5211. [iSalesDB close_db:db];
  5212. return [RAUtils dict2data:ret];
  5213. }
  5214. #pragma mark update gnotes
  5215. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  5216. {
  5217. DebugLog(@"params: %@",params);
  5218. // comments = Meyoyoyoyoyoyoy;
  5219. // orderCode = MOB1608110001;
  5220. // password = 123456;
  5221. // user = EvanK;
  5222. 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]];
  5223. int ret = [iSalesDB execSql:sql];
  5224. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5225. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5226. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5227. // [dic setValue:@"160409" forKey:@"min_ver"];
  5228. return [RAUtils dict2data:dic];
  5229. }
  5230. #pragma mark move to wishlist
  5231. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  5232. {
  5233. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  5234. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5235. _id = [NSString stringWithFormat:@"(%@)",_id];
  5236. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  5237. sqlite3 *db = [iSalesDB get_db];
  5238. __block NSString *product_id = @"";
  5239. __block NSString *item_count_str = @"";
  5240. // __block NSString *item_id = nil;
  5241. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5242. // product_id = [self textAtColumn:0 statement:stmt];
  5243. int item_count = sqlite3_column_int(stmt, 1);
  5244. // item_id = [self textAtColumn:2 statement:stmt];
  5245. NSString *p_id = [self textAtColumn:0 statement:stmt];
  5246. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  5247. if (p_id.length) {
  5248. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  5249. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  5250. }
  5251. }];
  5252. [iSalesDB close_db:db];
  5253. // 去除第一个,
  5254. product_id = [product_id substringFromIndex:1];
  5255. item_count_str = [item_count_str substringFromIndex:1];
  5256. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5257. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  5258. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  5259. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  5260. sqlite3 *db1 = [iSalesDB get_db];
  5261. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  5262. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5263. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  5264. // 删除
  5265. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  5266. int ret = [iSalesDB execSql:deleteSql db:db1];
  5267. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5268. [iSalesDB close_db:db1];
  5269. return [RAUtils dict2data:dic];
  5270. }
  5271. #pragma mark cart delete
  5272. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  5273. {
  5274. // cartItemId = 548;
  5275. // orderCode = MOB1608110001;
  5276. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  5277. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5278. _id = [NSString stringWithFormat:@"(%@)",_id];
  5279. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  5280. int ret = [iSalesDB execSql:sql];
  5281. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5282. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5283. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5284. // [dic setValue:@"160409" forKey:@"min_ver"];
  5285. return [RAUtils dict2data:dic];
  5286. }
  5287. #pragma mark set price
  5288. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  5289. {
  5290. DebugLog(@"cart set price params: %@",params);
  5291. // "cartitem_id" = 1;
  5292. // discount = "0.000000";
  5293. // "item_note" = "";
  5294. // price = "269.000000";
  5295. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  5296. NSString *notes = [self valueInParams:params key:@"item_note"];
  5297. NSString *discount = [self valueInParams:params key:@"discount"];
  5298. NSString *price = [self valueInParams:params key:@"price"];
  5299. // bool badd_price_changed=false;
  5300. // sqlite3* db=[iSalesDB get_db];
  5301. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5302. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  5303. // NSRange range;
  5304. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  5305. //
  5306. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  5307. // badd_price_changed=true;
  5308. // [iSalesDB close_db:db];
  5309. //
  5310. // if(badd_price_changed)
  5311. // {
  5312. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  5313. // }
  5314. //
  5315. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  5316. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  5317. int ret = [iSalesDB execSql:sql];
  5318. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5319. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5320. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5321. // [dic setValue:@"160409" forKey:@"min_ver"];
  5322. return [RAUtils dict2data:dic];
  5323. }
  5324. #pragma mark set line notes
  5325. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  5326. {
  5327. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  5328. NSString *notes = [self valueInParams:params key:@"notes"];
  5329. notes = [self translateSingleQuote:notes];
  5330. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  5331. int ret = [iSalesDB execSql:sql];
  5332. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5333. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5334. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5335. // [dic setValue:@"160409" forKey:@"min_ver"];
  5336. return [RAUtils dict2data:dic];
  5337. }
  5338. #pragma mark set qty
  5339. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  5340. {
  5341. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  5342. int item_count = [params[@"inputInt"] integerValue];
  5343. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  5344. int ret = [iSalesDB execSql:sql];
  5345. __block int item_id = 0;
  5346. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5347. item_id = sqlite3_column_int(stmt, 0);
  5348. }];
  5349. sqlite3 *db = [iSalesDB get_db];
  5350. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  5351. [iSalesDB close_db:db];
  5352. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5353. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5354. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5355. // [dic setValue:@"160409" forKey:@"min_ver"];
  5356. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  5357. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  5358. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  5359. return [RAUtils dict2data:dic];
  5360. }
  5361. #pragma mark place order
  5362. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  5363. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5364. if (aname) {
  5365. [dic setValue:aname forKey:@"aname"];
  5366. }
  5367. if (control) {
  5368. [dic setValue:control forKey:@"control"];
  5369. }
  5370. if (keyboard) {
  5371. [dic setValue:keyboard forKey:@"keyboard"];
  5372. }
  5373. if (name) {
  5374. [dic setValue:name forKey:@"name"];
  5375. }
  5376. if (value) {
  5377. [dic setValue:value forKey:@"value"];
  5378. }
  5379. return dic;
  5380. }
  5381. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  5382. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5383. orderCode = [self translateSingleQuote:orderCode];
  5384. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  5385. 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];
  5386. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5387. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  5388. NSString *img0 = [self textAtColumn:1 statement:stmt];
  5389. NSString *img1 = [self textAtColumn:2 statement:stmt];
  5390. NSString *img2 = [self textAtColumn:3 statement:stmt];
  5391. NSString *company_name = [self textAtColumn:4 statement:stmt];
  5392. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  5393. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  5394. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  5395. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  5396. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  5397. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  5398. NSString *email = [self textAtColumn:11 statement:stmt];
  5399. NSString *phone = [self textAtColumn:12 statement:stmt];
  5400. NSString *fax = [self textAtColumn:13 statement:stmt];
  5401. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  5402. NSString *city = [self textAtColumn:15 statement:stmt];
  5403. NSString *state = [self textAtColumn:16 statement:stmt];
  5404. NSString *country = [self textAtColumn:17 statement:stmt];
  5405. NSString *name = [self textAtColumn:18 statement:stmt];
  5406. // contact id
  5407. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  5408. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  5409. [contact_id_dic setValue:@"text" forKey:@"control"];
  5410. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  5411. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  5412. [contact_id_dic setValue:@"true" forKey:@"required"];
  5413. [contact_id_dic setValue:contact_id forKey:@"value"];
  5414. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  5415. // business card
  5416. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  5417. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  5418. [business_card_dic setValue:@"img" forKey:@"control"];
  5419. [business_card_dic setValue:@"1" forKey:@"disable"];
  5420. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  5421. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  5422. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  5423. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  5424. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  5425. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  5426. [business_card_dic setValue:@"business_card" forKey:@"name"];
  5427. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  5428. // fax
  5429. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5430. [customer_dic setValue:fax_dic forKey:@"item_10"];
  5431. // zipcode
  5432. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  5433. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  5434. // city
  5435. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  5436. [customer_dic setValue:city_dic forKey:@"item_12"];
  5437. // state
  5438. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  5439. [customer_dic setValue:state_dic forKey:@"item_13"];
  5440. // country
  5441. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  5442. [customer_dic setValue:country_dic forKey:@"item_14"];
  5443. // company name
  5444. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  5445. [customer_dic setValue:company_dic forKey:@"item_2"];
  5446. // addr_1
  5447. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  5448. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  5449. // addr_2
  5450. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  5451. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  5452. // addr_3
  5453. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  5454. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  5455. // addr_4
  5456. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  5457. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  5458. // Contact
  5459. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  5460. [customer_dic setValue:contact_dic forKey:@"item_7"];
  5461. // email
  5462. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5463. [customer_dic setValue:email_dic forKey:@"item_8"];
  5464. // phone
  5465. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5466. [customer_dic setValue:phone_dic forKey:@"item_9"];
  5467. // title
  5468. [customer_dic setValue:@"Customer" forKey:@"title"];
  5469. // count
  5470. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  5471. }];
  5472. // setting
  5473. NSDictionary *setting = params[@"setting"];
  5474. NSNumber *hide = setting[@"CustomerHide"];
  5475. [customer_dic setValue:hide forKey:@"hide"];
  5476. return customer_dic;
  5477. }
  5478. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  5479. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5480. // setting
  5481. NSDictionary *setting = params[@"setting"];
  5482. NSNumber *hide = setting[@"ShipToHide"];
  5483. [dic setValue:hide forKey:@"hide"];
  5484. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5485. orderCode = [self translateSingleQuote:orderCode];
  5486. 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];
  5487. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5488. NSString *cid = [self textAtColumn:0 statement:stmt];
  5489. NSString *name = [self textAtColumn:1 statement:stmt];
  5490. NSString *ext = [self textAtColumn:2 statement:stmt];
  5491. NSString *contact = [self textAtColumn:3 statement:stmt];
  5492. NSString *email = [self textAtColumn:4 statement:stmt];
  5493. NSString *fax = [self textAtColumn:5 statement:stmt];
  5494. NSString *phone = [self textAtColumn:6 statement:stmt];
  5495. // count
  5496. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5497. // title
  5498. [dic setValue:@"Ship To" forKey:@"title"];
  5499. // choose
  5500. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5501. [choose_dic setValue:@"choose" forKey:@"aname"];
  5502. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5503. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  5504. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  5505. @"key_map" : @{@"receive_cid" : @"customer_cid",
  5506. @"receive_contact" : @"customer_contact",
  5507. @"receive_email" : @"customer_email",
  5508. @"receive_ext" : @"customer_contact_ext",
  5509. @"receive_fax" : @"customer_fax",
  5510. @"receive_name" : @"customer_name",
  5511. @"receive_phone" : @"customer_phone"},
  5512. @"refresh" : [NSNumber numberWithInteger:1],
  5513. @"type" : @"pull"};
  5514. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  5515. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  5516. @"name" : @"Add new address",
  5517. @"refresh" : [NSNumber numberWithInteger:1],
  5518. @"value" : @"new_addr"
  5519. };
  5520. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  5521. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  5522. @"key_map" : @{@"receive_cid" : @"customer_cid",
  5523. @"receive_contact" : @"customer_contact",
  5524. @"receive_email" : @"customer_email",
  5525. @"receive_ext" : @"customer_contact_ext",
  5526. @"receive_fax" : @"customer_fax",
  5527. @"receive_name" : @"customer_name",
  5528. @"receive_phone" : @"customer_phone"},
  5529. @"name" : @"select_ship_to",
  5530. @"refresh" : [NSNumber numberWithInteger:1],
  5531. @"value" : @"Sales_Order_Ship_To"};
  5532. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  5533. [dic setValue:choose_dic forKey:@"item_0"];
  5534. // contact id
  5535. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5536. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5537. [contact_id_dic setValue:@"true" forKey:@"required"];
  5538. [dic setValue:contact_id_dic forKey:@"item_1"];
  5539. // company name
  5540. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5541. [dic setValue:company_name_dic forKey:@"item_2"];
  5542. // address
  5543. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  5544. NSNumber *required = setting[@"ShippingToAddressRequire"];
  5545. if ([required integerValue]) {
  5546. [ext_dic setValue:@"true" forKey:@"required"];
  5547. }
  5548. [dic setValue:ext_dic forKey:@"item_3"];
  5549. // contact
  5550. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5551. [dic setValue:contact_dic forKey:@"item_4"];
  5552. // phone
  5553. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5554. [dic setValue:phone_dic forKey:@"item_5"];
  5555. // fax
  5556. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5557. [dic setValue:fax_dic forKey:@"item_6"];
  5558. // email
  5559. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5560. [dic setValue:email_dic forKey:@"item_7"];
  5561. }];
  5562. return dic;
  5563. }
  5564. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  5565. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5566. // setting
  5567. NSDictionary *setting = params[@"setting"];
  5568. NSNumber *hide = setting[@"ShipFromHide"];
  5569. [dic setValue:hide forKey:@"hide"];
  5570. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5571. orderCode = [self translateSingleQuote:orderCode];
  5572. NSString *sql = [NSString stringWithFormat:@"select sender_cid,sender_name,sender_ext,sender_contact,sender_email,sender_fax,sender_phone from offline_order where so_id = '%@';",orderCode];
  5573. NSString *ship_from_default_sql = @"select contact_id,decrypt(company_name),decrypt(addr_1),first_name || ' ' || last_name,email,fax,phone from offline_contact where contact_id = 'NPD';";
  5574. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5575. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  5576. __block NSString *name = [self textAtColumn:1 statement:stmt];
  5577. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  5578. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  5579. __block NSString *email = [self textAtColumn:4 statement:stmt];
  5580. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  5581. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  5582. if (!cid.length) {
  5583. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  5584. cid = [self textAtColumn:0 statement:statment];
  5585. name = [self textAtColumn:1 statement:statment];
  5586. ext = [self textAtColumn:2 statement:statment];
  5587. contact = [self textAtColumn:3 statement:statment];
  5588. email = [self textAtColumn:4 statement:statment];
  5589. fax = [self textAtColumn:5 statement:statment];
  5590. phone = [self textAtColumn:6 statement:statment];
  5591. }];
  5592. }
  5593. // count
  5594. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5595. // title
  5596. [dic setValue:@"Ship From" forKey:@"title"];
  5597. // hide
  5598. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  5599. // choose
  5600. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5601. [choose_dic setValue:@"choose" forKey:@"aname"];
  5602. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5603. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  5604. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  5605. @"key_map" : @{@"sender_cid" : @"customer_cid",
  5606. @"sender_contact" : @"customer_contact",
  5607. @"sender_email" : @"customer_email",
  5608. @"sender_ext" : @"customer_contact_ext",
  5609. @"sender_fax" : @"customer_fax",
  5610. @"sender_name" : @"customer_name",
  5611. @"sender_phone" : @"customer_phone"},
  5612. @"name" : @"select_cid",
  5613. @"refresh" : [NSNumber numberWithInteger:0],
  5614. @"value" : @"Sales_Order_Ship_From"};
  5615. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  5616. [dic setValue:choose_dic forKey:@"item_0"];
  5617. // contact id
  5618. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5619. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5620. [contact_id_dic setValue:@"true" forKey:@"required"];
  5621. [dic setValue:contact_id_dic forKey:@"item_1"];
  5622. // company name
  5623. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5624. [dic setValue:company_name_dic forKey:@"item_2"];
  5625. // address
  5626. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  5627. [dic setValue:ext_dic forKey:@"item_3"];
  5628. // contact
  5629. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5630. [dic setValue:contact_dic forKey:@"item_4"];
  5631. // phone
  5632. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5633. [dic setValue:phone_dic forKey:@"item_5"];
  5634. // fax
  5635. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5636. [dic setValue:fax_dic forKey:@"item_6"];
  5637. // email
  5638. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5639. [dic setValue:email_dic forKey:@"item_7"];
  5640. }];
  5641. return dic;
  5642. }
  5643. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  5644. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5645. // setting
  5646. NSDictionary *setting = params[@"setting"];
  5647. NSNumber *hide = setting[@"FreightBillToHide"];
  5648. [dic setValue:hide forKey:@"hide"];
  5649. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5650. orderCode = [self translateSingleQuote:orderCode];
  5651. NSString *sql = [NSString stringWithFormat:@"select shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_email,shipping_billto_fax,shipping_billto_phone from offline_order where so_id = '%@';",orderCode];
  5652. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5653. NSString *cid = [self textAtColumn:0 statement:stmt];
  5654. NSString *name = [self textAtColumn:1 statement:stmt];
  5655. NSString *ext = [self textAtColumn:2 statement:stmt];
  5656. NSString *contact = [self textAtColumn:3 statement:stmt];
  5657. NSString *email = [self textAtColumn:4 statement:stmt];
  5658. NSString *fax = [self textAtColumn:5 statement:stmt];
  5659. NSString *phone = [self textAtColumn:6 statement:stmt];
  5660. // count
  5661. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5662. // title
  5663. [dic setValue:@"Freight Bill To" forKey:@"title"];
  5664. // hide
  5665. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  5666. // choose
  5667. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5668. [choose_dic setValue:@"choose" forKey:@"aname"];
  5669. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5670. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  5671. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  5672. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  5673. @"shipping_billto_contact" : @"receive_contact",
  5674. @"shipping_billto_email" : @"receive_email",
  5675. @"shipping_billto_ext" : @"receive_ext",
  5676. @"shipping_billto_fax" : @"receive_fax",
  5677. @"shipping_billto_name" : @"receive_name",
  5678. @"shipping_billto_phone" : @"receive_phone"},
  5679. @"type" : @"pull"};
  5680. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  5681. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  5682. @"type" : @"pull",
  5683. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  5684. @"shipping_billto_contact" : @"customer_contact",
  5685. @"shipping_billto_email" : @"customer_email",
  5686. @"shipping_billto_ext" : @"customer_contact_ext",
  5687. @"shipping_billto_fax" : @"customer_fax",
  5688. @"shipping_billto_name" : @"customer_name",
  5689. @"shipping_billto_phone" : @"customer_phone"}
  5690. };
  5691. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  5692. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  5693. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  5694. @"shipping_billto_contact" : @"sender_contact",
  5695. @"shipping_billto_email" : @"sender_email",
  5696. @"shipping_billto_ext" : @"sender_ext",
  5697. @"shipping_billto_fax" : @"sender_fax",
  5698. @"shipping_billto_name" : @"sender_name",
  5699. @"shipping_billto_phone" : @"sender_phone"},
  5700. @"type" : @"pull"
  5701. };
  5702. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  5703. NSDictionary *select_freight_bill_to_dic = @{
  5704. @"aname" : @"Select freight bill to",
  5705. @"name" : @"select_cid",
  5706. @"refresh" : [NSNumber numberWithInteger:0],
  5707. @"value" : @"Sales_Order_Freight_Bill_To",
  5708. @"key_map" : @{
  5709. @"shipping_billto_cid" : @"customer_cid",
  5710. @"shipping_billto_contact" : @"customer_contact",
  5711. @"shipping_billto_email" : @"customer_email",
  5712. @"shipping_billto_ext" : @"customer_contact_ext",
  5713. @"shipping_billto_fax" : @"customer_fax",
  5714. @"shipping_billto_name" : @"customer_name",
  5715. @"shipping_billto_phone" : @"customer_phone"
  5716. }
  5717. };
  5718. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  5719. [dic setValue:choose_dic forKey:@"item_0"];
  5720. // contact id
  5721. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5722. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5723. [contact_id_dic setValue:@"true" forKey:@"required"];
  5724. [dic setValue:contact_id_dic forKey:@"item_1"];
  5725. // company name
  5726. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5727. [dic setValue:company_name_dic forKey:@"item_2"];
  5728. // address
  5729. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  5730. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  5731. if ([ext_required integerValue]) {
  5732. [ext_dic setValue:@"true" forKey:@"required"];
  5733. }
  5734. [dic setValue:ext_dic forKey:@"item_3"];
  5735. // contact
  5736. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5737. [dic setValue:contact_dic forKey:@"item_4"];
  5738. // phone
  5739. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5740. [dic setValue:phone_dic forKey:@"item_5"];
  5741. // fax
  5742. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5743. [dic setValue:fax_dic forKey:@"item_6"];
  5744. // email
  5745. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5746. [dic setValue:email_dic forKey:@"item_7"];
  5747. }];
  5748. return dic;
  5749. }
  5750. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  5751. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5752. // setting
  5753. NSDictionary *setting = params[@"setting"];
  5754. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  5755. [dic setValue:hide forKey:@"hide"];
  5756. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5757. orderCode = [self translateSingleQuote:orderCode];
  5758. NSString *sql = [NSString stringWithFormat:@"select billing_cid,billing_name,billing_ext,billing_contact,billing_email,billing_fax,billing_phone from offline_order where so_id = '%@';",orderCode];
  5759. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5760. NSString *cid = [self textAtColumn:0 statement:stmt];
  5761. NSString *name = [self textAtColumn:1 statement:stmt];
  5762. NSString *ext = [self textAtColumn:2 statement:stmt];
  5763. NSString *contact = [self textAtColumn:3 statement:stmt];
  5764. NSString *email = [self textAtColumn:4 statement:stmt];
  5765. NSString *fax = [self textAtColumn:5 statement:stmt];
  5766. NSString *phone = [self textAtColumn:6 statement:stmt];
  5767. // count
  5768. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5769. // title
  5770. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  5771. // hide
  5772. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  5773. // choose
  5774. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5775. [choose_dic setValue:@"choose" forKey:@"aname"];
  5776. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5777. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  5778. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  5779. @"key_map" : @{@"billing_cid" : @"receive_cid",
  5780. @"billing_contact" : @"receive_contact",
  5781. @"billing_email" : @"receive_email",
  5782. @"billing_ext" : @"receive_ext",
  5783. @"billing_fax" : @"receive_fax",
  5784. @"billing_name" : @"receive_name",
  5785. @"billing_phone" : @"receive_phone"},
  5786. @"type" : @"pull"};
  5787. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  5788. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  5789. @"type" : @"pull",
  5790. @"key_map" : @{@"billing_cid" : @"customer_cid",
  5791. @"billing_contact" : @"customer_contact",
  5792. @"billing_email" : @"customer_email",
  5793. @"billing_ext" : @"customer_contact_ext",
  5794. @"billing_fax" : @"customer_fax",
  5795. @"billing_name" : @"customer_name",
  5796. @"billing_phone" : @"customer_phone"}
  5797. };
  5798. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  5799. NSDictionary *select_bill_to_dic = @{
  5800. @"aname" : @"Select bill to",
  5801. @"name" : @"select_cid",
  5802. @"refresh" : [NSNumber numberWithInteger:0],
  5803. @"value" : @"Sales_Order_Merchandise_Bill_To",
  5804. @"key_map" : @{
  5805. @"billing_cid" : @"customer_cid",
  5806. @"billing_contact" : @"customer_contact",
  5807. @"billing_email" : @"customer_email",
  5808. @"billing_ext" : @"customer_contact_ext",
  5809. @"billing_fax" : @"customer_fax",
  5810. @"billing_name" : @"customer_name",
  5811. @"billing_phone" : @"customer_phone"
  5812. }
  5813. };
  5814. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  5815. [dic setValue:choose_dic forKey:@"item_0"];
  5816. // contact id
  5817. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5818. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5819. [contact_id_dic setValue:@"true" forKey:@"required"];
  5820. [dic setValue:contact_id_dic forKey:@"item_1"];
  5821. // company name
  5822. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5823. [dic setValue:company_name_dic forKey:@"item_2"];
  5824. // address
  5825. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  5826. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  5827. if ([ext_required integerValue]) {
  5828. [ext_dic setValue:@"true" forKey:@"required"];
  5829. }
  5830. [dic setValue:ext_dic forKey:@"item_3"];
  5831. // contact
  5832. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5833. [dic setValue:contact_dic forKey:@"item_4"];
  5834. // phone
  5835. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5836. [dic setValue:phone_dic forKey:@"item_5"];
  5837. // fax
  5838. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5839. [dic setValue:fax_dic forKey:@"item_6"];
  5840. // email
  5841. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5842. [dic setValue:email_dic forKey:@"item_7"];
  5843. }];
  5844. return dic;
  5845. }
  5846. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  5847. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5848. // setting
  5849. NSDictionary *setting = params[@"setting"];
  5850. NSNumber *hide = setting[@"ReturnToHide"];
  5851. [dic setValue:hide forKey:@"hide"];
  5852. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5853. orderCode = [self translateSingleQuote:orderCode];
  5854. NSString *sql = [NSString stringWithFormat:@"select returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_email,returnto_fax,returnto_phone from offline_order where so_id = '%@';",orderCode];
  5855. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5856. NSString *cid = [self textAtColumn:0 statement:stmt];
  5857. NSString *name = [self textAtColumn:1 statement:stmt];
  5858. NSString *ext = [self textAtColumn:2 statement:stmt];
  5859. NSString *contact = [self textAtColumn:3 statement:stmt];
  5860. NSString *email = [self textAtColumn:4 statement:stmt];
  5861. NSString *fax = [self textAtColumn:5 statement:stmt];
  5862. NSString *phone = [self textAtColumn:6 statement:stmt];
  5863. // count
  5864. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5865. // title
  5866. [dic setValue:@"Return To" forKey:@"title"];
  5867. // hide
  5868. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  5869. // choose
  5870. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5871. [choose_dic setValue:@"choose" forKey:@"aname"];
  5872. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5873. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  5874. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  5875. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  5876. @"returnto_contact" : @"sender_contact",
  5877. @"returnto_email" : @"sender_email",
  5878. @"returnto_ext" : @"sender_ext",
  5879. @"returnto_fax" : @"sender_fax",
  5880. @"returnto_name" : @"sender_name",
  5881. @"returnto_phone" : @"sender_phone"},
  5882. @"type" : @"pull"};
  5883. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  5884. NSDictionary *select_return_to_dic = @{
  5885. @"aname" : @"Select return to",
  5886. @"name" : @"select_cid",
  5887. @"refresh" : [NSNumber numberWithInteger:0],
  5888. @"value" : @"Contact_Return_To",
  5889. @"key_map" : @{
  5890. @"returnto_cid" : @"customer_cid",
  5891. @"returnto_contact" : @"customer_contact",
  5892. @"returnto_email" : @"customer_email",
  5893. @"returnto_ext" : @"customer_contact_ext",
  5894. @"returnto_fax" : @"customer_fax",
  5895. @"returnto_name" : @"customer_name",
  5896. @"returnto_phone" : @"customer_phone"
  5897. }
  5898. };
  5899. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  5900. [dic setValue:choose_dic forKey:@"item_0"];
  5901. // contact id
  5902. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5903. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5904. [contact_id_dic setValue:@"true" forKey:@"required"];
  5905. [dic setValue:contact_id_dic forKey:@"item_1"];
  5906. // company name
  5907. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5908. [dic setValue:company_name_dic forKey:@"item_2"];
  5909. // address
  5910. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  5911. [dic setValue:ext_dic forKey:@"item_3"];
  5912. // contact
  5913. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5914. [dic setValue:contact_dic forKey:@"item_4"];
  5915. // phone
  5916. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5917. [dic setValue:phone_dic forKey:@"item_5"];
  5918. // fax
  5919. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5920. [dic setValue:fax_dic forKey:@"item_6"];
  5921. // email
  5922. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5923. [dic setValue:email_dic forKey:@"item_7"];
  5924. }];
  5925. return dic;
  5926. }
  5927. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  5928. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5929. orderCode = [self translateSingleQuote:orderCode];
  5930. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5931. __block double TotalCuft = 0;
  5932. __block double TotalWeight = 0;
  5933. __block int TotalCarton = 0;
  5934. __block double payments = 0;
  5935. // setting
  5936. NSDictionary *setting = params[@"setting"];
  5937. NSNumber *hide = setting[@"ModelInformationHide"];
  5938. [dic setValue:hide forKey:@"hide"];
  5939. 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];
  5940. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5941. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5942. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  5943. // item id
  5944. int item_id = sqlite3_column_int(stmt, 0);
  5945. // count
  5946. int item_count = sqlite3_column_int(stmt, 1);
  5947. // stockUom
  5948. int stockUom = sqlite3_column_int(stmt, 2);
  5949. // unit price
  5950. NSString *str_price = [self textAtColumn:3 statement:stmt];
  5951. NSString* Price=nil;
  5952. if([str_price isEqualToString:@""])
  5953. {
  5954. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  5955. if(price==nil)
  5956. Price=@"No Price.";
  5957. else
  5958. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  5959. }
  5960. else
  5961. {
  5962. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  5963. }
  5964. // discount
  5965. double discount = sqlite3_column_double(stmt, 4);
  5966. // name
  5967. NSString *name = [self textAtColumn:5 statement:stmt];
  5968. // description
  5969. NSString *description = [self textAtColumn:6 statement:stmt];
  5970. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  5971. // line note
  5972. NSString *line_note = [self textAtColumn:7 statement:stmt];
  5973. int avaulability = sqlite3_column_int(stmt, 8);
  5974. // img
  5975. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  5976. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false];
  5977. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  5978. if(combine != nil)
  5979. {
  5980. // int citem=0;
  5981. int bcount=[[combine valueForKey:@"count"] intValue];
  5982. for(int bc=0;bc<bcount;bc++)
  5983. {
  5984. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  5985. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  5986. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  5987. subTotal += uprice * modulus * item_count;
  5988. }
  5989. }
  5990. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  5991. [model_dic setValue:@"model" forKey:@"control"];
  5992. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  5993. [model_dic setValue:description forKey:@"description"];
  5994. [model_dic setValue:line_note forKey:@"note"];
  5995. [model_dic setValue:img forKey:@"img_url"];
  5996. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  5997. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  5998. [model_dic setValue:Price forKey:@"unit_price"];
  5999. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  6000. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  6001. if (combine) {
  6002. [model_dic setValue:combine forKey:@"combine"];
  6003. }
  6004. // well,what under the row is the info for total
  6005. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  6006. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  6007. double weight=[bsubtotaljson[@"weight"] doubleValue];
  6008. int carton=[bsubtotaljson[@"carton"] intValue];
  6009. TotalCuft += cuft;
  6010. TotalWeight += weight;
  6011. TotalCarton += carton;
  6012. payments += subTotal;
  6013. //---------------------------
  6014. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  6015. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  6016. }];
  6017. [dic setValue:@"Model Information" forKey:@"title"];
  6018. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  6019. return dic;
  6020. }
  6021. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  6022. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6023. [dic setValue:@"Remarks Content" forKey:@"title"];
  6024. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  6025. // setting
  6026. NSDictionary *setting = params[@"setting"];
  6027. NSNumber *hide = setting[@"RemarksContentHide"];
  6028. [dic setValue:hide forKey:@"hide"];
  6029. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6030. orderCode = [self translateSingleQuote:orderCode];
  6031. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes from offline_order where so_id = '%@';",orderCode];
  6032. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6033. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  6034. int mustCall = sqlite3_column_int(stmt, 1);
  6035. NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  6036. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  6037. NSDictionary *po_dic = @{
  6038. @"aname" : @"PO#",
  6039. @"control" : @"edit",
  6040. @"keyboard" : @"text",
  6041. @"name" : @"poNumber",
  6042. @"value" : poNumber
  6043. };
  6044. NSDictionary *must_call_dic = @{
  6045. @"aname" : @"MUST CALL BEFORE DELIVERY",
  6046. @"control" : @"switch",
  6047. @"name" : @"must_call",
  6048. @"value" : mustCall ? @"true" : @"false"
  6049. };
  6050. NSDictionary *general_notes_dic = @{
  6051. @"aname" : @"General notes",
  6052. @"control" : @"text_view",
  6053. @"keyboard" : @"text",
  6054. @"name" : @"comments",
  6055. @"value" : generalNotes
  6056. };
  6057. NSDictionary *internal_notes_dic = @{
  6058. @"aname" : @"Internal notes",
  6059. @"control" : @"text_view",
  6060. @"keyboard" : @"text",
  6061. @"name" : @"internal_notes",
  6062. @"value" : internalNotes
  6063. };
  6064. [dic setValue:po_dic forKey:@"item_0"];
  6065. [dic setValue:must_call_dic forKey:@"item_1"];
  6066. [dic setValue:general_notes_dic forKey:@"item_2"];
  6067. [dic setValue:internal_notes_dic forKey:@"item_3"];
  6068. }];
  6069. return dic;
  6070. }
  6071. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  6072. // params
  6073. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6074. orderCode = [self translateSingleQuote:orderCode];
  6075. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6076. // setting
  6077. NSDictionary *setting = params[@"setting"];
  6078. NSNumber *hide = setting[@"OrderTotalHide"];
  6079. [dic setValue:hide forKey:@"hide"];
  6080. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  6081. __block double lift_gate_value = 0;
  6082. __block double handling_fee = 0;
  6083. __block double shipping = 0;
  6084. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6085. int lift_gate = sqlite3_column_int(stmt, 0);
  6086. lift_gate_value = sqlite3_column_double(stmt, 1);
  6087. shipping = sqlite3_column_double(stmt, 2);
  6088. handling_fee = sqlite3_column_double(stmt, 3);
  6089. if (!lift_gate) {
  6090. lift_gate_value = 0;
  6091. }
  6092. }];
  6093. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  6094. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  6095. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  6096. double payments = [[total valueForKey:@"payments"] doubleValue];
  6097. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  6098. double totalPrice = payments;
  6099. [dic setValue:@"Order Total" forKey:@"title"];
  6100. NSDictionary *payments_dic = @{
  6101. @"align" : @"right",
  6102. @"aname" : @"Payments/Credits",
  6103. @"control" : @"text",
  6104. @"name" : @"paymentsAndCredits",
  6105. @"type" : @"price",
  6106. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  6107. };
  6108. [dic setValue:payments_dic forKey:@"item_0"];
  6109. NSDictionary *handling_fee_dic = @{
  6110. @"align" : @"right",
  6111. @"aname" : @"Handling Fee",
  6112. @"control" : @"text",
  6113. @"name" : @"handling_fee_value",
  6114. @"required" : @"true",
  6115. @"type" : @"price",
  6116. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  6117. };
  6118. NSDictionary *shipping_dic = @{
  6119. @"align" : @"right",
  6120. @"aname" : @"Shipping*",
  6121. @"control" : @"text",
  6122. @"name" : @"shipping",
  6123. @"required" : @"true",
  6124. @"type" : @"price",
  6125. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  6126. };
  6127. NSDictionary *lift_gate_dic = @{
  6128. @"align" : @"right",
  6129. @"aname" : @"Liftgate Fee(No Loading Dock)",
  6130. @"control" : @"text",
  6131. @"name" : @"lift_gate_value",
  6132. @"required" : @"true",
  6133. @"type" : @"price",
  6134. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  6135. };
  6136. int item_count = 1;
  6137. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  6138. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  6139. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6140. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6141. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  6142. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6143. } else {
  6144. }
  6145. [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6146. NSDictionary *total_price_dic = @{
  6147. @"align" : @"right",
  6148. @"aname" : @"Total",
  6149. @"control" : @"text",
  6150. @"name" : @"totalPrice",
  6151. @"type" : @"price",
  6152. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  6153. };
  6154. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6155. NSDictionary *total_cuft_dic = @{
  6156. @"align" : @"right",
  6157. @"aname" : @"Total Cuft",
  6158. @"control" : @"text",
  6159. @"name" : @"",
  6160. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  6161. };
  6162. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6163. NSDictionary *total_weight_dic = @{
  6164. @"align" : @"right",
  6165. @"aname" : @"Total Weight",
  6166. @"control" : @"text",
  6167. @"name" : @"",
  6168. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  6169. };
  6170. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6171. NSDictionary *total_carton_dic = @{
  6172. @"align" : @"right",
  6173. @"aname" : @"Total Carton",
  6174. @"control" : @"text",
  6175. @"name" : @"",
  6176. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  6177. };
  6178. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6179. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  6180. return dic;
  6181. }
  6182. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  6183. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6184. orderCode = [self translateSingleQuote:orderCode];
  6185. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6186. // setting
  6187. NSDictionary *setting = params[@"setting"];
  6188. NSNumber *hide = setting[@"SignatureHide"];
  6189. [dic setValue:hide forKey:@"hide"];
  6190. [dic setValue:@"Signature" forKey:@"title"];
  6191. __block NSString *pic_path = @"";
  6192. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  6193. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6194. pic_path = [self textAtColumn:0 statement:stmt];
  6195. }];
  6196. NSDictionary *pic_dic = @{
  6197. @"aname" : @"Signature",
  6198. @"avalue" :pic_path,
  6199. @"control" : @"signature",
  6200. @"name" : @"sign_picpath",
  6201. @"value" : pic_path
  6202. };
  6203. [dic setValue:pic_dic forKey:@"item_0"];
  6204. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6205. return dic;
  6206. }
  6207. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  6208. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6209. // setting
  6210. NSDictionary *setting = params[@"setting"];
  6211. NSNumber *hide = setting[@"ShippingMethodHide"];
  6212. [dic setValue:hide forKey:@"hide"];
  6213. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6214. orderCode = [self translateSingleQuote:orderCode];
  6215. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  6216. __block NSString *logist = @"";
  6217. __block NSString *lift_gate = @"";
  6218. __block int lift_gate_integer = 0;
  6219. __block NSString *logistic_note = @"";
  6220. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6221. logist = [self textAtColumn:0 statement:stmt];
  6222. lift_gate_integer = sqlite3_column_int(stmt, 1);
  6223. logistic_note = [self textAtColumn:2 statement:stmt];
  6224. }];
  6225. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6226. [dic setValue:@"Shipping Method" forKey:@"title"];
  6227. // val_0
  6228. int PERSONAL_PICK_UP_check = 0;
  6229. int USE_MY_CARRIER_check = 0;
  6230. NSString *logistic_note_text = @"";
  6231. int will_call_check = 0;
  6232. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  6233. will_call_check = 1;
  6234. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  6235. PERSONAL_PICK_UP_check = 1;
  6236. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  6237. USE_MY_CARRIER_check = 1;
  6238. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  6239. logistic_note = [logistic_note_array firstObject];
  6240. if (logistic_note_array.count > 1) {
  6241. logistic_note_text = [logistic_note_array lastObject];
  6242. }
  6243. }
  6244. }
  6245. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  6246. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  6247. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  6248. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  6249. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  6250. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6251. NSDictionary *val0_subItem_item0 = @{
  6252. @"aname" : @"Option",
  6253. @"cadedate" : @{
  6254. @"count" : [NSNumber numberWithInteger:2],
  6255. @"val_0" : @{
  6256. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  6257. @"refresh" : [NSNumber numberWithInteger:0],
  6258. @"value" : @"PERSONAL PICK UP",
  6259. @"value_id" : @"PERSONAL PICK UP"
  6260. },
  6261. @"val_1" : @{
  6262. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  6263. @"refresh" : [NSNumber numberWithInteger:0],
  6264. @"sub_item" : @{
  6265. @"count" : [NSNumber numberWithInteger:1],
  6266. @"item_0" : @{
  6267. @"aname" : @"BOL",
  6268. @"control" : @"edit",
  6269. @"keyboard" : @"text",
  6270. @"name" : @"logist_note_text",
  6271. @"refresh" : [NSNumber numberWithInteger:0],
  6272. @"required" : @"false",
  6273. @"value" : logistic_note_text
  6274. }
  6275. },
  6276. @"value" : @"USE MY CARRIER",
  6277. @"value_id" : @"USE MY CARRIER"
  6278. }
  6279. },
  6280. @"control" : @"enum",
  6281. @"name" : @"logistic_note",
  6282. @"required" : @"true",
  6283. @"single_select" : @"true"
  6284. };
  6285. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  6286. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  6287. // val_1
  6288. lift_gate = [self valueInParams:params key:@"lift_gate"];
  6289. if([lift_gate isEqualToString:@""]) {
  6290. if (lift_gate_integer == 1) {
  6291. lift_gate = @"true";
  6292. } else {
  6293. lift_gate = @"false";
  6294. }
  6295. }
  6296. int common_carrier_check = 0;
  6297. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  6298. common_carrier_check = 1;
  6299. [params setValue:lift_gate forKey:@"lift_gate"];
  6300. }
  6301. NSDictionary *val_1 = @{
  6302. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  6303. @"sub_item" : @{
  6304. @"count" : [NSNumber numberWithInteger:1],
  6305. @"item_0" : @{
  6306. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  6307. @"control" : @"switch",
  6308. @"name" : @"lift_gate",
  6309. @"refresh" : [NSNumber numberWithInteger:1],
  6310. @"required" : @"true",
  6311. @"value" : lift_gate
  6312. }
  6313. },
  6314. @"value" : @"COMMON CARRIER",
  6315. @"value_id" : @"COMMON CARRIER"
  6316. };
  6317. // cadedate
  6318. NSDictionary *cadedate = @{
  6319. @"count" : [NSNumber numberWithInteger:2],
  6320. @"val_0" : val_0,
  6321. @"val_1" : val_1
  6322. };
  6323. // item_0
  6324. NSMutableDictionary *item_0 = @{
  6325. @"aname" : @"Shipping",
  6326. @"cadedate" : cadedate,
  6327. @"control" : @"enum",
  6328. @"name" : @"logist",
  6329. @"refresh" : [NSNumber numberWithInteger:1],
  6330. @"single_select" : @"true",
  6331. }.mutableCopy;
  6332. NSNumber *required = setting[@"ShippingMethodRequire"];
  6333. if ([required integerValue]) {
  6334. [item_0 setValue:@"true" forKey:@"required"];
  6335. }
  6336. [dic setValue:item_0 forKey:@"item_0"];
  6337. return dic;
  6338. }
  6339. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  6340. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6341. orderCode = [self translateSingleQuote:orderCode];
  6342. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  6343. __block int submit_as_integer = 0;
  6344. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6345. submit_as_integer = sqlite3_column_int(stmt, 0);
  6346. }];
  6347. int check11 = 0;
  6348. int check10 = 0;
  6349. if (submit_as_integer == 11) {
  6350. check11 = 1;
  6351. }
  6352. if (submit_as_integer == 10) {
  6353. check10 = 1;
  6354. }
  6355. // section_0
  6356. NSMutableDictionary *dic = @{
  6357. @"count" : @(1),
  6358. @"item_0" : @{
  6359. @"aname" : @"Submit Order As",
  6360. @"cadedate" : @{
  6361. @"count" : @(2),
  6362. @"val_0" : @{
  6363. @"check" : [NSNumber numberWithInteger:check11],
  6364. @"value" : @"Sales Order",
  6365. @"value_id" : @(11)
  6366. },
  6367. @"val_1" : @{
  6368. @"check" : [NSNumber numberWithInteger:check10],
  6369. @"value" : @"Quote",
  6370. @"value_id" : @(10)
  6371. }
  6372. },
  6373. @"control" : @"enum",
  6374. @"name" : @"erpOrderStatus",
  6375. @"required" : @"true",
  6376. @"single_select" : @"true"
  6377. },
  6378. @"title" : @"Order Type"
  6379. }.mutableCopy;
  6380. // setting
  6381. NSDictionary *setting = params[@"setting"];
  6382. NSNumber *hide = setting[@"OrderTypeHide"];
  6383. [dic setValue:hide forKey:@"hide"];
  6384. return dic;
  6385. }
  6386. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  6387. countryCode = [self translateSingleQuote:countryCode];
  6388. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  6389. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6390. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  6391. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  6392. if (name == NULL) {
  6393. name = "";
  6394. }
  6395. if (code == NULL) {
  6396. code = "";
  6397. }
  6398. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  6399. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  6400. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  6401. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  6402. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  6403. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  6404. }
  6405. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  6406. [container setValue:stateDic forKey:key];
  6407. }] mutableCopy];
  6408. // failure 可以不用了,一样的
  6409. if (ret.allKeys.count == 0) {
  6410. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  6411. [stateDic setValue:@"Other" forKey:@"value"];
  6412. [stateDic setValue:@"" forKey:@"value_id"];
  6413. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  6414. if (state_code && [@"" isEqualToString:state_code]) {
  6415. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  6416. }
  6417. NSString *key = [NSString stringWithFormat:@"val_0"];
  6418. [ret setValue:stateDic forKey:key];
  6419. }
  6420. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  6421. return ret;
  6422. }
  6423. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  6424. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6425. orderCode = [self translateSingleQuote:orderCode];
  6426. 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];
  6427. __block NSString *payType = @"";
  6428. __block NSString *firstName = @"";
  6429. __block NSString *lastName = @"";
  6430. __block NSString *addr1 = @"";
  6431. __block NSString *addr2 = @"";
  6432. __block NSString *zipcode = @"";
  6433. __block NSString *cardType = @"";
  6434. __block NSString *cardNumber = @"";
  6435. __block NSString *securityCode = @"";
  6436. __block NSString *month = @"";
  6437. __block NSString *year = @"";
  6438. __block NSString *city = @"";
  6439. __block NSString *state = @"";
  6440. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6441. payType = [self textAtColumn:0 statement:stmt];
  6442. firstName = [self textAtColumn:1 statement:stmt];
  6443. lastName = [self textAtColumn:2 statement:stmt];
  6444. addr1 = [self textAtColumn:3 statement:stmt];
  6445. addr2 = [self textAtColumn:4 statement:stmt];
  6446. zipcode = [self textAtColumn:5 statement:stmt];
  6447. cardType = [self textAtColumn:6 statement:stmt];
  6448. cardNumber = [self textAtColumn:7 statement:stmt];
  6449. securityCode = [self textAtColumn:8 statement:stmt];
  6450. month = [self textAtColumn:9 statement:stmt];
  6451. year = [self textAtColumn:10 statement:stmt];
  6452. city = [self textAtColumn:11 statement:stmt];
  6453. state = [self textAtColumn:12 statement:stmt];
  6454. }];
  6455. NSString *required = @"true";
  6456. // setting
  6457. NSDictionary *setting = params[@"setting"];
  6458. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  6459. if ([requiredNumber integerValue]) {
  6460. required = @"true";
  6461. } else {
  6462. required = @"false";
  6463. }
  6464. // "section_2"
  6465. NSMutableDictionary *dic = @{
  6466. @"count" : @(1),
  6467. @"item_0" : @{
  6468. @"aname" : @"Payment",
  6469. @"required" : required,
  6470. @"cadedate" : @{
  6471. @"count" : @(6),
  6472. @"val_0" : @{
  6473. @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  6474. @"value" : @"Check",
  6475. @"value_id" : @"Check"
  6476. },
  6477. @"val_1" : @{
  6478. @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  6479. @"value" : @"Cash",
  6480. @"value_id" : @"Cash"
  6481. },
  6482. @"val_2" : @{
  6483. @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  6484. @"value" : @"NET 30",
  6485. @"value_id" : @"NET 30"
  6486. },
  6487. @"val_3" : @{
  6488. @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  6489. @"value" : @"Wire Transfer",
  6490. @"value_id" : @"Wire Transfer"
  6491. },
  6492. @"val_4" : @{
  6493. @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  6494. @"sub_item" : @{
  6495. @"count" : @(3),
  6496. @"item_0" : @{
  6497. @"aname" : @"choose",
  6498. @"control" : @"multi_action",
  6499. @"count" : @(1),
  6500. @"item_0" : @{
  6501. @"aname" : @"Same as customer",
  6502. @"key_map" : @{
  6503. @"credit_card_address1" : @"customer_address1",
  6504. @"credit_card_address2" : @"customer_address2",
  6505. @"credit_card_city" : @"customer_city",
  6506. @"credit_card_first_name" : @"customer_first_name",
  6507. @"credit_card_last_name" : @"customer_last_name",
  6508. @"credit_card_state" : @"customer_state",
  6509. @"credit_card_zipcode" : @"customer_zipcode"
  6510. },
  6511. @"type" : @"pull"
  6512. }
  6513. },
  6514. @"item_1" : @{
  6515. @"aname" : @"",
  6516. @"color" : @"red",
  6517. @"control" : @"text",
  6518. @"name" : @"",
  6519. @"value" : @"USA Credit cards only"
  6520. },
  6521. @"item_2" : @{
  6522. @"aname" : @"Fill",
  6523. @"cadedate" : @{
  6524. @"count" : @(2),
  6525. @"val_0" : @{
  6526. @"check" : @(1),
  6527. @"sub_item" : @{
  6528. @"count" : @(11),
  6529. @"item_0" : @{
  6530. @"aname" : @"Type",
  6531. @"cadedate" : @{
  6532. @"count" : @(2),
  6533. @"val_0" : @{
  6534. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  6535. @"value" : @"VISA",
  6536. @"value_id" : @(0)
  6537. },
  6538. @"val_1" : @{
  6539. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  6540. @"value" : @"MASTER CARD",
  6541. @"value_id" : @(1)
  6542. }
  6543. },
  6544. @"control" : @"enum",
  6545. @"name" : @"credit_card_type",
  6546. @"required" : @"true",
  6547. @"single_select" : @"true"
  6548. },
  6549. @"item_1" : @{
  6550. @"aname" : @"Number",
  6551. @"control" : @"edit",
  6552. @"keyboard" : @"int",
  6553. @"length" : @"16",
  6554. @"name" : @"credit_card_number",
  6555. @"required" : @"true",
  6556. @"value" : cardNumber
  6557. },
  6558. @"item_10" : @{
  6559. @"aname" : @"State",
  6560. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  6561. @"control" : @"enum",
  6562. @"enum" : @"true",
  6563. @"name" : @"credit_card_state",
  6564. @"required" : @"true",
  6565. @"single_select" : @"true"
  6566. },
  6567. @"item_2" : @{
  6568. @"aname" : @"Expiration Date",
  6569. @"control" : @"monthpicker",
  6570. @"name" : @"credit_card_expiration",
  6571. @"required" : @"true",
  6572. @"type" : @"date",
  6573. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  6574. },
  6575. @"item_3" : @{
  6576. @"aname" : @"Security Code",
  6577. @"control" : @"edit",
  6578. @"keyboard" : @"int",
  6579. @"length" : @"3",
  6580. @"name" : @"credit_card_security_code",
  6581. @"required" : @"true",
  6582. @"value" : securityCode
  6583. },
  6584. @"item_4" : @{
  6585. @"aname" : @"First Name",
  6586. @"control" : @"edit",
  6587. @"keyboard" : @"text",
  6588. @"name" : @"credit_card_first_name",
  6589. @"required" : @"true",
  6590. @"value" : firstName
  6591. },
  6592. @"item_5" : @{
  6593. @"aname" : @"Last Name",
  6594. @"control" : @"edit",
  6595. @"keyboard" : @"text",
  6596. @"name" : @"credit_card_last_name",
  6597. @"required" : @"true",
  6598. @"value" : lastName
  6599. },
  6600. @"item_6" : @{
  6601. @"aname" : @"Address 1",
  6602. @"control" : @"edit",
  6603. @"keyboard" : @"text",
  6604. @"name" : @"credit_card_address1",
  6605. @"required" : @"true",
  6606. @"value" : addr1
  6607. },
  6608. @"item_7" : @{
  6609. @"aname" : @"Address 2",
  6610. @"control" : @"edit",
  6611. @"keyboard" : @"text",
  6612. @"name" : @"credit_card_address2",
  6613. @"value" : addr2
  6614. },
  6615. @"item_8" : @{
  6616. @"aname" : @"zip code",
  6617. @"control" : @"edit",
  6618. @"keyboard" : @"text",
  6619. @"name" : @"credit_card_zipcode",
  6620. @"required" : @"true",
  6621. @"value" : zipcode
  6622. },
  6623. @"item_9" : @{
  6624. @"aname" : @"City",
  6625. @"control" : @"edit",
  6626. @"keyboard" : @"text",
  6627. @"name" : @"credit_card_city",
  6628. @"required" : @"true",
  6629. @"value" : city
  6630. }
  6631. },
  6632. @"value" : @"Fill Now",
  6633. @"value_id" : @""
  6634. },
  6635. @"val_1" : @{
  6636. @"check" : @(0),
  6637. @"value" : @"Fill Later",
  6638. @"value_id" : @""
  6639. }
  6640. },
  6641. @"control" : @"enum",
  6642. @"name" : @"",
  6643. @"single_select" : @"true"
  6644. }
  6645. },
  6646. @"value" : @"Credit Card",
  6647. @"value_id" : @"Credit Card"
  6648. },
  6649. @"val_5" : @{
  6650. @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  6651. @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  6652. @"value_id" : @"FOLLOW EXISTING"
  6653. }
  6654. },
  6655. @"control" : @"enum",
  6656. @"name" : @"paymentType",
  6657. @"single_select" : @"true"
  6658. },
  6659. @"title" : @"Payment Information"
  6660. }.mutableCopy;
  6661. NSNumber *hide = setting[@"PaymentInformationHide"];
  6662. [dic setValue:hide forKey:@"hide"];
  6663. return dic;
  6664. }
  6665. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  6666. {
  6667. sqlite3 *db = [iSalesDB get_db];
  6668. // UISetting
  6669. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  6670. NSString *cachefolder = [paths objectAtIndex:0];
  6671. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  6672. NSData* json =nil;
  6673. json=[NSData dataWithContentsOfFile:img_cache];
  6674. NSError *error=nil;
  6675. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  6676. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  6677. [params setObject:setting forKey:@"setting"];
  6678. int section_count = 0;
  6679. // params
  6680. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6681. orderCode = [self translateSingleQuote:orderCode];
  6682. // 0 Order Type 1 Shipping Method 2 Payment Information
  6683. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  6684. // 0 Order Type
  6685. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  6686. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  6687. [ret setValue:order_type_dic forKey:key0];
  6688. // 1 Shipping Method
  6689. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  6690. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  6691. [ret setValue:shipping_method_dic forKey:key1];
  6692. // 2 Payment Information
  6693. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  6694. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  6695. [ret setValue:payment_info_dic forKey:key2];
  6696. // 3 Customer
  6697. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  6698. NSDictionary *customer_dic = [self customerDic:params db:db];
  6699. [ret setValue:customer_dic forKey:key3];
  6700. // 4 Ship To
  6701. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  6702. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  6703. [ret setValue:ship_to_dic forKey:key4];
  6704. // 5 Ship From
  6705. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  6706. if (![shipFromDisable integerValue]) {
  6707. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  6708. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  6709. [ret setValue:ship_from_dic forKey:key5];
  6710. }
  6711. // 6 Freight Bill To
  6712. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  6713. if (![freightBillToDisable integerValue]) {
  6714. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  6715. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  6716. [ret setValue:freight_bill_to forKey:key6];
  6717. }
  6718. // 7 Merchandise Bill To
  6719. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  6720. if (![merchandiseBillToDisable integerValue]) {
  6721. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  6722. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  6723. [ret setValue:merchandise_bill_to_dic forKey:key7];
  6724. }
  6725. // 8 Return To
  6726. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  6727. if (![returnToDisable integerValue]) {
  6728. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  6729. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  6730. [ret setValue:return_to_dic forKey:key8];
  6731. }
  6732. // 9 Model Information
  6733. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  6734. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  6735. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  6736. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  6737. [ret setValue:model_info_dic forKey:key9];
  6738. // 10 Remarks Content
  6739. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  6740. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  6741. [ret setValue:remarks_content_dic forKey:key10];
  6742. // 11 Order Total
  6743. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  6744. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  6745. [ret setValue:order_total_dic forKey:key11];
  6746. // 12 Signature
  6747. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  6748. NSDictionary *sign_dic = [self signatureDic:params db:db];
  6749. [ret setValue:sign_dic forKey:key12];
  6750. [ret setValue:@(section_count) forKey:@"section_count"];
  6751. [iSalesDB close_db:db];
  6752. return [RAUtils dict2data:ret];
  6753. // return nil;
  6754. }
  6755. #pragma mark addr editor
  6756. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  6757. NSMutableDictionary *new_item = [item mutableCopy];
  6758. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  6759. return new_item;
  6760. }
  6761. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  6762. {
  6763. // "is_subaction" = true;
  6764. // orderCode = MOB1608240002;
  6765. // password = 123456;
  6766. // "subaction_tag" = 1;
  6767. // user = EvanK;
  6768. // {
  6769. // "is_subaction" = true;
  6770. // orderCode = MOB1608240002;
  6771. // password = 123456;
  6772. // "refresh_trigger" = zipcode;
  6773. // "subaction_tag" = 1;
  6774. // user = EvanK;
  6775. // }
  6776. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  6777. [ret removeObjectForKey:@"up_params"];
  6778. [ret setObject:@"New Address" forKey:@"title"];
  6779. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  6780. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  6781. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  6782. NSMutableDictionary *country_dic = [section_0[@"item_5"] mutableCopy];
  6783. [country_dic removeObjectForKey:@"refresh"];
  6784. [country_dic removeObjectForKey:@"restore"];
  6785. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  6786. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  6787. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_6"] add:YES] forKey:@"item_2"]; // Address 1*
  6788. [new_section_0 setObject:section_0[@"item_7"] forKey:@"item_3"]; // Address 2
  6789. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_4"]; // Address 3
  6790. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_5"]; // Address 4
  6791. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_10"] add:YES];
  6792. [zip_code_dic removeObjectForKey:@"refresh"];
  6793. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  6794. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_11"] add:YES] forKey:@"item_7"]; // State/Province*
  6795. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_8"]; // City*
  6796. [new_section_0 setObject:section_0[@"item_13"] forKey:@"item_9"]; // Contact First Name
  6797. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_10"]; // Contact Last Name
  6798. [new_section_0 setObject:section_0[@"item_4"] forKey:@"item_11"]; // Phone*
  6799. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_12"]; // Fax
  6800. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_3"] add:NO] forKey:@"item_13"]; // Email
  6801. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  6802. // country
  6803. NSString *countryCode = @"US";
  6804. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  6805. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  6806. // state
  6807. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:nil] mutableCopy];
  6808. NSDictionary *tmpDic = @{
  6809. @"value" : @"Other",
  6810. @"value_id" : @"",
  6811. @"check" : [NSNumber numberWithInteger:0]
  6812. };
  6813. for (int i = 0; i < allState.allKeys.count; i++) {
  6814. NSString *key = [NSString stringWithFormat:@"val_%d",i];
  6815. NSDictionary *tmp = allState[key];
  6816. [allState setValue:tmpDic forKey:key];
  6817. tmpDic = tmp;
  6818. }
  6819. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  6820. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  6821. [ret setValue:new_section_0 forKey:@"section_0"];
  6822. return [RAUtils dict2data:ret];
  6823. }
  6824. #pragma mark save addr
  6825. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  6826. {
  6827. // NSString *companyName = [self valueInParams:params key:@"company"];
  6828. // NSString *addr1 = [self valueInParams:params key:@"address"];
  6829. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  6830. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  6831. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  6832. // NSString *countryId = [self valueInParams:params key:@"country"];
  6833. // NSString *stateCode = [self valueInParams:params key:@"state"];
  6834. // NSString *city = [self valueInParams:params key:@"city"];
  6835. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  6836. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  6837. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  6838. // NSString *phone = [self valueInParams:params key:@"phone"];
  6839. // NSString *fax = [self valueInParams:params key:@"fax"];
  6840. // NSString *email = [self valueInParams:params key:@"email"];
  6841. return [self offline_saveContact:params update:NO isCustomer:NO];
  6842. }
  6843. #pragma mark cancel order
  6844. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  6845. {
  6846. // NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6847. NSString *order_id = [self valueInParams:params key:@"orderId"];
  6848. // NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  6849. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  6850. int ret = [iSalesDB execSql:sql];
  6851. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6852. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6853. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6854. // [dic setValue:@"160409" forKey:@"min_ver"];
  6855. return [RAUtils dict2data:dic];
  6856. }
  6857. #pragma mark sign order
  6858. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  6859. {
  6860. //参考 offline_saveBusinesscard
  6861. NSLog(@"sign order params: %@",params);
  6862. // orderCode = MOB1608240002;
  6863. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  6864. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6865. orderCode = [self translateSingleQuote:orderCode];
  6866. NSString *picPath = [self valueInParams:params key:@"picpath"];
  6867. picPath = [self translateSingleQuote:picPath];
  6868. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  6869. int ret = [iSalesDB execSql:sql];
  6870. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6871. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6872. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6873. // [dic setValue:@"160409" forKey:@"min_ver"];
  6874. return [RAUtils dict2data:dic];
  6875. }
  6876. #pragma mark save order
  6877. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  6878. NSString *ret = [self valueInParams:params key:key];
  6879. if (translate) {
  6880. ret = [self translateSingleQuote:ret];
  6881. }
  6882. return ret;
  6883. }
  6884. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  6885. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  6886. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  6887. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  6888. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  6889. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  6890. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  6891. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  6892. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  6893. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  6894. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  6895. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  6896. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  6897. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  6898. if (![total_price isEqualToString:@""]) {
  6899. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  6900. } else {
  6901. total_price = @"";
  6902. }
  6903. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  6904. if ([paymentsAndCredits isEqualToString:@""]) {
  6905. paymentsAndCredits = @"";
  6906. } else {
  6907. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  6908. }
  6909. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  6910. if ([handling_fee_value isEqualToString:@""]) {
  6911. handling_fee_value = @"";
  6912. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  6913. handling_fee_value = @"";
  6914. } else {
  6915. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  6916. }
  6917. NSString *handling_fee_placeholder = handling_fee_value;
  6918. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  6919. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  6920. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  6921. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  6922. NSString *lift_gate_placeholder = @"";
  6923. if ([lift_gate_value isEqualToString:@""]) {
  6924. lift_gate_placeholder = @"";
  6925. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  6926. lift_gate_placeholder = @"";
  6927. } else {
  6928. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  6929. }
  6930. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  6931. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  6932. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  6933. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  6934. 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];
  6935. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  6936. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  6937. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  6938. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  6939. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  6940. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  6941. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  6942. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  6943. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  6944. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  6945. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  6946. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  6947. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  6948. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  6949. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  6950. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  6951. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  6952. BOOL number_nil = [credit_card_number isEqualToString:@""];
  6953. if (!number_nil) {
  6954. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  6955. }
  6956. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  6957. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  6958. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  6959. if (!security_code_nil) {
  6960. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  6961. }
  6962. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  6963. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  6964. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  6965. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  6966. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  6967. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  6968. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  6969. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  6970. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  6971. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  6972. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  6973. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  6974. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  6975. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  6976. NSString *contact_id = customer_cid;
  6977. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  6978. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  6979. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  6980. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  6981. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  6982. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  6983. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  6984. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  6985. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  6986. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  6987. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  6988. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  6989. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  6990. //
  6991. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  6992. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  6993. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  6994. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  6995. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  6996. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  6997. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  6998. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  6999. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  7000. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  7001. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  7002. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  7003. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  7004. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  7005. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  7006. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  7007. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  7008. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  7009. 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];
  7010. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  7011. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  7012. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  7013. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  7014. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  7015. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  7016. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  7017. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  7018. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  7019. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  7020. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  7021. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  7022. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  7023. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  7024. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  7025. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  7026. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  7027. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  7028. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  7029. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  7030. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  7031. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  7032. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  7033. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  7034. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  7035. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  7036. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  7037. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  7038. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  7039. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  7040. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  7041. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  7042. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  7043. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  7044. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  7045. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  7046. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  7047. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  7048. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  7049. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  7050. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  7051. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  7052. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  7053. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  7054. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  7055. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  7056. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  7057. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  7058. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  7059. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  7060. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  7061. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  7062. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  7063. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  7064. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  7065. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  7066. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  7067. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  7068. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  7069. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  7070. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  7071. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  7072. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  7073. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  7074. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  7075. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  7076. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  7077. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  7078. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  7079. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  7080. NSString *order_status = @"status = 1,";
  7081. if (submit) {
  7082. order_status = @"status = -11,";
  7083. }
  7084. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  7085. NSString *sync_sql = @"";
  7086. if (submit) {
  7087. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  7088. [param setValue:sales_rep forKey:@"sales_rep"];
  7089. NSString *sync_data = [self translateSingleQuote:[RAUtils dict2string:param]];
  7090. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  7091. }
  7092. 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,so_id,sync_sql,so_id];
  7093. DebugLog(@"save order contactSql: %@",contactSql);
  7094. DebugLog(@"save order orderSql: %@",orderSql);
  7095. // int contact_ret = [iSalesDB execSql:contactSql];
  7096. int order_ret = [iSalesDB execSql:orderSql];
  7097. int ret = order_ret;
  7098. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7099. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7100. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7101. // [dic setValue:@"160409" forKey:@"min_ver"];
  7102. [dic setObject:so_id forKey:@"so#"];
  7103. return [RAUtils dict2data:dic];
  7104. }
  7105. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  7106. {
  7107. // id foo = nil;
  7108. // NSMutableArray *a = @[].mutableCopy;
  7109. // [a addObject:foo];
  7110. return [self saveorder:param submit:NO];
  7111. }
  7112. #pragma mark add to cart by name
  7113. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  7114. {
  7115. NSString *orderCode = [params objectForKey:@"orderCode"];
  7116. NSString *product_name = [params objectForKey:@"product_name"];
  7117. product_name = [self translateSingleQuote:product_name];
  7118. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  7119. sqlite3 *db = [iSalesDB get_db];
  7120. __block NSMutableString *product_id_string = [NSMutableString string];
  7121. for (int i = 0; i < product_name_array.count; i++) {
  7122. NSString *name = [product_name_array objectAtIndex:i];
  7123. NSString *sql = [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  7124. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7125. int product_id = sqlite3_column_int(stmt, 0);
  7126. if (i == product_name_array.count - 1) {
  7127. [product_id_string appendFormat:@"%d",product_id];
  7128. } else {
  7129. [product_id_string appendFormat:@"%d,",product_id];
  7130. }
  7131. }];
  7132. }
  7133. NSDictionary *newParams = @{
  7134. @"product_id" : product_id_string,
  7135. @"orderCode" : orderCode
  7136. };
  7137. [iSalesDB close_db:db];
  7138. return [self offline_add2cart:[newParams mutableCopy]];
  7139. }
  7140. #pragma mark reset order
  7141. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  7142. {
  7143. UIApplication * app = [UIApplication sharedApplication];
  7144. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  7145. appDelegate.disable_trigger=true;
  7146. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  7147. appDelegate.disable_trigger=false;
  7148. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7149. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  7150. return [RAUtils dict2data:dic];
  7151. }
  7152. #pragma mark submit order
  7153. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  7154. {
  7155. return [self saveorder:params submit:YES];
  7156. }
  7157. #pragma mark copy order
  7158. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  7159. {
  7160. NSMutableDictionary *ret = @{}.mutableCopy;
  7161. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  7162. sqlite3 *db = [iSalesDB get_db];
  7163. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  7164. // 首先查看联系人是否active
  7165. 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];
  7166. __block int customer_is_active = 1;
  7167. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7168. customer_is_active = sqlite3_column_int(stmt, 0);
  7169. }];
  7170. if (!customer_is_active) {
  7171. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7172. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7173. [iSalesDB close_db:db];
  7174. return [RAUtils dict2data:ret];
  7175. }
  7176. // new order
  7177. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  7178. NSString *new_order_code = [self get_offline_soid:db];
  7179. NSString *user = ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  7180. user = [self translateSingleQuote:user];
  7181. NSString *insert_order_sql = [NSString stringWithFormat:@"insert into offline_order (so_id,status,submit_as,internal_notes,sales_rep,create_by,total_price,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) select '%@',1,submit_as,internal_notes,sales_rep,'%@',total_price,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 from offline_order where so_id = '%@';",new_order_code,user,order_code]; // ,paymentsAndCredits,shipping,lift_gate_value,handling_fee_value
  7182. __block int result = 1;
  7183. result = [iSalesDB execSql:insert_order_sql db:db];
  7184. if (!result) {
  7185. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7186. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7187. [iSalesDB close_db:db];
  7188. return [RAUtils dict2data:ret];
  7189. }
  7190. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  7191. // __block NSString *product_id = @"";
  7192. __weak typeof(self) weakSelf = self;
  7193. 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];
  7194. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7195. int is_active = sqlite3_column_int(stmt, 1);
  7196. if (is_active) {
  7197. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  7198. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  7199. 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];
  7200. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  7201. }
  7202. }];
  7203. // product_id = [product_id substringFromIndex:1];
  7204. //
  7205. // [self offline_add2cart:@{}.mutableCopy];
  7206. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7207. [iSalesDB close_db:db];
  7208. if (result) {
  7209. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  7210. } else {
  7211. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7212. }
  7213. [ret setObject:new_order_code forKey:@"so_id"];
  7214. return [RAUtils dict2data:ret];
  7215. }
  7216. #pragma mark move wish list to cart
  7217. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  7218. NSString *collectId = params[@"collectId"];
  7219. NSString *sql = [NSString stringWithFormat:@"select product_id,qty from wishlist where _id in (%@);",collectId];
  7220. __block NSString *product_id = @"";
  7221. __block NSString *qty = @"";
  7222. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7223. int productId = sqlite3_column_int(stmt, 0);
  7224. int item_qty = sqlite3_column_int(stmt, 1);
  7225. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  7226. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  7227. }];
  7228. product_id = [product_id substringFromIndex:1];
  7229. qty = [qty substringFromIndex:1];
  7230. NSDictionary *newParams = @{
  7231. @"product_id" : product_id,
  7232. @"orderCode" : params[@"orderCode"],
  7233. @"qty" : qty
  7234. };
  7235. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  7236. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  7237. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  7238. // NSString *deleteSQL = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  7239. //
  7240. // int ret = [iSalesDB execSql:deleteSQL];
  7241. int ret = [[[self offline_deletewishlist:@{@"collectId" : collectId}.mutableCopy] valueForKey:@"result"] integerValue];
  7242. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  7243. }
  7244. return [RAUtils dict2data:retDic];
  7245. }
  7246. #pragma mark - portfolio
  7247. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  7248. int sort = [[params valueForKey:@"sort"] integerValue];
  7249. int offset = [[params valueForKey:@"offset"] integerValue];
  7250. int limit = [[params valueForKey:@"limit"] integerValue];
  7251. NSString *orderBy = @"";
  7252. switch (sort) {
  7253. case 0:{
  7254. orderBy = @"modify_time desc";
  7255. }
  7256. break;
  7257. case 1:{
  7258. orderBy = @"modify_time asc";
  7259. }
  7260. break;
  7261. case 2:{
  7262. orderBy = @"name asc";
  7263. }
  7264. break;
  7265. case 3:{
  7266. orderBy = @"name desc";
  7267. }
  7268. break;
  7269. case 4:{
  7270. orderBy = @"description asc";
  7271. }
  7272. break;
  7273. default:
  7274. break;
  7275. }
  7276. 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];
  7277. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7278. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7279. sqlite3 *db = [iSalesDB get_db];
  7280. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7281. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  7282. int product_id = sqlite3_column_int(stmt, 0);
  7283. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  7284. NSString *name = [self textAtColumn:1 statement:stmt];
  7285. NSString *description = [self textAtColumn:2 statement:stmt];
  7286. double price = sqlite3_column_double(stmt, 3);
  7287. double discount = sqlite3_column_double(stmt,4);
  7288. int qty = sqlite3_column_int(stmt, 5);
  7289. int percentage = sqlite3_column_int(stmt, 6);
  7290. int item_id = sqlite3_column_int(stmt, 7);
  7291. // int fashion_id = sqlite3_column_int(stmt, 8);
  7292. NSString *img_path = [self textAtColumn:9 statement:stmt];
  7293. NSString *line_note = [self textAtColumn:10 statement:stmt];
  7294. double percent = sqlite3_column_double(stmt, 11);
  7295. NSString *price_null = [self textAtColumn:3 statement:stmt];
  7296. if ([price_null isEqualToString:@"null"]) {
  7297. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  7298. }
  7299. NSMutableDictionary *item = @{
  7300. @"linenotes": line_note,
  7301. @"check": @(1),
  7302. @"product_id": product_id_string,
  7303. @"available_qty": @(qty),
  7304. @"available_percent" : @(percent),
  7305. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  7306. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  7307. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  7308. @"img": img_path,
  7309. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  7310. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  7311. }.mutableCopy;
  7312. if (percentage) {
  7313. [item removeObjectForKey:@"available_qty"];
  7314. } else {
  7315. [item removeObjectForKey:@"available_percent"];
  7316. }
  7317. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  7318. if ([qty_null isEqualToString:@"null"]) {
  7319. [item removeObjectForKey:@"available_qty"];
  7320. }
  7321. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  7322. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  7323. }];
  7324. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  7325. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7326. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  7327. }
  7328. [iSalesDB close_db:db];
  7329. [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  7330. [dic setValue:@"" forKey:@"email_content"];
  7331. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  7332. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7333. return [RAUtils dict2data:dic];
  7334. }
  7335. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct {
  7336. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  7337. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  7338. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  7339. tear_name = [self translateSingleQuote:tear_name];
  7340. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  7341. tear_note = [self translateSingleQuote:tear_note];
  7342. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  7343. NSString *pdf_path = @"";
  7344. if (direct) {
  7345. configureParams = [self valueInParams:params key:@"pdfUrl"];
  7346. } else {
  7347. pdf_path = [self valueInParams:params key:@"pdfPath"];
  7348. }
  7349. pdf_path = [self translateSingleQuote:pdf_path];
  7350. NSString *create_user = [self valueInParams:params key:@"user"];
  7351. create_user = [self translateSingleQuote:create_user];
  7352. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  7353. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  7354. // model info
  7355. NSString *sql = [NSString stringWithFormat:@"select product_id,ifnull(sheet_price,'null'),sheet_discount,ifnull(available_qty,'null'),percentage,item_id,line_note,percent from offline_portfolio where product_id in (%@);",product_ids];
  7356. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7357. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7358. sqlite3 *db = [iSalesDB get_db];
  7359. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7360. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  7361. int product_id = sqlite3_column_int(stmt, 0);
  7362. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  7363. double price = sqlite3_column_double(stmt, 1);
  7364. double discount = sqlite3_column_double(stmt,2);
  7365. int qty = sqlite3_column_int(stmt, 3);
  7366. int percentage = sqlite3_column_int(stmt, 4);
  7367. int item_id = sqlite3_column_int(stmt, 5);
  7368. NSString *line_note = [self textAtColumn:6 statement:stmt];
  7369. double percent = sqlite3_column_double(stmt, 7);
  7370. NSString *price_null = [self textAtColumn:3 statement:stmt];
  7371. /* if ([price_null isEqualToString:@"null"]) {
  7372. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  7373. }
  7374. */
  7375. int regular_price = [[params objectForKey:@"regular_price"] integerValue];
  7376. NSString *regular_price_str = [self get_portfolio_price:appDelegate.contact_id item_id:item_id price:regular_price db:db];
  7377. NSMutableDictionary *item = @{
  7378. @"linenotes": line_note,
  7379. @"product_id": product_id_string,
  7380. @"available_qty": @(qty),
  7381. @"available_percent" : @(percent),
  7382. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  7383. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  7384. @"regular_price" : regular_price_str
  7385. }.mutableCopy;
  7386. if ([price_null isEqualToString:@"null"]) {
  7387. } else {
  7388. [item setObject:[NSString stringWithFormat:@"%.2f",price] forKey:@"special_price"];
  7389. }
  7390. if (percentage) {
  7391. [item removeObjectForKey:@"available_qty"];
  7392. } else {
  7393. [item removeObjectForKey:@"available_percent"];
  7394. }
  7395. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  7396. if ([qty_null isEqualToString:@"null"]) {
  7397. [item removeObjectForKey:@"available_qty"];
  7398. }
  7399. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  7400. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  7401. }];
  7402. [iSalesDB close_db:db];
  7403. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  7404. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  7405. return [RAUtils dict2data:resultDictionary];
  7406. }
  7407. NSString *model_info = [RAUtils dict2string:dic];
  7408. model_info = [self translateSingleQuote:model_info];
  7409. // 创建PDF
  7410. // 在preview情况下保存,则不需要新建了
  7411. if (direct) {
  7412. NSData *pdfData = [self offline_request_tearsheet:params]; // 新建PDF,获取本地路径
  7413. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  7414. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  7415. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  7416. resultDictionary = pdfInfo.mutableCopy;
  7417. } else { // 创建PDF失败
  7418. return pdfData;
  7419. }
  7420. } else {
  7421. // pdf_path 就是本地路径
  7422. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  7423. }
  7424. // 将文件移动到PDF Cache文件夹
  7425. NSString *newPath = [pdf_path lastPathComponent];
  7426. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  7427. NSString *cachefolder = [paths objectAtIndex:0];
  7428. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  7429. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  7430. NSFileManager *fileManager = [NSFileManager defaultManager];
  7431. NSError *error = nil;
  7432. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  7433. if (error) { // 移动文件失败
  7434. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  7435. return [RAUtils dict2data:resultDictionary];
  7436. }
  7437. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  7438. pdf_path = [newPath lastPathComponent];
  7439. // 保存信息
  7440. NSString *off_params = [RAUtils dict2string:params];
  7441. off_params = [self translateSingleQuote:off_params];
  7442. NSString *save_pdf_sql = [NSString stringWithFormat:@"insert into offline_pdf (tearsheets_id,pdf_path,create_user,tear_note,tear_name,model_info,configureParams,is_local,off_params) values ((select ifnull(max(tearsheets_id),0) from offline_pdf) + 1,'%@','%@','%@','%@','%@','%@',1,'%@');",pdf_path,create_user,tear_note,tear_name,model_info,configureParams,off_params];
  7443. int result = [iSalesDB execSql:save_pdf_sql];
  7444. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  7445. //
  7446. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  7447. if (remove_Item) {
  7448. // portfolioId
  7449. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  7450. }
  7451. return [RAUtils dict2data:resultDictionary];
  7452. }
  7453. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  7454. return [self offline_savePDF:params direct:YES];
  7455. }
  7456. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  7457. return [self offline_savePDF:params direct:NO];
  7458. }
  7459. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  7460. int offset = [[params valueForKey:@"offset"] integerValue];
  7461. int limit = [[params valueForKey:@"limit"] integerValue];
  7462. NSString *keyword = [params valueForKey:@"keyWord"];
  7463. NSString *where = @"where is_delete is null or is_delete = 0";
  7464. if (keyword.length) {
  7465. keyword = [self translateSingleQuote:keyword];
  7466. 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];
  7467. }
  7468. NSString *sql = [NSString stringWithFormat:@"select tear_name,tear_note,createtime,create_user,pdf_path,tearsheets_id from offline_pdf %@ order by createtime desc limit %d offset %d;",where,limit,offset];
  7469. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7470. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  7471. NSString *cachefolder = [paths objectAtIndex:0];
  7472. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  7473. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7474. NSString *name = [self textAtColumn:0 statement:stmt];
  7475. NSString *note = [self textAtColumn:1 statement:stmt];
  7476. NSString *time = [self textAtColumn:2 statement:stmt];
  7477. NSString *user = [self textAtColumn:3 statement:stmt];
  7478. NSString *path = [self textAtColumn:4 statement:stmt];
  7479. path = [pdfFolder stringByAppendingPathComponent:path];
  7480. time = [self changeDateTimeFormate:time];
  7481. time = [time stringByAppendingString:@" PST"];
  7482. int sheet_id = sqlite3_column_int(stmt, 5);
  7483. NSMutableDictionary *item = @{
  7484. @"tearsheetsId": @(sheet_id),
  7485. @"pdf_path": path,
  7486. @"create_time": time,
  7487. @"create_user": user,
  7488. @"tear_note": note,
  7489. @"tear_name": name,
  7490. @"isLocalFile":@"true"
  7491. }.mutableCopy;
  7492. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  7493. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  7494. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  7495. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7496. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  7497. }];
  7498. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7499. return [RAUtils dict2data:dic];
  7500. }
  7501. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  7502. NSString *prodct_ids = [params objectForKey:@"product_id"];
  7503. // NSString *user = [params objectForKey:@"user"];
  7504. 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];
  7505. int result = [iSalesDB execSql:sql];
  7506. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7507. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  7508. if (result == RESULT_TRUE) {
  7509. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  7510. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  7511. sqlite3 *db = [iSalesDB get_db];
  7512. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  7513. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  7514. [iSalesDB close_db:db];
  7515. }
  7516. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7517. return [RAUtils dict2data:dic];
  7518. }
  7519. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  7520. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  7521. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  7522. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  7523. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  7524. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  7525. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  7526. // {
  7527. // "count": 6,
  7528. // "val_1": {
  7529. // "value": "West Wholesale Price",
  7530. // "value_id": 17
  7531. // },
  7532. //
  7533. // }
  7534. __block long val_count = 0;
  7535. NSString *sql = @"select name,type,order_by from price order by order_by";
  7536. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7537. NSString *name = [self textAtColumn:0 statement:stmt];
  7538. int type = sqlite3_column_int(stmt, 1);
  7539. int order_by = sqlite3_column_int(stmt, 2);
  7540. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7541. NSDictionary *price_dic = @{
  7542. @"value" : name,
  7543. @"value_id" : [NSNumber numberWithInteger:type],
  7544. @"check" : order_by == 0 ? @(1) : @(0)
  7545. };
  7546. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  7547. val_count = ++(*count);
  7548. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  7549. val_count = 0;
  7550. }];
  7551. [cadedate setObject:@{@"value" : @"None",
  7552. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  7553. val_count++;
  7554. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  7555. [price setObject:cadedate forKey:@"cadedate"];
  7556. [section1 setObject:price forKey:@"item_2"];
  7557. [dic setObject:section1 forKey:@"section_1"];
  7558. return [RAUtils dict2data:dic];
  7559. }
  7560. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  7561. NSString *product_id = [params objectForKey:@"fashionId"];
  7562. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  7563. __block int result = RESULT_TRUE;
  7564. __block int qty = 0;
  7565. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7566. qty = sqlite3_column_int(stmt, 0);
  7567. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  7568. result = RESULT_FALSE;
  7569. }];
  7570. NSMutableDictionary *dic = @{
  7571. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  7572. @"mode" : @"Regular Mode",
  7573. @"quantity_available" : @(qty),
  7574. @"result" : @(result),
  7575. }.mutableCopy;
  7576. return [RAUtils dict2data:dic];
  7577. }
  7578. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  7579. NSString *item_ids = [params objectForKey:@"item_id"];
  7580. NSString *line_notes = [params objectForKey:@"notes"];
  7581. NSString *price_str = [params objectForKey:@"price"];
  7582. NSString *discount_str = [params objectForKey:@"discount"];
  7583. NSString *percent = [params objectForKey:@"available_percent"];
  7584. NSString *qty = [params objectForKey:@"available_qty"];
  7585. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  7586. int dot = 0;
  7587. if (line_notes) {
  7588. line_notes = [self translateSingleQuote:line_notes];
  7589. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  7590. sql = [sql stringByAppendingString:line_notes];
  7591. dot = 1;
  7592. } else {
  7593. line_notes = @"";
  7594. }
  7595. if (price_str) {
  7596. if (dot) {
  7597. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  7598. } else {
  7599. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  7600. dot = 1;
  7601. }
  7602. sql = [sql stringByAppendingString:price_str];
  7603. } else {
  7604. price_str = @"";
  7605. }
  7606. if (discount_str) {
  7607. if (dot) {
  7608. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  7609. } else {
  7610. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  7611. dot = 1;
  7612. }
  7613. sql = [sql stringByAppendingString:discount_str];
  7614. } else {
  7615. discount_str = @"";
  7616. }
  7617. if (percent) {
  7618. if (dot) {
  7619. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  7620. } else {
  7621. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  7622. dot = 1;
  7623. }
  7624. sql = [sql stringByAppendingString:percent];
  7625. } else {
  7626. percent = @"";
  7627. }
  7628. if (qty) {
  7629. if (dot) {
  7630. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  7631. } else {
  7632. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  7633. dot = 1;
  7634. }
  7635. sql = [sql stringByAppendingString:qty];
  7636. } else {
  7637. qty = @"";
  7638. }
  7639. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  7640. sql = [sql stringByAppendingString:where];
  7641. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7642. int result = [iSalesDB execSql:sql];
  7643. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  7644. [dic setObject:@"Regular Mode" forKey:@"mode"];
  7645. return [RAUtils dict2data:dic];
  7646. }
  7647. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  7648. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  7649. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7650. sqlite3 *db = [iSalesDB get_db];
  7651. 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];
  7652. int result = [iSalesDB execSql:sql db:db];
  7653. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  7654. [dic setObject:@"Regular Mode" forKey:@"mode"];
  7655. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  7656. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  7657. [iSalesDB close_db:db];
  7658. return [RAUtils dict2data:dic];
  7659. }
  7660. + (void)offline_removePDFWithName:(NSString *)name {
  7661. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  7662. NSString *cachefolder = [paths objectAtIndex:0];
  7663. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  7664. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  7665. NSFileManager *fileManager = [NSFileManager defaultManager];
  7666. [fileManager removeItemAtPath:path error:nil];
  7667. }
  7668. + (void)offline_clear_PDFCache {
  7669. NSFileManager *fileManager = [NSFileManager defaultManager];
  7670. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  7671. NSString *cachefolder = [paths objectAtIndex:0];
  7672. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  7673. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  7674. for (NSString *path in pdf_files) {
  7675. [self offline_removePDFWithName:[path lastPathComponent]];
  7676. }
  7677. }
  7678. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  7679. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7680. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  7681. NSString *user = [params objectForKey:@"user"];
  7682. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where tearsheets_id = %d;",tearsheetsId]];
  7683. if (![create_user isEqualToString:user]) {
  7684. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  7685. [dic setObject:@"Regular Mode" forKey:@"mode"];
  7686. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  7687. return [RAUtils dict2data:dic];
  7688. }
  7689. NSString *is_local_sql = [NSString stringWithFormat:@"select is_local,pdf_path from offline_pdf where tearsheets_id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  7690. __block int is_local = 0;
  7691. __block NSString *path = @"";
  7692. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7693. is_local = sqlite3_column_int(stmt, 0);
  7694. path = [self textAtColumn:1 statement:stmt];
  7695. }];
  7696. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where tearsheets_id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  7697. if (is_local == 1) {
  7698. sql = [NSString stringWithFormat:@"delete from offline_pdf where tearsheets_id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  7699. // 删除文件
  7700. [self offline_removePDFWithName:path];
  7701. }
  7702. int result = [iSalesDB execSql:sql];
  7703. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  7704. [dic setObject:@"Regular Mode" forKey:@"mode"];
  7705. return [RAUtils dict2data:dic];
  7706. }
  7707. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial
  7708. {
  7709. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  7710. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  7711. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  7712. NSString* where=@"1=1";
  7713. if (ver!=nil) {
  7714. where=@"is_dirty=1";
  7715. }
  7716. 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];
  7717. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  7718. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7719. sqlite3 *db = [iSalesDB get_db];
  7720. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7721. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  7722. NSInteger _id = sqlite3_column_int(stmt, 0);
  7723. NSInteger product_id = sqlite3_column_int(stmt, 1);
  7724. NSString *name = [self textAtColumn:2 statement:stmt];
  7725. NSString *desc = [self textAtColumn:3 statement:stmt];
  7726. NSInteger item_id = sqlite3_column_int(stmt, 4);
  7727. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  7728. NSInteger qty = sqlite3_column_int(stmt, 6);
  7729. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  7730. double percent = sqlite3_column_double(stmt, 8);
  7731. double price = sqlite3_column_double(stmt, 9);
  7732. double discount = sqlite3_column_double(stmt, 10);
  7733. NSString *img = [self textAtColumn:11 statement:stmt];
  7734. NSString *line_note = [self textAtColumn:12 statement:stmt];
  7735. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  7736. NSString *create_time = [self textAtColumn:14 statement:stmt];
  7737. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  7738. NSString *price_null = [self textAtColumn:9 statement:stmt];
  7739. if ([price_null isEqualToString:@"null"]) {
  7740. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  7741. }
  7742. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  7743. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  7744. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  7745. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  7746. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  7747. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  7748. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  7749. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  7750. if ([qty_null isEqualToString:@"null"]) {
  7751. // [item setValue:@"null" forKey:@"available_qty"];
  7752. } else {
  7753. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  7754. }
  7755. if ([is_percent_null isEqualToString:@"null"]) {
  7756. // [item setValue:@"null" forKey:@"percentage"];
  7757. } else {
  7758. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  7759. }
  7760. if ([percent_null isEqualToString:@"null"]) {
  7761. // [item setValue:@"null" forKey:@"percent"];
  7762. } else {
  7763. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  7764. }
  7765. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  7766. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  7767. [item setValue:name forKey:@"name"];
  7768. [item setValue:desc forKey:@"description"];
  7769. [item setValue:img forKey:@"img"];
  7770. [item setValue:line_note forKey:@"line_note"];
  7771. [item setValue:create_time forKey:@"createtime"];
  7772. [item setValue:modify_time forKey:@"modifytime"];
  7773. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  7774. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  7775. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  7776. }];
  7777. [iSalesDB close_db:db];
  7778. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  7779. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7780. }
  7781. return ret;
  7782. }
  7783. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  7784. {
  7785. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  7786. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  7787. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  7788. NSString* where=@"1=1";
  7789. if (ver!=nil) {
  7790. where=@"is_dirty=1";
  7791. }
  7792. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,tearsheets_id,pdf_path,create_user,tear_note,tear_name,model_info,createtime,modify_time from offline_pdf where %@",where];
  7793. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  7794. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7795. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  7796. NSInteger _id = sqlite3_column_int(stmt, 0);
  7797. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  7798. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  7799. NSString *create_user = [self textAtColumn:3 statement:stmt];
  7800. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  7801. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  7802. NSString *model_info = [self textAtColumn:6 statement:stmt];
  7803. NSString *createtime = [self textAtColumn:7 statement:stmt];
  7804. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  7805. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  7806. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  7807. [item setObject:pdf_path forKey:@"pdf_path"];
  7808. [item setObject:create_user forKey:@"create_user"];
  7809. [item setObject:tear_note forKey:@"tear_note"];
  7810. [item setObject:tear_name forKey:@"tear_name"];
  7811. [item setObject:model_info forKey:@"model_info"];
  7812. [item setObject:createtime forKey:@"createtime"];
  7813. [item setObject:modifytime forKey:@"modifytime"];
  7814. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  7815. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  7816. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  7817. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  7818. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7819. }];
  7820. return ret;
  7821. }
  7822. @end