OLDataProvider.m 534 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724
  1. //
  2. // OLDataProvider.m
  3. // iSales-NPD
  4. //
  5. // Created by Ray on 2/2/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "OLDataProvider.h"
  9. #import "iSalesDB.h"
  10. #import "RAUtils.h"
  11. #import "AESCrypt.h"
  12. #import "AppDelegate.h"
  13. #import "ZipArchive.h"
  14. #import "UIProgressView+AFNetworking.h"
  15. #import "AFHTTPSessionManager.h"
  16. #import "AppDelegate.h"
  17. #import "pdfCreator.h"
  18. #import "Singleton.h"
  19. #import "QRCodeGenerator.h"
  20. #import "config.h"
  21. @interface OLDataProvider ()
  22. @end
  23. @implementation OLDataProvider
  24. +(NSString*) get_portfolio_price:(NSString*) contact_id item_id:(int) item_id price:(int)price_id db:(sqlite3*)db
  25. {
  26. NSString * sqlQuery = [NSString stringWithFormat:@"select decrypt(price) from model_price where item_id=%d and price_name in (select name from price where type=%d) ",item_id,price_id];
  27. NSString* ret=@"No Price.";
  28. sqlite3_stmt * statement;
  29. // int count=0;
  30. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  31. {
  32. if (sqlite3_step(statement) == SQLITE_ROW)
  33. {
  34. char *price = (char*)sqlite3_column_text(statement, 0);
  35. if(price==nil)
  36. price="";
  37. NSString *nsprice = [[NSString alloc]initWithUTF8String:price];
  38. double dp= [nsprice doubleValue];
  39. ret=[NSString stringWithFormat:@"%.2f",dp];
  40. }
  41. sqlite3_finalize(statement);
  42. }
  43. return ret;
  44. // // [iSalesDB close_db:db];
  45. //
  46. // return nil;
  47. //// if(dprice==DBL_MAX)
  48. //// ret= nil;
  49. //// else
  50. //// ret= [NSNumber numberWithDouble:dprice];
  51. //// return ret;
  52. }
  53. + (NSString *)portfolio_img:(NSString *)item_id db:(sqlite3 *)db
  54. {
  55. NSString* ret= nil;
  56. NSString *sqlQuery = nil;
  57. // sqlQuery = [NSString stringWithFormat:@"select i.url from product m LEFT join model_image i on m.product_id = i.product_id where m.item_id=%@ order by i.default_img desc, i._id asc limit 1;",item_id];// select i.url from model m
  58. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image as i where item_id = %@ and i.type<=1 order by i.default_img desc, i.type desc limit 1;",item_id];
  59. sqlite3_stmt * statement;
  60. // int count=0;
  61. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  62. {
  63. if (sqlite3_step(statement) == SQLITE_ROW)
  64. {
  65. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  66. if(imgurl==nil)
  67. imgurl="";
  68. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  69. ret=nsimgurl;
  70. }
  71. sqlite3_finalize(statement);
  72. }
  73. else
  74. {
  75. ret=@"";
  76. }
  77. // [iSalesDB close_db:db];
  78. // DebugLog(@"data string: %@",ret );
  79. return ret;
  80. }
  81. +(NSMutableDictionary*) prepare_portfolio_data:(NSMutableDictionary*) params
  82. {
  83. NSMutableDictionary* values=params[@"replaceValue"];
  84. //生成portfolio pdf需要的数据
  85. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  86. data[@"npd_url"]=@"www.newpacificdirect.com";
  87. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  88. [formatter setDateFormat:@"MMMM yyyy"];
  89. NSString* date = [formatter stringFromDate:[NSDate date]];
  90. data[@"create_month"]=date;
  91. data[@"company_name"]=@"New Pacific Direct,Inc.";
  92. data[@"catalog_name"]=params[@"catalog_name"];
  93. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  94. data[@"TOTAL_PAGE"]=0;
  95. data[@"CURRENT_PAGE"]=0;
  96. NSMutableDictionary* grid = [[NSMutableDictionary alloc]init];
  97. bool show_stockout=[params[@"show_stockout"] boolValue];
  98. NSString *sql = nil;
  99. if(values==nil)
  100. {
  101. sql=[NSString stringWithFormat:@"select a.product_id,a.name,a.description,a.price,a.sheet_discount,a.qty,a.is_percentage,a.item_id,a.fashion_id,a.line_note,a.percent,a._id,b.availability,b.color,b.legcolor,b.demension,b.seat_height,b.material,b.box_dim,b.volume,b.weight,stockuom,b.load_ability,b.fabric_content,b.assembling,b.made_in from (select _id, product_id,name,description,ifnull(sheet_price,'null') as price,sheet_discount,ifnull(available_qty,'null') as qty,percentage as is_percentage,item_id,fashion_id,line_note,percent from offline_portfolio where product_id in(%@)) a left join (select * from product where product_id in(%@)) b on a.product_id=b.product_id order by a.name",params[@"product_ids"],params[@"product_ids"]];
  102. }
  103. else
  104. {
  105. sql=[NSString stringWithFormat:@"select product_id,name,description,null,null,null,null,item_id,null,null,null,-1,availability,color,legcolor,demension,seat_height,material,box_dim,volume,weight,stockuom,load_ability,fabric_content,assembling,made_in from product where product_id in(%@) order by name",params[@"product_ids"]];
  106. }
  107. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  108. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  109. sqlite3 *db = [iSalesDB get_db];
  110. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  111. // [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  112. int product_id = sqlite3_column_int(stmt, 0);
  113. // NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  114. NSString *name = [self textAtColumn:1 statement:stmt];
  115. NSString *description = [self textAtColumn:2 statement:stmt];
  116. double price = sqlite3_column_double(stmt, 3);
  117. NSString *s_price = [self textAtColumn:3 statement:stmt];
  118. double discount = sqlite3_column_double(stmt,4);
  119. int qty = sqlite3_column_int(stmt, 5);
  120. NSString *s_qty = [self textAtColumn:5 statement:stmt];
  121. int is_percentage = sqlite3_column_int(stmt, 6);
  122. NSString *s_is_percentage = [self textAtColumn:6 statement:stmt];
  123. int item_id = sqlite3_column_int(stmt, 7);
  124. // int fashion_id = sqlite3_column_int(stmt, 8);
  125. NSString *line_note = [self textAtColumn:9 statement:stmt];
  126. NSString *s_percent = [self textAtColumn:10 statement:stmt];
  127. double percent = sqlite3_column_double(stmt, 10);
  128. // int portfolio_id = sqlite3_column_int(stmt, 11);
  129. NSString *s_availability = [self textAtColumn:12 statement:stmt];
  130. int availability = sqlite3_column_int(stmt, 12);
  131. NSString *color = [self textAtColumn:13 statement:stmt];
  132. NSString *legcolor = [self textAtColumn:14 statement:stmt];
  133. NSString *demension = [self textAtColumn:15 statement:stmt];
  134. NSString *seat_height = [self textAtColumn:16 statement:stmt];
  135. NSString *material = [self textAtColumn:17 statement:stmt];
  136. NSString *box_dim = [self textAtColumn:18 statement:stmt];
  137. NSString *s_volume = [self textAtColumn:19 statement:stmt];
  138. double volume = sqlite3_column_double(stmt, 19);
  139. NSString *s_weight = [self textAtColumn:20 statement:stmt];
  140. double weight = sqlite3_column_double(stmt, 20);
  141. NSString *s_model_set = [self textAtColumn:21 statement:stmt];
  142. int model_set = sqlite3_column_int(stmt, 21);
  143. NSString *s_load_ability = [self textAtColumn:22 statement:stmt];
  144. double load_ability = sqlite3_column_double(stmt, 22);
  145. NSString *fabric_content = [self textAtColumn:23 statement:stmt];
  146. NSString *assembling = [self textAtColumn:24 statement:stmt];
  147. NSString *made_in = [self textAtColumn:25 statement:stmt];
  148. NSString* gprice = [self get_portfolio_price :appDelegate.contact_id item_id:item_id price:[params[@"regular_price"] intValue] db:db];
  149. float bprice=0;
  150. NSDictionary* bundle=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  151. for(int b=0;b< [bundle[@"count"] intValue];b++)
  152. {
  153. NSDictionary* bi=bundle[[NSString stringWithFormat:@"item_%d",b]];
  154. bprice+= [bi[@"modulus"] intValue] * [bi[@"unit_price"] floatValue];
  155. }
  156. if(gprice!=nil)
  157. gprice = [NSString stringWithFormat:@"%.2f",[gprice floatValue]+bprice];
  158. if(s_percent.length==0||is_percentage==0)
  159. {
  160. percent=100.0;
  161. if([s_qty isEqualToString:@"null"])
  162. qty=availability;
  163. }
  164. else
  165. {
  166. qty=availability;
  167. }
  168. qty=qty*percent/100+0.5;
  169. for(int i=0;i<[values[@"count"] intValue];i++)
  170. {
  171. NSMutableDictionary * valueitem=values[[NSString stringWithFormat:@"item_%d",i]];;
  172. if([valueitem[@"product_id"] intValue]==product_id)
  173. {
  174. line_note=valueitem[@"line_note"];
  175. qty=[valueitem[@"available_qty"] intValue];
  176. if([[valueitem allKeys] containsObject:@"available_qty"])
  177. s_qty=[NSString stringWithFormat:@"%d",qty];
  178. else
  179. s_qty=@"null";
  180. if(valueitem[@"regular_price"]!=nil)
  181. gprice=[NSString stringWithFormat:@"%.2f",[valueitem[@"regular_price"] floatValue]];
  182. s_price=valueitem[@"special_price"];
  183. price=[valueitem[@"special_price"] floatValue];
  184. discount=0;
  185. }
  186. }
  187. if(is_percentage==0&&qty==0&&!show_stockout)
  188. {
  189. return;
  190. }
  191. if((is_percentage!=0||s_is_percentage.length!=0)&&qty==0&&!show_stockout)
  192. {
  193. return;
  194. }
  195. NSString* set_price=@"";
  196. if([params[@"entered_price"] boolValue])
  197. {
  198. if (s_price==nil ) {
  199. // set_price=[NSString stringWithFormat:@"Special: $ %@",gprice];
  200. }
  201. else
  202. {
  203. if(price*(1-discount/100.0)!=gprice.floatValue)
  204. set_price=[NSString stringWithFormat:@"Special: $ %.2f",price*(1-discount/100.0)];
  205. }
  206. }
  207. NSString* get_price=@"";
  208. {
  209. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  210. // DebugLog(@"price time interval");
  211. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  212. if(gprice==nil)
  213. get_price=@"Price:No Price.";
  214. else
  215. get_price=[NSString stringWithFormat:@"Price: $ %@",gprice];
  216. }
  217. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  218. // if ([qty_null isEqualToString:@"null"]) {
  219. // qty=availability;
  220. // }
  221. //
  222. // NSString *qty_percent_null = [self textAtColumn:6 statement:stmt];
  223. // if ([qty_percent_null isEqualToString:@"null"]) {
  224. // percentage=1;
  225. // }
  226. NSString * str_availability=[NSString stringWithFormat:@"<b>Availability:</b> %d",(int)qty];
  227. NSString * str_color=[NSString stringWithFormat:@"<b>Color:</b> %@",color];
  228. NSString * str_model_set=[NSString stringWithFormat:@"<b>Set:</b> Sold in QTY of %d",model_set];
  229. NSString * str_legcolor=[NSString stringWithFormat:@"<b>Leg Color:</b> %@",legcolor];
  230. NSString * str_demension=[NSString stringWithFormat:@"<b>Dimension:</b> %@",demension];
  231. NSString * str_seat_height=[NSString stringWithFormat:@"<b>Seat Height:</b> %@",seat_height];
  232. NSString * str_meterial=[NSString stringWithFormat:@"<b>Meterial:</b> %@",material];
  233. NSString * str_box_dim=[NSString stringWithFormat:@"<b>Box Dimension:</b> %@",box_dim];
  234. NSString * str_volume=[NSString stringWithFormat:@"<b>Volume:</b> %.2f",volume];
  235. NSString * str_weight=[NSString stringWithFormat:@"<b>Weight:</b> %.2f",weight];
  236. NSString * str_load_ability=[NSString stringWithFormat:@"<b>Load Ability:</b> %.2f",load_ability];
  237. NSString * str_fabric_content=[NSString stringWithFormat:@"<b>Fabric Content:</b> %@",fabric_content];
  238. NSString * str_assembling=[NSString stringWithFormat:@"<b>Assembling:</b> %@",assembling];
  239. NSString * str_made_in=[NSString stringWithFormat:@"<b>Made In:</b> %@",made_in];
  240. NSString * str_line_note=[NSString stringWithFormat:@"<b>Line Note:</b> %@",line_note];
  241. if([params[@"availability"] boolValue]==false)
  242. str_availability=@"";
  243. else
  244. {
  245. if ([s_qty isEqualToString:@"null"] && [s_availability isEqualToString:@"0"])
  246. str_availability=@"<b>Availability:</b> In Production";
  247. }
  248. if([params[@"color"] boolValue]==false || color.length==0 )
  249. str_color=@"";
  250. if([params[@"set"] boolValue]==false || s_model_set.length==0 )
  251. str_model_set=@"";
  252. if([params[@"leg_color"] boolValue]==false || legcolor.length==0 )
  253. str_legcolor=@"";
  254. if([params[@"dimension"] boolValue]==false || demension.length==0 )
  255. str_demension=@"";
  256. if([params[@"seat_height"] boolValue]==false || seat_height.length==0 )
  257. str_seat_height=@"";
  258. if([params[@"material"] boolValue]==false || material.length==0 )
  259. str_meterial=@"";
  260. if([params[@"box_dimension"] boolValue]==false || box_dim.length==0 )
  261. str_box_dim=@"";
  262. if([params[@"volume"] boolValue]==false || s_volume.length==0 )
  263. str_volume=@"";
  264. if([params[@"weight"] boolValue]==false || s_weight.length==0 )
  265. str_weight=@"";
  266. if([params[@"load_ability"] boolValue]==false || s_load_ability.length==0 )
  267. str_load_ability=@"";
  268. if([params[@"fabric_content"] boolValue]==false || fabric_content.length==0 )
  269. str_fabric_content=@"";
  270. if([params[@"assembling"] boolValue]==false || assembling.length==0 )
  271. str_assembling=@"";
  272. if([params[@"made"] boolValue]==false || made_in.length==0 )
  273. str_made_in=@"";
  274. if([params[@"show_line_note"] boolValue]==false || line_note.length==0 )
  275. str_line_note=@"";
  276. NSMutableArray* arr_detail=[[NSMutableArray alloc]init];
  277. [arr_detail addObject:str_availability];
  278. [arr_detail addObject:str_color];
  279. [arr_detail addObject:str_model_set];
  280. [arr_detail addObject:str_legcolor];
  281. [arr_detail addObject:str_demension];
  282. [arr_detail addObject:str_seat_height];
  283. [arr_detail addObject:str_meterial];
  284. [arr_detail addObject:str_box_dim];
  285. [arr_detail addObject:str_volume];
  286. [arr_detail addObject:str_weight];
  287. [arr_detail addObject:str_load_ability];
  288. [arr_detail addObject:str_fabric_content];
  289. [arr_detail addObject:str_assembling];
  290. [arr_detail addObject:str_made_in];
  291. [arr_detail addObject:str_line_note];
  292. // NSString* detail =[NSString stringWithFormat:@"<font size=0.2>%@</font>",[RAUtils arr2string:arr_detail separator:@" " trim:true]];
  293. NSString* detail =[RAUtils arr2string:arr_detail separator:@" " trim:true];
  294. //model image;
  295. NSString* img_path = [self portfolio_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  296. // qr image
  297. NSString* qrpath=nil;
  298. if([params[@"show_barcode"] boolValue])
  299. {
  300. NSString* temp = NSTemporaryDirectory();
  301. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  302. qrpath=[temp stringByAppendingPathComponent:filename];
  303. UIImage* qrimg=[QRCodeGenerator qrImageForString:name imageSize:240];
  304. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  305. }
  306. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  307. item[@"img"]=img_path;
  308. item[@"img_qr"]=qrpath;
  309. item[@"special_price"]=set_price;
  310. item[@"price"]=get_price;
  311. item[@"name"]=name;
  312. item[@"description"]=description;
  313. item[@"detail"]=detail;
  314. item[@"img"]=img_path;
  315. // @{
  316. // //@"linenotes": line_note,
  317. //// @"product_id": product_id_string,
  318. //// @"available_qty": @(qty),
  319. //// @"available_percent" : @(percent),
  320. //// @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  321. //// @"item_id": [NSString stringWithFormat:@"%d",item_id],
  322. //// @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  323. // @"": ,
  324. // @"": ,
  325. // @"": ,
  326. // @"": ,
  327. // @"": ,
  328. // @"": ,
  329. // @"":
  330. // }.mutableCopy;
  331. // if (percentage) {
  332. // [item removeObjectForKey:@"available_qty"];
  333. // } else {
  334. // [item removeObjectForKey:@"available_percent"];
  335. // }
  336. //
  337. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  338. // if ([qty_null isEqualToString:@"null"]) {
  339. // [item removeObjectForKey:@"available_qty"];
  340. // }
  341. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  342. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  343. }];
  344. [iSalesDB close_db:db];
  345. // [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  346. // [dic setValue:@"" forKey:@"email_content"];
  347. // [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  348. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  349. //
  350. // NSMutableDictionary* cell = [[NSMutableDictionary alloc]init];
  351. // cell[@"count"]=[NSNumber numberWithInt:10];
  352. grid[@"cell0"]=dic;
  353. data[@"grid0"]=grid;
  354. return data;
  355. }
  356. +(NSMutableDictionary*) get_pdftemplate:(NSString*) template
  357. {
  358. NSString *default_path = [[NSBundle mainBundle] pathForResource:template ofType:nil];
  359. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  360. NSString *cache_folder=[paths objectAtIndex:0];
  361. NSString *template_path = [cache_folder stringByAppendingPathComponent:[NSString stringWithFormat:@"offline_data/%@",template]];
  362. BOOL bdir=NO;
  363. NSFileManager* fileManager = [NSFileManager defaultManager];
  364. if(PDF_DEBUG)
  365. {
  366. NSData *data = [NSData dataWithContentsOfFile:default_path];
  367. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  368. return ret;
  369. }
  370. if(! [fileManager fileExistsAtPath:template_path isDirectory:&bdir] )
  371. {
  372. NSError * error=nil;
  373. if(![fileManager copyItemAtPath:default_path toPath:template_path error:&error])
  374. {
  375. return nil;
  376. }
  377. }
  378. NSData *data = [NSData dataWithContentsOfFile:template_path];
  379. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  380. return ret;
  381. }
  382. +(NSMutableDictionary*) query_login:(NSString*) user db:(sqlite3*) db
  383. {
  384. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  385. 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 ];
  386. DebugLog(@"offline_login sql:%@",sqlQuery);
  387. sqlite3_stmt * statement;
  388. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  389. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  390. {
  391. if (sqlite3_step(statement) == SQLITE_ROW)
  392. {
  393. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  394. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  395. int can_show_price = sqlite3_column_int(statement, 0);
  396. int can_see_price = sqlite3_column_int(statement, 1);
  397. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  398. if(contact_id==nil)
  399. contact_id="";
  400. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  401. int user_type = sqlite3_column_int(statement, 3);
  402. int can_cancel_order = sqlite3_column_int(statement, 4);
  403. int can_set_cart_price = sqlite3_column_int(statement, 5);
  404. int can_create_portfolio = sqlite3_column_int(statement, 6);
  405. int can_delete_order = sqlite3_column_int(statement, 7);
  406. int can_submit_order = sqlite3_column_int(statement, 8);
  407. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  408. int can_create_order = sqlite3_column_int(statement, 10);
  409. char *mode = (char*)sqlite3_column_text(statement, 11);
  410. if(mode==nil)
  411. mode="";
  412. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  413. char *username = (char*)sqlite3_column_text(statement, 12);
  414. if(username==nil)
  415. username="";
  416. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  417. int can_update_contact_info = sqlite3_column_int(statement, 13);
  418. char *first_name = (char*)sqlite3_column_text(statement, 14);
  419. if(first_name==nil)
  420. first_name="";
  421. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  422. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  423. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  424. // [header setValue:nscontact_id forKey:@"contact_id"];
  425. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  426. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  427. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  428. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  429. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  430. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  431. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  432. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  433. //
  434. // [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  435. //
  436. // [header setValue:nsusername forKey:@"username"];
  437. //
  438. // NSError* error=nil;
  439. //
  440. // NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  441. // [header setValue:statusFilter forKey:@"statusFilter"];
  442. //
  443. //
  444. //
  445. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  446. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  447. //
  448. // // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  449. // // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  450. // int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  451. // [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  452. //
  453. // [ret setObject:header forKey:@"header"];
  454. [ret setValue:nsfirst_name forKey:@"first_name"];
  455. }
  456. sqlite3_finalize(statement);
  457. }
  458. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  459. return ret;
  460. }
  461. +(NSMutableDictionary*) prepare_salesorder_data:(NSString*) order_code type:(int) type
  462. {
  463. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  464. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  465. data[@"npd_url"]=@"www.newpacificdirect.com";
  466. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  467. [formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
  468. NSString* date = [formatter stringFromDate:[NSDate date]];
  469. data[@"print_date"]=date;
  470. data[@"company_name"]=@"New Pacific Direct,Inc.";
  471. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  472. data[@"TOTAL_PAGE"]=0;
  473. data[@"CURRENT_PAGE"]=0;
  474. // [formatter setDateFormat:@"MM/dd/yyyy"];
  475. // date = [formatter stringFromDate:[NSDate date]];
  476. // data[@"creat_date"]=date;
  477. NSString* temp = NSTemporaryDirectory();
  478. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  479. NSString* qrpath=[temp stringByAppendingPathComponent:filename];
  480. UIImage* qrimg=[RAUtils generateBarCode:order_code width:480 height:44];//[QRCodeGenerator qrImageForString:order_code imageSize:240];
  481. bool bwrite=[UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  482. data[@"img_barcode"]=qrpath;
  483. data[@"so_id"]=order_code;
  484. data[@"company"]=@"NEW PACIFIC DIRECT, INC.";
  485. // 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];
  486. 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];
  487. sqlite3 *db = [iSalesDB get_db];
  488. data[@"print_by"]= [self query_login:appDelegate.user db:db][@"first_name"];
  489. sqlite3_stmt * statement;
  490. NSMutableDictionary* datasource = [[NSMutableDictionary alloc]init];
  491. NSMutableDictionary* row0 = [[NSMutableDictionary alloc]init];
  492. row0[@"count"]=[NSNumber numberWithInt:1];
  493. datasource[@"row0"]=row0;
  494. NSString* print_status=@"";
  495. NSString* order_type=@"Offline Quote";
  496. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  497. {
  498. if (sqlite3_step(statement) == SQLITE_ROW)
  499. {
  500. int erpOrderStatus = sqlite3_column_int(statement, 0);
  501. int status = sqlite3_column_int(statement, 26);
  502. if(status==2)
  503. {
  504. switch (erpOrderStatus) {
  505. case 10:
  506. // order_type=@"Quoted Order";
  507. print_status=@"Quote";
  508. break;
  509. case 11:
  510. print_status=@"Booked";
  511. break;
  512. case 12:
  513. print_status=@"Picked";
  514. break;
  515. case 13:
  516. print_status=@"Shipped";
  517. break;
  518. case 14:
  519. print_status=@"Closed";
  520. break;
  521. case 15:
  522. print_status=@"Canceled";
  523. break;
  524. case 16:
  525. print_status=@"All";
  526. break;
  527. case 18:
  528. print_status=@"Processing";
  529. break;
  530. default:
  531. break;
  532. }
  533. }
  534. else{
  535. switch (status) {
  536. case 0:
  537. {
  538. print_status=@"Quote";
  539. break;
  540. }
  541. case 1:
  542. {
  543. print_status=@"Saved";
  544. break;
  545. }
  546. case 3:
  547. {
  548. //canceled
  549. print_status=@"Canceled";
  550. break;
  551. }
  552. default:
  553. break;
  554. }
  555. }
  556. NSString* sender_name = [self textAtColumn:1 statement:statement];
  557. NSString* sender_ext = [self textAtColumn:2 statement:statement];
  558. NSString* sender_phone = [self textAtColumn:3 statement:statement];
  559. NSString* sender_fax = [self textAtColumn:4 statement:statement];
  560. NSString* sender_email = [self textAtColumn:5 statement:statement];
  561. NSString* warehouse_name = [self textAtColumn:6 statement:statement];
  562. NSString* billing_name = [self textAtColumn:7 statement:statement];
  563. NSString* billing_ext = [self textAtColumn:8 statement:statement];
  564. NSString* billing_phone = [self textAtColumn:9 statement:statement];
  565. if(billing_phone.length>0)
  566. billing_phone= [@"TEL: " stringByAppendingString:billing_phone];
  567. NSString* billing_fax = [self textAtColumn:10 statement:statement];
  568. if(billing_fax.length>0)
  569. billing_fax= [@"FAX: " stringByAppendingString:billing_fax];
  570. NSMutableArray* billto_arr0 = [[NSMutableArray alloc]init];
  571. [billto_arr0 addObject:billing_phone];
  572. [billto_arr0 addObject:billing_fax];
  573. NSString* billing_pf=[RAUtils arr2string:billto_arr0 separator:@" " trim:true];
  574. NSString* billing_email = [self textAtColumn:11 statement:statement];
  575. if(billing_email.length>0)
  576. billing_email= [@"EMAIL: " stringByAppendingString:billing_email];
  577. NSString* billing_contact = [self textAtColumn:31 statement:statement];
  578. if(billing_contact.length>0)
  579. billing_contact= [@"ATTN: " stringByAppendingString:billing_contact];
  580. NSString* receive_name = [self textAtColumn:12 statement:statement];
  581. NSString* receive_ext = [self textAtColumn:13 statement:statement];
  582. NSString* receive_phone = [self textAtColumn:14 statement:statement];
  583. if(receive_phone.length>0)
  584. receive_phone= [@"TEL: " stringByAppendingString:receive_phone];
  585. NSString* receive_fax = [self textAtColumn:15 statement:statement];
  586. if(receive_fax.length>0)
  587. receive_fax= [@"FAX: " stringByAppendingString:receive_fax];
  588. NSMutableArray* receive_arr0 = [[NSMutableArray alloc]init];
  589. [receive_arr0 addObject:receive_phone];
  590. [receive_arr0 addObject:receive_fax];
  591. NSString* receive_email = [self textAtColumn:16 statement:statement];
  592. NSString* receive_pf=[RAUtils arr2string:billto_arr0 separator:@" " trim:true];
  593. if(receive_email.length>0)
  594. receive_email= [@"EMAIL: " stringByAppendingString:receive_email];
  595. NSString* receive_contact = [self textAtColumn:32 statement:statement];
  596. if(receive_contact.length>0)
  597. receive_contact= [@"ATTN: " stringByAppendingString:receive_contact];
  598. NSString* poNumber = [self textAtColumn:17 statement:statement];
  599. NSString* sales_rep = [self textAtColumn:18 statement:statement];
  600. NSString* carrier = [self textAtColumn:19 statement:statement];
  601. NSString* vendor_no = [self textAtColumn:20 statement:statement];
  602. NSString* terms = [self textAtColumn:21 statement:statement];
  603. NSString* sales_terms = [self textAtColumn:22 statement:statement];
  604. NSString* port_destination = [self textAtColumn:23 statement:statement];
  605. NSString* etd = [self textAtColumn:24 statement:statement];
  606. NSString* sign_picpath = [self textAtColumn:25 statement:statement];
  607. if(sign_picpath.length==0)
  608. sign_picpath=nil;
  609. NSString* create_date = [self textAtColumn:27 statement:statement];
  610. create_date=[self changeDateTime:create_date Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy"];
  611. NSString* general_notes=[self textAtColumn:33 statement:statement];
  612. NSString *customerID = [self textAtColumn:34 statement:statement];
  613. if (customerID.length == 0) {
  614. customerID = nil;
  615. }
  616. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  617. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  618. if(general_notes.length>0)
  619. {
  620. NSMutableDictionary* row2 = [[NSMutableDictionary alloc]init];
  621. row2[@"item_0"]=@{@"g_notes":general_notes };
  622. row2[@"count"]=[NSNumber numberWithInt:1];
  623. datasource[@"row2"]=row2;
  624. }
  625. NSMutableArray* billto_arr = [[NSMutableArray alloc]init];
  626. [billto_arr addObject:billing_ext];
  627. [billto_arr addObject:billing_pf];
  628. [billto_arr addObject:billing_contact];
  629. [billto_arr addObject:billing_email];
  630. NSString* billto=[RAUtils arr2string:billto_arr separator:@"\r\n" trim:true];
  631. NSMutableArray* receive_arr = [[NSMutableArray alloc]init];
  632. [receive_arr addObject:receive_ext];
  633. [receive_arr addObject:receive_pf];
  634. [receive_arr addObject:receive_contact];
  635. [receive_arr addObject:receive_email];
  636. NSString* receive=[RAUtils arr2string:receive_arr separator:@"\r\n" trim:true];
  637. float shipping = sqlite3_column_double(statement, 28);
  638. float lift_gate_value = sqlite3_column_double(statement, 29);
  639. float handling_fee_value = sqlite3_column_double(statement, 30);
  640. data[@"company"]=sender_name;
  641. data[@"shipfrom_addr"]=sender_ext;
  642. data[@"shipfrom_phone"]=sender_phone;
  643. data[@"shipfrom_fax"]=sender_fax;
  644. data[@"shipfrom_email"]=sender_email;
  645. data[@"warehouse_name"]=warehouse_name;
  646. data[@"bill_to_name"]=billing_name;
  647. data[@"bill_to_ext"]=billto;
  648. data[@"ship_to_name"]=receive_name;
  649. data[@"ship_to_ext"]=receive;
  650. data[@"po_no"]=poNumber;
  651. data[@"rep"]=sales_rep;
  652. data[@"e_schdule"]=etd;
  653. data[@"sales_terms"]=sales_terms;
  654. data[@"port_destination"]=port_destination;
  655. data[@"terms"]=terms;
  656. data[@"carrier"]=carrier;
  657. data[@"vendor_no"]=vendor_no;
  658. data[@"customer_sign"]=sign_picpath;
  659. data[@"print_status"]=print_status;
  660. data[@"create_date"]=create_date;
  661. 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];;
  662. sqlite3_stmt * statement_cart;
  663. // NSDate *date1 = [NSDate date];
  664. int count=0;
  665. int cart_count=0;
  666. float credits=0;
  667. int dbresult=sqlite3_prepare_v2(db, [sql_cart UTF8String], -1, &statement_cart, nil);
  668. if ( dbresult== SQLITE_OK)
  669. {
  670. NSMutableDictionary* row1 = [[NSMutableDictionary alloc]init];
  671. int row1count=0;
  672. while (sqlite3_step(statement_cart) == SQLITE_ROW)
  673. {
  674. NSDate *row_date = [NSDate date];
  675. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  676. // int product_id = sqlite3_column_int(statement_cart, 0);
  677. char *str_price = (char*)sqlite3_column_text(statement_cart, 1);
  678. int item_id = sqlite3_column_int(statement_cart, 7);
  679. double discount = sqlite3_column_double(statement_cart, 2);
  680. NSString* Price=nil;
  681. if(str_price==nil)
  682. {
  683. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  684. // NSDate *price_date = [NSDate date];
  685. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db];
  686. // DebugLog(@"price time interval");
  687. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  688. if(price==nil)
  689. Price=@"No Price.";
  690. else
  691. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  692. }
  693. else
  694. {
  695. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  696. }
  697. float dunitprice=0;
  698. int item_count = sqlite3_column_int(statement_cart, 3);
  699. NSString* Amount=@"";
  700. if(![Price isEqualToString:@"No Price."])
  701. {
  702. dunitprice=[Price floatValue];
  703. dunitprice=dunitprice* (1-discount/100.0);
  704. Price=[NSString stringWithFormat:@"%.2f",dunitprice];
  705. credits+=dunitprice*item_count;
  706. Amount=[NSString stringWithFormat:@"%.2f",dunitprice*item_count];
  707. }
  708. else
  709. {
  710. Price=@"";
  711. }
  712. // char *line_note = (char*)sqlite3_column_text(statement_cart, 4);
  713. // NSString *nsline_note=nil;
  714. // if(line_note!=nil)
  715. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  716. char *name = (char*)sqlite3_column_text(statement_cart, 5);
  717. NSString *nsname=nil;
  718. if(name!=nil)
  719. nsname= [[NSString alloc]initWithUTF8String:name];
  720. char *description = (char*)sqlite3_column_text(statement_cart, 6);
  721. NSString *nsdescription=nil;
  722. if(description!=nil)
  723. nsdescription= [[NSString alloc]initWithUTF8String:description];
  724. // int stockUom = sqlite3_column_int(statement_cart, 8);
  725. // int _id = sqlite3_column_int(statement_cart, 9);
  726. // NSDate *subtotal_date = [NSDate date];
  727. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  728. // DebugLog(@"subtotal_date time interval");
  729. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  730. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  731. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  732. // int carton=[bsubtotaljson[@"carton"] intValue];
  733. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  734. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  735. // NSDate *img_date = [NSDate date];
  736. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  737. // DebugLog(@"img_date time interval");
  738. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  739. itemjson[@"model"]=nsname;
  740. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  741. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  742. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  743. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  744. // itemjson[@"check"]=@"true";
  745. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  746. itemjson[@"count"]=[NSString stringWithFormat:@"%d",item_count];
  747. itemjson[@"unit_price"]=Price;
  748. itemjson[@"amount"]=Amount;
  749. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  750. itemjson[@"description"]=[NSString stringWithFormat:@"%@",nsdescription ];
  751. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  752. // itemjson[@"note"]=nsline_note;
  753. // NSDate *date2 = [NSDate date];
  754. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  755. // DebugLog(@"model_bundle time interval");
  756. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  757. itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  758. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=itemjson;
  759. row1count++;
  760. NSDictionary* combine =[self model_bundle:item_id db:db compute_part:false contactID:customerID];
  761. for(int c=0;c<[combine[@"count"] intValue];c++)
  762. {
  763. NSMutableDictionary* combine_item=[combine[[NSString stringWithFormat:@"item_%d",c]] mutableCopy];
  764. combine_item[@"count"]=[NSString stringWithFormat:@"%d",[combine_item[@"modulus"] intValue]*item_count];
  765. // combine_item[@"count"]=[NSNumber numberWithInt:[combine_item[@"modulus"] intValue]*item_count];
  766. double combine_amount=[combine_item[@"modulus"] intValue]*item_count* [combine_item[@"unit_price"] floatValue];
  767. credits+=combine_amount;
  768. combine_item[@"amount"]=[NSString stringWithFormat:@"%.2f",combine_amount];
  769. combine_item[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,c+2];
  770. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=combine_item;
  771. row1count++;
  772. }
  773. count++;
  774. // DebugLog(@"row time interval");
  775. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  776. }
  777. row1[@"count"]=[NSNumber numberWithInt:row1count];
  778. datasource[@"row1"]=row1;
  779. data[@"table_model"]=datasource;
  780. sqlite3_finalize(statement_cart);
  781. }
  782. NSMutableDictionary* fee = [[NSMutableDictionary alloc]init];
  783. NSMutableDictionary* fee_row0 = [[NSMutableDictionary alloc]init];
  784. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  785. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  786. // if(credits>0)
  787. fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  788. // else
  789. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": @""};
  790. if(shipping>0)
  791. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": [NSString stringWithFormat:@"%.2f",shipping]};
  792. else
  793. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": @""};
  794. if(lift_gate_value>0)
  795. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",lift_gate_value]};
  796. else
  797. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": @""};
  798. if(handling_fee_value>0)
  799. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",handling_fee_value]};
  800. else
  801. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": @""};
  802. float total = credits+shipping+lift_gate_value+handling_fee_value;
  803. if(total>0)
  804. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": [NSString stringWithFormat:@"%.2f",total]};
  805. else
  806. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": @""};
  807. fee_row0[@"count"]=[NSNumber numberWithInt:5];
  808. fee[@"row0"]=fee_row0;
  809. data[@"table_fee"]=fee;
  810. // NSString *sign_url = [self textAtColumn:51 statement:statement];
  811. // ret[@"sign_url"] = sign_url;
  812. //
  813. // customer_contact = [self textAtColumn:52 statement:statement];
  814. // customer_email = [self textAtColumn:53 statement:statement];
  815. // customer_phone = [self textAtColumn:54 statement:statement];
  816. // customer_fax = [self textAtColumn:55 statement:statement];
  817. //
  818. //
  819. // int offline_edit=sqlite3_column_int(statement, 56);
  820. //
  821. // ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  822. //
  823. // char *soid = (char*)sqlite3_column_text(statement, 1);
  824. // if(soid==nil)
  825. // soid= "";
  826. // nssoid= [[NSString alloc]initWithUTF8String:soid];
  827. // // so#
  828. // ret[@"so#"] = nssoid;
  829. //
  830. // char *poNumber = (char*)sqlite3_column_text(statement, 2);
  831. // if(poNumber==nil)
  832. // poNumber= "";
  833. // NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  834. //
  835. //
  836. //
  837. // char *create_time = (char*)sqlite3_column_text(statement, 3);
  838. // if(create_time==nil)
  839. // create_time= "";
  840. // NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  841. // nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  842. //
  843. // int status = sqlite3_column_int(statement, 4);
  844. // int erpStatus = sqlite3_column_int(statement, 49);
  845. // NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  846. //
  847. // // status
  848. // if (status > 1 && status != 3) {
  849. // status = erpStatus;
  850. // } else if (status == 3) {
  851. // status = 15;
  852. // }
  853. // ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  854. // ret[@"order_status"] = nsstatus;
  855. //
  856. //
  857. // char *company_name = (char*)sqlite3_column_text(statement, 5);
  858. // if(company_name==nil)
  859. // company_name= "";
  860. // NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  861. // // company name
  862. // ret[@"company_name"] = nscompany_name;
  863. //
  864. // char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  865. // if(customer_contact==nil)
  866. // customer_contact= "";
  867. // NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  868. //
  869. //
  870. // char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  871. // if(addr_1==nil)
  872. // addr_1="";
  873. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  874. //
  875. // char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  876. // if(addr_2==nil)
  877. // addr_2="";
  878. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  879. //
  880. //
  881. // char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  882. // if(addr_3==nil)
  883. // addr_3="";
  884. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  885. //
  886. //
  887. // char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  888. // if(addr_4==nil)
  889. // addr_4="";
  890. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  891. //
  892. // NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  893. // [arr_addr addObject:nsaddr_1];
  894. // [arr_addr addObject:nsaddr_2];
  895. // [arr_addr addObject:nsaddr_3];
  896. // [arr_addr addObject:nsaddr_4];
  897. // NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  898. //
  899. //
  900. // char *logist = (char*)sqlite3_column_text(statement, 11);
  901. // if(logist==nil)
  902. // logist= "";
  903. // NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  904. // if (status == -11 || status == 10 || status == 11) {
  905. // nslogist = [self textAtColumn:59 statement:statement];
  906. // };
  907. //
  908. // NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  909. // if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  910. // shipping = @"Shipping To Be Quoted";
  911. // } else {
  912. // shippingFee = sqlite3_column_double(statement, 12);
  913. // }
  914. //
  915. // // Shipping
  916. // ret[@"Shipping"] = shipping;
  917. //
  918. // int have_lift_gate = sqlite3_column_int(statement, 17);
  919. // lift_gate = sqlite3_column_double(statement, 13);
  920. // // Liftgate Fee(No loading dock)
  921. // if (!have_lift_gate) {
  922. // lift_gate = 0;
  923. // }
  924. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  925. // if (sqlite3_column_int(statement, 57)) {
  926. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  927. // }
  928. //
  929. // char *general_notes = (char*)sqlite3_column_text(statement, 14);
  930. // if(general_notes==nil)
  931. // general_notes= "";
  932. // NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  933. //
  934. // char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  935. // if(internal_notes==nil)
  936. // internal_notes= "";
  937. // NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  938. //
  939. // char *payment_type = (char*)sqlite3_column_text(statement, 16);
  940. // if(payment_type==nil)
  941. // payment_type= "";
  942. // NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  943. //
  944. //
  945. //
  946. // // order info
  947. // orderinfo = [self textFileName:@"order_info.html"];
  948. //
  949. //
  950. // orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  951. // orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  952. // orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  953. // orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  954. //
  955. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  956. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  957. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  958. // orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  959. // orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  960. // orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  961. // orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  962. // orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  963. //
  964. // NSString *payment = nil;
  965. // if([nspayment_type isEqualToString:@"Credit Card"])
  966. // {
  967. // payment = [self textFileName:@"creditcardpayment.html"];
  968. //
  969. // NSString *card_first_name = [self textAtColumn:37 statement:statement];
  970. // NSString *card_last_name = [self textAtColumn:38 statement:statement];
  971. // NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  972. // NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  973. // NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  974. // NSString *card_type = [self textAtColumn:42 statement:statement];
  975. // if (card_type.length > 0) { // 显示星号
  976. // card_type = @"****";
  977. // }
  978. // NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  979. // if (card_number.length > 0 && card_number.length > 4) {
  980. // for (int i = 0; i < card_number.length - 4; i++) {
  981. // card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  982. // }
  983. // } else {
  984. // card_number = @"";
  985. // }
  986. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  987. // NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  988. // NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  989. // NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  990. // if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  991. // card_expiration = @"****";
  992. // }
  993. //
  994. //
  995. // NSString *card_city = [self textAtColumn:46 statement:statement];
  996. // NSString *card_state = [self textAtColumn:47 statement:statement];
  997. //
  998. // payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  999. // payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  1000. // payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  1001. // payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  1002. // payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  1003. // payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  1004. // payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  1005. // payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  1006. // payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  1007. // payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  1008. //
  1009. //
  1010. // }
  1011. // else
  1012. // {
  1013. // payment=[self textFileName:@"normalpayment.html"];
  1014. // payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  1015. // }
  1016. // orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  1017. //
  1018. // ret[@"result"]= [NSNumber numberWithInt:2];
  1019. //
  1020. // // more info
  1021. // moreInfo = [self textFileName:@"more_info.html"];
  1022. //
  1023. //
  1024. // // ShipToCompany_or_&nbsp
  1025. // NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  1026. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  1027. // NSString *shipToName = [self textAtColumn:19 statement:statement];
  1028. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  1029. // NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  1030. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  1031. //
  1032. //
  1033. // // ShipFromCompany_or_&nbsp
  1034. // NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  1035. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  1036. // NSString *shipFromName = [self textAtColumn:22 statement:statement];
  1037. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  1038. // NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  1039. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  1040. //
  1041. //
  1042. // // FreightBillToCompany_or_&nbsp
  1043. // NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  1044. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  1045. // NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  1046. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  1047. // NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  1048. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  1049. //
  1050. //
  1051. // // MerchandiseBillToCompany_or_&nbsp
  1052. // NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  1053. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  1054. // NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  1055. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  1056. // NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  1057. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  1058. //
  1059. //
  1060. // // ReturnToCompany_or_&nbsp
  1061. // NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  1062. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  1063. // NSString *returnToName = [self textAtColumn:31 statement:statement];
  1064. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  1065. // NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  1066. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  1067. // //
  1068. //
  1069. // // DebugLog(@"more info : %@",moreInfo);
  1070. //
  1071. // // handling fee
  1072. // handlingFee = sqlite3_column_double(statement, 33);
  1073. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  1074. // if (sqlite3_column_int(statement, 58)) {
  1075. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  1076. // }
  1077. // //
  1078. //
  1079. // // customer info
  1080. // customerID = [self textAtColumn:36 statement:statement];
  1081. //
  1082. // // mode
  1083. // ret[@"mode"] = appDelegate.mode;
  1084. //
  1085. // // model_count
  1086. // ret[@"model_count"] = @(0);
  1087. }
  1088. sqlite3_finalize(statement);
  1089. }
  1090. [iSalesDB close_db:db];
  1091. data[@"order_type"]=order_type;
  1092. /*
  1093. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  1094. // "customer_email" = "Shui Hu";
  1095. // "customer_fax" = "";
  1096. // "customer_first_name" = F;
  1097. // "customer_last_name" = L;
  1098. // "customer_name" = ",da He Xiang Dong Liu A";
  1099. // "customer_phone" = "Hey Xuan Feng";
  1100. contactInfo[@"customer_phone"] = customer_phone;
  1101. contactInfo[@"customer_fax"] = customer_fax;
  1102. contactInfo[@"customer_email"] = customer_email;
  1103. NSString *first_name = @"";
  1104. NSString *last_name = @"";
  1105. if ([customer_contact isEqualToString:@""]) {
  1106. } else if ([customer_contact containsString:@" "]) {
  1107. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  1108. first_name = [customer_contact substringToIndex:first_space_index];
  1109. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  1110. }
  1111. contactInfo[@"customer_first_name"] = first_name;
  1112. contactInfo[@"customer_last_name"] = last_name;
  1113. ret[@"customerInfo"] = contactInfo;
  1114. // models
  1115. if (nssoid) {
  1116. __block double TotalCuft = 0;
  1117. __block double TotalWeight = 0;
  1118. __block int TotalCarton = 0;
  1119. __block double allItemPrice = 0;
  1120. 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];
  1121. sqlite3 *db1 = [iSalesDB get_db];
  1122. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1123. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1124. int product_id = sqlite3_column_int(stmt, 0);
  1125. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  1126. int item_id = sqlite3_column_int(stmt, 7);
  1127. NSString* Price=nil;
  1128. if(str_price==nil)
  1129. {
  1130. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  1131. if(price==nil)
  1132. Price=@"No Price.";
  1133. else
  1134. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1135. }
  1136. else
  1137. {
  1138. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1139. }
  1140. double discount = sqlite3_column_double(stmt, 2);
  1141. int item_count = sqlite3_column_int(stmt, 3);
  1142. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  1143. NSString *nsline_note=nil;
  1144. if(line_note!=nil)
  1145. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1146. // char *name = (char*)sqlite3_column_text(stmt, 5);
  1147. // NSString *nsname = nil;
  1148. // if(name!=nil)
  1149. // nsname= [[NSString alloc]initWithUTF8String:name];
  1150. NSString *nsname = [self textAtColumn:5 statement:stmt];
  1151. // char *description = (char*)sqlite3_column_text(stmt, 6);
  1152. // NSString *nsdescription=nil;
  1153. // if(description!=nil)
  1154. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  1155. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  1156. // int stockUom = sqlite3_column_int(stmt, 8);
  1157. // int _id = sqlite3_column_int(stmt, 9);
  1158. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  1159. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1160. double weight=[bsubtotaljson[@"weight"] doubleValue];
  1161. int carton=[bsubtotaljson[@"carton"] intValue];
  1162. TotalCuft += cuft;
  1163. TotalWeight += weight;
  1164. TotalCarton += carton;
  1165. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  1166. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  1167. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  1168. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1169. itemjson[@"note"]=nsline_note;
  1170. itemjson[@"origin_price"] = Price;
  1171. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1172. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1173. itemjson[@"order_item_status"] = @""; // 暂时不处理
  1174. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  1175. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  1176. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  1177. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  1178. if(itemjson[@"combine"] != nil)
  1179. {
  1180. // int citem=0;
  1181. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  1182. for(int bc=0;bc<bcount;bc++)
  1183. {
  1184. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  1185. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  1186. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  1187. subTotal += uprice * modulus * item_count;
  1188. }
  1189. }
  1190. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  1191. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  1192. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  1193. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  1194. allItemPrice += subTotal;
  1195. }];
  1196. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  1197. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  1198. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  1199. // payments/Credits
  1200. // payments_and_credist = sqlite3_column_double(statement, 34);
  1201. payments_and_credist = allItemPrice;
  1202. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1203. // // total
  1204. // totalPrice = sqlite3_column_double(statement, 35);
  1205. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1206. } else {
  1207. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  1208. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  1209. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  1210. // payments/Credits
  1211. payments_and_credist = 0;
  1212. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1213. }
  1214. // total
  1215. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  1216. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1217. ret[@"order_info"]= orderinfo;
  1218. ret[@"more_order_info"] = moreInfo;
  1219. return [RAUtils dict2data:ret];
  1220. */
  1221. DebugLog(@"debug sales order data:%@", [RAUtils dict2string:data]);
  1222. return data;
  1223. }
  1224. +(NSData*) offline_request_salesorder :(NSMutableDictionary *) params
  1225. {
  1226. NSString* template_file=nil;
  1227. switch([params[@"thumb"] intValue])
  1228. {
  1229. case 0:
  1230. template_file= @"so.json";
  1231. break;
  1232. case 1:
  1233. template_file= @"so_thumb.json";
  1234. break;
  1235. default:
  1236. template_file= @"so.json";
  1237. }
  1238. NSMutableDictionary* data = [self prepare_salesorder_data:params[@"order_code"] type:[params[@"thumb"] intValue]];
  1239. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1240. if(data[@"table_model"][@"row2"]==nil)
  1241. template[@"pages"][@"page_0"][@"contents"][@"item_0"][@"rows"][@"count"]=[NSNumber numberWithInt:1];
  1242. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1243. DebugLog(@"%@",file);
  1244. // return nil;
  1245. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1246. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1247. if (file) {
  1248. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1249. [dic setObject:file forKey:@"pdf_path"];
  1250. dic[@"isLocalFile"]=@"true";
  1251. return [RAUtils dict2data:dic];
  1252. }
  1253. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1254. dic[@"isLocalFile"]=@"true";
  1255. return [RAUtils dict2data:dic];
  1256. }
  1257. +(NSData*) offline_request_tearsheet :(NSMutableDictionary *) params
  1258. {
  1259. NSString* template_file=nil;
  1260. switch([params[@"pdf_style"] intValue])
  1261. {
  1262. case 0:
  1263. template_file= @"portfolio_2x3.json";
  1264. break;
  1265. case 1:
  1266. template_file= @"portfolio_3x2.json";
  1267. break;
  1268. default:
  1269. template_file= @"portfolio_3x2.json";
  1270. }
  1271. NSMutableDictionary* data = [self prepare_portfolio_data:params];
  1272. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1273. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1274. DebugLog(@"%@",file);
  1275. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1276. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1277. if (file) {
  1278. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1279. [dic setObject:file forKey:@"pdf_path"];
  1280. dic[@"isLocalFile"]=@"true";
  1281. return [RAUtils dict2data:dic];
  1282. }
  1283. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1284. dic[@"isLocalFile"]=@"true";
  1285. return [RAUtils dict2data:dic];
  1286. }
  1287. +(NSString*) get_offline_soid:(sqlite3*)db
  1288. {
  1289. NSString* soid=nil;
  1290. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  1291. [formatter setDateFormat:@"yyMMdd"];
  1292. NSString* date = [formatter stringFromDate:[NSDate date]];
  1293. for(int i=1;i<999;i++)
  1294. {
  1295. soid=[NSString stringWithFormat:@"OFF%@%4d",date,i];
  1296. soid= [soid stringByReplacingOccurrencesOfString:@" " withString:@"0"];
  1297. int count=[iSalesDB get_recordcount:db table:@"offline_order" where:[NSString stringWithFormat:@"so_id='%@'",soid]];
  1298. if(count==0)
  1299. return soid;
  1300. }
  1301. return nil;
  1302. }
  1303. +(NSArray*) enumOfflineOrder
  1304. {
  1305. NSMutableArray* ret = [[NSMutableArray alloc] init];
  1306. sqlite3 *db = [iSalesDB get_db];
  1307. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  1308. sqlite3_stmt * statement;
  1309. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1310. if ( dbresult== SQLITE_OK)
  1311. {
  1312. while (sqlite3_step(statement) == SQLITE_ROW)
  1313. {
  1314. char *so_id = (char*)sqlite3_column_text(statement, 0);
  1315. NSString* nsso_id=nil;
  1316. if(so_id!=nil)
  1317. {
  1318. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  1319. }
  1320. [ret addObject:nsso_id];
  1321. }
  1322. sqlite3_finalize(statement);
  1323. }
  1324. [iSalesDB close_db:db];
  1325. return ret;
  1326. }
  1327. //+(void) uploadFile:(NSString*) file
  1328. //{
  1329. // NSData* data = [NSData dataWithContentsOfFile: file];
  1330. // UIApplication * app = [UIApplication sharedApplication];
  1331. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1332. //
  1333. //
  1334. // AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  1335. //
  1336. //
  1337. //
  1338. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  1339. //
  1340. // if(appDelegate.user!=nil)
  1341. // [params setValue:appDelegate.user forKey:@"user"];
  1342. // // if(appDelegate.contact_id!=nil)
  1343. // // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  1344. // if(appDelegate.password!=nil)
  1345. // [params setValue:appDelegate.password forKey:@"password"];
  1346. //
  1347. //
  1348. // NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_OFFLINE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  1349. // [formData appendPartWithFileData:data name:@"upfile" fileName:[file lastPathComponent] mimeType:@"application/zip"];
  1350. // } error:nil];
  1351. //
  1352. //
  1353. //
  1354. // AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  1355. //
  1356. // NSProgress *progress = nil;
  1357. //
  1358. // NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  1359. //
  1360. //
  1361. //
  1362. // if (error) {
  1363. //
  1364. // NSString* err_msg = [error localizedDescription];
  1365. // DebugLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  1366. //
  1367. //
  1368. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1369. //
  1370. // DebugLog(@"data string: %@",str);
  1371. // [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  1372. //
  1373. // } else {
  1374. // DebugLog(@"response ");
  1375. //
  1376. //
  1377. //
  1378. // NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  1379. //
  1380. // // 再将NSData转为字符串
  1381. // NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  1382. //
  1383. // // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1384. //
  1385. // DebugLog(@"data string: %@",jsonStr);
  1386. //
  1387. // NSDictionary* json = responseObject;
  1388. //
  1389. //
  1390. // if([[json valueForKey:@"result"] intValue]==2)
  1391. // {
  1392. //// NSString* img_url_down = json[@"img_url_aname"];
  1393. //// NSString* img_url_up = json[@"img_url"];
  1394. //
  1395. //
  1396. // }
  1397. // else
  1398. // {
  1399. // [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload file" controller:nil] ;
  1400. // }
  1401. //
  1402. //
  1403. // }
  1404. // // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  1405. // }];
  1406. //
  1407. //
  1408. //
  1409. //
  1410. // // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  1411. // //
  1412. //
  1413. //
  1414. //
  1415. //
  1416. //
  1417. //
  1418. // [uploadTask resume];
  1419. //
  1420. //}
  1421. +(NSDictionary *) prepareUpload:(NSArray*) arr_order
  1422. {
  1423. NSString* serial= [[NSUUID UUID] UUIDString];
  1424. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1425. NSString *cachefolder = [paths objectAtIndex:0];
  1426. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1427. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1428. NSFileManager* fileManager = [NSFileManager defaultManager];
  1429. BOOL bdir=YES;
  1430. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  1431. {
  1432. NSError *error = nil;
  1433. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  1434. if(!bsuccess)
  1435. DebugLog(@"Create UPLOAD folder failed");
  1436. }
  1437. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1438. ret[@"contact"]=[self prepareContact:serial];
  1439. ret[@"wishlist"]=[self prepareWishlist:serial];
  1440. ret[@"order"]=[self prepareOrder:serial soid:arr_order];
  1441. ret[@"portfolio"] = [self preparePortfolio:serial];
  1442. ret[@"view_portfolio"] = [self preparePDF:serial];
  1443. NSString* str= [RAUtils dict2string:ret];
  1444. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  1445. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  1446. NSError *error=nil;
  1447. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  1448. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  1449. ZipArchive* zip = [[ZipArchive alloc] init];
  1450. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  1451. for(int i=0;i<arr_files.count;i++)
  1452. {
  1453. NSString* file=arr_files[i];
  1454. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  1455. }
  1456. if( ![zip CloseZipFile2] )
  1457. {
  1458. zippath = @"";
  1459. }
  1460. ret[@"file"]=zippath;
  1461. return ret;
  1462. }
  1463. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  1464. {
  1465. if(filename.length==0)
  1466. return false;
  1467. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1468. bool ret=false;
  1469. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1470. NSString *cachefolder = [paths objectAtIndex:0];
  1471. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  1472. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  1473. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1474. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1475. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  1476. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  1477. NSFileManager* fileManager = [NSFileManager defaultManager];
  1478. BOOL bdir=NO;
  1479. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  1480. {
  1481. NSError *error = nil;
  1482. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  1483. {
  1484. ret=false;
  1485. }
  1486. else
  1487. {
  1488. ret=true;
  1489. }
  1490. }
  1491. return ret;
  1492. }
  1493. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  1494. {
  1495. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1496. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  1497. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  1498. NSString* where=@" 1=1";
  1499. if (ver!=nil) {
  1500. where=@"is_dirty=1";
  1501. }
  1502. sqlite3 *db = [iSalesDB get_db];
  1503. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  1504. sqlite3_stmt * statement;
  1505. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1506. int count=0;
  1507. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1508. if ( dbresult== SQLITE_OK)
  1509. {
  1510. while (sqlite3_step(statement) == SQLITE_ROW)
  1511. {
  1512. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1513. int _id = sqlite3_column_int(statement, 0);
  1514. int product_id = sqlite3_column_int(statement, 1);
  1515. int item_id = sqlite3_column_int(statement, 2);
  1516. int qty = sqlite3_column_int(statement, 3);
  1517. char *create_time = (char*)sqlite3_column_text(statement, 4);
  1518. NSString* nscreate_time=nil;
  1519. if(create_time!=nil)
  1520. {
  1521. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  1522. }
  1523. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  1524. NSString* nsmodify_time=nil;
  1525. if(modify_time!=nil)
  1526. {
  1527. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  1528. }
  1529. int is_delete = sqlite3_column_int(statement, 6);
  1530. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1531. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  1532. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1533. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  1534. itemjson[@"create_time"]=nscreate_time;
  1535. itemjson[@"modify_time"]=nsmodify_time;
  1536. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  1537. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1538. count++;
  1539. }
  1540. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1541. sqlite3_finalize(statement);
  1542. }
  1543. ret[@"count"]=[NSNumber numberWithInt:count ];
  1544. [iSalesDB close_db:db];
  1545. return ret;
  1546. }
  1547. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode db:(sqlite3*)db
  1548. {
  1549. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1550. UIApplication * app = [UIApplication sharedApplication];
  1551. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1552. 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 ];
  1553. // 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 ];
  1554. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  1555. DebugLog(@"offline_login sql:%@",sqlQuery);
  1556. sqlite3_stmt * statement;
  1557. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1558. int count=0;
  1559. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1560. if ( dbresult== SQLITE_OK)
  1561. {
  1562. while (sqlite3_step(statement) == SQLITE_ROW)
  1563. {
  1564. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1565. char *customer_cid = (char *)sqlite3_column_text(statement, 10);
  1566. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  1567. int product_id = sqlite3_column_int(statement, 0);
  1568. char *str_price = (char*)sqlite3_column_text(statement, 1);
  1569. int item_id = sqlite3_column_int(statement, 7);
  1570. NSString* Price=nil;
  1571. if(str_price==nil)
  1572. {
  1573. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1574. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db];
  1575. if(price==nil)
  1576. Price=@"No Price.";
  1577. else
  1578. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1579. }
  1580. else
  1581. {
  1582. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1583. }
  1584. double discount = sqlite3_column_double(statement, 2);
  1585. int item_count = sqlite3_column_int(statement, 3);
  1586. char *line_note = (char*)sqlite3_column_text(statement, 4);
  1587. NSString *nsline_note=nil;
  1588. if(line_note!=nil)
  1589. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1590. char *name = (char*)sqlite3_column_text(statement, 5);
  1591. NSString *nsname=nil;
  1592. if(name!=nil)
  1593. nsname= [[NSString alloc]initWithUTF8String:name];
  1594. char *description = (char*)sqlite3_column_text(statement, 6);
  1595. NSString *nsdescription=nil;
  1596. if(description!=nil)
  1597. nsdescription= [[NSString alloc]initWithUTF8String:description];
  1598. // int stockUom = sqlite3_column_int(statement, 8);
  1599. // int _id = sqlite3_column_int(statement, 9);
  1600. //
  1601. //
  1602. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  1603. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1604. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  1605. // int carton=[bsubtotaljson[@"carton"] intValue];
  1606. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1607. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  1608. itemjson[@"model"]=nsname;
  1609. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  1610. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  1611. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  1612. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  1613. // itemjson[@"check"]=@"true";
  1614. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1615. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  1616. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  1617. itemjson[@"unit_price"]=Price;
  1618. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  1619. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1620. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  1621. itemjson[@"note"]=nsline_note;
  1622. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:customerID];
  1623. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1624. count++;
  1625. }
  1626. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1627. sqlite3_finalize(statement);
  1628. }
  1629. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1630. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  1631. return ret;
  1632. }
  1633. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid
  1634. {
  1635. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1636. sqlite3 *db = [iSalesDB get_db];
  1637. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  1638. // for(int i=0;i<arr_soid.count;i++)
  1639. sqlite3_stmt * statement;
  1640. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1641. //int count=0;
  1642. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1643. if ( dbresult== SQLITE_OK)
  1644. {
  1645. int count=0;
  1646. while (sqlite3_step(statement) == SQLITE_ROW)
  1647. {
  1648. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1649. int _id = sqlite3_column_int(statement, 0);
  1650. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  1651. NSString* nssync_data=nil;
  1652. if(sync_data!=nil)
  1653. {
  1654. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1655. }
  1656. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1657. NSString* nsimg_1=nil;
  1658. if(img_1!=nil)
  1659. {
  1660. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1661. [self copy_upImg:serial file:nsimg_1];
  1662. }
  1663. char *so_no = (char*)sqlite3_column_text(statement, 3);
  1664. NSString* nsso_no=nil;
  1665. if(so_no!=nil)
  1666. {
  1667. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  1668. }
  1669. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  1670. itemjson[@"order_type"]=@"submit order";
  1671. else
  1672. itemjson[@"order_type"]=@"archive order";
  1673. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1674. itemjson[@"json_data"]= nssync_data;
  1675. itemjson[@"order_items"]=[self preparecart:nsso_no db:db];
  1676. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1677. count++;
  1678. }
  1679. ret[@"count"]=[NSNumber numberWithInt:count ];
  1680. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1681. }
  1682. sqlite3_finalize(statement);
  1683. [iSalesDB close_db:db];
  1684. return ret;
  1685. }
  1686. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  1687. {
  1688. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1689. sqlite3 *db = [iSalesDB get_db];
  1690. // UIApplication * app = [UIApplication sharedApplication];
  1691. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1692. 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";
  1693. sqlite3_stmt * statement;
  1694. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1695. int count=0;
  1696. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1697. if ( dbresult== SQLITE_OK)
  1698. {
  1699. while (sqlite3_step(statement) == SQLITE_ROW)
  1700. {
  1701. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1702. int _id = sqlite3_column_int(statement, 0);
  1703. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  1704. NSString* nsimg_0=nil;
  1705. if(img_0!=nil)
  1706. {
  1707. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  1708. [self copy_upImg:serial file:nsimg_0];
  1709. }
  1710. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1711. NSString* nsimg_1=nil;
  1712. if(img_1!=nil)
  1713. {
  1714. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1715. [self copy_upImg:serial file:nsimg_1];
  1716. }
  1717. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  1718. NSString* nsimg_2=nil;
  1719. if(img_2!=nil)
  1720. {
  1721. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  1722. [self copy_upImg:serial file:nsimg_2];
  1723. }
  1724. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  1725. NSString* nssync_data=nil;
  1726. if(sync_data!=nil)
  1727. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1728. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1729. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1730. itemjson[@"json_data"]= nssync_data;
  1731. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1732. count++;
  1733. }
  1734. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1735. sqlite3_finalize(statement);
  1736. }
  1737. ret[@"count"]=[NSNumber numberWithInt:count ];
  1738. [iSalesDB close_db:db];
  1739. return ret;
  1740. }
  1741. +(bool) check_offlinedata
  1742. {
  1743. UIApplication * app = [UIApplication sharedApplication];
  1744. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1745. sqlite3 *db = [iSalesDB get_db];
  1746. NSString * where=@"1=1";
  1747. // if(appDelegate.user!=nil)
  1748. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  1749. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  1750. [iSalesDB close_db:db];
  1751. if(count==0)
  1752. {
  1753. return false;
  1754. }
  1755. return true;
  1756. //
  1757. //[iSalesDB close_db:db];
  1758. }
  1759. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  1760. {
  1761. UIApplication * app = [UIApplication sharedApplication];
  1762. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1763. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1764. sqlite3 *db = [iSalesDB get_db];
  1765. NSString* collectId=params[@"collectId"];
  1766. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  1767. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  1768. [iSalesDB execSql:sqlQuery db:db];
  1769. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  1770. [iSalesDB close_db:db];
  1771. appDelegate.wish_count =count;
  1772. [appDelegate update_count_mark];
  1773. ret[@"result"]= [NSNumber numberWithInt:2];
  1774. return ret;
  1775. }
  1776. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  1777. //{
  1778. //
  1779. // UIApplication * app = [UIApplication sharedApplication];
  1780. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1781. //
  1782. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1783. // sqlite3 *db = [iSalesDB get_db];
  1784. // NSString* product_id=params[@"product_id"];
  1785. //
  1786. //
  1787. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  1788. //
  1789. //
  1790. // for(int i=0;i<arr.count;i++)
  1791. // {
  1792. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  1793. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  1794. // if(count==0)
  1795. // {
  1796. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  1797. // [iSalesDB execSql:sqlQuery db:db];
  1798. // }
  1799. // }
  1800. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  1801. // [iSalesDB close_db:db];
  1802. //
  1803. // appDelegate.wish_count =count;
  1804. //
  1805. // [appDelegate update_count_mark];
  1806. // ret[@"result"]= [NSNumber numberWithInt:2];
  1807. // return ret;
  1808. // //
  1809. // //return ret;
  1810. //}
  1811. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  1812. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  1813. DebugLog(@"time interval: %lf",interval);
  1814. }
  1815. +(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  1816. {
  1817. UIApplication * app = [UIApplication sharedApplication];
  1818. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1819. int sort = [[params objectForKey:@"sort"] intValue];
  1820. NSString *sort_str = @"";
  1821. switch (sort) {
  1822. case 0:{
  1823. sort_str = @"order by w.modify_time desc";
  1824. }
  1825. break;
  1826. case 1:{
  1827. sort_str = @"order by w.modify_time asc";
  1828. }
  1829. break;
  1830. case 2:{
  1831. sort_str = @"order by m.name asc";
  1832. }
  1833. break;
  1834. case 3:{
  1835. sort_str = @"order by m.name desc";
  1836. }
  1837. break;
  1838. case 4:{
  1839. sort_str = @"order by m.description asc";
  1840. }
  1841. break;
  1842. default:
  1843. break;
  1844. }
  1845. NSString* user = appDelegate.user;
  1846. sqlite3 *db = [iSalesDB get_db];
  1847. // order by w.create_time
  1848. 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];
  1849. // 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];
  1850. sqlite3_stmt * statement;
  1851. NSDate *date1 = [NSDate date];
  1852. NSDate *date2 = nil;
  1853. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1854. int count=0;
  1855. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1856. {
  1857. while (sqlite3_step(statement) == SQLITE_ROW)
  1858. {
  1859. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1860. int product_id = sqlite3_column_double(statement, 0);
  1861. char *description = (char*)sqlite3_column_text(statement, 1);
  1862. if(description==nil)
  1863. description= "";
  1864. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  1865. int item_id = sqlite3_column_double(statement, 2);
  1866. NSDate *date_image = [NSDate date];
  1867. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  1868. printf("image : ");
  1869. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  1870. // char *url = (char*)sqlite3_column_text(statement, 3);
  1871. // if(url==nil)
  1872. // url="";
  1873. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1874. int qty = sqlite3_column_int(statement, 3);
  1875. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  1876. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  1877. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  1878. item[@"description"]= nsdescription;
  1879. item[@"img"]= nsurl;
  1880. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  1881. count++;
  1882. }
  1883. printf("total time:");
  1884. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  1885. ret[@"count"]= [NSNumber numberWithInt:count];
  1886. ret[@"total_count"]= [NSNumber numberWithInt:count];
  1887. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  1888. ret[@"result"]= [NSNumber numberWithInt:2];
  1889. appDelegate.wish_count =count;
  1890. [appDelegate update_count_mark];
  1891. sqlite3_finalize(statement);
  1892. }
  1893. [iSalesDB close_db:db];
  1894. return ret;
  1895. }
  1896. +(NSDictionary*) offline_notimpl
  1897. {
  1898. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1899. ret[@"result"]=@"8";
  1900. ret[@"err_msg"]=@"offline mode does not support this function.";
  1901. return ret;
  1902. }
  1903. +(NSDictionary*) offline_home
  1904. {
  1905. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1906. NSString *cachefolder = [paths objectAtIndex:0];
  1907. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  1908. NSData* json =nil;
  1909. json=[NSData dataWithContentsOfFile:img_cache];
  1910. if(json==nil)
  1911. return nil;
  1912. NSError *error=nil;
  1913. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1914. return menu;
  1915. }
  1916. +(NSDictionary*) offline_category_menu
  1917. {
  1918. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1919. NSString *cachefolder = [paths objectAtIndex:0];
  1920. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  1921. NSData* json =nil;
  1922. json=[NSData dataWithContentsOfFile:img_cache];
  1923. if(json==nil)
  1924. return nil;
  1925. NSError *error=nil;
  1926. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1927. return menu;
  1928. }
  1929. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  1930. {
  1931. NSString* offline_command=params[@"offline_Command"];
  1932. NSDictionary* ret=nil;
  1933. if([offline_command isEqualToString:@"model_NIYMAL"])
  1934. {
  1935. NSString* category = params[@"category"];
  1936. ret = [self refresh_model_NIYMAL:category];
  1937. }
  1938. return ret;
  1939. }
  1940. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  1941. {
  1942. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1943. [ret setValue:@"2" forKey:@"result"];
  1944. [ret setValue:@"Regular Mode" forKey:@"mode"];
  1945. sqlite3* db= [iSalesDB get_db];
  1946. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  1947. [iSalesDB close_db:db];
  1948. [ret setObject:detail1_section forKey:@"detail_1"];
  1949. return ret;
  1950. }
  1951. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id db:(sqlite3*)db
  1952. {
  1953. UIApplication * app = [UIApplication sharedApplication];
  1954. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1955. // NSArray* arr1 = [self get_user_all_price_type];
  1956. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  1957. // NSSet *set1 = [NSSet setWithArray:arr1];
  1958. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  1959. // if(appDelegate.contact_id==nil)
  1960. // set2=[set1 mutableCopy];
  1961. // else
  1962. // [set2 intersectsSet:set1];
  1963. // NSArray *retarr = [set2 allObjects];
  1964. NSString* whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  1965. // sqlite3 *db = [iSalesDB get_db];
  1966. NSString* sqlQuery = nil;
  1967. if(appDelegate.contact_id==nil)
  1968. {
  1969. if(!appDelegate.bLogin)
  1970. return nil;
  1971. 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];
  1972. }
  1973. else
  1974. 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];
  1975. sqlite3_stmt * statement;
  1976. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1977. int count=0;
  1978. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1979. {
  1980. while (sqlite3_step(statement) == SQLITE_ROW)
  1981. {
  1982. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1983. char *name = (char*)sqlite3_column_text(statement, 0);
  1984. if(name==nil)
  1985. name="";
  1986. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1987. // double price = sqlite3_column_double(statement, 1);
  1988. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  1989. // if(isnull==nil)
  1990. // item[nsname]= @"No Price";
  1991. // else
  1992. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  1993. char *price = (char*)sqlite3_column_text(statement, 1);
  1994. if(price!=nil)
  1995. {
  1996. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  1997. nsprice=[AESCrypt fastdecrypt:nsprice];
  1998. if(nsprice.length>0)
  1999. {
  2000. double dp= [nsprice doubleValue];
  2001. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  2002. }
  2003. }
  2004. else
  2005. {
  2006. item[nsname]= @"No Price";
  2007. }
  2008. // int type= sqlite3_column_int(statement, 2);
  2009. //item[@"type"]=@"price";
  2010. // item[nsname]= nsprice;
  2011. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2012. count++;
  2013. }
  2014. ret[@"count"]= [NSNumber numberWithInt:count];
  2015. sqlite3_finalize(statement);
  2016. }
  2017. // [iSalesDB close_db:db];
  2018. return ret;
  2019. }
  2020. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  2021. //{
  2022. // NSArray* arr1 = [self get_user_all_price_type:db];
  2023. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2024. //
  2025. // // NSSet *set1 = [NSSet setWithArray:arr1];
  2026. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2027. // // [set2 intersectsSet:set1];
  2028. // //
  2029. // //
  2030. // // NSArray *retarr = [set2 allObjects];
  2031. //
  2032. // NSString* whereprice=nil;
  2033. // if(contact_id==nil)
  2034. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2035. // else
  2036. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2037. //
  2038. //
  2039. // // sqlite3 *db = [iSalesDB get_db];
  2040. //
  2041. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  2042. // sqlite3_stmt * statement;
  2043. //
  2044. //
  2045. // NSNumber* ret = nil;
  2046. // double dprice=DBL_MAX;
  2047. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2048. // {
  2049. //
  2050. //
  2051. // while (sqlite3_step(statement) == SQLITE_ROW)
  2052. // {
  2053. //
  2054. // // double val = sqlite3_column_double(statement, 0);
  2055. // char *price = (char*)sqlite3_column_text(statement, 0);
  2056. // if(price!=nil)
  2057. // {
  2058. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2059. // nsprice=[AESCrypt fastdecrypt:nsprice];
  2060. // if(nsprice.length>0)
  2061. // {
  2062. // double dp= [nsprice doubleValue];
  2063. // if(dp<dprice)
  2064. // dprice=dp;
  2065. // }
  2066. // }
  2067. // }
  2068. //
  2069. //
  2070. //
  2071. //
  2072. // sqlite3_finalize(statement);
  2073. //
  2074. //
  2075. //
  2076. //
  2077. // }
  2078. //
  2079. // // [iSalesDB close_db:db];
  2080. //
  2081. // if(dprice==DBL_MAX)
  2082. // ret= nil;
  2083. // else
  2084. // ret= [NSNumber numberWithDouble:dprice];
  2085. // return ret;
  2086. //}
  2087. +(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  2088. {
  2089. NSArray* arr1 = [self get_user_all_price_type:db];
  2090. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2091. NSString* whereprice=nil;
  2092. if(contact_id==nil)
  2093. whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2094. else
  2095. whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2096. // sqlite3 *db = [iSalesDB get_db];
  2097. NSString *productIdCondition = @"1 = 1";
  2098. if (product_id) {
  2099. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  2100. }
  2101. NSString *itemIdCondition = @"";
  2102. if (item_id) {
  2103. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  2104. }
  2105. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  2106. sqlite3_stmt * statement;
  2107. NSNumber* ret = nil;
  2108. double dprice=DBL_MAX;
  2109. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2110. {
  2111. while (sqlite3_step(statement) == SQLITE_ROW)
  2112. {
  2113. // double val = sqlite3_column_double(statement, 0);
  2114. char *price = (char*)sqlite3_column_text(statement, 0);
  2115. if(price!=nil)
  2116. {
  2117. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2118. nsprice=[AESCrypt fastdecrypt:nsprice];
  2119. if(nsprice.length>0)
  2120. {
  2121. double dp= [nsprice doubleValue];
  2122. if(dp<dprice)
  2123. dprice=dp;
  2124. }
  2125. }
  2126. }
  2127. sqlite3_finalize(statement);
  2128. }
  2129. // [iSalesDB close_db:db];
  2130. if(dprice==DBL_MAX)
  2131. ret= nil;
  2132. else
  2133. ret= [NSNumber numberWithDouble:dprice];
  2134. return ret;
  2135. }
  2136. +(NSArray*) get_user_all_price_type:(sqlite3*)db
  2137. {
  2138. NSArray* ret=nil;
  2139. // sqlite3 *db = [iSalesDB get_db];
  2140. // no customer assigned , use login user contact_id
  2141. UIApplication * app = [UIApplication sharedApplication];
  2142. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2143. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",appDelegate.user];
  2144. sqlite3_stmt * statement;
  2145. // int count=0;
  2146. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2147. {
  2148. if (sqlite3_step(statement) == SQLITE_ROW)
  2149. {
  2150. char *val = (char*)sqlite3_column_text(statement, 0);
  2151. if(val==nil)
  2152. val="";
  2153. NSString* price = [[NSString alloc]initWithUTF8String:val];
  2154. ret=[RAUtils string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  2155. }
  2156. sqlite3_finalize(statement);
  2157. }
  2158. // [iSalesDB close_db:db];
  2159. return ret;
  2160. }
  2161. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id db:(sqlite3*)db
  2162. {
  2163. // sqlite3 *db = [iSalesDB get_db];
  2164. if(contact_id==nil)
  2165. {
  2166. // no customer assigned , use login user contact_id
  2167. UIApplication * app = [UIApplication sharedApplication];
  2168. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2169. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",appDelegate.user];
  2170. sqlite3_stmt * statement;
  2171. // int count=0;
  2172. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2173. {
  2174. if (sqlite3_step(statement) == SQLITE_ROW)
  2175. {
  2176. char *val = (char*)sqlite3_column_text(statement, 0);
  2177. if(val==nil)
  2178. val="";
  2179. contact_id = [[NSString alloc]initWithUTF8String:val];
  2180. }
  2181. sqlite3_finalize(statement);
  2182. }
  2183. if(contact_id.length<=0)
  2184. {
  2185. // [iSalesDB close_db:db];
  2186. return nil;
  2187. }
  2188. }
  2189. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  2190. sqlite3_stmt * statement;
  2191. NSArray* ret=nil;
  2192. // int count=0;
  2193. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2194. {
  2195. if (sqlite3_step(statement) == SQLITE_ROW)
  2196. {
  2197. char *price_type = (char*)sqlite3_column_text(statement, 0);
  2198. if(price_type==nil)
  2199. price_type="";
  2200. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  2201. if(nsprice_type.length>0)
  2202. ret=[RAUtils string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  2203. else
  2204. ret=nil;
  2205. }
  2206. sqlite3_finalize(statement);
  2207. }
  2208. // [iSalesDB close_db:db];
  2209. return ret;
  2210. }
  2211. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  2212. {
  2213. NSString* ret= nil;
  2214. // sqlite3 *db = [iSalesDB get_db];
  2215. NSString *sqlQuery = nil;
  2216. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  2217. // 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;
  2218. // 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
  2219. if(product_id==nil && model_name)
  2220. 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;
  2221. else if (product_id)
  2222. 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
  2223. sqlite3_stmt * statement;
  2224. // int count=0;
  2225. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2226. {
  2227. if (sqlite3_step(statement) == SQLITE_ROW)
  2228. {
  2229. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  2230. if(imgurl==nil)
  2231. imgurl="";
  2232. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  2233. ret=nsimgurl;
  2234. }
  2235. sqlite3_finalize(statement);
  2236. }
  2237. else
  2238. {
  2239. [ret setValue:@"8" forKey:@"result"];
  2240. }
  2241. // [iSalesDB close_db:db];
  2242. DebugLog(@"data string: %@",ret );
  2243. return ret;
  2244. }
  2245. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name
  2246. {
  2247. NSString* ret= nil;
  2248. sqlite3 *db = [iSalesDB get_db];
  2249. NSString *sqlQuery = nil;
  2250. if(product_id==nil)
  2251. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  2252. else
  2253. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  2254. sqlite3_stmt * statement;
  2255. // int count=0;
  2256. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2257. {
  2258. if (sqlite3_step(statement) == SQLITE_ROW)
  2259. {
  2260. char *default_category = (char*)sqlite3_column_text(statement, 0);
  2261. if(default_category==nil)
  2262. default_category="";
  2263. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2264. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  2265. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  2266. ret=nsdefault_category;
  2267. }
  2268. sqlite3_finalize(statement);
  2269. }
  2270. else
  2271. {
  2272. [ret setValue:@"8" forKey:@"result"];
  2273. }
  2274. [iSalesDB close_db:db];
  2275. DebugLog(@"data string: %@",ret );
  2276. return ret;
  2277. }
  2278. +(NSDictionary*) offline_model :(NSMutableDictionary *) params
  2279. {
  2280. NSString* model_name = [params valueForKey:@"product_name"];
  2281. NSString* product_id = [params valueForKey:@"product_id"];
  2282. NSString* category = [params valueForKey:@"category"];
  2283. if(category==nil)
  2284. category = [self model_default_category:product_id model_name:model_name];
  2285. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2286. sqlite3 *db = [iSalesDB get_db];
  2287. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  2288. NSString *sqlQuery = nil;
  2289. if(product_id==nil)
  2290. sqlQuery = [NSString stringWithFormat:@"select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging,ETA,item_id from product where name='%@';",model_name]; // select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging from model where name='%@';
  2291. else
  2292. sqlQuery = [NSString stringWithFormat:@"select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging,ETA,item_id from product where product_id=%@;",product_id]; // select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging from model where product_id=%@;
  2293. sqlite3_stmt * statement;
  2294. [ret setValue:@"2" forKey:@"result"];
  2295. [ret setValue:@"3" forKey:@"detail_section_count"];
  2296. // int count=0;
  2297. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2298. {
  2299. if (sqlite3_step(statement) == SQLITE_ROW)
  2300. {
  2301. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2302. char *name = (char*)sqlite3_column_text(statement, 0);
  2303. if(name==nil)
  2304. name="";
  2305. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2306. char *description = (char*)sqlite3_column_text(statement, 1);
  2307. if(description==nil)
  2308. description="";
  2309. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2310. int product_id = sqlite3_column_int(statement, 2);
  2311. char *color = (char*)sqlite3_column_text(statement, 3);
  2312. if(color==nil)
  2313. color="";
  2314. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  2315. //
  2316. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  2317. // if(legcolor==nil)
  2318. // legcolor="";
  2319. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  2320. //
  2321. //
  2322. int availability = sqlite3_column_int(statement, 5);
  2323. //
  2324. int incoming_stock = sqlite3_column_int(statement, 6);
  2325. char *demension = (char*)sqlite3_column_text(statement, 7);
  2326. if(demension==nil)
  2327. demension="";
  2328. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  2329. // ,,,,,,,,,
  2330. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  2331. if(seat_height==nil)
  2332. seat_height="";
  2333. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  2334. char *material = (char*)sqlite3_column_text(statement, 9);
  2335. if(material==nil)
  2336. material="";
  2337. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  2338. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  2339. if(box_dim==nil)
  2340. box_dim="";
  2341. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  2342. char *volume = (char*)sqlite3_column_text(statement, 11);
  2343. if(volume==nil)
  2344. volume="";
  2345. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  2346. double weight = sqlite3_column_double(statement, 12);
  2347. char *model_set = (char*)sqlite3_column_text(statement, 13);
  2348. if(model_set==nil)
  2349. model_set="";
  2350. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  2351. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  2352. if(load_ability==nil)
  2353. load_ability="";
  2354. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  2355. char *default_category = (char*)sqlite3_column_text(statement, 15);
  2356. if(default_category==nil)
  2357. default_category="";
  2358. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2359. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  2360. if(fabric_content==nil)
  2361. fabric_content="";
  2362. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  2363. char *assembling = (char*)sqlite3_column_text(statement, 17);
  2364. if(assembling==nil)
  2365. assembling="";
  2366. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  2367. char *made_in = (char*)sqlite3_column_text(statement, 18);
  2368. if(made_in==nil)
  2369. made_in="";
  2370. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  2371. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  2372. if(special_remarks==nil)
  2373. special_remarks="";
  2374. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  2375. int stockUcom = sqlite3_column_double(statement, 20);
  2376. char *product_group = (char*)sqlite3_column_text(statement, 21);
  2377. if(product_group==nil)
  2378. product_group="";
  2379. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  2380. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  2381. // if(fashion_selector==nil)
  2382. // fashion_selector="";
  2383. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  2384. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  2385. if(selector_field==nil)
  2386. selector_field="";
  2387. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  2388. char *property_field = (char*)sqlite3_column_text(statement, 23);
  2389. if(property_field==nil)
  2390. property_field="";
  2391. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  2392. char *packaging = (char*)sqlite3_column_text(statement, 24);
  2393. if(packaging==nil)
  2394. packaging="";
  2395. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  2396. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2397. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  2398. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  2399. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  2400. [img_section setValue:model_s_img forKey:@"model_s_img"];
  2401. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  2402. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  2403. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  2404. NSString* Availability=nil;
  2405. if(availability>0)
  2406. Availability=[NSString stringWithFormat:@"%d",availability];
  2407. else
  2408. Availability = @"In Production";
  2409. [img_section setValue:Availability forKey:@"Availability"];
  2410. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  2411. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  2412. char *eta = (char*)sqlite3_column_text(statement, 25);
  2413. if(eta==nil)
  2414. eta="";
  2415. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  2416. if ([nseta isEqualToString:@"null"]) {
  2417. nseta = @"";
  2418. }
  2419. if (availability <= 0) {
  2420. [img_section setValue:nseta forKey:@"ETA"];
  2421. }
  2422. int item_id = sqlite3_column_int(statement, 26);
  2423. NSString* Price=nil;
  2424. if(appDelegate.bLogin==false)
  2425. Price=@"Must Sign in.";
  2426. else
  2427. {
  2428. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2429. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  2430. if(price==nil)
  2431. Price=@"No Price.";
  2432. else
  2433. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2434. }
  2435. [img_section setValue:Price forKey:@"price"];
  2436. [img_section setValue:nsname forKey:@"model_name"];
  2437. [img_section setValue:nsdescription forKey:@"model_descrition"];
  2438. if (appDelegate.order_code) { // 离线order code即so#
  2439. 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];
  2440. __block int cartQTY = 0;
  2441. [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2442. cartQTY = sqlite3_column_int(stmt, 0);
  2443. }];
  2444. if (cartQTY > 0) {
  2445. [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  2446. }
  2447. }
  2448. [ret setObject:img_section forKey:@"img_section"];
  2449. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  2450. int detail0_item_count=0;
  2451. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2452. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2453. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2454. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2455. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2456. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2457. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2458. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2459. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2460. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2461. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2462. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2463. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2464. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  2465. NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  2466. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  2467. {
  2468. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  2469. [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2470. }
  2471. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  2472. [detail0_section setValue:@"kv" forKey:@"type"];
  2473. [detail0_section setValue:@"Product Information" forKey:@"title"];
  2474. [ret setObject:detail0_section forKey:@"detail_0"];
  2475. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  2476. // [detail1_section setValue:@"detail" forKey:@"target"];
  2477. // [detail1_section setValue:@"popup" forKey:@"action"];
  2478. // [detail1_section setValue:@"content" forKey:@"type"];
  2479. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  2480. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  2481. // [detail1_section setValue:@"true" forKey:@"single_row"];
  2482. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  2483. [ret setObject:detail1_section forKey:@"detail_1"];
  2484. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  2485. [detail2_section setValue:@"detail" forKey:@"target"];
  2486. [detail2_section setValue:@"popup" forKey:@"action"];
  2487. [detail2_section setValue:@"content" forKey:@"type"];
  2488. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  2489. [detail2_section setValue:@"true" forKey:@"single_row"];
  2490. [detail2_section setValue:@"local" forKey:@"data"];
  2491. [ret setObject:detail2_section forKey:@"detail_2"];
  2492. }
  2493. sqlite3_finalize(statement);
  2494. }
  2495. else
  2496. {
  2497. [ret setValue:@"8" forKey:@"result"];
  2498. }
  2499. DebugLog(@"count:%d",count);
  2500. [iSalesDB close_db:db];
  2501. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2502. return ret;
  2503. }
  2504. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  2505. NSString* orderCode = [params valueForKey:@"orderCode"];
  2506. NSString* keyword = [params valueForKey:@"keyword"];
  2507. keyword=keyword.lowercaseString;
  2508. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue];
  2509. int limit = [[params valueForKey:@"limit"] intValue];
  2510. int offset = [[params valueForKey:@"offset"] intValue];
  2511. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2512. NSString *limit_str = @"";
  2513. if (limited) {
  2514. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  2515. }
  2516. sqlite3 *db = [iSalesDB get_db];
  2517. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  2518. UIApplication * app = [UIApplication sharedApplication];
  2519. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2520. NSString *sqlQuery = nil;
  2521. if(exactMatch )
  2522. 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 ;
  2523. else
  2524. 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'%%%@%%') and m.is_active = 1 order by m.name %@ ;",appDelegate.order_code, keyword,keyword,limit_str];// select m.name,m.description,m.product_id,w._id,m.closeout from model m left join wishlist w on m.product_id=w.product_id where lower(name) like'%%%@%%' or
  2525. DebugLog(@"offline_search sql:%@",sqlQuery);
  2526. sqlite3_stmt * statement;
  2527. [ret setValue:@"2" forKey:@"result"];
  2528. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  2529. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  2530. // int count=0;
  2531. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2532. {
  2533. int i=0;
  2534. while (sqlite3_step(statement) == SQLITE_ROW)
  2535. {
  2536. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2537. // char *name = (char*)sqlite3_column_text(statement, 1);
  2538. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2539. char *name = (char*)sqlite3_column_text(statement, 0);
  2540. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2541. char *description = (char*)sqlite3_column_text(statement, 1);
  2542. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2543. int product_id = sqlite3_column_int(statement, 2);
  2544. // char *url = (char*)sqlite3_column_text(statement, 3);
  2545. // if(url==nil)
  2546. // url="";
  2547. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2548. int wid = sqlite3_column_int(statement, 3);
  2549. int closeout = sqlite3_column_int(statement, 4);
  2550. int cid = sqlite3_column_int(statement, 5);
  2551. int wisdelete = sqlite3_column_int(statement, 6);
  2552. int more_color = sqlite3_column_int(statement, 7);
  2553. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  2554. if(wid !=0 && wisdelete != 1)
  2555. [item setValue:@"true" forKey:@"wish_exists"];
  2556. else
  2557. [item setValue:@"false" forKey:@"wish_exists"];
  2558. if(closeout==0)
  2559. [item setValue:@"false" forKey:@"is_closeout"];
  2560. else
  2561. [item setValue:@"true" forKey:@"is_closeout"];
  2562. if(cid==0)
  2563. [item setValue:@"false" forKey:@"cart_exists"];
  2564. else
  2565. [item setValue:@"true" forKey:@"cart_exists"];
  2566. if (more_color == 0) {
  2567. [item setObject:@(false) forKey:@"more_color"];
  2568. } else if (more_color == 1) {
  2569. [item setObject:@(true) forKey:@"more_color"];
  2570. }
  2571. [item addEntriesFromDictionary:imgjson];
  2572. // [item setValue:nsurl forKey:@"img"];
  2573. [item setValue:nsname forKey:@"fash_name"];
  2574. [item setValue:nsdescription forKey:@"description"];
  2575. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2576. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2577. i++;
  2578. }
  2579. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2580. [ret setObject:items forKey:@"items"];
  2581. sqlite3_finalize(statement);
  2582. }
  2583. DebugLog(@"count:%d",count);
  2584. [iSalesDB close_db:db];
  2585. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2586. return ret;
  2587. }
  2588. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  2589. {
  2590. return [self search:params limited:YES];
  2591. }
  2592. +(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  2593. {
  2594. UIApplication * app = [UIApplication sharedApplication];
  2595. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2596. appDelegate.disable_trigger=true;
  2597. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  2598. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2599. appDelegate.disable_trigger=false;
  2600. //
  2601. // NSString* user = [params valueForKey:@"user"];
  2602. //
  2603. // NSString* password = [params valueForKey:@"password"];
  2604. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2605. ret[@"result"]=[NSNumber numberWithInt:2 ];
  2606. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  2607. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  2608. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  2609. [appDelegate update_count_mark];
  2610. appDelegate.can_show_price =false;
  2611. appDelegate.can_see_price =false;
  2612. appDelegate.can_create_portfolio =false;
  2613. appDelegate.can_create_order =false;
  2614. appDelegate.can_cancel_order =false;
  2615. appDelegate.can_set_cart_price =false;
  2616. appDelegate.can_delete_order =false;
  2617. appDelegate.can_submit_order =false;
  2618. appDelegate.can_set_tearsheet_price =false;
  2619. appDelegate.can_update_contact_info = false;
  2620. appDelegate.save_order_logout = false;
  2621. appDelegate.submit_order_logout = false;
  2622. appDelegate.alert_sold_in_quantities = false;
  2623. appDelegate.ipad_perm =nil ;
  2624. appDelegate.user_type = USER_ROLE_UNKNOWN;
  2625. appDelegate.OrderFilter= nil;
  2626. [appDelegate SetSo:nil];
  2627. [appDelegate set_main_button_panel];
  2628. // sqlite3 *db = [iSalesDB get_db];
  2629. //
  2630. //
  2631. //
  2632. //
  2633. //
  2634. // 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"]];
  2635. //
  2636. //
  2637. //
  2638. //
  2639. // DebugLog(@"offline_login sql:%@",sqlQuery);
  2640. // sqlite3_stmt * statement;
  2641. //
  2642. //
  2643. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2644. //
  2645. //
  2646. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2647. // {
  2648. //
  2649. //
  2650. // if (sqlite3_step(statement) == SQLITE_ROW)
  2651. // {
  2652. //
  2653. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2654. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  2655. //
  2656. //
  2657. //
  2658. // int can_show_price = sqlite3_column_int(statement, 0);
  2659. // int can_see_price = sqlite3_column_int(statement, 1);
  2660. //
  2661. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  2662. // if(contact_id==nil)
  2663. // contact_id="";
  2664. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2665. //
  2666. // int user_type = sqlite3_column_int(statement, 3);
  2667. //
  2668. // int can_cancel_order = sqlite3_column_int(statement, 4);
  2669. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  2670. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  2671. // int can_delete_order = sqlite3_column_int(statement, 7);
  2672. // int can_submit_order = sqlite3_column_int(statement, 8);
  2673. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  2674. // int can_create_order = sqlite3_column_int(statement, 10);
  2675. //
  2676. //
  2677. // char *mode = (char*)sqlite3_column_text(statement, 11);
  2678. // if(mode==nil)
  2679. // mode="";
  2680. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  2681. //
  2682. //
  2683. // char *username = (char*)sqlite3_column_text(statement, 12);
  2684. // if(username==nil)
  2685. // username="";
  2686. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  2687. //
  2688. //
  2689. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  2690. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  2691. // [header setValue:nscontact_id forKey:@"contact_id"];
  2692. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  2693. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  2694. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  2695. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  2696. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  2697. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  2698. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  2699. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  2700. //
  2701. // [header setValue:nsusername forKey:@"username"];
  2702. //
  2703. //
  2704. // [ret setObject:header forKey:@"header"];
  2705. // [ret setValue:nsmode forKey:@"mode"];
  2706. //
  2707. //
  2708. // }
  2709. //
  2710. //
  2711. //
  2712. // sqlite3_finalize(statement);
  2713. // }
  2714. //
  2715. //
  2716. //
  2717. // [iSalesDB close_db:db];
  2718. //
  2719. //
  2720. //
  2721. //
  2722. // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2723. return ret;
  2724. }
  2725. +(NSData*) offline_createorder :(NSMutableDictionary *) params
  2726. {
  2727. UIApplication * app = [UIApplication sharedApplication];
  2728. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2729. appDelegate.disable_trigger=true;
  2730. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  2731. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2732. appDelegate.disable_trigger=false;
  2733. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2734. NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  2735. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  2736. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  2737. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  2738. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  2739. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  2740. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  2741. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  2742. NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  2743. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  2744. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  2745. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  2746. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  2747. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  2748. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  2749. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  2750. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  2751. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  2752. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  2753. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  2754. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  2755. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  2756. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  2757. [arr_name addObject:customer_first_name];
  2758. [arr_name addObject:customer_last_name];
  2759. NSString *contact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  2760. // default ship from
  2761. 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';";
  2762. __block NSString *cid = @"";
  2763. __block NSString *name = @"";
  2764. __block NSString *ext = @"";
  2765. __block NSString *contact = @"";
  2766. __block NSString *email = @"";
  2767. __block NSString *fax = @"";
  2768. __block NSString *phone = @"";
  2769. sqlite3 *db = [iSalesDB get_db];
  2770. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  2771. cid = [self textAtColumn:0 statement:statment];
  2772. name = [self textAtColumn:1 statement:statment];
  2773. ext = [self textAtColumn:2 statement:statment];
  2774. contact = [self textAtColumn:3 statement:statment];
  2775. email = [self textAtColumn:4 statement:statment];
  2776. fax = [self textAtColumn:5 statement:statment];
  2777. phone = [self textAtColumn:6 statement:statment];
  2778. }];
  2779. NSString* so_id = [self get_offline_soid:db];
  2780. if(so_id==nil)
  2781. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  2782. 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];
  2783. int result =[iSalesDB execSql:sql_neworder db:db];
  2784. [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  2785. //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'
  2786. //soId
  2787. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  2788. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  2789. sqlite3_stmt * statement;
  2790. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2791. {
  2792. if (sqlite3_step(statement) == SQLITE_ROW)
  2793. {
  2794. // char *name = (char*)sqlite3_column_text(statement, 1);
  2795. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2796. //ret = sqlite3_column_int(statement, 0);
  2797. char *soId = (char*)sqlite3_column_text(statement, 0);
  2798. if(soId==nil)
  2799. soId="";
  2800. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  2801. [ret setValue:nssoId forKey:@"soId"];
  2802. [ret setValue:nssoId forKey:@"orderCode"];
  2803. }
  2804. sqlite3_finalize(statement);
  2805. }
  2806. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  2807. [ret setValue:@"Regular Mode" forKey:@"mode"];
  2808. [iSalesDB close_db:db];
  2809. return [RAUtils dict2data:ret];
  2810. }
  2811. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  2812. {
  2813. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2814. NSString* orderCode = [params valueForKey:@"orderCode"];
  2815. UIApplication * app = [UIApplication sharedApplication];
  2816. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2817. if(![appDelegate.order_code isEqualToString:orderCode]&& appDelegate.order_code.length!=0)
  2818. {
  2819. appDelegate.disable_trigger=true;
  2820. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  2821. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2822. appDelegate.disable_trigger=false;
  2823. }
  2824. sqlite3 *db = [iSalesDB get_db];
  2825. int cart_count=[self query_ordercartcount:orderCode db:db];
  2826. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2827. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2828. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2829. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2830. [iSalesDB close_db:db];
  2831. return [RAUtils dict2data:ret];
  2832. }
  2833. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  2834. {
  2835. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2836. sqlite3 *db = [iSalesDB get_db];
  2837. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  2838. int count =0;
  2839. if(params[@"count"]!=nil)
  2840. {
  2841. count = [params[@"count"] intValue];
  2842. }
  2843. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  2844. NSString* product_id=params[@"product_id"];
  2845. NSString* orderCode=params[@"orderCode"];
  2846. NSString *qty = params[@"qty"];
  2847. NSArray* arr_id=[RAUtils string2arr:product_id separator:@","];
  2848. NSArray *qty_arr = [RAUtils string2arr:qty separator:@","];
  2849. __block int number_of_outOfStock = 0;
  2850. for(int i=0;i<arr_id.count;i++)
  2851. {
  2852. NSInteger item_qty= count;
  2853. if (qty) {
  2854. item_qty = [qty_arr[i] integerValue];
  2855. }
  2856. if(item_qty==0)
  2857. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  2858. // 检查新加Model数量是否大于库存
  2859. if (!appDelegate.can_create_backorder) {
  2860. __block BOOL needContinue = NO;
  2861. [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) {
  2862. int availability = sqlite3_column_int(stmt, 0);
  2863. // 库存小于购买量为缺货
  2864. if (availability < item_qty || availability <= 0) {
  2865. number_of_outOfStock++;
  2866. needContinue = YES;
  2867. }
  2868. }];
  2869. if (needContinue) {
  2870. continue;
  2871. }
  2872. }
  2873. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  2874. NSString* sql=nil;
  2875. sqlite3_stmt *stmt = nil;
  2876. BOOL shouldStep = NO;
  2877. if(_id<0)
  2878. {
  2879. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  2880. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  2881. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2882. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],(int)[arr_id[i] length],NULL);
  2883. sqlite3_bind_text(stmt,2,[orderCode UTF8String],(int)[orderCode length],NULL);
  2884. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  2885. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  2886. shouldStep = YES;
  2887. }
  2888. else
  2889. {
  2890. if (qty) { // wish list move to cart
  2891. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  2892. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2893. sqlite3_bind_int(stmt, 1, _id);
  2894. shouldStep = YES;
  2895. } else {
  2896. 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];
  2897. __block BOOL update = YES;
  2898. if (!appDelegate.can_create_backorder) {
  2899. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2900. int newQTY = sqlite3_column_int(stmt, 0);
  2901. int availability = sqlite3_column_int(stmt, 1);
  2902. if (newQTY > availability) { // 库存不够
  2903. update = NO;
  2904. number_of_outOfStock++;
  2905. }
  2906. }];
  2907. }
  2908. if (update) {
  2909. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  2910. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2911. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  2912. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",_id] length],NULL);
  2913. shouldStep = YES;
  2914. }
  2915. }
  2916. }
  2917. if (shouldStep) {
  2918. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  2919. [iSalesDB execSql:@"ROLLBACK" db:db];
  2920. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  2921. [iSalesDB close_db:db];
  2922. DebugLog(@"add to cart error");
  2923. return [RAUtils dict2data:ret];
  2924. }
  2925. }
  2926. }
  2927. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2928. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2929. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2930. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2931. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2932. if (!appDelegate.can_create_backorder) {
  2933. if (number_of_outOfStock > 0) {
  2934. ret[@"result"]=[NSNumber numberWithInt:8];
  2935. ret[@"err_msg"] = @"Out of Stock.\nNo more quantity available.\nPlease try again after sync.";
  2936. /*[NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock]*/
  2937. }
  2938. }
  2939. [iSalesDB execSql:@"END TRANSACTION" db:db];
  2940. [iSalesDB close_db:db];
  2941. return [RAUtils dict2data:ret];
  2942. }
  2943. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute contactID:(NSString *)contactID
  2944. {
  2945. // UIApplication * app = [UIApplication sharedApplication];
  2946. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2947. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  2948. 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];
  2949. sqlite3_stmt * statement;
  2950. int count=0;
  2951. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2952. {
  2953. while (sqlite3_step(statement) == SQLITE_ROW)
  2954. {
  2955. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  2956. int bitem_id = sqlite3_column_int(statement, 0);
  2957. int bitem_qty = sqlite3_column_int(statement, 1);
  2958. char *name = (char*)sqlite3_column_text(statement, 2);
  2959. if(name==nil)
  2960. name="";
  2961. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  2962. char *description = (char*)sqlite3_column_text(statement, 3);
  2963. if(description==nil)
  2964. description="";
  2965. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  2966. double unit_price = sqlite3_column_double(statement, 4);
  2967. int use_unitprice = sqlite3_column_int(statement, 5);
  2968. if(use_unitprice!=1)
  2969. {
  2970. unit_price=[self get_model_default_price:contactID product_id:nil item_id:@(bitem_id) db:db].doubleValue;
  2971. }
  2972. itemjson[@"model"]=nsname;
  2973. itemjson[@"description"]=nsdescription;
  2974. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  2975. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  2976. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  2977. if(compute)
  2978. {
  2979. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute contactID:contactID];
  2980. }
  2981. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2982. count++;
  2983. }
  2984. sqlite3_finalize(statement);
  2985. }
  2986. ret[@"count"]=@(count);
  2987. if(count==0)
  2988. return nil;
  2989. else
  2990. return ret;
  2991. }
  2992. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  2993. {
  2994. //compute: add part to subtotal;
  2995. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  2996. dict_item[@(item_id)]=@"1";
  2997. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  2998. double cuft=0;
  2999. double weight=0;
  3000. int carton=0;
  3001. 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];
  3002. sqlite3_stmt * statement;
  3003. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3004. {
  3005. if (sqlite3_step(statement) == SQLITE_ROW)
  3006. {
  3007. double ulength = sqlite3_column_double(statement, 0);
  3008. double uwidth = sqlite3_column_double(statement, 1);
  3009. double uheight = sqlite3_column_double(statement, 2);
  3010. double uweight = sqlite3_column_double(statement, 3);
  3011. double mlength = sqlite3_column_double(statement, 4);
  3012. double mwidth = sqlite3_column_double(statement, 5);
  3013. double mheight = sqlite3_column_double(statement, 6);
  3014. double mweight = sqlite3_column_double(statement, 7);
  3015. double ilength = sqlite3_column_double(statement, 8);
  3016. double iwidth = sqlite3_column_double(statement, 9);
  3017. double iheight = sqlite3_column_double(statement, 10);
  3018. double iweight = sqlite3_column_double(statement, 11);
  3019. int pcs = sqlite3_column_int(statement,12);
  3020. int mpack = sqlite3_column_int(statement, 13);
  3021. int ipack = sqlite3_column_int(statement, 14);
  3022. double ucbf = sqlite3_column_double(statement, 15);
  3023. double icbf = sqlite3_column_double(statement, 16);
  3024. double mcbf = sqlite3_column_double(statement, 17);
  3025. if(ipack==0)
  3026. {
  3027. carton= count/mpack ;
  3028. weight = mweight*carton;
  3029. cuft= carton*(mlength*mwidth*mheight);
  3030. int remain=count%mpack;
  3031. if(remain==0)
  3032. {
  3033. //do nothing;
  3034. }
  3035. else
  3036. {
  3037. carton++;
  3038. weight += uweight*remain;
  3039. cuft += (ulength*uwidth*uheight)*remain;
  3040. }
  3041. }
  3042. else
  3043. {
  3044. carton = count/(mpack*ipack);
  3045. weight = mweight*carton;
  3046. cuft= carton*(mlength*mwidth*mheight);
  3047. int remain=count%(mpack*ipack);
  3048. if(remain==0)
  3049. {
  3050. // do nothing;
  3051. }
  3052. else
  3053. {
  3054. carton++;
  3055. int icarton =remain/ipack;
  3056. int iremain=remain%ipack;
  3057. weight += iweight*icarton;
  3058. cuft += (ilength*iwidth*iheight)*icarton;
  3059. if(iremain==0)
  3060. {
  3061. //do nothing;
  3062. }
  3063. else
  3064. {
  3065. weight += uweight*iremain;
  3066. cuft += (ulength*uwidth*uheight)*iremain;
  3067. }
  3068. }
  3069. }
  3070. #ifdef BUILD_NPD
  3071. cuft=ucbf*count;
  3072. weight= uweight*count;
  3073. #endif
  3074. }
  3075. sqlite3_finalize(statement);
  3076. }
  3077. if(compute)
  3078. {
  3079. NSArray * arr_count=nil;
  3080. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  3081. for(int i=0;i<arr_bundle.count;i++)
  3082. {
  3083. dict_item[arr_bundle[i]]=@"1";
  3084. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  3085. cuft+=[bundlejson[@"cuft"] doubleValue];
  3086. weight+=[bundlejson[@"weight"] doubleValue];
  3087. carton+=[bundlejson[@"carton"] intValue];
  3088. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  3089. }
  3090. }
  3091. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  3092. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  3093. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  3094. ret[@"items"]=dict_item;
  3095. return ret;
  3096. }
  3097. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  3098. {
  3099. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  3100. // 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 ];
  3101. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3102. DebugLog(@"offline_login sql:%@",sqlQuery);
  3103. sqlite3_stmt * statement;
  3104. int cart_count=0;
  3105. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3106. if ( dbresult== SQLITE_OK)
  3107. {
  3108. while (sqlite3_step(statement) == SQLITE_ROW)
  3109. {
  3110. int item_id = sqlite3_column_int(statement, 0);
  3111. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  3112. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3113. }
  3114. sqlite3_finalize(statement);
  3115. }
  3116. return cart_count;
  3117. }
  3118. +(NSData*) offline_requestcart :(NSMutableDictionary *) params
  3119. {
  3120. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3121. sqlite3 *db = [iSalesDB get_db];
  3122. UIApplication * app = [UIApplication sharedApplication];
  3123. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3124. NSString* orderCode=params[@"orderCode"];
  3125. int sort = [[params objectForKey:@"sort"] intValue];
  3126. NSString *sort_str = @"";
  3127. switch (sort) {
  3128. case 0:{
  3129. sort_str = @"order by c.modify_time desc";
  3130. }
  3131. break;
  3132. case 1:{
  3133. sort_str = @"order by c.modify_time asc";
  3134. }
  3135. break;
  3136. case 2:{
  3137. sort_str = @"order by m.name asc";
  3138. }
  3139. break;
  3140. case 3:{
  3141. sort_str = @"order by m.name desc";
  3142. }
  3143. break;
  3144. case 4:{
  3145. sort_str = @"order by m.description asc";
  3146. }
  3147. break;
  3148. default:
  3149. break;
  3150. }
  3151. 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 ];
  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. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3157. NSDate *date1 = [NSDate date];
  3158. int count=0;
  3159. int cart_count=0;
  3160. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3161. if ( dbresult== SQLITE_OK)
  3162. {
  3163. while (sqlite3_step(statement) == SQLITE_ROW)
  3164. {
  3165. // NSDate *row_date = [NSDate date];
  3166. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  3167. int product_id = sqlite3_column_int(statement, 0);
  3168. char *str_price = (char*)sqlite3_column_text(statement, 1);
  3169. int item_id = sqlite3_column_int(statement, 7);
  3170. NSString* Price=nil;
  3171. if(str_price==nil)
  3172. {
  3173. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  3174. // NSDate *price_date = [NSDate date];
  3175. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  3176. // DebugLog(@"price time interval");
  3177. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  3178. if(price==nil)
  3179. Price=@"No Price.";
  3180. else
  3181. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  3182. }
  3183. else
  3184. {
  3185. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  3186. }
  3187. double discount = sqlite3_column_double(statement, 2);
  3188. int item_count = sqlite3_column_int(statement, 3);
  3189. char *line_note = (char*)sqlite3_column_text(statement, 4);
  3190. NSString *nsline_note=nil;
  3191. if(line_note!=nil)
  3192. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  3193. char *name = (char*)sqlite3_column_text(statement, 5);
  3194. NSString *nsname=nil;
  3195. if(name!=nil)
  3196. nsname= [[NSString alloc]initWithUTF8String:name];
  3197. char *description = (char*)sqlite3_column_text(statement, 6);
  3198. NSString *nsdescription=nil;
  3199. if(description!=nil)
  3200. nsdescription= [[NSString alloc]initWithUTF8String:description];
  3201. int stockUom = sqlite3_column_int(statement, 8);
  3202. int _id = sqlite3_column_int(statement, 9);
  3203. int availability = sqlite3_column_int(statement, 10);
  3204. // NSDate *subtotal_date = [NSDate date];
  3205. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  3206. // DebugLog(@"subtotal_date time interval");
  3207. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  3208. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  3209. double weight=[bsubtotaljson[@"weight"] doubleValue];
  3210. int carton=[bsubtotaljson[@"carton"] intValue];
  3211. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3212. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  3213. // NSDate *img_date = [NSDate date];
  3214. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  3215. // DebugLog(@"img_date time interval");
  3216. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  3217. itemjson[@"model"]=nsname;
  3218. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  3219. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  3220. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  3221. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  3222. itemjson[@"check"]=@"true";
  3223. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  3224. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  3225. itemjson[@"unit_price"]=Price;
  3226. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  3227. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  3228. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  3229. itemjson[@"note"]=nsline_note;
  3230. if (!appDelegate.can_create_backorder) {
  3231. itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  3232. }
  3233. // NSDate *date2 = [NSDate date];
  3234. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  3235. // DebugLog(@"model_bundle time interval");
  3236. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  3237. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  3238. count++;
  3239. // DebugLog(@"row time interval");
  3240. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  3241. }
  3242. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3243. sqlite3_finalize(statement);
  3244. }
  3245. DebugLog(@"request cart total time interval");
  3246. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  3247. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  3248. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  3249. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3250. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3251. ret[@"count"]=[NSNumber numberWithInt:count ];
  3252. ret[@"mode"]=@"Regular Mode";
  3253. [iSalesDB close_db:db];
  3254. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  3255. DebugLog(@"general notes :%@",general_note);
  3256. ret[@"general_note"]= general_note;
  3257. return [RAUtils dict2data:ret];
  3258. }
  3259. +(NSData*) offline_login :(NSMutableDictionary *) params
  3260. {
  3261. NSString* user = [params valueForKey:@"user"];
  3262. NSString* password = [params valueForKey:@"password"];
  3263. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3264. sqlite3 *db = [iSalesDB get_db];
  3265. 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"]];
  3266. DebugLog(@"offline_login sql:%@",sqlQuery);
  3267. sqlite3_stmt * statement;
  3268. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3269. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3270. {
  3271. if (sqlite3_step(statement) == SQLITE_ROW)
  3272. {
  3273. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3274. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  3275. int can_show_price = sqlite3_column_int(statement, 0);
  3276. int can_see_price = sqlite3_column_int(statement, 1);
  3277. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  3278. if(contact_id==nil)
  3279. contact_id="";
  3280. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3281. int user_type = sqlite3_column_int(statement, 3);
  3282. int can_cancel_order = sqlite3_column_int(statement, 4);
  3283. int can_set_cart_price = sqlite3_column_int(statement, 5);
  3284. int can_create_portfolio = sqlite3_column_int(statement, 6);
  3285. int can_delete_order = sqlite3_column_int(statement, 7);
  3286. int can_submit_order = sqlite3_column_int(statement, 8);
  3287. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  3288. int can_create_order = sqlite3_column_int(statement, 10);
  3289. char *mode = (char*)sqlite3_column_text(statement, 11);
  3290. if(mode==nil)
  3291. mode="";
  3292. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  3293. char *username = (char*)sqlite3_column_text(statement, 12);
  3294. if(username==nil)
  3295. username="";
  3296. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  3297. int can_update_contact_info = sqlite3_column_int(statement, 13);
  3298. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  3299. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  3300. [header setValue:nscontact_id forKey:@"contact_id"];
  3301. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  3302. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  3303. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  3304. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  3305. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  3306. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  3307. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  3308. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  3309. [header setObject:@(YES) forKey:@"can_create_backorder"];
  3310. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  3311. [header setValue:nsusername forKey:@"username"];
  3312. NSError* error=nil;
  3313. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  3314. [header setValue:statusFilter forKey:@"statusFilter"];
  3315. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3316. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  3317. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  3318. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  3319. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  3320. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  3321. [ret setObject:header forKey:@"header"];
  3322. [ret setValue:nsmode forKey:@"mode"];
  3323. }
  3324. sqlite3_finalize(statement);
  3325. }
  3326. [iSalesDB close_db:db];
  3327. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3328. return [RAUtils dict2data:ret];
  3329. }
  3330. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  3331. {
  3332. NSString* contactId = [params valueForKey:@"contactId"];
  3333. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3334. sqlite3 *db = [iSalesDB get_db];
  3335. NSString *sqlQuery = nil;
  3336. {
  3337. sqlQuery=[NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email,img_0,img_1,img_2,price_type,notes,sales_rep,type from offline_contact where contact_id='%@'",contactId];
  3338. }
  3339. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  3340. sqlite3_stmt * statement;
  3341. [ret setValue:@"2" forKey:@"result"];
  3342. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3343. {
  3344. //int i = 0;
  3345. if (sqlite3_step(statement) == SQLITE_ROW)
  3346. {
  3347. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3348. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3349. // int editable = sqlite3_column_int(statement, 0);
  3350. char *company_name = (char*)sqlite3_column_text(statement, 1);
  3351. NSString *nscompany_name =nil;
  3352. if(company_name==nil)
  3353. nscompany_name=@"";
  3354. else
  3355. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  3356. char *country = (char*)sqlite3_column_text(statement, 2);
  3357. if(country==nil)
  3358. country="";
  3359. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  3360. // char *addr = (char*)sqlite3_column_text(statement, 3);
  3361. // if(addr==nil)
  3362. // addr="";
  3363. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  3364. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  3365. if(zipcode==nil)
  3366. zipcode="";
  3367. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  3368. char *state = (char*)sqlite3_column_text(statement, 5);
  3369. if(state==nil)
  3370. state="";
  3371. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  3372. char *city = (char*)sqlite3_column_text(statement, 6);
  3373. if(city==nil)
  3374. city="";
  3375. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  3376. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  3377. // NSString *nscontact_name = nil;
  3378. // if(contact_name==nil)
  3379. // nscontact_name=@"";
  3380. // else
  3381. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  3382. char *phone = (char*)sqlite3_column_text(statement, 8);
  3383. NSString *nsphone = nil;
  3384. if(phone==nil)
  3385. nsphone=@"";
  3386. else
  3387. nsphone= [[NSString alloc]initWithUTF8String:phone];
  3388. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  3389. if(contact_id==nil)
  3390. contact_id="";
  3391. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3392. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  3393. if(addr_1==nil)
  3394. addr_1="";
  3395. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  3396. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  3397. if(addr_2==nil)
  3398. addr_2="";
  3399. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  3400. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  3401. if(addr_3==nil)
  3402. addr_3="";
  3403. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  3404. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  3405. if(addr_4==nil)
  3406. addr_4="";
  3407. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  3408. char *first_name = (char*)sqlite3_column_text(statement, 14);
  3409. if(first_name==nil)
  3410. first_name="";
  3411. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  3412. char *last_name = (char*)sqlite3_column_text(statement, 15);
  3413. if(last_name==nil)
  3414. last_name="";
  3415. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  3416. char *fax = (char*)sqlite3_column_text(statement, 16);
  3417. NSString *nsfax = nil;
  3418. if(fax==nil)
  3419. nsfax=@"";
  3420. else
  3421. nsfax= [[NSString alloc]initWithUTF8String:fax];
  3422. char *email = (char*)sqlite3_column_text(statement, 17);
  3423. NSString *nsemail = nil;
  3424. if(email==nil)
  3425. nsemail=@"";
  3426. else
  3427. nsemail= [[NSString alloc]initWithUTF8String:email];
  3428. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  3429. NSString *nsimg_0 = nil;
  3430. if(img_0==nil)
  3431. nsimg_0=@"";
  3432. else
  3433. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  3434. [self copy_bcardImg:nsimg_0];
  3435. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  3436. NSString *nsimg_1 = nil;
  3437. if(img_1==nil)
  3438. nsimg_1=@"";
  3439. else
  3440. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  3441. [self copy_bcardImg:nsimg_1];
  3442. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  3443. NSString *nsimg_2 = nil;
  3444. if(img_2==nil)
  3445. nsimg_2=@"";
  3446. else
  3447. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  3448. [self copy_bcardImg:nsimg_2];
  3449. char *price_type = (char*)sqlite3_column_text(statement, 21);
  3450. NSString *nsprice_type = nil;
  3451. if(price_type==nil)
  3452. nsprice_type=@"";
  3453. else
  3454. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  3455. char *notes = (char*)sqlite3_column_text(statement, 22);
  3456. NSString *nsnotes = nil;
  3457. if(notes==nil)
  3458. nsnotes=@"";
  3459. else
  3460. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  3461. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  3462. NSString *nssalesrep = nil;
  3463. if(salesrep==nil)
  3464. nssalesrep=@"";
  3465. else
  3466. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  3467. NSString *contact_type = [self textAtColumn:24 statement:statement];
  3468. {
  3469. // decrypt
  3470. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  3471. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  3472. nsphone=[AESCrypt fastdecrypt:nsphone];
  3473. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3474. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  3475. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3476. }
  3477. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3478. [arr_name addObject:nsfirst_name];
  3479. [arr_name addObject:nslast_name];
  3480. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  3481. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  3482. [arr_ext addObject:nsaddr_1];
  3483. [arr_ext addObject:nsaddr_2];
  3484. [arr_ext addObject:nsaddr_3];
  3485. [arr_ext addObject:nsaddr_4];
  3486. [arr_ext addObject:@"\r\n"];
  3487. [arr_ext addObject:nscity];
  3488. [arr_ext addObject:nsstate];
  3489. [arr_ext addObject:nszipcode];
  3490. [arr_ext addObject:nscountry];
  3491. NSString *nsext=[RAUtils arr2string:arr_ext separator:@", " trim:true];
  3492. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  3493. [item setValue:nsimg_2 forKey:@"business_card_2"];
  3494. [item setValue:nsimg_0 forKey:@"business_card_0"];
  3495. [item setValue:nscountry forKey:@"customer_country"];
  3496. [item setValue:nsphone forKey:@"customer_phone"];
  3497. [item setValue:nsimg_1 forKey:@"business_card_1"];
  3498. [item setValue:nscompany_name forKey:@"customer_name"];
  3499. [item setValue:nsprice_type forKey:@"customer_price_type"];
  3500. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  3501. [item setValue:nsext forKey:@"customer_contact_ext"];
  3502. [item setValue:nszipcode forKey:@"customer_zipcode"];
  3503. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  3504. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  3505. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  3506. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  3507. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  3508. [item setValue:nslast_name forKey:@"customer_last_name"];
  3509. [item setValue:nscity forKey:@"customer_city"];
  3510. [item setValue:nsstate forKey:@"customer_state"];
  3511. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  3512. [item setValue:contactId forKey:@"customer_cid"];
  3513. [item setValue:nscontact_name forKey:@"customer_contact"];
  3514. [item setValue:nsfax forKey:@"customer_fax"];
  3515. [item setValue:nsemail forKey:@"customer_email"];
  3516. [item setValue:contact_type forKey:@"customer_contact_type"];
  3517. [ret setObject:item forKey:@"customerInfo"];
  3518. // i++;
  3519. }
  3520. UIApplication * app = [UIApplication sharedApplication];
  3521. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3522. [ret setValue:appDelegate.mode forKey:@"mode"];
  3523. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  3524. sqlite3_finalize(statement);
  3525. }
  3526. [iSalesDB close_db:db];
  3527. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3528. return ret;
  3529. }
  3530. + (bool) copy_bcardImg:(NSString*) filename
  3531. {
  3532. if(filename.length==0)
  3533. return false;
  3534. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3535. bool ret=false;
  3536. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3537. NSString *cachefolder = [paths objectAtIndex:0];
  3538. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  3539. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  3540. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  3541. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  3542. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  3543. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  3544. //
  3545. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  3546. NSFileManager* fileManager = [NSFileManager defaultManager];
  3547. BOOL bdir=NO;
  3548. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  3549. {
  3550. NSError *error = nil;
  3551. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  3552. {
  3553. ret=false;
  3554. }
  3555. else
  3556. {
  3557. ret=true;
  3558. }
  3559. // NSError *error = nil;
  3560. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  3561. //
  3562. // if(!bsuccess)
  3563. // {
  3564. // DebugLog(@"Create offline_createimg folder failed");
  3565. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3566. // return [RAUtils dict2data:ret];
  3567. // }
  3568. // if(bsuccess)
  3569. // {
  3570. // sqlite3 *db = [self get_db];
  3571. //
  3572. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  3573. // [iSalesDB close_db:db];
  3574. // }
  3575. }
  3576. return ret;
  3577. //
  3578. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  3579. // if(bsuccess)
  3580. // {
  3581. // NSError *error = nil;
  3582. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  3583. // {
  3584. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3585. // }
  3586. // else
  3587. // {
  3588. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  3589. //
  3590. // ret[@"img_url_aname"]=filename;
  3591. // ret[@"img_url"]=savedImagePath;
  3592. // }
  3593. // }
  3594. }
  3595. +(NSData *) offline_saveBusinesscard:(NSData *) image
  3596. {
  3597. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3598. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3599. NSString *cachefolder = [paths objectAtIndex:0];
  3600. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  3601. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  3602. NSFileManager* fileManager = [NSFileManager defaultManager];
  3603. BOOL bdir=YES;
  3604. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  3605. {
  3606. NSError *error = nil;
  3607. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  3608. if(!bsuccess)
  3609. {
  3610. DebugLog(@"Create offline_createimg folder failed");
  3611. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3612. return [RAUtils dict2data:ret];
  3613. }
  3614. // if(bsuccess)
  3615. // {
  3616. // sqlite3 *db = [self get_db];
  3617. //
  3618. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  3619. // [iSalesDB close_db:db];
  3620. // }
  3621. }
  3622. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  3623. //JEPG格式
  3624. //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  3625. NSString* filename =[NSString stringWithFormat:@"%@.png",[[NSUUID UUID] UUIDString]];
  3626. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  3627. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  3628. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  3629. if(bsuccess)
  3630. {
  3631. NSError *error = nil;
  3632. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  3633. {
  3634. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3635. }
  3636. else
  3637. {
  3638. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  3639. ret[@"img_url_aname"]=filename;
  3640. ret[@"img_url"]=filename;
  3641. }
  3642. }
  3643. else
  3644. {
  3645. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3646. }
  3647. return [RAUtils dict2data:ret];
  3648. }
  3649. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  3650. {
  3651. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  3652. category = [category substringToIndex:3];
  3653. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3654. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  3655. params[@"category"]= category;
  3656. ret[@"params"]= params;
  3657. [ret setValue:@"detail" forKey:@"target"];
  3658. [ret setValue:@"popup" forKey:@"action"];
  3659. [ret setValue:@"content" forKey:@"type"];
  3660. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  3661. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  3662. [ret setValue:@"true" forKey:@"single_row"];
  3663. [ret setValue:@"true" forKey:@"partial_refresh"];
  3664. // sqlite3 *db = [iSalesDB get_db];
  3665. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  3666. 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 ;
  3667. sqlite3_stmt * statement;
  3668. int count = 0;
  3669. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  3670. // int count=0;
  3671. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3672. {
  3673. int i=0;
  3674. while (sqlite3_step(statement) == SQLITE_ROW)
  3675. {
  3676. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3677. // char *name = (char*)sqlite3_column_text(statement, 1);
  3678. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3679. char *name = (char*)sqlite3_column_text(statement, 0);
  3680. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3681. int product_id = sqlite3_column_int(statement, 1);
  3682. char *url = (char*)sqlite3_column_text(statement, 2);
  3683. if(url==nil)
  3684. url="";
  3685. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3686. [item setValue:nsurl forKey:@"picture_path"];
  3687. [item setValue:nsname forKey:@"fash_name"];
  3688. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3689. [item setValue:category forKey:@"category"];
  3690. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3691. i++;
  3692. }
  3693. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3694. sqlite3_finalize(statement);
  3695. }
  3696. DebugLog(@"count:%d",count);
  3697. // [iSalesDB close_db:db];
  3698. return ret;
  3699. }
  3700. +(NSDictionary*) model_priceKvItem:(NSString*) key value:(NSString*)value
  3701. {
  3702. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3703. [ret setValue:key forKey:@"key"];
  3704. [ret setValue:value forKey:@"val"];
  3705. [ret setValue:@"price" forKey:@"type"];
  3706. return ret;
  3707. }
  3708. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  3709. {
  3710. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3711. [ret setValue:key forKey:@"key"];
  3712. [ret setValue:value forKey:@"val"];
  3713. return ret;
  3714. }
  3715. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  3716. {
  3717. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3718. [ret setValue:@"0" forKey:@"img_count"];
  3719. // sqlite3 *db = [iSalesDB get_db];
  3720. 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 ;
  3721. sqlite3_stmt * statement;
  3722. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3723. {
  3724. int i=0;
  3725. if (sqlite3_step(statement) == SQLITE_ROW)
  3726. {
  3727. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3728. // char *name = (char*)sqlite3_column_text(statement, 1);
  3729. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3730. char *value = (char*)sqlite3_column_text(statement, 0);
  3731. if(value==nil)
  3732. value="";
  3733. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  3734. char *key = (char*)sqlite3_column_text(statement, 1);
  3735. if(key==nil)
  3736. key="";
  3737. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  3738. [item setValue:nsvalue forKey:@"val"];
  3739. [item setValue:nskey forKey:@"key"];
  3740. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3741. [ret setValue:@"1" forKey:@"count"];
  3742. i++;
  3743. }
  3744. sqlite3_finalize(statement);
  3745. }
  3746. // [iSalesDB close_db:db];
  3747. return ret;
  3748. }
  3749. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  3750. {
  3751. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3752. [ret setValue:@"0" forKey:@"count"];
  3753. // sqlite3 *db = [iSalesDB get_db];
  3754. 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;
  3755. sqlite3_stmt * statement;
  3756. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3757. {
  3758. int i=0;
  3759. while (sqlite3_step(statement) == SQLITE_ROW)
  3760. {
  3761. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3762. // char *name = (char*)sqlite3_column_text(statement, 1);
  3763. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3764. char *value = (char*)sqlite3_column_text(statement, 0);
  3765. if(value==nil)
  3766. value="";
  3767. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  3768. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  3769. if(selector_display==nil)
  3770. selector_display="";
  3771. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  3772. int product_id = sqlite3_column_int(statement, 2);
  3773. char *category = (char*)sqlite3_column_text(statement, 3);
  3774. if(category==nil)
  3775. category="";
  3776. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  3777. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  3778. [item setValue:nsvalue forKey:@"title"];
  3779. [item setValue:url forKey:@"pic_url"];
  3780. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  3781. [params setValue:@"2" forKey:@"count"];
  3782. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  3783. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  3784. [param0 setValue:@"product_id" forKey:@"name"];
  3785. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  3786. [param1 setValue:nscategory forKey:@"val"];
  3787. [param1 setValue:@"category" forKey:@"name"];
  3788. [params setObject:param0 forKey:@"param_0"];
  3789. [params setObject:param1 forKey:@"param_1"];
  3790. [item setObject:params forKey:@"params"];
  3791. [ret setValue:nsselector_display forKey:@"name"];
  3792. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3793. i++;
  3794. }
  3795. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3796. [ret setValue:@"switch" forKey:@"action"];
  3797. sqlite3_finalize(statement);
  3798. }
  3799. // [iSalesDB close_db:db];
  3800. return ret;
  3801. }
  3802. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  3803. {
  3804. // model 在 category search 显示的图片。
  3805. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  3806. // sqlite3 *db = [iSalesDB get_db];
  3807. 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];
  3808. sqlite3_stmt * statement;
  3809. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3810. {
  3811. while (sqlite3_step(statement) == SQLITE_ROW)
  3812. {
  3813. char *url = (char*)sqlite3_column_text(statement, 0);
  3814. if(url==nil)
  3815. url="";
  3816. int type = sqlite3_column_int(statement, 1);
  3817. if(type==0)
  3818. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  3819. else
  3820. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  3821. }
  3822. sqlite3_finalize(statement);
  3823. }
  3824. // [iSalesDB close_db:db];
  3825. return ret;
  3826. }
  3827. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  3828. {
  3829. int item_id=-1;
  3830. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  3831. sqlite3_stmt * statement;
  3832. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3833. {
  3834. if (sqlite3_step(statement) == SQLITE_ROW)
  3835. {
  3836. item_id = sqlite3_column_int(statement, 0);
  3837. }
  3838. sqlite3_finalize(statement);
  3839. }
  3840. return item_id;
  3841. }
  3842. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  3843. {
  3844. // NSString* ret = @"";
  3845. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  3846. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  3847. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  3848. sqlite3_stmt * statement;
  3849. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3850. {
  3851. while (sqlite3_step(statement) == SQLITE_ROW)
  3852. {
  3853. int bitem_id = sqlite3_column_int(statement, 0);
  3854. int bitem_qty = sqlite3_column_int(statement, 1);
  3855. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  3856. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  3857. }
  3858. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  3859. sqlite3_finalize(statement);
  3860. }
  3861. // if(ret==nil)
  3862. // ret=@"";
  3863. *count=arr_count;
  3864. return arr_bundle;
  3865. }
  3866. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  3867. {
  3868. // get default sold qty, return -1 if model not found;
  3869. int ret = -1;
  3870. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  3871. sqlite3_stmt * statement;
  3872. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3873. {
  3874. if (sqlite3_step(statement) == SQLITE_ROW)
  3875. {
  3876. ret = sqlite3_column_int(statement, 0);
  3877. }
  3878. sqlite3_finalize(statement);
  3879. }
  3880. return ret;
  3881. }
  3882. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  3883. {
  3884. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3885. [ret setValue:@"0" forKey:@"img_count"];
  3886. // sqlite3 *db = [iSalesDB get_db];
  3887. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  3888. sqlite3_stmt * statement;
  3889. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3890. {
  3891. int i=0;
  3892. while (sqlite3_step(statement) == SQLITE_ROW)
  3893. {
  3894. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3895. // char *name = (char*)sqlite3_column_text(statement, 1);
  3896. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3897. char *url = (char*)sqlite3_column_text(statement, 0);
  3898. if(url==nil)
  3899. url="";
  3900. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3901. [item setValue:nsurl forKey:@"s"];
  3902. [item setValue:nsurl forKey:@"l"];
  3903. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  3904. i++;
  3905. }
  3906. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  3907. sqlite3_finalize(statement);
  3908. }
  3909. // [iSalesDB close_db:db];
  3910. return ret;
  3911. }
  3912. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  3913. UIApplication * app = [UIApplication sharedApplication];
  3914. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3915. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  3916. sqlite3 *db = [iSalesDB get_db];
  3917. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  3918. NSString* product_id=params[@"product_id"];
  3919. NSString *item_count_str = params[@"item_count"];
  3920. NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  3921. NSArray *item_count_arr = item_count_str ? [RAUtils string2arr:item_count_str separator:@","] : nil;
  3922. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  3923. // NSString *sql = @"";
  3924. for(int i=0;i<arr.count;i++)
  3925. {
  3926. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  3927. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  3928. __block int cart_count = 0;
  3929. if (!item_count_str) {
  3930. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  3931. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3932. NSString *model_set = [self textAtColumn:0 statement:stmt];
  3933. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  3934. cart_count = [[model_set_components lastObject] integerValue];
  3935. }];
  3936. }
  3937. if(count==0)
  3938. {
  3939. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  3940. sqlite3_stmt *stmt;
  3941. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  3942. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],[arr[i] length],NULL);
  3943. if (item_count_arr) {
  3944. sqlite3_bind_int(stmt,2,[item_count_arr[i] integerValue]);
  3945. } else {
  3946. sqlite3_bind_int(stmt,2,cart_count);
  3947. }
  3948. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  3949. [iSalesDB execSql:@"ROLLBACK" db:db];
  3950. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  3951. [iSalesDB close_db:db];
  3952. DebugLog(@"add to wishlist error");
  3953. return ret;
  3954. }
  3955. } else {
  3956. int qty = 0;
  3957. if (item_count_arr) {
  3958. qty = [item_count_arr[i] integerValue];
  3959. } else {
  3960. qty = cart_count;
  3961. }
  3962. 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]];
  3963. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  3964. [iSalesDB execSql:@"ROLLBACK" db:db];
  3965. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  3966. [iSalesDB close_db:db];
  3967. DebugLog(@"add to wishlist error");
  3968. return ret;
  3969. }
  3970. }
  3971. }
  3972. // [iSalesDB execSql:sql db:db];
  3973. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3974. [iSalesDB execSql:@"END TRANSACTION" db:db];
  3975. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  3976. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  3977. [iSalesDB close_db:db];
  3978. appDelegate.wish_count =count;
  3979. [appDelegate update_count_mark];
  3980. ret[@"result"]= [NSNumber numberWithInt:2];
  3981. return ret;
  3982. }
  3983. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  3984. // 0 category
  3985. // 1 search
  3986. // 2 itemsearch
  3987. NSData *ret = nil;
  3988. NSDictionary *items = nil;
  3989. switch (from) {
  3990. case 0:{
  3991. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  3992. }
  3993. break;
  3994. case 1:{
  3995. items = [[self search:params limited:NO] objectForKey:@"items"];
  3996. }
  3997. break;
  3998. case 2:{
  3999. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  4000. }
  4001. break;
  4002. default:
  4003. break;
  4004. }
  4005. if (!items) {
  4006. return ret;
  4007. }
  4008. int count = [[items objectForKey:@"count"] intValue];
  4009. NSMutableString *product_id_str = [@"" mutableCopy];
  4010. for (int i = 0; i < count; i++) {
  4011. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  4012. NSDictionary *item = [items objectForKey:key];
  4013. NSString *product_id = [item objectForKey:@"product_id"];
  4014. if (i == 0) {
  4015. [product_id_str appendString:product_id];
  4016. } else {
  4017. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  4018. }
  4019. }
  4020. NSString *add_to = [self valueInParams:params key:@"addTo"];
  4021. if ([add_to isEqualToString:@"cart"]) {
  4022. NSString *order_code = [params objectForKey:@"orderCode"];
  4023. if (order_code.length) {
  4024. NSDictionary *newParams = @{
  4025. @"product_id" : product_id_str,
  4026. @"orderCode" : order_code
  4027. };
  4028. ret = [self offline_add2cart:newParams.mutableCopy];
  4029. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4030. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4031. ret = [RAUtils dict2data:retDic];
  4032. }
  4033. } else if([add_to isEqualToString:@"wishlist"]) {
  4034. NSDictionary *newParams = @{
  4035. @"product_id" : product_id_str
  4036. };
  4037. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  4038. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4039. ret = [RAUtils dict2data:retDic];
  4040. } else if([add_to isEqualToString:@"portfolio"]) {
  4041. NSDictionary *newParams = @{
  4042. @"product_id" : product_id_str
  4043. };
  4044. NSData *data = [self offline_add2Portfolio:newParams.mutableCopy];
  4045. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4046. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4047. ret = [RAUtils dict2data:retDic];
  4048. }
  4049. return ret;
  4050. }
  4051. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  4052. {
  4053. return [self addAll:params from:0];
  4054. }
  4055. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  4056. {
  4057. return [self addAll:params from:1];
  4058. }
  4059. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  4060. {
  4061. return [self addAll:params from:2];
  4062. }
  4063. #pragma mark - Jack
  4064. #warning 做SQL操作时转义!!
  4065. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  4066. // "val_227" : {
  4067. // "check" : 1,
  4068. // "value" : "US United States",
  4069. // "value_id" : "228"
  4070. // },
  4071. if (!countryCode) {
  4072. countryCode = @"US";
  4073. }
  4074. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  4075. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  4076. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  4077. int code_id = sqlite3_column_int(stmt, 3); // id
  4078. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4079. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  4080. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  4081. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4082. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  4083. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4084. }
  4085. long n = *count;
  4086. *count = n + 1;
  4087. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  4088. [container setValue:countryDic forKey:key];
  4089. }] mutableCopy];
  4090. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4091. return ret;
  4092. }
  4093. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  4094. countryCode = [self translateSingleQuote:countryCode];
  4095. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  4096. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4097. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  4098. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  4099. if (name == NULL) {
  4100. name = "";
  4101. }
  4102. if (code == NULL) {
  4103. code = "";
  4104. }
  4105. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4106. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4107. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  4108. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4109. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  4110. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4111. }
  4112. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4113. [container setValue:stateDic forKey:key];
  4114. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  4115. DebugLog(@"query all state error: %@",err_msg);
  4116. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4117. // [stateDic setValue:@"Other" forKey:@"value"];
  4118. // [stateDic setValue:@"" forKey:@"value_id"];
  4119. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4120. //
  4121. // if (state_code && [@"" isEqualToString:state_code]) {
  4122. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4123. // }
  4124. //
  4125. // NSString *key = [NSString stringWithFormat:@"val_0"];
  4126. // [container setValue:stateDic forKey:key];
  4127. }] mutableCopy];
  4128. [ret removeObjectForKey:@"result"];
  4129. // failure 可以不用了,一样的
  4130. if (ret.allKeys.count == 0) {
  4131. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4132. [stateDic setValue:@"Other" forKey:@"value"];
  4133. [stateDic setValue:@"" forKey:@"value_id"];
  4134. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4135. if (state_code && [@"" isEqualToString:state_code]) {
  4136. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4137. }
  4138. NSString *key = [NSString stringWithFormat:@"val_0"];
  4139. [ret setValue:stateDic forKey:key];
  4140. }
  4141. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4142. return ret;
  4143. }
  4144. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  4145. codeId = [self translateSingleQuote:codeId];
  4146. 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];
  4147. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4148. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  4149. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  4150. if (name == NULL) {
  4151. name = "";
  4152. }
  4153. if (code == NULL) {
  4154. code = "";
  4155. }
  4156. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4157. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4158. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  4159. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4160. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  4161. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4162. }
  4163. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4164. [container setValue:stateDic forKey:key];
  4165. }] mutableCopy];
  4166. [ret removeObjectForKey:@"result"];
  4167. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4168. return ret;
  4169. }
  4170. + (NSDictionary *)offline_getPrice {
  4171. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price where is_show = 1;"];
  4172. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4173. char *name = (char *) sqlite3_column_text(stmt, 1);
  4174. int type = sqlite3_column_int(stmt, 2);
  4175. int orderBy = sqlite3_column_int(stmt, 3);
  4176. if (name == NULL) {
  4177. name = "";
  4178. }
  4179. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  4180. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4181. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  4182. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4183. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4184. if (orderBy == 0) {
  4185. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4186. }
  4187. [container setValue:priceDic forKey:key];
  4188. }] mutableCopy];
  4189. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4190. return ret;
  4191. }
  4192. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  4193. NSString *sql = @"select _id,type_name from contact_type where is_show = 1";
  4194. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  4195. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  4196. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4197. int _id = sqlite3_column_int(stmt, 0);
  4198. NSString *name = [self textAtColumn:1 statement:stmt];
  4199. NSDictionary *typeDic = @{
  4200. @"value_id" : name,
  4201. @"value" : name,
  4202. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  4203. };
  4204. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  4205. [ret setValue:[NSNumber numberWithInt:++(*count)] forKey:@"count"];
  4206. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  4207. }];
  4208. return ret;
  4209. }
  4210. + (NSDictionary *)offline_getSalesRep {
  4211. // 首先从offline_login表中取出sales_code
  4212. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4213. NSString *user = app.user;
  4214. user = [self translateSingleQuote:user];
  4215. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  4216. __block NSString *user_code = @"";
  4217. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4218. char *code = (char *)sqlite3_column_text(stmt, 0);
  4219. if (code == NULL) {
  4220. code = "";
  4221. }
  4222. user_code = [NSString stringWithUTF8String:code];
  4223. }];
  4224. // 再取所有salesRep
  4225. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  4226. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4227. // 1 name 2 code 3 salesrep_id
  4228. char *name = (char *)sqlite3_column_text(stmt, 1);
  4229. char *code = (char *)sqlite3_column_text(stmt, 2);
  4230. int salesrep_id = sqlite3_column_int(stmt, 3);
  4231. if (name == NULL) {
  4232. name = "";
  4233. }
  4234. if (code == NULL) {
  4235. code = "";
  4236. }
  4237. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  4238. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  4239. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  4240. // 比较code 相等则check
  4241. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  4242. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4243. }
  4244. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  4245. }] mutableCopy];
  4246. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4247. return ret;
  4248. }
  4249. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  4250. zipcode = [self translateSingleQuote:zipcode];
  4251. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  4252. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4253. char *country = (char *)sqlite3_column_text(stmt, 0);
  4254. char *state = (char *)sqlite3_column_text(stmt, 1);
  4255. char *city = (char *)sqlite3_column_text(stmt, 2);
  4256. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  4257. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  4258. if (country == NULL) {
  4259. country = "";
  4260. }
  4261. if (state == NULL) {
  4262. state = "";
  4263. }
  4264. if (city == NULL) {
  4265. city = "";
  4266. }
  4267. if (country_code == NULL) {
  4268. country_code = "";
  4269. }
  4270. if (state_code == NULL) {
  4271. state_code = "";
  4272. }
  4273. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  4274. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  4275. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  4276. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  4277. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  4278. }] mutableCopy];
  4279. return ret;
  4280. }
  4281. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  4282. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  4283. [item setValue:value forKey:valueKey];
  4284. [dic setValue:item forKey:itemKey];
  4285. }
  4286. + (NSString *)countryCodeByid:(NSString *)code_id {
  4287. NSString *ret = nil;
  4288. code_id = [self translateSingleQuote:code_id];
  4289. sqlite3 *db = [iSalesDB get_db];
  4290. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  4291. sqlite3_stmt * statement;
  4292. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4293. while (sqlite3_step(statement) == SQLITE_ROW) {
  4294. char *code = (char *)sqlite3_column_text(statement, 0);
  4295. if (code == NULL) {
  4296. code = "";
  4297. }
  4298. ret = [NSString stringWithUTF8String:code];
  4299. }
  4300. sqlite3_finalize(statement);
  4301. }
  4302. [iSalesDB close_db:db];
  4303. return ret;
  4304. }
  4305. + (NSString *)countryCodeIdByCode:(NSString *)code {
  4306. NSString *ret = nil;
  4307. code = [self translateSingleQuote:code];
  4308. sqlite3 *db = [iSalesDB get_db];
  4309. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  4310. sqlite3_stmt * statement;
  4311. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4312. while (sqlite3_step(statement) == SQLITE_ROW) {
  4313. char *_id = (char *)sqlite3_column_text(statement, 0);
  4314. if (_id == NULL) {
  4315. _id = "";
  4316. }
  4317. ret = [NSString stringWithFormat:@"%s",_id];
  4318. }
  4319. sqlite3_finalize(statement);
  4320. }
  4321. [iSalesDB close_db:db];
  4322. return ret;
  4323. }
  4324. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  4325. NSString *name = nil;
  4326. codeId = [self translateSingleQuote:codeId];
  4327. sqlite3 *db = [iSalesDB get_db];
  4328. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  4329. sqlite3_stmt * statement;
  4330. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4331. while (sqlite3_step(statement) == SQLITE_ROW) {
  4332. char *value = (char *)sqlite3_column_text(statement, 0);
  4333. if (value == NULL) {
  4334. value = "";
  4335. }
  4336. name = [NSString stringWithUTF8String:value];
  4337. }
  4338. sqlite3_finalize(statement);
  4339. }
  4340. [iSalesDB close_db:db];
  4341. return name;
  4342. }
  4343. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  4344. NSString *ret = nil;
  4345. sqlite3 *db = [iSalesDB get_db];
  4346. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  4347. sqlite3_stmt * statement;
  4348. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4349. while (sqlite3_step(statement) == SQLITE_ROW) {
  4350. char *name = (char *)sqlite3_column_text(statement, 0);
  4351. if (name == NULL) {
  4352. name = "";
  4353. }
  4354. ret = [NSString stringWithUTF8String:name];
  4355. }
  4356. sqlite3_finalize(statement);
  4357. }
  4358. [iSalesDB close_db:db];
  4359. return ret;
  4360. }
  4361. + (NSString *)salesRepCodeById:(NSString *)_id {
  4362. NSString *ret = nil;
  4363. _id = [self translateSingleQuote:_id];
  4364. sqlite3 *db = [iSalesDB get_db];
  4365. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  4366. sqlite3_stmt * statement;
  4367. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4368. while (sqlite3_step(statement) == SQLITE_ROW) {
  4369. char *rep = (char *)sqlite3_column_text(statement, 0);
  4370. if (rep == NULL) {
  4371. rep = "";
  4372. }
  4373. ret = [NSString stringWithUTF8String:rep];
  4374. }
  4375. sqlite3_finalize(statement);
  4376. }
  4377. [iSalesDB close_db:db];
  4378. return ret;
  4379. }
  4380. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  4381. char *tx = (char *)sqlite3_column_text(stmt, col);
  4382. if (tx == NULL) {
  4383. tx = "";
  4384. }
  4385. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  4386. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  4387. if (!text) {
  4388. text = @"";
  4389. }
  4390. // 将字符全部为' '的字符串干掉
  4391. int spaceCount = 0;
  4392. for (int i = 0; i < text.length; i++) {
  4393. if ([text characterAtIndex:i] == ' ') {
  4394. spaceCount++;
  4395. }
  4396. }
  4397. if (spaceCount == text.length) {
  4398. text = @"";
  4399. }
  4400. return text;
  4401. }
  4402. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  4403. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  4404. NSData *data = [NSData dataWithContentsOfFile:path];
  4405. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4406. return ret;
  4407. }
  4408. + (NSString *)textFileName:(NSString *)name {
  4409. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  4410. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  4411. if (!text) {
  4412. text = @"";
  4413. }
  4414. return text;
  4415. }
  4416. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  4417. return [[dic objectForKey:key] mutableCopy];
  4418. }
  4419. + (id)translateSingleQuote:(NSString *)string {
  4420. if ([string isKindOfClass:[NSString class]])
  4421. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4422. return string;
  4423. }
  4424. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  4425. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  4426. }
  4427. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  4428. NSString* ret= nil;
  4429. NSString *sqlQuery = nil;
  4430. // 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
  4431. 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];
  4432. sqlite3_stmt * statement;
  4433. // int count=0;
  4434. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4435. {
  4436. if (sqlite3_step(statement) == SQLITE_ROW)
  4437. {
  4438. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  4439. if(imgurl==nil)
  4440. imgurl="";
  4441. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  4442. ret=nsimgurl;
  4443. }
  4444. sqlite3_finalize(statement);
  4445. }
  4446. else
  4447. {
  4448. [ret setValue:@"8" forKey:@"result"];
  4449. }
  4450. // [iSalesDB close_db:db];
  4451. // DebugLog(@"data string: %@",ret );
  4452. return ret;
  4453. }
  4454. #pragma mark contact list
  4455. +(NSDictionary*) offline_contactlist :(NSMutableDictionary *) params
  4456. {
  4457. // contactType = "Sales_Order_Customer";
  4458. // limit = 25;
  4459. // offset = 0;
  4460. // password = 123456;
  4461. // "price_name" = 16;
  4462. // user = EvanK;
  4463. sqlite3 *db = [iSalesDB get_db];
  4464. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  4465. if (contactType) {
  4466. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  4467. } else {
  4468. contactType = @"1 = 1";
  4469. }
  4470. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  4471. DebugLog(@"offline contact list keyword: %@",keyword);
  4472. // advanced search
  4473. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  4474. if (contact_name) {
  4475. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4476. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  4477. } else {
  4478. contact_name = @"";
  4479. }
  4480. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  4481. if (customer_phone) {
  4482. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4483. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  4484. } else {
  4485. customer_phone = @"";
  4486. }
  4487. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  4488. if (customer_fax) {
  4489. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4490. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  4491. } else {
  4492. customer_fax = @"";
  4493. }
  4494. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  4495. if (customer_zipcode) {
  4496. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4497. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  4498. } else {
  4499. customer_zipcode = @"";
  4500. }
  4501. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  4502. if (customer_sales_rep) {
  4503. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4504. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  4505. } else {
  4506. customer_sales_rep = @"";
  4507. }
  4508. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  4509. if (customer_state) {
  4510. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4511. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  4512. } else {
  4513. customer_state = @"";
  4514. }
  4515. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  4516. if (customer_name) {
  4517. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4518. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  4519. } else {
  4520. customer_name = @"";
  4521. }
  4522. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  4523. if (customer_country) {
  4524. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4525. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  4526. } else {
  4527. customer_country = @"";
  4528. }
  4529. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  4530. if (customer_cid) {
  4531. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4532. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  4533. } else {
  4534. customer_cid = @"";
  4535. }
  4536. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  4537. if (customer_city) {
  4538. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4539. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  4540. } else {
  4541. customer_city = @"";
  4542. }
  4543. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  4544. if (customer_address) {
  4545. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4546. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  4547. } else {
  4548. customer_address = @"";
  4549. }
  4550. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  4551. if (customer_email) {
  4552. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4553. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  4554. } else {
  4555. customer_email = @"";
  4556. }
  4557. NSString *price_name = [params valueForKey:@"price_name"];
  4558. if (price_name) {
  4559. if ([price_name containsString:@","]) {
  4560. // 首先从 price表中查处name
  4561. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  4562. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  4563. for (int i = 1;i < pArray.count;i++) {
  4564. NSString *p = pArray[i];
  4565. [mutablePStr appendFormat:@" or type = %@ ",p];
  4566. }
  4567. [mutablePStr appendString:@";"];
  4568. __block NSMutableArray *price_name_array = [NSMutableArray array];
  4569. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4570. char *name = (char *)sqlite3_column_text(stmt, 0);
  4571. if (!name)
  4572. name = "";
  4573. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  4574. }];
  4575. // 再根据name 拼sql
  4576. NSMutableString *mutable_price_name = [NSMutableString string];
  4577. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  4578. for (int i = 1; i < price_name_array.count; i++) {
  4579. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  4580. }
  4581. [mutable_price_name appendString:@")"];
  4582. price_name = mutable_price_name;
  4583. } else {
  4584. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  4585. if ([price_name isEqualToString:@""]) {
  4586. price_name = @"";
  4587. } else {
  4588. __block NSString *price;
  4589. price_name = [self translateSingleQuote:price_name];
  4590. [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) {
  4591. char *p = (char *)sqlite3_column_text(stmt, 0);
  4592. if (p == NULL) {
  4593. p = "";
  4594. }
  4595. price = [NSString stringWithUTF8String:p];
  4596. }];
  4597. if ([price isEqualToString:@""]) {
  4598. price_name = @"";
  4599. } else {
  4600. price = [self translateSingleQuote:price];
  4601. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  4602. }
  4603. }
  4604. }
  4605. } else {
  4606. price_name = @"";
  4607. }
  4608. int limit = [[params valueForKey:@"limit"] intValue];
  4609. int offset = [[params valueForKey:@"offset"] intValue];
  4610. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4611. 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];
  4612. 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];
  4613. // int result= [iSalesDB AddExFunction:db];
  4614. int count =0;
  4615. NSString *sqlQuery = nil;
  4616. if(keyword.length==0)
  4617. {
  4618. // 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];
  4619. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  4620. sqlQuery = sql;
  4621. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  4622. }
  4623. else
  4624. {
  4625. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  4626. keyword = keyword.lowercaseString;
  4627. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4628. 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];
  4629. 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]];
  4630. }
  4631. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  4632. sqlite3_stmt * statement;
  4633. [ret setValue:@"2" forKey:@"result"];
  4634. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  4635. // 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";
  4636. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4637. {
  4638. int i = 0;
  4639. while (sqlite3_step(statement) == SQLITE_ROW)
  4640. {
  4641. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4642. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4643. int editable = sqlite3_column_int(statement, 0);
  4644. char *company_name = (char*)sqlite3_column_text(statement, 1);
  4645. NSString *nscompany_name =nil;
  4646. if(company_name==nil)
  4647. nscompany_name=@"";
  4648. else
  4649. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  4650. char *country = (char*)sqlite3_column_text(statement, 2);
  4651. if(country==nil)
  4652. country="";
  4653. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  4654. // char *addr = (char*)sqlite3_column_text(statement, 3);
  4655. // if(addr==nil)
  4656. // addr="";
  4657. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  4658. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  4659. if(zipcode==nil)
  4660. zipcode="";
  4661. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  4662. char *state = (char*)sqlite3_column_text(statement, 5);
  4663. if(state==nil)
  4664. state="";
  4665. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  4666. char *city = (char*)sqlite3_column_text(statement, 6);
  4667. if(city==nil)
  4668. city="";
  4669. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  4670. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  4671. // NSString *nscontact_name = nil;
  4672. // if(contact_name==nil)
  4673. // nscontact_name=@"";
  4674. // else
  4675. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  4676. char *phone = (char*)sqlite3_column_text(statement, 8);
  4677. NSString *nsphone = nil;
  4678. if(phone==nil)
  4679. nsphone=@"";
  4680. else
  4681. nsphone= [[NSString alloc]initWithUTF8String:phone];
  4682. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  4683. if(contact_id==nil)
  4684. contact_id="";
  4685. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4686. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  4687. if(addr_1==nil)
  4688. addr_1="";
  4689. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4690. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  4691. if(addr_2==nil)
  4692. addr_2="";
  4693. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4694. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  4695. if(addr_3==nil)
  4696. addr_3="";
  4697. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4698. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  4699. if(addr_4==nil)
  4700. addr_4="";
  4701. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4702. char *first_name = (char*)sqlite3_column_text(statement, 14);
  4703. if(first_name==nil)
  4704. first_name="";
  4705. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  4706. char *last_name = (char*)sqlite3_column_text(statement, 15);
  4707. if(last_name==nil)
  4708. last_name="";
  4709. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  4710. char *fax = (char*)sqlite3_column_text(statement, 16);
  4711. NSString *nsfax = nil;
  4712. if(fax==nil)
  4713. nsfax=@"";
  4714. else
  4715. {
  4716. nsfax= [[NSString alloc]initWithUTF8String:fax];
  4717. if(nsfax.length>0)
  4718. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  4719. }
  4720. char *email = (char*)sqlite3_column_text(statement, 17);
  4721. NSString *nsemail = nil;
  4722. if(email==nil)
  4723. nsemail=@"";
  4724. else
  4725. {
  4726. nsemail= [[NSString alloc]initWithUTF8String:email];
  4727. if(nsemail.length>0)
  4728. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  4729. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  4730. }
  4731. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4732. [arr_name addObject:nsfirst_name];
  4733. [arr_name addObject:nslast_name];
  4734. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  4735. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  4736. {
  4737. // decrypt
  4738. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  4739. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  4740. nsphone=[AESCrypt fastdecrypt:nsphone];
  4741. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4742. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  4743. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4744. }
  4745. [arr_addr addObject:nscompany_name];
  4746. [arr_addr addObject:nscontact_name];
  4747. [arr_addr addObject:@"<br>"];
  4748. [arr_addr addObject:nsaddr_1];
  4749. [arr_addr addObject:nsaddr_2];
  4750. [arr_addr addObject:nsaddr_3];
  4751. [arr_addr addObject:nsaddr_4];
  4752. //[arr_addr addObject:nsaddr];
  4753. [arr_addr addObject:nszipcode];
  4754. [arr_addr addObject:nscity];
  4755. [arr_addr addObject:nsstate];
  4756. [arr_addr addObject:nscountry];
  4757. [arr_addr addObject:@"<br>"];
  4758. [arr_addr addObject:nsphone];
  4759. [arr_addr addObject:nsfax];
  4760. [arr_addr addObject:nsemail];
  4761. NSString * name = [RAUtils arr2string:arr_addr separator:@", " trim:true];
  4762. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  4763. [item setValue:name forKey:@"name"];
  4764. [item setValue:nscontact_id forKey:@"contact_id"];
  4765. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  4766. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4767. i++;
  4768. }
  4769. UIApplication * app = [UIApplication sharedApplication];
  4770. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4771. [ret setValue:appDelegate.mode forKey:@"mode"];
  4772. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  4773. sqlite3_finalize(statement);
  4774. }
  4775. [iSalesDB close_db:db];
  4776. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  4777. return ret;
  4778. }
  4779. #pragma mark contact Advanced search
  4780. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  4781. {
  4782. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  4783. return [RAUtils dict2data:contactAdvanceDic];
  4784. }
  4785. #pragma mark create new contact
  4786. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  4787. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  4788. NSData *data = [NSData dataWithContentsOfFile:path];
  4789. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4790. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  4791. NSString *countryCode = nil;
  4792. NSString *countryCode_id = nil;
  4793. NSString *stateCode = nil;
  4794. NSString *city = nil;
  4795. NSString *zipCode = nil;
  4796. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  4797. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  4798. NSString *code_id = params[@"country"];
  4799. countryCode_id = code_id;
  4800. countryCode = [self countryCodeByid:code_id];
  4801. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  4802. NSString *zip_code = params[@"zipcode"];
  4803. // 剔除全部为空格
  4804. int spaceCount = 0;
  4805. for (int i = 0; i < zip_code.length; i++) {
  4806. if ([zip_code characterAtIndex:i] == ' ') {
  4807. spaceCount++;
  4808. }
  4809. }
  4810. if (spaceCount == zip_code.length) {
  4811. zip_code = @"";
  4812. }
  4813. zipCode = zip_code;
  4814. if (zipCode.length > 0) {
  4815. countryCode_id = params[@"country"];
  4816. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  4817. countryCode = [dic valueForKey:@"country_code"];
  4818. if (!countryCode) {
  4819. // countryCode = @"US";
  4820. NSString *code_id = params[@"country"];
  4821. countryCode = [self countryCodeByid:code_id];
  4822. }
  4823. stateCode = [dic valueForKey:@"state_code"];
  4824. if (!stateCode.length) {
  4825. stateCode = params[@"state"];
  4826. }
  4827. city = [dic valueForKey:@"city"];
  4828. if (!city.length) {
  4829. city = params[@"city"];
  4830. }
  4831. // zip code
  4832. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  4833. [zipDic setValue:zipCode forKey:@"value"];
  4834. [section_0 setValue:zipDic forKey:@"item_11"];
  4835. } else {
  4836. NSString *code_id = params[@"country"];
  4837. countryCode = [self countryCodeByid:code_id];
  4838. stateCode = params[@"state"];
  4839. city = params[@"city"];
  4840. }
  4841. }
  4842. } else {
  4843. // default: US United States
  4844. countryCode = @"US";
  4845. countryCode_id = @"228";
  4846. }
  4847. // country
  4848. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  4849. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  4850. // state
  4851. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  4852. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  4853. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  4854. // city
  4855. if (city) {
  4856. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  4857. [cityDic setValue:city forKey:@"value"];
  4858. [section_0 setValue:cityDic forKey:@"item_13"];
  4859. }
  4860. // price type
  4861. NSDictionary *priceDic = [self offline_getPrice];
  4862. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  4863. // contact type
  4864. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  4865. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  4866. // Sales Rep
  4867. NSDictionary *repDic = [self offline_getSalesRep];
  4868. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  4869. [ret setValue:section_0 forKey:@"section_0"];
  4870. return [RAUtils dict2data:ret];
  4871. }
  4872. #pragma mark save
  4873. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  4874. NSString *addr = nil;
  4875. NSString *contact_name = nil;
  4876. NSString *companyName = [params objectForKey:@"company"];
  4877. if (companyName) {
  4878. companyName = [AESCrypt fastencrypt:companyName];
  4879. } else {
  4880. companyName = @"";
  4881. }
  4882. DebugLog(@"company");
  4883. companyName = [self translateSingleQuote:companyName];
  4884. NSString *addr1 = [params objectForKey:@"address"];
  4885. NSString *addr2 = [params objectForKey:@"address2"];
  4886. NSString *addr3 = [params objectForKey:@"address_3"];
  4887. NSString *addr4 = [params objectForKey:@"address_4"];
  4888. if (!addr2) {
  4889. addr2 = @"";
  4890. }
  4891. if (!addr3) {
  4892. addr3 = @"";
  4893. }
  4894. if (!addr4) {
  4895. addr4 = @"";
  4896. }
  4897. if (!addr1) {
  4898. addr1 = @"";
  4899. }
  4900. DebugLog(@"addr");
  4901. addr = [RAUtils arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  4902. addr = [AESCrypt fastencrypt:addr];
  4903. addr = [self translateSingleQuote:addr];
  4904. if (addr1 && ![addr1 isEqualToString:@""]) {
  4905. addr1 = [AESCrypt fastencrypt:addr1];
  4906. }
  4907. addr1 = [self translateSingleQuote:addr1];
  4908. addr2 = [self translateSingleQuote:addr2];
  4909. addr3 = [self translateSingleQuote:addr3];
  4910. addr4 = [self translateSingleQuote:addr4];
  4911. NSString *country = [params objectForKey:@"country"];
  4912. if (country) {
  4913. country = [self countryNameByCountryCodeId:country];
  4914. } else {
  4915. country = @"";
  4916. }
  4917. DebugLog(@"country");
  4918. country = [self translateSingleQuote:country];
  4919. NSString *state = [params objectForKey:@"state"];
  4920. if (!state) {
  4921. state = @"";
  4922. }
  4923. DebugLog(@"state");
  4924. state = [self translateSingleQuote:state];
  4925. NSString *city = [params objectForKey:@"city"];
  4926. if (!city) {
  4927. city = @"";
  4928. }
  4929. city = [self translateSingleQuote:city];
  4930. NSString *zipcode = [params objectForKey:@"zipcode"];
  4931. if (!zipcode) {
  4932. zipcode = @"";
  4933. }
  4934. DebugLog(@"zip");
  4935. zipcode = [self translateSingleQuote:zipcode];
  4936. NSString *fistName = [params objectForKey:@"firstname"];
  4937. if (!fistName) {
  4938. fistName = @"";
  4939. }
  4940. NSString *lastName = [params objectForKey:@"lastname"];
  4941. if (!lastName) {
  4942. lastName = @"";
  4943. }
  4944. contact_name = [RAUtils arr2string:@[fistName,lastName] separator:@" " trim:true];
  4945. DebugLog(@"contact_name");
  4946. contact_name = [self translateSingleQuote:contact_name];
  4947. fistName = [self translateSingleQuote:fistName];
  4948. lastName = [self translateSingleQuote:lastName];
  4949. NSString *phone = [params objectForKey:@"phone"];
  4950. if (phone) {
  4951. phone = [AESCrypt fastencrypt:phone];
  4952. } else {
  4953. phone = @"";
  4954. }
  4955. DebugLog(@"PHONE");
  4956. phone = [self translateSingleQuote:phone];
  4957. NSString *fax = [params objectForKey:@"fax"];
  4958. if (!fax) {
  4959. fax = @"";
  4960. }
  4961. DebugLog(@"FAX");
  4962. fax = [self translateSingleQuote:fax];
  4963. NSString *email = [params objectForKey:@"email"];
  4964. if (!email) {
  4965. email = @"";
  4966. }
  4967. DebugLog(@"EMAIL:%@",email);
  4968. email = [self translateSingleQuote:email];
  4969. NSString *notes = [params objectForKey:@"contact_notes"];
  4970. if (!notes) {
  4971. notes = @"";
  4972. }
  4973. DebugLog(@"NOTE:%@",notes);
  4974. notes = [self translateSingleQuote:notes];
  4975. NSString *price = [params objectForKey:@"price_name"];
  4976. if (price) {
  4977. price = [self priceNameByPriceId:price];
  4978. } else {
  4979. price = @"";
  4980. }
  4981. DebugLog(@"PRICE");
  4982. price = [self translateSingleQuote:price];
  4983. NSString *salesRep = [params objectForKey:@"sales_rep"];
  4984. if (salesRep) {
  4985. salesRep = [self salesRepCodeById:salesRep];
  4986. } else {
  4987. salesRep = @"";
  4988. }
  4989. salesRep = [self translateSingleQuote:salesRep];
  4990. NSString *img = [params objectForKey:@"business_card"];
  4991. NSArray *array = [img componentsSeparatedByString:@","];
  4992. NSString *img_0 = array[0];
  4993. if (!img_0) {
  4994. img_0 = @"";
  4995. }
  4996. img_0 = [self translateSingleQuote:img_0];
  4997. NSString *img_1 = array[1];
  4998. if (!img_1) {
  4999. img_1 = @"";
  5000. }
  5001. img_1 = [self translateSingleQuote:img_1];
  5002. NSString *img_2 = array[2];
  5003. if (!img_2) {
  5004. img_2 = @"";
  5005. }
  5006. img_2 = [self translateSingleQuote:img_2];
  5007. NSString *contact_id = [NSUUID UUID].UUIDString;
  5008. NSString *contact_type = [params objectForKey:@"type_name"];
  5009. if (!contact_type) {
  5010. contact_type = @"";
  5011. }
  5012. contact_type = [self translateSingleQuote:contact_type];
  5013. // 判断更新时是否为customer
  5014. if (update) {
  5015. contact_id = [params objectForKey:@"contact_id"];
  5016. if (!contact_id) {
  5017. contact_id = @"";
  5018. }
  5019. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  5020. __block int customer = 0;
  5021. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5022. customer = sqlite3_column_int(stmt, 0);
  5023. }];
  5024. isCustomer = customer ? YES : NO;
  5025. }
  5026. NSMutableDictionary *sync_dic = [params mutableCopy];
  5027. if (isCustomer) {
  5028. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  5029. } else {
  5030. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  5031. }
  5032. NSString *sync_data = nil;
  5033. NSString *sql = nil;
  5034. if (update){
  5035. contact_id = [params objectForKey:@"contact_id"];
  5036. if (!contact_id) {
  5037. contact_id = @"";
  5038. }
  5039. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5040. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5041. sync_data = [RAUtils dict2string:sync_dic];
  5042. sync_data = [self translateSingleQuote:sync_data];
  5043. 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];
  5044. } else {
  5045. contact_id = [self translateSingleQuote:contact_id];
  5046. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5047. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5048. sync_data = [RAUtils dict2string:sync_dic];
  5049. sync_data = [self translateSingleQuote:sync_data];
  5050. 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];
  5051. }
  5052. int result = [iSalesDB execSql:sql];
  5053. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  5054. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  5055. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  5056. }
  5057. #pragma mark save new contact
  5058. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  5059. {
  5060. return [self offline_saveContact:params update:NO isCustomer:YES];
  5061. }
  5062. #pragma mark edit contact
  5063. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  5064. {
  5065. // {
  5066. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  5067. // password = 123456;
  5068. // user = EvanK;
  5069. // }
  5070. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  5071. NSData *data = [NSData dataWithContentsOfFile:path];
  5072. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5073. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5074. NSString *countryCode = nil;
  5075. NSString *countryCode_id = nil;
  5076. NSString *stateCode = nil;
  5077. /*------contact infor------*/
  5078. __block NSString *country = nil;
  5079. __block NSString *company_name = nil;
  5080. __block NSString *contact_id = params[@"contact_id"];
  5081. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  5082. __block NSString *zipcode = nil;
  5083. __block NSString *state = nil; // state_code
  5084. __block NSString *city = nil; //
  5085. __block NSString *firt_name,*last_name;
  5086. __block NSString *phone,*fax,*email;
  5087. __block NSString *notes,*price_type,*sales_rep;
  5088. __block NSString *img_0,*img_1,*img_2;
  5089. __block NSString *contact_type;
  5090. contact_id = [self translateSingleQuote:contact_id];
  5091. 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];
  5092. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5093. country = [self textAtColumn:0 statement:stmt]; // country name
  5094. company_name = [self textAtColumn:1 statement:stmt];
  5095. addr_1 = [self textAtColumn:2 statement:stmt];
  5096. addr_2 = [self textAtColumn:3 statement:stmt];
  5097. addr_3 = [self textAtColumn:4 statement:stmt];
  5098. addr_4 = [self textAtColumn:5 statement:stmt];
  5099. zipcode = [self textAtColumn:6 statement:stmt];
  5100. state = [self textAtColumn:7 statement:stmt]; // state code
  5101. city = [self textAtColumn:8 statement:stmt];
  5102. firt_name = [self textAtColumn:9 statement:stmt];
  5103. last_name = [self textAtColumn:10 statement:stmt];
  5104. phone = [self textAtColumn:11 statement:stmt];
  5105. fax = [self textAtColumn:12 statement:stmt];
  5106. email = [self textAtColumn:13 statement:stmt];
  5107. notes = [self textAtColumn:14 statement:stmt];
  5108. price_type = [self textAtColumn:15 statement:stmt]; // name
  5109. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  5110. img_0 = [self textAtColumn:17 statement:stmt];
  5111. img_1 = [self textAtColumn:18 statement:stmt];
  5112. img_2 = [self textAtColumn:19 statement:stmt];
  5113. contact_type = [self textAtColumn:20 statement:stmt];
  5114. }];
  5115. // decrypt
  5116. if (company_name) {
  5117. company_name = [AESCrypt fastdecrypt:company_name];
  5118. }
  5119. if (addr_1) {
  5120. addr_1 = [AESCrypt fastdecrypt:addr_1];
  5121. }
  5122. if (phone) {
  5123. phone = [AESCrypt fastdecrypt:phone];
  5124. }
  5125. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  5126. countryCode = [iSalesDB jk_queryText:countrySql];
  5127. stateCode = state;
  5128. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5129. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  5130. NSString *code_id = params[@"country"];
  5131. countryCode_id = code_id;
  5132. countryCode = [self countryCodeByid:code_id];
  5133. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  5134. NSString *zip_code = params[@"zipcode"];
  5135. // 剔除全部为空格
  5136. int spaceCount = 0;
  5137. for (int i = 0; i < zip_code.length; i++) {
  5138. if ([zip_code characterAtIndex:i] == ' ') {
  5139. spaceCount++;
  5140. }
  5141. }
  5142. if (spaceCount == zip_code.length) {
  5143. zip_code = @"";
  5144. }
  5145. if (zipcode.length > 0) {
  5146. zipcode = zip_code;
  5147. countryCode_id = params[@"country"];
  5148. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  5149. countryCode = [dic valueForKey:@"country_code"];
  5150. if (!countryCode) {
  5151. NSString *code_id = params[@"country"];
  5152. countryCode = [self countryCodeByid:code_id];
  5153. }
  5154. stateCode = [dic valueForKey:@"state_code"];
  5155. if (!stateCode.length) {
  5156. stateCode = params[@"state"];
  5157. }
  5158. city = [dic valueForKey:@"city"];
  5159. if (!city.length) {
  5160. city = params[@"city"];
  5161. }
  5162. // zip code
  5163. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  5164. [zipDic setValue:zipcode forKey:@"value"];
  5165. [section_0 setValue:zipDic forKey:@"item_8"];
  5166. } else {
  5167. NSString *code_id = params[@"country"];
  5168. countryCode = [self countryCodeByid:code_id];
  5169. stateCode = params[@"state"];
  5170. city = params[@"city"];
  5171. }
  5172. }
  5173. }
  5174. // 0 Country
  5175. // 1 Company Name
  5176. // 2 Contact ID
  5177. // 3 Picture
  5178. // 4 Address 1
  5179. // 5 Address 2
  5180. // 6 Address 3
  5181. // 7 Address 4
  5182. // 8 Zip Code
  5183. // 9 State/Province
  5184. // 10 City
  5185. // 11 Contact First Name
  5186. // 12 Contact Last Name
  5187. // 13 Phone
  5188. // 14 Fax
  5189. // 15 Email
  5190. // 16 Contact Notes
  5191. // 17 Price Type
  5192. // 18 Contact Type
  5193. // 19 Sales Rep
  5194. // country
  5195. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5196. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  5197. // company
  5198. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  5199. // contact_id
  5200. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  5201. // picture
  5202. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  5203. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  5204. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  5205. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  5206. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  5207. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  5208. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  5209. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  5210. // addr 1 2 3 4
  5211. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  5212. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  5213. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  5214. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  5215. // zip code
  5216. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  5217. // state
  5218. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5219. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  5220. // city
  5221. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  5222. // first last
  5223. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  5224. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  5225. // phone fax email
  5226. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  5227. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  5228. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  5229. // notes
  5230. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  5231. // price
  5232. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  5233. for (NSString *key in priceDic.allKeys) {
  5234. if ([key containsString:@"val_"]) {
  5235. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  5236. if ([dic[@"value"] isEqualToString:price_type]) {
  5237. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5238. [priceDic setValue:dic forKey:key];
  5239. }
  5240. }
  5241. }
  5242. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  5243. // Contact Rep
  5244. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  5245. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5246. // Sales Rep
  5247. NSMutableDictionary *repDic = [[self offline_getSalesRep] mutableCopy];
  5248. for (NSString *key in repDic.allKeys) {
  5249. if ([key containsString:@"val_"]) {
  5250. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  5251. NSString *value = dic[@"value"];
  5252. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  5253. if (code && [code isEqualToString:sales_rep]) {
  5254. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5255. [repDic setValue:dic forKey:key];
  5256. }
  5257. }
  5258. }
  5259. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  5260. [ret setValue:section_0 forKey:@"section_0"];
  5261. return [RAUtils dict2data:ret];
  5262. }
  5263. #pragma mark save contact
  5264. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  5265. {
  5266. return [self offline_saveContact:params update:YES isCustomer:YES];
  5267. }
  5268. #pragma mark category
  5269. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5270. if (ck) {
  5271. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  5272. for (NSString *key in res.allKeys) {
  5273. if (![key isEqualToString:@"count"]) {
  5274. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  5275. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5276. if ([val[@"value"] isEqualToString:ck]) {
  5277. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5278. }
  5279. [res setValue:val forKey:key];
  5280. }
  5281. }
  5282. [dic setValue:res forKey:valueKey];
  5283. }
  5284. }
  5285. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  5286. // NSString* orderCode = [params valueForKey:@"orderCode"];
  5287. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5288. NSString* category = [params valueForKey:@"category"];
  5289. if (!category || [category isEqualToString:@""]) {
  5290. category = @"%";
  5291. }
  5292. category = [self translateSingleQuote:category];
  5293. int limit = [[params valueForKey:@"limit"] intValue];
  5294. int offset = [[params valueForKey:@"offset"] intValue];
  5295. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5296. NSString *limit_str = @"";
  5297. if (limited) {
  5298. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  5299. }
  5300. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5301. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5302. sqlite3 *db = [iSalesDB get_db];
  5303. // [iSalesDB AddExFunction:db];
  5304. int count;
  5305. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  5306. 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];
  5307. double price_min = 0;
  5308. double price_max = 0;
  5309. if ([params.allKeys containsObject:@"alert"]) {
  5310. // alert
  5311. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5312. NSString *alert = params[@"alert"];
  5313. if ([alert isEqualToString:@"Display All"]) {
  5314. alert = [NSString stringWithFormat:@""];
  5315. } else {
  5316. alert = [self translateSingleQuote:alert];
  5317. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5318. }
  5319. // available
  5320. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5321. NSString *available = params[@"available"];
  5322. NSString *available_condition;
  5323. if ([available isEqualToString:@"Display All"]) {
  5324. available_condition = @"";
  5325. } else if ([available isEqualToString:@"Available Now"]) {
  5326. available_condition = @"and availability > 0";
  5327. } else {
  5328. available_condition = @"and availability == 0";
  5329. }
  5330. // best seller
  5331. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5332. NSString *best_seller = @"";
  5333. NSString *order_best_seller = @"m.name asc";
  5334. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5335. best_seller = @"and best_seller > 0";
  5336. order_best_seller = @"m.best_seller desc,m.name asc";
  5337. }
  5338. // price
  5339. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5340. NSString *price = params[@"price"];
  5341. price_min = 0;
  5342. price_max = MAXFLOAT;
  5343. if (appDelegate.user && price != nil) {
  5344. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  5345. NSMutableString *priceName = [NSMutableString string];
  5346. for (int i = 0; i < priceTypeArray.count; i++) {
  5347. NSString *pricetype = priceTypeArray[i];
  5348. pricetype = [self translateSingleQuote:pricetype];
  5349. if (i == 0) {
  5350. [priceName appendFormat:@"'%@'",pricetype];
  5351. } else {
  5352. [priceName appendFormat:@",'%@'",pricetype];
  5353. }
  5354. }
  5355. if ([price isEqualToString:@"Display All"]) {
  5356. price = [NSString stringWithFormat:@""];
  5357. } else if([price containsString:@"+"]){
  5358. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5359. price_min = [price doubleValue];
  5360. 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];
  5361. } else {
  5362. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5363. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5364. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5365. 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];
  5366. }
  5367. } else {
  5368. price = @"";
  5369. }
  5370. // sold_by_qty : Sold in quantities of %@
  5371. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5372. NSString *qty = params[@"sold_by_qty"];
  5373. if ([qty isEqualToString:@"Display All"]) {
  5374. qty = @"";
  5375. } else {
  5376. qty = [self translateSingleQuote:qty];
  5377. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5378. }
  5379. // cate
  5380. // category = [self translateSingleQuote:category];
  5381. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  5382. // cate mutiple selection
  5383. NSString *category_id = params[@"category"];
  5384. NSMutableArray *cate_id_array = nil;
  5385. NSMutableString *cateWhere = [NSMutableString string];
  5386. if ([category_id isEqualToString:@""] || !category_id) {
  5387. [cateWhere appendString:@"1 = 1"];
  5388. } else {
  5389. if ([category_id containsString:@","]) {
  5390. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  5391. } else {
  5392. cate_id_array = [@[category_id] mutableCopy];
  5393. }
  5394. [cateWhere appendString:@"("];
  5395. for (int i = 0; i < cate_id_array.count; i++) {
  5396. if (i == 0) {
  5397. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5398. } else {
  5399. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  5400. }
  5401. }
  5402. [cateWhere appendString:@")"];
  5403. }
  5404. // where bestseller > 0 order by bestseller desc
  5405. // sql query: alert availability(int) best_seller(int) price qty
  5406. 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 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];
  5407. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  5408. }
  5409. DebugLog(@"offline category where: %@",where);
  5410. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  5411. if (!appDelegate.user) {
  5412. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  5413. }
  5414. [ret setValue:filter forKey:@"filter"];
  5415. DebugLog(@"offline_category sql:%@",sqlQuery);
  5416. sqlite3_stmt * statement;
  5417. [ret setValue:@"2" forKey:@"result"];
  5418. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  5419. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  5420. // int count=0;
  5421. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5422. {
  5423. int i=0;
  5424. while (sqlite3_step(statement) == SQLITE_ROW)
  5425. {
  5426. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5427. char *name = (char*)sqlite3_column_text(statement, 0);
  5428. if(name==nil)
  5429. name="";
  5430. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5431. char *description = (char*)sqlite3_column_text(statement, 1);
  5432. if(description==nil)
  5433. description="";
  5434. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  5435. int product_id = sqlite3_column_int(statement, 2);
  5436. int wid = sqlite3_column_int(statement, 3);
  5437. int closeout = sqlite3_column_int(statement, 4);
  5438. int cid = sqlite3_column_int(statement, 5);
  5439. int wisdelete = sqlite3_column_int(statement, 6);
  5440. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  5441. int more_color = sqlite3_column_int(statement, 8);
  5442. // Defaul Category ID
  5443. __block NSString *categoryID = nil;
  5444. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  5445. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5446. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  5447. if(default_category==nil)
  5448. default_category="";
  5449. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  5450. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  5451. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  5452. categoryID = nsdefault_category;
  5453. }];
  5454. if (!categoryID.length) {
  5455. NSString *cateIDs = params[@"category"];
  5456. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  5457. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  5458. for (NSString *cateID in requestCategoryArr) {
  5459. BOOL needBreak = NO;
  5460. for (NSString *itemCateIDBox in itemCategoryArr) {
  5461. if (itemCateIDBox.length > 4) {
  5462. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  5463. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  5464. if ([itemCategoryID isEqualToString:cateID]) {
  5465. needBreak = YES;
  5466. categoryID = itemCategoryID;
  5467. break;
  5468. }
  5469. }
  5470. }
  5471. if (needBreak) {
  5472. break;
  5473. }
  5474. }
  5475. }
  5476. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  5477. if(wid !=0 && wisdelete != 1)
  5478. [item setValue:@"true" forKey:@"wish_exists"];
  5479. else
  5480. [item setValue:@"false" forKey:@"wish_exists"];
  5481. if(closeout==0)
  5482. [item setValue:@"false" forKey:@"is_closeout"];
  5483. else
  5484. [item setValue:@"true" forKey:@"is_closeout"];
  5485. if(cid==0)
  5486. [item setValue:@"false" forKey:@"cart_exists"];
  5487. else
  5488. [item setValue:@"true" forKey:@"cart_exists"];
  5489. if (more_color == 0) {
  5490. [item setObject:@(false) forKey:@"more_color"];
  5491. } else if (more_color == 1) {
  5492. [item setObject:@(true) forKey:@"more_color"];
  5493. }
  5494. [item addEntriesFromDictionary:imgjson];
  5495. // [item setValue:nsurl forKey:@"img"];
  5496. [item setValue:nsname forKey:@"name"];
  5497. [item setValue:nsdescription forKey:@"description"];
  5498. if (categoryID) {
  5499. [item setValue:categoryID forKey:@"item_category_id"];
  5500. }
  5501. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5502. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5503. i++;
  5504. }
  5505. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5506. [ret setObject:items forKey:@"items"];
  5507. sqlite3_finalize(statement);
  5508. } else {
  5509. DebugLog(@"nothing...");
  5510. }
  5511. DebugLog(@"count:%d",count);
  5512. [iSalesDB close_db:db];
  5513. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  5514. return ret;
  5515. }
  5516. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  5517. {
  5518. return [self categoryList:params limited:YES];
  5519. }
  5520. # pragma mark item search
  5521. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  5522. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5523. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  5524. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5525. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5526. // category
  5527. NSDictionary *category_menu = [self offline_category_menu];
  5528. [filter setValue:category_menu forKey:@"category"];
  5529. NSString* where= nil;
  5530. NSString* orderby= @"m.name";
  5531. if (!filterSearch) {
  5532. int covertype = [[params valueForKey:@"covertype"] intValue];
  5533. switch (covertype) {
  5534. case 0:
  5535. {
  5536. where=@"m.category like'%%#005#%%'";
  5537. break;
  5538. }
  5539. case 1:
  5540. {
  5541. where=@"m.alert like '%QS%'";
  5542. break;
  5543. }
  5544. case 2:
  5545. {
  5546. where=@"m.availability>0";
  5547. break;
  5548. }
  5549. case 3:
  5550. {
  5551. where=@"m.best_seller>0";
  5552. orderby=@"m.best_seller desc,m.name asc";
  5553. break;
  5554. }
  5555. default:
  5556. where=@"1=1";
  5557. break;
  5558. }
  5559. }
  5560. int limit = [[params valueForKey:@"limit"] intValue];
  5561. int offset = [[params valueForKey:@"offset"] intValue];
  5562. NSString *limit_str = @"";
  5563. if (limited) {
  5564. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  5565. }
  5566. sqlite3 *db = [iSalesDB get_db];
  5567. // [iSalesDB AddExFunction:db];
  5568. int count;
  5569. NSString *sqlQuery = nil;
  5570. where = [where stringByAppendingString:@" and m.is_active = 1"];
  5571. 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];
  5572. double price_min = 0;
  5573. double price_max = 0;
  5574. if (filterSearch) {
  5575. // alert
  5576. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5577. NSString *alert = params[@"alert"];
  5578. if ([alert isEqualToString:@"Display All"]) {
  5579. alert = [NSString stringWithFormat:@""];
  5580. } else {
  5581. alert = [self translateSingleQuote:alert];
  5582. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5583. }
  5584. // available
  5585. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5586. NSString *available = params[@"available"];
  5587. NSString *available_condition;
  5588. if ([available isEqualToString:@"Display All"]) {
  5589. available_condition = @"";
  5590. } else if ([available isEqualToString:@"Available Now"]) {
  5591. available_condition = @"and availability > 0";
  5592. } else {
  5593. available_condition = @"and availability == 0";
  5594. }
  5595. // best seller
  5596. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5597. NSString *best_seller = @"";
  5598. NSString *order_best_seller = @"m.name asc";
  5599. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5600. best_seller = @"and best_seller > 0";
  5601. order_best_seller = @"m.best_seller desc,m.name asc";
  5602. }
  5603. // price
  5604. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5605. NSString *price = params[@"price"];
  5606. price_min = 0;
  5607. price_max = MAXFLOAT;
  5608. if (appDelegate.user) {
  5609. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  5610. NSMutableString *priceName = [NSMutableString string];
  5611. for (int i = 0; i < priceTypeArray.count; i++) {
  5612. NSString *pricetype = priceTypeArray[i];
  5613. pricetype = [self translateSingleQuote:pricetype];
  5614. if (i == 0) {
  5615. [priceName appendFormat:@"'%@'",pricetype];
  5616. } else {
  5617. [priceName appendFormat:@",'%@'",pricetype];
  5618. }
  5619. }
  5620. if ([price isEqualToString:@"Display All"]) {
  5621. price = [NSString stringWithFormat:@""];
  5622. } else if([price containsString:@"+"]){
  5623. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5624. price_min = [price doubleValue];
  5625. 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];
  5626. } else {
  5627. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5628. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5629. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5630. 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];
  5631. }
  5632. } else {
  5633. price = @"";
  5634. }
  5635. // sold_by_qty : Sold in quantities of %@
  5636. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5637. NSString *qty = params[@"sold_by_qty"];
  5638. if ([qty isEqualToString:@"Display All"]) {
  5639. qty = @"";
  5640. } else {
  5641. qty = [self translateSingleQuote:qty];
  5642. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5643. }
  5644. // category
  5645. NSString *category_id = params[@"ctgId"];
  5646. NSMutableArray *cate_id_array = nil;
  5647. NSMutableString *cateWhere = [NSMutableString string];
  5648. if ([category_id isEqualToString:@""] || !category_id) {
  5649. [cateWhere appendString:@"1 = 1"];
  5650. } else {
  5651. if ([category_id containsString:@","]) {
  5652. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  5653. } else {
  5654. cate_id_array = [@[category_id] mutableCopy];
  5655. }
  5656. /*-----------*/
  5657. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  5658. [cateWhere appendString:@"("];
  5659. for (int i = 0; i < cate_id_array.count; i++) {
  5660. for (NSString *key0 in cateDic.allKeys) {
  5661. if ([key0 containsString:@"category_"]) {
  5662. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  5663. for (NSString *key1 in category0.allKeys) {
  5664. if ([key1 containsString:@"category_"]) {
  5665. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  5666. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5667. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  5668. cate_id_array[i] = [category1 objectForKey:@"id"];
  5669. if (i == 0) {
  5670. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5671. } else {
  5672. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  5673. }
  5674. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5675. [category0 setValue:category1 forKey:key1];
  5676. [cateDic setValue:category0 forKey:key0];
  5677. }
  5678. } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  5679. if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  5680. cate_id_array[i] = [category0 objectForKey:@"id"];
  5681. if (i == 0) {
  5682. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5683. } else {
  5684. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  5685. }
  5686. [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5687. [cateDic setValue:category0 forKey:key0];
  5688. }
  5689. }
  5690. }
  5691. }
  5692. }
  5693. }
  5694. [cateWhere appendString:@")"];
  5695. [filter setValue:cateDic forKey:@"category"];
  5696. }
  5697. // where bestseller > 0 order by bestseller desc
  5698. // sql query: alert availability(int) best_seller(int) price qty
  5699. 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 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];
  5700. // count
  5701. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  5702. }
  5703. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  5704. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  5705. if (!appDelegate.user) {
  5706. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  5707. }
  5708. [ret setValue:filter forKey:@"filter"];
  5709. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  5710. sqlite3_stmt * statement;
  5711. [ret setValue:@"2" forKey:@"result"];
  5712. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  5713. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  5714. // int count=0;
  5715. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5716. {
  5717. int i=0;
  5718. while (sqlite3_step(statement) == SQLITE_ROW)
  5719. {
  5720. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5721. char *name = (char*)sqlite3_column_text(statement, 0);
  5722. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5723. char *description = (char*)sqlite3_column_text(statement, 1);
  5724. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  5725. int product_id = sqlite3_column_int(statement, 2);
  5726. int wid = sqlite3_column_int(statement, 3);
  5727. int closeout = sqlite3_column_int(statement, 4);
  5728. int cid = sqlite3_column_int(statement, 5);
  5729. int wisdelete = sqlite3_column_int(statement, 6);
  5730. int more_color = sqlite3_column_int(statement, 7);
  5731. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  5732. if(wid !=0 && wisdelete != 1)
  5733. [item setValue:@"true" forKey:@"wish_exists"];
  5734. else
  5735. [item setValue:@"false" forKey:@"wish_exists"];
  5736. if(closeout==0)
  5737. [item setValue:@"false" forKey:@"is_closeout"];
  5738. else
  5739. [item setValue:@"true" forKey:@"is_closeout"];
  5740. if(cid==0)
  5741. [item setValue:@"false" forKey:@"cart_exists"];
  5742. else
  5743. [item setValue:@"true" forKey:@"cart_exists"];
  5744. if (more_color == 0) {
  5745. [item setObject:@(false) forKey:@"more_color"];
  5746. } else if (more_color == 1) {
  5747. [item setObject:@(true) forKey:@"more_color"];
  5748. }
  5749. [item addEntriesFromDictionary:imgjson];
  5750. // [item setValue:nsurl forKey:@"img"];
  5751. [item setValue:nsname forKey:@"fash_name"];
  5752. [item setValue:nsdescription forKey:@"description"];
  5753. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5754. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5755. i++;
  5756. }
  5757. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5758. [ret setObject:items forKey:@"items"];
  5759. sqlite3_finalize(statement);
  5760. }
  5761. [iSalesDB close_db:db];
  5762. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  5763. return ret;
  5764. }
  5765. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  5766. {
  5767. return [self itemsearch:params limited:YES];
  5768. }
  5769. #pragma mark order detail
  5770. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  5771. if (str1.length == 0) {
  5772. str1 = @"&nbsp";
  5773. }
  5774. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  5775. return str;
  5776. }
  5777. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  5778. {
  5779. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  5780. [fromformatter setDateFormat:from];
  5781. NSDate *date = [fromformatter dateFromString:datetime];
  5782. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  5783. [toformatter setDateFormat:to];
  5784. NSString * ret = [toformatter stringFromDate:date];
  5785. return ret;
  5786. }
  5787. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  5788. // 把毫秒去掉
  5789. if ([dateTime containsString:@"."]) {
  5790. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  5791. }
  5792. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  5793. formatter.dateFormat = formate;
  5794. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  5795. NSDate *date = [formatter dateFromString:dateTime];
  5796. formatter.dateFormat = newFormate;
  5797. NSString *result = [formatter stringFromDate:date];
  5798. return result ? result : @"";
  5799. }
  5800. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  5801. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  5802. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  5803. }
  5804. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  5805. {
  5806. DebugLog(@"offline oderdetail params: %@",params);
  5807. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5808. int orderId = [params[@"orderId"] intValue];
  5809. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  5810. // decrypt card number and card security code
  5811. // 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 ];
  5812. 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];
  5813. sqlite3 *db = [iSalesDB get_db];
  5814. sqlite3_stmt * statement;
  5815. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  5816. NSString *nssoid = @"";
  5817. NSString* orderinfo = @"";
  5818. NSString *moreInfo = @"";
  5819. double handlingFee = 0;
  5820. double payments_and_credist = 0;
  5821. double totalPrice = 0;
  5822. double shippingFee = 0;
  5823. double lift_gate = 0;
  5824. NSString *customer_contact = @"";
  5825. NSString *customer_email = @"";
  5826. NSString *customer_fax = @"";
  5827. NSString *customer_phone = @"";
  5828. NSString *customer_city = @"";
  5829. NSString *customer_state = @"";
  5830. NSString *customer_zipcode = @"";
  5831. NSString *customer_country = @"";
  5832. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  5833. {
  5834. if (sqlite3_step(statement) == SQLITE_ROW)
  5835. {
  5836. int order_id = sqlite3_column_int(statement, 0);
  5837. NSString *sign_url = [self textAtColumn:51 statement:statement];
  5838. ret[@"sign_url"] = sign_url;
  5839. customer_contact = [self textAtColumn:52 statement:statement];
  5840. customer_email = [self textAtColumn:53 statement:statement];
  5841. customer_phone = [self textAtColumn:54 statement:statement];
  5842. customer_fax = [self textAtColumn:55 statement:statement];
  5843. customer_city = [self textAtColumn:60 statement:statement];
  5844. customer_state = [self textAtColumn:61 statement:statement];
  5845. customer_zipcode = [self textAtColumn:62 statement:statement];
  5846. customer_country = [self textAtColumn:63 statement:statement];
  5847. int offline_edit=sqlite3_column_int(statement, 56);
  5848. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  5849. char *soid = (char*)sqlite3_column_text(statement, 1);
  5850. if(soid==nil)
  5851. soid= "";
  5852. nssoid= [[NSString alloc]initWithUTF8String:soid];
  5853. // so#
  5854. ret[@"so#"] = nssoid;
  5855. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  5856. if(poNumber==nil)
  5857. poNumber= "";
  5858. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  5859. char *create_time = (char*)sqlite3_column_text(statement, 3);
  5860. if(create_time==nil)
  5861. create_time= "";
  5862. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  5863. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  5864. int status = sqlite3_column_int(statement, 4);
  5865. int erpStatus = sqlite3_column_int(statement, 49);
  5866. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  5867. // status
  5868. if (status > 1 && status != 3) {
  5869. status = erpStatus;
  5870. } else if (status == 3) {
  5871. status = 15;
  5872. }
  5873. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  5874. ret[@"order_status"] = nsstatus;
  5875. char *company_name = (char*)sqlite3_column_text(statement, 5);
  5876. if(company_name==nil)
  5877. company_name= "";
  5878. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  5879. // company name
  5880. ret[@"company_name"] = nscompany_name;
  5881. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  5882. if(customer_contact==nil)
  5883. customer_contact= "";
  5884. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  5885. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  5886. if(addr_1==nil)
  5887. addr_1="";
  5888. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  5889. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  5890. if(addr_2==nil)
  5891. addr_2="";
  5892. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  5893. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  5894. if(addr_3==nil)
  5895. addr_3="";
  5896. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  5897. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  5898. if(addr_4==nil)
  5899. addr_4="";
  5900. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  5901. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  5902. [arr_addr addObject:nsaddr_1];
  5903. [arr_addr addObject:nsaddr_2];
  5904. [arr_addr addObject:nsaddr_3];
  5905. [arr_addr addObject:nsaddr_4];
  5906. [arr_addr addObject:[NSString stringWithFormat:@"%@,",customer_city]];
  5907. [arr_addr addObject:customer_state];
  5908. [arr_addr addObject:customer_zipcode];
  5909. [arr_addr addObject:customer_country];
  5910. NSString * customer_address = [RAUtils arr2string:arr_addr separator:@" " trim:true];
  5911. char *logist = (char*)sqlite3_column_text(statement, 11);
  5912. if(logist==nil)
  5913. logist= "";
  5914. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  5915. if (/*status == -11 || */status == 10 || status == 11) {
  5916. nslogist = [self textAtColumn:59 statement:statement];
  5917. };
  5918. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  5919. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  5920. shipping = @"Shipping To Be Quoted";
  5921. } else {
  5922. shippingFee = sqlite3_column_double(statement, 12);
  5923. }
  5924. // Shipping
  5925. ret[@"Shipping"] = shipping;
  5926. int have_lift_gate = sqlite3_column_int(statement, 17);
  5927. lift_gate = sqlite3_column_double(statement, 13);
  5928. // Liftgate Fee(No loading dock)
  5929. if (!have_lift_gate) {
  5930. lift_gate = 0;
  5931. }
  5932. ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  5933. if (sqlite3_column_int(statement, 57)) {
  5934. ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  5935. }
  5936. // if ([nslogist isEqualToString:@"WILL CALL"]) {
  5937. // [ret removeObjectForKey:@"Shipping"];
  5938. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  5939. // }
  5940. //
  5941. // if (have_lift_gate) {
  5942. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  5943. // }
  5944. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  5945. if(general_notes==nil)
  5946. general_notes= "";
  5947. NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  5948. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  5949. if(internal_notes==nil)
  5950. internal_notes= "";
  5951. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  5952. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  5953. if(payment_type==nil)
  5954. payment_type= "";
  5955. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  5956. // order info
  5957. orderinfo = [self textFileName:@"order_info.html"];
  5958. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  5959. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  5960. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  5961. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  5962. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  5963. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  5964. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  5965. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  5966. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  5967. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  5968. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  5969. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  5970. NSString *payment = nil;
  5971. if([nspayment_type isEqualToString:@"Credit Card"])
  5972. {
  5973. payment = [self textFileName:@"creditcardpayment.html"];
  5974. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  5975. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  5976. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  5977. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  5978. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  5979. NSString *card_type = [self textAtColumn:42 statement:statement];
  5980. if (card_type.length > 0) { // 显示星号
  5981. card_type = @"****";
  5982. }
  5983. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  5984. if (card_number.length > 0 && card_number.length > 4) {
  5985. for (int i = 0; i < card_number.length - 4; i++) {
  5986. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  5987. }
  5988. } else {
  5989. card_number = @"";
  5990. }
  5991. NSString *card_security_code = [self textAtColumn:44 statement:statement];
  5992. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  5993. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  5994. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  5995. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  5996. card_expiration = @"****";
  5997. }
  5998. NSString *card_city = [self textAtColumn:46 statement:statement];
  5999. NSString *card_state = [self textAtColumn:47 statement:statement];
  6000. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  6001. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  6002. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  6003. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  6004. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  6005. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  6006. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  6007. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  6008. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  6009. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  6010. }
  6011. else
  6012. {
  6013. payment=[self textFileName:@"normalpayment.html"];
  6014. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  6015. }
  6016. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  6017. ret[@"result"]= [NSNumber numberWithInt:2];
  6018. // more info
  6019. moreInfo = [self textFileName:@"more_info.html"];
  6020. /*****ship to******/
  6021. // ShipToCompany_or_&nbsp
  6022. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  6023. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  6024. NSString *shipToName = [self textAtColumn:19 statement:statement];
  6025. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  6026. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  6027. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  6028. /*****ship from******/
  6029. // ShipFromCompany_or_&nbsp
  6030. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  6031. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  6032. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  6033. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  6034. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  6035. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  6036. /*****freight to******/
  6037. // FreightBillToCompany_or_&nbsp
  6038. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  6039. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  6040. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  6041. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  6042. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  6043. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  6044. /*****merchandise to******/
  6045. // MerchandiseBillToCompany_or_&nbsp
  6046. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  6047. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  6048. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  6049. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  6050. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  6051. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  6052. /*****return to******/
  6053. // ReturnToCompany_or_&nbsp
  6054. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  6055. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  6056. NSString *returnToName = [self textAtColumn:31 statement:statement];
  6057. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  6058. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  6059. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  6060. //
  6061. // DebugLog(@"more info : %@",moreInfo);
  6062. // handling fee
  6063. handlingFee = sqlite3_column_double(statement, 33);
  6064. ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  6065. if (sqlite3_column_int(statement, 58)) {
  6066. ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  6067. }
  6068. //
  6069. // customer info
  6070. customerID = [self textAtColumn:36 statement:statement];
  6071. // mode
  6072. ret[@"mode"] = appDelegate.mode;
  6073. // model_count
  6074. ret[@"model_count"] = @(0);
  6075. }
  6076. sqlite3_finalize(statement);
  6077. }
  6078. [iSalesDB close_db:db];
  6079. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  6080. // "customer_email" = "Shui Hu";
  6081. // "customer_fax" = "";
  6082. // "customer_first_name" = F;
  6083. // "customer_last_name" = L;
  6084. // "customer_name" = ",da He Xiang Dong Liu A";
  6085. // "customer_phone" = "Hey Xuan Feng";
  6086. contactInfo[@"customer_phone"] = customer_phone;
  6087. contactInfo[@"customer_fax"] = customer_fax;
  6088. contactInfo[@"customer_email"] = customer_email;
  6089. NSString *first_name = @"";
  6090. NSString *last_name = @"";
  6091. if ([customer_contact isEqualToString:@""]) {
  6092. } else if ([customer_contact containsString:@" "]) {
  6093. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  6094. first_name = [customer_contact substringToIndex:first_space_index];
  6095. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  6096. }
  6097. contactInfo[@"customer_first_name"] = first_name;
  6098. contactInfo[@"customer_last_name"] = last_name;
  6099. ret[@"customerInfo"] = contactInfo;
  6100. // models
  6101. if (nssoid) {
  6102. __block double TotalCuft = 0;
  6103. __block double TotalWeight = 0;
  6104. __block int TotalCarton = 0;
  6105. __block double allItemPrice = 0;
  6106. 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];
  6107. sqlite3 *db1 = [iSalesDB get_db];
  6108. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6109. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  6110. int product_id = sqlite3_column_int(stmt, 0);
  6111. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  6112. int item_id = sqlite3_column_int(stmt, 7);
  6113. NSString* Price=nil;
  6114. if(str_price==nil)
  6115. {
  6116. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db1];
  6117. if(price==nil)
  6118. Price=@"No Price.";
  6119. else
  6120. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6121. }
  6122. else
  6123. {
  6124. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  6125. }
  6126. double discount = sqlite3_column_double(stmt, 2);
  6127. int item_count = sqlite3_column_int(stmt, 3);
  6128. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  6129. NSString *nsline_note=nil;
  6130. if(line_note!=nil)
  6131. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  6132. // char *name = (char*)sqlite3_column_text(stmt, 5);
  6133. // NSString *nsname = nil;
  6134. // if(name!=nil)
  6135. // nsname= [[NSString alloc]initWithUTF8String:name];
  6136. NSString *nsname = [self textAtColumn:5 statement:stmt];
  6137. // char *description = (char*)sqlite3_column_text(stmt, 6);
  6138. // NSString *nsdescription=nil;
  6139. // if(description!=nil)
  6140. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  6141. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  6142. // int stockUom = sqlite3_column_int(stmt, 8);
  6143. // int _id = sqlite3_column_int(stmt, 9);
  6144. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  6145. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  6146. double weight=[bsubtotaljson[@"weight"] doubleValue];
  6147. int carton=[bsubtotaljson[@"carton"] intValue];
  6148. TotalCuft += cuft;
  6149. TotalWeight += weight;
  6150. TotalCarton += carton;
  6151. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID];
  6152. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  6153. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  6154. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  6155. itemjson[@"note"]=nsline_note;
  6156. itemjson[@"origin_price"] = Price;
  6157. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  6158. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  6159. itemjson[@"order_item_status"] = @""; // 暂时不处理
  6160. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  6161. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  6162. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  6163. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6164. if(itemjson[@"combine"] != nil)
  6165. {
  6166. // int citem=0;
  6167. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  6168. for(int bc=0;bc<bcount;bc++)
  6169. {
  6170. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6171. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6172. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6173. subTotal += uprice * modulus * item_count;
  6174. }
  6175. }
  6176. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  6177. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  6178. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  6179. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  6180. allItemPrice += subTotal;
  6181. }];
  6182. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  6183. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  6184. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  6185. // payments/Credits
  6186. // payments_and_credist = sqlite3_column_double(statement, 34);
  6187. payments_and_credist = allItemPrice;
  6188. ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6189. // // total
  6190. // totalPrice = sqlite3_column_double(statement, 35);
  6191. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6192. } else {
  6193. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  6194. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  6195. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  6196. // payments/Credits
  6197. payments_and_credist = 0;
  6198. ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6199. }
  6200. // total
  6201. totalPrice = payments_and_credist + lift_gate + shippingFee + handlingFee;
  6202. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6203. ret[@"order_info"]= orderinfo;
  6204. ret[@"more_order_info"] = moreInfo;
  6205. return [RAUtils dict2data:ret];
  6206. }
  6207. #pragma mark order list
  6208. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  6209. NSString *nsstatus = @"";
  6210. switch (status) {
  6211. case 0:
  6212. {
  6213. nsstatus=@"Temp Order";
  6214. break;
  6215. }
  6216. case 1:
  6217. {
  6218. nsstatus=@"Saved Order";
  6219. break;
  6220. }
  6221. case 2: {
  6222. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  6223. break;
  6224. }
  6225. case 3:
  6226. case 15:
  6227. {
  6228. nsstatus=@"Cancelled";
  6229. break;
  6230. }
  6231. case 10:
  6232. {
  6233. nsstatus=@"Quote Submitted";
  6234. break;
  6235. }
  6236. case 11:
  6237. {
  6238. nsstatus=@"Sales Order Submitted";
  6239. break;
  6240. }
  6241. case 12:
  6242. {
  6243. nsstatus=@"Processing";
  6244. break;
  6245. }
  6246. case 13:
  6247. {
  6248. nsstatus=@"Shipped";
  6249. break;
  6250. }
  6251. case 14:
  6252. {
  6253. nsstatus=@"Closed";
  6254. break;
  6255. }
  6256. case -11:
  6257. {
  6258. nsstatus = @"Ready For Submit";
  6259. break;
  6260. }
  6261. default:
  6262. break;
  6263. }
  6264. return nsstatus;
  6265. }
  6266. + (double) orderTotalPrice:(NSString *)so_id db:(sqlite3 *)db1 close:(BOOL)close{
  6267. double total = 0;
  6268. __block double payments_and_credist = 0;
  6269. __block double allItemPrice = 0;
  6270. // sqlite3 *db1 = [iSalesDB get_db];
  6271. if (so_id) {
  6272. // 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];
  6273. 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];
  6274. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6275. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6276. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  6277. int product_id = sqlite3_column_int(stmt, 0);
  6278. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  6279. int discount = sqlite3_column_int(stmt, 2);
  6280. int item_count = sqlite3_column_int(stmt, 3);
  6281. // int item_id = sqlite3_column_int(stmt, 7);
  6282. int item_id = sqlite3_column_int(stmt, 4);
  6283. char *customer_cid = (char *)sqlite3_column_text(stmt, 5);
  6284. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  6285. NSString* Price=nil;
  6286. if(str_price==nil)
  6287. {
  6288. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db1];
  6289. if(price==nil)
  6290. Price=@"No Price.";
  6291. else
  6292. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6293. }
  6294. else
  6295. {
  6296. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  6297. }
  6298. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID];
  6299. itemjson[@"The unit price"]=Price;
  6300. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  6301. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  6302. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6303. if(itemjson[@"combine"] != nil)
  6304. {
  6305. // int citem=0;
  6306. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  6307. for(int bc=0;bc<bcount;bc++)
  6308. {
  6309. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6310. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6311. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6312. subTotal += uprice * modulus * item_count;
  6313. }
  6314. }
  6315. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  6316. allItemPrice += subTotal;
  6317. }];
  6318. payments_and_credist = allItemPrice;
  6319. } else {
  6320. // payments/Credits
  6321. payments_and_credist = 0;
  6322. }
  6323. // lift_gate handlingFee shippingFee
  6324. __block double lift_gate = 0;
  6325. __block double handlingFee = 0;
  6326. __block double shippingFee = 0;
  6327. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  6328. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6329. int have_lift_gate = sqlite3_column_int(stmt, 0);
  6330. if (have_lift_gate) {
  6331. lift_gate = sqlite3_column_double(stmt, 1);
  6332. }
  6333. handlingFee = sqlite3_column_double(stmt, 2);
  6334. shippingFee = sqlite3_column_double(stmt, 3);
  6335. }];
  6336. // total
  6337. total = payments_and_credist + lift_gate + shippingFee + handlingFee;
  6338. if (close) {
  6339. [iSalesDB close_db:db1];
  6340. }
  6341. return total;
  6342. }
  6343. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  6344. {
  6345. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  6346. int limit = [[params valueForKey:@"limit"] intValue];
  6347. int offset = [[params valueForKey:@"offset"] intValue];
  6348. NSString* keyword = [params valueForKey:@"keyWord"];
  6349. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  6350. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  6351. NSString* where=@"1 = 1";
  6352. if(keyword.length>0)
  6353. 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]];
  6354. if (orderStatus.length > 0) {
  6355. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  6356. if (order_status_array.count == 1) {
  6357. int status_value = [[order_status_array firstObject] integerValue];
  6358. if (status_value <= 1 || status_value == 3) {
  6359. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  6360. } else {
  6361. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  6362. }
  6363. } else if (order_status_array.count > 1) {
  6364. for (int i = 0; i < order_status_array.count;i++) {
  6365. NSString *status = order_status_array[i];
  6366. NSString *condition = @" or";
  6367. if (i == 0) {
  6368. condition = @" and (";
  6369. }
  6370. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  6371. int status_value = [status integerValue];
  6372. if (status_value <= 1 || status_value == 3) {
  6373. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  6374. } else {
  6375. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  6376. }
  6377. }
  6378. where = [where stringByAppendingString:@" )"];
  6379. }
  6380. }
  6381. 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];
  6382. // DebugLog(@"order list sql: %@",sqlQuery);
  6383. sqlite3 *db = [iSalesDB get_db];
  6384. sqlite3_stmt * statement;
  6385. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  6386. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6387. {
  6388. int count=0;
  6389. while (sqlite3_step(statement) == SQLITE_ROW)
  6390. {
  6391. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  6392. int order_id = sqlite3_column_double(statement, 0);
  6393. char *soid = (char*)sqlite3_column_text(statement, 1);
  6394. if(soid==nil)
  6395. soid= "";
  6396. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  6397. int status = sqlite3_column_double(statement, 2);
  6398. int erpStatus = sqlite3_column_double(statement, 9);
  6399. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  6400. if(sales_rep==nil)
  6401. sales_rep= "";
  6402. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  6403. char *create_by = (char*)sqlite3_column_text(statement, 4);
  6404. if(create_by==nil)
  6405. create_by= "";
  6406. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  6407. char *company_name = (char*)sqlite3_column_text(statement, 5);
  6408. if(company_name==nil)
  6409. company_name= "";
  6410. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  6411. char *create_time = (char*)sqlite3_column_text(statement, 6);
  6412. if(create_time==nil)
  6413. create_time= "";
  6414. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  6415. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  6416. // double total_price = sqlite3_column_double(statement, 7);
  6417. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] db:db close:NO];
  6418. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  6419. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  6420. int offline_edit = sqlite3_column_int(statement, 10);
  6421. // ": "JH",
  6422. // "": "$8307.00",
  6423. // "": "MOB1608050001",
  6424. // "": "ArpithaT",
  6425. // "": "1st Stage Property Transformations",
  6426. // "": "JANICE SUTTON",
  6427. // "": 2255,
  6428. // "": "08/02/2016 09:49:18",
  6429. // "": 1,
  6430. // "": "Saved Order"
  6431. // "": "1470384050483",
  6432. // "model_count": "6 / 28"
  6433. item[@"sales_rep"]= nssales_rep;
  6434. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  6435. item[@"so#"]= nssoid;
  6436. item[@"create_by"]= nscreate_by;
  6437. item[@"customer_name"]= nscompany_name;
  6438. item[@"customer_contact"] = customer_contact;
  6439. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  6440. item[@"purchase_time"]= nscreate_time;
  6441. int statusCode = status;
  6442. if (statusCode == 2) {
  6443. statusCode = erpStatus;
  6444. } else if (statusCode == 3) {
  6445. statusCode = 15;
  6446. }
  6447. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",statusCode];
  6448. item[@"order_status"]= nsstatus;
  6449. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  6450. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  6451. // item[@"model_count"]
  6452. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  6453. count++;
  6454. }
  6455. ret[@"count"]= [NSNumber numberWithInt:count];
  6456. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  6457. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  6458. ret[@"result"]= [NSNumber numberWithInt:2];
  6459. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  6460. ret[@"time_zone"] = @"PST";
  6461. sqlite3_finalize(statement);
  6462. }
  6463. 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];
  6464. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  6465. [iSalesDB execSql:@"END TRANSACTION" db:db];
  6466. [iSalesDB close_db:db];
  6467. return [RAUtils dict2data:ret];
  6468. }
  6469. #pragma mark update gnotes
  6470. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  6471. {
  6472. DebugLog(@"params: %@",params);
  6473. // comments = Meyoyoyoyoyoyoy;
  6474. // orderCode = MOB1608110001;
  6475. // password = 123456;
  6476. // user = EvanK;
  6477. 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]];
  6478. int ret = [iSalesDB execSql:sql];
  6479. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6480. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6481. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6482. // [dic setValue:@"160409" forKey:@"min_ver"];
  6483. return [RAUtils dict2data:dic];
  6484. }
  6485. #pragma mark move to wishlist
  6486. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  6487. {
  6488. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  6489. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6490. _id = [NSString stringWithFormat:@"(%@)",_id];
  6491. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  6492. sqlite3 *db = [iSalesDB get_db];
  6493. __block NSString *product_id = @"";
  6494. __block NSString *item_count_str = @"";
  6495. // __block NSString *item_id = nil;
  6496. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6497. // product_id = [self textAtColumn:0 statement:stmt];
  6498. int item_count = sqlite3_column_int(stmt, 1);
  6499. // item_id = [self textAtColumn:2 statement:stmt];
  6500. NSString *p_id = [self textAtColumn:0 statement:stmt];
  6501. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  6502. if (p_id.length) {
  6503. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  6504. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  6505. }
  6506. }];
  6507. [iSalesDB close_db:db];
  6508. // 去除第一个,
  6509. if (product_id.length > 1) {
  6510. product_id = [product_id substringFromIndex:1];
  6511. }
  6512. if (item_count_str.length > 1) {
  6513. item_count_str = [item_count_str substringFromIndex:1];
  6514. }
  6515. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6516. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  6517. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  6518. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  6519. sqlite3 *db1 = [iSalesDB get_db];
  6520. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  6521. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6522. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  6523. // 删除
  6524. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  6525. int ret = [iSalesDB execSql:deleteSql db:db1];
  6526. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6527. [iSalesDB close_db:db1];
  6528. return [RAUtils dict2data:dic];
  6529. }
  6530. #pragma mark cart delete
  6531. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  6532. {
  6533. // cartItemId = 548;
  6534. // orderCode = MOB1608110001;
  6535. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  6536. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6537. _id = [NSString stringWithFormat:@"(%@)",_id];
  6538. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  6539. int ret = [iSalesDB execSql:sql];
  6540. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6541. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6542. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6543. // [dic setValue:@"160409" forKey:@"min_ver"];
  6544. return [RAUtils dict2data:dic];
  6545. }
  6546. #pragma mark set price
  6547. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  6548. {
  6549. DebugLog(@"cart set price params: %@",params);
  6550. // "cartitem_id" = 1;
  6551. // discount = "0.000000";
  6552. // "item_note" = "";
  6553. // price = "269.000000";
  6554. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6555. NSString *notes = [self valueInParams:params key:@"item_note"];
  6556. NSString *discount = [self valueInParams:params key:@"discount"];
  6557. NSString *price = [self valueInParams:params key:@"price"];
  6558. // bool badd_price_changed=false;
  6559. // sqlite3* db=[iSalesDB get_db];
  6560. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6561. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  6562. // NSRange range;
  6563. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  6564. //
  6565. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  6566. // badd_price_changed=true;
  6567. // [iSalesDB close_db:db];
  6568. //
  6569. // if(badd_price_changed)
  6570. // {
  6571. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  6572. // }
  6573. //
  6574. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  6575. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  6576. int ret = [iSalesDB execSql:sql];
  6577. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6578. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6579. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6580. // [dic setValue:@"160409" forKey:@"min_ver"];
  6581. return [RAUtils dict2data:dic];
  6582. }
  6583. #pragma mark set line notes
  6584. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  6585. {
  6586. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6587. NSString *notes = [self valueInParams:params key:@"notes"];
  6588. notes = [self translateSingleQuote:notes];
  6589. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  6590. int ret = [iSalesDB execSql:sql];
  6591. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6592. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6593. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6594. // [dic setValue:@"160409" forKey:@"min_ver"];
  6595. return [RAUtils dict2data:dic];
  6596. }
  6597. #pragma mark set qty
  6598. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  6599. {
  6600. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6601. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6602. int item_count = [params[@"inputInt"] intValue];
  6603. // 购买检查数量大于库存
  6604. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6605. if (!appDelegate.can_create_backorder) {
  6606. 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];
  6607. __block BOOL out_of_stock = NO;
  6608. [iSalesDB jk_query:checkSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6609. int availability = sqlite3_column_int(stmt, 0);
  6610. if (availability < item_count) {
  6611. out_of_stock = YES;
  6612. }
  6613. }];
  6614. if (out_of_stock) { // 缺货
  6615. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"result"];
  6616. [dic setObject:@"Item is out of stock" forKey:@"err_msg"];
  6617. return [RAUtils dict2data:dic];
  6618. }
  6619. }
  6620. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  6621. int ret = [iSalesDB execSql:sql];
  6622. __block int item_id = 0;
  6623. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6624. item_id = sqlite3_column_int(stmt, 0);
  6625. }];
  6626. sqlite3 *db = [iSalesDB get_db];
  6627. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  6628. [iSalesDB close_db:db];
  6629. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6630. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6631. // [dic setValue:@"160409" forKey:@"min_ver"];
  6632. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  6633. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  6634. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  6635. return [RAUtils dict2data:dic];
  6636. }
  6637. #pragma mark place order
  6638. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  6639. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6640. if (aname) {
  6641. [dic setValue:aname forKey:@"aname"];
  6642. }
  6643. if (control) {
  6644. [dic setValue:control forKey:@"control"];
  6645. }
  6646. if (keyboard) {
  6647. [dic setValue:keyboard forKey:@"keyboard"];
  6648. }
  6649. if (name) {
  6650. [dic setValue:name forKey:@"name"];
  6651. }
  6652. if (value) {
  6653. [dic setValue:value forKey:@"value"];
  6654. }
  6655. return dic;
  6656. }
  6657. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  6658. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6659. orderCode = [self translateSingleQuote:orderCode];
  6660. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  6661. 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];
  6662. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6663. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  6664. NSString *img0 = [self textAtColumn:1 statement:stmt];
  6665. NSString *img1 = [self textAtColumn:2 statement:stmt];
  6666. NSString *img2 = [self textAtColumn:3 statement:stmt];
  6667. NSString *company_name = [self textAtColumn:4 statement:stmt];
  6668. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  6669. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  6670. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  6671. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  6672. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  6673. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  6674. NSString *email = [self textAtColumn:11 statement:stmt];
  6675. NSString *phone = [self textAtColumn:12 statement:stmt];
  6676. NSString *fax = [self textAtColumn:13 statement:stmt];
  6677. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  6678. NSString *city = [self textAtColumn:15 statement:stmt];
  6679. NSString *state = [self textAtColumn:16 statement:stmt];
  6680. NSString *country = [self textAtColumn:17 statement:stmt];
  6681. NSString *name = [self textAtColumn:18 statement:stmt];
  6682. // contact id
  6683. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  6684. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  6685. [contact_id_dic setValue:@"text" forKey:@"control"];
  6686. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  6687. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  6688. [contact_id_dic setValue:@"true" forKey:@"required"];
  6689. [contact_id_dic setValue:contact_id forKey:@"value"];
  6690. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  6691. // business card
  6692. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  6693. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  6694. [business_card_dic setValue:@"img" forKey:@"control"];
  6695. [business_card_dic setValue:@"1" forKey:@"disable"];
  6696. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  6697. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  6698. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  6699. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  6700. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  6701. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  6702. [business_card_dic setValue:@"business_card" forKey:@"name"];
  6703. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  6704. // fax
  6705. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6706. [customer_dic setValue:fax_dic forKey:@"item_10"];
  6707. // zipcode
  6708. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  6709. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  6710. // city
  6711. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  6712. [customer_dic setValue:city_dic forKey:@"item_12"];
  6713. // state
  6714. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  6715. [customer_dic setValue:state_dic forKey:@"item_13"];
  6716. // country
  6717. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  6718. [customer_dic setValue:country_dic forKey:@"item_14"];
  6719. // company name
  6720. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  6721. [customer_dic setValue:company_dic forKey:@"item_2"];
  6722. // addr_1
  6723. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  6724. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  6725. // addr_2
  6726. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  6727. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  6728. // addr_3
  6729. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  6730. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  6731. // addr_4
  6732. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  6733. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  6734. // Contact
  6735. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  6736. [customer_dic setValue:contact_dic forKey:@"item_7"];
  6737. // email
  6738. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6739. [customer_dic setValue:email_dic forKey:@"item_8"];
  6740. // phone
  6741. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6742. [customer_dic setValue:phone_dic forKey:@"item_9"];
  6743. // title
  6744. [customer_dic setValue:@"Customer" forKey:@"title"];
  6745. // count
  6746. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  6747. }];
  6748. // setting
  6749. NSDictionary *setting = params[@"setting"];
  6750. NSNumber *hide = setting[@"CustomerHide"];
  6751. [customer_dic setValue:hide forKey:@"hide"];
  6752. return customer_dic;
  6753. }
  6754. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  6755. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6756. // setting
  6757. NSDictionary *setting = params[@"setting"];
  6758. NSNumber *hide = setting[@"ShipToHide"];
  6759. [dic setValue:hide forKey:@"hide"];
  6760. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6761. orderCode = [self translateSingleQuote:orderCode];
  6762. 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];
  6763. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6764. NSString *cid = [self textAtColumn:0 statement:stmt];
  6765. NSString *name = [self textAtColumn:1 statement:stmt];
  6766. NSString *ext = [self textAtColumn:2 statement:stmt];
  6767. NSString *contact = [self textAtColumn:3 statement:stmt];
  6768. NSString *email = [self textAtColumn:4 statement:stmt];
  6769. NSString *fax = [self textAtColumn:5 statement:stmt];
  6770. NSString *phone = [self textAtColumn:6 statement:stmt];
  6771. // count
  6772. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6773. // title
  6774. [dic setValue:@"Ship To" forKey:@"title"];
  6775. // choose
  6776. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6777. [choose_dic setValue:@"choose" forKey:@"aname"];
  6778. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6779. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  6780. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  6781. @"key_map" : @{@"receive_cid" : @"customer_cid",
  6782. @"receive_contact" : @"customer_contact",
  6783. @"receive_email" : @"customer_email",
  6784. @"receive_ext" : @"customer_contact_ext",
  6785. @"receive_fax" : @"customer_fax",
  6786. @"receive_name" : @"customer_name",
  6787. @"receive_phone" : @"customer_phone"},
  6788. @"refresh" : [NSNumber numberWithInteger:1],
  6789. @"type" : @"pull"};
  6790. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  6791. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  6792. @"name" : @"Add new address",
  6793. @"refresh" : [NSNumber numberWithInteger:1],
  6794. @"value" : @"new_addr"
  6795. };
  6796. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  6797. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  6798. @"key_map" : @{@"receive_cid" : @"customer_cid",
  6799. @"receive_contact" : @"customer_contact",
  6800. @"receive_email" : @"customer_email",
  6801. @"receive_ext" : @"customer_contact_ext",
  6802. @"receive_fax" : @"customer_fax",
  6803. @"receive_name" : @"customer_name",
  6804. @"receive_phone" : @"customer_phone"},
  6805. @"name" : @"select_ship_to",
  6806. @"refresh" : [NSNumber numberWithInteger:1],
  6807. @"value" : @"Sales_Order_Ship_To"};
  6808. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  6809. [dic setValue:choose_dic forKey:@"item_0"];
  6810. // contact id
  6811. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6812. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6813. [contact_id_dic setValue:@"true" forKey:@"required"];
  6814. [dic setValue:contact_id_dic forKey:@"item_1"];
  6815. // company name
  6816. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6817. [dic setValue:company_name_dic forKey:@"item_2"];
  6818. // address
  6819. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  6820. NSNumber *required = setting[@"ShippingToAddressRequire"];
  6821. if ([required integerValue]) {
  6822. [ext_dic setValue:@"true" forKey:@"required"];
  6823. }
  6824. [dic setValue:ext_dic forKey:@"item_3"];
  6825. // contact
  6826. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6827. [dic setValue:contact_dic forKey:@"item_4"];
  6828. // phone
  6829. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6830. [dic setValue:phone_dic forKey:@"item_5"];
  6831. // fax
  6832. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6833. [dic setValue:fax_dic forKey:@"item_6"];
  6834. // email
  6835. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6836. [dic setValue:email_dic forKey:@"item_7"];
  6837. }];
  6838. return dic;
  6839. }
  6840. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  6841. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6842. // setting
  6843. NSDictionary *setting = params[@"setting"];
  6844. NSNumber *hide = setting[@"ShipFromHide"];
  6845. [dic setValue:hide forKey:@"hide"];
  6846. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6847. orderCode = [self translateSingleQuote:orderCode];
  6848. 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];
  6849. 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';";
  6850. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6851. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  6852. __block NSString *name = [self textAtColumn:1 statement:stmt];
  6853. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  6854. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  6855. __block NSString *email = [self textAtColumn:4 statement:stmt];
  6856. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  6857. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  6858. if (!cid.length) {
  6859. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  6860. cid = [self textAtColumn:0 statement:statment];
  6861. name = [self textAtColumn:1 statement:statment];
  6862. ext = [self textAtColumn:2 statement:statment];
  6863. contact = [self textAtColumn:3 statement:statment];
  6864. email = [self textAtColumn:4 statement:statment];
  6865. fax = [self textAtColumn:5 statement:statment];
  6866. phone = [self textAtColumn:6 statement:statment];
  6867. }];
  6868. }
  6869. // count
  6870. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6871. // title
  6872. [dic setValue:@"Ship From" forKey:@"title"];
  6873. // hide
  6874. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6875. // choose
  6876. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6877. [choose_dic setValue:@"choose" forKey:@"aname"];
  6878. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6879. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6880. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  6881. @"key_map" : @{@"sender_cid" : @"customer_cid",
  6882. @"sender_contact" : @"customer_contact",
  6883. @"sender_email" : @"customer_email",
  6884. @"sender_ext" : @"customer_contact_ext",
  6885. @"sender_fax" : @"customer_fax",
  6886. @"sender_name" : @"customer_name",
  6887. @"sender_phone" : @"customer_phone"},
  6888. @"name" : @"select_cid",
  6889. @"refresh" : [NSNumber numberWithInteger:0],
  6890. @"value" : @"Sales_Order_Ship_From"};
  6891. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  6892. [dic setValue:choose_dic forKey:@"item_0"];
  6893. // contact id
  6894. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6895. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6896. [contact_id_dic setValue:@"true" forKey:@"required"];
  6897. [dic setValue:contact_id_dic forKey:@"item_1"];
  6898. // company name
  6899. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6900. [dic setValue:company_name_dic forKey:@"item_2"];
  6901. // address
  6902. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  6903. [dic setValue:ext_dic forKey:@"item_3"];
  6904. // contact
  6905. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6906. [dic setValue:contact_dic forKey:@"item_4"];
  6907. // phone
  6908. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6909. [dic setValue:phone_dic forKey:@"item_5"];
  6910. // fax
  6911. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6912. [dic setValue:fax_dic forKey:@"item_6"];
  6913. // email
  6914. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6915. [dic setValue:email_dic forKey:@"item_7"];
  6916. }];
  6917. return dic;
  6918. }
  6919. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  6920. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6921. // setting
  6922. NSDictionary *setting = params[@"setting"];
  6923. NSNumber *hide = setting[@"FreightBillToHide"];
  6924. [dic setValue:hide forKey:@"hide"];
  6925. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6926. orderCode = [self translateSingleQuote:orderCode];
  6927. 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];
  6928. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6929. NSString *cid = [self textAtColumn:0 statement:stmt];
  6930. NSString *name = [self textAtColumn:1 statement:stmt];
  6931. NSString *ext = [self textAtColumn:2 statement:stmt];
  6932. NSString *contact = [self textAtColumn:3 statement:stmt];
  6933. NSString *email = [self textAtColumn:4 statement:stmt];
  6934. NSString *fax = [self textAtColumn:5 statement:stmt];
  6935. NSString *phone = [self textAtColumn:6 statement:stmt];
  6936. // count
  6937. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6938. // title
  6939. [dic setValue:@"Freight Bill To" forKey:@"title"];
  6940. // hide
  6941. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6942. // choose
  6943. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6944. [choose_dic setValue:@"choose" forKey:@"aname"];
  6945. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6946. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  6947. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  6948. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  6949. @"shipping_billto_contact" : @"receive_contact",
  6950. @"shipping_billto_email" : @"receive_email",
  6951. @"shipping_billto_ext" : @"receive_ext",
  6952. @"shipping_billto_fax" : @"receive_fax",
  6953. @"shipping_billto_name" : @"receive_name",
  6954. @"shipping_billto_phone" : @"receive_phone"},
  6955. @"type" : @"pull"};
  6956. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  6957. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  6958. @"type" : @"pull",
  6959. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  6960. @"shipping_billto_contact" : @"customer_contact",
  6961. @"shipping_billto_email" : @"customer_email",
  6962. @"shipping_billto_ext" : @"customer_contact_ext",
  6963. @"shipping_billto_fax" : @"customer_fax",
  6964. @"shipping_billto_name" : @"customer_name",
  6965. @"shipping_billto_phone" : @"customer_phone"}
  6966. };
  6967. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  6968. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  6969. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  6970. @"shipping_billto_contact" : @"sender_contact",
  6971. @"shipping_billto_email" : @"sender_email",
  6972. @"shipping_billto_ext" : @"sender_ext",
  6973. @"shipping_billto_fax" : @"sender_fax",
  6974. @"shipping_billto_name" : @"sender_name",
  6975. @"shipping_billto_phone" : @"sender_phone"},
  6976. @"type" : @"pull"
  6977. };
  6978. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  6979. NSDictionary *select_freight_bill_to_dic = @{
  6980. @"aname" : @"Select freight bill to",
  6981. @"name" : @"select_cid",
  6982. @"refresh" : [NSNumber numberWithInteger:0],
  6983. @"value" : @"Sales_Order_Freight_Bill_To",
  6984. @"key_map" : @{
  6985. @"shipping_billto_cid" : @"customer_cid",
  6986. @"shipping_billto_contact" : @"customer_contact",
  6987. @"shipping_billto_email" : @"customer_email",
  6988. @"shipping_billto_ext" : @"customer_contact_ext",
  6989. @"shipping_billto_fax" : @"customer_fax",
  6990. @"shipping_billto_name" : @"customer_name",
  6991. @"shipping_billto_phone" : @"customer_phone"
  6992. }
  6993. };
  6994. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  6995. [dic setValue:choose_dic forKey:@"item_0"];
  6996. // contact id
  6997. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6998. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6999. [contact_id_dic setValue:@"true" forKey:@"required"];
  7000. [dic setValue:contact_id_dic forKey:@"item_1"];
  7001. // company name
  7002. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7003. [dic setValue:company_name_dic forKey:@"item_2"];
  7004. // address
  7005. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7006. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  7007. if ([ext_required integerValue]) {
  7008. [ext_dic setValue:@"true" forKey:@"required"];
  7009. }
  7010. [dic setValue:ext_dic forKey:@"item_3"];
  7011. // contact
  7012. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7013. [dic setValue:contact_dic forKey:@"item_4"];
  7014. // phone
  7015. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7016. [dic setValue:phone_dic forKey:@"item_5"];
  7017. // fax
  7018. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7019. [dic setValue:fax_dic forKey:@"item_6"];
  7020. // email
  7021. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7022. [dic setValue:email_dic forKey:@"item_7"];
  7023. }];
  7024. return dic;
  7025. }
  7026. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7027. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7028. // setting
  7029. NSDictionary *setting = params[@"setting"];
  7030. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  7031. [dic setValue:hide forKey:@"hide"];
  7032. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7033. orderCode = [self translateSingleQuote:orderCode];
  7034. 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];
  7035. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7036. NSString *cid = [self textAtColumn:0 statement:stmt];
  7037. NSString *name = [self textAtColumn:1 statement:stmt];
  7038. NSString *ext = [self textAtColumn:2 statement:stmt];
  7039. NSString *contact = [self textAtColumn:3 statement:stmt];
  7040. NSString *email = [self textAtColumn:4 statement:stmt];
  7041. NSString *fax = [self textAtColumn:5 statement:stmt];
  7042. NSString *phone = [self textAtColumn:6 statement:stmt];
  7043. // count
  7044. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7045. // title
  7046. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  7047. // hide
  7048. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7049. // choose
  7050. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7051. [choose_dic setValue:@"choose" forKey:@"aname"];
  7052. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7053. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  7054. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  7055. @"key_map" : @{@"billing_cid" : @"receive_cid",
  7056. @"billing_contact" : @"receive_contact",
  7057. @"billing_email" : @"receive_email",
  7058. @"billing_ext" : @"receive_ext",
  7059. @"billing_fax" : @"receive_fax",
  7060. @"billing_name" : @"receive_name",
  7061. @"billing_phone" : @"receive_phone"},
  7062. @"type" : @"pull"};
  7063. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  7064. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7065. @"type" : @"pull",
  7066. @"key_map" : @{@"billing_cid" : @"customer_cid",
  7067. @"billing_contact" : @"customer_contact",
  7068. @"billing_email" : @"customer_email",
  7069. @"billing_ext" : @"customer_contact_ext",
  7070. @"billing_fax" : @"customer_fax",
  7071. @"billing_name" : @"customer_name",
  7072. @"billing_phone" : @"customer_phone"}
  7073. };
  7074. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  7075. NSDictionary *select_bill_to_dic = @{
  7076. @"aname" : @"Select bill to",
  7077. @"name" : @"select_cid",
  7078. @"refresh" : [NSNumber numberWithInteger:0],
  7079. @"value" : @"Sales_Order_Merchandise_Bill_To",
  7080. @"key_map" : @{
  7081. @"billing_cid" : @"customer_cid",
  7082. @"billing_contact" : @"customer_contact",
  7083. @"billing_email" : @"customer_email",
  7084. @"billing_ext" : @"customer_contact_ext",
  7085. @"billing_fax" : @"customer_fax",
  7086. @"billing_name" : @"customer_name",
  7087. @"billing_phone" : @"customer_phone"
  7088. }
  7089. };
  7090. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  7091. [dic setValue:choose_dic forKey:@"item_0"];
  7092. // contact id
  7093. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7094. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7095. [contact_id_dic setValue:@"true" forKey:@"required"];
  7096. [dic setValue:contact_id_dic forKey:@"item_1"];
  7097. // company name
  7098. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7099. [dic setValue:company_name_dic forKey:@"item_2"];
  7100. // address
  7101. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7102. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  7103. if ([ext_required integerValue]) {
  7104. [ext_dic setValue:@"true" forKey:@"required"];
  7105. }
  7106. [dic setValue:ext_dic forKey:@"item_3"];
  7107. // contact
  7108. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7109. [dic setValue:contact_dic forKey:@"item_4"];
  7110. // phone
  7111. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7112. [dic setValue:phone_dic forKey:@"item_5"];
  7113. // fax
  7114. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7115. [dic setValue:fax_dic forKey:@"item_6"];
  7116. // email
  7117. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7118. [dic setValue:email_dic forKey:@"item_7"];
  7119. }];
  7120. return dic;
  7121. }
  7122. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7123. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7124. // setting
  7125. NSDictionary *setting = params[@"setting"];
  7126. NSNumber *hide = setting[@"ReturnToHide"];
  7127. [dic setValue:hide forKey:@"hide"];
  7128. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7129. orderCode = [self translateSingleQuote:orderCode];
  7130. 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];
  7131. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7132. NSString *cid = [self textAtColumn:0 statement:stmt];
  7133. NSString *name = [self textAtColumn:1 statement:stmt];
  7134. NSString *ext = [self textAtColumn:2 statement:stmt];
  7135. NSString *contact = [self textAtColumn:3 statement:stmt];
  7136. NSString *email = [self textAtColumn:4 statement:stmt];
  7137. NSString *fax = [self textAtColumn:5 statement:stmt];
  7138. NSString *phone = [self textAtColumn:6 statement:stmt];
  7139. // count
  7140. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7141. // title
  7142. [dic setValue:@"Return To" forKey:@"title"];
  7143. // hide
  7144. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7145. // choose
  7146. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7147. [choose_dic setValue:@"choose" forKey:@"aname"];
  7148. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7149. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  7150. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  7151. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  7152. @"returnto_contact" : @"sender_contact",
  7153. @"returnto_email" : @"sender_email",
  7154. @"returnto_ext" : @"sender_ext",
  7155. @"returnto_fax" : @"sender_fax",
  7156. @"returnto_name" : @"sender_name",
  7157. @"returnto_phone" : @"sender_phone"},
  7158. @"type" : @"pull"};
  7159. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  7160. NSDictionary *select_return_to_dic = @{
  7161. @"aname" : @"Select return to",
  7162. @"name" : @"select_cid",
  7163. @"refresh" : [NSNumber numberWithInteger:0],
  7164. @"value" : @"Contact_Return_To",
  7165. @"key_map" : @{
  7166. @"returnto_cid" : @"customer_cid",
  7167. @"returnto_contact" : @"customer_contact",
  7168. @"returnto_email" : @"customer_email",
  7169. @"returnto_ext" : @"customer_contact_ext",
  7170. @"returnto_fax" : @"customer_fax",
  7171. @"returnto_name" : @"customer_name",
  7172. @"returnto_phone" : @"customer_phone"
  7173. }
  7174. };
  7175. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  7176. [dic setValue:choose_dic forKey:@"item_0"];
  7177. // contact id
  7178. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7179. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7180. [contact_id_dic setValue:@"true" forKey:@"required"];
  7181. [dic setValue:contact_id_dic forKey:@"item_1"];
  7182. // company name
  7183. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7184. [dic setValue:company_name_dic forKey:@"item_2"];
  7185. // address
  7186. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  7187. [dic setValue:ext_dic forKey:@"item_3"];
  7188. // contact
  7189. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7190. [dic setValue:contact_dic forKey:@"item_4"];
  7191. // phone
  7192. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7193. [dic setValue:phone_dic forKey:@"item_5"];
  7194. // fax
  7195. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7196. [dic setValue:fax_dic forKey:@"item_6"];
  7197. // email
  7198. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7199. [dic setValue:email_dic forKey:@"item_7"];
  7200. }];
  7201. return dic;
  7202. }
  7203. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  7204. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7205. orderCode = [self translateSingleQuote:orderCode];
  7206. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7207. __block double TotalCuft = 0;
  7208. __block double TotalWeight = 0;
  7209. __block int TotalCarton = 0;
  7210. __block double payments = 0;
  7211. // setting
  7212. NSDictionary *setting = params[@"setting"];
  7213. NSNumber *hide = setting[@"ModelInformationHide"];
  7214. [dic setValue:hide forKey:@"hide"];
  7215. 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];
  7216. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7217. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7218. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  7219. // item id
  7220. int item_id = sqlite3_column_int(stmt, 0);
  7221. // count
  7222. int item_count = sqlite3_column_int(stmt, 1);
  7223. // stockUom
  7224. int stockUom = sqlite3_column_int(stmt, 2);
  7225. // unit price
  7226. NSString *str_price = [self textAtColumn:3 statement:stmt];
  7227. NSString* Price=nil;
  7228. if([str_price isEqualToString:@""])
  7229. {
  7230. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  7231. if(price==nil)
  7232. Price=@"No Price.";
  7233. else
  7234. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7235. }
  7236. else
  7237. {
  7238. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  7239. }
  7240. // discount
  7241. double discount = sqlite3_column_double(stmt, 4);
  7242. // name
  7243. NSString *name = [self textAtColumn:5 statement:stmt];
  7244. // description
  7245. NSString *description = [self textAtColumn:6 statement:stmt];
  7246. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  7247. // line note
  7248. NSString *line_note = [self textAtColumn:7 statement:stmt];
  7249. int avaulability = sqlite3_column_int(stmt, 8);
  7250. // img
  7251. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  7252. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  7253. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7254. if(combine != nil)
  7255. {
  7256. // int citem=0;
  7257. int bcount=[[combine valueForKey:@"count"] intValue];
  7258. for(int bc=0;bc<bcount;bc++)
  7259. {
  7260. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7261. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7262. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7263. subTotal += uprice * modulus * item_count;
  7264. }
  7265. }
  7266. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  7267. [model_dic setValue:@"model" forKey:@"control"];
  7268. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  7269. [model_dic setValue:description forKey:@"description"];
  7270. [model_dic setValue:line_note forKey:@"note"];
  7271. [model_dic setValue:img forKey:@"img_url"];
  7272. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  7273. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  7274. [model_dic setValue:Price forKey:@"unit_price"];
  7275. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  7276. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  7277. if (combine) {
  7278. [model_dic setValue:combine forKey:@"combine"];
  7279. }
  7280. // well,what under the row is the info for total
  7281. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  7282. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  7283. double weight=[bsubtotaljson[@"weight"] doubleValue];
  7284. int carton=[bsubtotaljson[@"carton"] intValue];
  7285. TotalCuft += cuft;
  7286. TotalWeight += weight;
  7287. TotalCarton += carton;
  7288. payments += subTotal;
  7289. //---------------------------
  7290. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  7291. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  7292. }];
  7293. [dic setValue:@"Model Information" forKey:@"title"];
  7294. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  7295. return dic;
  7296. }
  7297. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  7298. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7299. [dic setValue:@"Remarks Content" forKey:@"title"];
  7300. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  7301. // setting
  7302. NSDictionary *setting = params[@"setting"];
  7303. NSNumber *hide = setting[@"RemarksContentHide"];
  7304. [dic setValue:hide forKey:@"hide"];
  7305. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7306. orderCode = [self translateSingleQuote:orderCode];
  7307. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes from offline_order where so_id = '%@';",orderCode];
  7308. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7309. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  7310. int mustCall = sqlite3_column_int(stmt, 1);
  7311. NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  7312. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  7313. NSDictionary *po_dic = @{
  7314. @"aname" : @"PO#",
  7315. @"control" : @"edit",
  7316. @"keyboard" : @"text",
  7317. @"name" : @"poNumber",
  7318. @"value" : poNumber
  7319. };
  7320. NSDictionary *must_call_dic = @{
  7321. @"aname" : @"MUST CALL BEFORE DELIVERY",
  7322. @"control" : @"switch",
  7323. @"name" : @"must_call",
  7324. @"value" : mustCall ? @"true" : @"false"
  7325. };
  7326. NSDictionary *general_notes_dic = @{
  7327. @"aname" : @"General notes",
  7328. @"control" : @"text_view",
  7329. @"keyboard" : @"text",
  7330. @"name" : @"comments",
  7331. @"value" : generalNotes
  7332. };
  7333. NSDictionary *internal_notes_dic = @{
  7334. @"aname" : @"Internal notes",
  7335. @"control" : @"text_view",
  7336. @"keyboard" : @"text",
  7337. @"name" : @"internal_notes",
  7338. @"value" : internalNotes
  7339. };
  7340. [dic setValue:po_dic forKey:@"item_0"];
  7341. [dic setValue:must_call_dic forKey:@"item_1"];
  7342. [dic setValue:general_notes_dic forKey:@"item_2"];
  7343. [dic setValue:internal_notes_dic forKey:@"item_3"];
  7344. }];
  7345. return dic;
  7346. }
  7347. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  7348. // params
  7349. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7350. orderCode = [self translateSingleQuote:orderCode];
  7351. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7352. // setting
  7353. NSDictionary *setting = params[@"setting"];
  7354. NSNumber *hide = setting[@"OrderTotalHide"];
  7355. [dic setValue:hide forKey:@"hide"];
  7356. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  7357. __block double lift_gate_value = 0;
  7358. __block double handling_fee = 0;
  7359. __block double shipping = 0;
  7360. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7361. int lift_gate = sqlite3_column_int(stmt, 0);
  7362. lift_gate_value = sqlite3_column_double(stmt, 1);
  7363. shipping = sqlite3_column_double(stmt, 2);
  7364. handling_fee = sqlite3_column_double(stmt, 3);
  7365. if (!lift_gate) {
  7366. lift_gate_value = 0;
  7367. }
  7368. }];
  7369. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  7370. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  7371. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  7372. double payments = [[total valueForKey:@"payments"] doubleValue];
  7373. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  7374. double totalPrice = payments;
  7375. [dic setValue:@"Order Total" forKey:@"title"];
  7376. NSDictionary *payments_dic = @{
  7377. @"align" : @"right",
  7378. @"aname" : @"Payments/Credits",
  7379. @"control" : @"text",
  7380. @"name" : @"paymentsAndCredits",
  7381. @"type" : @"price",
  7382. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  7383. };
  7384. [dic setValue:payments_dic forKey:@"item_0"];
  7385. // version 1.71 remove
  7386. // NSDictionary *handling_fee_dic = @{
  7387. // @"align" : @"right",
  7388. // @"aname" : @"Handling Fee",
  7389. // @"control" : @"text",
  7390. // @"name" : @"handling_fee_value",
  7391. // @"required" : @"true",
  7392. // @"type" : @"price",
  7393. // @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  7394. // };
  7395. NSDictionary *shipping_dic = @{
  7396. @"align" : @"right",
  7397. @"aname" : @"Shipping*",
  7398. @"control" : @"text",
  7399. @"name" : @"shipping",
  7400. @"required" : @"true",
  7401. @"type" : @"price",
  7402. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  7403. };
  7404. NSDictionary *lift_gate_dic = @{
  7405. @"align" : @"right",
  7406. @"aname" : @"Liftgate Fee(No Loading Dock)",
  7407. @"control" : @"text",
  7408. @"name" : @"lift_gate_value",
  7409. @"required" : @"true",
  7410. @"type" : @"price",
  7411. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  7412. };
  7413. int item_count = 1;
  7414. if (![[params objectForKey:@"logist"] isEqualToString:@"WILL CALL"]) {
  7415. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  7416. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  7417. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7418. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7419. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  7420. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7421. } else {
  7422. }
  7423. }
  7424. // version 1.71 remove
  7425. // [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7426. NSDictionary *total_price_dic = @{
  7427. @"align" : @"right",
  7428. @"aname" : @"Total",
  7429. @"control" : @"text",
  7430. @"name" : @"totalPrice",
  7431. @"type" : @"price",
  7432. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  7433. };
  7434. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7435. NSDictionary *total_cuft_dic = @{
  7436. @"align" : @"right",
  7437. @"aname" : @"Total Cuft",
  7438. @"control" : @"text",
  7439. @"name" : @"",
  7440. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  7441. };
  7442. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7443. NSDictionary *total_weight_dic = @{
  7444. @"align" : @"right",
  7445. @"aname" : @"Total Weight",
  7446. @"control" : @"text",
  7447. @"name" : @"",
  7448. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  7449. };
  7450. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7451. NSDictionary *total_carton_dic = @{
  7452. @"align" : @"right",
  7453. @"aname" : @"Total Carton",
  7454. @"control" : @"text",
  7455. @"name" : @"",
  7456. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  7457. };
  7458. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7459. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  7460. return dic;
  7461. }
  7462. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  7463. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7464. orderCode = [self translateSingleQuote:orderCode];
  7465. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7466. // setting
  7467. NSDictionary *setting = params[@"setting"];
  7468. NSNumber *hide = setting[@"SignatureHide"];
  7469. [dic setValue:hide forKey:@"hide"];
  7470. [dic setValue:@"Signature" forKey:@"title"];
  7471. __block NSString *pic_path = @"";
  7472. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  7473. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7474. pic_path = [self textAtColumn:0 statement:stmt];
  7475. }];
  7476. NSDictionary *pic_dic = @{
  7477. @"aname" : @"Signature",
  7478. @"avalue" :pic_path,
  7479. @"control" : @"signature",
  7480. @"name" : @"sign_picpath",
  7481. @"value" : pic_path
  7482. };
  7483. [dic setValue:pic_dic forKey:@"item_0"];
  7484. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7485. return dic;
  7486. }
  7487. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  7488. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7489. // setting
  7490. NSDictionary *setting = params[@"setting"];
  7491. NSNumber *hide = setting[@"ShippingMethodHide"];
  7492. [dic setValue:hide forKey:@"hide"];
  7493. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7494. orderCode = [self translateSingleQuote:orderCode];
  7495. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  7496. __block NSString *logist = @"";
  7497. __block NSString *lift_gate = @"";
  7498. __block int lift_gate_integer = 0;
  7499. __block NSString *logistic_note = @"";
  7500. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7501. logist = [self textAtColumn:0 statement:stmt];
  7502. lift_gate_integer = sqlite3_column_int(stmt, 1);
  7503. logistic_note = [self textAtColumn:2 statement:stmt];
  7504. }];
  7505. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7506. [dic setValue:@"Shipping Method" forKey:@"title"];
  7507. // val_0
  7508. int PERSONAL_PICK_UP_check = 0;
  7509. int USE_MY_CARRIER_check = 0;
  7510. NSString *logistic_note_text = @"";
  7511. int will_call_check = 0;
  7512. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  7513. will_call_check = 1;
  7514. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  7515. PERSONAL_PICK_UP_check = 1;
  7516. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  7517. USE_MY_CARRIER_check = 1;
  7518. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  7519. logistic_note = [logistic_note_array firstObject];
  7520. if (logistic_note_array.count > 1) {
  7521. logistic_note_text = [logistic_note_array lastObject];
  7522. }
  7523. }
  7524. }
  7525. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  7526. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  7527. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  7528. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  7529. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  7530. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7531. NSDictionary *val0_subItem_item0 = @{
  7532. @"aname" : @"Option",
  7533. @"cadedate" : @{
  7534. @"count" : [NSNumber numberWithInteger:2],
  7535. @"val_0" : @{
  7536. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  7537. @"refresh" : [NSNumber numberWithInteger:0],
  7538. @"value" : @"PERSONAL PICK UP",
  7539. @"value_id" : @"PERSONAL PICK UP"
  7540. },
  7541. @"val_1" : @{
  7542. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  7543. @"refresh" : [NSNumber numberWithInteger:0],
  7544. @"sub_item" : @{
  7545. @"count" : [NSNumber numberWithInteger:1],
  7546. @"item_0" : @{
  7547. @"aname" : @"BOL",
  7548. @"control" : @"edit",
  7549. @"keyboard" : @"text",
  7550. @"name" : @"logist_note_text",
  7551. @"refresh" : [NSNumber numberWithInteger:0],
  7552. @"required" : @"false",
  7553. @"value" : logistic_note_text
  7554. }
  7555. },
  7556. @"value" : @"USE MY CARRIER",
  7557. @"value_id" : @"USE MY CARRIER"
  7558. }
  7559. },
  7560. @"control" : @"enum",
  7561. @"name" : @"logistic_note",
  7562. @"required" : @"true",
  7563. @"single_select" : @"true"
  7564. };
  7565. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  7566. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  7567. // val_1
  7568. lift_gate = [self valueInParams:params key:@"lift_gate"];
  7569. if([lift_gate isEqualToString:@""]) {
  7570. if (lift_gate_integer == 1) {
  7571. lift_gate = @"true";
  7572. } else {
  7573. lift_gate = @"false";
  7574. }
  7575. }
  7576. int common_carrier_check = 0;
  7577. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  7578. common_carrier_check = 1;
  7579. [params setValue:lift_gate forKey:@"lift_gate"];
  7580. }
  7581. NSDictionary *val_1 = @{
  7582. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  7583. @"sub_item" : @{
  7584. @"count" : [NSNumber numberWithInteger:1],
  7585. @"item_0" : @{
  7586. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  7587. @"control" : @"switch",
  7588. @"name" : @"lift_gate",
  7589. @"refresh" : [NSNumber numberWithInteger:1],
  7590. @"required" : @"true",
  7591. @"value" : lift_gate
  7592. }
  7593. },
  7594. @"value" : @"COMMON CARRIER",
  7595. @"value_id" : @"COMMON CARRIER"
  7596. };
  7597. // cadedate
  7598. NSDictionary *cadedate = @{
  7599. @"count" : [NSNumber numberWithInteger:2],
  7600. @"val_0" : val_0,
  7601. @"val_1" : val_1
  7602. };
  7603. // item_0
  7604. NSMutableDictionary *item_0 = @{
  7605. @"aname" : @"Shipping",
  7606. @"cadedate" : cadedate,
  7607. @"control" : @"enum",
  7608. @"name" : @"logist",
  7609. @"refresh" : [NSNumber numberWithInteger:1],
  7610. @"single_select" : @"true",
  7611. }.mutableCopy;
  7612. NSNumber *required = setting[@"ShippingMethodRequire"];
  7613. if ([required integerValue]) {
  7614. [item_0 setValue:@"true" forKey:@"required"];
  7615. }
  7616. [dic setValue:item_0 forKey:@"item_0"];
  7617. if ([logist isEqualToString:@"WILL CALL"]) {
  7618. [dic removeObjectForKey:@"lift_gate"];
  7619. }
  7620. return dic;
  7621. }
  7622. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  7623. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7624. orderCode = [self translateSingleQuote:orderCode];
  7625. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  7626. __block int submit_as_integer = 0;
  7627. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7628. submit_as_integer = sqlite3_column_int(stmt, 0);
  7629. }];
  7630. int check11 = 0;
  7631. int check10 = 0;
  7632. if (submit_as_integer == 11) {
  7633. check11 = 1;
  7634. }
  7635. if (submit_as_integer == 10) {
  7636. check10 = 1;
  7637. }
  7638. // section_0
  7639. NSMutableDictionary *dic = @{
  7640. @"count" : @(1),
  7641. @"item_0" : @{
  7642. @"aname" : @"Submit Order As",
  7643. @"cadedate" : @{
  7644. @"count" : @(2),
  7645. @"val_0" : @{
  7646. @"check" : [NSNumber numberWithInteger:check11],
  7647. @"value" : @"Sales Order",
  7648. @"value_id" : @(11)
  7649. },
  7650. @"val_1" : @{
  7651. @"check" : [NSNumber numberWithInteger:check10],
  7652. @"value" : @"Quote",
  7653. @"value_id" : @(10)
  7654. }
  7655. },
  7656. @"control" : @"enum",
  7657. @"name" : @"erpOrderStatus",
  7658. @"required" : @"true",
  7659. @"single_select" : @"true"
  7660. },
  7661. @"title" : @"Order Type"
  7662. }.mutableCopy;
  7663. // setting
  7664. NSDictionary *setting = params[@"setting"];
  7665. NSNumber *hide = setting[@"OrderTypeHide"];
  7666. [dic setValue:hide forKey:@"hide"];
  7667. return dic;
  7668. }
  7669. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  7670. countryCode = [self translateSingleQuote:countryCode];
  7671. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  7672. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7673. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  7674. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  7675. if (name == NULL) {
  7676. name = "";
  7677. }
  7678. if (code == NULL) {
  7679. code = "";
  7680. }
  7681. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  7682. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  7683. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  7684. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  7685. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  7686. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  7687. }
  7688. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  7689. [container setValue:stateDic forKey:key];
  7690. }] mutableCopy];
  7691. [ret removeObjectForKey:@"result"];
  7692. // failure 可以不用了,一样的
  7693. if (ret.allKeys.count == 0) {
  7694. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  7695. [stateDic setValue:@"Other" forKey:@"value"];
  7696. [stateDic setValue:@"" forKey:@"value_id"];
  7697. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  7698. if (state_code && [@"" isEqualToString:state_code]) {
  7699. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  7700. }
  7701. NSString *key = [NSString stringWithFormat:@"val_0"];
  7702. [ret setValue:stateDic forKey:key];
  7703. }
  7704. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  7705. return ret;
  7706. }
  7707. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  7708. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7709. orderCode = [self translateSingleQuote:orderCode];
  7710. 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];
  7711. __block NSString *payType = @"";
  7712. __block NSString *firstName = @"";
  7713. __block NSString *lastName = @"";
  7714. __block NSString *addr1 = @"";
  7715. __block NSString *addr2 = @"";
  7716. __block NSString *zipcode = @"";
  7717. __block NSString *cardType = @"";
  7718. __block NSString *cardNumber = @"";
  7719. __block NSString *securityCode = @"";
  7720. __block NSString *month = @"";
  7721. __block NSString *year = @"";
  7722. __block NSString *city = @"";
  7723. __block NSString *state = @"";
  7724. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7725. payType = [self textAtColumn:0 statement:stmt];
  7726. firstName = [self textAtColumn:1 statement:stmt];
  7727. lastName = [self textAtColumn:2 statement:stmt];
  7728. addr1 = [self textAtColumn:3 statement:stmt];
  7729. addr2 = [self textAtColumn:4 statement:stmt];
  7730. zipcode = [self textAtColumn:5 statement:stmt];
  7731. cardType = [self textAtColumn:6 statement:stmt];
  7732. cardNumber = [self textAtColumn:7 statement:stmt];
  7733. securityCode = [self textAtColumn:8 statement:stmt];
  7734. month = [self textAtColumn:9 statement:stmt];
  7735. year = [self textAtColumn:10 statement:stmt];
  7736. city = [self textAtColumn:11 statement:stmt];
  7737. state = [self textAtColumn:12 statement:stmt];
  7738. }];
  7739. NSString *required = @"true";
  7740. // setting
  7741. NSDictionary *setting = params[@"setting"];
  7742. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  7743. if ([requiredNumber integerValue]) {
  7744. required = @"true";
  7745. } else {
  7746. required = @"false";
  7747. }
  7748. // "section_2"
  7749. NSMutableDictionary *dic = @{
  7750. @"count" : @(1),
  7751. @"item_0" : @{
  7752. @"aname" : @"Payment",
  7753. @"required" : required,
  7754. @"cadedate" : @{
  7755. @"count" : @(6),
  7756. @"val_0" : @{
  7757. @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  7758. @"value" : @"Check",
  7759. @"value_id" : @"Check"
  7760. },
  7761. @"val_1" : @{
  7762. @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  7763. @"value" : @"Cash",
  7764. @"value_id" : @"Cash"
  7765. },
  7766. @"val_2" : @{
  7767. @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  7768. @"value" : @"NET 30",
  7769. @"value_id" : @"NET 30"
  7770. },
  7771. @"val_3" : @{
  7772. @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  7773. @"value" : @"Wire Transfer",
  7774. @"value_id" : @"Wire Transfer"
  7775. },
  7776. @"val_4" : @{
  7777. @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  7778. @"sub_item" : @{
  7779. @"count" : @(3),
  7780. @"item_0" : @{
  7781. @"aname" : @"choose",
  7782. @"control" : @"multi_action",
  7783. @"count" : @(1),
  7784. @"item_0" : @{
  7785. @"aname" : @"Same as customer",
  7786. @"key_map" : @{
  7787. @"credit_card_address1" : @"customer_address1",
  7788. @"credit_card_address2" : @"customer_address2",
  7789. @"credit_card_city" : @"customer_city",
  7790. @"credit_card_first_name" : @"customer_first_name",
  7791. @"credit_card_last_name" : @"customer_last_name",
  7792. @"credit_card_state" : @"customer_state",
  7793. @"credit_card_zipcode" : @"customer_zipcode"
  7794. },
  7795. @"type" : @"pull"
  7796. }
  7797. },
  7798. @"item_1" : @{
  7799. @"aname" : @"",
  7800. @"color" : @"red",
  7801. @"control" : @"text",
  7802. @"name" : @"",
  7803. @"value" : @"USA Credit cards only"
  7804. },
  7805. @"item_2" : @{
  7806. @"aname" : @"Fill",
  7807. @"cadedate" : @{
  7808. @"count" : @(2),
  7809. @"val_0" : @{
  7810. @"check" : @(1),
  7811. @"sub_item" : @{
  7812. @"count" : @(11),
  7813. @"item_0" : @{
  7814. @"aname" : @"Type",
  7815. @"cadedate" : @{
  7816. @"count" : @(2),
  7817. @"val_0" : @{
  7818. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  7819. @"value" : @"VISA",
  7820. @"value_id" : @(0)
  7821. },
  7822. @"val_1" : @{
  7823. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  7824. @"value" : @"MASTER CARD",
  7825. @"value_id" : @(1)
  7826. }
  7827. },
  7828. @"control" : @"enum",
  7829. @"name" : @"credit_card_type",
  7830. @"required" : @"true",
  7831. @"single_select" : @"true"
  7832. },
  7833. @"item_1" : @{
  7834. @"aname" : @"Number",
  7835. @"control" : @"edit",
  7836. @"keyboard" : @"int",
  7837. @"length" : @"16",
  7838. @"name" : @"credit_card_number",
  7839. @"required" : @"true",
  7840. @"value" : cardNumber
  7841. },
  7842. @"item_10" : @{
  7843. @"aname" : @"State",
  7844. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  7845. @"control" : @"enum",
  7846. @"enum" : @"true",
  7847. @"name" : @"credit_card_state",
  7848. @"required" : @"true",
  7849. @"single_select" : @"true"
  7850. },
  7851. @"item_2" : @{
  7852. @"aname" : @"Expiration Date",
  7853. @"control" : @"monthpicker",
  7854. @"name" : @"credit_card_expiration",
  7855. @"required" : @"true",
  7856. @"type" : @"date",
  7857. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  7858. },
  7859. @"item_3" : @{
  7860. @"aname" : @"Security Code",
  7861. @"control" : @"edit",
  7862. @"keyboard" : @"int",
  7863. @"length" : @"3",
  7864. @"name" : @"credit_card_security_code",
  7865. @"required" : @"true",
  7866. @"value" : securityCode
  7867. },
  7868. @"item_4" : @{
  7869. @"aname" : @"First Name",
  7870. @"control" : @"edit",
  7871. @"keyboard" : @"text",
  7872. @"name" : @"credit_card_first_name",
  7873. @"required" : @"true",
  7874. @"value" : firstName
  7875. },
  7876. @"item_5" : @{
  7877. @"aname" : @"Last Name",
  7878. @"control" : @"edit",
  7879. @"keyboard" : @"text",
  7880. @"name" : @"credit_card_last_name",
  7881. @"required" : @"true",
  7882. @"value" : lastName
  7883. },
  7884. @"item_6" : @{
  7885. @"aname" : @"Address 1",
  7886. @"control" : @"edit",
  7887. @"keyboard" : @"text",
  7888. @"name" : @"credit_card_address1",
  7889. @"required" : @"true",
  7890. @"value" : addr1
  7891. },
  7892. @"item_7" : @{
  7893. @"aname" : @"Address 2",
  7894. @"control" : @"edit",
  7895. @"keyboard" : @"text",
  7896. @"name" : @"credit_card_address2",
  7897. @"value" : addr2
  7898. },
  7899. @"item_8" : @{
  7900. @"aname" : @"zip code",
  7901. @"control" : @"edit",
  7902. @"keyboard" : @"text",
  7903. @"name" : @"credit_card_zipcode",
  7904. @"required" : @"true",
  7905. @"value" : zipcode
  7906. },
  7907. @"item_9" : @{
  7908. @"aname" : @"City",
  7909. @"control" : @"edit",
  7910. @"keyboard" : @"text",
  7911. @"name" : @"credit_card_city",
  7912. @"required" : @"true",
  7913. @"value" : city
  7914. }
  7915. },
  7916. @"value" : @"Fill Now",
  7917. @"value_id" : @""
  7918. },
  7919. @"val_1" : @{
  7920. @"check" : @(0),
  7921. @"value" : @"Fill Later",
  7922. @"value_id" : @""
  7923. }
  7924. },
  7925. @"control" : @"enum",
  7926. @"name" : @"",
  7927. @"single_select" : @"true"
  7928. }
  7929. },
  7930. @"value" : @"Credit Card",
  7931. @"value_id" : @"Credit Card"
  7932. },
  7933. @"val_5" : @{
  7934. @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  7935. @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  7936. @"value_id" : @"FOLLOW EXISTING"
  7937. }
  7938. },
  7939. @"control" : @"enum",
  7940. @"name" : @"paymentType",
  7941. @"single_select" : @"true"
  7942. },
  7943. @"title" : @"Payment Information"
  7944. }.mutableCopy;
  7945. NSNumber *hide = setting[@"PaymentInformationHide"];
  7946. [dic setValue:hide forKey:@"hide"];
  7947. return dic;
  7948. }
  7949. + (BOOL)orderOutOfStock:(NSDictionary *)params db:(sqlite3 *)db{
  7950. // params
  7951. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7952. orderCode = [self translateSingleQuote:orderCode];
  7953. // 缺货检查
  7954. 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];
  7955. __block BOOL outOfStock = NO;
  7956. sqlite3 *database = db;
  7957. if (!db) {
  7958. database = [iSalesDB get_db];
  7959. }
  7960. [iSalesDB jk_query:out_of_stock_SQL db:database close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7961. int availability = sqlite3_column_int(stmt, 0);
  7962. int item_qty = sqlite3_column_int(stmt, 1);
  7963. outOfStock = outOfStock || (availability < item_qty ? YES : NO);
  7964. }];
  7965. if (!db) {
  7966. [iSalesDB close_db:database];
  7967. }
  7968. return outOfStock;
  7969. }
  7970. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  7971. {
  7972. sqlite3 *db = [iSalesDB get_db];
  7973. // params
  7974. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7975. orderCode = [self translateSingleQuote:orderCode];
  7976. // 缺货检查
  7977. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7978. if (!appDelegate.can_create_backorder) {
  7979. BOOL out_of_stock = [self orderOutOfStock:params db:db];
  7980. if (out_of_stock) {
  7981. [iSalesDB close_db:db];
  7982. NSMutableDictionary *resultDic = [NSMutableDictionary dictionary];
  7983. [resultDic setObject:@"some item(s) are out of stock,please remove them from cart before placing order with NPD or try again after sync" forKey:@"err_msg"];
  7984. [resultDic setObject:[NSNumber numberWithInt:8] forKey:@"result"];
  7985. return [RAUtils dict2data:resultDic];
  7986. }
  7987. }
  7988. // UISetting
  7989. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  7990. NSString *cachefolder = [paths objectAtIndex:0];
  7991. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  7992. NSData* json =nil;
  7993. json=[NSData dataWithContentsOfFile:img_cache];
  7994. NSError *error=nil;
  7995. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  7996. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  7997. [params setObject:setting forKey:@"setting"];
  7998. int section_count = 0;
  7999. // 0 Order Type 1 Shipping Method 2 Payment Information
  8000. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  8001. // 0 Order Type
  8002. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  8003. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  8004. [ret setValue:order_type_dic forKey:key0];
  8005. // 1 Shipping Method
  8006. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  8007. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  8008. [ret setValue:shipping_method_dic forKey:key1];
  8009. // 2 Payment Information
  8010. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  8011. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  8012. [ret setValue:payment_info_dic forKey:key2];
  8013. // 3 Customer
  8014. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  8015. NSDictionary *customer_dic = [self customerDic:params db:db];
  8016. [ret setValue:customer_dic forKey:key3];
  8017. // 4 Ship To
  8018. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  8019. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  8020. [ret setValue:ship_to_dic forKey:key4];
  8021. // 5 Ship From
  8022. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  8023. if (![shipFromDisable integerValue]) {
  8024. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  8025. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  8026. [ret setValue:ship_from_dic forKey:key5];
  8027. }
  8028. // 6 Freight Bill To
  8029. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  8030. if (![freightBillToDisable integerValue]) {
  8031. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  8032. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  8033. [ret setValue:freight_bill_to forKey:key6];
  8034. }
  8035. // 7 Merchandise Bill To
  8036. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  8037. if (![merchandiseBillToDisable integerValue]) {
  8038. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  8039. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  8040. [ret setValue:merchandise_bill_to_dic forKey:key7];
  8041. }
  8042. // 8 Return To
  8043. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  8044. if (![returnToDisable integerValue]) {
  8045. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  8046. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  8047. [ret setValue:return_to_dic forKey:key8];
  8048. }
  8049. // 9 Model Information
  8050. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  8051. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  8052. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  8053. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  8054. [ret setValue:model_info_dic forKey:key9];
  8055. // 10 Remarks Content
  8056. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  8057. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  8058. [ret setValue:remarks_content_dic forKey:key10];
  8059. // 11 Order Total
  8060. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  8061. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  8062. [ret setValue:order_total_dic forKey:key11];
  8063. // 12 Signature
  8064. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  8065. NSDictionary *sign_dic = [self signatureDic:params db:db];
  8066. [ret setValue:sign_dic forKey:key12];
  8067. [ret setValue:@(section_count) forKey:@"section_count"];
  8068. [iSalesDB close_db:db];
  8069. return [RAUtils dict2data:ret];
  8070. // return nil;
  8071. }
  8072. #pragma mark addr editor
  8073. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  8074. NSMutableDictionary *new_item = [item mutableCopy];
  8075. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  8076. return new_item;
  8077. }
  8078. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  8079. {
  8080. // "is_subaction" = true;
  8081. // orderCode = MOB1608240002;
  8082. // password = 123456;
  8083. // "subaction_tag" = 1;
  8084. // user = EvanK;
  8085. // {
  8086. // "is_subaction" = true;
  8087. // orderCode = MOB1608240002;
  8088. // password = 123456;
  8089. // "refresh_trigger" = zipcode;
  8090. // "subaction_tag" = 1;
  8091. // user = EvanK;
  8092. // }
  8093. NSString *country_code = nil;
  8094. NSString *zipCode = nil;
  8095. NSString *stateCode = nil;
  8096. NSString *city = nil;
  8097. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  8098. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  8099. NSString *code_id = params[@"country"];
  8100. country_code = [self countryCodeByid:code_id];
  8101. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  8102. NSString *zip_code = params[@"zipcode"];
  8103. // 剔除全部为空格
  8104. int spaceCount = 0;
  8105. for (int i = 0; i < zip_code.length; i++) {
  8106. if ([zip_code characterAtIndex:i] == ' ') {
  8107. spaceCount++;
  8108. }
  8109. }
  8110. if (spaceCount == zip_code.length) {
  8111. zip_code = @"";
  8112. }
  8113. zipCode = zip_code;
  8114. if (zipCode.length > 0) {
  8115. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  8116. country_code = [dic valueForKey:@"country_code"];
  8117. if (!country_code) {
  8118. // country_code = @"US";
  8119. NSString *code_id = params[@"country"];
  8120. country_code = [self countryCodeByid:code_id];
  8121. }
  8122. stateCode = [dic valueForKey:@"state_code"];
  8123. if(!stateCode.length) {
  8124. stateCode = params[@"state"];
  8125. }
  8126. city = [dic valueForKey:@"city"];
  8127. if (!city.length) {
  8128. city = params[@"city"];
  8129. }
  8130. // zip code
  8131. // NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  8132. // [zipDic setValue:zipCode forKey:@"value"];
  8133. // [section_0 setValue:zipDic forKey:@"item_11"];
  8134. } else {
  8135. NSString *code_id = params[@"country"];
  8136. country_code = [self countryCodeByid:code_id];
  8137. stateCode = params[@"state"];
  8138. city = params[@"city"];
  8139. }
  8140. }
  8141. }
  8142. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  8143. // [ret removeObjectForKey:@"up_params"];
  8144. [ret setObject:@"New Address" forKey:@"title"];
  8145. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  8146. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  8147. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  8148. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  8149. // [country_dic removeObjectForKey:@"refresh"];
  8150. // [country_dic removeObjectForKey:@"restore"];
  8151. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  8152. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  8153. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  8154. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  8155. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  8156. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  8157. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  8158. // [zip_code_dic removeObjectForKey:@"refresh"];
  8159. [zip_code_dic setValue:zipCode ? zipCode :@"" forKey:@"value"];
  8160. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  8161. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  8162. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  8163. NSDictionary *cityDic = [[new_section_0 objectForKey:@"item_8"] mutableCopy];
  8164. [cityDic setValue:city ? city : @"" forKey:@"value"];
  8165. [new_section_0 setObject:cityDic forKey:@"item_8"];
  8166. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  8167. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  8168. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  8169. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  8170. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  8171. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  8172. // country
  8173. NSString *countryCode = country_code == nil ? @"US" : country_code;
  8174. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  8175. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  8176. // state
  8177. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:stateCode] mutableCopy];
  8178. // NSDictionary *tmpDic = @{
  8179. // @"value" : @"Other",
  8180. // @"value_id" : @"",
  8181. // @"check" : [NSNumber numberWithInteger:0]
  8182. // };
  8183. //
  8184. // for (int i = 0; i < allState.allKeys.count; i++) {
  8185. // NSString *key = [NSString stringWithFormat:@"val_%d",i];
  8186. //
  8187. // NSDictionary *tmp = allState[key];
  8188. // [allState setValue:tmpDic forKey:key];
  8189. // tmpDic = tmp;
  8190. // }
  8191. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  8192. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  8193. [ret setValue:new_section_0 forKey:@"section_0"];
  8194. return [RAUtils dict2data:ret];
  8195. }
  8196. #pragma mark save addr
  8197. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  8198. {
  8199. // NSString *companyName = [self valueInParams:params key:@"company"];
  8200. // NSString *addr1 = [self valueInParams:params key:@"address"];
  8201. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  8202. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  8203. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  8204. // NSString *countryId = [self valueInParams:params key:@"country"];
  8205. // NSString *stateCode = [self valueInParams:params key:@"state"];
  8206. // NSString *city = [self valueInParams:params key:@"city"];
  8207. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  8208. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  8209. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  8210. // NSString *phone = [self valueInParams:params key:@"phone"];
  8211. // NSString *fax = [self valueInParams:params key:@"fax"];
  8212. // NSString *email = [self valueInParams:params key:@"email"];
  8213. return [self offline_saveContact:params update:NO isCustomer:NO];
  8214. }
  8215. #pragma mark cancel order
  8216. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  8217. {
  8218. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8219. NSString *order_id = [self valueInParams:params key:@"orderId"];
  8220. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  8221. if (order_id.length) {
  8222. sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  8223. }
  8224. int ret = [iSalesDB execSql:sql];
  8225. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8226. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8227. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8228. // [dic setValue:@"160409" forKey:@"min_ver"];
  8229. return [RAUtils dict2data:dic];
  8230. }
  8231. #pragma mark sign order
  8232. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  8233. {
  8234. //参考 offline_saveBusinesscard
  8235. DebugLog(@"sign order params: %@",params);
  8236. // orderCode = MOB1608240002;
  8237. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  8238. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8239. orderCode = [self translateSingleQuote:orderCode];
  8240. NSString *picPath = [self valueInParams:params key:@"picpath"];
  8241. picPath = [self translateSingleQuote:picPath];
  8242. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  8243. int ret = [iSalesDB execSql:sql];
  8244. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8245. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8246. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8247. // [dic setValue:@"160409" forKey:@"min_ver"];
  8248. return [RAUtils dict2data:dic];
  8249. }
  8250. #pragma mark save order
  8251. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  8252. NSString *ret = [self valueInParams:params key:key];
  8253. if (translate) {
  8254. ret = [self translateSingleQuote:ret];
  8255. }
  8256. return ret;
  8257. }
  8258. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  8259. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  8260. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  8261. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  8262. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  8263. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  8264. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  8265. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  8266. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  8267. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  8268. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  8269. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  8270. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  8271. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  8272. if (![total_price isEqualToString:@""]) {
  8273. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  8274. } else {
  8275. total_price = @"";
  8276. }
  8277. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  8278. if ([paymentsAndCredits isEqualToString:@""]) {
  8279. paymentsAndCredits = @"";
  8280. } else {
  8281. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  8282. }
  8283. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  8284. if ([handling_fee_value isEqualToString:@""]) {
  8285. handling_fee_value = @"";
  8286. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  8287. handling_fee_value = @"";
  8288. } else {
  8289. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  8290. }
  8291. NSString *handling_fee_placeholder = handling_fee_value;
  8292. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  8293. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  8294. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  8295. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  8296. NSString *lift_gate_placeholder = @"";
  8297. if ([lift_gate_value isEqualToString:@""]) {
  8298. lift_gate_placeholder = @"";
  8299. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  8300. lift_gate_placeholder = @"";
  8301. } else {
  8302. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  8303. }
  8304. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  8305. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  8306. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  8307. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  8308. 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];
  8309. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  8310. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  8311. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  8312. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  8313. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  8314. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  8315. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  8316. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  8317. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  8318. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  8319. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  8320. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  8321. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  8322. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  8323. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  8324. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  8325. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  8326. BOOL number_nil = [credit_card_number isEqualToString:@""];
  8327. if (!number_nil) {
  8328. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  8329. }
  8330. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  8331. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  8332. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  8333. if (!security_code_nil) {
  8334. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  8335. }
  8336. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  8337. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  8338. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  8339. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  8340. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  8341. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  8342. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  8343. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  8344. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  8345. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  8346. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  8347. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  8348. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  8349. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  8350. NSString *contact_id = customer_cid;
  8351. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  8352. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  8353. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  8354. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  8355. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  8356. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  8357. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  8358. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  8359. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  8360. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  8361. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  8362. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  8363. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  8364. //
  8365. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  8366. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  8367. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  8368. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  8369. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  8370. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  8371. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  8372. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  8373. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  8374. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  8375. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  8376. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  8377. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  8378. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  8379. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  8380. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  8381. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  8382. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  8383. 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];
  8384. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  8385. if (receive_cid.length) {
  8386. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  8387. }
  8388. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  8389. if (receive_name.length) {
  8390. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  8391. }
  8392. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  8393. if (receive_ext.length) {
  8394. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  8395. }
  8396. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  8397. if (receive_contact.length) {
  8398. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  8399. }
  8400. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  8401. if (receive_phone.length) {
  8402. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  8403. }
  8404. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  8405. if (receive_email.length) {
  8406. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  8407. }
  8408. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  8409. if (receive_fax.length) {
  8410. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  8411. }
  8412. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  8413. if (sender_cid.length) {
  8414. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  8415. }
  8416. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  8417. if (sender_name.length) {
  8418. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  8419. }
  8420. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  8421. if (sender_ext.length) {
  8422. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  8423. }
  8424. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  8425. if(sender_contact.length) {
  8426. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  8427. }
  8428. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  8429. if (sender_phone.length) {
  8430. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  8431. }
  8432. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  8433. if (sender_fax.length) {
  8434. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  8435. }
  8436. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  8437. if (sender_email.length) {
  8438. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  8439. }
  8440. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  8441. if (shipping_billto_cid.length) {
  8442. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  8443. }
  8444. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  8445. if (shipping_billto_name.length) {
  8446. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  8447. }
  8448. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  8449. if (shipping_billto_ext.length) {
  8450. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  8451. }
  8452. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  8453. if (shipping_billto_contact.length) {
  8454. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  8455. }
  8456. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  8457. if (shipping_billto_phone.length) {
  8458. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  8459. }
  8460. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  8461. if (shipping_billto_fax.length) {
  8462. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  8463. }
  8464. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  8465. if (shipping_billto_email.length) {
  8466. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  8467. }
  8468. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  8469. if (billing_cid.length) {
  8470. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  8471. }
  8472. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  8473. if (billing_name.length) {
  8474. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  8475. }
  8476. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  8477. if (billing_ext.length) {
  8478. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  8479. }
  8480. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  8481. if (billing_contact.length) {
  8482. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  8483. }
  8484. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  8485. if (billing_phone.length) {
  8486. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  8487. }
  8488. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  8489. if (billing_fax.length) {
  8490. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  8491. }
  8492. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  8493. if (billing_email.length) {
  8494. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  8495. }
  8496. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  8497. if (returnto_cid.length) {
  8498. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  8499. }
  8500. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  8501. if (returnto_name.length) {
  8502. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  8503. }
  8504. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  8505. if (returnto_ext.length) {
  8506. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  8507. }
  8508. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  8509. if (returnto_contact.length) {
  8510. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  8511. }
  8512. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  8513. if (returnto_phone.length) {
  8514. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  8515. }
  8516. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  8517. if (returnto_fax.length) {
  8518. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  8519. }
  8520. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  8521. if (returnto_email.length) {
  8522. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  8523. }
  8524. NSString *order_status = @"status = 1,";
  8525. if (submit) {
  8526. order_status = @"status = -11,";
  8527. }
  8528. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  8529. NSString *sync_sql = @"";
  8530. if (submit) {
  8531. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  8532. [param setValue:sales_rep forKey:@"sales_rep"];
  8533. NSString *sync_data = [self translateSingleQuote:[RAUtils dict2string:param]];
  8534. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  8535. }
  8536. 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];
  8537. DebugLog(@"save order contactSql: %@",contactSql);
  8538. DebugLog(@"save order orderSql: %@",orderSql);
  8539. // int contact_ret = [iSalesDB execSql:contactSql];
  8540. int order_ret = [iSalesDB execSql:orderSql];
  8541. int ret = order_ret;
  8542. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8543. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8544. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8545. // [dic setValue:@"160409" forKey:@"min_ver"];
  8546. [dic setObject:so_id forKey:@"so#"];
  8547. return [RAUtils dict2data:dic];
  8548. }
  8549. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  8550. {
  8551. // id foo = nil;
  8552. // NSMutableArray *a = @[].mutableCopy;
  8553. // [a addObject:foo];
  8554. return [self saveorder:param submit:NO];
  8555. }
  8556. #pragma mark add to cart by name
  8557. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  8558. {
  8559. NSString *orderCode = [params objectForKey:@"orderCode"];
  8560. NSString *product_name = [params objectForKey:@"product_name"];
  8561. product_name = [self translateSingleQuote:product_name];
  8562. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  8563. sqlite3 *db = [iSalesDB get_db];
  8564. __block NSMutableString *product_id_string = [NSMutableString string];
  8565. for (int i = 0; i < product_name_array.count; i++) {
  8566. NSString *name = [product_name_array objectAtIndex:i];
  8567. NSString *sql = [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  8568. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8569. int product_id = sqlite3_column_int(stmt, 0);
  8570. if (i == product_name_array.count - 1) {
  8571. [product_id_string appendFormat:@"%d",product_id];
  8572. } else {
  8573. [product_id_string appendFormat:@"%d,",product_id];
  8574. }
  8575. }];
  8576. }
  8577. NSDictionary *newParams = @{
  8578. @"product_id" : product_id_string,
  8579. @"orderCode" : orderCode
  8580. };
  8581. [iSalesDB close_db:db];
  8582. return [self offline_add2cart:[newParams mutableCopy]];
  8583. }
  8584. #pragma mark reset order
  8585. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  8586. {
  8587. UIApplication * app = [UIApplication sharedApplication];
  8588. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  8589. appDelegate.disable_trigger=true;
  8590. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  8591. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  8592. appDelegate.disable_trigger=false;
  8593. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8594. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  8595. return [RAUtils dict2data:dic];
  8596. }
  8597. #pragma mark submit order
  8598. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  8599. {
  8600. return [self saveorder:params submit:YES];
  8601. }
  8602. #pragma mark copy order
  8603. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  8604. {
  8605. NSMutableDictionary *ret = @{}.mutableCopy;
  8606. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  8607. sqlite3 *db = [iSalesDB get_db];
  8608. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  8609. // 首先查看联系人是否active
  8610. 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];
  8611. __block int customer_is_active = 1;
  8612. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8613. customer_is_active = sqlite3_column_int(stmt, 0);
  8614. }];
  8615. if (!customer_is_active) {
  8616. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8617. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8618. [iSalesDB close_db:db];
  8619. return [RAUtils dict2data:ret];
  8620. }
  8621. // new order
  8622. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  8623. NSString *new_order_code = [self get_offline_soid:db];
  8624. NSString *user = ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  8625. user = [self translateSingleQuote:user];
  8626. 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
  8627. __block int result = 1;
  8628. result = [iSalesDB execSql:insert_order_sql db:db];
  8629. if (!result) {
  8630. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8631. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8632. [iSalesDB close_db:db];
  8633. return [RAUtils dict2data:ret];
  8634. }
  8635. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  8636. // __block NSString *product_id = @"";
  8637. __weak typeof(self) weakSelf = self;
  8638. 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];
  8639. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8640. int is_active = sqlite3_column_int(stmt, 1);
  8641. if (is_active) {
  8642. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  8643. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  8644. 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];
  8645. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  8646. }
  8647. }];
  8648. // product_id = [product_id substringFromIndex:1];
  8649. //
  8650. // [self offline_add2cart:@{}.mutableCopy];
  8651. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8652. [iSalesDB close_db:db];
  8653. if (result) {
  8654. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8655. } else {
  8656. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8657. }
  8658. [ret setObject:new_order_code forKey:@"so_id"];
  8659. return [RAUtils dict2data:ret];
  8660. }
  8661. #pragma mark move wish list to cart
  8662. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  8663. // cart中存在的Model在wish list move to cart之后,cart中Model数量为1
  8664. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8665. NSString *collectId = params[@"collectId"];
  8666. 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];
  8667. __block NSString *product_id = @"";
  8668. __block NSString *qty = @"";
  8669. __block int number_of_outOfStock = 0;
  8670. __block NSMutableArray *delete_collectId = [NSMutableArray array];
  8671. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8672. int productId = sqlite3_column_int(stmt, 0);
  8673. int item_qty = sqlite3_column_int(stmt, 1);
  8674. int availability = sqlite3_column_int(stmt, 2);
  8675. int _id = sqlite3_column_int(stmt, 3);
  8676. if (!appDelegate.can_create_backorder) {
  8677. // 库存小于购买量为缺货
  8678. if (availability >= item_qty) {
  8679. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  8680. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  8681. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  8682. } else {
  8683. number_of_outOfStock++;
  8684. }
  8685. } else {
  8686. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  8687. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  8688. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  8689. }
  8690. }];
  8691. NSMutableDictionary *retDic = nil;
  8692. if (!appDelegate.can_create_backorder) {
  8693. if (delete_collectId.count == 0) {
  8694. retDic = [NSMutableDictionary dictionary];
  8695. retDic[@"result"] = [NSNumber numberWithInteger:8];
  8696. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  8697. return [RAUtils dict2data:retDic];
  8698. }
  8699. }
  8700. if (product_id.length > 1) {
  8701. product_id = [product_id substringFromIndex:1];
  8702. }
  8703. if (qty.length > 1) {
  8704. qty = [qty substringFromIndex:1];
  8705. }
  8706. NSDictionary *newParams = @{
  8707. @"product_id" : product_id,
  8708. @"orderCode" : params[@"orderCode"],
  8709. @"qty" : qty
  8710. };
  8711. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  8712. retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  8713. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  8714. NSInteger ret = [[[self offline_deletewishlist:@{@"collectId" : [delete_collectId componentsJoinedByString:@","]}.mutableCopy] valueForKey:@"result"] integerValue];
  8715. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  8716. }
  8717. if (!appDelegate.can_create_backorder) {
  8718. if (number_of_outOfStock > 0) {
  8719. retDic[@"result"] = [NSNumber numberWithInteger:8];
  8720. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  8721. }
  8722. }
  8723. return [RAUtils dict2data:retDic];
  8724. }
  8725. #pragma mark - portfolio
  8726. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  8727. int sort = [[params valueForKey:@"sort"] intValue];
  8728. int offset = [[params valueForKey:@"offset"] intValue];
  8729. int limit = [[params valueForKey:@"limit"] intValue];
  8730. NSString *orderBy = @"";
  8731. switch (sort) {
  8732. case 0:{
  8733. orderBy = @"p.modify_time desc";
  8734. }
  8735. break;
  8736. case 1:{
  8737. orderBy = @"modify_time asc";
  8738. }
  8739. break;
  8740. case 2:{
  8741. orderBy = @"p.name asc";
  8742. }
  8743. break;
  8744. case 3:{
  8745. orderBy = @"p.name desc";
  8746. }
  8747. break;
  8748. case 4:{
  8749. orderBy = @"p.description asc";
  8750. }
  8751. break;
  8752. case 5: {
  8753. orderBy = @"m.default_category asc";
  8754. }
  8755. default:
  8756. break;
  8757. }
  8758. // 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];
  8759. 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];
  8760. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8761. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8762. sqlite3 *db = [iSalesDB get_db];
  8763. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8764. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8765. int product_id = sqlite3_column_int(stmt, 0);
  8766. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  8767. NSString *name = [self textAtColumn:1 statement:stmt];
  8768. NSString *description = [self textAtColumn:2 statement:stmt];
  8769. double price = sqlite3_column_double(stmt, 3);
  8770. double discount = sqlite3_column_double(stmt,4);
  8771. int qty = sqlite3_column_int(stmt, 5);
  8772. int percentage = sqlite3_column_int(stmt, 6);
  8773. int item_id = sqlite3_column_int(stmt, 7);
  8774. // int fashion_id = sqlite3_column_int(stmt, 8);
  8775. NSString *img_path = [self textAtColumn:9 statement:stmt];
  8776. NSString *line_note = [self textAtColumn:10 statement:stmt];
  8777. double percent = sqlite3_column_double(stmt, 11);
  8778. NSString *price_null = [self textAtColumn:3 statement:stmt];
  8779. if ([price_null isEqualToString:@"null"]) {
  8780. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  8781. }
  8782. NSMutableDictionary *item = @{
  8783. @"linenotes": line_note,
  8784. @"check": @(1),
  8785. @"product_id": product_id_string,
  8786. @"available_qty": @(qty),
  8787. @"available_percent" : @(percent),
  8788. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  8789. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  8790. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  8791. @"img": img_path,
  8792. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  8793. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  8794. }.mutableCopy;
  8795. if (percentage) {
  8796. [item removeObjectForKey:@"available_qty"];
  8797. } else {
  8798. [item removeObjectForKey:@"available_percent"];
  8799. }
  8800. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  8801. if ([qty_null isEqualToString:@"null"]) {
  8802. [item removeObjectForKey:@"available_qty"];
  8803. }
  8804. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  8805. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  8806. }];
  8807. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  8808. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8809. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  8810. }
  8811. [iSalesDB close_db:db];
  8812. [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  8813. [dic setValue:@"" forKey:@"email_content"];
  8814. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  8815. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8816. return [RAUtils dict2data:dic];
  8817. }
  8818. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  8819. __block int qty = 0;
  8820. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  8821. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8822. qty = sqlite3_column_int(stmt, 0);
  8823. }];
  8824. return qty;
  8825. }
  8826. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct {
  8827. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  8828. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  8829. NSMutableDictionary * values = params[@"replaceValue"];
  8830. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  8831. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  8832. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  8833. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  8834. NSString *pdf_path = @"";
  8835. if (direct) {
  8836. configureParams = [self valueInParams:params key:@"pdfUrl"];
  8837. } else {
  8838. pdf_path = [self valueInParams:params key:@"pdfPath"];
  8839. }
  8840. NSString *create_user = [self valueInParams:params key:@"user"];
  8841. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  8842. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  8843. // model info
  8844. 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];
  8845. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8846. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  8847. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8848. sqlite3 *db = [iSalesDB get_db];
  8849. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8850. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8851. int product_id = sqlite3_column_int(stmt, 0);
  8852. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  8853. double price = sqlite3_column_double(stmt, 1);
  8854. double discount = sqlite3_column_double(stmt,2);
  8855. int qty = sqlite3_column_int(stmt, 3);
  8856. int percentage = sqlite3_column_int(stmt, 4);
  8857. int item_id = sqlite3_column_int(stmt, 5);
  8858. NSString *line_note = [self textAtColumn:6 statement:stmt];
  8859. double percent = sqlite3_column_double(stmt, 7);
  8860. NSString *price_null = [self textAtColumn:1 statement:stmt];
  8861. /* if ([price_null isEqualToString:@"null"]) {
  8862. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  8863. }
  8864. */
  8865. [product_ids_string appendFormat:@"%@,",product_id_string];
  8866. // Regular Price
  8867. int regular_price = [[params objectForKey:@"regular_price"] integerValue];
  8868. NSString *regular_price_str = [self get_portfolio_price:appDelegate.contact_id item_id:item_id price:regular_price db:db];
  8869. // QTY
  8870. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  8871. if ([qty_null isEqualToString:@"null"]) {
  8872. // 查available
  8873. qty = [self model_QTY:product_id_string db:db];
  8874. }
  8875. if (percentage) {
  8876. qty = qty * percent / 100;
  8877. }
  8878. // Special Price
  8879. if ([price_null isEqualToString:@"null"]) {
  8880. // price = regular price
  8881. price = [regular_price_str doubleValue];
  8882. }
  8883. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  8884. if (![discount_null isEqualToString:@"null"]) {
  8885. price = price * (1 - discount / 100.0);
  8886. }
  8887. NSMutableDictionary *item = @{
  8888. @"line_note": line_note,
  8889. @"product_id": product_id_string,
  8890. @"available_qty": @(qty),
  8891. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  8892. @"regular_price" : regular_price_str,
  8893. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  8894. }.mutableCopy;
  8895. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  8896. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  8897. }];
  8898. [iSalesDB close_db:db];
  8899. if (product_ids_string.length > 0) {
  8900. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  8901. }
  8902. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  8903. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  8904. return [RAUtils dict2data:resultDictionary];
  8905. }
  8906. NSString *model_info = [RAUtils dict2string:dic];
  8907. // 创建PDF
  8908. // 在preview情况下保存,则不需要新建了
  8909. if (direct) {
  8910. NSMutableDictionary *tear_sheet_params = params;
  8911. // if (tear_sheet_id) {
  8912. // tear_sheet_params = values;
  8913. // }
  8914. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  8915. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  8916. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  8917. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  8918. resultDictionary = pdfInfo.mutableCopy;
  8919. } else { // 创建PDF失败
  8920. return pdfData;
  8921. }
  8922. } else {
  8923. // pdf_path 就是本地路径
  8924. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  8925. }
  8926. // 将文件移动到PDF Cache文件夹
  8927. NSString *newPath = [pdf_path lastPathComponent];
  8928. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8929. NSString *cachefolder = [paths objectAtIndex:0];
  8930. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  8931. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  8932. NSFileManager *fileManager = [NSFileManager defaultManager];
  8933. NSError *error = nil;
  8934. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  8935. if (error) { // 移动文件失败
  8936. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  8937. return [RAUtils dict2data:resultDictionary];
  8938. }
  8939. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  8940. pdf_path = [newPath lastPathComponent];
  8941. // 保存信息
  8942. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  8943. NSString *off_params = [RAUtils dict2string:params];
  8944. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  8945. pdf_path = [self translateSingleQuote:pdf_path];
  8946. create_user = [self translateSingleQuote:create_user];
  8947. tear_note = [self translateSingleQuote:tear_note];
  8948. tear_name = [self translateSingleQuote:tear_name];
  8949. model_info = [self translateSingleQuote:model_info];
  8950. configureParams = [self translateSingleQuote:configureParams];
  8951. off_params = [self translateSingleQuote:off_params];
  8952. 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];
  8953. if (tear_sheet_id) {
  8954. int _id = [tear_sheet_id integerValue];
  8955. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  8956. }
  8957. int result = [iSalesDB execSql:save_pdf_sql];
  8958. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  8959. //
  8960. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  8961. if (remove_Item) {
  8962. // portfolioId
  8963. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  8964. }
  8965. return [RAUtils dict2data:resultDictionary];
  8966. }
  8967. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  8968. return [self offline_savePDF:params direct:YES];
  8969. }
  8970. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  8971. return [self offline_savePDF:params direct:NO];
  8972. }
  8973. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  8974. int offset = [[params valueForKey:@"offset"] integerValue];
  8975. int limit = [[params valueForKey:@"limit"] integerValue];
  8976. NSString *keyword = [params valueForKey:@"keyWord"];
  8977. NSString *where = @"where is_delete is null or is_delete = 0";
  8978. if (keyword.length) {
  8979. keyword = [self translateSingleQuote:keyword];
  8980. 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];
  8981. }
  8982. 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
  8983. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8984. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8985. NSString *cachefolder = [paths objectAtIndex:0];
  8986. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  8987. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8988. NSString *name = [self textAtColumn:0 statement:stmt];
  8989. NSString *note = [self textAtColumn:1 statement:stmt];
  8990. NSString *time = [self textAtColumn:2 statement:stmt];
  8991. NSString *user = [self textAtColumn:3 statement:stmt];
  8992. NSString *path = [self textAtColumn:4 statement:stmt];
  8993. NSString *model_info = [self textAtColumn:6 statement:stmt];
  8994. NSString *off_params = [self textAtColumn:7 statement:stmt];
  8995. path = [pdfFolder stringByAppendingPathComponent:path];
  8996. BOOL bdir=NO;
  8997. NSFileManager* fileManager = [NSFileManager defaultManager];
  8998. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  8999. {
  9000. //pdf文件不存在
  9001. path=nil;
  9002. }
  9003. time = [self changeDateTimeFormate:time];
  9004. time = [time stringByAppendingString:@" PST"];
  9005. int sheet_id = sqlite3_column_int(stmt, 5);
  9006. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  9007. item[@"tearsheetsId"]=@(sheet_id);
  9008. item[@"pdf_path"]=path;
  9009. item[@"create_time"]=time;
  9010. item[@"create_user"]=user;
  9011. item[@"tear_note"]=note;
  9012. item[@"tear_name"]=name;
  9013. item[@"isLocalFile"]=[NSNumber numberWithBool:YES];
  9014. item[@"model_info"]=model_info;
  9015. item[@"off_params"]=off_params;
  9016. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9017. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9018. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9019. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9020. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  9021. }];
  9022. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9023. return [RAUtils dict2data:dic];
  9024. }
  9025. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  9026. NSString *prodct_ids = [params objectForKey:@"product_id"];
  9027. // NSString *user = [params objectForKey:@"user"];
  9028. // 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];
  9029. 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];
  9030. int result = [iSalesDB execSql:sql];
  9031. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9032. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  9033. if (result == RESULT_TRUE) {
  9034. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  9035. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  9036. sqlite3 *db = [iSalesDB get_db];
  9037. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  9038. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  9039. [iSalesDB close_db:db];
  9040. }
  9041. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9042. return [RAUtils dict2data:dic];
  9043. }
  9044. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  9045. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  9046. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  9047. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  9048. // Regurlar Price
  9049. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  9050. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  9051. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  9052. __block long val_count = 0;
  9053. NSString *sql = @"select name,type,order_by from price order by order_by";
  9054. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9055. NSString *name = [self textAtColumn:0 statement:stmt];
  9056. int type = sqlite3_column_int(stmt, 1);
  9057. int order_by = sqlite3_column_int(stmt, 2);
  9058. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9059. NSDictionary *price_dic = @{
  9060. @"value" : name,
  9061. @"value_id" : [NSNumber numberWithInteger:type],
  9062. @"check" : order_by == 0 ? @(1) : @(0)
  9063. };
  9064. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  9065. val_count = ++(*count);
  9066. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9067. val_count = 0;
  9068. }];
  9069. [cadedate setObject:@{@"value" : @"None",
  9070. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  9071. val_count++;
  9072. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  9073. [price setObject:cadedate forKey:@"cadedate"];
  9074. [section1 setObject:price forKey:@"item_2"];
  9075. [dic setObject:section1 forKey:@"section_1"];
  9076. return [RAUtils dict2data:dic];
  9077. }
  9078. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  9079. NSString *product_id = [params objectForKey:@"fashionId"];
  9080. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  9081. __block int result = RESULT_TRUE;
  9082. __block int qty = 0;
  9083. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9084. qty = sqlite3_column_int(stmt, 0);
  9085. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9086. result = RESULT_FALSE;
  9087. }];
  9088. NSMutableDictionary *dic = @{
  9089. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  9090. @"mode" : @"Regular Mode",
  9091. @"quantity_available" : @(qty),
  9092. @"result" : @(result),
  9093. }.mutableCopy;
  9094. return [RAUtils dict2data:dic];
  9095. }
  9096. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  9097. NSString *item_ids = [params objectForKey:@"item_id"];
  9098. NSString *line_notes = [params objectForKey:@"notes"];
  9099. NSString *price_str = [params objectForKey:@"price"];
  9100. NSString *discount_str = [params objectForKey:@"discount"];
  9101. NSString *percent = [params objectForKey:@"available_percent"];
  9102. NSString *qty = [params objectForKey:@"available_qty"];
  9103. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  9104. int dot = 0;
  9105. if (line_notes) {
  9106. line_notes = [self translateSingleQuote:line_notes];
  9107. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  9108. sql = [sql stringByAppendingString:line_notes];
  9109. dot = 1;
  9110. } else {
  9111. line_notes = @"";
  9112. }
  9113. if (price_str) {
  9114. if (dot) {
  9115. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  9116. } else {
  9117. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  9118. dot = 1;
  9119. }
  9120. sql = [sql stringByAppendingString:price_str];
  9121. } else {
  9122. price_str = @"";
  9123. }
  9124. if (discount_str) {
  9125. if (dot) {
  9126. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  9127. } else {
  9128. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  9129. dot = 1;
  9130. }
  9131. sql = [sql stringByAppendingString:discount_str];
  9132. } else {
  9133. discount_str = @"";
  9134. }
  9135. if (percent) {
  9136. if (dot) {
  9137. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  9138. } else {
  9139. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  9140. dot = 1;
  9141. }
  9142. sql = [sql stringByAppendingString:percent];
  9143. } else {
  9144. percent = @"";
  9145. }
  9146. if (qty) {
  9147. if (dot) {
  9148. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  9149. } else {
  9150. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  9151. dot = 1;
  9152. }
  9153. sql = [sql stringByAppendingString:qty];
  9154. } else {
  9155. qty = @"";
  9156. }
  9157. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  9158. sql = [sql stringByAppendingString:where];
  9159. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9160. int result = [iSalesDB execSql:sql];
  9161. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9162. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9163. return [RAUtils dict2data:dic];
  9164. }
  9165. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  9166. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  9167. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9168. sqlite3 *db = [iSalesDB get_db];
  9169. 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];
  9170. int result = [iSalesDB execSql:sql db:db];
  9171. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9172. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9173. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  9174. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  9175. [iSalesDB close_db:db];
  9176. return [RAUtils dict2data:dic];
  9177. }
  9178. + (void)offline_removePDFWithName:(NSString *)name {
  9179. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9180. NSString *cachefolder = [paths objectAtIndex:0];
  9181. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9182. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  9183. NSFileManager *fileManager = [NSFileManager defaultManager];
  9184. [fileManager removeItemAtPath:path error:nil];
  9185. }
  9186. + (void)offline_clear_PDFCache {
  9187. NSFileManager *fileManager = [NSFileManager defaultManager];
  9188. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9189. NSString *cachefolder = [paths objectAtIndex:0];
  9190. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9191. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  9192. for (NSString *path in pdf_files) {
  9193. [self offline_removePDFWithName:[path lastPathComponent]];
  9194. }
  9195. }
  9196. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  9197. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9198. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  9199. NSString *user = [params objectForKey:@"user"];
  9200. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %d;",tearsheetsId]]; // tearsheets_id ---> _id
  9201. if (![create_user isEqualToString:user]) {
  9202. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  9203. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9204. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  9205. return [RAUtils dict2data:dic];
  9206. }
  9207. NSString *is_local_sql = [NSString stringWithFormat:@"select is_local,pdf_path from offline_pdf where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  9208. __block int is_local = 0;
  9209. __block NSString *path = @"";
  9210. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9211. is_local = sqlite3_column_int(stmt, 0);
  9212. path = [self textAtColumn:1 statement:stmt];
  9213. }];
  9214. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  9215. if (is_local == 1) {
  9216. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  9217. // 删除文件
  9218. [self offline_removePDFWithName:path];
  9219. }
  9220. int result = [iSalesDB execSql:sql];
  9221. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9222. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9223. return [RAUtils dict2data:dic];
  9224. }
  9225. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial
  9226. {
  9227. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  9228. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  9229. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  9230. NSString* where=@"1=1";
  9231. if (ver!=nil) {
  9232. where=@"is_dirty=1";
  9233. }
  9234. 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];
  9235. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  9236. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9237. sqlite3 *db = [iSalesDB get_db];
  9238. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9239. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  9240. NSInteger _id = sqlite3_column_int(stmt, 0);
  9241. NSInteger product_id = sqlite3_column_int(stmt, 1);
  9242. NSString *name = [self textAtColumn:2 statement:stmt];
  9243. NSString *desc = [self textAtColumn:3 statement:stmt];
  9244. NSInteger item_id = sqlite3_column_int(stmt, 4);
  9245. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  9246. NSInteger qty = sqlite3_column_int(stmt, 6);
  9247. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  9248. double percent = sqlite3_column_double(stmt, 8);
  9249. double price = sqlite3_column_double(stmt, 9);
  9250. double discount = sqlite3_column_double(stmt, 10);
  9251. NSString *img = [self textAtColumn:11 statement:stmt];
  9252. NSString *line_note = [self textAtColumn:12 statement:stmt];
  9253. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  9254. NSString *create_time = [self textAtColumn:14 statement:stmt];
  9255. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  9256. NSString *price_null = [self textAtColumn:9 statement:stmt];
  9257. if ([price_null isEqualToString:@"null"]) {
  9258. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  9259. }
  9260. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  9261. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  9262. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  9263. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  9264. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  9265. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  9266. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  9267. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  9268. if ([qty_null isEqualToString:@"null"]) {
  9269. // [item setValue:@"null" forKey:@"available_qty"];
  9270. } else {
  9271. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  9272. }
  9273. if ([is_percent_null isEqualToString:@"null"]) {
  9274. // [item setValue:@"null" forKey:@"percentage"];
  9275. } else {
  9276. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  9277. }
  9278. if ([percent_null isEqualToString:@"null"]) {
  9279. // [item setValue:@"null" forKey:@"percent"];
  9280. } else {
  9281. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  9282. }
  9283. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  9284. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  9285. [item setValue:name forKey:@"name"];
  9286. [item setValue:desc forKey:@"description"];
  9287. [item setValue:img forKey:@"img"];
  9288. [item setValue:line_note forKey:@"line_note"];
  9289. [item setValue:create_time forKey:@"createtime"];
  9290. [item setValue:modify_time forKey:@"modifytime"];
  9291. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  9292. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9293. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  9294. }];
  9295. [iSalesDB close_db:db];
  9296. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  9297. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9298. }
  9299. return ret;
  9300. }
  9301. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  9302. {
  9303. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  9304. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  9305. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  9306. NSString* where=@"1=1";
  9307. if (ver!=nil) {
  9308. where=@"is_dirty=1";
  9309. }
  9310. 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];
  9311. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  9312. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9313. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  9314. NSInteger _id = sqlite3_column_int(stmt, 0);
  9315. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  9316. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  9317. NSString *create_user = [self textAtColumn:3 statement:stmt];
  9318. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  9319. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  9320. NSString *model_info = [self textAtColumn:6 statement:stmt];
  9321. NSString *createtime = [self textAtColumn:7 statement:stmt];
  9322. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  9323. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  9324. NSString *off_params = [self textAtColumn:10 statement:stmt];
  9325. NSString *uuid = [NSUUID UUID].UUIDString;
  9326. int is_delete = sqlite3_column_int(stmt, 11);
  9327. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  9328. int is_local = sqlite3_column_int(stmt, 12);
  9329. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  9330. if (![tearsheet_id_null isEqualToString:@"null"]) {
  9331. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  9332. }
  9333. [item setObject:pdf_path forKey:@"pdf_path"];
  9334. [item setObject:create_user forKey:@"create_user"];
  9335. [item setObject:tear_note forKey:@"tear_note"];
  9336. [item setObject:tear_name forKey:@"tear_name"];
  9337. [item setObject:model_info forKey:@"model_info"];
  9338. [item setObject:createtime forKey:@"createtime"];
  9339. [item setObject:modifytime forKey:@"modifytime"];
  9340. [item setObject:urlParams forKey:@"urlParams"];
  9341. [item setObject:off_params forKey:@"off_params"];
  9342. [item setObject:uuid forKey:@"pdf_token"];
  9343. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  9344. NSDictionary *off_Params_dic = [RAUtils string2dict:off_params];
  9345. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  9346. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  9347. } else {
  9348. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  9349. }
  9350. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9351. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  9352. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9353. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9354. }];
  9355. return ret;
  9356. }
  9357. @end