OLDataProvider.m 381 KB

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