OLDataProvider.m 559 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078
  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. #import "ImageUtils.h"
  22. #import "RAConvertor.h"
  23. @interface OLDataProvider ()
  24. @end
  25. @implementation OLDataProvider
  26. +(NSString*) get_portfolio_price:(NSString*) contact_id item_id:(int) item_id price:(int)price_id db:(sqlite3*)db
  27. {
  28. 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];
  29. NSString* ret=@"No Price.";
  30. sqlite3_stmt * statement;
  31. // int count=0;
  32. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  33. {
  34. if (sqlite3_step(statement) == SQLITE_ROW)
  35. {
  36. char *price = (char*)sqlite3_column_text(statement, 0);
  37. if(price==nil)
  38. price="";
  39. NSString *nsprice = [[NSString alloc]initWithUTF8String:price];
  40. double dp= [nsprice doubleValue];
  41. ret=[NSString stringWithFormat:@"%.2f",dp];
  42. }
  43. sqlite3_finalize(statement);
  44. }
  45. return ret;
  46. // // [iSalesDB close_db:db];
  47. //
  48. // return nil;
  49. //// if(dprice==DBL_MAX)
  50. //// ret= nil;
  51. //// else
  52. //// ret= [NSNumber numberWithDouble:dprice];
  53. //// return ret;
  54. }
  55. + (NSString *)portfolio_img:(NSString *)item_id db:(sqlite3 *)db
  56. {
  57. NSString* ret= nil;
  58. NSString *sqlQuery = nil;
  59. // 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
  60. 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];
  61. sqlite3_stmt * statement;
  62. // int count=0;
  63. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  64. {
  65. if (sqlite3_step(statement) == SQLITE_ROW)
  66. {
  67. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  68. if(imgurl==nil)
  69. imgurl="";
  70. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  71. ret=nsimgurl;
  72. }
  73. sqlite3_finalize(statement);
  74. }
  75. else
  76. {
  77. ret=@"";
  78. }
  79. // [iSalesDB close_db:db];
  80. // DebugLog(@"data string: %@",ret );
  81. return ret;
  82. }
  83. +(NSMutableDictionary*) prepare_portfolio_data:(NSMutableDictionary*) params
  84. {
  85. NSMutableDictionary* values=params[@"replaceValue"]; // 离线下发的参数
  86. //生成portfolio pdf需要的数据
  87. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  88. data[@"npd_url"]=COMPANY_WEB;
  89. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  90. [formatter setDateFormat:@"MMMM yyyy"];
  91. NSString* date = [formatter stringFromDate:[NSDate date]];
  92. data[@"create_month"]=date;
  93. data[@"company_name"]=COMPANY_FULL_NAME;
  94. data[@"catalog_name"]=params[@"catalog_name"];
  95. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  96. data[@"TOTAL_PAGE"]=0;
  97. data[@"CURRENT_PAGE"]=0;
  98. NSMutableDictionary* grid = [[NSMutableDictionary alloc]init];
  99. BOOL show_group_by = [[params objectForKey:@"show_group_by"] boolValue];
  100. bool show_stockout=[params[@"show_stockout"] boolValue];
  101. NSString *sql = nil;
  102. // v1.90 more color
  103. if(values==nil)
  104. {
  105. 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,b.more_color 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"]];
  106. }
  107. else
  108. {
  109. 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,more_color from product where product_id in(%@) order by name",params[@"product_ids"]];
  110. }
  111. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  112. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  113. sqlite3 *db = [iSalesDB get_db];
  114. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  115. // [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  116. int product_id = sqlite3_column_int(stmt, 0);
  117. // NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  118. NSString *name = [self textAtColumn:1 statement:stmt];
  119. NSString *description = [self textAtColumn:2 statement:stmt];
  120. double price = sqlite3_column_double(stmt, 3);
  121. NSString *s_price = [self textAtColumn:3 statement:stmt];
  122. double discount = sqlite3_column_double(stmt,4);
  123. int qty = sqlite3_column_int(stmt, 5);
  124. NSString *s_qty = [self textAtColumn:5 statement:stmt];
  125. int is_percentage = sqlite3_column_int(stmt, 6);
  126. NSString *s_is_percentage = [self textAtColumn:6 statement:stmt];
  127. int item_id = sqlite3_column_int(stmt, 7);
  128. // int fashion_id = sqlite3_column_int(stmt, 8);
  129. NSString *line_note = [self textAtColumn:9 statement:stmt];
  130. NSString *s_percent = [self textAtColumn:10 statement:stmt];
  131. double percent = sqlite3_column_double(stmt, 10);
  132. // int portfolio_id = sqlite3_column_int(stmt, 11);
  133. NSString *s_availability = [self textAtColumn:12 statement:stmt];
  134. int availability = sqlite3_column_int(stmt, 12);
  135. NSString *color = [self textAtColumn:13 statement:stmt];
  136. NSString *legcolor = [self textAtColumn:14 statement:stmt];
  137. NSString *demension = [self textAtColumn:15 statement:stmt];
  138. NSString *seat_height = [self textAtColumn:16 statement:stmt];
  139. NSString *material = [self textAtColumn:17 statement:stmt];
  140. NSString *box_dim = [self textAtColumn:18 statement:stmt];
  141. NSString *s_volume = [self textAtColumn:19 statement:stmt];
  142. double volume = sqlite3_column_double(stmt, 19);
  143. NSString *s_weight = [self textAtColumn:20 statement:stmt];
  144. double weight = sqlite3_column_double(stmt, 20);
  145. NSString *s_model_set = [self textAtColumn:21 statement:stmt];
  146. int model_set = sqlite3_column_int(stmt, 21);
  147. NSString *s_load_ability = [self textAtColumn:22 statement:stmt];
  148. double load_ability = sqlite3_column_double(stmt, 22);
  149. NSString *fabric_content = [self textAtColumn:23 statement:stmt];
  150. NSString *assembling = [self textAtColumn:24 statement:stmt];
  151. NSString *made_in = [self textAtColumn:25 statement:stmt];
  152. NSInteger more_color_int = sqlite3_column_int(stmt, 26);
  153. NSString* gprice = [self get_portfolio_price :appDelegate.contact_id item_id:item_id price:[params[@"regular_price"] intValue] db:db];
  154. float bprice=0;
  155. NSDictionary* bundle=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  156. for(int b=0;b< [bundle[@"count"] intValue];b++)
  157. {
  158. NSDictionary* bi=bundle[[NSString stringWithFormat:@"item_%d",b]];
  159. bprice+= [bi[@"modulus"] intValue] * [bi[@"unit_price"] floatValue];
  160. }
  161. if(gprice!=nil)
  162. gprice = [NSString stringWithFormat:@"%.2f",[gprice floatValue]+bprice];
  163. if(s_percent.length==0||is_percentage==0)
  164. {
  165. percent=100.0;
  166. if([s_qty isEqualToString:@"null"])
  167. qty=availability;
  168. }
  169. else
  170. {
  171. qty=availability;
  172. }
  173. qty=qty*percent/100+0.5;
  174. for(int i=0;i<[values[@"count"] intValue];i++)
  175. {
  176. NSMutableDictionary * valueitem=values[[NSString stringWithFormat:@"item_%d",i]];;
  177. if([valueitem[@"product_id"] intValue]==product_id)
  178. {
  179. line_note=valueitem[@"line_note"];
  180. qty=[valueitem[@"available_qty"] intValue];
  181. if([[valueitem allKeys] containsObject:@"available_qty"])
  182. s_qty=[NSString stringWithFormat:@"%d",qty];
  183. else
  184. s_qty=@"null";
  185. if(valueitem[@"regular_price"]!=nil)
  186. gprice=[NSString stringWithFormat:@"%.2f",[valueitem[@"regular_price"] floatValue]];
  187. s_price=valueitem[@"special_price"];
  188. price=[valueitem[@"special_price"] floatValue];
  189. discount=0;
  190. // v1.90
  191. more_color_int = [[valueitem objectForKey:@"more_color"] integerValue];
  192. }
  193. }
  194. if(is_percentage==0&&qty==0&&!show_stockout)
  195. {
  196. return;
  197. }
  198. if((is_percentage!=0||s_is_percentage.length!=0)&&qty==0&&!show_stockout)
  199. {
  200. return;
  201. }
  202. NSString* set_price=@"";
  203. if([params[@"entered_price"] boolValue])
  204. {
  205. if (s_price==nil ) {
  206. // set_price=[NSString stringWithFormat:@"Special: $ %@",gprice];
  207. }
  208. else
  209. {
  210. if(price*(1-discount/100.0)!=gprice.floatValue)
  211. set_price=[NSString stringWithFormat:@"Special: $ %.2f",price*(1-discount/100.0)];
  212. }
  213. }
  214. NSString* get_price=@"";
  215. {
  216. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  217. // DebugLog(@"price time interval");
  218. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  219. if(gprice==nil)
  220. get_price=@"Price:No Price.";
  221. else
  222. get_price=[NSString stringWithFormat:@"Price: $ %@",gprice];
  223. }
  224. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  225. // if ([qty_null isEqualToString:@"null"]) {
  226. // qty=availability;
  227. // }
  228. //
  229. // NSString *qty_percent_null = [self textAtColumn:6 statement:stmt];
  230. // if ([qty_percent_null isEqualToString:@"null"]) {
  231. // percentage=1;
  232. // }
  233. NSString * str_availability=[NSString stringWithFormat:@"<b>Availability:</b> %d",(int)qty];
  234. NSString * str_color=[NSString stringWithFormat:@"<b>Color:</b> %@",color];
  235. NSString * str_model_set=[NSString stringWithFormat:@"<b>Set:</b> Sold in QTY of %d",model_set];
  236. NSString * str_legcolor=[NSString stringWithFormat:@"<b>Leg Color:</b> %@",legcolor];
  237. NSString * str_demension=[NSString stringWithFormat:@"<b>Dimension:</b> %@",demension];
  238. NSString * str_seat_height=[NSString stringWithFormat:@"<b>Seat Height:</b> %@",seat_height];
  239. NSString * str_meterial=[NSString stringWithFormat:@"<b>Meterial:</b> %@",material];
  240. NSString * str_box_dim=[NSString stringWithFormat:@"<b>Box Dimension:</b> %@",box_dim];
  241. NSString * str_volume=[NSString stringWithFormat:@"<b>Volume:</b> %.2f",volume];
  242. NSString * str_weight=[NSString stringWithFormat:@"<b>Weight:</b> %.2f",weight];
  243. NSString * str_load_ability=[NSString stringWithFormat:@"<b>Load Ability:</b> %.2f",load_ability];
  244. NSString * str_fabric_content=[NSString stringWithFormat:@"<b>Fabric Content:</b> %@",fabric_content];
  245. NSString * str_assembling=[NSString stringWithFormat:@"<b>Assembling:</b> %@",assembling];
  246. NSString * str_made_in=[NSString stringWithFormat:@"<b>Made In:</b> %@",made_in];
  247. NSString * str_line_note=[NSString stringWithFormat:@"<b>Line Note:</b> %@",line_note];
  248. if([params[@"availability"] boolValue]==false)
  249. str_availability=@"";
  250. else
  251. {
  252. if ([s_qty isEqualToString:@"null"] && [s_availability isEqualToString:@"0"])
  253. str_availability=@"<b>Availability:</b> In Production";
  254. }
  255. if([params[@"color"] boolValue]==false || color.length==0 )
  256. str_color=@"";
  257. if([params[@"set"] boolValue]==false || s_model_set.length==0 )
  258. str_model_set=@"";
  259. if([params[@"leg_color"] boolValue]==false || legcolor.length==0 )
  260. str_legcolor=@"";
  261. if([params[@"dimension"] boolValue]==false || demension.length==0 )
  262. str_demension=@"";
  263. if([params[@"seat_height"] boolValue]==false || seat_height.length==0 )
  264. str_seat_height=@"";
  265. if([params[@"material"] boolValue]==false || material.length==0 )
  266. str_meterial=@"";
  267. if([params[@"box_dimension"] boolValue]==false || box_dim.length==0 )
  268. str_box_dim=@"";
  269. if([params[@"volume"] boolValue]==false || s_volume.length==0 )
  270. str_volume=@"";
  271. if([params[@"weight"] boolValue]==false || s_weight.length==0 )
  272. str_weight=@"";
  273. if([params[@"load_ability"] boolValue]==false || s_load_ability.length==0 )
  274. str_load_ability=@"";
  275. if([params[@"fabric_content"] boolValue]==false || fabric_content.length==0 )
  276. str_fabric_content=@"";
  277. if([params[@"assembling"] boolValue]==false || assembling.length==0 )
  278. str_assembling=@"";
  279. if([params[@"made"] boolValue]==false || made_in.length==0 )
  280. str_made_in=@"";
  281. if([params[@"show_line_note"] boolValue]==false || line_note.length==0 )
  282. str_line_note=@"";
  283. NSMutableArray* arr_detail=[[NSMutableArray alloc]init];
  284. [arr_detail addObject:str_availability];
  285. [arr_detail addObject:str_color];
  286. [arr_detail addObject:str_model_set];
  287. [arr_detail addObject:str_legcolor];
  288. [arr_detail addObject:str_demension];
  289. [arr_detail addObject:str_seat_height];
  290. [arr_detail addObject:str_meterial];
  291. [arr_detail addObject:str_box_dim];
  292. [arr_detail addObject:str_volume];
  293. [arr_detail addObject:str_weight];
  294. [arr_detail addObject:str_load_ability];
  295. [arr_detail addObject:str_fabric_content];
  296. [arr_detail addObject:str_assembling];
  297. [arr_detail addObject:str_made_in];
  298. [arr_detail addObject:str_line_note];
  299. // NSString* detail =[NSString stringWithFormat:@"<font size=0.2>%@</font>",[RAUtils arr2string:arr_detail separator:@" " trim:true]];
  300. NSString* detail =[RAConvertor arr2string:arr_detail separator:@" " trim:true];
  301. //model image;
  302. NSString* img_path = [self portfolio_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  303. // qr image
  304. NSString* qrpath=nil;
  305. if([params[@"show_barcode"] boolValue])
  306. {
  307. NSString* temp = NSTemporaryDirectory();
  308. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  309. qrpath=[temp stringByAppendingPathComponent:filename];
  310. UIImage* qrimg=[QRCodeGenerator qrImageForString:name imageSize:240];
  311. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  312. }
  313. NSString *more_color_path = [[NSBundle mainBundle] pathForResource:@"more_color_64" ofType:@"png"];
  314. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  315. item[@"img"]=img_path;
  316. item[@"img_qr"]=qrpath;
  317. item[@"special_price"]=set_price;
  318. item[@"price"]=get_price;
  319. item[@"name"]=name;
  320. item[@"description"]=description;
  321. item[@"detail"]=detail;
  322. item[@"img"]=img_path;
  323. if (show_group_by && more_color_int != 0) {
  324. item[@"more_color"] = more_color_path;
  325. }
  326. // @{
  327. // //@"linenotes": line_note,
  328. //// @"product_id": product_id_string,
  329. //// @"available_qty": @(qty),
  330. //// @"available_percent" : @(percent),
  331. //// @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  332. //// @"item_id": [NSString stringWithFormat:@"%d",item_id],
  333. //// @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  334. // @"": ,
  335. // @"": ,
  336. // @"": ,
  337. // @"": ,
  338. // @"": ,
  339. // @"": ,
  340. // @"":
  341. // }.mutableCopy;
  342. // if (percentage) {
  343. // [item removeObjectForKey:@"available_qty"];
  344. // } else {
  345. // [item removeObjectForKey:@"available_percent"];
  346. // }
  347. //
  348. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  349. // if ([qty_null isEqualToString:@"null"]) {
  350. // [item removeObjectForKey:@"available_qty"];
  351. // }
  352. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  353. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  354. }];
  355. [iSalesDB close_db:db];
  356. // [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  357. // [dic setValue:@"" forKey:@"email_content"];
  358. // [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  359. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  360. //
  361. // NSMutableDictionary* cell = [[NSMutableDictionary alloc]init];
  362. // cell[@"count"]=[NSNumber numberWithInt:10];
  363. grid[@"cell0"]=dic;
  364. data[@"grid0"]=grid;
  365. return data;
  366. }
  367. +(NSMutableDictionary*) get_pdftemplate:(NSString*) template
  368. {
  369. NSString *default_path = [[NSBundle mainBundle] pathForResource:template ofType:nil];
  370. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  371. NSString *cache_folder=[paths objectAtIndex:0];
  372. NSString *template_path = [cache_folder stringByAppendingPathComponent:[NSString stringWithFormat:@"offline_data/%@",template]];
  373. BOOL bdir=NO;
  374. NSFileManager* fileManager = [NSFileManager defaultManager];
  375. if(PDF_DEBUG)
  376. {
  377. NSData *data = [NSData dataWithContentsOfFile:default_path];
  378. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  379. return ret;
  380. }
  381. if(! [fileManager fileExistsAtPath:template_path isDirectory:&bdir] )
  382. {
  383. NSError * error=nil;
  384. if(![fileManager copyItemAtPath:default_path toPath:template_path error:&error])
  385. {
  386. return nil;
  387. }
  388. }
  389. NSData *data = [NSData dataWithContentsOfFile:template_path];
  390. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  391. return ret;
  392. }
  393. +(NSMutableDictionary*) query_login:(NSString*) user db:(sqlite3*) db
  394. {
  395. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  396. 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 ];
  397. DebugLog(@"offline_login sql:%@",sqlQuery);
  398. sqlite3_stmt * statement;
  399. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  400. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  401. {
  402. if (sqlite3_step(statement) == SQLITE_ROW)
  403. {
  404. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  405. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  406. int can_show_price = sqlite3_column_int(statement, 0);
  407. int can_see_price = sqlite3_column_int(statement, 1);
  408. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  409. if(contact_id==nil)
  410. contact_id="";
  411. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  412. int user_type = sqlite3_column_int(statement, 3);
  413. int can_cancel_order = sqlite3_column_int(statement, 4);
  414. int can_set_cart_price = sqlite3_column_int(statement, 5);
  415. int can_create_portfolio = sqlite3_column_int(statement, 6);
  416. int can_delete_order = sqlite3_column_int(statement, 7);
  417. int can_submit_order = sqlite3_column_int(statement, 8);
  418. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  419. int can_create_order = sqlite3_column_int(statement, 10);
  420. char *mode = (char*)sqlite3_column_text(statement, 11);
  421. if(mode==nil)
  422. mode="";
  423. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  424. char *username = (char*)sqlite3_column_text(statement, 12);
  425. if(username==nil)
  426. username="";
  427. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  428. int can_update_contact_info = sqlite3_column_int(statement, 13);
  429. char *first_name = (char*)sqlite3_column_text(statement, 14);
  430. if(first_name==nil)
  431. first_name="";
  432. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  433. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  434. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  435. // [header setValue:nscontact_id forKey:@"contact_id"];
  436. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  437. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  438. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  439. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  440. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  441. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  442. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  443. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  444. //
  445. // [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  446. //
  447. // [header setValue:nsusername forKey:@"username"];
  448. //
  449. // NSError* error=nil;
  450. //
  451. // NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  452. // [header setValue:statusFilter forKey:@"statusFilter"];
  453. //
  454. //
  455. //
  456. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  457. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  458. //
  459. // // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  460. // // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  461. // int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  462. // [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  463. //
  464. // [ret setObject:header forKey:@"header"];
  465. [ret setValue:nsfirst_name forKey:@"first_name"];
  466. }
  467. sqlite3_finalize(statement);
  468. }
  469. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  470. return ret;
  471. }
  472. +(NSMutableDictionary*) prepare_salesorder_data:(NSString*) order_code type:(int) type
  473. {
  474. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  475. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  476. data[@"npd_url"]=COMPANY_WEB;
  477. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  478. [formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
  479. NSString* date = [formatter stringFromDate:[NSDate date]];
  480. data[@"print_date"]=date;
  481. data[@"company_name"]=COMPANY_FULL_NAME;
  482. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  483. data[@"TOTAL_PAGE"]=0;
  484. data[@"CURRENT_PAGE"]=0;
  485. // [formatter setDateFormat:@"MM/dd/yyyy"];
  486. // date = [formatter stringFromDate:[NSDate date]];
  487. // data[@"creat_date"]=date;
  488. NSString* temp = NSTemporaryDirectory();
  489. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  490. NSString* qrpath=[temp stringByAppendingPathComponent:filename];
  491. UIImage* qrimg=[ImageUtils generateBarCode:order_code width:480 height:44];//[QRCodeGenerator qrImageForString:order_code imageSize:240];
  492. bool bwrite=[UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  493. data[@"img_barcode"]=qrpath;
  494. data[@"so_id"]=order_code;
  495. data[@"company"]=COMPANY_FULL_NAME;
  496. // 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];
  497. 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];
  498. sqlite3 *db = [iSalesDB get_db];
  499. data[@"print_by"]= [self query_login:appDelegate.user db:db][@"first_name"];
  500. sqlite3_stmt * statement;
  501. NSMutableDictionary* datasource = [[NSMutableDictionary alloc]init];
  502. NSMutableDictionary* row0 = [[NSMutableDictionary alloc]init];
  503. row0[@"count"]=[NSNumber numberWithInt:1];
  504. datasource[@"row0"]=row0;
  505. NSString* print_status=@"";
  506. NSString* order_type=@"Offline Quote";
  507. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  508. {
  509. if (sqlite3_step(statement) == SQLITE_ROW)
  510. {
  511. int erpOrderStatus = sqlite3_column_int(statement, 0);
  512. int status = sqlite3_column_int(statement, 26);
  513. if(status==2)
  514. {
  515. switch (erpOrderStatus) {
  516. case 10:
  517. // order_type=@"Quoted Order";
  518. print_status=@"Quote";
  519. break;
  520. case 11:
  521. print_status=@"Booked";
  522. break;
  523. case 12:
  524. print_status=@"Picked";
  525. break;
  526. case 13:
  527. print_status=@"Shipped";
  528. break;
  529. case 14:
  530. print_status=@"Closed";
  531. break;
  532. case 15:
  533. print_status=@"Canceled";
  534. break;
  535. case 16:
  536. print_status=@"All";
  537. break;
  538. case 18:
  539. print_status=@"Processing";
  540. break;
  541. default:
  542. break;
  543. }
  544. }
  545. else{
  546. switch (status) {
  547. case 0:
  548. {
  549. print_status=@"Quote";
  550. break;
  551. }
  552. case 1:
  553. {
  554. print_status=@"Saved";
  555. break;
  556. }
  557. case 3:
  558. {
  559. //canceled
  560. print_status=@"Canceled";
  561. break;
  562. }
  563. default:
  564. break;
  565. }
  566. }
  567. NSString* sender_name = [self textAtColumn:1 statement:statement];
  568. NSString* sender_ext = [self textAtColumn:2 statement:statement];
  569. NSString* sender_phone = [self textAtColumn:3 statement:statement];
  570. NSString* sender_fax = [self textAtColumn:4 statement:statement];
  571. NSString* sender_email = [self textAtColumn:5 statement:statement];
  572. NSString* warehouse_name = [self textAtColumn:6 statement:statement];
  573. NSString* billing_name = [self textAtColumn:7 statement:statement];
  574. NSString* billing_ext = [self textAtColumn:8 statement:statement];
  575. NSString* billing_phone = [self textAtColumn:9 statement:statement];
  576. if(billing_phone.length>0)
  577. billing_phone= [@"TEL: " stringByAppendingString:billing_phone];
  578. NSString* billing_fax = [self textAtColumn:10 statement:statement];
  579. if(billing_fax.length>0)
  580. billing_fax= [@"FAX: " stringByAppendingString:billing_fax];
  581. NSMutableArray* billto_arr0 = [[NSMutableArray alloc]init];
  582. [billto_arr0 addObject:billing_phone];
  583. [billto_arr0 addObject:billing_fax];
  584. NSString* billing_pf=[RAConvertor arr2string:billto_arr0 separator:@" " trim:true];
  585. NSString* billing_email = [self textAtColumn:11 statement:statement];
  586. if(billing_email.length>0)
  587. billing_email= [@"EMAIL: " stringByAppendingString:billing_email];
  588. NSString* billing_contact = [self textAtColumn:31 statement:statement];
  589. if(billing_contact.length>0)
  590. billing_contact= [@"ATTN: " stringByAppendingString:billing_contact];
  591. NSString* receive_name = [self textAtColumn:12 statement:statement];
  592. NSString* receive_ext = [self textAtColumn:13 statement:statement];
  593. NSString* receive_phone = [self textAtColumn:14 statement:statement];
  594. if(receive_phone.length>0)
  595. receive_phone= [@"TEL: " stringByAppendingString:receive_phone];
  596. NSString* receive_fax = [self textAtColumn:15 statement:statement];
  597. if(receive_fax.length>0)
  598. receive_fax= [@"FAX: " stringByAppendingString:receive_fax];
  599. NSMutableArray* receive_arr0 = [[NSMutableArray alloc]init];
  600. [receive_arr0 addObject:receive_phone];
  601. [receive_arr0 addObject:receive_fax];
  602. NSString* receive_email = [self textAtColumn:16 statement:statement];
  603. NSString* receive_pf=[RAConvertor arr2string:billto_arr0 separator:@" " trim:true];
  604. if(receive_email.length>0)
  605. receive_email= [@"EMAIL: " stringByAppendingString:receive_email];
  606. NSString* receive_contact = [self textAtColumn:32 statement:statement];
  607. if(receive_contact.length>0)
  608. receive_contact= [@"ATTN: " stringByAppendingString:receive_contact];
  609. NSString* poNumber = [self textAtColumn:17 statement:statement];
  610. NSString* sales_rep = [self textAtColumn:18 statement:statement];
  611. NSString* carrier = [self textAtColumn:19 statement:statement];
  612. NSString* vendor_no = [self textAtColumn:20 statement:statement];
  613. NSString* terms = [self textAtColumn:21 statement:statement];
  614. NSString* sales_terms = [self textAtColumn:22 statement:statement];
  615. NSString* port_destination = [self textAtColumn:23 statement:statement];
  616. NSString* etd = [self textAtColumn:24 statement:statement];
  617. NSString* sign_picpath = [self textAtColumn:25 statement:statement];
  618. if(sign_picpath.length==0)
  619. sign_picpath=nil;
  620. NSString* create_date = [self textAtColumn:27 statement:statement];
  621. create_date=[self changeDateTime:create_date Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy"];
  622. NSString* general_notes=[self textAtColumn:33 statement:statement];
  623. NSString *customerID = [self textAtColumn:34 statement:statement];
  624. if (customerID.length == 0) {
  625. customerID = nil;
  626. }
  627. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  628. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  629. if(general_notes.length>0)
  630. {
  631. NSMutableDictionary* row2 = [[NSMutableDictionary alloc]init];
  632. row2[@"item_0"]=@{@"g_notes":general_notes };
  633. row2[@"count"]=[NSNumber numberWithInt:1];
  634. datasource[@"row2"]=row2;
  635. }
  636. NSMutableArray* billto_arr = [[NSMutableArray alloc]init];
  637. [billto_arr addObject:billing_ext];
  638. [billto_arr addObject:billing_pf];
  639. [billto_arr addObject:billing_contact];
  640. [billto_arr addObject:billing_email];
  641. NSString* billto=[RAConvertor arr2string:billto_arr separator:@"\r\n" trim:true];
  642. NSMutableArray* receive_arr = [[NSMutableArray alloc]init];
  643. [receive_arr addObject:receive_ext];
  644. [receive_arr addObject:receive_pf];
  645. [receive_arr addObject:receive_contact];
  646. [receive_arr addObject:receive_email];
  647. NSString* receive=[RAConvertor arr2string:receive_arr separator:@"\r\n" trim:true];
  648. float shipping = sqlite3_column_double(statement, 28);
  649. float lift_gate_value = sqlite3_column_double(statement, 29);
  650. float handling_fee_value = sqlite3_column_double(statement, 30);
  651. data[@"company"]=sender_name;
  652. data[@"shipfrom_addr"]=sender_ext;
  653. data[@"shipfrom_phone"]=sender_phone;
  654. data[@"shipfrom_fax"]=sender_fax;
  655. data[@"shipfrom_email"]=sender_email;
  656. data[@"warehouse_name"]=warehouse_name;
  657. data[@"bill_to_name"]=billing_name;
  658. data[@"bill_to_ext"]=billto;
  659. data[@"ship_to_name"]=receive_name;
  660. data[@"ship_to_ext"]=receive;
  661. data[@"po_no"]=poNumber;
  662. data[@"rep"]=sales_rep;
  663. data[@"e_schdule"]=etd;
  664. data[@"sales_terms"]=sales_terms;
  665. data[@"port_destination"]=port_destination;
  666. data[@"terms"]=terms;
  667. data[@"carrier"]=carrier;
  668. data[@"vendor_no"]=vendor_no;
  669. data[@"customer_sign"]=sign_picpath;
  670. data[@"print_status"]=print_status;
  671. data[@"create_date"]=create_date;
  672. 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];;
  673. sqlite3_stmt * statement_cart;
  674. // NSDate *date1 = [NSDate date];
  675. int count=0;
  676. int cart_count=0;
  677. float credits=0;
  678. int dbresult=sqlite3_prepare_v2(db, [sql_cart UTF8String], -1, &statement_cart, nil);
  679. if ( dbresult== SQLITE_OK)
  680. {
  681. NSMutableDictionary* row1 = [[NSMutableDictionary alloc]init];
  682. int row1count=0;
  683. while (sqlite3_step(statement_cart) == SQLITE_ROW)
  684. {
  685. NSDate *row_date = [NSDate date];
  686. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  687. // int product_id = sqlite3_column_int(statement_cart, 0);
  688. char *str_price = (char*)sqlite3_column_text(statement_cart, 1);
  689. int item_id = sqlite3_column_int(statement_cart, 7);
  690. double discount = sqlite3_column_double(statement_cart, 2);
  691. NSString* Price=nil;
  692. if(str_price==nil)
  693. {
  694. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  695. // NSDate *price_date = [NSDate date];
  696. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db];
  697. // DebugLog(@"price time interval");
  698. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  699. if(price==nil)
  700. Price=@"No Price.";
  701. else
  702. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  703. }
  704. else
  705. {
  706. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  707. }
  708. float dunitprice=0;
  709. int item_count = sqlite3_column_int(statement_cart, 3);
  710. NSString* Amount=@"";
  711. if(![Price isEqualToString:@"No Price."])
  712. {
  713. dunitprice=[Price floatValue];
  714. dunitprice=dunitprice* (1-discount/100.0);
  715. Price=[NSString stringWithFormat:@"%.2f",dunitprice];
  716. credits+=dunitprice*item_count;
  717. Amount=[NSString stringWithFormat:@"%.2f",dunitprice*item_count];
  718. }
  719. else
  720. {
  721. Price=@"";
  722. }
  723. // char *line_note = (char*)sqlite3_column_text(statement_cart, 4);
  724. // NSString *nsline_note=nil;
  725. // if(line_note!=nil)
  726. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  727. char *name = (char*)sqlite3_column_text(statement_cart, 5);
  728. NSString *nsname=nil;
  729. if(name!=nil)
  730. nsname= [[NSString alloc]initWithUTF8String:name];
  731. char *description = (char*)sqlite3_column_text(statement_cart, 6);
  732. NSString *nsdescription=nil;
  733. if(description!=nil)
  734. nsdescription= [[NSString alloc]initWithUTF8String:description];
  735. // int stockUom = sqlite3_column_int(statement_cart, 8);
  736. // int _id = sqlite3_column_int(statement_cart, 9);
  737. // NSDate *subtotal_date = [NSDate date];
  738. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  739. // DebugLog(@"subtotal_date time interval");
  740. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  741. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  742. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  743. // int carton=[bsubtotaljson[@"carton"] intValue];
  744. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  745. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  746. // NSDate *img_date = [NSDate date];
  747. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  748. // DebugLog(@"img_date time interval");
  749. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  750. itemjson[@"model"]=nsname;
  751. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  752. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  753. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  754. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  755. // itemjson[@"check"]=@"true";
  756. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  757. itemjson[@"count"]=[NSString stringWithFormat:@"%d",item_count];
  758. itemjson[@"unit_price"]=Price;
  759. itemjson[@"amount"]=Amount;
  760. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  761. itemjson[@"description"]=[NSString stringWithFormat:@"%@",nsdescription ];
  762. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  763. // itemjson[@"note"]=nsline_note;
  764. // NSDate *date2 = [NSDate date];
  765. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  766. // DebugLog(@"model_bundle time interval");
  767. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  768. itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  769. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=itemjson;
  770. row1count++;
  771. NSDictionary* combine =[self model_bundle:item_id db:db compute_part:false contactID:customerID];
  772. for(int c=0;c<[combine[@"count"] intValue];c++)
  773. {
  774. NSMutableDictionary* combine_item=[combine[[NSString stringWithFormat:@"item_%d",c]] mutableCopy];
  775. combine_item[@"count"]=[NSString stringWithFormat:@"%d",[combine_item[@"modulus"] intValue]*item_count];
  776. // combine_item[@"count"]=[NSNumber numberWithInt:[combine_item[@"modulus"] intValue]*item_count];
  777. double combine_amount=[combine_item[@"modulus"] intValue]*item_count* [combine_item[@"unit_price"] floatValue];
  778. credits+=combine_amount;
  779. combine_item[@"amount"]=[NSString stringWithFormat:@"%.2f",combine_amount];
  780. combine_item[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,c+2];
  781. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=combine_item;
  782. row1count++;
  783. }
  784. count++;
  785. // DebugLog(@"row time interval");
  786. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  787. }
  788. row1[@"count"]=[NSNumber numberWithInt:row1count];
  789. datasource[@"row1"]=row1;
  790. data[@"table_model"]=datasource;
  791. sqlite3_finalize(statement_cart);
  792. }
  793. NSMutableDictionary* fee = [[NSMutableDictionary alloc]init];
  794. NSMutableDictionary* fee_row0 = [[NSMutableDictionary alloc]init];
  795. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  796. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  797. // if(credits>0)
  798. fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  799. // else
  800. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": @""};
  801. if(shipping>0)
  802. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": [NSString stringWithFormat:@"%.2f",shipping]};
  803. else
  804. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": @""};
  805. if(lift_gate_value>0)
  806. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",lift_gate_value]};
  807. else
  808. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": @""};
  809. if(handling_fee_value>0)
  810. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",handling_fee_value]};
  811. else
  812. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": @""};
  813. float total = credits+shipping+lift_gate_value+handling_fee_value;
  814. if(total>0)
  815. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": [NSString stringWithFormat:@"%.2f",total]};
  816. else
  817. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": @""};
  818. fee_row0[@"count"]=[NSNumber numberWithInt:5];
  819. fee[@"row0"]=fee_row0;
  820. data[@"table_fee"]=fee;
  821. // NSString *sign_url = [self textAtColumn:51 statement:statement];
  822. // ret[@"sign_url"] = sign_url;
  823. //
  824. // customer_contact = [self textAtColumn:52 statement:statement];
  825. // customer_email = [self textAtColumn:53 statement:statement];
  826. // customer_phone = [self textAtColumn:54 statement:statement];
  827. // customer_fax = [self textAtColumn:55 statement:statement];
  828. //
  829. //
  830. // int offline_edit=sqlite3_column_int(statement, 56);
  831. //
  832. // ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  833. //
  834. // char *soid = (char*)sqlite3_column_text(statement, 1);
  835. // if(soid==nil)
  836. // soid= "";
  837. // nssoid= [[NSString alloc]initWithUTF8String:soid];
  838. // // so#
  839. // ret[@"so#"] = nssoid;
  840. //
  841. // char *poNumber = (char*)sqlite3_column_text(statement, 2);
  842. // if(poNumber==nil)
  843. // poNumber= "";
  844. // NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  845. //
  846. //
  847. //
  848. // char *create_time = (char*)sqlite3_column_text(statement, 3);
  849. // if(create_time==nil)
  850. // create_time= "";
  851. // NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  852. // nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  853. //
  854. // int status = sqlite3_column_int(statement, 4);
  855. // int erpStatus = sqlite3_column_int(statement, 49);
  856. // NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  857. //
  858. // // status
  859. // if (status > 1 && status != 3) {
  860. // status = erpStatus;
  861. // } else if (status == 3) {
  862. // status = 15;
  863. // }
  864. // ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  865. // ret[@"order_status"] = nsstatus;
  866. //
  867. //
  868. // char *company_name = (char*)sqlite3_column_text(statement, 5);
  869. // if(company_name==nil)
  870. // company_name= "";
  871. // NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  872. // // company name
  873. // ret[@"company_name"] = nscompany_name;
  874. //
  875. // char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  876. // if(customer_contact==nil)
  877. // customer_contact= "";
  878. // NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  879. //
  880. //
  881. // char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  882. // if(addr_1==nil)
  883. // addr_1="";
  884. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  885. //
  886. // char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  887. // if(addr_2==nil)
  888. // addr_2="";
  889. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  890. //
  891. //
  892. // char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  893. // if(addr_3==nil)
  894. // addr_3="";
  895. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  896. //
  897. //
  898. // char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  899. // if(addr_4==nil)
  900. // addr_4="";
  901. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  902. //
  903. // NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  904. // [arr_addr addObject:nsaddr_1];
  905. // [arr_addr addObject:nsaddr_2];
  906. // [arr_addr addObject:nsaddr_3];
  907. // [arr_addr addObject:nsaddr_4];
  908. // NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  909. //
  910. //
  911. // char *logist = (char*)sqlite3_column_text(statement, 11);
  912. // if(logist==nil)
  913. // logist= "";
  914. // NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  915. // if (status == -11 || status == 10 || status == 11) {
  916. // nslogist = [self textAtColumn:59 statement:statement];
  917. // };
  918. //
  919. // NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  920. // if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  921. // shipping = @"Shipping To Be Quoted";
  922. // } else {
  923. // shippingFee = sqlite3_column_double(statement, 12);
  924. // }
  925. //
  926. // // Shipping
  927. // ret[@"Shipping"] = shipping;
  928. //
  929. // int have_lift_gate = sqlite3_column_int(statement, 17);
  930. // lift_gate = sqlite3_column_double(statement, 13);
  931. // // Liftgate Fee(No loading dock)
  932. // if (!have_lift_gate) {
  933. // lift_gate = 0;
  934. // }
  935. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  936. // if (sqlite3_column_int(statement, 57)) {
  937. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  938. // }
  939. //
  940. // char *general_notes = (char*)sqlite3_column_text(statement, 14);
  941. // if(general_notes==nil)
  942. // general_notes= "";
  943. // NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  944. //
  945. // char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  946. // if(internal_notes==nil)
  947. // internal_notes= "";
  948. // NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  949. //
  950. // char *payment_type = (char*)sqlite3_column_text(statement, 16);
  951. // if(payment_type==nil)
  952. // payment_type= "";
  953. // NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  954. //
  955. //
  956. //
  957. // // order info
  958. // orderinfo = [self textFileName:@"order_info.html"];
  959. //
  960. //
  961. // orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  962. // orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  963. // orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  964. // orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  965. //
  966. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  967. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  968. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  969. // orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  970. // orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  971. // orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  972. // orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  973. // orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  974. //
  975. // NSString *payment = nil;
  976. // if([nspayment_type isEqualToString:@"Credit Card"])
  977. // {
  978. // payment = [self textFileName:@"creditcardpayment.html"];
  979. //
  980. // NSString *card_first_name = [self textAtColumn:37 statement:statement];
  981. // NSString *card_last_name = [self textAtColumn:38 statement:statement];
  982. // NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  983. // NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  984. // NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  985. // NSString *card_type = [self textAtColumn:42 statement:statement];
  986. // if (card_type.length > 0) { // 显示星号
  987. // card_type = @"****";
  988. // }
  989. // NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  990. // if (card_number.length > 0 && card_number.length > 4) {
  991. // for (int i = 0; i < card_number.length - 4; i++) {
  992. // card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  993. // }
  994. // } else {
  995. // card_number = @"";
  996. // }
  997. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  998. // NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  999. // NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  1000. // NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  1001. // if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  1002. // card_expiration = @"****";
  1003. // }
  1004. //
  1005. //
  1006. // NSString *card_city = [self textAtColumn:46 statement:statement];
  1007. // NSString *card_state = [self textAtColumn:47 statement:statement];
  1008. //
  1009. // payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  1010. // payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  1011. // payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  1012. // payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  1013. // payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  1014. // payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  1015. // payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  1016. // payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  1017. // payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  1018. // payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  1019. //
  1020. //
  1021. // }
  1022. // else
  1023. // {
  1024. // payment=[self textFileName:@"normalpayment.html"];
  1025. // payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  1026. // }
  1027. // orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  1028. //
  1029. // ret[@"result"]= [NSNumber numberWithInt:2];
  1030. //
  1031. // // more info
  1032. // moreInfo = [self textFileName:@"more_info.html"];
  1033. //
  1034. //
  1035. // // ShipToCompany_or_&nbsp
  1036. // NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  1037. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  1038. // NSString *shipToName = [self textAtColumn:19 statement:statement];
  1039. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  1040. // NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  1041. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  1042. //
  1043. //
  1044. // // ShipFromCompany_or_&nbsp
  1045. // NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  1046. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  1047. // NSString *shipFromName = [self textAtColumn:22 statement:statement];
  1048. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  1049. // NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  1050. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  1051. //
  1052. //
  1053. // // FreightBillToCompany_or_&nbsp
  1054. // NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  1055. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  1056. // NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  1057. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  1058. // NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  1059. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  1060. //
  1061. //
  1062. // // MerchandiseBillToCompany_or_&nbsp
  1063. // NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  1064. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  1065. // NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  1066. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  1067. // NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  1068. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  1069. //
  1070. //
  1071. // // ReturnToCompany_or_&nbsp
  1072. // NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  1073. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  1074. // NSString *returnToName = [self textAtColumn:31 statement:statement];
  1075. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  1076. // NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  1077. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  1078. // //
  1079. //
  1080. // // DebugLog(@"more info : %@",moreInfo);
  1081. //
  1082. // // handling fee
  1083. // handlingFee = sqlite3_column_double(statement, 33);
  1084. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  1085. // if (sqlite3_column_int(statement, 58)) {
  1086. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  1087. // }
  1088. // //
  1089. //
  1090. // // customer info
  1091. // customerID = [self textAtColumn:36 statement:statement];
  1092. //
  1093. // // mode
  1094. // ret[@"mode"] = appDelegate.mode;
  1095. //
  1096. // // model_count
  1097. // ret[@"model_count"] = @(0);
  1098. }
  1099. sqlite3_finalize(statement);
  1100. }
  1101. [iSalesDB close_db:db];
  1102. data[@"order_type"]=order_type;
  1103. /*
  1104. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  1105. // "customer_email" = "Shui Hu";
  1106. // "customer_fax" = "";
  1107. // "customer_first_name" = F;
  1108. // "customer_last_name" = L;
  1109. // "customer_name" = ",da He Xiang Dong Liu A";
  1110. // "customer_phone" = "Hey Xuan Feng";
  1111. contactInfo[@"customer_phone"] = customer_phone;
  1112. contactInfo[@"customer_fax"] = customer_fax;
  1113. contactInfo[@"customer_email"] = customer_email;
  1114. NSString *first_name = @"";
  1115. NSString *last_name = @"";
  1116. if ([customer_contact isEqualToString:@""]) {
  1117. } else if ([customer_contact containsString:@" "]) {
  1118. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  1119. first_name = [customer_contact substringToIndex:first_space_index];
  1120. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  1121. }
  1122. contactInfo[@"customer_first_name"] = first_name;
  1123. contactInfo[@"customer_last_name"] = last_name;
  1124. ret[@"customerInfo"] = contactInfo;
  1125. // models
  1126. if (nssoid) {
  1127. __block double TotalCuft = 0;
  1128. __block double TotalWeight = 0;
  1129. __block int TotalCarton = 0;
  1130. __block double allItemPrice = 0;
  1131. 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];
  1132. sqlite3 *db1 = [iSalesDB get_db];
  1133. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1134. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1135. int product_id = sqlite3_column_int(stmt, 0);
  1136. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  1137. int item_id = sqlite3_column_int(stmt, 7);
  1138. NSString* Price=nil;
  1139. if(str_price==nil)
  1140. {
  1141. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  1142. if(price==nil)
  1143. Price=@"No Price.";
  1144. else
  1145. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1146. }
  1147. else
  1148. {
  1149. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1150. }
  1151. double discount = sqlite3_column_double(stmt, 2);
  1152. int item_count = sqlite3_column_int(stmt, 3);
  1153. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  1154. NSString *nsline_note=nil;
  1155. if(line_note!=nil)
  1156. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1157. // char *name = (char*)sqlite3_column_text(stmt, 5);
  1158. // NSString *nsname = nil;
  1159. // if(name!=nil)
  1160. // nsname= [[NSString alloc]initWithUTF8String:name];
  1161. NSString *nsname = [self textAtColumn:5 statement:stmt];
  1162. // char *description = (char*)sqlite3_column_text(stmt, 6);
  1163. // NSString *nsdescription=nil;
  1164. // if(description!=nil)
  1165. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  1166. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  1167. // int stockUom = sqlite3_column_int(stmt, 8);
  1168. // int _id = sqlite3_column_int(stmt, 9);
  1169. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  1170. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1171. double weight=[bsubtotaljson[@"weight"] doubleValue];
  1172. int carton=[bsubtotaljson[@"carton"] intValue];
  1173. TotalCuft += cuft;
  1174. TotalWeight += weight;
  1175. TotalCarton += carton;
  1176. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  1177. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  1178. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  1179. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1180. itemjson[@"note"]=nsline_note;
  1181. itemjson[@"origin_price"] = Price;
  1182. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1183. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1184. itemjson[@"order_item_status"] = @""; // 暂时不处理
  1185. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  1186. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  1187. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  1188. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  1189. if(itemjson[@"combine"] != nil)
  1190. {
  1191. // int citem=0;
  1192. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  1193. for(int bc=0;bc<bcount;bc++)
  1194. {
  1195. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  1196. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  1197. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  1198. subTotal += uprice * modulus * item_count;
  1199. }
  1200. }
  1201. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  1202. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  1203. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  1204. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  1205. allItemPrice += subTotal;
  1206. }];
  1207. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  1208. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  1209. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  1210. // payments/Credits
  1211. // payments_and_credist = sqlite3_column_double(statement, 34);
  1212. payments_and_credist = allItemPrice;
  1213. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1214. // // total
  1215. // totalPrice = sqlite3_column_double(statement, 35);
  1216. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1217. } else {
  1218. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  1219. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  1220. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  1221. // payments/Credits
  1222. payments_and_credist = 0;
  1223. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1224. }
  1225. // total
  1226. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  1227. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1228. ret[@"order_info"]= orderinfo;
  1229. ret[@"more_order_info"] = moreInfo;
  1230. return [RAConvertor dict2data:ret];
  1231. */
  1232. DebugLog(@"debug sales order data:%@", [RAConvertor dict2string:data]);
  1233. return data;
  1234. }
  1235. +(NSData*) offline_request_salesorder :(NSMutableDictionary *) params
  1236. {
  1237. NSString* template_file=nil;
  1238. switch([params[@"thumb"] intValue])
  1239. {
  1240. case 0:
  1241. template_file= @"so.json";
  1242. break;
  1243. case 1:
  1244. template_file= @"so_thumb.json";
  1245. break;
  1246. default:
  1247. template_file= @"so.json";
  1248. }
  1249. NSMutableDictionary* data = [self prepare_salesorder_data:params[@"order_code"] type:[params[@"thumb"] intValue]];
  1250. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1251. if(data[@"table_model"][@"row2"]==nil)
  1252. template[@"pages"][@"page_0"][@"contents"][@"item_0"][@"rows"][@"count"]=[NSNumber numberWithInt:1];
  1253. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1254. DebugLog(@"%@",file);
  1255. // return nil;
  1256. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1257. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1258. if (file) {
  1259. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1260. [dic setObject:file forKey:@"pdf_path"];
  1261. dic[@"isLocalFile"]=@"true";
  1262. return [RAConvertor dict2data:dic];
  1263. }
  1264. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1265. dic[@"isLocalFile"]=@"true";
  1266. return [RAConvertor dict2data:dic];
  1267. }
  1268. +(NSData*) offline_request_tearsheet :(NSMutableDictionary *) params
  1269. {
  1270. NSString* template_file=nil;
  1271. switch([params[@"pdf_style"] intValue])
  1272. {
  1273. case 0:
  1274. template_file= @"portfolio_2x3.json";
  1275. break;
  1276. case 1:
  1277. template_file= @"portfolio_3x2.json";
  1278. break;
  1279. default:
  1280. template_file= @"portfolio_3x2.json";
  1281. }
  1282. NSMutableDictionary* data = [self prepare_portfolio_data:params];
  1283. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1284. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1285. DebugLog(@"%@",file);
  1286. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1287. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1288. if (file) {
  1289. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1290. [dic setObject:file forKey:@"pdf_path"];
  1291. dic[@"isLocalFile"]=@"true";
  1292. return [RAConvertor dict2data:dic];
  1293. }
  1294. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1295. dic[@"isLocalFile"]=@"true";
  1296. return [RAConvertor dict2data:dic];
  1297. }
  1298. +(NSString*) get_offline_soid:(sqlite3*)db
  1299. {
  1300. NSString* soid=nil;
  1301. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  1302. [formatter setDateFormat:@"yyMMdd"];
  1303. NSString* date = [formatter stringFromDate:[NSDate date]];
  1304. for(int i=1;i<999;i++)
  1305. {
  1306. soid=[NSString stringWithFormat:@"OFF%@%4d",date,i];
  1307. soid= [soid stringByReplacingOccurrencesOfString:@" " withString:@"0"];
  1308. int count=[iSalesDB get_recordcount:db table:@"offline_order" where:[NSString stringWithFormat:@"so_id='%@'",soid]];
  1309. if(count==0)
  1310. return soid;
  1311. }
  1312. return nil;
  1313. }
  1314. +(NSArray*) enumOfflineOrder
  1315. {
  1316. NSMutableArray* ret = [[NSMutableArray alloc] init];
  1317. sqlite3 *db = [iSalesDB get_db];
  1318. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  1319. sqlite3_stmt * statement;
  1320. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1321. if ( dbresult== SQLITE_OK)
  1322. {
  1323. while (sqlite3_step(statement) == SQLITE_ROW)
  1324. {
  1325. char *so_id = (char*)sqlite3_column_text(statement, 0);
  1326. NSString* nsso_id=nil;
  1327. if(so_id!=nil)
  1328. {
  1329. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  1330. }
  1331. [ret addObject:nsso_id];
  1332. }
  1333. sqlite3_finalize(statement);
  1334. }
  1335. [iSalesDB close_db:db];
  1336. return ret;
  1337. }
  1338. //+(void) uploadFile:(NSString*) file
  1339. //{
  1340. // NSData* data = [NSData dataWithContentsOfFile: file];
  1341. // UIApplication * app = [UIApplication sharedApplication];
  1342. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1343. //
  1344. //
  1345. // AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  1346. //
  1347. //
  1348. //
  1349. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  1350. //
  1351. // if(appDelegate.user!=nil)
  1352. // [params setValue:appDelegate.user forKey:@"user"];
  1353. // // if(appDelegate.contact_id!=nil)
  1354. // // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  1355. // if(appDelegate.password!=nil)
  1356. // [params setValue:appDelegate.password forKey:@"password"];
  1357. //
  1358. //
  1359. // NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_OFFLINE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  1360. // [formData appendPartWithFileData:data name:@"upfile" fileName:[file lastPathComponent] mimeType:@"application/zip"];
  1361. // } error:nil];
  1362. //
  1363. //
  1364. //
  1365. // AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  1366. //
  1367. // NSProgress *progress = nil;
  1368. //
  1369. // NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  1370. //
  1371. //
  1372. //
  1373. // if (error) {
  1374. //
  1375. // NSString* err_msg = [error localizedDescription];
  1376. // DebugLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  1377. //
  1378. //
  1379. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1380. //
  1381. // DebugLog(@"data string: %@",str);
  1382. // [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  1383. //
  1384. // } else {
  1385. // DebugLog(@"response ");
  1386. //
  1387. //
  1388. //
  1389. // NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  1390. //
  1391. // // 再将NSData转为字符串
  1392. // NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  1393. //
  1394. // // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1395. //
  1396. // DebugLog(@"data string: %@",jsonStr);
  1397. //
  1398. // NSDictionary* json = responseObject;
  1399. //
  1400. //
  1401. // if([[json valueForKey:@"result"] intValue]==2)
  1402. // {
  1403. //// NSString* img_url_down = json[@"img_url_aname"];
  1404. //// NSString* img_url_up = json[@"img_url"];
  1405. //
  1406. //
  1407. // }
  1408. // else
  1409. // {
  1410. // [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload file" controller:nil] ;
  1411. // }
  1412. //
  1413. //
  1414. // }
  1415. // // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  1416. // }];
  1417. //
  1418. //
  1419. //
  1420. //
  1421. // // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  1422. // //
  1423. //
  1424. //
  1425. //
  1426. //
  1427. //
  1428. //
  1429. // [uploadTask resume];
  1430. //
  1431. //}
  1432. +(NSDictionary *) prepareUpload:(NSArray*) arr_order
  1433. {
  1434. NSString* serial= [[NSUUID UUID] UUIDString];
  1435. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1436. NSString *cachefolder = [paths objectAtIndex:0];
  1437. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1438. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1439. NSFileManager* fileManager = [NSFileManager defaultManager];
  1440. BOOL bdir=YES;
  1441. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  1442. {
  1443. NSError *error = nil;
  1444. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  1445. if(!bsuccess)
  1446. DebugLog(@"Create UPLOAD folder failed");
  1447. }
  1448. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1449. ret[@"contact"]=[self prepareContact:serial];
  1450. ret[@"wishlist"]=[self prepareWishlist:serial];
  1451. ret[@"order"]=[self prepareOrder:serial soid:arr_order];
  1452. ret[@"portfolio"] = [self preparePortfolio:serial];
  1453. ret[@"view_portfolio"] = [self preparePDF:serial];
  1454. NSString* str= [RAConvertor dict2string:ret];
  1455. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  1456. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  1457. NSError *error=nil;
  1458. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  1459. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  1460. ZipArchive* zip = [[ZipArchive alloc] init];
  1461. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  1462. for(int i=0;i<arr_files.count;i++)
  1463. {
  1464. NSString* file=arr_files[i];
  1465. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  1466. }
  1467. if( ![zip CloseZipFile2] )
  1468. {
  1469. zippath = @"";
  1470. }
  1471. ret[@"file"]=zippath;
  1472. return ret;
  1473. }
  1474. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  1475. {
  1476. if(filename.length==0)
  1477. return false;
  1478. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1479. bool ret=false;
  1480. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1481. NSString *cachefolder = [paths objectAtIndex:0];
  1482. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  1483. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  1484. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1485. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1486. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  1487. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  1488. NSFileManager* fileManager = [NSFileManager defaultManager];
  1489. BOOL bdir=NO;
  1490. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  1491. {
  1492. NSError *error = nil;
  1493. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  1494. {
  1495. ret=false;
  1496. }
  1497. else
  1498. {
  1499. ret=true;
  1500. }
  1501. }
  1502. return ret;
  1503. }
  1504. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  1505. {
  1506. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1507. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  1508. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  1509. NSString* where=@" 1=1";
  1510. if (ver!=nil) {
  1511. where=@"is_dirty=1";
  1512. }
  1513. sqlite3 *db = [iSalesDB get_db];
  1514. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  1515. sqlite3_stmt * statement;
  1516. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1517. int count=0;
  1518. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1519. if ( dbresult== SQLITE_OK)
  1520. {
  1521. while (sqlite3_step(statement) == SQLITE_ROW)
  1522. {
  1523. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1524. int _id = sqlite3_column_int(statement, 0);
  1525. int product_id = sqlite3_column_int(statement, 1);
  1526. int item_id = sqlite3_column_int(statement, 2);
  1527. int qty = sqlite3_column_int(statement, 3);
  1528. char *create_time = (char*)sqlite3_column_text(statement, 4);
  1529. NSString* nscreate_time=nil;
  1530. if(create_time!=nil)
  1531. {
  1532. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  1533. }
  1534. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  1535. NSString* nsmodify_time=nil;
  1536. if(modify_time!=nil)
  1537. {
  1538. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  1539. }
  1540. int is_delete = sqlite3_column_int(statement, 6);
  1541. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1542. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  1543. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1544. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  1545. itemjson[@"create_time"]=nscreate_time;
  1546. itemjson[@"modify_time"]=nsmodify_time;
  1547. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  1548. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1549. count++;
  1550. }
  1551. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1552. sqlite3_finalize(statement);
  1553. }
  1554. ret[@"count"]=[NSNumber numberWithInt:count ];
  1555. [iSalesDB close_db:db];
  1556. return ret;
  1557. }
  1558. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode db:(sqlite3*)db
  1559. {
  1560. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1561. UIApplication * app = [UIApplication sharedApplication];
  1562. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1563. 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 ];
  1564. // 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 ];
  1565. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  1566. DebugLog(@"offline_login sql:%@",sqlQuery);
  1567. sqlite3_stmt * statement;
  1568. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1569. int count=0;
  1570. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1571. if ( dbresult== SQLITE_OK)
  1572. {
  1573. while (sqlite3_step(statement) == SQLITE_ROW)
  1574. {
  1575. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1576. char *customer_cid = (char *)sqlite3_column_text(statement, 10);
  1577. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  1578. int product_id = sqlite3_column_int(statement, 0);
  1579. char *str_price = (char*)sqlite3_column_text(statement, 1);
  1580. int item_id = sqlite3_column_int(statement, 7);
  1581. NSString* Price=nil;
  1582. if(str_price==nil)
  1583. {
  1584. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1585. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db];
  1586. if(price==nil)
  1587. Price=@"No Price.";
  1588. else
  1589. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1590. }
  1591. else
  1592. {
  1593. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1594. }
  1595. double discount = sqlite3_column_double(statement, 2);
  1596. int item_count = sqlite3_column_int(statement, 3);
  1597. char *line_note = (char*)sqlite3_column_text(statement, 4);
  1598. NSString *nsline_note=nil;
  1599. if(line_note!=nil)
  1600. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1601. char *name = (char*)sqlite3_column_text(statement, 5);
  1602. NSString *nsname=nil;
  1603. if(name!=nil)
  1604. nsname= [[NSString alloc]initWithUTF8String:name];
  1605. char *description = (char*)sqlite3_column_text(statement, 6);
  1606. NSString *nsdescription=nil;
  1607. if(description!=nil)
  1608. nsdescription= [[NSString alloc]initWithUTF8String:description];
  1609. // int stockUom = sqlite3_column_int(statement, 8);
  1610. // int _id = sqlite3_column_int(statement, 9);
  1611. //
  1612. //
  1613. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  1614. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1615. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  1616. // int carton=[bsubtotaljson[@"carton"] intValue];
  1617. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1618. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  1619. itemjson[@"model"]=nsname;
  1620. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  1621. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  1622. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  1623. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  1624. // itemjson[@"check"]=@"true";
  1625. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1626. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  1627. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  1628. itemjson[@"unit_price"]=Price;
  1629. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  1630. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1631. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  1632. itemjson[@"note"]=nsline_note;
  1633. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:customerID];
  1634. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1635. count++;
  1636. }
  1637. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1638. sqlite3_finalize(statement);
  1639. }
  1640. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1641. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  1642. return ret;
  1643. }
  1644. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid
  1645. {
  1646. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1647. sqlite3 *db = [iSalesDB get_db];
  1648. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  1649. // for(int i=0;i<arr_soid.count;i++)
  1650. sqlite3_stmt * statement;
  1651. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1652. //int count=0;
  1653. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1654. if ( dbresult== SQLITE_OK)
  1655. {
  1656. int count=0;
  1657. while (sqlite3_step(statement) == SQLITE_ROW)
  1658. {
  1659. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1660. int _id = sqlite3_column_int(statement, 0);
  1661. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  1662. NSString* nssync_data=nil;
  1663. if(sync_data!=nil)
  1664. {
  1665. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1666. }
  1667. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1668. NSString* nsimg_1=nil;
  1669. if(img_1!=nil)
  1670. {
  1671. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1672. [self copy_upImg:serial file:nsimg_1];
  1673. }
  1674. char *so_no = (char*)sqlite3_column_text(statement, 3);
  1675. NSString* nsso_no=nil;
  1676. if(so_no!=nil)
  1677. {
  1678. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  1679. }
  1680. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  1681. itemjson[@"order_type"]=@"submit order";
  1682. else
  1683. itemjson[@"order_type"]=@"archive order";
  1684. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1685. itemjson[@"json_data"]= nssync_data;
  1686. itemjson[@"order_items"]=[self preparecart:nsso_no db:db];
  1687. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1688. count++;
  1689. }
  1690. ret[@"count"]=[NSNumber numberWithInt:count ];
  1691. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1692. }
  1693. sqlite3_finalize(statement);
  1694. [iSalesDB close_db:db];
  1695. return ret;
  1696. }
  1697. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  1698. {
  1699. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1700. sqlite3 *db = [iSalesDB get_db];
  1701. // UIApplication * app = [UIApplication sharedApplication];
  1702. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1703. 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";
  1704. sqlite3_stmt * statement;
  1705. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1706. int count=0;
  1707. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1708. if ( dbresult== SQLITE_OK)
  1709. {
  1710. while (sqlite3_step(statement) == SQLITE_ROW)
  1711. {
  1712. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1713. int _id = sqlite3_column_int(statement, 0);
  1714. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  1715. NSString* nsimg_0=nil;
  1716. if(img_0!=nil)
  1717. {
  1718. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  1719. [self copy_upImg:serial file:nsimg_0];
  1720. }
  1721. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1722. NSString* nsimg_1=nil;
  1723. if(img_1!=nil)
  1724. {
  1725. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1726. [self copy_upImg:serial file:nsimg_1];
  1727. }
  1728. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  1729. NSString* nsimg_2=nil;
  1730. if(img_2!=nil)
  1731. {
  1732. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  1733. [self copy_upImg:serial file:nsimg_2];
  1734. }
  1735. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  1736. NSString* nssync_data=nil;
  1737. if(sync_data!=nil)
  1738. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1739. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1740. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1741. itemjson[@"json_data"]= nssync_data;
  1742. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1743. count++;
  1744. }
  1745. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1746. sqlite3_finalize(statement);
  1747. }
  1748. ret[@"count"]=[NSNumber numberWithInt:count ];
  1749. [iSalesDB close_db:db];
  1750. return ret;
  1751. }
  1752. +(bool) check_offlinedata
  1753. {
  1754. UIApplication * app = [UIApplication sharedApplication];
  1755. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1756. sqlite3 *db = [iSalesDB get_db];
  1757. NSString * where=@"1=1";
  1758. // if(appDelegate.user!=nil)
  1759. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  1760. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  1761. [iSalesDB close_db:db];
  1762. if(count==0)
  1763. {
  1764. return false;
  1765. }
  1766. return true;
  1767. //
  1768. //[iSalesDB close_db:db];
  1769. }
  1770. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  1771. {
  1772. UIApplication * app = [UIApplication sharedApplication];
  1773. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1774. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1775. sqlite3 *db = [iSalesDB get_db];
  1776. NSString* collectId=params[@"collectId"];
  1777. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  1778. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  1779. [iSalesDB execSql:sqlQuery db:db];
  1780. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  1781. [iSalesDB close_db:db];
  1782. appDelegate.wish_count =count;
  1783. [appDelegate update_count_mark];
  1784. ret[@"result"]= [NSNumber numberWithInt:2];
  1785. return ret;
  1786. }
  1787. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  1788. //{
  1789. //
  1790. // UIApplication * app = [UIApplication sharedApplication];
  1791. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1792. //
  1793. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1794. // sqlite3 *db = [iSalesDB get_db];
  1795. // NSString* product_id=params[@"product_id"];
  1796. //
  1797. //
  1798. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  1799. //
  1800. //
  1801. // for(int i=0;i<arr.count;i++)
  1802. // {
  1803. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  1804. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  1805. // if(count==0)
  1806. // {
  1807. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  1808. // [iSalesDB execSql:sqlQuery db:db];
  1809. // }
  1810. // }
  1811. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  1812. // [iSalesDB close_db:db];
  1813. //
  1814. // appDelegate.wish_count =count;
  1815. //
  1816. // [appDelegate update_count_mark];
  1817. // ret[@"result"]= [NSNumber numberWithInt:2];
  1818. // return ret;
  1819. // //
  1820. // //return ret;
  1821. //}
  1822. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  1823. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  1824. DebugLog(@"time interval: %lf",interval);
  1825. }
  1826. +(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  1827. {
  1828. UIApplication * app = [UIApplication sharedApplication];
  1829. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1830. int sort = [[params objectForKey:@"sort"] intValue];
  1831. NSString *sort_str = @"";
  1832. switch (sort) {
  1833. case 0:{
  1834. sort_str = @"order by w.modify_time desc";
  1835. }
  1836. break;
  1837. case 1:{
  1838. sort_str = @"order by w.modify_time asc";
  1839. }
  1840. break;
  1841. case 2:{
  1842. sort_str = @"order by m.name asc";
  1843. }
  1844. break;
  1845. case 3:{
  1846. sort_str = @"order by m.name desc";
  1847. }
  1848. break;
  1849. case 4:{
  1850. sort_str = @"order by m.description asc";
  1851. }
  1852. break;
  1853. default:
  1854. break;
  1855. }
  1856. NSString* user = appDelegate.user;
  1857. sqlite3 *db = [iSalesDB get_db];
  1858. // order by w.create_time
  1859. 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];
  1860. // 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];
  1861. sqlite3_stmt * statement;
  1862. NSDate *date1 = [NSDate date];
  1863. NSDate *date2 = nil;
  1864. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1865. int count=0;
  1866. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1867. {
  1868. while (sqlite3_step(statement) == SQLITE_ROW)
  1869. {
  1870. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1871. int product_id = sqlite3_column_double(statement, 0);
  1872. char *description = (char*)sqlite3_column_text(statement, 1);
  1873. if(description==nil)
  1874. description= "";
  1875. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  1876. int item_id = sqlite3_column_double(statement, 2);
  1877. NSDate *date_image = [NSDate date];
  1878. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  1879. printf("image : ");
  1880. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  1881. // char *url = (char*)sqlite3_column_text(statement, 3);
  1882. // if(url==nil)
  1883. // url="";
  1884. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1885. int qty = sqlite3_column_int(statement, 3);
  1886. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  1887. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  1888. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  1889. item[@"description"]= nsdescription;
  1890. item[@"img"]= nsurl;
  1891. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  1892. count++;
  1893. }
  1894. printf("total time:");
  1895. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  1896. ret[@"count"]= [NSNumber numberWithInt:count];
  1897. ret[@"total_count"]= [NSNumber numberWithInt:count];
  1898. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  1899. ret[@"result"]= [NSNumber numberWithInt:2];
  1900. appDelegate.wish_count =count;
  1901. [appDelegate update_count_mark];
  1902. sqlite3_finalize(statement);
  1903. }
  1904. [iSalesDB close_db:db];
  1905. return ret;
  1906. }
  1907. +(NSDictionary*) offline_notimpl
  1908. {
  1909. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1910. ret[@"result"]=@"8";
  1911. ret[@"err_msg"]=@"offline mode does not support this function.";
  1912. return ret;
  1913. }
  1914. +(NSDictionary*) offline_home
  1915. {
  1916. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1917. NSString *cachefolder = [paths objectAtIndex:0];
  1918. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  1919. NSData* json =nil;
  1920. json=[NSData dataWithContentsOfFile:img_cache];
  1921. if(json==nil)
  1922. return nil;
  1923. NSError *error=nil;
  1924. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1925. return menu;
  1926. }
  1927. +(NSDictionary*) offline_category_menu
  1928. {
  1929. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1930. NSString *cachefolder = [paths objectAtIndex:0];
  1931. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  1932. NSData* json =nil;
  1933. json=[NSData dataWithContentsOfFile:img_cache];
  1934. if(json==nil)
  1935. return nil;
  1936. NSError *error=nil;
  1937. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1938. return menu;
  1939. }
  1940. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  1941. {
  1942. NSString* offline_command=params[@"offline_Command"];
  1943. NSDictionary* ret=nil;
  1944. if([offline_command isEqualToString:@"model_NIYMAL"])
  1945. {
  1946. NSString* category = params[@"category"];
  1947. ret = [self refresh_model_NIYMAL:category];
  1948. }
  1949. return ret;
  1950. }
  1951. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  1952. {
  1953. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1954. [ret setValue:@"2" forKey:@"result"];
  1955. [ret setValue:@"Regular Mode" forKey:@"mode"];
  1956. sqlite3* db= [iSalesDB get_db];
  1957. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  1958. [iSalesDB close_db:db];
  1959. [ret setObject:detail1_section forKey:@"detail_1"];
  1960. return ret;
  1961. }
  1962. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id db:(sqlite3*)db
  1963. {
  1964. UIApplication * app = [UIApplication sharedApplication];
  1965. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1966. // NSArray* arr1 = [self get_user_all_price_type];
  1967. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  1968. // NSSet *set1 = [NSSet setWithArray:arr1];
  1969. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  1970. // if(appDelegate.contact_id==nil)
  1971. // set2=[set1 mutableCopy];
  1972. // else
  1973. // [set2 intersectsSet:set1];
  1974. // NSArray *retarr = [set2 allObjects];
  1975. NSString* whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  1976. // sqlite3 *db = [iSalesDB get_db];
  1977. NSString* sqlQuery = nil;
  1978. if(appDelegate.contact_id==nil)
  1979. {
  1980. if(!appDelegate.bLogin)
  1981. return nil;
  1982. 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];
  1983. }
  1984. else
  1985. 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];
  1986. sqlite3_stmt * statement;
  1987. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1988. int count=0;
  1989. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1990. {
  1991. while (sqlite3_step(statement) == SQLITE_ROW)
  1992. {
  1993. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1994. char *name = (char*)sqlite3_column_text(statement, 0);
  1995. if(name==nil)
  1996. name="";
  1997. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1998. // double price = sqlite3_column_double(statement, 1);
  1999. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  2000. // if(isnull==nil)
  2001. // item[nsname]= @"No Price";
  2002. // else
  2003. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  2004. char *price = (char*)sqlite3_column_text(statement, 1);
  2005. if(price!=nil)
  2006. {
  2007. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2008. nsprice=[AESCrypt fastdecrypt:nsprice];
  2009. if(nsprice.length>0)
  2010. {
  2011. double dp= [nsprice doubleValue];
  2012. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  2013. }
  2014. }
  2015. else
  2016. {
  2017. item[nsname]= @"No Price";
  2018. }
  2019. // int type= sqlite3_column_int(statement, 2);
  2020. //item[@"type"]=@"price";
  2021. // item[nsname]= nsprice;
  2022. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2023. count++;
  2024. }
  2025. ret[@"count"]= [NSNumber numberWithInt:count];
  2026. sqlite3_finalize(statement);
  2027. }
  2028. // [iSalesDB close_db:db];
  2029. return ret;
  2030. }
  2031. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  2032. //{
  2033. // NSArray* arr1 = [self get_user_all_price_type:db];
  2034. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2035. //
  2036. // // NSSet *set1 = [NSSet setWithArray:arr1];
  2037. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2038. // // [set2 intersectsSet:set1];
  2039. // //
  2040. // //
  2041. // // NSArray *retarr = [set2 allObjects];
  2042. //
  2043. // NSString* whereprice=nil;
  2044. // if(contact_id==nil)
  2045. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2046. // else
  2047. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2048. //
  2049. //
  2050. // // sqlite3 *db = [iSalesDB get_db];
  2051. //
  2052. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  2053. // sqlite3_stmt * statement;
  2054. //
  2055. //
  2056. // NSNumber* ret = nil;
  2057. // double dprice=DBL_MAX;
  2058. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2059. // {
  2060. //
  2061. //
  2062. // while (sqlite3_step(statement) == SQLITE_ROW)
  2063. // {
  2064. //
  2065. // // double val = sqlite3_column_double(statement, 0);
  2066. // char *price = (char*)sqlite3_column_text(statement, 0);
  2067. // if(price!=nil)
  2068. // {
  2069. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2070. // nsprice=[AESCrypt fastdecrypt:nsprice];
  2071. // if(nsprice.length>0)
  2072. // {
  2073. // double dp= [nsprice doubleValue];
  2074. // if(dp<dprice)
  2075. // dprice=dp;
  2076. // }
  2077. // }
  2078. // }
  2079. //
  2080. //
  2081. //
  2082. //
  2083. // sqlite3_finalize(statement);
  2084. //
  2085. //
  2086. //
  2087. //
  2088. // }
  2089. //
  2090. // // [iSalesDB close_db:db];
  2091. //
  2092. // if(dprice==DBL_MAX)
  2093. // ret= nil;
  2094. // else
  2095. // ret= [NSNumber numberWithDouble:dprice];
  2096. // return ret;
  2097. //}
  2098. +(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  2099. {
  2100. NSArray* arr1 = [self get_user_all_price_type:db];
  2101. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2102. NSString* whereprice=nil;
  2103. if(contact_id==nil)
  2104. whereprice=[RAConvertor arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2105. else
  2106. whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2107. // sqlite3 *db = [iSalesDB get_db];
  2108. NSString *productIdCondition = @"1 = 1";
  2109. if (product_id) {
  2110. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  2111. }
  2112. NSString *itemIdCondition = @"";
  2113. if (item_id) {
  2114. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  2115. }
  2116. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  2117. sqlite3_stmt * statement;
  2118. NSNumber* ret = nil;
  2119. double dprice=DBL_MAX;
  2120. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2121. {
  2122. while (sqlite3_step(statement) == SQLITE_ROW)
  2123. {
  2124. // double val = sqlite3_column_double(statement, 0);
  2125. char *price = (char*)sqlite3_column_text(statement, 0);
  2126. if(price!=nil)
  2127. {
  2128. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2129. nsprice=[AESCrypt fastdecrypt:nsprice];
  2130. if(nsprice.length>0)
  2131. {
  2132. double dp= [nsprice doubleValue];
  2133. if(dp<dprice)
  2134. dprice=dp;
  2135. }
  2136. }
  2137. }
  2138. sqlite3_finalize(statement);
  2139. }
  2140. // [iSalesDB close_db:db];
  2141. if(dprice==DBL_MAX)
  2142. ret= nil;
  2143. else
  2144. ret= [NSNumber numberWithDouble:dprice];
  2145. return ret;
  2146. }
  2147. +(NSArray*) get_user_all_price_type:(sqlite3*)db
  2148. {
  2149. NSArray* ret=nil;
  2150. // sqlite3 *db = [iSalesDB get_db];
  2151. // no customer assigned , use login user contact_id
  2152. UIApplication * app = [UIApplication sharedApplication];
  2153. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2154. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",appDelegate.user];
  2155. sqlite3_stmt * statement;
  2156. // int count=0;
  2157. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2158. {
  2159. if (sqlite3_step(statement) == SQLITE_ROW)
  2160. {
  2161. char *val = (char*)sqlite3_column_text(statement, 0);
  2162. if(val==nil)
  2163. val="";
  2164. NSString* price = [[NSString alloc]initWithUTF8String:val];
  2165. ret=[RAUtils string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  2166. }
  2167. sqlite3_finalize(statement);
  2168. }
  2169. // [iSalesDB close_db:db];
  2170. return ret;
  2171. }
  2172. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id db:(sqlite3*)db
  2173. {
  2174. // sqlite3 *db = [iSalesDB get_db];
  2175. if(contact_id==nil)
  2176. {
  2177. // no customer assigned , use login user contact_id
  2178. UIApplication * app = [UIApplication sharedApplication];
  2179. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2180. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",appDelegate.user];
  2181. sqlite3_stmt * statement;
  2182. // int count=0;
  2183. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2184. {
  2185. if (sqlite3_step(statement) == SQLITE_ROW)
  2186. {
  2187. char *val = (char*)sqlite3_column_text(statement, 0);
  2188. if(val==nil)
  2189. val="";
  2190. contact_id = [[NSString alloc]initWithUTF8String:val];
  2191. }
  2192. sqlite3_finalize(statement);
  2193. }
  2194. if(contact_id.length<=0)
  2195. {
  2196. // [iSalesDB close_db:db];
  2197. return nil;
  2198. }
  2199. }
  2200. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  2201. sqlite3_stmt * statement;
  2202. NSArray* ret=nil;
  2203. // int count=0;
  2204. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2205. {
  2206. if (sqlite3_step(statement) == SQLITE_ROW)
  2207. {
  2208. char *price_type = (char*)sqlite3_column_text(statement, 0);
  2209. if(price_type==nil)
  2210. price_type="";
  2211. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  2212. if(nsprice_type.length>0)
  2213. ret=[RAUtils string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  2214. else
  2215. ret=nil;
  2216. }
  2217. sqlite3_finalize(statement);
  2218. }
  2219. // [iSalesDB close_db:db];
  2220. return ret;
  2221. }
  2222. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  2223. {
  2224. NSString* ret= nil;
  2225. // sqlite3 *db = [iSalesDB get_db];
  2226. NSString *sqlQuery = nil;
  2227. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  2228. // 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;
  2229. // 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
  2230. if(product_id==nil && model_name)
  2231. 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;
  2232. else if (product_id)
  2233. 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
  2234. sqlite3_stmt * statement;
  2235. // int count=0;
  2236. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2237. {
  2238. if (sqlite3_step(statement) == SQLITE_ROW)
  2239. {
  2240. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  2241. if(imgurl==nil)
  2242. imgurl="";
  2243. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  2244. ret=nsimgurl;
  2245. }
  2246. sqlite3_finalize(statement);
  2247. }
  2248. else
  2249. {
  2250. [ret setValue:@"8" forKey:@"result"];
  2251. }
  2252. // [iSalesDB close_db:db];
  2253. DebugLog(@"data string: %@",ret );
  2254. return ret;
  2255. }
  2256. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name
  2257. {
  2258. NSString* ret= nil;
  2259. sqlite3 *db = [iSalesDB get_db];
  2260. NSString *sqlQuery = nil;
  2261. if(product_id==nil)
  2262. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  2263. else
  2264. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  2265. sqlite3_stmt * statement;
  2266. // int count=0;
  2267. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2268. {
  2269. if (sqlite3_step(statement) == SQLITE_ROW)
  2270. {
  2271. char *default_category = (char*)sqlite3_column_text(statement, 0);
  2272. if(default_category==nil)
  2273. default_category="";
  2274. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2275. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  2276. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  2277. ret=nsdefault_category;
  2278. }
  2279. sqlite3_finalize(statement);
  2280. }
  2281. else
  2282. {
  2283. [ret setValue:@"8" forKey:@"result"];
  2284. }
  2285. [iSalesDB close_db:db];
  2286. DebugLog(@"data string: %@",ret );
  2287. return ret;
  2288. }
  2289. + (NSString *) categoryIdOfProduct:(NSString *)product_id name:(NSString *)product_name {
  2290. NSString *sql = nil;
  2291. if (product_id != nil) {
  2292. sql = [NSString stringWithFormat:@"select category from product where product_id = '%@';",product_id];
  2293. } else {
  2294. sql = [NSString stringWithFormat:@"select category from product where name = '%@';",product_name];
  2295. }
  2296. NSString *result_set = [iSalesDB jk_queryText:sql];
  2297. result_set = [result_set stringByReplacingOccurrencesOfString:@"%" withString:@""];
  2298. result_set = [result_set stringByReplacingOccurrencesOfString:@"#" withString:@""];
  2299. return result_set;
  2300. }
  2301. +(NSDictionary*) offline_model :(NSMutableDictionary *) params
  2302. {
  2303. NSString* model_name = [params valueForKey:@"product_name"];
  2304. NSString* product_id = [params valueForKey:@"product_id"];
  2305. NSString* category = [params valueForKey:@"category"];
  2306. NSString *default_category_id = [self model_default_category:product_id model_name:model_name];
  2307. if(category==nil) {
  2308. category = default_category_id;
  2309. } else {
  2310. NSArray *arr_0 = [category componentsSeparatedByString:@","];
  2311. // 参数重有多个category id
  2312. if (arr_0.count > 1) {
  2313. NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  2314. // 取交集
  2315. NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  2316. NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  2317. [set_0 intersectSet:set_1];
  2318. if (set_0.count == 1) {
  2319. category = (NSString *)[set_0 anyObject];
  2320. } else {
  2321. if ([set_0 containsObject:default_category_id]) {
  2322. category = default_category_id;
  2323. } else {
  2324. category = (NSString *)[set_0 anyObject];
  2325. }
  2326. }
  2327. }
  2328. }
  2329. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2330. sqlite3 *db = [iSalesDB get_db];
  2331. // int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  2332. NSString *sqlQuery = nil;
  2333. if(product_id==nil)
  2334. 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,seat_dimension,product_content_writing 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='%@';
  2335. else
  2336. 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,seat_dimension,product_content_writing 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=%@;
  2337. sqlite3_stmt * statement;
  2338. [ret setValue:@"2" forKey:@"result"];
  2339. [ret setValue:@"3" forKey:@"detail_section_count"];
  2340. // int count=0;
  2341. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2342. {
  2343. if (sqlite3_step(statement) == SQLITE_ROW)
  2344. {
  2345. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2346. char *name = (char*)sqlite3_column_text(statement, 0);
  2347. if(name==nil)
  2348. name="";
  2349. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2350. char *description = (char*)sqlite3_column_text(statement, 1);
  2351. if(description==nil)
  2352. description="";
  2353. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2354. int product_id = sqlite3_column_int(statement, 2);
  2355. char *color = (char*)sqlite3_column_text(statement, 3);
  2356. if(color==nil)
  2357. color="";
  2358. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  2359. //
  2360. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  2361. // if(legcolor==nil)
  2362. // legcolor="";
  2363. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  2364. //
  2365. //
  2366. int availability = sqlite3_column_int(statement, 5);
  2367. //
  2368. int incoming_stock = sqlite3_column_int(statement, 6);
  2369. char *demension = (char*)sqlite3_column_text(statement, 7);
  2370. if(demension==nil)
  2371. demension="";
  2372. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  2373. // ,,,,,,,,,
  2374. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  2375. if(seat_height==nil)
  2376. seat_height="";
  2377. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  2378. char *material = (char*)sqlite3_column_text(statement, 9);
  2379. if(material==nil)
  2380. material="";
  2381. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  2382. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  2383. if(box_dim==nil)
  2384. box_dim="";
  2385. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  2386. char *volume = (char*)sqlite3_column_text(statement, 11);
  2387. if(volume==nil)
  2388. volume="";
  2389. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  2390. double weight = sqlite3_column_double(statement, 12);
  2391. char *model_set = (char*)sqlite3_column_text(statement, 13);
  2392. if(model_set==nil)
  2393. model_set="";
  2394. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  2395. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  2396. if(load_ability==nil)
  2397. load_ability="";
  2398. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  2399. char *default_category = (char*)sqlite3_column_text(statement, 15);
  2400. if(default_category==nil)
  2401. default_category="";
  2402. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2403. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  2404. if(fabric_content==nil)
  2405. fabric_content="";
  2406. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  2407. char *assembling = (char*)sqlite3_column_text(statement, 17);
  2408. if(assembling==nil)
  2409. assembling="";
  2410. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  2411. char *made_in = (char*)sqlite3_column_text(statement, 18);
  2412. if(made_in==nil)
  2413. made_in="";
  2414. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  2415. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  2416. if(special_remarks==nil)
  2417. special_remarks="";
  2418. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  2419. int stockUcom = sqlite3_column_double(statement, 20);
  2420. char *product_group = (char*)sqlite3_column_text(statement, 21);
  2421. if(product_group==nil)
  2422. product_group="";
  2423. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  2424. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  2425. // if(fashion_selector==nil)
  2426. // fashion_selector="";
  2427. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  2428. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  2429. if(selector_field==nil)
  2430. selector_field="";
  2431. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  2432. char *property_field = (char*)sqlite3_column_text(statement, 23);
  2433. if(property_field==nil)
  2434. property_field="";
  2435. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  2436. char *packaging = (char*)sqlite3_column_text(statement, 24);
  2437. if(packaging==nil)
  2438. packaging="";
  2439. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  2440. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2441. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  2442. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  2443. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  2444. [img_section setValue:model_s_img forKey:@"model_s_img"];
  2445. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  2446. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  2447. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  2448. NSString* Availability=nil;
  2449. if(availability>0)
  2450. Availability=[NSString stringWithFormat:@"%d",availability];
  2451. else
  2452. Availability = @"Out of Stock";
  2453. [img_section setValue:Availability forKey:@"Availability"];
  2454. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  2455. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  2456. char *eta = (char*)sqlite3_column_text(statement, 25);
  2457. if(eta==nil)
  2458. eta="";
  2459. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  2460. if ([nseta isEqualToString:@"null"]) {
  2461. nseta = @"";
  2462. }
  2463. if (availability <= 0) {
  2464. [img_section setValue:nseta forKey:@"ETA"];
  2465. }
  2466. int item_id = sqlite3_column_int(statement, 26);
  2467. NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  2468. NSString *content_writing = [self textAtColumn:28 statement:statement];
  2469. NSString* Price=nil;
  2470. if(appDelegate.bLogin==false)
  2471. Price=@"Must Sign in.";
  2472. else
  2473. {
  2474. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2475. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  2476. if(price==nil)
  2477. Price=@"No Price.";
  2478. else
  2479. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2480. }
  2481. [img_section setObject:content_writing forKey:@"product_content_writing"];
  2482. [img_section setValue:Price forKey:@"price"];
  2483. [img_section setValue:nsname forKey:@"model_name"];
  2484. [img_section setValue:nsdescription forKey:@"model_descrition"];
  2485. if (appDelegate.order_code) { // 离线order code即so#
  2486. NSString *cartQtySql = [NSString stringWithFormat:@"select c.item_count from offline_cart as c left join offline_order as o on c.so_no = o.so_id where o.so_id = '%@' and c.item_id = %d;",appDelegate.order_code,item_id];
  2487. __block int cartQTY = 0;
  2488. [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2489. cartQTY = sqlite3_column_int(stmt, 0);
  2490. }];
  2491. if (cartQTY > 0) {
  2492. [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  2493. }
  2494. }
  2495. [ret setObject:img_section forKey:@"img_section"];
  2496. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  2497. int detail0_item_count=0;
  2498. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2499. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2500. [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2501. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2502. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2503. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2504. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2505. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2506. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2507. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2508. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2509. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2510. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2511. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2512. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  2513. NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  2514. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  2515. {
  2516. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  2517. [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2518. }
  2519. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  2520. [detail0_section setValue:@"kv" forKey:@"type"];
  2521. [detail0_section setValue:@"Product Information" forKey:@"title"];
  2522. [ret setObject:detail0_section forKey:@"detail_0"];
  2523. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  2524. // [detail1_section setValue:@"detail" forKey:@"target"];
  2525. // [detail1_section setValue:@"popup" forKey:@"action"];
  2526. // [detail1_section setValue:@"content" forKey:@"type"];
  2527. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  2528. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  2529. // [detail1_section setValue:@"true" forKey:@"single_row"];
  2530. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  2531. [ret setObject:detail1_section forKey:@"detail_1"];
  2532. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  2533. [detail2_section setValue:@"detail" forKey:@"target"];
  2534. [detail2_section setValue:@"popup" forKey:@"action"];
  2535. [detail2_section setValue:@"content" forKey:@"type"];
  2536. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  2537. [detail2_section setValue:@"true" forKey:@"single_row"];
  2538. [detail2_section setValue:@"local" forKey:@"data"];
  2539. [ret setObject:detail2_section forKey:@"detail_2"];
  2540. }
  2541. sqlite3_finalize(statement);
  2542. }
  2543. else
  2544. {
  2545. [ret setValue:@"8" forKey:@"result"];
  2546. }
  2547. // DebugLog(@"count:%d",count);
  2548. [iSalesDB close_db:db];
  2549. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  2550. return ret;
  2551. }
  2552. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  2553. NSString* orderCode = [params valueForKey:@"orderCode"];
  2554. NSString* keyword = [params valueForKey:@"keyword"];
  2555. keyword=keyword.lowercaseString;
  2556. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  2557. int limit = [[params valueForKey:@"limit"] intValue];
  2558. int offset = [[params valueForKey:@"offset"] intValue];
  2559. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2560. NSString *limit_str = @"";
  2561. if (limited) {
  2562. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  2563. }
  2564. sqlite3 *db = [iSalesDB get_db];
  2565. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  2566. UIApplication * app = [UIApplication sharedApplication];
  2567. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2568. NSString *sqlQuery = nil;
  2569. if(exactMatch )
  2570. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color 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 ;
  2571. else
  2572. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color 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'%%%@%%' or lower(made_in) like '%%%@%%' or lower(material) like '%%%@%%' or lower(packaging) like '%%%@%%') and m.is_active = 1 order by m.name %@ ;",appDelegate.order_code, keyword,keyword,keyword,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
  2573. DebugLog(@"offline_search sql:%@",sqlQuery);
  2574. sqlite3_stmt * statement;
  2575. [ret setValue:@"2" forKey:@"result"];
  2576. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  2577. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  2578. // int count=0;
  2579. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2580. {
  2581. int i=0;
  2582. while (sqlite3_step(statement) == SQLITE_ROW)
  2583. {
  2584. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2585. // char *name = (char*)sqlite3_column_text(statement, 1);
  2586. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2587. char *name = (char*)sqlite3_column_text(statement, 0);
  2588. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2589. char *description = (char*)sqlite3_column_text(statement, 1);
  2590. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2591. int product_id = sqlite3_column_int(statement, 2);
  2592. // char *url = (char*)sqlite3_column_text(statement, 3);
  2593. // if(url==nil)
  2594. // url="";
  2595. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2596. int wid = sqlite3_column_int(statement, 3);
  2597. int closeout = sqlite3_column_int(statement, 4);
  2598. int cid = sqlite3_column_int(statement, 5);
  2599. int wisdelete = sqlite3_column_int(statement, 6);
  2600. int more_color = sqlite3_column_int(statement, 7);
  2601. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  2602. if(wid !=0 && wisdelete != 1)
  2603. [item setValue:@"true" forKey:@"wish_exists"];
  2604. else
  2605. [item setValue:@"false" forKey:@"wish_exists"];
  2606. if(closeout==0)
  2607. [item setValue:@"false" forKey:@"is_closeout"];
  2608. else
  2609. [item setValue:@"true" forKey:@"is_closeout"];
  2610. if(cid==0)
  2611. [item setValue:@"false" forKey:@"cart_exists"];
  2612. else
  2613. [item setValue:@"true" forKey:@"cart_exists"];
  2614. if (more_color == 0) {
  2615. [item setObject:@(false) forKey:@"more_color"];
  2616. } else if (more_color == 1) {
  2617. [item setObject:@(true) forKey:@"more_color"];
  2618. }
  2619. [item addEntriesFromDictionary:imgjson];
  2620. // [item setValue:nsurl forKey:@"img"];
  2621. [item setValue:nsname forKey:@"fash_name"];
  2622. [item setValue:nsdescription forKey:@"description"];
  2623. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2624. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2625. i++;
  2626. }
  2627. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2628. [ret setObject:items forKey:@"items"];
  2629. sqlite3_finalize(statement);
  2630. }
  2631. DebugLog(@"count:%d",count);
  2632. [iSalesDB close_db:db];
  2633. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  2634. return ret;
  2635. }
  2636. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  2637. {
  2638. return [self search:params limited:YES];
  2639. }
  2640. +(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  2641. {
  2642. UIApplication * app = [UIApplication sharedApplication];
  2643. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2644. appDelegate.disable_trigger=true;
  2645. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  2646. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2647. appDelegate.disable_trigger=false;
  2648. //
  2649. // NSString* user = [params valueForKey:@"user"];
  2650. //
  2651. // NSString* password = [params valueForKey:@"password"];
  2652. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2653. ret[@"result"]=[NSNumber numberWithInt:2 ];
  2654. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  2655. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  2656. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  2657. [appDelegate update_count_mark];
  2658. appDelegate.can_show_price =false;
  2659. appDelegate.can_see_price =false;
  2660. appDelegate.can_create_portfolio =false;
  2661. appDelegate.can_create_order =false;
  2662. appDelegate.can_cancel_order =false;
  2663. appDelegate.can_set_cart_price =false;
  2664. appDelegate.can_delete_order =false;
  2665. appDelegate.can_submit_order =false;
  2666. appDelegate.can_set_tearsheet_price =false;
  2667. appDelegate.can_update_contact_info = false;
  2668. appDelegate.save_order_logout = false;
  2669. appDelegate.submit_order_logout = false;
  2670. appDelegate.alert_sold_in_quantities = false;
  2671. appDelegate.ipad_perm =nil ;
  2672. appDelegate.user_type = USER_ROLE_UNKNOWN;
  2673. appDelegate.OrderFilter= nil;
  2674. [appDelegate SetSo:nil];
  2675. [appDelegate set_main_button_panel];
  2676. // sqlite3 *db = [iSalesDB get_db];
  2677. //
  2678. //
  2679. //
  2680. //
  2681. //
  2682. // 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"]];
  2683. //
  2684. //
  2685. //
  2686. //
  2687. // DebugLog(@"offline_login sql:%@",sqlQuery);
  2688. // sqlite3_stmt * statement;
  2689. //
  2690. //
  2691. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2692. //
  2693. //
  2694. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2695. // {
  2696. //
  2697. //
  2698. // if (sqlite3_step(statement) == SQLITE_ROW)
  2699. // {
  2700. //
  2701. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2702. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  2703. //
  2704. //
  2705. //
  2706. // int can_show_price = sqlite3_column_int(statement, 0);
  2707. // int can_see_price = sqlite3_column_int(statement, 1);
  2708. //
  2709. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  2710. // if(contact_id==nil)
  2711. // contact_id="";
  2712. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2713. //
  2714. // int user_type = sqlite3_column_int(statement, 3);
  2715. //
  2716. // int can_cancel_order = sqlite3_column_int(statement, 4);
  2717. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  2718. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  2719. // int can_delete_order = sqlite3_column_int(statement, 7);
  2720. // int can_submit_order = sqlite3_column_int(statement, 8);
  2721. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  2722. // int can_create_order = sqlite3_column_int(statement, 10);
  2723. //
  2724. //
  2725. // char *mode = (char*)sqlite3_column_text(statement, 11);
  2726. // if(mode==nil)
  2727. // mode="";
  2728. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  2729. //
  2730. //
  2731. // char *username = (char*)sqlite3_column_text(statement, 12);
  2732. // if(username==nil)
  2733. // username="";
  2734. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  2735. //
  2736. //
  2737. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  2738. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  2739. // [header setValue:nscontact_id forKey:@"contact_id"];
  2740. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  2741. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  2742. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  2743. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  2744. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  2745. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  2746. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  2747. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  2748. //
  2749. // [header setValue:nsusername forKey:@"username"];
  2750. //
  2751. //
  2752. // [ret setObject:header forKey:@"header"];
  2753. // [ret setValue:nsmode forKey:@"mode"];
  2754. //
  2755. //
  2756. // }
  2757. //
  2758. //
  2759. //
  2760. // sqlite3_finalize(statement);
  2761. // }
  2762. //
  2763. //
  2764. //
  2765. // [iSalesDB close_db:db];
  2766. //
  2767. //
  2768. //
  2769. //
  2770. // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2771. return ret;
  2772. }
  2773. +(NSData*) offline_createorder :(NSMutableDictionary *) params
  2774. {
  2775. UIApplication * app = [UIApplication sharedApplication];
  2776. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2777. appDelegate.disable_trigger=true;
  2778. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  2779. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2780. appDelegate.disable_trigger=false;
  2781. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2782. NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  2783. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  2784. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  2785. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  2786. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  2787. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  2788. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  2789. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  2790. NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  2791. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  2792. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  2793. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  2794. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  2795. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  2796. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  2797. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  2798. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  2799. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  2800. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  2801. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  2802. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  2803. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  2804. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  2805. [arr_name addObject:customer_first_name];
  2806. [arr_name addObject:customer_last_name];
  2807. NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  2808. // default ship from
  2809. 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';";
  2810. __block NSString *cid = @"";
  2811. __block NSString *name = @"";
  2812. __block NSString *ext = @"";
  2813. __block NSString *contact = @"";
  2814. __block NSString *email = @"";
  2815. __block NSString *fax = @"";
  2816. __block NSString *phone = @"";
  2817. sqlite3 *db = [iSalesDB get_db];
  2818. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  2819. cid = [self textAtColumn:0 statement:statment];
  2820. name = [self textAtColumn:1 statement:statment];
  2821. ext = [self textAtColumn:2 statement:statment];
  2822. contact = [self textAtColumn:3 statement:statment];
  2823. email = [self textAtColumn:4 statement:statment];
  2824. fax = [self textAtColumn:5 statement:statment];
  2825. phone = [self textAtColumn:6 statement:statment];
  2826. }];
  2827. NSString* so_id = [self get_offline_soid:db];
  2828. if(so_id==nil)
  2829. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  2830. 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];
  2831. int result =[iSalesDB execSql:sql_neworder db:db];
  2832. [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  2833. //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'
  2834. //soId
  2835. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  2836. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  2837. sqlite3_stmt * statement;
  2838. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2839. {
  2840. if (sqlite3_step(statement) == SQLITE_ROW)
  2841. {
  2842. // char *name = (char*)sqlite3_column_text(statement, 1);
  2843. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2844. //ret = sqlite3_column_int(statement, 0);
  2845. char *soId = (char*)sqlite3_column_text(statement, 0);
  2846. if(soId==nil)
  2847. soId="";
  2848. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  2849. [ret setValue:nssoId forKey:@"soId"];
  2850. [ret setValue:nssoId forKey:@"orderCode"];
  2851. }
  2852. sqlite3_finalize(statement);
  2853. }
  2854. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  2855. [ret setValue:@"Regular Mode" forKey:@"mode"];
  2856. [iSalesDB close_db:db];
  2857. return [RAConvertor dict2data:ret];
  2858. }
  2859. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  2860. {
  2861. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2862. NSString* orderCode = [params valueForKey:@"orderCode"];
  2863. UIApplication * app = [UIApplication sharedApplication];
  2864. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2865. if(![appDelegate.order_code isEqualToString:orderCode]&& appDelegate.order_code.length!=0)
  2866. {
  2867. appDelegate.disable_trigger=true;
  2868. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  2869. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2870. appDelegate.disable_trigger=false;
  2871. }
  2872. sqlite3 *db = [iSalesDB get_db];
  2873. int cart_count=[self query_ordercartcount:orderCode db:db];
  2874. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2875. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2876. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2877. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2878. [iSalesDB close_db:db];
  2879. return [RAConvertor dict2data:ret];
  2880. }
  2881. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  2882. {
  2883. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2884. sqlite3 *db = [iSalesDB get_db];
  2885. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  2886. int count =0;
  2887. if(params[@"count"]!=nil)
  2888. {
  2889. count = [params[@"count"] intValue];
  2890. }
  2891. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  2892. NSString* product_id=params[@"product_id"];
  2893. NSString* orderCode=params[@"orderCode"];
  2894. NSString *qty = params[@"qty"];
  2895. NSArray* arr_id=[RAUtils string2arr:product_id separator:@","];
  2896. NSArray *qty_arr = [RAUtils string2arr:qty separator:@","];
  2897. __block int number_of_outOfStock = 0;
  2898. for(int i=0;i<arr_id.count;i++)
  2899. {
  2900. NSInteger item_qty= count;
  2901. if (qty) {
  2902. item_qty = [qty_arr[i] integerValue];
  2903. }
  2904. if(item_qty==0)
  2905. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  2906. // 检查新加Model数量是否大于库存
  2907. if (!appDelegate.can_create_backorder) {
  2908. __block BOOL needContinue = NO;
  2909. [iSalesDB jk_query:[NSString stringWithFormat:@"select availability from product where product_id = %@;",arr_id[i]] db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2910. int availability = sqlite3_column_int(stmt, 0);
  2911. // 库存小于购买量为缺货
  2912. if (availability < item_qty || availability <= 0) {
  2913. number_of_outOfStock++;
  2914. needContinue = YES;
  2915. }
  2916. }];
  2917. if (needContinue) {
  2918. continue;
  2919. }
  2920. }
  2921. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  2922. NSString* sql=nil;
  2923. sqlite3_stmt *stmt = nil;
  2924. BOOL shouldStep = NO;
  2925. if(_id<0)
  2926. {
  2927. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  2928. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  2929. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2930. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],(int)[arr_id[i] length],NULL);
  2931. sqlite3_bind_text(stmt,2,[orderCode UTF8String],(int)[orderCode length],NULL);
  2932. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  2933. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  2934. shouldStep = YES;
  2935. }
  2936. else
  2937. {
  2938. if (qty) { // wish list move to cart
  2939. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  2940. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2941. sqlite3_bind_int(stmt, 1, _id);
  2942. shouldStep = YES;
  2943. } else {
  2944. sql = [NSString stringWithFormat:@"select c.item_count + %ld,p.availability from product as p left join offline_cart as c on c.item_id = p.item_id where c._id = %d;",item_qty,_id];
  2945. __block BOOL update = YES;
  2946. if (!appDelegate.can_create_backorder) {
  2947. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2948. int newQTY = sqlite3_column_int(stmt, 0);
  2949. int availability = sqlite3_column_int(stmt, 1);
  2950. if (newQTY > availability) { // 库存不够
  2951. update = NO;
  2952. number_of_outOfStock++;
  2953. }
  2954. }];
  2955. }
  2956. if (update) {
  2957. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  2958. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2959. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  2960. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",_id] length],NULL);
  2961. shouldStep = YES;
  2962. }
  2963. }
  2964. }
  2965. if (shouldStep) {
  2966. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  2967. [iSalesDB execSql:@"ROLLBACK" db:db];
  2968. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  2969. [iSalesDB close_db:db];
  2970. DebugLog(@"add to cart error");
  2971. return [RAConvertor dict2data:ret];
  2972. }
  2973. }
  2974. }
  2975. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2976. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2977. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2978. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2979. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2980. if (!appDelegate.can_create_backorder) {
  2981. if (number_of_outOfStock > 0) {
  2982. ret[@"result"]=[NSNumber numberWithInt:8];
  2983. ret[@"err_msg"] = @"Out of Stock.\nNo more quantity available.\nPlease try again after sync.";
  2984. /*[NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock]*/
  2985. }
  2986. }
  2987. [iSalesDB execSql:@"END TRANSACTION" db:db];
  2988. [iSalesDB close_db:db];
  2989. return [RAConvertor dict2data:ret];
  2990. }
  2991. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute contactID:(NSString *)contactID
  2992. {
  2993. // UIApplication * app = [UIApplication sharedApplication];
  2994. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2995. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  2996. 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];
  2997. sqlite3_stmt * statement;
  2998. int count=0;
  2999. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3000. {
  3001. while (sqlite3_step(statement) == SQLITE_ROW)
  3002. {
  3003. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  3004. int bitem_id = sqlite3_column_int(statement, 0);
  3005. int bitem_qty = sqlite3_column_int(statement, 1);
  3006. char *name = (char*)sqlite3_column_text(statement, 2);
  3007. if(name==nil)
  3008. name="";
  3009. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  3010. char *description = (char*)sqlite3_column_text(statement, 3);
  3011. if(description==nil)
  3012. description="";
  3013. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  3014. double unit_price = sqlite3_column_double(statement, 4);
  3015. int use_unitprice = sqlite3_column_int(statement, 5);
  3016. if(use_unitprice!=1)
  3017. {
  3018. unit_price=[self get_model_default_price:contactID product_id:nil item_id:@(bitem_id) db:db].doubleValue;
  3019. }
  3020. itemjson[@"model"]=nsname;
  3021. itemjson[@"description"]=nsdescription;
  3022. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  3023. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  3024. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  3025. if(compute)
  3026. {
  3027. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute contactID:contactID];
  3028. }
  3029. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  3030. count++;
  3031. }
  3032. sqlite3_finalize(statement);
  3033. }
  3034. ret[@"count"]=@(count);
  3035. if(count==0)
  3036. return nil;
  3037. else
  3038. return ret;
  3039. }
  3040. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  3041. {
  3042. //compute: add part to subtotal;
  3043. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  3044. dict_item[@(item_id)]=@"1";
  3045. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  3046. double cuft=0;
  3047. double weight=0;
  3048. int carton=0;
  3049. 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];
  3050. sqlite3_stmt * statement;
  3051. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3052. {
  3053. if (sqlite3_step(statement) == SQLITE_ROW)
  3054. {
  3055. double ulength = sqlite3_column_double(statement, 0);
  3056. double uwidth = sqlite3_column_double(statement, 1);
  3057. double uheight = sqlite3_column_double(statement, 2);
  3058. double uweight = sqlite3_column_double(statement, 3);
  3059. double mlength = sqlite3_column_double(statement, 4);
  3060. double mwidth = sqlite3_column_double(statement, 5);
  3061. double mheight = sqlite3_column_double(statement, 6);
  3062. double mweight = sqlite3_column_double(statement, 7);
  3063. double ilength = sqlite3_column_double(statement, 8);
  3064. double iwidth = sqlite3_column_double(statement, 9);
  3065. double iheight = sqlite3_column_double(statement, 10);
  3066. double iweight = sqlite3_column_double(statement, 11);
  3067. int pcs = sqlite3_column_int(statement,12);
  3068. int mpack = sqlite3_column_int(statement, 13);
  3069. int ipack = sqlite3_column_int(statement, 14);
  3070. double ucbf = sqlite3_column_double(statement, 15);
  3071. double icbf = sqlite3_column_double(statement, 16);
  3072. double mcbf = sqlite3_column_double(statement, 17);
  3073. if(ipack==0)
  3074. {
  3075. carton= count/mpack ;
  3076. weight = mweight*carton;
  3077. cuft= carton*(mlength*mwidth*mheight);
  3078. int remain=count%mpack;
  3079. if(remain==0)
  3080. {
  3081. //do nothing;
  3082. }
  3083. else
  3084. {
  3085. carton++;
  3086. weight += uweight*remain;
  3087. cuft += (ulength*uwidth*uheight)*remain;
  3088. }
  3089. }
  3090. else
  3091. {
  3092. carton = count/(mpack*ipack);
  3093. weight = mweight*carton;
  3094. cuft= carton*(mlength*mwidth*mheight);
  3095. int remain=count%(mpack*ipack);
  3096. if(remain==0)
  3097. {
  3098. // do nothing;
  3099. }
  3100. else
  3101. {
  3102. carton++;
  3103. int icarton =remain/ipack;
  3104. int iremain=remain%ipack;
  3105. weight += iweight*icarton;
  3106. cuft += (ilength*iwidth*iheight)*icarton;
  3107. if(iremain==0)
  3108. {
  3109. //do nothing;
  3110. }
  3111. else
  3112. {
  3113. weight += uweight*iremain;
  3114. cuft += (ulength*uwidth*uheight)*iremain;
  3115. }
  3116. }
  3117. }
  3118. #ifdef BUILD_NPD
  3119. cuft=ucbf*count;
  3120. weight= uweight*count;
  3121. #endif
  3122. #if defined(BUILD_HOMER) || defined(BUILD_GATIT)
  3123. cuft=ucbf*count;
  3124. weight= uweight*count;
  3125. #endif
  3126. }
  3127. sqlite3_finalize(statement);
  3128. }
  3129. if(compute)
  3130. {
  3131. NSArray * arr_count=nil;
  3132. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  3133. for(int i=0;i<arr_bundle.count;i++)
  3134. {
  3135. dict_item[arr_bundle[i]]=@"1";
  3136. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  3137. cuft+=[bundlejson[@"cuft"] doubleValue];
  3138. weight+=[bundlejson[@"weight"] doubleValue];
  3139. carton+=[bundlejson[@"carton"] intValue];
  3140. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  3141. }
  3142. }
  3143. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  3144. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  3145. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  3146. ret[@"items"]=dict_item;
  3147. return ret;
  3148. }
  3149. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  3150. {
  3151. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  3152. // 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 ];
  3153. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3154. DebugLog(@"offline_login sql:%@",sqlQuery);
  3155. sqlite3_stmt * statement;
  3156. int cart_count=0;
  3157. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3158. if ( dbresult== SQLITE_OK)
  3159. {
  3160. while (sqlite3_step(statement) == SQLITE_ROW)
  3161. {
  3162. int item_id = sqlite3_column_int(statement, 0);
  3163. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  3164. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3165. }
  3166. sqlite3_finalize(statement);
  3167. }
  3168. return cart_count;
  3169. }
  3170. +(NSData*) offline_requestcart :(NSMutableDictionary *) params
  3171. {
  3172. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3173. sqlite3 *db = [iSalesDB get_db];
  3174. UIApplication * app = [UIApplication sharedApplication];
  3175. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3176. NSString* orderCode=params[@"orderCode"];
  3177. int sort = [[params objectForKey:@"sort"] intValue];
  3178. NSString *sort_str = @"";
  3179. switch (sort) {
  3180. case 0:{
  3181. sort_str = @"order by c.modify_time desc";
  3182. }
  3183. break;
  3184. case 1:{
  3185. sort_str = @"order by c.modify_time asc";
  3186. }
  3187. break;
  3188. case 2:{
  3189. sort_str = @"order by m.name asc";
  3190. }
  3191. break;
  3192. case 3:{
  3193. sort_str = @"order by m.name desc";
  3194. }
  3195. break;
  3196. case 4:{
  3197. sort_str = @"order by m.description asc";
  3198. }
  3199. break;
  3200. default:
  3201. break;
  3202. }
  3203. 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,m.availability 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 ];
  3204. // 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 ];
  3205. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3206. DebugLog(@"offline_login sql:%@",sqlQuery);
  3207. sqlite3_stmt * statement;
  3208. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3209. NSDate *date1 = [NSDate date];
  3210. int count=0;
  3211. int cart_count=0;
  3212. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3213. if ( dbresult== SQLITE_OK)
  3214. {
  3215. while (sqlite3_step(statement) == SQLITE_ROW)
  3216. {
  3217. // NSDate *row_date = [NSDate date];
  3218. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  3219. int product_id = sqlite3_column_int(statement, 0);
  3220. char *str_price = (char*)sqlite3_column_text(statement, 1);
  3221. int item_id = sqlite3_column_int(statement, 7);
  3222. NSString* Price=nil;
  3223. if(str_price==nil)
  3224. {
  3225. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  3226. // NSDate *price_date = [NSDate date];
  3227. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  3228. // DebugLog(@"price time interval");
  3229. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  3230. if(price==nil)
  3231. Price=@"No Price.";
  3232. else
  3233. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  3234. }
  3235. else
  3236. {
  3237. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  3238. }
  3239. double discount = sqlite3_column_double(statement, 2);
  3240. int item_count = sqlite3_column_int(statement, 3);
  3241. char *line_note = (char*)sqlite3_column_text(statement, 4);
  3242. NSString *nsline_note=nil;
  3243. if(line_note!=nil)
  3244. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  3245. char *name = (char*)sqlite3_column_text(statement, 5);
  3246. NSString *nsname=nil;
  3247. if(name!=nil)
  3248. nsname= [[NSString alloc]initWithUTF8String:name];
  3249. char *description = (char*)sqlite3_column_text(statement, 6);
  3250. NSString *nsdescription=nil;
  3251. if(description!=nil)
  3252. nsdescription= [[NSString alloc]initWithUTF8String:description];
  3253. int stockUom = sqlite3_column_int(statement, 8);
  3254. int _id = sqlite3_column_int(statement, 9);
  3255. int availability = sqlite3_column_int(statement, 10);
  3256. // NSDate *subtotal_date = [NSDate date];
  3257. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  3258. // DebugLog(@"subtotal_date time interval");
  3259. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  3260. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  3261. double weight=[bsubtotaljson[@"weight"] doubleValue];
  3262. int carton=[bsubtotaljson[@"carton"] intValue];
  3263. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3264. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  3265. // NSDate *img_date = [NSDate date];
  3266. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  3267. // DebugLog(@"img_date time interval");
  3268. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  3269. itemjson[@"model"]=nsname;
  3270. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  3271. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  3272. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  3273. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  3274. itemjson[@"check"]=@"true";
  3275. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  3276. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  3277. itemjson[@"unit_price"]=Price;
  3278. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  3279. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  3280. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  3281. itemjson[@"note"]=nsline_note;
  3282. if (!appDelegate.can_create_backorder) {
  3283. itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  3284. }
  3285. // NSDate *date2 = [NSDate date];
  3286. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  3287. // DebugLog(@"model_bundle time interval");
  3288. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  3289. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  3290. count++;
  3291. // DebugLog(@"row time interval");
  3292. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  3293. }
  3294. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3295. sqlite3_finalize(statement);
  3296. }
  3297. DebugLog(@"request cart total time interval");
  3298. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  3299. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  3300. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  3301. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3302. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3303. ret[@"count"]=[NSNumber numberWithInt:count ];
  3304. ret[@"mode"]=@"Regular Mode";
  3305. [iSalesDB close_db:db];
  3306. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  3307. DebugLog(@"general notes :%@",general_note);
  3308. ret[@"general_note"]= general_note;
  3309. return [RAConvertor dict2data:ret];
  3310. }
  3311. +(NSData*) offline_login :(NSMutableDictionary *) params
  3312. {
  3313. NSString* user = [params valueForKey:@"user"];
  3314. NSString* password = [params valueForKey:@"password"];
  3315. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3316. sqlite3 *db = [iSalesDB get_db];
  3317. 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"]];
  3318. DebugLog(@"offline_login sql:%@",sqlQuery);
  3319. sqlite3_stmt * statement;
  3320. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3321. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3322. {
  3323. if (sqlite3_step(statement) == SQLITE_ROW)
  3324. {
  3325. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3326. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  3327. int can_show_price = sqlite3_column_int(statement, 0);
  3328. int can_see_price = sqlite3_column_int(statement, 1);
  3329. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  3330. if(contact_id==nil)
  3331. contact_id="";
  3332. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3333. int user_type = sqlite3_column_int(statement, 3);
  3334. int can_cancel_order = sqlite3_column_int(statement, 4);
  3335. int can_set_cart_price = sqlite3_column_int(statement, 5);
  3336. int can_create_portfolio = sqlite3_column_int(statement, 6);
  3337. int can_delete_order = sqlite3_column_int(statement, 7);
  3338. int can_submit_order = sqlite3_column_int(statement, 8);
  3339. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  3340. int can_create_order = sqlite3_column_int(statement, 10);
  3341. char *mode = (char*)sqlite3_column_text(statement, 11);
  3342. if(mode==nil)
  3343. mode="";
  3344. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  3345. char *username = (char*)sqlite3_column_text(statement, 12);
  3346. if(username==nil)
  3347. username="";
  3348. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  3349. int can_update_contact_info = sqlite3_column_int(statement, 13);
  3350. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  3351. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  3352. [header setValue:nscontact_id forKey:@"contact_id"];
  3353. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  3354. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  3355. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  3356. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  3357. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  3358. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  3359. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  3360. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  3361. [header setObject:@(YES) forKey:@"can_create_backorder"];
  3362. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  3363. [header setValue:nsusername forKey:@"username"];
  3364. NSError* error=nil;
  3365. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  3366. [header setValue:statusFilter forKey:@"statusFilter"];
  3367. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3368. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  3369. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  3370. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  3371. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  3372. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  3373. [ret setObject:header forKey:@"header"];
  3374. [ret setValue:nsmode forKey:@"mode"];
  3375. }
  3376. sqlite3_finalize(statement);
  3377. }
  3378. [iSalesDB close_db:db];
  3379. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3380. return [RAConvertor dict2data:ret];
  3381. }
  3382. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  3383. {
  3384. NSString* contactId = [params valueForKey:@"contactId"];
  3385. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3386. sqlite3 *db = [iSalesDB get_db];
  3387. NSString *sqlQuery = nil;
  3388. {
  3389. 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];
  3390. }
  3391. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  3392. sqlite3_stmt * statement;
  3393. [ret setValue:@"2" forKey:@"result"];
  3394. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3395. {
  3396. //int i = 0;
  3397. if (sqlite3_step(statement) == SQLITE_ROW)
  3398. {
  3399. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3400. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3401. // int editable = sqlite3_column_int(statement, 0);
  3402. char *company_name = (char*)sqlite3_column_text(statement, 1);
  3403. NSString *nscompany_name =nil;
  3404. if(company_name==nil)
  3405. nscompany_name=@"";
  3406. else
  3407. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  3408. char *country = (char*)sqlite3_column_text(statement, 2);
  3409. if(country==nil)
  3410. country="";
  3411. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  3412. // char *addr = (char*)sqlite3_column_text(statement, 3);
  3413. // if(addr==nil)
  3414. // addr="";
  3415. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  3416. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  3417. if(zipcode==nil)
  3418. zipcode="";
  3419. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  3420. char *state = (char*)sqlite3_column_text(statement, 5);
  3421. if(state==nil)
  3422. state="";
  3423. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  3424. char *city = (char*)sqlite3_column_text(statement, 6);
  3425. if(city==nil)
  3426. city="";
  3427. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  3428. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  3429. // NSString *nscontact_name = nil;
  3430. // if(contact_name==nil)
  3431. // nscontact_name=@"";
  3432. // else
  3433. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  3434. char *phone = (char*)sqlite3_column_text(statement, 8);
  3435. NSString *nsphone = nil;
  3436. if(phone==nil)
  3437. nsphone=@"";
  3438. else
  3439. nsphone= [[NSString alloc]initWithUTF8String:phone];
  3440. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  3441. if(contact_id==nil)
  3442. contact_id="";
  3443. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3444. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  3445. if(addr_1==nil)
  3446. addr_1="";
  3447. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  3448. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  3449. if(addr_2==nil)
  3450. addr_2="";
  3451. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  3452. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  3453. if(addr_3==nil)
  3454. addr_3="";
  3455. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  3456. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  3457. if(addr_4==nil)
  3458. addr_4="";
  3459. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  3460. char *first_name = (char*)sqlite3_column_text(statement, 14);
  3461. if(first_name==nil)
  3462. first_name="";
  3463. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  3464. char *last_name = (char*)sqlite3_column_text(statement, 15);
  3465. if(last_name==nil)
  3466. last_name="";
  3467. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  3468. char *fax = (char*)sqlite3_column_text(statement, 16);
  3469. NSString *nsfax = nil;
  3470. if(fax==nil)
  3471. nsfax=@"";
  3472. else
  3473. nsfax= [[NSString alloc]initWithUTF8String:fax];
  3474. char *email = (char*)sqlite3_column_text(statement, 17);
  3475. NSString *nsemail = nil;
  3476. if(email==nil)
  3477. nsemail=@"";
  3478. else
  3479. nsemail= [[NSString alloc]initWithUTF8String:email];
  3480. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  3481. NSString *nsimg_0 = nil;
  3482. if(img_0==nil)
  3483. nsimg_0=@"";
  3484. else
  3485. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  3486. [self copy_bcardImg:nsimg_0];
  3487. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  3488. NSString *nsimg_1 = nil;
  3489. if(img_1==nil)
  3490. nsimg_1=@"";
  3491. else
  3492. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  3493. [self copy_bcardImg:nsimg_1];
  3494. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  3495. NSString *nsimg_2 = nil;
  3496. if(img_2==nil)
  3497. nsimg_2=@"";
  3498. else
  3499. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  3500. [self copy_bcardImg:nsimg_2];
  3501. char *price_type = (char*)sqlite3_column_text(statement, 21);
  3502. NSString *nsprice_type = nil;
  3503. if(price_type==nil)
  3504. nsprice_type=@"";
  3505. else
  3506. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  3507. char *notes = (char*)sqlite3_column_text(statement, 22);
  3508. NSString *nsnotes = nil;
  3509. if(notes==nil)
  3510. nsnotes=@"";
  3511. else
  3512. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  3513. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  3514. NSString *nssalesrep = nil;
  3515. if(salesrep==nil)
  3516. nssalesrep=@"";
  3517. else
  3518. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  3519. NSString *contact_type = [self textAtColumn:24 statement:statement];
  3520. {
  3521. // decrypt
  3522. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  3523. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  3524. nsphone=[AESCrypt fastdecrypt:nsphone];
  3525. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3526. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  3527. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3528. }
  3529. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3530. [arr_name addObject:nsfirst_name];
  3531. [arr_name addObject:nslast_name];
  3532. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  3533. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  3534. [arr_ext addObject:nsaddr_1];
  3535. [arr_ext addObject:nsaddr_2];
  3536. [arr_ext addObject:nsaddr_3];
  3537. [arr_ext addObject:nsaddr_4];
  3538. [arr_ext addObject:@"\r\n"];
  3539. [arr_ext addObject:nscity];
  3540. [arr_ext addObject:nsstate];
  3541. [arr_ext addObject:nszipcode];
  3542. [arr_ext addObject:nscountry];
  3543. NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  3544. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  3545. [item setValue:nsimg_2 forKey:@"business_card_2"];
  3546. [item setValue:nsimg_0 forKey:@"business_card_0"];
  3547. [item setValue:nscountry forKey:@"customer_country"];
  3548. [item setValue:nsphone forKey:@"customer_phone"];
  3549. [item setValue:nsimg_1 forKey:@"business_card_1"];
  3550. [item setValue:nscompany_name forKey:@"customer_name"];
  3551. [item setValue:nsprice_type forKey:@"customer_price_type"];
  3552. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  3553. [item setValue:nsext forKey:@"customer_contact_ext"];
  3554. [item setValue:nszipcode forKey:@"customer_zipcode"];
  3555. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  3556. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  3557. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  3558. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  3559. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  3560. [item setValue:nslast_name forKey:@"customer_last_name"];
  3561. [item setValue:nscity forKey:@"customer_city"];
  3562. [item setValue:nsstate forKey:@"customer_state"];
  3563. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  3564. [item setValue:contactId forKey:@"customer_cid"];
  3565. [item setValue:nscontact_name forKey:@"customer_contact"];
  3566. [item setValue:nsfax forKey:@"customer_fax"];
  3567. [item setValue:nsemail forKey:@"customer_email"];
  3568. [item setValue:contact_type forKey:@"customer_contact_type"];
  3569. [ret setObject:item forKey:@"customerInfo"];
  3570. // i++;
  3571. }
  3572. UIApplication * app = [UIApplication sharedApplication];
  3573. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3574. [ret setValue:appDelegate.mode forKey:@"mode"];
  3575. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  3576. sqlite3_finalize(statement);
  3577. }
  3578. [iSalesDB close_db:db];
  3579. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3580. return ret;
  3581. }
  3582. + (bool) copy_bcardImg:(NSString*) filename
  3583. {
  3584. if(filename.length==0)
  3585. return false;
  3586. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3587. bool ret=false;
  3588. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3589. NSString *cachefolder = [paths objectAtIndex:0];
  3590. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  3591. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  3592. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  3593. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  3594. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  3595. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  3596. //
  3597. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  3598. NSFileManager* fileManager = [NSFileManager defaultManager];
  3599. BOOL bdir=NO;
  3600. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  3601. {
  3602. NSError *error = nil;
  3603. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  3604. {
  3605. ret=false;
  3606. }
  3607. else
  3608. {
  3609. ret=true;
  3610. }
  3611. // NSError *error = nil;
  3612. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  3613. //
  3614. // if(!bsuccess)
  3615. // {
  3616. // DebugLog(@"Create offline_createimg folder failed");
  3617. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3618. // return [RAConvertor dict2data:ret];
  3619. // }
  3620. // if(bsuccess)
  3621. // {
  3622. // sqlite3 *db = [self get_db];
  3623. //
  3624. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  3625. // [iSalesDB close_db:db];
  3626. // }
  3627. }
  3628. return ret;
  3629. //
  3630. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  3631. // if(bsuccess)
  3632. // {
  3633. // NSError *error = nil;
  3634. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  3635. // {
  3636. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3637. // }
  3638. // else
  3639. // {
  3640. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  3641. //
  3642. // ret[@"img_url_aname"]=filename;
  3643. // ret[@"img_url"]=savedImagePath;
  3644. // }
  3645. // }
  3646. }
  3647. +(NSData *) offline_saveBusinesscard:(NSData *) image
  3648. {
  3649. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3650. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3651. NSString *cachefolder = [paths objectAtIndex:0];
  3652. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  3653. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  3654. NSFileManager* fileManager = [NSFileManager defaultManager];
  3655. BOOL bdir=YES;
  3656. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  3657. {
  3658. NSError *error = nil;
  3659. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  3660. if(!bsuccess)
  3661. {
  3662. DebugLog(@"Create offline_createimg folder failed");
  3663. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3664. return [RAConvertor dict2data:ret];
  3665. }
  3666. // if(bsuccess)
  3667. // {
  3668. // sqlite3 *db = [self get_db];
  3669. //
  3670. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  3671. // [iSalesDB close_db:db];
  3672. // }
  3673. }
  3674. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  3675. //JEPG格式
  3676. //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  3677. NSString* filename =[NSString stringWithFormat:@"%@.png",[[NSUUID UUID] UUIDString]];
  3678. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  3679. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  3680. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  3681. if(bsuccess)
  3682. {
  3683. NSError *error = nil;
  3684. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  3685. {
  3686. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3687. }
  3688. else
  3689. {
  3690. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  3691. ret[@"img_url_aname"]=filename;
  3692. ret[@"img_url"]=filename;
  3693. }
  3694. }
  3695. else
  3696. {
  3697. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3698. }
  3699. return [RAConvertor dict2data:ret];
  3700. }
  3701. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  3702. {
  3703. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  3704. category = [category substringToIndex:3];
  3705. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3706. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  3707. params[@"category"]= category;
  3708. ret[@"params"]= params;
  3709. [ret setValue:@"detail" forKey:@"target"];
  3710. [ret setValue:@"popup" forKey:@"action"];
  3711. [ret setValue:@"content" forKey:@"type"];
  3712. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  3713. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  3714. [ret setValue:@"true" forKey:@"single_row"];
  3715. [ret setValue:@"true" forKey:@"partial_refresh"];
  3716. // sqlite3 *db = [iSalesDB get_db];
  3717. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  3718. 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 ;
  3719. sqlite3_stmt * statement;
  3720. int count = 0;
  3721. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  3722. // int count=0;
  3723. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3724. {
  3725. int i=0;
  3726. while (sqlite3_step(statement) == SQLITE_ROW)
  3727. {
  3728. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3729. // char *name = (char*)sqlite3_column_text(statement, 1);
  3730. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3731. char *name = (char*)sqlite3_column_text(statement, 0);
  3732. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3733. int product_id = sqlite3_column_int(statement, 1);
  3734. char *url = (char*)sqlite3_column_text(statement, 2);
  3735. if(url==nil)
  3736. url="";
  3737. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3738. [item setValue:nsurl forKey:@"picture_path"];
  3739. [item setValue:nsname forKey:@"fash_name"];
  3740. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3741. [item setValue:category forKey:@"category"];
  3742. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3743. i++;
  3744. }
  3745. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3746. sqlite3_finalize(statement);
  3747. }
  3748. DebugLog(@"count:%d",count);
  3749. // [iSalesDB close_db:db];
  3750. return ret;
  3751. }
  3752. +(NSDictionary*) model_priceKvItem:(NSString*) key value:(NSString*)value
  3753. {
  3754. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3755. [ret setValue:key forKey:@"key"];
  3756. [ret setValue:value forKey:@"val"];
  3757. [ret setValue:@"price" forKey:@"type"];
  3758. return ret;
  3759. }
  3760. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  3761. {
  3762. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3763. [ret setValue:key forKey:@"key"];
  3764. [ret setValue:value forKey:@"val"];
  3765. return ret;
  3766. }
  3767. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  3768. {
  3769. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3770. [ret setValue:@"0" forKey:@"img_count"];
  3771. // sqlite3 *db = [iSalesDB get_db];
  3772. 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 ;
  3773. sqlite3_stmt * statement;
  3774. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3775. {
  3776. int i=0;
  3777. if (sqlite3_step(statement) == SQLITE_ROW)
  3778. {
  3779. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3780. // char *name = (char*)sqlite3_column_text(statement, 1);
  3781. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3782. char *value = (char*)sqlite3_column_text(statement, 0);
  3783. if(value==nil)
  3784. value="";
  3785. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  3786. char *key = (char*)sqlite3_column_text(statement, 1);
  3787. if(key==nil)
  3788. key="";
  3789. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  3790. [item setValue:nsvalue forKey:@"val"];
  3791. [item setValue:nskey forKey:@"key"];
  3792. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3793. [ret setValue:@"1" forKey:@"count"];
  3794. i++;
  3795. }
  3796. sqlite3_finalize(statement);
  3797. }
  3798. // [iSalesDB close_db:db];
  3799. return ret;
  3800. }
  3801. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  3802. {
  3803. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3804. [ret setValue:@"0" forKey:@"count"];
  3805. // sqlite3 *db = [iSalesDB get_db];
  3806. 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;
  3807. sqlite3_stmt * statement;
  3808. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3809. {
  3810. int i=0;
  3811. while (sqlite3_step(statement) == SQLITE_ROW)
  3812. {
  3813. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3814. // char *name = (char*)sqlite3_column_text(statement, 1);
  3815. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3816. char *value = (char*)sqlite3_column_text(statement, 0);
  3817. if(value==nil)
  3818. value="";
  3819. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  3820. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  3821. if(selector_display==nil)
  3822. selector_display="";
  3823. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  3824. int product_id = sqlite3_column_int(statement, 2);
  3825. char *category = (char*)sqlite3_column_text(statement, 3);
  3826. if(category==nil)
  3827. category="";
  3828. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  3829. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  3830. [item setValue:nsvalue forKey:@"title"];
  3831. [item setValue:url forKey:@"pic_url"];
  3832. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  3833. [params setValue:@"2" forKey:@"count"];
  3834. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  3835. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  3836. [param0 setValue:@"product_id" forKey:@"name"];
  3837. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  3838. [param1 setValue:nscategory forKey:@"val"];
  3839. [param1 setValue:@"category" forKey:@"name"];
  3840. [params setObject:param0 forKey:@"param_0"];
  3841. [params setObject:param1 forKey:@"param_1"];
  3842. [item setObject:params forKey:@"params"];
  3843. [ret setValue:nsselector_display forKey:@"name"];
  3844. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3845. i++;
  3846. }
  3847. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3848. [ret setValue:@"switch" forKey:@"action"];
  3849. sqlite3_finalize(statement);
  3850. }
  3851. // [iSalesDB close_db:db];
  3852. return ret;
  3853. }
  3854. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  3855. {
  3856. // model 在 category search 显示的图片。
  3857. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  3858. // sqlite3 *db = [iSalesDB get_db];
  3859. 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];
  3860. sqlite3_stmt * statement;
  3861. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3862. {
  3863. while (sqlite3_step(statement) == SQLITE_ROW)
  3864. {
  3865. char *url = (char*)sqlite3_column_text(statement, 0);
  3866. if(url==nil)
  3867. url="";
  3868. int type = sqlite3_column_int(statement, 1);
  3869. if(type==0)
  3870. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  3871. else
  3872. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  3873. }
  3874. sqlite3_finalize(statement);
  3875. }
  3876. // [iSalesDB close_db:db];
  3877. return ret;
  3878. }
  3879. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  3880. {
  3881. int item_id=-1;
  3882. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  3883. sqlite3_stmt * statement;
  3884. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3885. {
  3886. if (sqlite3_step(statement) == SQLITE_ROW)
  3887. {
  3888. item_id = sqlite3_column_int(statement, 0);
  3889. }
  3890. sqlite3_finalize(statement);
  3891. }
  3892. return item_id;
  3893. }
  3894. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  3895. {
  3896. // NSString* ret = @"";
  3897. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  3898. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  3899. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  3900. sqlite3_stmt * statement;
  3901. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3902. {
  3903. while (sqlite3_step(statement) == SQLITE_ROW)
  3904. {
  3905. int bitem_id = sqlite3_column_int(statement, 0);
  3906. int bitem_qty = sqlite3_column_int(statement, 1);
  3907. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  3908. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  3909. }
  3910. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  3911. sqlite3_finalize(statement);
  3912. }
  3913. // if(ret==nil)
  3914. // ret=@"";
  3915. *count=arr_count;
  3916. return arr_bundle;
  3917. }
  3918. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  3919. {
  3920. // get default sold qty, return -1 if model not found;
  3921. int ret = -1;
  3922. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  3923. sqlite3_stmt * statement;
  3924. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3925. {
  3926. if (sqlite3_step(statement) == SQLITE_ROW)
  3927. {
  3928. ret = sqlite3_column_int(statement, 0);
  3929. }
  3930. sqlite3_finalize(statement);
  3931. }
  3932. return ret;
  3933. }
  3934. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  3935. {
  3936. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3937. [ret setValue:@"0" forKey:@"img_count"];
  3938. // sqlite3 *db = [iSalesDB get_db];
  3939. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  3940. sqlite3_stmt * statement;
  3941. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3942. {
  3943. int i=0;
  3944. while (sqlite3_step(statement) == SQLITE_ROW)
  3945. {
  3946. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3947. // char *name = (char*)sqlite3_column_text(statement, 1);
  3948. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3949. char *url = (char*)sqlite3_column_text(statement, 0);
  3950. if(url==nil)
  3951. url="";
  3952. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3953. [item setValue:nsurl forKey:@"s"];
  3954. [item setValue:nsurl forKey:@"l"];
  3955. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  3956. i++;
  3957. }
  3958. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  3959. sqlite3_finalize(statement);
  3960. }
  3961. // [iSalesDB close_db:db];
  3962. return ret;
  3963. }
  3964. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  3965. UIApplication * app = [UIApplication sharedApplication];
  3966. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3967. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  3968. sqlite3 *db = [iSalesDB get_db];
  3969. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  3970. NSString* product_id=params[@"product_id"];
  3971. NSString *item_count_str = params[@"item_count"];
  3972. NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  3973. NSArray *item_count_arr = item_count_str ? [RAUtils string2arr:item_count_str separator:@","] : nil;
  3974. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  3975. // NSString *sql = @"";
  3976. for(int i=0;i<arr.count;i++)
  3977. {
  3978. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  3979. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  3980. __block int cart_count = 0;
  3981. if (!item_count_str) {
  3982. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  3983. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3984. NSString *model_set = [self textAtColumn:0 statement:stmt];
  3985. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  3986. cart_count = [[model_set_components lastObject] integerValue];
  3987. }];
  3988. }
  3989. if(count==0)
  3990. {
  3991. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  3992. sqlite3_stmt *stmt;
  3993. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  3994. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],[arr[i] length],NULL);
  3995. if (item_count_arr) {
  3996. sqlite3_bind_int(stmt,2,[item_count_arr[i] integerValue]);
  3997. } else {
  3998. sqlite3_bind_int(stmt,2,cart_count);
  3999. }
  4000. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  4001. [iSalesDB execSql:@"ROLLBACK" db:db];
  4002. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  4003. [iSalesDB close_db:db];
  4004. DebugLog(@"add to wishlist error");
  4005. return ret;
  4006. }
  4007. } else {
  4008. int qty = 0;
  4009. if (item_count_arr) {
  4010. qty = [item_count_arr[i] integerValue];
  4011. } else {
  4012. qty = cart_count;
  4013. }
  4014. 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]];
  4015. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  4016. [iSalesDB execSql:@"ROLLBACK" db:db];
  4017. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  4018. [iSalesDB close_db:db];
  4019. DebugLog(@"add to wishlist error");
  4020. return ret;
  4021. }
  4022. }
  4023. }
  4024. // [iSalesDB execSql:sql db:db];
  4025. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4026. [iSalesDB execSql:@"END TRANSACTION" db:db];
  4027. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  4028. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  4029. [iSalesDB close_db:db];
  4030. appDelegate.wish_count =count;
  4031. [appDelegate update_count_mark];
  4032. ret[@"result"]= [NSNumber numberWithInt:2];
  4033. return ret;
  4034. }
  4035. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  4036. // 0 category
  4037. // 1 search
  4038. // 2 itemsearch
  4039. NSData *ret = nil;
  4040. NSDictionary *items = nil;
  4041. switch (from) {
  4042. case 0:{
  4043. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  4044. }
  4045. break;
  4046. case 1:{
  4047. items = [[self search:params limited:NO] objectForKey:@"items"];
  4048. }
  4049. break;
  4050. case 2:{
  4051. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  4052. }
  4053. break;
  4054. default:
  4055. break;
  4056. }
  4057. if (!items) {
  4058. return ret;
  4059. }
  4060. int count = [[items objectForKey:@"count"] intValue];
  4061. NSMutableString *product_id_str = [@"" mutableCopy];
  4062. for (int i = 0; i < count; i++) {
  4063. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  4064. NSDictionary *item = [items objectForKey:key];
  4065. NSString *product_id = [item objectForKey:@"product_id"];
  4066. if (i == 0) {
  4067. [product_id_str appendString:product_id];
  4068. } else {
  4069. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  4070. }
  4071. }
  4072. NSString *add_to = [self valueInParams:params key:@"addTo"];
  4073. if ([add_to isEqualToString:@"cart"]) {
  4074. NSString *order_code = [params objectForKey:@"orderCode"];
  4075. if (order_code.length) {
  4076. NSDictionary *newParams = @{
  4077. @"product_id" : product_id_str,
  4078. @"orderCode" : order_code
  4079. };
  4080. ret = [self offline_add2cart:newParams.mutableCopy];
  4081. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4082. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4083. ret = [RAConvertor dict2data:retDic];
  4084. }
  4085. } else if([add_to isEqualToString:@"wishlist"]) {
  4086. NSDictionary *newParams = @{
  4087. @"product_id" : product_id_str
  4088. };
  4089. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  4090. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4091. ret = [RAConvertor dict2data:retDic];
  4092. } else if([add_to isEqualToString:@"portfolio"]) {
  4093. NSDictionary *newParams = @{
  4094. @"product_id" : product_id_str
  4095. };
  4096. NSData *data = [self offline_add2Portfolio:newParams.mutableCopy];
  4097. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4098. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4099. ret = [RAConvertor dict2data:retDic];
  4100. }
  4101. return ret;
  4102. }
  4103. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  4104. {
  4105. return [self addAll:params from:0];
  4106. }
  4107. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  4108. {
  4109. return [self addAll:params from:1];
  4110. }
  4111. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  4112. {
  4113. return [self addAll:params from:2];
  4114. }
  4115. #pragma mark - Jack
  4116. #warning 做SQL操作时转义!!
  4117. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  4118. // "val_227" : {
  4119. // "check" : 1,
  4120. // "value" : "US United States",
  4121. // "value_id" : "228"
  4122. // },
  4123. if (!countryCode) {
  4124. countryCode = @"US";
  4125. }
  4126. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  4127. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  4128. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  4129. int code_id = sqlite3_column_int(stmt, 3); // id
  4130. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4131. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  4132. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  4133. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4134. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  4135. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4136. }
  4137. long n = *count;
  4138. *count = n + 1;
  4139. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  4140. [container setValue:countryDic forKey:key];
  4141. }] mutableCopy];
  4142. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4143. return ret;
  4144. }
  4145. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  4146. countryCode = [self translateSingleQuote:countryCode];
  4147. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  4148. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4149. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  4150. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  4151. if (name == NULL) {
  4152. name = "";
  4153. }
  4154. if (code == NULL) {
  4155. code = "";
  4156. }
  4157. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4158. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4159. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  4160. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4161. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  4162. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4163. }
  4164. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4165. [container setValue:stateDic forKey:key];
  4166. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  4167. DebugLog(@"query all state error: %@",err_msg);
  4168. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4169. // [stateDic setValue:@"Other" forKey:@"value"];
  4170. // [stateDic setValue:@"" forKey:@"value_id"];
  4171. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4172. //
  4173. // if (state_code && [@"" isEqualToString:state_code]) {
  4174. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4175. // }
  4176. //
  4177. // NSString *key = [NSString stringWithFormat:@"val_0"];
  4178. // [container setValue:stateDic forKey:key];
  4179. }] mutableCopy];
  4180. [ret removeObjectForKey:@"result"];
  4181. // failure 可以不用了,一样的
  4182. if (ret.allKeys.count == 0) {
  4183. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4184. [stateDic setValue:@"Other" forKey:@"value"];
  4185. [stateDic setValue:@"" forKey:@"value_id"];
  4186. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4187. if (state_code && [@"" isEqualToString:state_code]) {
  4188. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4189. }
  4190. NSString *key = [NSString stringWithFormat:@"val_0"];
  4191. [ret setValue:stateDic forKey:key];
  4192. }
  4193. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4194. return ret;
  4195. }
  4196. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  4197. codeId = [self translateSingleQuote:codeId];
  4198. 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];
  4199. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4200. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  4201. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  4202. if (name == NULL) {
  4203. name = "";
  4204. }
  4205. if (code == NULL) {
  4206. code = "";
  4207. }
  4208. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4209. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4210. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  4211. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4212. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  4213. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4214. }
  4215. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4216. [container setValue:stateDic forKey:key];
  4217. }] mutableCopy];
  4218. [ret removeObjectForKey:@"result"];
  4219. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4220. return ret;
  4221. }
  4222. + (NSDictionary *)offline_getPrice {
  4223. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price where is_show = 1;"];
  4224. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4225. char *name = (char *) sqlite3_column_text(stmt, 1);
  4226. int type = sqlite3_column_int(stmt, 2);
  4227. int orderBy = sqlite3_column_int(stmt, 3);
  4228. if (name == NULL) {
  4229. name = "";
  4230. }
  4231. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  4232. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4233. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  4234. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4235. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4236. if (orderBy == 0) {
  4237. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4238. }
  4239. [container setValue:priceDic forKey:key];
  4240. }] mutableCopy];
  4241. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4242. return ret;
  4243. }
  4244. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  4245. NSString *sql = @"select _id,type_name from contact_type where is_show = 1";
  4246. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  4247. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  4248. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4249. int _id = sqlite3_column_int(stmt, 0);
  4250. NSString *name = [self textAtColumn:1 statement:stmt];
  4251. NSDictionary *typeDic = @{
  4252. @"value_id" : name,
  4253. @"value" : name,
  4254. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  4255. };
  4256. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  4257. [ret setValue:[NSNumber numberWithInt:++(*count)] forKey:@"count"];
  4258. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  4259. }];
  4260. return ret;
  4261. }
  4262. + (NSDictionary *)offline_getSalesRep {
  4263. // 首先从offline_login表中取出sales_code
  4264. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4265. NSString *user = app.user;
  4266. user = [self translateSingleQuote:user];
  4267. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  4268. __block NSString *user_code = @"";
  4269. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4270. char *code = (char *)sqlite3_column_text(stmt, 0);
  4271. if (code == NULL) {
  4272. code = "";
  4273. }
  4274. user_code = [NSString stringWithUTF8String:code];
  4275. }];
  4276. // 再取所有salesRep
  4277. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  4278. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4279. // 1 name 2 code 3 salesrep_id
  4280. char *name = (char *)sqlite3_column_text(stmt, 1);
  4281. char *code = (char *)sqlite3_column_text(stmt, 2);
  4282. int salesrep_id = sqlite3_column_int(stmt, 3);
  4283. if (name == NULL) {
  4284. name = "";
  4285. }
  4286. if (code == NULL) {
  4287. code = "";
  4288. }
  4289. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  4290. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  4291. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  4292. // 比较code 相等则check
  4293. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  4294. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4295. }
  4296. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  4297. }] mutableCopy];
  4298. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4299. return ret;
  4300. }
  4301. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  4302. zipcode = [self translateSingleQuote:zipcode];
  4303. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  4304. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4305. char *country = (char *)sqlite3_column_text(stmt, 0);
  4306. char *state = (char *)sqlite3_column_text(stmt, 1);
  4307. char *city = (char *)sqlite3_column_text(stmt, 2);
  4308. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  4309. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  4310. if (country == NULL) {
  4311. country = "";
  4312. }
  4313. if (state == NULL) {
  4314. state = "";
  4315. }
  4316. if (city == NULL) {
  4317. city = "";
  4318. }
  4319. if (country_code == NULL) {
  4320. country_code = "";
  4321. }
  4322. if (state_code == NULL) {
  4323. state_code = "";
  4324. }
  4325. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  4326. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  4327. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  4328. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  4329. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  4330. }] mutableCopy];
  4331. return ret;
  4332. }
  4333. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  4334. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  4335. [item setValue:value forKey:valueKey];
  4336. [dic setValue:item forKey:itemKey];
  4337. }
  4338. + (NSString *)countryCodeByid:(NSString *)code_id {
  4339. NSString *ret = nil;
  4340. code_id = [self translateSingleQuote:code_id];
  4341. sqlite3 *db = [iSalesDB get_db];
  4342. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  4343. sqlite3_stmt * statement;
  4344. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4345. while (sqlite3_step(statement) == SQLITE_ROW) {
  4346. char *code = (char *)sqlite3_column_text(statement, 0);
  4347. if (code == NULL) {
  4348. code = "";
  4349. }
  4350. ret = [NSString stringWithUTF8String:code];
  4351. }
  4352. sqlite3_finalize(statement);
  4353. }
  4354. [iSalesDB close_db:db];
  4355. return ret;
  4356. }
  4357. + (NSString *)countryCodeIdByCode:(NSString *)code {
  4358. NSString *ret = nil;
  4359. code = [self translateSingleQuote:code];
  4360. sqlite3 *db = [iSalesDB get_db];
  4361. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  4362. sqlite3_stmt * statement;
  4363. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4364. while (sqlite3_step(statement) == SQLITE_ROW) {
  4365. char *_id = (char *)sqlite3_column_text(statement, 0);
  4366. if (_id == NULL) {
  4367. _id = "";
  4368. }
  4369. ret = [NSString stringWithFormat:@"%s",_id];
  4370. }
  4371. sqlite3_finalize(statement);
  4372. }
  4373. [iSalesDB close_db:db];
  4374. return ret;
  4375. }
  4376. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  4377. NSString *name = nil;
  4378. codeId = [self translateSingleQuote:codeId];
  4379. sqlite3 *db = [iSalesDB get_db];
  4380. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  4381. sqlite3_stmt * statement;
  4382. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4383. while (sqlite3_step(statement) == SQLITE_ROW) {
  4384. char *value = (char *)sqlite3_column_text(statement, 0);
  4385. if (value == NULL) {
  4386. value = "";
  4387. }
  4388. name = [NSString stringWithUTF8String:value];
  4389. }
  4390. sqlite3_finalize(statement);
  4391. }
  4392. [iSalesDB close_db:db];
  4393. return name;
  4394. }
  4395. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  4396. NSString *ret = nil;
  4397. sqlite3 *db = [iSalesDB get_db];
  4398. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  4399. sqlite3_stmt * statement;
  4400. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4401. while (sqlite3_step(statement) == SQLITE_ROW) {
  4402. char *name = (char *)sqlite3_column_text(statement, 0);
  4403. if (name == NULL) {
  4404. name = "";
  4405. }
  4406. ret = [NSString stringWithUTF8String:name];
  4407. }
  4408. sqlite3_finalize(statement);
  4409. }
  4410. [iSalesDB close_db:db];
  4411. return ret;
  4412. }
  4413. + (NSString *)salesRepCodeById:(NSString *)_id {
  4414. NSString *ret = nil;
  4415. _id = [self translateSingleQuote:_id];
  4416. sqlite3 *db = [iSalesDB get_db];
  4417. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  4418. sqlite3_stmt * statement;
  4419. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4420. while (sqlite3_step(statement) == SQLITE_ROW) {
  4421. char *rep = (char *)sqlite3_column_text(statement, 0);
  4422. if (rep == NULL) {
  4423. rep = "";
  4424. }
  4425. ret = [NSString stringWithUTF8String:rep];
  4426. }
  4427. sqlite3_finalize(statement);
  4428. }
  4429. [iSalesDB close_db:db];
  4430. return ret;
  4431. }
  4432. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  4433. char *tx = (char *)sqlite3_column_text(stmt, col);
  4434. if (tx == NULL) {
  4435. tx = "";
  4436. }
  4437. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  4438. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  4439. if (!text) {
  4440. text = @"";
  4441. }
  4442. // 将字符全部为' '的字符串干掉
  4443. int spaceCount = 0;
  4444. for (int i = 0; i < text.length; i++) {
  4445. if ([text characterAtIndex:i] == ' ') {
  4446. spaceCount++;
  4447. }
  4448. }
  4449. if (spaceCount == text.length) {
  4450. text = @"";
  4451. }
  4452. return text;
  4453. }
  4454. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  4455. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  4456. NSData *data = [NSData dataWithContentsOfFile:path];
  4457. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4458. return ret;
  4459. }
  4460. + (NSString *)textFileName:(NSString *)name {
  4461. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  4462. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  4463. if (!text) {
  4464. text = @"";
  4465. }
  4466. return text;
  4467. }
  4468. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  4469. return [[dic objectForKey:key] mutableCopy];
  4470. }
  4471. + (id)translateSingleQuote:(NSString *)string {
  4472. if ([string isKindOfClass:[NSString class]])
  4473. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4474. return string;
  4475. }
  4476. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  4477. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  4478. }
  4479. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  4480. NSString* ret= nil;
  4481. NSString *sqlQuery = nil;
  4482. // 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
  4483. 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];
  4484. sqlite3_stmt * statement;
  4485. // int count=0;
  4486. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4487. {
  4488. if (sqlite3_step(statement) == SQLITE_ROW)
  4489. {
  4490. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  4491. if(imgurl==nil)
  4492. imgurl="";
  4493. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  4494. ret=nsimgurl;
  4495. }
  4496. sqlite3_finalize(statement);
  4497. }
  4498. else
  4499. {
  4500. [ret setValue:@"8" forKey:@"result"];
  4501. }
  4502. // [iSalesDB close_db:db];
  4503. // DebugLog(@"data string: %@",ret );
  4504. return ret;
  4505. }
  4506. #pragma mark contact list
  4507. +(NSDictionary*) offline_contactlist :(NSMutableDictionary *) params
  4508. {
  4509. // contactType = "Sales_Order_Customer";
  4510. // limit = 25;
  4511. // offset = 0;
  4512. // password = 123456;
  4513. // "price_name" = 16;
  4514. // user = EvanK;
  4515. sqlite3 *db = [iSalesDB get_db];
  4516. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  4517. if (contactType) {
  4518. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  4519. } else {
  4520. contactType = @"1 = 1";
  4521. }
  4522. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  4523. DebugLog(@"offline contact list keyword: %@",keyword);
  4524. // advanced search
  4525. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  4526. if (contact_name) {
  4527. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4528. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  4529. } else {
  4530. contact_name = @"";
  4531. }
  4532. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  4533. if (customer_phone) {
  4534. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4535. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  4536. } else {
  4537. customer_phone = @"";
  4538. }
  4539. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  4540. if (customer_fax) {
  4541. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4542. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  4543. } else {
  4544. customer_fax = @"";
  4545. }
  4546. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  4547. if (customer_zipcode) {
  4548. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4549. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  4550. } else {
  4551. customer_zipcode = @"";
  4552. }
  4553. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  4554. if (customer_sales_rep) {
  4555. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4556. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  4557. } else {
  4558. customer_sales_rep = @"";
  4559. }
  4560. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  4561. if (customer_state) {
  4562. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4563. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  4564. } else {
  4565. customer_state = @"";
  4566. }
  4567. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  4568. if (customer_name) {
  4569. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4570. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  4571. } else {
  4572. customer_name = @"";
  4573. }
  4574. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  4575. if (customer_country) {
  4576. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4577. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  4578. } else {
  4579. customer_country = @"";
  4580. }
  4581. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  4582. if (customer_cid) {
  4583. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4584. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  4585. } else {
  4586. customer_cid = @"";
  4587. }
  4588. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  4589. if (customer_city) {
  4590. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4591. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  4592. } else {
  4593. customer_city = @"";
  4594. }
  4595. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  4596. if (customer_address) {
  4597. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4598. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  4599. } else {
  4600. customer_address = @"";
  4601. }
  4602. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  4603. if (customer_email) {
  4604. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4605. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  4606. } else {
  4607. customer_email = @"";
  4608. }
  4609. NSString *price_name = [params valueForKey:@"price_name"];
  4610. if (price_name) {
  4611. if ([price_name containsString:@","]) {
  4612. // 首先从 price表中查处name
  4613. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  4614. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  4615. for (int i = 1;i < pArray.count;i++) {
  4616. NSString *p = pArray[i];
  4617. [mutablePStr appendFormat:@" or type = %@ ",p];
  4618. }
  4619. [mutablePStr appendString:@";"];
  4620. __block NSMutableArray *price_name_array = [NSMutableArray array];
  4621. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4622. char *name = (char *)sqlite3_column_text(stmt, 0);
  4623. if (!name)
  4624. name = "";
  4625. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  4626. }];
  4627. // 再根据name 拼sql
  4628. NSMutableString *mutable_price_name = [NSMutableString string];
  4629. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  4630. for (int i = 1; i < price_name_array.count; i++) {
  4631. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  4632. }
  4633. [mutable_price_name appendString:@")"];
  4634. price_name = mutable_price_name;
  4635. } else {
  4636. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  4637. if ([price_name isEqualToString:@""]) {
  4638. price_name = @"";
  4639. } else {
  4640. __block NSString *price;
  4641. price_name = [self translateSingleQuote:price_name];
  4642. [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) {
  4643. char *p = (char *)sqlite3_column_text(stmt, 0);
  4644. if (p == NULL) {
  4645. p = "";
  4646. }
  4647. price = [NSString stringWithUTF8String:p];
  4648. }];
  4649. if ([price isEqualToString:@""]) {
  4650. price_name = @"";
  4651. } else {
  4652. price = [self translateSingleQuote:price];
  4653. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  4654. }
  4655. }
  4656. }
  4657. } else {
  4658. price_name = @"";
  4659. }
  4660. int limit = [[params valueForKey:@"limit"] intValue];
  4661. int offset = [[params valueForKey:@"offset"] intValue];
  4662. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4663. 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];
  4664. 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];
  4665. // int result= [iSalesDB AddExFunction:db];
  4666. int count =0;
  4667. NSString *sqlQuery = nil;
  4668. if(keyword.length==0)
  4669. {
  4670. // 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];
  4671. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  4672. sqlQuery = sql;
  4673. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  4674. }
  4675. else
  4676. {
  4677. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  4678. keyword = keyword.lowercaseString;
  4679. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4680. 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];
  4681. 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]];
  4682. }
  4683. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  4684. sqlite3_stmt * statement;
  4685. [ret setValue:@"2" forKey:@"result"];
  4686. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  4687. // 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";
  4688. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4689. {
  4690. int i = 0;
  4691. while (sqlite3_step(statement) == SQLITE_ROW)
  4692. {
  4693. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4694. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4695. int editable = sqlite3_column_int(statement, 0);
  4696. char *company_name = (char*)sqlite3_column_text(statement, 1);
  4697. NSString *nscompany_name =nil;
  4698. if(company_name==nil)
  4699. nscompany_name=@"";
  4700. else
  4701. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  4702. char *country = (char*)sqlite3_column_text(statement, 2);
  4703. if(country==nil)
  4704. country="";
  4705. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  4706. // char *addr = (char*)sqlite3_column_text(statement, 3);
  4707. // if(addr==nil)
  4708. // addr="";
  4709. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  4710. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  4711. if(zipcode==nil)
  4712. zipcode="";
  4713. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  4714. char *state = (char*)sqlite3_column_text(statement, 5);
  4715. if(state==nil)
  4716. state="";
  4717. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  4718. char *city = (char*)sqlite3_column_text(statement, 6);
  4719. if(city==nil)
  4720. city="";
  4721. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  4722. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  4723. // NSString *nscontact_name = nil;
  4724. // if(contact_name==nil)
  4725. // nscontact_name=@"";
  4726. // else
  4727. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  4728. char *phone = (char*)sqlite3_column_text(statement, 8);
  4729. NSString *nsphone = nil;
  4730. if(phone==nil)
  4731. nsphone=@"";
  4732. else
  4733. nsphone= [[NSString alloc]initWithUTF8String:phone];
  4734. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  4735. if(contact_id==nil)
  4736. contact_id="";
  4737. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4738. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  4739. if(addr_1==nil)
  4740. addr_1="";
  4741. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4742. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  4743. if(addr_2==nil)
  4744. addr_2="";
  4745. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4746. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  4747. if(addr_3==nil)
  4748. addr_3="";
  4749. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4750. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  4751. if(addr_4==nil)
  4752. addr_4="";
  4753. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4754. char *first_name = (char*)sqlite3_column_text(statement, 14);
  4755. if(first_name==nil)
  4756. first_name="";
  4757. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  4758. char *last_name = (char*)sqlite3_column_text(statement, 15);
  4759. if(last_name==nil)
  4760. last_name="";
  4761. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  4762. char *fax = (char*)sqlite3_column_text(statement, 16);
  4763. NSString *nsfax = nil;
  4764. if(fax==nil)
  4765. nsfax=@"";
  4766. else
  4767. {
  4768. nsfax= [[NSString alloc]initWithUTF8String:fax];
  4769. if(nsfax.length>0)
  4770. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  4771. }
  4772. char *email = (char*)sqlite3_column_text(statement, 17);
  4773. NSString *nsemail = nil;
  4774. if(email==nil)
  4775. nsemail=@"";
  4776. else
  4777. {
  4778. nsemail= [[NSString alloc]initWithUTF8String:email];
  4779. if(nsemail.length>0)
  4780. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  4781. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  4782. }
  4783. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4784. [arr_name addObject:nsfirst_name];
  4785. [arr_name addObject:nslast_name];
  4786. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  4787. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  4788. {
  4789. // decrypt
  4790. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  4791. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  4792. nsphone=[AESCrypt fastdecrypt:nsphone];
  4793. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4794. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  4795. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4796. }
  4797. [arr_addr addObject:nscompany_name];
  4798. [arr_addr addObject:nscontact_name];
  4799. [arr_addr addObject:@"<br>"];
  4800. [arr_addr addObject:nsaddr_1];
  4801. [arr_addr addObject:nsaddr_2];
  4802. [arr_addr addObject:nsaddr_3];
  4803. [arr_addr addObject:nsaddr_4];
  4804. //[arr_addr addObject:nsaddr];
  4805. [arr_addr addObject:nszipcode];
  4806. [arr_addr addObject:nscity];
  4807. [arr_addr addObject:nsstate];
  4808. [arr_addr addObject:nscountry];
  4809. [arr_addr addObject:@"<br>"];
  4810. [arr_addr addObject:nsphone];
  4811. [arr_addr addObject:nsfax];
  4812. [arr_addr addObject:nsemail];
  4813. NSString * name = [RAConvertor arr2string:arr_addr separator:@", " trim:true];
  4814. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  4815. [item setValue:name forKey:@"name"];
  4816. [item setValue:nscontact_id forKey:@"contact_id"];
  4817. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  4818. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4819. i++;
  4820. }
  4821. UIApplication * app = [UIApplication sharedApplication];
  4822. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4823. [ret setValue:appDelegate.mode forKey:@"mode"];
  4824. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  4825. sqlite3_finalize(statement);
  4826. }
  4827. [iSalesDB close_db:db];
  4828. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4829. return ret;
  4830. }
  4831. #pragma mark contact Advanced search
  4832. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  4833. {
  4834. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  4835. return [RAConvertor dict2data:contactAdvanceDic];
  4836. }
  4837. #pragma mark create new contact
  4838. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  4839. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  4840. NSData *data = [NSData dataWithContentsOfFile:path];
  4841. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4842. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  4843. NSString *countryCode = nil;
  4844. NSString *countryCode_id = nil;
  4845. NSString *stateCode = nil;
  4846. NSString *city = nil;
  4847. NSString *zipCode = nil;
  4848. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  4849. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  4850. NSString *code_id = params[@"country"];
  4851. countryCode_id = code_id;
  4852. countryCode = [self countryCodeByid:code_id];
  4853. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  4854. NSString *zip_code = params[@"zipcode"];
  4855. // 剔除全部为空格
  4856. int spaceCount = 0;
  4857. for (int i = 0; i < zip_code.length; i++) {
  4858. if ([zip_code characterAtIndex:i] == ' ') {
  4859. spaceCount++;
  4860. }
  4861. }
  4862. if (spaceCount == zip_code.length) {
  4863. zip_code = @"";
  4864. }
  4865. zipCode = zip_code;
  4866. if (zipCode.length > 0) {
  4867. countryCode_id = params[@"country"];
  4868. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  4869. countryCode = [dic valueForKey:@"country_code"];
  4870. if (!countryCode) {
  4871. // countryCode = @"US";
  4872. NSString *code_id = params[@"country"];
  4873. countryCode = [self countryCodeByid:code_id];
  4874. }
  4875. stateCode = [dic valueForKey:@"state_code"];
  4876. if (!stateCode.length) {
  4877. stateCode = params[@"state"];
  4878. }
  4879. city = [dic valueForKey:@"city"];
  4880. if (!city.length) {
  4881. city = params[@"city"];
  4882. }
  4883. // zip code
  4884. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  4885. [zipDic setValue:zipCode forKey:@"value"];
  4886. [section_0 setValue:zipDic forKey:@"item_11"];
  4887. } else {
  4888. NSString *code_id = params[@"country"];
  4889. countryCode = [self countryCodeByid:code_id];
  4890. stateCode = params[@"state"];
  4891. city = params[@"city"];
  4892. }
  4893. }
  4894. } else {
  4895. // default: US United States
  4896. countryCode = @"US";
  4897. countryCode_id = @"228";
  4898. }
  4899. // country
  4900. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  4901. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  4902. // state
  4903. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  4904. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  4905. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  4906. // city
  4907. if (city) {
  4908. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  4909. [cityDic setValue:city forKey:@"value"];
  4910. [section_0 setValue:cityDic forKey:@"item_13"];
  4911. }
  4912. // price type
  4913. NSDictionary *priceDic = [self offline_getPrice];
  4914. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  4915. // contact type
  4916. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  4917. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  4918. // Sales Rep
  4919. NSDictionary *repDic = [self offline_getSalesRep];
  4920. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  4921. [ret setValue:section_0 forKey:@"section_0"];
  4922. return [RAConvertor dict2data:ret];
  4923. }
  4924. #pragma mark save
  4925. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  4926. NSString *addr = nil;
  4927. NSString *contact_name = nil;
  4928. NSString *companyName = [params objectForKey:@"company"];
  4929. if (companyName) {
  4930. companyName = [AESCrypt fastencrypt:companyName];
  4931. } else {
  4932. companyName = @"";
  4933. }
  4934. DebugLog(@"company");
  4935. companyName = [self translateSingleQuote:companyName];
  4936. NSString *addr1 = [params objectForKey:@"address"];
  4937. NSString *addr2 = [params objectForKey:@"address2"];
  4938. NSString *addr3 = [params objectForKey:@"address_3"];
  4939. NSString *addr4 = [params objectForKey:@"address_4"];
  4940. if (!addr2) {
  4941. addr2 = @"";
  4942. }
  4943. if (!addr3) {
  4944. addr3 = @"";
  4945. }
  4946. if (!addr4) {
  4947. addr4 = @"";
  4948. }
  4949. if (!addr1) {
  4950. addr1 = @"";
  4951. }
  4952. DebugLog(@"addr");
  4953. addr = [RAConvertor arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  4954. addr = [AESCrypt fastencrypt:addr];
  4955. addr = [self translateSingleQuote:addr];
  4956. if (addr1 && ![addr1 isEqualToString:@""]) {
  4957. addr1 = [AESCrypt fastencrypt:addr1];
  4958. }
  4959. addr1 = [self translateSingleQuote:addr1];
  4960. addr2 = [self translateSingleQuote:addr2];
  4961. addr3 = [self translateSingleQuote:addr3];
  4962. addr4 = [self translateSingleQuote:addr4];
  4963. NSString *country = [params objectForKey:@"country"];
  4964. if (country) {
  4965. country = [self countryNameByCountryCodeId:country];
  4966. } else {
  4967. country = @"";
  4968. }
  4969. DebugLog(@"country");
  4970. country = [self translateSingleQuote:country];
  4971. NSString *state = [params objectForKey:@"state"];
  4972. if (!state) {
  4973. state = @"";
  4974. }
  4975. DebugLog(@"state");
  4976. state = [self translateSingleQuote:state];
  4977. NSString *city = [params objectForKey:@"city"];
  4978. if (!city) {
  4979. city = @"";
  4980. }
  4981. city = [self translateSingleQuote:city];
  4982. NSString *zipcode = [params objectForKey:@"zipcode"];
  4983. if (!zipcode) {
  4984. zipcode = @"";
  4985. }
  4986. DebugLog(@"zip");
  4987. zipcode = [self translateSingleQuote:zipcode];
  4988. NSString *fistName = [params objectForKey:@"firstname"];
  4989. if (!fistName) {
  4990. fistName = @"";
  4991. }
  4992. NSString *lastName = [params objectForKey:@"lastname"];
  4993. if (!lastName) {
  4994. lastName = @"";
  4995. }
  4996. contact_name = [RAConvertor arr2string:@[fistName,lastName] separator:@" " trim:true];
  4997. DebugLog(@"contact_name");
  4998. contact_name = [self translateSingleQuote:contact_name];
  4999. fistName = [self translateSingleQuote:fistName];
  5000. lastName = [self translateSingleQuote:lastName];
  5001. NSString *phone = [params objectForKey:@"phone"];
  5002. if (phone) {
  5003. phone = [AESCrypt fastencrypt:phone];
  5004. } else {
  5005. phone = @"";
  5006. }
  5007. DebugLog(@"PHONE");
  5008. phone = [self translateSingleQuote:phone];
  5009. NSString *fax = [params objectForKey:@"fax"];
  5010. if (!fax) {
  5011. fax = @"";
  5012. }
  5013. DebugLog(@"FAX");
  5014. fax = [self translateSingleQuote:fax];
  5015. NSString *email = [params objectForKey:@"email"];
  5016. if (!email) {
  5017. email = @"";
  5018. }
  5019. DebugLog(@"EMAIL:%@",email);
  5020. email = [self translateSingleQuote:email];
  5021. NSString *notes = [params objectForKey:@"contact_notes"];
  5022. if (!notes) {
  5023. notes = @"";
  5024. }
  5025. DebugLog(@"NOTE:%@",notes);
  5026. notes = [self translateSingleQuote:notes];
  5027. NSString *price = [params objectForKey:@"price_name"];
  5028. if (price) {
  5029. price = [self priceNameByPriceId:price];
  5030. } else {
  5031. price = @"";
  5032. }
  5033. DebugLog(@"PRICE");
  5034. price = [self translateSingleQuote:price];
  5035. NSString *salesRep = [params objectForKey:@"sales_rep"];
  5036. if (salesRep) {
  5037. salesRep = [self salesRepCodeById:salesRep];
  5038. } else {
  5039. salesRep = @"";
  5040. }
  5041. salesRep = [self translateSingleQuote:salesRep];
  5042. NSString *img = [params objectForKey:@"business_card"];
  5043. NSArray *array = [img componentsSeparatedByString:@","];
  5044. NSString *img_0 = array[0];
  5045. if (!img_0) {
  5046. img_0 = @"";
  5047. }
  5048. img_0 = [self translateSingleQuote:img_0];
  5049. NSString *img_1 = array[1];
  5050. if (!img_1) {
  5051. img_1 = @"";
  5052. }
  5053. img_1 = [self translateSingleQuote:img_1];
  5054. NSString *img_2 = array[2];
  5055. if (!img_2) {
  5056. img_2 = @"";
  5057. }
  5058. img_2 = [self translateSingleQuote:img_2];
  5059. NSString *contact_id = [NSUUID UUID].UUIDString;
  5060. NSString *contact_type = [params objectForKey:@"type_name"];
  5061. if (!contact_type) {
  5062. contact_type = @"";
  5063. }
  5064. contact_type = [self translateSingleQuote:contact_type];
  5065. // 判断更新时是否为customer
  5066. if (update) {
  5067. contact_id = [params objectForKey:@"contact_id"];
  5068. if (!contact_id) {
  5069. contact_id = @"";
  5070. }
  5071. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  5072. __block int customer = 0;
  5073. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5074. customer = sqlite3_column_int(stmt, 0);
  5075. }];
  5076. isCustomer = customer ? YES : NO;
  5077. }
  5078. NSMutableDictionary *sync_dic = [params mutableCopy];
  5079. if (isCustomer) {
  5080. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  5081. } else {
  5082. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  5083. }
  5084. NSString *sync_data = nil;
  5085. NSString *sql = nil;
  5086. if (update){
  5087. contact_id = [params objectForKey:@"contact_id"];
  5088. if (!contact_id) {
  5089. contact_id = @"";
  5090. }
  5091. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5092. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5093. sync_data = [RAConvertor dict2string:sync_dic];
  5094. sync_data = [self translateSingleQuote:sync_data];
  5095. 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];
  5096. } else {
  5097. contact_id = [self translateSingleQuote:contact_id];
  5098. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5099. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5100. sync_data = [RAConvertor dict2string:sync_dic];
  5101. sync_data = [self translateSingleQuote:sync_data];
  5102. 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];
  5103. }
  5104. int result = [iSalesDB execSql:sql];
  5105. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  5106. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  5107. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  5108. }
  5109. #pragma mark save new contact
  5110. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  5111. {
  5112. return [self offline_saveContact:params update:NO isCustomer:YES];
  5113. }
  5114. #pragma mark edit contact
  5115. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  5116. {
  5117. // {
  5118. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  5119. // password = 123456;
  5120. // user = EvanK;
  5121. // }
  5122. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  5123. NSData *data = [NSData dataWithContentsOfFile:path];
  5124. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5125. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5126. NSString *countryCode = nil;
  5127. NSString *countryCode_id = nil;
  5128. NSString *stateCode = nil;
  5129. /*------contact infor------*/
  5130. __block NSString *country = nil;
  5131. __block NSString *company_name = nil;
  5132. __block NSString *contact_id = params[@"contact_id"];
  5133. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  5134. __block NSString *zipcode = nil;
  5135. __block NSString *state = nil; // state_code
  5136. __block NSString *city = nil; //
  5137. __block NSString *firt_name,*last_name;
  5138. __block NSString *phone,*fax,*email;
  5139. __block NSString *notes,*price_type,*sales_rep;
  5140. __block NSString *img_0,*img_1,*img_2;
  5141. __block NSString *contact_type;
  5142. contact_id = [self translateSingleQuote:contact_id];
  5143. 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];
  5144. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5145. country = [self textAtColumn:0 statement:stmt]; // country name
  5146. company_name = [self textAtColumn:1 statement:stmt];
  5147. addr_1 = [self textAtColumn:2 statement:stmt];
  5148. addr_2 = [self textAtColumn:3 statement:stmt];
  5149. addr_3 = [self textAtColumn:4 statement:stmt];
  5150. addr_4 = [self textAtColumn:5 statement:stmt];
  5151. zipcode = [self textAtColumn:6 statement:stmt];
  5152. state = [self textAtColumn:7 statement:stmt]; // state code
  5153. city = [self textAtColumn:8 statement:stmt];
  5154. firt_name = [self textAtColumn:9 statement:stmt];
  5155. last_name = [self textAtColumn:10 statement:stmt];
  5156. phone = [self textAtColumn:11 statement:stmt];
  5157. fax = [self textAtColumn:12 statement:stmt];
  5158. email = [self textAtColumn:13 statement:stmt];
  5159. notes = [self textAtColumn:14 statement:stmt];
  5160. price_type = [self textAtColumn:15 statement:stmt]; // name
  5161. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  5162. img_0 = [self textAtColumn:17 statement:stmt];
  5163. img_1 = [self textAtColumn:18 statement:stmt];
  5164. img_2 = [self textAtColumn:19 statement:stmt];
  5165. contact_type = [self textAtColumn:20 statement:stmt];
  5166. }];
  5167. // decrypt
  5168. if (company_name) {
  5169. company_name = [AESCrypt fastdecrypt:company_name];
  5170. }
  5171. if (addr_1) {
  5172. addr_1 = [AESCrypt fastdecrypt:addr_1];
  5173. }
  5174. if (phone) {
  5175. phone = [AESCrypt fastdecrypt:phone];
  5176. }
  5177. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  5178. countryCode = [iSalesDB jk_queryText:countrySql];
  5179. stateCode = state;
  5180. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5181. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  5182. NSString *code_id = params[@"country"];
  5183. countryCode_id = code_id;
  5184. countryCode = [self countryCodeByid:code_id];
  5185. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  5186. NSString *zip_code = params[@"zipcode"];
  5187. // 剔除全部为空格
  5188. int spaceCount = 0;
  5189. for (int i = 0; i < zip_code.length; i++) {
  5190. if ([zip_code characterAtIndex:i] == ' ') {
  5191. spaceCount++;
  5192. }
  5193. }
  5194. if (spaceCount == zip_code.length) {
  5195. zip_code = @"";
  5196. }
  5197. if (zipcode.length > 0) {
  5198. zipcode = zip_code;
  5199. countryCode_id = params[@"country"];
  5200. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  5201. countryCode = [dic valueForKey:@"country_code"];
  5202. if (!countryCode) {
  5203. NSString *code_id = params[@"country"];
  5204. countryCode = [self countryCodeByid:code_id];
  5205. }
  5206. stateCode = [dic valueForKey:@"state_code"];
  5207. if (!stateCode.length) {
  5208. stateCode = params[@"state"];
  5209. }
  5210. city = [dic valueForKey:@"city"];
  5211. if (!city.length) {
  5212. city = params[@"city"];
  5213. }
  5214. // zip code
  5215. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  5216. [zipDic setValue:zipcode forKey:@"value"];
  5217. [section_0 setValue:zipDic forKey:@"item_8"];
  5218. } else {
  5219. NSString *code_id = params[@"country"];
  5220. countryCode = [self countryCodeByid:code_id];
  5221. stateCode = params[@"state"];
  5222. city = params[@"city"];
  5223. }
  5224. }
  5225. }
  5226. // 0 Country
  5227. // 1 Company Name
  5228. // 2 Contact ID
  5229. // 3 Picture
  5230. // 4 Address 1
  5231. // 5 Address 2
  5232. // 6 Address 3
  5233. // 7 Address 4
  5234. // 8 Zip Code
  5235. // 9 State/Province
  5236. // 10 City
  5237. // 11 Contact First Name
  5238. // 12 Contact Last Name
  5239. // 13 Phone
  5240. // 14 Fax
  5241. // 15 Email
  5242. // 16 Contact Notes
  5243. // 17 Price Type
  5244. // 18 Contact Type
  5245. // 19 Sales Rep
  5246. // country
  5247. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5248. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  5249. // company
  5250. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  5251. // contact_id
  5252. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  5253. // picture
  5254. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  5255. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  5256. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  5257. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  5258. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  5259. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  5260. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  5261. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  5262. // addr 1 2 3 4
  5263. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  5264. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  5265. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  5266. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  5267. // zip code
  5268. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  5269. // state
  5270. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5271. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  5272. // city
  5273. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  5274. // first last
  5275. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  5276. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  5277. // phone fax email
  5278. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  5279. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  5280. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  5281. // notes
  5282. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  5283. // price
  5284. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  5285. for (NSString *key in priceDic.allKeys) {
  5286. if ([key containsString:@"val_"]) {
  5287. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  5288. if ([dic[@"value"] isEqualToString:price_type]) {
  5289. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5290. [priceDic setValue:dic forKey:key];
  5291. }
  5292. }
  5293. }
  5294. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  5295. // Contact Rep
  5296. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  5297. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5298. // Sales Rep
  5299. NSMutableDictionary *repDic = [[self offline_getSalesRep] mutableCopy];
  5300. for (NSString *key in repDic.allKeys) {
  5301. if ([key containsString:@"val_"]) {
  5302. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  5303. NSString *value = dic[@"value"];
  5304. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  5305. if (code && [code isEqualToString:sales_rep]) {
  5306. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5307. [repDic setValue:dic forKey:key];
  5308. }
  5309. }
  5310. }
  5311. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  5312. [ret setValue:section_0 forKey:@"section_0"];
  5313. return [RAConvertor dict2data:ret];
  5314. }
  5315. #pragma mark save contact
  5316. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  5317. {
  5318. return [self offline_saveContact:params update:YES isCustomer:YES];
  5319. }
  5320. #pragma mark category
  5321. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5322. if (ck) {
  5323. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  5324. for (NSString *key in res.allKeys) {
  5325. if (![key isEqualToString:@"count"]) {
  5326. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  5327. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5328. if ([val[@"value"] isEqualToString:ck]) {
  5329. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5330. }
  5331. [res setValue:val forKey:key];
  5332. }
  5333. }
  5334. [dic setValue:res forKey:valueKey];
  5335. }
  5336. }
  5337. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  5338. // NSString* orderCode = [params valueForKey:@"orderCode"];
  5339. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5340. NSString* category = [params valueForKey:@"category"];
  5341. if (!category || [category isEqualToString:@""]) {
  5342. category = @"%";
  5343. }
  5344. category = [self translateSingleQuote:category];
  5345. int limit = [[params valueForKey:@"limit"] intValue];
  5346. int offset = [[params valueForKey:@"offset"] intValue];
  5347. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5348. NSString *limit_str = @"";
  5349. if (limited) {
  5350. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  5351. }
  5352. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5353. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5354. sqlite3 *db = [iSalesDB get_db];
  5355. // [iSalesDB AddExFunction:db];
  5356. int count;
  5357. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  5358. NSString *sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.category,m.more_color 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];
  5359. double price_min = 0;
  5360. double price_max = 0;
  5361. if ([params.allKeys containsObject:@"alert"]) {
  5362. // alert
  5363. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5364. NSString *alert = params[@"alert"];
  5365. if ([alert isEqualToString:@"Display All"]) {
  5366. alert = [NSString stringWithFormat:@""];
  5367. } else {
  5368. alert = [self translateSingleQuote:alert];
  5369. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5370. }
  5371. // available
  5372. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5373. NSString *available = params[@"available"];
  5374. NSString *available_condition;
  5375. if ([available isEqualToString:@"Display All"]) {
  5376. available_condition = @"";
  5377. } else if ([available isEqualToString:@"Available Now"]) {
  5378. available_condition = @"and availability > 0";
  5379. } else {
  5380. available_condition = @"and availability == 0";
  5381. }
  5382. // best seller
  5383. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5384. NSString *best_seller = @"";
  5385. NSString *order_best_seller = @"m.name asc";
  5386. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5387. best_seller = @"and best_seller > 0";
  5388. order_best_seller = @"m.best_seller desc,m.name asc";
  5389. }
  5390. // price
  5391. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5392. NSString *price = params[@"price"];
  5393. price_min = 0;
  5394. price_max = MAXFLOAT;
  5395. if (appDelegate.user && price != nil) {
  5396. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  5397. NSMutableString *priceName = [NSMutableString string];
  5398. for (int i = 0; i < priceTypeArray.count; i++) {
  5399. NSString *pricetype = priceTypeArray[i];
  5400. pricetype = [self translateSingleQuote:pricetype];
  5401. if (i == 0) {
  5402. [priceName appendFormat:@"'%@'",pricetype];
  5403. } else {
  5404. [priceName appendFormat:@",'%@'",pricetype];
  5405. }
  5406. }
  5407. if ([price isEqualToString:@"Display All"]) {
  5408. price = [NSString stringWithFormat:@""];
  5409. } else if([price containsString:@"+"]){
  5410. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5411. price_min = [price doubleValue];
  5412. 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];
  5413. } else {
  5414. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5415. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5416. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5417. 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];
  5418. }
  5419. } else {
  5420. price = @"";
  5421. }
  5422. // sold_by_qty : Sold in quantities of %@
  5423. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5424. NSString *qty = params[@"sold_by_qty"];
  5425. if ([qty isEqualToString:@"Display All"]) {
  5426. qty = @"";
  5427. } else {
  5428. qty = [self translateSingleQuote:qty];
  5429. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5430. }
  5431. // cate
  5432. // category = [self translateSingleQuote:category];
  5433. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  5434. // cate mutiple selection
  5435. NSString *category_id = params[@"category"];
  5436. NSMutableArray *cate_id_array = nil;
  5437. NSMutableString *cateWhere = [NSMutableString string];
  5438. if ([category_id isEqualToString:@""] || !category_id) {
  5439. [cateWhere appendString:@"1 = 1"];
  5440. } else {
  5441. if ([category_id containsString:@","]) {
  5442. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  5443. } else {
  5444. cate_id_array = [@[category_id] mutableCopy];
  5445. }
  5446. [cateWhere appendString:@"("];
  5447. for (int i = 0; i < cate_id_array.count; i++) {
  5448. if (i == 0) {
  5449. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5450. } else {
  5451. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  5452. }
  5453. }
  5454. [cateWhere appendString:@")"];
  5455. }
  5456. // where bestseller > 0 order by bestseller desc
  5457. // sql query: alert availability(int) best_seller(int) price qty
  5458. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.category,m.more_color from (select name,description,product_id,closeout,best_seller,category,more_color 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];
  5459. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  5460. }
  5461. DebugLog(@"offline category where: %@",where);
  5462. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  5463. if (!appDelegate.user) {
  5464. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  5465. }
  5466. [ret setValue:filter forKey:@"filter"];
  5467. DebugLog(@"offline_category sql:%@",sqlQuery);
  5468. sqlite3_stmt * statement;
  5469. [ret setValue:@"2" forKey:@"result"];
  5470. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  5471. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  5472. // int count=0;
  5473. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5474. {
  5475. int i=0;
  5476. while (sqlite3_step(statement) == SQLITE_ROW)
  5477. {
  5478. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5479. char *name = (char*)sqlite3_column_text(statement, 0);
  5480. if(name==nil)
  5481. name="";
  5482. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5483. char *description = (char*)sqlite3_column_text(statement, 1);
  5484. if(description==nil)
  5485. description="";
  5486. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  5487. int product_id = sqlite3_column_int(statement, 2);
  5488. int wid = sqlite3_column_int(statement, 3);
  5489. int closeout = sqlite3_column_int(statement, 4);
  5490. int cid = sqlite3_column_int(statement, 5);
  5491. int wisdelete = sqlite3_column_int(statement, 6);
  5492. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  5493. int more_color = sqlite3_column_int(statement, 8);
  5494. // Defaul Category ID
  5495. __block NSString *categoryID = nil;
  5496. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  5497. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5498. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  5499. if(default_category==nil)
  5500. default_category="";
  5501. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  5502. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  5503. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  5504. categoryID = nsdefault_category;
  5505. }];
  5506. if (!categoryID.length) {
  5507. NSString *cateIDs = params[@"category"];
  5508. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  5509. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  5510. for (NSString *cateID in requestCategoryArr) {
  5511. BOOL needBreak = NO;
  5512. for (NSString *itemCateIDBox in itemCategoryArr) {
  5513. if (itemCateIDBox.length > 4) {
  5514. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  5515. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  5516. if ([itemCategoryID isEqualToString:cateID]) {
  5517. needBreak = YES;
  5518. categoryID = itemCategoryID;
  5519. break;
  5520. }
  5521. }
  5522. }
  5523. if (needBreak) {
  5524. break;
  5525. }
  5526. }
  5527. }
  5528. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  5529. if(wid !=0 && wisdelete != 1)
  5530. [item setValue:@"true" forKey:@"wish_exists"];
  5531. else
  5532. [item setValue:@"false" forKey:@"wish_exists"];
  5533. if(closeout==0)
  5534. [item setValue:@"false" forKey:@"is_closeout"];
  5535. else
  5536. [item setValue:@"true" forKey:@"is_closeout"];
  5537. if(cid==0)
  5538. [item setValue:@"false" forKey:@"cart_exists"];
  5539. else
  5540. [item setValue:@"true" forKey:@"cart_exists"];
  5541. if (more_color == 0) {
  5542. [item setObject:@(false) forKey:@"more_color"];
  5543. } else if (more_color == 1) {
  5544. [item setObject:@(true) forKey:@"more_color"];
  5545. }
  5546. [item addEntriesFromDictionary:imgjson];
  5547. // [item setValue:nsurl forKey:@"img"];
  5548. [item setValue:nsname forKey:@"name"];
  5549. [item setValue:nsdescription forKey:@"description"];
  5550. if (categoryID) {
  5551. [item setValue:categoryID forKey:@"item_category_id"];
  5552. }
  5553. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5554. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5555. i++;
  5556. }
  5557. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5558. [ret setObject:items forKey:@"items"];
  5559. sqlite3_finalize(statement);
  5560. } else {
  5561. DebugLog(@"nothing...");
  5562. }
  5563. DebugLog(@"count:%d",count);
  5564. [iSalesDB close_db:db];
  5565. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  5566. return ret;
  5567. }
  5568. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  5569. {
  5570. return [self categoryList:params limited:YES];
  5571. }
  5572. # pragma mark item search
  5573. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  5574. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5575. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  5576. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5577. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5578. // category
  5579. NSDictionary *category_menu = [self offline_category_menu];
  5580. [filter setValue:category_menu forKey:@"category"];
  5581. NSString* where= nil;
  5582. NSString* orderby= @"m.name";
  5583. if (!filterSearch) {
  5584. int covertype = [[params valueForKey:@"covertype"] intValue];
  5585. switch (covertype) {
  5586. case 0:
  5587. {
  5588. where=@"m.category like'%%#005#%%'";
  5589. break;
  5590. }
  5591. case 1:
  5592. {
  5593. where=@"m.alert like '%QS%'";
  5594. break;
  5595. }
  5596. case 2:
  5597. {
  5598. where=@"m.availability>0";
  5599. break;
  5600. }
  5601. case 3:
  5602. {
  5603. where=@"m.best_seller>0";
  5604. orderby=@"m.best_seller desc,m.name asc";
  5605. break;
  5606. }
  5607. default:
  5608. where=@"1=1";
  5609. break;
  5610. }
  5611. }
  5612. int limit = [[params valueForKey:@"limit"] intValue];
  5613. int offset = [[params valueForKey:@"offset"] intValue];
  5614. NSString *limit_str = @"";
  5615. if (limited) {
  5616. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  5617. }
  5618. sqlite3 *db = [iSalesDB get_db];
  5619. // [iSalesDB AddExFunction:db];
  5620. int count;
  5621. NSString *sqlQuery = nil;
  5622. where = [where stringByAppendingString:@" and m.is_active = 1"];
  5623. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color 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];
  5624. double price_min = 0;
  5625. double price_max = 0;
  5626. if (filterSearch) {
  5627. // alert
  5628. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5629. NSString *alert = params[@"alert"];
  5630. if ([alert isEqualToString:@"Display All"]) {
  5631. alert = [NSString stringWithFormat:@""];
  5632. } else {
  5633. alert = [self translateSingleQuote:alert];
  5634. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5635. }
  5636. // available
  5637. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5638. NSString *available = params[@"available"];
  5639. NSString *available_condition;
  5640. if ([available isEqualToString:@"Display All"]) {
  5641. available_condition = @"";
  5642. } else if ([available isEqualToString:@"Available Now"]) {
  5643. available_condition = @"and availability > 0";
  5644. } else {
  5645. available_condition = @"and availability == 0";
  5646. }
  5647. // best seller
  5648. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5649. NSString *best_seller = @"";
  5650. NSString *order_best_seller = @"m.name asc";
  5651. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5652. best_seller = @"and best_seller > 0";
  5653. order_best_seller = @"m.best_seller desc,m.name asc";
  5654. }
  5655. // price
  5656. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5657. NSString *price = params[@"price"];
  5658. price_min = 0;
  5659. price_max = MAXFLOAT;
  5660. if (appDelegate.user) {
  5661. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  5662. NSMutableString *priceName = [NSMutableString string];
  5663. for (int i = 0; i < priceTypeArray.count; i++) {
  5664. NSString *pricetype = priceTypeArray[i];
  5665. pricetype = [self translateSingleQuote:pricetype];
  5666. if (i == 0) {
  5667. [priceName appendFormat:@"'%@'",pricetype];
  5668. } else {
  5669. [priceName appendFormat:@",'%@'",pricetype];
  5670. }
  5671. }
  5672. if ([price isEqualToString:@"Display All"]) {
  5673. price = [NSString stringWithFormat:@""];
  5674. } else if([price containsString:@"+"]){
  5675. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5676. price_min = [price doubleValue];
  5677. 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];
  5678. } else {
  5679. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5680. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5681. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5682. 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];
  5683. }
  5684. } else {
  5685. price = @"";
  5686. }
  5687. // sold_by_qty : Sold in quantities of %@
  5688. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5689. NSString *qty = params[@"sold_by_qty"];
  5690. if ([qty isEqualToString:@"Display All"]) {
  5691. qty = @"";
  5692. } else {
  5693. qty = [self translateSingleQuote:qty];
  5694. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5695. }
  5696. // category
  5697. NSString *category_id = params[@"ctgId"];
  5698. NSMutableArray *cate_id_array = nil;
  5699. NSMutableString *cateWhere = [NSMutableString string];
  5700. if ([category_id isEqualToString:@""] || !category_id) {
  5701. [cateWhere appendString:@"1 = 1"];
  5702. } else {
  5703. if ([category_id containsString:@","]) {
  5704. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  5705. } else {
  5706. cate_id_array = [@[category_id] mutableCopy];
  5707. }
  5708. /*-----------*/
  5709. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  5710. [cateWhere appendString:@"("];
  5711. for (int i = 0; i < cate_id_array.count; i++) {
  5712. for (NSString *key0 in cateDic.allKeys) {
  5713. if ([key0 containsString:@"category_"]) {
  5714. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  5715. for (NSString *key1 in category0.allKeys) {
  5716. if ([key1 containsString:@"category_"]) {
  5717. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  5718. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5719. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  5720. cate_id_array[i] = [category1 objectForKey:@"id"];
  5721. if (i == 0) {
  5722. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5723. } else {
  5724. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  5725. }
  5726. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5727. [category0 setValue:category1 forKey:key1];
  5728. [cateDic setValue:category0 forKey:key0];
  5729. }
  5730. } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  5731. if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  5732. cate_id_array[i] = [category0 objectForKey:@"id"];
  5733. if (i == 0) {
  5734. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5735. } else {
  5736. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  5737. }
  5738. [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5739. [cateDic setValue:category0 forKey:key0];
  5740. }
  5741. }
  5742. }
  5743. }
  5744. }
  5745. }
  5746. [cateWhere appendString:@")"];
  5747. [filter setValue:cateDic forKey:@"category"];
  5748. }
  5749. // where bestseller > 0 order by bestseller desc
  5750. // sql query: alert availability(int) best_seller(int) price qty
  5751. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color from (select name,description,product_id,closeout,best_seller,more_color 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];
  5752. // count
  5753. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  5754. }
  5755. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  5756. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  5757. if (!appDelegate.user) {
  5758. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  5759. }
  5760. [ret setValue:filter forKey:@"filter"];
  5761. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  5762. sqlite3_stmt * statement;
  5763. [ret setValue:@"2" forKey:@"result"];
  5764. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  5765. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  5766. // int count=0;
  5767. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5768. {
  5769. int i=0;
  5770. while (sqlite3_step(statement) == SQLITE_ROW)
  5771. {
  5772. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5773. char *name = (char*)sqlite3_column_text(statement, 0);
  5774. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5775. char *description = (char*)sqlite3_column_text(statement, 1);
  5776. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  5777. int product_id = sqlite3_column_int(statement, 2);
  5778. int wid = sqlite3_column_int(statement, 3);
  5779. int closeout = sqlite3_column_int(statement, 4);
  5780. int cid = sqlite3_column_int(statement, 5);
  5781. int wisdelete = sqlite3_column_int(statement, 6);
  5782. int more_color = sqlite3_column_int(statement, 7);
  5783. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  5784. if(wid !=0 && wisdelete != 1)
  5785. [item setValue:@"true" forKey:@"wish_exists"];
  5786. else
  5787. [item setValue:@"false" forKey:@"wish_exists"];
  5788. if(closeout==0)
  5789. [item setValue:@"false" forKey:@"is_closeout"];
  5790. else
  5791. [item setValue:@"true" forKey:@"is_closeout"];
  5792. if(cid==0)
  5793. [item setValue:@"false" forKey:@"cart_exists"];
  5794. else
  5795. [item setValue:@"true" forKey:@"cart_exists"];
  5796. if (more_color == 0) {
  5797. [item setObject:@(false) forKey:@"more_color"];
  5798. } else if (more_color == 1) {
  5799. [item setObject:@(true) forKey:@"more_color"];
  5800. }
  5801. [item addEntriesFromDictionary:imgjson];
  5802. // [item setValue:nsurl forKey:@"img"];
  5803. [item setValue:nsname forKey:@"fash_name"];
  5804. [item setValue:nsdescription forKey:@"description"];
  5805. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5806. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5807. i++;
  5808. }
  5809. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5810. [ret setObject:items forKey:@"items"];
  5811. sqlite3_finalize(statement);
  5812. }
  5813. [iSalesDB close_db:db];
  5814. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  5815. return ret;
  5816. }
  5817. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  5818. {
  5819. return [self itemsearch:params limited:YES];
  5820. }
  5821. #pragma mark order detail
  5822. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  5823. if (str1.length == 0) {
  5824. str1 = @"&nbsp";
  5825. }
  5826. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  5827. return str;
  5828. }
  5829. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  5830. {
  5831. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  5832. [fromformatter setDateFormat:from];
  5833. NSDate *date = [fromformatter dateFromString:datetime];
  5834. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  5835. [toformatter setDateFormat:to];
  5836. NSString * ret = [toformatter stringFromDate:date];
  5837. return ret;
  5838. }
  5839. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  5840. // 把毫秒去掉
  5841. if ([dateTime containsString:@"."]) {
  5842. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  5843. }
  5844. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  5845. formatter.dateFormat = formate;
  5846. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  5847. NSDate *date = [formatter dateFromString:dateTime];
  5848. formatter.dateFormat = newFormate;
  5849. NSString *result = [formatter stringFromDate:date];
  5850. return result ? result : @"";
  5851. }
  5852. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  5853. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  5854. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  5855. }
  5856. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  5857. {
  5858. DebugLog(@"offline oderdetail params: %@",params);
  5859. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5860. int orderId = [params[@"orderId"] intValue];
  5861. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  5862. // decrypt card number and card security code
  5863. // 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 ];
  5864. 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,c.city,c.state,c.zipcode,c.country 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];
  5865. sqlite3 *db = [iSalesDB get_db];
  5866. sqlite3_stmt * statement;
  5867. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  5868. NSString *nssoid = @"";
  5869. NSString* orderinfo = @"";
  5870. NSString *moreInfo = @"";
  5871. double handlingFee = 0;
  5872. double payments_and_credist = 0;
  5873. double totalPrice = 0;
  5874. double shippingFee = 0;
  5875. double lift_gate = 0;
  5876. NSString *customer_contact = @"";
  5877. NSString *customer_email = @"";
  5878. NSString *customer_fax = @"";
  5879. NSString *customer_phone = @"";
  5880. NSString *customer_city = @"";
  5881. NSString *customer_state = @"";
  5882. NSString *customer_zipcode = @"";
  5883. NSString *customer_country = @"";
  5884. __block NSMutableDictionary *section_0 = [NSMutableDictionary dictionary];
  5885. __block NSMutableDictionary *section_1 = [NSMutableDictionary dictionary];
  5886. __block NSMutableDictionary *section_2 = [NSMutableDictionary dictionary];
  5887. __block NSMutableDictionary *section_3 = [NSMutableDictionary dictionary];
  5888. __block NSMutableDictionary *section_4 = [NSMutableDictionary dictionary];
  5889. __block NSMutableDictionary *price_data = [NSMutableDictionary dictionary];
  5890. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  5891. {
  5892. if (sqlite3_step(statement) == SQLITE_ROW)
  5893. {
  5894. int order_id = sqlite3_column_int(statement, 0);
  5895. NSString *sign_url = [self textAtColumn:51 statement:statement];
  5896. // ret[@"sign_url"] = sign_url;
  5897. section_1 = @{
  5898. @"data":sign_url,
  5899. @"title":@"Signature",
  5900. @"type":@"sign_url"
  5901. }.mutableCopy;
  5902. [ret setObject:section_1 forKey:@"section_1"];
  5903. customer_contact = [self textAtColumn:52 statement:statement];
  5904. customer_email = [self textAtColumn:53 statement:statement];
  5905. customer_phone = [self textAtColumn:54 statement:statement];
  5906. customer_fax = [self textAtColumn:55 statement:statement];
  5907. customer_city = [self textAtColumn:60 statement:statement];
  5908. customer_state = [self textAtColumn:61 statement:statement];
  5909. customer_zipcode = [self textAtColumn:62 statement:statement];
  5910. customer_country = [self textAtColumn:63 statement:statement];
  5911. int offline_edit=sqlite3_column_int(statement, 56);
  5912. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  5913. char *soid = (char*)sqlite3_column_text(statement, 1);
  5914. if(soid==nil)
  5915. soid= "";
  5916. nssoid= [[NSString alloc]initWithUTF8String:soid];
  5917. // so#
  5918. ret[@"so#"] = nssoid;
  5919. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  5920. if(poNumber==nil)
  5921. poNumber= "";
  5922. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  5923. char *create_time = (char*)sqlite3_column_text(statement, 3);
  5924. if(create_time==nil)
  5925. create_time= "";
  5926. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  5927. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  5928. int status = sqlite3_column_int(statement, 4);
  5929. int erpStatus = sqlite3_column_int(statement, 49);
  5930. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  5931. // status
  5932. if (status > 1 && status != 3) {
  5933. status = erpStatus;
  5934. } else if (status == 3) {
  5935. status = 15;
  5936. }
  5937. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  5938. ret[@"order_status"] = nsstatus;
  5939. char *company_name = (char*)sqlite3_column_text(statement, 5);
  5940. if(company_name==nil)
  5941. company_name= "";
  5942. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  5943. // company name
  5944. ret[@"company_name"] = nscompany_name;
  5945. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  5946. if(customer_contact==nil)
  5947. customer_contact= "";
  5948. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  5949. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  5950. if(addr_1==nil)
  5951. addr_1="";
  5952. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  5953. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  5954. if(addr_2==nil)
  5955. addr_2="";
  5956. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  5957. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  5958. if(addr_3==nil)
  5959. addr_3="";
  5960. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  5961. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  5962. if(addr_4==nil)
  5963. addr_4="";
  5964. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  5965. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  5966. [arr_addr addObject:nsaddr_1];
  5967. [arr_addr addObject:nsaddr_2];
  5968. [arr_addr addObject:nsaddr_3];
  5969. [arr_addr addObject:nsaddr_4];
  5970. [arr_addr addObject:[NSString stringWithFormat:@"%@,",customer_city]];
  5971. [arr_addr addObject:customer_state];
  5972. [arr_addr addObject:customer_zipcode];
  5973. [arr_addr addObject:customer_country];
  5974. NSString * customer_address = [RAConvertor arr2string:arr_addr separator:@" " trim:true];
  5975. char *logist = (char*)sqlite3_column_text(statement, 11);
  5976. if(logist==nil)
  5977. logist= "";
  5978. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  5979. if (/*status == -11 || */status == 10 || status == 11) {
  5980. nslogist = [self textAtColumn:59 statement:statement];
  5981. };
  5982. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  5983. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  5984. shipping = @"Shipping To Be Quoted";
  5985. } else {
  5986. shippingFee = sqlite3_column_double(statement, 12);
  5987. }
  5988. // Shipping
  5989. // ret[@"Shipping"] = shipping;
  5990. NSDictionary *shipping_item = @{
  5991. @"title":@"Shipping",
  5992. @"value":shipping
  5993. };
  5994. [price_data setObject:shipping_item forKey:@"item_1"];
  5995. int have_lift_gate = sqlite3_column_int(statement, 17);
  5996. lift_gate = sqlite3_column_double(statement, 13);
  5997. // Liftgate Fee(No loading dock)
  5998. if (!have_lift_gate) {
  5999. lift_gate = 0;
  6000. }
  6001. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  6002. NSString *liftgate_value = [NSString stringWithFormat:@"$%.2f",lift_gate];
  6003. if (sqlite3_column_int(statement, 57)) {
  6004. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  6005. liftgate_value = @"Shipping To Be Quoted";
  6006. }
  6007. NSDictionary *liftgate_item = @{
  6008. @"title":@"Liftgate Fee(No loading dock)",
  6009. @"value":liftgate_value
  6010. };
  6011. [price_data setObject:liftgate_item forKey:@"item_2"];
  6012. // if ([nslogist isEqualToString:@"WILL CALL"]) {
  6013. // [ret removeObjectForKey:@"Shipping"];
  6014. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  6015. // }
  6016. //
  6017. // if (have_lift_gate) {
  6018. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  6019. // }
  6020. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  6021. if(general_notes==nil)
  6022. general_notes= "";
  6023. NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  6024. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  6025. if(internal_notes==nil)
  6026. internal_notes= "";
  6027. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  6028. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  6029. if(payment_type==nil)
  6030. payment_type= "";
  6031. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  6032. // order info
  6033. orderinfo = [self textFileName:@"order_info.html"];
  6034. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  6035. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  6036. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  6037. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  6038. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  6039. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  6040. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  6041. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  6042. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  6043. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  6044. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  6045. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  6046. NSString *payment = nil;
  6047. // id -> show
  6048. __block NSString *show_pay_type = nspayment_type;
  6049. [iSalesDB jk_query:[NSString stringWithFormat:@"select pay_type from payment_type where type_id = '%@';",nspayment_type] db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6050. char *show_typ_ch = (char*)sqlite3_column_text(stmt, 0);
  6051. if (show_typ_ch != NULL) {
  6052. show_pay_type = [NSString stringWithUTF8String:show_typ_ch];
  6053. }
  6054. }];
  6055. if([nspayment_type isEqualToString:@"Credit Card"] || [nspayment_type isEqualToString:@"Visa/Master"])
  6056. {
  6057. payment = [self textFileName:@"creditcardpayment.html"];
  6058. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  6059. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  6060. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  6061. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  6062. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  6063. NSString *card_type = [self textAtColumn:42 statement:statement];
  6064. if (card_type.length > 0) { // 显示星号
  6065. card_type = @"****";
  6066. }
  6067. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  6068. if (card_number.length > 0 && card_number.length > 4) {
  6069. for (int i = 0; i < card_number.length - 4; i++) {
  6070. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  6071. }
  6072. } else {
  6073. card_number = @"";
  6074. }
  6075. NSString *card_security_code = [self textAtColumn:44 statement:statement];
  6076. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  6077. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  6078. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  6079. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  6080. card_expiration = @"****";
  6081. }
  6082. NSString *card_city = [self textAtColumn:46 statement:statement];
  6083. NSString *card_state = [self textAtColumn:47 statement:statement];
  6084. payment = [self replaceHtml:payment String:@"Payment_Type" withString:show_pay_type];
  6085. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  6086. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  6087. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  6088. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  6089. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  6090. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  6091. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  6092. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  6093. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  6094. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  6095. }
  6096. else
  6097. {
  6098. payment=[self textFileName:@"normalpayment.html"];
  6099. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  6100. }
  6101. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  6102. ret[@"result"]= [NSNumber numberWithInt:2];
  6103. // more info
  6104. moreInfo = [self textFileName:@"more_info.html"];
  6105. /*****ship to******/
  6106. // ShipToCompany_or_&nbsp
  6107. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  6108. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  6109. NSString *shipToName = [self textAtColumn:19 statement:statement];
  6110. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  6111. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  6112. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  6113. /*****ship from******/
  6114. // ShipFromCompany_or_&nbsp
  6115. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  6116. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  6117. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  6118. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  6119. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  6120. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  6121. /*****freight to******/
  6122. // FreightBillToCompany_or_&nbsp
  6123. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  6124. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  6125. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  6126. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  6127. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  6128. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  6129. /*****merchandise to******/
  6130. // MerchandiseBillToCompany_or_&nbsp
  6131. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  6132. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  6133. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  6134. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  6135. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  6136. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  6137. /*****return to******/
  6138. // ReturnToCompany_or_&nbsp
  6139. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  6140. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  6141. NSString *returnToName = [self textAtColumn:31 statement:statement];
  6142. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  6143. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  6144. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  6145. //
  6146. // DebugLog(@"more info : %@",moreInfo);
  6147. // handling fee
  6148. handlingFee = sqlite3_column_double(statement, 33);
  6149. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  6150. NSString *handling_fee_value = [NSString stringWithFormat:@"$%.2f",handlingFee];
  6151. if (sqlite3_column_int(statement, 58)) {
  6152. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  6153. handling_fee_value = @"Shipping To Be Quoted";
  6154. }
  6155. NSDictionary *handling_fee_item = @{
  6156. @"title":@"Handling Fee",
  6157. @"value":handling_fee_value
  6158. };
  6159. [price_data setObject:handling_fee_item forKey:@"item_3"];
  6160. //
  6161. // customer info
  6162. customerID = [self textAtColumn:36 statement:statement];
  6163. // mode
  6164. ret[@"mode"] = appDelegate.mode;
  6165. // model_count
  6166. ret[@"model_count"] = @(0);
  6167. }
  6168. sqlite3_finalize(statement);
  6169. }
  6170. [iSalesDB close_db:db];
  6171. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  6172. // "customer_email" = "Shui Hu";
  6173. // "customer_fax" = "";
  6174. // "customer_first_name" = F;
  6175. // "customer_last_name" = L;
  6176. // "customer_name" = ",da He Xiang Dong Liu A";
  6177. // "customer_phone" = "Hey Xuan Feng";
  6178. contactInfo[@"customer_phone"] = customer_phone;
  6179. contactInfo[@"customer_fax"] = customer_fax;
  6180. contactInfo[@"customer_email"] = customer_email;
  6181. NSString *first_name = @"";
  6182. NSString *last_name = @"";
  6183. if ([customer_contact isEqualToString:@""]) {
  6184. } else if ([customer_contact containsString:@" "]) {
  6185. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  6186. first_name = [customer_contact substringToIndex:first_space_index];
  6187. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  6188. }
  6189. contactInfo[@"customer_first_name"] = first_name;
  6190. contactInfo[@"customer_last_name"] = last_name;
  6191. ret[@"customerInfo"] = contactInfo;
  6192. // models
  6193. if (nssoid) {
  6194. __block NSMutableDictionary *model_data = [NSMutableDictionary dictionary];
  6195. __block double TotalCuft = 0;
  6196. __block double TotalWeight = 0;
  6197. __block int TotalCarton = 0;
  6198. __block double allItemPrice = 0;
  6199. 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];
  6200. sqlite3 *db1 = [iSalesDB get_db];
  6201. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6202. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  6203. int product_id = sqlite3_column_int(stmt, 0);
  6204. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  6205. int item_id = sqlite3_column_int(stmt, 7);
  6206. NSString* Price=nil;
  6207. if(str_price==nil)
  6208. {
  6209. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db1];
  6210. if(price==nil)
  6211. Price=@"No Price.";
  6212. else
  6213. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6214. }
  6215. else
  6216. {
  6217. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  6218. }
  6219. double discount = sqlite3_column_double(stmt, 2);
  6220. int item_count = sqlite3_column_int(stmt, 3);
  6221. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  6222. NSString *nsline_note=nil;
  6223. if(line_note!=nil)
  6224. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  6225. // char *name = (char*)sqlite3_column_text(stmt, 5);
  6226. // NSString *nsname = nil;
  6227. // if(name!=nil)
  6228. // nsname= [[NSString alloc]initWithUTF8String:name];
  6229. NSString *nsname = [self textAtColumn:5 statement:stmt];
  6230. // char *description = (char*)sqlite3_column_text(stmt, 6);
  6231. // NSString *nsdescription=nil;
  6232. // if(description!=nil)
  6233. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  6234. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  6235. // int stockUom = sqlite3_column_int(stmt, 8);
  6236. // int _id = sqlite3_column_int(stmt, 9);
  6237. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  6238. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  6239. double weight=[bsubtotaljson[@"weight"] doubleValue];
  6240. int carton=[bsubtotaljson[@"carton"] intValue];
  6241. TotalCuft += cuft;
  6242. TotalWeight += weight;
  6243. TotalCarton += carton;
  6244. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID];
  6245. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  6246. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  6247. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  6248. itemjson[@"note"]=nsline_note;
  6249. itemjson[@"origin_price"] = Price;
  6250. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  6251. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  6252. itemjson[@"order_item_status"] = @""; // 暂时不处理
  6253. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  6254. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  6255. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  6256. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6257. if(itemjson[@"combine"] != nil)
  6258. {
  6259. // int citem=0;
  6260. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  6261. for(int bc=0;bc<bcount;bc++)
  6262. {
  6263. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6264. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6265. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6266. subTotal += uprice * modulus * item_count;
  6267. }
  6268. }
  6269. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  6270. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  6271. // ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  6272. // ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  6273. itemjson[@"type"] = @"order_item";
  6274. model_data[[NSString stringWithFormat:@"item_%ld",(*count)++]] = itemjson;
  6275. model_data[@"count"] = [NSNumber numberWithInteger:*count];
  6276. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  6277. allItemPrice += subTotal;
  6278. }];
  6279. section_3[@"data"] = model_data;
  6280. section_3[@"type"] = @"sub_order";
  6281. section_3[@"title"] = @"Models";
  6282. section_3[@"switch"] = @(false);
  6283. ret[@"section_3"] = section_3;
  6284. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  6285. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  6286. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  6287. // payments/Credits
  6288. // payments_and_credist = sqlite3_column_double(statement, 34);
  6289. payments_and_credist = allItemPrice;
  6290. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6291. NSDictionary *sub_total_item = @{
  6292. @"title":@"Sub-Total",
  6293. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  6294. };
  6295. [price_data setObject:sub_total_item forKey:@"item_0"];
  6296. // // total
  6297. // totalPrice = sqlite3_column_double(statement, 35);
  6298. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6299. } else {
  6300. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  6301. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  6302. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  6303. // payments/Credits
  6304. payments_and_credist = 0;
  6305. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6306. NSDictionary *sub_total_item = @{
  6307. @"title":@"Sub-Total",
  6308. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  6309. };
  6310. [price_data setObject:sub_total_item forKey:@"item_0"];
  6311. }
  6312. // total
  6313. totalPrice = payments_and_credist + lift_gate + shippingFee + handlingFee;
  6314. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6315. NSDictionary *total_item = @{
  6316. @"title":@"Total",
  6317. @"value":[NSString stringWithFormat:@"$%.2f",totalPrice]
  6318. };
  6319. [price_data setObject:total_item forKey:@"item_4"];
  6320. [price_data setObject:@(price_data.allKeys.count) forKey:@"count"];
  6321. section_4 = @{
  6322. @"data":price_data,
  6323. @"title":@"Price Info",
  6324. @"type":@"price_info"
  6325. }.mutableCopy;
  6326. ret[@"section_4"] = section_4;
  6327. // ret[@"order_info"]= orderinfo;
  6328. section_0 = @{
  6329. @"data":orderinfo,
  6330. @"title":@"Order Info",
  6331. @"type":@"order_info"
  6332. }.mutableCopy;
  6333. ret[@"section_0"] = section_0;
  6334. // ret[@"more_order_info"] = moreInfo;
  6335. section_2 = @{
  6336. @"data":moreInfo,
  6337. @"title":@"More Info",
  6338. @"type":@"more_order_info"
  6339. }.mutableCopy;
  6340. ret[@"section_2"] = section_2;
  6341. ret[@"count"] = @(5);
  6342. return [RAConvertor dict2data:ret];
  6343. }
  6344. #pragma mark order list
  6345. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  6346. NSString *nsstatus = @"";
  6347. switch (status) {
  6348. case 0:
  6349. {
  6350. nsstatus=@"Temp Order";
  6351. break;
  6352. }
  6353. case 1:
  6354. {
  6355. nsstatus=@"Saved Order";
  6356. break;
  6357. }
  6358. case 2: {
  6359. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  6360. break;
  6361. }
  6362. case 3:
  6363. case 15:
  6364. {
  6365. nsstatus=@"Cancelled";
  6366. break;
  6367. }
  6368. case 10:
  6369. {
  6370. nsstatus=@"Quote Submitted";
  6371. break;
  6372. }
  6373. case 11:
  6374. {
  6375. nsstatus=@"Sales Order Submitted";
  6376. break;
  6377. }
  6378. case 12:
  6379. {
  6380. nsstatus=@"Processing";
  6381. break;
  6382. }
  6383. case 13:
  6384. {
  6385. nsstatus=@"Shipped";
  6386. break;
  6387. }
  6388. case 14:
  6389. {
  6390. nsstatus=@"Closed";
  6391. break;
  6392. }
  6393. case -11:
  6394. {
  6395. nsstatus = @"Ready For Submit";
  6396. break;
  6397. }
  6398. default:
  6399. break;
  6400. }
  6401. return nsstatus;
  6402. }
  6403. + (double) orderTotalPrice:(NSString *)so_id db:(sqlite3 *)db1 close:(BOOL)close{
  6404. double total = 0;
  6405. __block double payments_and_credist = 0;
  6406. __block double allItemPrice = 0;
  6407. // sqlite3 *db1 = [iSalesDB get_db];
  6408. if (so_id) {
  6409. // 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];
  6410. 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];
  6411. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6412. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6413. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  6414. int product_id = sqlite3_column_int(stmt, 0);
  6415. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  6416. int discount = sqlite3_column_int(stmt, 2);
  6417. int item_count = sqlite3_column_int(stmt, 3);
  6418. // int item_id = sqlite3_column_int(stmt, 7);
  6419. int item_id = sqlite3_column_int(stmt, 4);
  6420. char *customer_cid = (char *)sqlite3_column_text(stmt, 5);
  6421. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  6422. NSString* Price=nil;
  6423. if(str_price==nil)
  6424. {
  6425. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db1];
  6426. if(price==nil)
  6427. Price=@"No Price.";
  6428. else
  6429. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6430. }
  6431. else
  6432. {
  6433. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  6434. }
  6435. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID];
  6436. itemjson[@"The unit price"]=Price;
  6437. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  6438. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  6439. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6440. if(itemjson[@"combine"] != nil)
  6441. {
  6442. // int citem=0;
  6443. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  6444. for(int bc=0;bc<bcount;bc++)
  6445. {
  6446. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6447. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6448. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6449. subTotal += uprice * modulus * item_count;
  6450. }
  6451. }
  6452. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  6453. allItemPrice += subTotal;
  6454. }];
  6455. payments_and_credist = allItemPrice;
  6456. } else {
  6457. // payments/Credits
  6458. payments_and_credist = 0;
  6459. }
  6460. // lift_gate handlingFee shippingFee
  6461. __block double lift_gate = 0;
  6462. __block double handlingFee = 0;
  6463. __block double shippingFee = 0;
  6464. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  6465. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6466. int have_lift_gate = sqlite3_column_int(stmt, 0);
  6467. if (have_lift_gate) {
  6468. lift_gate = sqlite3_column_double(stmt, 1);
  6469. }
  6470. handlingFee = sqlite3_column_double(stmt, 2);
  6471. shippingFee = sqlite3_column_double(stmt, 3);
  6472. }];
  6473. // total
  6474. total = payments_and_credist + lift_gate + shippingFee + handlingFee;
  6475. if (close) {
  6476. [iSalesDB close_db:db1];
  6477. }
  6478. return total;
  6479. }
  6480. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  6481. {
  6482. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  6483. int limit = [[params valueForKey:@"limit"] intValue];
  6484. int offset = [[params valueForKey:@"offset"] intValue];
  6485. NSString* keyword = [params valueForKey:@"keyWord"];
  6486. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  6487. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  6488. NSString* where=@"1 = 1";
  6489. if(keyword.length>0)
  6490. 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]];
  6491. if (orderStatus.length > 0) {
  6492. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  6493. if (order_status_array.count == 1) {
  6494. int status_value = [[order_status_array firstObject] integerValue];
  6495. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  6496. if (status_value == 15 || status_value == 3) {
  6497. where = [where stringByAppendingString:[NSString stringWithFormat:@" and (o.status = 3 or o.erpOrderStatus = 15)"]];
  6498. } else {
  6499. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  6500. }
  6501. } else {
  6502. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  6503. }
  6504. } else if (order_status_array.count > 1) {
  6505. for (int i = 0; i < order_status_array.count;i++) {
  6506. NSString *status = order_status_array[i];
  6507. NSString *condition = @" or";
  6508. if (i == 0) {
  6509. condition = @" and (";
  6510. }
  6511. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  6512. int status_value = [status integerValue];
  6513. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  6514. if (status_value == 15 || status_value == 3) {
  6515. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ (o.status = 3 or o.erpOrderStatus = 15)",condition]];
  6516. } else {
  6517. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  6518. }
  6519. } else {
  6520. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  6521. }
  6522. }
  6523. where = [where stringByAppendingString:@" )"];
  6524. }
  6525. }
  6526. 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];
  6527. // DebugLog(@"order list sql: %@",sqlQuery);
  6528. sqlite3 *db = [iSalesDB get_db];
  6529. sqlite3_stmt * statement;
  6530. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  6531. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6532. {
  6533. int count=0;
  6534. while (sqlite3_step(statement) == SQLITE_ROW)
  6535. {
  6536. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  6537. int order_id = sqlite3_column_double(statement, 0);
  6538. char *soid = (char*)sqlite3_column_text(statement, 1);
  6539. if(soid==nil)
  6540. soid= "";
  6541. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  6542. int status = sqlite3_column_double(statement, 2);
  6543. int erpStatus = sqlite3_column_double(statement, 9);
  6544. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  6545. if(sales_rep==nil)
  6546. sales_rep= "";
  6547. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  6548. char *create_by = (char*)sqlite3_column_text(statement, 4);
  6549. if(create_by==nil)
  6550. create_by= "";
  6551. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  6552. char *company_name = (char*)sqlite3_column_text(statement, 5);
  6553. if(company_name==nil)
  6554. company_name= "";
  6555. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  6556. char *create_time = (char*)sqlite3_column_text(statement, 6);
  6557. if(create_time==nil)
  6558. create_time= "";
  6559. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  6560. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  6561. // double total_price = sqlite3_column_double(statement, 7);
  6562. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] db:db close:NO];
  6563. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  6564. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  6565. int offline_edit = sqlite3_column_int(statement, 10);
  6566. // ": "JH",
  6567. // "": "$8307.00",
  6568. // "": "MOB1608050001",
  6569. // "": "ArpithaT",
  6570. // "": "1st Stage Property Transformations",
  6571. // "": "JANICE SUTTON",
  6572. // "": 2255,
  6573. // "": "08/02/2016 09:49:18",
  6574. // "": 1,
  6575. // "": "Saved Order"
  6576. // "": "1470384050483",
  6577. // "model_count": "6 / 28"
  6578. item[@"sales_rep"]= nssales_rep;
  6579. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  6580. item[@"so#"]= nssoid;
  6581. item[@"create_by"]= nscreate_by;
  6582. item[@"customer_name"]= nscompany_name;
  6583. item[@"customer_contact"] = customer_contact;
  6584. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  6585. item[@"purchase_time"]= nscreate_time;
  6586. int statusCode = status;
  6587. if (statusCode == 2) {
  6588. statusCode = erpStatus;
  6589. } else if (statusCode == 3) {
  6590. statusCode = 15;
  6591. }
  6592. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",statusCode];
  6593. item[@"order_status"]= nsstatus;
  6594. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  6595. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  6596. // item[@"model_count"]
  6597. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  6598. count++;
  6599. }
  6600. ret[@"count"]= [NSNumber numberWithInt:count];
  6601. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  6602. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  6603. ret[@"result"]= [NSNumber numberWithInt:2];
  6604. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  6605. ret[@"time_zone"] = @"PST";
  6606. sqlite3_finalize(statement);
  6607. }
  6608. 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];
  6609. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  6610. [iSalesDB execSql:@"END TRANSACTION" db:db];
  6611. [iSalesDB close_db:db];
  6612. return [RAConvertor dict2data:ret];
  6613. }
  6614. #pragma mark update gnotes
  6615. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  6616. {
  6617. DebugLog(@"params: %@",params);
  6618. // comments = Meyoyoyoyoyoyoy;
  6619. // orderCode = MOB1608110001;
  6620. // password = 123456;
  6621. // user = EvanK;
  6622. 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]];
  6623. int ret = [iSalesDB execSql:sql];
  6624. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6625. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6626. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6627. // [dic setValue:@"160409" forKey:@"min_ver"];
  6628. return [RAConvertor dict2data:dic];
  6629. }
  6630. #pragma mark move to wishlist
  6631. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  6632. {
  6633. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  6634. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6635. _id = [NSString stringWithFormat:@"(%@)",_id];
  6636. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  6637. sqlite3 *db = [iSalesDB get_db];
  6638. __block NSString *product_id = @"";
  6639. __block NSString *item_count_str = @"";
  6640. // __block NSString *item_id = nil;
  6641. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6642. // product_id = [self textAtColumn:0 statement:stmt];
  6643. int item_count = sqlite3_column_int(stmt, 1);
  6644. // item_id = [self textAtColumn:2 statement:stmt];
  6645. NSString *p_id = [self textAtColumn:0 statement:stmt];
  6646. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  6647. if (p_id.length) {
  6648. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  6649. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  6650. }
  6651. }];
  6652. [iSalesDB close_db:db];
  6653. // 去除第一个,
  6654. if (product_id.length > 1) {
  6655. product_id = [product_id substringFromIndex:1];
  6656. }
  6657. if (item_count_str.length > 1) {
  6658. item_count_str = [item_count_str substringFromIndex:1];
  6659. }
  6660. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6661. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  6662. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  6663. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  6664. sqlite3 *db1 = [iSalesDB get_db];
  6665. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  6666. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6667. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  6668. // 删除
  6669. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  6670. int ret = [iSalesDB execSql:deleteSql db:db1];
  6671. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6672. [iSalesDB close_db:db1];
  6673. return [RAConvertor dict2data:dic];
  6674. }
  6675. #pragma mark cart delete
  6676. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  6677. {
  6678. // cartItemId = 548;
  6679. // orderCode = MOB1608110001;
  6680. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  6681. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6682. _id = [NSString stringWithFormat:@"(%@)",_id];
  6683. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  6684. int ret = [iSalesDB execSql:sql];
  6685. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6686. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6687. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6688. // [dic setValue:@"160409" forKey:@"min_ver"];
  6689. return [RAConvertor dict2data:dic];
  6690. }
  6691. #pragma mark set price
  6692. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  6693. {
  6694. DebugLog(@"cart set price params: %@",params);
  6695. // "cartitem_id" = 1;
  6696. // discount = "0.000000";
  6697. // "item_note" = "";
  6698. // price = "269.000000";
  6699. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6700. NSString *notes = [self valueInParams:params key:@"item_note"];
  6701. NSString *discount = [self valueInParams:params key:@"discount"];
  6702. NSString *price = [self valueInParams:params key:@"price"];
  6703. // bool badd_price_changed=false;
  6704. // sqlite3* db=[iSalesDB get_db];
  6705. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6706. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  6707. // NSRange range;
  6708. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  6709. //
  6710. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  6711. // badd_price_changed=true;
  6712. // [iSalesDB close_db:db];
  6713. //
  6714. // if(badd_price_changed)
  6715. // {
  6716. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  6717. // }
  6718. //
  6719. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  6720. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  6721. int ret = [iSalesDB execSql:sql];
  6722. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6723. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6724. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6725. // [dic setValue:@"160409" forKey:@"min_ver"];
  6726. return [RAConvertor dict2data:dic];
  6727. }
  6728. #pragma mark set line notes
  6729. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  6730. {
  6731. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6732. NSString *notes = [self valueInParams:params key:@"notes"];
  6733. notes = [self translateSingleQuote:notes];
  6734. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  6735. int ret = [iSalesDB execSql:sql];
  6736. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6737. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6738. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6739. // [dic setValue:@"160409" forKey:@"min_ver"];
  6740. return [RAConvertor dict2data:dic];
  6741. }
  6742. #pragma mark set qty
  6743. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  6744. {
  6745. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6746. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6747. int item_count = [params[@"inputInt"] intValue];
  6748. // 购买检查数量大于库存
  6749. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6750. if (!appDelegate.can_create_backorder) {
  6751. NSString *checkSql = [NSString stringWithFormat:@"select p.availability from product as p left join offline_cart as c on c.item_id = p.item_id where c._id = %@;",_id];
  6752. __block BOOL out_of_stock = NO;
  6753. [iSalesDB jk_query:checkSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6754. int availability = sqlite3_column_int(stmt, 0);
  6755. if (availability < item_count) {
  6756. out_of_stock = YES;
  6757. }
  6758. }];
  6759. if (out_of_stock) { // 缺货
  6760. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"result"];
  6761. [dic setObject:@"Item is out of stock" forKey:@"err_msg"];
  6762. return [RAConvertor dict2data:dic];
  6763. }
  6764. }
  6765. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  6766. int ret = [iSalesDB execSql:sql];
  6767. __block int item_id = 0;
  6768. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6769. item_id = sqlite3_column_int(stmt, 0);
  6770. }];
  6771. sqlite3 *db = [iSalesDB get_db];
  6772. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  6773. [iSalesDB close_db:db];
  6774. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6775. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6776. // [dic setValue:@"160409" forKey:@"min_ver"];
  6777. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  6778. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  6779. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  6780. return [RAConvertor dict2data:dic];
  6781. }
  6782. #pragma mark place order
  6783. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  6784. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6785. if (aname) {
  6786. [dic setValue:aname forKey:@"aname"];
  6787. }
  6788. if (control) {
  6789. [dic setValue:control forKey:@"control"];
  6790. }
  6791. if (keyboard) {
  6792. [dic setValue:keyboard forKey:@"keyboard"];
  6793. }
  6794. if (name) {
  6795. [dic setValue:name forKey:@"name"];
  6796. }
  6797. if (value) {
  6798. [dic setValue:value forKey:@"value"];
  6799. }
  6800. return dic;
  6801. }
  6802. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  6803. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6804. orderCode = [self translateSingleQuote:orderCode];
  6805. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  6806. 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];
  6807. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6808. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  6809. NSString *img0 = [self textAtColumn:1 statement:stmt];
  6810. NSString *img1 = [self textAtColumn:2 statement:stmt];
  6811. NSString *img2 = [self textAtColumn:3 statement:stmt];
  6812. NSString *company_name = [self textAtColumn:4 statement:stmt];
  6813. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  6814. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  6815. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  6816. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  6817. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  6818. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  6819. NSString *email = [self textAtColumn:11 statement:stmt];
  6820. NSString *phone = [self textAtColumn:12 statement:stmt];
  6821. NSString *fax = [self textAtColumn:13 statement:stmt];
  6822. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  6823. NSString *city = [self textAtColumn:15 statement:stmt];
  6824. NSString *state = [self textAtColumn:16 statement:stmt];
  6825. NSString *country = [self textAtColumn:17 statement:stmt];
  6826. NSString *name = [self textAtColumn:18 statement:stmt];
  6827. // contact id
  6828. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  6829. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  6830. [contact_id_dic setValue:@"text" forKey:@"control"];
  6831. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  6832. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  6833. [contact_id_dic setValue:@"true" forKey:@"required"];
  6834. [contact_id_dic setValue:contact_id forKey:@"value"];
  6835. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  6836. // business card
  6837. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  6838. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  6839. [business_card_dic setValue:@"img" forKey:@"control"];
  6840. [business_card_dic setValue:@"1" forKey:@"disable"];
  6841. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  6842. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  6843. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  6844. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  6845. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  6846. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  6847. [business_card_dic setValue:@"business_card" forKey:@"name"];
  6848. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  6849. // fax
  6850. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6851. [customer_dic setValue:fax_dic forKey:@"item_10"];
  6852. // zipcode
  6853. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  6854. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  6855. // city
  6856. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  6857. [customer_dic setValue:city_dic forKey:@"item_12"];
  6858. // state
  6859. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  6860. [customer_dic setValue:state_dic forKey:@"item_13"];
  6861. // country
  6862. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  6863. [customer_dic setValue:country_dic forKey:@"item_14"];
  6864. // company name
  6865. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  6866. [customer_dic setValue:company_dic forKey:@"item_2"];
  6867. // addr_1
  6868. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  6869. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  6870. // addr_2
  6871. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  6872. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  6873. // addr_3
  6874. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  6875. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  6876. // addr_4
  6877. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  6878. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  6879. // Contact
  6880. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  6881. [customer_dic setValue:contact_dic forKey:@"item_7"];
  6882. // email
  6883. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6884. [customer_dic setValue:email_dic forKey:@"item_8"];
  6885. // phone
  6886. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6887. [customer_dic setValue:phone_dic forKey:@"item_9"];
  6888. // title
  6889. [customer_dic setValue:@"Customer" forKey:@"title"];
  6890. // count
  6891. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  6892. }];
  6893. // setting
  6894. NSDictionary *setting = params[@"setting"];
  6895. NSNumber *hide = setting[@"CustomerHide"];
  6896. [customer_dic setValue:hide forKey:@"hide"];
  6897. return customer_dic;
  6898. }
  6899. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  6900. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6901. // setting
  6902. NSDictionary *setting = params[@"setting"];
  6903. NSNumber *hide = setting[@"ShipToHide"];
  6904. [dic setValue:hide forKey:@"hide"];
  6905. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6906. orderCode = [self translateSingleQuote:orderCode];
  6907. 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];
  6908. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6909. NSString *cid = [self textAtColumn:0 statement:stmt];
  6910. NSString *name = [self textAtColumn:1 statement:stmt];
  6911. NSString *ext = [self textAtColumn:2 statement:stmt];
  6912. NSString *contact = [self textAtColumn:3 statement:stmt];
  6913. NSString *email = [self textAtColumn:4 statement:stmt];
  6914. NSString *fax = [self textAtColumn:5 statement:stmt];
  6915. NSString *phone = [self textAtColumn:6 statement:stmt];
  6916. // count
  6917. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6918. // title
  6919. [dic setValue:@"Ship To" forKey:@"title"];
  6920. // choose
  6921. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6922. [choose_dic setValue:@"choose" forKey:@"aname"];
  6923. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6924. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  6925. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  6926. @"key_map" : @{@"receive_cid" : @"customer_cid",
  6927. @"receive_contact" : @"customer_contact",
  6928. @"receive_email" : @"customer_email",
  6929. @"receive_ext" : @"customer_contact_ext",
  6930. @"receive_fax" : @"customer_fax",
  6931. @"receive_name" : @"customer_name",
  6932. @"receive_phone" : @"customer_phone"},
  6933. @"refresh" : [NSNumber numberWithInteger:1],
  6934. @"type" : @"pull"};
  6935. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  6936. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  6937. @"name" : @"Add new address",
  6938. @"refresh" : [NSNumber numberWithInteger:1],
  6939. @"value" : @"new_addr"
  6940. };
  6941. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  6942. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  6943. @"key_map" : @{@"receive_cid" : @"customer_cid",
  6944. @"receive_contact" : @"customer_contact",
  6945. @"receive_email" : @"customer_email",
  6946. @"receive_ext" : @"customer_contact_ext",
  6947. @"receive_fax" : @"customer_fax",
  6948. @"receive_name" : @"customer_name",
  6949. @"receive_phone" : @"customer_phone"},
  6950. @"name" : @"select_ship_to",
  6951. @"refresh" : [NSNumber numberWithInteger:1],
  6952. @"value" : @"Sales_Order_Ship_To"};
  6953. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  6954. [dic setValue:choose_dic forKey:@"item_0"];
  6955. // contact id
  6956. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6957. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6958. [contact_id_dic setValue:@"true" forKey:@"required"];
  6959. [dic setValue:contact_id_dic forKey:@"item_1"];
  6960. // company name
  6961. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6962. [dic setValue:company_name_dic forKey:@"item_2"];
  6963. // address
  6964. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  6965. NSNumber *required = setting[@"ShippingToAddressRequire"];
  6966. if ([required integerValue]) {
  6967. [ext_dic setValue:@"true" forKey:@"required"];
  6968. }
  6969. [dic setValue:ext_dic forKey:@"item_3"];
  6970. // contact
  6971. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6972. [dic setValue:contact_dic forKey:@"item_4"];
  6973. // phone
  6974. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6975. [dic setValue:phone_dic forKey:@"item_5"];
  6976. // fax
  6977. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6978. [dic setValue:fax_dic forKey:@"item_6"];
  6979. // email
  6980. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6981. [dic setValue:email_dic forKey:@"item_7"];
  6982. }];
  6983. return dic;
  6984. }
  6985. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  6986. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6987. // setting
  6988. NSDictionary *setting = params[@"setting"];
  6989. NSNumber *hide = setting[@"ShipFromHide"];
  6990. [dic setValue:hide forKey:@"hide"];
  6991. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6992. orderCode = [self translateSingleQuote:orderCode];
  6993. 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];
  6994. 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';";
  6995. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6996. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  6997. __block NSString *name = [self textAtColumn:1 statement:stmt];
  6998. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  6999. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  7000. __block NSString *email = [self textAtColumn:4 statement:stmt];
  7001. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  7002. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  7003. if (!cid.length) {
  7004. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  7005. cid = [self textAtColumn:0 statement:statment];
  7006. name = [self textAtColumn:1 statement:statment];
  7007. ext = [self textAtColumn:2 statement:statment];
  7008. contact = [self textAtColumn:3 statement:statment];
  7009. email = [self textAtColumn:4 statement:statment];
  7010. fax = [self textAtColumn:5 statement:statment];
  7011. phone = [self textAtColumn:6 statement:statment];
  7012. }];
  7013. }
  7014. // count
  7015. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7016. // title
  7017. [dic setValue:@"Ship From" forKey:@"title"];
  7018. // hide
  7019. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7020. // choose
  7021. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7022. [choose_dic setValue:@"choose" forKey:@"aname"];
  7023. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7024. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7025. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  7026. @"key_map" : @{@"sender_cid" : @"customer_cid",
  7027. @"sender_contact" : @"customer_contact",
  7028. @"sender_email" : @"customer_email",
  7029. @"sender_ext" : @"customer_contact_ext",
  7030. @"sender_fax" : @"customer_fax",
  7031. @"sender_name" : @"customer_name",
  7032. @"sender_phone" : @"customer_phone"},
  7033. @"name" : @"select_cid",
  7034. @"refresh" : [NSNumber numberWithInteger:0],
  7035. @"value" : @"Sales_Order_Ship_From"};
  7036. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  7037. [dic setValue:choose_dic forKey:@"item_0"];
  7038. // contact id
  7039. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7040. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7041. [contact_id_dic setValue:@"true" forKey:@"required"];
  7042. [dic setValue:contact_id_dic forKey:@"item_1"];
  7043. // company name
  7044. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7045. [dic setValue:company_name_dic forKey:@"item_2"];
  7046. // address
  7047. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  7048. [dic setValue:ext_dic forKey:@"item_3"];
  7049. // contact
  7050. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7051. [dic setValue:contact_dic forKey:@"item_4"];
  7052. // phone
  7053. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7054. [dic setValue:phone_dic forKey:@"item_5"];
  7055. // fax
  7056. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7057. [dic setValue:fax_dic forKey:@"item_6"];
  7058. // email
  7059. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7060. [dic setValue:email_dic forKey:@"item_7"];
  7061. }];
  7062. return dic;
  7063. }
  7064. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  7065. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7066. // setting
  7067. NSDictionary *setting = params[@"setting"];
  7068. NSNumber *hide = setting[@"FreightBillToHide"];
  7069. [dic setValue:hide forKey:@"hide"];
  7070. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7071. orderCode = [self translateSingleQuote:orderCode];
  7072. 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];
  7073. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7074. NSString *cid = [self textAtColumn:0 statement:stmt];
  7075. NSString *name = [self textAtColumn:1 statement:stmt];
  7076. NSString *ext = [self textAtColumn:2 statement:stmt];
  7077. NSString *contact = [self textAtColumn:3 statement:stmt];
  7078. NSString *email = [self textAtColumn:4 statement:stmt];
  7079. NSString *fax = [self textAtColumn:5 statement:stmt];
  7080. NSString *phone = [self textAtColumn:6 statement:stmt];
  7081. // count
  7082. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7083. // title
  7084. [dic setValue:@"Freight Bill To" forKey:@"title"];
  7085. // hide
  7086. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7087. // choose
  7088. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7089. [choose_dic setValue:@"choose" forKey:@"aname"];
  7090. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7091. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  7092. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  7093. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  7094. @"shipping_billto_contact" : @"receive_contact",
  7095. @"shipping_billto_email" : @"receive_email",
  7096. @"shipping_billto_ext" : @"receive_ext",
  7097. @"shipping_billto_fax" : @"receive_fax",
  7098. @"shipping_billto_name" : @"receive_name",
  7099. @"shipping_billto_phone" : @"receive_phone"},
  7100. @"type" : @"pull"};
  7101. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  7102. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7103. @"type" : @"pull",
  7104. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  7105. @"shipping_billto_contact" : @"customer_contact",
  7106. @"shipping_billto_email" : @"customer_email",
  7107. @"shipping_billto_ext" : @"customer_contact_ext",
  7108. @"shipping_billto_fax" : @"customer_fax",
  7109. @"shipping_billto_name" : @"customer_name",
  7110. @"shipping_billto_phone" : @"customer_phone"}
  7111. };
  7112. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  7113. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  7114. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  7115. @"shipping_billto_contact" : @"sender_contact",
  7116. @"shipping_billto_email" : @"sender_email",
  7117. @"shipping_billto_ext" : @"sender_ext",
  7118. @"shipping_billto_fax" : @"sender_fax",
  7119. @"shipping_billto_name" : @"sender_name",
  7120. @"shipping_billto_phone" : @"sender_phone"},
  7121. @"type" : @"pull"
  7122. };
  7123. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  7124. NSDictionary *select_freight_bill_to_dic = @{
  7125. @"aname" : @"Select freight bill to",
  7126. @"name" : @"select_cid",
  7127. @"refresh" : [NSNumber numberWithInteger:0],
  7128. @"value" : @"Sales_Order_Freight_Bill_To",
  7129. @"key_map" : @{
  7130. @"shipping_billto_cid" : @"customer_cid",
  7131. @"shipping_billto_contact" : @"customer_contact",
  7132. @"shipping_billto_email" : @"customer_email",
  7133. @"shipping_billto_ext" : @"customer_contact_ext",
  7134. @"shipping_billto_fax" : @"customer_fax",
  7135. @"shipping_billto_name" : @"customer_name",
  7136. @"shipping_billto_phone" : @"customer_phone"
  7137. }
  7138. };
  7139. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  7140. [dic setValue:choose_dic forKey:@"item_0"];
  7141. // contact id
  7142. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7143. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7144. [contact_id_dic setValue:@"true" forKey:@"required"];
  7145. [dic setValue:contact_id_dic forKey:@"item_1"];
  7146. // company name
  7147. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7148. [dic setValue:company_name_dic forKey:@"item_2"];
  7149. // address
  7150. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7151. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  7152. if ([ext_required integerValue]) {
  7153. [ext_dic setValue:@"true" forKey:@"required"];
  7154. }
  7155. [dic setValue:ext_dic forKey:@"item_3"];
  7156. // contact
  7157. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7158. [dic setValue:contact_dic forKey:@"item_4"];
  7159. // phone
  7160. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7161. [dic setValue:phone_dic forKey:@"item_5"];
  7162. // fax
  7163. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7164. [dic setValue:fax_dic forKey:@"item_6"];
  7165. // email
  7166. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7167. [dic setValue:email_dic forKey:@"item_7"];
  7168. }];
  7169. return dic;
  7170. }
  7171. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7172. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7173. // setting
  7174. NSDictionary *setting = params[@"setting"];
  7175. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  7176. [dic setValue:hide forKey:@"hide"];
  7177. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7178. orderCode = [self translateSingleQuote:orderCode];
  7179. 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];
  7180. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7181. NSString *cid = [self textAtColumn:0 statement:stmt];
  7182. NSString *name = [self textAtColumn:1 statement:stmt];
  7183. NSString *ext = [self textAtColumn:2 statement:stmt];
  7184. NSString *contact = [self textAtColumn:3 statement:stmt];
  7185. NSString *email = [self textAtColumn:4 statement:stmt];
  7186. NSString *fax = [self textAtColumn:5 statement:stmt];
  7187. NSString *phone = [self textAtColumn:6 statement:stmt];
  7188. // count
  7189. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7190. // title
  7191. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  7192. // hide
  7193. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7194. // choose
  7195. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7196. [choose_dic setValue:@"choose" forKey:@"aname"];
  7197. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7198. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  7199. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  7200. @"key_map" : @{@"billing_cid" : @"receive_cid",
  7201. @"billing_contact" : @"receive_contact",
  7202. @"billing_email" : @"receive_email",
  7203. @"billing_ext" : @"receive_ext",
  7204. @"billing_fax" : @"receive_fax",
  7205. @"billing_name" : @"receive_name",
  7206. @"billing_phone" : @"receive_phone"},
  7207. @"type" : @"pull"};
  7208. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  7209. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7210. @"type" : @"pull",
  7211. @"key_map" : @{@"billing_cid" : @"customer_cid",
  7212. @"billing_contact" : @"customer_contact",
  7213. @"billing_email" : @"customer_email",
  7214. @"billing_ext" : @"customer_contact_ext",
  7215. @"billing_fax" : @"customer_fax",
  7216. @"billing_name" : @"customer_name",
  7217. @"billing_phone" : @"customer_phone"}
  7218. };
  7219. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  7220. NSDictionary *select_bill_to_dic = @{
  7221. @"aname" : @"Select bill to",
  7222. @"name" : @"select_cid",
  7223. @"refresh" : [NSNumber numberWithInteger:0],
  7224. @"value" : @"Sales_Order_Merchandise_Bill_To",
  7225. @"key_map" : @{
  7226. @"billing_cid" : @"customer_cid",
  7227. @"billing_contact" : @"customer_contact",
  7228. @"billing_email" : @"customer_email",
  7229. @"billing_ext" : @"customer_contact_ext",
  7230. @"billing_fax" : @"customer_fax",
  7231. @"billing_name" : @"customer_name",
  7232. @"billing_phone" : @"customer_phone"
  7233. }
  7234. };
  7235. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  7236. [dic setValue:choose_dic forKey:@"item_0"];
  7237. // contact id
  7238. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7239. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7240. [contact_id_dic setValue:@"true" forKey:@"required"];
  7241. [dic setValue:contact_id_dic forKey:@"item_1"];
  7242. // company name
  7243. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7244. [dic setValue:company_name_dic forKey:@"item_2"];
  7245. // address
  7246. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7247. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  7248. if ([ext_required integerValue]) {
  7249. [ext_dic setValue:@"true" forKey:@"required"];
  7250. }
  7251. [dic setValue:ext_dic forKey:@"item_3"];
  7252. // contact
  7253. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7254. [dic setValue:contact_dic forKey:@"item_4"];
  7255. // phone
  7256. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7257. [dic setValue:phone_dic forKey:@"item_5"];
  7258. // fax
  7259. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7260. [dic setValue:fax_dic forKey:@"item_6"];
  7261. // email
  7262. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7263. [dic setValue:email_dic forKey:@"item_7"];
  7264. }];
  7265. return dic;
  7266. }
  7267. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7268. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7269. // setting
  7270. NSDictionary *setting = params[@"setting"];
  7271. NSNumber *hide = setting[@"ReturnToHide"];
  7272. [dic setValue:hide forKey:@"hide"];
  7273. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7274. orderCode = [self translateSingleQuote:orderCode];
  7275. 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];
  7276. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7277. NSString *cid = [self textAtColumn:0 statement:stmt];
  7278. NSString *name = [self textAtColumn:1 statement:stmt];
  7279. NSString *ext = [self textAtColumn:2 statement:stmt];
  7280. NSString *contact = [self textAtColumn:3 statement:stmt];
  7281. NSString *email = [self textAtColumn:4 statement:stmt];
  7282. NSString *fax = [self textAtColumn:5 statement:stmt];
  7283. NSString *phone = [self textAtColumn:6 statement:stmt];
  7284. // count
  7285. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7286. // title
  7287. [dic setValue:@"Return To" forKey:@"title"];
  7288. // hide
  7289. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7290. // choose
  7291. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7292. [choose_dic setValue:@"choose" forKey:@"aname"];
  7293. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7294. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  7295. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  7296. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  7297. @"returnto_contact" : @"sender_contact",
  7298. @"returnto_email" : @"sender_email",
  7299. @"returnto_ext" : @"sender_ext",
  7300. @"returnto_fax" : @"sender_fax",
  7301. @"returnto_name" : @"sender_name",
  7302. @"returnto_phone" : @"sender_phone"},
  7303. @"type" : @"pull"};
  7304. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  7305. NSDictionary *select_return_to_dic = @{
  7306. @"aname" : @"Select return to",
  7307. @"name" : @"select_cid",
  7308. @"refresh" : [NSNumber numberWithInteger:0],
  7309. @"value" : @"Contact_Return_To",
  7310. @"key_map" : @{
  7311. @"returnto_cid" : @"customer_cid",
  7312. @"returnto_contact" : @"customer_contact",
  7313. @"returnto_email" : @"customer_email",
  7314. @"returnto_ext" : @"customer_contact_ext",
  7315. @"returnto_fax" : @"customer_fax",
  7316. @"returnto_name" : @"customer_name",
  7317. @"returnto_phone" : @"customer_phone"
  7318. }
  7319. };
  7320. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  7321. [dic setValue:choose_dic forKey:@"item_0"];
  7322. // contact id
  7323. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7324. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7325. [contact_id_dic setValue:@"true" forKey:@"required"];
  7326. [dic setValue:contact_id_dic forKey:@"item_1"];
  7327. // company name
  7328. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7329. [dic setValue:company_name_dic forKey:@"item_2"];
  7330. // address
  7331. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  7332. [dic setValue:ext_dic forKey:@"item_3"];
  7333. // contact
  7334. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7335. [dic setValue:contact_dic forKey:@"item_4"];
  7336. // phone
  7337. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7338. [dic setValue:phone_dic forKey:@"item_5"];
  7339. // fax
  7340. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7341. [dic setValue:fax_dic forKey:@"item_6"];
  7342. // email
  7343. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7344. [dic setValue:email_dic forKey:@"item_7"];
  7345. }];
  7346. return dic;
  7347. }
  7348. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  7349. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7350. orderCode = [self translateSingleQuote:orderCode];
  7351. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7352. __block double TotalCuft = 0;
  7353. __block double TotalWeight = 0;
  7354. __block int TotalCarton = 0;
  7355. __block double payments = 0;
  7356. // setting
  7357. NSDictionary *setting = params[@"setting"];
  7358. NSNumber *hide = setting[@"ModelInformationHide"];
  7359. [dic setValue:hide forKey:@"hide"];
  7360. 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];
  7361. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7362. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7363. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  7364. // item id
  7365. int item_id = sqlite3_column_int(stmt, 0);
  7366. // count
  7367. int item_count = sqlite3_column_int(stmt, 1);
  7368. // stockUom
  7369. int stockUom = sqlite3_column_int(stmt, 2);
  7370. // unit price
  7371. NSString *str_price = [self textAtColumn:3 statement:stmt];
  7372. NSString* Price=nil;
  7373. if([str_price isEqualToString:@""])
  7374. {
  7375. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  7376. if(price==nil)
  7377. Price=@"No Price.";
  7378. else
  7379. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7380. }
  7381. else
  7382. {
  7383. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  7384. }
  7385. // discount
  7386. double discount = sqlite3_column_double(stmt, 4);
  7387. // name
  7388. NSString *name = [self textAtColumn:5 statement:stmt];
  7389. // description
  7390. NSString *description = [self textAtColumn:6 statement:stmt];
  7391. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  7392. // line note
  7393. NSString *line_note = [self textAtColumn:7 statement:stmt];
  7394. int avaulability = sqlite3_column_int(stmt, 8);
  7395. // img
  7396. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  7397. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  7398. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7399. if(combine != nil)
  7400. {
  7401. // int citem=0;
  7402. int bcount=[[combine valueForKey:@"count"] intValue];
  7403. for(int bc=0;bc<bcount;bc++)
  7404. {
  7405. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7406. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7407. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7408. subTotal += uprice * modulus * item_count;
  7409. }
  7410. }
  7411. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  7412. [model_dic setValue:@"model" forKey:@"control"];
  7413. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  7414. [model_dic setValue:description forKey:@"description"];
  7415. [model_dic setValue:line_note forKey:@"note"];
  7416. [model_dic setValue:img forKey:@"img_url"];
  7417. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  7418. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  7419. [model_dic setValue:Price forKey:@"unit_price"];
  7420. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  7421. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  7422. if (combine) {
  7423. [model_dic setValue:combine forKey:@"combine"];
  7424. }
  7425. // well,what under the row is the info for total
  7426. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  7427. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  7428. double weight=[bsubtotaljson[@"weight"] doubleValue];
  7429. int carton=[bsubtotaljson[@"carton"] intValue];
  7430. TotalCuft += cuft;
  7431. TotalWeight += weight;
  7432. TotalCarton += carton;
  7433. payments += subTotal;
  7434. //---------------------------
  7435. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  7436. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  7437. }];
  7438. [dic setValue:@"Model Information" forKey:@"title"];
  7439. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  7440. return dic;
  7441. }
  7442. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  7443. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7444. [dic setValue:@"Remarks Content" forKey:@"title"];
  7445. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  7446. // setting
  7447. NSDictionary *setting = params[@"setting"];
  7448. NSNumber *hide = setting[@"RemarksContentHide"];
  7449. [dic setValue:hide forKey:@"hide"];
  7450. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7451. orderCode = [self translateSingleQuote:orderCode];
  7452. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes from offline_order where so_id = '%@';",orderCode];
  7453. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7454. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  7455. int mustCall = sqlite3_column_int(stmt, 1);
  7456. NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  7457. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  7458. NSDictionary *po_dic = @{
  7459. @"aname" : @"PO#",
  7460. @"control" : @"edit",
  7461. @"keyboard" : @"text",
  7462. @"name" : @"poNumber",
  7463. @"value" : poNumber
  7464. };
  7465. NSDictionary *must_call_dic = @{
  7466. @"aname" : @"MUST CALL BEFORE DELIVERY",
  7467. @"control" : @"switch",
  7468. @"name" : @"must_call",
  7469. @"value" : mustCall ? @"true" : @"false"
  7470. };
  7471. NSDictionary *general_notes_dic = @{
  7472. @"aname" : @"General notes",
  7473. @"control" : @"text_view",
  7474. @"keyboard" : @"text",
  7475. @"name" : @"comments",
  7476. @"value" : generalNotes
  7477. };
  7478. NSDictionary *internal_notes_dic = @{
  7479. @"aname" : @"Internal notes",
  7480. @"control" : @"text_view",
  7481. @"keyboard" : @"text",
  7482. @"name" : @"internal_notes",
  7483. @"value" : internalNotes
  7484. };
  7485. [dic setValue:po_dic forKey:@"item_0"];
  7486. [dic setValue:must_call_dic forKey:@"item_1"];
  7487. [dic setValue:general_notes_dic forKey:@"item_2"];
  7488. [dic setValue:internal_notes_dic forKey:@"item_3"];
  7489. }];
  7490. return dic;
  7491. }
  7492. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  7493. // params
  7494. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7495. orderCode = [self translateSingleQuote:orderCode];
  7496. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7497. // setting
  7498. NSDictionary *setting = params[@"setting"];
  7499. NSNumber *hide = setting[@"OrderTotalHide"];
  7500. [dic setValue:hide forKey:@"hide"];
  7501. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  7502. __block double lift_gate_value = 0;
  7503. __block double handling_fee = 0;
  7504. __block double shipping = 0;
  7505. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7506. int lift_gate = sqlite3_column_int(stmt, 0);
  7507. lift_gate_value = sqlite3_column_double(stmt, 1);
  7508. shipping = sqlite3_column_double(stmt, 2);
  7509. handling_fee = sqlite3_column_double(stmt, 3);
  7510. if (!lift_gate) {
  7511. lift_gate_value = 0;
  7512. }
  7513. }];
  7514. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  7515. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  7516. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  7517. double payments = [[total valueForKey:@"payments"] doubleValue];
  7518. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  7519. double totalPrice = payments;
  7520. [dic setValue:@"Order Total" forKey:@"title"];
  7521. NSDictionary *payments_dic = @{
  7522. @"align" : @"right",
  7523. @"aname" : @"Payments/Credits",
  7524. @"control" : @"text",
  7525. @"name" : @"paymentsAndCredits",
  7526. @"type" : @"price",
  7527. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  7528. };
  7529. [dic setValue:payments_dic forKey:@"item_0"];
  7530. // version 1.71 remove
  7531. // NSDictionary *handling_fee_dic = @{
  7532. // @"align" : @"right",
  7533. // @"aname" : @"Handling Fee",
  7534. // @"control" : @"text",
  7535. // @"name" : @"handling_fee_value",
  7536. // @"required" : @"true",
  7537. // @"type" : @"price",
  7538. // @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  7539. // };
  7540. NSDictionary *shipping_dic = @{
  7541. @"align" : @"right",
  7542. @"aname" : @"Shipping*",
  7543. @"control" : @"text",
  7544. @"name" : @"shipping",
  7545. @"required" : @"true",
  7546. @"type" : @"price",
  7547. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  7548. };
  7549. NSDictionary *lift_gate_dic = @{
  7550. @"align" : @"right",
  7551. @"aname" : @"Liftgate Fee(No Loading Dock)",
  7552. @"control" : @"text",
  7553. @"name" : @"lift_gate_value",
  7554. @"required" : @"true",
  7555. @"type" : @"price",
  7556. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  7557. };
  7558. int item_count = 1;
  7559. if (![[params objectForKey:@"logist"] isEqualToString:@"WILL CALL"]) {
  7560. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  7561. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  7562. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7563. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7564. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  7565. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7566. } else {
  7567. }
  7568. }
  7569. // version 1.71 remove
  7570. // [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7571. NSDictionary *total_price_dic = @{
  7572. @"align" : @"right",
  7573. @"aname" : @"Total",
  7574. @"control" : @"text",
  7575. @"name" : @"totalPrice",
  7576. @"type" : @"price",
  7577. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  7578. };
  7579. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7580. NSDictionary *total_cuft_dic = @{
  7581. @"align" : @"right",
  7582. @"aname" : @"Total Cuft",
  7583. @"control" : @"text",
  7584. @"name" : @"",
  7585. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  7586. };
  7587. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7588. NSDictionary *total_weight_dic = @{
  7589. @"align" : @"right",
  7590. @"aname" : @"Total Weight",
  7591. @"control" : @"text",
  7592. @"name" : @"",
  7593. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  7594. };
  7595. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7596. NSDictionary *total_carton_dic = @{
  7597. @"align" : @"right",
  7598. @"aname" : @"Total Carton",
  7599. @"control" : @"text",
  7600. @"name" : @"",
  7601. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  7602. };
  7603. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7604. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  7605. return dic;
  7606. }
  7607. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  7608. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7609. orderCode = [self translateSingleQuote:orderCode];
  7610. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7611. // setting
  7612. NSDictionary *setting = params[@"setting"];
  7613. NSNumber *hide = setting[@"SignatureHide"];
  7614. [dic setValue:hide forKey:@"hide"];
  7615. [dic setValue:@"Signature" forKey:@"title"];
  7616. __block NSString *pic_path = @"";
  7617. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  7618. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7619. pic_path = [self textAtColumn:0 statement:stmt];
  7620. }];
  7621. NSDictionary *pic_dic = @{
  7622. @"aname" : @"Signature",
  7623. @"avalue" :pic_path,
  7624. @"control" : @"signature",
  7625. @"name" : @"sign_picpath",
  7626. @"value" : pic_path
  7627. };
  7628. [dic setValue:pic_dic forKey:@"item_0"];
  7629. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7630. return dic;
  7631. }
  7632. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  7633. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7634. // setting
  7635. NSDictionary *setting = params[@"setting"];
  7636. NSNumber *hide = setting[@"ShippingMethodHide"];
  7637. [dic setValue:hide forKey:@"hide"];
  7638. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7639. orderCode = [self translateSingleQuote:orderCode];
  7640. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  7641. __block NSString *logist = @"";
  7642. __block NSString *lift_gate = @"";
  7643. __block int lift_gate_integer = 0;
  7644. __block NSString *logistic_note = @"";
  7645. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7646. logist = [self textAtColumn:0 statement:stmt];
  7647. lift_gate_integer = sqlite3_column_int(stmt, 1);
  7648. logistic_note = [self textAtColumn:2 statement:stmt];
  7649. }];
  7650. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7651. [dic setValue:@"Shipping Method" forKey:@"title"];
  7652. // val_0
  7653. int PERSONAL_PICK_UP_check = 0;
  7654. int USE_MY_CARRIER_check = 0;
  7655. NSString *logistic_note_text = @"";
  7656. int will_call_check = 0;
  7657. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  7658. will_call_check = 1;
  7659. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  7660. PERSONAL_PICK_UP_check = 1;
  7661. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  7662. USE_MY_CARRIER_check = 1;
  7663. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  7664. logistic_note = [logistic_note_array firstObject];
  7665. if (logistic_note_array.count > 1) {
  7666. logistic_note_text = [logistic_note_array lastObject];
  7667. }
  7668. }
  7669. }
  7670. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  7671. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  7672. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  7673. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  7674. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  7675. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7676. NSDictionary *val0_subItem_item0 = @{
  7677. @"aname" : @"Option",
  7678. @"cadedate" : @{
  7679. @"count" : [NSNumber numberWithInteger:2],
  7680. @"val_0" : @{
  7681. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  7682. @"refresh" : [NSNumber numberWithInteger:0],
  7683. @"value" : @"PERSONAL PICK UP",
  7684. @"value_id" : @"PERSONAL PICK UP"
  7685. },
  7686. @"val_1" : @{
  7687. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  7688. @"refresh" : [NSNumber numberWithInteger:0],
  7689. @"sub_item" : @{
  7690. @"count" : [NSNumber numberWithInteger:1],
  7691. @"item_0" : @{
  7692. @"aname" : @"BOL",
  7693. @"control" : @"edit",
  7694. @"keyboard" : @"text",
  7695. @"name" : @"logist_note_text",
  7696. @"refresh" : [NSNumber numberWithInteger:0],
  7697. @"required" : @"false",
  7698. @"value" : logistic_note_text
  7699. }
  7700. },
  7701. @"value" : @"USE MY CARRIER",
  7702. @"value_id" : @"USE MY CARRIER"
  7703. }
  7704. },
  7705. @"control" : @"enum",
  7706. @"name" : @"logistic_note",
  7707. @"required" : @"true",
  7708. @"single_select" : @"true"
  7709. };
  7710. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  7711. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  7712. // val_1
  7713. lift_gate = [self valueInParams:params key:@"lift_gate"];
  7714. if([lift_gate isEqualToString:@""]) {
  7715. if (lift_gate_integer == 1) {
  7716. lift_gate = @"true";
  7717. } else {
  7718. lift_gate = @"false";
  7719. }
  7720. }
  7721. int common_carrier_check = 0;
  7722. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  7723. common_carrier_check = 1;
  7724. [params setValue:lift_gate forKey:@"lift_gate"];
  7725. }
  7726. NSDictionary *val_1 = @{
  7727. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  7728. @"sub_item" : @{
  7729. @"count" : [NSNumber numberWithInteger:1],
  7730. @"item_0" : @{
  7731. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  7732. @"control" : @"switch",
  7733. @"name" : @"lift_gate",
  7734. @"refresh" : [NSNumber numberWithInteger:1],
  7735. @"required" : @"true",
  7736. @"value" : lift_gate
  7737. }
  7738. },
  7739. @"value" : @"COMMON CARRIER",
  7740. @"value_id" : @"COMMON CARRIER"
  7741. };
  7742. // cadedate
  7743. NSDictionary *cadedate = @{
  7744. @"count" : [NSNumber numberWithInteger:2],
  7745. @"val_0" : val_0,
  7746. @"val_1" : val_1
  7747. };
  7748. // item_0
  7749. NSMutableDictionary *item_0 = @{
  7750. @"aname" : @"Shipping",
  7751. @"cadedate" : cadedate,
  7752. @"control" : @"enum",
  7753. @"name" : @"logist",
  7754. @"refresh" : [NSNumber numberWithInteger:1],
  7755. @"single_select" : @"true",
  7756. }.mutableCopy;
  7757. NSNumber *required = setting[@"ShippingMethodRequire"];
  7758. if ([required integerValue]) {
  7759. [item_0 setValue:@"true" forKey:@"required"];
  7760. }
  7761. [dic setValue:item_0 forKey:@"item_0"];
  7762. if ([logist isEqualToString:@"WILL CALL"]) {
  7763. [dic removeObjectForKey:@"lift_gate"];
  7764. }
  7765. return dic;
  7766. }
  7767. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  7768. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7769. orderCode = [self translateSingleQuote:orderCode];
  7770. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  7771. __block int submit_as_integer = 0;
  7772. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7773. submit_as_integer = sqlite3_column_int(stmt, 0);
  7774. }];
  7775. int check11 = 0;
  7776. int check10 = 0;
  7777. if (submit_as_integer == 11) {
  7778. check11 = 1;
  7779. }
  7780. if (submit_as_integer == 10) {
  7781. check10 = 1;
  7782. }
  7783. // section_0
  7784. NSMutableDictionary *dic = @{
  7785. @"count" : @(1),
  7786. @"item_0" : @{
  7787. @"aname" : @"Submit Order As",
  7788. @"cadedate" : @{
  7789. @"count" : @(2),
  7790. @"val_0" : @{
  7791. @"check" : [NSNumber numberWithInteger:check11],
  7792. @"value" : @"Sales Order",
  7793. @"value_id" : @(11)
  7794. },
  7795. @"val_1" : @{
  7796. @"check" : [NSNumber numberWithInteger:check10],
  7797. @"value" : @"Quote",
  7798. @"value_id" : @(10)
  7799. }
  7800. },
  7801. @"control" : @"enum",
  7802. @"name" : @"erpOrderStatus",
  7803. @"required" : @"true",
  7804. @"single_select" : @"true"
  7805. },
  7806. @"title" : @"Order Type"
  7807. }.mutableCopy;
  7808. // setting
  7809. NSDictionary *setting = params[@"setting"];
  7810. NSNumber *hide = setting[@"OrderTypeHide"];
  7811. [dic setValue:hide forKey:@"hide"];
  7812. return dic;
  7813. }
  7814. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  7815. countryCode = [self translateSingleQuote:countryCode];
  7816. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  7817. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7818. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  7819. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  7820. if (name == NULL) {
  7821. name = "";
  7822. }
  7823. if (code == NULL) {
  7824. code = "";
  7825. }
  7826. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  7827. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  7828. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  7829. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  7830. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  7831. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  7832. }
  7833. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  7834. [container setValue:stateDic forKey:key];
  7835. }] mutableCopy];
  7836. [ret removeObjectForKey:@"result"];
  7837. // failure 可以不用了,一样的
  7838. if (ret.allKeys.count == 0) {
  7839. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  7840. [stateDic setValue:@"Other" forKey:@"value"];
  7841. [stateDic setValue:@"" forKey:@"value_id"];
  7842. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  7843. if (state_code && [@"" isEqualToString:state_code]) {
  7844. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  7845. }
  7846. NSString *key = [NSString stringWithFormat:@"val_0"];
  7847. [ret setValue:stateDic forKey:key];
  7848. }
  7849. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  7850. return ret;
  7851. }
  7852. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  7853. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7854. orderCode = [self translateSingleQuote:orderCode];
  7855. 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];
  7856. __block NSString *payType = @"";
  7857. __block NSString *firstName = @"";
  7858. __block NSString *lastName = @"";
  7859. __block NSString *addr1 = @"";
  7860. __block NSString *addr2 = @"";
  7861. __block NSString *zipcode = @"";
  7862. __block NSString *cardType = @"";
  7863. __block NSString *cardNumber = @"";
  7864. __block NSString *securityCode = @"";
  7865. __block NSString *month = @"";
  7866. __block NSString *year = @"";
  7867. __block NSString *city = @"";
  7868. __block NSString *state = @"";
  7869. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7870. payType = [self textAtColumn:0 statement:stmt];
  7871. firstName = [self textAtColumn:1 statement:stmt];
  7872. lastName = [self textAtColumn:2 statement:stmt];
  7873. addr1 = [self textAtColumn:3 statement:stmt];
  7874. addr2 = [self textAtColumn:4 statement:stmt];
  7875. zipcode = [self textAtColumn:5 statement:stmt];
  7876. cardType = [self textAtColumn:6 statement:stmt];
  7877. cardNumber = [self textAtColumn:7 statement:stmt];
  7878. securityCode = [self textAtColumn:8 statement:stmt];
  7879. month = [self textAtColumn:9 statement:stmt];
  7880. year = [self textAtColumn:10 statement:stmt];
  7881. city = [self textAtColumn:11 statement:stmt];
  7882. state = [self textAtColumn:12 statement:stmt];
  7883. }];
  7884. NSString *required = @"true";
  7885. // setting
  7886. NSDictionary *setting = params[@"setting"];
  7887. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  7888. if ([requiredNumber integerValue]) {
  7889. required = @"true";
  7890. } else {
  7891. required = @"false";
  7892. }
  7893. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  7894. // create table if not exists payment_type (_id integer primary key,type_id text,pay_type text,sort int);
  7895. NSString *pay_type_sql = @"select type_id,pay_type,sort_id from payment_type order by sort_id asc";
  7896. [iSalesDB jk_query:pay_type_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7897. NSString *type_id = [self textAtColumn:0 statement:stmt];
  7898. NSString *type = [self textAtColumn:1 statement:stmt];
  7899. NSMutableDictionary *val = @{
  7900. @"check" : [payType isEqualToString:type_id] ? @(1) : @(0),
  7901. @"value" : type,
  7902. @"value_id" : type_id
  7903. }.mutableCopy;
  7904. if ([type_id isEqualToString:@"Visa/Master"] || [type_id isEqualToString:@"Credit Card"]) {
  7905. NSDictionary *sub_item = @{
  7906. @"count" : @(3),
  7907. @"item_0" : @{
  7908. @"aname" : @"choose",
  7909. @"control" : @"multi_action",
  7910. @"count" : @(1),
  7911. @"item_0" : @{
  7912. @"aname" : @"Same as customer",
  7913. @"key_map" : @{
  7914. @"credit_card_address1" : @"customer_address1",
  7915. @"credit_card_address2" : @"customer_address2",
  7916. @"credit_card_city" : @"customer_city",
  7917. @"credit_card_first_name" : @"customer_first_name",
  7918. @"credit_card_last_name" : @"customer_last_name",
  7919. @"credit_card_state" : @"customer_state",
  7920. @"credit_card_zipcode" : @"customer_zipcode"
  7921. },
  7922. @"type" : @"pull"
  7923. }
  7924. },
  7925. @"item_1" : @{
  7926. @"aname" : @"",
  7927. @"color" : @"red",
  7928. @"control" : @"text",
  7929. @"name" : @"",
  7930. @"value" : @"USA Credit cards only"
  7931. },
  7932. @"item_2" : @{
  7933. @"aname" : @"Fill",
  7934. @"cadedate" : @{
  7935. @"count" : @(2),
  7936. @"val_0" : @{
  7937. @"check" : @(1),
  7938. @"sub_item" : @{
  7939. @"count" : @(11),
  7940. @"item_0" : @{
  7941. @"aname" : @"Type",
  7942. @"cadedate" : @{
  7943. @"count" : @(2),
  7944. @"val_0" : @{
  7945. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  7946. @"value" : @"VISA",
  7947. @"value_id" : @"VISA"/*@(0)*/
  7948. },
  7949. @"val_1" : @{
  7950. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  7951. @"value" : @"MASTER CARD",
  7952. @"value_id" : @"MASTER CARD"/*@(1)*/
  7953. }
  7954. },
  7955. @"control" : @"enum",
  7956. @"name" : @"credit_card_type",
  7957. @"required" : @"true",
  7958. @"single_select" : @"true"
  7959. },
  7960. @"item_1" : @{
  7961. @"aname" : @"Number",
  7962. @"control" : @"edit",
  7963. @"keyboard" : @"int",
  7964. @"length" : @"16",
  7965. @"name" : @"credit_card_number",
  7966. @"required" : @"true",
  7967. @"value" : cardNumber
  7968. },
  7969. @"item_10" : @{
  7970. @"aname" : @"State",
  7971. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  7972. @"control" : @"enum",
  7973. @"enum" : @"true",
  7974. @"name" : @"credit_card_state",
  7975. @"required" : @"true",
  7976. @"single_select" : @"true"
  7977. },
  7978. @"item_2" : @{
  7979. @"aname" : @"Expiration Date",
  7980. @"control" : @"monthpicker",
  7981. @"name" : @"credit_card_expiration",
  7982. @"required" : @"true",
  7983. @"type" : @"date",
  7984. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  7985. },
  7986. @"item_3" : @{
  7987. @"aname" : @"Security Code",
  7988. @"control" : @"edit",
  7989. @"keyboard" : @"int",
  7990. @"length" : @"3",
  7991. @"name" : @"credit_card_security_code",
  7992. @"required" : @"true",
  7993. @"value" : securityCode
  7994. },
  7995. @"item_4" : @{
  7996. @"aname" : @"First Name",
  7997. @"control" : @"edit",
  7998. @"keyboard" : @"text",
  7999. @"name" : @"credit_card_first_name",
  8000. @"required" : @"true",
  8001. @"value" : firstName
  8002. },
  8003. @"item_5" : @{
  8004. @"aname" : @"Last Name",
  8005. @"control" : @"edit",
  8006. @"keyboard" : @"text",
  8007. @"name" : @"credit_card_last_name",
  8008. @"required" : @"true",
  8009. @"value" : lastName
  8010. },
  8011. @"item_6" : @{
  8012. @"aname" : @"Address 1",
  8013. @"control" : @"edit",
  8014. @"keyboard" : @"text",
  8015. @"name" : @"credit_card_address1",
  8016. @"required" : @"true",
  8017. @"value" : addr1
  8018. },
  8019. @"item_7" : @{
  8020. @"aname" : @"Address 2",
  8021. @"control" : @"edit",
  8022. @"keyboard" : @"text",
  8023. @"name" : @"credit_card_address2",
  8024. @"value" : addr2
  8025. },
  8026. @"item_8" : @{
  8027. @"aname" : @"zip code",
  8028. @"control" : @"edit",
  8029. @"keyboard" : @"text",
  8030. @"name" : @"credit_card_zipcode",
  8031. @"required" : @"true",
  8032. @"value" : zipcode
  8033. },
  8034. @"item_9" : @{
  8035. @"aname" : @"City",
  8036. @"control" : @"edit",
  8037. @"keyboard" : @"text",
  8038. @"name" : @"credit_card_city",
  8039. @"required" : @"true",
  8040. @"value" : city
  8041. }
  8042. },
  8043. @"value" : @"Fill Now",
  8044. @"value_id" : @""
  8045. },
  8046. @"val_1" : @{
  8047. @"check" : @(0),
  8048. @"value" : @"Fill Later",
  8049. @"value_id" : @""
  8050. }
  8051. },
  8052. @"control" : @"enum",
  8053. @"name" : @"",
  8054. @"single_select" : @"true"
  8055. }
  8056. };
  8057. [val setObject:sub_item forKey:@"sub_item"];
  8058. }
  8059. [cadedate setObject:val forKey:[NSString stringWithFormat:@"val_%ld",(*count)]];
  8060. [cadedate setObject:[NSNumber numberWithInteger:(++*count)] forKey:@"count"];
  8061. }];
  8062. // "section_2"
  8063. NSMutableDictionary *dic = @{
  8064. @"count" : @(1),
  8065. @"item_0" : @{
  8066. @"aname" : @"Payment",
  8067. @"required" : required,
  8068. @"cadedate" : cadedate,
  8069. // @{
  8070. // @"count" : @(6),
  8071. // @"val_3" : @{
  8072. // @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  8073. // @"value" : @"Check",
  8074. // @"value_id" : @"Check"
  8075. // },
  8076. // @"val_2" : @{
  8077. // @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  8078. // @"value" : @"Cash",
  8079. // @"value_id" : @"Cash"
  8080. // },
  8081. // @"val_1" : @{
  8082. // @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  8083. // @"value" : @"NET 60",
  8084. // @"value_id" : @"NET 30"
  8085. // },
  8086. // @"val_4" : @{
  8087. // @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  8088. // @"value" : @"Wire Transfer",
  8089. // @"value_id" : @"Wire Transfer"
  8090. // },
  8091. // @"val_0" : @{
  8092. // @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  8093. // @"sub_item" : @{
  8094. // @"count" : @(3),
  8095. // @"item_0" : @{
  8096. // @"aname" : @"choose",
  8097. // @"control" : @"multi_action",
  8098. // @"count" : @(1),
  8099. // @"item_0" : @{
  8100. // @"aname" : @"Same as customer",
  8101. // @"key_map" : @{
  8102. // @"credit_card_address1" : @"customer_address1",
  8103. // @"credit_card_address2" : @"customer_address2",
  8104. // @"credit_card_city" : @"customer_city",
  8105. // @"credit_card_first_name" : @"customer_first_name",
  8106. // @"credit_card_last_name" : @"customer_last_name",
  8107. // @"credit_card_state" : @"customer_state",
  8108. // @"credit_card_zipcode" : @"customer_zipcode"
  8109. // },
  8110. // @"type" : @"pull"
  8111. // }
  8112. // },
  8113. // @"item_1" : @{
  8114. // @"aname" : @"",
  8115. // @"color" : @"red",
  8116. // @"control" : @"text",
  8117. // @"name" : @"",
  8118. // @"value" : @"USA Credit cards only"
  8119. // },
  8120. // @"item_2" : @{
  8121. // @"aname" : @"Fill",
  8122. // @"cadedate" : @{
  8123. // @"count" : @(2),
  8124. // @"val_0" : @{
  8125. // @"check" : @(1),
  8126. // @"sub_item" : @{
  8127. // @"count" : @(11),
  8128. // @"item_0" : @{
  8129. // @"aname" : @"Type",
  8130. // @"cadedate" : @{
  8131. // @"count" : @(2),
  8132. // @"val_0" : @{
  8133. // @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  8134. // @"value" : @"VISA",
  8135. // @"value_id" : @(0)
  8136. // },
  8137. // @"val_1" : @{
  8138. // @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  8139. // @"value" : @"MASTER CARD",
  8140. // @"value_id" : @(1)
  8141. // }
  8142. // },
  8143. // @"control" : @"enum",
  8144. // @"name" : @"credit_card_type",
  8145. // @"required" : @"true",
  8146. // @"single_select" : @"true"
  8147. // },
  8148. // @"item_1" : @{
  8149. // @"aname" : @"Number",
  8150. // @"control" : @"edit",
  8151. // @"keyboard" : @"int",
  8152. // @"length" : @"16",
  8153. // @"name" : @"credit_card_number",
  8154. // @"required" : @"true",
  8155. // @"value" : cardNumber
  8156. // },
  8157. // @"item_10" : @{
  8158. // @"aname" : @"State",
  8159. // @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  8160. // @"control" : @"enum",
  8161. // @"enum" : @"true",
  8162. // @"name" : @"credit_card_state",
  8163. // @"required" : @"true",
  8164. // @"single_select" : @"true"
  8165. // },
  8166. // @"item_2" : @{
  8167. // @"aname" : @"Expiration Date",
  8168. // @"control" : @"monthpicker",
  8169. // @"name" : @"credit_card_expiration",
  8170. // @"required" : @"true",
  8171. // @"type" : @"date",
  8172. // @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  8173. // },
  8174. // @"item_3" : @{
  8175. // @"aname" : @"Security Code",
  8176. // @"control" : @"edit",
  8177. // @"keyboard" : @"int",
  8178. // @"length" : @"3",
  8179. // @"name" : @"credit_card_security_code",
  8180. // @"required" : @"true",
  8181. // @"value" : securityCode
  8182. // },
  8183. // @"item_4" : @{
  8184. // @"aname" : @"First Name",
  8185. // @"control" : @"edit",
  8186. // @"keyboard" : @"text",
  8187. // @"name" : @"credit_card_first_name",
  8188. // @"required" : @"true",
  8189. // @"value" : firstName
  8190. // },
  8191. // @"item_5" : @{
  8192. // @"aname" : @"Last Name",
  8193. // @"control" : @"edit",
  8194. // @"keyboard" : @"text",
  8195. // @"name" : @"credit_card_last_name",
  8196. // @"required" : @"true",
  8197. // @"value" : lastName
  8198. // },
  8199. // @"item_6" : @{
  8200. // @"aname" : @"Address 1",
  8201. // @"control" : @"edit",
  8202. // @"keyboard" : @"text",
  8203. // @"name" : @"credit_card_address1",
  8204. // @"required" : @"true",
  8205. // @"value" : addr1
  8206. // },
  8207. // @"item_7" : @{
  8208. // @"aname" : @"Address 2",
  8209. // @"control" : @"edit",
  8210. // @"keyboard" : @"text",
  8211. // @"name" : @"credit_card_address2",
  8212. // @"value" : addr2
  8213. // },
  8214. // @"item_8" : @{
  8215. // @"aname" : @"zip code",
  8216. // @"control" : @"edit",
  8217. // @"keyboard" : @"text",
  8218. // @"name" : @"credit_card_zipcode",
  8219. // @"required" : @"true",
  8220. // @"value" : zipcode
  8221. // },
  8222. // @"item_9" : @{
  8223. // @"aname" : @"City",
  8224. // @"control" : @"edit",
  8225. // @"keyboard" : @"text",
  8226. // @"name" : @"credit_card_city",
  8227. // @"required" : @"true",
  8228. // @"value" : city
  8229. // }
  8230. // },
  8231. // @"value" : @"Fill Now",
  8232. // @"value_id" : @""
  8233. // },
  8234. // @"val_1" : @{
  8235. // @"check" : @(0),
  8236. // @"value" : @"Fill Later",
  8237. // @"value_id" : @""
  8238. // }
  8239. // },
  8240. // @"control" : @"enum",
  8241. // @"name" : @"",
  8242. // @"single_select" : @"true"
  8243. // }
  8244. // },
  8245. // @"value" : @"Visa/Master",
  8246. // @"value_id" : @"Credit Card"
  8247. // },
  8248. // @"val_5" : @{
  8249. // @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  8250. // @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  8251. // @"value_id" : @"FOLLOW EXISTING"
  8252. // }
  8253. // },
  8254. @"control" : @"enum",
  8255. @"name" : @"paymentType",
  8256. @"single_select" : @"true"
  8257. },
  8258. @"title" : @"Payment Information"
  8259. }.mutableCopy;
  8260. NSNumber *hide = setting[@"PaymentInformationHide"];
  8261. [dic setValue:hide forKey:@"hide"];
  8262. return dic;
  8263. }
  8264. + (BOOL)orderOutOfStock:(NSDictionary *)params db:(sqlite3 *)db{
  8265. // params
  8266. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8267. orderCode = [self translateSingleQuote:orderCode];
  8268. // 缺货检查
  8269. NSString *out_of_stock_SQL = [NSString stringWithFormat:@"select p.availability,c.item_count from product as p left join offline_cart as c on p.product_id = c.product_id where c.so_no = '%@';",orderCode];
  8270. __block BOOL outOfStock = NO;
  8271. sqlite3 *database = db;
  8272. if (!db) {
  8273. database = [iSalesDB get_db];
  8274. }
  8275. [iSalesDB jk_query:out_of_stock_SQL db:database close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8276. int availability = sqlite3_column_int(stmt, 0);
  8277. int item_qty = sqlite3_column_int(stmt, 1);
  8278. outOfStock = outOfStock || (availability < item_qty ? YES : NO);
  8279. }];
  8280. if (!db) {
  8281. [iSalesDB close_db:database];
  8282. }
  8283. return outOfStock;
  8284. }
  8285. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  8286. {
  8287. sqlite3 *db = [iSalesDB get_db];
  8288. // params
  8289. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8290. orderCode = [self translateSingleQuote:orderCode];
  8291. // 缺货检查
  8292. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8293. if (!appDelegate.can_create_backorder) {
  8294. BOOL out_of_stock = [self orderOutOfStock:params db:db];
  8295. if (out_of_stock) {
  8296. [iSalesDB close_db:db];
  8297. NSMutableDictionary *resultDic = [NSMutableDictionary dictionary];
  8298. [resultDic setObject:[NSString stringWithFormat:@"some item(s) are out of stock,please remove them from cart before placing order with %@ or try again after sync",COMPANY_SHORT_NAME] forKey:@"err_msg"];
  8299. [resultDic setObject:[NSNumber numberWithInt:8] forKey:@"result"];
  8300. return [RAConvertor dict2data:resultDic];
  8301. }
  8302. }
  8303. // UISetting
  8304. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8305. NSString *cachefolder = [paths objectAtIndex:0];
  8306. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  8307. NSData* json =nil;
  8308. json=[NSData dataWithContentsOfFile:img_cache];
  8309. NSError *error=nil;
  8310. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  8311. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  8312. [params setObject:setting forKey:@"setting"];
  8313. int section_count = 0;
  8314. // 0 Order Type 1 Shipping Method 2 Payment Information
  8315. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  8316. // 0 Order Type
  8317. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  8318. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  8319. [ret setValue:order_type_dic forKey:key0];
  8320. // 1 Shipping Method
  8321. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  8322. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  8323. [ret setValue:shipping_method_dic forKey:key1];
  8324. // 2 Payment Information
  8325. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  8326. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  8327. [ret setValue:payment_info_dic forKey:key2];
  8328. // 3 Customer
  8329. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  8330. NSDictionary *customer_dic = [self customerDic:params db:db];
  8331. [ret setValue:customer_dic forKey:key3];
  8332. // 4 Ship To
  8333. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  8334. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  8335. [ret setValue:ship_to_dic forKey:key4];
  8336. // 5 Ship From
  8337. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  8338. if (![shipFromDisable integerValue]) {
  8339. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  8340. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  8341. [ret setValue:ship_from_dic forKey:key5];
  8342. }
  8343. // 6 Freight Bill To
  8344. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  8345. if (![freightBillToDisable integerValue]) {
  8346. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  8347. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  8348. [ret setValue:freight_bill_to forKey:key6];
  8349. }
  8350. // 7 Merchandise Bill To
  8351. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  8352. if (![merchandiseBillToDisable integerValue]) {
  8353. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  8354. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  8355. [ret setValue:merchandise_bill_to_dic forKey:key7];
  8356. }
  8357. // 8 Return To
  8358. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  8359. if (![returnToDisable integerValue]) {
  8360. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  8361. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  8362. [ret setValue:return_to_dic forKey:key8];
  8363. }
  8364. // 9 Model Information
  8365. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  8366. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  8367. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  8368. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  8369. [ret setValue:model_info_dic forKey:key9];
  8370. // 10 Remarks Content
  8371. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  8372. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  8373. [ret setValue:remarks_content_dic forKey:key10];
  8374. // 11 Order Total
  8375. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  8376. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  8377. [ret setValue:order_total_dic forKey:key11];
  8378. // 12 Signature
  8379. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  8380. NSDictionary *sign_dic = [self signatureDic:params db:db];
  8381. [ret setValue:sign_dic forKey:key12];
  8382. [ret setValue:@(section_count) forKey:@"section_count"];
  8383. [iSalesDB close_db:db];
  8384. return [RAConvertor dict2data:ret];
  8385. // return nil;
  8386. }
  8387. #pragma mark addr editor
  8388. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  8389. NSMutableDictionary *new_item = [item mutableCopy];
  8390. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  8391. return new_item;
  8392. }
  8393. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  8394. {
  8395. // "is_subaction" = true;
  8396. // orderCode = MOB1608240002;
  8397. // password = 123456;
  8398. // "subaction_tag" = 1;
  8399. // user = EvanK;
  8400. // {
  8401. // "is_subaction" = true;
  8402. // orderCode = MOB1608240002;
  8403. // password = 123456;
  8404. // "refresh_trigger" = zipcode;
  8405. // "subaction_tag" = 1;
  8406. // user = EvanK;
  8407. // }
  8408. NSString *country_code = nil;
  8409. NSString *zipCode = nil;
  8410. NSString *stateCode = nil;
  8411. NSString *city = nil;
  8412. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  8413. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  8414. NSString *code_id = params[@"country"];
  8415. country_code = [self countryCodeByid:code_id];
  8416. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  8417. NSString *zip_code = params[@"zipcode"];
  8418. // 剔除全部为空格
  8419. int spaceCount = 0;
  8420. for (int i = 0; i < zip_code.length; i++) {
  8421. if ([zip_code characterAtIndex:i] == ' ') {
  8422. spaceCount++;
  8423. }
  8424. }
  8425. if (spaceCount == zip_code.length) {
  8426. zip_code = @"";
  8427. }
  8428. zipCode = zip_code;
  8429. if (zipCode.length > 0) {
  8430. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  8431. country_code = [dic valueForKey:@"country_code"];
  8432. if (!country_code) {
  8433. // country_code = @"US";
  8434. NSString *code_id = params[@"country"];
  8435. country_code = [self countryCodeByid:code_id];
  8436. }
  8437. stateCode = [dic valueForKey:@"state_code"];
  8438. if(!stateCode.length) {
  8439. stateCode = params[@"state"];
  8440. }
  8441. city = [dic valueForKey:@"city"];
  8442. if (!city.length) {
  8443. city = params[@"city"];
  8444. }
  8445. // zip code
  8446. // NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  8447. // [zipDic setValue:zipCode forKey:@"value"];
  8448. // [section_0 setValue:zipDic forKey:@"item_11"];
  8449. } else {
  8450. NSString *code_id = params[@"country"];
  8451. country_code = [self countryCodeByid:code_id];
  8452. stateCode = params[@"state"];
  8453. city = params[@"city"];
  8454. }
  8455. }
  8456. }
  8457. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  8458. // [ret removeObjectForKey:@"up_params"];
  8459. [ret setObject:@"New Address" forKey:@"title"];
  8460. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  8461. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  8462. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  8463. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  8464. // [country_dic removeObjectForKey:@"refresh"];
  8465. // [country_dic removeObjectForKey:@"restore"];
  8466. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  8467. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  8468. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  8469. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  8470. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  8471. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  8472. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  8473. // [zip_code_dic removeObjectForKey:@"refresh"];
  8474. [zip_code_dic setValue:zipCode ? zipCode :@"" forKey:@"value"];
  8475. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  8476. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  8477. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  8478. NSDictionary *cityDic = [[new_section_0 objectForKey:@"item_8"] mutableCopy];
  8479. [cityDic setValue:city ? city : @"" forKey:@"value"];
  8480. [new_section_0 setObject:cityDic forKey:@"item_8"];
  8481. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  8482. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  8483. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  8484. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  8485. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  8486. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  8487. // country
  8488. NSString *countryCode = country_code == nil ? @"US" : country_code;
  8489. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  8490. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  8491. // state
  8492. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:stateCode] mutableCopy];
  8493. // NSDictionary *tmpDic = @{
  8494. // @"value" : @"Other",
  8495. // @"value_id" : @"",
  8496. // @"check" : [NSNumber numberWithInteger:0]
  8497. // };
  8498. //
  8499. // for (int i = 0; i < allState.allKeys.count; i++) {
  8500. // NSString *key = [NSString stringWithFormat:@"val_%d",i];
  8501. //
  8502. // NSDictionary *tmp = allState[key];
  8503. // [allState setValue:tmpDic forKey:key];
  8504. // tmpDic = tmp;
  8505. // }
  8506. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  8507. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  8508. [ret setValue:new_section_0 forKey:@"section_0"];
  8509. return [RAConvertor dict2data:ret];
  8510. }
  8511. #pragma mark save addr
  8512. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  8513. {
  8514. // NSString *companyName = [self valueInParams:params key:@"company"];
  8515. // NSString *addr1 = [self valueInParams:params key:@"address"];
  8516. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  8517. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  8518. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  8519. // NSString *countryId = [self valueInParams:params key:@"country"];
  8520. // NSString *stateCode = [self valueInParams:params key:@"state"];
  8521. // NSString *city = [self valueInParams:params key:@"city"];
  8522. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  8523. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  8524. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  8525. // NSString *phone = [self valueInParams:params key:@"phone"];
  8526. // NSString *fax = [self valueInParams:params key:@"fax"];
  8527. // NSString *email = [self valueInParams:params key:@"email"];
  8528. return [self offline_saveContact:params update:NO isCustomer:NO];
  8529. }
  8530. #pragma mark cancel order
  8531. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  8532. {
  8533. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8534. NSString *order_id = [self valueInParams:params key:@"orderId"];
  8535. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  8536. if (order_id.length) {
  8537. sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  8538. }
  8539. int ret = [iSalesDB execSql:sql];
  8540. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8541. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8542. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8543. // [dic setValue:@"160409" forKey:@"min_ver"];
  8544. return [RAConvertor dict2data:dic];
  8545. }
  8546. #pragma mark sign order
  8547. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  8548. {
  8549. //参考 offline_saveBusinesscard
  8550. DebugLog(@"sign order params: %@",params);
  8551. // orderCode = MOB1608240002;
  8552. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  8553. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8554. orderCode = [self translateSingleQuote:orderCode];
  8555. NSString *picPath = [self valueInParams:params key:@"picpath"];
  8556. picPath = [self translateSingleQuote:picPath];
  8557. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  8558. int ret = [iSalesDB execSql:sql];
  8559. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8560. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8561. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8562. // [dic setValue:@"160409" forKey:@"min_ver"];
  8563. return [RAConvertor dict2data:dic];
  8564. }
  8565. #pragma mark save order
  8566. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  8567. NSString *ret = [self valueInParams:params key:key];
  8568. if (translate) {
  8569. ret = [self translateSingleQuote:ret];
  8570. }
  8571. return ret;
  8572. }
  8573. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  8574. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  8575. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  8576. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  8577. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  8578. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  8579. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  8580. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  8581. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  8582. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  8583. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  8584. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  8585. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  8586. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  8587. if (![total_price isEqualToString:@""]) {
  8588. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  8589. } else {
  8590. total_price = @"";
  8591. }
  8592. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  8593. if ([paymentsAndCredits isEqualToString:@""]) {
  8594. paymentsAndCredits = @"";
  8595. } else {
  8596. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  8597. }
  8598. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  8599. if ([handling_fee_value isEqualToString:@""]) {
  8600. handling_fee_value = @"";
  8601. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  8602. handling_fee_value = @"";
  8603. } else {
  8604. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  8605. }
  8606. NSString *handling_fee_placeholder = handling_fee_value;
  8607. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  8608. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  8609. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  8610. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  8611. NSString *lift_gate_placeholder = @"";
  8612. if ([lift_gate_value isEqualToString:@""]) {
  8613. lift_gate_placeholder = @"";
  8614. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  8615. lift_gate_placeholder = @"";
  8616. } else {
  8617. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  8618. }
  8619. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  8620. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  8621. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  8622. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  8623. 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];
  8624. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  8625. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  8626. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  8627. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  8628. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  8629. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  8630. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  8631. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  8632. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  8633. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  8634. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  8635. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  8636. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  8637. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  8638. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  8639. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  8640. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  8641. BOOL number_nil = [credit_card_number isEqualToString:@""];
  8642. if (!number_nil) {
  8643. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  8644. }
  8645. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  8646. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  8647. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  8648. if (!security_code_nil) {
  8649. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  8650. }
  8651. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  8652. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  8653. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  8654. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  8655. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  8656. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  8657. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  8658. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  8659. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  8660. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  8661. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  8662. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  8663. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  8664. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  8665. NSString *contact_id = customer_cid;
  8666. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  8667. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  8668. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  8669. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  8670. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  8671. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  8672. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  8673. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  8674. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  8675. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  8676. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  8677. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  8678. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  8679. //
  8680. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  8681. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  8682. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  8683. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  8684. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  8685. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  8686. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  8687. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  8688. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  8689. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  8690. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  8691. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  8692. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  8693. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  8694. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  8695. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  8696. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  8697. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  8698. 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];
  8699. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  8700. if (receive_cid.length) {
  8701. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  8702. }
  8703. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  8704. if (receive_name.length) {
  8705. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  8706. }
  8707. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  8708. if (receive_ext.length) {
  8709. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  8710. }
  8711. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  8712. if (receive_contact.length) {
  8713. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  8714. }
  8715. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  8716. if (receive_phone.length) {
  8717. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  8718. }
  8719. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  8720. if (receive_email.length) {
  8721. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  8722. }
  8723. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  8724. if (receive_fax.length) {
  8725. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  8726. }
  8727. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  8728. if (sender_cid.length) {
  8729. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  8730. }
  8731. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  8732. if (sender_name.length) {
  8733. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  8734. }
  8735. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  8736. if (sender_ext.length) {
  8737. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  8738. }
  8739. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  8740. if(sender_contact.length) {
  8741. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  8742. }
  8743. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  8744. if (sender_phone.length) {
  8745. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  8746. }
  8747. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  8748. if (sender_fax.length) {
  8749. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  8750. }
  8751. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  8752. if (sender_email.length) {
  8753. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  8754. }
  8755. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  8756. if (shipping_billto_cid.length) {
  8757. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  8758. }
  8759. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  8760. if (shipping_billto_name.length) {
  8761. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  8762. }
  8763. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  8764. if (shipping_billto_ext.length) {
  8765. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  8766. }
  8767. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  8768. if (shipping_billto_contact.length) {
  8769. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  8770. }
  8771. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  8772. if (shipping_billto_phone.length) {
  8773. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  8774. }
  8775. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  8776. if (shipping_billto_fax.length) {
  8777. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  8778. }
  8779. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  8780. if (shipping_billto_email.length) {
  8781. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  8782. }
  8783. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  8784. if (billing_cid.length) {
  8785. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  8786. }
  8787. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  8788. if (billing_name.length) {
  8789. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  8790. }
  8791. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  8792. if (billing_ext.length) {
  8793. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  8794. }
  8795. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  8796. if (billing_contact.length) {
  8797. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  8798. }
  8799. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  8800. if (billing_phone.length) {
  8801. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  8802. }
  8803. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  8804. if (billing_fax.length) {
  8805. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  8806. }
  8807. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  8808. if (billing_email.length) {
  8809. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  8810. }
  8811. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  8812. if (returnto_cid.length) {
  8813. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  8814. }
  8815. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  8816. if (returnto_name.length) {
  8817. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  8818. }
  8819. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  8820. if (returnto_ext.length) {
  8821. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  8822. }
  8823. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  8824. if (returnto_contact.length) {
  8825. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  8826. }
  8827. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  8828. if (returnto_phone.length) {
  8829. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  8830. }
  8831. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  8832. if (returnto_fax.length) {
  8833. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  8834. }
  8835. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  8836. if (returnto_email.length) {
  8837. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  8838. }
  8839. NSString *order_status = @"status = 1,";
  8840. if (submit) {
  8841. order_status = @"status = -11,";
  8842. }
  8843. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  8844. NSString *sync_sql = @"";
  8845. if (submit) {
  8846. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  8847. [param setValue:sales_rep forKey:@"sales_rep"];
  8848. NSString *sync_data = [self translateSingleQuote:[RAConvertor dict2string:param]];
  8849. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  8850. }
  8851. 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];
  8852. DebugLog(@"save order contactSql: %@",contactSql);
  8853. DebugLog(@"save order orderSql: %@",orderSql);
  8854. // int contact_ret = [iSalesDB execSql:contactSql];
  8855. int order_ret = [iSalesDB execSql:orderSql];
  8856. int ret = order_ret;
  8857. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8858. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8859. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8860. // [dic setValue:@"160409" forKey:@"min_ver"];
  8861. [dic setObject:so_id forKey:@"so#"];
  8862. return [RAConvertor dict2data:dic];
  8863. }
  8864. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  8865. {
  8866. // id foo = nil;
  8867. // NSMutableArray *a = @[].mutableCopy;
  8868. // [a addObject:foo];
  8869. return [self saveorder:param submit:NO];
  8870. }
  8871. #pragma mark add to cart by name
  8872. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  8873. {
  8874. NSString *orderCode = [params objectForKey:@"orderCode"];
  8875. NSString *product_name = [params objectForKey:@"product_name"];
  8876. product_name = [self translateSingleQuote:product_name];
  8877. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  8878. sqlite3 *db = [iSalesDB get_db];
  8879. __block NSMutableString *product_id_string = [NSMutableString string];
  8880. for (int i = 0; i < product_name_array.count; i++) {
  8881. NSString *name = [product_name_array objectAtIndex:i];
  8882. NSString *sql = [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  8883. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8884. int product_id = sqlite3_column_int(stmt, 0);
  8885. if (i == product_name_array.count - 1) {
  8886. [product_id_string appendFormat:@"%d",product_id];
  8887. } else {
  8888. [product_id_string appendFormat:@"%d,",product_id];
  8889. }
  8890. }];
  8891. }
  8892. NSDictionary *newParams = @{
  8893. @"product_id" : product_id_string,
  8894. @"orderCode" : orderCode
  8895. };
  8896. [iSalesDB close_db:db];
  8897. return [self offline_add2cart:[newParams mutableCopy]];
  8898. }
  8899. #pragma mark reset order
  8900. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  8901. {
  8902. UIApplication * app = [UIApplication sharedApplication];
  8903. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  8904. appDelegate.disable_trigger=true;
  8905. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  8906. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  8907. appDelegate.disable_trigger=false;
  8908. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8909. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  8910. return [RAConvertor dict2data:dic];
  8911. }
  8912. #pragma mark submit order
  8913. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  8914. {
  8915. return [self saveorder:params submit:YES];
  8916. }
  8917. #pragma mark copy order
  8918. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  8919. {
  8920. NSMutableDictionary *ret = @{}.mutableCopy;
  8921. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  8922. sqlite3 *db = [iSalesDB get_db];
  8923. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  8924. // 首先查看联系人是否active
  8925. 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];
  8926. __block int customer_is_active = 1;
  8927. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8928. customer_is_active = sqlite3_column_int(stmt, 0);
  8929. }];
  8930. if (!customer_is_active) {
  8931. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8932. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8933. [iSalesDB close_db:db];
  8934. return [RAConvertor dict2data:ret];
  8935. }
  8936. // new order
  8937. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  8938. NSString *new_order_code = [self get_offline_soid:db];
  8939. NSString *user = ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  8940. user = [self translateSingleQuote:user];
  8941. 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
  8942. __block int result = 1;
  8943. result = [iSalesDB execSql:insert_order_sql db:db];
  8944. if (!result) {
  8945. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8946. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8947. [iSalesDB close_db:db];
  8948. return [RAConvertor dict2data:ret];
  8949. }
  8950. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  8951. // __block NSString *product_id = @"";
  8952. __weak typeof(self) weakSelf = self;
  8953. 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];
  8954. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8955. int is_active = sqlite3_column_int(stmt, 1);
  8956. if (is_active) {
  8957. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  8958. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  8959. 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];
  8960. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  8961. }
  8962. }];
  8963. // product_id = [product_id substringFromIndex:1];
  8964. //
  8965. // [self offline_add2cart:@{}.mutableCopy];
  8966. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8967. [iSalesDB close_db:db];
  8968. if (result) {
  8969. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8970. } else {
  8971. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8972. }
  8973. [ret setObject:new_order_code forKey:@"so_id"];
  8974. return [RAConvertor dict2data:ret];
  8975. }
  8976. #pragma mark move wish list to cart
  8977. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  8978. // cart中存在的Model在wish list move to cart之后,cart中Model数量为1
  8979. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8980. NSString *collectId = params[@"collectId"];
  8981. NSString *sql = [NSString stringWithFormat:@"select w.product_id,w.qty,p.availability,w._id from wishlist as w left join product as p on w.product_id = p.product_id where w._id in (%@);",collectId];
  8982. __block NSString *product_id = @"";
  8983. __block NSString *qty = @"";
  8984. __block int number_of_outOfStock = 0;
  8985. __block NSMutableArray *delete_collectId = [NSMutableArray array];
  8986. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8987. int productId = sqlite3_column_int(stmt, 0);
  8988. int item_qty = sqlite3_column_int(stmt, 1);
  8989. int availability = sqlite3_column_int(stmt, 2);
  8990. int _id = sqlite3_column_int(stmt, 3);
  8991. if (!appDelegate.can_create_backorder) {
  8992. // 库存小于购买量为缺货
  8993. if (availability >= item_qty) {
  8994. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  8995. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  8996. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  8997. } else {
  8998. number_of_outOfStock++;
  8999. }
  9000. } else {
  9001. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  9002. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  9003. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  9004. }
  9005. }];
  9006. NSMutableDictionary *retDic = nil;
  9007. if (!appDelegate.can_create_backorder) {
  9008. if (delete_collectId.count == 0) {
  9009. retDic = [NSMutableDictionary dictionary];
  9010. retDic[@"result"] = [NSNumber numberWithInteger:8];
  9011. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  9012. return [RAConvertor dict2data:retDic];
  9013. }
  9014. }
  9015. if (product_id.length > 1) {
  9016. product_id = [product_id substringFromIndex:1];
  9017. }
  9018. if (qty.length > 1) {
  9019. qty = [qty substringFromIndex:1];
  9020. }
  9021. NSDictionary *newParams = @{
  9022. @"product_id" : product_id,
  9023. @"orderCode" : params[@"orderCode"],
  9024. @"qty" : qty
  9025. };
  9026. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  9027. retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  9028. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  9029. NSInteger ret = [[[self offline_deletewishlist:@{@"collectId" : [delete_collectId componentsJoinedByString:@","]}.mutableCopy] valueForKey:@"result"] integerValue];
  9030. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  9031. }
  9032. if (!appDelegate.can_create_backorder) {
  9033. if (number_of_outOfStock > 0) {
  9034. retDic[@"result"] = [NSNumber numberWithInteger:8];
  9035. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  9036. }
  9037. }
  9038. return [RAConvertor dict2data:retDic];
  9039. }
  9040. #pragma mark - portfolio
  9041. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  9042. int sort = [[params valueForKey:@"sort"] intValue];
  9043. int offset = [[params valueForKey:@"offset"] intValue];
  9044. int limit = [[params valueForKey:@"limit"] intValue];
  9045. NSString *orderBy = @"";
  9046. switch (sort) {
  9047. case 0:{
  9048. orderBy = @"p.modify_time desc";
  9049. }
  9050. break;
  9051. case 1:{
  9052. orderBy = @"modify_time asc";
  9053. }
  9054. break;
  9055. case 2:{
  9056. orderBy = @"p.name asc";
  9057. }
  9058. break;
  9059. case 3:{
  9060. orderBy = @"p.name desc";
  9061. }
  9062. break;
  9063. case 4:{
  9064. orderBy = @"p.description asc";
  9065. }
  9066. break;
  9067. case 5: {
  9068. orderBy = @"m.default_category asc";
  9069. }
  9070. default:
  9071. break;
  9072. }
  9073. // 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];
  9074. NSString *sql = [NSString stringWithFormat:@"select * from (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,p.modify_time 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 limit %d offset %d) as p left join product as m on p.product_id = m.product_id order by %@;",limit,offset,orderBy];
  9075. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9076. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9077. sqlite3 *db = [iSalesDB get_db];
  9078. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9079. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9080. int product_id = sqlite3_column_int(stmt, 0);
  9081. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  9082. NSString *name = [self textAtColumn:1 statement:stmt];
  9083. NSString *description = [self textAtColumn:2 statement:stmt];
  9084. double price = sqlite3_column_double(stmt, 3);
  9085. double discount = sqlite3_column_double(stmt,4);
  9086. int qty = sqlite3_column_int(stmt, 5);
  9087. int percentage = sqlite3_column_int(stmt, 6);
  9088. int item_id = sqlite3_column_int(stmt, 7);
  9089. // int fashion_id = sqlite3_column_int(stmt, 8);
  9090. NSString *img_path = [self textAtColumn:9 statement:stmt];
  9091. NSString *line_note = [self textAtColumn:10 statement:stmt];
  9092. double percent = sqlite3_column_double(stmt, 11);
  9093. NSString *price_null = [self textAtColumn:3 statement:stmt];
  9094. if ([price_null isEqualToString:@"null"]) {
  9095. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  9096. }
  9097. NSMutableDictionary *item = @{
  9098. @"linenotes": line_note,
  9099. @"check": @(1),
  9100. @"product_id": product_id_string,
  9101. @"available_qty": @(qty),
  9102. @"available_percent" : @(percent),
  9103. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  9104. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  9105. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  9106. @"img": img_path,
  9107. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  9108. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  9109. }.mutableCopy;
  9110. if (percentage) {
  9111. [item removeObjectForKey:@"available_qty"];
  9112. } else {
  9113. [item removeObjectForKey:@"available_percent"];
  9114. }
  9115. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  9116. if ([qty_null isEqualToString:@"null"]) {
  9117. [item removeObjectForKey:@"available_qty"];
  9118. }
  9119. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9120. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9121. }];
  9122. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  9123. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9124. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  9125. }
  9126. [iSalesDB close_db:db];
  9127. [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  9128. [dic setValue:@"" forKey:@"email_content"];
  9129. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  9130. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9131. return [RAConvertor dict2data:dic];
  9132. }
  9133. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  9134. __block int qty = 0;
  9135. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  9136. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9137. qty = sqlite3_column_int(stmt, 0);
  9138. }];
  9139. return qty;
  9140. }
  9141. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct {
  9142. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  9143. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  9144. NSMutableDictionary * values = params[@"replaceValue"];
  9145. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  9146. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  9147. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  9148. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  9149. NSString *pdf_path = @"";
  9150. if (direct) {
  9151. configureParams = [self valueInParams:params key:@"pdfUrl"];
  9152. } else {
  9153. pdf_path = [self valueInParams:params key:@"pdfPath"];
  9154. }
  9155. NSString *create_user = [self valueInParams:params key:@"user"];
  9156. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  9157. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  9158. // model info
  9159. // 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];
  9160. // V1.90 more color
  9161. NSString *sql = [NSString stringWithFormat:@"select p.product_id,ifnull(p.sheet_price,'null'),ifnull(p.sheet_discount,'null'),ifnull(p.available_qty,'null'),p.percentage,p.item_id,p.line_note,p.percent,m.more_color from offline_portfolio as p left join product as m on p.product_id = m.product_id where p.product_id in (%@);",product_ids];
  9162. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9163. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  9164. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9165. sqlite3 *db = [iSalesDB get_db];
  9166. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9167. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9168. int product_id = sqlite3_column_int(stmt, 0);
  9169. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  9170. double price = sqlite3_column_double(stmt, 1);
  9171. double discount = sqlite3_column_double(stmt,2);
  9172. int qty = sqlite3_column_int(stmt, 3);
  9173. int percentage = sqlite3_column_int(stmt, 4);
  9174. int item_id = sqlite3_column_int(stmt, 5);
  9175. NSString *line_note = [self textAtColumn:6 statement:stmt];
  9176. double percent = sqlite3_column_double(stmt, 7);
  9177. int more_color = sqlite3_column_int(stmt, 8);
  9178. NSString *price_null = [self textAtColumn:1 statement:stmt];
  9179. /* if ([price_null isEqualToString:@"null"]) {
  9180. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  9181. }
  9182. */
  9183. [product_ids_string appendFormat:@"%@,",product_id_string];
  9184. // Regular Price
  9185. int regular_price = [[params objectForKey:@"regular_price"] integerValue];
  9186. NSString *regular_price_str = [self get_portfolio_price:appDelegate.contact_id item_id:item_id price:regular_price db:db];
  9187. // QTY
  9188. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  9189. if ([qty_null isEqualToString:@"null"]) {
  9190. // 查available
  9191. qty = [self model_QTY:product_id_string db:db];
  9192. }
  9193. if (percentage) {
  9194. qty = qty * percent / 100;
  9195. }
  9196. // Special Price
  9197. if ([price_null isEqualToString:@"null"]) {
  9198. // price = regular price
  9199. price = [regular_price_str doubleValue];
  9200. }
  9201. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  9202. if (![discount_null isEqualToString:@"null"]) {
  9203. price = price * (1 - discount / 100.0);
  9204. }
  9205. NSMutableDictionary *item = @{
  9206. @"line_note": line_note,
  9207. @"product_id": product_id_string,
  9208. @"available_qty": @(qty),
  9209. @"more_color":@(more_color),
  9210. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  9211. @"regular_price" : regular_price_str,
  9212. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  9213. }.mutableCopy;
  9214. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9215. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9216. }];
  9217. [iSalesDB close_db:db];
  9218. if (product_ids_string.length > 0) {
  9219. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  9220. }
  9221. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  9222. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  9223. return [RAConvertor dict2data:resultDictionary];
  9224. }
  9225. NSString *model_info = [RAConvertor dict2string:dic];
  9226. // 创建PDF
  9227. // 在preview情况下保存,则不需要新建了
  9228. if (direct) {
  9229. NSMutableDictionary *tear_sheet_params = params;
  9230. // if (tear_sheet_id) {
  9231. // tear_sheet_params = values;
  9232. // }
  9233. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  9234. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  9235. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  9236. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  9237. resultDictionary = pdfInfo.mutableCopy;
  9238. } else { // 创建PDF失败
  9239. return pdfData;
  9240. }
  9241. } else {
  9242. // pdf_path 就是本地路径
  9243. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  9244. }
  9245. // 将文件移动到PDF Cache文件夹
  9246. NSString *newPath = [pdf_path lastPathComponent];
  9247. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9248. NSString *cachefolder = [paths objectAtIndex:0];
  9249. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9250. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  9251. NSFileManager *fileManager = [NSFileManager defaultManager];
  9252. NSError *error = nil;
  9253. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  9254. if (error) { // 移动文件失败
  9255. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  9256. return [RAConvertor dict2data:resultDictionary];
  9257. }
  9258. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  9259. pdf_path = [newPath lastPathComponent];
  9260. // 保存信息
  9261. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  9262. NSString *off_params = [RAConvertor dict2string:params];
  9263. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  9264. pdf_path = [self translateSingleQuote:pdf_path];
  9265. create_user = [self translateSingleQuote:create_user];
  9266. tear_note = [self translateSingleQuote:tear_note];
  9267. tear_name = [self translateSingleQuote:tear_name];
  9268. model_info = [self translateSingleQuote:model_info];
  9269. configureParams = [self translateSingleQuote:configureParams];
  9270. off_params = [self translateSingleQuote:off_params];
  9271. 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];
  9272. if (tear_sheet_id) {
  9273. int _id = [tear_sheet_id integerValue];
  9274. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  9275. }
  9276. int result = [iSalesDB execSql:save_pdf_sql];
  9277. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9278. //
  9279. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  9280. if (remove_Item) {
  9281. // portfolioId
  9282. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  9283. }
  9284. return [RAConvertor dict2data:resultDictionary];
  9285. }
  9286. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  9287. return [self offline_savePDF:params direct:YES];
  9288. }
  9289. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  9290. return [self offline_savePDF:params direct:NO];
  9291. }
  9292. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  9293. int offset = [[params valueForKey:@"offset"] integerValue];
  9294. int limit = [[params valueForKey:@"limit"] integerValue];
  9295. NSString *keyword = [params valueForKey:@"keyWord"];
  9296. NSString *where = @"where is_delete is null or is_delete = 0";
  9297. if (keyword.length) {
  9298. keyword = [self translateSingleQuote:keyword];
  9299. 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];
  9300. }
  9301. 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
  9302. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9303. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9304. NSString *cachefolder = [paths objectAtIndex:0];
  9305. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9306. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9307. NSString *name = [self textAtColumn:0 statement:stmt];
  9308. NSString *note = [self textAtColumn:1 statement:stmt];
  9309. NSString *time = [self textAtColumn:2 statement:stmt];
  9310. NSString *user = [self textAtColumn:3 statement:stmt];
  9311. NSString *path = [self textAtColumn:4 statement:stmt];
  9312. NSString *model_info = [self textAtColumn:6 statement:stmt];
  9313. NSString *off_params = [self textAtColumn:7 statement:stmt];
  9314. path = [pdfFolder stringByAppendingPathComponent:path];
  9315. BOOL bdir=NO;
  9316. NSFileManager* fileManager = [NSFileManager defaultManager];
  9317. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  9318. {
  9319. //pdf文件不存在
  9320. path=nil;
  9321. }
  9322. time = [self changeDateTimeFormate:time];
  9323. time = [time stringByAppendingString:@" PST"];
  9324. int sheet_id = sqlite3_column_int(stmt, 5);
  9325. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  9326. item[@"tearsheetsId"]=@(sheet_id);
  9327. item[@"pdf_path"]=path;
  9328. item[@"create_time"]=time;
  9329. item[@"create_user"]=user;
  9330. item[@"tear_note"]=note;
  9331. item[@"tear_name"]=name;
  9332. item[@"isLocalFile"]=[NSNumber numberWithBool:YES];
  9333. item[@"model_info"]=model_info;
  9334. item[@"off_params"]=off_params;
  9335. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9336. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9337. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9338. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9339. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  9340. }];
  9341. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9342. return [RAConvertor dict2data:dic];
  9343. }
  9344. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  9345. NSString *prodct_ids = [params objectForKey:@"product_id"];
  9346. // NSString *user = [params objectForKey:@"user"];
  9347. // 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];
  9348. 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];
  9349. int result = [iSalesDB execSql:sql];
  9350. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9351. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  9352. if (result == RESULT_TRUE) {
  9353. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  9354. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  9355. sqlite3 *db = [iSalesDB get_db];
  9356. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  9357. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  9358. [iSalesDB close_db:db];
  9359. }
  9360. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9361. return [RAConvertor dict2data:dic];
  9362. }
  9363. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  9364. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  9365. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  9366. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  9367. NSMutableDictionary *section_0 = [[dic objectForKey:@"section_0"] mutableCopy];
  9368. NSDictionary *company_item = @{
  9369. @"control": @"text",
  9370. @"keyboard": @"text",
  9371. @"name": @"company_name",
  9372. @"value": COMPANY_FULL_NAME,
  9373. @"aname": @"Company Name"
  9374. };
  9375. [section_0 setObject:company_item forKey:@"item_0"];
  9376. [dic setObject:section_0 forKey:@"section_0"];
  9377. // Regurlar Price
  9378. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  9379. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  9380. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  9381. __block long val_count = 0;
  9382. NSString *sql = @"select name,type,order_by from price order by order_by";
  9383. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9384. NSString *name = [self textAtColumn:0 statement:stmt];
  9385. int type = sqlite3_column_int(stmt, 1);
  9386. int order_by = sqlite3_column_int(stmt, 2);
  9387. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9388. NSDictionary *price_dic = @{
  9389. @"value" : name,
  9390. @"value_id" : [NSNumber numberWithInteger:type],
  9391. @"check" : order_by == 0 ? @(1) : @(0)
  9392. };
  9393. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  9394. val_count = ++(*count);
  9395. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9396. val_count = 0;
  9397. }];
  9398. [cadedate setObject:@{@"value" : @"None",
  9399. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  9400. val_count++;
  9401. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  9402. [price setObject:cadedate forKey:@"cadedate"];
  9403. [section1 setObject:price forKey:@"item_2"];
  9404. [dic setObject:section1 forKey:@"section_1"];
  9405. return [RAConvertor dict2data:dic];
  9406. }
  9407. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  9408. NSString *product_id = [params objectForKey:@"fashionId"];
  9409. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  9410. __block int result = RESULT_TRUE;
  9411. __block int qty = 0;
  9412. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9413. qty = sqlite3_column_int(stmt, 0);
  9414. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9415. result = RESULT_FALSE;
  9416. }];
  9417. NSMutableDictionary *dic = @{
  9418. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  9419. @"mode" : @"Regular Mode",
  9420. @"quantity_available" : @(qty),
  9421. @"result" : @(result),
  9422. }.mutableCopy;
  9423. return [RAConvertor dict2data:dic];
  9424. }
  9425. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  9426. NSString *item_ids = [params objectForKey:@"item_id"];
  9427. NSString *line_notes = [params objectForKey:@"notes"];
  9428. NSString *price_str = [params objectForKey:@"price"];
  9429. NSString *discount_str = [params objectForKey:@"discount"];
  9430. NSString *percent = [params objectForKey:@"available_percent"];
  9431. NSString *qty = [params objectForKey:@"available_qty"];
  9432. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  9433. int dot = 0;
  9434. if (line_notes) {
  9435. line_notes = [self translateSingleQuote:line_notes];
  9436. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  9437. sql = [sql stringByAppendingString:line_notes];
  9438. dot = 1;
  9439. } else {
  9440. line_notes = @"";
  9441. }
  9442. if (price_str) {
  9443. if (dot) {
  9444. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  9445. } else {
  9446. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  9447. dot = 1;
  9448. }
  9449. sql = [sql stringByAppendingString:price_str];
  9450. } else {
  9451. price_str = @"";
  9452. }
  9453. if (discount_str) {
  9454. if (dot) {
  9455. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  9456. } else {
  9457. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  9458. dot = 1;
  9459. }
  9460. sql = [sql stringByAppendingString:discount_str];
  9461. } else {
  9462. discount_str = @"";
  9463. }
  9464. if (percent) {
  9465. if (dot) {
  9466. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  9467. } else {
  9468. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  9469. dot = 1;
  9470. }
  9471. sql = [sql stringByAppendingString:percent];
  9472. } else {
  9473. percent = @"";
  9474. }
  9475. if (qty) {
  9476. if (dot) {
  9477. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  9478. } else {
  9479. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  9480. dot = 1;
  9481. }
  9482. sql = [sql stringByAppendingString:qty];
  9483. } else {
  9484. qty = @"";
  9485. }
  9486. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  9487. sql = [sql stringByAppendingString:where];
  9488. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9489. int result = [iSalesDB execSql:sql];
  9490. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9491. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9492. return [RAConvertor dict2data:dic];
  9493. }
  9494. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  9495. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  9496. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9497. sqlite3 *db = [iSalesDB get_db];
  9498. 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];
  9499. int result = [iSalesDB execSql:sql db:db];
  9500. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9501. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9502. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  9503. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  9504. [iSalesDB close_db:db];
  9505. return [RAConvertor dict2data:dic];
  9506. }
  9507. + (void)offline_removePDFWithName:(NSString *)name {
  9508. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9509. NSString *cachefolder = [paths objectAtIndex:0];
  9510. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9511. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  9512. NSFileManager *fileManager = [NSFileManager defaultManager];
  9513. [fileManager removeItemAtPath:path error:nil];
  9514. }
  9515. + (void)offline_clear_PDFCache {
  9516. NSFileManager *fileManager = [NSFileManager defaultManager];
  9517. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9518. NSString *cachefolder = [paths objectAtIndex:0];
  9519. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9520. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  9521. for (NSString *path in pdf_files) {
  9522. [self offline_removePDFWithName:[path lastPathComponent]];
  9523. }
  9524. }
  9525. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  9526. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9527. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  9528. NSString *user = [params objectForKey:@"user"];
  9529. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %d;",tearsheetsId]]; // tearsheets_id ---> _id
  9530. if (![create_user isEqualToString:user]) {
  9531. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  9532. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9533. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  9534. return [RAConvertor dict2data:dic];
  9535. }
  9536. NSString *is_local_sql = [NSString stringWithFormat:@"select is_local,pdf_path from offline_pdf where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  9537. __block int is_local = 0;
  9538. __block NSString *path = @"";
  9539. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9540. is_local = sqlite3_column_int(stmt, 0);
  9541. path = [self textAtColumn:1 statement:stmt];
  9542. }];
  9543. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  9544. if (is_local == 1) {
  9545. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  9546. // 删除文件
  9547. [self offline_removePDFWithName:path];
  9548. }
  9549. int result = [iSalesDB execSql:sql];
  9550. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9551. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9552. return [RAConvertor dict2data:dic];
  9553. }
  9554. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial
  9555. {
  9556. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  9557. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  9558. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  9559. NSString* where=@"1=1";
  9560. if (ver!=nil) {
  9561. where=@"is_dirty=1";
  9562. }
  9563. 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];
  9564. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  9565. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9566. sqlite3 *db = [iSalesDB get_db];
  9567. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9568. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  9569. NSInteger _id = sqlite3_column_int(stmt, 0);
  9570. NSInteger product_id = sqlite3_column_int(stmt, 1);
  9571. NSString *name = [self textAtColumn:2 statement:stmt];
  9572. NSString *desc = [self textAtColumn:3 statement:stmt];
  9573. NSInteger item_id = sqlite3_column_int(stmt, 4);
  9574. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  9575. NSInteger qty = sqlite3_column_int(stmt, 6);
  9576. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  9577. double percent = sqlite3_column_double(stmt, 8);
  9578. double price = sqlite3_column_double(stmt, 9);
  9579. double discount = sqlite3_column_double(stmt, 10);
  9580. NSString *img = [self textAtColumn:11 statement:stmt];
  9581. NSString *line_note = [self textAtColumn:12 statement:stmt];
  9582. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  9583. NSString *create_time = [self textAtColumn:14 statement:stmt];
  9584. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  9585. NSString *price_null = [self textAtColumn:9 statement:stmt];
  9586. if ([price_null isEqualToString:@"null"]) {
  9587. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  9588. }
  9589. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  9590. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  9591. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  9592. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  9593. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  9594. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  9595. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  9596. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  9597. if ([qty_null isEqualToString:@"null"]) {
  9598. // [item setValue:@"null" forKey:@"available_qty"];
  9599. } else {
  9600. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  9601. }
  9602. if ([is_percent_null isEqualToString:@"null"]) {
  9603. // [item setValue:@"null" forKey:@"percentage"];
  9604. } else {
  9605. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  9606. }
  9607. if ([percent_null isEqualToString:@"null"]) {
  9608. // [item setValue:@"null" forKey:@"percent"];
  9609. } else {
  9610. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  9611. }
  9612. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  9613. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  9614. [item setValue:name forKey:@"name"];
  9615. [item setValue:desc forKey:@"description"];
  9616. [item setValue:img forKey:@"img"];
  9617. [item setValue:line_note forKey:@"line_note"];
  9618. [item setValue:create_time forKey:@"createtime"];
  9619. [item setValue:modify_time forKey:@"modifytime"];
  9620. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  9621. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9622. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  9623. }];
  9624. [iSalesDB close_db:db];
  9625. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  9626. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9627. }
  9628. return ret;
  9629. }
  9630. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  9631. {
  9632. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  9633. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  9634. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  9635. NSString* where=@"1=1";
  9636. if (ver!=nil) {
  9637. where=@"is_dirty=1";
  9638. }
  9639. 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];
  9640. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  9641. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9642. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  9643. NSInteger _id = sqlite3_column_int(stmt, 0);
  9644. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  9645. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  9646. NSString *create_user = [self textAtColumn:3 statement:stmt];
  9647. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  9648. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  9649. NSString *model_info = [self textAtColumn:6 statement:stmt];
  9650. NSString *createtime = [self textAtColumn:7 statement:stmt];
  9651. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  9652. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  9653. NSString *off_params = [self textAtColumn:10 statement:stmt];
  9654. NSString *uuid = [NSUUID UUID].UUIDString;
  9655. int is_delete = sqlite3_column_int(stmt, 11);
  9656. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  9657. int is_local = sqlite3_column_int(stmt, 12);
  9658. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  9659. if (![tearsheet_id_null isEqualToString:@"null"]) {
  9660. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  9661. }
  9662. [item setObject:pdf_path forKey:@"pdf_path"];
  9663. [item setObject:create_user forKey:@"create_user"];
  9664. [item setObject:tear_note forKey:@"tear_note"];
  9665. [item setObject:tear_name forKey:@"tear_name"];
  9666. [item setObject:model_info forKey:@"model_info"];
  9667. [item setObject:createtime forKey:@"createtime"];
  9668. [item setObject:modifytime forKey:@"modifytime"];
  9669. [item setObject:urlParams forKey:@"urlParams"];
  9670. [item setObject:off_params forKey:@"off_params"];
  9671. [item setObject:uuid forKey:@"pdf_token"];
  9672. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  9673. NSDictionary *off_Params_dic = [RAConvertor string2dict:off_params];
  9674. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  9675. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  9676. } else {
  9677. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  9678. }
  9679. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9680. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  9681. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9682. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9683. }];
  9684. return ret;
  9685. }
  9686. @end