OLDataProvider.m 377 KB

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