OLDataProvider.m 359 KB

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