OLDataProvider.m 402 KB

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