OLDataProvider.m 410 KB

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