OLDataProvider.m 430 KB

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