OLDataProvider.m 480 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566
  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. #import "QRCodeGenerator.h"
  20. @interface OLDataProvider ()
  21. @end
  22. @implementation OLDataProvider
  23. +(NSString*) get_portfolio_price:(NSString*) contact_id item_id:(int) item_id price:(int)price_id db:(sqlite3*)db
  24. {
  25. NSString * sqlQuery = [NSString stringWithFormat:@"select decrypt(price) from model_price where item_id=%d and price_name in (select name from price where type=%d) ",item_id,price_id];
  26. NSString* ret=@"No Price.";
  27. sqlite3_stmt * statement;
  28. // int count=0;
  29. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  30. {
  31. if (sqlite3_step(statement) == SQLITE_ROW)
  32. {
  33. char *price = (char*)sqlite3_column_text(statement, 0);
  34. if(price==nil)
  35. price="";
  36. NSString *nsprice = [[NSString alloc]initWithUTF8String:price];
  37. double dp= [nsprice doubleValue];
  38. ret=[NSString stringWithFormat:@"%.2f",dp];
  39. }
  40. sqlite3_finalize(statement);
  41. }
  42. return ret;
  43. // // [iSalesDB close_db:db];
  44. //
  45. // return nil;
  46. //// if(dprice==DBL_MAX)
  47. //// ret= nil;
  48. //// else
  49. //// ret= [NSNumber numberWithDouble:dprice];
  50. //// return ret;
  51. }
  52. + (NSString *)portfolio_img:(NSString *)item_id db:(sqlite3 *)db
  53. {
  54. NSString* ret= nil;
  55. NSString *sqlQuery = nil;
  56. // 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
  57. 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];
  58. sqlite3_stmt * statement;
  59. // int count=0;
  60. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  61. {
  62. if (sqlite3_step(statement) == SQLITE_ROW)
  63. {
  64. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  65. if(imgurl==nil)
  66. imgurl="";
  67. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  68. ret=nsimgurl;
  69. }
  70. sqlite3_finalize(statement);
  71. }
  72. else
  73. {
  74. ret=@"";
  75. }
  76. // [iSalesDB close_db:db];
  77. // DebugLog(@"data string: %@",ret );
  78. return ret;
  79. }
  80. +(NSMutableDictionary*) prepare_portfolio_data:(NSMutableDictionary*) params
  81. {
  82. NSMutableDictionary* values=params[@"replaceValue"];
  83. //生成portfolio pdf需要的数据
  84. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  85. data[@"npd_url"]=@"www.newpacificdirect.com";
  86. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  87. [formatter setDateFormat:@"MMMM yyyy"];
  88. NSString* date = [formatter stringFromDate:[NSDate date]];
  89. data[@"create_month"]=date;
  90. data[@"company_name"]=@"New Pacific Direct,Inc.";
  91. data[@"catalog_name"]=params[@"catalog_name"];
  92. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  93. data[@"TOTAL_PAGE"]=0;
  94. data[@"CURRENT_PAGE"]=0;
  95. NSMutableDictionary* grid = [[NSMutableDictionary alloc]init];
  96. bool show_stockout=[params[@"show_stockout"] boolValue];
  97. NSString *sql = nil;
  98. if(values==nil)
  99. {
  100. sql=[NSString stringWithFormat:@"select a.product_id,a.name,a.description,a.price,a.sheet_discount,a.qty,a.is_percentage,a.item_id,a.fashion_id,a.line_note,a.percent,a._id,b.availability,b.color,b.legcolor,b.demension,b.seat_height,b.material,b.box_dim,b.volume,b.weight,stockuom,b.load_ability,b.fabric_content,b.assembling,b.made_in from (select _id, product_id,name,description,ifnull(sheet_price,'null') as price,sheet_discount,ifnull(available_qty,'null') as qty,percentage as is_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 order by a.name",params[@"product_ids"],params[@"product_ids"]];
  101. }
  102. else
  103. {
  104. sql=[NSString stringWithFormat:@"select product_id,name,description,null,null,null,null,item_id,null,null,null,-1,availability,color,legcolor,demension,seat_height,material,box_dim,volume,weight,stockuom,load_ability,fabric_content,assembling,made_in from product where product_id in(%@) order by name",params[@"product_ids"]];
  105. }
  106. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  107. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  108. sqlite3 *db = [iSalesDB get_db];
  109. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  110. // [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  111. int product_id = sqlite3_column_int(stmt, 0);
  112. // NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  113. NSString *name = [self textAtColumn:1 statement:stmt];
  114. NSString *description = [self textAtColumn:2 statement:stmt];
  115. double price = sqlite3_column_double(stmt, 3);
  116. NSString *s_price = [self textAtColumn:3 statement:stmt];
  117. double discount = sqlite3_column_double(stmt,4);
  118. int qty = sqlite3_column_int(stmt, 5);
  119. NSString *s_qty = [self textAtColumn:5 statement:stmt];
  120. int is_percentage = sqlite3_column_int(stmt, 6);
  121. NSString *s_is_percentage = [self textAtColumn:6 statement:stmt];
  122. int item_id = sqlite3_column_int(stmt, 7);
  123. // int fashion_id = sqlite3_column_int(stmt, 8);
  124. NSString *line_note = [self textAtColumn:9 statement:stmt];
  125. NSString *s_percent = [self textAtColumn:10 statement:stmt];
  126. double percent = sqlite3_column_double(stmt, 10);
  127. // int portfolio_id = sqlite3_column_int(stmt, 11);
  128. NSString *s_availability = [self textAtColumn:12 statement:stmt];
  129. int availability = sqlite3_column_int(stmt, 12);
  130. NSString *color = [self textAtColumn:13 statement:stmt];
  131. NSString *legcolor = [self textAtColumn:14 statement:stmt];
  132. NSString *demension = [self textAtColumn:15 statement:stmt];
  133. NSString *seat_height = [self textAtColumn:16 statement:stmt];
  134. NSString *material = [self textAtColumn:17 statement:stmt];
  135. NSString *box_dim = [self textAtColumn:18 statement:stmt];
  136. NSString *s_volume = [self textAtColumn:19 statement:stmt];
  137. double volume = sqlite3_column_double(stmt, 19);
  138. NSString *s_weight = [self textAtColumn:20 statement:stmt];
  139. double weight = sqlite3_column_double(stmt, 20);
  140. NSString *s_model_set = [self textAtColumn:21 statement:stmt];
  141. int model_set = sqlite3_column_int(stmt, 21);
  142. NSString *s_load_ability = [self textAtColumn:22 statement:stmt];
  143. double load_ability = sqlite3_column_double(stmt, 22);
  144. NSString *fabric_content = [self textAtColumn:23 statement:stmt];
  145. NSString *assembling = [self textAtColumn:24 statement:stmt];
  146. NSString *made_in = [self textAtColumn:25 statement:stmt];
  147. NSString* gprice = [self get_portfolio_price :appDelegate.contact_id item_id:item_id price:[params[@"regular_price"] intValue] db:db];
  148. if(is_percentage==0&&qty==0&&!show_stockout)
  149. {
  150. return;
  151. }
  152. if((is_percentage!=0||s_is_percentage.length==0)&&availability==0&&!show_stockout)
  153. {
  154. return;
  155. }
  156. if(s_percent.length==0||is_percentage==0)
  157. {
  158. percent=100.0;
  159. if([s_qty isEqualToString:@"null"])
  160. qty=availability;
  161. }
  162. else
  163. {
  164. qty=availability;
  165. }
  166. qty=qty*percent/100+0.5;
  167. for(int i=0;i<[values[@"count"] intValue];i++)
  168. {
  169. NSMutableDictionary * valueitem=values[[NSString stringWithFormat:@"item_%d",i]];;
  170. if([valueitem[@"product_id"] intValue]==product_id)
  171. {
  172. line_note=valueitem[@"line_note"];
  173. qty=[valueitem[@"available_qty"] intValue];
  174. if([[valueitem allKeys] containsObject:@"available_qty"])
  175. s_qty=[NSString stringWithFormat:@"%d",qty];
  176. else
  177. s_qty=@"null";
  178. if(valueitem[@"regular_price"]!=nil)
  179. gprice=[NSString stringWithFormat:@"%.2f",[valueitem[@"regular_price"] floatValue]];
  180. s_price=valueitem[@"special_price"];
  181. price=[valueitem[@"special_price"] floatValue];
  182. discount=0;
  183. }
  184. }
  185. NSString* set_price=@"";
  186. if([params[@"entered_price"] boolValue])
  187. {
  188. if (s_price==nil ) {
  189. // set_price=[NSString stringWithFormat:@"Special: $ %@",gprice];
  190. }
  191. else
  192. {
  193. if(price*(1-discount/100.0)!=gprice.floatValue)
  194. set_price=[NSString stringWithFormat:@"Special: $ %.2f",price*(1-discount/100.0)];
  195. }
  196. }
  197. NSString* get_price=@"";
  198. {
  199. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  200. // DebugLog(@"price time interval");
  201. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  202. if(gprice==nil)
  203. get_price=@"Price:No Price.";
  204. else
  205. get_price=[NSString stringWithFormat:@"Price: $ %@",gprice];
  206. }
  207. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  208. // if ([qty_null isEqualToString:@"null"]) {
  209. // qty=availability;
  210. // }
  211. //
  212. // NSString *qty_percent_null = [self textAtColumn:6 statement:stmt];
  213. // if ([qty_percent_null isEqualToString:@"null"]) {
  214. // percentage=1;
  215. // }
  216. NSString * str_availability=[NSString stringWithFormat:@"<b>Availability:</b> %d",(int)qty];
  217. NSString * str_color=[NSString stringWithFormat:@"<b>Color:</b> %@",color];
  218. NSString * str_model_set=[NSString stringWithFormat:@"<b>Set:</b> Sold in QTY of %d",model_set];
  219. NSString * str_legcolor=[NSString stringWithFormat:@"<b>Leg Color:</b> %@",legcolor];
  220. NSString * str_demension=[NSString stringWithFormat:@"<b>Dimension:</b> %@",demension];
  221. NSString * str_seat_height=[NSString stringWithFormat:@"<b>Seat Height:</b> %@",seat_height];
  222. NSString * str_meterial=[NSString stringWithFormat:@"<b>Meterial:</b> %@",material];
  223. NSString * str_box_dim=[NSString stringWithFormat:@"<b>Box Dimension:</b> %@",box_dim];
  224. NSString * str_volume=[NSString stringWithFormat:@"<b>Volume:</b> %.2f",volume];
  225. NSString * str_weight=[NSString stringWithFormat:@"<b>Weight:</b> %.2f",weight];
  226. NSString * str_load_ability=[NSString stringWithFormat:@"<b>Load Ability:</b> %.2f",load_ability];
  227. NSString * str_fabric_content=[NSString stringWithFormat:@"<b>Fabric Content:</b> %@",fabric_content];
  228. NSString * str_assembling=[NSString stringWithFormat:@"<b>Assembling:</b> %@",assembling];
  229. NSString * str_made_in=[NSString stringWithFormat:@"<b>Made In:</b> %@",made_in];
  230. NSString * str_line_note=[NSString stringWithFormat:@"<b>Line Note:</b> %@",line_note];
  231. if([params[@"availability"] boolValue]==false)
  232. str_availability=@"";
  233. else
  234. {
  235. if ([s_qty isEqualToString:@"null"] && [s_availability isEqualToString:@"0"])
  236. str_availability=@"<b>Availability:</b> In Production";
  237. }
  238. if([params[@"color"] boolValue]==false || color.length==0 )
  239. str_color=@"";
  240. if([params[@"set"] boolValue]==false || s_model_set.length==0 )
  241. str_model_set=@"";
  242. if([params[@"leg_color"] boolValue]==false || legcolor.length==0 )
  243. str_legcolor=@"";
  244. if([params[@"dimension"] boolValue]==false || demension.length==0 )
  245. str_demension=@"";
  246. if([params[@"seat_height"] boolValue]==false || seat_height.length==0 )
  247. str_seat_height=@"";
  248. if([params[@"material"] boolValue]==false || material.length==0 )
  249. str_meterial=@"";
  250. if([params[@"box_dimension"] boolValue]==false || box_dim.length==0 )
  251. str_box_dim=@"";
  252. if([params[@"volume"] boolValue]==false || s_volume.length==0 )
  253. str_volume=@"";
  254. if([params[@"weight"] boolValue]==false || s_weight.length==0 )
  255. str_weight=@"";
  256. if([params[@"load_ability"] boolValue]==false || s_load_ability.length==0 )
  257. str_load_ability=@"";
  258. if([params[@"fabric_content"] boolValue]==false || fabric_content.length==0 )
  259. str_fabric_content=@"";
  260. if([params[@"assembling"] boolValue]==false || assembling.length==0 )
  261. str_assembling=@"";
  262. if([params[@"made"] boolValue]==false || made_in.length==0 )
  263. str_made_in=@"";
  264. if([params[@"show_line_note"] boolValue]==false || line_note.length==0 )
  265. str_line_note=@"";
  266. NSMutableArray* arr_detail=[[NSMutableArray alloc]init];
  267. [arr_detail addObject:str_availability];
  268. [arr_detail addObject:str_color];
  269. [arr_detail addObject:str_model_set];
  270. [arr_detail addObject:str_legcolor];
  271. [arr_detail addObject:str_demension];
  272. [arr_detail addObject:str_seat_height];
  273. [arr_detail addObject:str_meterial];
  274. [arr_detail addObject:str_box_dim];
  275. [arr_detail addObject:str_volume];
  276. [arr_detail addObject:str_weight];
  277. [arr_detail addObject:str_load_ability];
  278. [arr_detail addObject:str_fabric_content];
  279. [arr_detail addObject:str_assembling];
  280. [arr_detail addObject:str_made_in];
  281. [arr_detail addObject:str_line_note];
  282. // NSString* detail =[NSString stringWithFormat:@"<font size=0.2>%@</font>",[RAUtils arr2string:arr_detail separator:@" " trim:true]];
  283. NSString* detail =[RAUtils arr2string:arr_detail separator:@" " trim:true];
  284. //model image;
  285. NSString* img_path = [self portfolio_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  286. // qr image
  287. NSString* qrpath=nil;
  288. if([params[@"show_barcode"] boolValue])
  289. {
  290. NSString* temp = NSTemporaryDirectory();
  291. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  292. qrpath=[temp stringByAppendingPathComponent:filename];
  293. UIImage* qrimg=[QRCodeGenerator qrImageForString:name imageSize:240];
  294. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  295. }
  296. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  297. item[@"img"]=img_path;
  298. item[@"img_qr"]=qrpath;
  299. item[@"special_price"]=set_price;
  300. item[@"price"]=get_price;
  301. item[@"name"]=name;
  302. item[@"description"]=description;
  303. item[@"detail"]=detail;
  304. item[@"img"]=img_path;
  305. // @{
  306. // //@"linenotes": line_note,
  307. //// @"product_id": product_id_string,
  308. //// @"available_qty": @(qty),
  309. //// @"available_percent" : @(percent),
  310. //// @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  311. //// @"item_id": [NSString stringWithFormat:@"%d",item_id],
  312. //// @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  313. // @"": ,
  314. // @"": ,
  315. // @"": ,
  316. // @"": ,
  317. // @"": ,
  318. // @"": ,
  319. // @"":
  320. // }.mutableCopy;
  321. // if (percentage) {
  322. // [item removeObjectForKey:@"available_qty"];
  323. // } else {
  324. // [item removeObjectForKey:@"available_percent"];
  325. // }
  326. //
  327. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  328. // if ([qty_null isEqualToString:@"null"]) {
  329. // [item removeObjectForKey:@"available_qty"];
  330. // }
  331. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  332. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  333. }];
  334. [iSalesDB close_db:db];
  335. // [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  336. // [dic setValue:@"" forKey:@"email_content"];
  337. // [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  338. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  339. //
  340. // NSMutableDictionary* cell = [[NSMutableDictionary alloc]init];
  341. // cell[@"count"]=[NSNumber numberWithInt:10];
  342. grid[@"cell0"]=dic;
  343. data[@"grid0"]=grid;
  344. return data;
  345. }
  346. +(NSMutableDictionary*) get_pdftemplate:(NSString*) template
  347. {
  348. NSString *default_path = [[NSBundle mainBundle] pathForResource:template ofType:nil];
  349. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  350. NSString *cache_folder=[paths objectAtIndex:0];
  351. NSString *template_path = [cache_folder stringByAppendingPathComponent:[NSString stringWithFormat:@"offline_data/%@",template]];
  352. BOOL bdir=NO;
  353. NSFileManager* fileManager = [NSFileManager defaultManager];
  354. if(! [fileManager fileExistsAtPath:template_path isDirectory:&bdir])
  355. {
  356. NSError * error=nil;
  357. if(![fileManager copyItemAtPath:default_path toPath:template_path error:&error])
  358. {
  359. return nil;
  360. }
  361. }
  362. NSData *data = [NSData dataWithContentsOfFile:template_path];
  363. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  364. return ret;
  365. }
  366. +(NSMutableDictionary*) prepare_salesorder_data:(NSString*) order_code type:(int) type
  367. {
  368. /*
  369. DebugLog(@"offline oderdetail params: %@",params);
  370. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  371. int orderId = [params[@"orderId"] intValue];
  372. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  373. 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];
  374. sqlite3 *db = [iSalesDB get_db];
  375. sqlite3_stmt * statement;
  376. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  377. NSString *nssoid = @"";
  378. NSString* orderinfo = @"";
  379. NSString *moreInfo = @"";
  380. double handlingFee = 0;
  381. double payments_and_credist = 0;
  382. double totalPrice = 0;
  383. double shippingFee = 0;
  384. double lift_gate = 0;
  385. NSString *customer_contact = @"";
  386. NSString *customer_email = @"";
  387. NSString *customer_fax = @"";
  388. NSString *customer_phone = @"";
  389. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  390. {
  391. if (sqlite3_step(statement) == SQLITE_ROW)
  392. {
  393. int order_id = sqlite3_column_int(statement, 0);
  394. NSString *sign_url = [self textAtColumn:51 statement:statement];
  395. ret[@"sign_url"] = sign_url;
  396. customer_contact = [self textAtColumn:52 statement:statement];
  397. customer_email = [self textAtColumn:53 statement:statement];
  398. customer_phone = [self textAtColumn:54 statement:statement];
  399. customer_fax = [self textAtColumn:55 statement:statement];
  400. int offline_edit=sqlite3_column_int(statement, 56);
  401. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  402. char *soid = (char*)sqlite3_column_text(statement, 1);
  403. if(soid==nil)
  404. soid= "";
  405. nssoid= [[NSString alloc]initWithUTF8String:soid];
  406. // so#
  407. ret[@"so#"] = nssoid;
  408. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  409. if(poNumber==nil)
  410. poNumber= "";
  411. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  412. char *create_time = (char*)sqlite3_column_text(statement, 3);
  413. if(create_time==nil)
  414. create_time= "";
  415. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  416. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  417. int status = sqlite3_column_int(statement, 4);
  418. int erpStatus = sqlite3_column_int(statement, 49);
  419. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  420. // status
  421. if (status > 1 && status != 3) {
  422. status = erpStatus;
  423. } else if (status == 3) {
  424. status = 15;
  425. }
  426. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  427. ret[@"order_status"] = nsstatus;
  428. char *company_name = (char*)sqlite3_column_text(statement, 5);
  429. if(company_name==nil)
  430. company_name= "";
  431. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  432. // company name
  433. ret[@"company_name"] = nscompany_name;
  434. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  435. if(customer_contact==nil)
  436. customer_contact= "";
  437. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  438. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  439. if(addr_1==nil)
  440. addr_1="";
  441. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  442. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  443. if(addr_2==nil)
  444. addr_2="";
  445. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  446. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  447. if(addr_3==nil)
  448. addr_3="";
  449. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  450. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  451. if(addr_4==nil)
  452. addr_4="";
  453. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  454. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  455. [arr_addr addObject:nsaddr_1];
  456. [arr_addr addObject:nsaddr_2];
  457. [arr_addr addObject:nsaddr_3];
  458. [arr_addr addObject:nsaddr_4];
  459. NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  460. char *logist = (char*)sqlite3_column_text(statement, 11);
  461. if(logist==nil)
  462. logist= "";
  463. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  464. if (status == -11 || status == 10 || status == 11) {
  465. nslogist = [self textAtColumn:59 statement:statement];
  466. };
  467. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  468. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  469. shipping = @"Shipping To Be Quoted";
  470. } else {
  471. shippingFee = sqlite3_column_double(statement, 12);
  472. }
  473. // Shipping
  474. ret[@"Shipping"] = shipping;
  475. int have_lift_gate = sqlite3_column_int(statement, 17);
  476. lift_gate = sqlite3_column_double(statement, 13);
  477. // Liftgate Fee(No loading dock)
  478. if (!have_lift_gate) {
  479. lift_gate = 0;
  480. }
  481. ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  482. if (sqlite3_column_int(statement, 57)) {
  483. ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  484. }
  485. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  486. if(general_notes==nil)
  487. general_notes= "";
  488. NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  489. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  490. if(internal_notes==nil)
  491. internal_notes= "";
  492. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  493. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  494. if(payment_type==nil)
  495. payment_type= "";
  496. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  497. // order info
  498. orderinfo = [self textFileName:@"order_info.html"];
  499. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  500. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  501. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  502. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  503. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  504. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  505. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  506. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  507. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  508. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  509. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  510. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  511. NSString *payment = nil;
  512. if([nspayment_type isEqualToString:@"Credit Card"])
  513. {
  514. payment = [self textFileName:@"creditcardpayment.html"];
  515. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  516. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  517. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  518. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  519. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  520. NSString *card_type = [self textAtColumn:42 statement:statement];
  521. if (card_type.length > 0) { // 显示星号
  522. card_type = @"****";
  523. }
  524. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  525. if (card_number.length > 0 && card_number.length > 4) {
  526. for (int i = 0; i < card_number.length - 4; i++) {
  527. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  528. }
  529. } else {
  530. card_number = @"";
  531. }
  532. NSString *card_security_code = [self textAtColumn:44 statement:statement];
  533. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  534. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  535. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  536. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  537. card_expiration = @"****";
  538. }
  539. NSString *card_city = [self textAtColumn:46 statement:statement];
  540. NSString *card_state = [self textAtColumn:47 statement:statement];
  541. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  542. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  543. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  544. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  545. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  546. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  547. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  548. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  549. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  550. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  551. }
  552. else
  553. {
  554. payment=[self textFileName:@"normalpayment.html"];
  555. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  556. }
  557. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  558. ret[@"result"]= [NSNumber numberWithInt:2];
  559. // more info
  560. moreInfo = [self textFileName:@"more_info.html"];
  561. // ShipToCompany_or_&nbsp
  562. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  563. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  564. NSString *shipToName = [self textAtColumn:19 statement:statement];
  565. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  566. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  567. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  568. // ShipFromCompany_or_&nbsp
  569. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  570. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  571. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  572. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  573. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  574. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  575. // FreightBillToCompany_or_&nbsp
  576. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  577. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  578. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  579. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  580. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  581. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  582. // MerchandiseBillToCompany_or_&nbsp
  583. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  584. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  585. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  586. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  587. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  588. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  589. // ReturnToCompany_or_&nbsp
  590. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  591. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  592. NSString *returnToName = [self textAtColumn:31 statement:statement];
  593. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  594. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  595. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  596. //
  597. // DebugLog(@"more info : %@",moreInfo);
  598. // handling fee
  599. handlingFee = sqlite3_column_double(statement, 33);
  600. ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  601. if (sqlite3_column_int(statement, 58)) {
  602. ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  603. }
  604. //
  605. // customer info
  606. customerID = [self textAtColumn:36 statement:statement];
  607. // mode
  608. ret[@"mode"] = appDelegate.mode;
  609. // model_count
  610. ret[@"model_count"] = @(0);
  611. }
  612. sqlite3_finalize(statement);
  613. }
  614. [iSalesDB close_db:db];
  615. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  616. // "customer_email" = "Shui Hu";
  617. // "customer_fax" = "";
  618. // "customer_first_name" = F;
  619. // "customer_last_name" = L;
  620. // "customer_name" = ",da He Xiang Dong Liu A";
  621. // "customer_phone" = "Hey Xuan Feng";
  622. contactInfo[@"customer_phone"] = customer_phone;
  623. contactInfo[@"customer_fax"] = customer_fax;
  624. contactInfo[@"customer_email"] = customer_email;
  625. NSString *first_name = @"";
  626. NSString *last_name = @"";
  627. if ([customer_contact isEqualToString:@""]) {
  628. } else if ([customer_contact containsString:@" "]) {
  629. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  630. first_name = [customer_contact substringToIndex:first_space_index];
  631. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  632. }
  633. contactInfo[@"customer_first_name"] = first_name;
  634. contactInfo[@"customer_last_name"] = last_name;
  635. ret[@"customerInfo"] = contactInfo;
  636. // models
  637. if (nssoid) {
  638. __block double TotalCuft = 0;
  639. __block double TotalWeight = 0;
  640. __block int TotalCarton = 0;
  641. __block double allItemPrice = 0;
  642. 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];
  643. sqlite3 *db1 = [iSalesDB get_db];
  644. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  645. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  646. int product_id = sqlite3_column_int(stmt, 0);
  647. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  648. int item_id = sqlite3_column_int(stmt, 7);
  649. NSString* Price=nil;
  650. if(str_price==nil)
  651. {
  652. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  653. if(price==nil)
  654. Price=@"No Price.";
  655. else
  656. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  657. }
  658. else
  659. {
  660. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  661. }
  662. double discount = sqlite3_column_double(stmt, 2);
  663. int item_count = sqlite3_column_int(stmt, 3);
  664. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  665. NSString *nsline_note=nil;
  666. if(line_note!=nil)
  667. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  668. // char *name = (char*)sqlite3_column_text(stmt, 5);
  669. // NSString *nsname = nil;
  670. // if(name!=nil)
  671. // nsname= [[NSString alloc]initWithUTF8String:name];
  672. NSString *nsname = [self textAtColumn:5 statement:stmt];
  673. // char *description = (char*)sqlite3_column_text(stmt, 6);
  674. // NSString *nsdescription=nil;
  675. // if(description!=nil)
  676. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  677. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  678. // int stockUom = sqlite3_column_int(stmt, 8);
  679. // int _id = sqlite3_column_int(stmt, 9);
  680. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  681. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  682. double weight=[bsubtotaljson[@"weight"] doubleValue];
  683. int carton=[bsubtotaljson[@"carton"] intValue];
  684. TotalCuft += cuft;
  685. TotalWeight += weight;
  686. TotalCarton += carton;
  687. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  688. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  689. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  690. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  691. itemjson[@"note"]=nsline_note;
  692. itemjson[@"origin_price"] = Price;
  693. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  694. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  695. itemjson[@"order_item_status"] = @""; // 暂时不处理
  696. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  697. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  698. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  699. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  700. if(itemjson[@"combine"] != nil)
  701. {
  702. // int citem=0;
  703. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  704. for(int bc=0;bc<bcount;bc++)
  705. {
  706. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  707. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  708. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  709. subTotal += uprice * modulus * item_count;
  710. }
  711. }
  712. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  713. // NSLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  714. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  715. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  716. allItemPrice += subTotal;
  717. }];
  718. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  719. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  720. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  721. // payments/Credits
  722. // payments_and_credist = sqlite3_column_double(statement, 34);
  723. payments_and_credist = allItemPrice;
  724. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  725. // // total
  726. // totalPrice = sqlite3_column_double(statement, 35);
  727. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  728. } else {
  729. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  730. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  731. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  732. // payments/Credits
  733. payments_and_credist = 0;
  734. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  735. }
  736. // total
  737. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  738. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  739. ret[@"order_info"]= orderinfo;
  740. ret[@"more_order_info"] = moreInfo;
  741. return [RAUtils dict2data:ret];
  742. */
  743. return nil;
  744. }
  745. +(NSData*) offline_request_salesorder :(NSMutableDictionary *) params
  746. {
  747. NSString* template_file=nil;
  748. switch([params[@"pdf_style"] intValue])
  749. {
  750. case 0:
  751. template_file= @"so.json";
  752. break;
  753. case 1:
  754. template_file= @"so_thumb.json";
  755. break;
  756. default:
  757. template_file= @"so.json";
  758. }
  759. NSMutableDictionary* data = [self prepare_salesorder_data:params[@"order_code"] type:[params[@"pdf_style"] intValue]];
  760. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  761. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  762. DebugLog(@"%@",file);
  763. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  764. [dic setObject:@"Regular Mode" forKey:@"mode"];
  765. if (file) {
  766. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  767. [dic setObject:file forKey:@"pdf_path"];
  768. dic[@"isLocalFile"]=@"true";
  769. return [RAUtils dict2data:dic];
  770. }
  771. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  772. dic[@"isLocalFile"]=@"true";
  773. return [RAUtils dict2data:dic];
  774. }
  775. +(NSData*) offline_request_tearsheet :(NSMutableDictionary *) params
  776. {
  777. NSString* template_file=nil;
  778. switch([params[@"pdf_style"] intValue])
  779. {
  780. case 0:
  781. template_file= @"portfolio_2x3.json";
  782. break;
  783. case 1:
  784. template_file= @"portfolio_3x2.json";
  785. break;
  786. default:
  787. template_file= @"portfolio_3x2.json";
  788. }
  789. NSMutableDictionary* data = [self prepare_portfolio_data:params];
  790. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  791. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  792. DebugLog(@"%@",file);
  793. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  794. [dic setObject:@"Regular Mode" forKey:@"mode"];
  795. if (file) {
  796. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  797. [dic setObject:file forKey:@"pdf_path"];
  798. dic[@"isLocalFile"]=@"true";
  799. return [RAUtils dict2data:dic];
  800. }
  801. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  802. dic[@"isLocalFile"]=@"true";
  803. return [RAUtils dict2data:dic];
  804. }
  805. +(NSString*) get_offline_soid:(sqlite3*)db
  806. {
  807. NSString* soid=nil;
  808. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  809. [formatter setDateFormat:@"yyMMdd"];
  810. NSString* date = [formatter stringFromDate:[NSDate date]];
  811. for(int i=1;i<999;i++)
  812. {
  813. soid=[NSString stringWithFormat:@"OFF%@%4d",date,i];
  814. soid= [soid stringByReplacingOccurrencesOfString:@" " withString:@"0"];
  815. int count=[iSalesDB get_recordcount:db table:@"offline_order" where:[NSString stringWithFormat:@"so_id='%@'",soid]];
  816. if(count==0)
  817. return soid;
  818. }
  819. return nil;
  820. }
  821. +(NSArray*) enumOfflineOrder
  822. {
  823. NSMutableArray* ret = [[NSMutableArray alloc] init];
  824. sqlite3 *db = [iSalesDB get_db];
  825. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  826. sqlite3_stmt * statement;
  827. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  828. if ( dbresult== SQLITE_OK)
  829. {
  830. while (sqlite3_step(statement) == SQLITE_ROW)
  831. {
  832. char *so_id = (char*)sqlite3_column_text(statement, 0);
  833. NSString* nsso_id=nil;
  834. if(so_id!=nil)
  835. {
  836. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  837. }
  838. [ret addObject:nsso_id];
  839. }
  840. sqlite3_finalize(statement);
  841. }
  842. [iSalesDB close_db:db];
  843. return ret;
  844. }
  845. +(void) uploadFile:(NSString*) file
  846. {
  847. NSData* data = [NSData dataWithContentsOfFile: file];
  848. UIApplication * app = [UIApplication sharedApplication];
  849. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  850. AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  851. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  852. if(appDelegate.user!=nil)
  853. [params setValue:appDelegate.user forKey:@"user"];
  854. // if(appDelegate.contact_id!=nil)
  855. // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  856. if(appDelegate.password!=nil)
  857. [params setValue:appDelegate.password forKey:@"password"];
  858. NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_OFFLINE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  859. [formData appendPartWithFileData:data name:@"upfile" fileName:[file lastPathComponent] mimeType:@"application/zip"];
  860. } error:nil];
  861. AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  862. NSProgress *progress = nil;
  863. NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  864. if (error) {
  865. NSString* err_msg = [error localizedDescription];
  866. NSLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  867. NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  868. NSLog(@"data string: %@",str);
  869. [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  870. } else {
  871. NSLog(@"response ");
  872. NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  873. // 再将NSData转为字符串
  874. NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  875. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  876. NSLog(@"data string: %@",jsonStr);
  877. NSDictionary* json = responseObject;
  878. if([[json valueForKey:@"result"] intValue]==2)
  879. {
  880. // NSString* img_url_down = json[@"img_url_aname"];
  881. // NSString* img_url_up = json[@"img_url"];
  882. }
  883. else
  884. {
  885. [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload file" controller:nil] ;
  886. }
  887. }
  888. // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  889. }];
  890. // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  891. //
  892. [uploadTask resume];
  893. }
  894. +(NSDictionary *) prepareUpload:(NSArray*) arr_order
  895. {
  896. NSString* serial= [[NSUUID UUID] UUIDString];
  897. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  898. NSString *cachefolder = [paths objectAtIndex:0];
  899. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  900. upfolder = [upfolder stringByAppendingPathComponent:serial];
  901. NSFileManager* fileManager = [NSFileManager defaultManager];
  902. BOOL bdir=YES;
  903. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  904. {
  905. NSError *error = nil;
  906. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  907. if(!bsuccess)
  908. DebugLog(@"Create UPLOAD folder failed");
  909. }
  910. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  911. ret[@"contact"]=[self prepareContact:serial];
  912. ret[@"wishlist"]=[self prepareWishlist:serial];
  913. ret[@"order"]=[self prepareOrder:serial soid:arr_order];
  914. ret[@"portfolio"] = [self preparePortfolio:serial];
  915. ret[@"view_portfolio"] = [self preparePDF:serial];
  916. NSString* str= [RAUtils dict2string:ret];
  917. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  918. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  919. NSError *error=nil;
  920. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  921. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  922. ZipArchive* zip = [[ZipArchive alloc] init];
  923. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  924. for(int i=0;i<arr_files.count;i++)
  925. {
  926. NSString* file=arr_files[i];
  927. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  928. }
  929. if( ![zip CloseZipFile2] )
  930. {
  931. zippath = @"";
  932. }
  933. ret[@"file"]=zippath;
  934. return ret;
  935. }
  936. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  937. {
  938. if(filename.length==0)
  939. return false;
  940. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  941. bool ret=false;
  942. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  943. NSString *cachefolder = [paths objectAtIndex:0];
  944. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  945. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  946. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  947. upfolder = [upfolder stringByAppendingPathComponent:serial];
  948. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  949. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  950. NSFileManager* fileManager = [NSFileManager defaultManager];
  951. BOOL bdir=NO;
  952. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  953. {
  954. NSError *error = nil;
  955. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  956. {
  957. ret=false;
  958. }
  959. else
  960. {
  961. ret=true;
  962. }
  963. }
  964. return ret;
  965. }
  966. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  967. {
  968. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  969. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  970. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  971. NSString* where=@" 1=1";
  972. if (ver!=nil) {
  973. where=@"is_dirty=1";
  974. }
  975. sqlite3 *db = [iSalesDB get_db];
  976. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  977. sqlite3_stmt * statement;
  978. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  979. int count=0;
  980. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  981. if ( dbresult== SQLITE_OK)
  982. {
  983. while (sqlite3_step(statement) == SQLITE_ROW)
  984. {
  985. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  986. int _id = sqlite3_column_int(statement, 0);
  987. int product_id = sqlite3_column_int(statement, 1);
  988. int item_id = sqlite3_column_int(statement, 2);
  989. int qty = sqlite3_column_int(statement, 3);
  990. char *create_time = (char*)sqlite3_column_text(statement, 4);
  991. NSString* nscreate_time=nil;
  992. if(create_time!=nil)
  993. {
  994. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  995. }
  996. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  997. NSString* nsmodify_time=nil;
  998. if(modify_time!=nil)
  999. {
  1000. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  1001. }
  1002. int is_delete = sqlite3_column_int(statement, 6);
  1003. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1004. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  1005. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1006. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  1007. itemjson[@"create_time"]=nscreate_time;
  1008. itemjson[@"modify_time"]=nsmodify_time;
  1009. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  1010. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1011. count++;
  1012. }
  1013. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1014. sqlite3_finalize(statement);
  1015. }
  1016. ret[@"count"]=[NSNumber numberWithInt:count ];
  1017. [iSalesDB close_db:db];
  1018. return ret;
  1019. }
  1020. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode db:(sqlite3*)db
  1021. {
  1022. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1023. UIApplication * app = [UIApplication sharedApplication];
  1024. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1025. 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 ];
  1026. // 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 ];
  1027. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  1028. DebugLog(@"offline_login sql:%@",sqlQuery);
  1029. sqlite3_stmt * statement;
  1030. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1031. int count=0;
  1032. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1033. if ( dbresult== SQLITE_OK)
  1034. {
  1035. while (sqlite3_step(statement) == SQLITE_ROW)
  1036. {
  1037. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1038. int product_id = sqlite3_column_int(statement, 0);
  1039. char *str_price = (char*)sqlite3_column_text(statement, 1);
  1040. int item_id = sqlite3_column_int(statement, 7);
  1041. NSString* Price=nil;
  1042. if(str_price==nil)
  1043. {
  1044. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1045. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  1046. if(price==nil)
  1047. Price=@"No Price.";
  1048. else
  1049. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1050. }
  1051. else
  1052. {
  1053. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1054. }
  1055. double discount = sqlite3_column_double(statement, 2);
  1056. int item_count = sqlite3_column_int(statement, 3);
  1057. char *line_note = (char*)sqlite3_column_text(statement, 4);
  1058. NSString *nsline_note=nil;
  1059. if(line_note!=nil)
  1060. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1061. char *name = (char*)sqlite3_column_text(statement, 5);
  1062. NSString *nsname=nil;
  1063. if(name!=nil)
  1064. nsname= [[NSString alloc]initWithUTF8String:name];
  1065. char *description = (char*)sqlite3_column_text(statement, 6);
  1066. NSString *nsdescription=nil;
  1067. if(description!=nil)
  1068. nsdescription= [[NSString alloc]initWithUTF8String:description];
  1069. // int stockUom = sqlite3_column_int(statement, 8);
  1070. // int _id = sqlite3_column_int(statement, 9);
  1071. //
  1072. //
  1073. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  1074. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1075. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  1076. // int carton=[bsubtotaljson[@"carton"] intValue];
  1077. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1078. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  1079. itemjson[@"model"]=nsname;
  1080. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  1081. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  1082. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  1083. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  1084. // itemjson[@"check"]=@"true";
  1085. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1086. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  1087. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  1088. itemjson[@"unit_price"]=Price;
  1089. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  1090. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1091. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  1092. itemjson[@"note"]=nsline_note;
  1093. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  1094. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1095. count++;
  1096. }
  1097. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1098. sqlite3_finalize(statement);
  1099. }
  1100. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1101. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  1102. return ret;
  1103. }
  1104. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid
  1105. {
  1106. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1107. sqlite3 *db = [iSalesDB get_db];
  1108. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  1109. // for(int i=0;i<arr_soid.count;i++)
  1110. sqlite3_stmt * statement;
  1111. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1112. //int count=0;
  1113. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1114. if ( dbresult== SQLITE_OK)
  1115. {
  1116. int count=0;
  1117. while (sqlite3_step(statement) == SQLITE_ROW)
  1118. {
  1119. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1120. int _id = sqlite3_column_int(statement, 0);
  1121. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  1122. NSString* nssync_data=nil;
  1123. if(sync_data!=nil)
  1124. {
  1125. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1126. }
  1127. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1128. NSString* nsimg_1=nil;
  1129. if(img_1!=nil)
  1130. {
  1131. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1132. [self copy_upImg:serial file:nsimg_1];
  1133. }
  1134. char *so_no = (char*)sqlite3_column_text(statement, 3);
  1135. NSString* nsso_no=nil;
  1136. if(so_no!=nil)
  1137. {
  1138. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  1139. }
  1140. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  1141. itemjson[@"order_type"]=@"submit order";
  1142. else
  1143. itemjson[@"order_type"]=@"archive order";
  1144. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1145. itemjson[@"json_data"]= nssync_data;
  1146. itemjson[@"order_items"]=[self preparecart:nsso_no db:db];
  1147. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1148. count++;
  1149. }
  1150. ret[@"count"]=[NSNumber numberWithInt:count ];
  1151. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1152. }
  1153. sqlite3_finalize(statement);
  1154. [iSalesDB close_db:db];
  1155. return ret;
  1156. }
  1157. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  1158. {
  1159. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1160. sqlite3 *db = [iSalesDB get_db];
  1161. // UIApplication * app = [UIApplication sharedApplication];
  1162. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1163. 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";
  1164. sqlite3_stmt * statement;
  1165. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1166. int count=0;
  1167. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1168. if ( dbresult== SQLITE_OK)
  1169. {
  1170. while (sqlite3_step(statement) == SQLITE_ROW)
  1171. {
  1172. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1173. int _id = sqlite3_column_int(statement, 0);
  1174. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  1175. NSString* nsimg_0=nil;
  1176. if(img_0!=nil)
  1177. {
  1178. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  1179. [self copy_upImg:serial file:nsimg_0];
  1180. }
  1181. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1182. NSString* nsimg_1=nil;
  1183. if(img_1!=nil)
  1184. {
  1185. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1186. [self copy_upImg:serial file:nsimg_1];
  1187. }
  1188. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  1189. NSString* nsimg_2=nil;
  1190. if(img_2!=nil)
  1191. {
  1192. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  1193. [self copy_upImg:serial file:nsimg_2];
  1194. }
  1195. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  1196. NSString* nssync_data=nil;
  1197. if(sync_data!=nil)
  1198. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1199. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1200. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1201. itemjson[@"json_data"]= nssync_data;
  1202. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1203. count++;
  1204. }
  1205. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1206. sqlite3_finalize(statement);
  1207. }
  1208. ret[@"count"]=[NSNumber numberWithInt:count ];
  1209. [iSalesDB close_db:db];
  1210. return ret;
  1211. }
  1212. +(bool) check_offlinedata
  1213. {
  1214. UIApplication * app = [UIApplication sharedApplication];
  1215. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1216. sqlite3 *db = [iSalesDB get_db];
  1217. NSString * where=@"1=1";
  1218. // if(appDelegate.user!=nil)
  1219. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  1220. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  1221. [iSalesDB close_db:db];
  1222. if(count==0)
  1223. {
  1224. return false;
  1225. }
  1226. return true;
  1227. //
  1228. //[iSalesDB close_db:db];
  1229. }
  1230. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  1231. {
  1232. UIApplication * app = [UIApplication sharedApplication];
  1233. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1234. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1235. sqlite3 *db = [iSalesDB get_db];
  1236. NSString* collectId=params[@"collectId"];
  1237. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  1238. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  1239. [iSalesDB execSql:sqlQuery db:db];
  1240. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  1241. [iSalesDB close_db:db];
  1242. appDelegate.wish_count =count;
  1243. [appDelegate update_count_mark];
  1244. ret[@"result"]= [NSNumber numberWithInt:2];
  1245. return ret;
  1246. }
  1247. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  1248. //{
  1249. //
  1250. // UIApplication * app = [UIApplication sharedApplication];
  1251. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1252. //
  1253. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1254. // sqlite3 *db = [iSalesDB get_db];
  1255. // NSString* product_id=params[@"product_id"];
  1256. //
  1257. //
  1258. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  1259. //
  1260. //
  1261. // for(int i=0;i<arr.count;i++)
  1262. // {
  1263. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  1264. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  1265. // if(count==0)
  1266. // {
  1267. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  1268. // [iSalesDB execSql:sqlQuery db:db];
  1269. // }
  1270. // }
  1271. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  1272. // [iSalesDB close_db:db];
  1273. //
  1274. // appDelegate.wish_count =count;
  1275. //
  1276. // [appDelegate update_count_mark];
  1277. // ret[@"result"]= [NSNumber numberWithInt:2];
  1278. // return ret;
  1279. // //
  1280. // //return ret;
  1281. //}
  1282. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  1283. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  1284. DebugLog(@"time interval: %lf",interval);
  1285. }
  1286. +(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  1287. {
  1288. UIApplication * app = [UIApplication sharedApplication];
  1289. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1290. int sort = [[params objectForKey:@"sort"] intValue];
  1291. NSString *sort_str = @"";
  1292. switch (sort) {
  1293. case 0:{
  1294. sort_str = @"order by w.modify_time desc";
  1295. }
  1296. break;
  1297. case 1:{
  1298. sort_str = @"order by w.modify_time asc";
  1299. }
  1300. break;
  1301. case 2:{
  1302. sort_str = @"order by m.name asc";
  1303. }
  1304. break;
  1305. case 3:{
  1306. sort_str = @"order by m.name desc";
  1307. }
  1308. break;
  1309. case 4:{
  1310. sort_str = @"order by m.description asc";
  1311. }
  1312. break;
  1313. default:
  1314. break;
  1315. }
  1316. NSString* user = appDelegate.user;
  1317. sqlite3 *db = [iSalesDB get_db];
  1318. // order by w.create_time
  1319. 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];
  1320. // 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];
  1321. sqlite3_stmt * statement;
  1322. NSDate *date1 = [NSDate date];
  1323. NSDate *date2 = nil;
  1324. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1325. int count=0;
  1326. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1327. {
  1328. while (sqlite3_step(statement) == SQLITE_ROW)
  1329. {
  1330. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1331. int product_id = sqlite3_column_double(statement, 0);
  1332. char *description = (char*)sqlite3_column_text(statement, 1);
  1333. if(description==nil)
  1334. description= "";
  1335. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  1336. int item_id = sqlite3_column_double(statement, 2);
  1337. NSDate *date_image = [NSDate date];
  1338. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  1339. printf("image : ");
  1340. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  1341. // char *url = (char*)sqlite3_column_text(statement, 3);
  1342. // if(url==nil)
  1343. // url="";
  1344. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1345. int qty = sqlite3_column_int(statement, 3);
  1346. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  1347. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  1348. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  1349. item[@"description"]= nsdescription;
  1350. item[@"img"]= nsurl;
  1351. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  1352. count++;
  1353. }
  1354. printf("total time:");
  1355. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  1356. ret[@"count"]= [NSNumber numberWithInt:count];
  1357. ret[@"total_count"]= [NSNumber numberWithInt:count];
  1358. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  1359. ret[@"result"]= [NSNumber numberWithInt:2];
  1360. appDelegate.wish_count =count;
  1361. [appDelegate update_count_mark];
  1362. sqlite3_finalize(statement);
  1363. }
  1364. [iSalesDB close_db:db];
  1365. return ret;
  1366. }
  1367. +(NSDictionary*) offline_notimpl
  1368. {
  1369. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1370. ret[@"result"]=@"8";
  1371. ret[@"err_msg"]=@"offline mode does not support this function.";
  1372. return ret;
  1373. }
  1374. +(NSDictionary*) offline_home
  1375. {
  1376. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1377. NSString *cachefolder = [paths objectAtIndex:0];
  1378. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  1379. NSData* json =nil;
  1380. json=[NSData dataWithContentsOfFile:img_cache];
  1381. NSError *error=nil;
  1382. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1383. return menu;
  1384. }
  1385. +(NSDictionary*) offline_category_menu
  1386. {
  1387. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1388. NSString *cachefolder = [paths objectAtIndex:0];
  1389. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  1390. NSData* json =nil;
  1391. json=[NSData dataWithContentsOfFile:img_cache];
  1392. if(json==nil)
  1393. return nil;
  1394. NSError *error=nil;
  1395. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1396. return menu;
  1397. }
  1398. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  1399. {
  1400. NSString* offline_command=params[@"offline_Command"];
  1401. NSDictionary* ret=nil;
  1402. if([offline_command isEqualToString:@"model_NIYMAL"])
  1403. {
  1404. NSString* category = params[@"category"];
  1405. ret = [self refresh_model_NIYMAL:category];
  1406. }
  1407. return ret;
  1408. }
  1409. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  1410. {
  1411. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1412. [ret setValue:@"2" forKey:@"result"];
  1413. [ret setValue:@"Regular Mode" forKey:@"mode"];
  1414. sqlite3* db= [iSalesDB get_db];
  1415. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  1416. [iSalesDB close_db:db];
  1417. [ret setObject:detail1_section forKey:@"detail_1"];
  1418. return ret;
  1419. }
  1420. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id db:(sqlite3*)db
  1421. {
  1422. UIApplication * app = [UIApplication sharedApplication];
  1423. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1424. // NSArray* arr1 = [self get_user_all_price_type];
  1425. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  1426. // NSSet *set1 = [NSSet setWithArray:arr1];
  1427. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  1428. // if(appDelegate.contact_id==nil)
  1429. // set2=[set1 mutableCopy];
  1430. // else
  1431. // [set2 intersectsSet:set1];
  1432. // NSArray *retarr = [set2 allObjects];
  1433. NSString* whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  1434. // sqlite3 *db = [iSalesDB get_db];
  1435. NSString* sqlQuery = nil;
  1436. if(appDelegate.contact_id==nil)
  1437. sqlQuery=[NSString stringWithFormat:@"select a.name,b.price,b.type 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];
  1438. else
  1439. sqlQuery=[NSString stringWithFormat:@"select a.name,b.price,b.type 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];
  1440. sqlite3_stmt * statement;
  1441. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1442. int count=0;
  1443. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1444. {
  1445. while (sqlite3_step(statement) == SQLITE_ROW)
  1446. {
  1447. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1448. char *name = (char*)sqlite3_column_text(statement, 0);
  1449. if(name==nil)
  1450. name="";
  1451. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1452. // double price = sqlite3_column_double(statement, 1);
  1453. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  1454. // if(isnull==nil)
  1455. // item[nsname]= @"No Price";
  1456. // else
  1457. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  1458. char *price = (char*)sqlite3_column_text(statement, 1);
  1459. if(price!=nil)
  1460. {
  1461. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  1462. nsprice=[AESCrypt fastdecrypt:nsprice];
  1463. if(nsprice.length>0)
  1464. {
  1465. double dp= [nsprice doubleValue];
  1466. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  1467. }
  1468. }
  1469. else
  1470. {
  1471. item[nsname]= @"No Price";
  1472. }
  1473. // int type= sqlite3_column_int(statement, 2);
  1474. // item[@"type"]=[NSNumber numberWithInt:type ];
  1475. // item[nsname]= nsprice;
  1476. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  1477. count++;
  1478. }
  1479. ret[@"count"]= [NSNumber numberWithInt:count];
  1480. sqlite3_finalize(statement);
  1481. }
  1482. // [iSalesDB close_db:db];
  1483. return ret;
  1484. }
  1485. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  1486. //{
  1487. // NSArray* arr1 = [self get_user_all_price_type:db];
  1488. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  1489. //
  1490. // // NSSet *set1 = [NSSet setWithArray:arr1];
  1491. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  1492. // // [set2 intersectsSet:set1];
  1493. // //
  1494. // //
  1495. // // NSArray *retarr = [set2 allObjects];
  1496. //
  1497. // NSString* whereprice=nil;
  1498. // if(contact_id==nil)
  1499. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  1500. // else
  1501. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  1502. //
  1503. //
  1504. // // sqlite3 *db = [iSalesDB get_db];
  1505. //
  1506. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  1507. // sqlite3_stmt * statement;
  1508. //
  1509. //
  1510. // NSNumber* ret = nil;
  1511. // double dprice=DBL_MAX;
  1512. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1513. // {
  1514. //
  1515. //
  1516. // while (sqlite3_step(statement) == SQLITE_ROW)
  1517. // {
  1518. //
  1519. // // double val = sqlite3_column_double(statement, 0);
  1520. // char *price = (char*)sqlite3_column_text(statement, 0);
  1521. // if(price!=nil)
  1522. // {
  1523. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  1524. // nsprice=[AESCrypt fastdecrypt:nsprice];
  1525. // if(nsprice.length>0)
  1526. // {
  1527. // double dp= [nsprice doubleValue];
  1528. // if(dp<dprice)
  1529. // dprice=dp;
  1530. // }
  1531. // }
  1532. // }
  1533. //
  1534. //
  1535. //
  1536. //
  1537. // sqlite3_finalize(statement);
  1538. //
  1539. //
  1540. //
  1541. //
  1542. // }
  1543. //
  1544. // // [iSalesDB close_db:db];
  1545. //
  1546. // if(dprice==DBL_MAX)
  1547. // ret= nil;
  1548. // else
  1549. // ret= [NSNumber numberWithDouble:dprice];
  1550. // return ret;
  1551. //}
  1552. +(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  1553. {
  1554. NSArray* arr1 = [self get_user_all_price_type:db];
  1555. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  1556. NSString* whereprice=nil;
  1557. if(contact_id==nil)
  1558. whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  1559. else
  1560. whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  1561. // sqlite3 *db = [iSalesDB get_db];
  1562. NSString *productIdCondition = @"1 = 1";
  1563. if (product_id) {
  1564. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  1565. }
  1566. NSString *itemIdCondition = @"";
  1567. if (item_id) {
  1568. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  1569. }
  1570. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  1571. sqlite3_stmt * statement;
  1572. NSNumber* ret = nil;
  1573. double dprice=DBL_MAX;
  1574. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1575. {
  1576. while (sqlite3_step(statement) == SQLITE_ROW)
  1577. {
  1578. // double val = sqlite3_column_double(statement, 0);
  1579. char *price = (char*)sqlite3_column_text(statement, 0);
  1580. if(price!=nil)
  1581. {
  1582. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  1583. nsprice=[AESCrypt fastdecrypt:nsprice];
  1584. if(nsprice.length>0)
  1585. {
  1586. double dp= [nsprice doubleValue];
  1587. if(dp<dprice)
  1588. dprice=dp;
  1589. }
  1590. }
  1591. }
  1592. sqlite3_finalize(statement);
  1593. }
  1594. // [iSalesDB close_db:db];
  1595. if(dprice==DBL_MAX)
  1596. ret= nil;
  1597. else
  1598. ret= [NSNumber numberWithDouble:dprice];
  1599. return ret;
  1600. }
  1601. +(NSArray*) get_user_all_price_type:(sqlite3*)db
  1602. {
  1603. NSArray* ret=nil;
  1604. // sqlite3 *db = [iSalesDB get_db];
  1605. // no customer assigned , use login user contact_id
  1606. UIApplication * app = [UIApplication sharedApplication];
  1607. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1608. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",appDelegate.user];
  1609. sqlite3_stmt * statement;
  1610. // int count=0;
  1611. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1612. {
  1613. if (sqlite3_step(statement) == SQLITE_ROW)
  1614. {
  1615. char *val = (char*)sqlite3_column_text(statement, 0);
  1616. if(val==nil)
  1617. val="";
  1618. NSString* price = [[NSString alloc]initWithUTF8String:val];
  1619. ret=[RAUtils string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  1620. }
  1621. sqlite3_finalize(statement);
  1622. }
  1623. // [iSalesDB close_db:db];
  1624. return ret;
  1625. }
  1626. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id db:(sqlite3*)db
  1627. {
  1628. // sqlite3 *db = [iSalesDB get_db];
  1629. if(contact_id==nil)
  1630. {
  1631. // no customer assigned , use login user contact_id
  1632. UIApplication * app = [UIApplication sharedApplication];
  1633. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1634. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",appDelegate.user];
  1635. sqlite3_stmt * statement;
  1636. // int count=0;
  1637. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1638. {
  1639. if (sqlite3_step(statement) == SQLITE_ROW)
  1640. {
  1641. char *val = (char*)sqlite3_column_text(statement, 0);
  1642. if(val==nil)
  1643. val="";
  1644. contact_id = [[NSString alloc]initWithUTF8String:val];
  1645. }
  1646. sqlite3_finalize(statement);
  1647. }
  1648. if(contact_id.length<=0)
  1649. {
  1650. // [iSalesDB close_db:db];
  1651. return nil;
  1652. }
  1653. }
  1654. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  1655. sqlite3_stmt * statement;
  1656. NSArray* ret=nil;
  1657. // int count=0;
  1658. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1659. {
  1660. if (sqlite3_step(statement) == SQLITE_ROW)
  1661. {
  1662. char *price_type = (char*)sqlite3_column_text(statement, 0);
  1663. if(price_type==nil)
  1664. price_type="";
  1665. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  1666. if(nsprice_type.length>0)
  1667. ret=[RAUtils string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  1668. else
  1669. ret=nil;
  1670. }
  1671. sqlite3_finalize(statement);
  1672. }
  1673. // [iSalesDB close_db:db];
  1674. return ret;
  1675. }
  1676. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  1677. {
  1678. NSString* ret= nil;
  1679. // sqlite3 *db = [iSalesDB get_db];
  1680. NSString *sqlQuery = nil;
  1681. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  1682. // 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;
  1683. // 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
  1684. if(product_id==nil && model_name)
  1685. 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;
  1686. else if (product_id)
  1687. 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
  1688. sqlite3_stmt * statement;
  1689. // int count=0;
  1690. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1691. {
  1692. if (sqlite3_step(statement) == SQLITE_ROW)
  1693. {
  1694. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  1695. if(imgurl==nil)
  1696. imgurl="";
  1697. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  1698. ret=nsimgurl;
  1699. }
  1700. sqlite3_finalize(statement);
  1701. }
  1702. else
  1703. {
  1704. [ret setValue:@"8" forKey:@"result"];
  1705. }
  1706. // [iSalesDB close_db:db];
  1707. DebugLog(@"data string: %@",ret );
  1708. return ret;
  1709. }
  1710. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name
  1711. {
  1712. NSString* ret= nil;
  1713. sqlite3 *db = [iSalesDB get_db];
  1714. NSString *sqlQuery = nil;
  1715. if(product_id==nil)
  1716. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  1717. else
  1718. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  1719. sqlite3_stmt * statement;
  1720. // int count=0;
  1721. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1722. {
  1723. if (sqlite3_step(statement) == SQLITE_ROW)
  1724. {
  1725. char *default_category = (char*)sqlite3_column_text(statement, 0);
  1726. if(default_category==nil)
  1727. default_category="";
  1728. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  1729. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  1730. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  1731. ret=nsdefault_category;
  1732. }
  1733. sqlite3_finalize(statement);
  1734. }
  1735. else
  1736. {
  1737. [ret setValue:@"8" forKey:@"result"];
  1738. }
  1739. [iSalesDB close_db:db];
  1740. DebugLog(@"data string: %@",ret );
  1741. return ret;
  1742. }
  1743. +(NSDictionary*) offline_model :(NSMutableDictionary *) params
  1744. {
  1745. NSString* model_name = [params valueForKey:@"product_name"];
  1746. NSString* product_id = [params valueForKey:@"product_id"];
  1747. NSString* category = [params valueForKey:@"category"];
  1748. if(category==nil)
  1749. category = [self model_default_category:product_id model_name:model_name];
  1750. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1751. sqlite3 *db = [iSalesDB get_db];
  1752. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  1753. NSString *sqlQuery = nil;
  1754. if(product_id==nil)
  1755. 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='%@';
  1756. else
  1757. 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=%@;
  1758. sqlite3_stmt * statement;
  1759. [ret setValue:@"2" forKey:@"result"];
  1760. [ret setValue:@"3" forKey:@"detail_section_count"];
  1761. // int count=0;
  1762. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1763. {
  1764. if (sqlite3_step(statement) == SQLITE_ROW)
  1765. {
  1766. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1767. char *name = (char*)sqlite3_column_text(statement, 0);
  1768. if(name==nil)
  1769. name="";
  1770. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1771. char *description = (char*)sqlite3_column_text(statement, 1);
  1772. if(description==nil)
  1773. description="";
  1774. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  1775. int product_id = sqlite3_column_int(statement, 2);
  1776. char *color = (char*)sqlite3_column_text(statement, 3);
  1777. if(color==nil)
  1778. color="";
  1779. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  1780. //
  1781. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  1782. // if(legcolor==nil)
  1783. // legcolor="";
  1784. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  1785. //
  1786. //
  1787. int availability = sqlite3_column_int(statement, 5);
  1788. //
  1789. int incoming_stock = sqlite3_column_int(statement, 6);
  1790. char *demension = (char*)sqlite3_column_text(statement, 7);
  1791. if(demension==nil)
  1792. demension="";
  1793. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  1794. // ,,,,,,,,,
  1795. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  1796. if(seat_height==nil)
  1797. seat_height="";
  1798. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  1799. char *material = (char*)sqlite3_column_text(statement, 9);
  1800. if(material==nil)
  1801. material="";
  1802. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  1803. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  1804. if(box_dim==nil)
  1805. box_dim="";
  1806. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  1807. char *volume = (char*)sqlite3_column_text(statement, 11);
  1808. if(volume==nil)
  1809. volume="";
  1810. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  1811. double weight = sqlite3_column_double(statement, 12);
  1812. char *model_set = (char*)sqlite3_column_text(statement, 13);
  1813. if(model_set==nil)
  1814. model_set="";
  1815. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  1816. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  1817. if(load_ability==nil)
  1818. load_ability="";
  1819. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  1820. char *default_category = (char*)sqlite3_column_text(statement, 15);
  1821. if(default_category==nil)
  1822. default_category="";
  1823. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  1824. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  1825. if(fabric_content==nil)
  1826. fabric_content="";
  1827. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  1828. char *assembling = (char*)sqlite3_column_text(statement, 17);
  1829. if(assembling==nil)
  1830. assembling="";
  1831. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  1832. char *made_in = (char*)sqlite3_column_text(statement, 18);
  1833. if(made_in==nil)
  1834. made_in="";
  1835. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  1836. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  1837. if(special_remarks==nil)
  1838. special_remarks="";
  1839. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  1840. int stockUcom = sqlite3_column_double(statement, 20);
  1841. char *product_group = (char*)sqlite3_column_text(statement, 21);
  1842. if(product_group==nil)
  1843. product_group="";
  1844. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  1845. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  1846. // if(fashion_selector==nil)
  1847. // fashion_selector="";
  1848. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  1849. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  1850. if(selector_field==nil)
  1851. selector_field="";
  1852. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  1853. char *property_field = (char*)sqlite3_column_text(statement, 23);
  1854. if(property_field==nil)
  1855. property_field="";
  1856. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  1857. char *packaging = (char*)sqlite3_column_text(statement, 24);
  1858. if(packaging==nil)
  1859. packaging="";
  1860. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  1861. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  1862. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  1863. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  1864. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  1865. [img_section setValue:model_s_img forKey:@"model_s_img"];
  1866. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  1867. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  1868. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  1869. NSString* Availability=nil;
  1870. if(availability>0)
  1871. Availability=[NSString stringWithFormat:@"%d",availability];
  1872. else
  1873. Availability = @"In Production";
  1874. [img_section setValue:Availability forKey:@"Availability"];
  1875. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  1876. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  1877. char *eta = (char*)sqlite3_column_text(statement, 25);
  1878. if(eta==nil)
  1879. eta="";
  1880. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  1881. [img_section setValue:nseta forKey:@"ETA"];
  1882. int item_id = sqlite3_column_int(statement, 26);
  1883. NSString* Price=nil;
  1884. if(appDelegate.bLogin==false)
  1885. Price=@"Must Sign in.";
  1886. else
  1887. {
  1888. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1889. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  1890. if(price==nil)
  1891. Price=@"No Price.";
  1892. else
  1893. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1894. }
  1895. [img_section setValue:Price forKey:@"price"];
  1896. [img_section setValue:nsname forKey:@"model_name"];
  1897. [img_section setValue:nsdescription forKey:@"model_descrition"];
  1898. [ret setObject:img_section forKey:@"img_section"];
  1899. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  1900. int detail0_item_count=0;
  1901. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1902. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1903. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1904. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1905. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1906. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1907. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1908. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1909. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1910. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1911. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1912. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1913. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1914. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  1915. NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  1916. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  1917. {
  1918. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  1919. [detail0_section setObject:[self model_kvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1920. }
  1921. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  1922. [detail0_section setValue:@"kv" forKey:@"type"];
  1923. [detail0_section setValue:@"Product Information" forKey:@"title"];
  1924. [ret setObject:detail0_section forKey:@"detail_0"];
  1925. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  1926. // [detail1_section setValue:@"detail" forKey:@"target"];
  1927. // [detail1_section setValue:@"popup" forKey:@"action"];
  1928. // [detail1_section setValue:@"content" forKey:@"type"];
  1929. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  1930. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  1931. // [detail1_section setValue:@"true" forKey:@"single_row"];
  1932. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  1933. [ret setObject:detail1_section forKey:@"detail_1"];
  1934. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  1935. [detail2_section setValue:@"detail" forKey:@"target"];
  1936. [detail2_section setValue:@"popup" forKey:@"action"];
  1937. [detail2_section setValue:@"content" forKey:@"type"];
  1938. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  1939. [detail2_section setValue:@"true" forKey:@"single_row"];
  1940. [detail2_section setValue:@"local" forKey:@"data"];
  1941. [ret setObject:detail2_section forKey:@"detail_2"];
  1942. }
  1943. sqlite3_finalize(statement);
  1944. }
  1945. else
  1946. {
  1947. [ret setValue:@"8" forKey:@"result"];
  1948. }
  1949. NSLog(@"count:%d",count);
  1950. [iSalesDB close_db:db];
  1951. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1952. return ret;
  1953. }
  1954. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  1955. NSString* orderCode = [params valueForKey:@"orderCode"];
  1956. NSString* keyword = [params valueForKey:@"keyword"];
  1957. keyword=keyword.lowercaseString;
  1958. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue];
  1959. int limit = [[params valueForKey:@"limit"] intValue];
  1960. int offset = [[params valueForKey:@"offset"] intValue];
  1961. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1962. NSString *limit_str = @"";
  1963. if (limited) {
  1964. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  1965. }
  1966. sqlite3 *db = [iSalesDB get_db];
  1967. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  1968. UIApplication * app = [UIApplication sharedApplication];
  1969. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1970. NSString *sqlQuery = nil;
  1971. if(exactMatch )
  1972. 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 ;
  1973. else
  1974. 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
  1975. DebugLog(@"offline_search sql:%@",sqlQuery);
  1976. sqlite3_stmt * statement;
  1977. [ret setValue:@"2" forKey:@"result"];
  1978. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  1979. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  1980. // int count=0;
  1981. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1982. {
  1983. int i=0;
  1984. while (sqlite3_step(statement) == SQLITE_ROW)
  1985. {
  1986. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1987. // char *name = (char*)sqlite3_column_text(statement, 1);
  1988. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1989. char *name = (char*)sqlite3_column_text(statement, 0);
  1990. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1991. char *description = (char*)sqlite3_column_text(statement, 1);
  1992. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  1993. int product_id = sqlite3_column_int(statement, 2);
  1994. // char *url = (char*)sqlite3_column_text(statement, 3);
  1995. // if(url==nil)
  1996. // url="";
  1997. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1998. int wid = sqlite3_column_int(statement, 3);
  1999. int closeout = sqlite3_column_int(statement, 4);
  2000. int cid = sqlite3_column_int(statement, 5);
  2001. int wisdelete = sqlite3_column_int(statement, 6);
  2002. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  2003. if(wid !=0 && wisdelete != 1)
  2004. [item setValue:@"true" forKey:@"wish_exists"];
  2005. else
  2006. [item setValue:@"false" forKey:@"wish_exists"];
  2007. if(closeout==0)
  2008. [item setValue:@"false" forKey:@"is_closeout"];
  2009. else
  2010. [item setValue:@"true" forKey:@"is_closeout"];
  2011. if(cid==0)
  2012. [item setValue:@"false" forKey:@"cart_exists"];
  2013. else
  2014. [item setValue:@"true" forKey:@"cart_exists"];
  2015. [item addEntriesFromDictionary:imgjson];
  2016. // [item setValue:nsurl forKey:@"img"];
  2017. [item setValue:nsname forKey:@"fash_name"];
  2018. [item setValue:nsdescription forKey:@"description"];
  2019. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2020. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2021. i++;
  2022. }
  2023. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2024. [ret setObject:items forKey:@"items"];
  2025. sqlite3_finalize(statement);
  2026. }
  2027. NSLog(@"count:%d",count);
  2028. [iSalesDB close_db:db];
  2029. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2030. return ret;
  2031. }
  2032. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  2033. {
  2034. return [self search:params limited:YES];
  2035. }
  2036. +(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  2037. {
  2038. UIApplication * app = [UIApplication sharedApplication];
  2039. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2040. appDelegate.disable_trigger=true;
  2041. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2042. appDelegate.disable_trigger=false;
  2043. //
  2044. // NSString* user = [params valueForKey:@"user"];
  2045. //
  2046. // NSString* password = [params valueForKey:@"password"];
  2047. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2048. ret[@"result"]=[NSNumber numberWithInt:2 ];
  2049. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  2050. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  2051. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  2052. [appDelegate update_count_mark];
  2053. appDelegate.can_show_price =false;
  2054. appDelegate.can_see_price =false;
  2055. appDelegate.can_create_portfolio =false;
  2056. appDelegate.can_create_order =false;
  2057. appDelegate.can_cancel_order =false;
  2058. appDelegate.can_set_cart_price =false;
  2059. appDelegate.can_delete_order =false;
  2060. appDelegate.can_submit_order =false;
  2061. appDelegate.can_set_tearsheet_price =false;
  2062. appDelegate.can_update_contact_info = false;
  2063. appDelegate.save_order_logout = false;
  2064. appDelegate.submit_order_logout = false;
  2065. appDelegate.alert_sold_in_quantities = false;
  2066. appDelegate.ipad_perm =nil ;
  2067. appDelegate.user_type = USER_ROLE_UNKNOWN;
  2068. appDelegate.OrderFilter= nil;
  2069. [appDelegate SetSo:nil];
  2070. [appDelegate set_main_button_panel];
  2071. // sqlite3 *db = [iSalesDB get_db];
  2072. //
  2073. //
  2074. //
  2075. //
  2076. //
  2077. // 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"]];
  2078. //
  2079. //
  2080. //
  2081. //
  2082. // DebugLog(@"offline_login sql:%@",sqlQuery);
  2083. // sqlite3_stmt * statement;
  2084. //
  2085. //
  2086. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2087. //
  2088. //
  2089. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2090. // {
  2091. //
  2092. //
  2093. // if (sqlite3_step(statement) == SQLITE_ROW)
  2094. // {
  2095. //
  2096. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2097. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  2098. //
  2099. //
  2100. //
  2101. // int can_show_price = sqlite3_column_int(statement, 0);
  2102. // int can_see_price = sqlite3_column_int(statement, 1);
  2103. //
  2104. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  2105. // if(contact_id==nil)
  2106. // contact_id="";
  2107. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2108. //
  2109. // int user_type = sqlite3_column_int(statement, 3);
  2110. //
  2111. // int can_cancel_order = sqlite3_column_int(statement, 4);
  2112. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  2113. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  2114. // int can_delete_order = sqlite3_column_int(statement, 7);
  2115. // int can_submit_order = sqlite3_column_int(statement, 8);
  2116. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  2117. // int can_create_order = sqlite3_column_int(statement, 10);
  2118. //
  2119. //
  2120. // char *mode = (char*)sqlite3_column_text(statement, 11);
  2121. // if(mode==nil)
  2122. // mode="";
  2123. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  2124. //
  2125. //
  2126. // char *username = (char*)sqlite3_column_text(statement, 12);
  2127. // if(username==nil)
  2128. // username="";
  2129. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  2130. //
  2131. //
  2132. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  2133. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  2134. // [header setValue:nscontact_id forKey:@"contact_id"];
  2135. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  2136. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  2137. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  2138. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  2139. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  2140. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  2141. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  2142. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  2143. //
  2144. // [header setValue:nsusername forKey:@"username"];
  2145. //
  2146. //
  2147. // [ret setObject:header forKey:@"header"];
  2148. // [ret setValue:nsmode forKey:@"mode"];
  2149. //
  2150. //
  2151. // }
  2152. //
  2153. //
  2154. //
  2155. // sqlite3_finalize(statement);
  2156. // }
  2157. //
  2158. //
  2159. //
  2160. // [iSalesDB close_db:db];
  2161. //
  2162. //
  2163. //
  2164. //
  2165. // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2166. return ret;
  2167. }
  2168. +(NSData*) offline_createorder :(NSMutableDictionary *) params
  2169. {
  2170. UIApplication * app = [UIApplication sharedApplication];
  2171. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2172. appDelegate.disable_trigger=true;
  2173. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2174. appDelegate.disable_trigger=false;
  2175. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2176. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  2177. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  2178. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  2179. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  2180. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  2181. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  2182. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  2183. // NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  2184. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  2185. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  2186. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  2187. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  2188. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  2189. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  2190. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  2191. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  2192. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  2193. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  2194. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  2195. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  2196. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  2197. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  2198. [arr_name addObject:customer_first_name];
  2199. [arr_name addObject:customer_last_name];
  2200. NSString *contact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  2201. // default ship from
  2202. 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';";
  2203. __block NSString *cid = @"";
  2204. __block NSString *name = @"";
  2205. __block NSString *ext = @"";
  2206. __block NSString *contact = @"";
  2207. __block NSString *email = @"";
  2208. __block NSString *fax = @"";
  2209. __block NSString *phone = @"";
  2210. sqlite3 *db = [iSalesDB get_db];
  2211. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  2212. cid = [self textAtColumn:0 statement:statment];
  2213. name = [self textAtColumn:1 statement:statment];
  2214. ext = [self textAtColumn:2 statement:statment];
  2215. contact = [self textAtColumn:3 statement:statment];
  2216. email = [self textAtColumn:4 statement:statment];
  2217. fax = [self textAtColumn:5 statement:statment];
  2218. phone = [self textAtColumn:6 statement:statment];
  2219. }];
  2220. NSString* so_id = [self get_offline_soid:db];
  2221. if(so_id==nil)
  2222. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  2223. 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];
  2224. int result =[iSalesDB execSql:sql_neworder db:db];
  2225. [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  2226. //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'
  2227. //soId
  2228. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  2229. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  2230. sqlite3_stmt * statement;
  2231. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2232. {
  2233. if (sqlite3_step(statement) == SQLITE_ROW)
  2234. {
  2235. // char *name = (char*)sqlite3_column_text(statement, 1);
  2236. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2237. //ret = sqlite3_column_int(statement, 0);
  2238. char *soId = (char*)sqlite3_column_text(statement, 0);
  2239. if(soId==nil)
  2240. soId="";
  2241. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  2242. [ret setValue:nssoId forKey:@"soId"];
  2243. [ret setValue:nssoId forKey:@"orderCode"];
  2244. }
  2245. sqlite3_finalize(statement);
  2246. }
  2247. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  2248. [ret setValue:@"Regular Mode" forKey:@"mode"];
  2249. [iSalesDB close_db:db];
  2250. return [RAUtils dict2data:ret];
  2251. }
  2252. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  2253. {
  2254. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2255. NSString* orderCode = [params valueForKey:@"orderCode"];
  2256. UIApplication * app = [UIApplication sharedApplication];
  2257. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2258. if(![appDelegate.order_code isEqualToString:orderCode]&& appDelegate.order_code.length!=0)
  2259. {
  2260. appDelegate.disable_trigger=true;
  2261. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2262. appDelegate.disable_trigger=false;
  2263. }
  2264. sqlite3 *db = [iSalesDB get_db];
  2265. int cart_count=[self query_ordercartcount:orderCode db:db];
  2266. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2267. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2268. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2269. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2270. [iSalesDB close_db:db];
  2271. return [RAUtils dict2data:ret];
  2272. }
  2273. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  2274. {
  2275. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2276. sqlite3 *db = [iSalesDB get_db];
  2277. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  2278. int count =0;
  2279. if(params[@"count"]!=nil)
  2280. {
  2281. count = [params[@"count"] intValue];
  2282. }
  2283. NSString* product_id=params[@"product_id"];
  2284. NSString* orderCode=params[@"orderCode"];
  2285. NSString *qty = params[@"qty"];
  2286. NSArray* arr_id=[RAUtils string2arr:product_id separator:@","];
  2287. NSArray *qty_arr = [RAUtils string2arr:qty separator:@","];
  2288. for(int i=0;i<arr_id.count;i++)
  2289. {
  2290. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  2291. int item_qty= count;
  2292. if (qty) {
  2293. item_qty = [qty_arr[i] integerValue];
  2294. }
  2295. if(item_qty==0)
  2296. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  2297. NSString* sql=nil;
  2298. sqlite3_stmt *stmt;
  2299. if(_id<0)
  2300. {
  2301. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  2302. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  2303. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2304. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],[arr_id[i] length],NULL);
  2305. sqlite3_bind_text(stmt,2,[orderCode UTF8String],[orderCode length],NULL);
  2306. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%d",item_qty] UTF8String],[[NSString stringWithFormat:@"%d",item_qty] length],NULL);
  2307. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  2308. }
  2309. else
  2310. {
  2311. if (qty) {
  2312. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  2313. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2314. sqlite3_bind_int(stmt, 1, _id);
  2315. } else {
  2316. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  2317. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2318. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%d",item_qty] UTF8String],[[NSString stringWithFormat:@"%d",item_qty] length],NULL);
  2319. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],[[NSString stringWithFormat:@"%d",_id] length],NULL);
  2320. }
  2321. }
  2322. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  2323. [iSalesDB execSql:@"ROLLBACK" db:db];
  2324. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  2325. [iSalesDB close_db:db];
  2326. DebugLog(@"add to cart error");
  2327. return [RAUtils dict2data:ret];
  2328. }
  2329. }
  2330. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2331. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2332. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2333. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2334. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2335. [iSalesDB execSql:@"END TRANSACTION" db:db];
  2336. [iSalesDB close_db:db];
  2337. return [RAUtils dict2data:ret];
  2338. }
  2339. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute
  2340. {
  2341. UIApplication * app = [UIApplication sharedApplication];
  2342. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2343. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  2344. 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];
  2345. sqlite3_stmt * statement;
  2346. int count=0;
  2347. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2348. {
  2349. while (sqlite3_step(statement) == SQLITE_ROW)
  2350. {
  2351. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  2352. int bitem_id = sqlite3_column_int(statement, 0);
  2353. int bitem_qty = sqlite3_column_int(statement, 1);
  2354. char *name = (char*)sqlite3_column_text(statement, 2);
  2355. if(name==nil)
  2356. name="";
  2357. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  2358. char *description = (char*)sqlite3_column_text(statement, 3);
  2359. if(description==nil)
  2360. description="";
  2361. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  2362. double unit_price = sqlite3_column_double(statement, 4);
  2363. int use_unitprice = sqlite3_column_int(statement, 5);
  2364. if(use_unitprice!=1)
  2365. {
  2366. unit_price=[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db].doubleValue;
  2367. }
  2368. itemjson[@"model"]=nsname;
  2369. itemjson[@"description"]=nsdescription;
  2370. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  2371. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  2372. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  2373. if(compute)
  2374. {
  2375. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute];
  2376. }
  2377. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2378. count++;
  2379. }
  2380. sqlite3_finalize(statement);
  2381. }
  2382. ret[@"count"]=@(count);
  2383. if(count==0)
  2384. return nil;
  2385. else
  2386. return ret;
  2387. }
  2388. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  2389. {
  2390. //compute: add part to subtotal;
  2391. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  2392. dict_item[@(item_id)]=@"1";
  2393. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  2394. double cuft=0;
  2395. double weight=0;
  2396. int carton=0;
  2397. 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];
  2398. sqlite3_stmt * statement;
  2399. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2400. {
  2401. if (sqlite3_step(statement) == SQLITE_ROW)
  2402. {
  2403. double ulength = sqlite3_column_double(statement, 0);
  2404. double uwidth = sqlite3_column_double(statement, 1);
  2405. double uheight = sqlite3_column_double(statement, 2);
  2406. double uweight = sqlite3_column_double(statement, 3);
  2407. double mlength = sqlite3_column_double(statement, 4);
  2408. double mwidth = sqlite3_column_double(statement, 5);
  2409. double mheight = sqlite3_column_double(statement, 6);
  2410. double mweight = sqlite3_column_double(statement, 7);
  2411. double ilength = sqlite3_column_double(statement, 8);
  2412. double iwidth = sqlite3_column_double(statement, 9);
  2413. double iheight = sqlite3_column_double(statement, 10);
  2414. double iweight = sqlite3_column_double(statement, 11);
  2415. int pcs = sqlite3_column_int(statement,12);
  2416. int mpack = sqlite3_column_int(statement, 13);
  2417. int ipack = sqlite3_column_int(statement, 14);
  2418. double ucbf = sqlite3_column_double(statement, 15);
  2419. double icbf = sqlite3_column_double(statement, 16);
  2420. double mcbf = sqlite3_column_double(statement, 17);
  2421. if(ipack==0)
  2422. {
  2423. carton= count/mpack ;
  2424. weight = mweight*carton;
  2425. cuft= carton*(mlength*mwidth*mheight);
  2426. int remain=count%mpack;
  2427. if(remain==0)
  2428. {
  2429. //do nothing;
  2430. }
  2431. else
  2432. {
  2433. carton++;
  2434. weight += uweight*remain;
  2435. cuft += (ulength*uwidth*uheight)*remain;
  2436. }
  2437. }
  2438. else
  2439. {
  2440. carton = count/(mpack*ipack);
  2441. weight = mweight*carton;
  2442. cuft= carton*(mlength*mwidth*mheight);
  2443. int remain=count%(mpack*ipack);
  2444. if(remain==0)
  2445. {
  2446. // do nothing;
  2447. }
  2448. else
  2449. {
  2450. carton++;
  2451. int icarton =remain/ipack;
  2452. int iremain=remain%ipack;
  2453. weight += iweight*icarton;
  2454. cuft += (ilength*iwidth*iheight)*icarton;
  2455. if(iremain==0)
  2456. {
  2457. //do nothing;
  2458. }
  2459. else
  2460. {
  2461. weight += uweight*iremain;
  2462. cuft += (ulength*uwidth*uheight)*iremain;
  2463. }
  2464. }
  2465. }
  2466. #ifdef BUILD_NPD
  2467. cuft=ucbf*count;
  2468. weight= uweight*count;
  2469. #endif
  2470. }
  2471. sqlite3_finalize(statement);
  2472. }
  2473. if(compute)
  2474. {
  2475. NSArray * arr_count=nil;
  2476. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  2477. for(int i=0;i<arr_bundle.count;i++)
  2478. {
  2479. dict_item[arr_bundle[i]]=@"1";
  2480. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  2481. cuft+=[bundlejson[@"cuft"] doubleValue];
  2482. weight+=[bundlejson[@"weight"] doubleValue];
  2483. carton+=[bundlejson[@"carton"] intValue];
  2484. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  2485. }
  2486. }
  2487. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  2488. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  2489. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  2490. ret[@"items"]=dict_item;
  2491. return ret;
  2492. }
  2493. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  2494. {
  2495. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  2496. // 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 ];
  2497. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  2498. DebugLog(@"offline_login sql:%@",sqlQuery);
  2499. sqlite3_stmt * statement;
  2500. int cart_count=0;
  2501. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2502. if ( dbresult== SQLITE_OK)
  2503. {
  2504. while (sqlite3_step(statement) == SQLITE_ROW)
  2505. {
  2506. int item_id = sqlite3_column_int(statement, 0);
  2507. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  2508. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  2509. }
  2510. sqlite3_finalize(statement);
  2511. }
  2512. return cart_count;
  2513. }
  2514. +(NSData*) offline_requestcart :(NSMutableDictionary *) params
  2515. {
  2516. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2517. sqlite3 *db = [iSalesDB get_db];
  2518. UIApplication * app = [UIApplication sharedApplication];
  2519. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2520. NSString* orderCode=params[@"orderCode"];
  2521. int sort = [[params objectForKey:@"sort"] intValue];
  2522. NSString *sort_str = @"";
  2523. switch (sort) {
  2524. case 0:{
  2525. sort_str = @"order by c.modify_time desc";
  2526. }
  2527. break;
  2528. case 1:{
  2529. sort_str = @"order by c.modify_time asc";
  2530. }
  2531. break;
  2532. case 2:{
  2533. sort_str = @"order by m.name asc";
  2534. }
  2535. break;
  2536. case 3:{
  2537. sort_str = @"order by m.name desc";
  2538. }
  2539. break;
  2540. case 4:{
  2541. sort_str = @"order by m.description asc";
  2542. }
  2543. break;
  2544. default:
  2545. break;
  2546. }
  2547. 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 ];
  2548. // 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 ];
  2549. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  2550. DebugLog(@"offline_login sql:%@",sqlQuery);
  2551. sqlite3_stmt * statement;
  2552. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2553. NSDate *date1 = [NSDate date];
  2554. int count=0;
  2555. int cart_count=0;
  2556. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2557. if ( dbresult== SQLITE_OK)
  2558. {
  2559. while (sqlite3_step(statement) == SQLITE_ROW)
  2560. {
  2561. NSDate *row_date = [NSDate date];
  2562. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2563. int product_id = sqlite3_column_int(statement, 0);
  2564. char *str_price = (char*)sqlite3_column_text(statement, 1);
  2565. int item_id = sqlite3_column_int(statement, 7);
  2566. NSString* Price=nil;
  2567. if(str_price==nil)
  2568. {
  2569. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2570. // NSDate *price_date = [NSDate date];
  2571. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  2572. // DebugLog(@"price time interval");
  2573. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  2574. if(price==nil)
  2575. Price=@"No Price.";
  2576. else
  2577. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2578. }
  2579. else
  2580. {
  2581. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  2582. }
  2583. double discount = sqlite3_column_double(statement, 2);
  2584. int item_count = sqlite3_column_int(statement, 3);
  2585. char *line_note = (char*)sqlite3_column_text(statement, 4);
  2586. NSString *nsline_note=nil;
  2587. if(line_note!=nil)
  2588. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  2589. char *name = (char*)sqlite3_column_text(statement, 5);
  2590. NSString *nsname=nil;
  2591. if(name!=nil)
  2592. nsname= [[NSString alloc]initWithUTF8String:name];
  2593. char *description = (char*)sqlite3_column_text(statement, 6);
  2594. NSString *nsdescription=nil;
  2595. if(description!=nil)
  2596. nsdescription= [[NSString alloc]initWithUTF8String:description];
  2597. int stockUom = sqlite3_column_int(statement, 8);
  2598. int _id = sqlite3_column_int(statement, 9);
  2599. // NSDate *subtotal_date = [NSDate date];
  2600. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  2601. // DebugLog(@"subtotal_date time interval");
  2602. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  2603. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  2604. double weight=[bsubtotaljson[@"weight"] doubleValue];
  2605. int carton=[bsubtotaljson[@"carton"] intValue];
  2606. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  2607. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2608. // NSDate *img_date = [NSDate date];
  2609. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  2610. // DebugLog(@"img_date time interval");
  2611. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  2612. itemjson[@"model"]=nsname;
  2613. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  2614. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  2615. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  2616. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  2617. itemjson[@"check"]=@"true";
  2618. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  2619. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  2620. itemjson[@"unit_price"]=Price;
  2621. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  2622. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  2623. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  2624. itemjson[@"note"]=nsline_note;
  2625. // NSDate *date2 = [NSDate date];
  2626. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  2627. // DebugLog(@"model_bundle time interval");
  2628. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  2629. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2630. count++;
  2631. // DebugLog(@"row time interval");
  2632. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  2633. }
  2634. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2635. sqlite3_finalize(statement);
  2636. }
  2637. DebugLog(@"request cart total time interval");
  2638. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  2639. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2640. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2641. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2642. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2643. ret[@"count"]=[NSNumber numberWithInt:count ];
  2644. ret[@"mode"]=@"Regular Mode";
  2645. [iSalesDB close_db:db];
  2646. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  2647. DebugLog(@"general notes :%@",general_note);
  2648. ret[@"general_note"]= general_note;
  2649. return [RAUtils dict2data:ret];
  2650. }
  2651. +(NSData*) offline_login :(NSMutableDictionary *) params
  2652. {
  2653. NSString* user = [params valueForKey:@"user"];
  2654. NSString* password = [params valueForKey:@"password"];
  2655. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2656. sqlite3 *db = [iSalesDB get_db];
  2657. 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"]];
  2658. DebugLog(@"offline_login sql:%@",sqlQuery);
  2659. sqlite3_stmt * statement;
  2660. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2661. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2662. {
  2663. if (sqlite3_step(statement) == SQLITE_ROW)
  2664. {
  2665. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2666. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  2667. int can_show_price = sqlite3_column_int(statement, 0);
  2668. int can_see_price = sqlite3_column_int(statement, 1);
  2669. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  2670. if(contact_id==nil)
  2671. contact_id="";
  2672. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2673. int user_type = sqlite3_column_int(statement, 3);
  2674. int can_cancel_order = sqlite3_column_int(statement, 4);
  2675. int can_set_cart_price = sqlite3_column_int(statement, 5);
  2676. int can_create_portfolio = sqlite3_column_int(statement, 6);
  2677. int can_delete_order = sqlite3_column_int(statement, 7);
  2678. int can_submit_order = sqlite3_column_int(statement, 8);
  2679. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  2680. int can_create_order = sqlite3_column_int(statement, 10);
  2681. char *mode = (char*)sqlite3_column_text(statement, 11);
  2682. if(mode==nil)
  2683. mode="";
  2684. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  2685. char *username = (char*)sqlite3_column_text(statement, 12);
  2686. if(username==nil)
  2687. username="";
  2688. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  2689. int can_update_contact_info = sqlite3_column_int(statement, 13);
  2690. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  2691. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  2692. [header setValue:nscontact_id forKey:@"contact_id"];
  2693. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  2694. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  2695. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  2696. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  2697. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  2698. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  2699. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  2700. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  2701. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  2702. [header setValue:nsusername forKey:@"username"];
  2703. NSError* error=nil;
  2704. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  2705. [header setValue:statusFilter forKey:@"statusFilter"];
  2706. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  2707. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  2708. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  2709. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  2710. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  2711. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  2712. [ret setObject:header forKey:@"header"];
  2713. [ret setValue:nsmode forKey:@"mode"];
  2714. }
  2715. sqlite3_finalize(statement);
  2716. }
  2717. [iSalesDB close_db:db];
  2718. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2719. return [RAUtils dict2data:ret];
  2720. }
  2721. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  2722. {
  2723. NSString* contactId = [params valueForKey:@"contactId"];
  2724. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2725. sqlite3 *db = [iSalesDB get_db];
  2726. NSString *sqlQuery = nil;
  2727. {
  2728. 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];
  2729. }
  2730. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  2731. sqlite3_stmt * statement;
  2732. [ret setValue:@"2" forKey:@"result"];
  2733. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2734. {
  2735. //int i = 0;
  2736. if (sqlite3_step(statement) == SQLITE_ROW)
  2737. {
  2738. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2739. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2740. // int editable = sqlite3_column_int(statement, 0);
  2741. char *company_name = (char*)sqlite3_column_text(statement, 1);
  2742. NSString *nscompany_name =nil;
  2743. if(company_name==nil)
  2744. nscompany_name=@"";
  2745. else
  2746. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  2747. char *country = (char*)sqlite3_column_text(statement, 2);
  2748. if(country==nil)
  2749. country="";
  2750. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  2751. // char *addr = (char*)sqlite3_column_text(statement, 3);
  2752. // if(addr==nil)
  2753. // addr="";
  2754. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  2755. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  2756. if(zipcode==nil)
  2757. zipcode="";
  2758. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  2759. char *state = (char*)sqlite3_column_text(statement, 5);
  2760. if(state==nil)
  2761. state="";
  2762. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  2763. char *city = (char*)sqlite3_column_text(statement, 6);
  2764. if(city==nil)
  2765. city="";
  2766. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  2767. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  2768. // NSString *nscontact_name = nil;
  2769. // if(contact_name==nil)
  2770. // nscontact_name=@"";
  2771. // else
  2772. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  2773. char *phone = (char*)sqlite3_column_text(statement, 8);
  2774. NSString *nsphone = nil;
  2775. if(phone==nil)
  2776. nsphone=@"";
  2777. else
  2778. nsphone= [[NSString alloc]initWithUTF8String:phone];
  2779. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  2780. if(contact_id==nil)
  2781. contact_id="";
  2782. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2783. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  2784. if(addr_1==nil)
  2785. addr_1="";
  2786. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  2787. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  2788. if(addr_2==nil)
  2789. addr_2="";
  2790. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  2791. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  2792. if(addr_3==nil)
  2793. addr_3="";
  2794. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  2795. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  2796. if(addr_4==nil)
  2797. addr_4="";
  2798. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  2799. char *first_name = (char*)sqlite3_column_text(statement, 14);
  2800. if(first_name==nil)
  2801. first_name="";
  2802. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  2803. char *last_name = (char*)sqlite3_column_text(statement, 15);
  2804. if(last_name==nil)
  2805. last_name="";
  2806. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  2807. char *fax = (char*)sqlite3_column_text(statement, 16);
  2808. NSString *nsfax = nil;
  2809. if(fax==nil)
  2810. nsfax=@"";
  2811. else
  2812. nsfax= [[NSString alloc]initWithUTF8String:fax];
  2813. char *email = (char*)sqlite3_column_text(statement, 17);
  2814. NSString *nsemail = nil;
  2815. if(email==nil)
  2816. nsemail=@"";
  2817. else
  2818. nsemail= [[NSString alloc]initWithUTF8String:email];
  2819. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  2820. NSString *nsimg_0 = nil;
  2821. if(img_0==nil)
  2822. nsimg_0=@"";
  2823. else
  2824. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  2825. [self copy_bcardImg:nsimg_0];
  2826. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  2827. NSString *nsimg_1 = nil;
  2828. if(img_1==nil)
  2829. nsimg_1=@"";
  2830. else
  2831. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2832. [self copy_bcardImg:nsimg_1];
  2833. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  2834. NSString *nsimg_2 = nil;
  2835. if(img_2==nil)
  2836. nsimg_2=@"";
  2837. else
  2838. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  2839. [self copy_bcardImg:nsimg_2];
  2840. char *price_type = (char*)sqlite3_column_text(statement, 21);
  2841. NSString *nsprice_type = nil;
  2842. if(price_type==nil)
  2843. nsprice_type=@"";
  2844. else
  2845. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  2846. char *notes = (char*)sqlite3_column_text(statement, 22);
  2847. NSString *nsnotes = nil;
  2848. if(notes==nil)
  2849. nsnotes=@"";
  2850. else
  2851. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  2852. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  2853. NSString *nssalesrep = nil;
  2854. if(salesrep==nil)
  2855. nssalesrep=@"";
  2856. else
  2857. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  2858. {
  2859. // decrypt
  2860. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  2861. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  2862. nsphone=[AESCrypt fastdecrypt:nsphone];
  2863. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  2864. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  2865. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  2866. }
  2867. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  2868. [arr_name addObject:nsfirst_name];
  2869. [arr_name addObject:nslast_name];
  2870. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  2871. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  2872. [arr_ext addObject:nsaddr_1];
  2873. [arr_ext addObject:nsaddr_2];
  2874. [arr_ext addObject:nsaddr_3];
  2875. [arr_ext addObject:nsaddr_4];
  2876. [arr_ext addObject:@"\r\n"];
  2877. [arr_ext addObject:nscity];
  2878. [arr_ext addObject:nsstate];
  2879. [arr_ext addObject:nszipcode];
  2880. [arr_ext addObject:nscountry];
  2881. NSString *nsext=[RAUtils arr2string:arr_ext separator:@", " trim:true];
  2882. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  2883. [item setValue:nsimg_2 forKey:@"business_card_2"];
  2884. [item setValue:nsimg_0 forKey:@"business_card_0"];
  2885. [item setValue:nscountry forKey:@"customer_country"];
  2886. [item setValue:nsphone forKey:@"customer_phone"];
  2887. [item setValue:nsimg_1 forKey:@"business_card_1"];
  2888. [item setValue:nscompany_name forKey:@"customer_name"];
  2889. [item setValue:nsprice_type forKey:@"customer_price_type"];
  2890. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  2891. [item setValue:nsext forKey:@"customer_contact_ext"];
  2892. [item setValue:nszipcode forKey:@"customer_zipcode"];
  2893. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  2894. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  2895. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  2896. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  2897. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  2898. [item setValue:nslast_name forKey:@"customer_last_name"];
  2899. [item setValue:nscity forKey:@"customer_city"];
  2900. [item setValue:nsstate forKey:@"customer_state"];
  2901. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  2902. [item setValue:contactId forKey:@"customer_cid"];
  2903. [item setValue:nscontact_name forKey:@"customer_contact"];
  2904. [item setValue:nsfax forKey:@"customer_fax"];
  2905. [item setValue:nsemail forKey:@"customer_email"];
  2906. [ret setObject:item forKey:@"customerInfo"];
  2907. // i++;
  2908. }
  2909. UIApplication * app = [UIApplication sharedApplication];
  2910. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2911. [ret setValue:appDelegate.mode forKey:@"mode"];
  2912. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  2913. sqlite3_finalize(statement);
  2914. }
  2915. [iSalesDB close_db:db];
  2916. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2917. return ret;
  2918. }
  2919. + (bool) copy_bcardImg:(NSString*) filename
  2920. {
  2921. if(filename.length==0)
  2922. return false;
  2923. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2924. bool ret=false;
  2925. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2926. NSString *cachefolder = [paths objectAtIndex:0];
  2927. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  2928. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  2929. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  2930. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  2931. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  2932. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  2933. //
  2934. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  2935. NSFileManager* fileManager = [NSFileManager defaultManager];
  2936. BOOL bdir=NO;
  2937. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  2938. {
  2939. NSError *error = nil;
  2940. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  2941. {
  2942. ret=false;
  2943. }
  2944. else
  2945. {
  2946. ret=true;
  2947. }
  2948. // NSError *error = nil;
  2949. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  2950. //
  2951. // if(!bsuccess)
  2952. // {
  2953. // DebugLog(@"Create offline_createimg folder failed");
  2954. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2955. // return [RAUtils dict2data:ret];
  2956. // }
  2957. // if(bsuccess)
  2958. // {
  2959. // sqlite3 *db = [self get_db];
  2960. //
  2961. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  2962. // [iSalesDB close_db:db];
  2963. // }
  2964. }
  2965. return ret;
  2966. //
  2967. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  2968. // if(bsuccess)
  2969. // {
  2970. // NSError *error = nil;
  2971. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  2972. // {
  2973. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2974. // }
  2975. // else
  2976. // {
  2977. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  2978. //
  2979. // ret[@"img_url_aname"]=filename;
  2980. // ret[@"img_url"]=savedImagePath;
  2981. // }
  2982. // }
  2983. }
  2984. +(NSData *) offline_saveBusinesscard:(NSData *) image
  2985. {
  2986. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2987. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2988. NSString *cachefolder = [paths objectAtIndex:0];
  2989. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  2990. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  2991. NSFileManager* fileManager = [NSFileManager defaultManager];
  2992. BOOL bdir=YES;
  2993. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  2994. {
  2995. NSError *error = nil;
  2996. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  2997. if(!bsuccess)
  2998. {
  2999. DebugLog(@"Create offline_createimg folder failed");
  3000. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3001. return [RAUtils dict2data:ret];
  3002. }
  3003. // if(bsuccess)
  3004. // {
  3005. // sqlite3 *db = [self get_db];
  3006. //
  3007. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  3008. // [iSalesDB close_db:db];
  3009. // }
  3010. }
  3011. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  3012. //JEPG格式
  3013. //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  3014. NSString* filename =[NSString stringWithFormat:@"%@.png",[[NSUUID UUID] UUIDString]];
  3015. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  3016. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  3017. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  3018. if(bsuccess)
  3019. {
  3020. NSError *error = nil;
  3021. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  3022. {
  3023. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3024. }
  3025. else
  3026. {
  3027. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  3028. ret[@"img_url_aname"]=filename;
  3029. ret[@"img_url"]=filename;
  3030. }
  3031. }
  3032. else
  3033. {
  3034. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3035. }
  3036. return [RAUtils dict2data:ret];
  3037. }
  3038. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  3039. {
  3040. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  3041. category = [category substringToIndex:3];
  3042. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3043. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  3044. params[@"category"]= category;
  3045. ret[@"params"]= params;
  3046. [ret setValue:@"detail" forKey:@"target"];
  3047. [ret setValue:@"popup" forKey:@"action"];
  3048. [ret setValue:@"content" forKey:@"type"];
  3049. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  3050. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  3051. [ret setValue:@"true" forKey:@"single_row"];
  3052. [ret setValue:@"true" forKey:@"partial_refresh"];
  3053. // sqlite3 *db = [iSalesDB get_db];
  3054. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  3055. 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 ;
  3056. sqlite3_stmt * statement;
  3057. int count = 0;
  3058. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  3059. // int count=0;
  3060. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3061. {
  3062. int i=0;
  3063. while (sqlite3_step(statement) == SQLITE_ROW)
  3064. {
  3065. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3066. // char *name = (char*)sqlite3_column_text(statement, 1);
  3067. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3068. char *name = (char*)sqlite3_column_text(statement, 0);
  3069. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3070. int product_id = sqlite3_column_int(statement, 1);
  3071. char *url = (char*)sqlite3_column_text(statement, 2);
  3072. if(url==nil)
  3073. url="";
  3074. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3075. [item setValue:nsurl forKey:@"picture_path"];
  3076. [item setValue:nsname forKey:@"fash_name"];
  3077. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3078. [item setValue:category forKey:@"category"];
  3079. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3080. i++;
  3081. }
  3082. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3083. sqlite3_finalize(statement);
  3084. }
  3085. NSLog(@"count:%d",count);
  3086. // [iSalesDB close_db:db];
  3087. return ret;
  3088. }
  3089. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  3090. {
  3091. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3092. [ret setValue:key forKey:@"key"];
  3093. [ret setValue:value forKey:@"val"];
  3094. return ret;
  3095. }
  3096. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  3097. {
  3098. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3099. [ret setValue:@"0" forKey:@"img_count"];
  3100. // sqlite3 *db = [iSalesDB get_db];
  3101. 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 ;
  3102. sqlite3_stmt * statement;
  3103. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3104. {
  3105. int i=0;
  3106. if (sqlite3_step(statement) == SQLITE_ROW)
  3107. {
  3108. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3109. // char *name = (char*)sqlite3_column_text(statement, 1);
  3110. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3111. char *value = (char*)sqlite3_column_text(statement, 0);
  3112. if(value==nil)
  3113. value="";
  3114. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  3115. char *key = (char*)sqlite3_column_text(statement, 1);
  3116. if(key==nil)
  3117. key="";
  3118. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  3119. [item setValue:nsvalue forKey:@"val"];
  3120. [item setValue:nskey forKey:@"key"];
  3121. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3122. [ret setValue:@"1" forKey:@"count"];
  3123. i++;
  3124. }
  3125. sqlite3_finalize(statement);
  3126. }
  3127. // [iSalesDB close_db:db];
  3128. return ret;
  3129. }
  3130. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  3131. {
  3132. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3133. [ret setValue:@"0" forKey:@"count"];
  3134. // sqlite3 *db = [iSalesDB get_db];
  3135. 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;
  3136. sqlite3_stmt * statement;
  3137. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3138. {
  3139. int i=0;
  3140. while (sqlite3_step(statement) == SQLITE_ROW)
  3141. {
  3142. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3143. // char *name = (char*)sqlite3_column_text(statement, 1);
  3144. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3145. char *value = (char*)sqlite3_column_text(statement, 0);
  3146. if(value==nil)
  3147. value="";
  3148. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  3149. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  3150. if(selector_display==nil)
  3151. selector_display="";
  3152. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  3153. int product_id = sqlite3_column_int(statement, 2);
  3154. char *category = (char*)sqlite3_column_text(statement, 3);
  3155. if(category==nil)
  3156. category="";
  3157. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  3158. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  3159. [item setValue:nsvalue forKey:@"title"];
  3160. [item setValue:url forKey:@"pic_url"];
  3161. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  3162. [params setValue:@"2" forKey:@"count"];
  3163. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  3164. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  3165. [param0 setValue:@"product_id" forKey:@"name"];
  3166. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  3167. [param1 setValue:nscategory forKey:@"val"];
  3168. [param1 setValue:@"category" forKey:@"name"];
  3169. [params setObject:param0 forKey:@"param_0"];
  3170. [params setObject:param1 forKey:@"param_1"];
  3171. [item setObject:params forKey:@"params"];
  3172. [ret setValue:nsselector_display forKey:@"name"];
  3173. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3174. i++;
  3175. }
  3176. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3177. [ret setValue:@"switch" forKey:@"action"];
  3178. sqlite3_finalize(statement);
  3179. }
  3180. // [iSalesDB close_db:db];
  3181. return ret;
  3182. }
  3183. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  3184. {
  3185. // model 在 category search 显示的图片。
  3186. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  3187. // sqlite3 *db = [iSalesDB get_db];
  3188. 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];
  3189. sqlite3_stmt * statement;
  3190. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3191. {
  3192. while (sqlite3_step(statement) == SQLITE_ROW)
  3193. {
  3194. char *url = (char*)sqlite3_column_text(statement, 0);
  3195. if(url==nil)
  3196. url="";
  3197. int type = sqlite3_column_int(statement, 1);
  3198. if(type==0)
  3199. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  3200. else
  3201. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  3202. }
  3203. sqlite3_finalize(statement);
  3204. }
  3205. // [iSalesDB close_db:db];
  3206. return ret;
  3207. }
  3208. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  3209. {
  3210. int item_id=-1;
  3211. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  3212. sqlite3_stmt * statement;
  3213. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3214. {
  3215. if (sqlite3_step(statement) == SQLITE_ROW)
  3216. {
  3217. item_id = sqlite3_column_int(statement, 0);
  3218. }
  3219. sqlite3_finalize(statement);
  3220. }
  3221. return item_id;
  3222. }
  3223. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  3224. {
  3225. // NSString* ret = @"";
  3226. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  3227. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  3228. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  3229. sqlite3_stmt * statement;
  3230. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3231. {
  3232. while (sqlite3_step(statement) == SQLITE_ROW)
  3233. {
  3234. int bitem_id = sqlite3_column_int(statement, 0);
  3235. int bitem_qty = sqlite3_column_int(statement, 1);
  3236. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  3237. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  3238. }
  3239. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  3240. sqlite3_finalize(statement);
  3241. }
  3242. // if(ret==nil)
  3243. // ret=@"";
  3244. *count=arr_count;
  3245. return arr_bundle;
  3246. }
  3247. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  3248. {
  3249. // get default sold qty, return -1 if model not found;
  3250. int ret = -1;
  3251. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  3252. sqlite3_stmt * statement;
  3253. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3254. {
  3255. if (sqlite3_step(statement) == SQLITE_ROW)
  3256. {
  3257. ret = sqlite3_column_int(statement, 0);
  3258. }
  3259. sqlite3_finalize(statement);
  3260. }
  3261. return ret;
  3262. }
  3263. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  3264. {
  3265. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3266. [ret setValue:@"0" forKey:@"img_count"];
  3267. // sqlite3 *db = [iSalesDB get_db];
  3268. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  3269. sqlite3_stmt * statement;
  3270. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3271. {
  3272. int i=0;
  3273. while (sqlite3_step(statement) == SQLITE_ROW)
  3274. {
  3275. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3276. // char *name = (char*)sqlite3_column_text(statement, 1);
  3277. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3278. char *url = (char*)sqlite3_column_text(statement, 0);
  3279. if(url==nil)
  3280. url="";
  3281. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3282. [item setValue:nsurl forKey:@"s"];
  3283. [item setValue:nsurl forKey:@"l"];
  3284. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  3285. i++;
  3286. }
  3287. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  3288. sqlite3_finalize(statement);
  3289. }
  3290. // [iSalesDB close_db:db];
  3291. return ret;
  3292. }
  3293. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  3294. UIApplication * app = [UIApplication sharedApplication];
  3295. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3296. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  3297. sqlite3 *db = [iSalesDB get_db];
  3298. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  3299. NSString* product_id=params[@"product_id"];
  3300. NSString *item_count_str = params[@"item_count"];
  3301. NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  3302. NSArray *item_count_arr = item_count_str ? [RAUtils string2arr:item_count_str separator:@","] : nil;
  3303. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  3304. // NSString *sql = @"";
  3305. for(int i=0;i<arr.count;i++)
  3306. {
  3307. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  3308. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  3309. __block int cart_count = 0;
  3310. if (!item_count_str) {
  3311. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  3312. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3313. NSString *model_set = [self textAtColumn:0 statement:stmt];
  3314. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  3315. cart_count = [[model_set_components lastObject] integerValue];
  3316. }];
  3317. }
  3318. if(count==0)
  3319. {
  3320. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  3321. sqlite3_stmt *stmt;
  3322. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  3323. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],[arr[i] length],NULL);
  3324. if (item_count_arr) {
  3325. sqlite3_bind_int(stmt,2,[item_count_arr[i] integerValue]);
  3326. } else {
  3327. sqlite3_bind_int(stmt,2,cart_count);
  3328. }
  3329. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  3330. [iSalesDB execSql:@"ROLLBACK" db:db];
  3331. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  3332. [iSalesDB close_db:db];
  3333. DebugLog(@"add to wishlist error");
  3334. return ret;
  3335. }
  3336. } else {
  3337. int qty = 0;
  3338. if (item_count_arr) {
  3339. qty = [item_count_arr[i] integerValue];
  3340. } else {
  3341. qty = cart_count;
  3342. }
  3343. 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]];
  3344. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  3345. [iSalesDB execSql:@"ROLLBACK" db:db];
  3346. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  3347. [iSalesDB close_db:db];
  3348. DebugLog(@"add to wishlist error");
  3349. return ret;
  3350. }
  3351. }
  3352. }
  3353. // [iSalesDB execSql:sql db:db];
  3354. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3355. [iSalesDB execSql:@"END TRANSACTION" db:db];
  3356. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  3357. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  3358. [iSalesDB close_db:db];
  3359. appDelegate.wish_count =count;
  3360. [appDelegate update_count_mark];
  3361. ret[@"result"]= [NSNumber numberWithInt:2];
  3362. return ret;
  3363. }
  3364. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  3365. // 0 category
  3366. // 1 search
  3367. // 2 itemsearch
  3368. NSData *ret = nil;
  3369. NSDictionary *items = nil;
  3370. switch (from) {
  3371. case 0:{
  3372. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  3373. }
  3374. break;
  3375. case 1:{
  3376. items = [[self search:params limited:NO] objectForKey:@"items"];
  3377. }
  3378. break;
  3379. case 2:{
  3380. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  3381. }
  3382. break;
  3383. default:
  3384. break;
  3385. }
  3386. if (!items) {
  3387. return ret;
  3388. }
  3389. int count = [[items objectForKey:@"count"] intValue];
  3390. NSMutableString *product_id_str = [@"" mutableCopy];
  3391. for (int i = 0; i < count; i++) {
  3392. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  3393. NSDictionary *item = [items objectForKey:key];
  3394. NSString *product_id = [item objectForKey:@"product_id"];
  3395. if (i == 0) {
  3396. [product_id_str appendString:product_id];
  3397. } else {
  3398. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  3399. }
  3400. }
  3401. NSString *add_to = [self valueInParams:params key:@"addTo"];
  3402. if ([add_to isEqualToString:@"cart"]) {
  3403. NSString *order_code = [params objectForKey:@"orderCode"];
  3404. if (order_code.length) {
  3405. NSDictionary *newParams = @{
  3406. @"product_id" : product_id_str,
  3407. @"orderCode" : order_code
  3408. };
  3409. ret = [self offline_add2cart:newParams.mutableCopy];
  3410. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  3411. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  3412. ret = [RAUtils dict2data:retDic];
  3413. }
  3414. } else if([add_to isEqualToString:@"wishlist"]) {
  3415. NSDictionary *newParams = @{
  3416. @"product_id" : product_id_str
  3417. };
  3418. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  3419. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  3420. ret = [RAUtils dict2data:retDic];
  3421. } else if([add_to isEqualToString:@"portfolio"]) {
  3422. }
  3423. return ret;
  3424. }
  3425. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  3426. {
  3427. return [self addAll:params from:0];
  3428. }
  3429. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  3430. {
  3431. return [self addAll:params from:1];
  3432. }
  3433. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  3434. {
  3435. return [self addAll:params from:2];
  3436. }
  3437. #pragma mark - Jack
  3438. #warning 做SQL操作时转义!!
  3439. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  3440. // "val_227" : {
  3441. // "check" : 1,
  3442. // "value" : "US United States",
  3443. // "value_id" : "228"
  3444. // },
  3445. if (!countryCode) {
  3446. countryCode = @"US";
  3447. }
  3448. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  3449. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  3450. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  3451. int code_id = sqlite3_column_int(stmt, 3); // id
  3452. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3453. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  3454. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  3455. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3456. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  3457. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3458. }
  3459. long n = *count;
  3460. *count = n + 1;
  3461. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  3462. [container setValue:countryDic forKey:key];
  3463. }] mutableCopy];
  3464. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3465. return ret;
  3466. }
  3467. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  3468. countryCode = [self translateSingleQuote:countryCode];
  3469. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  3470. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3471. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  3472. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  3473. if (name == NULL) {
  3474. name = "";
  3475. }
  3476. if (code == NULL) {
  3477. code = "";
  3478. }
  3479. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3480. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  3481. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  3482. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3483. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  3484. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3485. }
  3486. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  3487. [container setValue:stateDic forKey:key];
  3488. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  3489. DebugLog(@"query all state error: %@",err_msg);
  3490. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3491. // [stateDic setValue:@"Other" forKey:@"value"];
  3492. // [stateDic setValue:@"" forKey:@"value_id"];
  3493. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3494. //
  3495. // if (state_code && [@"" isEqualToString:state_code]) {
  3496. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3497. // }
  3498. //
  3499. // NSString *key = [NSString stringWithFormat:@"val_0"];
  3500. // [container setValue:stateDic forKey:key];
  3501. }] mutableCopy];
  3502. // failure 可以不用了,一样的
  3503. if (ret.allKeys.count == 0) {
  3504. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3505. [stateDic setValue:@"Other" forKey:@"value"];
  3506. [stateDic setValue:@"" forKey:@"value_id"];
  3507. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3508. if (state_code && [@"" isEqualToString:state_code]) {
  3509. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3510. }
  3511. NSString *key = [NSString stringWithFormat:@"val_0"];
  3512. [ret setValue:stateDic forKey:key];
  3513. }
  3514. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3515. return ret;
  3516. }
  3517. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  3518. codeId = [self translateSingleQuote:codeId];
  3519. 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];
  3520. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3521. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  3522. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  3523. if (name == NULL) {
  3524. name = "";
  3525. }
  3526. if (code == NULL) {
  3527. code = "";
  3528. }
  3529. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3530. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  3531. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  3532. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3533. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  3534. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3535. }
  3536. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  3537. [container setValue:stateDic forKey:key];
  3538. }] mutableCopy];
  3539. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3540. return ret;
  3541. }
  3542. + (NSDictionary *)offline_getPrice {
  3543. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price;"];
  3544. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3545. char *name = (char *) sqlite3_column_text(stmt, 1);
  3546. int type = sqlite3_column_int(stmt, 2);
  3547. int orderBy = sqlite3_column_int(stmt, 3);
  3548. if (name == NULL) {
  3549. name = "";
  3550. }
  3551. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  3552. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  3553. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  3554. NSString *key = [NSString stringWithFormat:@"val_%d",orderBy];
  3555. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3556. if (orderBy == 0) {
  3557. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3558. }
  3559. [container setValue:priceDic forKey:key];
  3560. }] mutableCopy];
  3561. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3562. return ret;
  3563. }
  3564. + (NSDictionary *)offline_getSalesRep {
  3565. // 首先从offline_login表中取出sales_code
  3566. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  3567. NSString *user = app.user;
  3568. user = [self translateSingleQuote:user];
  3569. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  3570. __block NSString *user_code = @"";
  3571. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3572. char *code = (char *)sqlite3_column_text(stmt, 0);
  3573. if (code == NULL) {
  3574. code = "";
  3575. }
  3576. user_code = [NSString stringWithUTF8String:code];
  3577. }];
  3578. // 再取所有salesRep
  3579. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  3580. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3581. // 1 name 2 code 3 salesrep_id
  3582. char *name = (char *)sqlite3_column_text(stmt, 1);
  3583. char *code = (char *)sqlite3_column_text(stmt, 2);
  3584. int salesrep_id = sqlite3_column_int(stmt, 3);
  3585. if (name == NULL) {
  3586. name = "";
  3587. }
  3588. if (code == NULL) {
  3589. code = "";
  3590. }
  3591. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  3592. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  3593. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  3594. // 比较code 相等则check
  3595. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  3596. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3597. }
  3598. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  3599. }] mutableCopy];
  3600. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3601. return ret;
  3602. }
  3603. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  3604. zipcode = [self translateSingleQuote:zipcode];
  3605. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  3606. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3607. char *country = (char *)sqlite3_column_text(stmt, 0);
  3608. char *state = (char *)sqlite3_column_text(stmt, 1);
  3609. char *city = (char *)sqlite3_column_text(stmt, 2);
  3610. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  3611. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  3612. if (country == NULL) {
  3613. country = "";
  3614. }
  3615. if (state == NULL) {
  3616. state = "";
  3617. }
  3618. if (city == NULL) {
  3619. city = "";
  3620. }
  3621. if (country_code == NULL) {
  3622. country_code = "";
  3623. }
  3624. if (state_code == NULL) {
  3625. state_code = "";
  3626. }
  3627. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  3628. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  3629. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  3630. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  3631. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  3632. }] mutableCopy];
  3633. return ret;
  3634. }
  3635. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  3636. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  3637. [item setValue:value forKey:valueKey];
  3638. [dic setValue:item forKey:itemKey];
  3639. }
  3640. + (NSString *)countryCodeByid:(NSString *)code_id {
  3641. NSString *ret = nil;
  3642. code_id = [self translateSingleQuote:code_id];
  3643. sqlite3 *db = [iSalesDB get_db];
  3644. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  3645. sqlite3_stmt * statement;
  3646. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3647. while (sqlite3_step(statement) == SQLITE_ROW) {
  3648. char *code = (char *)sqlite3_column_text(statement, 0);
  3649. if (code == NULL) {
  3650. code = "";
  3651. }
  3652. ret = [NSString stringWithUTF8String:code];
  3653. }
  3654. sqlite3_finalize(statement);
  3655. }
  3656. [iSalesDB close_db:db];
  3657. return ret;
  3658. }
  3659. + (NSString *)countryCodeIdByCode:(NSString *)code {
  3660. NSString *ret = nil;
  3661. code = [self translateSingleQuote:code];
  3662. sqlite3 *db = [iSalesDB get_db];
  3663. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  3664. sqlite3_stmt * statement;
  3665. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3666. while (sqlite3_step(statement) == SQLITE_ROW) {
  3667. char *_id = (char *)sqlite3_column_text(statement, 0);
  3668. if (_id == NULL) {
  3669. _id = "";
  3670. }
  3671. ret = [NSString stringWithFormat:@"%s",_id];
  3672. }
  3673. sqlite3_finalize(statement);
  3674. }
  3675. [iSalesDB close_db:db];
  3676. return ret;
  3677. }
  3678. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  3679. NSString *name = nil;
  3680. codeId = [self translateSingleQuote:codeId];
  3681. sqlite3 *db = [iSalesDB get_db];
  3682. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  3683. sqlite3_stmt * statement;
  3684. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3685. while (sqlite3_step(statement) == SQLITE_ROW) {
  3686. char *value = (char *)sqlite3_column_text(statement, 0);
  3687. if (value == NULL) {
  3688. value = "";
  3689. }
  3690. name = [NSString stringWithUTF8String:value];
  3691. }
  3692. sqlite3_finalize(statement);
  3693. }
  3694. [iSalesDB close_db:db];
  3695. return name;
  3696. }
  3697. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  3698. NSString *ret = nil;
  3699. sqlite3 *db = [iSalesDB get_db];
  3700. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  3701. sqlite3_stmt * statement;
  3702. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3703. while (sqlite3_step(statement) == SQLITE_ROW) {
  3704. char *name = (char *)sqlite3_column_text(statement, 0);
  3705. if (name == NULL) {
  3706. name = "";
  3707. }
  3708. ret = [NSString stringWithUTF8String:name];
  3709. }
  3710. sqlite3_finalize(statement);
  3711. }
  3712. [iSalesDB close_db:db];
  3713. return ret;
  3714. }
  3715. + (NSString *)salesRepCodeById:(NSString *)_id {
  3716. NSString *ret = nil;
  3717. _id = [self translateSingleQuote:_id];
  3718. sqlite3 *db = [iSalesDB get_db];
  3719. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  3720. sqlite3_stmt * statement;
  3721. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3722. while (sqlite3_step(statement) == SQLITE_ROW) {
  3723. char *rep = (char *)sqlite3_column_text(statement, 0);
  3724. if (rep == NULL) {
  3725. rep = "";
  3726. }
  3727. ret = [NSString stringWithUTF8String:rep];
  3728. }
  3729. sqlite3_finalize(statement);
  3730. }
  3731. [iSalesDB close_db:db];
  3732. return ret;
  3733. }
  3734. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  3735. char *tx = (char *)sqlite3_column_text(stmt, col);
  3736. if (tx == NULL) {
  3737. tx = "";
  3738. }
  3739. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  3740. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  3741. if (!text) {
  3742. text = @"";
  3743. }
  3744. // 将字符全部为' '的字符串干掉
  3745. int spaceCount = 0;
  3746. for (int i = 0; i < text.length; i++) {
  3747. if ([text characterAtIndex:i] == ' ') {
  3748. spaceCount++;
  3749. }
  3750. }
  3751. if (spaceCount == text.length) {
  3752. text = @"";
  3753. }
  3754. return text;
  3755. }
  3756. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  3757. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  3758. NSData *data = [NSData dataWithContentsOfFile:path];
  3759. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  3760. return ret;
  3761. }
  3762. + (NSString *)textFileName:(NSString *)name {
  3763. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  3764. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  3765. if (!text) {
  3766. text = @"";
  3767. }
  3768. return text;
  3769. }
  3770. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  3771. return [[dic objectForKey:key] mutableCopy];
  3772. }
  3773. + (id)translateSingleQuote:(NSString *)string {
  3774. if ([string isKindOfClass:[NSString class]])
  3775. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3776. return string;
  3777. }
  3778. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  3779. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  3780. }
  3781. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  3782. NSString* ret= nil;
  3783. NSString *sqlQuery = nil;
  3784. // 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
  3785. 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];
  3786. sqlite3_stmt * statement;
  3787. // int count=0;
  3788. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3789. {
  3790. if (sqlite3_step(statement) == SQLITE_ROW)
  3791. {
  3792. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  3793. if(imgurl==nil)
  3794. imgurl="";
  3795. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  3796. ret=nsimgurl;
  3797. }
  3798. sqlite3_finalize(statement);
  3799. }
  3800. else
  3801. {
  3802. [ret setValue:@"8" forKey:@"result"];
  3803. }
  3804. // [iSalesDB close_db:db];
  3805. // DebugLog(@"data string: %@",ret );
  3806. return ret;
  3807. }
  3808. #pragma mark contact list
  3809. +(NSDictionary*) offline_contactlist :(NSMutableDictionary *) params
  3810. {
  3811. // contactType = "Sales_Order_Customer";
  3812. // limit = 25;
  3813. // offset = 0;
  3814. // password = 123456;
  3815. // "price_name" = 16;
  3816. // user = EvanK;
  3817. sqlite3 *db = [iSalesDB get_db];
  3818. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  3819. if (contactType) {
  3820. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  3821. } else {
  3822. contactType = @"1 = 1";
  3823. }
  3824. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  3825. DebugLog(@"offline contact list keyword: %@",keyword);
  3826. // advanced search
  3827. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  3828. if (contact_name) {
  3829. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3830. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  3831. } else {
  3832. contact_name = @"";
  3833. }
  3834. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  3835. if (customer_phone) {
  3836. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3837. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  3838. } else {
  3839. customer_phone = @"";
  3840. }
  3841. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  3842. if (customer_fax) {
  3843. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3844. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  3845. } else {
  3846. customer_fax = @"";
  3847. }
  3848. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  3849. if (customer_zipcode) {
  3850. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3851. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  3852. } else {
  3853. customer_zipcode = @"";
  3854. }
  3855. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  3856. if (customer_sales_rep) {
  3857. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3858. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  3859. } else {
  3860. customer_sales_rep = @"";
  3861. }
  3862. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  3863. if (customer_state) {
  3864. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3865. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  3866. } else {
  3867. customer_state = @"";
  3868. }
  3869. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  3870. if (customer_name) {
  3871. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3872. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  3873. } else {
  3874. customer_name = @"";
  3875. }
  3876. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  3877. if (customer_country) {
  3878. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3879. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  3880. } else {
  3881. customer_country = @"";
  3882. }
  3883. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  3884. if (customer_cid) {
  3885. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3886. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  3887. } else {
  3888. customer_cid = @"";
  3889. }
  3890. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  3891. if (customer_city) {
  3892. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3893. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  3894. } else {
  3895. customer_city = @"";
  3896. }
  3897. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  3898. if (customer_address) {
  3899. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3900. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  3901. } else {
  3902. customer_address = @"";
  3903. }
  3904. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  3905. if (customer_email) {
  3906. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3907. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  3908. } else {
  3909. customer_email = @"";
  3910. }
  3911. NSString *price_name = [params valueForKey:@"price_name"];
  3912. if (price_name) {
  3913. if ([price_name containsString:@","]) {
  3914. // 首先从 price表中查处name
  3915. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  3916. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  3917. for (int i = 1;i < pArray.count;i++) {
  3918. NSString *p = pArray[i];
  3919. [mutablePStr appendFormat:@" or type = %@ ",p];
  3920. }
  3921. [mutablePStr appendString:@";"];
  3922. __block NSMutableArray *price_name_array = [NSMutableArray array];
  3923. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3924. char *name = (char *)sqlite3_column_text(stmt, 0);
  3925. if (!name)
  3926. name = "";
  3927. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  3928. }];
  3929. // 再根据name 拼sql
  3930. NSMutableString *mutable_price_name = [NSMutableString string];
  3931. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  3932. for (int i = 1; i < price_name_array.count; i++) {
  3933. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  3934. }
  3935. [mutable_price_name appendString:@")"];
  3936. price_name = mutable_price_name;
  3937. } else {
  3938. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  3939. if ([price_name isEqualToString:@""]) {
  3940. price_name = @"";
  3941. } else {
  3942. __block NSString *price;
  3943. price_name = [self translateSingleQuote:price_name];
  3944. [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) {
  3945. char *p = (char *)sqlite3_column_text(stmt, 0);
  3946. if (p == NULL) {
  3947. p = "";
  3948. }
  3949. price = [NSString stringWithUTF8String:p];
  3950. }];
  3951. if ([price isEqualToString:@""]) {
  3952. price_name = @"";
  3953. } else {
  3954. price = [self translateSingleQuote:price];
  3955. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  3956. }
  3957. }
  3958. }
  3959. } else {
  3960. price_name = @"";
  3961. }
  3962. int limit = [[params valueForKey:@"limit"] intValue];
  3963. int offset = [[params valueForKey:@"offset"] intValue];
  3964. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3965. 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];
  3966. 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];
  3967. // int result= [iSalesDB AddExFunction:db];
  3968. int count =0;
  3969. NSString *sqlQuery = nil;
  3970. if(keyword.length==0)
  3971. {
  3972. // 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];
  3973. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  3974. sqlQuery = sql;
  3975. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  3976. }
  3977. else
  3978. {
  3979. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  3980. keyword = keyword.lowercaseString;
  3981. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3982. 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];
  3983. 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]];
  3984. }
  3985. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  3986. sqlite3_stmt * statement;
  3987. [ret setValue:@"2" forKey:@"result"];
  3988. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  3989. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3990. {
  3991. int i = 0;
  3992. while (sqlite3_step(statement) == SQLITE_ROW)
  3993. {
  3994. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3995. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3996. int editable = sqlite3_column_int(statement, 0);
  3997. char *company_name = (char*)sqlite3_column_text(statement, 1);
  3998. NSString *nscompany_name =nil;
  3999. if(company_name==nil)
  4000. nscompany_name=@"";
  4001. else
  4002. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  4003. char *country = (char*)sqlite3_column_text(statement, 2);
  4004. if(country==nil)
  4005. country="";
  4006. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  4007. // char *addr = (char*)sqlite3_column_text(statement, 3);
  4008. // if(addr==nil)
  4009. // addr="";
  4010. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  4011. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  4012. if(zipcode==nil)
  4013. zipcode="";
  4014. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  4015. char *state = (char*)sqlite3_column_text(statement, 5);
  4016. if(state==nil)
  4017. state="";
  4018. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  4019. char *city = (char*)sqlite3_column_text(statement, 6);
  4020. if(city==nil)
  4021. city="";
  4022. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  4023. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  4024. // NSString *nscontact_name = nil;
  4025. // if(contact_name==nil)
  4026. // nscontact_name=@"";
  4027. // else
  4028. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  4029. char *phone = (char*)sqlite3_column_text(statement, 8);
  4030. NSString *nsphone = nil;
  4031. if(phone==nil)
  4032. nsphone=@"";
  4033. else
  4034. nsphone= [[NSString alloc]initWithUTF8String:phone];
  4035. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  4036. if(contact_id==nil)
  4037. contact_id="";
  4038. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4039. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  4040. if(addr_1==nil)
  4041. addr_1="";
  4042. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4043. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  4044. if(addr_2==nil)
  4045. addr_2="";
  4046. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4047. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  4048. if(addr_3==nil)
  4049. addr_3="";
  4050. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4051. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  4052. if(addr_4==nil)
  4053. addr_4="";
  4054. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4055. char *first_name = (char*)sqlite3_column_text(statement, 14);
  4056. if(first_name==nil)
  4057. first_name="";
  4058. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  4059. char *last_name = (char*)sqlite3_column_text(statement, 15);
  4060. if(last_name==nil)
  4061. last_name="";
  4062. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  4063. char *fax = (char*)sqlite3_column_text(statement, 16);
  4064. NSString *nsfax = nil;
  4065. if(fax==nil)
  4066. nsfax=@"";
  4067. else
  4068. {
  4069. nsfax= [[NSString alloc]initWithUTF8String:fax];
  4070. if(nsfax.length>0)
  4071. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  4072. }
  4073. char *email = (char*)sqlite3_column_text(statement, 17);
  4074. NSString *nsemail = nil;
  4075. if(email==nil)
  4076. nsemail=@"";
  4077. else
  4078. {
  4079. nsemail= [[NSString alloc]initWithUTF8String:email];
  4080. if(nsemail.length>0)
  4081. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  4082. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  4083. }
  4084. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4085. [arr_name addObject:nsfirst_name];
  4086. [arr_name addObject:nslast_name];
  4087. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  4088. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  4089. {
  4090. // decrypt
  4091. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  4092. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  4093. nsphone=[AESCrypt fastdecrypt:nsphone];
  4094. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4095. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  4096. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4097. }
  4098. [arr_addr addObject:nscompany_name];
  4099. [arr_addr addObject:nscontact_name];
  4100. [arr_addr addObject:@"<br>"];
  4101. [arr_addr addObject:nsaddr_1];
  4102. [arr_addr addObject:nsaddr_2];
  4103. [arr_addr addObject:nsaddr_3];
  4104. [arr_addr addObject:nsaddr_4];
  4105. //[arr_addr addObject:nsaddr];
  4106. [arr_addr addObject:nszipcode];
  4107. [arr_addr addObject:nscity];
  4108. [arr_addr addObject:nsstate];
  4109. [arr_addr addObject:nscountry];
  4110. [arr_addr addObject:@"<br>"];
  4111. [arr_addr addObject:nsphone];
  4112. [arr_addr addObject:nsfax];
  4113. [arr_addr addObject:nsemail];
  4114. NSString * name = [RAUtils arr2string:arr_addr separator:@", " trim:true];
  4115. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  4116. [item setValue:name forKey:@"name"];
  4117. [item setValue:nscontact_id forKey:@"contact_id"];
  4118. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  4119. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4120. i++;
  4121. }
  4122. UIApplication * app = [UIApplication sharedApplication];
  4123. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4124. [ret setValue:appDelegate.mode forKey:@"mode"];
  4125. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  4126. sqlite3_finalize(statement);
  4127. }
  4128. [iSalesDB close_db:db];
  4129. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  4130. return ret;
  4131. }
  4132. #pragma mark contact Advanced search
  4133. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  4134. {
  4135. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  4136. return [RAUtils dict2data:contactAdvanceDic];
  4137. }
  4138. #pragma mark create new contact
  4139. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  4140. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  4141. NSData *data = [NSData dataWithContentsOfFile:path];
  4142. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4143. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  4144. NSString *countryCode = nil;
  4145. NSString *countryCode_id = nil;
  4146. NSString *stateCode = nil;
  4147. NSString *city = nil;
  4148. NSString *zipCode = nil;
  4149. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  4150. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  4151. NSString *code_id = params[@"country"];
  4152. countryCode_id = code_id;
  4153. countryCode = [self countryCodeByid:code_id];
  4154. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  4155. NSString *zip_code = params[@"zipcode"];
  4156. // 剔除全部为空格
  4157. int spaceCount = 0;
  4158. for (int i = 0; i < zip_code.length; i++) {
  4159. if ([zip_code characterAtIndex:i] == ' ') {
  4160. spaceCount++;
  4161. }
  4162. }
  4163. if (spaceCount == zip_code.length) {
  4164. zip_code = @"";
  4165. }
  4166. zipCode = zip_code;
  4167. if (zipCode.length > 0) {
  4168. countryCode_id = params[@"country"];
  4169. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  4170. countryCode = [dic valueForKey:@"country_code"];
  4171. if (!countryCode) {
  4172. countryCode = @"US";
  4173. }
  4174. stateCode = [dic valueForKey:@"state_code"];
  4175. city = [dic valueForKey:@"city"];
  4176. // zip code
  4177. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_10"] mutableCopy];
  4178. [zipDic setValue:zipCode forKey:@"value"];
  4179. [section_0 setValue:zipDic forKey:@"item_10"];
  4180. }
  4181. }
  4182. } else {
  4183. // default: US United States
  4184. countryCode = @"US";
  4185. countryCode_id = @"228";
  4186. }
  4187. // country
  4188. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  4189. [self setValue:allCountry forItemKey:@"item_5" valueKey:@"cadedate" inDictionary:section_0];
  4190. // state
  4191. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  4192. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  4193. [self setValue:allState forItemKey:@"item_11" valueKey:@"cadedate" inDictionary:section_0];
  4194. // city
  4195. if (city) {
  4196. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_12"] mutableCopy];
  4197. [cityDic setValue:city forKey:@"value"];
  4198. [section_0 setValue:cityDic forKey:@"item_12"];
  4199. }
  4200. // price type
  4201. NSDictionary *priceDic = [self offline_getPrice];
  4202. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  4203. // Sales Rep
  4204. NSDictionary *repDic = [self offline_getSalesRep];
  4205. [self setValue:repDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  4206. [ret setValue:section_0 forKey:@"section_0"];
  4207. return [RAUtils dict2data:ret];
  4208. }
  4209. #pragma mark save
  4210. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  4211. NSString *addr = nil;
  4212. NSString *contact_name = nil;
  4213. NSString *companyName = [params objectForKey:@"company"];
  4214. if (companyName) {
  4215. companyName = [AESCrypt fastencrypt:companyName];
  4216. } else {
  4217. companyName = @"";
  4218. }
  4219. DebugLog(@"company");
  4220. companyName = [self translateSingleQuote:companyName];
  4221. NSString *addr1 = [params objectForKey:@"address"];
  4222. NSString *addr2 = [params objectForKey:@"address2"];
  4223. NSString *addr3 = [params objectForKey:@"address_3"];
  4224. NSString *addr4 = [params objectForKey:@"address_4"];
  4225. if (!addr2) {
  4226. addr2 = @"";
  4227. }
  4228. if (!addr3) {
  4229. addr3 = @"";
  4230. }
  4231. if (!addr4) {
  4232. addr4 = @"";
  4233. }
  4234. if (!addr1) {
  4235. addr1 = @"";
  4236. }
  4237. DebugLog(@"addr");
  4238. addr = [RAUtils arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  4239. addr = [AESCrypt fastencrypt:addr];
  4240. addr = [self translateSingleQuote:addr];
  4241. if (addr1 && ![addr1 isEqualToString:@""]) {
  4242. addr1 = [AESCrypt fastencrypt:addr1];
  4243. }
  4244. addr1 = [self translateSingleQuote:addr1];
  4245. addr2 = [self translateSingleQuote:addr2];
  4246. addr3 = [self translateSingleQuote:addr3];
  4247. addr4 = [self translateSingleQuote:addr4];
  4248. NSString *country = [params objectForKey:@"country"];
  4249. if (country) {
  4250. country = [self countryNameByCountryCodeId:country];
  4251. } else {
  4252. country = @"";
  4253. }
  4254. DebugLog(@"country");
  4255. country = [self translateSingleQuote:country];
  4256. NSString *state = [params objectForKey:@"state"];
  4257. if (!state) {
  4258. state = @"";
  4259. }
  4260. DebugLog(@"state");
  4261. state = [self translateSingleQuote:state];
  4262. NSString *city = [params objectForKey:@"city"];
  4263. if (!city) {
  4264. city = @"";
  4265. }
  4266. city = [self translateSingleQuote:city];
  4267. NSString *zipcode = [params objectForKey:@"zipcode"];
  4268. if (!zipcode) {
  4269. zipcode = @"";
  4270. }
  4271. DebugLog(@"zip");
  4272. zipcode = [self translateSingleQuote:zipcode];
  4273. NSString *fistName = [params objectForKey:@"firstname"];
  4274. if (!fistName) {
  4275. fistName = @"";
  4276. }
  4277. NSString *lastName = [params objectForKey:@"lastname"];
  4278. if (!lastName) {
  4279. lastName = @"";
  4280. }
  4281. contact_name = [RAUtils arr2string:@[fistName,lastName] separator:@" " trim:true];
  4282. DebugLog(@"contact_name");
  4283. contact_name = [self translateSingleQuote:contact_name];
  4284. fistName = [self translateSingleQuote:fistName];
  4285. lastName = [self translateSingleQuote:lastName];
  4286. NSString *phone = [params objectForKey:@"phone"];
  4287. if (phone) {
  4288. phone = [AESCrypt fastencrypt:phone];
  4289. } else {
  4290. phone = @"";
  4291. }
  4292. DebugLog(@"PHONE");
  4293. phone = [self translateSingleQuote:phone];
  4294. NSString *fax = [params objectForKey:@"fax"];
  4295. if (!fax) {
  4296. fax = @"";
  4297. }
  4298. DebugLog(@"FAX");
  4299. fax = [self translateSingleQuote:fax];
  4300. NSString *email = [params objectForKey:@"email"];
  4301. if (!email) {
  4302. email = @"";
  4303. }
  4304. DebugLog(@"EMAIL:%@",email);
  4305. email = [self translateSingleQuote:email];
  4306. NSString *notes = [params objectForKey:@"contact_notes"];
  4307. if (!notes) {
  4308. notes = @"";
  4309. }
  4310. DebugLog(@"NOTE:%@",notes);
  4311. notes = [self translateSingleQuote:notes];
  4312. NSString *price = [params objectForKey:@"price_name"];
  4313. if (price) {
  4314. price = [self priceNameByPriceId:price];
  4315. } else {
  4316. price = @"";
  4317. }
  4318. DebugLog(@"PRICE");
  4319. price = [self translateSingleQuote:price];
  4320. NSString *salesRep = [params objectForKey:@"sales_rep"];
  4321. if (salesRep) {
  4322. salesRep = [self salesRepCodeById:salesRep];
  4323. } else {
  4324. salesRep = @"";
  4325. }
  4326. salesRep = [self translateSingleQuote:salesRep];
  4327. NSString *img = [params objectForKey:@"business_card"];
  4328. NSArray *array = [img componentsSeparatedByString:@","];
  4329. NSString *img_0 = array[0];
  4330. if (!img_0) {
  4331. img_0 = @"";
  4332. }
  4333. img_0 = [self translateSingleQuote:img_0];
  4334. NSString *img_1 = array[1];
  4335. if (!img_1) {
  4336. img_1 = @"";
  4337. }
  4338. img_1 = [self translateSingleQuote:img_1];
  4339. NSString *img_2 = array[2];
  4340. if (!img_2) {
  4341. img_2 = @"";
  4342. }
  4343. img_2 = [self translateSingleQuote:img_2];
  4344. NSString *contact_id = [NSUUID UUID].UUIDString;
  4345. // 判断更新时是否为customer
  4346. if (update) {
  4347. contact_id = [params objectForKey:@"contact_id"];
  4348. if (!contact_id) {
  4349. contact_id = @"";
  4350. }
  4351. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  4352. __block int customer = 0;
  4353. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4354. customer = sqlite3_column_int(stmt, 0);
  4355. }];
  4356. isCustomer = customer ? YES : NO;
  4357. }
  4358. NSMutableDictionary *sync_dic = [params mutableCopy];
  4359. if (isCustomer) {
  4360. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  4361. } else {
  4362. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  4363. }
  4364. NSString *sync_data = nil;
  4365. NSString *sql = nil;
  4366. if (update){
  4367. contact_id = [params objectForKey:@"contact_id"];
  4368. if (!contact_id) {
  4369. contact_id = @"";
  4370. }
  4371. [sync_dic setValue:contact_id forKey:@"contact_id"];
  4372. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  4373. sync_data = [RAUtils dict2string:sync_dic];
  4374. sync_data = [self translateSingleQuote:sync_data];
  4375. 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];
  4376. } else {
  4377. contact_id = [self translateSingleQuote:contact_id];
  4378. [sync_dic setValue:contact_id forKey:@"contact_id"];
  4379. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  4380. sync_data = [RAUtils dict2string:sync_dic];
  4381. sync_data = [self translateSingleQuote:sync_data];
  4382. 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];
  4383. }
  4384. int result = [iSalesDB execSql:sql];
  4385. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  4386. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  4387. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  4388. }
  4389. #pragma mark save new contact
  4390. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  4391. {
  4392. return [self offline_saveContact:params update:NO isCustomer:YES];
  4393. }
  4394. #pragma mark edit contact
  4395. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  4396. {
  4397. // {
  4398. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  4399. // password = 123456;
  4400. // user = EvanK;
  4401. // }
  4402. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  4403. NSData *data = [NSData dataWithContentsOfFile:path];
  4404. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4405. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  4406. NSString *countryCode = nil;
  4407. NSString *countryCode_id = nil;
  4408. NSString *stateCode = nil;
  4409. /*------contact infor------*/
  4410. __block NSString *country = nil;
  4411. __block NSString *company_name = nil;
  4412. __block NSString *contact_id = params[@"contact_id"];
  4413. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  4414. __block NSString *zipcode = nil;
  4415. __block NSString *state = nil; // state_code
  4416. __block NSString *city = nil; //
  4417. __block NSString *firt_name,*last_name;
  4418. __block NSString *phone,*fax,*email;
  4419. __block NSString *notes,*price_type,*sales_rep;
  4420. __block NSString *img_0,*img_1,*img_2;
  4421. contact_id = [self translateSingleQuote:contact_id];
  4422. 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];
  4423. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4424. country = [self textAtColumn:0 statement:stmt]; // country name
  4425. company_name = [self textAtColumn:1 statement:stmt];
  4426. addr_1 = [self textAtColumn:2 statement:stmt];
  4427. addr_2 = [self textAtColumn:3 statement:stmt];
  4428. addr_3 = [self textAtColumn:4 statement:stmt];
  4429. addr_4 = [self textAtColumn:5 statement:stmt];
  4430. zipcode = [self textAtColumn:6 statement:stmt];
  4431. state = [self textAtColumn:7 statement:stmt]; // state code
  4432. city = [self textAtColumn:8 statement:stmt];
  4433. firt_name = [self textAtColumn:9 statement:stmt];
  4434. last_name = [self textAtColumn:10 statement:stmt];
  4435. phone = [self textAtColumn:11 statement:stmt];
  4436. fax = [self textAtColumn:12 statement:stmt];
  4437. email = [self textAtColumn:13 statement:stmt];
  4438. notes = [self textAtColumn:14 statement:stmt];
  4439. price_type = [self textAtColumn:15 statement:stmt]; // name
  4440. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  4441. img_0 = [self textAtColumn:17 statement:stmt];
  4442. img_1 = [self textAtColumn:18 statement:stmt];
  4443. img_2 = [self textAtColumn:19 statement:stmt];
  4444. }];
  4445. // decrypt
  4446. if (company_name) {
  4447. company_name = [AESCrypt fastdecrypt:company_name];
  4448. }
  4449. if (addr_1) {
  4450. addr_1 = [AESCrypt fastdecrypt:addr_1];
  4451. }
  4452. if (phone) {
  4453. phone = [AESCrypt fastdecrypt:phone];
  4454. }
  4455. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  4456. countryCode = [iSalesDB jk_queryText:countrySql];
  4457. stateCode = state;
  4458. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  4459. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  4460. NSString *code_id = params[@"country"];
  4461. countryCode_id = code_id;
  4462. countryCode = [self countryCodeByid:code_id];
  4463. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  4464. NSString *zip_code = params[@"zipcode"];
  4465. // 剔除全部为空格
  4466. int spaceCount = 0;
  4467. for (int i = 0; i < zip_code.length; i++) {
  4468. if ([zip_code characterAtIndex:i] == ' ') {
  4469. spaceCount++;
  4470. }
  4471. }
  4472. if (spaceCount == zip_code.length) {
  4473. zip_code = @"";
  4474. }
  4475. if (zipcode.length > 0) {
  4476. zipcode = zip_code;
  4477. countryCode_id = params[@"country"];
  4478. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  4479. countryCode = [dic valueForKey:@"country_code"];
  4480. stateCode = [dic valueForKey:@"state_code"];
  4481. city = [dic valueForKey:@"city"];
  4482. // zip code
  4483. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  4484. [zipDic setValue:zipcode forKey:@"value"];
  4485. [section_0 setValue:zipDic forKey:@"item_8"];
  4486. }
  4487. }
  4488. }
  4489. // 0 Country
  4490. // 1 Company Name
  4491. // 2 Contact ID
  4492. // 3 Picture
  4493. // 4 Address 1
  4494. // 5 Address 2
  4495. // 6 Address 3
  4496. // 7 Address 4
  4497. // 8 Zip Code
  4498. // 9 State/Province
  4499. // 10 City
  4500. // 11 Contact First Name
  4501. // 12 Contact Last Name
  4502. // 13 Phone
  4503. // 14 Fax
  4504. // 15 Email
  4505. // 16 Contact Notes
  4506. // 17 Price Type
  4507. // 18 Sales Rep
  4508. // country
  4509. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  4510. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  4511. // company
  4512. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  4513. // contact_id
  4514. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  4515. // picture
  4516. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  4517. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  4518. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  4519. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  4520. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  4521. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  4522. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  4523. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  4524. // addr 1 2 3 4
  4525. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  4526. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  4527. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  4528. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  4529. // zip code
  4530. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  4531. // state
  4532. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  4533. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  4534. // city
  4535. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  4536. // first last
  4537. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  4538. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  4539. // phone fax email
  4540. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  4541. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  4542. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  4543. // notes
  4544. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  4545. // price
  4546. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  4547. for (NSString *key in priceDic.allKeys) {
  4548. if ([key containsString:@"val_"]) {
  4549. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  4550. if ([dic[@"value"] isEqualToString:price_type]) {
  4551. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  4552. [priceDic setValue:dic forKey:key];
  4553. }
  4554. }
  4555. }
  4556. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  4557. // Sales Rep
  4558. NSMutableDictionary *repDic = [[self offline_getSalesRep] mutableCopy];
  4559. for (NSString *key in repDic.allKeys) {
  4560. if ([key containsString:@"val_"]) {
  4561. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  4562. NSString *value = dic[@"value"];
  4563. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  4564. if (code && [code isEqualToString:sales_rep]) {
  4565. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  4566. [repDic setValue:dic forKey:key];
  4567. }
  4568. }
  4569. }
  4570. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  4571. [ret setValue:section_0 forKey:@"section_0"];
  4572. return [RAUtils dict2data:ret];
  4573. }
  4574. #pragma mark save contact
  4575. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  4576. {
  4577. return [self offline_saveContact:params update:YES isCustomer:YES];
  4578. }
  4579. #pragma mark category
  4580. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  4581. if (ck) {
  4582. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  4583. for (NSString *key in res.allKeys) {
  4584. if (![key isEqualToString:@"count"]) {
  4585. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  4586. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  4587. if ([val[@"value"] isEqualToString:ck]) {
  4588. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  4589. }
  4590. [res setValue:val forKey:key];
  4591. }
  4592. }
  4593. [dic setValue:res forKey:valueKey];
  4594. }
  4595. }
  4596. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  4597. // NSString* orderCode = [params valueForKey:@"orderCode"];
  4598. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4599. NSString* category = [params valueForKey:@"category"];
  4600. if (!category || [category isEqualToString:@""]) {
  4601. category = @"%";
  4602. }
  4603. category = [self translateSingleQuote:category];
  4604. int limit = [[params valueForKey:@"limit"] intValue];
  4605. int offset = [[params valueForKey:@"offset"] intValue];
  4606. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4607. NSString *limit_str = @"";
  4608. if (limited) {
  4609. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  4610. }
  4611. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  4612. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  4613. sqlite3 *db = [iSalesDB get_db];
  4614. // [iSalesDB AddExFunction:db];
  4615. int count;
  4616. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  4617. 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];
  4618. double price_min = 0;
  4619. double price_max = 0;
  4620. if ([params.allKeys containsObject:@"alert"]) {
  4621. // alert
  4622. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  4623. NSString *alert = params[@"alert"];
  4624. if ([alert isEqualToString:@"Display All"]) {
  4625. alert = [NSString stringWithFormat:@""];
  4626. } else {
  4627. alert = [self translateSingleQuote:alert];
  4628. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  4629. }
  4630. // available
  4631. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  4632. NSString *available = params[@"available"];
  4633. NSString *available_condition;
  4634. if ([available isEqualToString:@"Display All"]) {
  4635. available_condition = @"";
  4636. } else if ([available isEqualToString:@"Available Now"]) {
  4637. available_condition = @"and availability > 0";
  4638. } else {
  4639. available_condition = @"and availability == 0";
  4640. }
  4641. // best seller
  4642. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  4643. NSString *best_seller = @"";
  4644. NSString *order_best_seller = @"m.name asc";
  4645. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  4646. best_seller = @"and best_seller > 0";
  4647. order_best_seller = @"m.best_seller desc,m.name asc";
  4648. }
  4649. // price
  4650. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  4651. NSString *price = params[@"price"];
  4652. price_min = 0;
  4653. price_max = MAXFLOAT;
  4654. if (appDelegate.user) {
  4655. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  4656. NSMutableString *priceName = [NSMutableString string];
  4657. for (int i = 0; i < priceTypeArray.count; i++) {
  4658. NSString *pricetype = priceTypeArray[i];
  4659. pricetype = [self translateSingleQuote:pricetype];
  4660. if (i == 0) {
  4661. [priceName appendFormat:@"'%@'",pricetype];
  4662. } else {
  4663. [priceName appendFormat:@",'%@'",pricetype];
  4664. }
  4665. }
  4666. if ([price isEqualToString:@"Display All"]) {
  4667. price = [NSString stringWithFormat:@""];
  4668. } else if([price containsString:@"+"]){
  4669. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  4670. price_min = [price doubleValue];
  4671. 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];
  4672. } else {
  4673. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  4674. price_min = [[priceArray objectAtIndex:0] doubleValue];
  4675. price_max = [[priceArray objectAtIndex:1] doubleValue];
  4676. 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];
  4677. }
  4678. } else {
  4679. price = @"";
  4680. }
  4681. // sold_by_qty : Sold in quantities of %@
  4682. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  4683. NSString *qty = params[@"sold_by_qty"];
  4684. if ([qty isEqualToString:@"Display All"]) {
  4685. qty = @"";
  4686. } else {
  4687. qty = [self translateSingleQuote:qty];
  4688. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  4689. }
  4690. // cate
  4691. category = [self translateSingleQuote:category];
  4692. NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  4693. // where bestseller > 0 order by bestseller desc
  4694. // sql query: alert availability(int) best_seller(int) price qty
  4695. 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];
  4696. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  4697. }
  4698. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  4699. if (!appDelegate.user) {
  4700. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  4701. }
  4702. [ret setValue:filter forKey:@"filter"];
  4703. DebugLog(@"offline_category sql:%@",sqlQuery);
  4704. sqlite3_stmt * statement;
  4705. [ret setValue:@"2" forKey:@"result"];
  4706. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  4707. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  4708. // int count=0;
  4709. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4710. {
  4711. int i=0;
  4712. while (sqlite3_step(statement) == SQLITE_ROW)
  4713. {
  4714. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4715. char *name = (char*)sqlite3_column_text(statement, 0);
  4716. if(name==nil)
  4717. name="";
  4718. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  4719. char *description = (char*)sqlite3_column_text(statement, 1);
  4720. if(description==nil)
  4721. description="";
  4722. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  4723. int product_id = sqlite3_column_int(statement, 2);
  4724. int wid = sqlite3_column_int(statement, 3);
  4725. int closeout = sqlite3_column_int(statement, 4);
  4726. int cid = sqlite3_column_int(statement, 5);
  4727. int wisdelete = sqlite3_column_int(statement, 6);
  4728. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  4729. if(wid !=0 && wisdelete != 1)
  4730. [item setValue:@"true" forKey:@"wish_exists"];
  4731. else
  4732. [item setValue:@"false" forKey:@"wish_exists"];
  4733. if(closeout==0)
  4734. [item setValue:@"false" forKey:@"is_closeout"];
  4735. else
  4736. [item setValue:@"true" forKey:@"is_closeout"];
  4737. if(cid==0)
  4738. [item setValue:@"false" forKey:@"cart_exists"];
  4739. else
  4740. [item setValue:@"true" forKey:@"cart_exists"];
  4741. [item addEntriesFromDictionary:imgjson];
  4742. // [item setValue:nsurl forKey:@"img"];
  4743. [item setValue:nsname forKey:@"name"];
  4744. [item setValue:nsdescription forKey:@"description"];
  4745. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  4746. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4747. i++;
  4748. }
  4749. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4750. [ret setObject:items forKey:@"items"];
  4751. sqlite3_finalize(statement);
  4752. } else {
  4753. DebugLog(@"nothing...");
  4754. }
  4755. DebugLog(@"count:%d",count);
  4756. [iSalesDB close_db:db];
  4757. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  4758. return ret;
  4759. }
  4760. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  4761. {
  4762. return [self categoryList:params limited:YES];
  4763. }
  4764. # pragma mark item search
  4765. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  4766. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4767. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  4768. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  4769. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  4770. // category
  4771. NSDictionary *category_menu = [self offline_category_menu];
  4772. [filter setValue:category_menu forKey:@"category"];
  4773. NSString* where= nil;
  4774. NSString* orderby= @"m.name";
  4775. if (!filterSearch) {
  4776. int covertype = [[params valueForKey:@"covertype"] intValue];
  4777. switch (covertype) {
  4778. case 0:
  4779. {
  4780. where=@"m.category like'%%#005#%%'";
  4781. break;
  4782. }
  4783. case 1:
  4784. {
  4785. where=@"m.alert like '%QS%'";
  4786. break;
  4787. }
  4788. case 2:
  4789. {
  4790. where=@"m.availability>0";
  4791. break;
  4792. }
  4793. case 3:
  4794. {
  4795. where=@"m.best_seller>0";
  4796. orderby=@"m.best_seller desc,m.name asc";
  4797. break;
  4798. }
  4799. default:
  4800. where=@"1=1";
  4801. break;
  4802. }
  4803. }
  4804. int limit = [[params valueForKey:@"limit"] intValue];
  4805. int offset = [[params valueForKey:@"offset"] intValue];
  4806. NSString *limit_str = @"";
  4807. if (limited) {
  4808. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  4809. }
  4810. sqlite3 *db = [iSalesDB get_db];
  4811. // [iSalesDB AddExFunction:db];
  4812. int count;
  4813. NSString *sqlQuery = nil;
  4814. where = [where stringByAppendingString:@" and m.is_active = 1"];
  4815. 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];
  4816. double price_min = 0;
  4817. double price_max = 0;
  4818. if (filterSearch) {
  4819. // alert
  4820. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  4821. NSString *alert = params[@"alert"];
  4822. if ([alert isEqualToString:@"Display All"]) {
  4823. alert = [NSString stringWithFormat:@""];
  4824. } else {
  4825. alert = [self translateSingleQuote:alert];
  4826. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  4827. }
  4828. // available
  4829. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  4830. NSString *available = params[@"available"];
  4831. NSString *available_condition;
  4832. if ([available isEqualToString:@"Display All"]) {
  4833. available_condition = @"";
  4834. } else if ([available isEqualToString:@"Available Now"]) {
  4835. available_condition = @"and availability > 0";
  4836. } else {
  4837. available_condition = @"and availability == 0";
  4838. }
  4839. // best seller
  4840. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  4841. NSString *best_seller = @"";
  4842. NSString *order_best_seller = @"m.name asc";
  4843. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  4844. best_seller = @"and best_seller > 0";
  4845. order_best_seller = @"m.best_seller desc,m.name asc";
  4846. }
  4847. // price
  4848. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  4849. NSString *price = params[@"price"];
  4850. price_min = 0;
  4851. price_max = MAXFLOAT;
  4852. if (appDelegate.user) {
  4853. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  4854. NSMutableString *priceName = [NSMutableString string];
  4855. for (int i = 0; i < priceTypeArray.count; i++) {
  4856. NSString *pricetype = priceTypeArray[i];
  4857. pricetype = [self translateSingleQuote:pricetype];
  4858. if (i == 0) {
  4859. [priceName appendFormat:@"'%@'",pricetype];
  4860. } else {
  4861. [priceName appendFormat:@",'%@'",pricetype];
  4862. }
  4863. }
  4864. if ([price isEqualToString:@"Display All"]) {
  4865. price = [NSString stringWithFormat:@""];
  4866. } else if([price containsString:@"+"]){
  4867. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  4868. price_min = [price doubleValue];
  4869. 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];
  4870. } else {
  4871. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  4872. price_min = [[priceArray objectAtIndex:0] doubleValue];
  4873. price_max = [[priceArray objectAtIndex:1] doubleValue];
  4874. 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];
  4875. }
  4876. } else {
  4877. price = @"";
  4878. }
  4879. // sold_by_qty : Sold in quantities of %@
  4880. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  4881. NSString *qty = params[@"sold_by_qty"];
  4882. if ([qty isEqualToString:@"Display All"]) {
  4883. qty = @"";
  4884. } else {
  4885. qty = [self translateSingleQuote:qty];
  4886. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  4887. }
  4888. // category
  4889. NSString *category_id = params[@"ctgId"];
  4890. NSMutableArray *cate_id_array = nil;
  4891. NSMutableString *cateWhere = [NSMutableString string];
  4892. if ([category_id isEqualToString:@""] || !category_id) {
  4893. [cateWhere appendString:@"1 = 1"];
  4894. } else {
  4895. if ([category_id containsString:@","]) {
  4896. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  4897. } else {
  4898. cate_id_array = [@[category_id] mutableCopy];
  4899. }
  4900. /*-----------*/
  4901. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];
  4902. for (int i = 0; i < cate_id_array.count; i++) {
  4903. for (NSString *key0 in cateDic.allKeys) {
  4904. if ([key0 containsString:@"category_"]) {
  4905. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy];
  4906. for (NSString *key1 in category0.allKeys) {
  4907. if ([key1 containsString:@"category_"]) {
  4908. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy];
  4909. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  4910. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  4911. cate_id_array[i] = [category1 objectForKey:@"id"];
  4912. if (i == 0) {
  4913. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  4914. } else {
  4915. [cateWhere appendFormat:@"or category like'%%#%@#%%'",cate_id_array[i]];
  4916. }
  4917. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  4918. [category0 setValue:category1 forKey:key1];
  4919. [cateDic setValue:category0 forKey:key0];
  4920. }
  4921. }
  4922. }
  4923. }
  4924. }
  4925. }
  4926. [filter setValue:cateDic forKey:@"category"];
  4927. }
  4928. // where bestseller > 0 order by bestseller desc
  4929. // sql query: alert availability(int) best_seller(int) price qty
  4930. 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];
  4931. // count
  4932. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  4933. }
  4934. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  4935. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  4936. if (!appDelegate.user) {
  4937. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  4938. }
  4939. [ret setValue:filter forKey:@"filter"];
  4940. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  4941. sqlite3_stmt * statement;
  4942. [ret setValue:@"2" forKey:@"result"];
  4943. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  4944. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  4945. // int count=0;
  4946. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4947. {
  4948. int i=0;
  4949. while (sqlite3_step(statement) == SQLITE_ROW)
  4950. {
  4951. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4952. char *name = (char*)sqlite3_column_text(statement, 0);
  4953. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  4954. char *description = (char*)sqlite3_column_text(statement, 1);
  4955. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  4956. int product_id = sqlite3_column_int(statement, 2);
  4957. int wid = sqlite3_column_int(statement, 3);
  4958. int closeout = sqlite3_column_int(statement, 4);
  4959. int cid = sqlite3_column_int(statement, 5);
  4960. int wisdelete = sqlite3_column_int(statement, 6);
  4961. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  4962. if(wid !=0 && wisdelete != 1)
  4963. [item setValue:@"true" forKey:@"wish_exists"];
  4964. else
  4965. [item setValue:@"false" forKey:@"wish_exists"];
  4966. if(closeout==0)
  4967. [item setValue:@"false" forKey:@"is_closeout"];
  4968. else
  4969. [item setValue:@"true" forKey:@"is_closeout"];
  4970. if(cid==0)
  4971. [item setValue:@"false" forKey:@"cart_exists"];
  4972. else
  4973. [item setValue:@"true" forKey:@"cart_exists"];
  4974. [item addEntriesFromDictionary:imgjson];
  4975. // [item setValue:nsurl forKey:@"img"];
  4976. [item setValue:nsname forKey:@"fash_name"];
  4977. [item setValue:nsdescription forKey:@"description"];
  4978. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  4979. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4980. i++;
  4981. }
  4982. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4983. [ret setObject:items forKey:@"items"];
  4984. sqlite3_finalize(statement);
  4985. }
  4986. [iSalesDB close_db:db];
  4987. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  4988. return ret;
  4989. }
  4990. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  4991. {
  4992. return [self itemsearch:params limited:YES];
  4993. }
  4994. #pragma mark order detail
  4995. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  4996. if (str1.length == 0) {
  4997. str1 = @"&nbsp";
  4998. }
  4999. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  5000. return str;
  5001. }
  5002. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  5003. {
  5004. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  5005. [fromformatter setDateFormat:from];
  5006. NSDate *date = [fromformatter dateFromString:datetime];
  5007. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  5008. [toformatter setDateFormat:to];
  5009. NSString * ret = [toformatter stringFromDate:date];
  5010. return ret;
  5011. }
  5012. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  5013. // 把毫秒去掉
  5014. if ([dateTime containsString:@"."]) {
  5015. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  5016. }
  5017. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  5018. formatter.dateFormat = formate;
  5019. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  5020. NSDate *date = [formatter dateFromString:dateTime];
  5021. formatter.dateFormat = newFormate;
  5022. NSString *result = [formatter stringFromDate:date];
  5023. return result ? result : @"";
  5024. }
  5025. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  5026. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  5027. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  5028. }
  5029. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  5030. {
  5031. DebugLog(@"offline oderdetail params: %@",params);
  5032. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5033. int orderId = [params[@"orderId"] intValue];
  5034. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  5035. // decrypt card number and card security code
  5036. // 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 ];
  5037. 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];
  5038. sqlite3 *db = [iSalesDB get_db];
  5039. sqlite3_stmt * statement;
  5040. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  5041. NSString *nssoid = @"";
  5042. NSString* orderinfo = @"";
  5043. NSString *moreInfo = @"";
  5044. double handlingFee = 0;
  5045. double payments_and_credist = 0;
  5046. double totalPrice = 0;
  5047. double shippingFee = 0;
  5048. double lift_gate = 0;
  5049. NSString *customer_contact = @"";
  5050. NSString *customer_email = @"";
  5051. NSString *customer_fax = @"";
  5052. NSString *customer_phone = @"";
  5053. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  5054. {
  5055. if (sqlite3_step(statement) == SQLITE_ROW)
  5056. {
  5057. int order_id = sqlite3_column_int(statement, 0);
  5058. NSString *sign_url = [self textAtColumn:51 statement:statement];
  5059. ret[@"sign_url"] = sign_url;
  5060. customer_contact = [self textAtColumn:52 statement:statement];
  5061. customer_email = [self textAtColumn:53 statement:statement];
  5062. customer_phone = [self textAtColumn:54 statement:statement];
  5063. customer_fax = [self textAtColumn:55 statement:statement];
  5064. int offline_edit=sqlite3_column_int(statement, 56);
  5065. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  5066. char *soid = (char*)sqlite3_column_text(statement, 1);
  5067. if(soid==nil)
  5068. soid= "";
  5069. nssoid= [[NSString alloc]initWithUTF8String:soid];
  5070. // so#
  5071. ret[@"so#"] = nssoid;
  5072. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  5073. if(poNumber==nil)
  5074. poNumber= "";
  5075. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  5076. char *create_time = (char*)sqlite3_column_text(statement, 3);
  5077. if(create_time==nil)
  5078. create_time= "";
  5079. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  5080. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  5081. int status = sqlite3_column_int(statement, 4);
  5082. int erpStatus = sqlite3_column_int(statement, 49);
  5083. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  5084. // status
  5085. if (status > 1 && status != 3) {
  5086. status = erpStatus;
  5087. } else if (status == 3) {
  5088. status = 15;
  5089. }
  5090. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  5091. ret[@"order_status"] = nsstatus;
  5092. char *company_name = (char*)sqlite3_column_text(statement, 5);
  5093. if(company_name==nil)
  5094. company_name= "";
  5095. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  5096. // company name
  5097. ret[@"company_name"] = nscompany_name;
  5098. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  5099. if(customer_contact==nil)
  5100. customer_contact= "";
  5101. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  5102. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  5103. if(addr_1==nil)
  5104. addr_1="";
  5105. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  5106. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  5107. if(addr_2==nil)
  5108. addr_2="";
  5109. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  5110. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  5111. if(addr_3==nil)
  5112. addr_3="";
  5113. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  5114. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  5115. if(addr_4==nil)
  5116. addr_4="";
  5117. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  5118. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  5119. [arr_addr addObject:nsaddr_1];
  5120. [arr_addr addObject:nsaddr_2];
  5121. [arr_addr addObject:nsaddr_3];
  5122. [arr_addr addObject:nsaddr_4];
  5123. NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  5124. char *logist = (char*)sqlite3_column_text(statement, 11);
  5125. if(logist==nil)
  5126. logist= "";
  5127. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  5128. if (status == -11 || status == 10 || status == 11) {
  5129. nslogist = [self textAtColumn:59 statement:statement];
  5130. };
  5131. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  5132. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  5133. shipping = @"Shipping To Be Quoted";
  5134. } else {
  5135. shippingFee = sqlite3_column_double(statement, 12);
  5136. }
  5137. // Shipping
  5138. ret[@"Shipping"] = shipping;
  5139. int have_lift_gate = sqlite3_column_int(statement, 17);
  5140. lift_gate = sqlite3_column_double(statement, 13);
  5141. // Liftgate Fee(No loading dock)
  5142. if (!have_lift_gate) {
  5143. lift_gate = 0;
  5144. }
  5145. ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  5146. if (sqlite3_column_int(statement, 57)) {
  5147. ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  5148. }
  5149. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  5150. if(general_notes==nil)
  5151. general_notes= "";
  5152. NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  5153. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  5154. if(internal_notes==nil)
  5155. internal_notes= "";
  5156. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  5157. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  5158. if(payment_type==nil)
  5159. payment_type= "";
  5160. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  5161. // order info
  5162. orderinfo = [self textFileName:@"order_info.html"];
  5163. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  5164. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  5165. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  5166. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  5167. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  5168. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  5169. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  5170. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  5171. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  5172. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  5173. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  5174. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  5175. NSString *payment = nil;
  5176. if([nspayment_type isEqualToString:@"Credit Card"])
  5177. {
  5178. payment = [self textFileName:@"creditcardpayment.html"];
  5179. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  5180. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  5181. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  5182. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  5183. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  5184. NSString *card_type = [self textAtColumn:42 statement:statement];
  5185. if (card_type.length > 0) { // 显示星号
  5186. card_type = @"****";
  5187. }
  5188. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  5189. if (card_number.length > 0 && card_number.length > 4) {
  5190. for (int i = 0; i < card_number.length - 4; i++) {
  5191. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  5192. }
  5193. } else {
  5194. card_number = @"";
  5195. }
  5196. NSString *card_security_code = [self textAtColumn:44 statement:statement];
  5197. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  5198. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  5199. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  5200. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  5201. card_expiration = @"****";
  5202. }
  5203. NSString *card_city = [self textAtColumn:46 statement:statement];
  5204. NSString *card_state = [self textAtColumn:47 statement:statement];
  5205. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  5206. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  5207. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  5208. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  5209. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  5210. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  5211. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  5212. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  5213. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  5214. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  5215. }
  5216. else
  5217. {
  5218. payment=[self textFileName:@"normalpayment.html"];
  5219. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  5220. }
  5221. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  5222. ret[@"result"]= [NSNumber numberWithInt:2];
  5223. // more info
  5224. moreInfo = [self textFileName:@"more_info.html"];
  5225. /*****ship to******/
  5226. // ShipToCompany_or_&nbsp
  5227. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  5228. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  5229. NSString *shipToName = [self textAtColumn:19 statement:statement];
  5230. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  5231. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  5232. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  5233. /*****ship from******/
  5234. // ShipFromCompany_or_&nbsp
  5235. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  5236. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  5237. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  5238. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  5239. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  5240. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  5241. /*****freight to******/
  5242. // FreightBillToCompany_or_&nbsp
  5243. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  5244. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  5245. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  5246. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  5247. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  5248. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  5249. /*****merchandise to******/
  5250. // MerchandiseBillToCompany_or_&nbsp
  5251. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  5252. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  5253. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  5254. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  5255. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  5256. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  5257. /*****return to******/
  5258. // ReturnToCompany_or_&nbsp
  5259. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  5260. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  5261. NSString *returnToName = [self textAtColumn:31 statement:statement];
  5262. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  5263. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  5264. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  5265. //
  5266. // DebugLog(@"more info : %@",moreInfo);
  5267. // handling fee
  5268. handlingFee = sqlite3_column_double(statement, 33);
  5269. ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  5270. if (sqlite3_column_int(statement, 58)) {
  5271. ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  5272. }
  5273. //
  5274. // customer info
  5275. customerID = [self textAtColumn:36 statement:statement];
  5276. // mode
  5277. ret[@"mode"] = appDelegate.mode;
  5278. // model_count
  5279. ret[@"model_count"] = @(0);
  5280. }
  5281. sqlite3_finalize(statement);
  5282. }
  5283. [iSalesDB close_db:db];
  5284. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  5285. // "customer_email" = "Shui Hu";
  5286. // "customer_fax" = "";
  5287. // "customer_first_name" = F;
  5288. // "customer_last_name" = L;
  5289. // "customer_name" = ",da He Xiang Dong Liu A";
  5290. // "customer_phone" = "Hey Xuan Feng";
  5291. contactInfo[@"customer_phone"] = customer_phone;
  5292. contactInfo[@"customer_fax"] = customer_fax;
  5293. contactInfo[@"customer_email"] = customer_email;
  5294. NSString *first_name = @"";
  5295. NSString *last_name = @"";
  5296. if ([customer_contact isEqualToString:@""]) {
  5297. } else if ([customer_contact containsString:@" "]) {
  5298. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  5299. first_name = [customer_contact substringToIndex:first_space_index];
  5300. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  5301. }
  5302. contactInfo[@"customer_first_name"] = first_name;
  5303. contactInfo[@"customer_last_name"] = last_name;
  5304. ret[@"customerInfo"] = contactInfo;
  5305. // models
  5306. if (nssoid) {
  5307. __block double TotalCuft = 0;
  5308. __block double TotalWeight = 0;
  5309. __block int TotalCarton = 0;
  5310. __block double allItemPrice = 0;
  5311. 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];
  5312. sqlite3 *db1 = [iSalesDB get_db];
  5313. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5314. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  5315. int product_id = sqlite3_column_int(stmt, 0);
  5316. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  5317. int item_id = sqlite3_column_int(stmt, 7);
  5318. NSString* Price=nil;
  5319. if(str_price==nil)
  5320. {
  5321. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  5322. if(price==nil)
  5323. Price=@"No Price.";
  5324. else
  5325. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  5326. }
  5327. else
  5328. {
  5329. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  5330. }
  5331. double discount = sqlite3_column_double(stmt, 2);
  5332. int item_count = sqlite3_column_int(stmt, 3);
  5333. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  5334. NSString *nsline_note=nil;
  5335. if(line_note!=nil)
  5336. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  5337. // char *name = (char*)sqlite3_column_text(stmt, 5);
  5338. // NSString *nsname = nil;
  5339. // if(name!=nil)
  5340. // nsname= [[NSString alloc]initWithUTF8String:name];
  5341. NSString *nsname = [self textAtColumn:5 statement:stmt];
  5342. // char *description = (char*)sqlite3_column_text(stmt, 6);
  5343. // NSString *nsdescription=nil;
  5344. // if(description!=nil)
  5345. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  5346. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  5347. // int stockUom = sqlite3_column_int(stmt, 8);
  5348. // int _id = sqlite3_column_int(stmt, 9);
  5349. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  5350. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  5351. double weight=[bsubtotaljson[@"weight"] doubleValue];
  5352. int carton=[bsubtotaljson[@"carton"] intValue];
  5353. TotalCuft += cuft;
  5354. TotalWeight += weight;
  5355. TotalCarton += carton;
  5356. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  5357. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  5358. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  5359. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  5360. itemjson[@"note"]=nsline_note;
  5361. itemjson[@"origin_price"] = Price;
  5362. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  5363. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  5364. itemjson[@"order_item_status"] = @""; // 暂时不处理
  5365. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  5366. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  5367. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  5368. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  5369. if(itemjson[@"combine"] != nil)
  5370. {
  5371. // int citem=0;
  5372. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  5373. for(int bc=0;bc<bcount;bc++)
  5374. {
  5375. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  5376. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  5377. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  5378. subTotal += uprice * modulus * item_count;
  5379. }
  5380. }
  5381. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  5382. // NSLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  5383. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  5384. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  5385. allItemPrice += subTotal;
  5386. }];
  5387. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  5388. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  5389. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  5390. // payments/Credits
  5391. // payments_and_credist = sqlite3_column_double(statement, 34);
  5392. payments_and_credist = allItemPrice;
  5393. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  5394. // // total
  5395. // totalPrice = sqlite3_column_double(statement, 35);
  5396. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  5397. } else {
  5398. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  5399. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  5400. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  5401. // payments/Credits
  5402. payments_and_credist = 0;
  5403. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  5404. }
  5405. // total
  5406. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  5407. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  5408. ret[@"order_info"]= orderinfo;
  5409. ret[@"more_order_info"] = moreInfo;
  5410. return [RAUtils dict2data:ret];
  5411. }
  5412. #pragma mark order list
  5413. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  5414. NSString *nsstatus = @"";
  5415. switch (status) {
  5416. case 0:
  5417. {
  5418. nsstatus=@"Temp Order";
  5419. break;
  5420. }
  5421. case 1:
  5422. {
  5423. nsstatus=@"Saved Order";
  5424. break;
  5425. }
  5426. case 2: {
  5427. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  5428. break;
  5429. }
  5430. case 3:
  5431. case 15:
  5432. {
  5433. nsstatus=@"Cancelled";
  5434. break;
  5435. }
  5436. case 10:
  5437. {
  5438. nsstatus=@"Quote Submitted";
  5439. break;
  5440. }
  5441. case 11:
  5442. {
  5443. nsstatus=@"Sales Order Submitted";
  5444. break;
  5445. }
  5446. case 12:
  5447. {
  5448. nsstatus=@"Processing";
  5449. break;
  5450. }
  5451. case 13:
  5452. {
  5453. nsstatus=@"Shipped";
  5454. break;
  5455. }
  5456. case 14:
  5457. {
  5458. nsstatus=@"Closed";
  5459. break;
  5460. }
  5461. case -11:
  5462. {
  5463. nsstatus = @"Ready For Submit";
  5464. break;
  5465. }
  5466. default:
  5467. break;
  5468. }
  5469. return nsstatus;
  5470. }
  5471. + (double) orderTotalPrice:(NSString *)so_id db:(sqlite3 *)db1 close:(BOOL)close{
  5472. double total = 0;
  5473. __block double payments_and_credist = 0;
  5474. __block double allItemPrice = 0;
  5475. // sqlite3 *db1 = [iSalesDB get_db];
  5476. if (so_id) {
  5477. // 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];
  5478. 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];
  5479. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5480. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5481. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  5482. int product_id = sqlite3_column_int(stmt, 0);
  5483. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  5484. int discount = sqlite3_column_int(stmt, 2);
  5485. int item_count = sqlite3_column_int(stmt, 3);
  5486. // int item_id = sqlite3_column_int(stmt, 7);
  5487. int item_id = sqlite3_column_int(stmt, 4);
  5488. NSString* Price=nil;
  5489. if(str_price==nil)
  5490. {
  5491. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  5492. if(price==nil)
  5493. Price=@"No Price.";
  5494. else
  5495. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  5496. }
  5497. else
  5498. {
  5499. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  5500. }
  5501. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  5502. itemjson[@"The unit price"]=Price;
  5503. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  5504. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  5505. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  5506. if(itemjson[@"combine"] != nil)
  5507. {
  5508. // int citem=0;
  5509. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  5510. for(int bc=0;bc<bcount;bc++)
  5511. {
  5512. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  5513. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  5514. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  5515. subTotal += uprice * modulus * item_count;
  5516. }
  5517. }
  5518. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  5519. allItemPrice += subTotal;
  5520. }];
  5521. payments_and_credist = allItemPrice;
  5522. } else {
  5523. // payments/Credits
  5524. payments_and_credist = 0;
  5525. }
  5526. // lift_gate handlingFee shippingFee
  5527. __block double lift_gate = 0;
  5528. __block double handlingFee = 0;
  5529. __block double shippingFee = 0;
  5530. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  5531. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5532. int have_lift_gate = sqlite3_column_int(stmt, 0);
  5533. if (have_lift_gate) {
  5534. lift_gate = sqlite3_column_double(stmt, 1);
  5535. }
  5536. handlingFee = sqlite3_column_double(stmt, 2);
  5537. shippingFee = sqlite3_column_double(stmt, 3);
  5538. }];
  5539. // total
  5540. total = payments_and_credist + lift_gate + handlingFee + shippingFee;
  5541. if (close) {
  5542. [iSalesDB close_db:db1];
  5543. }
  5544. return total;
  5545. }
  5546. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  5547. {
  5548. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5549. int limit = [[params valueForKey:@"limit"] intValue];
  5550. int offset = [[params valueForKey:@"offset"] intValue];
  5551. NSString* keyword = [params valueForKey:@"keyWord"];
  5552. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  5553. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  5554. NSString* where=@"1 = 1";
  5555. if(keyword.length>0)
  5556. 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]];
  5557. if (orderStatus.length > 0) {
  5558. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  5559. if (order_status_array.count == 1) {
  5560. int status_value = [[order_status_array firstObject] integerValue];
  5561. if (status_value <= 1 || status_value == 3) {
  5562. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  5563. } else {
  5564. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  5565. }
  5566. } else if (order_status_array.count > 1) {
  5567. for (int i = 0; i < order_status_array.count;i++) {
  5568. NSString *status = order_status_array[i];
  5569. NSString *condition = @" or";
  5570. if (i == 0) {
  5571. condition = @" and (";
  5572. }
  5573. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  5574. int status_value = [status integerValue];
  5575. if (status_value <= 1 || status_value == 3) {
  5576. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  5577. } else {
  5578. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  5579. }
  5580. }
  5581. where = [where stringByAppendingString:@" )"];
  5582. }
  5583. }
  5584. 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];
  5585. // DebugLog(@"order list sql: %@",sqlQuery);
  5586. sqlite3 *db = [iSalesDB get_db];
  5587. sqlite3_stmt * statement;
  5588. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  5589. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5590. {
  5591. int count=0;
  5592. while (sqlite3_step(statement) == SQLITE_ROW)
  5593. {
  5594. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  5595. int order_id = sqlite3_column_double(statement, 0);
  5596. char *soid = (char*)sqlite3_column_text(statement, 1);
  5597. if(soid==nil)
  5598. soid= "";
  5599. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  5600. int status = sqlite3_column_double(statement, 2);
  5601. int erpStatus = sqlite3_column_double(statement, 9);
  5602. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  5603. if(sales_rep==nil)
  5604. sales_rep= "";
  5605. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  5606. char *create_by = (char*)sqlite3_column_text(statement, 4);
  5607. if(create_by==nil)
  5608. create_by= "";
  5609. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  5610. char *company_name = (char*)sqlite3_column_text(statement, 5);
  5611. if(company_name==nil)
  5612. company_name= "";
  5613. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  5614. char *create_time = (char*)sqlite3_column_text(statement, 6);
  5615. if(create_time==nil)
  5616. create_time= "";
  5617. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  5618. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  5619. // double total_price = sqlite3_column_double(statement, 7);
  5620. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] db:db close:NO];
  5621. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  5622. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  5623. int offline_edit = sqlite3_column_int(statement, 10);
  5624. // ": "JH",
  5625. // "": "$8307.00",
  5626. // "": "MOB1608050001",
  5627. // "": "ArpithaT",
  5628. // "": "1st Stage Property Transformations",
  5629. // "": "JANICE SUTTON",
  5630. // "": 2255,
  5631. // "": "08/02/2016 09:49:18",
  5632. // "": 1,
  5633. // "": "Saved Order"
  5634. // "": "1470384050483",
  5635. // "model_count": "6 / 28"
  5636. item[@"sales_rep"]= nssales_rep;
  5637. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  5638. item[@"so#"]= nssoid;
  5639. item[@"create_by"]= nscreate_by;
  5640. item[@"customer_name"]= nscompany_name;
  5641. item[@"customer_contact"] = customer_contact;
  5642. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  5643. item[@"purchase_time"]= nscreate_time;
  5644. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",status];
  5645. item[@"order_status"]= nsstatus;
  5646. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  5647. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  5648. // item[@"model_count"]
  5649. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  5650. count++;
  5651. }
  5652. ret[@"count"]= [NSNumber numberWithInt:count];
  5653. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  5654. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  5655. ret[@"result"]= [NSNumber numberWithInt:2];
  5656. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  5657. ret[@"time_zone"] = @"PST";
  5658. sqlite3_finalize(statement);
  5659. }
  5660. 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];
  5661. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  5662. [iSalesDB execSql:@"END TRANSACTION" db:db];
  5663. [iSalesDB close_db:db];
  5664. return [RAUtils dict2data:ret];
  5665. }
  5666. #pragma mark update gnotes
  5667. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  5668. {
  5669. DebugLog(@"params: %@",params);
  5670. // comments = Meyoyoyoyoyoyoy;
  5671. // orderCode = MOB1608110001;
  5672. // password = 123456;
  5673. // user = EvanK;
  5674. 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]];
  5675. int ret = [iSalesDB execSql:sql];
  5676. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5677. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5678. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5679. // [dic setValue:@"160409" forKey:@"min_ver"];
  5680. return [RAUtils dict2data:dic];
  5681. }
  5682. #pragma mark move to wishlist
  5683. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  5684. {
  5685. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  5686. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5687. _id = [NSString stringWithFormat:@"(%@)",_id];
  5688. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  5689. sqlite3 *db = [iSalesDB get_db];
  5690. __block NSString *product_id = @"";
  5691. __block NSString *item_count_str = @"";
  5692. // __block NSString *item_id = nil;
  5693. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5694. // product_id = [self textAtColumn:0 statement:stmt];
  5695. int item_count = sqlite3_column_int(stmt, 1);
  5696. // item_id = [self textAtColumn:2 statement:stmt];
  5697. NSString *p_id = [self textAtColumn:0 statement:stmt];
  5698. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  5699. if (p_id.length) {
  5700. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  5701. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  5702. }
  5703. }];
  5704. [iSalesDB close_db:db];
  5705. // 去除第一个,
  5706. product_id = [product_id substringFromIndex:1];
  5707. item_count_str = [item_count_str substringFromIndex:1];
  5708. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5709. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  5710. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  5711. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  5712. sqlite3 *db1 = [iSalesDB get_db];
  5713. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  5714. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5715. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  5716. // 删除
  5717. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  5718. int ret = [iSalesDB execSql:deleteSql db:db1];
  5719. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5720. [iSalesDB close_db:db1];
  5721. return [RAUtils dict2data:dic];
  5722. }
  5723. #pragma mark cart delete
  5724. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  5725. {
  5726. // cartItemId = 548;
  5727. // orderCode = MOB1608110001;
  5728. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  5729. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5730. _id = [NSString stringWithFormat:@"(%@)",_id];
  5731. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  5732. int ret = [iSalesDB execSql:sql];
  5733. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5734. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5735. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5736. // [dic setValue:@"160409" forKey:@"min_ver"];
  5737. return [RAUtils dict2data:dic];
  5738. }
  5739. #pragma mark set price
  5740. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  5741. {
  5742. DebugLog(@"cart set price params: %@",params);
  5743. // "cartitem_id" = 1;
  5744. // discount = "0.000000";
  5745. // "item_note" = "";
  5746. // price = "269.000000";
  5747. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  5748. NSString *notes = [self valueInParams:params key:@"item_note"];
  5749. NSString *discount = [self valueInParams:params key:@"discount"];
  5750. NSString *price = [self valueInParams:params key:@"price"];
  5751. // bool badd_price_changed=false;
  5752. // sqlite3* db=[iSalesDB get_db];
  5753. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5754. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  5755. // NSRange range;
  5756. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  5757. //
  5758. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  5759. // badd_price_changed=true;
  5760. // [iSalesDB close_db:db];
  5761. //
  5762. // if(badd_price_changed)
  5763. // {
  5764. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  5765. // }
  5766. //
  5767. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  5768. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  5769. int ret = [iSalesDB execSql:sql];
  5770. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5771. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5772. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5773. // [dic setValue:@"160409" forKey:@"min_ver"];
  5774. return [RAUtils dict2data:dic];
  5775. }
  5776. #pragma mark set line notes
  5777. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  5778. {
  5779. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  5780. NSString *notes = [self valueInParams:params key:@"notes"];
  5781. notes = [self translateSingleQuote:notes];
  5782. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  5783. int ret = [iSalesDB execSql:sql];
  5784. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5785. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5786. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5787. // [dic setValue:@"160409" forKey:@"min_ver"];
  5788. return [RAUtils dict2data:dic];
  5789. }
  5790. #pragma mark set qty
  5791. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  5792. {
  5793. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  5794. int item_count = [params[@"inputInt"] integerValue];
  5795. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  5796. int ret = [iSalesDB execSql:sql];
  5797. __block int item_id = 0;
  5798. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5799. item_id = sqlite3_column_int(stmt, 0);
  5800. }];
  5801. sqlite3 *db = [iSalesDB get_db];
  5802. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  5803. [iSalesDB close_db:db];
  5804. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5805. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5806. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5807. // [dic setValue:@"160409" forKey:@"min_ver"];
  5808. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  5809. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  5810. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  5811. return [RAUtils dict2data:dic];
  5812. }
  5813. #pragma mark place order
  5814. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  5815. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5816. if (aname) {
  5817. [dic setValue:aname forKey:@"aname"];
  5818. }
  5819. if (control) {
  5820. [dic setValue:control forKey:@"control"];
  5821. }
  5822. if (keyboard) {
  5823. [dic setValue:keyboard forKey:@"keyboard"];
  5824. }
  5825. if (name) {
  5826. [dic setValue:name forKey:@"name"];
  5827. }
  5828. if (value) {
  5829. [dic setValue:value forKey:@"value"];
  5830. }
  5831. return dic;
  5832. }
  5833. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  5834. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5835. orderCode = [self translateSingleQuote:orderCode];
  5836. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  5837. 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];
  5838. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5839. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  5840. NSString *img0 = [self textAtColumn:1 statement:stmt];
  5841. NSString *img1 = [self textAtColumn:2 statement:stmt];
  5842. NSString *img2 = [self textAtColumn:3 statement:stmt];
  5843. NSString *company_name = [self textAtColumn:4 statement:stmt];
  5844. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  5845. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  5846. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  5847. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  5848. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  5849. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  5850. NSString *email = [self textAtColumn:11 statement:stmt];
  5851. NSString *phone = [self textAtColumn:12 statement:stmt];
  5852. NSString *fax = [self textAtColumn:13 statement:stmt];
  5853. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  5854. NSString *city = [self textAtColumn:15 statement:stmt];
  5855. NSString *state = [self textAtColumn:16 statement:stmt];
  5856. NSString *country = [self textAtColumn:17 statement:stmt];
  5857. NSString *name = [self textAtColumn:18 statement:stmt];
  5858. // contact id
  5859. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  5860. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  5861. [contact_id_dic setValue:@"text" forKey:@"control"];
  5862. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  5863. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  5864. [contact_id_dic setValue:@"true" forKey:@"required"];
  5865. [contact_id_dic setValue:contact_id forKey:@"value"];
  5866. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  5867. // business card
  5868. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  5869. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  5870. [business_card_dic setValue:@"img" forKey:@"control"];
  5871. [business_card_dic setValue:@"1" forKey:@"disable"];
  5872. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  5873. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  5874. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  5875. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  5876. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  5877. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  5878. [business_card_dic setValue:@"business_card" forKey:@"name"];
  5879. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  5880. // fax
  5881. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5882. [customer_dic setValue:fax_dic forKey:@"item_10"];
  5883. // zipcode
  5884. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  5885. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  5886. // city
  5887. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  5888. [customer_dic setValue:city_dic forKey:@"item_12"];
  5889. // state
  5890. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  5891. [customer_dic setValue:state_dic forKey:@"item_13"];
  5892. // country
  5893. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  5894. [customer_dic setValue:country_dic forKey:@"item_14"];
  5895. // company name
  5896. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  5897. [customer_dic setValue:company_dic forKey:@"item_2"];
  5898. // addr_1
  5899. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  5900. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  5901. // addr_2
  5902. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  5903. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  5904. // addr_3
  5905. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  5906. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  5907. // addr_4
  5908. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  5909. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  5910. // Contact
  5911. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  5912. [customer_dic setValue:contact_dic forKey:@"item_7"];
  5913. // email
  5914. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5915. [customer_dic setValue:email_dic forKey:@"item_8"];
  5916. // phone
  5917. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5918. [customer_dic setValue:phone_dic forKey:@"item_9"];
  5919. // title
  5920. [customer_dic setValue:@"Customer" forKey:@"title"];
  5921. // count
  5922. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  5923. }];
  5924. // setting
  5925. NSDictionary *setting = params[@"setting"];
  5926. NSNumber *hide = setting[@"CustomerHide"];
  5927. [customer_dic setValue:hide forKey:@"hide"];
  5928. return customer_dic;
  5929. }
  5930. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  5931. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5932. // setting
  5933. NSDictionary *setting = params[@"setting"];
  5934. NSNumber *hide = setting[@"ShipToHide"];
  5935. [dic setValue:hide forKey:@"hide"];
  5936. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5937. orderCode = [self translateSingleQuote:orderCode];
  5938. 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];
  5939. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5940. NSString *cid = [self textAtColumn:0 statement:stmt];
  5941. NSString *name = [self textAtColumn:1 statement:stmt];
  5942. NSString *ext = [self textAtColumn:2 statement:stmt];
  5943. NSString *contact = [self textAtColumn:3 statement:stmt];
  5944. NSString *email = [self textAtColumn:4 statement:stmt];
  5945. NSString *fax = [self textAtColumn:5 statement:stmt];
  5946. NSString *phone = [self textAtColumn:6 statement:stmt];
  5947. // count
  5948. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5949. // title
  5950. [dic setValue:@"Ship To" forKey:@"title"];
  5951. // choose
  5952. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5953. [choose_dic setValue:@"choose" forKey:@"aname"];
  5954. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5955. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  5956. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  5957. @"key_map" : @{@"receive_cid" : @"customer_cid",
  5958. @"receive_contact" : @"customer_contact",
  5959. @"receive_email" : @"customer_email",
  5960. @"receive_ext" : @"customer_contact_ext",
  5961. @"receive_fax" : @"customer_fax",
  5962. @"receive_name" : @"customer_name",
  5963. @"receive_phone" : @"customer_phone"},
  5964. @"refresh" : [NSNumber numberWithInteger:1],
  5965. @"type" : @"pull"};
  5966. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  5967. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  5968. @"name" : @"Add new address",
  5969. @"refresh" : [NSNumber numberWithInteger:1],
  5970. @"value" : @"new_addr"
  5971. };
  5972. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  5973. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  5974. @"key_map" : @{@"receive_cid" : @"customer_cid",
  5975. @"receive_contact" : @"customer_contact",
  5976. @"receive_email" : @"customer_email",
  5977. @"receive_ext" : @"customer_contact_ext",
  5978. @"receive_fax" : @"customer_fax",
  5979. @"receive_name" : @"customer_name",
  5980. @"receive_phone" : @"customer_phone"},
  5981. @"name" : @"select_ship_to",
  5982. @"refresh" : [NSNumber numberWithInteger:1],
  5983. @"value" : @"Sales_Order_Ship_To"};
  5984. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  5985. [dic setValue:choose_dic forKey:@"item_0"];
  5986. // contact id
  5987. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5988. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5989. [contact_id_dic setValue:@"true" forKey:@"required"];
  5990. [dic setValue:contact_id_dic forKey:@"item_1"];
  5991. // company name
  5992. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5993. [dic setValue:company_name_dic forKey:@"item_2"];
  5994. // address
  5995. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  5996. NSNumber *required = setting[@"ShippingToAddressRequire"];
  5997. if ([required integerValue]) {
  5998. [ext_dic setValue:@"true" forKey:@"required"];
  5999. }
  6000. [dic setValue:ext_dic forKey:@"item_3"];
  6001. // contact
  6002. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6003. [dic setValue:contact_dic forKey:@"item_4"];
  6004. // phone
  6005. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6006. [dic setValue:phone_dic forKey:@"item_5"];
  6007. // fax
  6008. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6009. [dic setValue:fax_dic forKey:@"item_6"];
  6010. // email
  6011. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6012. [dic setValue:email_dic forKey:@"item_7"];
  6013. }];
  6014. return dic;
  6015. }
  6016. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  6017. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6018. // setting
  6019. NSDictionary *setting = params[@"setting"];
  6020. NSNumber *hide = setting[@"ShipFromHide"];
  6021. [dic setValue:hide forKey:@"hide"];
  6022. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6023. orderCode = [self translateSingleQuote:orderCode];
  6024. 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];
  6025. 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';";
  6026. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6027. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  6028. __block NSString *name = [self textAtColumn:1 statement:stmt];
  6029. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  6030. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  6031. __block NSString *email = [self textAtColumn:4 statement:stmt];
  6032. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  6033. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  6034. if (!cid.length) {
  6035. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  6036. cid = [self textAtColumn:0 statement:statment];
  6037. name = [self textAtColumn:1 statement:statment];
  6038. ext = [self textAtColumn:2 statement:statment];
  6039. contact = [self textAtColumn:3 statement:statment];
  6040. email = [self textAtColumn:4 statement:statment];
  6041. fax = [self textAtColumn:5 statement:statment];
  6042. phone = [self textAtColumn:6 statement:statment];
  6043. }];
  6044. }
  6045. // count
  6046. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6047. // title
  6048. [dic setValue:@"Ship From" forKey:@"title"];
  6049. // hide
  6050. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6051. // choose
  6052. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6053. [choose_dic setValue:@"choose" forKey:@"aname"];
  6054. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6055. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6056. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  6057. @"key_map" : @{@"sender_cid" : @"customer_cid",
  6058. @"sender_contact" : @"customer_contact",
  6059. @"sender_email" : @"customer_email",
  6060. @"sender_ext" : @"customer_contact_ext",
  6061. @"sender_fax" : @"customer_fax",
  6062. @"sender_name" : @"customer_name",
  6063. @"sender_phone" : @"customer_phone"},
  6064. @"name" : @"select_cid",
  6065. @"refresh" : [NSNumber numberWithInteger:0],
  6066. @"value" : @"Sales_Order_Ship_From"};
  6067. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  6068. [dic setValue:choose_dic forKey:@"item_0"];
  6069. // contact id
  6070. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6071. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6072. [contact_id_dic setValue:@"true" forKey:@"required"];
  6073. [dic setValue:contact_id_dic forKey:@"item_1"];
  6074. // company name
  6075. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6076. [dic setValue:company_name_dic forKey:@"item_2"];
  6077. // address
  6078. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  6079. [dic setValue:ext_dic forKey:@"item_3"];
  6080. // contact
  6081. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6082. [dic setValue:contact_dic forKey:@"item_4"];
  6083. // phone
  6084. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6085. [dic setValue:phone_dic forKey:@"item_5"];
  6086. // fax
  6087. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6088. [dic setValue:fax_dic forKey:@"item_6"];
  6089. // email
  6090. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6091. [dic setValue:email_dic forKey:@"item_7"];
  6092. }];
  6093. return dic;
  6094. }
  6095. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  6096. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6097. // setting
  6098. NSDictionary *setting = params[@"setting"];
  6099. NSNumber *hide = setting[@"FreightBillToHide"];
  6100. [dic setValue:hide forKey:@"hide"];
  6101. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6102. orderCode = [self translateSingleQuote:orderCode];
  6103. 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];
  6104. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6105. NSString *cid = [self textAtColumn:0 statement:stmt];
  6106. NSString *name = [self textAtColumn:1 statement:stmt];
  6107. NSString *ext = [self textAtColumn:2 statement:stmt];
  6108. NSString *contact = [self textAtColumn:3 statement:stmt];
  6109. NSString *email = [self textAtColumn:4 statement:stmt];
  6110. NSString *fax = [self textAtColumn:5 statement:stmt];
  6111. NSString *phone = [self textAtColumn:6 statement:stmt];
  6112. // count
  6113. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6114. // title
  6115. [dic setValue:@"Freight Bill To" forKey:@"title"];
  6116. // hide
  6117. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6118. // choose
  6119. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6120. [choose_dic setValue:@"choose" forKey:@"aname"];
  6121. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6122. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  6123. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  6124. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  6125. @"shipping_billto_contact" : @"receive_contact",
  6126. @"shipping_billto_email" : @"receive_email",
  6127. @"shipping_billto_ext" : @"receive_ext",
  6128. @"shipping_billto_fax" : @"receive_fax",
  6129. @"shipping_billto_name" : @"receive_name",
  6130. @"shipping_billto_phone" : @"receive_phone"},
  6131. @"type" : @"pull"};
  6132. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  6133. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  6134. @"type" : @"pull",
  6135. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  6136. @"shipping_billto_contact" : @"customer_contact",
  6137. @"shipping_billto_email" : @"customer_email",
  6138. @"shipping_billto_ext" : @"customer_contact_ext",
  6139. @"shipping_billto_fax" : @"customer_fax",
  6140. @"shipping_billto_name" : @"customer_name",
  6141. @"shipping_billto_phone" : @"customer_phone"}
  6142. };
  6143. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  6144. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  6145. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  6146. @"shipping_billto_contact" : @"sender_contact",
  6147. @"shipping_billto_email" : @"sender_email",
  6148. @"shipping_billto_ext" : @"sender_ext",
  6149. @"shipping_billto_fax" : @"sender_fax",
  6150. @"shipping_billto_name" : @"sender_name",
  6151. @"shipping_billto_phone" : @"sender_phone"},
  6152. @"type" : @"pull"
  6153. };
  6154. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  6155. NSDictionary *select_freight_bill_to_dic = @{
  6156. @"aname" : @"Select freight bill to",
  6157. @"name" : @"select_cid",
  6158. @"refresh" : [NSNumber numberWithInteger:0],
  6159. @"value" : @"Sales_Order_Freight_Bill_To",
  6160. @"key_map" : @{
  6161. @"shipping_billto_cid" : @"customer_cid",
  6162. @"shipping_billto_contact" : @"customer_contact",
  6163. @"shipping_billto_email" : @"customer_email",
  6164. @"shipping_billto_ext" : @"customer_contact_ext",
  6165. @"shipping_billto_fax" : @"customer_fax",
  6166. @"shipping_billto_name" : @"customer_name",
  6167. @"shipping_billto_phone" : @"customer_phone"
  6168. }
  6169. };
  6170. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  6171. [dic setValue:choose_dic forKey:@"item_0"];
  6172. // contact id
  6173. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6174. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6175. [contact_id_dic setValue:@"true" forKey:@"required"];
  6176. [dic setValue:contact_id_dic forKey:@"item_1"];
  6177. // company name
  6178. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6179. [dic setValue:company_name_dic forKey:@"item_2"];
  6180. // address
  6181. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  6182. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  6183. if ([ext_required integerValue]) {
  6184. [ext_dic setValue:@"true" forKey:@"required"];
  6185. }
  6186. [dic setValue:ext_dic forKey:@"item_3"];
  6187. // contact
  6188. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6189. [dic setValue:contact_dic forKey:@"item_4"];
  6190. // phone
  6191. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6192. [dic setValue:phone_dic forKey:@"item_5"];
  6193. // fax
  6194. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6195. [dic setValue:fax_dic forKey:@"item_6"];
  6196. // email
  6197. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6198. [dic setValue:email_dic forKey:@"item_7"];
  6199. }];
  6200. return dic;
  6201. }
  6202. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  6203. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6204. // setting
  6205. NSDictionary *setting = params[@"setting"];
  6206. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  6207. [dic setValue:hide forKey:@"hide"];
  6208. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6209. orderCode = [self translateSingleQuote:orderCode];
  6210. 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];
  6211. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6212. NSString *cid = [self textAtColumn:0 statement:stmt];
  6213. NSString *name = [self textAtColumn:1 statement:stmt];
  6214. NSString *ext = [self textAtColumn:2 statement:stmt];
  6215. NSString *contact = [self textAtColumn:3 statement:stmt];
  6216. NSString *email = [self textAtColumn:4 statement:stmt];
  6217. NSString *fax = [self textAtColumn:5 statement:stmt];
  6218. NSString *phone = [self textAtColumn:6 statement:stmt];
  6219. // count
  6220. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6221. // title
  6222. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  6223. // hide
  6224. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6225. // choose
  6226. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6227. [choose_dic setValue:@"choose" forKey:@"aname"];
  6228. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6229. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  6230. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  6231. @"key_map" : @{@"billing_cid" : @"receive_cid",
  6232. @"billing_contact" : @"receive_contact",
  6233. @"billing_email" : @"receive_email",
  6234. @"billing_ext" : @"receive_ext",
  6235. @"billing_fax" : @"receive_fax",
  6236. @"billing_name" : @"receive_name",
  6237. @"billing_phone" : @"receive_phone"},
  6238. @"type" : @"pull"};
  6239. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  6240. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  6241. @"type" : @"pull",
  6242. @"key_map" : @{@"billing_cid" : @"customer_cid",
  6243. @"billing_contact" : @"customer_contact",
  6244. @"billing_email" : @"customer_email",
  6245. @"billing_ext" : @"customer_contact_ext",
  6246. @"billing_fax" : @"customer_fax",
  6247. @"billing_name" : @"customer_name",
  6248. @"billing_phone" : @"customer_phone"}
  6249. };
  6250. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  6251. NSDictionary *select_bill_to_dic = @{
  6252. @"aname" : @"Select bill to",
  6253. @"name" : @"select_cid",
  6254. @"refresh" : [NSNumber numberWithInteger:0],
  6255. @"value" : @"Sales_Order_Merchandise_Bill_To",
  6256. @"key_map" : @{
  6257. @"billing_cid" : @"customer_cid",
  6258. @"billing_contact" : @"customer_contact",
  6259. @"billing_email" : @"customer_email",
  6260. @"billing_ext" : @"customer_contact_ext",
  6261. @"billing_fax" : @"customer_fax",
  6262. @"billing_name" : @"customer_name",
  6263. @"billing_phone" : @"customer_phone"
  6264. }
  6265. };
  6266. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  6267. [dic setValue:choose_dic forKey:@"item_0"];
  6268. // contact id
  6269. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6270. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6271. [contact_id_dic setValue:@"true" forKey:@"required"];
  6272. [dic setValue:contact_id_dic forKey:@"item_1"];
  6273. // company name
  6274. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6275. [dic setValue:company_name_dic forKey:@"item_2"];
  6276. // address
  6277. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  6278. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  6279. if ([ext_required integerValue]) {
  6280. [ext_dic setValue:@"true" forKey:@"required"];
  6281. }
  6282. [dic setValue:ext_dic forKey:@"item_3"];
  6283. // contact
  6284. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6285. [dic setValue:contact_dic forKey:@"item_4"];
  6286. // phone
  6287. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6288. [dic setValue:phone_dic forKey:@"item_5"];
  6289. // fax
  6290. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6291. [dic setValue:fax_dic forKey:@"item_6"];
  6292. // email
  6293. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6294. [dic setValue:email_dic forKey:@"item_7"];
  6295. }];
  6296. return dic;
  6297. }
  6298. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  6299. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6300. // setting
  6301. NSDictionary *setting = params[@"setting"];
  6302. NSNumber *hide = setting[@"ReturnToHide"];
  6303. [dic setValue:hide forKey:@"hide"];
  6304. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6305. orderCode = [self translateSingleQuote:orderCode];
  6306. 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];
  6307. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6308. NSString *cid = [self textAtColumn:0 statement:stmt];
  6309. NSString *name = [self textAtColumn:1 statement:stmt];
  6310. NSString *ext = [self textAtColumn:2 statement:stmt];
  6311. NSString *contact = [self textAtColumn:3 statement:stmt];
  6312. NSString *email = [self textAtColumn:4 statement:stmt];
  6313. NSString *fax = [self textAtColumn:5 statement:stmt];
  6314. NSString *phone = [self textAtColumn:6 statement:stmt];
  6315. // count
  6316. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6317. // title
  6318. [dic setValue:@"Return To" forKey:@"title"];
  6319. // hide
  6320. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6321. // choose
  6322. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6323. [choose_dic setValue:@"choose" forKey:@"aname"];
  6324. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6325. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  6326. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  6327. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  6328. @"returnto_contact" : @"sender_contact",
  6329. @"returnto_email" : @"sender_email",
  6330. @"returnto_ext" : @"sender_ext",
  6331. @"returnto_fax" : @"sender_fax",
  6332. @"returnto_name" : @"sender_name",
  6333. @"returnto_phone" : @"sender_phone"},
  6334. @"type" : @"pull"};
  6335. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  6336. NSDictionary *select_return_to_dic = @{
  6337. @"aname" : @"Select return to",
  6338. @"name" : @"select_cid",
  6339. @"refresh" : [NSNumber numberWithInteger:0],
  6340. @"value" : @"Contact_Return_To",
  6341. @"key_map" : @{
  6342. @"returnto_cid" : @"customer_cid",
  6343. @"returnto_contact" : @"customer_contact",
  6344. @"returnto_email" : @"customer_email",
  6345. @"returnto_ext" : @"customer_contact_ext",
  6346. @"returnto_fax" : @"customer_fax",
  6347. @"returnto_name" : @"customer_name",
  6348. @"returnto_phone" : @"customer_phone"
  6349. }
  6350. };
  6351. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  6352. [dic setValue:choose_dic forKey:@"item_0"];
  6353. // contact id
  6354. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6355. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6356. [contact_id_dic setValue:@"true" forKey:@"required"];
  6357. [dic setValue:contact_id_dic forKey:@"item_1"];
  6358. // company name
  6359. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6360. [dic setValue:company_name_dic forKey:@"item_2"];
  6361. // address
  6362. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  6363. [dic setValue:ext_dic forKey:@"item_3"];
  6364. // contact
  6365. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6366. [dic setValue:contact_dic forKey:@"item_4"];
  6367. // phone
  6368. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6369. [dic setValue:phone_dic forKey:@"item_5"];
  6370. // fax
  6371. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6372. [dic setValue:fax_dic forKey:@"item_6"];
  6373. // email
  6374. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6375. [dic setValue:email_dic forKey:@"item_7"];
  6376. }];
  6377. return dic;
  6378. }
  6379. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  6380. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6381. orderCode = [self translateSingleQuote:orderCode];
  6382. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6383. __block double TotalCuft = 0;
  6384. __block double TotalWeight = 0;
  6385. __block int TotalCarton = 0;
  6386. __block double payments = 0;
  6387. // setting
  6388. NSDictionary *setting = params[@"setting"];
  6389. NSNumber *hide = setting[@"ModelInformationHide"];
  6390. [dic setValue:hide forKey:@"hide"];
  6391. 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];
  6392. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6393. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6394. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  6395. // item id
  6396. int item_id = sqlite3_column_int(stmt, 0);
  6397. // count
  6398. int item_count = sqlite3_column_int(stmt, 1);
  6399. // stockUom
  6400. int stockUom = sqlite3_column_int(stmt, 2);
  6401. // unit price
  6402. NSString *str_price = [self textAtColumn:3 statement:stmt];
  6403. NSString* Price=nil;
  6404. if([str_price isEqualToString:@""])
  6405. {
  6406. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  6407. if(price==nil)
  6408. Price=@"No Price.";
  6409. else
  6410. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6411. }
  6412. else
  6413. {
  6414. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  6415. }
  6416. // discount
  6417. double discount = sqlite3_column_double(stmt, 4);
  6418. // name
  6419. NSString *name = [self textAtColumn:5 statement:stmt];
  6420. // description
  6421. NSString *description = [self textAtColumn:6 statement:stmt];
  6422. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  6423. // line note
  6424. NSString *line_note = [self textAtColumn:7 statement:stmt];
  6425. int avaulability = sqlite3_column_int(stmt, 8);
  6426. // img
  6427. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  6428. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false];
  6429. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6430. if(combine != nil)
  6431. {
  6432. // int citem=0;
  6433. int bcount=[[combine valueForKey:@"count"] intValue];
  6434. for(int bc=0;bc<bcount;bc++)
  6435. {
  6436. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6437. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6438. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6439. subTotal += uprice * modulus * item_count;
  6440. }
  6441. }
  6442. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  6443. [model_dic setValue:@"model" forKey:@"control"];
  6444. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  6445. [model_dic setValue:description forKey:@"description"];
  6446. [model_dic setValue:line_note forKey:@"note"];
  6447. [model_dic setValue:img forKey:@"img_url"];
  6448. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  6449. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  6450. [model_dic setValue:Price forKey:@"unit_price"];
  6451. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  6452. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  6453. if (combine) {
  6454. [model_dic setValue:combine forKey:@"combine"];
  6455. }
  6456. // well,what under the row is the info for total
  6457. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  6458. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  6459. double weight=[bsubtotaljson[@"weight"] doubleValue];
  6460. int carton=[bsubtotaljson[@"carton"] intValue];
  6461. TotalCuft += cuft;
  6462. TotalWeight += weight;
  6463. TotalCarton += carton;
  6464. payments += subTotal;
  6465. //---------------------------
  6466. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  6467. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  6468. }];
  6469. [dic setValue:@"Model Information" forKey:@"title"];
  6470. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  6471. return dic;
  6472. }
  6473. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  6474. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6475. [dic setValue:@"Remarks Content" forKey:@"title"];
  6476. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  6477. // setting
  6478. NSDictionary *setting = params[@"setting"];
  6479. NSNumber *hide = setting[@"RemarksContentHide"];
  6480. [dic setValue:hide forKey:@"hide"];
  6481. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6482. orderCode = [self translateSingleQuote:orderCode];
  6483. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes from offline_order where so_id = '%@';",orderCode];
  6484. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6485. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  6486. int mustCall = sqlite3_column_int(stmt, 1);
  6487. NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  6488. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  6489. NSDictionary *po_dic = @{
  6490. @"aname" : @"PO#",
  6491. @"control" : @"edit",
  6492. @"keyboard" : @"text",
  6493. @"name" : @"poNumber",
  6494. @"value" : poNumber
  6495. };
  6496. NSDictionary *must_call_dic = @{
  6497. @"aname" : @"MUST CALL BEFORE DELIVERY",
  6498. @"control" : @"switch",
  6499. @"name" : @"must_call",
  6500. @"value" : mustCall ? @"true" : @"false"
  6501. };
  6502. NSDictionary *general_notes_dic = @{
  6503. @"aname" : @"General notes",
  6504. @"control" : @"text_view",
  6505. @"keyboard" : @"text",
  6506. @"name" : @"comments",
  6507. @"value" : generalNotes
  6508. };
  6509. NSDictionary *internal_notes_dic = @{
  6510. @"aname" : @"Internal notes",
  6511. @"control" : @"text_view",
  6512. @"keyboard" : @"text",
  6513. @"name" : @"internal_notes",
  6514. @"value" : internalNotes
  6515. };
  6516. [dic setValue:po_dic forKey:@"item_0"];
  6517. [dic setValue:must_call_dic forKey:@"item_1"];
  6518. [dic setValue:general_notes_dic forKey:@"item_2"];
  6519. [dic setValue:internal_notes_dic forKey:@"item_3"];
  6520. }];
  6521. return dic;
  6522. }
  6523. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  6524. // params
  6525. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6526. orderCode = [self translateSingleQuote:orderCode];
  6527. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6528. // setting
  6529. NSDictionary *setting = params[@"setting"];
  6530. NSNumber *hide = setting[@"OrderTotalHide"];
  6531. [dic setValue:hide forKey:@"hide"];
  6532. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  6533. __block double lift_gate_value = 0;
  6534. __block double handling_fee = 0;
  6535. __block double shipping = 0;
  6536. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6537. int lift_gate = sqlite3_column_int(stmt, 0);
  6538. lift_gate_value = sqlite3_column_double(stmt, 1);
  6539. shipping = sqlite3_column_double(stmt, 2);
  6540. handling_fee = sqlite3_column_double(stmt, 3);
  6541. if (!lift_gate) {
  6542. lift_gate_value = 0;
  6543. }
  6544. }];
  6545. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  6546. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  6547. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  6548. double payments = [[total valueForKey:@"payments"] doubleValue];
  6549. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  6550. double totalPrice = payments;
  6551. [dic setValue:@"Order Total" forKey:@"title"];
  6552. NSDictionary *payments_dic = @{
  6553. @"align" : @"right",
  6554. @"aname" : @"Payments/Credits",
  6555. @"control" : @"text",
  6556. @"name" : @"paymentsAndCredits",
  6557. @"type" : @"price",
  6558. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  6559. };
  6560. [dic setValue:payments_dic forKey:@"item_0"];
  6561. NSDictionary *handling_fee_dic = @{
  6562. @"align" : @"right",
  6563. @"aname" : @"Handling Fee",
  6564. @"control" : @"text",
  6565. @"name" : @"handling_fee_value",
  6566. @"required" : @"true",
  6567. @"type" : @"price",
  6568. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  6569. };
  6570. NSDictionary *shipping_dic = @{
  6571. @"align" : @"right",
  6572. @"aname" : @"Shipping*",
  6573. @"control" : @"text",
  6574. @"name" : @"shipping",
  6575. @"required" : @"true",
  6576. @"type" : @"price",
  6577. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  6578. };
  6579. NSDictionary *lift_gate_dic = @{
  6580. @"align" : @"right",
  6581. @"aname" : @"Liftgate Fee(No Loading Dock)",
  6582. @"control" : @"text",
  6583. @"name" : @"lift_gate_value",
  6584. @"required" : @"true",
  6585. @"type" : @"price",
  6586. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  6587. };
  6588. int item_count = 1;
  6589. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  6590. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  6591. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6592. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6593. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  6594. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6595. } else {
  6596. }
  6597. [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6598. NSDictionary *total_price_dic = @{
  6599. @"align" : @"right",
  6600. @"aname" : @"Total",
  6601. @"control" : @"text",
  6602. @"name" : @"totalPrice",
  6603. @"type" : @"price",
  6604. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  6605. };
  6606. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6607. NSDictionary *total_cuft_dic = @{
  6608. @"align" : @"right",
  6609. @"aname" : @"Total Cuft",
  6610. @"control" : @"text",
  6611. @"name" : @"",
  6612. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  6613. };
  6614. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6615. NSDictionary *total_weight_dic = @{
  6616. @"align" : @"right",
  6617. @"aname" : @"Total Weight",
  6618. @"control" : @"text",
  6619. @"name" : @"",
  6620. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  6621. };
  6622. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6623. NSDictionary *total_carton_dic = @{
  6624. @"align" : @"right",
  6625. @"aname" : @"Total Carton",
  6626. @"control" : @"text",
  6627. @"name" : @"",
  6628. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  6629. };
  6630. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6631. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  6632. return dic;
  6633. }
  6634. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  6635. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6636. orderCode = [self translateSingleQuote:orderCode];
  6637. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6638. // setting
  6639. NSDictionary *setting = params[@"setting"];
  6640. NSNumber *hide = setting[@"SignatureHide"];
  6641. [dic setValue:hide forKey:@"hide"];
  6642. [dic setValue:@"Signature" forKey:@"title"];
  6643. __block NSString *pic_path = @"";
  6644. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  6645. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6646. pic_path = [self textAtColumn:0 statement:stmt];
  6647. }];
  6648. NSDictionary *pic_dic = @{
  6649. @"aname" : @"Signature",
  6650. @"avalue" :pic_path,
  6651. @"control" : @"signature",
  6652. @"name" : @"sign_picpath",
  6653. @"value" : pic_path
  6654. };
  6655. [dic setValue:pic_dic forKey:@"item_0"];
  6656. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6657. return dic;
  6658. }
  6659. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  6660. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6661. // setting
  6662. NSDictionary *setting = params[@"setting"];
  6663. NSNumber *hide = setting[@"ShippingMethodHide"];
  6664. [dic setValue:hide forKey:@"hide"];
  6665. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6666. orderCode = [self translateSingleQuote:orderCode];
  6667. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  6668. __block NSString *logist = @"";
  6669. __block NSString *lift_gate = @"";
  6670. __block int lift_gate_integer = 0;
  6671. __block NSString *logistic_note = @"";
  6672. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6673. logist = [self textAtColumn:0 statement:stmt];
  6674. lift_gate_integer = sqlite3_column_int(stmt, 1);
  6675. logistic_note = [self textAtColumn:2 statement:stmt];
  6676. }];
  6677. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6678. [dic setValue:@"Shipping Method" forKey:@"title"];
  6679. // val_0
  6680. int PERSONAL_PICK_UP_check = 0;
  6681. int USE_MY_CARRIER_check = 0;
  6682. NSString *logistic_note_text = @"";
  6683. int will_call_check = 0;
  6684. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  6685. will_call_check = 1;
  6686. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  6687. PERSONAL_PICK_UP_check = 1;
  6688. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  6689. USE_MY_CARRIER_check = 1;
  6690. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  6691. logistic_note = [logistic_note_array firstObject];
  6692. if (logistic_note_array.count > 1) {
  6693. logistic_note_text = [logistic_note_array lastObject];
  6694. }
  6695. }
  6696. }
  6697. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  6698. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  6699. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  6700. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  6701. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  6702. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6703. NSDictionary *val0_subItem_item0 = @{
  6704. @"aname" : @"Option",
  6705. @"cadedate" : @{
  6706. @"count" : [NSNumber numberWithInteger:2],
  6707. @"val_0" : @{
  6708. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  6709. @"refresh" : [NSNumber numberWithInteger:0],
  6710. @"value" : @"PERSONAL PICK UP",
  6711. @"value_id" : @"PERSONAL PICK UP"
  6712. },
  6713. @"val_1" : @{
  6714. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  6715. @"refresh" : [NSNumber numberWithInteger:0],
  6716. @"sub_item" : @{
  6717. @"count" : [NSNumber numberWithInteger:1],
  6718. @"item_0" : @{
  6719. @"aname" : @"BOL",
  6720. @"control" : @"edit",
  6721. @"keyboard" : @"text",
  6722. @"name" : @"logist_note_text",
  6723. @"refresh" : [NSNumber numberWithInteger:0],
  6724. @"required" : @"false",
  6725. @"value" : logistic_note_text
  6726. }
  6727. },
  6728. @"value" : @"USE MY CARRIER",
  6729. @"value_id" : @"USE MY CARRIER"
  6730. }
  6731. },
  6732. @"control" : @"enum",
  6733. @"name" : @"logistic_note",
  6734. @"required" : @"true",
  6735. @"single_select" : @"true"
  6736. };
  6737. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  6738. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  6739. // val_1
  6740. lift_gate = [self valueInParams:params key:@"lift_gate"];
  6741. if([lift_gate isEqualToString:@""]) {
  6742. if (lift_gate_integer == 1) {
  6743. lift_gate = @"true";
  6744. } else {
  6745. lift_gate = @"false";
  6746. }
  6747. }
  6748. int common_carrier_check = 0;
  6749. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  6750. common_carrier_check = 1;
  6751. [params setValue:lift_gate forKey:@"lift_gate"];
  6752. }
  6753. NSDictionary *val_1 = @{
  6754. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  6755. @"sub_item" : @{
  6756. @"count" : [NSNumber numberWithInteger:1],
  6757. @"item_0" : @{
  6758. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  6759. @"control" : @"switch",
  6760. @"name" : @"lift_gate",
  6761. @"refresh" : [NSNumber numberWithInteger:1],
  6762. @"required" : @"true",
  6763. @"value" : lift_gate
  6764. }
  6765. },
  6766. @"value" : @"COMMON CARRIER",
  6767. @"value_id" : @"COMMON CARRIER"
  6768. };
  6769. // cadedate
  6770. NSDictionary *cadedate = @{
  6771. @"count" : [NSNumber numberWithInteger:2],
  6772. @"val_0" : val_0,
  6773. @"val_1" : val_1
  6774. };
  6775. // item_0
  6776. NSMutableDictionary *item_0 = @{
  6777. @"aname" : @"Shipping",
  6778. @"cadedate" : cadedate,
  6779. @"control" : @"enum",
  6780. @"name" : @"logist",
  6781. @"refresh" : [NSNumber numberWithInteger:1],
  6782. @"single_select" : @"true",
  6783. }.mutableCopy;
  6784. NSNumber *required = setting[@"ShippingMethodRequire"];
  6785. if ([required integerValue]) {
  6786. [item_0 setValue:@"true" forKey:@"required"];
  6787. }
  6788. [dic setValue:item_0 forKey:@"item_0"];
  6789. return dic;
  6790. }
  6791. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  6792. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6793. orderCode = [self translateSingleQuote:orderCode];
  6794. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  6795. __block int submit_as_integer = 0;
  6796. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6797. submit_as_integer = sqlite3_column_int(stmt, 0);
  6798. }];
  6799. int check11 = 0;
  6800. int check10 = 0;
  6801. if (submit_as_integer == 11) {
  6802. check11 = 1;
  6803. }
  6804. if (submit_as_integer == 10) {
  6805. check10 = 1;
  6806. }
  6807. // section_0
  6808. NSMutableDictionary *dic = @{
  6809. @"count" : @(1),
  6810. @"item_0" : @{
  6811. @"aname" : @"Submit Order As",
  6812. @"cadedate" : @{
  6813. @"count" : @(2),
  6814. @"val_0" : @{
  6815. @"check" : [NSNumber numberWithInteger:check11],
  6816. @"value" : @"Sales Order",
  6817. @"value_id" : @(11)
  6818. },
  6819. @"val_1" : @{
  6820. @"check" : [NSNumber numberWithInteger:check10],
  6821. @"value" : @"Quote",
  6822. @"value_id" : @(10)
  6823. }
  6824. },
  6825. @"control" : @"enum",
  6826. @"name" : @"erpOrderStatus",
  6827. @"required" : @"true",
  6828. @"single_select" : @"true"
  6829. },
  6830. @"title" : @"Order Type"
  6831. }.mutableCopy;
  6832. // setting
  6833. NSDictionary *setting = params[@"setting"];
  6834. NSNumber *hide = setting[@"OrderTypeHide"];
  6835. [dic setValue:hide forKey:@"hide"];
  6836. return dic;
  6837. }
  6838. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  6839. countryCode = [self translateSingleQuote:countryCode];
  6840. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  6841. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6842. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  6843. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  6844. if (name == NULL) {
  6845. name = "";
  6846. }
  6847. if (code == NULL) {
  6848. code = "";
  6849. }
  6850. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  6851. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  6852. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  6853. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  6854. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  6855. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  6856. }
  6857. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  6858. [container setValue:stateDic forKey:key];
  6859. }] mutableCopy];
  6860. // failure 可以不用了,一样的
  6861. if (ret.allKeys.count == 0) {
  6862. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  6863. [stateDic setValue:@"Other" forKey:@"value"];
  6864. [stateDic setValue:@"" forKey:@"value_id"];
  6865. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  6866. if (state_code && [@"" isEqualToString:state_code]) {
  6867. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  6868. }
  6869. NSString *key = [NSString stringWithFormat:@"val_0"];
  6870. [ret setValue:stateDic forKey:key];
  6871. }
  6872. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  6873. return ret;
  6874. }
  6875. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  6876. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6877. orderCode = [self translateSingleQuote:orderCode];
  6878. 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];
  6879. __block NSString *payType = @"";
  6880. __block NSString *firstName = @"";
  6881. __block NSString *lastName = @"";
  6882. __block NSString *addr1 = @"";
  6883. __block NSString *addr2 = @"";
  6884. __block NSString *zipcode = @"";
  6885. __block NSString *cardType = @"";
  6886. __block NSString *cardNumber = @"";
  6887. __block NSString *securityCode = @"";
  6888. __block NSString *month = @"";
  6889. __block NSString *year = @"";
  6890. __block NSString *city = @"";
  6891. __block NSString *state = @"";
  6892. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6893. payType = [self textAtColumn:0 statement:stmt];
  6894. firstName = [self textAtColumn:1 statement:stmt];
  6895. lastName = [self textAtColumn:2 statement:stmt];
  6896. addr1 = [self textAtColumn:3 statement:stmt];
  6897. addr2 = [self textAtColumn:4 statement:stmt];
  6898. zipcode = [self textAtColumn:5 statement:stmt];
  6899. cardType = [self textAtColumn:6 statement:stmt];
  6900. cardNumber = [self textAtColumn:7 statement:stmt];
  6901. securityCode = [self textAtColumn:8 statement:stmt];
  6902. month = [self textAtColumn:9 statement:stmt];
  6903. year = [self textAtColumn:10 statement:stmt];
  6904. city = [self textAtColumn:11 statement:stmt];
  6905. state = [self textAtColumn:12 statement:stmt];
  6906. }];
  6907. NSString *required = @"true";
  6908. // setting
  6909. NSDictionary *setting = params[@"setting"];
  6910. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  6911. if ([requiredNumber integerValue]) {
  6912. required = @"true";
  6913. } else {
  6914. required = @"false";
  6915. }
  6916. // "section_2"
  6917. NSMutableDictionary *dic = @{
  6918. @"count" : @(1),
  6919. @"item_0" : @{
  6920. @"aname" : @"Payment",
  6921. @"required" : required,
  6922. @"cadedate" : @{
  6923. @"count" : @(6),
  6924. @"val_0" : @{
  6925. @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  6926. @"value" : @"Check",
  6927. @"value_id" : @"Check"
  6928. },
  6929. @"val_1" : @{
  6930. @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  6931. @"value" : @"Cash",
  6932. @"value_id" : @"Cash"
  6933. },
  6934. @"val_2" : @{
  6935. @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  6936. @"value" : @"NET 30",
  6937. @"value_id" : @"NET 30"
  6938. },
  6939. @"val_3" : @{
  6940. @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  6941. @"value" : @"Wire Transfer",
  6942. @"value_id" : @"Wire Transfer"
  6943. },
  6944. @"val_4" : @{
  6945. @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  6946. @"sub_item" : @{
  6947. @"count" : @(3),
  6948. @"item_0" : @{
  6949. @"aname" : @"choose",
  6950. @"control" : @"multi_action",
  6951. @"count" : @(1),
  6952. @"item_0" : @{
  6953. @"aname" : @"Same as customer",
  6954. @"key_map" : @{
  6955. @"credit_card_address1" : @"customer_address1",
  6956. @"credit_card_address2" : @"customer_address2",
  6957. @"credit_card_city" : @"customer_city",
  6958. @"credit_card_first_name" : @"customer_first_name",
  6959. @"credit_card_last_name" : @"customer_last_name",
  6960. @"credit_card_state" : @"customer_state",
  6961. @"credit_card_zipcode" : @"customer_zipcode"
  6962. },
  6963. @"type" : @"pull"
  6964. }
  6965. },
  6966. @"item_1" : @{
  6967. @"aname" : @"",
  6968. @"color" : @"red",
  6969. @"control" : @"text",
  6970. @"name" : @"",
  6971. @"value" : @"USA Credit cards only"
  6972. },
  6973. @"item_2" : @{
  6974. @"aname" : @"Fill",
  6975. @"cadedate" : @{
  6976. @"count" : @(2),
  6977. @"val_0" : @{
  6978. @"check" : @(1),
  6979. @"sub_item" : @{
  6980. @"count" : @(11),
  6981. @"item_0" : @{
  6982. @"aname" : @"Type",
  6983. @"cadedate" : @{
  6984. @"count" : @(2),
  6985. @"val_0" : @{
  6986. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  6987. @"value" : @"VISA",
  6988. @"value_id" : @(0)
  6989. },
  6990. @"val_1" : @{
  6991. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  6992. @"value" : @"MASTER CARD",
  6993. @"value_id" : @(1)
  6994. }
  6995. },
  6996. @"control" : @"enum",
  6997. @"name" : @"credit_card_type",
  6998. @"required" : @"true",
  6999. @"single_select" : @"true"
  7000. },
  7001. @"item_1" : @{
  7002. @"aname" : @"Number",
  7003. @"control" : @"edit",
  7004. @"keyboard" : @"int",
  7005. @"length" : @"16",
  7006. @"name" : @"credit_card_number",
  7007. @"required" : @"true",
  7008. @"value" : cardNumber
  7009. },
  7010. @"item_10" : @{
  7011. @"aname" : @"State",
  7012. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  7013. @"control" : @"enum",
  7014. @"enum" : @"true",
  7015. @"name" : @"credit_card_state",
  7016. @"required" : @"true",
  7017. @"single_select" : @"true"
  7018. },
  7019. @"item_2" : @{
  7020. @"aname" : @"Expiration Date",
  7021. @"control" : @"monthpicker",
  7022. @"name" : @"credit_card_expiration",
  7023. @"required" : @"true",
  7024. @"type" : @"date",
  7025. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  7026. },
  7027. @"item_3" : @{
  7028. @"aname" : @"Security Code",
  7029. @"control" : @"edit",
  7030. @"keyboard" : @"int",
  7031. @"length" : @"3",
  7032. @"name" : @"credit_card_security_code",
  7033. @"required" : @"true",
  7034. @"value" : securityCode
  7035. },
  7036. @"item_4" : @{
  7037. @"aname" : @"First Name",
  7038. @"control" : @"edit",
  7039. @"keyboard" : @"text",
  7040. @"name" : @"credit_card_first_name",
  7041. @"required" : @"true",
  7042. @"value" : firstName
  7043. },
  7044. @"item_5" : @{
  7045. @"aname" : @"Last Name",
  7046. @"control" : @"edit",
  7047. @"keyboard" : @"text",
  7048. @"name" : @"credit_card_last_name",
  7049. @"required" : @"true",
  7050. @"value" : lastName
  7051. },
  7052. @"item_6" : @{
  7053. @"aname" : @"Address 1",
  7054. @"control" : @"edit",
  7055. @"keyboard" : @"text",
  7056. @"name" : @"credit_card_address1",
  7057. @"required" : @"true",
  7058. @"value" : addr1
  7059. },
  7060. @"item_7" : @{
  7061. @"aname" : @"Address 2",
  7062. @"control" : @"edit",
  7063. @"keyboard" : @"text",
  7064. @"name" : @"credit_card_address2",
  7065. @"value" : addr2
  7066. },
  7067. @"item_8" : @{
  7068. @"aname" : @"zip code",
  7069. @"control" : @"edit",
  7070. @"keyboard" : @"text",
  7071. @"name" : @"credit_card_zipcode",
  7072. @"required" : @"true",
  7073. @"value" : zipcode
  7074. },
  7075. @"item_9" : @{
  7076. @"aname" : @"City",
  7077. @"control" : @"edit",
  7078. @"keyboard" : @"text",
  7079. @"name" : @"credit_card_city",
  7080. @"required" : @"true",
  7081. @"value" : city
  7082. }
  7083. },
  7084. @"value" : @"Fill Now",
  7085. @"value_id" : @""
  7086. },
  7087. @"val_1" : @{
  7088. @"check" : @(0),
  7089. @"value" : @"Fill Later",
  7090. @"value_id" : @""
  7091. }
  7092. },
  7093. @"control" : @"enum",
  7094. @"name" : @"",
  7095. @"single_select" : @"true"
  7096. }
  7097. },
  7098. @"value" : @"Credit Card",
  7099. @"value_id" : @"Credit Card"
  7100. },
  7101. @"val_5" : @{
  7102. @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  7103. @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  7104. @"value_id" : @"FOLLOW EXISTING"
  7105. }
  7106. },
  7107. @"control" : @"enum",
  7108. @"name" : @"paymentType",
  7109. @"single_select" : @"true"
  7110. },
  7111. @"title" : @"Payment Information"
  7112. }.mutableCopy;
  7113. NSNumber *hide = setting[@"PaymentInformationHide"];
  7114. [dic setValue:hide forKey:@"hide"];
  7115. return dic;
  7116. }
  7117. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  7118. {
  7119. sqlite3 *db = [iSalesDB get_db];
  7120. // UISetting
  7121. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  7122. NSString *cachefolder = [paths objectAtIndex:0];
  7123. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  7124. NSData* json =nil;
  7125. json=[NSData dataWithContentsOfFile:img_cache];
  7126. NSError *error=nil;
  7127. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  7128. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  7129. [params setObject:setting forKey:@"setting"];
  7130. int section_count = 0;
  7131. // params
  7132. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7133. orderCode = [self translateSingleQuote:orderCode];
  7134. // 0 Order Type 1 Shipping Method 2 Payment Information
  7135. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  7136. // 0 Order Type
  7137. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  7138. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  7139. [ret setValue:order_type_dic forKey:key0];
  7140. // 1 Shipping Method
  7141. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  7142. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  7143. [ret setValue:shipping_method_dic forKey:key1];
  7144. // 2 Payment Information
  7145. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  7146. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  7147. [ret setValue:payment_info_dic forKey:key2];
  7148. // 3 Customer
  7149. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  7150. NSDictionary *customer_dic = [self customerDic:params db:db];
  7151. [ret setValue:customer_dic forKey:key3];
  7152. // 4 Ship To
  7153. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  7154. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  7155. [ret setValue:ship_to_dic forKey:key4];
  7156. // 5 Ship From
  7157. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  7158. if (![shipFromDisable integerValue]) {
  7159. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  7160. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  7161. [ret setValue:ship_from_dic forKey:key5];
  7162. }
  7163. // 6 Freight Bill To
  7164. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  7165. if (![freightBillToDisable integerValue]) {
  7166. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  7167. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  7168. [ret setValue:freight_bill_to forKey:key6];
  7169. }
  7170. // 7 Merchandise Bill To
  7171. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  7172. if (![merchandiseBillToDisable integerValue]) {
  7173. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  7174. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  7175. [ret setValue:merchandise_bill_to_dic forKey:key7];
  7176. }
  7177. // 8 Return To
  7178. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  7179. if (![returnToDisable integerValue]) {
  7180. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  7181. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  7182. [ret setValue:return_to_dic forKey:key8];
  7183. }
  7184. // 9 Model Information
  7185. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  7186. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  7187. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  7188. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  7189. [ret setValue:model_info_dic forKey:key9];
  7190. // 10 Remarks Content
  7191. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  7192. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  7193. [ret setValue:remarks_content_dic forKey:key10];
  7194. // 11 Order Total
  7195. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  7196. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  7197. [ret setValue:order_total_dic forKey:key11];
  7198. // 12 Signature
  7199. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  7200. NSDictionary *sign_dic = [self signatureDic:params db:db];
  7201. [ret setValue:sign_dic forKey:key12];
  7202. [ret setValue:@(section_count) forKey:@"section_count"];
  7203. [iSalesDB close_db:db];
  7204. return [RAUtils dict2data:ret];
  7205. // return nil;
  7206. }
  7207. #pragma mark addr editor
  7208. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  7209. NSMutableDictionary *new_item = [item mutableCopy];
  7210. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  7211. return new_item;
  7212. }
  7213. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  7214. {
  7215. // "is_subaction" = true;
  7216. // orderCode = MOB1608240002;
  7217. // password = 123456;
  7218. // "subaction_tag" = 1;
  7219. // user = EvanK;
  7220. // {
  7221. // "is_subaction" = true;
  7222. // orderCode = MOB1608240002;
  7223. // password = 123456;
  7224. // "refresh_trigger" = zipcode;
  7225. // "subaction_tag" = 1;
  7226. // user = EvanK;
  7227. // }
  7228. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  7229. [ret removeObjectForKey:@"up_params"];
  7230. [ret setObject:@"New Address" forKey:@"title"];
  7231. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  7232. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  7233. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  7234. NSMutableDictionary *country_dic = [section_0[@"item_5"] mutableCopy];
  7235. [country_dic removeObjectForKey:@"refresh"];
  7236. [country_dic removeObjectForKey:@"restore"];
  7237. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  7238. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  7239. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_6"] add:YES] forKey:@"item_2"]; // Address 1*
  7240. [new_section_0 setObject:section_0[@"item_7"] forKey:@"item_3"]; // Address 2
  7241. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_4"]; // Address 3
  7242. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_5"]; // Address 4
  7243. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_10"] add:YES];
  7244. [zip_code_dic removeObjectForKey:@"refresh"];
  7245. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  7246. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_11"] add:YES] forKey:@"item_7"]; // State/Province*
  7247. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_8"]; // City*
  7248. [new_section_0 setObject:section_0[@"item_13"] forKey:@"item_9"]; // Contact First Name
  7249. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_10"]; // Contact Last Name
  7250. [new_section_0 setObject:section_0[@"item_4"] forKey:@"item_11"]; // Phone*
  7251. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_12"]; // Fax
  7252. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_3"] add:NO] forKey:@"item_13"]; // Email
  7253. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  7254. // country
  7255. NSString *countryCode = @"US";
  7256. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  7257. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  7258. // state
  7259. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:nil] mutableCopy];
  7260. NSDictionary *tmpDic = @{
  7261. @"value" : @"Other",
  7262. @"value_id" : @"",
  7263. @"check" : [NSNumber numberWithInteger:0]
  7264. };
  7265. for (int i = 0; i < allState.allKeys.count; i++) {
  7266. NSString *key = [NSString stringWithFormat:@"val_%d",i];
  7267. NSDictionary *tmp = allState[key];
  7268. [allState setValue:tmpDic forKey:key];
  7269. tmpDic = tmp;
  7270. }
  7271. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  7272. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  7273. [ret setValue:new_section_0 forKey:@"section_0"];
  7274. return [RAUtils dict2data:ret];
  7275. }
  7276. #pragma mark save addr
  7277. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  7278. {
  7279. // NSString *companyName = [self valueInParams:params key:@"company"];
  7280. // NSString *addr1 = [self valueInParams:params key:@"address"];
  7281. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  7282. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  7283. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  7284. // NSString *countryId = [self valueInParams:params key:@"country"];
  7285. // NSString *stateCode = [self valueInParams:params key:@"state"];
  7286. // NSString *city = [self valueInParams:params key:@"city"];
  7287. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  7288. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  7289. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  7290. // NSString *phone = [self valueInParams:params key:@"phone"];
  7291. // NSString *fax = [self valueInParams:params key:@"fax"];
  7292. // NSString *email = [self valueInParams:params key:@"email"];
  7293. return [self offline_saveContact:params update:NO isCustomer:NO];
  7294. }
  7295. #pragma mark cancel order
  7296. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  7297. {
  7298. // NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7299. NSString *order_id = [self valueInParams:params key:@"orderId"];
  7300. // NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  7301. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  7302. int ret = [iSalesDB execSql:sql];
  7303. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7304. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7305. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7306. // [dic setValue:@"160409" forKey:@"min_ver"];
  7307. return [RAUtils dict2data:dic];
  7308. }
  7309. #pragma mark sign order
  7310. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  7311. {
  7312. //参考 offline_saveBusinesscard
  7313. NSLog(@"sign order params: %@",params);
  7314. // orderCode = MOB1608240002;
  7315. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  7316. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7317. orderCode = [self translateSingleQuote:orderCode];
  7318. NSString *picPath = [self valueInParams:params key:@"picpath"];
  7319. picPath = [self translateSingleQuote:picPath];
  7320. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  7321. int ret = [iSalesDB execSql:sql];
  7322. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7323. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7324. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7325. // [dic setValue:@"160409" forKey:@"min_ver"];
  7326. return [RAUtils dict2data:dic];
  7327. }
  7328. #pragma mark save order
  7329. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  7330. NSString *ret = [self valueInParams:params key:key];
  7331. if (translate) {
  7332. ret = [self translateSingleQuote:ret];
  7333. }
  7334. return ret;
  7335. }
  7336. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  7337. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  7338. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  7339. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  7340. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  7341. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  7342. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  7343. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  7344. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  7345. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  7346. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  7347. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  7348. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  7349. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  7350. if (![total_price isEqualToString:@""]) {
  7351. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  7352. } else {
  7353. total_price = @"";
  7354. }
  7355. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  7356. if ([paymentsAndCredits isEqualToString:@""]) {
  7357. paymentsAndCredits = @"";
  7358. } else {
  7359. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  7360. }
  7361. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  7362. if ([handling_fee_value isEqualToString:@""]) {
  7363. handling_fee_value = @"";
  7364. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  7365. handling_fee_value = @"";
  7366. } else {
  7367. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  7368. }
  7369. NSString *handling_fee_placeholder = handling_fee_value;
  7370. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  7371. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  7372. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  7373. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  7374. NSString *lift_gate_placeholder = @"";
  7375. if ([lift_gate_value isEqualToString:@""]) {
  7376. lift_gate_placeholder = @"";
  7377. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  7378. lift_gate_placeholder = @"";
  7379. } else {
  7380. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  7381. }
  7382. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  7383. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  7384. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  7385. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  7386. 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];
  7387. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  7388. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  7389. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  7390. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  7391. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  7392. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  7393. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  7394. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  7395. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  7396. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  7397. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  7398. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  7399. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  7400. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  7401. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  7402. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  7403. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  7404. BOOL number_nil = [credit_card_number isEqualToString:@""];
  7405. if (!number_nil) {
  7406. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  7407. }
  7408. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  7409. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  7410. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  7411. if (!security_code_nil) {
  7412. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  7413. }
  7414. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  7415. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  7416. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  7417. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  7418. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  7419. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  7420. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  7421. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  7422. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  7423. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  7424. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  7425. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  7426. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  7427. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  7428. NSString *contact_id = customer_cid;
  7429. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  7430. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  7431. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  7432. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  7433. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  7434. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  7435. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  7436. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  7437. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  7438. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  7439. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  7440. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  7441. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  7442. //
  7443. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  7444. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  7445. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  7446. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  7447. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  7448. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  7449. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  7450. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  7451. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  7452. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  7453. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  7454. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  7455. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  7456. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  7457. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  7458. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  7459. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  7460. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  7461. 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];
  7462. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  7463. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  7464. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  7465. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  7466. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  7467. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  7468. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  7469. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  7470. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  7471. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  7472. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  7473. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  7474. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  7475. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  7476. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  7477. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  7478. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  7479. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  7480. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  7481. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  7482. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  7483. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  7484. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  7485. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  7486. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  7487. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  7488. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  7489. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  7490. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  7491. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  7492. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  7493. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  7494. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  7495. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  7496. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  7497. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  7498. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  7499. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  7500. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  7501. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  7502. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  7503. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  7504. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  7505. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  7506. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  7507. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  7508. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  7509. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  7510. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  7511. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  7512. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  7513. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  7514. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  7515. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  7516. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  7517. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  7518. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  7519. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  7520. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  7521. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  7522. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  7523. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  7524. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  7525. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  7526. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  7527. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  7528. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  7529. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  7530. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  7531. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  7532. NSString *order_status = @"status = 1,";
  7533. if (submit) {
  7534. order_status = @"status = -11,";
  7535. }
  7536. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  7537. NSString *sync_sql = @"";
  7538. if (submit) {
  7539. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  7540. [param setValue:sales_rep forKey:@"sales_rep"];
  7541. NSString *sync_data = [self translateSingleQuote:[RAUtils dict2string:param]];
  7542. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  7543. }
  7544. 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];
  7545. DebugLog(@"save order contactSql: %@",contactSql);
  7546. DebugLog(@"save order orderSql: %@",orderSql);
  7547. // int contact_ret = [iSalesDB execSql:contactSql];
  7548. int order_ret = [iSalesDB execSql:orderSql];
  7549. int ret = order_ret;
  7550. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7551. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7552. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7553. // [dic setValue:@"160409" forKey:@"min_ver"];
  7554. [dic setObject:so_id forKey:@"so#"];
  7555. return [RAUtils dict2data:dic];
  7556. }
  7557. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  7558. {
  7559. // id foo = nil;
  7560. // NSMutableArray *a = @[].mutableCopy;
  7561. // [a addObject:foo];
  7562. return [self saveorder:param submit:NO];
  7563. }
  7564. #pragma mark add to cart by name
  7565. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  7566. {
  7567. NSString *orderCode = [params objectForKey:@"orderCode"];
  7568. NSString *product_name = [params objectForKey:@"product_name"];
  7569. product_name = [self translateSingleQuote:product_name];
  7570. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  7571. sqlite3 *db = [iSalesDB get_db];
  7572. __block NSMutableString *product_id_string = [NSMutableString string];
  7573. for (int i = 0; i < product_name_array.count; i++) {
  7574. NSString *name = [product_name_array objectAtIndex:i];
  7575. NSString *sql = [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  7576. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7577. int product_id = sqlite3_column_int(stmt, 0);
  7578. if (i == product_name_array.count - 1) {
  7579. [product_id_string appendFormat:@"%d",product_id];
  7580. } else {
  7581. [product_id_string appendFormat:@"%d,",product_id];
  7582. }
  7583. }];
  7584. }
  7585. NSDictionary *newParams = @{
  7586. @"product_id" : product_id_string,
  7587. @"orderCode" : orderCode
  7588. };
  7589. [iSalesDB close_db:db];
  7590. return [self offline_add2cart:[newParams mutableCopy]];
  7591. }
  7592. #pragma mark reset order
  7593. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  7594. {
  7595. UIApplication * app = [UIApplication sharedApplication];
  7596. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  7597. appDelegate.disable_trigger=true;
  7598. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  7599. appDelegate.disable_trigger=false;
  7600. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7601. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  7602. return [RAUtils dict2data:dic];
  7603. }
  7604. #pragma mark submit order
  7605. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  7606. {
  7607. return [self saveorder:params submit:YES];
  7608. }
  7609. #pragma mark copy order
  7610. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  7611. {
  7612. NSMutableDictionary *ret = @{}.mutableCopy;
  7613. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  7614. sqlite3 *db = [iSalesDB get_db];
  7615. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  7616. // 首先查看联系人是否active
  7617. 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];
  7618. __block int customer_is_active = 1;
  7619. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7620. customer_is_active = sqlite3_column_int(stmt, 0);
  7621. }];
  7622. if (!customer_is_active) {
  7623. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7624. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7625. [iSalesDB close_db:db];
  7626. return [RAUtils dict2data:ret];
  7627. }
  7628. // new order
  7629. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  7630. NSString *new_order_code = [self get_offline_soid:db];
  7631. NSString *user = ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  7632. user = [self translateSingleQuote:user];
  7633. NSString *insert_order_sql = [NSString stringWithFormat:@"insert into offline_order (so_id,status,submit_as,internal_notes,sales_rep,create_by,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,'%@',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
  7634. __block int result = 1;
  7635. result = [iSalesDB execSql:insert_order_sql db:db];
  7636. if (!result) {
  7637. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7638. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7639. [iSalesDB close_db:db];
  7640. return [RAUtils dict2data:ret];
  7641. }
  7642. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  7643. // __block NSString *product_id = @"";
  7644. __weak typeof(self) weakSelf = self;
  7645. 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];
  7646. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7647. int is_active = sqlite3_column_int(stmt, 1);
  7648. if (is_active) {
  7649. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  7650. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  7651. 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];
  7652. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  7653. }
  7654. }];
  7655. // product_id = [product_id substringFromIndex:1];
  7656. //
  7657. // [self offline_add2cart:@{}.mutableCopy];
  7658. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7659. [iSalesDB close_db:db];
  7660. if (result) {
  7661. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  7662. } else {
  7663. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7664. }
  7665. [ret setObject:new_order_code forKey:@"so_id"];
  7666. return [RAUtils dict2data:ret];
  7667. }
  7668. #pragma mark move wish list to cart
  7669. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  7670. NSString *collectId = params[@"collectId"];
  7671. NSString *sql = [NSString stringWithFormat:@"select product_id,qty from wishlist where _id in (%@);",collectId];
  7672. __block NSString *product_id = @"";
  7673. __block NSString *qty = @"";
  7674. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7675. int productId = sqlite3_column_int(stmt, 0);
  7676. int item_qty = sqlite3_column_int(stmt, 1);
  7677. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  7678. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  7679. }];
  7680. product_id = [product_id substringFromIndex:1];
  7681. qty = [qty substringFromIndex:1];
  7682. NSDictionary *newParams = @{
  7683. @"product_id" : product_id,
  7684. @"orderCode" : params[@"orderCode"],
  7685. @"qty" : qty
  7686. };
  7687. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  7688. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  7689. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  7690. // NSString *deleteSQL = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  7691. //
  7692. // int ret = [iSalesDB execSql:deleteSQL];
  7693. int ret = [[[self offline_deletewishlist:@{@"collectId" : collectId}.mutableCopy] valueForKey:@"result"] integerValue];
  7694. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  7695. }
  7696. return [RAUtils dict2data:retDic];
  7697. }
  7698. #pragma mark - portfolio
  7699. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  7700. int sort = [[params valueForKey:@"sort"] integerValue];
  7701. int offset = [[params valueForKey:@"offset"] integerValue];
  7702. int limit = [[params valueForKey:@"limit"] integerValue];
  7703. NSString *orderBy = @"";
  7704. switch (sort) {
  7705. case 0:{
  7706. orderBy = @"modify_time desc";
  7707. }
  7708. break;
  7709. case 1:{
  7710. orderBy = @"modify_time asc";
  7711. }
  7712. break;
  7713. case 2:{
  7714. orderBy = @"name asc";
  7715. }
  7716. break;
  7717. case 3:{
  7718. orderBy = @"name desc";
  7719. }
  7720. break;
  7721. case 4:{
  7722. orderBy = @"description asc";
  7723. }
  7724. break;
  7725. default:
  7726. break;
  7727. }
  7728. 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 and (p.is_delete is null or p.is_delete = 0) group by p.product_id order by %@ limit %d offset %d;",orderBy,limit,offset];
  7729. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7730. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7731. sqlite3 *db = [iSalesDB get_db];
  7732. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7733. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  7734. int product_id = sqlite3_column_int(stmt, 0);
  7735. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  7736. NSString *name = [self textAtColumn:1 statement:stmt];
  7737. NSString *description = [self textAtColumn:2 statement:stmt];
  7738. double price = sqlite3_column_double(stmt, 3);
  7739. double discount = sqlite3_column_double(stmt,4);
  7740. int qty = sqlite3_column_int(stmt, 5);
  7741. int percentage = sqlite3_column_int(stmt, 6);
  7742. int item_id = sqlite3_column_int(stmt, 7);
  7743. // int fashion_id = sqlite3_column_int(stmt, 8);
  7744. NSString *img_path = [self textAtColumn:9 statement:stmt];
  7745. NSString *line_note = [self textAtColumn:10 statement:stmt];
  7746. double percent = sqlite3_column_double(stmt, 11);
  7747. NSString *price_null = [self textAtColumn:3 statement:stmt];
  7748. if ([price_null isEqualToString:@"null"]) {
  7749. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  7750. }
  7751. NSMutableDictionary *item = @{
  7752. @"linenotes": line_note,
  7753. @"check": @(1),
  7754. @"product_id": product_id_string,
  7755. @"available_qty": @(qty),
  7756. @"available_percent" : @(percent),
  7757. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  7758. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  7759. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  7760. @"img": img_path,
  7761. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  7762. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  7763. }.mutableCopy;
  7764. if (percentage) {
  7765. [item removeObjectForKey:@"available_qty"];
  7766. } else {
  7767. [item removeObjectForKey:@"available_percent"];
  7768. }
  7769. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  7770. if ([qty_null isEqualToString:@"null"]) {
  7771. [item removeObjectForKey:@"available_qty"];
  7772. }
  7773. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  7774. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  7775. }];
  7776. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  7777. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7778. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  7779. }
  7780. [iSalesDB close_db:db];
  7781. [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  7782. [dic setValue:@"" forKey:@"email_content"];
  7783. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  7784. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7785. return [RAUtils dict2data:dic];
  7786. }
  7787. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  7788. __block int qty = 0;
  7789. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  7790. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7791. qty = sqlite3_column_int(stmt, 0);
  7792. }];
  7793. return qty;
  7794. }
  7795. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct {
  7796. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  7797. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  7798. NSMutableDictionary * values = params[@"replaceValue"];
  7799. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  7800. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  7801. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  7802. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  7803. NSString *pdf_path = @"";
  7804. if (direct) {
  7805. configureParams = [self valueInParams:params key:@"pdfUrl"];
  7806. } else {
  7807. pdf_path = [self valueInParams:params key:@"pdfPath"];
  7808. }
  7809. NSString *create_user = [self valueInParams:params key:@"user"];
  7810. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  7811. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  7812. // model info
  7813. NSString *sql = [NSString stringWithFormat:@"select product_id,ifnull(sheet_price,'null'),ifnull(sheet_discount,'null'),ifnull(available_qty,'null'),percentage,item_id,line_note,percent from offline_portfolio where product_id in (%@);",product_ids];
  7814. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7815. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  7816. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7817. sqlite3 *db = [iSalesDB get_db];
  7818. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7819. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  7820. int product_id = sqlite3_column_int(stmt, 0);
  7821. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  7822. double price = sqlite3_column_double(stmt, 1);
  7823. double discount = sqlite3_column_double(stmt,2);
  7824. int qty = sqlite3_column_int(stmt, 3);
  7825. int percentage = sqlite3_column_int(stmt, 4);
  7826. int item_id = sqlite3_column_int(stmt, 5);
  7827. NSString *line_note = [self textAtColumn:6 statement:stmt];
  7828. double percent = sqlite3_column_double(stmt, 7);
  7829. NSString *price_null = [self textAtColumn:1 statement:stmt];
  7830. /* if ([price_null isEqualToString:@"null"]) {
  7831. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  7832. }
  7833. */
  7834. [product_ids_string appendFormat:@"%@,",product_id_string];
  7835. // Regular Price
  7836. int regular_price = [[params objectForKey:@"regular_price"] integerValue];
  7837. NSString *regular_price_str = [self get_portfolio_price:appDelegate.contact_id item_id:item_id price:regular_price db:db];
  7838. // QTY
  7839. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  7840. if ([qty_null isEqualToString:@"null"]) {
  7841. // 查available
  7842. qty = [self model_QTY:product_id_string db:db];
  7843. }
  7844. if (percentage) {
  7845. qty = qty * percent / 100;
  7846. }
  7847. // Special Price
  7848. if ([price_null isEqualToString:@"null"]) {
  7849. // price = regular price
  7850. price = [regular_price_str doubleValue];
  7851. }
  7852. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  7853. if (![discount_null isEqualToString:@"null"]) {
  7854. price = price * (1 - discount / 100.0);
  7855. }
  7856. NSMutableDictionary *item = @{
  7857. @"line_note": line_note,
  7858. @"product_id": product_id_string,
  7859. @"available_qty": @(qty),
  7860. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  7861. @"regular_price" : regular_price_str,
  7862. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  7863. }.mutableCopy;
  7864. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  7865. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  7866. }];
  7867. [iSalesDB close_db:db];
  7868. if (product_ids_string.length > 0) {
  7869. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  7870. }
  7871. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  7872. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  7873. return [RAUtils dict2data:resultDictionary];
  7874. }
  7875. NSString *model_info = [RAUtils dict2string:dic];
  7876. // 创建PDF
  7877. // 在preview情况下保存,则不需要新建了
  7878. if (direct) {
  7879. NSMutableDictionary *tear_sheet_params = params;
  7880. // if (tear_sheet_id) {
  7881. // tear_sheet_params = values;
  7882. // }
  7883. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  7884. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  7885. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  7886. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  7887. resultDictionary = pdfInfo.mutableCopy;
  7888. } else { // 创建PDF失败
  7889. return pdfData;
  7890. }
  7891. } else {
  7892. // pdf_path 就是本地路径
  7893. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  7894. }
  7895. // 将文件移动到PDF Cache文件夹
  7896. NSString *newPath = [pdf_path lastPathComponent];
  7897. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  7898. NSString *cachefolder = [paths objectAtIndex:0];
  7899. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  7900. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  7901. NSFileManager *fileManager = [NSFileManager defaultManager];
  7902. NSError *error = nil;
  7903. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  7904. if (error) { // 移动文件失败
  7905. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  7906. return [RAUtils dict2data:resultDictionary];
  7907. }
  7908. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  7909. pdf_path = [newPath lastPathComponent];
  7910. // 保存信息
  7911. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  7912. NSString *off_params = [RAUtils dict2string:params];
  7913. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  7914. pdf_path = [self translateSingleQuote:pdf_path];
  7915. create_user = [self translateSingleQuote:create_user];
  7916. tear_note = [self translateSingleQuote:tear_note];
  7917. tear_name = [self translateSingleQuote:tear_name];
  7918. model_info = [self translateSingleQuote:model_info];
  7919. configureParams = [self translateSingleQuote:configureParams];
  7920. off_params = [self translateSingleQuote:off_params];
  7921. NSString *save_pdf_sql = [NSString stringWithFormat:@"insert into offline_pdf (pdf_path,create_user,tear_note,tear_name,model_info,configureParams,is_local,off_params) values ('%@','%@','%@','%@','%@','%@',1,'%@');",pdf_path,create_user,tear_note,tear_name,model_info,configureParams,off_params];
  7922. if (tear_sheet_id) {
  7923. int _id = [tear_sheet_id integerValue];
  7924. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  7925. }
  7926. int result = [iSalesDB execSql:save_pdf_sql];
  7927. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  7928. //
  7929. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  7930. if (remove_Item) {
  7931. // portfolioId
  7932. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  7933. }
  7934. return [RAUtils dict2data:resultDictionary];
  7935. }
  7936. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  7937. return [self offline_savePDF:params direct:YES];
  7938. }
  7939. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  7940. return [self offline_savePDF:params direct:NO];
  7941. }
  7942. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  7943. int offset = [[params valueForKey:@"offset"] integerValue];
  7944. int limit = [[params valueForKey:@"limit"] integerValue];
  7945. NSString *keyword = [params valueForKey:@"keyWord"];
  7946. NSString *where = @"where is_delete is null or is_delete = 0";
  7947. if (keyword.length) {
  7948. keyword = [self translateSingleQuote:keyword];
  7949. where = [NSString stringWithFormat:@"where (is_delete is null or is_delete = 0 ) and lower(create_user) like '%%%@%%' or lower(tear_note) like '%%%@%%' or lower(tear_name) like '%%%@%%'",keyword.lowercaseString,keyword.lowercaseString,keyword.lowercaseString];
  7950. }
  7951. NSString *sql = [NSString stringWithFormat:@"select tear_name,tear_note,createtime,create_user,pdf_path,_id,model_info,off_params from offline_pdf %@ order by createtime desc limit %d offset %d;",where,limit,offset];// tearsheets_id ---> _id
  7952. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7953. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  7954. NSString *cachefolder = [paths objectAtIndex:0];
  7955. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  7956. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7957. NSString *name = [self textAtColumn:0 statement:stmt];
  7958. NSString *note = [self textAtColumn:1 statement:stmt];
  7959. NSString *time = [self textAtColumn:2 statement:stmt];
  7960. NSString *user = [self textAtColumn:3 statement:stmt];
  7961. NSString *path = [self textAtColumn:4 statement:stmt];
  7962. NSString *model_info = [self textAtColumn:6 statement:stmt];
  7963. NSString *off_params = [self textAtColumn:7 statement:stmt];
  7964. path = [pdfFolder stringByAppendingPathComponent:path];
  7965. BOOL bdir=NO;
  7966. NSFileManager* fileManager = [NSFileManager defaultManager];
  7967. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  7968. {
  7969. //pdf文件不存在
  7970. path=nil;
  7971. }
  7972. time = [self changeDateTimeFormate:time];
  7973. time = [time stringByAppendingString:@" PST"];
  7974. int sheet_id = sqlite3_column_int(stmt, 5);
  7975. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  7976. item[@"tearsheetsId"]=@(sheet_id);
  7977. item[@"pdf_path"]=path;
  7978. item[@"create_time"]=time;
  7979. item[@"create_user"]=user;
  7980. item[@"tear_note"]=note;
  7981. item[@"tear_name"]=name;
  7982. item[@"isLocalFile"]=@"true";
  7983. item[@"model_info"]=model_info;
  7984. item[@"off_params"]=off_params;
  7985. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  7986. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  7987. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  7988. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7989. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  7990. }];
  7991. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7992. return [RAUtils dict2data:dic];
  7993. }
  7994. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  7995. NSString *prodct_ids = [params objectForKey:@"product_id"];
  7996. // NSString *user = [params objectForKey:@"user"];
  7997. // NSString *sql = [NSString stringWithFormat:@"insert into offline_portfolio (product_id,name,description,item_id) select * from (select p.product_id,p.name,p.description,p.item_id from product as p where p.product_id in (%@) and (select count(0) from offline_portfolio where product_id in (%@) and (is_delete is null or is_delete = 0)) = 0);",prodct_ids,prodct_ids];
  7998. NSString *sql = [NSString stringWithFormat:@"insert into offline_portfolio (product_id,name,description,item_id) select * from (select p.product_id,p.name,p.description,p.item_id from product as p where p.product_id in (%@) and p.product_id not in (select product_id from offline_portfolio));insert into offline_portfolio (product_id,name,description,item_id,is_delete) select * from (select p.product_id,p.name,p.description,p.item_id,0 from product as p left join offline_portfolio as f on p.product_id = f.product_id where p.product_id in (%@) and p.product_id in (select product_id from offline_portfolio) and f.is_delete = 1);delete from offline_portfolio where (select count(0) from offline_portfolio where product_id in (%@)) > 1 and is_delete = 1;",prodct_ids,prodct_ids,prodct_ids];
  7999. int result = [iSalesDB execSql:sql];
  8000. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8001. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  8002. if (result == RESULT_TRUE) {
  8003. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  8004. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  8005. sqlite3 *db = [iSalesDB get_db];
  8006. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  8007. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  8008. [iSalesDB close_db:db];
  8009. }
  8010. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8011. return [RAUtils dict2data:dic];
  8012. }
  8013. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  8014. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  8015. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  8016. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  8017. // Regurlar Price
  8018. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  8019. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  8020. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  8021. __block long val_count = 0;
  8022. NSString *sql = @"select name,type,order_by from price order by order_by";
  8023. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8024. NSString *name = [self textAtColumn:0 statement:stmt];
  8025. int type = sqlite3_column_int(stmt, 1);
  8026. int order_by = sqlite3_column_int(stmt, 2);
  8027. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8028. NSDictionary *price_dic = @{
  8029. @"value" : name,
  8030. @"value_id" : [NSNumber numberWithInteger:type],
  8031. @"check" : order_by == 0 ? @(1) : @(0)
  8032. };
  8033. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  8034. val_count = ++(*count);
  8035. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  8036. val_count = 0;
  8037. }];
  8038. [cadedate setObject:@{@"value" : @"None",
  8039. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  8040. val_count++;
  8041. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  8042. [price setObject:cadedate forKey:@"cadedate"];
  8043. [section1 setObject:price forKey:@"item_2"];
  8044. [dic setObject:section1 forKey:@"section_1"];
  8045. return [RAUtils dict2data:dic];
  8046. }
  8047. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  8048. NSString *product_id = [params objectForKey:@"fashionId"];
  8049. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  8050. __block int result = RESULT_TRUE;
  8051. __block int qty = 0;
  8052. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8053. qty = sqlite3_column_int(stmt, 0);
  8054. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  8055. result = RESULT_FALSE;
  8056. }];
  8057. NSMutableDictionary *dic = @{
  8058. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  8059. @"mode" : @"Regular Mode",
  8060. @"quantity_available" : @(qty),
  8061. @"result" : @(result),
  8062. }.mutableCopy;
  8063. return [RAUtils dict2data:dic];
  8064. }
  8065. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  8066. NSString *item_ids = [params objectForKey:@"item_id"];
  8067. NSString *line_notes = [params objectForKey:@"notes"];
  8068. NSString *price_str = [params objectForKey:@"price"];
  8069. NSString *discount_str = [params objectForKey:@"discount"];
  8070. NSString *percent = [params objectForKey:@"available_percent"];
  8071. NSString *qty = [params objectForKey:@"available_qty"];
  8072. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  8073. int dot = 0;
  8074. if (line_notes) {
  8075. line_notes = [self translateSingleQuote:line_notes];
  8076. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  8077. sql = [sql stringByAppendingString:line_notes];
  8078. dot = 1;
  8079. } else {
  8080. line_notes = @"";
  8081. }
  8082. if (price_str) {
  8083. if (dot) {
  8084. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  8085. } else {
  8086. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  8087. dot = 1;
  8088. }
  8089. sql = [sql stringByAppendingString:price_str];
  8090. } else {
  8091. price_str = @"";
  8092. }
  8093. if (discount_str) {
  8094. if (dot) {
  8095. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  8096. } else {
  8097. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  8098. dot = 1;
  8099. }
  8100. sql = [sql stringByAppendingString:discount_str];
  8101. } else {
  8102. discount_str = @"";
  8103. }
  8104. if (percent) {
  8105. if (dot) {
  8106. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  8107. } else {
  8108. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  8109. dot = 1;
  8110. }
  8111. sql = [sql stringByAppendingString:percent];
  8112. } else {
  8113. percent = @"";
  8114. }
  8115. if (qty) {
  8116. if (dot) {
  8117. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  8118. } else {
  8119. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  8120. dot = 1;
  8121. }
  8122. sql = [sql stringByAppendingString:qty];
  8123. } else {
  8124. qty = @"";
  8125. }
  8126. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  8127. sql = [sql stringByAppendingString:where];
  8128. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8129. int result = [iSalesDB execSql:sql];
  8130. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  8131. [dic setObject:@"Regular Mode" forKey:@"mode"];
  8132. return [RAUtils dict2data:dic];
  8133. }
  8134. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  8135. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  8136. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8137. sqlite3 *db = [iSalesDB get_db];
  8138. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set is_delete = 1 where item_id in (%@) and (is_delete is null or is_delete = 0);",portfolio_ids];
  8139. int result = [iSalesDB execSql:sql db:db];
  8140. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  8141. [dic setObject:@"Regular Mode" forKey:@"mode"];
  8142. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  8143. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  8144. [iSalesDB close_db:db];
  8145. return [RAUtils dict2data:dic];
  8146. }
  8147. + (void)offline_removePDFWithName:(NSString *)name {
  8148. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8149. NSString *cachefolder = [paths objectAtIndex:0];
  8150. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  8151. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  8152. NSFileManager *fileManager = [NSFileManager defaultManager];
  8153. [fileManager removeItemAtPath:path error:nil];
  8154. }
  8155. + (void)offline_clear_PDFCache {
  8156. NSFileManager *fileManager = [NSFileManager defaultManager];
  8157. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8158. NSString *cachefolder = [paths objectAtIndex:0];
  8159. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  8160. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  8161. for (NSString *path in pdf_files) {
  8162. [self offline_removePDFWithName:[path lastPathComponent]];
  8163. }
  8164. }
  8165. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  8166. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8167. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  8168. NSString *user = [params objectForKey:@"user"];
  8169. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %d;",tearsheetsId]]; // tearsheets_id ---> _id
  8170. if (![create_user isEqualToString:user]) {
  8171. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  8172. [dic setObject:@"Regular Mode" forKey:@"mode"];
  8173. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  8174. return [RAUtils dict2data:dic];
  8175. }
  8176. NSString *is_local_sql = [NSString stringWithFormat:@"select is_local,pdf_path from offline_pdf where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  8177. __block int is_local = 0;
  8178. __block NSString *path = @"";
  8179. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8180. is_local = sqlite3_column_int(stmt, 0);
  8181. path = [self textAtColumn:1 statement:stmt];
  8182. }];
  8183. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  8184. if (is_local == 1) {
  8185. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  8186. // 删除文件
  8187. [self offline_removePDFWithName:path];
  8188. }
  8189. int result = [iSalesDB execSql:sql];
  8190. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  8191. [dic setObject:@"Regular Mode" forKey:@"mode"];
  8192. return [RAUtils dict2data:dic];
  8193. }
  8194. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial
  8195. {
  8196. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  8197. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  8198. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  8199. NSString* where=@"1=1";
  8200. if (ver!=nil) {
  8201. where=@"is_dirty=1";
  8202. }
  8203. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,name,description,item_id,fashion_id,ifnull(available_qty,'null'),ifnull(percentage,'null'),ifnull(percent,'null'),ifnull(sheet_price,'null'),sheet_discount,img,line_note,is_delete,createtime,modify_time from offline_portfolio where %@",where];
  8204. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  8205. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8206. sqlite3 *db = [iSalesDB get_db];
  8207. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8208. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  8209. NSInteger _id = sqlite3_column_int(stmt, 0);
  8210. NSInteger product_id = sqlite3_column_int(stmt, 1);
  8211. NSString *name = [self textAtColumn:2 statement:stmt];
  8212. NSString *desc = [self textAtColumn:3 statement:stmt];
  8213. NSInteger item_id = sqlite3_column_int(stmt, 4);
  8214. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  8215. NSInteger qty = sqlite3_column_int(stmt, 6);
  8216. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  8217. double percent = sqlite3_column_double(stmt, 8);
  8218. double price = sqlite3_column_double(stmt, 9);
  8219. double discount = sqlite3_column_double(stmt, 10);
  8220. NSString *img = [self textAtColumn:11 statement:stmt];
  8221. NSString *line_note = [self textAtColumn:12 statement:stmt];
  8222. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  8223. NSString *create_time = [self textAtColumn:14 statement:stmt];
  8224. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  8225. NSString *price_null = [self textAtColumn:9 statement:stmt];
  8226. if ([price_null isEqualToString:@"null"]) {
  8227. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  8228. }
  8229. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  8230. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  8231. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  8232. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  8233. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  8234. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  8235. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  8236. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  8237. if ([qty_null isEqualToString:@"null"]) {
  8238. // [item setValue:@"null" forKey:@"available_qty"];
  8239. } else {
  8240. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  8241. }
  8242. if ([is_percent_null isEqualToString:@"null"]) {
  8243. // [item setValue:@"null" forKey:@"percentage"];
  8244. } else {
  8245. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  8246. }
  8247. if ([percent_null isEqualToString:@"null"]) {
  8248. // [item setValue:@"null" forKey:@"percent"];
  8249. } else {
  8250. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  8251. }
  8252. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  8253. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  8254. [item setValue:name forKey:@"name"];
  8255. [item setValue:desc forKey:@"description"];
  8256. [item setValue:img forKey:@"img"];
  8257. [item setValue:line_note forKey:@"line_note"];
  8258. [item setValue:create_time forKey:@"createtime"];
  8259. [item setValue:modify_time forKey:@"modifytime"];
  8260. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  8261. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8262. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  8263. }];
  8264. [iSalesDB close_db:db];
  8265. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  8266. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8267. }
  8268. return ret;
  8269. }
  8270. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  8271. {
  8272. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  8273. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  8274. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  8275. NSString* where=@"1=1";
  8276. if (ver!=nil) {
  8277. where=@"is_dirty=1";
  8278. }
  8279. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,ifnull(tearsheets_id,'null'),pdf_path,create_user,tear_note,tear_name,model_info,createtime,modify_time,configureParams,off_params,is_delete,is_local from offline_pdf where %@",where];
  8280. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  8281. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8282. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  8283. NSInteger _id = sqlite3_column_int(stmt, 0);
  8284. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  8285. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  8286. NSString *create_user = [self textAtColumn:3 statement:stmt];
  8287. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  8288. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  8289. NSString *model_info = [self textAtColumn:6 statement:stmt];
  8290. NSString *createtime = [self textAtColumn:7 statement:stmt];
  8291. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  8292. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  8293. NSString *off_params = [self textAtColumn:10 statement:stmt];
  8294. NSString *uuid = [NSUUID UUID].UUIDString;
  8295. int is_delete = sqlite3_column_int(stmt, 11);
  8296. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  8297. int is_local = sqlite3_column_int(stmt, 12);
  8298. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  8299. if (![tearsheet_id_null isEqualToString:@"null"]) {
  8300. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  8301. }
  8302. [item setObject:pdf_path forKey:@"pdf_path"];
  8303. [item setObject:create_user forKey:@"create_user"];
  8304. [item setObject:tear_note forKey:@"tear_note"];
  8305. [item setObject:tear_name forKey:@"tear_name"];
  8306. [item setObject:model_info forKey:@"model_info"];
  8307. [item setObject:createtime forKey:@"createtime"];
  8308. [item setObject:modifytime forKey:@"modifytime"];
  8309. [item setObject:urlParams forKey:@"urlParams"];
  8310. [item setObject:off_params forKey:@"off_params"];
  8311. [item setObject:uuid forKey:@"pdf_token"];
  8312. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  8313. NSDictionary *off_Params_dic = [RAUtils string2dict:off_params];
  8314. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  8315. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  8316. } else {
  8317. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  8318. }
  8319. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8320. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  8321. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  8322. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8323. }];
  8324. return ret;
  8325. }
  8326. @end