OLDataProvider.m 517 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355
  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. #import "config.h"
  21. @interface OLDataProvider ()
  22. @end
  23. @implementation OLDataProvider
  24. +(NSString*) get_portfolio_price:(NSString*) contact_id item_id:(int) item_id price:(int)price_id db:(sqlite3*)db
  25. {
  26. 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];
  27. NSString* ret=@"No Price.";
  28. sqlite3_stmt * statement;
  29. // int count=0;
  30. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  31. {
  32. if (sqlite3_step(statement) == SQLITE_ROW)
  33. {
  34. char *price = (char*)sqlite3_column_text(statement, 0);
  35. if(price==nil)
  36. price="";
  37. NSString *nsprice = [[NSString alloc]initWithUTF8String:price];
  38. double dp= [nsprice doubleValue];
  39. ret=[NSString stringWithFormat:@"%.2f",dp];
  40. }
  41. sqlite3_finalize(statement);
  42. }
  43. return ret;
  44. // // [iSalesDB close_db:db];
  45. //
  46. // return nil;
  47. //// if(dprice==DBL_MAX)
  48. //// ret= nil;
  49. //// else
  50. //// ret= [NSNumber numberWithDouble:dprice];
  51. //// return ret;
  52. }
  53. + (NSString *)portfolio_img:(NSString *)item_id db:(sqlite3 *)db
  54. {
  55. NSString* ret= nil;
  56. NSString *sqlQuery = nil;
  57. // 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
  58. 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];
  59. sqlite3_stmt * statement;
  60. // int count=0;
  61. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  62. {
  63. if (sqlite3_step(statement) == SQLITE_ROW)
  64. {
  65. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  66. if(imgurl==nil)
  67. imgurl="";
  68. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  69. ret=nsimgurl;
  70. }
  71. sqlite3_finalize(statement);
  72. }
  73. else
  74. {
  75. ret=@"";
  76. }
  77. // [iSalesDB close_db:db];
  78. // DebugLog(@"data string: %@",ret );
  79. return ret;
  80. }
  81. +(NSMutableDictionary*) prepare_portfolio_data:(NSMutableDictionary*) params
  82. {
  83. NSMutableDictionary* values=params[@"replaceValue"];
  84. //生成portfolio pdf需要的数据
  85. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  86. data[@"npd_url"]=@"www.newpacificdirect.com";
  87. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  88. [formatter setDateFormat:@"MMMM yyyy"];
  89. NSString* date = [formatter stringFromDate:[NSDate date]];
  90. data[@"create_month"]=date;
  91. data[@"company_name"]=@"New Pacific Direct,Inc.";
  92. data[@"catalog_name"]=params[@"catalog_name"];
  93. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  94. data[@"TOTAL_PAGE"]=0;
  95. data[@"CURRENT_PAGE"]=0;
  96. NSMutableDictionary* grid = [[NSMutableDictionary alloc]init];
  97. bool show_stockout=[params[@"show_stockout"] boolValue];
  98. NSString *sql = nil;
  99. if(values==nil)
  100. {
  101. 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"]];
  102. }
  103. else
  104. {
  105. 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"]];
  106. }
  107. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  108. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  109. sqlite3 *db = [iSalesDB get_db];
  110. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  111. // [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  112. int product_id = sqlite3_column_int(stmt, 0);
  113. // NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  114. NSString *name = [self textAtColumn:1 statement:stmt];
  115. NSString *description = [self textAtColumn:2 statement:stmt];
  116. double price = sqlite3_column_double(stmt, 3);
  117. NSString *s_price = [self textAtColumn:3 statement:stmt];
  118. double discount = sqlite3_column_double(stmt,4);
  119. int qty = sqlite3_column_int(stmt, 5);
  120. NSString *s_qty = [self textAtColumn:5 statement:stmt];
  121. int is_percentage = sqlite3_column_int(stmt, 6);
  122. NSString *s_is_percentage = [self textAtColumn:6 statement:stmt];
  123. int item_id = sqlite3_column_int(stmt, 7);
  124. // int fashion_id = sqlite3_column_int(stmt, 8);
  125. NSString *line_note = [self textAtColumn:9 statement:stmt];
  126. NSString *s_percent = [self textAtColumn:10 statement:stmt];
  127. double percent = sqlite3_column_double(stmt, 10);
  128. // int portfolio_id = sqlite3_column_int(stmt, 11);
  129. NSString *s_availability = [self textAtColumn:12 statement:stmt];
  130. int availability = sqlite3_column_int(stmt, 12);
  131. NSString *color = [self textAtColumn:13 statement:stmt];
  132. NSString *legcolor = [self textAtColumn:14 statement:stmt];
  133. NSString *demension = [self textAtColumn:15 statement:stmt];
  134. NSString *seat_height = [self textAtColumn:16 statement:stmt];
  135. NSString *material = [self textAtColumn:17 statement:stmt];
  136. NSString *box_dim = [self textAtColumn:18 statement:stmt];
  137. NSString *s_volume = [self textAtColumn:19 statement:stmt];
  138. double volume = sqlite3_column_double(stmt, 19);
  139. NSString *s_weight = [self textAtColumn:20 statement:stmt];
  140. double weight = sqlite3_column_double(stmt, 20);
  141. NSString *s_model_set = [self textAtColumn:21 statement:stmt];
  142. int model_set = sqlite3_column_int(stmt, 21);
  143. NSString *s_load_ability = [self textAtColumn:22 statement:stmt];
  144. double load_ability = sqlite3_column_double(stmt, 22);
  145. NSString *fabric_content = [self textAtColumn:23 statement:stmt];
  146. NSString *assembling = [self textAtColumn:24 statement:stmt];
  147. NSString *made_in = [self textAtColumn:25 statement:stmt];
  148. NSString* gprice = [self get_portfolio_price :appDelegate.contact_id item_id:item_id price:[params[@"regular_price"] intValue] db:db];
  149. if(s_percent.length==0||is_percentage==0)
  150. {
  151. percent=100.0;
  152. if([s_qty isEqualToString:@"null"])
  153. qty=availability;
  154. }
  155. else
  156. {
  157. qty=availability;
  158. }
  159. qty=qty*percent/100+0.5;
  160. for(int i=0;i<[values[@"count"] intValue];i++)
  161. {
  162. NSMutableDictionary * valueitem=values[[NSString stringWithFormat:@"item_%d",i]];;
  163. if([valueitem[@"product_id"] intValue]==product_id)
  164. {
  165. line_note=valueitem[@"line_note"];
  166. qty=[valueitem[@"available_qty"] intValue];
  167. if([[valueitem allKeys] containsObject:@"available_qty"])
  168. s_qty=[NSString stringWithFormat:@"%d",qty];
  169. else
  170. s_qty=@"null";
  171. if(valueitem[@"regular_price"]!=nil)
  172. gprice=[NSString stringWithFormat:@"%.2f",[valueitem[@"regular_price"] floatValue]];
  173. s_price=valueitem[@"special_price"];
  174. price=[valueitem[@"special_price"] floatValue];
  175. discount=0;
  176. }
  177. }
  178. if(is_percentage==0&&qty==0&&!show_stockout)
  179. {
  180. return;
  181. }
  182. if((is_percentage!=0||s_is_percentage.length!=0)&&qty==0&&!show_stockout)
  183. {
  184. return;
  185. }
  186. NSString* set_price=@"";
  187. if([params[@"entered_price"] boolValue])
  188. {
  189. if (s_price==nil ) {
  190. // set_price=[NSString stringWithFormat:@"Special: $ %@",gprice];
  191. }
  192. else
  193. {
  194. if(price*(1-discount/100.0)!=gprice.floatValue)
  195. set_price=[NSString stringWithFormat:@"Special: $ %.2f",price*(1-discount/100.0)];
  196. }
  197. }
  198. NSString* get_price=@"";
  199. {
  200. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  201. // DebugLog(@"price time interval");
  202. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  203. if(gprice==nil)
  204. get_price=@"Price:No Price.";
  205. else
  206. get_price=[NSString stringWithFormat:@"Price: $ %@",gprice];
  207. }
  208. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  209. // if ([qty_null isEqualToString:@"null"]) {
  210. // qty=availability;
  211. // }
  212. //
  213. // NSString *qty_percent_null = [self textAtColumn:6 statement:stmt];
  214. // if ([qty_percent_null isEqualToString:@"null"]) {
  215. // percentage=1;
  216. // }
  217. NSString * str_availability=[NSString stringWithFormat:@"<b>Availability:</b> %d",(int)qty];
  218. NSString * str_color=[NSString stringWithFormat:@"<b>Color:</b> %@",color];
  219. NSString * str_model_set=[NSString stringWithFormat:@"<b>Set:</b> Sold in QTY of %d",model_set];
  220. NSString * str_legcolor=[NSString stringWithFormat:@"<b>Leg Color:</b> %@",legcolor];
  221. NSString * str_demension=[NSString stringWithFormat:@"<b>Dimension:</b> %@",demension];
  222. NSString * str_seat_height=[NSString stringWithFormat:@"<b>Seat Height:</b> %@",seat_height];
  223. NSString * str_meterial=[NSString stringWithFormat:@"<b>Meterial:</b> %@",material];
  224. NSString * str_box_dim=[NSString stringWithFormat:@"<b>Box Dimension:</b> %@",box_dim];
  225. NSString * str_volume=[NSString stringWithFormat:@"<b>Volume:</b> %.2f",volume];
  226. NSString * str_weight=[NSString stringWithFormat:@"<b>Weight:</b> %.2f",weight];
  227. NSString * str_load_ability=[NSString stringWithFormat:@"<b>Load Ability:</b> %.2f",load_ability];
  228. NSString * str_fabric_content=[NSString stringWithFormat:@"<b>Fabric Content:</b> %@",fabric_content];
  229. NSString * str_assembling=[NSString stringWithFormat:@"<b>Assembling:</b> %@",assembling];
  230. NSString * str_made_in=[NSString stringWithFormat:@"<b>Made In:</b> %@",made_in];
  231. NSString * str_line_note=[NSString stringWithFormat:@"<b>Line Note:</b> %@",line_note];
  232. if([params[@"availability"] boolValue]==false)
  233. str_availability=@"";
  234. else
  235. {
  236. if ([s_qty isEqualToString:@"null"] && [s_availability isEqualToString:@"0"])
  237. str_availability=@"<b>Availability:</b> In Production";
  238. }
  239. if([params[@"color"] boolValue]==false || color.length==0 )
  240. str_color=@"";
  241. if([params[@"set"] boolValue]==false || s_model_set.length==0 )
  242. str_model_set=@"";
  243. if([params[@"leg_color"] boolValue]==false || legcolor.length==0 )
  244. str_legcolor=@"";
  245. if([params[@"dimension"] boolValue]==false || demension.length==0 )
  246. str_demension=@"";
  247. if([params[@"seat_height"] boolValue]==false || seat_height.length==0 )
  248. str_seat_height=@"";
  249. if([params[@"material"] boolValue]==false || material.length==0 )
  250. str_meterial=@"";
  251. if([params[@"box_dimension"] boolValue]==false || box_dim.length==0 )
  252. str_box_dim=@"";
  253. if([params[@"volume"] boolValue]==false || s_volume.length==0 )
  254. str_volume=@"";
  255. if([params[@"weight"] boolValue]==false || s_weight.length==0 )
  256. str_weight=@"";
  257. if([params[@"load_ability"] boolValue]==false || s_load_ability.length==0 )
  258. str_load_ability=@"";
  259. if([params[@"fabric_content"] boolValue]==false || fabric_content.length==0 )
  260. str_fabric_content=@"";
  261. if([params[@"assembling"] boolValue]==false || assembling.length==0 )
  262. str_assembling=@"";
  263. if([params[@"made"] boolValue]==false || made_in.length==0 )
  264. str_made_in=@"";
  265. if([params[@"show_line_note"] boolValue]==false || line_note.length==0 )
  266. str_line_note=@"";
  267. NSMutableArray* arr_detail=[[NSMutableArray alloc]init];
  268. [arr_detail addObject:str_availability];
  269. [arr_detail addObject:str_color];
  270. [arr_detail addObject:str_model_set];
  271. [arr_detail addObject:str_legcolor];
  272. [arr_detail addObject:str_demension];
  273. [arr_detail addObject:str_seat_height];
  274. [arr_detail addObject:str_meterial];
  275. [arr_detail addObject:str_box_dim];
  276. [arr_detail addObject:str_volume];
  277. [arr_detail addObject:str_weight];
  278. [arr_detail addObject:str_load_ability];
  279. [arr_detail addObject:str_fabric_content];
  280. [arr_detail addObject:str_assembling];
  281. [arr_detail addObject:str_made_in];
  282. [arr_detail addObject:str_line_note];
  283. // NSString* detail =[NSString stringWithFormat:@"<font size=0.2>%@</font>",[RAUtils arr2string:arr_detail separator:@" " trim:true]];
  284. NSString* detail =[RAUtils arr2string:arr_detail separator:@" " trim:true];
  285. //model image;
  286. NSString* img_path = [self portfolio_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  287. // qr image
  288. NSString* qrpath=nil;
  289. if([params[@"show_barcode"] boolValue])
  290. {
  291. NSString* temp = NSTemporaryDirectory();
  292. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  293. qrpath=[temp stringByAppendingPathComponent:filename];
  294. UIImage* qrimg=[QRCodeGenerator qrImageForString:name imageSize:240];
  295. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  296. }
  297. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  298. item[@"img"]=img_path;
  299. item[@"img_qr"]=qrpath;
  300. item[@"special_price"]=set_price;
  301. item[@"price"]=get_price;
  302. item[@"name"]=name;
  303. item[@"description"]=description;
  304. item[@"detail"]=detail;
  305. item[@"img"]=img_path;
  306. // @{
  307. // //@"linenotes": line_note,
  308. //// @"product_id": product_id_string,
  309. //// @"available_qty": @(qty),
  310. //// @"available_percent" : @(percent),
  311. //// @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  312. //// @"item_id": [NSString stringWithFormat:@"%d",item_id],
  313. //// @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  314. // @"": ,
  315. // @"": ,
  316. // @"": ,
  317. // @"": ,
  318. // @"": ,
  319. // @"": ,
  320. // @"":
  321. // }.mutableCopy;
  322. // if (percentage) {
  323. // [item removeObjectForKey:@"available_qty"];
  324. // } else {
  325. // [item removeObjectForKey:@"available_percent"];
  326. // }
  327. //
  328. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  329. // if ([qty_null isEqualToString:@"null"]) {
  330. // [item removeObjectForKey:@"available_qty"];
  331. // }
  332. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  333. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  334. }];
  335. [iSalesDB close_db:db];
  336. // [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  337. // [dic setValue:@"" forKey:@"email_content"];
  338. // [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  339. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  340. //
  341. // NSMutableDictionary* cell = [[NSMutableDictionary alloc]init];
  342. // cell[@"count"]=[NSNumber numberWithInt:10];
  343. grid[@"cell0"]=dic;
  344. data[@"grid0"]=grid;
  345. return data;
  346. }
  347. +(NSMutableDictionary*) get_pdftemplate:(NSString*) template
  348. {
  349. NSString *default_path = [[NSBundle mainBundle] pathForResource:template ofType:nil];
  350. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  351. NSString *cache_folder=[paths objectAtIndex:0];
  352. NSString *template_path = [cache_folder stringByAppendingPathComponent:[NSString stringWithFormat:@"offline_data/%@",template]];
  353. BOOL bdir=NO;
  354. NSFileManager* fileManager = [NSFileManager defaultManager];
  355. if(PDF_DEBUG)
  356. {
  357. NSData *data = [NSData dataWithContentsOfFile:default_path];
  358. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  359. return ret;
  360. }
  361. if(! [fileManager fileExistsAtPath:template_path isDirectory:&bdir] )
  362. {
  363. NSError * error=nil;
  364. if(![fileManager copyItemAtPath:default_path toPath:template_path error:&error])
  365. {
  366. return nil;
  367. }
  368. }
  369. NSData *data = [NSData dataWithContentsOfFile:template_path];
  370. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  371. return ret;
  372. }
  373. +(NSMutableDictionary*) query_login:(NSString*) user db:(sqlite3*) db
  374. {
  375. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  376. 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,first_name from offline_login where lower(username)='%@' ",user.lowercaseString ];
  377. DebugLog(@"offline_login sql:%@",sqlQuery);
  378. sqlite3_stmt * statement;
  379. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  380. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  381. {
  382. if (sqlite3_step(statement) == SQLITE_ROW)
  383. {
  384. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  385. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  386. int can_show_price = sqlite3_column_int(statement, 0);
  387. int can_see_price = sqlite3_column_int(statement, 1);
  388. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  389. if(contact_id==nil)
  390. contact_id="";
  391. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  392. int user_type = sqlite3_column_int(statement, 3);
  393. int can_cancel_order = sqlite3_column_int(statement, 4);
  394. int can_set_cart_price = sqlite3_column_int(statement, 5);
  395. int can_create_portfolio = sqlite3_column_int(statement, 6);
  396. int can_delete_order = sqlite3_column_int(statement, 7);
  397. int can_submit_order = sqlite3_column_int(statement, 8);
  398. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  399. int can_create_order = sqlite3_column_int(statement, 10);
  400. char *mode = (char*)sqlite3_column_text(statement, 11);
  401. if(mode==nil)
  402. mode="";
  403. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  404. char *username = (char*)sqlite3_column_text(statement, 12);
  405. if(username==nil)
  406. username="";
  407. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  408. int can_update_contact_info = sqlite3_column_int(statement, 13);
  409. char *first_name = (char*)sqlite3_column_text(statement, 14);
  410. if(first_name==nil)
  411. first_name="";
  412. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  413. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  414. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  415. // [header setValue:nscontact_id forKey:@"contact_id"];
  416. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  417. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  418. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  419. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  420. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  421. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  422. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  423. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  424. //
  425. // [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  426. //
  427. // [header setValue:nsusername forKey:@"username"];
  428. //
  429. // NSError* error=nil;
  430. //
  431. // NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  432. // [header setValue:statusFilter forKey:@"statusFilter"];
  433. //
  434. //
  435. //
  436. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  437. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  438. //
  439. // // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  440. // // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  441. // int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  442. // [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  443. //
  444. // [ret setObject:header forKey:@"header"];
  445. [ret setValue:nsfirst_name forKey:@"first_name"];
  446. }
  447. sqlite3_finalize(statement);
  448. }
  449. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  450. return ret;
  451. }
  452. +(NSMutableDictionary*) prepare_salesorder_data:(NSString*) order_code type:(int) type
  453. {
  454. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  455. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  456. data[@"npd_url"]=@"www.newpacificdirect.com";
  457. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  458. [formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
  459. NSString* date = [formatter stringFromDate:[NSDate date]];
  460. data[@"print_date"]=date;
  461. data[@"company_name"]=@"New Pacific Direct,Inc.";
  462. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  463. data[@"TOTAL_PAGE"]=0;
  464. data[@"CURRENT_PAGE"]=0;
  465. // [formatter setDateFormat:@"MM/dd/yyyy"];
  466. // date = [formatter stringFromDate:[NSDate date]];
  467. // data[@"creat_date"]=date;
  468. NSString* temp = NSTemporaryDirectory();
  469. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  470. NSString* qrpath=[temp stringByAppendingPathComponent:filename];
  471. UIImage* qrimg=[RAUtils generateBarCode:order_code width:480 height:44];//[QRCodeGenerator qrImageForString:order_code imageSize:240];
  472. bool bwrite=[UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  473. data[@"img_barcode"]=qrpath;
  474. data[@"so_id"]=order_code;
  475. data[@"company"]=@"NEW PACIFIC DIRECT, INC.";
  476. // 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 so_id=%@) o left join offline_contact c on o.customer_cid=c.contact_id",order_code];
  477. NSString *sql = [NSString stringWithFormat:@"select erpOrderStatus,sender_name,sender_ext,sender_phone,sender_fax,sender_email,warehouse_name,billing_name,billing_ext,billing_phone,billing_fax,billing_email,receive_name,receive_ext,receive_phone,receive_fax,receive_email,poNumber,sales_rep,carrier,vendor_no,terms,sales_terms,port_destination,etd,sign_picpath,status,create_time,shipping,lift_gate_value,handling_fee_value,billing_contact,receive_contact,general_notes,customer_cid from offline_order where so_id='%@'",order_code];
  478. sqlite3 *db = [iSalesDB get_db];
  479. data[@"print_by"]= [self query_login:appDelegate.user db:db][@"first_name"];
  480. sqlite3_stmt * statement;
  481. NSMutableDictionary* datasource = [[NSMutableDictionary alloc]init];
  482. NSMutableDictionary* row0 = [[NSMutableDictionary alloc]init];
  483. row0[@"count"]=[NSNumber numberWithInt:1];
  484. datasource[@"row0"]=row0;
  485. NSString* print_status=@"";
  486. NSString* order_type=@"Offline Quote";
  487. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  488. {
  489. if (sqlite3_step(statement) == SQLITE_ROW)
  490. {
  491. int erpOrderStatus = sqlite3_column_int(statement, 0);
  492. int status = sqlite3_column_int(statement, 26);
  493. if(status==2)
  494. {
  495. switch (erpOrderStatus) {
  496. case 10:
  497. // order_type=@"Quoted Order";
  498. print_status=@"Quote";
  499. break;
  500. case 11:
  501. print_status=@"Booked";
  502. break;
  503. case 12:
  504. print_status=@"Picked";
  505. break;
  506. case 13:
  507. print_status=@"Shipped";
  508. break;
  509. case 14:
  510. print_status=@"Closed";
  511. break;
  512. case 15:
  513. print_status=@"Canceled";
  514. break;
  515. case 16:
  516. print_status=@"All";
  517. break;
  518. case 18:
  519. print_status=@"Processing";
  520. break;
  521. default:
  522. break;
  523. }
  524. }
  525. else{
  526. switch (status) {
  527. case 0:
  528. {
  529. print_status=@"Quote";
  530. break;
  531. }
  532. case 1:
  533. {
  534. print_status=@"Saved";
  535. break;
  536. }
  537. case 3:
  538. {
  539. //canceled
  540. print_status=@"Canceled";
  541. break;
  542. }
  543. default:
  544. break;
  545. }
  546. }
  547. NSString* sender_name = [self textAtColumn:1 statement:statement];
  548. NSString* sender_ext = [self textAtColumn:2 statement:statement];
  549. NSString* sender_phone = [self textAtColumn:3 statement:statement];
  550. NSString* sender_fax = [self textAtColumn:4 statement:statement];
  551. NSString* sender_email = [self textAtColumn:5 statement:statement];
  552. NSString* warehouse_name = [self textAtColumn:6 statement:statement];
  553. NSString* billing_name = [self textAtColumn:7 statement:statement];
  554. NSString* billing_ext = [self textAtColumn:8 statement:statement];
  555. NSString* billing_phone = [self textAtColumn:9 statement:statement];
  556. if(billing_phone.length>0)
  557. billing_phone= [@"TEL: " stringByAppendingString:billing_phone];
  558. NSString* billing_fax = [self textAtColumn:10 statement:statement];
  559. if(billing_fax.length>0)
  560. billing_fax= [@"FAX: " stringByAppendingString:billing_fax];
  561. NSMutableArray* billto_arr0 = [[NSMutableArray alloc]init];
  562. [billto_arr0 addObject:billing_phone];
  563. [billto_arr0 addObject:billing_fax];
  564. NSString* billing_pf=[RAUtils arr2string:billto_arr0 separator:@" " trim:true];
  565. NSString* billing_email = [self textAtColumn:11 statement:statement];
  566. if(billing_email.length>0)
  567. billing_email= [@"EMAIL: " stringByAppendingString:billing_email];
  568. NSString* billing_contact = [self textAtColumn:31 statement:statement];
  569. if(billing_contact.length>0)
  570. billing_contact= [@"ATTN: " stringByAppendingString:billing_contact];
  571. NSString* receive_name = [self textAtColumn:12 statement:statement];
  572. NSString* receive_ext = [self textAtColumn:13 statement:statement];
  573. NSString* receive_phone = [self textAtColumn:14 statement:statement];
  574. if(receive_phone.length>0)
  575. receive_phone= [@"TEL: " stringByAppendingString:receive_phone];
  576. NSString* receive_fax = [self textAtColumn:15 statement:statement];
  577. if(receive_fax.length>0)
  578. receive_fax= [@"FAX: " stringByAppendingString:receive_fax];
  579. NSMutableArray* receive_arr0 = [[NSMutableArray alloc]init];
  580. [receive_arr0 addObject:receive_phone];
  581. [receive_arr0 addObject:receive_fax];
  582. NSString* receive_email = [self textAtColumn:16 statement:statement];
  583. NSString* receive_pf=[RAUtils arr2string:billto_arr0 separator:@" " trim:true];
  584. if(receive_email.length>0)
  585. receive_email= [@"EMAIL: " stringByAppendingString:receive_email];
  586. NSString* receive_contact = [self textAtColumn:32 statement:statement];
  587. if(receive_contact.length>0)
  588. receive_contact= [@"ATTN: " stringByAppendingString:receive_contact];
  589. NSString* poNumber = [self textAtColumn:17 statement:statement];
  590. NSString* sales_rep = [self textAtColumn:18 statement:statement];
  591. NSString* carrier = [self textAtColumn:19 statement:statement];
  592. NSString* vendor_no = [self textAtColumn:20 statement:statement];
  593. NSString* terms = [self textAtColumn:21 statement:statement];
  594. NSString* sales_terms = [self textAtColumn:22 statement:statement];
  595. NSString* port_destination = [self textAtColumn:23 statement:statement];
  596. NSString* etd = [self textAtColumn:24 statement:statement];
  597. NSString* sign_picpath = [self textAtColumn:25 statement:statement];
  598. if(sign_picpath.length==0)
  599. sign_picpath=nil;
  600. NSString* create_date = [self textAtColumn:27 statement:statement];
  601. create_date=[self changeDateTime:create_date Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy"];
  602. NSString* general_notes=[self textAtColumn:33 statement:statement];
  603. NSString *customerID = [self textAtColumn:34 statement:statement];
  604. if (customerID.length == 0) {
  605. customerID = nil;
  606. }
  607. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  608. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  609. if(general_notes.length>0)
  610. {
  611. NSMutableDictionary* row2 = [[NSMutableDictionary alloc]init];
  612. row2[@"item_0"]=@{@"g_notes":general_notes };
  613. row2[@"count"]=[NSNumber numberWithInt:1];
  614. datasource[@"row2"]=row2;
  615. }
  616. NSMutableArray* billto_arr = [[NSMutableArray alloc]init];
  617. [billto_arr addObject:billing_ext];
  618. [billto_arr addObject:billing_pf];
  619. [billto_arr addObject:billing_contact];
  620. [billto_arr addObject:billing_email];
  621. NSString* billto=[RAUtils arr2string:billto_arr separator:@"\r\n" trim:true];
  622. NSMutableArray* receive_arr = [[NSMutableArray alloc]init];
  623. [receive_arr addObject:receive_ext];
  624. [receive_arr addObject:receive_pf];
  625. [receive_arr addObject:receive_contact];
  626. [receive_arr addObject:receive_email];
  627. NSString* receive=[RAUtils arr2string:receive_arr separator:@"\r\n" trim:true];
  628. float shipping = sqlite3_column_double(statement, 28);
  629. float lift_gate_value = sqlite3_column_double(statement, 29);
  630. float handling_fee_value = sqlite3_column_double(statement, 30);
  631. data[@"company"]=sender_name;
  632. data[@"shipfrom_addr"]=sender_ext;
  633. data[@"shipfrom_phone"]=sender_phone;
  634. data[@"shipfrom_fax"]=sender_fax;
  635. data[@"shipfrom_email"]=sender_email;
  636. data[@"warehouse_name"]=warehouse_name;
  637. data[@"bill_to_name"]=billing_name;
  638. data[@"bill_to_ext"]=billto;
  639. data[@"ship_to_name"]=receive_name;
  640. data[@"ship_to_ext"]=receive;
  641. data[@"po_no"]=poNumber;
  642. data[@"rep"]=sales_rep;
  643. data[@"e_schdule"]=etd;
  644. data[@"sales_terms"]=sales_terms;
  645. data[@"port_destination"]=port_destination;
  646. data[@"terms"]=terms;
  647. data[@"carrier"]=carrier;
  648. data[@"vendor_no"]=vendor_no;
  649. data[@"customer_sign"]=sign_picpath;
  650. data[@"print_status"]=print_status;
  651. data[@"create_date"]=create_date;
  652. NSString *sql_cart=[ 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 ",order_code];;
  653. sqlite3_stmt * statement_cart;
  654. // NSDate *date1 = [NSDate date];
  655. int count=0;
  656. int cart_count=0;
  657. float credits=0;
  658. int dbresult=sqlite3_prepare_v2(db, [sql_cart UTF8String], -1, &statement_cart, nil);
  659. if ( dbresult== SQLITE_OK)
  660. {
  661. NSMutableDictionary* row1 = [[NSMutableDictionary alloc]init];
  662. int row1count=0;
  663. while (sqlite3_step(statement_cart) == SQLITE_ROW)
  664. {
  665. NSDate *row_date = [NSDate date];
  666. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  667. // int product_id = sqlite3_column_int(statement_cart, 0);
  668. char *str_price = (char*)sqlite3_column_text(statement_cart, 1);
  669. int item_id = sqlite3_column_int(statement_cart, 7);
  670. double discount = sqlite3_column_double(statement_cart, 2);
  671. NSString* Price=nil;
  672. if(str_price==nil)
  673. {
  674. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  675. // NSDate *price_date = [NSDate date];
  676. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db];
  677. // DebugLog(@"price time interval");
  678. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  679. if(price==nil)
  680. Price=@"No Price.";
  681. else
  682. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  683. }
  684. else
  685. {
  686. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  687. }
  688. float dunitprice=0;
  689. int item_count = sqlite3_column_int(statement_cart, 3);
  690. NSString* Amount=@"";
  691. if(![Price isEqualToString:@"No Price."])
  692. {
  693. dunitprice=[Price floatValue];
  694. dunitprice=dunitprice* (1-discount/100.0);
  695. Price=[NSString stringWithFormat:@"%.2f",dunitprice];
  696. credits+=dunitprice*item_count;
  697. Amount=[NSString stringWithFormat:@"%.2f",dunitprice*item_count];
  698. }
  699. else
  700. {
  701. Price=@"";
  702. }
  703. // char *line_note = (char*)sqlite3_column_text(statement_cart, 4);
  704. // NSString *nsline_note=nil;
  705. // if(line_note!=nil)
  706. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  707. char *name = (char*)sqlite3_column_text(statement_cart, 5);
  708. NSString *nsname=nil;
  709. if(name!=nil)
  710. nsname= [[NSString alloc]initWithUTF8String:name];
  711. char *description = (char*)sqlite3_column_text(statement_cart, 6);
  712. NSString *nsdescription=nil;
  713. if(description!=nil)
  714. nsdescription= [[NSString alloc]initWithUTF8String:description];
  715. // int stockUom = sqlite3_column_int(statement_cart, 8);
  716. // int _id = sqlite3_column_int(statement_cart, 9);
  717. // NSDate *subtotal_date = [NSDate date];
  718. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  719. // DebugLog(@"subtotal_date time interval");
  720. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  721. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  722. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  723. // int carton=[bsubtotaljson[@"carton"] intValue];
  724. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  725. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  726. // NSDate *img_date = [NSDate date];
  727. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  728. // DebugLog(@"img_date time interval");
  729. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  730. itemjson[@"model"]=nsname;
  731. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  732. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  733. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  734. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  735. // itemjson[@"check"]=@"true";
  736. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  737. itemjson[@"count"]=[NSString stringWithFormat:@"%d",item_count];
  738. itemjson[@"unit_price"]=Price;
  739. itemjson[@"amount"]=Amount;
  740. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  741. itemjson[@"description"]=[NSString stringWithFormat:@"%@",nsdescription ];
  742. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  743. // itemjson[@"note"]=nsline_note;
  744. // NSDate *date2 = [NSDate date];
  745. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  746. // DebugLog(@"model_bundle time interval");
  747. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  748. itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  749. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=itemjson;
  750. row1count++;
  751. NSDictionary* combine =[self model_bundle:item_id db:db compute_part:false contactID:customerID];
  752. for(int c=0;c<[combine[@"count"] intValue];c++)
  753. {
  754. NSMutableDictionary* combine_item=[combine[[NSString stringWithFormat:@"item_%d",c]] mutableCopy];
  755. combine_item[@"count"]=[NSString stringWithFormat:@"%d",[combine_item[@"modulus"] intValue]*item_count];
  756. // combine_item[@"count"]=[NSNumber numberWithInt:[combine_item[@"modulus"] intValue]*item_count];
  757. double combine_amount=[combine_item[@"modulus"] intValue]*item_count* [combine_item[@"unit_price"] floatValue];
  758. credits+=combine_amount;
  759. combine_item[@"amount"]=[NSString stringWithFormat:@"%.2f",combine_amount];
  760. combine_item[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,c+2];
  761. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=combine_item;
  762. row1count++;
  763. }
  764. count++;
  765. // DebugLog(@"row time interval");
  766. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  767. }
  768. row1[@"count"]=[NSNumber numberWithInt:row1count];
  769. datasource[@"row1"]=row1;
  770. data[@"table_model"]=datasource;
  771. sqlite3_finalize(statement_cart);
  772. }
  773. NSMutableDictionary* fee = [[NSMutableDictionary alloc]init];
  774. NSMutableDictionary* fee_row0 = [[NSMutableDictionary alloc]init];
  775. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  776. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  777. // if(credits>0)
  778. fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  779. // else
  780. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": @""};
  781. if(shipping>0)
  782. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": [NSString stringWithFormat:@"%.2f",shipping]};
  783. else
  784. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": @""};
  785. if(lift_gate_value>0)
  786. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",lift_gate_value]};
  787. else
  788. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": @""};
  789. if(handling_fee_value>0)
  790. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",handling_fee_value]};
  791. else
  792. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": @""};
  793. float total = credits+shipping+lift_gate_value+handling_fee_value;
  794. if(total>0)
  795. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": [NSString stringWithFormat:@"%.2f",total]};
  796. else
  797. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": @""};
  798. fee_row0[@"count"]=[NSNumber numberWithInt:5];
  799. fee[@"row0"]=fee_row0;
  800. data[@"table_fee"]=fee;
  801. // NSString *sign_url = [self textAtColumn:51 statement:statement];
  802. // ret[@"sign_url"] = sign_url;
  803. //
  804. // customer_contact = [self textAtColumn:52 statement:statement];
  805. // customer_email = [self textAtColumn:53 statement:statement];
  806. // customer_phone = [self textAtColumn:54 statement:statement];
  807. // customer_fax = [self textAtColumn:55 statement:statement];
  808. //
  809. //
  810. // int offline_edit=sqlite3_column_int(statement, 56);
  811. //
  812. // ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  813. //
  814. // char *soid = (char*)sqlite3_column_text(statement, 1);
  815. // if(soid==nil)
  816. // soid= "";
  817. // nssoid= [[NSString alloc]initWithUTF8String:soid];
  818. // // so#
  819. // ret[@"so#"] = nssoid;
  820. //
  821. // char *poNumber = (char*)sqlite3_column_text(statement, 2);
  822. // if(poNumber==nil)
  823. // poNumber= "";
  824. // NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  825. //
  826. //
  827. //
  828. // char *create_time = (char*)sqlite3_column_text(statement, 3);
  829. // if(create_time==nil)
  830. // create_time= "";
  831. // NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  832. // nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  833. //
  834. // int status = sqlite3_column_int(statement, 4);
  835. // int erpStatus = sqlite3_column_int(statement, 49);
  836. // NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  837. //
  838. // // status
  839. // if (status > 1 && status != 3) {
  840. // status = erpStatus;
  841. // } else if (status == 3) {
  842. // status = 15;
  843. // }
  844. // ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  845. // ret[@"order_status"] = nsstatus;
  846. //
  847. //
  848. // char *company_name = (char*)sqlite3_column_text(statement, 5);
  849. // if(company_name==nil)
  850. // company_name= "";
  851. // NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  852. // // company name
  853. // ret[@"company_name"] = nscompany_name;
  854. //
  855. // char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  856. // if(customer_contact==nil)
  857. // customer_contact= "";
  858. // NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  859. //
  860. //
  861. // char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  862. // if(addr_1==nil)
  863. // addr_1="";
  864. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  865. //
  866. // char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  867. // if(addr_2==nil)
  868. // addr_2="";
  869. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  870. //
  871. //
  872. // char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  873. // if(addr_3==nil)
  874. // addr_3="";
  875. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  876. //
  877. //
  878. // char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  879. // if(addr_4==nil)
  880. // addr_4="";
  881. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  882. //
  883. // NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  884. // [arr_addr addObject:nsaddr_1];
  885. // [arr_addr addObject:nsaddr_2];
  886. // [arr_addr addObject:nsaddr_3];
  887. // [arr_addr addObject:nsaddr_4];
  888. // NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  889. //
  890. //
  891. // char *logist = (char*)sqlite3_column_text(statement, 11);
  892. // if(logist==nil)
  893. // logist= "";
  894. // NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  895. // if (status == -11 || status == 10 || status == 11) {
  896. // nslogist = [self textAtColumn:59 statement:statement];
  897. // };
  898. //
  899. // NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  900. // if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  901. // shipping = @"Shipping To Be Quoted";
  902. // } else {
  903. // shippingFee = sqlite3_column_double(statement, 12);
  904. // }
  905. //
  906. // // Shipping
  907. // ret[@"Shipping"] = shipping;
  908. //
  909. // int have_lift_gate = sqlite3_column_int(statement, 17);
  910. // lift_gate = sqlite3_column_double(statement, 13);
  911. // // Liftgate Fee(No loading dock)
  912. // if (!have_lift_gate) {
  913. // lift_gate = 0;
  914. // }
  915. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  916. // if (sqlite3_column_int(statement, 57)) {
  917. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  918. // }
  919. //
  920. // char *general_notes = (char*)sqlite3_column_text(statement, 14);
  921. // if(general_notes==nil)
  922. // general_notes= "";
  923. // NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  924. //
  925. // char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  926. // if(internal_notes==nil)
  927. // internal_notes= "";
  928. // NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  929. //
  930. // char *payment_type = (char*)sqlite3_column_text(statement, 16);
  931. // if(payment_type==nil)
  932. // payment_type= "";
  933. // NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  934. //
  935. //
  936. //
  937. // // order info
  938. // orderinfo = [self textFileName:@"order_info.html"];
  939. //
  940. //
  941. // orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  942. // orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  943. // orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  944. // orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  945. //
  946. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  947. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  948. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  949. // orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  950. // orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  951. // orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  952. // orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  953. // orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  954. //
  955. // NSString *payment = nil;
  956. // if([nspayment_type isEqualToString:@"Credit Card"])
  957. // {
  958. // payment = [self textFileName:@"creditcardpayment.html"];
  959. //
  960. // NSString *card_first_name = [self textAtColumn:37 statement:statement];
  961. // NSString *card_last_name = [self textAtColumn:38 statement:statement];
  962. // NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  963. // NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  964. // NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  965. // NSString *card_type = [self textAtColumn:42 statement:statement];
  966. // if (card_type.length > 0) { // 显示星号
  967. // card_type = @"****";
  968. // }
  969. // NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  970. // if (card_number.length > 0 && card_number.length > 4) {
  971. // for (int i = 0; i < card_number.length - 4; i++) {
  972. // card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  973. // }
  974. // } else {
  975. // card_number = @"";
  976. // }
  977. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  978. // NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  979. // NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  980. // NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  981. // if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  982. // card_expiration = @"****";
  983. // }
  984. //
  985. //
  986. // NSString *card_city = [self textAtColumn:46 statement:statement];
  987. // NSString *card_state = [self textAtColumn:47 statement:statement];
  988. //
  989. // payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  990. // payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  991. // payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  992. // payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  993. // payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  994. // payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  995. // payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  996. // payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  997. // payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  998. // payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  999. //
  1000. //
  1001. // }
  1002. // else
  1003. // {
  1004. // payment=[self textFileName:@"normalpayment.html"];
  1005. // payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  1006. // }
  1007. // orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  1008. //
  1009. // ret[@"result"]= [NSNumber numberWithInt:2];
  1010. //
  1011. // // more info
  1012. // moreInfo = [self textFileName:@"more_info.html"];
  1013. //
  1014. //
  1015. // // ShipToCompany_or_&nbsp
  1016. // NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  1017. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  1018. // NSString *shipToName = [self textAtColumn:19 statement:statement];
  1019. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  1020. // NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  1021. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  1022. //
  1023. //
  1024. // // ShipFromCompany_or_&nbsp
  1025. // NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  1026. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  1027. // NSString *shipFromName = [self textAtColumn:22 statement:statement];
  1028. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  1029. // NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  1030. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  1031. //
  1032. //
  1033. // // FreightBillToCompany_or_&nbsp
  1034. // NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  1035. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  1036. // NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  1037. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  1038. // NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  1039. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  1040. //
  1041. //
  1042. // // MerchandiseBillToCompany_or_&nbsp
  1043. // NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  1044. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  1045. // NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  1046. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  1047. // NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  1048. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  1049. //
  1050. //
  1051. // // ReturnToCompany_or_&nbsp
  1052. // NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  1053. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  1054. // NSString *returnToName = [self textAtColumn:31 statement:statement];
  1055. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  1056. // NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  1057. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  1058. // //
  1059. //
  1060. // // DebugLog(@"more info : %@",moreInfo);
  1061. //
  1062. // // handling fee
  1063. // handlingFee = sqlite3_column_double(statement, 33);
  1064. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  1065. // if (sqlite3_column_int(statement, 58)) {
  1066. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  1067. // }
  1068. // //
  1069. //
  1070. // // customer info
  1071. // customerID = [self textAtColumn:36 statement:statement];
  1072. //
  1073. // // mode
  1074. // ret[@"mode"] = appDelegate.mode;
  1075. //
  1076. // // model_count
  1077. // ret[@"model_count"] = @(0);
  1078. }
  1079. sqlite3_finalize(statement);
  1080. }
  1081. [iSalesDB close_db:db];
  1082. data[@"order_type"]=order_type;
  1083. /*
  1084. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  1085. // "customer_email" = "Shui Hu";
  1086. // "customer_fax" = "";
  1087. // "customer_first_name" = F;
  1088. // "customer_last_name" = L;
  1089. // "customer_name" = ",da He Xiang Dong Liu A";
  1090. // "customer_phone" = "Hey Xuan Feng";
  1091. contactInfo[@"customer_phone"] = customer_phone;
  1092. contactInfo[@"customer_fax"] = customer_fax;
  1093. contactInfo[@"customer_email"] = customer_email;
  1094. NSString *first_name = @"";
  1095. NSString *last_name = @"";
  1096. if ([customer_contact isEqualToString:@""]) {
  1097. } else if ([customer_contact containsString:@" "]) {
  1098. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  1099. first_name = [customer_contact substringToIndex:first_space_index];
  1100. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  1101. }
  1102. contactInfo[@"customer_first_name"] = first_name;
  1103. contactInfo[@"customer_last_name"] = last_name;
  1104. ret[@"customerInfo"] = contactInfo;
  1105. // models
  1106. if (nssoid) {
  1107. __block double TotalCuft = 0;
  1108. __block double TotalWeight = 0;
  1109. __block int TotalCarton = 0;
  1110. __block double allItemPrice = 0;
  1111. 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];
  1112. sqlite3 *db1 = [iSalesDB get_db];
  1113. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1114. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1115. int product_id = sqlite3_column_int(stmt, 0);
  1116. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  1117. int item_id = sqlite3_column_int(stmt, 7);
  1118. NSString* Price=nil;
  1119. if(str_price==nil)
  1120. {
  1121. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  1122. if(price==nil)
  1123. Price=@"No Price.";
  1124. else
  1125. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1126. }
  1127. else
  1128. {
  1129. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1130. }
  1131. double discount = sqlite3_column_double(stmt, 2);
  1132. int item_count = sqlite3_column_int(stmt, 3);
  1133. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  1134. NSString *nsline_note=nil;
  1135. if(line_note!=nil)
  1136. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1137. // char *name = (char*)sqlite3_column_text(stmt, 5);
  1138. // NSString *nsname = nil;
  1139. // if(name!=nil)
  1140. // nsname= [[NSString alloc]initWithUTF8String:name];
  1141. NSString *nsname = [self textAtColumn:5 statement:stmt];
  1142. // char *description = (char*)sqlite3_column_text(stmt, 6);
  1143. // NSString *nsdescription=nil;
  1144. // if(description!=nil)
  1145. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  1146. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  1147. // int stockUom = sqlite3_column_int(stmt, 8);
  1148. // int _id = sqlite3_column_int(stmt, 9);
  1149. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  1150. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1151. double weight=[bsubtotaljson[@"weight"] doubleValue];
  1152. int carton=[bsubtotaljson[@"carton"] intValue];
  1153. TotalCuft += cuft;
  1154. TotalWeight += weight;
  1155. TotalCarton += carton;
  1156. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  1157. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  1158. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  1159. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1160. itemjson[@"note"]=nsline_note;
  1161. itemjson[@"origin_price"] = Price;
  1162. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1163. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1164. itemjson[@"order_item_status"] = @""; // 暂时不处理
  1165. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  1166. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  1167. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  1168. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  1169. if(itemjson[@"combine"] != nil)
  1170. {
  1171. // int citem=0;
  1172. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  1173. for(int bc=0;bc<bcount;bc++)
  1174. {
  1175. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  1176. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  1177. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  1178. subTotal += uprice * modulus * item_count;
  1179. }
  1180. }
  1181. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  1182. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  1183. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  1184. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  1185. allItemPrice += subTotal;
  1186. }];
  1187. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  1188. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  1189. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  1190. // payments/Credits
  1191. // payments_and_credist = sqlite3_column_double(statement, 34);
  1192. payments_and_credist = allItemPrice;
  1193. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1194. // // total
  1195. // totalPrice = sqlite3_column_double(statement, 35);
  1196. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1197. } else {
  1198. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  1199. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  1200. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  1201. // payments/Credits
  1202. payments_and_credist = 0;
  1203. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1204. }
  1205. // total
  1206. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  1207. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1208. ret[@"order_info"]= orderinfo;
  1209. ret[@"more_order_info"] = moreInfo;
  1210. return [RAUtils dict2data:ret];
  1211. */
  1212. DebugLog(@"debug sales order data:%@", [RAUtils dict2string:data]);
  1213. return data;
  1214. }
  1215. +(NSData*) offline_request_salesorder :(NSMutableDictionary *) params
  1216. {
  1217. NSString* template_file=nil;
  1218. switch([params[@"thumb"] intValue])
  1219. {
  1220. case 0:
  1221. template_file= @"so.json";
  1222. break;
  1223. case 1:
  1224. template_file= @"so_thumb.json";
  1225. break;
  1226. default:
  1227. template_file= @"so.json";
  1228. }
  1229. NSMutableDictionary* data = [self prepare_salesorder_data:params[@"order_code"] type:[params[@"thumb"] intValue]];
  1230. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1231. if(data[@"table_model"][@"row2"]==nil)
  1232. template[@"pages"][@"page_0"][@"contents"][@"item_0"][@"rows"][@"count"]=[NSNumber numberWithInt:1];
  1233. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1234. DebugLog(@"%@",file);
  1235. // return nil;
  1236. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1237. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1238. if (file) {
  1239. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1240. [dic setObject:file forKey:@"pdf_path"];
  1241. dic[@"isLocalFile"]=@"true";
  1242. return [RAUtils dict2data:dic];
  1243. }
  1244. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1245. dic[@"isLocalFile"]=@"true";
  1246. return [RAUtils dict2data:dic];
  1247. }
  1248. +(NSData*) offline_request_tearsheet :(NSMutableDictionary *) params
  1249. {
  1250. NSString* template_file=nil;
  1251. switch([params[@"pdf_style"] intValue])
  1252. {
  1253. case 0:
  1254. template_file= @"portfolio_2x3.json";
  1255. break;
  1256. case 1:
  1257. template_file= @"portfolio_3x2.json";
  1258. break;
  1259. default:
  1260. template_file= @"portfolio_3x2.json";
  1261. }
  1262. NSMutableDictionary* data = [self prepare_portfolio_data:params];
  1263. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1264. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1265. DebugLog(@"%@",file);
  1266. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1267. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1268. if (file) {
  1269. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1270. [dic setObject:file forKey:@"pdf_path"];
  1271. dic[@"isLocalFile"]=@"true";
  1272. return [RAUtils dict2data:dic];
  1273. }
  1274. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1275. dic[@"isLocalFile"]=@"true";
  1276. return [RAUtils dict2data:dic];
  1277. }
  1278. +(NSString*) get_offline_soid:(sqlite3*)db
  1279. {
  1280. NSString* soid=nil;
  1281. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  1282. [formatter setDateFormat:@"yyMMdd"];
  1283. NSString* date = [formatter stringFromDate:[NSDate date]];
  1284. for(int i=1;i<999;i++)
  1285. {
  1286. soid=[NSString stringWithFormat:@"OFF%@%4d",date,i];
  1287. soid= [soid stringByReplacingOccurrencesOfString:@" " withString:@"0"];
  1288. int count=[iSalesDB get_recordcount:db table:@"offline_order" where:[NSString stringWithFormat:@"so_id='%@'",soid]];
  1289. if(count==0)
  1290. return soid;
  1291. }
  1292. return nil;
  1293. }
  1294. +(NSArray*) enumOfflineOrder
  1295. {
  1296. NSMutableArray* ret = [[NSMutableArray alloc] init];
  1297. sqlite3 *db = [iSalesDB get_db];
  1298. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  1299. sqlite3_stmt * statement;
  1300. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1301. if ( dbresult== SQLITE_OK)
  1302. {
  1303. while (sqlite3_step(statement) == SQLITE_ROW)
  1304. {
  1305. char *so_id = (char*)sqlite3_column_text(statement, 0);
  1306. NSString* nsso_id=nil;
  1307. if(so_id!=nil)
  1308. {
  1309. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  1310. }
  1311. [ret addObject:nsso_id];
  1312. }
  1313. sqlite3_finalize(statement);
  1314. }
  1315. [iSalesDB close_db:db];
  1316. return ret;
  1317. }
  1318. //+(void) uploadFile:(NSString*) file
  1319. //{
  1320. // NSData* data = [NSData dataWithContentsOfFile: file];
  1321. // UIApplication * app = [UIApplication sharedApplication];
  1322. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1323. //
  1324. //
  1325. // AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  1326. //
  1327. //
  1328. //
  1329. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  1330. //
  1331. // if(appDelegate.user!=nil)
  1332. // [params setValue:appDelegate.user forKey:@"user"];
  1333. // // if(appDelegate.contact_id!=nil)
  1334. // // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  1335. // if(appDelegate.password!=nil)
  1336. // [params setValue:appDelegate.password forKey:@"password"];
  1337. //
  1338. //
  1339. // NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_OFFLINE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  1340. // [formData appendPartWithFileData:data name:@"upfile" fileName:[file lastPathComponent] mimeType:@"application/zip"];
  1341. // } error:nil];
  1342. //
  1343. //
  1344. //
  1345. // AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  1346. //
  1347. // NSProgress *progress = nil;
  1348. //
  1349. // NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  1350. //
  1351. //
  1352. //
  1353. // if (error) {
  1354. //
  1355. // NSString* err_msg = [error localizedDescription];
  1356. // DebugLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  1357. //
  1358. //
  1359. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1360. //
  1361. // DebugLog(@"data string: %@",str);
  1362. // [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  1363. //
  1364. // } else {
  1365. // DebugLog(@"response ");
  1366. //
  1367. //
  1368. //
  1369. // NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  1370. //
  1371. // // 再将NSData转为字符串
  1372. // NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  1373. //
  1374. // // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1375. //
  1376. // DebugLog(@"data string: %@",jsonStr);
  1377. //
  1378. // NSDictionary* json = responseObject;
  1379. //
  1380. //
  1381. // if([[json valueForKey:@"result"] intValue]==2)
  1382. // {
  1383. //// NSString* img_url_down = json[@"img_url_aname"];
  1384. //// NSString* img_url_up = json[@"img_url"];
  1385. //
  1386. //
  1387. // }
  1388. // else
  1389. // {
  1390. // [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload file" controller:nil] ;
  1391. // }
  1392. //
  1393. //
  1394. // }
  1395. // // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  1396. // }];
  1397. //
  1398. //
  1399. //
  1400. //
  1401. // // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  1402. // //
  1403. //
  1404. //
  1405. //
  1406. //
  1407. //
  1408. //
  1409. // [uploadTask resume];
  1410. //
  1411. //}
  1412. +(NSDictionary *) prepareUpload:(NSArray*) arr_order
  1413. {
  1414. NSString* serial= [[NSUUID UUID] UUIDString];
  1415. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1416. NSString *cachefolder = [paths objectAtIndex:0];
  1417. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1418. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1419. NSFileManager* fileManager = [NSFileManager defaultManager];
  1420. BOOL bdir=YES;
  1421. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  1422. {
  1423. NSError *error = nil;
  1424. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  1425. if(!bsuccess)
  1426. DebugLog(@"Create UPLOAD folder failed");
  1427. }
  1428. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1429. ret[@"contact"]=[self prepareContact:serial];
  1430. ret[@"wishlist"]=[self prepareWishlist:serial];
  1431. ret[@"order"]=[self prepareOrder:serial soid:arr_order];
  1432. ret[@"portfolio"] = [self preparePortfolio:serial];
  1433. ret[@"view_portfolio"] = [self preparePDF:serial];
  1434. NSString* str= [RAUtils dict2string:ret];
  1435. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  1436. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  1437. NSError *error=nil;
  1438. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  1439. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  1440. ZipArchive* zip = [[ZipArchive alloc] init];
  1441. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  1442. for(int i=0;i<arr_files.count;i++)
  1443. {
  1444. NSString* file=arr_files[i];
  1445. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  1446. }
  1447. if( ![zip CloseZipFile2] )
  1448. {
  1449. zippath = @"";
  1450. }
  1451. ret[@"file"]=zippath;
  1452. return ret;
  1453. }
  1454. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  1455. {
  1456. if(filename.length==0)
  1457. return false;
  1458. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1459. bool ret=false;
  1460. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1461. NSString *cachefolder = [paths objectAtIndex:0];
  1462. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  1463. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  1464. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1465. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1466. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  1467. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  1468. NSFileManager* fileManager = [NSFileManager defaultManager];
  1469. BOOL bdir=NO;
  1470. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  1471. {
  1472. NSError *error = nil;
  1473. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  1474. {
  1475. ret=false;
  1476. }
  1477. else
  1478. {
  1479. ret=true;
  1480. }
  1481. }
  1482. return ret;
  1483. }
  1484. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  1485. {
  1486. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1487. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  1488. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  1489. NSString* where=@" 1=1";
  1490. if (ver!=nil) {
  1491. where=@"is_dirty=1";
  1492. }
  1493. sqlite3 *db = [iSalesDB get_db];
  1494. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  1495. sqlite3_stmt * statement;
  1496. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1497. int count=0;
  1498. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1499. if ( dbresult== SQLITE_OK)
  1500. {
  1501. while (sqlite3_step(statement) == SQLITE_ROW)
  1502. {
  1503. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1504. int _id = sqlite3_column_int(statement, 0);
  1505. int product_id = sqlite3_column_int(statement, 1);
  1506. int item_id = sqlite3_column_int(statement, 2);
  1507. int qty = sqlite3_column_int(statement, 3);
  1508. char *create_time = (char*)sqlite3_column_text(statement, 4);
  1509. NSString* nscreate_time=nil;
  1510. if(create_time!=nil)
  1511. {
  1512. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  1513. }
  1514. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  1515. NSString* nsmodify_time=nil;
  1516. if(modify_time!=nil)
  1517. {
  1518. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  1519. }
  1520. int is_delete = sqlite3_column_int(statement, 6);
  1521. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1522. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  1523. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1524. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  1525. itemjson[@"create_time"]=nscreate_time;
  1526. itemjson[@"modify_time"]=nsmodify_time;
  1527. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  1528. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1529. count++;
  1530. }
  1531. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1532. sqlite3_finalize(statement);
  1533. }
  1534. ret[@"count"]=[NSNumber numberWithInt:count ];
  1535. [iSalesDB close_db:db];
  1536. return ret;
  1537. }
  1538. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode db:(sqlite3*)db
  1539. {
  1540. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1541. UIApplication * app = [UIApplication sharedApplication];
  1542. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1543. 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,o.customer_cid from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id,so_no from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id left join offline_order as o on c.so_no = o.so_id;",orderCode ];
  1544. // 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 ];
  1545. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  1546. DebugLog(@"offline_login sql:%@",sqlQuery);
  1547. sqlite3_stmt * statement;
  1548. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1549. int count=0;
  1550. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1551. if ( dbresult== SQLITE_OK)
  1552. {
  1553. while (sqlite3_step(statement) == SQLITE_ROW)
  1554. {
  1555. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1556. char *customer_cid = (char *)sqlite3_column_text(statement, 10);
  1557. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  1558. int product_id = sqlite3_column_int(statement, 0);
  1559. char *str_price = (char*)sqlite3_column_text(statement, 1);
  1560. int item_id = sqlite3_column_int(statement, 7);
  1561. NSString* Price=nil;
  1562. if(str_price==nil)
  1563. {
  1564. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1565. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db];
  1566. if(price==nil)
  1567. Price=@"No Price.";
  1568. else
  1569. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1570. }
  1571. else
  1572. {
  1573. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1574. }
  1575. double discount = sqlite3_column_double(statement, 2);
  1576. int item_count = sqlite3_column_int(statement, 3);
  1577. char *line_note = (char*)sqlite3_column_text(statement, 4);
  1578. NSString *nsline_note=nil;
  1579. if(line_note!=nil)
  1580. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1581. char *name = (char*)sqlite3_column_text(statement, 5);
  1582. NSString *nsname=nil;
  1583. if(name!=nil)
  1584. nsname= [[NSString alloc]initWithUTF8String:name];
  1585. char *description = (char*)sqlite3_column_text(statement, 6);
  1586. NSString *nsdescription=nil;
  1587. if(description!=nil)
  1588. nsdescription= [[NSString alloc]initWithUTF8String:description];
  1589. // int stockUom = sqlite3_column_int(statement, 8);
  1590. // int _id = sqlite3_column_int(statement, 9);
  1591. //
  1592. //
  1593. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  1594. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1595. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  1596. // int carton=[bsubtotaljson[@"carton"] intValue];
  1597. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1598. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  1599. itemjson[@"model"]=nsname;
  1600. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  1601. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  1602. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  1603. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  1604. // itemjson[@"check"]=@"true";
  1605. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1606. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  1607. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  1608. itemjson[@"unit_price"]=Price;
  1609. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  1610. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1611. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  1612. itemjson[@"note"]=nsline_note;
  1613. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:customerID];
  1614. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1615. count++;
  1616. }
  1617. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1618. sqlite3_finalize(statement);
  1619. }
  1620. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1621. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  1622. return ret;
  1623. }
  1624. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid
  1625. {
  1626. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1627. sqlite3 *db = [iSalesDB get_db];
  1628. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  1629. // for(int i=0;i<arr_soid.count;i++)
  1630. sqlite3_stmt * statement;
  1631. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1632. //int count=0;
  1633. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1634. if ( dbresult== SQLITE_OK)
  1635. {
  1636. int count=0;
  1637. while (sqlite3_step(statement) == SQLITE_ROW)
  1638. {
  1639. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1640. int _id = sqlite3_column_int(statement, 0);
  1641. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  1642. NSString* nssync_data=nil;
  1643. if(sync_data!=nil)
  1644. {
  1645. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1646. }
  1647. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1648. NSString* nsimg_1=nil;
  1649. if(img_1!=nil)
  1650. {
  1651. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1652. [self copy_upImg:serial file:nsimg_1];
  1653. }
  1654. char *so_no = (char*)sqlite3_column_text(statement, 3);
  1655. NSString* nsso_no=nil;
  1656. if(so_no!=nil)
  1657. {
  1658. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  1659. }
  1660. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  1661. itemjson[@"order_type"]=@"submit order";
  1662. else
  1663. itemjson[@"order_type"]=@"archive order";
  1664. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1665. itemjson[@"json_data"]= nssync_data;
  1666. itemjson[@"order_items"]=[self preparecart:nsso_no db:db];
  1667. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1668. count++;
  1669. }
  1670. ret[@"count"]=[NSNumber numberWithInt:count ];
  1671. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1672. }
  1673. sqlite3_finalize(statement);
  1674. [iSalesDB close_db:db];
  1675. return ret;
  1676. }
  1677. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  1678. {
  1679. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1680. sqlite3 *db = [iSalesDB get_db];
  1681. // UIApplication * app = [UIApplication sharedApplication];
  1682. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1683. 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";
  1684. sqlite3_stmt * statement;
  1685. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1686. int count=0;
  1687. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1688. if ( dbresult== SQLITE_OK)
  1689. {
  1690. while (sqlite3_step(statement) == SQLITE_ROW)
  1691. {
  1692. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1693. int _id = sqlite3_column_int(statement, 0);
  1694. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  1695. NSString* nsimg_0=nil;
  1696. if(img_0!=nil)
  1697. {
  1698. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  1699. [self copy_upImg:serial file:nsimg_0];
  1700. }
  1701. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1702. NSString* nsimg_1=nil;
  1703. if(img_1!=nil)
  1704. {
  1705. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1706. [self copy_upImg:serial file:nsimg_1];
  1707. }
  1708. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  1709. NSString* nsimg_2=nil;
  1710. if(img_2!=nil)
  1711. {
  1712. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  1713. [self copy_upImg:serial file:nsimg_2];
  1714. }
  1715. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  1716. NSString* nssync_data=nil;
  1717. if(sync_data!=nil)
  1718. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1719. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1720. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1721. itemjson[@"json_data"]= nssync_data;
  1722. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1723. count++;
  1724. }
  1725. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1726. sqlite3_finalize(statement);
  1727. }
  1728. ret[@"count"]=[NSNumber numberWithInt:count ];
  1729. [iSalesDB close_db:db];
  1730. return ret;
  1731. }
  1732. +(bool) check_offlinedata
  1733. {
  1734. UIApplication * app = [UIApplication sharedApplication];
  1735. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1736. sqlite3 *db = [iSalesDB get_db];
  1737. NSString * where=@"1=1";
  1738. // if(appDelegate.user!=nil)
  1739. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  1740. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  1741. [iSalesDB close_db:db];
  1742. if(count==0)
  1743. {
  1744. return false;
  1745. }
  1746. return true;
  1747. //
  1748. //[iSalesDB close_db:db];
  1749. }
  1750. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  1751. {
  1752. UIApplication * app = [UIApplication sharedApplication];
  1753. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1754. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1755. sqlite3 *db = [iSalesDB get_db];
  1756. NSString* collectId=params[@"collectId"];
  1757. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  1758. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  1759. [iSalesDB execSql:sqlQuery db:db];
  1760. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  1761. [iSalesDB close_db:db];
  1762. appDelegate.wish_count =count;
  1763. [appDelegate update_count_mark];
  1764. ret[@"result"]= [NSNumber numberWithInt:2];
  1765. return ret;
  1766. }
  1767. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  1768. //{
  1769. //
  1770. // UIApplication * app = [UIApplication sharedApplication];
  1771. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1772. //
  1773. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1774. // sqlite3 *db = [iSalesDB get_db];
  1775. // NSString* product_id=params[@"product_id"];
  1776. //
  1777. //
  1778. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  1779. //
  1780. //
  1781. // for(int i=0;i<arr.count;i++)
  1782. // {
  1783. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  1784. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  1785. // if(count==0)
  1786. // {
  1787. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  1788. // [iSalesDB execSql:sqlQuery db:db];
  1789. // }
  1790. // }
  1791. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  1792. // [iSalesDB close_db:db];
  1793. //
  1794. // appDelegate.wish_count =count;
  1795. //
  1796. // [appDelegate update_count_mark];
  1797. // ret[@"result"]= [NSNumber numberWithInt:2];
  1798. // return ret;
  1799. // //
  1800. // //return ret;
  1801. //}
  1802. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  1803. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  1804. DebugLog(@"time interval: %lf",interval);
  1805. }
  1806. +(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  1807. {
  1808. UIApplication * app = [UIApplication sharedApplication];
  1809. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1810. int sort = [[params objectForKey:@"sort"] intValue];
  1811. NSString *sort_str = @"";
  1812. switch (sort) {
  1813. case 0:{
  1814. sort_str = @"order by w.modify_time desc";
  1815. }
  1816. break;
  1817. case 1:{
  1818. sort_str = @"order by w.modify_time asc";
  1819. }
  1820. break;
  1821. case 2:{
  1822. sort_str = @"order by m.name asc";
  1823. }
  1824. break;
  1825. case 3:{
  1826. sort_str = @"order by m.name desc";
  1827. }
  1828. break;
  1829. case 4:{
  1830. sort_str = @"order by m.description asc";
  1831. }
  1832. break;
  1833. default:
  1834. break;
  1835. }
  1836. NSString* user = appDelegate.user;
  1837. sqlite3 *db = [iSalesDB get_db];
  1838. // order by w.create_time
  1839. 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];
  1840. // 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];
  1841. sqlite3_stmt * statement;
  1842. NSDate *date1 = [NSDate date];
  1843. NSDate *date2 = nil;
  1844. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1845. int count=0;
  1846. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1847. {
  1848. while (sqlite3_step(statement) == SQLITE_ROW)
  1849. {
  1850. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1851. int product_id = sqlite3_column_double(statement, 0);
  1852. char *description = (char*)sqlite3_column_text(statement, 1);
  1853. if(description==nil)
  1854. description= "";
  1855. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  1856. int item_id = sqlite3_column_double(statement, 2);
  1857. NSDate *date_image = [NSDate date];
  1858. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  1859. printf("image : ");
  1860. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  1861. // char *url = (char*)sqlite3_column_text(statement, 3);
  1862. // if(url==nil)
  1863. // url="";
  1864. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1865. int qty = sqlite3_column_int(statement, 3);
  1866. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  1867. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  1868. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  1869. item[@"description"]= nsdescription;
  1870. item[@"img"]= nsurl;
  1871. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  1872. count++;
  1873. }
  1874. printf("total time:");
  1875. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  1876. ret[@"count"]= [NSNumber numberWithInt:count];
  1877. ret[@"total_count"]= [NSNumber numberWithInt:count];
  1878. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  1879. ret[@"result"]= [NSNumber numberWithInt:2];
  1880. appDelegate.wish_count =count;
  1881. [appDelegate update_count_mark];
  1882. sqlite3_finalize(statement);
  1883. }
  1884. [iSalesDB close_db:db];
  1885. return ret;
  1886. }
  1887. +(NSDictionary*) offline_notimpl
  1888. {
  1889. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1890. ret[@"result"]=@"8";
  1891. ret[@"err_msg"]=@"offline mode does not support this function.";
  1892. return ret;
  1893. }
  1894. +(NSDictionary*) offline_home
  1895. {
  1896. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1897. NSString *cachefolder = [paths objectAtIndex:0];
  1898. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  1899. NSData* json =nil;
  1900. json=[NSData dataWithContentsOfFile:img_cache];
  1901. if(json==nil)
  1902. return nil;
  1903. NSError *error=nil;
  1904. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1905. return menu;
  1906. }
  1907. +(NSDictionary*) offline_category_menu
  1908. {
  1909. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1910. NSString *cachefolder = [paths objectAtIndex:0];
  1911. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  1912. NSData* json =nil;
  1913. json=[NSData dataWithContentsOfFile:img_cache];
  1914. if(json==nil)
  1915. return nil;
  1916. NSError *error=nil;
  1917. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1918. return menu;
  1919. }
  1920. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  1921. {
  1922. NSString* offline_command=params[@"offline_Command"];
  1923. NSDictionary* ret=nil;
  1924. if([offline_command isEqualToString:@"model_NIYMAL"])
  1925. {
  1926. NSString* category = params[@"category"];
  1927. ret = [self refresh_model_NIYMAL:category];
  1928. }
  1929. return ret;
  1930. }
  1931. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  1932. {
  1933. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1934. [ret setValue:@"2" forKey:@"result"];
  1935. [ret setValue:@"Regular Mode" forKey:@"mode"];
  1936. sqlite3* db= [iSalesDB get_db];
  1937. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  1938. [iSalesDB close_db:db];
  1939. [ret setObject:detail1_section forKey:@"detail_1"];
  1940. return ret;
  1941. }
  1942. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id db:(sqlite3*)db
  1943. {
  1944. UIApplication * app = [UIApplication sharedApplication];
  1945. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1946. // NSArray* arr1 = [self get_user_all_price_type];
  1947. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  1948. // NSSet *set1 = [NSSet setWithArray:arr1];
  1949. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  1950. // if(appDelegate.contact_id==nil)
  1951. // set2=[set1 mutableCopy];
  1952. // else
  1953. // [set2 intersectsSet:set1];
  1954. // NSArray *retarr = [set2 allObjects];
  1955. NSString* whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  1956. // sqlite3 *db = [iSalesDB get_db];
  1957. NSString* sqlQuery = nil;
  1958. if(appDelegate.contact_id==nil)
  1959. {
  1960. if(!appDelegate.bLogin)
  1961. return nil;
  1962. 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];
  1963. }
  1964. else
  1965. 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];
  1966. sqlite3_stmt * statement;
  1967. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1968. int count=0;
  1969. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1970. {
  1971. while (sqlite3_step(statement) == SQLITE_ROW)
  1972. {
  1973. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1974. char *name = (char*)sqlite3_column_text(statement, 0);
  1975. if(name==nil)
  1976. name="";
  1977. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1978. // double price = sqlite3_column_double(statement, 1);
  1979. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  1980. // if(isnull==nil)
  1981. // item[nsname]= @"No Price";
  1982. // else
  1983. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  1984. char *price = (char*)sqlite3_column_text(statement, 1);
  1985. if(price!=nil)
  1986. {
  1987. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  1988. nsprice=[AESCrypt fastdecrypt:nsprice];
  1989. if(nsprice.length>0)
  1990. {
  1991. double dp= [nsprice doubleValue];
  1992. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  1993. }
  1994. }
  1995. else
  1996. {
  1997. item[nsname]= @"No Price";
  1998. }
  1999. // int type= sqlite3_column_int(statement, 2);
  2000. //item[@"type"]=@"price";
  2001. // item[nsname]= nsprice;
  2002. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2003. count++;
  2004. }
  2005. ret[@"count"]= [NSNumber numberWithInt:count];
  2006. sqlite3_finalize(statement);
  2007. }
  2008. // [iSalesDB close_db:db];
  2009. return ret;
  2010. }
  2011. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  2012. //{
  2013. // NSArray* arr1 = [self get_user_all_price_type:db];
  2014. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2015. //
  2016. // // NSSet *set1 = [NSSet setWithArray:arr1];
  2017. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2018. // // [set2 intersectsSet:set1];
  2019. // //
  2020. // //
  2021. // // NSArray *retarr = [set2 allObjects];
  2022. //
  2023. // NSString* whereprice=nil;
  2024. // if(contact_id==nil)
  2025. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2026. // else
  2027. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2028. //
  2029. //
  2030. // // sqlite3 *db = [iSalesDB get_db];
  2031. //
  2032. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  2033. // sqlite3_stmt * statement;
  2034. //
  2035. //
  2036. // NSNumber* ret = nil;
  2037. // double dprice=DBL_MAX;
  2038. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2039. // {
  2040. //
  2041. //
  2042. // while (sqlite3_step(statement) == SQLITE_ROW)
  2043. // {
  2044. //
  2045. // // double val = sqlite3_column_double(statement, 0);
  2046. // char *price = (char*)sqlite3_column_text(statement, 0);
  2047. // if(price!=nil)
  2048. // {
  2049. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2050. // nsprice=[AESCrypt fastdecrypt:nsprice];
  2051. // if(nsprice.length>0)
  2052. // {
  2053. // double dp= [nsprice doubleValue];
  2054. // if(dp<dprice)
  2055. // dprice=dp;
  2056. // }
  2057. // }
  2058. // }
  2059. //
  2060. //
  2061. //
  2062. //
  2063. // sqlite3_finalize(statement);
  2064. //
  2065. //
  2066. //
  2067. //
  2068. // }
  2069. //
  2070. // // [iSalesDB close_db:db];
  2071. //
  2072. // if(dprice==DBL_MAX)
  2073. // ret= nil;
  2074. // else
  2075. // ret= [NSNumber numberWithDouble:dprice];
  2076. // return ret;
  2077. //}
  2078. +(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  2079. {
  2080. NSArray* arr1 = [self get_user_all_price_type:db];
  2081. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2082. NSString* whereprice=nil;
  2083. if(contact_id==nil)
  2084. whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2085. else
  2086. whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2087. // sqlite3 *db = [iSalesDB get_db];
  2088. NSString *productIdCondition = @"1 = 1";
  2089. if (product_id) {
  2090. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  2091. }
  2092. NSString *itemIdCondition = @"";
  2093. if (item_id) {
  2094. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  2095. }
  2096. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  2097. sqlite3_stmt * statement;
  2098. NSNumber* ret = nil;
  2099. double dprice=DBL_MAX;
  2100. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2101. {
  2102. while (sqlite3_step(statement) == SQLITE_ROW)
  2103. {
  2104. // double val = sqlite3_column_double(statement, 0);
  2105. char *price = (char*)sqlite3_column_text(statement, 0);
  2106. if(price!=nil)
  2107. {
  2108. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2109. nsprice=[AESCrypt fastdecrypt:nsprice];
  2110. if(nsprice.length>0)
  2111. {
  2112. double dp= [nsprice doubleValue];
  2113. if(dp<dprice)
  2114. dprice=dp;
  2115. }
  2116. }
  2117. }
  2118. sqlite3_finalize(statement);
  2119. }
  2120. // [iSalesDB close_db:db];
  2121. if(dprice==DBL_MAX)
  2122. ret= nil;
  2123. else
  2124. ret= [NSNumber numberWithDouble:dprice];
  2125. return ret;
  2126. }
  2127. +(NSArray*) get_user_all_price_type:(sqlite3*)db
  2128. {
  2129. NSArray* ret=nil;
  2130. // sqlite3 *db = [iSalesDB get_db];
  2131. // no customer assigned , use login user contact_id
  2132. UIApplication * app = [UIApplication sharedApplication];
  2133. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2134. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",appDelegate.user];
  2135. sqlite3_stmt * statement;
  2136. // int count=0;
  2137. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2138. {
  2139. if (sqlite3_step(statement) == SQLITE_ROW)
  2140. {
  2141. char *val = (char*)sqlite3_column_text(statement, 0);
  2142. if(val==nil)
  2143. val="";
  2144. NSString* price = [[NSString alloc]initWithUTF8String:val];
  2145. ret=[RAUtils string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  2146. }
  2147. sqlite3_finalize(statement);
  2148. }
  2149. // [iSalesDB close_db:db];
  2150. return ret;
  2151. }
  2152. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id db:(sqlite3*)db
  2153. {
  2154. // sqlite3 *db = [iSalesDB get_db];
  2155. if(contact_id==nil)
  2156. {
  2157. // no customer assigned , use login user contact_id
  2158. UIApplication * app = [UIApplication sharedApplication];
  2159. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2160. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",appDelegate.user];
  2161. sqlite3_stmt * statement;
  2162. // int count=0;
  2163. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2164. {
  2165. if (sqlite3_step(statement) == SQLITE_ROW)
  2166. {
  2167. char *val = (char*)sqlite3_column_text(statement, 0);
  2168. if(val==nil)
  2169. val="";
  2170. contact_id = [[NSString alloc]initWithUTF8String:val];
  2171. }
  2172. sqlite3_finalize(statement);
  2173. }
  2174. if(contact_id.length<=0)
  2175. {
  2176. // [iSalesDB close_db:db];
  2177. return nil;
  2178. }
  2179. }
  2180. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  2181. sqlite3_stmt * statement;
  2182. NSArray* ret=nil;
  2183. // int count=0;
  2184. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2185. {
  2186. if (sqlite3_step(statement) == SQLITE_ROW)
  2187. {
  2188. char *price_type = (char*)sqlite3_column_text(statement, 0);
  2189. if(price_type==nil)
  2190. price_type="";
  2191. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  2192. if(nsprice_type.length>0)
  2193. ret=[RAUtils string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  2194. else
  2195. ret=nil;
  2196. }
  2197. sqlite3_finalize(statement);
  2198. }
  2199. // [iSalesDB close_db:db];
  2200. return ret;
  2201. }
  2202. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  2203. {
  2204. NSString* ret= nil;
  2205. // sqlite3 *db = [iSalesDB get_db];
  2206. NSString *sqlQuery = nil;
  2207. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  2208. // 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;
  2209. // 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
  2210. if(product_id==nil && model_name)
  2211. 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;
  2212. else if (product_id)
  2213. 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
  2214. sqlite3_stmt * statement;
  2215. // int count=0;
  2216. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2217. {
  2218. if (sqlite3_step(statement) == SQLITE_ROW)
  2219. {
  2220. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  2221. if(imgurl==nil)
  2222. imgurl="";
  2223. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  2224. ret=nsimgurl;
  2225. }
  2226. sqlite3_finalize(statement);
  2227. }
  2228. else
  2229. {
  2230. [ret setValue:@"8" forKey:@"result"];
  2231. }
  2232. // [iSalesDB close_db:db];
  2233. DebugLog(@"data string: %@",ret );
  2234. return ret;
  2235. }
  2236. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name
  2237. {
  2238. NSString* ret= nil;
  2239. sqlite3 *db = [iSalesDB get_db];
  2240. NSString *sqlQuery = nil;
  2241. if(product_id==nil)
  2242. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  2243. else
  2244. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  2245. sqlite3_stmt * statement;
  2246. // int count=0;
  2247. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2248. {
  2249. if (sqlite3_step(statement) == SQLITE_ROW)
  2250. {
  2251. char *default_category = (char*)sqlite3_column_text(statement, 0);
  2252. if(default_category==nil)
  2253. default_category="";
  2254. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2255. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  2256. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  2257. ret=nsdefault_category;
  2258. }
  2259. sqlite3_finalize(statement);
  2260. }
  2261. else
  2262. {
  2263. [ret setValue:@"8" forKey:@"result"];
  2264. }
  2265. [iSalesDB close_db:db];
  2266. DebugLog(@"data string: %@",ret );
  2267. return ret;
  2268. }
  2269. +(NSDictionary*) offline_model :(NSMutableDictionary *) params
  2270. {
  2271. NSString* model_name = [params valueForKey:@"product_name"];
  2272. NSString* product_id = [params valueForKey:@"product_id"];
  2273. NSString* category = [params valueForKey:@"category"];
  2274. if(category==nil)
  2275. category = [self model_default_category:product_id model_name:model_name];
  2276. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2277. sqlite3 *db = [iSalesDB get_db];
  2278. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  2279. NSString *sqlQuery = nil;
  2280. if(product_id==nil)
  2281. 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='%@';
  2282. else
  2283. 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=%@;
  2284. sqlite3_stmt * statement;
  2285. [ret setValue:@"2" forKey:@"result"];
  2286. [ret setValue:@"3" forKey:@"detail_section_count"];
  2287. // int count=0;
  2288. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2289. {
  2290. if (sqlite3_step(statement) == SQLITE_ROW)
  2291. {
  2292. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2293. char *name = (char*)sqlite3_column_text(statement, 0);
  2294. if(name==nil)
  2295. name="";
  2296. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2297. char *description = (char*)sqlite3_column_text(statement, 1);
  2298. if(description==nil)
  2299. description="";
  2300. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2301. int product_id = sqlite3_column_int(statement, 2);
  2302. char *color = (char*)sqlite3_column_text(statement, 3);
  2303. if(color==nil)
  2304. color="";
  2305. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  2306. //
  2307. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  2308. // if(legcolor==nil)
  2309. // legcolor="";
  2310. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  2311. //
  2312. //
  2313. int availability = sqlite3_column_int(statement, 5);
  2314. //
  2315. int incoming_stock = sqlite3_column_int(statement, 6);
  2316. char *demension = (char*)sqlite3_column_text(statement, 7);
  2317. if(demension==nil)
  2318. demension="";
  2319. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  2320. // ,,,,,,,,,
  2321. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  2322. if(seat_height==nil)
  2323. seat_height="";
  2324. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  2325. char *material = (char*)sqlite3_column_text(statement, 9);
  2326. if(material==nil)
  2327. material="";
  2328. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  2329. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  2330. if(box_dim==nil)
  2331. box_dim="";
  2332. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  2333. char *volume = (char*)sqlite3_column_text(statement, 11);
  2334. if(volume==nil)
  2335. volume="";
  2336. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  2337. double weight = sqlite3_column_double(statement, 12);
  2338. char *model_set = (char*)sqlite3_column_text(statement, 13);
  2339. if(model_set==nil)
  2340. model_set="";
  2341. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  2342. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  2343. if(load_ability==nil)
  2344. load_ability="";
  2345. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  2346. char *default_category = (char*)sqlite3_column_text(statement, 15);
  2347. if(default_category==nil)
  2348. default_category="";
  2349. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2350. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  2351. if(fabric_content==nil)
  2352. fabric_content="";
  2353. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  2354. char *assembling = (char*)sqlite3_column_text(statement, 17);
  2355. if(assembling==nil)
  2356. assembling="";
  2357. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  2358. char *made_in = (char*)sqlite3_column_text(statement, 18);
  2359. if(made_in==nil)
  2360. made_in="";
  2361. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  2362. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  2363. if(special_remarks==nil)
  2364. special_remarks="";
  2365. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  2366. int stockUcom = sqlite3_column_double(statement, 20);
  2367. char *product_group = (char*)sqlite3_column_text(statement, 21);
  2368. if(product_group==nil)
  2369. product_group="";
  2370. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  2371. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  2372. // if(fashion_selector==nil)
  2373. // fashion_selector="";
  2374. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  2375. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  2376. if(selector_field==nil)
  2377. selector_field="";
  2378. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  2379. char *property_field = (char*)sqlite3_column_text(statement, 23);
  2380. if(property_field==nil)
  2381. property_field="";
  2382. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  2383. char *packaging = (char*)sqlite3_column_text(statement, 24);
  2384. if(packaging==nil)
  2385. packaging="";
  2386. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  2387. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2388. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  2389. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  2390. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  2391. [img_section setValue:model_s_img forKey:@"model_s_img"];
  2392. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  2393. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  2394. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  2395. NSString* Availability=nil;
  2396. if(availability>0)
  2397. Availability=[NSString stringWithFormat:@"%d",availability];
  2398. else
  2399. Availability = @"In Production";
  2400. [img_section setValue:Availability forKey:@"Availability"];
  2401. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  2402. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  2403. char *eta = (char*)sqlite3_column_text(statement, 25);
  2404. if(eta==nil)
  2405. eta="";
  2406. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  2407. if ([nseta isEqualToString:@"null"]) {
  2408. nseta = @"";
  2409. }
  2410. if (availability <= 0) {
  2411. [img_section setValue:nseta forKey:@"ETA"];
  2412. }
  2413. int item_id = sqlite3_column_int(statement, 26);
  2414. NSString* Price=nil;
  2415. if(appDelegate.bLogin==false)
  2416. Price=@"Must Sign in.";
  2417. else
  2418. {
  2419. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2420. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  2421. if(price==nil)
  2422. Price=@"No Price.";
  2423. else
  2424. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2425. }
  2426. [img_section setValue:Price forKey:@"price"];
  2427. [img_section setValue:nsname forKey:@"model_name"];
  2428. [img_section setValue:nsdescription forKey:@"model_descrition"];
  2429. [ret setObject:img_section forKey:@"img_section"];
  2430. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  2431. int detail0_item_count=0;
  2432. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2433. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2434. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2435. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2436. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2437. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2438. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2439. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2440. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2441. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2442. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2443. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2444. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2445. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  2446. NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  2447. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  2448. {
  2449. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  2450. [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2451. }
  2452. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  2453. [detail0_section setValue:@"kv" forKey:@"type"];
  2454. [detail0_section setValue:@"Product Information" forKey:@"title"];
  2455. [ret setObject:detail0_section forKey:@"detail_0"];
  2456. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  2457. // [detail1_section setValue:@"detail" forKey:@"target"];
  2458. // [detail1_section setValue:@"popup" forKey:@"action"];
  2459. // [detail1_section setValue:@"content" forKey:@"type"];
  2460. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  2461. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  2462. // [detail1_section setValue:@"true" forKey:@"single_row"];
  2463. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  2464. [ret setObject:detail1_section forKey:@"detail_1"];
  2465. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  2466. [detail2_section setValue:@"detail" forKey:@"target"];
  2467. [detail2_section setValue:@"popup" forKey:@"action"];
  2468. [detail2_section setValue:@"content" forKey:@"type"];
  2469. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  2470. [detail2_section setValue:@"true" forKey:@"single_row"];
  2471. [detail2_section setValue:@"local" forKey:@"data"];
  2472. [ret setObject:detail2_section forKey:@"detail_2"];
  2473. }
  2474. sqlite3_finalize(statement);
  2475. }
  2476. else
  2477. {
  2478. [ret setValue:@"8" forKey:@"result"];
  2479. }
  2480. DebugLog(@"count:%d",count);
  2481. [iSalesDB close_db:db];
  2482. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2483. return ret;
  2484. }
  2485. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  2486. NSString* orderCode = [params valueForKey:@"orderCode"];
  2487. NSString* keyword = [params valueForKey:@"keyword"];
  2488. keyword=keyword.lowercaseString;
  2489. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue];
  2490. int limit = [[params valueForKey:@"limit"] intValue];
  2491. int offset = [[params valueForKey:@"offset"] intValue];
  2492. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2493. NSString *limit_str = @"";
  2494. if (limited) {
  2495. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  2496. }
  2497. sqlite3 *db = [iSalesDB get_db];
  2498. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  2499. UIApplication * app = [UIApplication sharedApplication];
  2500. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2501. NSString *sqlQuery = nil;
  2502. if(exactMatch )
  2503. 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 ;
  2504. else
  2505. 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
  2506. DebugLog(@"offline_search sql:%@",sqlQuery);
  2507. sqlite3_stmt * statement;
  2508. [ret setValue:@"2" forKey:@"result"];
  2509. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  2510. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  2511. // int count=0;
  2512. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2513. {
  2514. int i=0;
  2515. while (sqlite3_step(statement) == SQLITE_ROW)
  2516. {
  2517. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2518. // char *name = (char*)sqlite3_column_text(statement, 1);
  2519. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2520. char *name = (char*)sqlite3_column_text(statement, 0);
  2521. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2522. char *description = (char*)sqlite3_column_text(statement, 1);
  2523. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2524. int product_id = sqlite3_column_int(statement, 2);
  2525. // char *url = (char*)sqlite3_column_text(statement, 3);
  2526. // if(url==nil)
  2527. // url="";
  2528. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2529. int wid = sqlite3_column_int(statement, 3);
  2530. int closeout = sqlite3_column_int(statement, 4);
  2531. int cid = sqlite3_column_int(statement, 5);
  2532. int wisdelete = sqlite3_column_int(statement, 6);
  2533. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  2534. if(wid !=0 && wisdelete != 1)
  2535. [item setValue:@"true" forKey:@"wish_exists"];
  2536. else
  2537. [item setValue:@"false" forKey:@"wish_exists"];
  2538. if(closeout==0)
  2539. [item setValue:@"false" forKey:@"is_closeout"];
  2540. else
  2541. [item setValue:@"true" forKey:@"is_closeout"];
  2542. if(cid==0)
  2543. [item setValue:@"false" forKey:@"cart_exists"];
  2544. else
  2545. [item setValue:@"true" forKey:@"cart_exists"];
  2546. [item addEntriesFromDictionary:imgjson];
  2547. // [item setValue:nsurl forKey:@"img"];
  2548. [item setValue:nsname forKey:@"fash_name"];
  2549. [item setValue:nsdescription forKey:@"description"];
  2550. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2551. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2552. i++;
  2553. }
  2554. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2555. [ret setObject:items forKey:@"items"];
  2556. sqlite3_finalize(statement);
  2557. }
  2558. DebugLog(@"count:%d",count);
  2559. [iSalesDB close_db:db];
  2560. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2561. return ret;
  2562. }
  2563. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  2564. {
  2565. return [self search:params limited:YES];
  2566. }
  2567. +(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  2568. {
  2569. UIApplication * app = [UIApplication sharedApplication];
  2570. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2571. appDelegate.disable_trigger=true;
  2572. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  2573. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2574. appDelegate.disable_trigger=false;
  2575. //
  2576. // NSString* user = [params valueForKey:@"user"];
  2577. //
  2578. // NSString* password = [params valueForKey:@"password"];
  2579. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2580. ret[@"result"]=[NSNumber numberWithInt:2 ];
  2581. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  2582. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  2583. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  2584. [appDelegate update_count_mark];
  2585. appDelegate.can_show_price =false;
  2586. appDelegate.can_see_price =false;
  2587. appDelegate.can_create_portfolio =false;
  2588. appDelegate.can_create_order =false;
  2589. appDelegate.can_cancel_order =false;
  2590. appDelegate.can_set_cart_price =false;
  2591. appDelegate.can_delete_order =false;
  2592. appDelegate.can_submit_order =false;
  2593. appDelegate.can_set_tearsheet_price =false;
  2594. appDelegate.can_update_contact_info = false;
  2595. appDelegate.save_order_logout = false;
  2596. appDelegate.submit_order_logout = false;
  2597. appDelegate.alert_sold_in_quantities = false;
  2598. appDelegate.ipad_perm =nil ;
  2599. appDelegate.user_type = USER_ROLE_UNKNOWN;
  2600. appDelegate.OrderFilter= nil;
  2601. [appDelegate SetSo:nil];
  2602. [appDelegate set_main_button_panel];
  2603. // sqlite3 *db = [iSalesDB get_db];
  2604. //
  2605. //
  2606. //
  2607. //
  2608. //
  2609. // 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"]];
  2610. //
  2611. //
  2612. //
  2613. //
  2614. // DebugLog(@"offline_login sql:%@",sqlQuery);
  2615. // sqlite3_stmt * statement;
  2616. //
  2617. //
  2618. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2619. //
  2620. //
  2621. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2622. // {
  2623. //
  2624. //
  2625. // if (sqlite3_step(statement) == SQLITE_ROW)
  2626. // {
  2627. //
  2628. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2629. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  2630. //
  2631. //
  2632. //
  2633. // int can_show_price = sqlite3_column_int(statement, 0);
  2634. // int can_see_price = sqlite3_column_int(statement, 1);
  2635. //
  2636. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  2637. // if(contact_id==nil)
  2638. // contact_id="";
  2639. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2640. //
  2641. // int user_type = sqlite3_column_int(statement, 3);
  2642. //
  2643. // int can_cancel_order = sqlite3_column_int(statement, 4);
  2644. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  2645. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  2646. // int can_delete_order = sqlite3_column_int(statement, 7);
  2647. // int can_submit_order = sqlite3_column_int(statement, 8);
  2648. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  2649. // int can_create_order = sqlite3_column_int(statement, 10);
  2650. //
  2651. //
  2652. // char *mode = (char*)sqlite3_column_text(statement, 11);
  2653. // if(mode==nil)
  2654. // mode="";
  2655. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  2656. //
  2657. //
  2658. // char *username = (char*)sqlite3_column_text(statement, 12);
  2659. // if(username==nil)
  2660. // username="";
  2661. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  2662. //
  2663. //
  2664. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  2665. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  2666. // [header setValue:nscontact_id forKey:@"contact_id"];
  2667. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  2668. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  2669. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  2670. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  2671. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  2672. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  2673. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  2674. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  2675. //
  2676. // [header setValue:nsusername forKey:@"username"];
  2677. //
  2678. //
  2679. // [ret setObject:header forKey:@"header"];
  2680. // [ret setValue:nsmode forKey:@"mode"];
  2681. //
  2682. //
  2683. // }
  2684. //
  2685. //
  2686. //
  2687. // sqlite3_finalize(statement);
  2688. // }
  2689. //
  2690. //
  2691. //
  2692. // [iSalesDB close_db:db];
  2693. //
  2694. //
  2695. //
  2696. //
  2697. // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2698. return ret;
  2699. }
  2700. +(NSData*) offline_createorder :(NSMutableDictionary *) params
  2701. {
  2702. UIApplication * app = [UIApplication sharedApplication];
  2703. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2704. appDelegate.disable_trigger=true;
  2705. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  2706. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2707. appDelegate.disable_trigger=false;
  2708. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2709. NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  2710. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  2711. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  2712. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  2713. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  2714. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  2715. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  2716. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  2717. NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  2718. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  2719. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  2720. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  2721. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  2722. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  2723. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  2724. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  2725. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  2726. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  2727. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  2728. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  2729. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  2730. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  2731. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  2732. [arr_name addObject:customer_first_name];
  2733. [arr_name addObject:customer_last_name];
  2734. NSString *contact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  2735. // default ship from
  2736. 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';";
  2737. __block NSString *cid = @"";
  2738. __block NSString *name = @"";
  2739. __block NSString *ext = @"";
  2740. __block NSString *contact = @"";
  2741. __block NSString *email = @"";
  2742. __block NSString *fax = @"";
  2743. __block NSString *phone = @"";
  2744. sqlite3 *db = [iSalesDB get_db];
  2745. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  2746. cid = [self textAtColumn:0 statement:statment];
  2747. name = [self textAtColumn:1 statement:statment];
  2748. ext = [self textAtColumn:2 statement:statment];
  2749. contact = [self textAtColumn:3 statement:statment];
  2750. email = [self textAtColumn:4 statement:statment];
  2751. fax = [self textAtColumn:5 statement:statment];
  2752. phone = [self textAtColumn:6 statement:statment];
  2753. }];
  2754. NSString* so_id = [self get_offline_soid:db];
  2755. if(so_id==nil)
  2756. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  2757. 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,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,sales_terms) values('%@',0,'%@','%@','%@','%@','%@','%@','%@','%@',1,'%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','FOB')",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,customer_cid,customer_name,customer_contact_ext,customer_contact,customer_phone,customer_fax,customer_email];
  2758. int result =[iSalesDB execSql:sql_neworder db:db];
  2759. [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  2760. //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'
  2761. //soId
  2762. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  2763. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  2764. sqlite3_stmt * statement;
  2765. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2766. {
  2767. if (sqlite3_step(statement) == SQLITE_ROW)
  2768. {
  2769. // char *name = (char*)sqlite3_column_text(statement, 1);
  2770. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2771. //ret = sqlite3_column_int(statement, 0);
  2772. char *soId = (char*)sqlite3_column_text(statement, 0);
  2773. if(soId==nil)
  2774. soId="";
  2775. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  2776. [ret setValue:nssoId forKey:@"soId"];
  2777. [ret setValue:nssoId forKey:@"orderCode"];
  2778. }
  2779. sqlite3_finalize(statement);
  2780. }
  2781. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  2782. [ret setValue:@"Regular Mode" forKey:@"mode"];
  2783. [iSalesDB close_db:db];
  2784. return [RAUtils dict2data:ret];
  2785. }
  2786. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  2787. {
  2788. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2789. NSString* orderCode = [params valueForKey:@"orderCode"];
  2790. UIApplication * app = [UIApplication sharedApplication];
  2791. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2792. if(![appDelegate.order_code isEqualToString:orderCode]&& appDelegate.order_code.length!=0)
  2793. {
  2794. appDelegate.disable_trigger=true;
  2795. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  2796. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2797. appDelegate.disable_trigger=false;
  2798. }
  2799. sqlite3 *db = [iSalesDB get_db];
  2800. int cart_count=[self query_ordercartcount:orderCode db:db];
  2801. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2802. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2803. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2804. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2805. [iSalesDB close_db:db];
  2806. return [RAUtils dict2data:ret];
  2807. }
  2808. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  2809. {
  2810. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2811. sqlite3 *db = [iSalesDB get_db];
  2812. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  2813. int count =0;
  2814. if(params[@"count"]!=nil)
  2815. {
  2816. count = [params[@"count"] intValue];
  2817. }
  2818. NSString* product_id=params[@"product_id"];
  2819. NSString* orderCode=params[@"orderCode"];
  2820. NSString *qty = params[@"qty"];
  2821. NSArray* arr_id=[RAUtils string2arr:product_id separator:@","];
  2822. NSArray *qty_arr = [RAUtils string2arr:qty separator:@","];
  2823. for(int i=0;i<arr_id.count;i++)
  2824. {
  2825. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  2826. int item_qty= count;
  2827. if (qty) {
  2828. item_qty = [qty_arr[i] integerValue];
  2829. }
  2830. if(item_qty==0)
  2831. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  2832. NSString* sql=nil;
  2833. sqlite3_stmt *stmt;
  2834. if(_id<0)
  2835. {
  2836. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  2837. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  2838. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2839. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],[arr_id[i] length],NULL);
  2840. sqlite3_bind_text(stmt,2,[orderCode UTF8String],[orderCode length],NULL);
  2841. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%d",item_qty] UTF8String],[[NSString stringWithFormat:@"%d",item_qty] length],NULL);
  2842. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  2843. }
  2844. else
  2845. {
  2846. if (qty) {
  2847. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  2848. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2849. sqlite3_bind_int(stmt, 1, _id);
  2850. } else {
  2851. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  2852. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2853. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%d",item_qty] UTF8String],[[NSString stringWithFormat:@"%d",item_qty] length],NULL);
  2854. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],[[NSString stringWithFormat:@"%d",_id] length],NULL);
  2855. }
  2856. }
  2857. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  2858. [iSalesDB execSql:@"ROLLBACK" db:db];
  2859. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  2860. [iSalesDB close_db:db];
  2861. DebugLog(@"add to cart error");
  2862. return [RAUtils dict2data:ret];
  2863. }
  2864. }
  2865. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2866. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2867. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2868. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2869. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2870. [iSalesDB execSql:@"END TRANSACTION" db:db];
  2871. [iSalesDB close_db:db];
  2872. return [RAUtils dict2data:ret];
  2873. }
  2874. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute contactID:(NSString *)contactID
  2875. {
  2876. UIApplication * app = [UIApplication sharedApplication];
  2877. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2878. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  2879. 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];
  2880. sqlite3_stmt * statement;
  2881. int count=0;
  2882. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2883. {
  2884. while (sqlite3_step(statement) == SQLITE_ROW)
  2885. {
  2886. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  2887. int bitem_id = sqlite3_column_int(statement, 0);
  2888. int bitem_qty = sqlite3_column_int(statement, 1);
  2889. char *name = (char*)sqlite3_column_text(statement, 2);
  2890. if(name==nil)
  2891. name="";
  2892. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  2893. char *description = (char*)sqlite3_column_text(statement, 3);
  2894. if(description==nil)
  2895. description="";
  2896. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  2897. double unit_price = sqlite3_column_double(statement, 4);
  2898. int use_unitprice = sqlite3_column_int(statement, 5);
  2899. if(use_unitprice!=1)
  2900. {
  2901. unit_price=[self get_model_default_price:contactID product_id:nil item_id:@(bitem_id) db:db].doubleValue;
  2902. }
  2903. itemjson[@"model"]=nsname;
  2904. itemjson[@"description"]=nsdescription;
  2905. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  2906. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  2907. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  2908. if(compute)
  2909. {
  2910. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute contactID:contactID];
  2911. }
  2912. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2913. count++;
  2914. }
  2915. sqlite3_finalize(statement);
  2916. }
  2917. ret[@"count"]=@(count);
  2918. if(count==0)
  2919. return nil;
  2920. else
  2921. return ret;
  2922. }
  2923. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  2924. {
  2925. //compute: add part to subtotal;
  2926. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  2927. dict_item[@(item_id)]=@"1";
  2928. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  2929. double cuft=0;
  2930. double weight=0;
  2931. int carton=0;
  2932. 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];
  2933. sqlite3_stmt * statement;
  2934. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2935. {
  2936. if (sqlite3_step(statement) == SQLITE_ROW)
  2937. {
  2938. double ulength = sqlite3_column_double(statement, 0);
  2939. double uwidth = sqlite3_column_double(statement, 1);
  2940. double uheight = sqlite3_column_double(statement, 2);
  2941. double uweight = sqlite3_column_double(statement, 3);
  2942. double mlength = sqlite3_column_double(statement, 4);
  2943. double mwidth = sqlite3_column_double(statement, 5);
  2944. double mheight = sqlite3_column_double(statement, 6);
  2945. double mweight = sqlite3_column_double(statement, 7);
  2946. double ilength = sqlite3_column_double(statement, 8);
  2947. double iwidth = sqlite3_column_double(statement, 9);
  2948. double iheight = sqlite3_column_double(statement, 10);
  2949. double iweight = sqlite3_column_double(statement, 11);
  2950. int pcs = sqlite3_column_int(statement,12);
  2951. int mpack = sqlite3_column_int(statement, 13);
  2952. int ipack = sqlite3_column_int(statement, 14);
  2953. double ucbf = sqlite3_column_double(statement, 15);
  2954. double icbf = sqlite3_column_double(statement, 16);
  2955. double mcbf = sqlite3_column_double(statement, 17);
  2956. if(ipack==0)
  2957. {
  2958. carton= count/mpack ;
  2959. weight = mweight*carton;
  2960. cuft= carton*(mlength*mwidth*mheight);
  2961. int remain=count%mpack;
  2962. if(remain==0)
  2963. {
  2964. //do nothing;
  2965. }
  2966. else
  2967. {
  2968. carton++;
  2969. weight += uweight*remain;
  2970. cuft += (ulength*uwidth*uheight)*remain;
  2971. }
  2972. }
  2973. else
  2974. {
  2975. carton = count/(mpack*ipack);
  2976. weight = mweight*carton;
  2977. cuft= carton*(mlength*mwidth*mheight);
  2978. int remain=count%(mpack*ipack);
  2979. if(remain==0)
  2980. {
  2981. // do nothing;
  2982. }
  2983. else
  2984. {
  2985. carton++;
  2986. int icarton =remain/ipack;
  2987. int iremain=remain%ipack;
  2988. weight += iweight*icarton;
  2989. cuft += (ilength*iwidth*iheight)*icarton;
  2990. if(iremain==0)
  2991. {
  2992. //do nothing;
  2993. }
  2994. else
  2995. {
  2996. weight += uweight*iremain;
  2997. cuft += (ulength*uwidth*uheight)*iremain;
  2998. }
  2999. }
  3000. }
  3001. #ifdef BUILD_NPD
  3002. cuft=ucbf*count;
  3003. weight= uweight*count;
  3004. #endif
  3005. }
  3006. sqlite3_finalize(statement);
  3007. }
  3008. if(compute)
  3009. {
  3010. NSArray * arr_count=nil;
  3011. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  3012. for(int i=0;i<arr_bundle.count;i++)
  3013. {
  3014. dict_item[arr_bundle[i]]=@"1";
  3015. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  3016. cuft+=[bundlejson[@"cuft"] doubleValue];
  3017. weight+=[bundlejson[@"weight"] doubleValue];
  3018. carton+=[bundlejson[@"carton"] intValue];
  3019. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  3020. }
  3021. }
  3022. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  3023. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  3024. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  3025. ret[@"items"]=dict_item;
  3026. return ret;
  3027. }
  3028. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  3029. {
  3030. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  3031. // 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 ];
  3032. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3033. DebugLog(@"offline_login sql:%@",sqlQuery);
  3034. sqlite3_stmt * statement;
  3035. int cart_count=0;
  3036. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3037. if ( dbresult== SQLITE_OK)
  3038. {
  3039. while (sqlite3_step(statement) == SQLITE_ROW)
  3040. {
  3041. int item_id = sqlite3_column_int(statement, 0);
  3042. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  3043. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3044. }
  3045. sqlite3_finalize(statement);
  3046. }
  3047. return cart_count;
  3048. }
  3049. +(NSData*) offline_requestcart :(NSMutableDictionary *) params
  3050. {
  3051. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3052. sqlite3 *db = [iSalesDB get_db];
  3053. UIApplication * app = [UIApplication sharedApplication];
  3054. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3055. NSString* orderCode=params[@"orderCode"];
  3056. int sort = [[params objectForKey:@"sort"] intValue];
  3057. NSString *sort_str = @"";
  3058. switch (sort) {
  3059. case 0:{
  3060. sort_str = @"order by c.modify_time desc";
  3061. }
  3062. break;
  3063. case 1:{
  3064. sort_str = @"order by c.modify_time asc";
  3065. }
  3066. break;
  3067. case 2:{
  3068. sort_str = @"order by m.name asc";
  3069. }
  3070. break;
  3071. case 3:{
  3072. sort_str = @"order by m.name desc";
  3073. }
  3074. break;
  3075. case 4:{
  3076. sort_str = @"order by m.description asc";
  3077. }
  3078. break;
  3079. default:
  3080. break;
  3081. }
  3082. 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 ];
  3083. // 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 ];
  3084. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3085. DebugLog(@"offline_login sql:%@",sqlQuery);
  3086. sqlite3_stmt * statement;
  3087. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3088. NSDate *date1 = [NSDate date];
  3089. int count=0;
  3090. int cart_count=0;
  3091. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3092. if ( dbresult== SQLITE_OK)
  3093. {
  3094. while (sqlite3_step(statement) == SQLITE_ROW)
  3095. {
  3096. NSDate *row_date = [NSDate date];
  3097. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  3098. int product_id = sqlite3_column_int(statement, 0);
  3099. char *str_price = (char*)sqlite3_column_text(statement, 1);
  3100. int item_id = sqlite3_column_int(statement, 7);
  3101. NSString* Price=nil;
  3102. if(str_price==nil)
  3103. {
  3104. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  3105. // NSDate *price_date = [NSDate date];
  3106. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  3107. // DebugLog(@"price time interval");
  3108. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  3109. if(price==nil)
  3110. Price=@"No Price.";
  3111. else
  3112. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  3113. }
  3114. else
  3115. {
  3116. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  3117. }
  3118. double discount = sqlite3_column_double(statement, 2);
  3119. int item_count = sqlite3_column_int(statement, 3);
  3120. char *line_note = (char*)sqlite3_column_text(statement, 4);
  3121. NSString *nsline_note=nil;
  3122. if(line_note!=nil)
  3123. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  3124. char *name = (char*)sqlite3_column_text(statement, 5);
  3125. NSString *nsname=nil;
  3126. if(name!=nil)
  3127. nsname= [[NSString alloc]initWithUTF8String:name];
  3128. char *description = (char*)sqlite3_column_text(statement, 6);
  3129. NSString *nsdescription=nil;
  3130. if(description!=nil)
  3131. nsdescription= [[NSString alloc]initWithUTF8String:description];
  3132. int stockUom = sqlite3_column_int(statement, 8);
  3133. int _id = sqlite3_column_int(statement, 9);
  3134. // NSDate *subtotal_date = [NSDate date];
  3135. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  3136. // DebugLog(@"subtotal_date time interval");
  3137. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  3138. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  3139. double weight=[bsubtotaljson[@"weight"] doubleValue];
  3140. int carton=[bsubtotaljson[@"carton"] intValue];
  3141. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3142. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  3143. // NSDate *img_date = [NSDate date];
  3144. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  3145. // DebugLog(@"img_date time interval");
  3146. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  3147. itemjson[@"model"]=nsname;
  3148. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  3149. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  3150. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  3151. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  3152. itemjson[@"check"]=@"true";
  3153. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  3154. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  3155. itemjson[@"unit_price"]=Price;
  3156. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  3157. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  3158. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  3159. itemjson[@"note"]=nsline_note;
  3160. // NSDate *date2 = [NSDate date];
  3161. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  3162. // DebugLog(@"model_bundle time interval");
  3163. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  3164. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  3165. count++;
  3166. // DebugLog(@"row time interval");
  3167. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  3168. }
  3169. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3170. sqlite3_finalize(statement);
  3171. }
  3172. DebugLog(@"request cart total time interval");
  3173. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  3174. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  3175. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  3176. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3177. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3178. ret[@"count"]=[NSNumber numberWithInt:count ];
  3179. ret[@"mode"]=@"Regular Mode";
  3180. [iSalesDB close_db:db];
  3181. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  3182. DebugLog(@"general notes :%@",general_note);
  3183. ret[@"general_note"]= general_note;
  3184. return [RAUtils dict2data:ret];
  3185. }
  3186. +(NSData*) offline_login :(NSMutableDictionary *) params
  3187. {
  3188. NSString* user = [params valueForKey:@"user"];
  3189. NSString* password = [params valueForKey:@"password"];
  3190. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3191. sqlite3 *db = [iSalesDB get_db];
  3192. 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"]];
  3193. DebugLog(@"offline_login sql:%@",sqlQuery);
  3194. sqlite3_stmt * statement;
  3195. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3196. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3197. {
  3198. if (sqlite3_step(statement) == SQLITE_ROW)
  3199. {
  3200. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3201. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  3202. int can_show_price = sqlite3_column_int(statement, 0);
  3203. int can_see_price = sqlite3_column_int(statement, 1);
  3204. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  3205. if(contact_id==nil)
  3206. contact_id="";
  3207. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3208. int user_type = sqlite3_column_int(statement, 3);
  3209. int can_cancel_order = sqlite3_column_int(statement, 4);
  3210. int can_set_cart_price = sqlite3_column_int(statement, 5);
  3211. int can_create_portfolio = sqlite3_column_int(statement, 6);
  3212. int can_delete_order = sqlite3_column_int(statement, 7);
  3213. int can_submit_order = sqlite3_column_int(statement, 8);
  3214. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  3215. int can_create_order = sqlite3_column_int(statement, 10);
  3216. char *mode = (char*)sqlite3_column_text(statement, 11);
  3217. if(mode==nil)
  3218. mode="";
  3219. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  3220. char *username = (char*)sqlite3_column_text(statement, 12);
  3221. if(username==nil)
  3222. username="";
  3223. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  3224. int can_update_contact_info = sqlite3_column_int(statement, 13);
  3225. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  3226. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  3227. [header setValue:nscontact_id forKey:@"contact_id"];
  3228. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  3229. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  3230. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  3231. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  3232. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  3233. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  3234. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  3235. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  3236. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  3237. [header setValue:nsusername forKey:@"username"];
  3238. NSError* error=nil;
  3239. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  3240. [header setValue:statusFilter forKey:@"statusFilter"];
  3241. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3242. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  3243. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  3244. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  3245. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  3246. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  3247. [ret setObject:header forKey:@"header"];
  3248. [ret setValue:nsmode forKey:@"mode"];
  3249. }
  3250. sqlite3_finalize(statement);
  3251. }
  3252. [iSalesDB close_db:db];
  3253. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3254. return [RAUtils dict2data:ret];
  3255. }
  3256. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  3257. {
  3258. NSString* contactId = [params valueForKey:@"contactId"];
  3259. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3260. sqlite3 *db = [iSalesDB get_db];
  3261. NSString *sqlQuery = nil;
  3262. {
  3263. 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,type from offline_contact where contact_id='%@'",contactId];
  3264. }
  3265. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  3266. sqlite3_stmt * statement;
  3267. [ret setValue:@"2" forKey:@"result"];
  3268. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3269. {
  3270. //int i = 0;
  3271. if (sqlite3_step(statement) == SQLITE_ROW)
  3272. {
  3273. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3274. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3275. // int editable = sqlite3_column_int(statement, 0);
  3276. char *company_name = (char*)sqlite3_column_text(statement, 1);
  3277. NSString *nscompany_name =nil;
  3278. if(company_name==nil)
  3279. nscompany_name=@"";
  3280. else
  3281. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  3282. char *country = (char*)sqlite3_column_text(statement, 2);
  3283. if(country==nil)
  3284. country="";
  3285. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  3286. // char *addr = (char*)sqlite3_column_text(statement, 3);
  3287. // if(addr==nil)
  3288. // addr="";
  3289. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  3290. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  3291. if(zipcode==nil)
  3292. zipcode="";
  3293. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  3294. char *state = (char*)sqlite3_column_text(statement, 5);
  3295. if(state==nil)
  3296. state="";
  3297. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  3298. char *city = (char*)sqlite3_column_text(statement, 6);
  3299. if(city==nil)
  3300. city="";
  3301. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  3302. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  3303. // NSString *nscontact_name = nil;
  3304. // if(contact_name==nil)
  3305. // nscontact_name=@"";
  3306. // else
  3307. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  3308. char *phone = (char*)sqlite3_column_text(statement, 8);
  3309. NSString *nsphone = nil;
  3310. if(phone==nil)
  3311. nsphone=@"";
  3312. else
  3313. nsphone= [[NSString alloc]initWithUTF8String:phone];
  3314. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  3315. if(contact_id==nil)
  3316. contact_id="";
  3317. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3318. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  3319. if(addr_1==nil)
  3320. addr_1="";
  3321. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  3322. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  3323. if(addr_2==nil)
  3324. addr_2="";
  3325. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  3326. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  3327. if(addr_3==nil)
  3328. addr_3="";
  3329. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  3330. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  3331. if(addr_4==nil)
  3332. addr_4="";
  3333. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  3334. char *first_name = (char*)sqlite3_column_text(statement, 14);
  3335. if(first_name==nil)
  3336. first_name="";
  3337. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  3338. char *last_name = (char*)sqlite3_column_text(statement, 15);
  3339. if(last_name==nil)
  3340. last_name="";
  3341. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  3342. char *fax = (char*)sqlite3_column_text(statement, 16);
  3343. NSString *nsfax = nil;
  3344. if(fax==nil)
  3345. nsfax=@"";
  3346. else
  3347. nsfax= [[NSString alloc]initWithUTF8String:fax];
  3348. char *email = (char*)sqlite3_column_text(statement, 17);
  3349. NSString *nsemail = nil;
  3350. if(email==nil)
  3351. nsemail=@"";
  3352. else
  3353. nsemail= [[NSString alloc]initWithUTF8String:email];
  3354. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  3355. NSString *nsimg_0 = nil;
  3356. if(img_0==nil)
  3357. nsimg_0=@"";
  3358. else
  3359. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  3360. [self copy_bcardImg:nsimg_0];
  3361. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  3362. NSString *nsimg_1 = nil;
  3363. if(img_1==nil)
  3364. nsimg_1=@"";
  3365. else
  3366. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  3367. [self copy_bcardImg:nsimg_1];
  3368. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  3369. NSString *nsimg_2 = nil;
  3370. if(img_2==nil)
  3371. nsimg_2=@"";
  3372. else
  3373. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  3374. [self copy_bcardImg:nsimg_2];
  3375. char *price_type = (char*)sqlite3_column_text(statement, 21);
  3376. NSString *nsprice_type = nil;
  3377. if(price_type==nil)
  3378. nsprice_type=@"";
  3379. else
  3380. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  3381. char *notes = (char*)sqlite3_column_text(statement, 22);
  3382. NSString *nsnotes = nil;
  3383. if(notes==nil)
  3384. nsnotes=@"";
  3385. else
  3386. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  3387. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  3388. NSString *nssalesrep = nil;
  3389. if(salesrep==nil)
  3390. nssalesrep=@"";
  3391. else
  3392. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  3393. NSString *contact_type = [self textAtColumn:24 statement:statement];
  3394. {
  3395. // decrypt
  3396. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  3397. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  3398. nsphone=[AESCrypt fastdecrypt:nsphone];
  3399. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3400. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  3401. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3402. }
  3403. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3404. [arr_name addObject:nsfirst_name];
  3405. [arr_name addObject:nslast_name];
  3406. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  3407. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  3408. [arr_ext addObject:nsaddr_1];
  3409. [arr_ext addObject:nsaddr_2];
  3410. [arr_ext addObject:nsaddr_3];
  3411. [arr_ext addObject:nsaddr_4];
  3412. [arr_ext addObject:@"\r\n"];
  3413. [arr_ext addObject:nscity];
  3414. [arr_ext addObject:nsstate];
  3415. [arr_ext addObject:nszipcode];
  3416. [arr_ext addObject:nscountry];
  3417. NSString *nsext=[RAUtils arr2string:arr_ext separator:@", " trim:true];
  3418. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  3419. [item setValue:nsimg_2 forKey:@"business_card_2"];
  3420. [item setValue:nsimg_0 forKey:@"business_card_0"];
  3421. [item setValue:nscountry forKey:@"customer_country"];
  3422. [item setValue:nsphone forKey:@"customer_phone"];
  3423. [item setValue:nsimg_1 forKey:@"business_card_1"];
  3424. [item setValue:nscompany_name forKey:@"customer_name"];
  3425. [item setValue:nsprice_type forKey:@"customer_price_type"];
  3426. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  3427. [item setValue:nsext forKey:@"customer_contact_ext"];
  3428. [item setValue:nszipcode forKey:@"customer_zipcode"];
  3429. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  3430. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  3431. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  3432. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  3433. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  3434. [item setValue:nslast_name forKey:@"customer_last_name"];
  3435. [item setValue:nscity forKey:@"customer_city"];
  3436. [item setValue:nsstate forKey:@"customer_state"];
  3437. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  3438. [item setValue:contactId forKey:@"customer_cid"];
  3439. [item setValue:nscontact_name forKey:@"customer_contact"];
  3440. [item setValue:nsfax forKey:@"customer_fax"];
  3441. [item setValue:nsemail forKey:@"customer_email"];
  3442. [item setValue:contact_type forKey:@"customer_contact_type"];
  3443. [ret setObject:item forKey:@"customerInfo"];
  3444. // i++;
  3445. }
  3446. UIApplication * app = [UIApplication sharedApplication];
  3447. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3448. [ret setValue:appDelegate.mode forKey:@"mode"];
  3449. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  3450. sqlite3_finalize(statement);
  3451. }
  3452. [iSalesDB close_db:db];
  3453. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3454. return ret;
  3455. }
  3456. + (bool) copy_bcardImg:(NSString*) filename
  3457. {
  3458. if(filename.length==0)
  3459. return false;
  3460. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3461. bool ret=false;
  3462. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3463. NSString *cachefolder = [paths objectAtIndex:0];
  3464. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  3465. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  3466. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  3467. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  3468. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  3469. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  3470. //
  3471. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  3472. NSFileManager* fileManager = [NSFileManager defaultManager];
  3473. BOOL bdir=NO;
  3474. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  3475. {
  3476. NSError *error = nil;
  3477. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  3478. {
  3479. ret=false;
  3480. }
  3481. else
  3482. {
  3483. ret=true;
  3484. }
  3485. // NSError *error = nil;
  3486. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  3487. //
  3488. // if(!bsuccess)
  3489. // {
  3490. // DebugLog(@"Create offline_createimg folder failed");
  3491. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3492. // return [RAUtils dict2data:ret];
  3493. // }
  3494. // if(bsuccess)
  3495. // {
  3496. // sqlite3 *db = [self get_db];
  3497. //
  3498. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  3499. // [iSalesDB close_db:db];
  3500. // }
  3501. }
  3502. return ret;
  3503. //
  3504. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  3505. // if(bsuccess)
  3506. // {
  3507. // NSError *error = nil;
  3508. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  3509. // {
  3510. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3511. // }
  3512. // else
  3513. // {
  3514. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  3515. //
  3516. // ret[@"img_url_aname"]=filename;
  3517. // ret[@"img_url"]=savedImagePath;
  3518. // }
  3519. // }
  3520. }
  3521. +(NSData *) offline_saveBusinesscard:(NSData *) image
  3522. {
  3523. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3524. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3525. NSString *cachefolder = [paths objectAtIndex:0];
  3526. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  3527. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  3528. NSFileManager* fileManager = [NSFileManager defaultManager];
  3529. BOOL bdir=YES;
  3530. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  3531. {
  3532. NSError *error = nil;
  3533. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  3534. if(!bsuccess)
  3535. {
  3536. DebugLog(@"Create offline_createimg folder failed");
  3537. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3538. return [RAUtils dict2data:ret];
  3539. }
  3540. // if(bsuccess)
  3541. // {
  3542. // sqlite3 *db = [self get_db];
  3543. //
  3544. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  3545. // [iSalesDB close_db:db];
  3546. // }
  3547. }
  3548. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  3549. //JEPG格式
  3550. //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  3551. NSString* filename =[NSString stringWithFormat:@"%@.png",[[NSUUID UUID] UUIDString]];
  3552. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  3553. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  3554. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  3555. if(bsuccess)
  3556. {
  3557. NSError *error = nil;
  3558. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  3559. {
  3560. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3561. }
  3562. else
  3563. {
  3564. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  3565. ret[@"img_url_aname"]=filename;
  3566. ret[@"img_url"]=filename;
  3567. }
  3568. }
  3569. else
  3570. {
  3571. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3572. }
  3573. return [RAUtils dict2data:ret];
  3574. }
  3575. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  3576. {
  3577. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  3578. category = [category substringToIndex:3];
  3579. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3580. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  3581. params[@"category"]= category;
  3582. ret[@"params"]= params;
  3583. [ret setValue:@"detail" forKey:@"target"];
  3584. [ret setValue:@"popup" forKey:@"action"];
  3585. [ret setValue:@"content" forKey:@"type"];
  3586. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  3587. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  3588. [ret setValue:@"true" forKey:@"single_row"];
  3589. [ret setValue:@"true" forKey:@"partial_refresh"];
  3590. // sqlite3 *db = [iSalesDB get_db];
  3591. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  3592. 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 ;
  3593. sqlite3_stmt * statement;
  3594. int count = 0;
  3595. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  3596. // int count=0;
  3597. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3598. {
  3599. int i=0;
  3600. while (sqlite3_step(statement) == SQLITE_ROW)
  3601. {
  3602. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3603. // char *name = (char*)sqlite3_column_text(statement, 1);
  3604. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3605. char *name = (char*)sqlite3_column_text(statement, 0);
  3606. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3607. int product_id = sqlite3_column_int(statement, 1);
  3608. char *url = (char*)sqlite3_column_text(statement, 2);
  3609. if(url==nil)
  3610. url="";
  3611. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3612. [item setValue:nsurl forKey:@"picture_path"];
  3613. [item setValue:nsname forKey:@"fash_name"];
  3614. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3615. [item setValue:category forKey:@"category"];
  3616. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3617. i++;
  3618. }
  3619. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3620. sqlite3_finalize(statement);
  3621. }
  3622. DebugLog(@"count:%d",count);
  3623. // [iSalesDB close_db:db];
  3624. return ret;
  3625. }
  3626. +(NSDictionary*) model_priceKvItem:(NSString*) key value:(NSString*)value
  3627. {
  3628. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3629. [ret setValue:key forKey:@"key"];
  3630. [ret setValue:value forKey:@"val"];
  3631. [ret setValue:@"price" forKey:@"type"];
  3632. return ret;
  3633. }
  3634. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  3635. {
  3636. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3637. [ret setValue:key forKey:@"key"];
  3638. [ret setValue:value forKey:@"val"];
  3639. return ret;
  3640. }
  3641. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  3642. {
  3643. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3644. [ret setValue:@"0" forKey:@"img_count"];
  3645. // sqlite3 *db = [iSalesDB get_db];
  3646. 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 ;
  3647. sqlite3_stmt * statement;
  3648. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3649. {
  3650. int i=0;
  3651. if (sqlite3_step(statement) == SQLITE_ROW)
  3652. {
  3653. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3654. // char *name = (char*)sqlite3_column_text(statement, 1);
  3655. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3656. char *value = (char*)sqlite3_column_text(statement, 0);
  3657. if(value==nil)
  3658. value="";
  3659. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  3660. char *key = (char*)sqlite3_column_text(statement, 1);
  3661. if(key==nil)
  3662. key="";
  3663. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  3664. [item setValue:nsvalue forKey:@"val"];
  3665. [item setValue:nskey forKey:@"key"];
  3666. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3667. [ret setValue:@"1" forKey:@"count"];
  3668. i++;
  3669. }
  3670. sqlite3_finalize(statement);
  3671. }
  3672. // [iSalesDB close_db:db];
  3673. return ret;
  3674. }
  3675. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  3676. {
  3677. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3678. [ret setValue:@"0" forKey:@"count"];
  3679. // sqlite3 *db = [iSalesDB get_db];
  3680. 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;
  3681. sqlite3_stmt * statement;
  3682. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3683. {
  3684. int i=0;
  3685. while (sqlite3_step(statement) == SQLITE_ROW)
  3686. {
  3687. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3688. // char *name = (char*)sqlite3_column_text(statement, 1);
  3689. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3690. char *value = (char*)sqlite3_column_text(statement, 0);
  3691. if(value==nil)
  3692. value="";
  3693. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  3694. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  3695. if(selector_display==nil)
  3696. selector_display="";
  3697. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  3698. int product_id = sqlite3_column_int(statement, 2);
  3699. char *category = (char*)sqlite3_column_text(statement, 3);
  3700. if(category==nil)
  3701. category="";
  3702. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  3703. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  3704. [item setValue:nsvalue forKey:@"title"];
  3705. [item setValue:url forKey:@"pic_url"];
  3706. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  3707. [params setValue:@"2" forKey:@"count"];
  3708. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  3709. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  3710. [param0 setValue:@"product_id" forKey:@"name"];
  3711. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  3712. [param1 setValue:nscategory forKey:@"val"];
  3713. [param1 setValue:@"category" forKey:@"name"];
  3714. [params setObject:param0 forKey:@"param_0"];
  3715. [params setObject:param1 forKey:@"param_1"];
  3716. [item setObject:params forKey:@"params"];
  3717. [ret setValue:nsselector_display forKey:@"name"];
  3718. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3719. i++;
  3720. }
  3721. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3722. [ret setValue:@"switch" forKey:@"action"];
  3723. sqlite3_finalize(statement);
  3724. }
  3725. // [iSalesDB close_db:db];
  3726. return ret;
  3727. }
  3728. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  3729. {
  3730. // model 在 category search 显示的图片。
  3731. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  3732. // sqlite3 *db = [iSalesDB get_db];
  3733. 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];
  3734. sqlite3_stmt * statement;
  3735. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3736. {
  3737. while (sqlite3_step(statement) == SQLITE_ROW)
  3738. {
  3739. char *url = (char*)sqlite3_column_text(statement, 0);
  3740. if(url==nil)
  3741. url="";
  3742. int type = sqlite3_column_int(statement, 1);
  3743. if(type==0)
  3744. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  3745. else
  3746. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  3747. }
  3748. sqlite3_finalize(statement);
  3749. }
  3750. // [iSalesDB close_db:db];
  3751. return ret;
  3752. }
  3753. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  3754. {
  3755. int item_id=-1;
  3756. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  3757. sqlite3_stmt * statement;
  3758. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3759. {
  3760. if (sqlite3_step(statement) == SQLITE_ROW)
  3761. {
  3762. item_id = sqlite3_column_int(statement, 0);
  3763. }
  3764. sqlite3_finalize(statement);
  3765. }
  3766. return item_id;
  3767. }
  3768. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  3769. {
  3770. // NSString* ret = @"";
  3771. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  3772. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  3773. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  3774. sqlite3_stmt * statement;
  3775. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3776. {
  3777. while (sqlite3_step(statement) == SQLITE_ROW)
  3778. {
  3779. int bitem_id = sqlite3_column_int(statement, 0);
  3780. int bitem_qty = sqlite3_column_int(statement, 1);
  3781. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  3782. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  3783. }
  3784. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  3785. sqlite3_finalize(statement);
  3786. }
  3787. // if(ret==nil)
  3788. // ret=@"";
  3789. *count=arr_count;
  3790. return arr_bundle;
  3791. }
  3792. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  3793. {
  3794. // get default sold qty, return -1 if model not found;
  3795. int ret = -1;
  3796. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  3797. sqlite3_stmt * statement;
  3798. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3799. {
  3800. if (sqlite3_step(statement) == SQLITE_ROW)
  3801. {
  3802. ret = sqlite3_column_int(statement, 0);
  3803. }
  3804. sqlite3_finalize(statement);
  3805. }
  3806. return ret;
  3807. }
  3808. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  3809. {
  3810. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3811. [ret setValue:@"0" forKey:@"img_count"];
  3812. // sqlite3 *db = [iSalesDB get_db];
  3813. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  3814. sqlite3_stmt * statement;
  3815. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3816. {
  3817. int i=0;
  3818. while (sqlite3_step(statement) == SQLITE_ROW)
  3819. {
  3820. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3821. // char *name = (char*)sqlite3_column_text(statement, 1);
  3822. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3823. char *url = (char*)sqlite3_column_text(statement, 0);
  3824. if(url==nil)
  3825. url="";
  3826. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3827. [item setValue:nsurl forKey:@"s"];
  3828. [item setValue:nsurl forKey:@"l"];
  3829. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  3830. i++;
  3831. }
  3832. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  3833. sqlite3_finalize(statement);
  3834. }
  3835. // [iSalesDB close_db:db];
  3836. return ret;
  3837. }
  3838. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  3839. UIApplication * app = [UIApplication sharedApplication];
  3840. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3841. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  3842. sqlite3 *db = [iSalesDB get_db];
  3843. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  3844. NSString* product_id=params[@"product_id"];
  3845. NSString *item_count_str = params[@"item_count"];
  3846. NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  3847. NSArray *item_count_arr = item_count_str ? [RAUtils string2arr:item_count_str separator:@","] : nil;
  3848. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  3849. // NSString *sql = @"";
  3850. for(int i=0;i<arr.count;i++)
  3851. {
  3852. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  3853. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  3854. __block int cart_count = 0;
  3855. if (!item_count_str) {
  3856. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  3857. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3858. NSString *model_set = [self textAtColumn:0 statement:stmt];
  3859. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  3860. cart_count = [[model_set_components lastObject] integerValue];
  3861. }];
  3862. }
  3863. if(count==0)
  3864. {
  3865. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  3866. sqlite3_stmt *stmt;
  3867. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  3868. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],[arr[i] length],NULL);
  3869. if (item_count_arr) {
  3870. sqlite3_bind_int(stmt,2,[item_count_arr[i] integerValue]);
  3871. } else {
  3872. sqlite3_bind_int(stmt,2,cart_count);
  3873. }
  3874. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  3875. [iSalesDB execSql:@"ROLLBACK" db:db];
  3876. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  3877. [iSalesDB close_db:db];
  3878. DebugLog(@"add to wishlist error");
  3879. return ret;
  3880. }
  3881. } else {
  3882. int qty = 0;
  3883. if (item_count_arr) {
  3884. qty = [item_count_arr[i] integerValue];
  3885. } else {
  3886. qty = cart_count;
  3887. }
  3888. 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]];
  3889. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  3890. [iSalesDB execSql:@"ROLLBACK" db:db];
  3891. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  3892. [iSalesDB close_db:db];
  3893. DebugLog(@"add to wishlist error");
  3894. return ret;
  3895. }
  3896. }
  3897. }
  3898. // [iSalesDB execSql:sql db:db];
  3899. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3900. [iSalesDB execSql:@"END TRANSACTION" db:db];
  3901. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  3902. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  3903. [iSalesDB close_db:db];
  3904. appDelegate.wish_count =count;
  3905. [appDelegate update_count_mark];
  3906. ret[@"result"]= [NSNumber numberWithInt:2];
  3907. return ret;
  3908. }
  3909. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  3910. // 0 category
  3911. // 1 search
  3912. // 2 itemsearch
  3913. NSData *ret = nil;
  3914. NSDictionary *items = nil;
  3915. switch (from) {
  3916. case 0:{
  3917. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  3918. }
  3919. break;
  3920. case 1:{
  3921. items = [[self search:params limited:NO] objectForKey:@"items"];
  3922. }
  3923. break;
  3924. case 2:{
  3925. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  3926. }
  3927. break;
  3928. default:
  3929. break;
  3930. }
  3931. if (!items) {
  3932. return ret;
  3933. }
  3934. int count = [[items objectForKey:@"count"] intValue];
  3935. NSMutableString *product_id_str = [@"" mutableCopy];
  3936. for (int i = 0; i < count; i++) {
  3937. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  3938. NSDictionary *item = [items objectForKey:key];
  3939. NSString *product_id = [item objectForKey:@"product_id"];
  3940. if (i == 0) {
  3941. [product_id_str appendString:product_id];
  3942. } else {
  3943. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  3944. }
  3945. }
  3946. NSString *add_to = [self valueInParams:params key:@"addTo"];
  3947. if ([add_to isEqualToString:@"cart"]) {
  3948. NSString *order_code = [params objectForKey:@"orderCode"];
  3949. if (order_code.length) {
  3950. NSDictionary *newParams = @{
  3951. @"product_id" : product_id_str,
  3952. @"orderCode" : order_code
  3953. };
  3954. ret = [self offline_add2cart:newParams.mutableCopy];
  3955. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  3956. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  3957. ret = [RAUtils dict2data:retDic];
  3958. }
  3959. } else if([add_to isEqualToString:@"wishlist"]) {
  3960. NSDictionary *newParams = @{
  3961. @"product_id" : product_id_str
  3962. };
  3963. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  3964. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  3965. ret = [RAUtils dict2data:retDic];
  3966. } else if([add_to isEqualToString:@"portfolio"]) {
  3967. }
  3968. return ret;
  3969. }
  3970. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  3971. {
  3972. return [self addAll:params from:0];
  3973. }
  3974. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  3975. {
  3976. return [self addAll:params from:1];
  3977. }
  3978. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  3979. {
  3980. return [self addAll:params from:2];
  3981. }
  3982. #pragma mark - Jack
  3983. #warning 做SQL操作时转义!!
  3984. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  3985. // "val_227" : {
  3986. // "check" : 1,
  3987. // "value" : "US United States",
  3988. // "value_id" : "228"
  3989. // },
  3990. if (!countryCode) {
  3991. countryCode = @"US";
  3992. }
  3993. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  3994. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  3995. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  3996. int code_id = sqlite3_column_int(stmt, 3); // id
  3997. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3998. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  3999. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  4000. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4001. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  4002. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4003. }
  4004. long n = *count;
  4005. *count = n + 1;
  4006. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  4007. [container setValue:countryDic forKey:key];
  4008. }] mutableCopy];
  4009. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4010. return ret;
  4011. }
  4012. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  4013. countryCode = [self translateSingleQuote:countryCode];
  4014. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  4015. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4016. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  4017. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  4018. if (name == NULL) {
  4019. name = "";
  4020. }
  4021. if (code == NULL) {
  4022. code = "";
  4023. }
  4024. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4025. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4026. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  4027. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4028. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  4029. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4030. }
  4031. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4032. [container setValue:stateDic forKey:key];
  4033. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  4034. DebugLog(@"query all state error: %@",err_msg);
  4035. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4036. // [stateDic setValue:@"Other" forKey:@"value"];
  4037. // [stateDic setValue:@"" forKey:@"value_id"];
  4038. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4039. //
  4040. // if (state_code && [@"" isEqualToString:state_code]) {
  4041. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4042. // }
  4043. //
  4044. // NSString *key = [NSString stringWithFormat:@"val_0"];
  4045. // [container setValue:stateDic forKey:key];
  4046. }] mutableCopy];
  4047. [ret removeObjectForKey:@"result"];
  4048. // failure 可以不用了,一样的
  4049. if (ret.allKeys.count == 0) {
  4050. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4051. [stateDic setValue:@"Other" forKey:@"value"];
  4052. [stateDic setValue:@"" forKey:@"value_id"];
  4053. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4054. if (state_code && [@"" isEqualToString:state_code]) {
  4055. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4056. }
  4057. NSString *key = [NSString stringWithFormat:@"val_0"];
  4058. [ret setValue:stateDic forKey:key];
  4059. }
  4060. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4061. return ret;
  4062. }
  4063. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  4064. codeId = [self translateSingleQuote:codeId];
  4065. 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];
  4066. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4067. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  4068. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  4069. if (name == NULL) {
  4070. name = "";
  4071. }
  4072. if (code == NULL) {
  4073. code = "";
  4074. }
  4075. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4076. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4077. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  4078. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4079. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  4080. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4081. }
  4082. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4083. [container setValue:stateDic forKey:key];
  4084. }] mutableCopy];
  4085. [ret removeObjectForKey:@"result"];
  4086. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4087. return ret;
  4088. }
  4089. + (NSDictionary *)offline_getPrice {
  4090. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price where is_show = 1;"];
  4091. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4092. char *name = (char *) sqlite3_column_text(stmt, 1);
  4093. int type = sqlite3_column_int(stmt, 2);
  4094. int orderBy = sqlite3_column_int(stmt, 3);
  4095. if (name == NULL) {
  4096. name = "";
  4097. }
  4098. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  4099. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4100. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  4101. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4102. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4103. if (orderBy == 0) {
  4104. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4105. }
  4106. [container setValue:priceDic forKey:key];
  4107. }] mutableCopy];
  4108. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4109. return ret;
  4110. }
  4111. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  4112. NSString *sql = @"select _id,type_name from contact_type where is_show = 1";
  4113. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  4114. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  4115. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4116. int _id = sqlite3_column_int(stmt, 0);
  4117. NSString *name = [self textAtColumn:1 statement:stmt];
  4118. NSDictionary *typeDic = @{
  4119. @"value_id" : name,
  4120. @"value" : name,
  4121. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  4122. };
  4123. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  4124. [ret setValue:[NSNumber numberWithInt:++(*count)] forKey:@"count"];
  4125. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  4126. }];
  4127. return ret;
  4128. }
  4129. + (NSDictionary *)offline_getSalesRep {
  4130. // 首先从offline_login表中取出sales_code
  4131. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4132. NSString *user = app.user;
  4133. user = [self translateSingleQuote:user];
  4134. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  4135. __block NSString *user_code = @"";
  4136. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4137. char *code = (char *)sqlite3_column_text(stmt, 0);
  4138. if (code == NULL) {
  4139. code = "";
  4140. }
  4141. user_code = [NSString stringWithUTF8String:code];
  4142. }];
  4143. // 再取所有salesRep
  4144. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  4145. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4146. // 1 name 2 code 3 salesrep_id
  4147. char *name = (char *)sqlite3_column_text(stmt, 1);
  4148. char *code = (char *)sqlite3_column_text(stmt, 2);
  4149. int salesrep_id = sqlite3_column_int(stmt, 3);
  4150. if (name == NULL) {
  4151. name = "";
  4152. }
  4153. if (code == NULL) {
  4154. code = "";
  4155. }
  4156. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  4157. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  4158. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  4159. // 比较code 相等则check
  4160. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  4161. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4162. }
  4163. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  4164. }] mutableCopy];
  4165. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4166. return ret;
  4167. }
  4168. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  4169. zipcode = [self translateSingleQuote:zipcode];
  4170. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  4171. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4172. char *country = (char *)sqlite3_column_text(stmt, 0);
  4173. char *state = (char *)sqlite3_column_text(stmt, 1);
  4174. char *city = (char *)sqlite3_column_text(stmt, 2);
  4175. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  4176. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  4177. if (country == NULL) {
  4178. country = "";
  4179. }
  4180. if (state == NULL) {
  4181. state = "";
  4182. }
  4183. if (city == NULL) {
  4184. city = "";
  4185. }
  4186. if (country_code == NULL) {
  4187. country_code = "";
  4188. }
  4189. if (state_code == NULL) {
  4190. state_code = "";
  4191. }
  4192. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  4193. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  4194. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  4195. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  4196. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  4197. }] mutableCopy];
  4198. return ret;
  4199. }
  4200. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  4201. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  4202. [item setValue:value forKey:valueKey];
  4203. [dic setValue:item forKey:itemKey];
  4204. }
  4205. + (NSString *)countryCodeByid:(NSString *)code_id {
  4206. NSString *ret = nil;
  4207. code_id = [self translateSingleQuote:code_id];
  4208. sqlite3 *db = [iSalesDB get_db];
  4209. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  4210. sqlite3_stmt * statement;
  4211. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4212. while (sqlite3_step(statement) == SQLITE_ROW) {
  4213. char *code = (char *)sqlite3_column_text(statement, 0);
  4214. if (code == NULL) {
  4215. code = "";
  4216. }
  4217. ret = [NSString stringWithUTF8String:code];
  4218. }
  4219. sqlite3_finalize(statement);
  4220. }
  4221. [iSalesDB close_db:db];
  4222. return ret;
  4223. }
  4224. + (NSString *)countryCodeIdByCode:(NSString *)code {
  4225. NSString *ret = nil;
  4226. code = [self translateSingleQuote:code];
  4227. sqlite3 *db = [iSalesDB get_db];
  4228. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  4229. sqlite3_stmt * statement;
  4230. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4231. while (sqlite3_step(statement) == SQLITE_ROW) {
  4232. char *_id = (char *)sqlite3_column_text(statement, 0);
  4233. if (_id == NULL) {
  4234. _id = "";
  4235. }
  4236. ret = [NSString stringWithFormat:@"%s",_id];
  4237. }
  4238. sqlite3_finalize(statement);
  4239. }
  4240. [iSalesDB close_db:db];
  4241. return ret;
  4242. }
  4243. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  4244. NSString *name = nil;
  4245. codeId = [self translateSingleQuote:codeId];
  4246. sqlite3 *db = [iSalesDB get_db];
  4247. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  4248. sqlite3_stmt * statement;
  4249. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4250. while (sqlite3_step(statement) == SQLITE_ROW) {
  4251. char *value = (char *)sqlite3_column_text(statement, 0);
  4252. if (value == NULL) {
  4253. value = "";
  4254. }
  4255. name = [NSString stringWithUTF8String:value];
  4256. }
  4257. sqlite3_finalize(statement);
  4258. }
  4259. [iSalesDB close_db:db];
  4260. return name;
  4261. }
  4262. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  4263. NSString *ret = nil;
  4264. sqlite3 *db = [iSalesDB get_db];
  4265. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  4266. sqlite3_stmt * statement;
  4267. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4268. while (sqlite3_step(statement) == SQLITE_ROW) {
  4269. char *name = (char *)sqlite3_column_text(statement, 0);
  4270. if (name == NULL) {
  4271. name = "";
  4272. }
  4273. ret = [NSString stringWithUTF8String:name];
  4274. }
  4275. sqlite3_finalize(statement);
  4276. }
  4277. [iSalesDB close_db:db];
  4278. return ret;
  4279. }
  4280. + (NSString *)salesRepCodeById:(NSString *)_id {
  4281. NSString *ret = nil;
  4282. _id = [self translateSingleQuote:_id];
  4283. sqlite3 *db = [iSalesDB get_db];
  4284. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  4285. sqlite3_stmt * statement;
  4286. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4287. while (sqlite3_step(statement) == SQLITE_ROW) {
  4288. char *rep = (char *)sqlite3_column_text(statement, 0);
  4289. if (rep == NULL) {
  4290. rep = "";
  4291. }
  4292. ret = [NSString stringWithUTF8String:rep];
  4293. }
  4294. sqlite3_finalize(statement);
  4295. }
  4296. [iSalesDB close_db:db];
  4297. return ret;
  4298. }
  4299. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  4300. char *tx = (char *)sqlite3_column_text(stmt, col);
  4301. if (tx == NULL) {
  4302. tx = "";
  4303. }
  4304. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  4305. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  4306. if (!text) {
  4307. text = @"";
  4308. }
  4309. // 将字符全部为' '的字符串干掉
  4310. int spaceCount = 0;
  4311. for (int i = 0; i < text.length; i++) {
  4312. if ([text characterAtIndex:i] == ' ') {
  4313. spaceCount++;
  4314. }
  4315. }
  4316. if (spaceCount == text.length) {
  4317. text = @"";
  4318. }
  4319. return text;
  4320. }
  4321. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  4322. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  4323. NSData *data = [NSData dataWithContentsOfFile:path];
  4324. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4325. return ret;
  4326. }
  4327. + (NSString *)textFileName:(NSString *)name {
  4328. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  4329. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  4330. if (!text) {
  4331. text = @"";
  4332. }
  4333. return text;
  4334. }
  4335. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  4336. return [[dic objectForKey:key] mutableCopy];
  4337. }
  4338. + (id)translateSingleQuote:(NSString *)string {
  4339. if ([string isKindOfClass:[NSString class]])
  4340. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4341. return string;
  4342. }
  4343. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  4344. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  4345. }
  4346. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  4347. NSString* ret= nil;
  4348. NSString *sqlQuery = nil;
  4349. // 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
  4350. 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];
  4351. sqlite3_stmt * statement;
  4352. // int count=0;
  4353. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4354. {
  4355. if (sqlite3_step(statement) == SQLITE_ROW)
  4356. {
  4357. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  4358. if(imgurl==nil)
  4359. imgurl="";
  4360. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  4361. ret=nsimgurl;
  4362. }
  4363. sqlite3_finalize(statement);
  4364. }
  4365. else
  4366. {
  4367. [ret setValue:@"8" forKey:@"result"];
  4368. }
  4369. // [iSalesDB close_db:db];
  4370. // DebugLog(@"data string: %@",ret );
  4371. return ret;
  4372. }
  4373. #pragma mark contact list
  4374. +(NSDictionary*) offline_contactlist :(NSMutableDictionary *) params
  4375. {
  4376. // contactType = "Sales_Order_Customer";
  4377. // limit = 25;
  4378. // offset = 0;
  4379. // password = 123456;
  4380. // "price_name" = 16;
  4381. // user = EvanK;
  4382. sqlite3 *db = [iSalesDB get_db];
  4383. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  4384. if (contactType) {
  4385. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  4386. } else {
  4387. contactType = @"1 = 1";
  4388. }
  4389. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  4390. DebugLog(@"offline contact list keyword: %@",keyword);
  4391. // advanced search
  4392. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  4393. if (contact_name) {
  4394. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4395. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  4396. } else {
  4397. contact_name = @"";
  4398. }
  4399. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  4400. if (customer_phone) {
  4401. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4402. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  4403. } else {
  4404. customer_phone = @"";
  4405. }
  4406. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  4407. if (customer_fax) {
  4408. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4409. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  4410. } else {
  4411. customer_fax = @"";
  4412. }
  4413. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  4414. if (customer_zipcode) {
  4415. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4416. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  4417. } else {
  4418. customer_zipcode = @"";
  4419. }
  4420. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  4421. if (customer_sales_rep) {
  4422. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4423. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  4424. } else {
  4425. customer_sales_rep = @"";
  4426. }
  4427. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  4428. if (customer_state) {
  4429. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4430. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  4431. } else {
  4432. customer_state = @"";
  4433. }
  4434. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  4435. if (customer_name) {
  4436. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4437. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  4438. } else {
  4439. customer_name = @"";
  4440. }
  4441. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  4442. if (customer_country) {
  4443. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4444. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  4445. } else {
  4446. customer_country = @"";
  4447. }
  4448. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  4449. if (customer_cid) {
  4450. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4451. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  4452. } else {
  4453. customer_cid = @"";
  4454. }
  4455. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  4456. if (customer_city) {
  4457. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4458. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  4459. } else {
  4460. customer_city = @"";
  4461. }
  4462. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  4463. if (customer_address) {
  4464. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4465. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  4466. } else {
  4467. customer_address = @"";
  4468. }
  4469. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  4470. if (customer_email) {
  4471. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4472. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  4473. } else {
  4474. customer_email = @"";
  4475. }
  4476. NSString *price_name = [params valueForKey:@"price_name"];
  4477. if (price_name) {
  4478. if ([price_name containsString:@","]) {
  4479. // 首先从 price表中查处name
  4480. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  4481. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  4482. for (int i = 1;i < pArray.count;i++) {
  4483. NSString *p = pArray[i];
  4484. [mutablePStr appendFormat:@" or type = %@ ",p];
  4485. }
  4486. [mutablePStr appendString:@";"];
  4487. __block NSMutableArray *price_name_array = [NSMutableArray array];
  4488. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4489. char *name = (char *)sqlite3_column_text(stmt, 0);
  4490. if (!name)
  4491. name = "";
  4492. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  4493. }];
  4494. // 再根据name 拼sql
  4495. NSMutableString *mutable_price_name = [NSMutableString string];
  4496. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  4497. for (int i = 1; i < price_name_array.count; i++) {
  4498. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  4499. }
  4500. [mutable_price_name appendString:@")"];
  4501. price_name = mutable_price_name;
  4502. } else {
  4503. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  4504. if ([price_name isEqualToString:@""]) {
  4505. price_name = @"";
  4506. } else {
  4507. __block NSString *price;
  4508. price_name = [self translateSingleQuote:price_name];
  4509. [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) {
  4510. char *p = (char *)sqlite3_column_text(stmt, 0);
  4511. if (p == NULL) {
  4512. p = "";
  4513. }
  4514. price = [NSString stringWithUTF8String:p];
  4515. }];
  4516. if ([price isEqualToString:@""]) {
  4517. price_name = @"";
  4518. } else {
  4519. price = [self translateSingleQuote:price];
  4520. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  4521. }
  4522. }
  4523. }
  4524. } else {
  4525. price_name = @"";
  4526. }
  4527. int limit = [[params valueForKey:@"limit"] intValue];
  4528. int offset = [[params valueForKey:@"offset"] intValue];
  4529. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4530. 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];
  4531. 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];
  4532. // int result= [iSalesDB AddExFunction:db];
  4533. int count =0;
  4534. NSString *sqlQuery = nil;
  4535. if(keyword.length==0)
  4536. {
  4537. // 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];
  4538. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  4539. sqlQuery = sql;
  4540. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  4541. }
  4542. else
  4543. {
  4544. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  4545. keyword = keyword.lowercaseString;
  4546. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4547. 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];
  4548. 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]];
  4549. }
  4550. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  4551. sqlite3_stmt * statement;
  4552. [ret setValue:@"2" forKey:@"result"];
  4553. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  4554. // sqlQuery = @"select editable,decrypt(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 Sales_Order_Customer = 1 and is_active=1 order by decrypt(company_name) limit 25 offset 0";
  4555. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4556. {
  4557. int i = 0;
  4558. while (sqlite3_step(statement) == SQLITE_ROW)
  4559. {
  4560. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4561. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4562. int editable = sqlite3_column_int(statement, 0);
  4563. char *company_name = (char*)sqlite3_column_text(statement, 1);
  4564. NSString *nscompany_name =nil;
  4565. if(company_name==nil)
  4566. nscompany_name=@"";
  4567. else
  4568. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  4569. char *country = (char*)sqlite3_column_text(statement, 2);
  4570. if(country==nil)
  4571. country="";
  4572. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  4573. // char *addr = (char*)sqlite3_column_text(statement, 3);
  4574. // if(addr==nil)
  4575. // addr="";
  4576. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  4577. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  4578. if(zipcode==nil)
  4579. zipcode="";
  4580. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  4581. char *state = (char*)sqlite3_column_text(statement, 5);
  4582. if(state==nil)
  4583. state="";
  4584. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  4585. char *city = (char*)sqlite3_column_text(statement, 6);
  4586. if(city==nil)
  4587. city="";
  4588. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  4589. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  4590. // NSString *nscontact_name = nil;
  4591. // if(contact_name==nil)
  4592. // nscontact_name=@"";
  4593. // else
  4594. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  4595. char *phone = (char*)sqlite3_column_text(statement, 8);
  4596. NSString *nsphone = nil;
  4597. if(phone==nil)
  4598. nsphone=@"";
  4599. else
  4600. nsphone= [[NSString alloc]initWithUTF8String:phone];
  4601. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  4602. if(contact_id==nil)
  4603. contact_id="";
  4604. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4605. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  4606. if(addr_1==nil)
  4607. addr_1="";
  4608. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4609. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  4610. if(addr_2==nil)
  4611. addr_2="";
  4612. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4613. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  4614. if(addr_3==nil)
  4615. addr_3="";
  4616. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4617. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  4618. if(addr_4==nil)
  4619. addr_4="";
  4620. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4621. char *first_name = (char*)sqlite3_column_text(statement, 14);
  4622. if(first_name==nil)
  4623. first_name="";
  4624. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  4625. char *last_name = (char*)sqlite3_column_text(statement, 15);
  4626. if(last_name==nil)
  4627. last_name="";
  4628. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  4629. char *fax = (char*)sqlite3_column_text(statement, 16);
  4630. NSString *nsfax = nil;
  4631. if(fax==nil)
  4632. nsfax=@"";
  4633. else
  4634. {
  4635. nsfax= [[NSString alloc]initWithUTF8String:fax];
  4636. if(nsfax.length>0)
  4637. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  4638. }
  4639. char *email = (char*)sqlite3_column_text(statement, 17);
  4640. NSString *nsemail = nil;
  4641. if(email==nil)
  4642. nsemail=@"";
  4643. else
  4644. {
  4645. nsemail= [[NSString alloc]initWithUTF8String:email];
  4646. if(nsemail.length>0)
  4647. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  4648. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  4649. }
  4650. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4651. [arr_name addObject:nsfirst_name];
  4652. [arr_name addObject:nslast_name];
  4653. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  4654. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  4655. {
  4656. // decrypt
  4657. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  4658. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  4659. nsphone=[AESCrypt fastdecrypt:nsphone];
  4660. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4661. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  4662. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4663. }
  4664. [arr_addr addObject:nscompany_name];
  4665. [arr_addr addObject:nscontact_name];
  4666. [arr_addr addObject:@"<br>"];
  4667. [arr_addr addObject:nsaddr_1];
  4668. [arr_addr addObject:nsaddr_2];
  4669. [arr_addr addObject:nsaddr_3];
  4670. [arr_addr addObject:nsaddr_4];
  4671. //[arr_addr addObject:nsaddr];
  4672. [arr_addr addObject:nszipcode];
  4673. [arr_addr addObject:nscity];
  4674. [arr_addr addObject:nsstate];
  4675. [arr_addr addObject:nscountry];
  4676. [arr_addr addObject:@"<br>"];
  4677. [arr_addr addObject:nsphone];
  4678. [arr_addr addObject:nsfax];
  4679. [arr_addr addObject:nsemail];
  4680. NSString * name = [RAUtils arr2string:arr_addr separator:@", " trim:true];
  4681. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  4682. [item setValue:name forKey:@"name"];
  4683. [item setValue:nscontact_id forKey:@"contact_id"];
  4684. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  4685. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4686. i++;
  4687. }
  4688. UIApplication * app = [UIApplication sharedApplication];
  4689. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4690. [ret setValue:appDelegate.mode forKey:@"mode"];
  4691. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  4692. sqlite3_finalize(statement);
  4693. }
  4694. [iSalesDB close_db:db];
  4695. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  4696. return ret;
  4697. }
  4698. #pragma mark contact Advanced search
  4699. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  4700. {
  4701. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  4702. return [RAUtils dict2data:contactAdvanceDic];
  4703. }
  4704. #pragma mark create new contact
  4705. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  4706. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  4707. NSData *data = [NSData dataWithContentsOfFile:path];
  4708. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4709. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  4710. NSString *countryCode = nil;
  4711. NSString *countryCode_id = nil;
  4712. NSString *stateCode = nil;
  4713. NSString *city = nil;
  4714. NSString *zipCode = nil;
  4715. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  4716. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  4717. NSString *code_id = params[@"country"];
  4718. countryCode_id = code_id;
  4719. countryCode = [self countryCodeByid:code_id];
  4720. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  4721. NSString *zip_code = params[@"zipcode"];
  4722. // 剔除全部为空格
  4723. int spaceCount = 0;
  4724. for (int i = 0; i < zip_code.length; i++) {
  4725. if ([zip_code characterAtIndex:i] == ' ') {
  4726. spaceCount++;
  4727. }
  4728. }
  4729. if (spaceCount == zip_code.length) {
  4730. zip_code = @"";
  4731. }
  4732. zipCode = zip_code;
  4733. if (zipCode.length > 0) {
  4734. countryCode_id = params[@"country"];
  4735. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  4736. countryCode = [dic valueForKey:@"country_code"];
  4737. if (!countryCode) {
  4738. // countryCode = @"US";
  4739. NSString *code_id = params[@"country"];
  4740. countryCode = [self countryCodeByid:code_id];
  4741. }
  4742. stateCode = [dic valueForKey:@"state_code"];
  4743. if (!stateCode.length) {
  4744. stateCode = params[@"state"];
  4745. }
  4746. city = [dic valueForKey:@"city"];
  4747. if (!city.length) {
  4748. city = params[@"city"];
  4749. }
  4750. // zip code
  4751. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  4752. [zipDic setValue:zipCode forKey:@"value"];
  4753. [section_0 setValue:zipDic forKey:@"item_11"];
  4754. } else {
  4755. NSString *code_id = params[@"country"];
  4756. countryCode = [self countryCodeByid:code_id];
  4757. stateCode = params[@"state"];
  4758. city = params[@"city"];
  4759. }
  4760. }
  4761. } else {
  4762. // default: US United States
  4763. countryCode = @"US";
  4764. countryCode_id = @"228";
  4765. }
  4766. // country
  4767. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  4768. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  4769. // state
  4770. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  4771. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  4772. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  4773. // city
  4774. if (city) {
  4775. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  4776. [cityDic setValue:city forKey:@"value"];
  4777. [section_0 setValue:cityDic forKey:@"item_13"];
  4778. }
  4779. // price type
  4780. NSDictionary *priceDic = [self offline_getPrice];
  4781. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  4782. // contact type
  4783. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  4784. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  4785. // Sales Rep
  4786. NSDictionary *repDic = [self offline_getSalesRep];
  4787. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  4788. [ret setValue:section_0 forKey:@"section_0"];
  4789. return [RAUtils dict2data:ret];
  4790. }
  4791. #pragma mark save
  4792. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  4793. NSString *addr = nil;
  4794. NSString *contact_name = nil;
  4795. NSString *companyName = [params objectForKey:@"company"];
  4796. if (companyName) {
  4797. companyName = [AESCrypt fastencrypt:companyName];
  4798. } else {
  4799. companyName = @"";
  4800. }
  4801. DebugLog(@"company");
  4802. companyName = [self translateSingleQuote:companyName];
  4803. NSString *addr1 = [params objectForKey:@"address"];
  4804. NSString *addr2 = [params objectForKey:@"address2"];
  4805. NSString *addr3 = [params objectForKey:@"address_3"];
  4806. NSString *addr4 = [params objectForKey:@"address_4"];
  4807. if (!addr2) {
  4808. addr2 = @"";
  4809. }
  4810. if (!addr3) {
  4811. addr3 = @"";
  4812. }
  4813. if (!addr4) {
  4814. addr4 = @"";
  4815. }
  4816. if (!addr1) {
  4817. addr1 = @"";
  4818. }
  4819. DebugLog(@"addr");
  4820. addr = [RAUtils arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  4821. addr = [AESCrypt fastencrypt:addr];
  4822. addr = [self translateSingleQuote:addr];
  4823. if (addr1 && ![addr1 isEqualToString:@""]) {
  4824. addr1 = [AESCrypt fastencrypt:addr1];
  4825. }
  4826. addr1 = [self translateSingleQuote:addr1];
  4827. addr2 = [self translateSingleQuote:addr2];
  4828. addr3 = [self translateSingleQuote:addr3];
  4829. addr4 = [self translateSingleQuote:addr4];
  4830. NSString *country = [params objectForKey:@"country"];
  4831. if (country) {
  4832. country = [self countryNameByCountryCodeId:country];
  4833. } else {
  4834. country = @"";
  4835. }
  4836. DebugLog(@"country");
  4837. country = [self translateSingleQuote:country];
  4838. NSString *state = [params objectForKey:@"state"];
  4839. if (!state) {
  4840. state = @"";
  4841. }
  4842. DebugLog(@"state");
  4843. state = [self translateSingleQuote:state];
  4844. NSString *city = [params objectForKey:@"city"];
  4845. if (!city) {
  4846. city = @"";
  4847. }
  4848. city = [self translateSingleQuote:city];
  4849. NSString *zipcode = [params objectForKey:@"zipcode"];
  4850. if (!zipcode) {
  4851. zipcode = @"";
  4852. }
  4853. DebugLog(@"zip");
  4854. zipcode = [self translateSingleQuote:zipcode];
  4855. NSString *fistName = [params objectForKey:@"firstname"];
  4856. if (!fistName) {
  4857. fistName = @"";
  4858. }
  4859. NSString *lastName = [params objectForKey:@"lastname"];
  4860. if (!lastName) {
  4861. lastName = @"";
  4862. }
  4863. contact_name = [RAUtils arr2string:@[fistName,lastName] separator:@" " trim:true];
  4864. DebugLog(@"contact_name");
  4865. contact_name = [self translateSingleQuote:contact_name];
  4866. fistName = [self translateSingleQuote:fistName];
  4867. lastName = [self translateSingleQuote:lastName];
  4868. NSString *phone = [params objectForKey:@"phone"];
  4869. if (phone) {
  4870. phone = [AESCrypt fastencrypt:phone];
  4871. } else {
  4872. phone = @"";
  4873. }
  4874. DebugLog(@"PHONE");
  4875. phone = [self translateSingleQuote:phone];
  4876. NSString *fax = [params objectForKey:@"fax"];
  4877. if (!fax) {
  4878. fax = @"";
  4879. }
  4880. DebugLog(@"FAX");
  4881. fax = [self translateSingleQuote:fax];
  4882. NSString *email = [params objectForKey:@"email"];
  4883. if (!email) {
  4884. email = @"";
  4885. }
  4886. DebugLog(@"EMAIL:%@",email);
  4887. email = [self translateSingleQuote:email];
  4888. NSString *notes = [params objectForKey:@"contact_notes"];
  4889. if (!notes) {
  4890. notes = @"";
  4891. }
  4892. DebugLog(@"NOTE:%@",notes);
  4893. notes = [self translateSingleQuote:notes];
  4894. NSString *price = [params objectForKey:@"price_name"];
  4895. if (price) {
  4896. price = [self priceNameByPriceId:price];
  4897. } else {
  4898. price = @"";
  4899. }
  4900. DebugLog(@"PRICE");
  4901. price = [self translateSingleQuote:price];
  4902. NSString *salesRep = [params objectForKey:@"sales_rep"];
  4903. if (salesRep) {
  4904. salesRep = [self salesRepCodeById:salesRep];
  4905. } else {
  4906. salesRep = @"";
  4907. }
  4908. salesRep = [self translateSingleQuote:salesRep];
  4909. NSString *img = [params objectForKey:@"business_card"];
  4910. NSArray *array = [img componentsSeparatedByString:@","];
  4911. NSString *img_0 = array[0];
  4912. if (!img_0) {
  4913. img_0 = @"";
  4914. }
  4915. img_0 = [self translateSingleQuote:img_0];
  4916. NSString *img_1 = array[1];
  4917. if (!img_1) {
  4918. img_1 = @"";
  4919. }
  4920. img_1 = [self translateSingleQuote:img_1];
  4921. NSString *img_2 = array[2];
  4922. if (!img_2) {
  4923. img_2 = @"";
  4924. }
  4925. img_2 = [self translateSingleQuote:img_2];
  4926. NSString *contact_id = [NSUUID UUID].UUIDString;
  4927. NSString *contact_type = [params objectForKey:@"type_name"];
  4928. if (!contact_type) {
  4929. contact_type = @"";
  4930. }
  4931. contact_type = [self translateSingleQuote:contact_type];
  4932. // 判断更新时是否为customer
  4933. if (update) {
  4934. contact_id = [params objectForKey:@"contact_id"];
  4935. if (!contact_id) {
  4936. contact_id = @"";
  4937. }
  4938. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  4939. __block int customer = 0;
  4940. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4941. customer = sqlite3_column_int(stmt, 0);
  4942. }];
  4943. isCustomer = customer ? YES : NO;
  4944. }
  4945. NSMutableDictionary *sync_dic = [params mutableCopy];
  4946. if (isCustomer) {
  4947. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  4948. } else {
  4949. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  4950. }
  4951. NSString *sync_data = nil;
  4952. NSString *sql = nil;
  4953. if (update){
  4954. contact_id = [params objectForKey:@"contact_id"];
  4955. if (!contact_id) {
  4956. contact_id = @"";
  4957. }
  4958. [sync_dic setValue:contact_id forKey:@"contact_id"];
  4959. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  4960. sync_data = [RAUtils dict2string:sync_dic];
  4961. sync_data = [self translateSingleQuote:sync_data];
  4962. 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 = '%@',type = '%@' 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_type,contact_id];
  4963. } else {
  4964. contact_id = [self translateSingleQuote:contact_id];
  4965. [sync_dic setValue:contact_id forKey:@"contact_id"];
  4966. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  4967. sync_data = [RAUtils dict2string:sync_dic];
  4968. sync_data = [self translateSingleQuote:sync_data];
  4969. 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,type) 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,contact_type];
  4970. }
  4971. int result = [iSalesDB execSql:sql];
  4972. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  4973. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  4974. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  4975. }
  4976. #pragma mark save new contact
  4977. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  4978. {
  4979. return [self offline_saveContact:params update:NO isCustomer:YES];
  4980. }
  4981. #pragma mark edit contact
  4982. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  4983. {
  4984. // {
  4985. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  4986. // password = 123456;
  4987. // user = EvanK;
  4988. // }
  4989. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  4990. NSData *data = [NSData dataWithContentsOfFile:path];
  4991. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4992. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  4993. NSString *countryCode = nil;
  4994. NSString *countryCode_id = nil;
  4995. NSString *stateCode = nil;
  4996. /*------contact infor------*/
  4997. __block NSString *country = nil;
  4998. __block NSString *company_name = nil;
  4999. __block NSString *contact_id = params[@"contact_id"];
  5000. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  5001. __block NSString *zipcode = nil;
  5002. __block NSString *state = nil; // state_code
  5003. __block NSString *city = nil; //
  5004. __block NSString *firt_name,*last_name;
  5005. __block NSString *phone,*fax,*email;
  5006. __block NSString *notes,*price_type,*sales_rep;
  5007. __block NSString *img_0,*img_1,*img_2;
  5008. __block NSString *contact_type;
  5009. contact_id = [self translateSingleQuote:contact_id];
  5010. 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,type from offline_contact where contact_id = '%@';",contact_id];
  5011. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5012. country = [self textAtColumn:0 statement:stmt]; // country name
  5013. company_name = [self textAtColumn:1 statement:stmt];
  5014. addr_1 = [self textAtColumn:2 statement:stmt];
  5015. addr_2 = [self textAtColumn:3 statement:stmt];
  5016. addr_3 = [self textAtColumn:4 statement:stmt];
  5017. addr_4 = [self textAtColumn:5 statement:stmt];
  5018. zipcode = [self textAtColumn:6 statement:stmt];
  5019. state = [self textAtColumn:7 statement:stmt]; // state code
  5020. city = [self textAtColumn:8 statement:stmt];
  5021. firt_name = [self textAtColumn:9 statement:stmt];
  5022. last_name = [self textAtColumn:10 statement:stmt];
  5023. phone = [self textAtColumn:11 statement:stmt];
  5024. fax = [self textAtColumn:12 statement:stmt];
  5025. email = [self textAtColumn:13 statement:stmt];
  5026. notes = [self textAtColumn:14 statement:stmt];
  5027. price_type = [self textAtColumn:15 statement:stmt]; // name
  5028. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  5029. img_0 = [self textAtColumn:17 statement:stmt];
  5030. img_1 = [self textAtColumn:18 statement:stmt];
  5031. img_2 = [self textAtColumn:19 statement:stmt];
  5032. contact_type = [self textAtColumn:20 statement:stmt];
  5033. }];
  5034. // decrypt
  5035. if (company_name) {
  5036. company_name = [AESCrypt fastdecrypt:company_name];
  5037. }
  5038. if (addr_1) {
  5039. addr_1 = [AESCrypt fastdecrypt:addr_1];
  5040. }
  5041. if (phone) {
  5042. phone = [AESCrypt fastdecrypt:phone];
  5043. }
  5044. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  5045. countryCode = [iSalesDB jk_queryText:countrySql];
  5046. stateCode = state;
  5047. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5048. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  5049. NSString *code_id = params[@"country"];
  5050. countryCode_id = code_id;
  5051. countryCode = [self countryCodeByid:code_id];
  5052. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  5053. NSString *zip_code = params[@"zipcode"];
  5054. // 剔除全部为空格
  5055. int spaceCount = 0;
  5056. for (int i = 0; i < zip_code.length; i++) {
  5057. if ([zip_code characterAtIndex:i] == ' ') {
  5058. spaceCount++;
  5059. }
  5060. }
  5061. if (spaceCount == zip_code.length) {
  5062. zip_code = @"";
  5063. }
  5064. if (zipcode.length > 0) {
  5065. zipcode = zip_code;
  5066. countryCode_id = params[@"country"];
  5067. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  5068. countryCode = [dic valueForKey:@"country_code"];
  5069. if (!countryCode) {
  5070. NSString *code_id = params[@"country"];
  5071. countryCode = [self countryCodeByid:code_id];
  5072. }
  5073. stateCode = [dic valueForKey:@"state_code"];
  5074. if (!stateCode.length) {
  5075. stateCode = params[@"state"];
  5076. }
  5077. city = [dic valueForKey:@"city"];
  5078. if (!city.length) {
  5079. city = params[@"city"];
  5080. }
  5081. // zip code
  5082. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  5083. [zipDic setValue:zipcode forKey:@"value"];
  5084. [section_0 setValue:zipDic forKey:@"item_8"];
  5085. } else {
  5086. NSString *code_id = params[@"country"];
  5087. countryCode = [self countryCodeByid:code_id];
  5088. stateCode = params[@"state"];
  5089. city = params[@"city"];
  5090. }
  5091. }
  5092. }
  5093. // 0 Country
  5094. // 1 Company Name
  5095. // 2 Contact ID
  5096. // 3 Picture
  5097. // 4 Address 1
  5098. // 5 Address 2
  5099. // 6 Address 3
  5100. // 7 Address 4
  5101. // 8 Zip Code
  5102. // 9 State/Province
  5103. // 10 City
  5104. // 11 Contact First Name
  5105. // 12 Contact Last Name
  5106. // 13 Phone
  5107. // 14 Fax
  5108. // 15 Email
  5109. // 16 Contact Notes
  5110. // 17 Price Type
  5111. // 18 Contact Type
  5112. // 19 Sales Rep
  5113. // country
  5114. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5115. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  5116. // company
  5117. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  5118. // contact_id
  5119. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  5120. // picture
  5121. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  5122. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  5123. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  5124. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  5125. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  5126. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  5127. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  5128. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  5129. // addr 1 2 3 4
  5130. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  5131. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  5132. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  5133. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  5134. // zip code
  5135. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  5136. // state
  5137. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5138. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  5139. // city
  5140. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  5141. // first last
  5142. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  5143. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  5144. // phone fax email
  5145. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  5146. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  5147. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  5148. // notes
  5149. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  5150. // price
  5151. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  5152. for (NSString *key in priceDic.allKeys) {
  5153. if ([key containsString:@"val_"]) {
  5154. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  5155. if ([dic[@"value"] isEqualToString:price_type]) {
  5156. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5157. [priceDic setValue:dic forKey:key];
  5158. }
  5159. }
  5160. }
  5161. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  5162. // Contact Rep
  5163. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  5164. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5165. // Sales Rep
  5166. NSMutableDictionary *repDic = [[self offline_getSalesRep] mutableCopy];
  5167. for (NSString *key in repDic.allKeys) {
  5168. if ([key containsString:@"val_"]) {
  5169. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  5170. NSString *value = dic[@"value"];
  5171. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  5172. if (code && [code isEqualToString:sales_rep]) {
  5173. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5174. [repDic setValue:dic forKey:key];
  5175. }
  5176. }
  5177. }
  5178. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  5179. [ret setValue:section_0 forKey:@"section_0"];
  5180. return [RAUtils dict2data:ret];
  5181. }
  5182. #pragma mark save contact
  5183. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  5184. {
  5185. return [self offline_saveContact:params update:YES isCustomer:YES];
  5186. }
  5187. #pragma mark category
  5188. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5189. if (ck) {
  5190. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  5191. for (NSString *key in res.allKeys) {
  5192. if (![key isEqualToString:@"count"]) {
  5193. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  5194. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5195. if ([val[@"value"] isEqualToString:ck]) {
  5196. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5197. }
  5198. [res setValue:val forKey:key];
  5199. }
  5200. }
  5201. [dic setValue:res forKey:valueKey];
  5202. }
  5203. }
  5204. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  5205. // NSString* orderCode = [params valueForKey:@"orderCode"];
  5206. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5207. NSString* category = [params valueForKey:@"category"];
  5208. if (!category || [category isEqualToString:@""]) {
  5209. category = @"%";
  5210. }
  5211. category = [self translateSingleQuote:category];
  5212. int limit = [[params valueForKey:@"limit"] intValue];
  5213. int offset = [[params valueForKey:@"offset"] intValue];
  5214. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5215. NSString *limit_str = @"";
  5216. if (limited) {
  5217. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  5218. }
  5219. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5220. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5221. sqlite3 *db = [iSalesDB get_db];
  5222. // [iSalesDB AddExFunction:db];
  5223. int count;
  5224. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  5225. 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];
  5226. double price_min = 0;
  5227. double price_max = 0;
  5228. if ([params.allKeys containsObject:@"alert"]) {
  5229. // alert
  5230. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5231. NSString *alert = params[@"alert"];
  5232. if ([alert isEqualToString:@"Display All"]) {
  5233. alert = [NSString stringWithFormat:@""];
  5234. } else {
  5235. alert = [self translateSingleQuote:alert];
  5236. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5237. }
  5238. // available
  5239. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5240. NSString *available = params[@"available"];
  5241. NSString *available_condition;
  5242. if ([available isEqualToString:@"Display All"]) {
  5243. available_condition = @"";
  5244. } else if ([available isEqualToString:@"Available Now"]) {
  5245. available_condition = @"and availability > 0";
  5246. } else {
  5247. available_condition = @"and availability == 0";
  5248. }
  5249. // best seller
  5250. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5251. NSString *best_seller = @"";
  5252. NSString *order_best_seller = @"m.name asc";
  5253. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5254. best_seller = @"and best_seller > 0";
  5255. order_best_seller = @"m.best_seller desc,m.name asc";
  5256. }
  5257. // price
  5258. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5259. NSString *price = params[@"price"];
  5260. price_min = 0;
  5261. price_max = MAXFLOAT;
  5262. if (appDelegate.user) {
  5263. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  5264. NSMutableString *priceName = [NSMutableString string];
  5265. for (int i = 0; i < priceTypeArray.count; i++) {
  5266. NSString *pricetype = priceTypeArray[i];
  5267. pricetype = [self translateSingleQuote:pricetype];
  5268. if (i == 0) {
  5269. [priceName appendFormat:@"'%@'",pricetype];
  5270. } else {
  5271. [priceName appendFormat:@",'%@'",pricetype];
  5272. }
  5273. }
  5274. if ([price isEqualToString:@"Display All"]) {
  5275. price = [NSString stringWithFormat:@""];
  5276. } else if([price containsString:@"+"]){
  5277. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5278. price_min = [price doubleValue];
  5279. 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];
  5280. } else {
  5281. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5282. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5283. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5284. 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];
  5285. }
  5286. } else {
  5287. price = @"";
  5288. }
  5289. // sold_by_qty : Sold in quantities of %@
  5290. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5291. NSString *qty = params[@"sold_by_qty"];
  5292. if ([qty isEqualToString:@"Display All"]) {
  5293. qty = @"";
  5294. } else {
  5295. qty = [self translateSingleQuote:qty];
  5296. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5297. }
  5298. // cate
  5299. category = [self translateSingleQuote:category];
  5300. NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  5301. // where bestseller > 0 order by bestseller desc
  5302. // sql query: alert availability(int) best_seller(int) price qty
  5303. 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];
  5304. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  5305. }
  5306. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  5307. if (!appDelegate.user) {
  5308. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  5309. }
  5310. [ret setValue:filter forKey:@"filter"];
  5311. DebugLog(@"offline_category sql:%@",sqlQuery);
  5312. sqlite3_stmt * statement;
  5313. [ret setValue:@"2" forKey:@"result"];
  5314. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  5315. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  5316. // int count=0;
  5317. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5318. {
  5319. int i=0;
  5320. while (sqlite3_step(statement) == SQLITE_ROW)
  5321. {
  5322. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5323. char *name = (char*)sqlite3_column_text(statement, 0);
  5324. if(name==nil)
  5325. name="";
  5326. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5327. char *description = (char*)sqlite3_column_text(statement, 1);
  5328. if(description==nil)
  5329. description="";
  5330. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  5331. int product_id = sqlite3_column_int(statement, 2);
  5332. int wid = sqlite3_column_int(statement, 3);
  5333. int closeout = sqlite3_column_int(statement, 4);
  5334. int cid = sqlite3_column_int(statement, 5);
  5335. int wisdelete = sqlite3_column_int(statement, 6);
  5336. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  5337. if(wid !=0 && wisdelete != 1)
  5338. [item setValue:@"true" forKey:@"wish_exists"];
  5339. else
  5340. [item setValue:@"false" forKey:@"wish_exists"];
  5341. if(closeout==0)
  5342. [item setValue:@"false" forKey:@"is_closeout"];
  5343. else
  5344. [item setValue:@"true" forKey:@"is_closeout"];
  5345. if(cid==0)
  5346. [item setValue:@"false" forKey:@"cart_exists"];
  5347. else
  5348. [item setValue:@"true" forKey:@"cart_exists"];
  5349. [item addEntriesFromDictionary:imgjson];
  5350. // [item setValue:nsurl forKey:@"img"];
  5351. [item setValue:nsname forKey:@"name"];
  5352. [item setValue:nsdescription forKey:@"description"];
  5353. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5354. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5355. i++;
  5356. }
  5357. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5358. [ret setObject:items forKey:@"items"];
  5359. sqlite3_finalize(statement);
  5360. } else {
  5361. DebugLog(@"nothing...");
  5362. }
  5363. DebugLog(@"count:%d",count);
  5364. [iSalesDB close_db:db];
  5365. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  5366. return ret;
  5367. }
  5368. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  5369. {
  5370. return [self categoryList:params limited:YES];
  5371. }
  5372. # pragma mark item search
  5373. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  5374. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5375. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  5376. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5377. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5378. // category
  5379. NSDictionary *category_menu = [self offline_category_menu];
  5380. [filter setValue:category_menu forKey:@"category"];
  5381. NSString* where= nil;
  5382. NSString* orderby= @"m.name";
  5383. if (!filterSearch) {
  5384. int covertype = [[params valueForKey:@"covertype"] intValue];
  5385. switch (covertype) {
  5386. case 0:
  5387. {
  5388. where=@"m.category like'%%#005#%%'";
  5389. break;
  5390. }
  5391. case 1:
  5392. {
  5393. where=@"m.alert like '%QS%'";
  5394. break;
  5395. }
  5396. case 2:
  5397. {
  5398. where=@"m.availability>0";
  5399. break;
  5400. }
  5401. case 3:
  5402. {
  5403. where=@"m.best_seller>0";
  5404. orderby=@"m.best_seller desc,m.name asc";
  5405. break;
  5406. }
  5407. default:
  5408. where=@"1=1";
  5409. break;
  5410. }
  5411. }
  5412. int limit = [[params valueForKey:@"limit"] intValue];
  5413. int offset = [[params valueForKey:@"offset"] intValue];
  5414. NSString *limit_str = @"";
  5415. if (limited) {
  5416. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  5417. }
  5418. sqlite3 *db = [iSalesDB get_db];
  5419. // [iSalesDB AddExFunction:db];
  5420. int count;
  5421. NSString *sqlQuery = nil;
  5422. where = [where stringByAppendingString:@" and m.is_active = 1"];
  5423. 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];
  5424. double price_min = 0;
  5425. double price_max = 0;
  5426. if (filterSearch) {
  5427. // alert
  5428. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5429. NSString *alert = params[@"alert"];
  5430. if ([alert isEqualToString:@"Display All"]) {
  5431. alert = [NSString stringWithFormat:@""];
  5432. } else {
  5433. alert = [self translateSingleQuote:alert];
  5434. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5435. }
  5436. // available
  5437. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5438. NSString *available = params[@"available"];
  5439. NSString *available_condition;
  5440. if ([available isEqualToString:@"Display All"]) {
  5441. available_condition = @"";
  5442. } else if ([available isEqualToString:@"Available Now"]) {
  5443. available_condition = @"and availability > 0";
  5444. } else {
  5445. available_condition = @"and availability == 0";
  5446. }
  5447. // best seller
  5448. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5449. NSString *best_seller = @"";
  5450. NSString *order_best_seller = @"m.name asc";
  5451. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5452. best_seller = @"and best_seller > 0";
  5453. order_best_seller = @"m.best_seller desc,m.name asc";
  5454. }
  5455. // price
  5456. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5457. NSString *price = params[@"price"];
  5458. price_min = 0;
  5459. price_max = MAXFLOAT;
  5460. if (appDelegate.user) {
  5461. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  5462. NSMutableString *priceName = [NSMutableString string];
  5463. for (int i = 0; i < priceTypeArray.count; i++) {
  5464. NSString *pricetype = priceTypeArray[i];
  5465. pricetype = [self translateSingleQuote:pricetype];
  5466. if (i == 0) {
  5467. [priceName appendFormat:@"'%@'",pricetype];
  5468. } else {
  5469. [priceName appendFormat:@",'%@'",pricetype];
  5470. }
  5471. }
  5472. if ([price isEqualToString:@"Display All"]) {
  5473. price = [NSString stringWithFormat:@""];
  5474. } else if([price containsString:@"+"]){
  5475. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5476. price_min = [price doubleValue];
  5477. 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];
  5478. } else {
  5479. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5480. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5481. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5482. 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];
  5483. }
  5484. } else {
  5485. price = @"";
  5486. }
  5487. // sold_by_qty : Sold in quantities of %@
  5488. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5489. NSString *qty = params[@"sold_by_qty"];
  5490. if ([qty isEqualToString:@"Display All"]) {
  5491. qty = @"";
  5492. } else {
  5493. qty = [self translateSingleQuote:qty];
  5494. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5495. }
  5496. // category
  5497. NSString *category_id = params[@"ctgId"];
  5498. NSMutableArray *cate_id_array = nil;
  5499. NSMutableString *cateWhere = [NSMutableString string];
  5500. if ([category_id isEqualToString:@""] || !category_id) {
  5501. [cateWhere appendString:@"1 = 1"];
  5502. } else {
  5503. if ([category_id containsString:@","]) {
  5504. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  5505. } else {
  5506. cate_id_array = [@[category_id] mutableCopy];
  5507. }
  5508. /*-----------*/
  5509. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];
  5510. for (int i = 0; i < cate_id_array.count; i++) {
  5511. for (NSString *key0 in cateDic.allKeys) {
  5512. if ([key0 containsString:@"category_"]) {
  5513. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy];
  5514. for (NSString *key1 in category0.allKeys) {
  5515. if ([key1 containsString:@"category_"]) {
  5516. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy];
  5517. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5518. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  5519. cate_id_array[i] = [category1 objectForKey:@"id"];
  5520. if (i == 0) {
  5521. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5522. } else {
  5523. [cateWhere appendFormat:@"or category like'%%#%@#%%'",cate_id_array[i]];
  5524. }
  5525. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5526. [category0 setValue:category1 forKey:key1];
  5527. [cateDic setValue:category0 forKey:key0];
  5528. }
  5529. }
  5530. }
  5531. }
  5532. }
  5533. }
  5534. [filter setValue:cateDic forKey:@"category"];
  5535. }
  5536. // where bestseller > 0 order by bestseller desc
  5537. // sql query: alert availability(int) best_seller(int) price qty
  5538. 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];
  5539. // count
  5540. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  5541. }
  5542. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  5543. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  5544. if (!appDelegate.user) {
  5545. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  5546. }
  5547. [ret setValue:filter forKey:@"filter"];
  5548. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  5549. sqlite3_stmt * statement;
  5550. [ret setValue:@"2" forKey:@"result"];
  5551. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  5552. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  5553. // int count=0;
  5554. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5555. {
  5556. int i=0;
  5557. while (sqlite3_step(statement) == SQLITE_ROW)
  5558. {
  5559. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5560. char *name = (char*)sqlite3_column_text(statement, 0);
  5561. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5562. char *description = (char*)sqlite3_column_text(statement, 1);
  5563. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  5564. int product_id = sqlite3_column_int(statement, 2);
  5565. int wid = sqlite3_column_int(statement, 3);
  5566. int closeout = sqlite3_column_int(statement, 4);
  5567. int cid = sqlite3_column_int(statement, 5);
  5568. int wisdelete = sqlite3_column_int(statement, 6);
  5569. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  5570. if(wid !=0 && wisdelete != 1)
  5571. [item setValue:@"true" forKey:@"wish_exists"];
  5572. else
  5573. [item setValue:@"false" forKey:@"wish_exists"];
  5574. if(closeout==0)
  5575. [item setValue:@"false" forKey:@"is_closeout"];
  5576. else
  5577. [item setValue:@"true" forKey:@"is_closeout"];
  5578. if(cid==0)
  5579. [item setValue:@"false" forKey:@"cart_exists"];
  5580. else
  5581. [item setValue:@"true" forKey:@"cart_exists"];
  5582. [item addEntriesFromDictionary:imgjson];
  5583. // [item setValue:nsurl forKey:@"img"];
  5584. [item setValue:nsname forKey:@"fash_name"];
  5585. [item setValue:nsdescription forKey:@"description"];
  5586. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5587. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5588. i++;
  5589. }
  5590. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5591. [ret setObject:items forKey:@"items"];
  5592. sqlite3_finalize(statement);
  5593. }
  5594. [iSalesDB close_db:db];
  5595. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  5596. return ret;
  5597. }
  5598. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  5599. {
  5600. return [self itemsearch:params limited:YES];
  5601. }
  5602. #pragma mark order detail
  5603. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  5604. if (str1.length == 0) {
  5605. str1 = @"&nbsp";
  5606. }
  5607. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  5608. return str;
  5609. }
  5610. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  5611. {
  5612. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  5613. [fromformatter setDateFormat:from];
  5614. NSDate *date = [fromformatter dateFromString:datetime];
  5615. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  5616. [toformatter setDateFormat:to];
  5617. NSString * ret = [toformatter stringFromDate:date];
  5618. return ret;
  5619. }
  5620. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  5621. // 把毫秒去掉
  5622. if ([dateTime containsString:@"."]) {
  5623. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  5624. }
  5625. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  5626. formatter.dateFormat = formate;
  5627. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  5628. NSDate *date = [formatter dateFromString:dateTime];
  5629. formatter.dateFormat = newFormate;
  5630. NSString *result = [formatter stringFromDate:date];
  5631. return result ? result : @"";
  5632. }
  5633. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  5634. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  5635. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  5636. }
  5637. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  5638. {
  5639. DebugLog(@"offline oderdetail params: %@",params);
  5640. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5641. int orderId = [params[@"orderId"] intValue];
  5642. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  5643. // decrypt card number and card security code
  5644. // 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 ];
  5645. 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];
  5646. sqlite3 *db = [iSalesDB get_db];
  5647. sqlite3_stmt * statement;
  5648. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  5649. NSString *nssoid = @"";
  5650. NSString* orderinfo = @"";
  5651. NSString *moreInfo = @"";
  5652. double handlingFee = 0;
  5653. double payments_and_credist = 0;
  5654. double totalPrice = 0;
  5655. double shippingFee = 0;
  5656. double lift_gate = 0;
  5657. NSString *customer_contact = @"";
  5658. NSString *customer_email = @"";
  5659. NSString *customer_fax = @"";
  5660. NSString *customer_phone = @"";
  5661. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  5662. {
  5663. if (sqlite3_step(statement) == SQLITE_ROW)
  5664. {
  5665. int order_id = sqlite3_column_int(statement, 0);
  5666. NSString *sign_url = [self textAtColumn:51 statement:statement];
  5667. ret[@"sign_url"] = sign_url;
  5668. customer_contact = [self textAtColumn:52 statement:statement];
  5669. customer_email = [self textAtColumn:53 statement:statement];
  5670. customer_phone = [self textAtColumn:54 statement:statement];
  5671. customer_fax = [self textAtColumn:55 statement:statement];
  5672. int offline_edit=sqlite3_column_int(statement, 56);
  5673. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  5674. char *soid = (char*)sqlite3_column_text(statement, 1);
  5675. if(soid==nil)
  5676. soid= "";
  5677. nssoid= [[NSString alloc]initWithUTF8String:soid];
  5678. // so#
  5679. ret[@"so#"] = nssoid;
  5680. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  5681. if(poNumber==nil)
  5682. poNumber= "";
  5683. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  5684. char *create_time = (char*)sqlite3_column_text(statement, 3);
  5685. if(create_time==nil)
  5686. create_time= "";
  5687. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  5688. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  5689. int status = sqlite3_column_int(statement, 4);
  5690. int erpStatus = sqlite3_column_int(statement, 49);
  5691. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  5692. // status
  5693. if (status > 1 && status != 3) {
  5694. status = erpStatus;
  5695. } else if (status == 3) {
  5696. status = 15;
  5697. }
  5698. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  5699. ret[@"order_status"] = nsstatus;
  5700. char *company_name = (char*)sqlite3_column_text(statement, 5);
  5701. if(company_name==nil)
  5702. company_name= "";
  5703. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  5704. // company name
  5705. ret[@"company_name"] = nscompany_name;
  5706. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  5707. if(customer_contact==nil)
  5708. customer_contact= "";
  5709. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  5710. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  5711. if(addr_1==nil)
  5712. addr_1="";
  5713. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  5714. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  5715. if(addr_2==nil)
  5716. addr_2="";
  5717. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  5718. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  5719. if(addr_3==nil)
  5720. addr_3="";
  5721. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  5722. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  5723. if(addr_4==nil)
  5724. addr_4="";
  5725. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  5726. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  5727. [arr_addr addObject:nsaddr_1];
  5728. [arr_addr addObject:nsaddr_2];
  5729. [arr_addr addObject:nsaddr_3];
  5730. [arr_addr addObject:nsaddr_4];
  5731. NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  5732. char *logist = (char*)sqlite3_column_text(statement, 11);
  5733. if(logist==nil)
  5734. logist= "";
  5735. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  5736. if (status == -11 || status == 10 || status == 11) {
  5737. nslogist = [self textAtColumn:59 statement:statement];
  5738. };
  5739. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  5740. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  5741. shipping = @"Shipping To Be Quoted";
  5742. } else {
  5743. shippingFee = sqlite3_column_double(statement, 12);
  5744. }
  5745. // Shipping
  5746. ret[@"Shipping"] = shipping;
  5747. int have_lift_gate = sqlite3_column_int(statement, 17);
  5748. lift_gate = sqlite3_column_double(statement, 13);
  5749. // Liftgate Fee(No loading dock)
  5750. if (!have_lift_gate) {
  5751. lift_gate = 0;
  5752. }
  5753. ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  5754. if (sqlite3_column_int(statement, 57)) {
  5755. ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  5756. }
  5757. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  5758. if(general_notes==nil)
  5759. general_notes= "";
  5760. NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  5761. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  5762. if(internal_notes==nil)
  5763. internal_notes= "";
  5764. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  5765. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  5766. if(payment_type==nil)
  5767. payment_type= "";
  5768. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  5769. // order info
  5770. orderinfo = [self textFileName:@"order_info.html"];
  5771. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  5772. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  5773. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  5774. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  5775. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  5776. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  5777. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  5778. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  5779. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  5780. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  5781. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  5782. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  5783. NSString *payment = nil;
  5784. if([nspayment_type isEqualToString:@"Credit Card"])
  5785. {
  5786. payment = [self textFileName:@"creditcardpayment.html"];
  5787. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  5788. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  5789. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  5790. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  5791. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  5792. NSString *card_type = [self textAtColumn:42 statement:statement];
  5793. if (card_type.length > 0) { // 显示星号
  5794. card_type = @"****";
  5795. }
  5796. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  5797. if (card_number.length > 0 && card_number.length > 4) {
  5798. for (int i = 0; i < card_number.length - 4; i++) {
  5799. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  5800. }
  5801. } else {
  5802. card_number = @"";
  5803. }
  5804. NSString *card_security_code = [self textAtColumn:44 statement:statement];
  5805. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  5806. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  5807. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  5808. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  5809. card_expiration = @"****";
  5810. }
  5811. NSString *card_city = [self textAtColumn:46 statement:statement];
  5812. NSString *card_state = [self textAtColumn:47 statement:statement];
  5813. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  5814. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  5815. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  5816. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  5817. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  5818. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  5819. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  5820. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  5821. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  5822. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  5823. }
  5824. else
  5825. {
  5826. payment=[self textFileName:@"normalpayment.html"];
  5827. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  5828. }
  5829. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  5830. ret[@"result"]= [NSNumber numberWithInt:2];
  5831. // more info
  5832. moreInfo = [self textFileName:@"more_info.html"];
  5833. /*****ship to******/
  5834. // ShipToCompany_or_&nbsp
  5835. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  5836. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  5837. NSString *shipToName = [self textAtColumn:19 statement:statement];
  5838. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  5839. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  5840. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  5841. /*****ship from******/
  5842. // ShipFromCompany_or_&nbsp
  5843. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  5844. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  5845. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  5846. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  5847. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  5848. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  5849. /*****freight to******/
  5850. // FreightBillToCompany_or_&nbsp
  5851. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  5852. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  5853. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  5854. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  5855. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  5856. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  5857. /*****merchandise to******/
  5858. // MerchandiseBillToCompany_or_&nbsp
  5859. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  5860. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  5861. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  5862. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  5863. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  5864. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  5865. /*****return to******/
  5866. // ReturnToCompany_or_&nbsp
  5867. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  5868. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  5869. NSString *returnToName = [self textAtColumn:31 statement:statement];
  5870. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  5871. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  5872. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  5873. //
  5874. // DebugLog(@"more info : %@",moreInfo);
  5875. // handling fee
  5876. handlingFee = sqlite3_column_double(statement, 33);
  5877. ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  5878. if (sqlite3_column_int(statement, 58)) {
  5879. ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  5880. }
  5881. //
  5882. // customer info
  5883. customerID = [self textAtColumn:36 statement:statement];
  5884. // mode
  5885. ret[@"mode"] = appDelegate.mode;
  5886. // model_count
  5887. ret[@"model_count"] = @(0);
  5888. }
  5889. sqlite3_finalize(statement);
  5890. }
  5891. [iSalesDB close_db:db];
  5892. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  5893. // "customer_email" = "Shui Hu";
  5894. // "customer_fax" = "";
  5895. // "customer_first_name" = F;
  5896. // "customer_last_name" = L;
  5897. // "customer_name" = ",da He Xiang Dong Liu A";
  5898. // "customer_phone" = "Hey Xuan Feng";
  5899. contactInfo[@"customer_phone"] = customer_phone;
  5900. contactInfo[@"customer_fax"] = customer_fax;
  5901. contactInfo[@"customer_email"] = customer_email;
  5902. NSString *first_name = @"";
  5903. NSString *last_name = @"";
  5904. if ([customer_contact isEqualToString:@""]) {
  5905. } else if ([customer_contact containsString:@" "]) {
  5906. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  5907. first_name = [customer_contact substringToIndex:first_space_index];
  5908. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  5909. }
  5910. contactInfo[@"customer_first_name"] = first_name;
  5911. contactInfo[@"customer_last_name"] = last_name;
  5912. ret[@"customerInfo"] = contactInfo;
  5913. // models
  5914. if (nssoid) {
  5915. __block double TotalCuft = 0;
  5916. __block double TotalWeight = 0;
  5917. __block int TotalCarton = 0;
  5918. __block double allItemPrice = 0;
  5919. 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];
  5920. sqlite3 *db1 = [iSalesDB get_db];
  5921. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5922. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  5923. int product_id = sqlite3_column_int(stmt, 0);
  5924. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  5925. int item_id = sqlite3_column_int(stmt, 7);
  5926. NSString* Price=nil;
  5927. if(str_price==nil)
  5928. {
  5929. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db1];
  5930. if(price==nil)
  5931. Price=@"No Price.";
  5932. else
  5933. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  5934. }
  5935. else
  5936. {
  5937. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  5938. }
  5939. double discount = sqlite3_column_double(stmt, 2);
  5940. int item_count = sqlite3_column_int(stmt, 3);
  5941. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  5942. NSString *nsline_note=nil;
  5943. if(line_note!=nil)
  5944. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  5945. // char *name = (char*)sqlite3_column_text(stmt, 5);
  5946. // NSString *nsname = nil;
  5947. // if(name!=nil)
  5948. // nsname= [[NSString alloc]initWithUTF8String:name];
  5949. NSString *nsname = [self textAtColumn:5 statement:stmt];
  5950. // char *description = (char*)sqlite3_column_text(stmt, 6);
  5951. // NSString *nsdescription=nil;
  5952. // if(description!=nil)
  5953. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  5954. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  5955. // int stockUom = sqlite3_column_int(stmt, 8);
  5956. // int _id = sqlite3_column_int(stmt, 9);
  5957. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  5958. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  5959. double weight=[bsubtotaljson[@"weight"] doubleValue];
  5960. int carton=[bsubtotaljson[@"carton"] intValue];
  5961. TotalCuft += cuft;
  5962. TotalWeight += weight;
  5963. TotalCarton += carton;
  5964. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID];
  5965. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  5966. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  5967. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  5968. itemjson[@"note"]=nsline_note;
  5969. itemjson[@"origin_price"] = Price;
  5970. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  5971. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  5972. itemjson[@"order_item_status"] = @""; // 暂时不处理
  5973. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  5974. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  5975. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  5976. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  5977. if(itemjson[@"combine"] != nil)
  5978. {
  5979. // int citem=0;
  5980. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  5981. for(int bc=0;bc<bcount;bc++)
  5982. {
  5983. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  5984. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  5985. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  5986. subTotal += uprice * modulus * item_count;
  5987. }
  5988. }
  5989. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  5990. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  5991. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  5992. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  5993. allItemPrice += subTotal;
  5994. }];
  5995. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  5996. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  5997. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  5998. // payments/Credits
  5999. // payments_and_credist = sqlite3_column_double(statement, 34);
  6000. payments_and_credist = allItemPrice;
  6001. ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6002. // // total
  6003. // totalPrice = sqlite3_column_double(statement, 35);
  6004. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6005. } else {
  6006. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  6007. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  6008. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  6009. // payments/Credits
  6010. payments_and_credist = 0;
  6011. ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6012. }
  6013. // total
  6014. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  6015. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6016. ret[@"order_info"]= orderinfo;
  6017. ret[@"more_order_info"] = moreInfo;
  6018. return [RAUtils dict2data:ret];
  6019. }
  6020. #pragma mark order list
  6021. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  6022. NSString *nsstatus = @"";
  6023. switch (status) {
  6024. case 0:
  6025. {
  6026. nsstatus=@"Temp Order";
  6027. break;
  6028. }
  6029. case 1:
  6030. {
  6031. nsstatus=@"Saved Order";
  6032. break;
  6033. }
  6034. case 2: {
  6035. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  6036. break;
  6037. }
  6038. case 3:
  6039. case 15:
  6040. {
  6041. nsstatus=@"Cancelled";
  6042. break;
  6043. }
  6044. case 10:
  6045. {
  6046. nsstatus=@"Quote Submitted";
  6047. break;
  6048. }
  6049. case 11:
  6050. {
  6051. nsstatus=@"Sales Order Submitted";
  6052. break;
  6053. }
  6054. case 12:
  6055. {
  6056. nsstatus=@"Processing";
  6057. break;
  6058. }
  6059. case 13:
  6060. {
  6061. nsstatus=@"Shipped";
  6062. break;
  6063. }
  6064. case 14:
  6065. {
  6066. nsstatus=@"Closed";
  6067. break;
  6068. }
  6069. case -11:
  6070. {
  6071. nsstatus = @"Ready For Submit";
  6072. break;
  6073. }
  6074. default:
  6075. break;
  6076. }
  6077. return nsstatus;
  6078. }
  6079. + (double) orderTotalPrice:(NSString *)so_id db:(sqlite3 *)db1 close:(BOOL)close{
  6080. double total = 0;
  6081. __block double payments_and_credist = 0;
  6082. __block double allItemPrice = 0;
  6083. // sqlite3 *db1 = [iSalesDB get_db];
  6084. if (so_id) {
  6085. // 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];
  6086. NSString *modelSql = [NSString stringWithFormat:@"select product_id,price,discount,item_count,item_id,customer_cid from (select c.product_id,decrypt(c.str_price) as price,c.discount,c.item_count,c.item_id,o.customer_cid from offline_cart as c left join offline_order as o on c.so_no = o.so_id where c.so_no='%@');",so_id];
  6087. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6088. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6089. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  6090. int product_id = sqlite3_column_int(stmt, 0);
  6091. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  6092. int discount = sqlite3_column_int(stmt, 2);
  6093. int item_count = sqlite3_column_int(stmt, 3);
  6094. // int item_id = sqlite3_column_int(stmt, 7);
  6095. int item_id = sqlite3_column_int(stmt, 4);
  6096. char *customer_cid = (char *)sqlite3_column_text(stmt, 5);
  6097. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  6098. NSString* Price=nil;
  6099. if(str_price==nil)
  6100. {
  6101. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db1];
  6102. if(price==nil)
  6103. Price=@"No Price.";
  6104. else
  6105. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6106. }
  6107. else
  6108. {
  6109. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  6110. }
  6111. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID];
  6112. itemjson[@"The unit price"]=Price;
  6113. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  6114. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  6115. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6116. if(itemjson[@"combine"] != nil)
  6117. {
  6118. // int citem=0;
  6119. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  6120. for(int bc=0;bc<bcount;bc++)
  6121. {
  6122. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6123. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6124. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6125. subTotal += uprice * modulus * item_count;
  6126. }
  6127. }
  6128. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  6129. allItemPrice += subTotal;
  6130. }];
  6131. payments_and_credist = allItemPrice;
  6132. } else {
  6133. // payments/Credits
  6134. payments_and_credist = 0;
  6135. }
  6136. // lift_gate handlingFee shippingFee
  6137. __block double lift_gate = 0;
  6138. __block double handlingFee = 0;
  6139. __block double shippingFee = 0;
  6140. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  6141. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6142. int have_lift_gate = sqlite3_column_int(stmt, 0);
  6143. if (have_lift_gate) {
  6144. lift_gate = sqlite3_column_double(stmt, 1);
  6145. }
  6146. handlingFee = sqlite3_column_double(stmt, 2);
  6147. shippingFee = sqlite3_column_double(stmt, 3);
  6148. }];
  6149. // total
  6150. total = payments_and_credist + lift_gate + handlingFee + shippingFee;
  6151. if (close) {
  6152. [iSalesDB close_db:db1];
  6153. }
  6154. return total;
  6155. }
  6156. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  6157. {
  6158. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  6159. int limit = [[params valueForKey:@"limit"] intValue];
  6160. int offset = [[params valueForKey:@"offset"] intValue];
  6161. NSString* keyword = [params valueForKey:@"keyWord"];
  6162. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  6163. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  6164. NSString* where=@"1 = 1";
  6165. if(keyword.length>0)
  6166. 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]];
  6167. if (orderStatus.length > 0) {
  6168. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  6169. if (order_status_array.count == 1) {
  6170. int status_value = [[order_status_array firstObject] integerValue];
  6171. if (status_value <= 1 || status_value == 3) {
  6172. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  6173. } else {
  6174. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  6175. }
  6176. } else if (order_status_array.count > 1) {
  6177. for (int i = 0; i < order_status_array.count;i++) {
  6178. NSString *status = order_status_array[i];
  6179. NSString *condition = @" or";
  6180. if (i == 0) {
  6181. condition = @" and (";
  6182. }
  6183. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  6184. int status_value = [status integerValue];
  6185. if (status_value <= 1 || status_value == 3) {
  6186. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  6187. } else {
  6188. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  6189. }
  6190. }
  6191. where = [where stringByAppendingString:@" )"];
  6192. }
  6193. }
  6194. 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.create_time desc limit %d offset %d" ,where, limit, offset];
  6195. // DebugLog(@"order list sql: %@",sqlQuery);
  6196. sqlite3 *db = [iSalesDB get_db];
  6197. sqlite3_stmt * statement;
  6198. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  6199. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6200. {
  6201. int count=0;
  6202. while (sqlite3_step(statement) == SQLITE_ROW)
  6203. {
  6204. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  6205. int order_id = sqlite3_column_double(statement, 0);
  6206. char *soid = (char*)sqlite3_column_text(statement, 1);
  6207. if(soid==nil)
  6208. soid= "";
  6209. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  6210. int status = sqlite3_column_double(statement, 2);
  6211. int erpStatus = sqlite3_column_double(statement, 9);
  6212. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  6213. if(sales_rep==nil)
  6214. sales_rep= "";
  6215. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  6216. char *create_by = (char*)sqlite3_column_text(statement, 4);
  6217. if(create_by==nil)
  6218. create_by= "";
  6219. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  6220. char *company_name = (char*)sqlite3_column_text(statement, 5);
  6221. if(company_name==nil)
  6222. company_name= "";
  6223. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  6224. char *create_time = (char*)sqlite3_column_text(statement, 6);
  6225. if(create_time==nil)
  6226. create_time= "";
  6227. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  6228. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  6229. // double total_price = sqlite3_column_double(statement, 7);
  6230. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] db:db close:NO];
  6231. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  6232. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  6233. int offline_edit = sqlite3_column_int(statement, 10);
  6234. // ": "JH",
  6235. // "": "$8307.00",
  6236. // "": "MOB1608050001",
  6237. // "": "ArpithaT",
  6238. // "": "1st Stage Property Transformations",
  6239. // "": "JANICE SUTTON",
  6240. // "": 2255,
  6241. // "": "08/02/2016 09:49:18",
  6242. // "": 1,
  6243. // "": "Saved Order"
  6244. // "": "1470384050483",
  6245. // "model_count": "6 / 28"
  6246. item[@"sales_rep"]= nssales_rep;
  6247. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  6248. item[@"so#"]= nssoid;
  6249. item[@"create_by"]= nscreate_by;
  6250. item[@"customer_name"]= nscompany_name;
  6251. item[@"customer_contact"] = customer_contact;
  6252. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  6253. item[@"purchase_time"]= nscreate_time;
  6254. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",status];
  6255. item[@"order_status"]= nsstatus;
  6256. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  6257. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  6258. // item[@"model_count"]
  6259. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  6260. count++;
  6261. }
  6262. ret[@"count"]= [NSNumber numberWithInt:count];
  6263. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  6264. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  6265. ret[@"result"]= [NSNumber numberWithInt:2];
  6266. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  6267. ret[@"time_zone"] = @"PST";
  6268. sqlite3_finalize(statement);
  6269. }
  6270. 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];
  6271. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  6272. [iSalesDB execSql:@"END TRANSACTION" db:db];
  6273. [iSalesDB close_db:db];
  6274. return [RAUtils dict2data:ret];
  6275. }
  6276. #pragma mark update gnotes
  6277. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  6278. {
  6279. DebugLog(@"params: %@",params);
  6280. // comments = Meyoyoyoyoyoyoy;
  6281. // orderCode = MOB1608110001;
  6282. // password = 123456;
  6283. // user = EvanK;
  6284. 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]];
  6285. int ret = [iSalesDB execSql:sql];
  6286. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6287. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6288. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6289. // [dic setValue:@"160409" forKey:@"min_ver"];
  6290. return [RAUtils dict2data:dic];
  6291. }
  6292. #pragma mark move to wishlist
  6293. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  6294. {
  6295. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  6296. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6297. _id = [NSString stringWithFormat:@"(%@)",_id];
  6298. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  6299. sqlite3 *db = [iSalesDB get_db];
  6300. __block NSString *product_id = @"";
  6301. __block NSString *item_count_str = @"";
  6302. // __block NSString *item_id = nil;
  6303. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6304. // product_id = [self textAtColumn:0 statement:stmt];
  6305. int item_count = sqlite3_column_int(stmt, 1);
  6306. // item_id = [self textAtColumn:2 statement:stmt];
  6307. NSString *p_id = [self textAtColumn:0 statement:stmt];
  6308. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  6309. if (p_id.length) {
  6310. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  6311. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  6312. }
  6313. }];
  6314. [iSalesDB close_db:db];
  6315. // 去除第一个,
  6316. product_id = [product_id substringFromIndex:1];
  6317. item_count_str = [item_count_str substringFromIndex:1];
  6318. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6319. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  6320. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  6321. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  6322. sqlite3 *db1 = [iSalesDB get_db];
  6323. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  6324. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6325. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  6326. // 删除
  6327. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  6328. int ret = [iSalesDB execSql:deleteSql db:db1];
  6329. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6330. [iSalesDB close_db:db1];
  6331. return [RAUtils dict2data:dic];
  6332. }
  6333. #pragma mark cart delete
  6334. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  6335. {
  6336. // cartItemId = 548;
  6337. // orderCode = MOB1608110001;
  6338. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  6339. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6340. _id = [NSString stringWithFormat:@"(%@)",_id];
  6341. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  6342. int ret = [iSalesDB execSql:sql];
  6343. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6344. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6345. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6346. // [dic setValue:@"160409" forKey:@"min_ver"];
  6347. return [RAUtils dict2data:dic];
  6348. }
  6349. #pragma mark set price
  6350. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  6351. {
  6352. DebugLog(@"cart set price params: %@",params);
  6353. // "cartitem_id" = 1;
  6354. // discount = "0.000000";
  6355. // "item_note" = "";
  6356. // price = "269.000000";
  6357. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6358. NSString *notes = [self valueInParams:params key:@"item_note"];
  6359. NSString *discount = [self valueInParams:params key:@"discount"];
  6360. NSString *price = [self valueInParams:params key:@"price"];
  6361. // bool badd_price_changed=false;
  6362. // sqlite3* db=[iSalesDB get_db];
  6363. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6364. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  6365. // NSRange range;
  6366. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  6367. //
  6368. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  6369. // badd_price_changed=true;
  6370. // [iSalesDB close_db:db];
  6371. //
  6372. // if(badd_price_changed)
  6373. // {
  6374. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  6375. // }
  6376. //
  6377. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  6378. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  6379. int ret = [iSalesDB execSql:sql];
  6380. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6381. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6382. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6383. // [dic setValue:@"160409" forKey:@"min_ver"];
  6384. return [RAUtils dict2data:dic];
  6385. }
  6386. #pragma mark set line notes
  6387. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  6388. {
  6389. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6390. NSString *notes = [self valueInParams:params key:@"notes"];
  6391. notes = [self translateSingleQuote:notes];
  6392. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  6393. int ret = [iSalesDB execSql:sql];
  6394. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6395. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6396. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6397. // [dic setValue:@"160409" forKey:@"min_ver"];
  6398. return [RAUtils dict2data:dic];
  6399. }
  6400. #pragma mark set qty
  6401. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  6402. {
  6403. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6404. int item_count = [params[@"inputInt"] integerValue];
  6405. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  6406. int ret = [iSalesDB execSql:sql];
  6407. __block int item_id = 0;
  6408. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6409. item_id = sqlite3_column_int(stmt, 0);
  6410. }];
  6411. sqlite3 *db = [iSalesDB get_db];
  6412. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  6413. [iSalesDB close_db:db];
  6414. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6415. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6416. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6417. // [dic setValue:@"160409" forKey:@"min_ver"];
  6418. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  6419. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  6420. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  6421. return [RAUtils dict2data:dic];
  6422. }
  6423. #pragma mark place order
  6424. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  6425. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6426. if (aname) {
  6427. [dic setValue:aname forKey:@"aname"];
  6428. }
  6429. if (control) {
  6430. [dic setValue:control forKey:@"control"];
  6431. }
  6432. if (keyboard) {
  6433. [dic setValue:keyboard forKey:@"keyboard"];
  6434. }
  6435. if (name) {
  6436. [dic setValue:name forKey:@"name"];
  6437. }
  6438. if (value) {
  6439. [dic setValue:value forKey:@"value"];
  6440. }
  6441. return dic;
  6442. }
  6443. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  6444. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6445. orderCode = [self translateSingleQuote:orderCode];
  6446. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  6447. 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];
  6448. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6449. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  6450. NSString *img0 = [self textAtColumn:1 statement:stmt];
  6451. NSString *img1 = [self textAtColumn:2 statement:stmt];
  6452. NSString *img2 = [self textAtColumn:3 statement:stmt];
  6453. NSString *company_name = [self textAtColumn:4 statement:stmt];
  6454. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  6455. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  6456. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  6457. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  6458. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  6459. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  6460. NSString *email = [self textAtColumn:11 statement:stmt];
  6461. NSString *phone = [self textAtColumn:12 statement:stmt];
  6462. NSString *fax = [self textAtColumn:13 statement:stmt];
  6463. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  6464. NSString *city = [self textAtColumn:15 statement:stmt];
  6465. NSString *state = [self textAtColumn:16 statement:stmt];
  6466. NSString *country = [self textAtColumn:17 statement:stmt];
  6467. NSString *name = [self textAtColumn:18 statement:stmt];
  6468. // contact id
  6469. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  6470. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  6471. [contact_id_dic setValue:@"text" forKey:@"control"];
  6472. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  6473. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  6474. [contact_id_dic setValue:@"true" forKey:@"required"];
  6475. [contact_id_dic setValue:contact_id forKey:@"value"];
  6476. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  6477. // business card
  6478. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  6479. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  6480. [business_card_dic setValue:@"img" forKey:@"control"];
  6481. [business_card_dic setValue:@"1" forKey:@"disable"];
  6482. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  6483. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  6484. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  6485. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  6486. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  6487. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  6488. [business_card_dic setValue:@"business_card" forKey:@"name"];
  6489. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  6490. // fax
  6491. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6492. [customer_dic setValue:fax_dic forKey:@"item_10"];
  6493. // zipcode
  6494. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  6495. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  6496. // city
  6497. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  6498. [customer_dic setValue:city_dic forKey:@"item_12"];
  6499. // state
  6500. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  6501. [customer_dic setValue:state_dic forKey:@"item_13"];
  6502. // country
  6503. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  6504. [customer_dic setValue:country_dic forKey:@"item_14"];
  6505. // company name
  6506. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  6507. [customer_dic setValue:company_dic forKey:@"item_2"];
  6508. // addr_1
  6509. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  6510. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  6511. // addr_2
  6512. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  6513. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  6514. // addr_3
  6515. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  6516. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  6517. // addr_4
  6518. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  6519. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  6520. // Contact
  6521. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  6522. [customer_dic setValue:contact_dic forKey:@"item_7"];
  6523. // email
  6524. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6525. [customer_dic setValue:email_dic forKey:@"item_8"];
  6526. // phone
  6527. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6528. [customer_dic setValue:phone_dic forKey:@"item_9"];
  6529. // title
  6530. [customer_dic setValue:@"Customer" forKey:@"title"];
  6531. // count
  6532. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  6533. }];
  6534. // setting
  6535. NSDictionary *setting = params[@"setting"];
  6536. NSNumber *hide = setting[@"CustomerHide"];
  6537. [customer_dic setValue:hide forKey:@"hide"];
  6538. return customer_dic;
  6539. }
  6540. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  6541. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6542. // setting
  6543. NSDictionary *setting = params[@"setting"];
  6544. NSNumber *hide = setting[@"ShipToHide"];
  6545. [dic setValue:hide forKey:@"hide"];
  6546. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6547. orderCode = [self translateSingleQuote:orderCode];
  6548. 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];
  6549. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6550. NSString *cid = [self textAtColumn:0 statement:stmt];
  6551. NSString *name = [self textAtColumn:1 statement:stmt];
  6552. NSString *ext = [self textAtColumn:2 statement:stmt];
  6553. NSString *contact = [self textAtColumn:3 statement:stmt];
  6554. NSString *email = [self textAtColumn:4 statement:stmt];
  6555. NSString *fax = [self textAtColumn:5 statement:stmt];
  6556. NSString *phone = [self textAtColumn:6 statement:stmt];
  6557. // count
  6558. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6559. // title
  6560. [dic setValue:@"Ship To" forKey:@"title"];
  6561. // choose
  6562. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6563. [choose_dic setValue:@"choose" forKey:@"aname"];
  6564. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6565. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  6566. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  6567. @"key_map" : @{@"receive_cid" : @"customer_cid",
  6568. @"receive_contact" : @"customer_contact",
  6569. @"receive_email" : @"customer_email",
  6570. @"receive_ext" : @"customer_contact_ext",
  6571. @"receive_fax" : @"customer_fax",
  6572. @"receive_name" : @"customer_name",
  6573. @"receive_phone" : @"customer_phone"},
  6574. @"refresh" : [NSNumber numberWithInteger:1],
  6575. @"type" : @"pull"};
  6576. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  6577. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  6578. @"name" : @"Add new address",
  6579. @"refresh" : [NSNumber numberWithInteger:1],
  6580. @"value" : @"new_addr"
  6581. };
  6582. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  6583. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  6584. @"key_map" : @{@"receive_cid" : @"customer_cid",
  6585. @"receive_contact" : @"customer_contact",
  6586. @"receive_email" : @"customer_email",
  6587. @"receive_ext" : @"customer_contact_ext",
  6588. @"receive_fax" : @"customer_fax",
  6589. @"receive_name" : @"customer_name",
  6590. @"receive_phone" : @"customer_phone"},
  6591. @"name" : @"select_ship_to",
  6592. @"refresh" : [NSNumber numberWithInteger:1],
  6593. @"value" : @"Sales_Order_Ship_To"};
  6594. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  6595. [dic setValue:choose_dic forKey:@"item_0"];
  6596. // contact id
  6597. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6598. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6599. [contact_id_dic setValue:@"true" forKey:@"required"];
  6600. [dic setValue:contact_id_dic forKey:@"item_1"];
  6601. // company name
  6602. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6603. [dic setValue:company_name_dic forKey:@"item_2"];
  6604. // address
  6605. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  6606. NSNumber *required = setting[@"ShippingToAddressRequire"];
  6607. if ([required integerValue]) {
  6608. [ext_dic setValue:@"true" forKey:@"required"];
  6609. }
  6610. [dic setValue:ext_dic forKey:@"item_3"];
  6611. // contact
  6612. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6613. [dic setValue:contact_dic forKey:@"item_4"];
  6614. // phone
  6615. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6616. [dic setValue:phone_dic forKey:@"item_5"];
  6617. // fax
  6618. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6619. [dic setValue:fax_dic forKey:@"item_6"];
  6620. // email
  6621. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6622. [dic setValue:email_dic forKey:@"item_7"];
  6623. }];
  6624. return dic;
  6625. }
  6626. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  6627. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6628. // setting
  6629. NSDictionary *setting = params[@"setting"];
  6630. NSNumber *hide = setting[@"ShipFromHide"];
  6631. [dic setValue:hide forKey:@"hide"];
  6632. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6633. orderCode = [self translateSingleQuote:orderCode];
  6634. 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];
  6635. 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';";
  6636. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6637. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  6638. __block NSString *name = [self textAtColumn:1 statement:stmt];
  6639. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  6640. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  6641. __block NSString *email = [self textAtColumn:4 statement:stmt];
  6642. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  6643. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  6644. if (!cid.length) {
  6645. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  6646. cid = [self textAtColumn:0 statement:statment];
  6647. name = [self textAtColumn:1 statement:statment];
  6648. ext = [self textAtColumn:2 statement:statment];
  6649. contact = [self textAtColumn:3 statement:statment];
  6650. email = [self textAtColumn:4 statement:statment];
  6651. fax = [self textAtColumn:5 statement:statment];
  6652. phone = [self textAtColumn:6 statement:statment];
  6653. }];
  6654. }
  6655. // count
  6656. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6657. // title
  6658. [dic setValue:@"Ship From" forKey:@"title"];
  6659. // hide
  6660. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6661. // choose
  6662. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6663. [choose_dic setValue:@"choose" forKey:@"aname"];
  6664. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6665. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6666. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  6667. @"key_map" : @{@"sender_cid" : @"customer_cid",
  6668. @"sender_contact" : @"customer_contact",
  6669. @"sender_email" : @"customer_email",
  6670. @"sender_ext" : @"customer_contact_ext",
  6671. @"sender_fax" : @"customer_fax",
  6672. @"sender_name" : @"customer_name",
  6673. @"sender_phone" : @"customer_phone"},
  6674. @"name" : @"select_cid",
  6675. @"refresh" : [NSNumber numberWithInteger:0],
  6676. @"value" : @"Sales_Order_Ship_From"};
  6677. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  6678. [dic setValue:choose_dic forKey:@"item_0"];
  6679. // contact id
  6680. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6681. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6682. [contact_id_dic setValue:@"true" forKey:@"required"];
  6683. [dic setValue:contact_id_dic forKey:@"item_1"];
  6684. // company name
  6685. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6686. [dic setValue:company_name_dic forKey:@"item_2"];
  6687. // address
  6688. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  6689. [dic setValue:ext_dic forKey:@"item_3"];
  6690. // contact
  6691. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6692. [dic setValue:contact_dic forKey:@"item_4"];
  6693. // phone
  6694. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6695. [dic setValue:phone_dic forKey:@"item_5"];
  6696. // fax
  6697. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6698. [dic setValue:fax_dic forKey:@"item_6"];
  6699. // email
  6700. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6701. [dic setValue:email_dic forKey:@"item_7"];
  6702. }];
  6703. return dic;
  6704. }
  6705. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  6706. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6707. // setting
  6708. NSDictionary *setting = params[@"setting"];
  6709. NSNumber *hide = setting[@"FreightBillToHide"];
  6710. [dic setValue:hide forKey:@"hide"];
  6711. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6712. orderCode = [self translateSingleQuote:orderCode];
  6713. 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];
  6714. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6715. NSString *cid = [self textAtColumn:0 statement:stmt];
  6716. NSString *name = [self textAtColumn:1 statement:stmt];
  6717. NSString *ext = [self textAtColumn:2 statement:stmt];
  6718. NSString *contact = [self textAtColumn:3 statement:stmt];
  6719. NSString *email = [self textAtColumn:4 statement:stmt];
  6720. NSString *fax = [self textAtColumn:5 statement:stmt];
  6721. NSString *phone = [self textAtColumn:6 statement:stmt];
  6722. // count
  6723. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6724. // title
  6725. [dic setValue:@"Freight Bill To" forKey:@"title"];
  6726. // hide
  6727. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6728. // choose
  6729. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6730. [choose_dic setValue:@"choose" forKey:@"aname"];
  6731. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6732. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  6733. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  6734. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  6735. @"shipping_billto_contact" : @"receive_contact",
  6736. @"shipping_billto_email" : @"receive_email",
  6737. @"shipping_billto_ext" : @"receive_ext",
  6738. @"shipping_billto_fax" : @"receive_fax",
  6739. @"shipping_billto_name" : @"receive_name",
  6740. @"shipping_billto_phone" : @"receive_phone"},
  6741. @"type" : @"pull"};
  6742. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  6743. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  6744. @"type" : @"pull",
  6745. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  6746. @"shipping_billto_contact" : @"customer_contact",
  6747. @"shipping_billto_email" : @"customer_email",
  6748. @"shipping_billto_ext" : @"customer_contact_ext",
  6749. @"shipping_billto_fax" : @"customer_fax",
  6750. @"shipping_billto_name" : @"customer_name",
  6751. @"shipping_billto_phone" : @"customer_phone"}
  6752. };
  6753. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  6754. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  6755. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  6756. @"shipping_billto_contact" : @"sender_contact",
  6757. @"shipping_billto_email" : @"sender_email",
  6758. @"shipping_billto_ext" : @"sender_ext",
  6759. @"shipping_billto_fax" : @"sender_fax",
  6760. @"shipping_billto_name" : @"sender_name",
  6761. @"shipping_billto_phone" : @"sender_phone"},
  6762. @"type" : @"pull"
  6763. };
  6764. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  6765. NSDictionary *select_freight_bill_to_dic = @{
  6766. @"aname" : @"Select freight bill to",
  6767. @"name" : @"select_cid",
  6768. @"refresh" : [NSNumber numberWithInteger:0],
  6769. @"value" : @"Sales_Order_Freight_Bill_To",
  6770. @"key_map" : @{
  6771. @"shipping_billto_cid" : @"customer_cid",
  6772. @"shipping_billto_contact" : @"customer_contact",
  6773. @"shipping_billto_email" : @"customer_email",
  6774. @"shipping_billto_ext" : @"customer_contact_ext",
  6775. @"shipping_billto_fax" : @"customer_fax",
  6776. @"shipping_billto_name" : @"customer_name",
  6777. @"shipping_billto_phone" : @"customer_phone"
  6778. }
  6779. };
  6780. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  6781. [dic setValue:choose_dic forKey:@"item_0"];
  6782. // contact id
  6783. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6784. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6785. [contact_id_dic setValue:@"true" forKey:@"required"];
  6786. [dic setValue:contact_id_dic forKey:@"item_1"];
  6787. // company name
  6788. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6789. [dic setValue:company_name_dic forKey:@"item_2"];
  6790. // address
  6791. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  6792. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  6793. if ([ext_required integerValue]) {
  6794. [ext_dic setValue:@"true" forKey:@"required"];
  6795. }
  6796. [dic setValue:ext_dic forKey:@"item_3"];
  6797. // contact
  6798. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6799. [dic setValue:contact_dic forKey:@"item_4"];
  6800. // phone
  6801. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6802. [dic setValue:phone_dic forKey:@"item_5"];
  6803. // fax
  6804. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6805. [dic setValue:fax_dic forKey:@"item_6"];
  6806. // email
  6807. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6808. [dic setValue:email_dic forKey:@"item_7"];
  6809. }];
  6810. return dic;
  6811. }
  6812. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  6813. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6814. // setting
  6815. NSDictionary *setting = params[@"setting"];
  6816. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  6817. [dic setValue:hide forKey:@"hide"];
  6818. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6819. orderCode = [self translateSingleQuote:orderCode];
  6820. 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];
  6821. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6822. NSString *cid = [self textAtColumn:0 statement:stmt];
  6823. NSString *name = [self textAtColumn:1 statement:stmt];
  6824. NSString *ext = [self textAtColumn:2 statement:stmt];
  6825. NSString *contact = [self textAtColumn:3 statement:stmt];
  6826. NSString *email = [self textAtColumn:4 statement:stmt];
  6827. NSString *fax = [self textAtColumn:5 statement:stmt];
  6828. NSString *phone = [self textAtColumn:6 statement:stmt];
  6829. // count
  6830. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6831. // title
  6832. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  6833. // hide
  6834. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6835. // choose
  6836. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6837. [choose_dic setValue:@"choose" forKey:@"aname"];
  6838. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6839. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  6840. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  6841. @"key_map" : @{@"billing_cid" : @"receive_cid",
  6842. @"billing_contact" : @"receive_contact",
  6843. @"billing_email" : @"receive_email",
  6844. @"billing_ext" : @"receive_ext",
  6845. @"billing_fax" : @"receive_fax",
  6846. @"billing_name" : @"receive_name",
  6847. @"billing_phone" : @"receive_phone"},
  6848. @"type" : @"pull"};
  6849. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  6850. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  6851. @"type" : @"pull",
  6852. @"key_map" : @{@"billing_cid" : @"customer_cid",
  6853. @"billing_contact" : @"customer_contact",
  6854. @"billing_email" : @"customer_email",
  6855. @"billing_ext" : @"customer_contact_ext",
  6856. @"billing_fax" : @"customer_fax",
  6857. @"billing_name" : @"customer_name",
  6858. @"billing_phone" : @"customer_phone"}
  6859. };
  6860. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  6861. NSDictionary *select_bill_to_dic = @{
  6862. @"aname" : @"Select bill to",
  6863. @"name" : @"select_cid",
  6864. @"refresh" : [NSNumber numberWithInteger:0],
  6865. @"value" : @"Sales_Order_Merchandise_Bill_To",
  6866. @"key_map" : @{
  6867. @"billing_cid" : @"customer_cid",
  6868. @"billing_contact" : @"customer_contact",
  6869. @"billing_email" : @"customer_email",
  6870. @"billing_ext" : @"customer_contact_ext",
  6871. @"billing_fax" : @"customer_fax",
  6872. @"billing_name" : @"customer_name",
  6873. @"billing_phone" : @"customer_phone"
  6874. }
  6875. };
  6876. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  6877. [dic setValue:choose_dic forKey:@"item_0"];
  6878. // contact id
  6879. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6880. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6881. [contact_id_dic setValue:@"true" forKey:@"required"];
  6882. [dic setValue:contact_id_dic forKey:@"item_1"];
  6883. // company name
  6884. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6885. [dic setValue:company_name_dic forKey:@"item_2"];
  6886. // address
  6887. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  6888. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  6889. if ([ext_required integerValue]) {
  6890. [ext_dic setValue:@"true" forKey:@"required"];
  6891. }
  6892. [dic setValue:ext_dic forKey:@"item_3"];
  6893. // contact
  6894. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6895. [dic setValue:contact_dic forKey:@"item_4"];
  6896. // phone
  6897. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6898. [dic setValue:phone_dic forKey:@"item_5"];
  6899. // fax
  6900. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6901. [dic setValue:fax_dic forKey:@"item_6"];
  6902. // email
  6903. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6904. [dic setValue:email_dic forKey:@"item_7"];
  6905. }];
  6906. return dic;
  6907. }
  6908. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  6909. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6910. // setting
  6911. NSDictionary *setting = params[@"setting"];
  6912. NSNumber *hide = setting[@"ReturnToHide"];
  6913. [dic setValue:hide forKey:@"hide"];
  6914. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6915. orderCode = [self translateSingleQuote:orderCode];
  6916. 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];
  6917. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6918. NSString *cid = [self textAtColumn:0 statement:stmt];
  6919. NSString *name = [self textAtColumn:1 statement:stmt];
  6920. NSString *ext = [self textAtColumn:2 statement:stmt];
  6921. NSString *contact = [self textAtColumn:3 statement:stmt];
  6922. NSString *email = [self textAtColumn:4 statement:stmt];
  6923. NSString *fax = [self textAtColumn:5 statement:stmt];
  6924. NSString *phone = [self textAtColumn:6 statement:stmt];
  6925. // count
  6926. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6927. // title
  6928. [dic setValue:@"Return To" forKey:@"title"];
  6929. // hide
  6930. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6931. // choose
  6932. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6933. [choose_dic setValue:@"choose" forKey:@"aname"];
  6934. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6935. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  6936. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  6937. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  6938. @"returnto_contact" : @"sender_contact",
  6939. @"returnto_email" : @"sender_email",
  6940. @"returnto_ext" : @"sender_ext",
  6941. @"returnto_fax" : @"sender_fax",
  6942. @"returnto_name" : @"sender_name",
  6943. @"returnto_phone" : @"sender_phone"},
  6944. @"type" : @"pull"};
  6945. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  6946. NSDictionary *select_return_to_dic = @{
  6947. @"aname" : @"Select return to",
  6948. @"name" : @"select_cid",
  6949. @"refresh" : [NSNumber numberWithInteger:0],
  6950. @"value" : @"Contact_Return_To",
  6951. @"key_map" : @{
  6952. @"returnto_cid" : @"customer_cid",
  6953. @"returnto_contact" : @"customer_contact",
  6954. @"returnto_email" : @"customer_email",
  6955. @"returnto_ext" : @"customer_contact_ext",
  6956. @"returnto_fax" : @"customer_fax",
  6957. @"returnto_name" : @"customer_name",
  6958. @"returnto_phone" : @"customer_phone"
  6959. }
  6960. };
  6961. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  6962. [dic setValue:choose_dic forKey:@"item_0"];
  6963. // contact id
  6964. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6965. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6966. [contact_id_dic setValue:@"true" forKey:@"required"];
  6967. [dic setValue:contact_id_dic forKey:@"item_1"];
  6968. // company name
  6969. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6970. [dic setValue:company_name_dic forKey:@"item_2"];
  6971. // address
  6972. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  6973. [dic setValue:ext_dic forKey:@"item_3"];
  6974. // contact
  6975. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6976. [dic setValue:contact_dic forKey:@"item_4"];
  6977. // phone
  6978. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6979. [dic setValue:phone_dic forKey:@"item_5"];
  6980. // fax
  6981. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6982. [dic setValue:fax_dic forKey:@"item_6"];
  6983. // email
  6984. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6985. [dic setValue:email_dic forKey:@"item_7"];
  6986. }];
  6987. return dic;
  6988. }
  6989. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  6990. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6991. orderCode = [self translateSingleQuote:orderCode];
  6992. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6993. __block double TotalCuft = 0;
  6994. __block double TotalWeight = 0;
  6995. __block int TotalCarton = 0;
  6996. __block double payments = 0;
  6997. // setting
  6998. NSDictionary *setting = params[@"setting"];
  6999. NSNumber *hide = setting[@"ModelInformationHide"];
  7000. [dic setValue:hide forKey:@"hide"];
  7001. 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];
  7002. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7003. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7004. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  7005. // item id
  7006. int item_id = sqlite3_column_int(stmt, 0);
  7007. // count
  7008. int item_count = sqlite3_column_int(stmt, 1);
  7009. // stockUom
  7010. int stockUom = sqlite3_column_int(stmt, 2);
  7011. // unit price
  7012. NSString *str_price = [self textAtColumn:3 statement:stmt];
  7013. NSString* Price=nil;
  7014. if([str_price isEqualToString:@""])
  7015. {
  7016. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  7017. if(price==nil)
  7018. Price=@"No Price.";
  7019. else
  7020. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7021. }
  7022. else
  7023. {
  7024. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  7025. }
  7026. // discount
  7027. double discount = sqlite3_column_double(stmt, 4);
  7028. // name
  7029. NSString *name = [self textAtColumn:5 statement:stmt];
  7030. // description
  7031. NSString *description = [self textAtColumn:6 statement:stmt];
  7032. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  7033. // line note
  7034. NSString *line_note = [self textAtColumn:7 statement:stmt];
  7035. int avaulability = sqlite3_column_int(stmt, 8);
  7036. // img
  7037. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  7038. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  7039. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7040. if(combine != nil)
  7041. {
  7042. // int citem=0;
  7043. int bcount=[[combine valueForKey:@"count"] intValue];
  7044. for(int bc=0;bc<bcount;bc++)
  7045. {
  7046. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7047. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7048. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7049. subTotal += uprice * modulus * item_count;
  7050. }
  7051. }
  7052. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  7053. [model_dic setValue:@"model" forKey:@"control"];
  7054. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  7055. [model_dic setValue:description forKey:@"description"];
  7056. [model_dic setValue:line_note forKey:@"note"];
  7057. [model_dic setValue:img forKey:@"img_url"];
  7058. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  7059. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  7060. [model_dic setValue:Price forKey:@"unit_price"];
  7061. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  7062. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  7063. if (combine) {
  7064. [model_dic setValue:combine forKey:@"combine"];
  7065. }
  7066. // well,what under the row is the info for total
  7067. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  7068. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  7069. double weight=[bsubtotaljson[@"weight"] doubleValue];
  7070. int carton=[bsubtotaljson[@"carton"] intValue];
  7071. TotalCuft += cuft;
  7072. TotalWeight += weight;
  7073. TotalCarton += carton;
  7074. payments += subTotal;
  7075. //---------------------------
  7076. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  7077. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  7078. }];
  7079. [dic setValue:@"Model Information" forKey:@"title"];
  7080. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  7081. return dic;
  7082. }
  7083. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  7084. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7085. [dic setValue:@"Remarks Content" forKey:@"title"];
  7086. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  7087. // setting
  7088. NSDictionary *setting = params[@"setting"];
  7089. NSNumber *hide = setting[@"RemarksContentHide"];
  7090. [dic setValue:hide forKey:@"hide"];
  7091. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7092. orderCode = [self translateSingleQuote:orderCode];
  7093. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes from offline_order where so_id = '%@';",orderCode];
  7094. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7095. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  7096. int mustCall = sqlite3_column_int(stmt, 1);
  7097. NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  7098. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  7099. NSDictionary *po_dic = @{
  7100. @"aname" : @"PO#",
  7101. @"control" : @"edit",
  7102. @"keyboard" : @"text",
  7103. @"name" : @"poNumber",
  7104. @"value" : poNumber
  7105. };
  7106. NSDictionary *must_call_dic = @{
  7107. @"aname" : @"MUST CALL BEFORE DELIVERY",
  7108. @"control" : @"switch",
  7109. @"name" : @"must_call",
  7110. @"value" : mustCall ? @"true" : @"false"
  7111. };
  7112. NSDictionary *general_notes_dic = @{
  7113. @"aname" : @"General notes",
  7114. @"control" : @"text_view",
  7115. @"keyboard" : @"text",
  7116. @"name" : @"comments",
  7117. @"value" : generalNotes
  7118. };
  7119. NSDictionary *internal_notes_dic = @{
  7120. @"aname" : @"Internal notes",
  7121. @"control" : @"text_view",
  7122. @"keyboard" : @"text",
  7123. @"name" : @"internal_notes",
  7124. @"value" : internalNotes
  7125. };
  7126. [dic setValue:po_dic forKey:@"item_0"];
  7127. [dic setValue:must_call_dic forKey:@"item_1"];
  7128. [dic setValue:general_notes_dic forKey:@"item_2"];
  7129. [dic setValue:internal_notes_dic forKey:@"item_3"];
  7130. }];
  7131. return dic;
  7132. }
  7133. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  7134. // params
  7135. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7136. orderCode = [self translateSingleQuote:orderCode];
  7137. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7138. // setting
  7139. NSDictionary *setting = params[@"setting"];
  7140. NSNumber *hide = setting[@"OrderTotalHide"];
  7141. [dic setValue:hide forKey:@"hide"];
  7142. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  7143. __block double lift_gate_value = 0;
  7144. __block double handling_fee = 0;
  7145. __block double shipping = 0;
  7146. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7147. int lift_gate = sqlite3_column_int(stmt, 0);
  7148. lift_gate_value = sqlite3_column_double(stmt, 1);
  7149. shipping = sqlite3_column_double(stmt, 2);
  7150. handling_fee = sqlite3_column_double(stmt, 3);
  7151. if (!lift_gate) {
  7152. lift_gate_value = 0;
  7153. }
  7154. }];
  7155. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  7156. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  7157. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  7158. double payments = [[total valueForKey:@"payments"] doubleValue];
  7159. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  7160. double totalPrice = payments;
  7161. [dic setValue:@"Order Total" forKey:@"title"];
  7162. NSDictionary *payments_dic = @{
  7163. @"align" : @"right",
  7164. @"aname" : @"Payments/Credits",
  7165. @"control" : @"text",
  7166. @"name" : @"paymentsAndCredits",
  7167. @"type" : @"price",
  7168. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  7169. };
  7170. [dic setValue:payments_dic forKey:@"item_0"];
  7171. NSDictionary *handling_fee_dic = @{
  7172. @"align" : @"right",
  7173. @"aname" : @"Handling Fee",
  7174. @"control" : @"text",
  7175. @"name" : @"handling_fee_value",
  7176. @"required" : @"true",
  7177. @"type" : @"price",
  7178. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  7179. };
  7180. NSDictionary *shipping_dic = @{
  7181. @"align" : @"right",
  7182. @"aname" : @"Shipping*",
  7183. @"control" : @"text",
  7184. @"name" : @"shipping",
  7185. @"required" : @"true",
  7186. @"type" : @"price",
  7187. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  7188. };
  7189. NSDictionary *lift_gate_dic = @{
  7190. @"align" : @"right",
  7191. @"aname" : @"Liftgate Fee(No Loading Dock)",
  7192. @"control" : @"text",
  7193. @"name" : @"lift_gate_value",
  7194. @"required" : @"true",
  7195. @"type" : @"price",
  7196. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  7197. };
  7198. int item_count = 1;
  7199. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  7200. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  7201. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7202. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7203. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  7204. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7205. } else {
  7206. }
  7207. [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7208. NSDictionary *total_price_dic = @{
  7209. @"align" : @"right",
  7210. @"aname" : @"Total",
  7211. @"control" : @"text",
  7212. @"name" : @"totalPrice",
  7213. @"type" : @"price",
  7214. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  7215. };
  7216. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7217. NSDictionary *total_cuft_dic = @{
  7218. @"align" : @"right",
  7219. @"aname" : @"Total Cuft",
  7220. @"control" : @"text",
  7221. @"name" : @"",
  7222. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  7223. };
  7224. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7225. NSDictionary *total_weight_dic = @{
  7226. @"align" : @"right",
  7227. @"aname" : @"Total Weight",
  7228. @"control" : @"text",
  7229. @"name" : @"",
  7230. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  7231. };
  7232. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7233. NSDictionary *total_carton_dic = @{
  7234. @"align" : @"right",
  7235. @"aname" : @"Total Carton",
  7236. @"control" : @"text",
  7237. @"name" : @"",
  7238. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  7239. };
  7240. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7241. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  7242. return dic;
  7243. }
  7244. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  7245. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7246. orderCode = [self translateSingleQuote:orderCode];
  7247. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7248. // setting
  7249. NSDictionary *setting = params[@"setting"];
  7250. NSNumber *hide = setting[@"SignatureHide"];
  7251. [dic setValue:hide forKey:@"hide"];
  7252. [dic setValue:@"Signature" forKey:@"title"];
  7253. __block NSString *pic_path = @"";
  7254. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  7255. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7256. pic_path = [self textAtColumn:0 statement:stmt];
  7257. }];
  7258. NSDictionary *pic_dic = @{
  7259. @"aname" : @"Signature",
  7260. @"avalue" :pic_path,
  7261. @"control" : @"signature",
  7262. @"name" : @"sign_picpath",
  7263. @"value" : pic_path
  7264. };
  7265. [dic setValue:pic_dic forKey:@"item_0"];
  7266. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7267. return dic;
  7268. }
  7269. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  7270. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7271. // setting
  7272. NSDictionary *setting = params[@"setting"];
  7273. NSNumber *hide = setting[@"ShippingMethodHide"];
  7274. [dic setValue:hide forKey:@"hide"];
  7275. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7276. orderCode = [self translateSingleQuote:orderCode];
  7277. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  7278. __block NSString *logist = @"";
  7279. __block NSString *lift_gate = @"";
  7280. __block int lift_gate_integer = 0;
  7281. __block NSString *logistic_note = @"";
  7282. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7283. logist = [self textAtColumn:0 statement:stmt];
  7284. lift_gate_integer = sqlite3_column_int(stmt, 1);
  7285. logistic_note = [self textAtColumn:2 statement:stmt];
  7286. }];
  7287. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7288. [dic setValue:@"Shipping Method" forKey:@"title"];
  7289. // val_0
  7290. int PERSONAL_PICK_UP_check = 0;
  7291. int USE_MY_CARRIER_check = 0;
  7292. NSString *logistic_note_text = @"";
  7293. int will_call_check = 0;
  7294. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  7295. will_call_check = 1;
  7296. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  7297. PERSONAL_PICK_UP_check = 1;
  7298. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  7299. USE_MY_CARRIER_check = 1;
  7300. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  7301. logistic_note = [logistic_note_array firstObject];
  7302. if (logistic_note_array.count > 1) {
  7303. logistic_note_text = [logistic_note_array lastObject];
  7304. }
  7305. }
  7306. }
  7307. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  7308. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  7309. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  7310. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  7311. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  7312. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7313. NSDictionary *val0_subItem_item0 = @{
  7314. @"aname" : @"Option",
  7315. @"cadedate" : @{
  7316. @"count" : [NSNumber numberWithInteger:2],
  7317. @"val_0" : @{
  7318. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  7319. @"refresh" : [NSNumber numberWithInteger:0],
  7320. @"value" : @"PERSONAL PICK UP",
  7321. @"value_id" : @"PERSONAL PICK UP"
  7322. },
  7323. @"val_1" : @{
  7324. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  7325. @"refresh" : [NSNumber numberWithInteger:0],
  7326. @"sub_item" : @{
  7327. @"count" : [NSNumber numberWithInteger:1],
  7328. @"item_0" : @{
  7329. @"aname" : @"BOL",
  7330. @"control" : @"edit",
  7331. @"keyboard" : @"text",
  7332. @"name" : @"logist_note_text",
  7333. @"refresh" : [NSNumber numberWithInteger:0],
  7334. @"required" : @"false",
  7335. @"value" : logistic_note_text
  7336. }
  7337. },
  7338. @"value" : @"USE MY CARRIER",
  7339. @"value_id" : @"USE MY CARRIER"
  7340. }
  7341. },
  7342. @"control" : @"enum",
  7343. @"name" : @"logistic_note",
  7344. @"required" : @"true",
  7345. @"single_select" : @"true"
  7346. };
  7347. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  7348. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  7349. // val_1
  7350. lift_gate = [self valueInParams:params key:@"lift_gate"];
  7351. if([lift_gate isEqualToString:@""]) {
  7352. if (lift_gate_integer == 1) {
  7353. lift_gate = @"true";
  7354. } else {
  7355. lift_gate = @"false";
  7356. }
  7357. }
  7358. int common_carrier_check = 0;
  7359. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  7360. common_carrier_check = 1;
  7361. [params setValue:lift_gate forKey:@"lift_gate"];
  7362. }
  7363. NSDictionary *val_1 = @{
  7364. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  7365. @"sub_item" : @{
  7366. @"count" : [NSNumber numberWithInteger:1],
  7367. @"item_0" : @{
  7368. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  7369. @"control" : @"switch",
  7370. @"name" : @"lift_gate",
  7371. @"refresh" : [NSNumber numberWithInteger:1],
  7372. @"required" : @"true",
  7373. @"value" : lift_gate
  7374. }
  7375. },
  7376. @"value" : @"COMMON CARRIER",
  7377. @"value_id" : @"COMMON CARRIER"
  7378. };
  7379. // cadedate
  7380. NSDictionary *cadedate = @{
  7381. @"count" : [NSNumber numberWithInteger:2],
  7382. @"val_0" : val_0,
  7383. @"val_1" : val_1
  7384. };
  7385. // item_0
  7386. NSMutableDictionary *item_0 = @{
  7387. @"aname" : @"Shipping",
  7388. @"cadedate" : cadedate,
  7389. @"control" : @"enum",
  7390. @"name" : @"logist",
  7391. @"refresh" : [NSNumber numberWithInteger:1],
  7392. @"single_select" : @"true",
  7393. }.mutableCopy;
  7394. NSNumber *required = setting[@"ShippingMethodRequire"];
  7395. if ([required integerValue]) {
  7396. [item_0 setValue:@"true" forKey:@"required"];
  7397. }
  7398. [dic setValue:item_0 forKey:@"item_0"];
  7399. return dic;
  7400. }
  7401. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  7402. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7403. orderCode = [self translateSingleQuote:orderCode];
  7404. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  7405. __block int submit_as_integer = 0;
  7406. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7407. submit_as_integer = sqlite3_column_int(stmt, 0);
  7408. }];
  7409. int check11 = 0;
  7410. int check10 = 0;
  7411. if (submit_as_integer == 11) {
  7412. check11 = 1;
  7413. }
  7414. if (submit_as_integer == 10) {
  7415. check10 = 1;
  7416. }
  7417. // section_0
  7418. NSMutableDictionary *dic = @{
  7419. @"count" : @(1),
  7420. @"item_0" : @{
  7421. @"aname" : @"Submit Order As",
  7422. @"cadedate" : @{
  7423. @"count" : @(2),
  7424. @"val_0" : @{
  7425. @"check" : [NSNumber numberWithInteger:check11],
  7426. @"value" : @"Sales Order",
  7427. @"value_id" : @(11)
  7428. },
  7429. @"val_1" : @{
  7430. @"check" : [NSNumber numberWithInteger:check10],
  7431. @"value" : @"Quote",
  7432. @"value_id" : @(10)
  7433. }
  7434. },
  7435. @"control" : @"enum",
  7436. @"name" : @"erpOrderStatus",
  7437. @"required" : @"true",
  7438. @"single_select" : @"true"
  7439. },
  7440. @"title" : @"Order Type"
  7441. }.mutableCopy;
  7442. // setting
  7443. NSDictionary *setting = params[@"setting"];
  7444. NSNumber *hide = setting[@"OrderTypeHide"];
  7445. [dic setValue:hide forKey:@"hide"];
  7446. return dic;
  7447. }
  7448. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  7449. countryCode = [self translateSingleQuote:countryCode];
  7450. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  7451. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7452. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  7453. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  7454. if (name == NULL) {
  7455. name = "";
  7456. }
  7457. if (code == NULL) {
  7458. code = "";
  7459. }
  7460. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  7461. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  7462. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  7463. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  7464. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  7465. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  7466. }
  7467. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  7468. [container setValue:stateDic forKey:key];
  7469. }] mutableCopy];
  7470. [ret removeObjectForKey:@"result"];
  7471. // failure 可以不用了,一样的
  7472. if (ret.allKeys.count == 0) {
  7473. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  7474. [stateDic setValue:@"Other" forKey:@"value"];
  7475. [stateDic setValue:@"" forKey:@"value_id"];
  7476. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  7477. if (state_code && [@"" isEqualToString:state_code]) {
  7478. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  7479. }
  7480. NSString *key = [NSString stringWithFormat:@"val_0"];
  7481. [ret setValue:stateDic forKey:key];
  7482. }
  7483. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  7484. return ret;
  7485. }
  7486. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  7487. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7488. orderCode = [self translateSingleQuote:orderCode];
  7489. 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];
  7490. __block NSString *payType = @"";
  7491. __block NSString *firstName = @"";
  7492. __block NSString *lastName = @"";
  7493. __block NSString *addr1 = @"";
  7494. __block NSString *addr2 = @"";
  7495. __block NSString *zipcode = @"";
  7496. __block NSString *cardType = @"";
  7497. __block NSString *cardNumber = @"";
  7498. __block NSString *securityCode = @"";
  7499. __block NSString *month = @"";
  7500. __block NSString *year = @"";
  7501. __block NSString *city = @"";
  7502. __block NSString *state = @"";
  7503. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7504. payType = [self textAtColumn:0 statement:stmt];
  7505. firstName = [self textAtColumn:1 statement:stmt];
  7506. lastName = [self textAtColumn:2 statement:stmt];
  7507. addr1 = [self textAtColumn:3 statement:stmt];
  7508. addr2 = [self textAtColumn:4 statement:stmt];
  7509. zipcode = [self textAtColumn:5 statement:stmt];
  7510. cardType = [self textAtColumn:6 statement:stmt];
  7511. cardNumber = [self textAtColumn:7 statement:stmt];
  7512. securityCode = [self textAtColumn:8 statement:stmt];
  7513. month = [self textAtColumn:9 statement:stmt];
  7514. year = [self textAtColumn:10 statement:stmt];
  7515. city = [self textAtColumn:11 statement:stmt];
  7516. state = [self textAtColumn:12 statement:stmt];
  7517. }];
  7518. NSString *required = @"true";
  7519. // setting
  7520. NSDictionary *setting = params[@"setting"];
  7521. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  7522. if ([requiredNumber integerValue]) {
  7523. required = @"true";
  7524. } else {
  7525. required = @"false";
  7526. }
  7527. // "section_2"
  7528. NSMutableDictionary *dic = @{
  7529. @"count" : @(1),
  7530. @"item_0" : @{
  7531. @"aname" : @"Payment",
  7532. @"required" : required,
  7533. @"cadedate" : @{
  7534. @"count" : @(6),
  7535. @"val_0" : @{
  7536. @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  7537. @"value" : @"Check",
  7538. @"value_id" : @"Check"
  7539. },
  7540. @"val_1" : @{
  7541. @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  7542. @"value" : @"Cash",
  7543. @"value_id" : @"Cash"
  7544. },
  7545. @"val_2" : @{
  7546. @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  7547. @"value" : @"NET 30",
  7548. @"value_id" : @"NET 30"
  7549. },
  7550. @"val_3" : @{
  7551. @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  7552. @"value" : @"Wire Transfer",
  7553. @"value_id" : @"Wire Transfer"
  7554. },
  7555. @"val_4" : @{
  7556. @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  7557. @"sub_item" : @{
  7558. @"count" : @(3),
  7559. @"item_0" : @{
  7560. @"aname" : @"choose",
  7561. @"control" : @"multi_action",
  7562. @"count" : @(1),
  7563. @"item_0" : @{
  7564. @"aname" : @"Same as customer",
  7565. @"key_map" : @{
  7566. @"credit_card_address1" : @"customer_address1",
  7567. @"credit_card_address2" : @"customer_address2",
  7568. @"credit_card_city" : @"customer_city",
  7569. @"credit_card_first_name" : @"customer_first_name",
  7570. @"credit_card_last_name" : @"customer_last_name",
  7571. @"credit_card_state" : @"customer_state",
  7572. @"credit_card_zipcode" : @"customer_zipcode"
  7573. },
  7574. @"type" : @"pull"
  7575. }
  7576. },
  7577. @"item_1" : @{
  7578. @"aname" : @"",
  7579. @"color" : @"red",
  7580. @"control" : @"text",
  7581. @"name" : @"",
  7582. @"value" : @"USA Credit cards only"
  7583. },
  7584. @"item_2" : @{
  7585. @"aname" : @"Fill",
  7586. @"cadedate" : @{
  7587. @"count" : @(2),
  7588. @"val_0" : @{
  7589. @"check" : @(1),
  7590. @"sub_item" : @{
  7591. @"count" : @(11),
  7592. @"item_0" : @{
  7593. @"aname" : @"Type",
  7594. @"cadedate" : @{
  7595. @"count" : @(2),
  7596. @"val_0" : @{
  7597. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  7598. @"value" : @"VISA",
  7599. @"value_id" : @(0)
  7600. },
  7601. @"val_1" : @{
  7602. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  7603. @"value" : @"MASTER CARD",
  7604. @"value_id" : @(1)
  7605. }
  7606. },
  7607. @"control" : @"enum",
  7608. @"name" : @"credit_card_type",
  7609. @"required" : @"true",
  7610. @"single_select" : @"true"
  7611. },
  7612. @"item_1" : @{
  7613. @"aname" : @"Number",
  7614. @"control" : @"edit",
  7615. @"keyboard" : @"int",
  7616. @"length" : @"16",
  7617. @"name" : @"credit_card_number",
  7618. @"required" : @"true",
  7619. @"value" : cardNumber
  7620. },
  7621. @"item_10" : @{
  7622. @"aname" : @"State",
  7623. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  7624. @"control" : @"enum",
  7625. @"enum" : @"true",
  7626. @"name" : @"credit_card_state",
  7627. @"required" : @"true",
  7628. @"single_select" : @"true"
  7629. },
  7630. @"item_2" : @{
  7631. @"aname" : @"Expiration Date",
  7632. @"control" : @"monthpicker",
  7633. @"name" : @"credit_card_expiration",
  7634. @"required" : @"true",
  7635. @"type" : @"date",
  7636. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  7637. },
  7638. @"item_3" : @{
  7639. @"aname" : @"Security Code",
  7640. @"control" : @"edit",
  7641. @"keyboard" : @"int",
  7642. @"length" : @"3",
  7643. @"name" : @"credit_card_security_code",
  7644. @"required" : @"true",
  7645. @"value" : securityCode
  7646. },
  7647. @"item_4" : @{
  7648. @"aname" : @"First Name",
  7649. @"control" : @"edit",
  7650. @"keyboard" : @"text",
  7651. @"name" : @"credit_card_first_name",
  7652. @"required" : @"true",
  7653. @"value" : firstName
  7654. },
  7655. @"item_5" : @{
  7656. @"aname" : @"Last Name",
  7657. @"control" : @"edit",
  7658. @"keyboard" : @"text",
  7659. @"name" : @"credit_card_last_name",
  7660. @"required" : @"true",
  7661. @"value" : lastName
  7662. },
  7663. @"item_6" : @{
  7664. @"aname" : @"Address 1",
  7665. @"control" : @"edit",
  7666. @"keyboard" : @"text",
  7667. @"name" : @"credit_card_address1",
  7668. @"required" : @"true",
  7669. @"value" : addr1
  7670. },
  7671. @"item_7" : @{
  7672. @"aname" : @"Address 2",
  7673. @"control" : @"edit",
  7674. @"keyboard" : @"text",
  7675. @"name" : @"credit_card_address2",
  7676. @"value" : addr2
  7677. },
  7678. @"item_8" : @{
  7679. @"aname" : @"zip code",
  7680. @"control" : @"edit",
  7681. @"keyboard" : @"text",
  7682. @"name" : @"credit_card_zipcode",
  7683. @"required" : @"true",
  7684. @"value" : zipcode
  7685. },
  7686. @"item_9" : @{
  7687. @"aname" : @"City",
  7688. @"control" : @"edit",
  7689. @"keyboard" : @"text",
  7690. @"name" : @"credit_card_city",
  7691. @"required" : @"true",
  7692. @"value" : city
  7693. }
  7694. },
  7695. @"value" : @"Fill Now",
  7696. @"value_id" : @""
  7697. },
  7698. @"val_1" : @{
  7699. @"check" : @(0),
  7700. @"value" : @"Fill Later",
  7701. @"value_id" : @""
  7702. }
  7703. },
  7704. @"control" : @"enum",
  7705. @"name" : @"",
  7706. @"single_select" : @"true"
  7707. }
  7708. },
  7709. @"value" : @"Credit Card",
  7710. @"value_id" : @"Credit Card"
  7711. },
  7712. @"val_5" : @{
  7713. @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  7714. @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  7715. @"value_id" : @"FOLLOW EXISTING"
  7716. }
  7717. },
  7718. @"control" : @"enum",
  7719. @"name" : @"paymentType",
  7720. @"single_select" : @"true"
  7721. },
  7722. @"title" : @"Payment Information"
  7723. }.mutableCopy;
  7724. NSNumber *hide = setting[@"PaymentInformationHide"];
  7725. [dic setValue:hide forKey:@"hide"];
  7726. return dic;
  7727. }
  7728. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  7729. {
  7730. sqlite3 *db = [iSalesDB get_db];
  7731. // UISetting
  7732. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  7733. NSString *cachefolder = [paths objectAtIndex:0];
  7734. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  7735. NSData* json =nil;
  7736. json=[NSData dataWithContentsOfFile:img_cache];
  7737. NSError *error=nil;
  7738. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  7739. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  7740. [params setObject:setting forKey:@"setting"];
  7741. int section_count = 0;
  7742. // params
  7743. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7744. orderCode = [self translateSingleQuote:orderCode];
  7745. // 0 Order Type 1 Shipping Method 2 Payment Information
  7746. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  7747. // 0 Order Type
  7748. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  7749. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  7750. [ret setValue:order_type_dic forKey:key0];
  7751. // 1 Shipping Method
  7752. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  7753. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  7754. [ret setValue:shipping_method_dic forKey:key1];
  7755. // 2 Payment Information
  7756. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  7757. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  7758. [ret setValue:payment_info_dic forKey:key2];
  7759. // 3 Customer
  7760. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  7761. NSDictionary *customer_dic = [self customerDic:params db:db];
  7762. [ret setValue:customer_dic forKey:key3];
  7763. // 4 Ship To
  7764. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  7765. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  7766. [ret setValue:ship_to_dic forKey:key4];
  7767. // 5 Ship From
  7768. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  7769. if (![shipFromDisable integerValue]) {
  7770. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  7771. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  7772. [ret setValue:ship_from_dic forKey:key5];
  7773. }
  7774. // 6 Freight Bill To
  7775. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  7776. if (![freightBillToDisable integerValue]) {
  7777. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  7778. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  7779. [ret setValue:freight_bill_to forKey:key6];
  7780. }
  7781. // 7 Merchandise Bill To
  7782. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  7783. if (![merchandiseBillToDisable integerValue]) {
  7784. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  7785. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  7786. [ret setValue:merchandise_bill_to_dic forKey:key7];
  7787. }
  7788. // 8 Return To
  7789. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  7790. if (![returnToDisable integerValue]) {
  7791. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  7792. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  7793. [ret setValue:return_to_dic forKey:key8];
  7794. }
  7795. // 9 Model Information
  7796. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  7797. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  7798. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  7799. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  7800. [ret setValue:model_info_dic forKey:key9];
  7801. // 10 Remarks Content
  7802. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  7803. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  7804. [ret setValue:remarks_content_dic forKey:key10];
  7805. // 11 Order Total
  7806. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  7807. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  7808. [ret setValue:order_total_dic forKey:key11];
  7809. // 12 Signature
  7810. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  7811. NSDictionary *sign_dic = [self signatureDic:params db:db];
  7812. [ret setValue:sign_dic forKey:key12];
  7813. [ret setValue:@(section_count) forKey:@"section_count"];
  7814. [iSalesDB close_db:db];
  7815. return [RAUtils dict2data:ret];
  7816. // return nil;
  7817. }
  7818. #pragma mark addr editor
  7819. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  7820. NSMutableDictionary *new_item = [item mutableCopy];
  7821. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  7822. return new_item;
  7823. }
  7824. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  7825. {
  7826. // "is_subaction" = true;
  7827. // orderCode = MOB1608240002;
  7828. // password = 123456;
  7829. // "subaction_tag" = 1;
  7830. // user = EvanK;
  7831. // {
  7832. // "is_subaction" = true;
  7833. // orderCode = MOB1608240002;
  7834. // password = 123456;
  7835. // "refresh_trigger" = zipcode;
  7836. // "subaction_tag" = 1;
  7837. // user = EvanK;
  7838. // }
  7839. NSString *country_code = nil;
  7840. NSString *zipCode = nil;
  7841. NSString *stateCode = nil;
  7842. NSString *city = nil;
  7843. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  7844. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  7845. NSString *code_id = params[@"country"];
  7846. country_code = [self countryCodeByid:code_id];
  7847. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  7848. NSString *zip_code = params[@"zipcode"];
  7849. // 剔除全部为空格
  7850. int spaceCount = 0;
  7851. for (int i = 0; i < zip_code.length; i++) {
  7852. if ([zip_code characterAtIndex:i] == ' ') {
  7853. spaceCount++;
  7854. }
  7855. }
  7856. if (spaceCount == zip_code.length) {
  7857. zip_code = @"";
  7858. }
  7859. zipCode = zip_code;
  7860. if (zipCode.length > 0) {
  7861. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  7862. country_code = [dic valueForKey:@"country_code"];
  7863. if (!country_code) {
  7864. // country_code = @"US";
  7865. NSString *code_id = params[@"country"];
  7866. country_code = [self countryCodeByid:code_id];
  7867. }
  7868. stateCode = [dic valueForKey:@"state_code"];
  7869. if(!stateCode.length) {
  7870. stateCode = params[@"state"];
  7871. }
  7872. city = [dic valueForKey:@"city"];
  7873. if (!city.length) {
  7874. city = params[@"city"];
  7875. }
  7876. // zip code
  7877. // NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  7878. // [zipDic setValue:zipCode forKey:@"value"];
  7879. // [section_0 setValue:zipDic forKey:@"item_11"];
  7880. } else {
  7881. NSString *code_id = params[@"country"];
  7882. country_code = [self countryCodeByid:code_id];
  7883. stateCode = params[@"state"];
  7884. city = params[@"city"];
  7885. }
  7886. }
  7887. }
  7888. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  7889. // [ret removeObjectForKey:@"up_params"];
  7890. [ret setObject:@"New Address" forKey:@"title"];
  7891. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  7892. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  7893. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  7894. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  7895. // [country_dic removeObjectForKey:@"refresh"];
  7896. // [country_dic removeObjectForKey:@"restore"];
  7897. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  7898. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  7899. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  7900. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  7901. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  7902. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  7903. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  7904. // [zip_code_dic removeObjectForKey:@"refresh"];
  7905. [zip_code_dic setValue:zipCode ? zipCode :@"" forKey:@"value"];
  7906. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  7907. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  7908. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  7909. NSDictionary *cityDic = [[new_section_0 objectForKey:@"item_8"] mutableCopy];
  7910. [cityDic setValue:city ? city : @"" forKey:@"value"];
  7911. [new_section_0 setObject:cityDic forKey:@"item_8"];
  7912. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  7913. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  7914. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  7915. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  7916. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  7917. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  7918. // country
  7919. NSString *countryCode = country_code == nil ? @"US" : country_code;
  7920. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  7921. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  7922. // state
  7923. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:stateCode] mutableCopy];
  7924. // NSDictionary *tmpDic = @{
  7925. // @"value" : @"Other",
  7926. // @"value_id" : @"",
  7927. // @"check" : [NSNumber numberWithInteger:0]
  7928. // };
  7929. //
  7930. // for (int i = 0; i < allState.allKeys.count; i++) {
  7931. // NSString *key = [NSString stringWithFormat:@"val_%d",i];
  7932. //
  7933. // NSDictionary *tmp = allState[key];
  7934. // [allState setValue:tmpDic forKey:key];
  7935. // tmpDic = tmp;
  7936. // }
  7937. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  7938. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  7939. [ret setValue:new_section_0 forKey:@"section_0"];
  7940. return [RAUtils dict2data:ret];
  7941. }
  7942. #pragma mark save addr
  7943. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  7944. {
  7945. // NSString *companyName = [self valueInParams:params key:@"company"];
  7946. // NSString *addr1 = [self valueInParams:params key:@"address"];
  7947. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  7948. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  7949. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  7950. // NSString *countryId = [self valueInParams:params key:@"country"];
  7951. // NSString *stateCode = [self valueInParams:params key:@"state"];
  7952. // NSString *city = [self valueInParams:params key:@"city"];
  7953. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  7954. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  7955. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  7956. // NSString *phone = [self valueInParams:params key:@"phone"];
  7957. // NSString *fax = [self valueInParams:params key:@"fax"];
  7958. // NSString *email = [self valueInParams:params key:@"email"];
  7959. return [self offline_saveContact:params update:NO isCustomer:NO];
  7960. }
  7961. #pragma mark cancel order
  7962. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  7963. {
  7964. // NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7965. NSString *order_id = [self valueInParams:params key:@"orderId"];
  7966. // NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  7967. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  7968. int ret = [iSalesDB execSql:sql];
  7969. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7970. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7971. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7972. // [dic setValue:@"160409" forKey:@"min_ver"];
  7973. return [RAUtils dict2data:dic];
  7974. }
  7975. #pragma mark sign order
  7976. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  7977. {
  7978. //参考 offline_saveBusinesscard
  7979. DebugLog(@"sign order params: %@",params);
  7980. // orderCode = MOB1608240002;
  7981. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  7982. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7983. orderCode = [self translateSingleQuote:orderCode];
  7984. NSString *picPath = [self valueInParams:params key:@"picpath"];
  7985. picPath = [self translateSingleQuote:picPath];
  7986. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  7987. int ret = [iSalesDB execSql:sql];
  7988. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7989. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7990. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7991. // [dic setValue:@"160409" forKey:@"min_ver"];
  7992. return [RAUtils dict2data:dic];
  7993. }
  7994. #pragma mark save order
  7995. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  7996. NSString *ret = [self valueInParams:params key:key];
  7997. if (translate) {
  7998. ret = [self translateSingleQuote:ret];
  7999. }
  8000. return ret;
  8001. }
  8002. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  8003. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  8004. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  8005. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  8006. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  8007. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  8008. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  8009. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  8010. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  8011. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  8012. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  8013. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  8014. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  8015. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  8016. if (![total_price isEqualToString:@""]) {
  8017. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  8018. } else {
  8019. total_price = @"";
  8020. }
  8021. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  8022. if ([paymentsAndCredits isEqualToString:@""]) {
  8023. paymentsAndCredits = @"";
  8024. } else {
  8025. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  8026. }
  8027. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  8028. if ([handling_fee_value isEqualToString:@""]) {
  8029. handling_fee_value = @"";
  8030. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  8031. handling_fee_value = @"";
  8032. } else {
  8033. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  8034. }
  8035. NSString *handling_fee_placeholder = handling_fee_value;
  8036. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  8037. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  8038. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  8039. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  8040. NSString *lift_gate_placeholder = @"";
  8041. if ([lift_gate_value isEqualToString:@""]) {
  8042. lift_gate_placeholder = @"";
  8043. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  8044. lift_gate_placeholder = @"";
  8045. } else {
  8046. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  8047. }
  8048. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  8049. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  8050. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  8051. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  8052. 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];
  8053. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  8054. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  8055. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  8056. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  8057. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  8058. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  8059. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  8060. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  8061. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  8062. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  8063. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  8064. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  8065. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  8066. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  8067. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  8068. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  8069. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  8070. BOOL number_nil = [credit_card_number isEqualToString:@""];
  8071. if (!number_nil) {
  8072. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  8073. }
  8074. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  8075. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  8076. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  8077. if (!security_code_nil) {
  8078. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  8079. }
  8080. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  8081. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  8082. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  8083. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  8084. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  8085. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  8086. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  8087. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  8088. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  8089. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  8090. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  8091. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  8092. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  8093. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  8094. NSString *contact_id = customer_cid;
  8095. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  8096. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  8097. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  8098. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  8099. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  8100. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  8101. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  8102. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  8103. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  8104. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  8105. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  8106. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  8107. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  8108. //
  8109. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  8110. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  8111. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  8112. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  8113. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  8114. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  8115. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  8116. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  8117. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  8118. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  8119. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  8120. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  8121. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  8122. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  8123. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  8124. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  8125. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  8126. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  8127. 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];
  8128. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  8129. if (receive_cid.length) {
  8130. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  8131. }
  8132. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  8133. if (receive_name.length) {
  8134. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  8135. }
  8136. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  8137. if (receive_ext.length) {
  8138. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  8139. }
  8140. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  8141. if (receive_contact.length) {
  8142. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  8143. }
  8144. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  8145. if (receive_phone.length) {
  8146. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  8147. }
  8148. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  8149. if (receive_email.length) {
  8150. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  8151. }
  8152. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  8153. if (receive_fax.length) {
  8154. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  8155. }
  8156. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  8157. if (sender_cid.length) {
  8158. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  8159. }
  8160. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  8161. if (sender_name.length) {
  8162. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  8163. }
  8164. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  8165. if (sender_ext.length) {
  8166. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  8167. }
  8168. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  8169. if(sender_contact.length) {
  8170. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  8171. }
  8172. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  8173. if (sender_phone.length) {
  8174. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  8175. }
  8176. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  8177. if (sender_fax.length) {
  8178. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  8179. }
  8180. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  8181. if (sender_email.length) {
  8182. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  8183. }
  8184. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  8185. if (shipping_billto_cid.length) {
  8186. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  8187. }
  8188. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  8189. if (shipping_billto_name.length) {
  8190. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  8191. }
  8192. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  8193. if (shipping_billto_ext.length) {
  8194. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  8195. }
  8196. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  8197. if (shipping_billto_contact.length) {
  8198. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  8199. }
  8200. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  8201. if (shipping_billto_phone.length) {
  8202. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  8203. }
  8204. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  8205. if (shipping_billto_fax.length) {
  8206. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  8207. }
  8208. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  8209. if (shipping_billto_email.length) {
  8210. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  8211. }
  8212. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  8213. if (billing_cid.length) {
  8214. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  8215. }
  8216. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  8217. if (billing_name.length) {
  8218. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  8219. }
  8220. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  8221. if (billing_ext.length) {
  8222. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  8223. }
  8224. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  8225. if (billing_contact.length) {
  8226. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  8227. }
  8228. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  8229. if (billing_phone.length) {
  8230. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  8231. }
  8232. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  8233. if (billing_fax.length) {
  8234. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  8235. }
  8236. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  8237. if (billing_email.length) {
  8238. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  8239. }
  8240. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  8241. if (returnto_cid.length) {
  8242. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  8243. }
  8244. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  8245. if (returnto_name.length) {
  8246. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  8247. }
  8248. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  8249. if (returnto_ext.length) {
  8250. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  8251. }
  8252. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  8253. if (returnto_contact.length) {
  8254. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  8255. }
  8256. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  8257. if (returnto_phone.length) {
  8258. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  8259. }
  8260. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  8261. if (returnto_fax.length) {
  8262. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  8263. }
  8264. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  8265. if (returnto_email.length) {
  8266. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  8267. }
  8268. NSString *order_status = @"status = 1,";
  8269. if (submit) {
  8270. order_status = @"status = -11,";
  8271. }
  8272. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  8273. NSString *sync_sql = @"";
  8274. if (submit) {
  8275. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  8276. [param setValue:sales_rep forKey:@"sales_rep"];
  8277. NSString *sync_data = [self translateSingleQuote:[RAUtils dict2string:param]];
  8278. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  8279. }
  8280. 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];
  8281. DebugLog(@"save order contactSql: %@",contactSql);
  8282. DebugLog(@"save order orderSql: %@",orderSql);
  8283. // int contact_ret = [iSalesDB execSql:contactSql];
  8284. int order_ret = [iSalesDB execSql:orderSql];
  8285. int ret = order_ret;
  8286. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8287. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8288. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8289. // [dic setValue:@"160409" forKey:@"min_ver"];
  8290. [dic setObject:so_id forKey:@"so#"];
  8291. return [RAUtils dict2data:dic];
  8292. }
  8293. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  8294. {
  8295. // id foo = nil;
  8296. // NSMutableArray *a = @[].mutableCopy;
  8297. // [a addObject:foo];
  8298. return [self saveorder:param submit:NO];
  8299. }
  8300. #pragma mark add to cart by name
  8301. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  8302. {
  8303. NSString *orderCode = [params objectForKey:@"orderCode"];
  8304. NSString *product_name = [params objectForKey:@"product_name"];
  8305. product_name = [self translateSingleQuote:product_name];
  8306. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  8307. sqlite3 *db = [iSalesDB get_db];
  8308. __block NSMutableString *product_id_string = [NSMutableString string];
  8309. for (int i = 0; i < product_name_array.count; i++) {
  8310. NSString *name = [product_name_array objectAtIndex:i];
  8311. NSString *sql = [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  8312. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8313. int product_id = sqlite3_column_int(stmt, 0);
  8314. if (i == product_name_array.count - 1) {
  8315. [product_id_string appendFormat:@"%d",product_id];
  8316. } else {
  8317. [product_id_string appendFormat:@"%d,",product_id];
  8318. }
  8319. }];
  8320. }
  8321. NSDictionary *newParams = @{
  8322. @"product_id" : product_id_string,
  8323. @"orderCode" : orderCode
  8324. };
  8325. [iSalesDB close_db:db];
  8326. return [self offline_add2cart:[newParams mutableCopy]];
  8327. }
  8328. #pragma mark reset order
  8329. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  8330. {
  8331. UIApplication * app = [UIApplication sharedApplication];
  8332. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  8333. appDelegate.disable_trigger=true;
  8334. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  8335. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  8336. appDelegate.disable_trigger=false;
  8337. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8338. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  8339. return [RAUtils dict2data:dic];
  8340. }
  8341. #pragma mark submit order
  8342. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  8343. {
  8344. return [self saveorder:params submit:YES];
  8345. }
  8346. #pragma mark copy order
  8347. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  8348. {
  8349. NSMutableDictionary *ret = @{}.mutableCopy;
  8350. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  8351. sqlite3 *db = [iSalesDB get_db];
  8352. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  8353. // 首先查看联系人是否active
  8354. 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];
  8355. __block int customer_is_active = 1;
  8356. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8357. customer_is_active = sqlite3_column_int(stmt, 0);
  8358. }];
  8359. if (!customer_is_active) {
  8360. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8361. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8362. [iSalesDB close_db:db];
  8363. return [RAUtils dict2data:ret];
  8364. }
  8365. // new order
  8366. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  8367. NSString *new_order_code = [self get_offline_soid:db];
  8368. NSString *user = ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  8369. user = [self translateSingleQuote:user];
  8370. 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,sales_terms) 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,sales_terms from offline_order where so_id = '%@';",new_order_code,user,order_code]; // ,paymentsAndCredits,shipping,lift_gate_value,handling_fee_value
  8371. __block int result = 1;
  8372. result = [iSalesDB execSql:insert_order_sql db:db];
  8373. if (!result) {
  8374. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8375. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8376. [iSalesDB close_db:db];
  8377. return [RAUtils dict2data:ret];
  8378. }
  8379. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  8380. // __block NSString *product_id = @"";
  8381. __weak typeof(self) weakSelf = self;
  8382. 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];
  8383. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8384. int is_active = sqlite3_column_int(stmt, 1);
  8385. if (is_active) {
  8386. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  8387. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  8388. 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];
  8389. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  8390. }
  8391. }];
  8392. // product_id = [product_id substringFromIndex:1];
  8393. //
  8394. // [self offline_add2cart:@{}.mutableCopy];
  8395. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8396. [iSalesDB close_db:db];
  8397. if (result) {
  8398. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8399. } else {
  8400. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8401. }
  8402. [ret setObject:new_order_code forKey:@"so_id"];
  8403. return [RAUtils dict2data:ret];
  8404. }
  8405. #pragma mark move wish list to cart
  8406. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  8407. NSString *collectId = params[@"collectId"];
  8408. NSString *sql = [NSString stringWithFormat:@"select product_id,qty from wishlist where _id in (%@);",collectId];
  8409. __block NSString *product_id = @"";
  8410. __block NSString *qty = @"";
  8411. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8412. int productId = sqlite3_column_int(stmt, 0);
  8413. int item_qty = sqlite3_column_int(stmt, 1);
  8414. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  8415. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  8416. }];
  8417. product_id = [product_id substringFromIndex:1];
  8418. qty = [qty substringFromIndex:1];
  8419. NSDictionary *newParams = @{
  8420. @"product_id" : product_id,
  8421. @"orderCode" : params[@"orderCode"],
  8422. @"qty" : qty
  8423. };
  8424. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  8425. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  8426. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  8427. // NSString *deleteSQL = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  8428. //
  8429. // int ret = [iSalesDB execSql:deleteSQL];
  8430. int ret = [[[self offline_deletewishlist:@{@"collectId" : collectId}.mutableCopy] valueForKey:@"result"] integerValue];
  8431. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  8432. }
  8433. return [RAUtils dict2data:retDic];
  8434. }
  8435. #pragma mark - portfolio
  8436. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  8437. int sort = [[params valueForKey:@"sort"] integerValue];
  8438. int offset = [[params valueForKey:@"offset"] integerValue];
  8439. int limit = [[params valueForKey:@"limit"] integerValue];
  8440. NSString *orderBy = @"";
  8441. switch (sort) {
  8442. case 0:{
  8443. orderBy = @"modify_time desc";
  8444. }
  8445. break;
  8446. case 1:{
  8447. orderBy = @"modify_time asc";
  8448. }
  8449. break;
  8450. case 2:{
  8451. orderBy = @"name asc";
  8452. }
  8453. break;
  8454. case 3:{
  8455. orderBy = @"name desc";
  8456. }
  8457. break;
  8458. case 4:{
  8459. orderBy = @"description asc";
  8460. }
  8461. break;
  8462. default:
  8463. break;
  8464. }
  8465. 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];
  8466. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8467. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8468. sqlite3 *db = [iSalesDB get_db];
  8469. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8470. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8471. int product_id = sqlite3_column_int(stmt, 0);
  8472. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  8473. NSString *name = [self textAtColumn:1 statement:stmt];
  8474. NSString *description = [self textAtColumn:2 statement:stmt];
  8475. double price = sqlite3_column_double(stmt, 3);
  8476. double discount = sqlite3_column_double(stmt,4);
  8477. int qty = sqlite3_column_int(stmt, 5);
  8478. int percentage = sqlite3_column_int(stmt, 6);
  8479. int item_id = sqlite3_column_int(stmt, 7);
  8480. // int fashion_id = sqlite3_column_int(stmt, 8);
  8481. NSString *img_path = [self textAtColumn:9 statement:stmt];
  8482. NSString *line_note = [self textAtColumn:10 statement:stmt];
  8483. double percent = sqlite3_column_double(stmt, 11);
  8484. NSString *price_null = [self textAtColumn:3 statement:stmt];
  8485. if ([price_null isEqualToString:@"null"]) {
  8486. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  8487. }
  8488. NSMutableDictionary *item = @{
  8489. @"linenotes": line_note,
  8490. @"check": @(1),
  8491. @"product_id": product_id_string,
  8492. @"available_qty": @(qty),
  8493. @"available_percent" : @(percent),
  8494. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  8495. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  8496. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  8497. @"img": img_path,
  8498. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  8499. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  8500. }.mutableCopy;
  8501. if (percentage) {
  8502. [item removeObjectForKey:@"available_qty"];
  8503. } else {
  8504. [item removeObjectForKey:@"available_percent"];
  8505. }
  8506. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  8507. if ([qty_null isEqualToString:@"null"]) {
  8508. [item removeObjectForKey:@"available_qty"];
  8509. }
  8510. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  8511. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  8512. }];
  8513. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  8514. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8515. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  8516. }
  8517. [iSalesDB close_db:db];
  8518. [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  8519. [dic setValue:@"" forKey:@"email_content"];
  8520. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  8521. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8522. return [RAUtils dict2data:dic];
  8523. }
  8524. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  8525. __block int qty = 0;
  8526. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  8527. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8528. qty = sqlite3_column_int(stmt, 0);
  8529. }];
  8530. return qty;
  8531. }
  8532. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct {
  8533. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  8534. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  8535. NSMutableDictionary * values = params[@"replaceValue"];
  8536. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  8537. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  8538. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  8539. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  8540. NSString *pdf_path = @"";
  8541. if (direct) {
  8542. configureParams = [self valueInParams:params key:@"pdfUrl"];
  8543. } else {
  8544. pdf_path = [self valueInParams:params key:@"pdfPath"];
  8545. }
  8546. NSString *create_user = [self valueInParams:params key:@"user"];
  8547. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  8548. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  8549. // model info
  8550. 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];
  8551. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8552. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  8553. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8554. sqlite3 *db = [iSalesDB get_db];
  8555. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8556. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8557. int product_id = sqlite3_column_int(stmt, 0);
  8558. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  8559. double price = sqlite3_column_double(stmt, 1);
  8560. double discount = sqlite3_column_double(stmt,2);
  8561. int qty = sqlite3_column_int(stmt, 3);
  8562. int percentage = sqlite3_column_int(stmt, 4);
  8563. int item_id = sqlite3_column_int(stmt, 5);
  8564. NSString *line_note = [self textAtColumn:6 statement:stmt];
  8565. double percent = sqlite3_column_double(stmt, 7);
  8566. NSString *price_null = [self textAtColumn:1 statement:stmt];
  8567. /* if ([price_null isEqualToString:@"null"]) {
  8568. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  8569. }
  8570. */
  8571. [product_ids_string appendFormat:@"%@,",product_id_string];
  8572. // Regular Price
  8573. int regular_price = [[params objectForKey:@"regular_price"] integerValue];
  8574. NSString *regular_price_str = [self get_portfolio_price:appDelegate.contact_id item_id:item_id price:regular_price db:db];
  8575. // QTY
  8576. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  8577. if ([qty_null isEqualToString:@"null"]) {
  8578. // 查available
  8579. qty = [self model_QTY:product_id_string db:db];
  8580. }
  8581. if (percentage) {
  8582. qty = qty * percent / 100;
  8583. }
  8584. // Special Price
  8585. if ([price_null isEqualToString:@"null"]) {
  8586. // price = regular price
  8587. price = [regular_price_str doubleValue];
  8588. }
  8589. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  8590. if (![discount_null isEqualToString:@"null"]) {
  8591. price = price * (1 - discount / 100.0);
  8592. }
  8593. NSMutableDictionary *item = @{
  8594. @"line_note": line_note,
  8595. @"product_id": product_id_string,
  8596. @"available_qty": @(qty),
  8597. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  8598. @"regular_price" : regular_price_str,
  8599. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  8600. }.mutableCopy;
  8601. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  8602. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  8603. }];
  8604. [iSalesDB close_db:db];
  8605. if (product_ids_string.length > 0) {
  8606. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  8607. }
  8608. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  8609. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  8610. return [RAUtils dict2data:resultDictionary];
  8611. }
  8612. NSString *model_info = [RAUtils dict2string:dic];
  8613. // 创建PDF
  8614. // 在preview情况下保存,则不需要新建了
  8615. if (direct) {
  8616. NSMutableDictionary *tear_sheet_params = params;
  8617. // if (tear_sheet_id) {
  8618. // tear_sheet_params = values;
  8619. // }
  8620. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  8621. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  8622. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  8623. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  8624. resultDictionary = pdfInfo.mutableCopy;
  8625. } else { // 创建PDF失败
  8626. return pdfData;
  8627. }
  8628. } else {
  8629. // pdf_path 就是本地路径
  8630. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  8631. }
  8632. // 将文件移动到PDF Cache文件夹
  8633. NSString *newPath = [pdf_path lastPathComponent];
  8634. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8635. NSString *cachefolder = [paths objectAtIndex:0];
  8636. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  8637. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  8638. NSFileManager *fileManager = [NSFileManager defaultManager];
  8639. NSError *error = nil;
  8640. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  8641. if (error) { // 移动文件失败
  8642. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  8643. return [RAUtils dict2data:resultDictionary];
  8644. }
  8645. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  8646. pdf_path = [newPath lastPathComponent];
  8647. // 保存信息
  8648. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  8649. NSString *off_params = [RAUtils dict2string:params];
  8650. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  8651. pdf_path = [self translateSingleQuote:pdf_path];
  8652. create_user = [self translateSingleQuote:create_user];
  8653. tear_note = [self translateSingleQuote:tear_note];
  8654. tear_name = [self translateSingleQuote:tear_name];
  8655. model_info = [self translateSingleQuote:model_info];
  8656. configureParams = [self translateSingleQuote:configureParams];
  8657. off_params = [self translateSingleQuote:off_params];
  8658. 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];
  8659. if (tear_sheet_id) {
  8660. int _id = [tear_sheet_id integerValue];
  8661. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  8662. }
  8663. int result = [iSalesDB execSql:save_pdf_sql];
  8664. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  8665. //
  8666. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  8667. if (remove_Item) {
  8668. // portfolioId
  8669. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  8670. }
  8671. return [RAUtils dict2data:resultDictionary];
  8672. }
  8673. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  8674. return [self offline_savePDF:params direct:YES];
  8675. }
  8676. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  8677. return [self offline_savePDF:params direct:NO];
  8678. }
  8679. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  8680. int offset = [[params valueForKey:@"offset"] integerValue];
  8681. int limit = [[params valueForKey:@"limit"] integerValue];
  8682. NSString *keyword = [params valueForKey:@"keyWord"];
  8683. NSString *where = @"where is_delete is null or is_delete = 0";
  8684. if (keyword.length) {
  8685. keyword = [self translateSingleQuote:keyword];
  8686. 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];
  8687. }
  8688. 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
  8689. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8690. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8691. NSString *cachefolder = [paths objectAtIndex:0];
  8692. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  8693. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8694. NSString *name = [self textAtColumn:0 statement:stmt];
  8695. NSString *note = [self textAtColumn:1 statement:stmt];
  8696. NSString *time = [self textAtColumn:2 statement:stmt];
  8697. NSString *user = [self textAtColumn:3 statement:stmt];
  8698. NSString *path = [self textAtColumn:4 statement:stmt];
  8699. NSString *model_info = [self textAtColumn:6 statement:stmt];
  8700. NSString *off_params = [self textAtColumn:7 statement:stmt];
  8701. path = [pdfFolder stringByAppendingPathComponent:path];
  8702. BOOL bdir=NO;
  8703. NSFileManager* fileManager = [NSFileManager defaultManager];
  8704. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  8705. {
  8706. //pdf文件不存在
  8707. path=nil;
  8708. }
  8709. time = [self changeDateTimeFormate:time];
  8710. time = [time stringByAppendingString:@" PST"];
  8711. int sheet_id = sqlite3_column_int(stmt, 5);
  8712. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  8713. item[@"tearsheetsId"]=@(sheet_id);
  8714. item[@"pdf_path"]=path;
  8715. item[@"create_time"]=time;
  8716. item[@"create_user"]=user;
  8717. item[@"tear_note"]=note;
  8718. item[@"tear_name"]=name;
  8719. item[@"isLocalFile"]=[NSNumber numberWithBool:YES];
  8720. item[@"model_info"]=model_info;
  8721. item[@"off_params"]=off_params;
  8722. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  8723. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  8724. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  8725. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8726. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  8727. }];
  8728. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8729. return [RAUtils dict2data:dic];
  8730. }
  8731. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  8732. NSString *prodct_ids = [params objectForKey:@"product_id"];
  8733. // NSString *user = [params objectForKey:@"user"];
  8734. // 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];
  8735. 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];
  8736. int result = [iSalesDB execSql:sql];
  8737. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8738. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  8739. if (result == RESULT_TRUE) {
  8740. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  8741. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  8742. sqlite3 *db = [iSalesDB get_db];
  8743. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  8744. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  8745. [iSalesDB close_db:db];
  8746. }
  8747. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8748. return [RAUtils dict2data:dic];
  8749. }
  8750. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  8751. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  8752. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  8753. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  8754. // Regurlar Price
  8755. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  8756. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  8757. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  8758. __block long val_count = 0;
  8759. NSString *sql = @"select name,type,order_by from price order by order_by";
  8760. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8761. NSString *name = [self textAtColumn:0 statement:stmt];
  8762. int type = sqlite3_column_int(stmt, 1);
  8763. int order_by = sqlite3_column_int(stmt, 2);
  8764. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8765. NSDictionary *price_dic = @{
  8766. @"value" : name,
  8767. @"value_id" : [NSNumber numberWithInteger:type],
  8768. @"check" : order_by == 0 ? @(1) : @(0)
  8769. };
  8770. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  8771. val_count = ++(*count);
  8772. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  8773. val_count = 0;
  8774. }];
  8775. [cadedate setObject:@{@"value" : @"None",
  8776. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  8777. val_count++;
  8778. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  8779. [price setObject:cadedate forKey:@"cadedate"];
  8780. [section1 setObject:price forKey:@"item_2"];
  8781. [dic setObject:section1 forKey:@"section_1"];
  8782. return [RAUtils dict2data:dic];
  8783. }
  8784. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  8785. NSString *product_id = [params objectForKey:@"fashionId"];
  8786. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  8787. __block int result = RESULT_TRUE;
  8788. __block int qty = 0;
  8789. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8790. qty = sqlite3_column_int(stmt, 0);
  8791. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  8792. result = RESULT_FALSE;
  8793. }];
  8794. NSMutableDictionary *dic = @{
  8795. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  8796. @"mode" : @"Regular Mode",
  8797. @"quantity_available" : @(qty),
  8798. @"result" : @(result),
  8799. }.mutableCopy;
  8800. return [RAUtils dict2data:dic];
  8801. }
  8802. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  8803. NSString *item_ids = [params objectForKey:@"item_id"];
  8804. NSString *line_notes = [params objectForKey:@"notes"];
  8805. NSString *price_str = [params objectForKey:@"price"];
  8806. NSString *discount_str = [params objectForKey:@"discount"];
  8807. NSString *percent = [params objectForKey:@"available_percent"];
  8808. NSString *qty = [params objectForKey:@"available_qty"];
  8809. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  8810. int dot = 0;
  8811. if (line_notes) {
  8812. line_notes = [self translateSingleQuote:line_notes];
  8813. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  8814. sql = [sql stringByAppendingString:line_notes];
  8815. dot = 1;
  8816. } else {
  8817. line_notes = @"";
  8818. }
  8819. if (price_str) {
  8820. if (dot) {
  8821. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  8822. } else {
  8823. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  8824. dot = 1;
  8825. }
  8826. sql = [sql stringByAppendingString:price_str];
  8827. } else {
  8828. price_str = @"";
  8829. }
  8830. if (discount_str) {
  8831. if (dot) {
  8832. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  8833. } else {
  8834. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  8835. dot = 1;
  8836. }
  8837. sql = [sql stringByAppendingString:discount_str];
  8838. } else {
  8839. discount_str = @"";
  8840. }
  8841. if (percent) {
  8842. if (dot) {
  8843. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  8844. } else {
  8845. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  8846. dot = 1;
  8847. }
  8848. sql = [sql stringByAppendingString:percent];
  8849. } else {
  8850. percent = @"";
  8851. }
  8852. if (qty) {
  8853. if (dot) {
  8854. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  8855. } else {
  8856. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  8857. dot = 1;
  8858. }
  8859. sql = [sql stringByAppendingString:qty];
  8860. } else {
  8861. qty = @"";
  8862. }
  8863. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  8864. sql = [sql stringByAppendingString:where];
  8865. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8866. int result = [iSalesDB execSql:sql];
  8867. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  8868. [dic setObject:@"Regular Mode" forKey:@"mode"];
  8869. return [RAUtils dict2data:dic];
  8870. }
  8871. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  8872. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  8873. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8874. sqlite3 *db = [iSalesDB get_db];
  8875. 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];
  8876. int result = [iSalesDB execSql:sql db:db];
  8877. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  8878. [dic setObject:@"Regular Mode" forKey:@"mode"];
  8879. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  8880. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  8881. [iSalesDB close_db:db];
  8882. return [RAUtils dict2data:dic];
  8883. }
  8884. + (void)offline_removePDFWithName:(NSString *)name {
  8885. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8886. NSString *cachefolder = [paths objectAtIndex:0];
  8887. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  8888. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  8889. NSFileManager *fileManager = [NSFileManager defaultManager];
  8890. [fileManager removeItemAtPath:path error:nil];
  8891. }
  8892. + (void)offline_clear_PDFCache {
  8893. NSFileManager *fileManager = [NSFileManager defaultManager];
  8894. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8895. NSString *cachefolder = [paths objectAtIndex:0];
  8896. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  8897. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  8898. for (NSString *path in pdf_files) {
  8899. [self offline_removePDFWithName:[path lastPathComponent]];
  8900. }
  8901. }
  8902. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  8903. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8904. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  8905. NSString *user = [params objectForKey:@"user"];
  8906. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %d;",tearsheetsId]]; // tearsheets_id ---> _id
  8907. if (![create_user isEqualToString:user]) {
  8908. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  8909. [dic setObject:@"Regular Mode" forKey:@"mode"];
  8910. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  8911. return [RAUtils dict2data:dic];
  8912. }
  8913. NSString *is_local_sql = [NSString stringWithFormat:@"select is_local,pdf_path from offline_pdf where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  8914. __block int is_local = 0;
  8915. __block NSString *path = @"";
  8916. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8917. is_local = sqlite3_column_int(stmt, 0);
  8918. path = [self textAtColumn:1 statement:stmt];
  8919. }];
  8920. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  8921. if (is_local == 1) {
  8922. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  8923. // 删除文件
  8924. [self offline_removePDFWithName:path];
  8925. }
  8926. int result = [iSalesDB execSql:sql];
  8927. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  8928. [dic setObject:@"Regular Mode" forKey:@"mode"];
  8929. return [RAUtils dict2data:dic];
  8930. }
  8931. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial
  8932. {
  8933. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  8934. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  8935. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  8936. NSString* where=@"1=1";
  8937. if (ver!=nil) {
  8938. where=@"is_dirty=1";
  8939. }
  8940. 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];
  8941. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  8942. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8943. sqlite3 *db = [iSalesDB get_db];
  8944. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8945. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  8946. NSInteger _id = sqlite3_column_int(stmt, 0);
  8947. NSInteger product_id = sqlite3_column_int(stmt, 1);
  8948. NSString *name = [self textAtColumn:2 statement:stmt];
  8949. NSString *desc = [self textAtColumn:3 statement:stmt];
  8950. NSInteger item_id = sqlite3_column_int(stmt, 4);
  8951. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  8952. NSInteger qty = sqlite3_column_int(stmt, 6);
  8953. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  8954. double percent = sqlite3_column_double(stmt, 8);
  8955. double price = sqlite3_column_double(stmt, 9);
  8956. double discount = sqlite3_column_double(stmt, 10);
  8957. NSString *img = [self textAtColumn:11 statement:stmt];
  8958. NSString *line_note = [self textAtColumn:12 statement:stmt];
  8959. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  8960. NSString *create_time = [self textAtColumn:14 statement:stmt];
  8961. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  8962. NSString *price_null = [self textAtColumn:9 statement:stmt];
  8963. if ([price_null isEqualToString:@"null"]) {
  8964. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  8965. }
  8966. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  8967. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  8968. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  8969. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  8970. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  8971. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  8972. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  8973. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  8974. if ([qty_null isEqualToString:@"null"]) {
  8975. // [item setValue:@"null" forKey:@"available_qty"];
  8976. } else {
  8977. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  8978. }
  8979. if ([is_percent_null isEqualToString:@"null"]) {
  8980. // [item setValue:@"null" forKey:@"percentage"];
  8981. } else {
  8982. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  8983. }
  8984. if ([percent_null isEqualToString:@"null"]) {
  8985. // [item setValue:@"null" forKey:@"percent"];
  8986. } else {
  8987. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  8988. }
  8989. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  8990. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  8991. [item setValue:name forKey:@"name"];
  8992. [item setValue:desc forKey:@"description"];
  8993. [item setValue:img forKey:@"img"];
  8994. [item setValue:line_note forKey:@"line_note"];
  8995. [item setValue:create_time forKey:@"createtime"];
  8996. [item setValue:modify_time forKey:@"modifytime"];
  8997. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  8998. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8999. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  9000. }];
  9001. [iSalesDB close_db:db];
  9002. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  9003. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9004. }
  9005. return ret;
  9006. }
  9007. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  9008. {
  9009. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  9010. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  9011. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  9012. NSString* where=@"1=1";
  9013. if (ver!=nil) {
  9014. where=@"is_dirty=1";
  9015. }
  9016. 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];
  9017. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  9018. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9019. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  9020. NSInteger _id = sqlite3_column_int(stmt, 0);
  9021. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  9022. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  9023. NSString *create_user = [self textAtColumn:3 statement:stmt];
  9024. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  9025. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  9026. NSString *model_info = [self textAtColumn:6 statement:stmt];
  9027. NSString *createtime = [self textAtColumn:7 statement:stmt];
  9028. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  9029. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  9030. NSString *off_params = [self textAtColumn:10 statement:stmt];
  9031. NSString *uuid = [NSUUID UUID].UUIDString;
  9032. int is_delete = sqlite3_column_int(stmt, 11);
  9033. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  9034. int is_local = sqlite3_column_int(stmt, 12);
  9035. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  9036. if (![tearsheet_id_null isEqualToString:@"null"]) {
  9037. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  9038. }
  9039. [item setObject:pdf_path forKey:@"pdf_path"];
  9040. [item setObject:create_user forKey:@"create_user"];
  9041. [item setObject:tear_note forKey:@"tear_note"];
  9042. [item setObject:tear_name forKey:@"tear_name"];
  9043. [item setObject:model_info forKey:@"model_info"];
  9044. [item setObject:createtime forKey:@"createtime"];
  9045. [item setObject:modifytime forKey:@"modifytime"];
  9046. [item setObject:urlParams forKey:@"urlParams"];
  9047. [item setObject:off_params forKey:@"off_params"];
  9048. [item setObject:uuid forKey:@"pdf_token"];
  9049. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  9050. NSDictionary *off_Params_dic = [RAUtils string2dict:off_params];
  9051. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  9052. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  9053. } else {
  9054. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  9055. }
  9056. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9057. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  9058. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9059. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9060. }];
  9061. return ret;
  9062. }
  9063. @end