OLDataProvider.m 409 KB

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