OLDataProvider.m 415 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138
  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. @interface OLDataProvider ()
  20. @end
  21. @implementation OLDataProvider
  22. +(NSString*) create_portfolio
  23. {
  24. // not impl
  25. return nil;
  26. }
  27. +(NSMutableDictionary*) prepare_portfolio_data:(NSMutableDictionary*) params
  28. {
  29. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  30. data[@"npd_url"]=@"www.newpacificdirect.com";
  31. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  32. [formatter setDateFormat:@"MMMM yyyy"];
  33. NSString* date = [formatter stringFromDate:[NSDate date]];
  34. data[@"create_month"]=date;
  35. data[@"company_name"]=@"New Pacific Direct,Inc.";
  36. data[@"catalog_name"]=params[@"catalog_name"];
  37. // NSMutableDictionary* header = [[NSMutableDictionary alloc]init];
  38. // header[@"icon_small"]=@"portfolio_logo_s";
  39. // data[@"header"]=header;
  40. return data;
  41. }
  42. +(NSMutableDictionary*) get_pdftemplate:(NSString*) template
  43. {
  44. NSString *path = [[NSBundle mainBundle] pathForResource:template ofType:nil];
  45. NSData *data = [NSData dataWithContentsOfFile:path];
  46. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  47. return ret;
  48. }
  49. +(NSData*) offline_request_tearsheet :(NSMutableDictionary *) params
  50. {
  51. NSString* template_file=@"portfolio_3x2.json";
  52. NSMutableDictionary* data = [self prepare_portfolio_data:params];
  53. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  54. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  55. NSLog(file);
  56. return nil;
  57. }
  58. +(NSString*) get_offline_soid:(sqlite3*)db
  59. {
  60. NSString* soid=nil;
  61. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  62. [formatter setDateFormat:@"yyMMdd"];
  63. NSString* date = [formatter stringFromDate:[NSDate date]];
  64. for(int i=1;i<999;i++)
  65. {
  66. soid=[NSString stringWithFormat:@"OFF%@%4d",date,i];
  67. soid= [soid stringByReplacingOccurrencesOfString:@" " withString:@"0"];
  68. int count=[iSalesDB get_recordcount:db table:@"offline_order" where:[NSString stringWithFormat:@"so_id='%@'",soid]];
  69. if(count==0)
  70. return soid;
  71. }
  72. return nil;
  73. }
  74. +(NSArray*) enumOfflineOrder
  75. {
  76. NSMutableArray* ret = [[NSMutableArray alloc] init];
  77. sqlite3 *db = [iSalesDB get_db];
  78. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  79. sqlite3_stmt * statement;
  80. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  81. if ( dbresult== SQLITE_OK)
  82. {
  83. while (sqlite3_step(statement) == SQLITE_ROW)
  84. {
  85. char *so_id = (char*)sqlite3_column_text(statement, 0);
  86. NSString* nsso_id=nil;
  87. if(so_id!=nil)
  88. {
  89. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  90. }
  91. [ret addObject:nsso_id];
  92. }
  93. sqlite3_finalize(statement);
  94. }
  95. [iSalesDB close_db:db];
  96. return ret;
  97. }
  98. +(void) uploadFile:(NSString*) file
  99. {
  100. NSData* data = [NSData dataWithContentsOfFile: file];
  101. UIApplication * app = [UIApplication sharedApplication];
  102. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  103. AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  104. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  105. if(appDelegate.user!=nil)
  106. [params setValue:appDelegate.user forKey:@"user"];
  107. // if(appDelegate.contact_id!=nil)
  108. // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  109. if(appDelegate.password!=nil)
  110. [params setValue:appDelegate.password forKey:@"password"];
  111. NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_OFFLINE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  112. [formData appendPartWithFileData:data name:@"upfile" fileName:[file lastPathComponent] mimeType:@"application/zip"];
  113. } error:nil];
  114. AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  115. NSProgress *progress = nil;
  116. NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  117. if (error) {
  118. NSString* err_msg = [error localizedDescription];
  119. NSLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  120. NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  121. NSLog(@"data string: %@",str);
  122. [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  123. } else {
  124. NSLog(@"response ");
  125. NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  126. // 再将NSData转为字符串
  127. NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  128. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  129. NSLog(@"data string: %@",jsonStr);
  130. NSDictionary* json = responseObject;
  131. if([[json valueForKey:@"result"] intValue]==2)
  132. {
  133. // NSString* img_url_down = json[@"img_url_aname"];
  134. // NSString* img_url_up = json[@"img_url"];
  135. }
  136. else
  137. {
  138. [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload file" controller:nil] ;
  139. }
  140. }
  141. // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  142. }];
  143. // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  144. //
  145. [uploadTask resume];
  146. }
  147. +(NSDictionary *) prepareUpload:(NSArray*) arr_order
  148. {
  149. NSString* serial= [[NSUUID UUID] UUIDString];
  150. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  151. NSString *cachefolder = [paths objectAtIndex:0];
  152. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  153. upfolder = [upfolder stringByAppendingPathComponent:serial];
  154. NSFileManager* fileManager = [NSFileManager defaultManager];
  155. BOOL bdir=YES;
  156. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  157. {
  158. NSError *error = nil;
  159. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  160. if(!bsuccess)
  161. DebugLog(@"Create UPLOAD folder failed");
  162. }
  163. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  164. ret[@"contact"]=[self prepareContact:serial];
  165. ret[@"wishlist"]=[self prepareWishlist:serial];
  166. ret[@"order"]=[self prepareOrder:serial soid:arr_order];
  167. NSString* str= [RAUtils dict2string:ret];
  168. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  169. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  170. NSError *error=nil;
  171. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  172. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  173. ZipArchive* zip = [[ZipArchive alloc] init];
  174. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  175. for(int i=0;i<arr_files.count;i++)
  176. {
  177. NSString* file=arr_files[i];
  178. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  179. }
  180. if( ![zip CloseZipFile2] )
  181. {
  182. zippath = @"";
  183. }
  184. ret[@"file"]=zippath;
  185. return ret;
  186. }
  187. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  188. {
  189. if(filename.length==0)
  190. return false;
  191. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  192. bool ret=false;
  193. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  194. NSString *cachefolder = [paths objectAtIndex:0];
  195. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  196. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  197. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  198. upfolder = [upfolder stringByAppendingPathComponent:serial];
  199. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  200. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  201. NSFileManager* fileManager = [NSFileManager defaultManager];
  202. BOOL bdir=NO;
  203. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  204. {
  205. NSError *error = nil;
  206. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  207. {
  208. ret=false;
  209. }
  210. else
  211. {
  212. ret=true;
  213. }
  214. }
  215. return ret;
  216. }
  217. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  218. {
  219. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  220. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  221. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  222. NSString* where=@" 1=1";
  223. if (ver!=nil) {
  224. where=@"is_dirty=1";
  225. }
  226. sqlite3 *db = [iSalesDB get_db];
  227. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  228. sqlite3_stmt * statement;
  229. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  230. int count=0;
  231. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  232. if ( dbresult== SQLITE_OK)
  233. {
  234. while (sqlite3_step(statement) == SQLITE_ROW)
  235. {
  236. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  237. int _id = sqlite3_column_int(statement, 0);
  238. int product_id = sqlite3_column_int(statement, 1);
  239. int item_id = sqlite3_column_int(statement, 2);
  240. int qty = sqlite3_column_int(statement, 3);
  241. char *create_time = (char*)sqlite3_column_text(statement, 4);
  242. NSString* nscreate_time=nil;
  243. if(create_time!=nil)
  244. {
  245. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  246. }
  247. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  248. NSString* nsmodify_time=nil;
  249. if(modify_time!=nil)
  250. {
  251. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  252. }
  253. int is_delete = sqlite3_column_int(statement, 6);
  254. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  255. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  256. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  257. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  258. itemjson[@"create_time"]=nscreate_time;
  259. itemjson[@"modify_time"]=nsmodify_time;
  260. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  261. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  262. count++;
  263. }
  264. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  265. sqlite3_finalize(statement);
  266. }
  267. ret[@"count"]=[NSNumber numberWithInt:count ];
  268. [iSalesDB close_db:db];
  269. return ret;
  270. }
  271. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode db:(sqlite3*)db
  272. {
  273. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  274. UIApplication * app = [UIApplication sharedApplication];
  275. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  276. 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 ];
  277. // 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 ];
  278. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  279. DebugLog(@"offline_login sql:%@",sqlQuery);
  280. sqlite3_stmt * statement;
  281. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  282. int count=0;
  283. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  284. if ( dbresult== SQLITE_OK)
  285. {
  286. while (sqlite3_step(statement) == SQLITE_ROW)
  287. {
  288. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  289. int product_id = sqlite3_column_int(statement, 0);
  290. char *str_price = (char*)sqlite3_column_text(statement, 1);
  291. int item_id = sqlite3_column_int(statement, 7);
  292. NSString* Price=nil;
  293. if(str_price==nil)
  294. {
  295. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  296. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  297. if(price==nil)
  298. Price=@"No Price.";
  299. else
  300. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  301. }
  302. else
  303. {
  304. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  305. }
  306. double discount = sqlite3_column_double(statement, 2);
  307. int item_count = sqlite3_column_int(statement, 3);
  308. char *line_note = (char*)sqlite3_column_text(statement, 4);
  309. NSString *nsline_note=nil;
  310. if(line_note!=nil)
  311. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  312. char *name = (char*)sqlite3_column_text(statement, 5);
  313. NSString *nsname=nil;
  314. if(name!=nil)
  315. nsname= [[NSString alloc]initWithUTF8String:name];
  316. char *description = (char*)sqlite3_column_text(statement, 6);
  317. NSString *nsdescription=nil;
  318. if(description!=nil)
  319. nsdescription= [[NSString alloc]initWithUTF8String:description];
  320. // int stockUom = sqlite3_column_int(statement, 8);
  321. // int _id = sqlite3_column_int(statement, 9);
  322. //
  323. //
  324. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  325. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  326. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  327. // int carton=[bsubtotaljson[@"carton"] intValue];
  328. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  329. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  330. itemjson[@"model"]=nsname;
  331. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  332. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  333. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  334. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  335. // itemjson[@"check"]=@"true";
  336. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  337. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  338. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  339. itemjson[@"unit_price"]=Price;
  340. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  341. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  342. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  343. itemjson[@"note"]=nsline_note;
  344. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  345. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  346. count++;
  347. }
  348. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  349. sqlite3_finalize(statement);
  350. }
  351. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  352. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  353. return ret;
  354. }
  355. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid
  356. {
  357. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  358. sqlite3 *db = [iSalesDB get_db];
  359. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  360. // for(int i=0;i<arr_soid.count;i++)
  361. sqlite3_stmt * statement;
  362. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  363. //int count=0;
  364. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  365. if ( dbresult== SQLITE_OK)
  366. {
  367. int count=0;
  368. while (sqlite3_step(statement) == SQLITE_ROW)
  369. {
  370. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  371. int _id = sqlite3_column_int(statement, 0);
  372. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  373. NSString* nssync_data=nil;
  374. if(sync_data!=nil)
  375. {
  376. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  377. }
  378. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  379. NSString* nsimg_1=nil;
  380. if(img_1!=nil)
  381. {
  382. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  383. [self copy_upImg:serial file:nsimg_1];
  384. }
  385. char *so_no = (char*)sqlite3_column_text(statement, 3);
  386. NSString* nsso_no=nil;
  387. if(so_no!=nil)
  388. {
  389. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  390. }
  391. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  392. itemjson[@"order_type"]=@"submit order";
  393. else
  394. itemjson[@"order_type"]=@"archive order";
  395. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  396. itemjson[@"json_data"]= nssync_data;
  397. itemjson[@"order_items"]=[self preparecart:nsso_no db:db];
  398. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  399. count++;
  400. }
  401. ret[@"count"]=[NSNumber numberWithInt:count ];
  402. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  403. }
  404. sqlite3_finalize(statement);
  405. [iSalesDB close_db:db];
  406. return ret;
  407. }
  408. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  409. {
  410. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  411. sqlite3 *db = [iSalesDB get_db];
  412. // UIApplication * app = [UIApplication sharedApplication];
  413. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  414. 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";
  415. sqlite3_stmt * statement;
  416. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  417. int count=0;
  418. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  419. if ( dbresult== SQLITE_OK)
  420. {
  421. while (sqlite3_step(statement) == SQLITE_ROW)
  422. {
  423. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  424. int _id = sqlite3_column_int(statement, 0);
  425. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  426. NSString* nsimg_0=nil;
  427. if(img_0!=nil)
  428. {
  429. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  430. [self copy_upImg:serial file:nsimg_0];
  431. }
  432. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  433. NSString* nsimg_1=nil;
  434. if(img_1!=nil)
  435. {
  436. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  437. [self copy_upImg:serial file:nsimg_1];
  438. }
  439. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  440. NSString* nsimg_2=nil;
  441. if(img_2!=nil)
  442. {
  443. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  444. [self copy_upImg:serial file:nsimg_2];
  445. }
  446. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  447. NSString* nssync_data=nil;
  448. if(sync_data!=nil)
  449. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  450. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  451. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  452. itemjson[@"json_data"]= nssync_data;
  453. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  454. count++;
  455. }
  456. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  457. sqlite3_finalize(statement);
  458. }
  459. ret[@"count"]=[NSNumber numberWithInt:count ];
  460. [iSalesDB close_db:db];
  461. return ret;
  462. }
  463. +(bool) check_offlinedata
  464. {
  465. UIApplication * app = [UIApplication sharedApplication];
  466. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  467. sqlite3 *db = [iSalesDB get_db];
  468. NSString * where=@"1=1";
  469. // if(appDelegate.user!=nil)
  470. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  471. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  472. [iSalesDB close_db:db];
  473. if(count==0)
  474. {
  475. return false;
  476. }
  477. return true;
  478. //
  479. //[iSalesDB close_db:db];
  480. }
  481. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  482. {
  483. UIApplication * app = [UIApplication sharedApplication];
  484. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  485. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  486. sqlite3 *db = [iSalesDB get_db];
  487. NSString* collectId=params[@"collectId"];
  488. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  489. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  490. [iSalesDB execSql:sqlQuery db:db];
  491. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  492. [iSalesDB close_db:db];
  493. appDelegate.wish_count =count;
  494. [appDelegate update_count_mark];
  495. ret[@"result"]= [NSNumber numberWithInt:2];
  496. return ret;
  497. }
  498. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  499. //{
  500. //
  501. // UIApplication * app = [UIApplication sharedApplication];
  502. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  503. //
  504. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  505. // sqlite3 *db = [iSalesDB get_db];
  506. // NSString* product_id=params[@"product_id"];
  507. //
  508. //
  509. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  510. //
  511. //
  512. // for(int i=0;i<arr.count;i++)
  513. // {
  514. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  515. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  516. // if(count==0)
  517. // {
  518. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  519. // [iSalesDB execSql:sqlQuery db:db];
  520. // }
  521. // }
  522. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  523. // [iSalesDB close_db:db];
  524. //
  525. // appDelegate.wish_count =count;
  526. //
  527. // [appDelegate update_count_mark];
  528. // ret[@"result"]= [NSNumber numberWithInt:2];
  529. // return ret;
  530. // //
  531. // //return ret;
  532. //}
  533. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  534. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  535. DebugLog(@"time interval: %lf",interval);
  536. }
  537. +(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  538. {
  539. UIApplication * app = [UIApplication sharedApplication];
  540. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  541. int sort = [[params objectForKey:@"sort"] intValue];
  542. NSString *sort_str = @"";
  543. switch (sort) {
  544. case 0:{
  545. sort_str = @"order by w.modify_time desc";
  546. }
  547. break;
  548. case 1:{
  549. sort_str = @"order by w.modify_time asc";
  550. }
  551. break;
  552. case 2:{
  553. sort_str = @"order by m.name asc";
  554. }
  555. break;
  556. case 3:{
  557. sort_str = @"order by m.name desc";
  558. }
  559. break;
  560. case 4:{
  561. sort_str = @"order by m.description asc";
  562. }
  563. break;
  564. default:
  565. break;
  566. }
  567. NSString* user = appDelegate.user;
  568. sqlite3 *db = [iSalesDB get_db];
  569. // order by w.create_time
  570. 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];
  571. // 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];
  572. sqlite3_stmt * statement;
  573. NSDate *date1 = [NSDate date];
  574. NSDate *date2 = nil;
  575. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  576. int count=0;
  577. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  578. {
  579. while (sqlite3_step(statement) == SQLITE_ROW)
  580. {
  581. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  582. int product_id = sqlite3_column_double(statement, 0);
  583. char *description = (char*)sqlite3_column_text(statement, 1);
  584. if(description==nil)
  585. description= "";
  586. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  587. int item_id = sqlite3_column_double(statement, 2);
  588. NSDate *date_image = [NSDate date];
  589. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  590. printf("image : ");
  591. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  592. // char *url = (char*)sqlite3_column_text(statement, 3);
  593. // if(url==nil)
  594. // url="";
  595. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  596. int qty = sqlite3_column_int(statement, 3);
  597. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  598. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  599. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  600. item[@"description"]= nsdescription;
  601. item[@"img"]= nsurl;
  602. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  603. count++;
  604. }
  605. printf("total time:");
  606. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  607. ret[@"count"]= [NSNumber numberWithInt:count];
  608. ret[@"total_count"]= [NSNumber numberWithInt:count];
  609. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  610. ret[@"result"]= [NSNumber numberWithInt:2];
  611. appDelegate.wish_count =count;
  612. [appDelegate update_count_mark];
  613. sqlite3_finalize(statement);
  614. }
  615. [iSalesDB close_db:db];
  616. return ret;
  617. }
  618. +(NSDictionary*) offline_notimpl
  619. {
  620. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  621. ret[@"result"]=@"8";
  622. ret[@"err_msg"]=@"offline mode does not support this function.";
  623. return ret;
  624. }
  625. +(NSDictionary*) offline_home
  626. {
  627. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  628. NSString *cachefolder = [paths objectAtIndex:0];
  629. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  630. NSData* json =nil;
  631. json=[NSData dataWithContentsOfFile:img_cache];
  632. NSError *error=nil;
  633. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  634. return menu;
  635. }
  636. +(NSDictionary*) offline_category_menu
  637. {
  638. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  639. NSString *cachefolder = [paths objectAtIndex:0];
  640. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  641. NSData* json =nil;
  642. json=[NSData dataWithContentsOfFile:img_cache];
  643. NSError *error=nil;
  644. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  645. return menu;
  646. }
  647. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  648. {
  649. NSString* offline_command=params[@"offline_Command"];
  650. NSDictionary* ret=nil;
  651. if([offline_command isEqualToString:@"model_NIYMAL"])
  652. {
  653. NSString* category = params[@"category"];
  654. ret = [self refresh_model_NIYMAL:category];
  655. }
  656. return ret;
  657. }
  658. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  659. {
  660. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  661. [ret setValue:@"2" forKey:@"result"];
  662. [ret setValue:@"Regular Mode" forKey:@"mode"];
  663. sqlite3* db= [iSalesDB get_db];
  664. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  665. [iSalesDB close_db:db];
  666. [ret setObject:detail1_section forKey:@"detail_1"];
  667. return ret;
  668. }
  669. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id db:(sqlite3*)db
  670. {
  671. UIApplication * app = [UIApplication sharedApplication];
  672. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  673. // NSArray* arr1 = [self get_user_all_price_type];
  674. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  675. // NSSet *set1 = [NSSet setWithArray:arr1];
  676. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  677. // if(appDelegate.contact_id==nil)
  678. // set2=[set1 mutableCopy];
  679. // else
  680. // [set2 intersectsSet:set1];
  681. // NSArray *retarr = [set2 allObjects];
  682. NSString* whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  683. // sqlite3 *db = [iSalesDB get_db];
  684. NSString* sqlQuery = nil;
  685. if(appDelegate.contact_id==nil)
  686. sqlQuery=[NSString stringWithFormat:@"select a.name,b.price 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];
  687. else
  688. sqlQuery=[NSString stringWithFormat:@"select a.name,b.price 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];
  689. sqlite3_stmt * statement;
  690. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  691. int count=0;
  692. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  693. {
  694. while (sqlite3_step(statement) == SQLITE_ROW)
  695. {
  696. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  697. char *name = (char*)sqlite3_column_text(statement, 0);
  698. if(name==nil)
  699. name="";
  700. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  701. // double price = sqlite3_column_double(statement, 1);
  702. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  703. // if(isnull==nil)
  704. // item[nsname]= @"No Price";
  705. // else
  706. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  707. char *price = (char*)sqlite3_column_text(statement, 1);
  708. if(price!=nil)
  709. {
  710. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  711. nsprice=[AESCrypt fastdecrypt:nsprice];
  712. if(nsprice.length>0)
  713. {
  714. double dp= [nsprice doubleValue];
  715. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  716. }
  717. }
  718. else
  719. {
  720. item[nsname]= @"No Price";
  721. }
  722. // item[nsname]= nsprice;
  723. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  724. count++;
  725. }
  726. ret[@"count"]= [NSNumber numberWithInt:count];
  727. sqlite3_finalize(statement);
  728. }
  729. // [iSalesDB close_db:db];
  730. return ret;
  731. }
  732. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  733. //{
  734. // NSArray* arr1 = [self get_user_all_price_type:db];
  735. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  736. //
  737. // // NSSet *set1 = [NSSet setWithArray:arr1];
  738. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  739. // // [set2 intersectsSet:set1];
  740. // //
  741. // //
  742. // // NSArray *retarr = [set2 allObjects];
  743. //
  744. // NSString* whereprice=nil;
  745. // if(contact_id==nil)
  746. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  747. // else
  748. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  749. //
  750. //
  751. // // sqlite3 *db = [iSalesDB get_db];
  752. //
  753. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  754. // sqlite3_stmt * statement;
  755. //
  756. //
  757. // NSNumber* ret = nil;
  758. // double dprice=DBL_MAX;
  759. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  760. // {
  761. //
  762. //
  763. // while (sqlite3_step(statement) == SQLITE_ROW)
  764. // {
  765. //
  766. // // double val = sqlite3_column_double(statement, 0);
  767. // char *price = (char*)sqlite3_column_text(statement, 0);
  768. // if(price!=nil)
  769. // {
  770. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  771. // nsprice=[AESCrypt fastdecrypt:nsprice];
  772. // if(nsprice.length>0)
  773. // {
  774. // double dp= [nsprice doubleValue];
  775. // if(dp<dprice)
  776. // dprice=dp;
  777. // }
  778. // }
  779. // }
  780. //
  781. //
  782. //
  783. //
  784. // sqlite3_finalize(statement);
  785. //
  786. //
  787. //
  788. //
  789. // }
  790. //
  791. // // [iSalesDB close_db:db];
  792. //
  793. // if(dprice==DBL_MAX)
  794. // ret= nil;
  795. // else
  796. // ret= [NSNumber numberWithDouble:dprice];
  797. // return ret;
  798. //}
  799. +(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  800. {
  801. NSArray* arr1 = [self get_user_all_price_type:db];
  802. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  803. NSString* whereprice=nil;
  804. if(contact_id==nil)
  805. whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  806. else
  807. whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  808. // sqlite3 *db = [iSalesDB get_db];
  809. NSString *productIdCondition = @"1 = 1";
  810. if (product_id) {
  811. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  812. }
  813. NSString *itemIdCondition = @"";
  814. if (item_id) {
  815. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  816. }
  817. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  818. sqlite3_stmt * statement;
  819. NSNumber* ret = nil;
  820. double dprice=DBL_MAX;
  821. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  822. {
  823. while (sqlite3_step(statement) == SQLITE_ROW)
  824. {
  825. // double val = sqlite3_column_double(statement, 0);
  826. char *price = (char*)sqlite3_column_text(statement, 0);
  827. if(price!=nil)
  828. {
  829. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  830. nsprice=[AESCrypt fastdecrypt:nsprice];
  831. if(nsprice.length>0)
  832. {
  833. double dp= [nsprice doubleValue];
  834. if(dp<dprice)
  835. dprice=dp;
  836. }
  837. }
  838. }
  839. sqlite3_finalize(statement);
  840. }
  841. // [iSalesDB close_db:db];
  842. if(dprice==DBL_MAX)
  843. ret= nil;
  844. else
  845. ret= [NSNumber numberWithDouble:dprice];
  846. return ret;
  847. }
  848. +(NSArray*) get_user_all_price_type:(sqlite3*)db
  849. {
  850. NSArray* ret=nil;
  851. // sqlite3 *db = [iSalesDB get_db];
  852. // no customer assigned , use login user contact_id
  853. UIApplication * app = [UIApplication sharedApplication];
  854. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  855. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",appDelegate.user];
  856. sqlite3_stmt * statement;
  857. // int count=0;
  858. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  859. {
  860. if (sqlite3_step(statement) == SQLITE_ROW)
  861. {
  862. char *val = (char*)sqlite3_column_text(statement, 0);
  863. if(val==nil)
  864. val="";
  865. NSString* price = [[NSString alloc]initWithUTF8String:val];
  866. ret=[RAUtils string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  867. }
  868. sqlite3_finalize(statement);
  869. }
  870. // [iSalesDB close_db:db];
  871. return ret;
  872. }
  873. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id db:(sqlite3*)db
  874. {
  875. // sqlite3 *db = [iSalesDB get_db];
  876. if(contact_id==nil)
  877. {
  878. // no customer assigned , use login user contact_id
  879. UIApplication * app = [UIApplication sharedApplication];
  880. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  881. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",appDelegate.user];
  882. sqlite3_stmt * statement;
  883. // int count=0;
  884. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  885. {
  886. if (sqlite3_step(statement) == SQLITE_ROW)
  887. {
  888. char *val = (char*)sqlite3_column_text(statement, 0);
  889. if(val==nil)
  890. val="";
  891. contact_id = [[NSString alloc]initWithUTF8String:val];
  892. }
  893. sqlite3_finalize(statement);
  894. }
  895. if(contact_id.length<=0)
  896. {
  897. // [iSalesDB close_db:db];
  898. return nil;
  899. }
  900. }
  901. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  902. sqlite3_stmt * statement;
  903. NSArray* ret=nil;
  904. // int count=0;
  905. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  906. {
  907. if (sqlite3_step(statement) == SQLITE_ROW)
  908. {
  909. char *price_type = (char*)sqlite3_column_text(statement, 0);
  910. if(price_type==nil)
  911. price_type="";
  912. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  913. if(nsprice_type.length>0)
  914. ret=[RAUtils string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  915. else
  916. ret=nil;
  917. }
  918. sqlite3_finalize(statement);
  919. }
  920. // [iSalesDB close_db:db];
  921. return ret;
  922. }
  923. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  924. {
  925. NSString* ret= nil;
  926. // sqlite3 *db = [iSalesDB get_db];
  927. NSString *sqlQuery = nil;
  928. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  929. // 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;
  930. // 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
  931. if(product_id==nil && model_name)
  932. 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;
  933. else if (product_id)
  934. 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
  935. sqlite3_stmt * statement;
  936. // int count=0;
  937. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  938. {
  939. if (sqlite3_step(statement) == SQLITE_ROW)
  940. {
  941. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  942. if(imgurl==nil)
  943. imgurl="";
  944. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  945. ret=nsimgurl;
  946. }
  947. sqlite3_finalize(statement);
  948. }
  949. else
  950. {
  951. [ret setValue:@"8" forKey:@"result"];
  952. }
  953. // [iSalesDB close_db:db];
  954. DebugLog(@"data string: %@",ret );
  955. return ret;
  956. }
  957. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name
  958. {
  959. NSString* ret= nil;
  960. sqlite3 *db = [iSalesDB get_db];
  961. NSString *sqlQuery = nil;
  962. if(product_id==nil)
  963. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  964. else
  965. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  966. sqlite3_stmt * statement;
  967. // int count=0;
  968. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  969. {
  970. if (sqlite3_step(statement) == SQLITE_ROW)
  971. {
  972. char *default_category = (char*)sqlite3_column_text(statement, 0);
  973. if(default_category==nil)
  974. default_category="";
  975. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  976. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  977. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  978. ret=nsdefault_category;
  979. }
  980. sqlite3_finalize(statement);
  981. }
  982. else
  983. {
  984. [ret setValue:@"8" forKey:@"result"];
  985. }
  986. [iSalesDB close_db:db];
  987. DebugLog(@"data string: %@",ret );
  988. return ret;
  989. }
  990. +(NSDictionary*) offline_model :(NSMutableDictionary *) params
  991. {
  992. NSString* model_name = [params valueForKey:@"product_name"];
  993. NSString* product_id = [params valueForKey:@"product_id"];
  994. NSString* category = [params valueForKey:@"category"];
  995. if(category==nil)
  996. category = [self model_default_category:product_id model_name:model_name];
  997. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  998. sqlite3 *db = [iSalesDB get_db];
  999. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  1000. NSString *sqlQuery = nil;
  1001. if(product_id==nil)
  1002. 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='%@';
  1003. else
  1004. 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=%@;
  1005. sqlite3_stmt * statement;
  1006. [ret setValue:@"2" forKey:@"result"];
  1007. [ret setValue:@"3" forKey:@"detail_section_count"];
  1008. // int count=0;
  1009. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1010. {
  1011. if (sqlite3_step(statement) == SQLITE_ROW)
  1012. {
  1013. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1014. char *name = (char*)sqlite3_column_text(statement, 0);
  1015. if(name==nil)
  1016. name="";
  1017. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1018. char *description = (char*)sqlite3_column_text(statement, 1);
  1019. if(description==nil)
  1020. description="";
  1021. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  1022. int product_id = sqlite3_column_int(statement, 2);
  1023. char *color = (char*)sqlite3_column_text(statement, 3);
  1024. if(color==nil)
  1025. color="";
  1026. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  1027. //
  1028. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  1029. // if(legcolor==nil)
  1030. // legcolor="";
  1031. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  1032. //
  1033. //
  1034. int availability = sqlite3_column_int(statement, 5);
  1035. //
  1036. int incoming_stock = sqlite3_column_int(statement, 6);
  1037. char *demension = (char*)sqlite3_column_text(statement, 7);
  1038. if(demension==nil)
  1039. demension="";
  1040. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  1041. // ,,,,,,,,,
  1042. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  1043. if(seat_height==nil)
  1044. seat_height="";
  1045. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  1046. char *material = (char*)sqlite3_column_text(statement, 9);
  1047. if(material==nil)
  1048. material="";
  1049. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  1050. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  1051. if(box_dim==nil)
  1052. box_dim="";
  1053. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  1054. char *volume = (char*)sqlite3_column_text(statement, 11);
  1055. if(volume==nil)
  1056. volume="";
  1057. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  1058. double weight = sqlite3_column_double(statement, 12);
  1059. char *model_set = (char*)sqlite3_column_text(statement, 13);
  1060. if(model_set==nil)
  1061. model_set="";
  1062. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  1063. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  1064. if(load_ability==nil)
  1065. load_ability="";
  1066. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  1067. char *default_category = (char*)sqlite3_column_text(statement, 15);
  1068. if(default_category==nil)
  1069. default_category="";
  1070. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  1071. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  1072. if(fabric_content==nil)
  1073. fabric_content="";
  1074. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  1075. char *assembling = (char*)sqlite3_column_text(statement, 17);
  1076. if(assembling==nil)
  1077. assembling="";
  1078. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  1079. char *made_in = (char*)sqlite3_column_text(statement, 18);
  1080. if(made_in==nil)
  1081. made_in="";
  1082. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  1083. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  1084. if(special_remarks==nil)
  1085. special_remarks="";
  1086. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  1087. int stockUcom = sqlite3_column_double(statement, 20);
  1088. char *product_group = (char*)sqlite3_column_text(statement, 21);
  1089. if(product_group==nil)
  1090. product_group="";
  1091. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  1092. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  1093. // if(fashion_selector==nil)
  1094. // fashion_selector="";
  1095. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  1096. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  1097. if(selector_field==nil)
  1098. selector_field="";
  1099. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  1100. char *property_field = (char*)sqlite3_column_text(statement, 23);
  1101. if(property_field==nil)
  1102. property_field="";
  1103. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  1104. char *packaging = (char*)sqlite3_column_text(statement, 24);
  1105. if(packaging==nil)
  1106. packaging="";
  1107. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  1108. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  1109. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  1110. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  1111. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  1112. [img_section setValue:model_s_img forKey:@"model_s_img"];
  1113. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  1114. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  1115. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  1116. NSString* Availability=nil;
  1117. if(availability>0)
  1118. Availability=[NSString stringWithFormat:@"%d",availability];
  1119. else
  1120. Availability = @"In Production";
  1121. [img_section setValue:Availability forKey:@"Availability"];
  1122. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  1123. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  1124. char *eta = (char*)sqlite3_column_text(statement, 25);
  1125. if(eta==nil)
  1126. eta="";
  1127. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  1128. [img_section setValue:nseta forKey:@"ETA"];
  1129. int item_id = sqlite3_column_int(statement, 26);
  1130. NSString* Price=nil;
  1131. if(appDelegate.bLogin==false)
  1132. Price=@"Must Sign in.";
  1133. else
  1134. {
  1135. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1136. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  1137. if(price==nil)
  1138. Price=@"No Price.";
  1139. else
  1140. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1141. }
  1142. [img_section setValue:Price forKey:@"price"];
  1143. [img_section setValue:nsname forKey:@"model_name"];
  1144. [img_section setValue:nsdescription forKey:@"model_descrition"];
  1145. [ret setObject:img_section forKey:@"img_section"];
  1146. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  1147. int detail0_item_count=0;
  1148. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1149. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1150. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1151. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1152. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1153. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1154. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1155. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1156. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1157. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1158. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1159. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1160. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1161. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  1162. NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  1163. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  1164. {
  1165. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  1166. [detail0_section setObject:[self model_kvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1167. }
  1168. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  1169. [detail0_section setValue:@"kv" forKey:@"type"];
  1170. [detail0_section setValue:@"Product Information" forKey:@"title"];
  1171. [ret setObject:detail0_section forKey:@"detail_0"];
  1172. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  1173. // [detail1_section setValue:@"detail" forKey:@"target"];
  1174. // [detail1_section setValue:@"popup" forKey:@"action"];
  1175. // [detail1_section setValue:@"content" forKey:@"type"];
  1176. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  1177. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  1178. // [detail1_section setValue:@"true" forKey:@"single_row"];
  1179. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  1180. [ret setObject:detail1_section forKey:@"detail_1"];
  1181. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  1182. [detail2_section setValue:@"detail" forKey:@"target"];
  1183. [detail2_section setValue:@"popup" forKey:@"action"];
  1184. [detail2_section setValue:@"content" forKey:@"type"];
  1185. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  1186. [detail2_section setValue:@"true" forKey:@"single_row"];
  1187. [detail2_section setValue:@"local" forKey:@"data"];
  1188. [ret setObject:detail2_section forKey:@"detail_2"];
  1189. }
  1190. sqlite3_finalize(statement);
  1191. }
  1192. else
  1193. {
  1194. [ret setValue:@"8" forKey:@"result"];
  1195. }
  1196. NSLog(@"count:%d",count);
  1197. [iSalesDB close_db:db];
  1198. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1199. return ret;
  1200. }
  1201. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  1202. NSString* orderCode = [params valueForKey:@"orderCode"];
  1203. NSString* keyword = [params valueForKey:@"keyword"];
  1204. keyword=keyword.lowercaseString;
  1205. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue];
  1206. int limit = [[params valueForKey:@"limit"] intValue];
  1207. int offset = [[params valueForKey:@"offset"] intValue];
  1208. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1209. NSString *limit_str = @"";
  1210. if (limited) {
  1211. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  1212. }
  1213. sqlite3 *db = [iSalesDB get_db];
  1214. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  1215. UIApplication * app = [UIApplication sharedApplication];
  1216. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1217. NSString *sqlQuery = nil;
  1218. if(exactMatch )
  1219. 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 ;
  1220. else
  1221. 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
  1222. DebugLog(@"offline_search sql:%@",sqlQuery);
  1223. sqlite3_stmt * statement;
  1224. [ret setValue:@"2" forKey:@"result"];
  1225. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  1226. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  1227. // int count=0;
  1228. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1229. {
  1230. int i=0;
  1231. while (sqlite3_step(statement) == SQLITE_ROW)
  1232. {
  1233. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1234. // char *name = (char*)sqlite3_column_text(statement, 1);
  1235. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1236. char *name = (char*)sqlite3_column_text(statement, 0);
  1237. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1238. char *description = (char*)sqlite3_column_text(statement, 1);
  1239. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  1240. int product_id = sqlite3_column_int(statement, 2);
  1241. // char *url = (char*)sqlite3_column_text(statement, 3);
  1242. // if(url==nil)
  1243. // url="";
  1244. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1245. int wid = sqlite3_column_int(statement, 3);
  1246. int closeout = sqlite3_column_int(statement, 4);
  1247. int cid = sqlite3_column_int(statement, 5);
  1248. int wisdelete = sqlite3_column_int(statement, 6);
  1249. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  1250. if(wid !=0 && wisdelete != 1)
  1251. [item setValue:@"true" forKey:@"wish_exists"];
  1252. else
  1253. [item setValue:@"false" forKey:@"wish_exists"];
  1254. if(closeout==0)
  1255. [item setValue:@"false" forKey:@"is_closeout"];
  1256. else
  1257. [item setValue:@"true" forKey:@"is_closeout"];
  1258. if(cid==0)
  1259. [item setValue:@"false" forKey:@"cart_exists"];
  1260. else
  1261. [item setValue:@"true" forKey:@"cart_exists"];
  1262. [item addEntriesFromDictionary:imgjson];
  1263. // [item setValue:nsurl forKey:@"img"];
  1264. [item setValue:nsname forKey:@"fash_name"];
  1265. [item setValue:nsdescription forKey:@"description"];
  1266. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  1267. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  1268. i++;
  1269. }
  1270. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  1271. [ret setObject:items forKey:@"items"];
  1272. sqlite3_finalize(statement);
  1273. }
  1274. NSLog(@"count:%d",count);
  1275. [iSalesDB close_db:db];
  1276. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1277. return ret;
  1278. }
  1279. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  1280. {
  1281. return [self search:params limited:YES];
  1282. }
  1283. +(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  1284. {
  1285. UIApplication * app = [UIApplication sharedApplication];
  1286. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1287. appDelegate.disable_trigger=true;
  1288. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  1289. appDelegate.disable_trigger=false;
  1290. //
  1291. // NSString* user = [params valueForKey:@"user"];
  1292. //
  1293. // NSString* password = [params valueForKey:@"password"];
  1294. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1295. ret[@"result"]=[NSNumber numberWithInt:2 ];
  1296. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  1297. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  1298. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  1299. [appDelegate update_count_mark];
  1300. appDelegate.can_show_price =false;
  1301. appDelegate.can_see_price =false;
  1302. appDelegate.can_create_portfolio =false;
  1303. appDelegate.can_create_order =false;
  1304. appDelegate.can_cancel_order =false;
  1305. appDelegate.can_set_cart_price =false;
  1306. appDelegate.can_delete_order =false;
  1307. appDelegate.can_submit_order =false;
  1308. appDelegate.can_set_tearsheet_price =false;
  1309. appDelegate.can_update_contact_info = false;
  1310. appDelegate.save_order_logout = false;
  1311. appDelegate.submit_order_logout = false;
  1312. appDelegate.alert_sold_in_quantities = false;
  1313. appDelegate.ipad_perm =nil ;
  1314. appDelegate.user_type = USER_ROLE_UNKNOWN;
  1315. appDelegate.OrderFilter= nil;
  1316. [appDelegate SetSo:nil];
  1317. [appDelegate set_main_button_panel];
  1318. // sqlite3 *db = [iSalesDB get_db];
  1319. //
  1320. //
  1321. //
  1322. //
  1323. //
  1324. // 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"]];
  1325. //
  1326. //
  1327. //
  1328. //
  1329. // DebugLog(@"offline_login sql:%@",sqlQuery);
  1330. // sqlite3_stmt * statement;
  1331. //
  1332. //
  1333. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1334. //
  1335. //
  1336. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1337. // {
  1338. //
  1339. //
  1340. // if (sqlite3_step(statement) == SQLITE_ROW)
  1341. // {
  1342. //
  1343. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  1344. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  1345. //
  1346. //
  1347. //
  1348. // int can_show_price = sqlite3_column_int(statement, 0);
  1349. // int can_see_price = sqlite3_column_int(statement, 1);
  1350. //
  1351. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  1352. // if(contact_id==nil)
  1353. // contact_id="";
  1354. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  1355. //
  1356. // int user_type = sqlite3_column_int(statement, 3);
  1357. //
  1358. // int can_cancel_order = sqlite3_column_int(statement, 4);
  1359. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  1360. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  1361. // int can_delete_order = sqlite3_column_int(statement, 7);
  1362. // int can_submit_order = sqlite3_column_int(statement, 8);
  1363. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  1364. // int can_create_order = sqlite3_column_int(statement, 10);
  1365. //
  1366. //
  1367. // char *mode = (char*)sqlite3_column_text(statement, 11);
  1368. // if(mode==nil)
  1369. // mode="";
  1370. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  1371. //
  1372. //
  1373. // char *username = (char*)sqlite3_column_text(statement, 12);
  1374. // if(username==nil)
  1375. // username="";
  1376. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  1377. //
  1378. //
  1379. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  1380. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  1381. // [header setValue:nscontact_id forKey:@"contact_id"];
  1382. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  1383. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  1384. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  1385. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  1386. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  1387. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  1388. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  1389. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  1390. //
  1391. // [header setValue:nsusername forKey:@"username"];
  1392. //
  1393. //
  1394. // [ret setObject:header forKey:@"header"];
  1395. // [ret setValue:nsmode forKey:@"mode"];
  1396. //
  1397. //
  1398. // }
  1399. //
  1400. //
  1401. //
  1402. // sqlite3_finalize(statement);
  1403. // }
  1404. //
  1405. //
  1406. //
  1407. // [iSalesDB close_db:db];
  1408. //
  1409. //
  1410. //
  1411. //
  1412. // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1413. return ret;
  1414. }
  1415. +(NSData*) offline_createorder :(NSMutableDictionary *) params
  1416. {
  1417. UIApplication * app = [UIApplication sharedApplication];
  1418. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1419. appDelegate.disable_trigger=true;
  1420. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  1421. appDelegate.disable_trigger=false;
  1422. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1423. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  1424. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  1425. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  1426. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  1427. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  1428. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  1429. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  1430. // NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  1431. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  1432. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  1433. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  1434. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  1435. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  1436. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  1437. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  1438. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  1439. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  1440. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  1441. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  1442. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  1443. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  1444. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  1445. [arr_name addObject:customer_first_name];
  1446. [arr_name addObject:customer_last_name];
  1447. NSString *contact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  1448. // default ship from
  1449. 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';";
  1450. __block NSString *cid = @"";
  1451. __block NSString *name = @"";
  1452. __block NSString *ext = @"";
  1453. __block NSString *contact = @"";
  1454. __block NSString *email = @"";
  1455. __block NSString *fax = @"";
  1456. __block NSString *phone = @"";
  1457. sqlite3 *db = [iSalesDB get_db];
  1458. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  1459. cid = [self textAtColumn:0 statement:statment];
  1460. name = [self textAtColumn:1 statement:statment];
  1461. ext = [self textAtColumn:2 statement:statment];
  1462. contact = [self textAtColumn:3 statement:statment];
  1463. email = [self textAtColumn:4 statement:statment];
  1464. fax = [self textAtColumn:5 statement:statment];
  1465. phone = [self textAtColumn:6 statement:statment];
  1466. }];
  1467. NSString* so_id = [self get_offline_soid:db];
  1468. if(so_id==nil)
  1469. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  1470. 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];
  1471. int result =[iSalesDB execSql:sql_neworder db:db];
  1472. [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  1473. //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'
  1474. //soId
  1475. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  1476. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  1477. sqlite3_stmt * statement;
  1478. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1479. {
  1480. if (sqlite3_step(statement) == SQLITE_ROW)
  1481. {
  1482. // char *name = (char*)sqlite3_column_text(statement, 1);
  1483. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1484. //ret = sqlite3_column_int(statement, 0);
  1485. char *soId = (char*)sqlite3_column_text(statement, 0);
  1486. if(soId==nil)
  1487. soId="";
  1488. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  1489. [ret setValue:nssoId forKey:@"soId"];
  1490. [ret setValue:nssoId forKey:@"orderCode"];
  1491. }
  1492. sqlite3_finalize(statement);
  1493. }
  1494. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  1495. [ret setValue:@"Regular Mode" forKey:@"mode"];
  1496. [iSalesDB close_db:db];
  1497. return [RAUtils dict2data:ret];
  1498. }
  1499. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  1500. {
  1501. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1502. NSString* orderCode = [params valueForKey:@"orderCode"];
  1503. UIApplication * app = [UIApplication sharedApplication];
  1504. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1505. if(![appDelegate.order_code isEqualToString:orderCode]&& appDelegate.order_code.length!=0)
  1506. {
  1507. appDelegate.disable_trigger=true;
  1508. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  1509. appDelegate.disable_trigger=false;
  1510. }
  1511. sqlite3 *db = [iSalesDB get_db];
  1512. int cart_count=[self query_ordercartcount:orderCode db:db];
  1513. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  1514. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  1515. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  1516. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1517. [iSalesDB close_db:db];
  1518. return [RAUtils dict2data:ret];
  1519. }
  1520. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  1521. {
  1522. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1523. sqlite3 *db = [iSalesDB get_db];
  1524. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  1525. int count =0;
  1526. if(params[@"count"]!=nil)
  1527. {
  1528. count = [params[@"count"] intValue];
  1529. }
  1530. NSString* product_id=params[@"product_id"];
  1531. NSString* orderCode=params[@"orderCode"];
  1532. NSString *qty = params[@"qty"];
  1533. NSArray* arr_id=[RAUtils string2arr:product_id separator:@","];
  1534. NSArray *qty_arr = [RAUtils string2arr:qty separator:@","];
  1535. for(int i=0;i<arr_id.count;i++)
  1536. {
  1537. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  1538. int item_qty= count;
  1539. if (qty) {
  1540. item_qty = [qty_arr[i] integerValue];
  1541. }
  1542. if(item_qty==0)
  1543. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  1544. NSString* sql=nil;
  1545. sqlite3_stmt *stmt;
  1546. if(_id<0)
  1547. {
  1548. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  1549. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  1550. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  1551. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],[arr_id[i] length],NULL);
  1552. sqlite3_bind_text(stmt,2,[orderCode UTF8String],[orderCode length],NULL);
  1553. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%d",item_qty] UTF8String],[[NSString stringWithFormat:@"%d",item_qty] length],NULL);
  1554. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  1555. }
  1556. else
  1557. {
  1558. if (qty) {
  1559. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  1560. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  1561. sqlite3_bind_int(stmt, 1, _id);
  1562. } else {
  1563. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  1564. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  1565. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%d",item_qty] UTF8String],[[NSString stringWithFormat:@"%d",item_qty] length],NULL);
  1566. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],[[NSString stringWithFormat:@"%d",_id] length],NULL);
  1567. }
  1568. }
  1569. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  1570. [iSalesDB execSql:@"ROLLBACK" db:db];
  1571. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  1572. [iSalesDB close_db:db];
  1573. DebugLog(@"add to cart error");
  1574. return [RAUtils dict2data:ret];
  1575. }
  1576. }
  1577. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1578. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  1579. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  1580. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  1581. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1582. [iSalesDB execSql:@"END TRANSACTION" db:db];
  1583. [iSalesDB close_db:db];
  1584. return [RAUtils dict2data:ret];
  1585. }
  1586. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute
  1587. {
  1588. UIApplication * app = [UIApplication sharedApplication];
  1589. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1590. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  1591. 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];
  1592. sqlite3_stmt * statement;
  1593. int count=0;
  1594. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1595. {
  1596. while (sqlite3_step(statement) == SQLITE_ROW)
  1597. {
  1598. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  1599. int bitem_id = sqlite3_column_int(statement, 0);
  1600. int bitem_qty = sqlite3_column_int(statement, 1);
  1601. char *name = (char*)sqlite3_column_text(statement, 2);
  1602. if(name==nil)
  1603. name="";
  1604. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  1605. char *description = (char*)sqlite3_column_text(statement, 3);
  1606. if(description==nil)
  1607. description="";
  1608. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  1609. double unit_price = sqlite3_column_double(statement, 4);
  1610. int use_unitprice = sqlite3_column_int(statement, 5);
  1611. if(use_unitprice!=1)
  1612. {
  1613. unit_price=[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db].doubleValue;
  1614. }
  1615. itemjson[@"model"]=nsname;
  1616. itemjson[@"description"]=nsdescription;
  1617. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  1618. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  1619. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  1620. if(compute)
  1621. {
  1622. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute];
  1623. }
  1624. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1625. count++;
  1626. }
  1627. sqlite3_finalize(statement);
  1628. }
  1629. ret[@"count"]=@(count);
  1630. if(count==0)
  1631. return nil;
  1632. else
  1633. return ret;
  1634. }
  1635. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  1636. {
  1637. //compute: add part to subtotal;
  1638. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  1639. dict_item[@(item_id)]=@"1";
  1640. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  1641. double cuft=0;
  1642. double weight=0;
  1643. int carton=0;
  1644. 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];
  1645. sqlite3_stmt * statement;
  1646. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1647. {
  1648. if (sqlite3_step(statement) == SQLITE_ROW)
  1649. {
  1650. double ulength = sqlite3_column_double(statement, 0);
  1651. double uwidth = sqlite3_column_double(statement, 1);
  1652. double uheight = sqlite3_column_double(statement, 2);
  1653. double uweight = sqlite3_column_double(statement, 3);
  1654. double mlength = sqlite3_column_double(statement, 4);
  1655. double mwidth = sqlite3_column_double(statement, 5);
  1656. double mheight = sqlite3_column_double(statement, 6);
  1657. double mweight = sqlite3_column_double(statement, 7);
  1658. double ilength = sqlite3_column_double(statement, 8);
  1659. double iwidth = sqlite3_column_double(statement, 9);
  1660. double iheight = sqlite3_column_double(statement, 10);
  1661. double iweight = sqlite3_column_double(statement, 11);
  1662. int pcs = sqlite3_column_int(statement,12);
  1663. int mpack = sqlite3_column_int(statement, 13);
  1664. int ipack = sqlite3_column_int(statement, 14);
  1665. double ucbf = sqlite3_column_double(statement, 15);
  1666. double icbf = sqlite3_column_double(statement, 16);
  1667. double mcbf = sqlite3_column_double(statement, 17);
  1668. if(ipack==0)
  1669. {
  1670. carton= count/mpack ;
  1671. weight = mweight*carton;
  1672. cuft= carton*(mlength*mwidth*mheight);
  1673. int remain=count%mpack;
  1674. if(remain==0)
  1675. {
  1676. //do nothing;
  1677. }
  1678. else
  1679. {
  1680. carton++;
  1681. weight += uweight*remain;
  1682. cuft += (ulength*uwidth*uheight)*remain;
  1683. }
  1684. }
  1685. else
  1686. {
  1687. carton = count/(mpack*ipack);
  1688. weight = mweight*carton;
  1689. cuft= carton*(mlength*mwidth*mheight);
  1690. int remain=count%(mpack*ipack);
  1691. if(remain==0)
  1692. {
  1693. // do nothing;
  1694. }
  1695. else
  1696. {
  1697. carton++;
  1698. int icarton =remain/ipack;
  1699. int iremain=remain%ipack;
  1700. weight += iweight*icarton;
  1701. cuft += (ilength*iwidth*iheight)*icarton;
  1702. if(iremain==0)
  1703. {
  1704. //do nothing;
  1705. }
  1706. else
  1707. {
  1708. weight += uweight*iremain;
  1709. cuft += (ulength*uwidth*uheight)*iremain;
  1710. }
  1711. }
  1712. }
  1713. #ifdef BUILD_NPD
  1714. cuft=ucbf*count;
  1715. weight= uweight*count;
  1716. #endif
  1717. }
  1718. sqlite3_finalize(statement);
  1719. }
  1720. if(compute)
  1721. {
  1722. NSArray * arr_count=nil;
  1723. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  1724. for(int i=0;i<arr_bundle.count;i++)
  1725. {
  1726. dict_item[arr_bundle[i]]=@"1";
  1727. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  1728. cuft+=[bundlejson[@"cuft"] doubleValue];
  1729. weight+=[bundlejson[@"weight"] doubleValue];
  1730. carton+=[bundlejson[@"carton"] intValue];
  1731. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  1732. }
  1733. }
  1734. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  1735. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  1736. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  1737. ret[@"items"]=dict_item;
  1738. return ret;
  1739. }
  1740. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  1741. {
  1742. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  1743. // 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 ];
  1744. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  1745. DebugLog(@"offline_login sql:%@",sqlQuery);
  1746. sqlite3_stmt * statement;
  1747. int cart_count=0;
  1748. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1749. if ( dbresult== SQLITE_OK)
  1750. {
  1751. while (sqlite3_step(statement) == SQLITE_ROW)
  1752. {
  1753. int item_id = sqlite3_column_int(statement, 0);
  1754. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  1755. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  1756. }
  1757. sqlite3_finalize(statement);
  1758. }
  1759. return cart_count;
  1760. }
  1761. +(NSData*) offline_requestcart :(NSMutableDictionary *) params
  1762. {
  1763. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1764. sqlite3 *db = [iSalesDB get_db];
  1765. UIApplication * app = [UIApplication sharedApplication];
  1766. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1767. NSString* orderCode=params[@"orderCode"];
  1768. int sort = [[params objectForKey:@"sort"] intValue];
  1769. NSString *sort_str = @"";
  1770. switch (sort) {
  1771. case 0:{
  1772. sort_str = @"order by c.modify_time desc";
  1773. }
  1774. break;
  1775. case 1:{
  1776. sort_str = @"order by c.modify_time asc";
  1777. }
  1778. break;
  1779. case 2:{
  1780. sort_str = @"order by m.name asc";
  1781. }
  1782. break;
  1783. case 3:{
  1784. sort_str = @"order by m.name desc";
  1785. }
  1786. break;
  1787. case 4:{
  1788. sort_str = @"order by m.description asc";
  1789. }
  1790. break;
  1791. default:
  1792. break;
  1793. }
  1794. 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 ];
  1795. // 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 ];
  1796. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  1797. DebugLog(@"offline_login sql:%@",sqlQuery);
  1798. sqlite3_stmt * statement;
  1799. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1800. NSDate *date1 = [NSDate date];
  1801. int count=0;
  1802. int cart_count=0;
  1803. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1804. if ( dbresult== SQLITE_OK)
  1805. {
  1806. while (sqlite3_step(statement) == SQLITE_ROW)
  1807. {
  1808. NSDate *row_date = [NSDate date];
  1809. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1810. int product_id = sqlite3_column_int(statement, 0);
  1811. char *str_price = (char*)sqlite3_column_text(statement, 1);
  1812. int item_id = sqlite3_column_int(statement, 7);
  1813. NSString* Price=nil;
  1814. if(str_price==nil)
  1815. {
  1816. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1817. // NSDate *price_date = [NSDate date];
  1818. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  1819. // DebugLog(@"price time interval");
  1820. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  1821. if(price==nil)
  1822. Price=@"No Price.";
  1823. else
  1824. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1825. }
  1826. else
  1827. {
  1828. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1829. }
  1830. double discount = sqlite3_column_double(statement, 2);
  1831. int item_count = sqlite3_column_int(statement, 3);
  1832. char *line_note = (char*)sqlite3_column_text(statement, 4);
  1833. NSString *nsline_note=nil;
  1834. if(line_note!=nil)
  1835. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1836. char *name = (char*)sqlite3_column_text(statement, 5);
  1837. NSString *nsname=nil;
  1838. if(name!=nil)
  1839. nsname= [[NSString alloc]initWithUTF8String:name];
  1840. char *description = (char*)sqlite3_column_text(statement, 6);
  1841. NSString *nsdescription=nil;
  1842. if(description!=nil)
  1843. nsdescription= [[NSString alloc]initWithUTF8String:description];
  1844. int stockUom = sqlite3_column_int(statement, 8);
  1845. int _id = sqlite3_column_int(statement, 9);
  1846. // NSDate *subtotal_date = [NSDate date];
  1847. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  1848. // DebugLog(@"subtotal_date time interval");
  1849. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  1850. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1851. double weight=[bsubtotaljson[@"weight"] doubleValue];
  1852. int carton=[bsubtotaljson[@"carton"] intValue];
  1853. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  1854. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1855. // NSDate *img_date = [NSDate date];
  1856. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  1857. // DebugLog(@"img_date time interval");
  1858. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  1859. itemjson[@"model"]=nsname;
  1860. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  1861. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  1862. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  1863. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  1864. itemjson[@"check"]=@"true";
  1865. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  1866. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  1867. itemjson[@"unit_price"]=Price;
  1868. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  1869. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1870. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  1871. itemjson[@"note"]=nsline_note;
  1872. // NSDate *date2 = [NSDate date];
  1873. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  1874. // DebugLog(@"model_bundle time interval");
  1875. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  1876. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1877. count++;
  1878. // DebugLog(@"row time interval");
  1879. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  1880. }
  1881. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1882. sqlite3_finalize(statement);
  1883. }
  1884. DebugLog(@"request cart total time interval");
  1885. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  1886. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1887. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  1888. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  1889. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  1890. ret[@"count"]=[NSNumber numberWithInt:count ];
  1891. ret[@"mode"]=@"Regular Mode";
  1892. [iSalesDB close_db:db];
  1893. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  1894. DebugLog(@"general notes :%@",general_note);
  1895. ret[@"general_note"]= general_note;
  1896. return [RAUtils dict2data:ret];
  1897. }
  1898. +(NSData*) offline_login :(NSMutableDictionary *) params
  1899. {
  1900. NSString* user = [params valueForKey:@"user"];
  1901. NSString* password = [params valueForKey:@"password"];
  1902. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1903. sqlite3 *db = [iSalesDB get_db];
  1904. 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"]];
  1905. DebugLog(@"offline_login sql:%@",sqlQuery);
  1906. sqlite3_stmt * statement;
  1907. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1908. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1909. {
  1910. if (sqlite3_step(statement) == SQLITE_ROW)
  1911. {
  1912. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  1913. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  1914. int can_show_price = sqlite3_column_int(statement, 0);
  1915. int can_see_price = sqlite3_column_int(statement, 1);
  1916. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  1917. if(contact_id==nil)
  1918. contact_id="";
  1919. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  1920. int user_type = sqlite3_column_int(statement, 3);
  1921. int can_cancel_order = sqlite3_column_int(statement, 4);
  1922. int can_set_cart_price = sqlite3_column_int(statement, 5);
  1923. int can_create_portfolio = sqlite3_column_int(statement, 6);
  1924. int can_delete_order = sqlite3_column_int(statement, 7);
  1925. int can_submit_order = sqlite3_column_int(statement, 8);
  1926. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  1927. int can_create_order = sqlite3_column_int(statement, 10);
  1928. char *mode = (char*)sqlite3_column_text(statement, 11);
  1929. if(mode==nil)
  1930. mode="";
  1931. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  1932. char *username = (char*)sqlite3_column_text(statement, 12);
  1933. if(username==nil)
  1934. username="";
  1935. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  1936. int can_update_contact_info = sqlite3_column_int(statement, 13);
  1937. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  1938. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  1939. [header setValue:nscontact_id forKey:@"contact_id"];
  1940. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  1941. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  1942. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  1943. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  1944. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  1945. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  1946. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  1947. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  1948. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  1949. [header setValue:nsusername forKey:@"username"];
  1950. NSError* error=nil;
  1951. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  1952. [header setValue:statusFilter forKey:@"statusFilter"];
  1953. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  1954. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  1955. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  1956. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  1957. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"1=1"];
  1958. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  1959. [ret setObject:header forKey:@"header"];
  1960. [ret setValue:nsmode forKey:@"mode"];
  1961. }
  1962. sqlite3_finalize(statement);
  1963. }
  1964. [iSalesDB close_db:db];
  1965. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1966. return [RAUtils dict2data:ret];
  1967. }
  1968. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  1969. {
  1970. NSString* contactId = [params valueForKey:@"contactId"];
  1971. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1972. sqlite3 *db = [iSalesDB get_db];
  1973. NSString *sqlQuery = nil;
  1974. {
  1975. 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];
  1976. }
  1977. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  1978. sqlite3_stmt * statement;
  1979. [ret setValue:@"2" forKey:@"result"];
  1980. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1981. {
  1982. //int i = 0;
  1983. if (sqlite3_step(statement) == SQLITE_ROW)
  1984. {
  1985. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  1986. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1987. // int editable = sqlite3_column_int(statement, 0);
  1988. char *company_name = (char*)sqlite3_column_text(statement, 1);
  1989. NSString *nscompany_name =nil;
  1990. if(company_name==nil)
  1991. nscompany_name=@"";
  1992. else
  1993. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  1994. char *country = (char*)sqlite3_column_text(statement, 2);
  1995. if(country==nil)
  1996. country="";
  1997. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  1998. // char *addr = (char*)sqlite3_column_text(statement, 3);
  1999. // if(addr==nil)
  2000. // addr="";
  2001. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  2002. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  2003. if(zipcode==nil)
  2004. zipcode="";
  2005. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  2006. char *state = (char*)sqlite3_column_text(statement, 5);
  2007. if(state==nil)
  2008. state="";
  2009. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  2010. char *city = (char*)sqlite3_column_text(statement, 6);
  2011. if(city==nil)
  2012. city="";
  2013. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  2014. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  2015. // NSString *nscontact_name = nil;
  2016. // if(contact_name==nil)
  2017. // nscontact_name=@"";
  2018. // else
  2019. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  2020. char *phone = (char*)sqlite3_column_text(statement, 8);
  2021. NSString *nsphone = nil;
  2022. if(phone==nil)
  2023. nsphone=@"";
  2024. else
  2025. nsphone= [[NSString alloc]initWithUTF8String:phone];
  2026. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  2027. if(contact_id==nil)
  2028. contact_id="";
  2029. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2030. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  2031. if(addr_1==nil)
  2032. addr_1="";
  2033. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  2034. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  2035. if(addr_2==nil)
  2036. addr_2="";
  2037. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  2038. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  2039. if(addr_3==nil)
  2040. addr_3="";
  2041. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  2042. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  2043. if(addr_4==nil)
  2044. addr_4="";
  2045. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  2046. char *first_name = (char*)sqlite3_column_text(statement, 14);
  2047. if(first_name==nil)
  2048. first_name="";
  2049. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  2050. char *last_name = (char*)sqlite3_column_text(statement, 15);
  2051. if(last_name==nil)
  2052. last_name="";
  2053. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  2054. char *fax = (char*)sqlite3_column_text(statement, 16);
  2055. NSString *nsfax = nil;
  2056. if(fax==nil)
  2057. nsfax=@"";
  2058. else
  2059. nsfax= [[NSString alloc]initWithUTF8String:fax];
  2060. char *email = (char*)sqlite3_column_text(statement, 17);
  2061. NSString *nsemail = nil;
  2062. if(email==nil)
  2063. nsemail=@"";
  2064. else
  2065. nsemail= [[NSString alloc]initWithUTF8String:email];
  2066. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  2067. NSString *nsimg_0 = nil;
  2068. if(img_0==nil)
  2069. nsimg_0=@"";
  2070. else
  2071. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  2072. [self copy_bcardImg:nsimg_0];
  2073. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  2074. NSString *nsimg_1 = nil;
  2075. if(img_1==nil)
  2076. nsimg_1=@"";
  2077. else
  2078. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2079. [self copy_bcardImg:nsimg_1];
  2080. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  2081. NSString *nsimg_2 = nil;
  2082. if(img_2==nil)
  2083. nsimg_2=@"";
  2084. else
  2085. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  2086. [self copy_bcardImg:nsimg_2];
  2087. char *price_type = (char*)sqlite3_column_text(statement, 21);
  2088. NSString *nsprice_type = nil;
  2089. if(price_type==nil)
  2090. nsprice_type=@"";
  2091. else
  2092. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  2093. char *notes = (char*)sqlite3_column_text(statement, 22);
  2094. NSString *nsnotes = nil;
  2095. if(notes==nil)
  2096. nsnotes=@"";
  2097. else
  2098. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  2099. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  2100. NSString *nssalesrep = nil;
  2101. if(salesrep==nil)
  2102. nssalesrep=@"";
  2103. else
  2104. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  2105. {
  2106. // decrypt
  2107. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  2108. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  2109. nsphone=[AESCrypt fastdecrypt:nsphone];
  2110. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  2111. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  2112. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  2113. }
  2114. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  2115. [arr_name addObject:nsfirst_name];
  2116. [arr_name addObject:nslast_name];
  2117. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  2118. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  2119. [arr_ext addObject:nsaddr_1];
  2120. [arr_ext addObject:nsaddr_2];
  2121. [arr_ext addObject:nsaddr_3];
  2122. [arr_ext addObject:nsaddr_4];
  2123. [arr_ext addObject:@"\r\n"];
  2124. [arr_ext addObject:nscity];
  2125. [arr_ext addObject:nsstate];
  2126. [arr_ext addObject:nszipcode];
  2127. [arr_ext addObject:nscountry];
  2128. NSString *nsext=[RAUtils arr2string:arr_ext separator:@", " trim:true];
  2129. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  2130. [item setValue:nsimg_2 forKey:@"business_card_2"];
  2131. [item setValue:nsimg_0 forKey:@"business_card_0"];
  2132. [item setValue:nscountry forKey:@"customer_country"];
  2133. [item setValue:nsphone forKey:@"customer_phone"];
  2134. [item setValue:nsimg_1 forKey:@"business_card_1"];
  2135. [item setValue:nscompany_name forKey:@"customer_name"];
  2136. [item setValue:nsprice_type forKey:@"customer_price_type"];
  2137. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  2138. [item setValue:nsext forKey:@"customer_contact_ext"];
  2139. [item setValue:nszipcode forKey:@"customer_zipcode"];
  2140. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  2141. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  2142. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  2143. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  2144. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  2145. [item setValue:nslast_name forKey:@"customer_last_name"];
  2146. [item setValue:nscity forKey:@"customer_city"];
  2147. [item setValue:nsstate forKey:@"customer_state"];
  2148. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  2149. [item setValue:contactId forKey:@"customer_cid"];
  2150. [item setValue:nscontact_name forKey:@"customer_contact"];
  2151. [item setValue:nsfax forKey:@"customer_fax"];
  2152. [item setValue:nsemail forKey:@"customer_email"];
  2153. [ret setObject:item forKey:@"customerInfo"];
  2154. // i++;
  2155. }
  2156. UIApplication * app = [UIApplication sharedApplication];
  2157. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2158. [ret setValue:appDelegate.mode forKey:@"mode"];
  2159. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  2160. sqlite3_finalize(statement);
  2161. }
  2162. [iSalesDB close_db:db];
  2163. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2164. return ret;
  2165. }
  2166. + (bool) copy_bcardImg:(NSString*) filename
  2167. {
  2168. if(filename.length==0)
  2169. return false;
  2170. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2171. bool ret=false;
  2172. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2173. NSString *cachefolder = [paths objectAtIndex:0];
  2174. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  2175. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  2176. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  2177. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  2178. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  2179. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  2180. //
  2181. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  2182. NSFileManager* fileManager = [NSFileManager defaultManager];
  2183. BOOL bdir=NO;
  2184. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  2185. {
  2186. NSError *error = nil;
  2187. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  2188. {
  2189. ret=false;
  2190. }
  2191. else
  2192. {
  2193. ret=true;
  2194. }
  2195. // NSError *error = nil;
  2196. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  2197. //
  2198. // if(!bsuccess)
  2199. // {
  2200. // DebugLog(@"Create offline_createimg folder failed");
  2201. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2202. // return [RAUtils dict2data:ret];
  2203. // }
  2204. // if(bsuccess)
  2205. // {
  2206. // sqlite3 *db = [self get_db];
  2207. //
  2208. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  2209. // [iSalesDB close_db:db];
  2210. // }
  2211. }
  2212. return ret;
  2213. //
  2214. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  2215. // if(bsuccess)
  2216. // {
  2217. // NSError *error = nil;
  2218. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  2219. // {
  2220. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2221. // }
  2222. // else
  2223. // {
  2224. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  2225. //
  2226. // ret[@"img_url_aname"]=filename;
  2227. // ret[@"img_url"]=savedImagePath;
  2228. // }
  2229. // }
  2230. }
  2231. +(NSData *) offline_saveBusinesscard:(NSData *) image
  2232. {
  2233. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2234. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2235. NSString *cachefolder = [paths objectAtIndex:0];
  2236. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  2237. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  2238. NSFileManager* fileManager = [NSFileManager defaultManager];
  2239. BOOL bdir=YES;
  2240. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  2241. {
  2242. NSError *error = nil;
  2243. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  2244. if(!bsuccess)
  2245. {
  2246. DebugLog(@"Create offline_createimg folder failed");
  2247. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2248. return [RAUtils dict2data:ret];
  2249. }
  2250. // if(bsuccess)
  2251. // {
  2252. // sqlite3 *db = [self get_db];
  2253. //
  2254. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  2255. // [iSalesDB close_db:db];
  2256. // }
  2257. }
  2258. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  2259. //JEPG格式
  2260. //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  2261. NSString* filename =[NSString stringWithFormat:@"%@.png",[[NSUUID UUID] UUIDString]];
  2262. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  2263. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  2264. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  2265. if(bsuccess)
  2266. {
  2267. NSError *error = nil;
  2268. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  2269. {
  2270. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2271. }
  2272. else
  2273. {
  2274. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  2275. ret[@"img_url_aname"]=filename;
  2276. ret[@"img_url"]=filename;
  2277. }
  2278. }
  2279. else
  2280. {
  2281. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2282. }
  2283. return [RAUtils dict2data:ret];
  2284. }
  2285. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  2286. {
  2287. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  2288. category = [category substringToIndex:3];
  2289. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2290. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  2291. params[@"category"]= category;
  2292. ret[@"params"]= params;
  2293. [ret setValue:@"detail" forKey:@"target"];
  2294. [ret setValue:@"popup" forKey:@"action"];
  2295. [ret setValue:@"content" forKey:@"type"];
  2296. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  2297. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  2298. [ret setValue:@"true" forKey:@"single_row"];
  2299. [ret setValue:@"true" forKey:@"partial_refresh"];
  2300. // sqlite3 *db = [iSalesDB get_db];
  2301. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  2302. 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 ;
  2303. sqlite3_stmt * statement;
  2304. int count = 0;
  2305. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  2306. // int count=0;
  2307. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2308. {
  2309. int i=0;
  2310. while (sqlite3_step(statement) == SQLITE_ROW)
  2311. {
  2312. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2313. // char *name = (char*)sqlite3_column_text(statement, 1);
  2314. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2315. char *name = (char*)sqlite3_column_text(statement, 0);
  2316. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2317. int product_id = sqlite3_column_int(statement, 1);
  2318. char *url = (char*)sqlite3_column_text(statement, 2);
  2319. if(url==nil)
  2320. url="";
  2321. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2322. [item setValue:nsurl forKey:@"picture_path"];
  2323. [item setValue:nsname forKey:@"fash_name"];
  2324. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2325. [item setValue:category forKey:@"category"];
  2326. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2327. i++;
  2328. }
  2329. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2330. sqlite3_finalize(statement);
  2331. }
  2332. NSLog(@"count:%d",count);
  2333. // [iSalesDB close_db:db];
  2334. return ret;
  2335. }
  2336. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  2337. {
  2338. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2339. [ret setValue:key forKey:@"key"];
  2340. [ret setValue:value forKey:@"val"];
  2341. return ret;
  2342. }
  2343. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  2344. {
  2345. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2346. [ret setValue:@"0" forKey:@"img_count"];
  2347. // sqlite3 *db = [iSalesDB get_db];
  2348. 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 ;
  2349. sqlite3_stmt * statement;
  2350. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2351. {
  2352. int i=0;
  2353. if (sqlite3_step(statement) == SQLITE_ROW)
  2354. {
  2355. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2356. // char *name = (char*)sqlite3_column_text(statement, 1);
  2357. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2358. char *value = (char*)sqlite3_column_text(statement, 0);
  2359. if(value==nil)
  2360. value="";
  2361. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  2362. char *key = (char*)sqlite3_column_text(statement, 1);
  2363. if(key==nil)
  2364. key="";
  2365. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  2366. [item setValue:nsvalue forKey:@"val"];
  2367. [item setValue:nskey forKey:@"key"];
  2368. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2369. [ret setValue:@"1" forKey:@"count"];
  2370. i++;
  2371. }
  2372. sqlite3_finalize(statement);
  2373. }
  2374. // [iSalesDB close_db:db];
  2375. return ret;
  2376. }
  2377. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  2378. {
  2379. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2380. [ret setValue:@"0" forKey:@"count"];
  2381. // sqlite3 *db = [iSalesDB get_db];
  2382. 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;
  2383. sqlite3_stmt * statement;
  2384. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2385. {
  2386. int i=0;
  2387. while (sqlite3_step(statement) == SQLITE_ROW)
  2388. {
  2389. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2390. // char *name = (char*)sqlite3_column_text(statement, 1);
  2391. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2392. char *value = (char*)sqlite3_column_text(statement, 0);
  2393. if(value==nil)
  2394. value="";
  2395. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  2396. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  2397. if(selector_display==nil)
  2398. selector_display="";
  2399. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  2400. int product_id = sqlite3_column_int(statement, 2);
  2401. char *category = (char*)sqlite3_column_text(statement, 3);
  2402. if(category==nil)
  2403. category="";
  2404. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  2405. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  2406. [item setValue:nsvalue forKey:@"title"];
  2407. [item setValue:url forKey:@"pic_url"];
  2408. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  2409. [params setValue:@"2" forKey:@"count"];
  2410. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  2411. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  2412. [param0 setValue:@"product_id" forKey:@"name"];
  2413. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  2414. [param1 setValue:nscategory forKey:@"val"];
  2415. [param1 setValue:@"category" forKey:@"name"];
  2416. [params setObject:param0 forKey:@"param_0"];
  2417. [params setObject:param1 forKey:@"param_1"];
  2418. [item setObject:params forKey:@"params"];
  2419. [ret setValue:nsselector_display forKey:@"name"];
  2420. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2421. i++;
  2422. }
  2423. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2424. [ret setValue:@"switch" forKey:@"action"];
  2425. sqlite3_finalize(statement);
  2426. }
  2427. // [iSalesDB close_db:db];
  2428. return ret;
  2429. }
  2430. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  2431. {
  2432. // model 在 category search 显示的图片。
  2433. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  2434. // sqlite3 *db = [iSalesDB get_db];
  2435. 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];
  2436. sqlite3_stmt * statement;
  2437. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2438. {
  2439. while (sqlite3_step(statement) == SQLITE_ROW)
  2440. {
  2441. char *url = (char*)sqlite3_column_text(statement, 0);
  2442. if(url==nil)
  2443. url="";
  2444. int type = sqlite3_column_int(statement, 1);
  2445. if(type==0)
  2446. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  2447. else
  2448. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  2449. }
  2450. sqlite3_finalize(statement);
  2451. }
  2452. // [iSalesDB close_db:db];
  2453. return ret;
  2454. }
  2455. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  2456. {
  2457. int item_id=-1;
  2458. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  2459. sqlite3_stmt * statement;
  2460. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2461. {
  2462. if (sqlite3_step(statement) == SQLITE_ROW)
  2463. {
  2464. item_id = sqlite3_column_int(statement, 0);
  2465. }
  2466. sqlite3_finalize(statement);
  2467. }
  2468. return item_id;
  2469. }
  2470. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  2471. {
  2472. // NSString* ret = @"";
  2473. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  2474. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  2475. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  2476. sqlite3_stmt * statement;
  2477. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2478. {
  2479. while (sqlite3_step(statement) == SQLITE_ROW)
  2480. {
  2481. int bitem_id = sqlite3_column_int(statement, 0);
  2482. int bitem_qty = sqlite3_column_int(statement, 1);
  2483. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  2484. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  2485. }
  2486. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  2487. sqlite3_finalize(statement);
  2488. }
  2489. // if(ret==nil)
  2490. // ret=@"";
  2491. *count=arr_count;
  2492. return arr_bundle;
  2493. }
  2494. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  2495. {
  2496. // get default sold qty, return -1 if model not found;
  2497. int ret = -1;
  2498. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  2499. sqlite3_stmt * statement;
  2500. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2501. {
  2502. if (sqlite3_step(statement) == SQLITE_ROW)
  2503. {
  2504. ret = sqlite3_column_int(statement, 0);
  2505. }
  2506. sqlite3_finalize(statement);
  2507. }
  2508. return ret;
  2509. }
  2510. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  2511. {
  2512. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2513. [ret setValue:@"0" forKey:@"img_count"];
  2514. // sqlite3 *db = [iSalesDB get_db];
  2515. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  2516. sqlite3_stmt * statement;
  2517. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2518. {
  2519. int i=0;
  2520. while (sqlite3_step(statement) == SQLITE_ROW)
  2521. {
  2522. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2523. // char *name = (char*)sqlite3_column_text(statement, 1);
  2524. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2525. char *url = (char*)sqlite3_column_text(statement, 0);
  2526. if(url==nil)
  2527. url="";
  2528. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2529. [item setValue:nsurl forKey:@"s"];
  2530. [item setValue:nsurl forKey:@"l"];
  2531. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  2532. i++;
  2533. }
  2534. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  2535. sqlite3_finalize(statement);
  2536. }
  2537. // [iSalesDB close_db:db];
  2538. return ret;
  2539. }
  2540. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  2541. UIApplication * app = [UIApplication sharedApplication];
  2542. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2543. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2544. sqlite3 *db = [iSalesDB get_db];
  2545. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  2546. NSString* product_id=params[@"product_id"];
  2547. NSString *item_count_str = params[@"item_count"];
  2548. NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  2549. NSArray *item_count_arr = item_count_str ? [RAUtils string2arr:item_count_str separator:@","] : nil;
  2550. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  2551. // NSString *sql = @"";
  2552. for(int i=0;i<arr.count;i++)
  2553. {
  2554. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  2555. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  2556. __block int cart_count = 0;
  2557. if (!item_count_str) {
  2558. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  2559. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2560. NSString *model_set = [self textAtColumn:0 statement:stmt];
  2561. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  2562. cart_count = [[model_set_components lastObject] integerValue];
  2563. }];
  2564. }
  2565. if(count==0)
  2566. {
  2567. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  2568. sqlite3_stmt *stmt;
  2569. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  2570. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],[arr[i] length],NULL);
  2571. if (item_count_arr) {
  2572. sqlite3_bind_int(stmt,2,[item_count_arr[i] integerValue]);
  2573. } else {
  2574. sqlite3_bind_int(stmt,2,cart_count);
  2575. }
  2576. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  2577. [iSalesDB execSql:@"ROLLBACK" db:db];
  2578. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  2579. [iSalesDB close_db:db];
  2580. DebugLog(@"add to wishlist error");
  2581. return ret;
  2582. }
  2583. } else {
  2584. int qty = 0;
  2585. if (item_count_arr) {
  2586. qty = [item_count_arr[i] integerValue];
  2587. } else {
  2588. qty = cart_count;
  2589. }
  2590. 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]];
  2591. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  2592. [iSalesDB execSql:@"ROLLBACK" db:db];
  2593. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  2594. [iSalesDB close_db:db];
  2595. DebugLog(@"add to wishlist error");
  2596. return ret;
  2597. }
  2598. }
  2599. }
  2600. // [iSalesDB execSql:sql db:db];
  2601. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  2602. [iSalesDB execSql:@"END TRANSACTION" db:db];
  2603. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  2604. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  2605. [iSalesDB close_db:db];
  2606. appDelegate.wish_count =count;
  2607. [appDelegate update_count_mark];
  2608. ret[@"result"]= [NSNumber numberWithInt:2];
  2609. return ret;
  2610. }
  2611. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  2612. // 0 category
  2613. // 1 search
  2614. // 2 itemsearch
  2615. NSData *ret = nil;
  2616. NSDictionary *items = nil;
  2617. switch (from) {
  2618. case 0:{
  2619. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  2620. }
  2621. break;
  2622. case 1:{
  2623. items = [[self search:params limited:NO] objectForKey:@"items"];
  2624. }
  2625. break;
  2626. case 2:{
  2627. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  2628. }
  2629. break;
  2630. default:
  2631. break;
  2632. }
  2633. if (!items) {
  2634. return ret;
  2635. }
  2636. int count = [[items objectForKey:@"count"] intValue];
  2637. NSMutableString *product_id_str = [@"" mutableCopy];
  2638. for (int i = 0; i < count; i++) {
  2639. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  2640. NSDictionary *item = [items objectForKey:key];
  2641. NSString *product_id = [item objectForKey:@"product_id"];
  2642. if (i == 0) {
  2643. [product_id_str appendString:product_id];
  2644. } else {
  2645. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  2646. }
  2647. }
  2648. NSString *add_to = [self valueInParams:params key:@"addTo"];
  2649. if ([add_to isEqualToString:@"cart"]) {
  2650. NSString *order_code = [params objectForKey:@"orderCode"];
  2651. if (order_code.length) {
  2652. NSDictionary *newParams = @{
  2653. @"product_id" : product_id_str,
  2654. @"orderCode" : order_code
  2655. };
  2656. ret = [self offline_add2cart:newParams.mutableCopy];
  2657. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  2658. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  2659. ret = [RAUtils dict2data:retDic];
  2660. }
  2661. } else if([add_to isEqualToString:@"wishlist"]) {
  2662. NSDictionary *newParams = @{
  2663. @"product_id" : product_id_str
  2664. };
  2665. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  2666. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  2667. ret = [RAUtils dict2data:retDic];
  2668. } else if([add_to isEqualToString:@"portfolio"]) {
  2669. }
  2670. return ret;
  2671. }
  2672. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  2673. {
  2674. return [self addAll:params from:0];
  2675. }
  2676. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  2677. {
  2678. return [self addAll:params from:1];
  2679. }
  2680. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  2681. {
  2682. return [self addAll:params from:2];
  2683. }
  2684. #pragma mark - Jack
  2685. #warning 做SQL操作时转义!!
  2686. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  2687. // "val_227" : {
  2688. // "check" : 1,
  2689. // "value" : "US United States",
  2690. // "value_id" : "228"
  2691. // },
  2692. if (!countryCode) {
  2693. countryCode = @"US";
  2694. }
  2695. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  2696. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  2697. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  2698. int code_id = sqlite3_column_int(stmt, 3); // id
  2699. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  2700. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  2701. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  2702. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  2703. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  2704. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2705. }
  2706. long n = *count;
  2707. *count = n + 1;
  2708. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  2709. [container setValue:countryDic forKey:key];
  2710. }] mutableCopy];
  2711. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  2712. return ret;
  2713. }
  2714. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  2715. countryCode = [self translateSingleQuote:countryCode];
  2716. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  2717. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2718. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  2719. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  2720. if (name == NULL) {
  2721. name = "";
  2722. }
  2723. if (code == NULL) {
  2724. code = "";
  2725. }
  2726. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  2727. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  2728. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  2729. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  2730. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  2731. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2732. }
  2733. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  2734. [container setValue:stateDic forKey:key];
  2735. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  2736. DebugLog(@"query all state error: %@",err_msg);
  2737. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  2738. // [stateDic setValue:@"Other" forKey:@"value"];
  2739. // [stateDic setValue:@"" forKey:@"value_id"];
  2740. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  2741. //
  2742. // if (state_code && [@"" isEqualToString:state_code]) {
  2743. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2744. // }
  2745. //
  2746. // NSString *key = [NSString stringWithFormat:@"val_0"];
  2747. // [container setValue:stateDic forKey:key];
  2748. }] mutableCopy];
  2749. // failure 可以不用了,一样的
  2750. if (ret.allKeys.count == 0) {
  2751. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  2752. [stateDic setValue:@"Other" forKey:@"value"];
  2753. [stateDic setValue:@"" forKey:@"value_id"];
  2754. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  2755. if (state_code && [@"" isEqualToString:state_code]) {
  2756. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2757. }
  2758. NSString *key = [NSString stringWithFormat:@"val_0"];
  2759. [ret setValue:stateDic forKey:key];
  2760. }
  2761. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  2762. return ret;
  2763. }
  2764. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  2765. codeId = [self translateSingleQuote:codeId];
  2766. 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];
  2767. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2768. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  2769. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  2770. if (name == NULL) {
  2771. name = "";
  2772. }
  2773. if (code == NULL) {
  2774. code = "";
  2775. }
  2776. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  2777. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  2778. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  2779. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  2780. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  2781. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2782. }
  2783. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  2784. [container setValue:stateDic forKey:key];
  2785. }] mutableCopy];
  2786. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  2787. return ret;
  2788. }
  2789. + (NSDictionary *)offline_getPrice {
  2790. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price;"];
  2791. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2792. char *name = (char *) sqlite3_column_text(stmt, 1);
  2793. int type = sqlite3_column_int(stmt, 2);
  2794. int orderBy = sqlite3_column_int(stmt, 3);
  2795. if (name == NULL) {
  2796. name = "";
  2797. }
  2798. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  2799. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  2800. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  2801. NSString *key = [NSString stringWithFormat:@"val_%d",orderBy];
  2802. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  2803. if (orderBy == 0) {
  2804. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2805. }
  2806. [container setValue:priceDic forKey:key];
  2807. }] mutableCopy];
  2808. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  2809. return ret;
  2810. }
  2811. + (NSDictionary *)offline_getSalesRep {
  2812. // 首先从offline_login表中取出sales_code
  2813. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  2814. NSString *user = app.user;
  2815. user = [self translateSingleQuote:user];
  2816. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  2817. __block NSString *user_code = @"";
  2818. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2819. char *code = (char *)sqlite3_column_text(stmt, 0);
  2820. if (code == NULL) {
  2821. code = "";
  2822. }
  2823. user_code = [NSString stringWithUTF8String:code];
  2824. }];
  2825. // 再取所有salesRep
  2826. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  2827. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2828. // 1 name 2 code 3 salesrep_id
  2829. char *name = (char *)sqlite3_column_text(stmt, 1);
  2830. char *code = (char *)sqlite3_column_text(stmt, 2);
  2831. int salesrep_id = sqlite3_column_int(stmt, 3);
  2832. if (name == NULL) {
  2833. name = "";
  2834. }
  2835. if (code == NULL) {
  2836. code = "";
  2837. }
  2838. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  2839. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  2840. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  2841. // 比较code 相等则check
  2842. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  2843. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2844. }
  2845. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  2846. }] mutableCopy];
  2847. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  2848. return ret;
  2849. }
  2850. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  2851. zipcode = [self translateSingleQuote:zipcode];
  2852. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  2853. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2854. char *country = (char *)sqlite3_column_text(stmt, 0);
  2855. char *state = (char *)sqlite3_column_text(stmt, 1);
  2856. char *city = (char *)sqlite3_column_text(stmt, 2);
  2857. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  2858. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  2859. if (country == NULL) {
  2860. country = "";
  2861. }
  2862. if (state == NULL) {
  2863. state = "";
  2864. }
  2865. if (city == NULL) {
  2866. city = "";
  2867. }
  2868. if (country_code == NULL) {
  2869. country_code = "";
  2870. }
  2871. if (state_code == NULL) {
  2872. state_code = "";
  2873. }
  2874. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  2875. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  2876. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  2877. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  2878. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  2879. }] mutableCopy];
  2880. return ret;
  2881. }
  2882. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  2883. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  2884. [item setValue:value forKey:valueKey];
  2885. [dic setValue:item forKey:itemKey];
  2886. }
  2887. + (NSString *)countryCodeByid:(NSString *)code_id {
  2888. NSString *ret = nil;
  2889. code_id = [self translateSingleQuote:code_id];
  2890. sqlite3 *db = [iSalesDB get_db];
  2891. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  2892. sqlite3_stmt * statement;
  2893. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  2894. while (sqlite3_step(statement) == SQLITE_ROW) {
  2895. char *code = (char *)sqlite3_column_text(statement, 0);
  2896. if (code == NULL) {
  2897. code = "";
  2898. }
  2899. ret = [NSString stringWithUTF8String:code];
  2900. }
  2901. sqlite3_finalize(statement);
  2902. }
  2903. [iSalesDB close_db:db];
  2904. return ret;
  2905. }
  2906. + (NSString *)countryCodeIdByCode:(NSString *)code {
  2907. NSString *ret = nil;
  2908. code = [self translateSingleQuote:code];
  2909. sqlite3 *db = [iSalesDB get_db];
  2910. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  2911. sqlite3_stmt * statement;
  2912. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  2913. while (sqlite3_step(statement) == SQLITE_ROW) {
  2914. char *_id = (char *)sqlite3_column_text(statement, 0);
  2915. if (_id == NULL) {
  2916. _id = "";
  2917. }
  2918. ret = [NSString stringWithFormat:@"%s",_id];
  2919. }
  2920. sqlite3_finalize(statement);
  2921. }
  2922. [iSalesDB close_db:db];
  2923. return ret;
  2924. }
  2925. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  2926. NSString *name = nil;
  2927. codeId = [self translateSingleQuote:codeId];
  2928. sqlite3 *db = [iSalesDB get_db];
  2929. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  2930. sqlite3_stmt * statement;
  2931. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  2932. while (sqlite3_step(statement) == SQLITE_ROW) {
  2933. char *value = (char *)sqlite3_column_text(statement, 0);
  2934. if (value == NULL) {
  2935. value = "";
  2936. }
  2937. name = [NSString stringWithUTF8String:value];
  2938. }
  2939. sqlite3_finalize(statement);
  2940. }
  2941. [iSalesDB close_db:db];
  2942. return name;
  2943. }
  2944. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  2945. NSString *ret = nil;
  2946. sqlite3 *db = [iSalesDB get_db];
  2947. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  2948. sqlite3_stmt * statement;
  2949. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  2950. while (sqlite3_step(statement) == SQLITE_ROW) {
  2951. char *name = (char *)sqlite3_column_text(statement, 0);
  2952. if (name == NULL) {
  2953. name = "";
  2954. }
  2955. ret = [NSString stringWithUTF8String:name];
  2956. }
  2957. sqlite3_finalize(statement);
  2958. }
  2959. [iSalesDB close_db:db];
  2960. return ret;
  2961. }
  2962. + (NSString *)salesRepCodeById:(NSString *)_id {
  2963. NSString *ret = nil;
  2964. _id = [self translateSingleQuote:_id];
  2965. sqlite3 *db = [iSalesDB get_db];
  2966. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  2967. sqlite3_stmt * statement;
  2968. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  2969. while (sqlite3_step(statement) == SQLITE_ROW) {
  2970. char *rep = (char *)sqlite3_column_text(statement, 0);
  2971. if (rep == NULL) {
  2972. rep = "";
  2973. }
  2974. ret = [NSString stringWithUTF8String:rep];
  2975. }
  2976. sqlite3_finalize(statement);
  2977. }
  2978. [iSalesDB close_db:db];
  2979. return ret;
  2980. }
  2981. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  2982. char *tx = (char *)sqlite3_column_text(stmt, col);
  2983. if (tx == NULL) {
  2984. tx = "";
  2985. }
  2986. NSString *text = [NSString stringWithFormat:@"%s",tx];
  2987. if (!text) {
  2988. text = @"";
  2989. }
  2990. // 将字符全部为' '的字符串干掉
  2991. int spaceCount = 0;
  2992. for (int i = 0; i < text.length; i++) {
  2993. if ([text characterAtIndex:i] == ' ') {
  2994. spaceCount++;
  2995. }
  2996. }
  2997. if (spaceCount == text.length) {
  2998. text = @"";
  2999. }
  3000. return text;
  3001. }
  3002. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  3003. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  3004. NSData *data = [NSData dataWithContentsOfFile:path];
  3005. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  3006. return ret;
  3007. }
  3008. + (NSString *)textFileName:(NSString *)name {
  3009. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  3010. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  3011. if (!text) {
  3012. text = @"";
  3013. }
  3014. return text;
  3015. }
  3016. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  3017. return [[dic objectForKey:key] mutableCopy];
  3018. }
  3019. + (id)translateSingleQuote:(NSString *)string {
  3020. if ([string isKindOfClass:[NSString class]])
  3021. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3022. return string;
  3023. }
  3024. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  3025. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  3026. }
  3027. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  3028. NSString* ret= nil;
  3029. NSString *sqlQuery = nil;
  3030. // 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
  3031. 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];
  3032. sqlite3_stmt * statement;
  3033. // int count=0;
  3034. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3035. {
  3036. if (sqlite3_step(statement) == SQLITE_ROW)
  3037. {
  3038. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  3039. if(imgurl==nil)
  3040. imgurl="";
  3041. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  3042. ret=nsimgurl;
  3043. }
  3044. sqlite3_finalize(statement);
  3045. }
  3046. else
  3047. {
  3048. [ret setValue:@"8" forKey:@"result"];
  3049. }
  3050. // [iSalesDB close_db:db];
  3051. // DebugLog(@"data string: %@",ret );
  3052. return ret;
  3053. }
  3054. #pragma mark contact list
  3055. +(NSDictionary*) offline_contactlist :(NSMutableDictionary *) params
  3056. {
  3057. // contactType = "Sales_Order_Customer";
  3058. // limit = 25;
  3059. // offset = 0;
  3060. // password = 123456;
  3061. // "price_name" = 16;
  3062. // user = EvanK;
  3063. sqlite3 *db = [iSalesDB get_db];
  3064. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  3065. if (contactType) {
  3066. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  3067. } else {
  3068. contactType = @"1 = 1";
  3069. }
  3070. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  3071. DebugLog(@"offline contact list keyword: %@",keyword);
  3072. // advanced search
  3073. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  3074. if (contact_name) {
  3075. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3076. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  3077. } else {
  3078. contact_name = @"";
  3079. }
  3080. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  3081. if (customer_phone) {
  3082. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3083. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  3084. } else {
  3085. customer_phone = @"";
  3086. }
  3087. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  3088. if (customer_fax) {
  3089. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3090. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  3091. } else {
  3092. customer_fax = @"";
  3093. }
  3094. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  3095. if (customer_zipcode) {
  3096. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3097. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  3098. } else {
  3099. customer_zipcode = @"";
  3100. }
  3101. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  3102. if (customer_sales_rep) {
  3103. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3104. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  3105. } else {
  3106. customer_sales_rep = @"";
  3107. }
  3108. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  3109. if (customer_state) {
  3110. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3111. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  3112. } else {
  3113. customer_state = @"";
  3114. }
  3115. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  3116. if (customer_name) {
  3117. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3118. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  3119. } else {
  3120. customer_name = @"";
  3121. }
  3122. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  3123. if (customer_country) {
  3124. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3125. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  3126. } else {
  3127. customer_country = @"";
  3128. }
  3129. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  3130. if (customer_cid) {
  3131. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3132. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  3133. } else {
  3134. customer_cid = @"";
  3135. }
  3136. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  3137. if (customer_city) {
  3138. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3139. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  3140. } else {
  3141. customer_city = @"";
  3142. }
  3143. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  3144. if (customer_address) {
  3145. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3146. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  3147. } else {
  3148. customer_address = @"";
  3149. }
  3150. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  3151. if (customer_email) {
  3152. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3153. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  3154. } else {
  3155. customer_email = @"";
  3156. }
  3157. NSString *price_name = [params valueForKey:@"price_name"];
  3158. if (price_name) {
  3159. if ([price_name containsString:@","]) {
  3160. // 首先从 price表中查处name
  3161. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  3162. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  3163. for (int i = 1;i < pArray.count;i++) {
  3164. NSString *p = pArray[i];
  3165. [mutablePStr appendFormat:@" or type = %@ ",p];
  3166. }
  3167. [mutablePStr appendString:@";"];
  3168. __block NSMutableArray *price_name_array = [NSMutableArray array];
  3169. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3170. char *name = (char *)sqlite3_column_text(stmt, 0);
  3171. if (!name)
  3172. name = "";
  3173. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  3174. }];
  3175. // 再根据name 拼sql
  3176. NSMutableString *mutable_price_name = [NSMutableString string];
  3177. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  3178. for (int i = 1; i < price_name_array.count; i++) {
  3179. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  3180. }
  3181. [mutable_price_name appendString:@")"];
  3182. price_name = mutable_price_name;
  3183. } else {
  3184. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  3185. if ([price_name isEqualToString:@""]) {
  3186. price_name = @"";
  3187. } else {
  3188. __block NSString *price;
  3189. price_name = [self translateSingleQuote:price_name];
  3190. [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) {
  3191. char *p = (char *)sqlite3_column_text(stmt, 0);
  3192. if (p == NULL) {
  3193. p = "";
  3194. }
  3195. price = [NSString stringWithUTF8String:p];
  3196. }];
  3197. if ([price isEqualToString:@""]) {
  3198. price_name = @"";
  3199. } else {
  3200. price = [self translateSingleQuote:price];
  3201. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  3202. }
  3203. }
  3204. }
  3205. } else {
  3206. price_name = @"";
  3207. }
  3208. int limit = [[params valueForKey:@"limit"] intValue];
  3209. int offset = [[params valueForKey:@"offset"] intValue];
  3210. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3211. 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];
  3212. 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];
  3213. // int result= [iSalesDB AddExFunction:db];
  3214. int count =0;
  3215. NSString *sqlQuery = nil;
  3216. if(keyword.length==0)
  3217. {
  3218. // 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];
  3219. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  3220. sqlQuery = sql;
  3221. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  3222. }
  3223. else
  3224. {
  3225. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  3226. keyword = keyword.lowercaseString;
  3227. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3228. 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];
  3229. 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]];
  3230. }
  3231. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  3232. sqlite3_stmt * statement;
  3233. [ret setValue:@"2" forKey:@"result"];
  3234. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  3235. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3236. {
  3237. int i = 0;
  3238. while (sqlite3_step(statement) == SQLITE_ROW)
  3239. {
  3240. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3241. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3242. int editable = sqlite3_column_int(statement, 0);
  3243. char *company_name = (char*)sqlite3_column_text(statement, 1);
  3244. NSString *nscompany_name =nil;
  3245. if(company_name==nil)
  3246. nscompany_name=@"";
  3247. else
  3248. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  3249. char *country = (char*)sqlite3_column_text(statement, 2);
  3250. if(country==nil)
  3251. country="";
  3252. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  3253. // char *addr = (char*)sqlite3_column_text(statement, 3);
  3254. // if(addr==nil)
  3255. // addr="";
  3256. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  3257. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  3258. if(zipcode==nil)
  3259. zipcode="";
  3260. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  3261. char *state = (char*)sqlite3_column_text(statement, 5);
  3262. if(state==nil)
  3263. state="";
  3264. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  3265. char *city = (char*)sqlite3_column_text(statement, 6);
  3266. if(city==nil)
  3267. city="";
  3268. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  3269. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  3270. // NSString *nscontact_name = nil;
  3271. // if(contact_name==nil)
  3272. // nscontact_name=@"";
  3273. // else
  3274. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  3275. char *phone = (char*)sqlite3_column_text(statement, 8);
  3276. NSString *nsphone = nil;
  3277. if(phone==nil)
  3278. nsphone=@"";
  3279. else
  3280. nsphone= [[NSString alloc]initWithUTF8String:phone];
  3281. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  3282. if(contact_id==nil)
  3283. contact_id="";
  3284. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3285. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  3286. if(addr_1==nil)
  3287. addr_1="";
  3288. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  3289. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  3290. if(addr_2==nil)
  3291. addr_2="";
  3292. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  3293. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  3294. if(addr_3==nil)
  3295. addr_3="";
  3296. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  3297. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  3298. if(addr_4==nil)
  3299. addr_4="";
  3300. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  3301. char *first_name = (char*)sqlite3_column_text(statement, 14);
  3302. if(first_name==nil)
  3303. first_name="";
  3304. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  3305. char *last_name = (char*)sqlite3_column_text(statement, 15);
  3306. if(last_name==nil)
  3307. last_name="";
  3308. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  3309. char *fax = (char*)sqlite3_column_text(statement, 16);
  3310. NSString *nsfax = nil;
  3311. if(fax==nil)
  3312. nsfax=@"";
  3313. else
  3314. {
  3315. nsfax= [[NSString alloc]initWithUTF8String:fax];
  3316. if(nsfax.length>0)
  3317. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  3318. }
  3319. char *email = (char*)sqlite3_column_text(statement, 17);
  3320. NSString *nsemail = nil;
  3321. if(email==nil)
  3322. nsemail=@"";
  3323. else
  3324. {
  3325. nsemail= [[NSString alloc]initWithUTF8String:email];
  3326. if(nsemail.length>0)
  3327. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  3328. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  3329. }
  3330. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3331. [arr_name addObject:nsfirst_name];
  3332. [arr_name addObject:nslast_name];
  3333. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  3334. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  3335. {
  3336. // decrypt
  3337. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  3338. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  3339. nsphone=[AESCrypt fastdecrypt:nsphone];
  3340. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3341. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  3342. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3343. }
  3344. [arr_addr addObject:nscompany_name];
  3345. [arr_addr addObject:nscontact_name];
  3346. [arr_addr addObject:@"<br>"];
  3347. [arr_addr addObject:nsaddr_1];
  3348. [arr_addr addObject:nsaddr_2];
  3349. [arr_addr addObject:nsaddr_3];
  3350. [arr_addr addObject:nsaddr_4];
  3351. //[arr_addr addObject:nsaddr];
  3352. [arr_addr addObject:nszipcode];
  3353. [arr_addr addObject:nscity];
  3354. [arr_addr addObject:nsstate];
  3355. [arr_addr addObject:nscountry];
  3356. [arr_addr addObject:@"<br>"];
  3357. [arr_addr addObject:nsphone];
  3358. [arr_addr addObject:nsfax];
  3359. [arr_addr addObject:nsemail];
  3360. NSString * name = [RAUtils arr2string:arr_addr separator:@", " trim:true];
  3361. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  3362. [item setValue:name forKey:@"name"];
  3363. [item setValue:nscontact_id forKey:@"contact_id"];
  3364. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  3365. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3366. i++;
  3367. }
  3368. UIApplication * app = [UIApplication sharedApplication];
  3369. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3370. [ret setValue:appDelegate.mode forKey:@"mode"];
  3371. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  3372. sqlite3_finalize(statement);
  3373. }
  3374. [iSalesDB close_db:db];
  3375. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3376. return ret;
  3377. }
  3378. #pragma mark contact Advanced search
  3379. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  3380. {
  3381. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  3382. return [RAUtils dict2data:contactAdvanceDic];
  3383. }
  3384. #pragma mark create new contact
  3385. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  3386. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  3387. NSData *data = [NSData dataWithContentsOfFile:path];
  3388. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  3389. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  3390. NSString *countryCode = nil;
  3391. NSString *countryCode_id = nil;
  3392. NSString *stateCode = nil;
  3393. NSString *city = nil;
  3394. NSString *zipCode = nil;
  3395. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  3396. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  3397. NSString *code_id = params[@"country"];
  3398. countryCode_id = code_id;
  3399. countryCode = [self countryCodeByid:code_id];
  3400. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  3401. NSString *zip_code = params[@"zipcode"];
  3402. // 剔除全部为空格
  3403. int spaceCount = 0;
  3404. for (int i = 0; i < zip_code.length; i++) {
  3405. if ([zip_code characterAtIndex:i] == ' ') {
  3406. spaceCount++;
  3407. }
  3408. }
  3409. if (spaceCount == zip_code.length) {
  3410. zip_code = @"";
  3411. }
  3412. zipCode = zip_code;
  3413. if (zipCode.length > 0) {
  3414. countryCode_id = params[@"country"];
  3415. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  3416. countryCode = [dic valueForKey:@"country_code"];
  3417. if (!countryCode) {
  3418. countryCode = @"US";
  3419. }
  3420. stateCode = [dic valueForKey:@"state_code"];
  3421. city = [dic valueForKey:@"city"];
  3422. // zip code
  3423. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_10"] mutableCopy];
  3424. [zipDic setValue:zipCode forKey:@"value"];
  3425. [section_0 setValue:zipDic forKey:@"item_10"];
  3426. }
  3427. }
  3428. } else {
  3429. // default: US United States
  3430. countryCode = @"US";
  3431. countryCode_id = @"228";
  3432. }
  3433. // country
  3434. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  3435. [self setValue:allCountry forItemKey:@"item_5" valueKey:@"cadedate" inDictionary:section_0];
  3436. // state
  3437. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  3438. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  3439. [self setValue:allState forItemKey:@"item_11" valueKey:@"cadedate" inDictionary:section_0];
  3440. // city
  3441. if (city) {
  3442. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_12"] mutableCopy];
  3443. [cityDic setValue:city forKey:@"value"];
  3444. [section_0 setValue:cityDic forKey:@"item_12"];
  3445. }
  3446. // price type
  3447. NSDictionary *priceDic = [self offline_getPrice];
  3448. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  3449. // Sales Rep
  3450. NSDictionary *repDic = [self offline_getSalesRep];
  3451. [self setValue:repDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  3452. [ret setValue:section_0 forKey:@"section_0"];
  3453. return [RAUtils dict2data:ret];
  3454. }
  3455. #pragma mark save
  3456. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  3457. NSString *addr = nil;
  3458. NSString *contact_name = nil;
  3459. NSString *companyName = [params objectForKey:@"company"];
  3460. if (companyName) {
  3461. companyName = [AESCrypt fastencrypt:companyName];
  3462. } else {
  3463. companyName = @"";
  3464. }
  3465. DebugLog(@"company");
  3466. companyName = [self translateSingleQuote:companyName];
  3467. NSString *addr1 = [params objectForKey:@"address"];
  3468. NSString *addr2 = [params objectForKey:@"address2"];
  3469. NSString *addr3 = [params objectForKey:@"address_3"];
  3470. NSString *addr4 = [params objectForKey:@"address_4"];
  3471. if (!addr2) {
  3472. addr2 = @"";
  3473. }
  3474. if (!addr3) {
  3475. addr3 = @"";
  3476. }
  3477. if (!addr4) {
  3478. addr4 = @"";
  3479. }
  3480. if (!addr1) {
  3481. addr1 = @"";
  3482. }
  3483. DebugLog(@"addr");
  3484. addr = [RAUtils arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  3485. addr = [AESCrypt fastencrypt:addr];
  3486. addr = [self translateSingleQuote:addr];
  3487. if (addr1 && ![addr1 isEqualToString:@""]) {
  3488. addr1 = [AESCrypt fastencrypt:addr1];
  3489. }
  3490. addr1 = [self translateSingleQuote:addr1];
  3491. addr2 = [self translateSingleQuote:addr2];
  3492. addr3 = [self translateSingleQuote:addr3];
  3493. addr4 = [self translateSingleQuote:addr4];
  3494. NSString *country = [params objectForKey:@"country"];
  3495. if (country) {
  3496. country = [self countryNameByCountryCodeId:country];
  3497. } else {
  3498. country = @"";
  3499. }
  3500. DebugLog(@"country");
  3501. country = [self translateSingleQuote:country];
  3502. NSString *state = [params objectForKey:@"state"];
  3503. if (!state) {
  3504. state = @"";
  3505. }
  3506. DebugLog(@"state");
  3507. state = [self translateSingleQuote:state];
  3508. NSString *city = [params objectForKey:@"city"];
  3509. if (!city) {
  3510. city = @"";
  3511. }
  3512. city = [self translateSingleQuote:city];
  3513. NSString *zipcode = [params objectForKey:@"zipcode"];
  3514. if (!zipcode) {
  3515. zipcode = @"";
  3516. }
  3517. DebugLog(@"zip");
  3518. zipcode = [self translateSingleQuote:zipcode];
  3519. NSString *fistName = [params objectForKey:@"firstname"];
  3520. if (!fistName) {
  3521. fistName = @"";
  3522. }
  3523. NSString *lastName = [params objectForKey:@"lastname"];
  3524. if (!lastName) {
  3525. lastName = @"";
  3526. }
  3527. contact_name = [RAUtils arr2string:@[fistName,lastName] separator:@" " trim:true];
  3528. DebugLog(@"contact_name");
  3529. contact_name = [self translateSingleQuote:contact_name];
  3530. fistName = [self translateSingleQuote:fistName];
  3531. lastName = [self translateSingleQuote:lastName];
  3532. NSString *phone = [params objectForKey:@"phone"];
  3533. if (phone) {
  3534. phone = [AESCrypt fastencrypt:phone];
  3535. } else {
  3536. phone = @"";
  3537. }
  3538. DebugLog(@"PHONE");
  3539. phone = [self translateSingleQuote:phone];
  3540. NSString *fax = [params objectForKey:@"fax"];
  3541. if (!fax) {
  3542. fax = @"";
  3543. }
  3544. DebugLog(@"FAX");
  3545. fax = [self translateSingleQuote:fax];
  3546. NSString *email = [params objectForKey:@"email"];
  3547. if (!email) {
  3548. email = @"";
  3549. }
  3550. DebugLog(@"EMAIL:%@",email);
  3551. email = [self translateSingleQuote:email];
  3552. NSString *notes = [params objectForKey:@"contact_notes"];
  3553. if (!notes) {
  3554. notes = @"";
  3555. }
  3556. DebugLog(@"NOTE:%@",notes);
  3557. notes = [self translateSingleQuote:notes];
  3558. NSString *price = [params objectForKey:@"price_name"];
  3559. if (price) {
  3560. price = [self priceNameByPriceId:price];
  3561. } else {
  3562. price = @"";
  3563. }
  3564. DebugLog(@"PRICE");
  3565. price = [self translateSingleQuote:price];
  3566. NSString *salesRep = [params objectForKey:@"sales_rep"];
  3567. if (salesRep) {
  3568. salesRep = [self salesRepCodeById:salesRep];
  3569. } else {
  3570. salesRep = @"";
  3571. }
  3572. salesRep = [self translateSingleQuote:salesRep];
  3573. NSString *img = [params objectForKey:@"business_card"];
  3574. NSArray *array = [img componentsSeparatedByString:@","];
  3575. NSString *img_0 = array[0];
  3576. if (!img_0) {
  3577. img_0 = @"";
  3578. }
  3579. img_0 = [self translateSingleQuote:img_0];
  3580. NSString *img_1 = array[1];
  3581. if (!img_1) {
  3582. img_1 = @"";
  3583. }
  3584. img_1 = [self translateSingleQuote:img_1];
  3585. NSString *img_2 = array[2];
  3586. if (!img_2) {
  3587. img_2 = @"";
  3588. }
  3589. img_2 = [self translateSingleQuote:img_2];
  3590. NSString *contact_id = [NSUUID UUID].UUIDString;
  3591. // 判断更新时是否为customer
  3592. if (update) {
  3593. contact_id = [params objectForKey:@"contact_id"];
  3594. if (!contact_id) {
  3595. contact_id = @"";
  3596. }
  3597. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  3598. __block int customer = 0;
  3599. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3600. customer = sqlite3_column_int(stmt, 0);
  3601. }];
  3602. isCustomer = customer ? YES : NO;
  3603. }
  3604. NSMutableDictionary *sync_dic = [params mutableCopy];
  3605. if (isCustomer) {
  3606. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  3607. } else {
  3608. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  3609. }
  3610. NSString *sync_data = nil;
  3611. NSString *sql = nil;
  3612. if (update){
  3613. contact_id = [params objectForKey:@"contact_id"];
  3614. if (!contact_id) {
  3615. contact_id = @"";
  3616. }
  3617. [sync_dic setValue:contact_id forKey:@"contact_id"];
  3618. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  3619. sync_data = [RAUtils dict2string:sync_dic];
  3620. sync_data = [self translateSingleQuote:sync_data];
  3621. 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];
  3622. } else {
  3623. contact_id = [self translateSingleQuote:contact_id];
  3624. [sync_dic setValue:contact_id forKey:@"contact_id"];
  3625. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  3626. sync_data = [RAUtils dict2string:sync_dic];
  3627. sync_data = [self translateSingleQuote:sync_data];
  3628. 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];
  3629. }
  3630. int result = [iSalesDB execSql:sql];
  3631. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  3632. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  3633. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  3634. }
  3635. #pragma mark save new contact
  3636. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  3637. {
  3638. return [self offline_saveContact:params update:NO isCustomer:YES];
  3639. }
  3640. #pragma mark edit contact
  3641. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  3642. {
  3643. // {
  3644. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  3645. // password = 123456;
  3646. // user = EvanK;
  3647. // }
  3648. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  3649. NSData *data = [NSData dataWithContentsOfFile:path];
  3650. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  3651. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  3652. NSString *countryCode = nil;
  3653. NSString *countryCode_id = nil;
  3654. NSString *stateCode = nil;
  3655. /*------contact infor------*/
  3656. __block NSString *country = nil;
  3657. __block NSString *company_name = nil;
  3658. __block NSString *contact_id = params[@"contact_id"];
  3659. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  3660. __block NSString *zipcode = nil;
  3661. __block NSString *state = nil; // state_code
  3662. __block NSString *city = nil; //
  3663. __block NSString *firt_name,*last_name;
  3664. __block NSString *phone,*fax,*email;
  3665. __block NSString *notes,*price_type,*sales_rep;
  3666. __block NSString *img_0,*img_1,*img_2;
  3667. contact_id = [self translateSingleQuote:contact_id];
  3668. 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];
  3669. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3670. country = [self textAtColumn:0 statement:stmt]; // country name
  3671. company_name = [self textAtColumn:1 statement:stmt];
  3672. addr_1 = [self textAtColumn:2 statement:stmt];
  3673. addr_2 = [self textAtColumn:3 statement:stmt];
  3674. addr_3 = [self textAtColumn:4 statement:stmt];
  3675. addr_4 = [self textAtColumn:5 statement:stmt];
  3676. zipcode = [self textAtColumn:6 statement:stmt];
  3677. state = [self textAtColumn:7 statement:stmt]; // state code
  3678. city = [self textAtColumn:8 statement:stmt];
  3679. firt_name = [self textAtColumn:9 statement:stmt];
  3680. last_name = [self textAtColumn:10 statement:stmt];
  3681. phone = [self textAtColumn:11 statement:stmt];
  3682. fax = [self textAtColumn:12 statement:stmt];
  3683. email = [self textAtColumn:13 statement:stmt];
  3684. notes = [self textAtColumn:14 statement:stmt];
  3685. price_type = [self textAtColumn:15 statement:stmt]; // name
  3686. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  3687. img_0 = [self textAtColumn:17 statement:stmt];
  3688. img_1 = [self textAtColumn:18 statement:stmt];
  3689. img_2 = [self textAtColumn:19 statement:stmt];
  3690. }];
  3691. // decrypt
  3692. if (company_name) {
  3693. company_name = [AESCrypt fastdecrypt:company_name];
  3694. }
  3695. if (addr_1) {
  3696. addr_1 = [AESCrypt fastdecrypt:addr_1];
  3697. }
  3698. if (phone) {
  3699. phone = [AESCrypt fastdecrypt:phone];
  3700. }
  3701. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  3702. countryCode = [iSalesDB jk_queryText:countrySql];
  3703. stateCode = state;
  3704. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  3705. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  3706. NSString *code_id = params[@"country"];
  3707. countryCode_id = code_id;
  3708. countryCode = [self countryCodeByid:code_id];
  3709. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  3710. NSString *zip_code = params[@"zipcode"];
  3711. // 剔除全部为空格
  3712. int spaceCount = 0;
  3713. for (int i = 0; i < zip_code.length; i++) {
  3714. if ([zip_code characterAtIndex:i] == ' ') {
  3715. spaceCount++;
  3716. }
  3717. }
  3718. if (spaceCount == zip_code.length) {
  3719. zip_code = @"";
  3720. }
  3721. if (zipcode.length > 0) {
  3722. zipcode = zip_code;
  3723. countryCode_id = params[@"country"];
  3724. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  3725. countryCode = [dic valueForKey:@"country_code"];
  3726. stateCode = [dic valueForKey:@"state_code"];
  3727. city = [dic valueForKey:@"city"];
  3728. // zip code
  3729. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  3730. [zipDic setValue:zipcode forKey:@"value"];
  3731. [section_0 setValue:zipDic forKey:@"item_8"];
  3732. }
  3733. }
  3734. }
  3735. // 0 Country
  3736. // 1 Company Name
  3737. // 2 Contact ID
  3738. // 3 Picture
  3739. // 4 Address 1
  3740. // 5 Address 2
  3741. // 6 Address 3
  3742. // 7 Address 4
  3743. // 8 Zip Code
  3744. // 9 State/Province
  3745. // 10 City
  3746. // 11 Contact First Name
  3747. // 12 Contact Last Name
  3748. // 13 Phone
  3749. // 14 Fax
  3750. // 15 Email
  3751. // 16 Contact Notes
  3752. // 17 Price Type
  3753. // 18 Sales Rep
  3754. // country
  3755. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  3756. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  3757. // company
  3758. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  3759. // contact_id
  3760. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  3761. // picture
  3762. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  3763. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  3764. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  3765. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  3766. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  3767. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  3768. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  3769. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  3770. // addr 1 2 3 4
  3771. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  3772. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  3773. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  3774. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  3775. // zip code
  3776. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  3777. // state
  3778. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  3779. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  3780. // city
  3781. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  3782. // first last
  3783. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  3784. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  3785. // phone fax email
  3786. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  3787. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  3788. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  3789. // notes
  3790. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  3791. // price
  3792. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  3793. for (NSString *key in priceDic.allKeys) {
  3794. if ([key containsString:@"val_"]) {
  3795. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  3796. if ([dic[@"value"] isEqualToString:price_type]) {
  3797. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  3798. [priceDic setValue:dic forKey:key];
  3799. }
  3800. }
  3801. }
  3802. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  3803. // Sales Rep
  3804. NSMutableDictionary *repDic = [[self offline_getSalesRep] mutableCopy];
  3805. for (NSString *key in repDic.allKeys) {
  3806. if ([key containsString:@"val_"]) {
  3807. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  3808. NSString *value = dic[@"value"];
  3809. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  3810. if (code && [code isEqualToString:sales_rep]) {
  3811. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  3812. [repDic setValue:dic forKey:key];
  3813. }
  3814. }
  3815. }
  3816. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  3817. [ret setValue:section_0 forKey:@"section_0"];
  3818. return [RAUtils dict2data:ret];
  3819. }
  3820. #pragma mark save contact
  3821. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  3822. {
  3823. return [self offline_saveContact:params update:YES isCustomer:YES];
  3824. }
  3825. #pragma mark category
  3826. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  3827. if (ck) {
  3828. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  3829. for (NSString *key in res.allKeys) {
  3830. if (![key isEqualToString:@"count"]) {
  3831. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  3832. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  3833. if ([val[@"value"] isEqualToString:ck]) {
  3834. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  3835. }
  3836. [res setValue:val forKey:key];
  3837. }
  3838. }
  3839. [dic setValue:res forKey:valueKey];
  3840. }
  3841. }
  3842. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  3843. // NSString* orderCode = [params valueForKey:@"orderCode"];
  3844. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  3845. NSString* category = [params valueForKey:@"category"];
  3846. if (!category || [category isEqualToString:@""]) {
  3847. category = @"%";
  3848. }
  3849. category = [self translateSingleQuote:category];
  3850. int limit = [[params valueForKey:@"limit"] intValue];
  3851. int offset = [[params valueForKey:@"offset"] intValue];
  3852. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3853. NSString *limit_str = @"";
  3854. if (limited) {
  3855. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  3856. }
  3857. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  3858. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  3859. sqlite3 *db = [iSalesDB get_db];
  3860. // [iSalesDB AddExFunction:db];
  3861. int count;
  3862. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  3863. 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];
  3864. double price_min = 0;
  3865. double price_max = 0;
  3866. if ([params.allKeys containsObject:@"alert"]) {
  3867. // alert
  3868. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  3869. NSString *alert = params[@"alert"];
  3870. if ([alert isEqualToString:@"Display All"]) {
  3871. alert = [NSString stringWithFormat:@""];
  3872. } else {
  3873. alert = [self translateSingleQuote:alert];
  3874. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  3875. }
  3876. // available
  3877. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  3878. NSString *available = params[@"available"];
  3879. NSString *available_condition;
  3880. if ([available isEqualToString:@"Display All"]) {
  3881. available_condition = @"";
  3882. } else if ([available isEqualToString:@"Available Now"]) {
  3883. available_condition = @"and availability > 0";
  3884. } else {
  3885. available_condition = @"and availability == 0";
  3886. }
  3887. // best seller
  3888. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  3889. NSString *best_seller = @"";
  3890. NSString *order_best_seller = @"m.name asc";
  3891. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  3892. best_seller = @"and best_seller > 0";
  3893. order_best_seller = @"m.best_seller desc,m.name asc";
  3894. }
  3895. // price
  3896. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  3897. NSString *price = params[@"price"];
  3898. price_min = 0;
  3899. price_max = MAXFLOAT;
  3900. if (appDelegate.user) {
  3901. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  3902. NSMutableString *priceName = [NSMutableString string];
  3903. for (int i = 0; i < priceTypeArray.count; i++) {
  3904. NSString *pricetype = priceTypeArray[i];
  3905. pricetype = [self translateSingleQuote:pricetype];
  3906. if (i == 0) {
  3907. [priceName appendFormat:@"'%@'",pricetype];
  3908. } else {
  3909. [priceName appendFormat:@",'%@'",pricetype];
  3910. }
  3911. }
  3912. if ([price isEqualToString:@"Display All"]) {
  3913. price = [NSString stringWithFormat:@""];
  3914. } else if([price containsString:@"+"]){
  3915. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  3916. price_min = [price doubleValue];
  3917. 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];
  3918. } else {
  3919. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  3920. price_min = [[priceArray objectAtIndex:0] doubleValue];
  3921. price_max = [[priceArray objectAtIndex:1] doubleValue];
  3922. 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];
  3923. }
  3924. } else {
  3925. price = @"";
  3926. }
  3927. // sold_by_qty : Sold in quantities of %@
  3928. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  3929. NSString *qty = params[@"sold_by_qty"];
  3930. if ([qty isEqualToString:@"Display All"]) {
  3931. qty = @"";
  3932. } else {
  3933. qty = [self translateSingleQuote:qty];
  3934. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  3935. }
  3936. // cate
  3937. category = [self translateSingleQuote:category];
  3938. NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  3939. // where bestseller > 0 order by bestseller desc
  3940. // sql query: alert availability(int) best_seller(int) price qty
  3941. 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];
  3942. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  3943. }
  3944. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  3945. if (!appDelegate.user) {
  3946. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  3947. }
  3948. [ret setValue:filter forKey:@"filter"];
  3949. DebugLog(@"offline_category sql:%@",sqlQuery);
  3950. sqlite3_stmt * statement;
  3951. [ret setValue:@"2" forKey:@"result"];
  3952. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  3953. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  3954. // int count=0;
  3955. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3956. {
  3957. int i=0;
  3958. while (sqlite3_step(statement) == SQLITE_ROW)
  3959. {
  3960. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3961. char *name = (char*)sqlite3_column_text(statement, 0);
  3962. if(name==nil)
  3963. name="";
  3964. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3965. char *description = (char*)sqlite3_column_text(statement, 1);
  3966. if(description==nil)
  3967. description="";
  3968. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3969. int product_id = sqlite3_column_int(statement, 2);
  3970. int wid = sqlite3_column_int(statement, 3);
  3971. int closeout = sqlite3_column_int(statement, 4);
  3972. int cid = sqlite3_column_int(statement, 5);
  3973. int wisdelete = sqlite3_column_int(statement, 6);
  3974. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  3975. if(wid !=0 && wisdelete != 1)
  3976. [item setValue:@"true" forKey:@"wish_exists"];
  3977. else
  3978. [item setValue:@"false" forKey:@"wish_exists"];
  3979. if(closeout==0)
  3980. [item setValue:@"false" forKey:@"is_closeout"];
  3981. else
  3982. [item setValue:@"true" forKey:@"is_closeout"];
  3983. if(cid==0)
  3984. [item setValue:@"false" forKey:@"cart_exists"];
  3985. else
  3986. [item setValue:@"true" forKey:@"cart_exists"];
  3987. [item addEntriesFromDictionary:imgjson];
  3988. // [item setValue:nsurl forKey:@"img"];
  3989. [item setValue:nsname forKey:@"name"];
  3990. [item setValue:nsdescription forKey:@"description"];
  3991. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3992. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3993. i++;
  3994. }
  3995. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3996. [ret setObject:items forKey:@"items"];
  3997. sqlite3_finalize(statement);
  3998. } else {
  3999. DebugLog(@"nothing...");
  4000. }
  4001. DebugLog(@"count:%d",count);
  4002. [iSalesDB close_db:db];
  4003. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  4004. return ret;
  4005. }
  4006. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  4007. {
  4008. return [self categoryList:params limited:YES];
  4009. }
  4010. # pragma mark item search
  4011. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  4012. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4013. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  4014. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  4015. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  4016. // category
  4017. NSDictionary *category_menu = [self offline_category_menu];
  4018. [filter setValue:category_menu forKey:@"category"];
  4019. NSString* where= nil;
  4020. NSString* orderby= @"m.name";
  4021. if (!filterSearch) {
  4022. int covertype = [[params valueForKey:@"covertype"] intValue];
  4023. switch (covertype) {
  4024. case 0:
  4025. {
  4026. where=@"m.category like'%%#005#%%'";
  4027. break;
  4028. }
  4029. case 1:
  4030. {
  4031. where=@"m.alert like '%QS%'";
  4032. break;
  4033. }
  4034. case 2:
  4035. {
  4036. where=@"m.availability>0";
  4037. break;
  4038. }
  4039. case 3:
  4040. {
  4041. where=@"m.best_seller>0";
  4042. orderby=@"m.best_seller desc,m.name asc";
  4043. break;
  4044. }
  4045. default:
  4046. where=@"1=1";
  4047. break;
  4048. }
  4049. }
  4050. int limit = [[params valueForKey:@"limit"] intValue];
  4051. int offset = [[params valueForKey:@"offset"] intValue];
  4052. NSString *limit_str = @"";
  4053. if (limited) {
  4054. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  4055. }
  4056. sqlite3 *db = [iSalesDB get_db];
  4057. // [iSalesDB AddExFunction:db];
  4058. int count;
  4059. NSString *sqlQuery = nil;
  4060. where = [where stringByAppendingString:@" and m.is_active = 1"];
  4061. 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];
  4062. double price_min = 0;
  4063. double price_max = 0;
  4064. if (filterSearch) {
  4065. // alert
  4066. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  4067. NSString *alert = params[@"alert"];
  4068. if ([alert isEqualToString:@"Display All"]) {
  4069. alert = [NSString stringWithFormat:@""];
  4070. } else {
  4071. alert = [self translateSingleQuote:alert];
  4072. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  4073. }
  4074. // available
  4075. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  4076. NSString *available = params[@"available"];
  4077. NSString *available_condition;
  4078. if ([available isEqualToString:@"Display All"]) {
  4079. available_condition = @"";
  4080. } else if ([available isEqualToString:@"Available Now"]) {
  4081. available_condition = @"and availability > 0";
  4082. } else {
  4083. available_condition = @"and availability == 0";
  4084. }
  4085. // best seller
  4086. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  4087. NSString *best_seller = @"";
  4088. NSString *order_best_seller = @"m.name asc";
  4089. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  4090. best_seller = @"and best_seller > 0";
  4091. order_best_seller = @"m.best_seller desc,m.name asc";
  4092. }
  4093. // price
  4094. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  4095. NSString *price = params[@"price"];
  4096. price_min = 0;
  4097. price_max = MAXFLOAT;
  4098. if (appDelegate.user) {
  4099. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  4100. NSMutableString *priceName = [NSMutableString string];
  4101. for (int i = 0; i < priceTypeArray.count; i++) {
  4102. NSString *pricetype = priceTypeArray[i];
  4103. pricetype = [self translateSingleQuote:pricetype];
  4104. if (i == 0) {
  4105. [priceName appendFormat:@"'%@'",pricetype];
  4106. } else {
  4107. [priceName appendFormat:@",'%@'",pricetype];
  4108. }
  4109. }
  4110. if ([price isEqualToString:@"Display All"]) {
  4111. price = [NSString stringWithFormat:@""];
  4112. } else if([price containsString:@"+"]){
  4113. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  4114. price_min = [price doubleValue];
  4115. 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];
  4116. } else {
  4117. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  4118. price_min = [[priceArray objectAtIndex:0] doubleValue];
  4119. price_max = [[priceArray objectAtIndex:1] doubleValue];
  4120. 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];
  4121. }
  4122. } else {
  4123. price = @"";
  4124. }
  4125. // sold_by_qty : Sold in quantities of %@
  4126. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  4127. NSString *qty = params[@"sold_by_qty"];
  4128. if ([qty isEqualToString:@"Display All"]) {
  4129. qty = @"";
  4130. } else {
  4131. qty = [self translateSingleQuote:qty];
  4132. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  4133. }
  4134. // category
  4135. NSString *category_id = params[@"ctgId"];
  4136. NSMutableArray *cate_id_array = nil;
  4137. NSMutableString *cateWhere = [NSMutableString string];
  4138. if ([category_id isEqualToString:@""] || !category_id) {
  4139. [cateWhere appendString:@"1 = 1"];
  4140. } else {
  4141. if ([category_id containsString:@","]) {
  4142. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  4143. } else {
  4144. cate_id_array = [@[category_id] mutableCopy];
  4145. }
  4146. /*-----------*/
  4147. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];
  4148. for (int i = 0; i < cate_id_array.count; i++) {
  4149. for (NSString *key0 in cateDic.allKeys) {
  4150. if ([key0 containsString:@"category_"]) {
  4151. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy];
  4152. for (NSString *key1 in category0.allKeys) {
  4153. if ([key1 containsString:@"category_"]) {
  4154. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy];
  4155. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  4156. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  4157. cate_id_array[i] = [category1 objectForKey:@"id"];
  4158. if (i == 0) {
  4159. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  4160. } else {
  4161. [cateWhere appendFormat:@"or category like'%%#%@#%%'",cate_id_array[i]];
  4162. }
  4163. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  4164. [category0 setValue:category1 forKey:key1];
  4165. [cateDic setValue:category0 forKey:key0];
  4166. }
  4167. }
  4168. }
  4169. }
  4170. }
  4171. }
  4172. [filter setValue:cateDic forKey:@"category"];
  4173. }
  4174. // where bestseller > 0 order by bestseller desc
  4175. // sql query: alert availability(int) best_seller(int) price qty
  4176. 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];
  4177. // count
  4178. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  4179. }
  4180. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  4181. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  4182. if (!appDelegate.user) {
  4183. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  4184. }
  4185. [ret setValue:filter forKey:@"filter"];
  4186. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  4187. sqlite3_stmt * statement;
  4188. [ret setValue:@"2" forKey:@"result"];
  4189. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  4190. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  4191. // int count=0;
  4192. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4193. {
  4194. int i=0;
  4195. while (sqlite3_step(statement) == SQLITE_ROW)
  4196. {
  4197. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4198. char *name = (char*)sqlite3_column_text(statement, 0);
  4199. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  4200. char *description = (char*)sqlite3_column_text(statement, 1);
  4201. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  4202. int product_id = sqlite3_column_int(statement, 2);
  4203. int wid = sqlite3_column_int(statement, 3);
  4204. int closeout = sqlite3_column_int(statement, 4);
  4205. int cid = sqlite3_column_int(statement, 5);
  4206. int wisdelete = sqlite3_column_int(statement, 6);
  4207. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  4208. if(wid !=0 && wisdelete != 1)
  4209. [item setValue:@"true" forKey:@"wish_exists"];
  4210. else
  4211. [item setValue:@"false" forKey:@"wish_exists"];
  4212. if(closeout==0)
  4213. [item setValue:@"false" forKey:@"is_closeout"];
  4214. else
  4215. [item setValue:@"true" forKey:@"is_closeout"];
  4216. if(cid==0)
  4217. [item setValue:@"false" forKey:@"cart_exists"];
  4218. else
  4219. [item setValue:@"true" forKey:@"cart_exists"];
  4220. [item addEntriesFromDictionary:imgjson];
  4221. // [item setValue:nsurl forKey:@"img"];
  4222. [item setValue:nsname forKey:@"fash_name"];
  4223. [item setValue:nsdescription forKey:@"description"];
  4224. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  4225. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4226. i++;
  4227. }
  4228. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4229. [ret setObject:items forKey:@"items"];
  4230. sqlite3_finalize(statement);
  4231. }
  4232. [iSalesDB close_db:db];
  4233. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  4234. return ret;
  4235. }
  4236. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  4237. {
  4238. return [self itemsearch:params limited:YES];
  4239. }
  4240. #pragma mark order detail
  4241. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  4242. if (str1.length == 0) {
  4243. str1 = @"&nbsp";
  4244. }
  4245. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  4246. return str;
  4247. }
  4248. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  4249. {
  4250. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  4251. [fromformatter setDateFormat:from];
  4252. NSDate *date = [fromformatter dateFromString:datetime];
  4253. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  4254. [toformatter setDateFormat:to];
  4255. NSString * ret = [toformatter stringFromDate:date];
  4256. return ret;
  4257. }
  4258. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  4259. // 把毫秒去掉
  4260. if ([dateTime containsString:@"."]) {
  4261. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  4262. }
  4263. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  4264. formatter.dateFormat = formate;
  4265. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  4266. NSDate *date = [formatter dateFromString:dateTime];
  4267. formatter.dateFormat = newFormate;
  4268. NSString *result = [formatter stringFromDate:date];
  4269. return result ? result : @"";
  4270. }
  4271. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  4272. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  4273. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  4274. }
  4275. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  4276. {
  4277. DebugLog(@"offline oderdetail params: %@",params);
  4278. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4279. int orderId = [params[@"orderId"] intValue];
  4280. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  4281. // decrypt card number and card security code
  4282. // 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 ];
  4283. 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];
  4284. sqlite3 *db = [iSalesDB get_db];
  4285. sqlite3_stmt * statement;
  4286. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  4287. NSString *nssoid = @"";
  4288. NSString* orderinfo = @"";
  4289. NSString *moreInfo = @"";
  4290. double handlingFee = 0;
  4291. double payments_and_credist = 0;
  4292. double totalPrice = 0;
  4293. double shippingFee = 0;
  4294. double lift_gate = 0;
  4295. NSString *customer_contact = @"";
  4296. NSString *customer_email = @"";
  4297. NSString *customer_fax = @"";
  4298. NSString *customer_phone = @"";
  4299. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  4300. {
  4301. if (sqlite3_step(statement) == SQLITE_ROW)
  4302. {
  4303. int order_id = sqlite3_column_int(statement, 0);
  4304. NSString *sign_url = [self textAtColumn:51 statement:statement];
  4305. ret[@"sign_url"] = sign_url;
  4306. customer_contact = [self textAtColumn:52 statement:statement];
  4307. customer_email = [self textAtColumn:53 statement:statement];
  4308. customer_phone = [self textAtColumn:54 statement:statement];
  4309. customer_fax = [self textAtColumn:55 statement:statement];
  4310. int offline_edit=sqlite3_column_int(statement, 56);
  4311. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  4312. char *soid = (char*)sqlite3_column_text(statement, 1);
  4313. if(soid==nil)
  4314. soid= "";
  4315. nssoid= [[NSString alloc]initWithUTF8String:soid];
  4316. // so#
  4317. ret[@"so#"] = nssoid;
  4318. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  4319. if(poNumber==nil)
  4320. poNumber= "";
  4321. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  4322. char *create_time = (char*)sqlite3_column_text(statement, 3);
  4323. if(create_time==nil)
  4324. create_time= "";
  4325. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  4326. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  4327. int status = sqlite3_column_int(statement, 4);
  4328. int erpStatus = sqlite3_column_int(statement, 49);
  4329. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  4330. // status
  4331. if (status > 1 && status != 3) {
  4332. status = erpStatus;
  4333. } else if (status == 3) {
  4334. status = 15;
  4335. }
  4336. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  4337. ret[@"order_status"] = nsstatus;
  4338. char *company_name = (char*)sqlite3_column_text(statement, 5);
  4339. if(company_name==nil)
  4340. company_name= "";
  4341. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  4342. // company name
  4343. ret[@"company_name"] = nscompany_name;
  4344. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  4345. if(customer_contact==nil)
  4346. customer_contact= "";
  4347. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  4348. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  4349. if(addr_1==nil)
  4350. addr_1="";
  4351. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4352. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  4353. if(addr_2==nil)
  4354. addr_2="";
  4355. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4356. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  4357. if(addr_3==nil)
  4358. addr_3="";
  4359. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4360. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  4361. if(addr_4==nil)
  4362. addr_4="";
  4363. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4364. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  4365. [arr_addr addObject:nsaddr_1];
  4366. [arr_addr addObject:nsaddr_2];
  4367. [arr_addr addObject:nsaddr_3];
  4368. [arr_addr addObject:nsaddr_4];
  4369. NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  4370. char *logist = (char*)sqlite3_column_text(statement, 11);
  4371. if(logist==nil)
  4372. logist= "";
  4373. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  4374. if (status == -11 || status == 10 || status == 11) {
  4375. nslogist = [self textAtColumn:59 statement:statement];
  4376. };
  4377. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  4378. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  4379. shipping = @"Shipping To Be Quoted";
  4380. } else {
  4381. shippingFee = sqlite3_column_double(statement, 12);
  4382. }
  4383. // Shipping
  4384. ret[@"Shipping"] = shipping;
  4385. int have_lift_gate = sqlite3_column_int(statement, 17);
  4386. lift_gate = sqlite3_column_double(statement, 13);
  4387. // Liftgate Fee(No loading dock)
  4388. if (!have_lift_gate) {
  4389. lift_gate = 0;
  4390. }
  4391. ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  4392. if (sqlite3_column_int(statement, 57)) {
  4393. ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  4394. }
  4395. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  4396. if(general_notes==nil)
  4397. general_notes= "";
  4398. NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  4399. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  4400. if(internal_notes==nil)
  4401. internal_notes= "";
  4402. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  4403. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  4404. if(payment_type==nil)
  4405. payment_type= "";
  4406. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  4407. // order info
  4408. orderinfo = [self textFileName:@"order_info.html"];
  4409. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  4410. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  4411. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  4412. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  4413. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  4414. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  4415. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  4416. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  4417. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  4418. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  4419. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  4420. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  4421. NSString *payment = nil;
  4422. if([nspayment_type isEqualToString:@"Credit Card"])
  4423. {
  4424. payment = [self textFileName:@"creditcardpayment.html"];
  4425. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  4426. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  4427. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  4428. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  4429. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  4430. NSString *card_type = [self textAtColumn:42 statement:statement];
  4431. if (card_type.length > 0) { // 显示星号
  4432. card_type = @"****";
  4433. }
  4434. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  4435. if (card_number.length > 0 && card_number.length > 4) {
  4436. for (int i = 0; i < card_number.length - 4; i++) {
  4437. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  4438. }
  4439. } else {
  4440. card_number = @"";
  4441. }
  4442. NSString *card_security_code = [self textAtColumn:44 statement:statement];
  4443. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  4444. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  4445. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  4446. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  4447. card_expiration = @"****";
  4448. }
  4449. NSString *card_city = [self textAtColumn:46 statement:statement];
  4450. NSString *card_state = [self textAtColumn:47 statement:statement];
  4451. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  4452. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  4453. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  4454. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  4455. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  4456. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  4457. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  4458. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  4459. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  4460. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  4461. }
  4462. else
  4463. {
  4464. payment=[self textFileName:@"normalpayment.html"];
  4465. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  4466. }
  4467. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  4468. ret[@"result"]= [NSNumber numberWithInt:2];
  4469. // more info
  4470. moreInfo = [self textFileName:@"more_info.html"];
  4471. /*****ship to******/
  4472. // ShipToCompany_or_&nbsp
  4473. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  4474. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  4475. NSString *shipToName = [self textAtColumn:19 statement:statement];
  4476. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  4477. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  4478. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  4479. /*****ship from******/
  4480. // ShipFromCompany_or_&nbsp
  4481. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  4482. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  4483. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  4484. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  4485. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  4486. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  4487. /*****freight to******/
  4488. // FreightBillToCompany_or_&nbsp
  4489. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  4490. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  4491. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  4492. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  4493. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  4494. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  4495. /*****merchandise to******/
  4496. // MerchandiseBillToCompany_or_&nbsp
  4497. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  4498. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  4499. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  4500. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  4501. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  4502. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  4503. /*****return to******/
  4504. // ReturnToCompany_or_&nbsp
  4505. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  4506. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  4507. NSString *returnToName = [self textAtColumn:31 statement:statement];
  4508. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  4509. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  4510. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  4511. //
  4512. // DebugLog(@"more info : %@",moreInfo);
  4513. // handling fee
  4514. handlingFee = sqlite3_column_double(statement, 33);
  4515. ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  4516. if (sqlite3_column_int(statement, 58)) {
  4517. ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  4518. }
  4519. //
  4520. // customer info
  4521. customerID = [self textAtColumn:36 statement:statement];
  4522. // mode
  4523. ret[@"mode"] = appDelegate.mode;
  4524. // model_count
  4525. ret[@"model_count"] = @(0);
  4526. }
  4527. sqlite3_finalize(statement);
  4528. }
  4529. [iSalesDB close_db:db];
  4530. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  4531. // "customer_email" = "Shui Hu";
  4532. // "customer_fax" = "";
  4533. // "customer_first_name" = F;
  4534. // "customer_last_name" = L;
  4535. // "customer_name" = ",da He Xiang Dong Liu A";
  4536. // "customer_phone" = "Hey Xuan Feng";
  4537. contactInfo[@"customer_phone"] = customer_phone;
  4538. contactInfo[@"customer_fax"] = customer_fax;
  4539. contactInfo[@"customer_email"] = customer_email;
  4540. NSString *first_name = @"";
  4541. NSString *last_name = @"";
  4542. if ([customer_contact isEqualToString:@""]) {
  4543. } else if ([customer_contact containsString:@" "]) {
  4544. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  4545. first_name = [customer_contact substringToIndex:first_space_index];
  4546. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  4547. }
  4548. contactInfo[@"customer_first_name"] = first_name;
  4549. contactInfo[@"customer_last_name"] = last_name;
  4550. ret[@"customerInfo"] = contactInfo;
  4551. // models
  4552. if (nssoid) {
  4553. __block double TotalCuft = 0;
  4554. __block double TotalWeight = 0;
  4555. __block int TotalCarton = 0;
  4556. __block double allItemPrice = 0;
  4557. 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];
  4558. sqlite3 *db1 = [iSalesDB get_db];
  4559. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4560. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  4561. int product_id = sqlite3_column_int(stmt, 0);
  4562. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  4563. int item_id = sqlite3_column_int(stmt, 7);
  4564. NSString* Price=nil;
  4565. if(str_price==nil)
  4566. {
  4567. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  4568. if(price==nil)
  4569. Price=@"No Price.";
  4570. else
  4571. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  4572. }
  4573. else
  4574. {
  4575. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  4576. }
  4577. double discount = sqlite3_column_double(stmt, 2);
  4578. int item_count = sqlite3_column_int(stmt, 3);
  4579. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  4580. NSString *nsline_note=nil;
  4581. if(line_note!=nil)
  4582. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  4583. // char *name = (char*)sqlite3_column_text(stmt, 5);
  4584. // NSString *nsname = nil;
  4585. // if(name!=nil)
  4586. // nsname= [[NSString alloc]initWithUTF8String:name];
  4587. NSString *nsname = [self textAtColumn:5 statement:stmt];
  4588. // char *description = (char*)sqlite3_column_text(stmt, 6);
  4589. // NSString *nsdescription=nil;
  4590. // if(description!=nil)
  4591. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  4592. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  4593. // int stockUom = sqlite3_column_int(stmt, 8);
  4594. // int _id = sqlite3_column_int(stmt, 9);
  4595. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  4596. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  4597. double weight=[bsubtotaljson[@"weight"] doubleValue];
  4598. int carton=[bsubtotaljson[@"carton"] intValue];
  4599. TotalCuft += cuft;
  4600. TotalWeight += weight;
  4601. TotalCarton += carton;
  4602. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  4603. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  4604. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  4605. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  4606. itemjson[@"note"]=nsline_note;
  4607. itemjson[@"origin_price"] = Price;
  4608. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  4609. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  4610. itemjson[@"order_item_status"] = @""; // 暂时不处理
  4611. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  4612. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  4613. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  4614. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  4615. if(itemjson[@"combine"] != nil)
  4616. {
  4617. // int citem=0;
  4618. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  4619. for(int bc=0;bc<bcount;bc++)
  4620. {
  4621. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  4622. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  4623. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  4624. subTotal += uprice * modulus * item_count;
  4625. }
  4626. }
  4627. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  4628. // NSLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  4629. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  4630. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  4631. allItemPrice += subTotal;
  4632. }];
  4633. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  4634. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  4635. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  4636. // payments/Credits
  4637. // payments_and_credist = sqlite3_column_double(statement, 34);
  4638. payments_and_credist = allItemPrice;
  4639. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  4640. // // total
  4641. // totalPrice = sqlite3_column_double(statement, 35);
  4642. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  4643. } else {
  4644. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  4645. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  4646. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  4647. // payments/Credits
  4648. payments_and_credist = 0;
  4649. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  4650. }
  4651. // total
  4652. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  4653. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  4654. ret[@"order_info"]= orderinfo;
  4655. ret[@"more_order_info"] = moreInfo;
  4656. return [RAUtils dict2data:ret];
  4657. }
  4658. #pragma mark order list
  4659. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  4660. NSString *nsstatus = @"";
  4661. switch (status) {
  4662. case 0:
  4663. {
  4664. nsstatus=@"Temp Order";
  4665. break;
  4666. }
  4667. case 1:
  4668. {
  4669. nsstatus=@"Saved Order";
  4670. break;
  4671. }
  4672. case 2: {
  4673. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  4674. break;
  4675. }
  4676. case 3:
  4677. case 15:
  4678. {
  4679. nsstatus=@"Cancelled";
  4680. break;
  4681. }
  4682. case 10:
  4683. {
  4684. nsstatus=@"Quote Submitted";
  4685. break;
  4686. }
  4687. case 11:
  4688. {
  4689. nsstatus=@"Sales Order Submitted";
  4690. break;
  4691. }
  4692. case 12:
  4693. {
  4694. nsstatus=@"Processing";
  4695. break;
  4696. }
  4697. case 13:
  4698. {
  4699. nsstatus=@"Shipped";
  4700. break;
  4701. }
  4702. case 14:
  4703. {
  4704. nsstatus=@"Closed";
  4705. break;
  4706. }
  4707. case -11:
  4708. {
  4709. nsstatus = @"Ready For Submit";
  4710. break;
  4711. }
  4712. default:
  4713. break;
  4714. }
  4715. return nsstatus;
  4716. }
  4717. + (double) orderTotalPrice:(NSString *)so_id db:(sqlite3 *)db1 close:(BOOL)close{
  4718. double total = 0;
  4719. __block double payments_and_credist = 0;
  4720. __block double allItemPrice = 0;
  4721. // sqlite3 *db1 = [iSalesDB get_db];
  4722. if (so_id) {
  4723. // 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];
  4724. 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];
  4725. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4726. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4727. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  4728. int product_id = sqlite3_column_int(stmt, 0);
  4729. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  4730. int discount = sqlite3_column_int(stmt, 2);
  4731. int item_count = sqlite3_column_int(stmt, 3);
  4732. // int item_id = sqlite3_column_int(stmt, 7);
  4733. int item_id = sqlite3_column_int(stmt, 4);
  4734. NSString* Price=nil;
  4735. if(str_price==nil)
  4736. {
  4737. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  4738. if(price==nil)
  4739. Price=@"No Price.";
  4740. else
  4741. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  4742. }
  4743. else
  4744. {
  4745. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  4746. }
  4747. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  4748. itemjson[@"The unit price"]=Price;
  4749. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  4750. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  4751. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  4752. if(itemjson[@"combine"] != nil)
  4753. {
  4754. // int citem=0;
  4755. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  4756. for(int bc=0;bc<bcount;bc++)
  4757. {
  4758. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  4759. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  4760. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  4761. subTotal += uprice * modulus * item_count;
  4762. }
  4763. }
  4764. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  4765. allItemPrice += subTotal;
  4766. }];
  4767. payments_and_credist = allItemPrice;
  4768. } else {
  4769. // payments/Credits
  4770. payments_and_credist = 0;
  4771. }
  4772. // lift_gate handlingFee shippingFee
  4773. __block double lift_gate = 0;
  4774. __block double handlingFee = 0;
  4775. __block double shippingFee = 0;
  4776. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  4777. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4778. int have_lift_gate = sqlite3_column_int(stmt, 0);
  4779. if (have_lift_gate) {
  4780. lift_gate = sqlite3_column_double(stmt, 1);
  4781. }
  4782. handlingFee = sqlite3_column_double(stmt, 2);
  4783. shippingFee = sqlite3_column_double(stmt, 3);
  4784. }];
  4785. // total
  4786. total = payments_and_credist + lift_gate + handlingFee + shippingFee;
  4787. if (close) {
  4788. [iSalesDB close_db:db1];
  4789. }
  4790. return total;
  4791. }
  4792. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  4793. {
  4794. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4795. int limit = [[params valueForKey:@"limit"] intValue];
  4796. int offset = [[params valueForKey:@"offset"] intValue];
  4797. NSString* keyword = [params valueForKey:@"keyWord"];
  4798. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  4799. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  4800. NSString* where=@"1 = 1";
  4801. if(keyword.length>0)
  4802. 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]];
  4803. if (orderStatus.length > 0) {
  4804. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  4805. if (order_status_array.count == 1) {
  4806. int status_value = [[order_status_array firstObject] integerValue];
  4807. if (status_value <= 1 || status_value == 3) {
  4808. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  4809. } else {
  4810. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  4811. }
  4812. } else if (order_status_array.count > 1) {
  4813. for (int i = 0; i < order_status_array.count;i++) {
  4814. NSString *status = order_status_array[i];
  4815. NSString *condition = @" or";
  4816. if (i == 0) {
  4817. condition = @" and (";
  4818. }
  4819. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  4820. int status_value = [status integerValue];
  4821. if (status_value <= 1 || status_value == 3) {
  4822. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  4823. } else {
  4824. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  4825. }
  4826. }
  4827. where = [where stringByAppendingString:@" )"];
  4828. }
  4829. }
  4830. 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];
  4831. // DebugLog(@"order list sql: %@",sqlQuery);
  4832. sqlite3 *db = [iSalesDB get_db];
  4833. sqlite3_stmt * statement;
  4834. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  4835. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4836. {
  4837. int count=0;
  4838. while (sqlite3_step(statement) == SQLITE_ROW)
  4839. {
  4840. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  4841. int order_id = sqlite3_column_double(statement, 0);
  4842. char *soid = (char*)sqlite3_column_text(statement, 1);
  4843. if(soid==nil)
  4844. soid= "";
  4845. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  4846. int status = sqlite3_column_double(statement, 2);
  4847. int erpStatus = sqlite3_column_double(statement, 9);
  4848. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  4849. if(sales_rep==nil)
  4850. sales_rep= "";
  4851. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  4852. char *create_by = (char*)sqlite3_column_text(statement, 4);
  4853. if(create_by==nil)
  4854. create_by= "";
  4855. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  4856. char *company_name = (char*)sqlite3_column_text(statement, 5);
  4857. if(company_name==nil)
  4858. company_name= "";
  4859. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  4860. char *create_time = (char*)sqlite3_column_text(statement, 6);
  4861. if(create_time==nil)
  4862. create_time= "";
  4863. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  4864. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  4865. // double total_price = sqlite3_column_double(statement, 7);
  4866. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] db:db close:NO];
  4867. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  4868. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  4869. int offline_edit = sqlite3_column_int(statement, 10);
  4870. // ": "JH",
  4871. // "": "$8307.00",
  4872. // "": "MOB1608050001",
  4873. // "": "ArpithaT",
  4874. // "": "1st Stage Property Transformations",
  4875. // "": "JANICE SUTTON",
  4876. // "": 2255,
  4877. // "": "08/02/2016 09:49:18",
  4878. // "": 1,
  4879. // "": "Saved Order"
  4880. // "": "1470384050483",
  4881. // "model_count": "6 / 28"
  4882. item[@"sales_rep"]= nssales_rep;
  4883. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  4884. item[@"so#"]= nssoid;
  4885. item[@"create_by"]= nscreate_by;
  4886. item[@"customer_name"]= nscompany_name;
  4887. item[@"customer_contact"] = customer_contact;
  4888. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  4889. item[@"purchase_time"]= nscreate_time;
  4890. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",status];
  4891. item[@"order_status"]= nsstatus;
  4892. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  4893. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  4894. // item[@"model_count"]
  4895. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  4896. count++;
  4897. }
  4898. ret[@"count"]= [NSNumber numberWithInt:count];
  4899. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  4900. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  4901. ret[@"result"]= [NSNumber numberWithInt:2];
  4902. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  4903. ret[@"time_zone"] = @"PST";
  4904. sqlite3_finalize(statement);
  4905. }
  4906. 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];
  4907. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  4908. [iSalesDB execSql:@"END TRANSACTION" db:db];
  4909. [iSalesDB close_db:db];
  4910. return [RAUtils dict2data:ret];
  4911. }
  4912. #pragma mark update gnotes
  4913. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  4914. {
  4915. DebugLog(@"params: %@",params);
  4916. // comments = Meyoyoyoyoyoyoy;
  4917. // orderCode = MOB1608110001;
  4918. // password = 123456;
  4919. // user = EvanK;
  4920. 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]];
  4921. int ret = [iSalesDB execSql:sql];
  4922. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4923. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  4924. [dic setValue:@"Regular Mode" forKey:@"mode"];
  4925. // [dic setValue:@"160409" forKey:@"min_ver"];
  4926. return [RAUtils dict2data:dic];
  4927. }
  4928. #pragma mark move to wishlist
  4929. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  4930. {
  4931. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  4932. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  4933. _id = [NSString stringWithFormat:@"(%@)",_id];
  4934. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  4935. sqlite3 *db = [iSalesDB get_db];
  4936. __block NSString *product_id = @"";
  4937. __block NSString *item_count_str = @"";
  4938. // __block NSString *item_id = nil;
  4939. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4940. // product_id = [self textAtColumn:0 statement:stmt];
  4941. int item_count = sqlite3_column_int(stmt, 1);
  4942. // item_id = [self textAtColumn:2 statement:stmt];
  4943. NSString *p_id = [self textAtColumn:0 statement:stmt];
  4944. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  4945. if (p_id.length) {
  4946. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  4947. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  4948. }
  4949. }];
  4950. [iSalesDB close_db:db];
  4951. // 去除第一个,
  4952. product_id = [product_id substringFromIndex:1];
  4953. item_count_str = [item_count_str substringFromIndex:1];
  4954. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4955. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  4956. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  4957. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  4958. sqlite3 *db1 = [iSalesDB get_db];
  4959. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4960. [dic setValue:@"Regular Mode" forKey:@"mode"];
  4961. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  4962. // 删除
  4963. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  4964. int ret = [iSalesDB execSql:deleteSql db:db1];
  4965. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  4966. [iSalesDB close_db:db1];
  4967. return [RAUtils dict2data:dic];
  4968. }
  4969. #pragma mark cart delete
  4970. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  4971. {
  4972. // cartItemId = 548;
  4973. // orderCode = MOB1608110001;
  4974. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  4975. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  4976. _id = [NSString stringWithFormat:@"(%@)",_id];
  4977. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  4978. int ret = [iSalesDB execSql:sql];
  4979. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4980. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  4981. [dic setValue:@"Regular Mode" forKey:@"mode"];
  4982. // [dic setValue:@"160409" forKey:@"min_ver"];
  4983. return [RAUtils dict2data:dic];
  4984. }
  4985. #pragma mark set price
  4986. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  4987. {
  4988. DebugLog(@"cart set price params: %@",params);
  4989. // "cartitem_id" = 1;
  4990. // discount = "0.000000";
  4991. // "item_note" = "";
  4992. // price = "269.000000";
  4993. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  4994. NSString *notes = [self valueInParams:params key:@"item_note"];
  4995. NSString *discount = [self valueInParams:params key:@"discount"];
  4996. NSString *price = [self valueInParams:params key:@"price"];
  4997. // bool badd_price_changed=false;
  4998. // sqlite3* db=[iSalesDB get_db];
  4999. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5000. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  5001. // NSRange range;
  5002. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  5003. //
  5004. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  5005. // badd_price_changed=true;
  5006. // [iSalesDB close_db:db];
  5007. //
  5008. // if(badd_price_changed)
  5009. // {
  5010. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  5011. // }
  5012. //
  5013. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  5014. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  5015. int ret = [iSalesDB execSql:sql];
  5016. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5017. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5018. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5019. // [dic setValue:@"160409" forKey:@"min_ver"];
  5020. return [RAUtils dict2data:dic];
  5021. }
  5022. #pragma mark set line notes
  5023. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  5024. {
  5025. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  5026. NSString *notes = [self valueInParams:params key:@"notes"];
  5027. notes = [self translateSingleQuote:notes];
  5028. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  5029. int ret = [iSalesDB execSql:sql];
  5030. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5031. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5032. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5033. // [dic setValue:@"160409" forKey:@"min_ver"];
  5034. return [RAUtils dict2data:dic];
  5035. }
  5036. #pragma mark set qty
  5037. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  5038. {
  5039. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  5040. int item_count = [params[@"inputInt"] integerValue];
  5041. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  5042. int ret = [iSalesDB execSql:sql];
  5043. __block int item_id = 0;
  5044. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5045. item_id = sqlite3_column_int(stmt, 0);
  5046. }];
  5047. sqlite3 *db = [iSalesDB get_db];
  5048. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  5049. [iSalesDB close_db:db];
  5050. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5051. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5052. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5053. // [dic setValue:@"160409" forKey:@"min_ver"];
  5054. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  5055. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  5056. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  5057. return [RAUtils dict2data:dic];
  5058. }
  5059. #pragma mark place order
  5060. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  5061. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5062. if (aname) {
  5063. [dic setValue:aname forKey:@"aname"];
  5064. }
  5065. if (control) {
  5066. [dic setValue:control forKey:@"control"];
  5067. }
  5068. if (keyboard) {
  5069. [dic setValue:keyboard forKey:@"keyboard"];
  5070. }
  5071. if (name) {
  5072. [dic setValue:name forKey:@"name"];
  5073. }
  5074. if (value) {
  5075. [dic setValue:value forKey:@"value"];
  5076. }
  5077. return dic;
  5078. }
  5079. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  5080. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5081. orderCode = [self translateSingleQuote:orderCode];
  5082. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  5083. 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];
  5084. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5085. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  5086. NSString *img0 = [self textAtColumn:1 statement:stmt];
  5087. NSString *img1 = [self textAtColumn:2 statement:stmt];
  5088. NSString *img2 = [self textAtColumn:3 statement:stmt];
  5089. NSString *company_name = [self textAtColumn:4 statement:stmt];
  5090. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  5091. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  5092. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  5093. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  5094. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  5095. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  5096. NSString *email = [self textAtColumn:11 statement:stmt];
  5097. NSString *phone = [self textAtColumn:12 statement:stmt];
  5098. NSString *fax = [self textAtColumn:13 statement:stmt];
  5099. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  5100. NSString *city = [self textAtColumn:15 statement:stmt];
  5101. NSString *state = [self textAtColumn:16 statement:stmt];
  5102. NSString *country = [self textAtColumn:17 statement:stmt];
  5103. NSString *name = [self textAtColumn:18 statement:stmt];
  5104. // contact id
  5105. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  5106. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  5107. [contact_id_dic setValue:@"text" forKey:@"control"];
  5108. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  5109. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  5110. [contact_id_dic setValue:@"true" forKey:@"required"];
  5111. [contact_id_dic setValue:contact_id forKey:@"value"];
  5112. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  5113. // business card
  5114. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  5115. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  5116. [business_card_dic setValue:@"img" forKey:@"control"];
  5117. [business_card_dic setValue:@"1" forKey:@"disable"];
  5118. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  5119. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  5120. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  5121. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  5122. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  5123. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  5124. [business_card_dic setValue:@"business_card" forKey:@"name"];
  5125. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  5126. // fax
  5127. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5128. [customer_dic setValue:fax_dic forKey:@"item_10"];
  5129. // zipcode
  5130. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  5131. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  5132. // city
  5133. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  5134. [customer_dic setValue:city_dic forKey:@"item_12"];
  5135. // state
  5136. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  5137. [customer_dic setValue:state_dic forKey:@"item_13"];
  5138. // country
  5139. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  5140. [customer_dic setValue:country_dic forKey:@"item_14"];
  5141. // company name
  5142. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  5143. [customer_dic setValue:company_dic forKey:@"item_2"];
  5144. // addr_1
  5145. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  5146. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  5147. // addr_2
  5148. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  5149. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  5150. // addr_3
  5151. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  5152. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  5153. // addr_4
  5154. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  5155. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  5156. // Contact
  5157. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  5158. [customer_dic setValue:contact_dic forKey:@"item_7"];
  5159. // email
  5160. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5161. [customer_dic setValue:email_dic forKey:@"item_8"];
  5162. // phone
  5163. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5164. [customer_dic setValue:phone_dic forKey:@"item_9"];
  5165. // title
  5166. [customer_dic setValue:@"Customer" forKey:@"title"];
  5167. // count
  5168. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  5169. }];
  5170. // setting
  5171. NSDictionary *setting = params[@"setting"];
  5172. NSNumber *hide = setting[@"CustomerHide"];
  5173. [customer_dic setValue:hide forKey:@"hide"];
  5174. return customer_dic;
  5175. }
  5176. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  5177. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5178. // setting
  5179. NSDictionary *setting = params[@"setting"];
  5180. NSNumber *hide = setting[@"ShipToHide"];
  5181. [dic setValue:hide forKey:@"hide"];
  5182. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5183. orderCode = [self translateSingleQuote:orderCode];
  5184. 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];
  5185. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5186. NSString *cid = [self textAtColumn:0 statement:stmt];
  5187. NSString *name = [self textAtColumn:1 statement:stmt];
  5188. NSString *ext = [self textAtColumn:2 statement:stmt];
  5189. NSString *contact = [self textAtColumn:3 statement:stmt];
  5190. NSString *email = [self textAtColumn:4 statement:stmt];
  5191. NSString *fax = [self textAtColumn:5 statement:stmt];
  5192. NSString *phone = [self textAtColumn:6 statement:stmt];
  5193. // count
  5194. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5195. // title
  5196. [dic setValue:@"Ship To" forKey:@"title"];
  5197. // choose
  5198. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5199. [choose_dic setValue:@"choose" forKey:@"aname"];
  5200. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5201. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  5202. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  5203. @"key_map" : @{@"receive_cid" : @"customer_cid",
  5204. @"receive_contact" : @"customer_contact",
  5205. @"receive_email" : @"customer_email",
  5206. @"receive_ext" : @"customer_contact_ext",
  5207. @"receive_fax" : @"customer_fax",
  5208. @"receive_name" : @"customer_name",
  5209. @"receive_phone" : @"customer_phone"},
  5210. @"refresh" : [NSNumber numberWithInteger:1],
  5211. @"type" : @"pull"};
  5212. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  5213. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  5214. @"name" : @"Add new address",
  5215. @"refresh" : [NSNumber numberWithInteger:1],
  5216. @"value" : @"new_addr"
  5217. };
  5218. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  5219. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  5220. @"key_map" : @{@"receive_cid" : @"customer_cid",
  5221. @"receive_contact" : @"customer_contact",
  5222. @"receive_email" : @"customer_email",
  5223. @"receive_ext" : @"customer_contact_ext",
  5224. @"receive_fax" : @"customer_fax",
  5225. @"receive_name" : @"customer_name",
  5226. @"receive_phone" : @"customer_phone"},
  5227. @"name" : @"select_ship_to",
  5228. @"refresh" : [NSNumber numberWithInteger:1],
  5229. @"value" : @"Sales_Order_Ship_To"};
  5230. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  5231. [dic setValue:choose_dic forKey:@"item_0"];
  5232. // contact id
  5233. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5234. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5235. [contact_id_dic setValue:@"true" forKey:@"required"];
  5236. [dic setValue:contact_id_dic forKey:@"item_1"];
  5237. // company name
  5238. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5239. [dic setValue:company_name_dic forKey:@"item_2"];
  5240. // address
  5241. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  5242. NSNumber *required = setting[@"ShippingToAddressRequire"];
  5243. if ([required integerValue]) {
  5244. [ext_dic setValue:@"true" forKey:@"required"];
  5245. }
  5246. [dic setValue:ext_dic forKey:@"item_3"];
  5247. // contact
  5248. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5249. [dic setValue:contact_dic forKey:@"item_4"];
  5250. // phone
  5251. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5252. [dic setValue:phone_dic forKey:@"item_5"];
  5253. // fax
  5254. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5255. [dic setValue:fax_dic forKey:@"item_6"];
  5256. // email
  5257. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5258. [dic setValue:email_dic forKey:@"item_7"];
  5259. }];
  5260. return dic;
  5261. }
  5262. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  5263. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5264. // setting
  5265. NSDictionary *setting = params[@"setting"];
  5266. NSNumber *hide = setting[@"ShipFromHide"];
  5267. [dic setValue:hide forKey:@"hide"];
  5268. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5269. orderCode = [self translateSingleQuote:orderCode];
  5270. 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];
  5271. 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';";
  5272. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5273. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  5274. __block NSString *name = [self textAtColumn:1 statement:stmt];
  5275. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  5276. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  5277. __block NSString *email = [self textAtColumn:4 statement:stmt];
  5278. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  5279. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  5280. if (!cid.length) {
  5281. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  5282. cid = [self textAtColumn:0 statement:statment];
  5283. name = [self textAtColumn:1 statement:statment];
  5284. ext = [self textAtColumn:2 statement:statment];
  5285. contact = [self textAtColumn:3 statement:statment];
  5286. email = [self textAtColumn:4 statement:statment];
  5287. fax = [self textAtColumn:5 statement:statment];
  5288. phone = [self textAtColumn:6 statement:statment];
  5289. }];
  5290. }
  5291. // count
  5292. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5293. // title
  5294. [dic setValue:@"Ship From" forKey:@"title"];
  5295. // hide
  5296. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  5297. // choose
  5298. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5299. [choose_dic setValue:@"choose" forKey:@"aname"];
  5300. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5301. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  5302. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  5303. @"key_map" : @{@"sender_cid" : @"customer_cid",
  5304. @"sender_contact" : @"customer_contact",
  5305. @"sender_email" : @"customer_email",
  5306. @"sender_ext" : @"customer_contact_ext",
  5307. @"sender_fax" : @"customer_fax",
  5308. @"sender_name" : @"customer_name",
  5309. @"sender_phone" : @"customer_phone"},
  5310. @"name" : @"select_cid",
  5311. @"refresh" : [NSNumber numberWithInteger:0],
  5312. @"value" : @"Sales_Order_Ship_From"};
  5313. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  5314. [dic setValue:choose_dic forKey:@"item_0"];
  5315. // contact id
  5316. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5317. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5318. [contact_id_dic setValue:@"true" forKey:@"required"];
  5319. [dic setValue:contact_id_dic forKey:@"item_1"];
  5320. // company name
  5321. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5322. [dic setValue:company_name_dic forKey:@"item_2"];
  5323. // address
  5324. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  5325. [dic setValue:ext_dic forKey:@"item_3"];
  5326. // contact
  5327. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5328. [dic setValue:contact_dic forKey:@"item_4"];
  5329. // phone
  5330. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5331. [dic setValue:phone_dic forKey:@"item_5"];
  5332. // fax
  5333. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5334. [dic setValue:fax_dic forKey:@"item_6"];
  5335. // email
  5336. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5337. [dic setValue:email_dic forKey:@"item_7"];
  5338. }];
  5339. return dic;
  5340. }
  5341. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  5342. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5343. // setting
  5344. NSDictionary *setting = params[@"setting"];
  5345. NSNumber *hide = setting[@"FreightBillToHide"];
  5346. [dic setValue:hide forKey:@"hide"];
  5347. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5348. orderCode = [self translateSingleQuote:orderCode];
  5349. 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];
  5350. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5351. NSString *cid = [self textAtColumn:0 statement:stmt];
  5352. NSString *name = [self textAtColumn:1 statement:stmt];
  5353. NSString *ext = [self textAtColumn:2 statement:stmt];
  5354. NSString *contact = [self textAtColumn:3 statement:stmt];
  5355. NSString *email = [self textAtColumn:4 statement:stmt];
  5356. NSString *fax = [self textAtColumn:5 statement:stmt];
  5357. NSString *phone = [self textAtColumn:6 statement:stmt];
  5358. // count
  5359. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5360. // title
  5361. [dic setValue:@"Freight Bill To" forKey:@"title"];
  5362. // hide
  5363. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  5364. // choose
  5365. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5366. [choose_dic setValue:@"choose" forKey:@"aname"];
  5367. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5368. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  5369. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  5370. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  5371. @"shipping_billto_contact" : @"receive_contact",
  5372. @"shipping_billto_email" : @"receive_email",
  5373. @"shipping_billto_ext" : @"receive_ext",
  5374. @"shipping_billto_fax" : @"receive_fax",
  5375. @"shipping_billto_name" : @"receive_name",
  5376. @"shipping_billto_phone" : @"receive_phone"},
  5377. @"type" : @"pull"};
  5378. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  5379. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  5380. @"type" : @"pull",
  5381. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  5382. @"shipping_billto_contact" : @"customer_contact",
  5383. @"shipping_billto_email" : @"customer_email",
  5384. @"shipping_billto_ext" : @"customer_contact_ext",
  5385. @"shipping_billto_fax" : @"customer_fax",
  5386. @"shipping_billto_name" : @"customer_name",
  5387. @"shipping_billto_phone" : @"customer_phone"}
  5388. };
  5389. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  5390. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  5391. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  5392. @"shipping_billto_contact" : @"sender_contact",
  5393. @"shipping_billto_email" : @"sender_email",
  5394. @"shipping_billto_ext" : @"sender_ext",
  5395. @"shipping_billto_fax" : @"sender_fax",
  5396. @"shipping_billto_name" : @"sender_name",
  5397. @"shipping_billto_phone" : @"sender_phone"},
  5398. @"type" : @"pull"
  5399. };
  5400. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  5401. NSDictionary *select_freight_bill_to_dic = @{
  5402. @"aname" : @"Select freight bill to",
  5403. @"name" : @"select_cid",
  5404. @"refresh" : [NSNumber numberWithInteger:0],
  5405. @"value" : @"Sales_Order_Freight_Bill_To",
  5406. @"key_map" : @{
  5407. @"shipping_billto_cid" : @"customer_cid",
  5408. @"shipping_billto_contact" : @"customer_contact",
  5409. @"shipping_billto_email" : @"customer_email",
  5410. @"shipping_billto_ext" : @"customer_contact_ext",
  5411. @"shipping_billto_fax" : @"customer_fax",
  5412. @"shipping_billto_name" : @"customer_name",
  5413. @"shipping_billto_phone" : @"customer_phone"
  5414. }
  5415. };
  5416. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  5417. [dic setValue:choose_dic forKey:@"item_0"];
  5418. // contact id
  5419. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5420. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5421. [contact_id_dic setValue:@"true" forKey:@"required"];
  5422. [dic setValue:contact_id_dic forKey:@"item_1"];
  5423. // company name
  5424. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5425. [dic setValue:company_name_dic forKey:@"item_2"];
  5426. // address
  5427. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  5428. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  5429. if ([ext_required integerValue]) {
  5430. [ext_dic setValue:@"true" forKey:@"required"];
  5431. }
  5432. [dic setValue:ext_dic forKey:@"item_3"];
  5433. // contact
  5434. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5435. [dic setValue:contact_dic forKey:@"item_4"];
  5436. // phone
  5437. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5438. [dic setValue:phone_dic forKey:@"item_5"];
  5439. // fax
  5440. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5441. [dic setValue:fax_dic forKey:@"item_6"];
  5442. // email
  5443. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5444. [dic setValue:email_dic forKey:@"item_7"];
  5445. }];
  5446. return dic;
  5447. }
  5448. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  5449. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5450. // setting
  5451. NSDictionary *setting = params[@"setting"];
  5452. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  5453. [dic setValue:hide forKey:@"hide"];
  5454. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5455. orderCode = [self translateSingleQuote:orderCode];
  5456. 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];
  5457. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5458. NSString *cid = [self textAtColumn:0 statement:stmt];
  5459. NSString *name = [self textAtColumn:1 statement:stmt];
  5460. NSString *ext = [self textAtColumn:2 statement:stmt];
  5461. NSString *contact = [self textAtColumn:3 statement:stmt];
  5462. NSString *email = [self textAtColumn:4 statement:stmt];
  5463. NSString *fax = [self textAtColumn:5 statement:stmt];
  5464. NSString *phone = [self textAtColumn:6 statement:stmt];
  5465. // count
  5466. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5467. // title
  5468. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  5469. // hide
  5470. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  5471. // choose
  5472. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5473. [choose_dic setValue:@"choose" forKey:@"aname"];
  5474. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5475. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  5476. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  5477. @"key_map" : @{@"billing_cid" : @"receive_cid",
  5478. @"billing_contact" : @"receive_contact",
  5479. @"billing_email" : @"receive_email",
  5480. @"billing_ext" : @"receive_ext",
  5481. @"billing_fax" : @"receive_fax",
  5482. @"billing_name" : @"receive_name",
  5483. @"billing_phone" : @"receive_phone"},
  5484. @"type" : @"pull"};
  5485. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  5486. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  5487. @"type" : @"pull",
  5488. @"key_map" : @{@"billing_cid" : @"customer_cid",
  5489. @"billing_contact" : @"customer_contact",
  5490. @"billing_email" : @"customer_email",
  5491. @"billing_ext" : @"customer_contact_ext",
  5492. @"billing_fax" : @"customer_fax",
  5493. @"billing_name" : @"customer_name",
  5494. @"billing_phone" : @"customer_phone"}
  5495. };
  5496. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  5497. NSDictionary *select_bill_to_dic = @{
  5498. @"aname" : @"Select bill to",
  5499. @"name" : @"select_cid",
  5500. @"refresh" : [NSNumber numberWithInteger:0],
  5501. @"value" : @"Sales_Order_Merchandise_Bill_To",
  5502. @"key_map" : @{
  5503. @"billing_cid" : @"customer_cid",
  5504. @"billing_contact" : @"customer_contact",
  5505. @"billing_email" : @"customer_email",
  5506. @"billing_ext" : @"customer_contact_ext",
  5507. @"billing_fax" : @"customer_fax",
  5508. @"billing_name" : @"customer_name",
  5509. @"billing_phone" : @"customer_phone"
  5510. }
  5511. };
  5512. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  5513. [dic setValue:choose_dic forKey:@"item_0"];
  5514. // contact id
  5515. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5516. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5517. [contact_id_dic setValue:@"true" forKey:@"required"];
  5518. [dic setValue:contact_id_dic forKey:@"item_1"];
  5519. // company name
  5520. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5521. [dic setValue:company_name_dic forKey:@"item_2"];
  5522. // address
  5523. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  5524. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  5525. if ([ext_required integerValue]) {
  5526. [ext_dic setValue:@"true" forKey:@"required"];
  5527. }
  5528. [dic setValue:ext_dic forKey:@"item_3"];
  5529. // contact
  5530. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5531. [dic setValue:contact_dic forKey:@"item_4"];
  5532. // phone
  5533. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5534. [dic setValue:phone_dic forKey:@"item_5"];
  5535. // fax
  5536. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5537. [dic setValue:fax_dic forKey:@"item_6"];
  5538. // email
  5539. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5540. [dic setValue:email_dic forKey:@"item_7"];
  5541. }];
  5542. return dic;
  5543. }
  5544. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  5545. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5546. // setting
  5547. NSDictionary *setting = params[@"setting"];
  5548. NSNumber *hide = setting[@"ReturnToHide"];
  5549. [dic setValue:hide forKey:@"hide"];
  5550. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5551. orderCode = [self translateSingleQuote:orderCode];
  5552. 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];
  5553. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5554. NSString *cid = [self textAtColumn:0 statement:stmt];
  5555. NSString *name = [self textAtColumn:1 statement:stmt];
  5556. NSString *ext = [self textAtColumn:2 statement:stmt];
  5557. NSString *contact = [self textAtColumn:3 statement:stmt];
  5558. NSString *email = [self textAtColumn:4 statement:stmt];
  5559. NSString *fax = [self textAtColumn:5 statement:stmt];
  5560. NSString *phone = [self textAtColumn:6 statement:stmt];
  5561. // count
  5562. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5563. // title
  5564. [dic setValue:@"Return To" forKey:@"title"];
  5565. // hide
  5566. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  5567. // choose
  5568. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5569. [choose_dic setValue:@"choose" forKey:@"aname"];
  5570. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5571. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  5572. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  5573. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  5574. @"returnto_contact" : @"sender_contact",
  5575. @"returnto_email" : @"sender_email",
  5576. @"returnto_ext" : @"sender_ext",
  5577. @"returnto_fax" : @"sender_fax",
  5578. @"returnto_name" : @"sender_name",
  5579. @"returnto_phone" : @"sender_phone"},
  5580. @"type" : @"pull"};
  5581. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  5582. NSDictionary *select_return_to_dic = @{
  5583. @"aname" : @"Select return to",
  5584. @"name" : @"select_cid",
  5585. @"refresh" : [NSNumber numberWithInteger:0],
  5586. @"value" : @"Contact_Return_To",
  5587. @"key_map" : @{
  5588. @"returnto_cid" : @"customer_cid",
  5589. @"returnto_contact" : @"customer_contact",
  5590. @"returnto_email" : @"customer_email",
  5591. @"returnto_ext" : @"customer_contact_ext",
  5592. @"returnto_fax" : @"customer_fax",
  5593. @"returnto_name" : @"customer_name",
  5594. @"returnto_phone" : @"customer_phone"
  5595. }
  5596. };
  5597. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  5598. [dic setValue:choose_dic forKey:@"item_0"];
  5599. // contact id
  5600. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5601. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5602. [contact_id_dic setValue:@"true" forKey:@"required"];
  5603. [dic setValue:contact_id_dic forKey:@"item_1"];
  5604. // company name
  5605. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5606. [dic setValue:company_name_dic forKey:@"item_2"];
  5607. // address
  5608. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  5609. [dic setValue:ext_dic forKey:@"item_3"];
  5610. // contact
  5611. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5612. [dic setValue:contact_dic forKey:@"item_4"];
  5613. // phone
  5614. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5615. [dic setValue:phone_dic forKey:@"item_5"];
  5616. // fax
  5617. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5618. [dic setValue:fax_dic forKey:@"item_6"];
  5619. // email
  5620. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5621. [dic setValue:email_dic forKey:@"item_7"];
  5622. }];
  5623. return dic;
  5624. }
  5625. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  5626. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5627. orderCode = [self translateSingleQuote:orderCode];
  5628. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5629. __block double TotalCuft = 0;
  5630. __block double TotalWeight = 0;
  5631. __block int TotalCarton = 0;
  5632. __block double payments = 0;
  5633. // setting
  5634. NSDictionary *setting = params[@"setting"];
  5635. NSNumber *hide = setting[@"ModelInformationHide"];
  5636. [dic setValue:hide forKey:@"hide"];
  5637. 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];
  5638. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5639. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5640. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  5641. // item id
  5642. int item_id = sqlite3_column_int(stmt, 0);
  5643. // count
  5644. int item_count = sqlite3_column_int(stmt, 1);
  5645. // stockUom
  5646. int stockUom = sqlite3_column_int(stmt, 2);
  5647. // unit price
  5648. NSString *str_price = [self textAtColumn:3 statement:stmt];
  5649. NSString* Price=nil;
  5650. if([str_price isEqualToString:@""])
  5651. {
  5652. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  5653. if(price==nil)
  5654. Price=@"No Price.";
  5655. else
  5656. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  5657. }
  5658. else
  5659. {
  5660. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  5661. }
  5662. // discount
  5663. double discount = sqlite3_column_double(stmt, 4);
  5664. // name
  5665. NSString *name = [self textAtColumn:5 statement:stmt];
  5666. // description
  5667. NSString *description = [self textAtColumn:6 statement:stmt];
  5668. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  5669. // line note
  5670. NSString *line_note = [self textAtColumn:7 statement:stmt];
  5671. int avaulability = sqlite3_column_int(stmt, 8);
  5672. // img
  5673. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  5674. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false];
  5675. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  5676. if(combine != nil)
  5677. {
  5678. // int citem=0;
  5679. int bcount=[[combine valueForKey:@"count"] intValue];
  5680. for(int bc=0;bc<bcount;bc++)
  5681. {
  5682. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  5683. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  5684. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  5685. subTotal += uprice * modulus * item_count;
  5686. }
  5687. }
  5688. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  5689. [model_dic setValue:@"model" forKey:@"control"];
  5690. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  5691. [model_dic setValue:description forKey:@"description"];
  5692. [model_dic setValue:line_note forKey:@"note"];
  5693. [model_dic setValue:img forKey:@"img_url"];
  5694. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  5695. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  5696. [model_dic setValue:Price forKey:@"unit_price"];
  5697. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  5698. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  5699. if (combine) {
  5700. [model_dic setValue:combine forKey:@"combine"];
  5701. }
  5702. // well,what under the row is the info for total
  5703. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  5704. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  5705. double weight=[bsubtotaljson[@"weight"] doubleValue];
  5706. int carton=[bsubtotaljson[@"carton"] intValue];
  5707. TotalCuft += cuft;
  5708. TotalWeight += weight;
  5709. TotalCarton += carton;
  5710. payments += subTotal;
  5711. //---------------------------
  5712. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  5713. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  5714. }];
  5715. [dic setValue:@"Model Information" forKey:@"title"];
  5716. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  5717. return dic;
  5718. }
  5719. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  5720. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5721. [dic setValue:@"Remarks Content" forKey:@"title"];
  5722. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  5723. // setting
  5724. NSDictionary *setting = params[@"setting"];
  5725. NSNumber *hide = setting[@"RemarksContentHide"];
  5726. [dic setValue:hide forKey:@"hide"];
  5727. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5728. orderCode = [self translateSingleQuote:orderCode];
  5729. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes from offline_order where so_id = '%@';",orderCode];
  5730. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5731. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  5732. int mustCall = sqlite3_column_int(stmt, 1);
  5733. NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  5734. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  5735. NSDictionary *po_dic = @{
  5736. @"aname" : @"PO#",
  5737. @"control" : @"edit",
  5738. @"keyboard" : @"text",
  5739. @"name" : @"poNumber",
  5740. @"value" : poNumber
  5741. };
  5742. NSDictionary *must_call_dic = @{
  5743. @"aname" : @"MUST CALL BEFORE DELIVERY",
  5744. @"control" : @"switch",
  5745. @"name" : @"must_call",
  5746. @"value" : mustCall ? @"true" : @"false"
  5747. };
  5748. NSDictionary *general_notes_dic = @{
  5749. @"aname" : @"General notes",
  5750. @"control" : @"text_view",
  5751. @"keyboard" : @"text",
  5752. @"name" : @"comments",
  5753. @"value" : generalNotes
  5754. };
  5755. NSDictionary *internal_notes_dic = @{
  5756. @"aname" : @"Internal notes",
  5757. @"control" : @"text_view",
  5758. @"keyboard" : @"text",
  5759. @"name" : @"internal_notes",
  5760. @"value" : internalNotes
  5761. };
  5762. [dic setValue:po_dic forKey:@"item_0"];
  5763. [dic setValue:must_call_dic forKey:@"item_1"];
  5764. [dic setValue:general_notes_dic forKey:@"item_2"];
  5765. [dic setValue:internal_notes_dic forKey:@"item_3"];
  5766. }];
  5767. return dic;
  5768. }
  5769. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  5770. // params
  5771. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5772. orderCode = [self translateSingleQuote:orderCode];
  5773. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5774. // setting
  5775. NSDictionary *setting = params[@"setting"];
  5776. NSNumber *hide = setting[@"OrderTotalHide"];
  5777. [dic setValue:hide forKey:@"hide"];
  5778. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  5779. __block double lift_gate_value = 0;
  5780. __block double handling_fee = 0;
  5781. __block double shipping = 0;
  5782. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5783. int lift_gate = sqlite3_column_int(stmt, 0);
  5784. lift_gate_value = sqlite3_column_double(stmt, 1);
  5785. shipping = sqlite3_column_double(stmt, 2);
  5786. handling_fee = sqlite3_column_double(stmt, 3);
  5787. if (!lift_gate) {
  5788. lift_gate_value = 0;
  5789. }
  5790. }];
  5791. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  5792. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  5793. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  5794. double payments = [[total valueForKey:@"payments"] doubleValue];
  5795. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  5796. double totalPrice = payments;
  5797. [dic setValue:@"Order Total" forKey:@"title"];
  5798. NSDictionary *payments_dic = @{
  5799. @"align" : @"right",
  5800. @"aname" : @"Payments/Credits",
  5801. @"control" : @"text",
  5802. @"name" : @"paymentsAndCredits",
  5803. @"type" : @"price",
  5804. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  5805. };
  5806. [dic setValue:payments_dic forKey:@"item_0"];
  5807. NSDictionary *handling_fee_dic = @{
  5808. @"align" : @"right",
  5809. @"aname" : @"Handling Fee",
  5810. @"control" : @"text",
  5811. @"name" : @"handling_fee_value",
  5812. @"required" : @"true",
  5813. @"type" : @"price",
  5814. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  5815. };
  5816. NSDictionary *shipping_dic = @{
  5817. @"align" : @"right",
  5818. @"aname" : @"Shipping*",
  5819. @"control" : @"text",
  5820. @"name" : @"shipping",
  5821. @"required" : @"true",
  5822. @"type" : @"price",
  5823. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  5824. };
  5825. NSDictionary *lift_gate_dic = @{
  5826. @"align" : @"right",
  5827. @"aname" : @"Liftgate Fee(No Loading Dock)",
  5828. @"control" : @"text",
  5829. @"name" : @"lift_gate_value",
  5830. @"required" : @"true",
  5831. @"type" : @"price",
  5832. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  5833. };
  5834. int item_count = 1;
  5835. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  5836. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  5837. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5838. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5839. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  5840. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5841. } else {
  5842. }
  5843. [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5844. NSDictionary *total_price_dic = @{
  5845. @"align" : @"right",
  5846. @"aname" : @"Total",
  5847. @"control" : @"text",
  5848. @"name" : @"totalPrice",
  5849. @"type" : @"price",
  5850. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  5851. };
  5852. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5853. NSDictionary *total_cuft_dic = @{
  5854. @"align" : @"right",
  5855. @"aname" : @"Total Cuft",
  5856. @"control" : @"text",
  5857. @"name" : @"",
  5858. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  5859. };
  5860. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5861. NSDictionary *total_weight_dic = @{
  5862. @"align" : @"right",
  5863. @"aname" : @"Total Weight",
  5864. @"control" : @"text",
  5865. @"name" : @"",
  5866. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  5867. };
  5868. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5869. NSDictionary *total_carton_dic = @{
  5870. @"align" : @"right",
  5871. @"aname" : @"Total Carton",
  5872. @"control" : @"text",
  5873. @"name" : @"",
  5874. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  5875. };
  5876. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5877. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  5878. return dic;
  5879. }
  5880. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  5881. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5882. orderCode = [self translateSingleQuote:orderCode];
  5883. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5884. // setting
  5885. NSDictionary *setting = params[@"setting"];
  5886. NSNumber *hide = setting[@"SignatureHide"];
  5887. [dic setValue:hide forKey:@"hide"];
  5888. [dic setValue:@"Signature" forKey:@"title"];
  5889. __block NSString *pic_path = @"";
  5890. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  5891. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5892. pic_path = [self textAtColumn:0 statement:stmt];
  5893. }];
  5894. NSDictionary *pic_dic = @{
  5895. @"aname" : @"Signature",
  5896. @"avalue" :pic_path,
  5897. @"control" : @"signature",
  5898. @"name" : @"sign_picpath",
  5899. @"value" : pic_path
  5900. };
  5901. [dic setValue:pic_dic forKey:@"item_0"];
  5902. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  5903. return dic;
  5904. }
  5905. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  5906. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5907. // setting
  5908. NSDictionary *setting = params[@"setting"];
  5909. NSNumber *hide = setting[@"ShippingMethodHide"];
  5910. [dic setValue:hide forKey:@"hide"];
  5911. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5912. orderCode = [self translateSingleQuote:orderCode];
  5913. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  5914. __block NSString *logist = @"";
  5915. __block NSString *lift_gate = @"";
  5916. __block int lift_gate_integer = 0;
  5917. __block NSString *logistic_note = @"";
  5918. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5919. logist = [self textAtColumn:0 statement:stmt];
  5920. lift_gate_integer = sqlite3_column_int(stmt, 1);
  5921. logistic_note = [self textAtColumn:2 statement:stmt];
  5922. }];
  5923. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  5924. [dic setValue:@"Shipping Method" forKey:@"title"];
  5925. // val_0
  5926. int PERSONAL_PICK_UP_check = 0;
  5927. int USE_MY_CARRIER_check = 0;
  5928. NSString *logistic_note_text = @"";
  5929. int will_call_check = 0;
  5930. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  5931. will_call_check = 1;
  5932. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  5933. PERSONAL_PICK_UP_check = 1;
  5934. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  5935. USE_MY_CARRIER_check = 1;
  5936. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  5937. logistic_note = [logistic_note_array firstObject];
  5938. if (logistic_note_array.count > 1) {
  5939. logistic_note_text = [logistic_note_array lastObject];
  5940. }
  5941. }
  5942. }
  5943. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  5944. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  5945. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  5946. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  5947. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  5948. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  5949. NSDictionary *val0_subItem_item0 = @{
  5950. @"aname" : @"Option",
  5951. @"cadedate" : @{
  5952. @"count" : [NSNumber numberWithInteger:2],
  5953. @"val_0" : @{
  5954. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  5955. @"refresh" : [NSNumber numberWithInteger:0],
  5956. @"value" : @"PERSONAL PICK UP",
  5957. @"value_id" : @"PERSONAL PICK UP"
  5958. },
  5959. @"val_1" : @{
  5960. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  5961. @"refresh" : [NSNumber numberWithInteger:0],
  5962. @"sub_item" : @{
  5963. @"count" : [NSNumber numberWithInteger:1],
  5964. @"item_0" : @{
  5965. @"aname" : @"BOL",
  5966. @"control" : @"edit",
  5967. @"keyboard" : @"text",
  5968. @"name" : @"logist_note_text",
  5969. @"refresh" : [NSNumber numberWithInteger:0],
  5970. @"required" : @"false",
  5971. @"value" : logistic_note_text
  5972. }
  5973. },
  5974. @"value" : @"USE MY CARRIER",
  5975. @"value_id" : @"USE MY CARRIER"
  5976. }
  5977. },
  5978. @"control" : @"enum",
  5979. @"name" : @"logistic_note",
  5980. @"required" : @"true",
  5981. @"single_select" : @"true"
  5982. };
  5983. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  5984. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  5985. // val_1
  5986. lift_gate = [self valueInParams:params key:@"lift_gate"];
  5987. if([lift_gate isEqualToString:@""]) {
  5988. if (lift_gate_integer == 1) {
  5989. lift_gate = @"true";
  5990. } else {
  5991. lift_gate = @"false";
  5992. }
  5993. }
  5994. int common_carrier_check = 0;
  5995. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  5996. common_carrier_check = 1;
  5997. [params setValue:lift_gate forKey:@"lift_gate"];
  5998. }
  5999. NSDictionary *val_1 = @{
  6000. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  6001. @"sub_item" : @{
  6002. @"count" : [NSNumber numberWithInteger:1],
  6003. @"item_0" : @{
  6004. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  6005. @"control" : @"switch",
  6006. @"name" : @"lift_gate",
  6007. @"refresh" : [NSNumber numberWithInteger:1],
  6008. @"required" : @"true",
  6009. @"value" : lift_gate
  6010. }
  6011. },
  6012. @"value" : @"COMMON CARRIER",
  6013. @"value_id" : @"COMMON CARRIER"
  6014. };
  6015. // cadedate
  6016. NSDictionary *cadedate = @{
  6017. @"count" : [NSNumber numberWithInteger:2],
  6018. @"val_0" : val_0,
  6019. @"val_1" : val_1
  6020. };
  6021. // item_0
  6022. NSMutableDictionary *item_0 = @{
  6023. @"aname" : @"Shipping",
  6024. @"cadedate" : cadedate,
  6025. @"control" : @"enum",
  6026. @"name" : @"logist",
  6027. @"refresh" : [NSNumber numberWithInteger:1],
  6028. @"single_select" : @"true",
  6029. }.mutableCopy;
  6030. NSNumber *required = setting[@"ShippingMethodRequire"];
  6031. if ([required integerValue]) {
  6032. [item_0 setValue:@"true" forKey:@"required"];
  6033. }
  6034. [dic setValue:item_0 forKey:@"item_0"];
  6035. return dic;
  6036. }
  6037. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  6038. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6039. orderCode = [self translateSingleQuote:orderCode];
  6040. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  6041. __block int submit_as_integer = 0;
  6042. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6043. submit_as_integer = sqlite3_column_int(stmt, 0);
  6044. }];
  6045. int check11 = 0;
  6046. int check10 = 0;
  6047. if (submit_as_integer == 11) {
  6048. check11 = 1;
  6049. }
  6050. if (submit_as_integer == 10) {
  6051. check10 = 1;
  6052. }
  6053. // section_0
  6054. NSMutableDictionary *dic = @{
  6055. @"count" : @(1),
  6056. @"item_0" : @{
  6057. @"aname" : @"Submit Order As",
  6058. @"cadedate" : @{
  6059. @"count" : @(2),
  6060. @"val_0" : @{
  6061. @"check" : [NSNumber numberWithInteger:check11],
  6062. @"value" : @"Sales Order",
  6063. @"value_id" : @(11)
  6064. },
  6065. @"val_1" : @{
  6066. @"check" : [NSNumber numberWithInteger:check10],
  6067. @"value" : @"Quote",
  6068. @"value_id" : @(10)
  6069. }
  6070. },
  6071. @"control" : @"enum",
  6072. @"name" : @"erpOrderStatus",
  6073. @"required" : @"true",
  6074. @"single_select" : @"true"
  6075. },
  6076. @"title" : @"Order Type"
  6077. }.mutableCopy;
  6078. // setting
  6079. NSDictionary *setting = params[@"setting"];
  6080. NSNumber *hide = setting[@"OrderTypeHide"];
  6081. [dic setValue:hide forKey:@"hide"];
  6082. return dic;
  6083. }
  6084. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  6085. countryCode = [self translateSingleQuote:countryCode];
  6086. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  6087. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6088. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  6089. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  6090. if (name == NULL) {
  6091. name = "";
  6092. }
  6093. if (code == NULL) {
  6094. code = "";
  6095. }
  6096. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  6097. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  6098. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  6099. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  6100. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  6101. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  6102. }
  6103. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  6104. [container setValue:stateDic forKey:key];
  6105. }] mutableCopy];
  6106. // failure 可以不用了,一样的
  6107. if (ret.allKeys.count == 0) {
  6108. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  6109. [stateDic setValue:@"Other" forKey:@"value"];
  6110. [stateDic setValue:@"" forKey:@"value_id"];
  6111. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  6112. if (state_code && [@"" isEqualToString:state_code]) {
  6113. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  6114. }
  6115. NSString *key = [NSString stringWithFormat:@"val_0"];
  6116. [ret setValue:stateDic forKey:key];
  6117. }
  6118. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  6119. return ret;
  6120. }
  6121. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  6122. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6123. orderCode = [self translateSingleQuote:orderCode];
  6124. 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];
  6125. __block NSString *payType = @"";
  6126. __block NSString *firstName = @"";
  6127. __block NSString *lastName = @"";
  6128. __block NSString *addr1 = @"";
  6129. __block NSString *addr2 = @"";
  6130. __block NSString *zipcode = @"";
  6131. __block NSString *cardType = @"";
  6132. __block NSString *cardNumber = @"";
  6133. __block NSString *securityCode = @"";
  6134. __block NSString *month = @"";
  6135. __block NSString *year = @"";
  6136. __block NSString *city = @"";
  6137. __block NSString *state = @"";
  6138. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6139. payType = [self textAtColumn:0 statement:stmt];
  6140. firstName = [self textAtColumn:1 statement:stmt];
  6141. lastName = [self textAtColumn:2 statement:stmt];
  6142. addr1 = [self textAtColumn:3 statement:stmt];
  6143. addr2 = [self textAtColumn:4 statement:stmt];
  6144. zipcode = [self textAtColumn:5 statement:stmt];
  6145. cardType = [self textAtColumn:6 statement:stmt];
  6146. cardNumber = [self textAtColumn:7 statement:stmt];
  6147. securityCode = [self textAtColumn:8 statement:stmt];
  6148. month = [self textAtColumn:9 statement:stmt];
  6149. year = [self textAtColumn:10 statement:stmt];
  6150. city = [self textAtColumn:11 statement:stmt];
  6151. state = [self textAtColumn:12 statement:stmt];
  6152. }];
  6153. NSString *required = @"true";
  6154. // setting
  6155. NSDictionary *setting = params[@"setting"];
  6156. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  6157. if ([requiredNumber integerValue]) {
  6158. required = @"true";
  6159. } else {
  6160. required = @"false";
  6161. }
  6162. // "section_2"
  6163. NSMutableDictionary *dic = @{
  6164. @"count" : @(1),
  6165. @"item_0" : @{
  6166. @"aname" : @"Payment",
  6167. @"required" : required,
  6168. @"cadedate" : @{
  6169. @"count" : @(6),
  6170. @"val_0" : @{
  6171. @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  6172. @"value" : @"Check",
  6173. @"value_id" : @"Check"
  6174. },
  6175. @"val_1" : @{
  6176. @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  6177. @"value" : @"Cash",
  6178. @"value_id" : @"Cash"
  6179. },
  6180. @"val_2" : @{
  6181. @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  6182. @"value" : @"NET 30",
  6183. @"value_id" : @"NET 30"
  6184. },
  6185. @"val_3" : @{
  6186. @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  6187. @"value" : @"Wire Transfer",
  6188. @"value_id" : @"Wire Transfer"
  6189. },
  6190. @"val_4" : @{
  6191. @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  6192. @"sub_item" : @{
  6193. @"count" : @(3),
  6194. @"item_0" : @{
  6195. @"aname" : @"choose",
  6196. @"control" : @"multi_action",
  6197. @"count" : @(1),
  6198. @"item_0" : @{
  6199. @"aname" : @"Same as customer",
  6200. @"key_map" : @{
  6201. @"credit_card_address1" : @"customer_address1",
  6202. @"credit_card_address2" : @"customer_address2",
  6203. @"credit_card_city" : @"customer_city",
  6204. @"credit_card_first_name" : @"customer_first_name",
  6205. @"credit_card_last_name" : @"customer_last_name",
  6206. @"credit_card_state" : @"customer_state",
  6207. @"credit_card_zipcode" : @"customer_zipcode"
  6208. },
  6209. @"type" : @"pull"
  6210. }
  6211. },
  6212. @"item_1" : @{
  6213. @"aname" : @"",
  6214. @"color" : @"red",
  6215. @"control" : @"text",
  6216. @"name" : @"",
  6217. @"value" : @"USA Credit cards only"
  6218. },
  6219. @"item_2" : @{
  6220. @"aname" : @"Fill",
  6221. @"cadedate" : @{
  6222. @"count" : @(2),
  6223. @"val_0" : @{
  6224. @"check" : @(1),
  6225. @"sub_item" : @{
  6226. @"count" : @(11),
  6227. @"item_0" : @{
  6228. @"aname" : @"Type",
  6229. @"cadedate" : @{
  6230. @"count" : @(2),
  6231. @"val_0" : @{
  6232. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  6233. @"value" : @"VISA",
  6234. @"value_id" : @(0)
  6235. },
  6236. @"val_1" : @{
  6237. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  6238. @"value" : @"MASTER CARD",
  6239. @"value_id" : @(1)
  6240. }
  6241. },
  6242. @"control" : @"enum",
  6243. @"name" : @"credit_card_type",
  6244. @"required" : @"true",
  6245. @"single_select" : @"true"
  6246. },
  6247. @"item_1" : @{
  6248. @"aname" : @"Number",
  6249. @"control" : @"edit",
  6250. @"keyboard" : @"int",
  6251. @"length" : @"16",
  6252. @"name" : @"credit_card_number",
  6253. @"required" : @"true",
  6254. @"value" : cardNumber
  6255. },
  6256. @"item_10" : @{
  6257. @"aname" : @"State",
  6258. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  6259. @"control" : @"enum",
  6260. @"enum" : @"true",
  6261. @"name" : @"credit_card_state",
  6262. @"required" : @"true",
  6263. @"single_select" : @"true"
  6264. },
  6265. @"item_2" : @{
  6266. @"aname" : @"Expiration Date",
  6267. @"control" : @"monthpicker",
  6268. @"name" : @"credit_card_expiration",
  6269. @"required" : @"true",
  6270. @"type" : @"date",
  6271. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  6272. },
  6273. @"item_3" : @{
  6274. @"aname" : @"Security Code",
  6275. @"control" : @"edit",
  6276. @"keyboard" : @"int",
  6277. @"length" : @"3",
  6278. @"name" : @"credit_card_security_code",
  6279. @"required" : @"true",
  6280. @"value" : securityCode
  6281. },
  6282. @"item_4" : @{
  6283. @"aname" : @"First Name",
  6284. @"control" : @"edit",
  6285. @"keyboard" : @"text",
  6286. @"name" : @"credit_card_first_name",
  6287. @"required" : @"true",
  6288. @"value" : firstName
  6289. },
  6290. @"item_5" : @{
  6291. @"aname" : @"Last Name",
  6292. @"control" : @"edit",
  6293. @"keyboard" : @"text",
  6294. @"name" : @"credit_card_last_name",
  6295. @"required" : @"true",
  6296. @"value" : lastName
  6297. },
  6298. @"item_6" : @{
  6299. @"aname" : @"Address 1",
  6300. @"control" : @"edit",
  6301. @"keyboard" : @"text",
  6302. @"name" : @"credit_card_address1",
  6303. @"required" : @"true",
  6304. @"value" : addr1
  6305. },
  6306. @"item_7" : @{
  6307. @"aname" : @"Address 2",
  6308. @"control" : @"edit",
  6309. @"keyboard" : @"text",
  6310. @"name" : @"credit_card_address2",
  6311. @"value" : addr2
  6312. },
  6313. @"item_8" : @{
  6314. @"aname" : @"zip code",
  6315. @"control" : @"edit",
  6316. @"keyboard" : @"text",
  6317. @"name" : @"credit_card_zipcode",
  6318. @"required" : @"true",
  6319. @"value" : zipcode
  6320. },
  6321. @"item_9" : @{
  6322. @"aname" : @"City",
  6323. @"control" : @"edit",
  6324. @"keyboard" : @"text",
  6325. @"name" : @"credit_card_city",
  6326. @"required" : @"true",
  6327. @"value" : city
  6328. }
  6329. },
  6330. @"value" : @"Fill Now",
  6331. @"value_id" : @""
  6332. },
  6333. @"val_1" : @{
  6334. @"check" : @(0),
  6335. @"value" : @"Fill Later",
  6336. @"value_id" : @""
  6337. }
  6338. },
  6339. @"control" : @"enum",
  6340. @"name" : @"",
  6341. @"single_select" : @"true"
  6342. }
  6343. },
  6344. @"value" : @"Credit Card",
  6345. @"value_id" : @"Credit Card"
  6346. },
  6347. @"val_5" : @{
  6348. @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  6349. @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  6350. @"value_id" : @"FOLLOW EXISTING"
  6351. }
  6352. },
  6353. @"control" : @"enum",
  6354. @"name" : @"paymentType",
  6355. @"single_select" : @"true"
  6356. },
  6357. @"title" : @"Payment Information"
  6358. }.mutableCopy;
  6359. NSNumber *hide = setting[@"PaymentInformationHide"];
  6360. [dic setValue:hide forKey:@"hide"];
  6361. return dic;
  6362. }
  6363. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  6364. {
  6365. sqlite3 *db = [iSalesDB get_db];
  6366. // UISetting
  6367. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  6368. NSString *cachefolder = [paths objectAtIndex:0];
  6369. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  6370. NSData* json =nil;
  6371. json=[NSData dataWithContentsOfFile:img_cache];
  6372. NSError *error=nil;
  6373. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  6374. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  6375. [params setObject:setting forKey:@"setting"];
  6376. int section_count = 0;
  6377. // params
  6378. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6379. orderCode = [self translateSingleQuote:orderCode];
  6380. // 0 Order Type 1 Shipping Method 2 Payment Information
  6381. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  6382. // 0 Order Type
  6383. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  6384. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  6385. [ret setValue:order_type_dic forKey:key0];
  6386. // 1 Shipping Method
  6387. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  6388. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  6389. [ret setValue:shipping_method_dic forKey:key1];
  6390. // 2 Payment Information
  6391. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  6392. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  6393. [ret setValue:payment_info_dic forKey:key2];
  6394. // 3 Customer
  6395. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  6396. NSDictionary *customer_dic = [self customerDic:params db:db];
  6397. [ret setValue:customer_dic forKey:key3];
  6398. // 4 Ship To
  6399. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  6400. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  6401. [ret setValue:ship_to_dic forKey:key4];
  6402. // 5 Ship From
  6403. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  6404. if (![shipFromDisable integerValue]) {
  6405. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  6406. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  6407. [ret setValue:ship_from_dic forKey:key5];
  6408. }
  6409. // 6 Freight Bill To
  6410. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  6411. if (![freightBillToDisable integerValue]) {
  6412. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  6413. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  6414. [ret setValue:freight_bill_to forKey:key6];
  6415. }
  6416. // 7 Merchandise Bill To
  6417. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  6418. if (![merchandiseBillToDisable integerValue]) {
  6419. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  6420. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  6421. [ret setValue:merchandise_bill_to_dic forKey:key7];
  6422. }
  6423. // 8 Return To
  6424. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  6425. if (![returnToDisable integerValue]) {
  6426. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  6427. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  6428. [ret setValue:return_to_dic forKey:key8];
  6429. }
  6430. // 9 Model Information
  6431. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  6432. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  6433. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  6434. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  6435. [ret setValue:model_info_dic forKey:key9];
  6436. // 10 Remarks Content
  6437. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  6438. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  6439. [ret setValue:remarks_content_dic forKey:key10];
  6440. // 11 Order Total
  6441. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  6442. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  6443. [ret setValue:order_total_dic forKey:key11];
  6444. // 12 Signature
  6445. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  6446. NSDictionary *sign_dic = [self signatureDic:params db:db];
  6447. [ret setValue:sign_dic forKey:key12];
  6448. [ret setValue:@(section_count) forKey:@"section_count"];
  6449. [iSalesDB close_db:db];
  6450. return [RAUtils dict2data:ret];
  6451. // return nil;
  6452. }
  6453. #pragma mark addr editor
  6454. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  6455. NSMutableDictionary *new_item = [item mutableCopy];
  6456. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  6457. return new_item;
  6458. }
  6459. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  6460. {
  6461. // "is_subaction" = true;
  6462. // orderCode = MOB1608240002;
  6463. // password = 123456;
  6464. // "subaction_tag" = 1;
  6465. // user = EvanK;
  6466. // {
  6467. // "is_subaction" = true;
  6468. // orderCode = MOB1608240002;
  6469. // password = 123456;
  6470. // "refresh_trigger" = zipcode;
  6471. // "subaction_tag" = 1;
  6472. // user = EvanK;
  6473. // }
  6474. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  6475. [ret removeObjectForKey:@"up_params"];
  6476. [ret setObject:@"New Address" forKey:@"title"];
  6477. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  6478. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  6479. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  6480. NSMutableDictionary *country_dic = [section_0[@"item_5"] mutableCopy];
  6481. [country_dic removeObjectForKey:@"refresh"];
  6482. [country_dic removeObjectForKey:@"restore"];
  6483. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  6484. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  6485. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_6"] add:YES] forKey:@"item_2"]; // Address 1*
  6486. [new_section_0 setObject:section_0[@"item_7"] forKey:@"item_3"]; // Address 2
  6487. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_4"]; // Address 3
  6488. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_5"]; // Address 4
  6489. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_10"] add:YES];
  6490. [zip_code_dic removeObjectForKey:@"refresh"];
  6491. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  6492. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_11"] add:YES] forKey:@"item_7"]; // State/Province*
  6493. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_8"]; // City*
  6494. [new_section_0 setObject:section_0[@"item_13"] forKey:@"item_9"]; // Contact First Name
  6495. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_10"]; // Contact Last Name
  6496. [new_section_0 setObject:section_0[@"item_4"] forKey:@"item_11"]; // Phone*
  6497. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_12"]; // Fax
  6498. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_3"] add:NO] forKey:@"item_13"]; // Email
  6499. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  6500. // country
  6501. NSString *countryCode = @"US";
  6502. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  6503. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  6504. // state
  6505. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:nil] mutableCopy];
  6506. NSDictionary *tmpDic = @{
  6507. @"value" : @"Other",
  6508. @"value_id" : @"",
  6509. @"check" : [NSNumber numberWithInteger:0]
  6510. };
  6511. for (int i = 0; i < allState.allKeys.count; i++) {
  6512. NSString *key = [NSString stringWithFormat:@"val_%d",i];
  6513. NSDictionary *tmp = allState[key];
  6514. [allState setValue:tmpDic forKey:key];
  6515. tmpDic = tmp;
  6516. }
  6517. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  6518. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  6519. [ret setValue:new_section_0 forKey:@"section_0"];
  6520. return [RAUtils dict2data:ret];
  6521. }
  6522. #pragma mark save addr
  6523. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  6524. {
  6525. // NSString *companyName = [self valueInParams:params key:@"company"];
  6526. // NSString *addr1 = [self valueInParams:params key:@"address"];
  6527. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  6528. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  6529. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  6530. // NSString *countryId = [self valueInParams:params key:@"country"];
  6531. // NSString *stateCode = [self valueInParams:params key:@"state"];
  6532. // NSString *city = [self valueInParams:params key:@"city"];
  6533. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  6534. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  6535. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  6536. // NSString *phone = [self valueInParams:params key:@"phone"];
  6537. // NSString *fax = [self valueInParams:params key:@"fax"];
  6538. // NSString *email = [self valueInParams:params key:@"email"];
  6539. return [self offline_saveContact:params update:NO isCustomer:NO];
  6540. }
  6541. #pragma mark cancel order
  6542. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  6543. {
  6544. // NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6545. NSString *order_id = [self valueInParams:params key:@"orderId"];
  6546. // NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  6547. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  6548. int ret = [iSalesDB execSql:sql];
  6549. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6550. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6551. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6552. // [dic setValue:@"160409" forKey:@"min_ver"];
  6553. return [RAUtils dict2data:dic];
  6554. }
  6555. #pragma mark sign order
  6556. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  6557. {
  6558. //参考 offline_saveBusinesscard
  6559. NSLog(@"sign order params: %@",params);
  6560. // orderCode = MOB1608240002;
  6561. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  6562. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6563. orderCode = [self translateSingleQuote:orderCode];
  6564. NSString *picPath = [self valueInParams:params key:@"picpath"];
  6565. picPath = [self translateSingleQuote:picPath];
  6566. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  6567. int ret = [iSalesDB execSql:sql];
  6568. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6569. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6570. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6571. // [dic setValue:@"160409" forKey:@"min_ver"];
  6572. return [RAUtils dict2data:dic];
  6573. }
  6574. #pragma mark save order
  6575. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  6576. NSString *ret = [self valueInParams:params key:key];
  6577. if (translate) {
  6578. ret = [self translateSingleQuote:ret];
  6579. }
  6580. return ret;
  6581. }
  6582. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  6583. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  6584. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  6585. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  6586. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  6587. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  6588. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  6589. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  6590. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  6591. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  6592. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  6593. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  6594. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  6595. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  6596. if (![total_price isEqualToString:@""]) {
  6597. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  6598. } else {
  6599. total_price = @"";
  6600. }
  6601. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  6602. if ([paymentsAndCredits isEqualToString:@""]) {
  6603. paymentsAndCredits = @"";
  6604. } else {
  6605. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  6606. }
  6607. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  6608. if ([handling_fee_value isEqualToString:@""]) {
  6609. handling_fee_value = @"";
  6610. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  6611. handling_fee_value = @"";
  6612. } else {
  6613. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  6614. }
  6615. NSString *handling_fee_placeholder = handling_fee_value;
  6616. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  6617. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  6618. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  6619. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  6620. NSString *lift_gate_placeholder = @"";
  6621. if ([lift_gate_value isEqualToString:@""]) {
  6622. lift_gate_placeholder = @"";
  6623. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  6624. lift_gate_placeholder = @"";
  6625. } else {
  6626. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  6627. }
  6628. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  6629. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  6630. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  6631. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  6632. 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];
  6633. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  6634. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  6635. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  6636. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  6637. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  6638. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  6639. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  6640. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  6641. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  6642. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  6643. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  6644. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  6645. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  6646. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  6647. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  6648. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  6649. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  6650. BOOL number_nil = [credit_card_number isEqualToString:@""];
  6651. if (!number_nil) {
  6652. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  6653. }
  6654. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  6655. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  6656. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  6657. if (!security_code_nil) {
  6658. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  6659. }
  6660. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  6661. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  6662. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  6663. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  6664. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  6665. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  6666. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  6667. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  6668. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  6669. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  6670. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  6671. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  6672. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  6673. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  6674. NSString *contact_id = customer_cid;
  6675. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  6676. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  6677. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  6678. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  6679. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  6680. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  6681. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  6682. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  6683. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  6684. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  6685. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  6686. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  6687. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  6688. //
  6689. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  6690. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  6691. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  6692. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  6693. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  6694. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  6695. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  6696. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  6697. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  6698. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  6699. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  6700. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  6701. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  6702. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  6703. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  6704. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  6705. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  6706. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  6707. 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];
  6708. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  6709. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  6710. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  6711. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  6712. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  6713. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  6714. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  6715. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  6716. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  6717. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  6718. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  6719. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  6720. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  6721. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  6722. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  6723. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  6724. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  6725. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  6726. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  6727. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  6728. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  6729. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  6730. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  6731. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  6732. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  6733. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  6734. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  6735. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  6736. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  6737. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  6738. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  6739. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  6740. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  6741. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  6742. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  6743. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  6744. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  6745. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  6746. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  6747. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  6748. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  6749. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  6750. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  6751. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  6752. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  6753. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  6754. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  6755. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  6756. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  6757. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  6758. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  6759. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  6760. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  6761. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  6762. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  6763. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  6764. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  6765. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  6766. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  6767. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  6768. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  6769. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  6770. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  6771. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  6772. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  6773. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  6774. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  6775. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  6776. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  6777. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  6778. NSString *order_status = @"status = 1,";
  6779. if (submit) {
  6780. order_status = @"status = -11,";
  6781. }
  6782. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  6783. NSString *sync_sql = @"";
  6784. if (submit) {
  6785. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  6786. [param setValue:sales_rep forKey:@"sales_rep"];
  6787. NSString *sync_data = [self translateSingleQuote:[RAUtils dict2string:param]];
  6788. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  6789. }
  6790. 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];
  6791. DebugLog(@"save order contactSql: %@",contactSql);
  6792. DebugLog(@"save order orderSql: %@",orderSql);
  6793. // int contact_ret = [iSalesDB execSql:contactSql];
  6794. int order_ret = [iSalesDB execSql:orderSql];
  6795. int ret = order_ret;
  6796. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6797. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6798. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6799. // [dic setValue:@"160409" forKey:@"min_ver"];
  6800. [dic setObject:so_id forKey:@"so#"];
  6801. return [RAUtils dict2data:dic];
  6802. }
  6803. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  6804. {
  6805. // id foo = nil;
  6806. // NSMutableArray *a = @[].mutableCopy;
  6807. // [a addObject:foo];
  6808. return [self saveorder:param submit:NO];
  6809. }
  6810. #pragma mark add to cart by name
  6811. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  6812. {
  6813. NSString *orderCode = [params objectForKey:@"orderCode"];
  6814. NSString *product_name = [params objectForKey:@"product_name"];
  6815. product_name = [self translateSingleQuote:product_name];
  6816. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  6817. sqlite3 *db = [iSalesDB get_db];
  6818. __block NSMutableString *product_id_string = [NSMutableString string];
  6819. for (int i = 0; i < product_name_array.count; i++) {
  6820. NSString *name = [product_name_array objectAtIndex:i];
  6821. NSString *sql = [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  6822. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6823. int product_id = sqlite3_column_int(stmt, 0);
  6824. if (i == product_name_array.count - 1) {
  6825. [product_id_string appendFormat:@"%d",product_id];
  6826. } else {
  6827. [product_id_string appendFormat:@"%d,",product_id];
  6828. }
  6829. }];
  6830. }
  6831. NSDictionary *newParams = @{
  6832. @"product_id" : product_id_string,
  6833. @"orderCode" : orderCode
  6834. };
  6835. [iSalesDB close_db:db];
  6836. return [self offline_add2cart:[newParams mutableCopy]];
  6837. }
  6838. #pragma mark reset order
  6839. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  6840. {
  6841. UIApplication * app = [UIApplication sharedApplication];
  6842. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  6843. appDelegate.disable_trigger=true;
  6844. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  6845. appDelegate.disable_trigger=false;
  6846. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6847. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  6848. return [RAUtils dict2data:dic];
  6849. }
  6850. #pragma mark submit order
  6851. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  6852. {
  6853. return [self saveorder:params submit:YES];
  6854. }
  6855. #pragma mark copy order
  6856. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  6857. {
  6858. NSMutableDictionary *ret = @{}.mutableCopy;
  6859. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  6860. sqlite3 *db = [iSalesDB get_db];
  6861. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  6862. // 首先查看联系人是否active
  6863. 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];
  6864. __block int customer_is_active = 1;
  6865. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6866. customer_is_active = sqlite3_column_int(stmt, 0);
  6867. }];
  6868. if (!customer_is_active) {
  6869. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  6870. [iSalesDB execSql:@"END TRANSACTION" db:db];
  6871. [iSalesDB close_db:db];
  6872. return [RAUtils dict2data:ret];
  6873. }
  6874. // new order
  6875. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  6876. NSString *new_order_code = [self get_offline_soid:db];
  6877. NSString *user = ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  6878. user = [self translateSingleQuote:user];
  6879. 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
  6880. __block int result = 1;
  6881. result = [iSalesDB execSql:insert_order_sql db:db];
  6882. if (!result) {
  6883. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  6884. [iSalesDB execSql:@"END TRANSACTION" db:db];
  6885. [iSalesDB close_db:db];
  6886. return [RAUtils dict2data:ret];
  6887. }
  6888. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  6889. // __block NSString *product_id = @"";
  6890. __weak typeof(self) weakSelf = self;
  6891. 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];
  6892. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6893. int is_active = sqlite3_column_int(stmt, 1);
  6894. if (is_active) {
  6895. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  6896. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  6897. 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];
  6898. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  6899. }
  6900. }];
  6901. // product_id = [product_id substringFromIndex:1];
  6902. //
  6903. // [self offline_add2cart:@{}.mutableCopy];
  6904. [iSalesDB execSql:@"END TRANSACTION" db:db];
  6905. [iSalesDB close_db:db];
  6906. if (result) {
  6907. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  6908. } else {
  6909. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  6910. }
  6911. [ret setObject:new_order_code forKey:@"so_id"];
  6912. return [RAUtils dict2data:ret];
  6913. }
  6914. #pragma mark move wish list to cart
  6915. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  6916. NSString *collectId = params[@"collectId"];
  6917. NSString *sql = [NSString stringWithFormat:@"select product_id,qty from wishlist where _id in (%@);",collectId];
  6918. __block NSString *product_id = @"";
  6919. __block NSString *qty = @"";
  6920. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6921. int productId = sqlite3_column_int(stmt, 0);
  6922. int item_qty = sqlite3_column_int(stmt, 1);
  6923. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  6924. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  6925. }];
  6926. product_id = [product_id substringFromIndex:1];
  6927. qty = [qty substringFromIndex:1];
  6928. NSDictionary *newParams = @{
  6929. @"product_id" : product_id,
  6930. @"orderCode" : params[@"orderCode"],
  6931. @"qty" : qty
  6932. };
  6933. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  6934. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  6935. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  6936. // NSString *deleteSQL = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  6937. //
  6938. // int ret = [iSalesDB execSql:deleteSQL];
  6939. int ret = [[[self offline_deletewishlist:@{@"collectId" : collectId}.mutableCopy] valueForKey:@"result"] integerValue];
  6940. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  6941. }
  6942. return [RAUtils dict2data:retDic];
  6943. }
  6944. #pragma mark - portfolio
  6945. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  6946. int sort = [[params valueForKey:@"sort"] integerValue];
  6947. int offset = [[params valueForKey:@"offset"] integerValue];
  6948. int limit = [[params valueForKey:@"limit"] integerValue];
  6949. NSString *orderBy = @"";
  6950. switch (sort) {
  6951. case 0:{
  6952. orderBy = @"modifytime desc";
  6953. }
  6954. break;
  6955. case 1:{
  6956. orderBy = @"modifytime asc";
  6957. }
  6958. break;
  6959. case 2:{
  6960. orderBy = @"name asc";
  6961. }
  6962. break;
  6963. case 3:{
  6964. orderBy = @"name desc";
  6965. }
  6966. break;
  6967. case 4:{
  6968. orderBy = @"description asc";
  6969. }
  6970. break;
  6971. default:
  6972. break;
  6973. }
  6974. NSString *sql = [NSString stringWithFormat:@"select product_id,name,description,ifnull(sheet_price,'null'),sheet_discount,ifnull(available_qty,'null'),percentage,item_id,fashion_id,img,line_note,percent from offline_portfolio order by %@ limit %d offset %d;",orderBy,limit,offset];
  6975. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6976. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6977. sqlite3 *db = [iSalesDB get_db];
  6978. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6979. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  6980. int product_id = sqlite3_column_int(stmt, 0);
  6981. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  6982. NSString *name = [self textAtColumn:1 statement:stmt];
  6983. NSString *description = [self textAtColumn:2 statement:stmt];
  6984. double price = sqlite3_column_double(stmt, 3);
  6985. double discount = sqlite3_column_double(stmt,4);
  6986. int qty = sqlite3_column_int(stmt, 5);
  6987. int percentage = sqlite3_column_int(stmt, 6);
  6988. int item_id = sqlite3_column_int(stmt, 7);
  6989. // int fashion_id = sqlite3_column_int(stmt, 8);
  6990. NSString *img_path = [self textAtColumn:9 statement:stmt];
  6991. NSString *line_note = [self textAtColumn:10 statement:stmt];
  6992. double percent = sqlite3_column_double(stmt, 11);
  6993. NSString *price_null = [self textAtColumn:3 statement:stmt];
  6994. if ([price_null isEqualToString:@"null"]) {
  6995. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  6996. }
  6997. NSMutableDictionary *item = @{
  6998. @"linenotes": line_note,
  6999. @"check": @(1),
  7000. @"product_id": product_id_string,
  7001. @"available_qty": @(qty),
  7002. @"available_percent" : @(percent),
  7003. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  7004. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  7005. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  7006. @"img": img_path,
  7007. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  7008. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  7009. }.mutableCopy;
  7010. if (percentage) {
  7011. [item removeObjectForKey:@"available_qty"];
  7012. } else {
  7013. [item removeObjectForKey:@"available_percent"];
  7014. }
  7015. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  7016. if ([qty_null isEqualToString:@"null"]) {
  7017. [item removeObjectForKey:@"available_qty"];
  7018. }
  7019. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  7020. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  7021. }];
  7022. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  7023. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7024. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  7025. }
  7026. [iSalesDB close_db:db];
  7027. [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  7028. [dic setValue:@"" forKey:@"email_content"];
  7029. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  7030. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7031. return [RAUtils dict2data:dic];
  7032. }
  7033. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  7034. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  7035. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  7036. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  7037. tear_name = [self translateSingleQuote:tear_name];
  7038. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  7039. tear_note = [self translateSingleQuote:tear_note];
  7040. NSString *pdf_path = [self valueInParams:params key:@"pdfUrl"];
  7041. pdf_path = [self translateSingleQuote:pdf_path];
  7042. NSString *create_user = [self valueInParams:params key:@"user"];
  7043. create_user = [self translateSingleQuote:create_user];
  7044. NSString *product_ids = [Singleton sharedInstance].pdf_product_ids;
  7045. NSString *item_ids = [Singleton sharedInstance].pdf_item_ids;
  7046. [Singleton sharedInstance].pdf_product_ids = nil;
  7047. [Singleton sharedInstance].pdf_item_ids = nil;
  7048. 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];
  7049. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7050. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7051. sqlite3 *db = [iSalesDB get_db];
  7052. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7053. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  7054. int product_id = sqlite3_column_int(stmt, 0);
  7055. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  7056. double price = sqlite3_column_double(stmt, 1);
  7057. double discount = sqlite3_column_double(stmt,2);
  7058. int qty = sqlite3_column_int(stmt, 3);
  7059. int percentage = sqlite3_column_int(stmt, 4);
  7060. int item_id = sqlite3_column_int(stmt, 5);
  7061. NSString *line_note = [self textAtColumn:6 statement:stmt];
  7062. double percent = sqlite3_column_double(stmt, 7);
  7063. NSString *price_null = [self textAtColumn:3 statement:stmt];
  7064. if ([price_null isEqualToString:@"null"]) {
  7065. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  7066. }
  7067. NSMutableDictionary *item = @{
  7068. @"linenotes": line_note,
  7069. @"product_id": product_id_string,
  7070. @"available_qty": @(qty),
  7071. @"available_percent" : @(percent),
  7072. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  7073. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  7074. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  7075. }.mutableCopy;
  7076. if (percentage) {
  7077. [item removeObjectForKey:@"available_qty"];
  7078. } else {
  7079. [item removeObjectForKey:@"available_percent"];
  7080. }
  7081. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  7082. if ([qty_null isEqualToString:@"null"]) {
  7083. [item removeObjectForKey:@"available_qty"];
  7084. }
  7085. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  7086. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  7087. }];
  7088. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  7089. [iSalesDB close_db:db];
  7090. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  7091. return [RAUtils dict2data:resultDictionary];
  7092. }
  7093. NSString *model_info = [RAUtils dict2string:dic];
  7094. model_info = [self translateSingleQuote:model_info];
  7095. NSString *save_pdf_sql = [NSString stringWithFormat:@"insert into offline_pdf (tearsheets_id,pdf_path,create_user,tear_note,tear_name,model_info) values ((select ifnull(max(tearsheets_id),0) from offline_pdf) + 1,'%@','%@','%@','%@','%@');",pdf_path,create_user,tear_note,tear_name,model_info];
  7096. int result = [iSalesDB execSql:save_pdf_sql db:db];
  7097. [iSalesDB close_db:db];
  7098. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  7099. return [RAUtils dict2data:resultDictionary];
  7100. }
  7101. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  7102. int offset = [[params valueForKey:@"offset"] integerValue];
  7103. int limit = [[params valueForKey:@"limit"] integerValue];
  7104. NSString *keyword = [params valueForKey:@"keyWord"];
  7105. NSString *where = @"";
  7106. if (keyword.length) {
  7107. keyword = [self translateSingleQuote:keyword];
  7108. where = [NSString stringWithFormat:@"where lower(create_user) like '%%%@%%' or lower(tear_note) like '%%%@%%' or lower(tear_name) like '%%%@%%'",keyword.lowercaseString,keyword.lowercaseString,keyword.lowercaseString];
  7109. }
  7110. 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];
  7111. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7112. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7113. NSString *name = [self textAtColumn:0 statement:stmt];
  7114. NSString *note = [self textAtColumn:1 statement:stmt];
  7115. NSString *time = [self textAtColumn:2 statement:stmt];
  7116. NSString *user = [self textAtColumn:3 statement:stmt];
  7117. NSString *patch = [self textAtColumn:4 statement:stmt];
  7118. time = [self changeDateTimeFormate:time];
  7119. time = [time stringByAppendingString:@" PST"];
  7120. int sheet_id = sqlite3_column_int(stmt, 5);
  7121. NSMutableDictionary *item = @{
  7122. @"tearsheetsId": @(sheet_id),
  7123. @"pdf_path": patch,
  7124. @"create_time": time,
  7125. @"create_user": user,
  7126. @"tear_note": note,
  7127. @"tear_name": name
  7128. }.mutableCopy;
  7129. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  7130. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  7131. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  7132. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7133. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  7134. }];
  7135. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7136. return [RAUtils dict2data:dic];
  7137. }
  7138. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  7139. NSString *prodct_ids = [params objectForKey:@"product_id"];
  7140. // NSString *user = [params objectForKey:@"user"];
  7141. NSString *sql = [NSString stringWithFormat:@"insert into offline_portfolio (product_id,name,description,item_id,img) select * from (select p.product_id,p.name,p.description,p.item_id,i.url from product as p left join model_image as i on i.product_id = p.product_id where p.product_id in (%@) and p.product_id not in (select product_id from offline_portfolio) and i.type = 1 group by p.product_id order by i.default_img desc, i._id asc );",prodct_ids];
  7142. int result = [iSalesDB execSql:sql];
  7143. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7144. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  7145. if (result == RESULT_TRUE) {
  7146. NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  7147. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];
  7148. }
  7149. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7150. return [RAUtils dict2data:dic];
  7151. }
  7152. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  7153. // {
  7154. // "item_ids" = "249,3859,5360,2662";
  7155. // password = 123456;
  7156. // "product_ids" = "3321,3804,4045,3732";
  7157. // user = EvanK;
  7158. // }
  7159. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  7160. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  7161. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  7162. return [RAUtils dict2data:dic];
  7163. }
  7164. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  7165. NSString *product_id = [params objectForKey:@"fashionId"];
  7166. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  7167. __block int result = RESULT_TRUE;
  7168. __block int qty = 0;
  7169. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7170. qty = sqlite3_column_int(stmt, 0);
  7171. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  7172. result = RESULT_FALSE;
  7173. }];
  7174. NSMutableDictionary *dic = @{
  7175. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  7176. @"mode" : @"Regular Mode",
  7177. @"quantity_available" : @(qty),
  7178. @"result" : @(result),
  7179. }.mutableCopy;
  7180. return [RAUtils dict2data:dic];
  7181. }
  7182. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  7183. NSString *item_ids = [params objectForKey:@"item_id"];
  7184. NSString *line_notes = [params objectForKey:@"notes"];
  7185. NSString *price_str = [params objectForKey:@"price"];
  7186. NSString *discount_str = [params objectForKey:@"discount"];
  7187. NSString *percent = [params objectForKey:@"available_percent"];
  7188. NSString *qty = [params objectForKey:@"available_qty"];
  7189. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  7190. int dot = 0;
  7191. if (line_notes) {
  7192. line_notes = [self translateSingleQuote:line_notes];
  7193. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  7194. sql = [sql stringByAppendingString:line_notes];
  7195. dot = 1;
  7196. } else {
  7197. line_notes = @"";
  7198. }
  7199. if (price_str) {
  7200. if (dot) {
  7201. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  7202. } else {
  7203. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  7204. dot = 1;
  7205. }
  7206. sql = [sql stringByAppendingString:price_str];
  7207. } else {
  7208. price_str = @"";
  7209. }
  7210. if (discount_str) {
  7211. if (dot) {
  7212. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  7213. } else {
  7214. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  7215. dot = 1;
  7216. }
  7217. sql = [sql stringByAppendingString:discount_str];
  7218. } else {
  7219. discount_str = @"";
  7220. }
  7221. if (percent) {
  7222. if (dot) {
  7223. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  7224. } else {
  7225. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  7226. dot = 1;
  7227. }
  7228. sql = [sql stringByAppendingString:percent];
  7229. } else {
  7230. percent = @"";
  7231. }
  7232. if (qty) {
  7233. if (dot) {
  7234. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  7235. } else {
  7236. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  7237. dot = 1;
  7238. }
  7239. sql = [sql stringByAppendingString:qty];
  7240. } else {
  7241. qty = @"";
  7242. }
  7243. NSString *where = [NSString stringWithFormat:@" where item_id in (%@);",item_ids];
  7244. sql = [sql stringByAppendingString:where];
  7245. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7246. int result = [iSalesDB execSql:sql];
  7247. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  7248. [dic setObject:@"Regular Mode" forKey:@"mode"];
  7249. return [RAUtils dict2data:dic];
  7250. }
  7251. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  7252. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  7253. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7254. sqlite3 *db = [iSalesDB get_db];
  7255. NSString *sql = [NSString stringWithFormat:@"delete from offline_portfolio where item_id in (%@);",portfolio_ids];
  7256. int result = [iSalesDB execSql:sql db:db];
  7257. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  7258. [dic setObject:@"Regular Mode" forKey:@"mode"];
  7259. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"1=1"];
  7260. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  7261. [iSalesDB close_db:db];
  7262. return [RAUtils dict2data:dic];
  7263. }
  7264. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  7265. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7266. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  7267. NSString *user = [params objectForKey:@"user"];
  7268. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where tearsheets_id = %d;",tearsheetsId]];
  7269. if (![create_user isEqualToString:user]) {
  7270. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  7271. [dic setObject:@"Regular Mode" forKey:@"mode"];
  7272. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  7273. return [RAUtils dict2data:dic];
  7274. }
  7275. NSString *sql = [NSString stringWithFormat:@"delete from offline_pdf where tearsheets_id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  7276. int result = [iSalesDB execSql:sql];
  7277. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  7278. [dic setObject:@"Regular Mode" forKey:@"mode"];
  7279. return [RAUtils dict2data:dic];
  7280. }
  7281. @end