OLDataProvider.m 290 KB

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