123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706 |
- <?php
- namespace app\empcrm\controller;
- use app\BaseController;
- use app\model\FishData;
- use app\model\User;
- use app\model\Admin;
- use think\facade\View;
- use think\facade\Session;
- use think\facade\Log;
- use app\model\Employee;
- use app\model\Company;
- use app\model\CustomerVisitLog;
- use app\model\Customer;
- use app\logics\OrgLogic;
- use app\model\CustomerSource;
- use app\model\Org;
- use app\model\CustomerPortraitField;
- use app\model\CustomerPortraitFieldSelect;
- use app\model\Setting;
- use app\logics\CustomerLogic;
- use app\model\Decostyle;
- use app\model\CustomerPackage;
- use app\model\CustomerRecycle;
- use app\model\CustomersSubscribe;
- use think\facade\Db;
- use app\model\Activity;
- use app\model\WeworksingleCompanySetting;
- use app\model\WeworksingleCustomer;
- use think\facade\Console;
- use app\model\PreformanceTasksOrg;
- use app\event\Msg;
- use app\model\CreditsSetting;
- use app\model\AgentIntegral;
- use app\model\AgentUser;
- use app\model\AgentCustomerLog;
- use app\model\CustomerImport;
- use app\model\EmployeeMsg;
- use app\model\Miniprogram;
- use app\model\PreformanceTasks as PreformanceTasksModel;
- use app\model\CustomerSharing;
- use app\model\OutCallLog;
- use app\model\OutCallMbLog;
- use app\model\ShareLog;
- use app\model\StatisticsSetting;
- use app\model\TrainCourseView;
- use app\model\TrainDoneLog;
- use app\model\OutCallSetting;
- use call\Yhuatong;
- use app\model\CustomerInvalidLog;
- use app\model\CustomerPortrait;
- use app\model\CustomerStateCheck;
- use app\model\WechatActivityIntegral;
- use toolkits\Aec;
- use PhpOffice\PhpSpreadsheet\Spreadsheet;
- use PhpOffice\PhpSpreadsheet\IOFactory;
- use wx\offiaccount\client\TmpMsg;
- use clue\Fish;
- use clue\Tencent;
- use app\model\FishCampaign;
- class Empcrm extends BaseController
- {
- //首页
- public function index()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $today = date('Y-m-d H:i:s', time());
- $firstday = $today;
- $lastday = date('Y-m-d 00:00:00', strtotime("$firstday -2 day"));
- $where[] = ['org_id', '=', $request->empcrm->org_id];
- $where[] = ['addtime', 'between', [$lastday, $firstday]];
- $where[] = ['state', 'in', array_merge(CustomerVisitLog::changeState('已交定', 'chaos'), CustomerVisitLog::changeState('已签单', 'chaos'))];
- $list = CustomerVisitLog::with(['employee' => function ($query) {
- $query->field('id,name');
- }])->where($where)->order('addtime desc')->select()->toArray();
- foreach ($list as $key => $val) {
- $list[$key]['package_name'] = !empty($val['package_id']) ? CustomerPackage::where('id', $val['package_id'])->value('name') : '';
- $cusdata = Customer::with(['designer'])->where('id', $val['customer_id'])->field('name,community_name,deposit_money,signed_money,addtime,designer_id')->find();
- //如果客户不存在直接销毁跳过本次循环
- if (empty($cusdata)) {
- unset($list[$key]);
- continue;
- }
- $cusdata['deposit_moneys'] = !empty($cusdata->getData('deposit_money')) ? number_format($cusdata->getData('deposit_money'), 2, '.', ',') : 0;
- $cusdata['signed_moneys'] = !empty($cusdata->getData('signed_money')) ? number_format($cusdata->getData('signed_money'), 2, '.', ',') : 0;
- $list[$key]['cusdata'] = $cusdata;
- }
- $company_name = Company::where('root_id', $root_id)->value('company_name');
- View::assign('company_name', $company_name);
- View::assign('list', $list);
- return View::fetch();
- }
- /************************************************* 客户池仿手机端团队客户搜索 start ***************************************/
- public function getCondition()
- {
- $param = $this->request->only(['addtime' => date('Y-m-d'), 'source' => '', 'employee_id' => '', 'org_id' => '', 'type' => '']);
- $is_manager = request()->empcrm->is_manager;
- $employee_id = request()->empcrm->id;
- $org_id = request()->empcrm->org_id;
- $startTime = strtotime('2015-01-01');
- $endTime = strtotime(date('Y-m-d')) + 86400;
- if (!empty($param['addtime'])) {
- //录入时间
- $addtimeArr = explode(' - ', $param['addtime']);
- $start = $addtimeArr[0];
- $end = date('Y-m-d', strtotime($addtimeArr[1]) + 86400);
- $startTime = strtotime($start) - 1;
- $endTime = strtotime($end);
- }
- // 查询条件
- // 设计师部门管理员,需要查询指派给下属设计师的客户信息
- $org_type = Org::where('id', '=', $org_id)->value('org_type');
- if ($org_type == 2) {
- if ($is_manager == 1) {
- if ($param['org_id']) {
- $param_sub_org = orgSubIds($param['org_id']);
- } else {
- $param_sub_org = orgSubIds($org_id);
- }
- $designer_employee = Employee::where([['org_id', 'in', $param_sub_org], ['uid', '<>', 0], ['state', '=', '在职']])->column('id');
- $query_condition[] = ['designer_id', 'in', $designer_employee];
- $condition[] = ['Customer.designer_id', 'in', $designer_employee];
- // 是哪个员工
- setCondition($param['employee_id'], 'designer_id', '=', $condition);
- setCondition($param['employee_id'], 'Customer.designer_id', '=', $condition_log);
- } else {
- $query_condition[] = ['designer_id', '=', $employee_id];
- $condition[] = ['Customer.designer_id', '=', $employee_id];
- }
- } else {
- if ($is_manager == 1) {
- if ($param['org_id']) {
- $param_sub_org = orgSubIds($param['org_id']);
- } else {
- $param_sub_org = orgSubIds($org_id);
- }
- $query_condition[] = ['org_id', 'in', $param_sub_org];
- $query_condition[] = ['employee_id', '>', 0];
- $condition[] = ['Customer.org_id', 'in', $param_sub_org];
- $condition[] = ['Customer.employee_id', '>', 0];
- setCondition($param['employee_id'], 'employee_id', '=', $condition);
- setCondition($param['employee_id'], 'Customer.employee_id', '=', $condition_log);
- } else {
- $query_condition[] = ['employee_id', '=', $employee_id];
- $condition[] = ['Customer.employee_id', '=', $employee_id];
- }
- }
- $query_condition[] = ['died', '<>', 2];
- $query_condition[] = ['state', 'not in', Customer::changeState('无效', 'chaos')];
- $condition[] = ['Customer.died', '<>', 2];
- $condition[] = ['Customer.state', 'not in', Customer::changeState('无效', 'chaos')];
- $state_n = Customer::changeState('待确认', 'chaos');
- $state_w = Customer::changeState('无效', 'chaos');
- $query1 = array_merge($query_condition, [['state', 'not in', array_merge($state_n, $state_w)]]);
- $query2 = array_merge($query_condition, [['state', 'in', $state_n], ['crm_res_id', 'NULL', NULL]]);
- $query3 = array_merge($query_condition, [['state', 'in', $state_n], ['crm_res_id', 'NOTNULL', NULL], ['valid_time', 'NOTNULL', NULL]]);
- $ids = Customer::whereOr([$query1, $query2, $query3])->column('id');
- // 有效
- $state1 = CustomerVisitLog::changeState('未到访', 'chaos');
- $state2 = CustomerVisitLog::changeState('已到访', 'chaos');
- $state3 = CustomerVisitLog::changeState('已量房', 'chaos');
- $state4 = CustomerVisitLog::changeState('已到店', 'chaos');
- $state5 = CustomerVisitLog::changeState('交定', 'chaos');
- $state6 = CustomerVisitLog::changeState('签单', 'chaos');
- $state7 = CustomerVisitLog::changeState('已到场', 'chaos');
- // 查询条件
- switch ($param['type']) {
- case 'jiav':
- $condition[] = ['Customer.add_wechat_time', '>', date('Y-m-d H:i:s', $startTime)];
- $condition[] = ['Customer.add_wechat_time', '<', date('Y-m-d H:i:s', $endTime)];
- break;
- case 'jiaofang':
- $condition[] = ['Customer.house_delivery_time', '>', date('Y/m/d H:i:s', $startTime)];
- $condition[] = ['Customer.house_delivery_time', '<', date('Y/m/d H:i:s', $endTime)];
- break;
- case 'yjlf': // 预计量房
- $s_where[] = ['org_id', 'in', $param_sub_org];
- $s_where[] = ['state', '=', 0];
- $s_where[] = ['type', '=', 3];
- $s_where[] = ['addtime', '>', date('Y-m-d H:i:s', $startTime)];
- $s_where[] = ['addtime', '<', date('Y-m-d H:i:s', $endTime)];
- $customer_ids = CustomersSubscribe::where($s_where)->group('customer_id')->column('customer_id');
- $condition[] = ['Customer.id', 'in', $customer_ids];
- break;
- case 'liangfang':
- $condition[] = ['CustomerVisitLog.state', 'in', $state3];
- $condition[] = ['CustomerVisitLog.addtime', '>', date('Y-m-d H:i:s', $startTime)];
- $condition[] = ['CustomerVisitLog.addtime', '<', date('Y-m-d H:i:s', $endTime)];
- break;
- case 'yjdd': // 预计到店
- $s_where[] = ['org_id', 'in', $param_sub_org];
- $s_where[] = ['state', '=', 0];
- $s_where[] = ['type', '=', 1];
- $s_where[] = ['addtime', '>', date('Y-m-d H:i:s', $startTime)];
- $s_where[] = ['addtime', '<', date('Y-m-d H:i:s', $endTime)];
- $customer_ids = CustomersSubscribe::where($s_where)->group('customer_id')->column('customer_id');
- $condition[] = ['Customer.id', 'in', $customer_ids];
- break;
- case 'daodian':
- $condition[] = ['CustomerVisitLog.state', 'in', $state4];
- $condition[] = ['CustomerVisitLog.addtime', '>', date('Y-m-d H:i:s', $startTime)];
- $condition[] = ['CustomerVisitLog.addtime', '<', date('Y-m-d H:i:s', $endTime)];
- break;
- case 'jiaoding':
- $condition[] = ['CustomerVisitLog.state', 'in', $state5];
- $condition[] = ['CustomerVisitLog.addtime', '>', date('Y-m-d H:i:s', $startTime)];
- $condition[] = ['CustomerVisitLog.addtime', '<', date('Y-m-d H:i:s', $endTime)];
- break;
- case 'qiandan':
- $condition[] = ['CustomerVisitLog.state', 'in', $state6];
- $condition[] = ['CustomerVisitLog.addtime', '>', date('Y-m-d H:i:s', $startTime)];
- $condition[] = ['CustomerVisitLog.addtime', '<', date('Y-m-d H:i:s', $endTime)];
- break;
- case 'jianqun':
- $condition[] = ['Customer.group_building_date', '>', date('Y-m-d H:i:s', $startTime)];
- $condition[] = ['Customer.group_building_date', '<', date('Y-m-d H:i:s', $endTime)];
- break;
- case 'zhibo':
- $condition[] = ['Customer.live_broadcast_date', '>', date('Y-m-d H:i:s', $startTime)];
- $condition[] = ['Customer.live_broadcast_date', '<', date('Y-m-d H:i:s', $endTime)];
- break;
- case 'yydc':
- $s_where[] = ['org_id', 'in', $param_sub_org];
- $s_where[] = ['state', '=', 0];
- $s_where[] = ['type', '=', 2];
- $s_where[] = ['addtime', '>', date('Y-m-d H:i:s', $startTime)];
- $s_where[] = ['addtime', '<', date('Y-m-d H:i:s', $endTime)];
- $customer_ids = CustomersSubscribe::where($s_where)->group('customer_id')->column('customer_id');
- $condition[] = ['Customer.id', 'in', $customer_ids];
- break;
- case 'daochang':
- $condition[] = ['CustomerVisitLog.state', 'in', $state7];
- $condition[] = ['CustomerVisitLog.addtime', '>', date('Y-m-d H:i:s', $startTime)];
- $condition[] = ['CustomerVisitLog.addtime', '<', date('Y-m-d H:i:s', $endTime)];
- break;
- case 'yygswjm':
- $condition[] = ['Customer.subscribe_meet_outside', '>', date('Y-m-d H:i:s', $startTime)];
- $condition[] = ['Customer.subscribe_meet_outside', '<', date('Y-m-d H:i:s', $endTime)];
- break;
- case 'gswjm':
- $condition[] = ['Customer.meet_outside', '>', date('Y-m-d H:i:s', $startTime)];
- $condition[] = ['Customer.meet_outside', '<', date('Y-m-d H:i:s', $endTime)];
- break;
- default:
- //$condition[] = ['Customer.addtime', '>', date('Y-m-d H:i:s', $startTime)];
- //$condition[] = ['Customer.addtime', '<', date('Y-m-d H:i:s', $endTime)];
- break;
- }
- return ['condition' => $condition, 'ids' => $ids];
- }
- /**
- * 以客户跟进为主的查询条件
- *
- */
- public function logCondition()
- {
- $param = $this->request->only(['followtime' => '', 'source' => '', 'employee_id' => '', 'org_id' => '', 'type' => '']);
- $is_manager = request()->empcrm->is_manager;
- $employee_id = request()->empcrm->id;
- $org_id = request()->empcrm->org_id;
- $startTime = strtotime('2015-01-01');
- $endTime = strtotime(date('Y-m-d')) + 86400;
- if (!empty($param['followtime'])) {
- //录入时间
- $addtimeArr = explode(' - ', $param['followtime']);
- $start = $addtimeArr[0];
- $end = date('Y-m-d', strtotime($addtimeArr[1]) + 86400);
- $startTime = strtotime($start) - 1;
- $endTime = strtotime($end);
- }
- // 查询条件
- // 设计师部门管理员,需要查询指派给下属设计师的客户信息
- $org_type = Org::where('id', '=', $org_id)->value('org_type');
- if ($param['org_id']) {
- $param_sub_org = orgSubIds($param['org_id']);
- } else {
- $param_sub_org = orgSubIds($org_id);
- }
- if ($org_type == 2) {
- if ($is_manager == 1) {
- $designer_employee = Employee::where([['org_id', 'in', $param_sub_org], ['uid', '<>', 0], ['state', '=', '在职']])->column('id');
- $condition[] = ['designer_id', 'in', $designer_employee];
- $condition_log[] = ['Customer.designer_id', 'in', $designer_employee];
- $s_where[] = ['employee_id', 'in', $designer_employee];
- // 是哪个员工
- setCondition($param['employee_id'], 'designer_id', '=', $condition);
- setCondition($param['employee_id'], 'Customer.designer_id', '=', $condition_log);
- setCondition($param['employee_id'], 'employee_id', '=', $s_where);
- } else {
- $condition[] = ['designer_id', '=', $employee_id];
- $condition_log[] = ['Customer.designer_id', '=', $employee_id];
- $s_where[] = ['employee_id', '=', $employee_id];
- }
- } else {
- if ($is_manager == 1) {
- $condition[] = ['org_id', 'in', $param_sub_org];
- $condition[] = ['employee_id', '>', 0];
- $condition_log[] = ['Customer.org_id', 'in', $param_sub_org];
- $condition_log[] = ['Customer.employee_id', '>', 0];
- $s_where[] = ['org_id', 'in', $param_sub_org];
- $s_where[] = ['employee_id', '>', 0];
- // 是哪个员工
- setCondition($param['employee_id'], 'employee_id', '=', $condition);
- setCondition($param['employee_id'], 'Customer.employee_id', '=', $condition_log);
- setCondition($param['employee_id'], 'employee_id', '=', $s_where);
- } else {
- $condition[] = ['employee_id', '=', $employee_id];
- $condition_log[] = ['Customer.employee_id', '=', $employee_id];
- $s_where[] = ['employee_id', '=', $employee_id];
- }
- }
- $condition[] = ['died', '<>', 2];
- $condition[] = ['state', 'not in', Customer::changeState('无效', 'chaos')];
- $condition_log[] = ['Customer.died', '<>', 2];
- $condition_log[] = ['Customer.state', 'not in', Customer::changeState('无效', 'chaos')];
- $state_n = Customer::changeState('待确认', 'chaos');
- $state_w = Customer::changeState('无效', 'chaos');
- $query1 = array_merge($condition, [['state', 'not in', array_merge($state_n, $state_w)]]);
- $query2 = array_merge($condition, [['state', 'in', $state_n], ['crm_res_id', 'NULL', NULL]]);
- $ids = Customer::whereOr([$query1, $query2])->column('id');
- // 模块
- // 加v
- $condition_jiav = $condition;
- $condition_jiav[] = ['add_wechat_time', '>', date('Y-m-d H:i:s', $startTime)];
- $condition_jiav[] = ['add_wechat_time', '<', date('Y-m-d H:i:s', $endTime)];
- $jiav_ids_find = Customer::where($condition_jiav)->column('id');
- $jiav_ids = array_intersect($jiav_ids_find, $ids);
- // 交房
- $condition_jiaofang = $condition;
- $condition_jiaofang[] = ['house_delivery_time', '>', date('Y/m/d H:i:s', $startTime)];
- $condition_jiaofang[] = ['house_delivery_time', '<', date('Y/m/d H:i:s', $endTime)];
- $jiaofang_ids_find = Customer::where($condition_jiaofang)->column('id');
- $jiaofang_ids = array_intersect($jiaofang_ids_find, $ids);
- //预计量房 预计到店
- if ($is_manager) {
- $s_where[] = ['org_id', 'in', $param_sub_org];
- } else {
- $s_where[] = ['employee_id', '=', $employee_id];
- }
- $s_where[] = ['state', '=', 0];
- $s_where[] = ['addtime', '>', date('Y-m-d H:i:s', $startTime)];
- $s_where[] = ['addtime', '<', date('Y-m-d H:i:s', $endTime)];
- $s_where[] = ['type', 'in', [1, 3]];
- $yuji_ids_find = CustomersSubscribe::where($s_where)->group('customer_id')->column('customer_id');
- $yuji_ids = array_intersect($yuji_ids_find, $ids);
- $customer_ids_1 = array_values(array_unique(array_merge($jiav_ids, $jiaofang_ids, $yuji_ids)));
- // 时间段内有跟踪
- $condition_log[] = ['CustomerVisitLog.addtime', '>', date('Y-m-d H:i:s', $startTime)];
- $condition_log[] = ['CustomerVisitLog.addtime', '<', date('Y-m-d H:i:s', $endTime)];
- $customer_ids_2_find = Db::view('Customer')
- ->view('Org', '', 'Org.id=Customer.org_id')
- ->view('CustomerVisitLog', '', 'Customer.id=CustomerVisitLog.customer_id')
- ->where($condition_log)
- ->group('Customer.id')
- ->column('Customer.id');
- $customer_ids_2 = array_intersect($customer_ids_2_find, $ids);
- $customer_ids = array_values(array_unique(array_merge($customer_ids_1, $customer_ids_2)));
- return $customer_ids;
- }
- /**
- * 团队统计数据
- * @param string $start_date
- * @param string $end_date
- * @return string
- */
- public function statistics()
- {
- $type = input('type', '', 'trim');
- $search_type = input('search_type', 1, 'intval'); //数据查询方式 1,以客户为主导 2,以满足的搜索状态为主导,合并出所有客户
- $param = $this->request->only(['addtime' => '', 'followtime' => '', 'source' => '', 'employee_id' => '', 'org_id' => '', 'type' => '']);
- $startTime = strtotime('2015-01-01');
- $endTime = strtotime(date('Y-m-d')) + 86400;
- if ($search_type == 1) {
- if (!empty($param['addtime'])) {
- //录入时间
- $addtimeArr = explode(' - ', $param['addtime']);
- $start = $addtimeArr[0];
- $end = date('Y-m-d', strtotime($addtimeArr[1]) + 86400);
- $startTime = strtotime($start) - 1;
- $endTime = strtotime($end);
- }
- } else {
- if (!empty($param['followtime'])) {
- //录入时间
- $addtimeArr = explode(' - ', $param['followtime']);
- $start = $addtimeArr[0];
- $end = date('Y-m-d', strtotime($addtimeArr[1]) + 86400);
- $startTime = strtotime($start) - 1;
- $endTime = strtotime($end);
- }
- }
- $is_manager = request()->empcrm->is_manager;
- $employee_id = request()->empcrm->id;
- $org_id = request()->empcrm->org_id;
- if ($type) {
- $condition_res = $this->getCondition();
- $condition = $condition_res['condition'];
- $condition_ids = $condition_res['ids'];
- // 客户总数
- $customer_ids = Db::view('Customer')
- ->view('Org', '', 'Org.id=Customer.org_id')
- ->view('CustomerVisitLog', '', 'Customer.id=CustomerVisitLog.customer_id')
- ->where($condition)
- ->group('Customer.id')
- ->column('Customer.id');
- $customer_ids = array_intersect($customer_ids, $condition_ids);
- $customer_num = count($customer_ids);
- if ($type == 'jiav') {
- $jiawei = $customer_num;
- } else {
- $jiav_condition = $condition;
- $jiav_condition[] = ['Customer.add_wechat_time', '<>', ''];
- $jiaweiIds = Db::view('Customer')
- ->view('Org', '', 'Org.id=Customer.org_id')
- ->view('CustomerVisitLog', '', 'Customer.id=CustomerVisitLog.customer_id')
- ->where($jiav_condition)
- ->group('Customer.id')
- ->column('Customer.id');
- $jiaweiIds = array_intersect($jiaweiIds, $condition_ids);
- $jiawei = count($jiaweiIds);
- }
- $signed_money = Customer::where('id', 'in', $customer_ids)->sum('signed_money');
- $customerData['signed_money'] = $signed_money ? round($signed_money / 10000, 2) : 0;
- } else {
- if ($search_type == 1) {
- $org_type = Org::where('id', '=', $org_id)->value('org_type');
- if ($org_type == 2) {
- if ($is_manager == 1) {
- if ($param['org_id']) {
- $param_sub_org = orgSubIds($param['org_id']);
- } else {
- $param_sub_org = orgSubIds($org_id);
- }
- $designer_employee = Employee::where([['org_id', 'in', $param_sub_org], ['uid', '<>', 0], ['state', '=', '在职']])->column('id');
- $condition[] = ['designer_id', 'in', $designer_employee];
- // 是哪个员工
- setCondition($param['employee_id'], 'designer_id', '=', $condition);
- } else {
- $condition[] = ['designer_id', '=', $employee_id];
- }
- } else {
- if ($is_manager == 1) {
- if ($param['org_id']) {
- $param_sub_org = orgSubIds($param['org_id']);
- } else {
- $param_sub_org = orgSubIds($org_id);
- }
- $condition[] = ['org_id', 'in', $param_sub_org];
- $condition[] = ['employee_id', '>', 0];
- // 是哪个员工
- setCondition($param['employee_id'], 'employee_id', '=', $condition);
- } else {
- $condition[] = ['employee_id', '=', $employee_id];
- }
- }
- $condition[] = ['died', '<>', 2];
- $condition[] = ['state', 'not in', Customer::changeState('无效', 'chaos')];
- $condition[] = ['addtime', '>', date('Y-m-d H:i:s', $startTime)];
- $condition[] = ['addtime', '<', date('Y-m-d H:i:s', $endTime)];
- $state_n = Customer::changeState('待确认', 'chaos');
- $state_w = Customer::changeState('无效', 'chaos');
- $query1 = array_merge($condition, [['state', 'not in', array_merge($state_n, $state_w)]]);
- $query2 = array_merge($condition, [['state', 'in', $state_n], ['crm_res_id', 'NULL', NULL]]);
- $query3 = array_merge($condition, [['state', 'in', $state_n], ['crm_res_id', 'NOTNULL', NULL], ['valid_time', 'NOTNULL', NULL]]);
- $query_ids = Customer::whereOr([$query1, $query2, $query3])->column('id');
- $customer_num = count($query_ids);
- // 加微数量
- $condition1 = $condition;
- $condition1[] = ['add_wechat_time', '<>', ''];
- $jiawei_ids_find = Customer::where($condition1)->column('id');
- $jiawei_ids = array_intersect($jiawei_ids_find, $query_ids);
- $jiawei = count($jiawei_ids);
- $signed_money = Customer::where('id', 'in', $query_ids)->sum('signed_money');
- $customerData['signed_money'] = $signed_money ? round($signed_money / 10000, 2) : 0;
- } else {
- $customer_ids = $this->logCondition();
- $customer_num = count($customer_ids);
- // 加微数量
- $condition1[] = ['id', 'in', $customer_ids];
- $condition1[] = ['add_wechat_time', '<>', ''];
- $jiawei = Customer::where($condition1)->count();
- $signed_money = Customer::where($condition1)->sum('signed_money');
- $customerData['signed_money'] = $signed_money ? round($signed_money / 10000, 2) : 0;
- }
- }
- $customerData['customer_num'] = $customer_num;
- $customerData['visit_num'] = $jiawei;
- return json(['code' => 0, 'data' => $customerData]);
- }
- /**
- * 团队统计数据
- * @param string $start_date
- * @param string $end_date
- * @return string
- */
- public function statistics_more()
- {
- $type = input('type', '', 'trim');
- $search_type = input('search_type', 1, 'intval'); //数据查询方式 1,以客户为主导 2,以满足的搜索状态为主导,合并出所有客户
- $param = $this->request->only(['addtime' => '', 'followtime' => '', 'source' => '', 'employee_id' => '', 'org_id' => '', 'type' => '']);
- $is_manager = request()->empcrm->is_manager;
- $employee_id = request()->empcrm->id;
- $org_id = request()->empcrm->org_id;
- $startTime = strtotime('2015-01-01');
- $endTime = strtotime(date('Y-m-d')) + 86400;
- if ($search_type == 1) {
- if (!empty($param['addtime'])) {
- //录入时间
- $addtimeArr = explode(' - ', $param['addtime']);
- $start = $addtimeArr[0];
- $end = date('Y-m-d', strtotime($addtimeArr[1]) + 86400);
- $startTime = strtotime($start) - 1;
- $endTime = strtotime($end);
- }
- } else {
- if (!empty($param['followtime'])) {
- //录入时间
- $addtimeArr = explode(' - ', $param['followtime']);
- $start = $addtimeArr[0];
- $end = date('Y-m-d', strtotime($addtimeArr[1]) + 86400);
- $startTime = strtotime($start) - 1;
- $endTime = strtotime($end);
- }
- }
- $org_type = Org::where('id', '=', $org_id)->value('org_type');
- if ($type) {
- $condition_res = $this->getCondition();
- $condition = $condition_res['condition'];
- $condition_ids = $condition_res['ids'];
- // 客户总数
- $customerIdList = Db::view('Customer')
- ->view('Org', '', 'Org.id=Customer.org_id')
- ->view('CustomerVisitLog', '', 'Customer.id=CustomerVisitLog.customer_id')
- ->where($condition)
- ->group('Customer.id')
- ->column('Customer.id');
- $customerIdList = array_intersect($customerIdList, $condition_ids);
- } else {
- if ($search_type == 1) {
- // 设计师部门管理员,需要查询指派给下属设计师的客户信息
- if ($org_type == 2) {
- if ($is_manager == 1) {
- if ($param['org_id']) {
- $param_sub_org = orgSubIds($param['org_id']);
- } else {
- $param_sub_org = orgSubIds($org_id);
- }
- $designer_employee = Employee::where([['org_id', 'in', $param_sub_org], ['uid', '<>', 0], ['state', '=', '在职']])->column('id');
- $condition[] = ['designer_id', 'in', $designer_employee];
- // 是哪个员工
- setCondition($param['employee_id'], 'designer_id', '=', $condition);
- } else {
- $condition[] = ['designer_id', '=', $employee_id];
- }
- } else {
- if ($is_manager == 1) {
- if ($param['org_id']) {
- $param_sub_org = orgSubIds($param['org_id']);
- } else {
- $param_sub_org = orgSubIds($org_id);
- }
- $condition[] = ['org_id', 'in', $param_sub_org];
- $condition[] = ['employee_id', '>', 0];
- // 是哪个员工
- setCondition($param['employee_id'], 'employee_id', '=', $condition);
- } else {
- $condition[] = ['employee_id', '=', $employee_id];
- }
- }
- $condition[] = ['died', '<>', 2];
- $condition[] = ['state', 'not in', Customer::changeState('无效', 'chaos')];
- $condition[] = ['addtime', 'between', [date('Y-m-d H:i:s', $startTime), date('Y-m-d H:i:s', $endTime)]];
- $state_n = Customer::changeState('待确认', 'chaos');
- $state_w = Customer::changeState('无效', 'chaos');
- $query1 = array_merge($condition, [['state', 'not in', array_merge($state_n, $state_w)]]);
- $query2 = array_merge($condition, [['state', 'in', $state_n], ['crm_res_id', 'NULL', NULL]]);
- $customerIdList = Customer::whereOr([$query1, $query2])->column('id');
- } else {
- $customerIdList = $this->logCondition();
- }
- }
- // 有效
- $state3 = CustomerVisitLog::changeState('已量房', 'chaos');
- $state4 = CustomerVisitLog::changeState('已到店', 'chaos');
- $state5 = CustomerVisitLog::changeState('交定', 'chaos');
- $state6 = CustomerVisitLog::changeState('签单', 'chaos');
- $state7 = CustomerVisitLog::changeState('已到场', 'chaos');
- $state8 = CustomerVisitLog::changeState('预约量房', 'chaos');
- $state9 = CustomerVisitLog::changeState('预约到店', 'chaos');
- $state10 = CustomerVisitLog::changeState('预约活动', 'chaos');
- // 签单
- if ($param['org_id']) {
- $condition_org_path = Org::where('id', '=', $param['org_id'])->value('path');
- } else {
- $condition_org_path = Org::where('id', '=', $org_id)->value('path');
- }
- // 设计师部门管理员,需要查询指派给下属设计师的客户信息
- if ($org_type == 2) {
- $org_id = Org::where([['path', 'like', $condition_org_path . '%']])->column('id');
- $designer_employee = Employee::where([['org_id', 'in', $org_id], ['uid', '<>', 0], ['state', '=', '在职']])->column('id');
- $condition_all[] = ['Customer.employee_id|Customer.designer_id', 'in', $designer_employee];
- } else {
- $condition_all[] = ['Org.path', 'like', $condition_org_path . '%'];
- }
- // 签单 交定 预计量房 量房 预计到店 预约活动 已到店 已到场
- $condition_state = array_merge($state3, $state4, $state5, $state6, $state7, $state8, $state9, $state10);
- $condition_all[] = ['CustomerVisitLog.state', 'in', $condition_state];
- if (empty($type)) {
- $condition_all[] = ['CustomerVisitLog.addtime', 'between', [date('Y-m-d H:i:s', $startTime), date('Y-m-d H:i:s', $endTime)]];
- }
- $visit_list = Db::view('CustomerVisitLog')
- ->view('Customer', '', 'Customer.id=CustomerVisitLog.customer_id')
- ->view('Org', '', 'Org.id=Customer.org_id')
- ->where($condition_all)
- ->column('CustomerVisitLog.customer_id,CustomerVisitLog.state,CustomerVisitLog.addtime');
- $qiandanIds = []; // 签单
- $jiaodingIds = []; // 交定
- $yylfIds = []; //预约量房
- $lfIds = []; // 量房
- $yyddIds = []; // 预约到店
- $yyhdIds = []; // 预约活动
- $yddIds = []; // 已到店
- $ydcIds = []; // 已到场
- $ydc_list = []; // 已到场记录
- $ydd_list = []; // 已到店记录
- foreach ($visit_list as $k => $v) {
- if (in_array($v['state'], $state6)) {
- $qiandanIds[] = $v['customer_id'];
- }
- if (in_array($v['state'], $state5)) {
- $jiaodingIds[] = $v['customer_id'];
- }
- if (in_array($v['state'], $state8)) {
- $yylfIds[] = $v['customer_id'];
- }
- if (in_array($v['state'], $state3)) {
- $lfIds[] = $v['customer_id'];
- }
- if (in_array($v['state'], $state9)) {
- $yyddIds[] = $v['customer_id'];
- }
- if (in_array($v['state'], $state10)) {
- $yyhdIds[] = $v['customer_id'];
- }
- if (in_array($v['state'], $state7)) {
- $ydcIds[] = $v['customer_id'];
- $ydc_list[] = $v;
- }
- if (in_array($v['state'], $state4)) {
- $yddIds[] = $v['customer_id'];
- $ydd_list[] = $v;
- }
- }
- $qiandan = array_intersect($customerIdList, $qiandanIds);
- $jiaoding = array_intersect($customerIdList, $jiaodingIds);
- $yylf_ing = CustomersSubscribe::where([['state', '=', 0], ['type', '=', 3], ['customer_id', 'in', $yylfIds]])->column('customer_id');
- $yylf = array_intersect($customerIdList, $yylf_ing);
- // 量房
- $lf = array_intersect($customerIdList, $lfIds);
- // 预计到店
- $yydd_ing = CustomersSubscribe::where([['state', '=', 0], ['type', '=', 1], ['customer_id', 'in', $yyddIds]])->column('customer_id');
- $yydd = array_intersect($customerIdList, $yydd_ing);
- // 预约活动
- $yyhd_ing = CustomersSubscribe::where([['state', '=', 0], ['type', '=', 2], ['customer_id', 'in', $yyhdIds]])->column('customer_id');
- $yyhd = array_intersect($customerIdList, $yyhd_ing);
- // 已到店
- // 客户5号加微信,10号到店,则10号手机端的到店数据要体现,15号再次到店,则15号手机端不计二次到店的数据,第二月5号再次到店,则5号的数据手机端计1次到店数据 小胖提
- if (empty($type)) {
- $ydd = [];
- $year_s = date('Y', $startTime);
- $year_e = date('Y', $endTime);
- $month_s = intval(date('m', $startTime));
- $month_e = intval(date('m', $endTime));
- if ($year_s != $year_e || $month_s != $month_e) {
- // 跨月
- if ($year_s == $year_e) { // 同一年
- for ($i = 0; $month_s <= $month_e; $i++) {
- if ($i == 0) {
- $startTimeFor = date('Y-m-d H:i:s', $startTime); //首次等于搜索的开始时间
- } else {
- $startTimeFor = $year_s . '-' . $month_s . '-01 00:00:00'; // 后面月份的开始时间
- }
- if ($month_s == $month_e) { // 等于搜索的结束月份
- $endTimeFor = date('Y-m-d H:i:s', $endTime);
- } else { //中间月份的结束时间
- $next_month = strtotime(($year_s . '-' . ($month_s + 1) . '-01 00:00:00')) - 1;
- $endTimeFor = date('Y-m-d 23:59:59', $next_month);
- }
- $month_ids = [];
- $ids_for_sel = [];
- $strtotime_start = strtotime($startTimeFor);
- $strtotime_end = strtotime($endTimeFor);
- foreach ($ydd_list as $k => $v) {
- if (strtotime($v['addtime']) > $strtotime_start && strtotime($v['addtime']) < $strtotime_end) {
- if (!in_array($v['customer_id'], $month_ids)) {
- $ids_for_sel[] = $v['customer_id'];
- }
- unset($ydd_list[$k]);
- }
- }
- $ids_for_one = array_intersect($customerIdList, $ids_for_sel);
- $ydd = array_merge($ydd, $ids_for_one);
- $month_s++; // 月份增加
- }
- } else {
- for ($y = 0; $year_s <= $year_e; $y++) {
- if ($year_s == $year_e) {
- $month_s_for = 1;
- $month_e_for = $month_e;
- for ($i = 0; $month_s_for <= $month_e_for; $i++) {
- $startTimeFor = $year_s . '-' . $month_s_for . '-01 00:00:00';
- if ($month_s_for == $month_e_for) {
- $endTimeFor = date('Y-m-d H:i:s', $endTime);
- } else {
- $next_month = strtotime(($year_s . '-' . ($month_s_for + 1) . '-01 00:00:00')) - 1;
- $endTimeFor = date('Y-m-d 23:59:59', $next_month);
- }
- $month_ids = [];
- $ids_for_sel = [];
- $strtotime_start = strtotime($startTimeFor);
- $strtotime_end = strtotime($endTimeFor);
- foreach ($ydd_list as $k => $v) {
- if (strtotime($v['addtime']) > $strtotime_start && strtotime($v['addtime']) < $strtotime_end) {
- if (!in_array($v['customer_id'], $month_ids)) {
- $ids_for_sel[] = $v['customer_id'];
- }
- unset($ydd_list[$k]);
- }
- }
- $ids_for_one = array_intersect($customerIdList, $ids_for_sel);
- $ydd = array_merge($ydd, $ids_for_one);
- $month_s_for++;
- }
- } else {
- if ($y == 0) {
- $month_s_for = $month_s;
- } else {
- $month_s_for = 1;
- }
- for ($i = 0; $month_s_for <= 12; $i++) {
- if ($y == 0 && $i == 0) {
- $startTimeFor = date('Y-m-d H:i:s', $startTime);
- } else {
- $startTimeFor = $year_s . '-' . $month_s_for . '-01 00:00:00';
- }
- if ($month_s_for == 12) {
- $next_month = strtotime($year_s . '-12-31 23:59:59');
- } else {
- $next_month = strtotime(($year_s . '-' . ($month_s_for + 1) . '-01 00:00:00')) - 1;
- }
- $endTimeFor = date('Y-m-d 23:59:59', $next_month);
- $month_ids = [];
- $ids_for_sel = [];
- $strtotime_start = strtotime($startTimeFor);
- $strtotime_end = strtotime($endTimeFor);
- foreach ($ydd_list as $k => $v) {
- if (strtotime($v['addtime']) > $strtotime_start && strtotime($v['addtime']) < $strtotime_end) {
- if (!in_array($v['customer_id'], $month_ids)) {
- $ids_for_sel[] = $v['customer_id'];
- }
- unset($ydd_list[$k]);
- }
- }
- $ids_for_one = array_intersect($customerIdList, $ids_for_sel);
- $ydd = array_merge($ydd, $ids_for_one);
- $month_s_for++;
- }
- }
- $year_s++; // 下一年
- }
- }
- } else {
- $ydd = array_intersect($customerIdList, $yddIds);
- }
- } else {
- $ydd = array_values(array_intersect($customerIdList, $yddIds));
- }
- // 已到场
- if (empty($type)) {
- $ydc = [];
- $year_s = date('Y', $startTime);
- $year_e = date('Y', $endTime);
- $month_s = intval(date('m', $startTime));
- $month_e = intval(date('m', $endTime));
- if ($year_s != $year_e || $month_s != $month_e) {
- // 跨月
- if ($year_s == $year_e) { // 同一年
- for ($i = 0; $month_s <= $month_e; $i++) {
- if ($i == 0) {
- $startTimeFor = date('Y-m-d H:i:s', $startTime); //首次等于搜索的开始时间
- } else {
- $startTimeFor = $year_s . '-' . $month_s . '-01 00:00:00'; // 后面月份的开始时间
- }
- if ($month_s == $month_e) { // 等于搜索的结束月份
- $endTimeFor = date('Y-m-d H:i:s', $endTime);
- } else { //中间月份的结束时间
- $next_month = strtotime(($year_s . '-' . ($month_s + 1) . '-01 00:00:00')) - 1;
- $endTimeFor = date('Y-m-d 23:59:59', $next_month);
- }
- $month_ids = [];
- $ids_for_sel = [];
- $strtotime_start = strtotime($startTimeFor);
- $strtotime_end = strtotime($endTimeFor);
- foreach ($ydc_list as $k => $v) {
- if (strtotime($v['addtime']) > $strtotime_start && strtotime($v['addtime']) < $strtotime_end) {
- if (!in_array($v['customer_id'], $month_ids)) {
- $ids_for_sel[] = $v['customer_id'];
- }
- unset($ydc_list[$k]);
- }
- }
- $ids_for_one = array_intersect($customerIdList, $ids_for_sel);
- $ydc = array_merge($ydc, $ids_for_one);
- $month_s++; // 月份增加
- }
- } else {
- for ($y = 0; $year_s <= $year_e; $y++) {
- if ($year_s == $year_e) {
- $month_s_for = 1;
- $month_e_for = $month_e;
- for ($i = 0; $month_s_for <= $month_e_for; $i++) {
- $startTimeFor = $year_s . '-' . $month_s_for . '-01 00:00:00';
- if ($month_s_for == $month_e_for) {
- $endTimeFor = date('Y-m-d H:i:s', $endTime);
- } else {
- $next_month = strtotime(($year_s . '-' . ($month_s_for + 1) . '-01 00:00:00')) - 1;
- $endTimeFor = date('Y-m-d 23:59:59', $next_month);
- }
- $month_ids = [];
- $ids_for_sel = [];
- $strtotime_start = strtotime($startTimeFor);
- $strtotime_end = strtotime($endTimeFor);
- foreach ($ydc_list as $k => $v) {
- if (strtotime($v['addtime']) > $strtotime_start && strtotime($v['addtime']) < $strtotime_end) {
- if (!in_array($v['customer_id'], $month_ids)) {
- $ids_for_sel[] = $v['customer_id'];
- }
- unset($ydc_list[$k]);
- }
- }
- $ids_for_one = array_intersect($customerIdList, $ids_for_sel);
- $ydc = array_merge($ydc, $ids_for_one);
- $month_s_for++;
- }
- } else {
- if ($y == 0) {
- $month_s_for = $month_s;
- } else {
- $month_s_for = 1;
- }
- for ($i = 0; $month_s_for <= 12; $i++) {
- if ($y == 0 && $i == 0) {
- $startTimeFor = date('Y-m-d H:i:s', $startTime);
- } else {
- $startTimeFor = $year_s . '-' . $month_s_for . '-01 00:00:00';
- }
- if ($month_s_for == 12) {
- $next_month = strtotime($year_s . '-12-31 23:59:59');
- } else {
- $next_month = strtotime(($year_s . '-' . ($month_s + 1) . '-01 00:00:00')) - 1;
- }
- $endTimeFor = date('Y-m-d 23:59:59', $next_month);
- $month_ids = [];
- $ids_for_sel = [];
- $strtotime_start = strtotime($startTimeFor);
- $strtotime_end = strtotime($endTimeFor);
- foreach ($ydc_list as $k => $v) {
- if (strtotime($v['addtime']) > $strtotime_start && strtotime($v['addtime']) < $strtotime_end) {
- if (!in_array($v['customer_id'], $month_ids)) {
- $ids_for_sel[] = $v['customer_id'];
- }
- unset($ydc_list[$k]);
- }
- }
- $ids_for_one = array_intersect($customerIdList, $ids_for_sel);
- $ydc = array_merge($ydc, $ids_for_one);
- $month_s_for++;
- }
- }
- $year_s++; // 下一年
- }
- }
- } else {
- $ydc = array_intersect($customerIdList, $ydcIds);
- }
- } else {
- $ydc = array_intersect($customerIdList, $ydcIds);
- }
- $customerData['signed_num'] = count($qiandan);
- $customerData['deposit_num'] = count($jiaoding);
- $customerData['yylf_num'] = count($yylf);
- $customerData['lf_num'] = count($lf);
- $customerData['yydd_num'] = count($yydd);
- $customerData['ydd_num'] = count($ydd);
- $customerData['yyhd_num'] = count($yyhd);
- $customerData['ydc_num'] = count($ydc);
- return json(['code' => 0, 'data' => $customerData]);
- }
- /************************************************* 客户池仿手机端团队客户搜索 end ***************************************/
- /**
- * 客户池
- */
- public function list()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $is_manager = $request->empcrm->is_manager;
- $sub_orgs = orgSubIds($request->empcrm->org_id);
- $org_id = input('org_id', $request->empcrm->org_id, 'intval');
- if (!in_array($org_id, $sub_orgs)) {
- $org_id = $request->empcrm->org_id;
- }
- if ($is_manager) {
- $team_orgs = orgSubIds($org_id);
- } else {
- $team_orgs = [$org_id];
- }
- if (!$request->isAjax()) {
- //来源
- $soudata = CustomerSource::field('id,source as name')->where('root_id', $root_id)->select()->toArray();
- View::assign('soudata', $soudata);
- //设计师
- $list = $this->get_designer();
- if (!empty($list)) {
- $sort = array_column($list, 's');
- sort($sort);
- array_multisort($sort, SORT_ASC, $list);
- $sort = array_unique($sort);
- $list = ['sort' => $sort, 'arr' => $list];
- } else {
- $list = ['sort' => [], 'arr' => []];
- }
- View::assign('designers', $list);
- //客户等级
- $level = CustomerPortraitField::with(['select'])->where([['root_id', '=', $root_id], ['keyname', '=', 'level']])->find()->toArray();
- View::assign('level', $level['select']);
- //部门所有员工
- $employee_list = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->field('id,name')->select()->toArray();
- View::assign('employee_list', $employee_list);
- View::assign('empid', $empid);
- View::assign('is_manager', $is_manager);
- //指派经理
- $assignemp_list = Employee::where([['org_id', 'in', orgSubIds($root_id)], ['id', 'not in', array_column($list['arr'], 'id')], ['state', '=', '在职'], ['uid', '>', 0]])->column('id,name');
- View::assign('assignemp_list', $assignemp_list);
- // 馨居上
- //View::assign('xinjushang', $request->empcrm['xinjushang']);
- //员工所属部门
- $emporg_list = Org::where([['id', 'in', $team_orgs]])->column('id,name');
- View::assign('emporg_list', $emporg_list);
- //设计师所属部门
- $designer_org_list = Org::where([['path', 'like', $root_id . '-%'], ['org_type', '=', 2]])->column('name,id');
- View::assign('designer_org_list', $designer_org_list);
- // 统计模块的控制
- $statistics_setting = Setting::where([['name', '=', 'empcrm_pool_statistics'], ['root_id', '=', $root_id]])->findOrEmpty();
- $statistics_setting_content = !$statistics_setting->isEmpty() ? $statistics_setting['content'] : 0;
- View::assign('statistics_setting', $statistics_setting_content);
- //统计字段展示控制权限
- $type_arr = ['customer_num' => 1, 'visit_num' => 1, 'valid_count' => 1, 'deposit_num' => 1, 'signed_num' => 1, 'yylf_num' => 1, 'lf_num' => 1, 'yydd_num' => 1, 'ydd_num' => 1, 'yyhd_num' => 1, 'ydc_num' => 1];
- $setting = Setting::where([['name', '=', 'teamStatisticsJurisdiction'], ['root_id', '=', $root_id]])->value('content');
- $setting = $setting ? explode(',', $setting) : [];
- foreach ($type_arr as $k => $v) {
- $type_arr[$k] = in_array($k, $setting) ? 1 : 0;
- }
- $sum = array_sum(array_values($type_arr));
- if ($sum < 5) $type_arr = ['customer_num' => 1, 'visit_num' => 1, 'valid_count' => 1, 'deposit_num' => 1, 'signed_num' => 1, 'yylf_num' => 1, 'lf_num' => 1, 'yydd_num' => 1, 'ydd_num' => 1, 'yyhd_num' => 1, 'ydc_num' => 1];
- View::assign('setting', $type_arr);
- return View::fetch();
- }
- $param = $request->only(['page' => 1, 'limit' => 10, 'addtime' => '', 'level' => '', 'source_name' => '', 'designer_name' => '', 'keyname' => '', 'followtime' => '', 'state' => '', 'employee_name' => '', 'type' => '', 'is_assign']);
- $search_type = input('search_type', 1, 'intval'); //数据查询方式 1,以客户为主导 2,以满足的搜索状态为主导,合并出所有客户
- $startTime = strtotime('2015-01-01');
- $endTime = strtotime(date('Y-m-d')) + 86400;
- $array_intersect = false;
- $array_intersect_ids = [];
- if ($param['type']) {
- $condition_res = $this->getCondition();
- $condition_get = $condition_res['condition'];
- $condition_ids = $condition_res['ids'];
- // 客户总数
- $customerIdList = Db::view('Customer')
- ->view('Org', '', 'Org.id=Customer.org_id')
- ->view('CustomerVisitLog', '', 'Customer.id=CustomerVisitLog.customer_id')
- ->where($condition_get)
- ->group('Customer.id')
- ->column('Customer.id');
- $customerIdList = array_intersect($customerIdList, $condition_ids);
- $condition[] = ['id', 'in', $customerIdList];
- } else {
- if ($search_type == 1) {
- $org_type = Org::where('id', '=', $org_id)->value('org_type');
- // 设计师部门管理员,需要查询指派给下属设计师的客户信息
- if ($org_type == 2) {
- if ($is_manager == 1) {
- $designer_employee = Employee::where([['org_id', 'in', $team_orgs], ['uid', '<>', 0], ['state', '=', '在职']])->column('id');
- $condition[] = ['designer_id', 'in', $designer_employee];
- // 是哪个员工
- // setCondition($param['employee_id'], 'designer_id', '=', $condition);
- if ($param['employee_name']) $condition[] = ['employee_id', 'in', explode(',', $param['employee_name'])];
- } else {
- $condition[] = ['designer_id', '=', $empid];
- }
- } else {
- if ($is_manager == 1) {
- $condition[] = ['org_id', 'in', $team_orgs];
- $condition[] = ['employee_id', '>', 0];
- // 是哪个员工
- // setCondition($param['employee_id'], 'employee_id', '=', $condition);
- if ($param['employee_name']) $condition[] = ['employee_id', 'in', explode(',', $param['employee_name'])];
- } else {
- $condition[] = ['employee_id', '=', $empid];
- }
- }
- $condition[] = ['state', 'not in', Customer::changeState('无效', 'chaos')];
- $condition[] = ['died', '<>', 2];
- if (!empty($param['addtime'])) {
- //录入时间
- $addtimeArr = explode(' - ', $param['addtime']);
- $start = $addtimeArr[0];
- $end = date('Y-m-d', strtotime($addtimeArr[1]) + 86400);
- $startTime = strtotime($start) - 1;
- $endTime = strtotime($end);
- }
- $condition[] = ['addtime', '>', date('Y-m-d H:i:s', $startTime)];
- $condition[] = ['addtime', '<', date('Y-m-d H:i:s', $endTime)];
- } else {
- $array_intersect_ids = $this->logCondition();
- $array_intersect = true;
- $org_type = Org::where('id', '=', $org_id)->value('org_type');
- // 设计师部门管理员,需要查询指派给下属设计师的客户信息
- if ($org_type == 2) {
- if ($is_manager == 1) {
- $designer_employee = Employee::where([['org_id', 'in', $team_orgs], ['uid', '<>', 0], ['state', '=', '在职']])->column('id');
- $condition[] = ['designer_id', 'in', $designer_employee];
- // 是哪个员工
- setCondition($param['employee_name'], 'designer_id', '=', $condition);
- } else {
- $condition[] = ['designer_id', '=', $empid];
- }
- } else {
- if ($is_manager == 1) {
- $condition[] = ['org_id', 'in', $team_orgs];
- $condition[] = ['employee_id', '>', 0];
- // 是哪个员工
- setCondition($param['employee_name'], 'employee_id', '=', $condition);
- } else {
- $condition[] = ['employee_id', '=', $empid];
- }
- }
- $condition[] = ['state', 'not in', Customer::changeState('无效', 'chaos')];
- $condition[] = ['died', '<>', 2];
- }
- }
- //$condition[] = ['transfer_info', 'null', ''];
- //$condition[] = ['state', 'not in', array_merge(Customer::changeState('无效', 'chaos'), Customer::changeState('签单', 'chaos'))];
- if (!empty($param['level'])) $condition[] = ['level', 'in', explode(',', $param['level'])];
- if (!empty($param['source_name'])) $condition[] = ['source_id', 'in', explode(',', $param['source_name'])];
- if (!empty($param['designer_name'])) $condition[] = ['designer_id', 'in', explode(',', $param['designer_name'])];
- if (!empty($param['is_assign']) && $param['is_assign'] == 1) $condition[] = ['assigned_personnel', 'NOTNULL', null];
- if (!empty($param['is_assign']) && $param['is_assign'] == 2) $condition[] = ['assigned_personnel', 'NULL', null];
- //2023-02-22 搜索条件修改
- $new_params = $request->only(['designer_org', 'emporg', 'assign_list', 'revisit_time' => '', 'phone' => '', 'name' => '', 'community_name' => '', 'customer_type' => '', 'first_liangfang' => '', 'last_contact_date' => '', 'fisttime' => '', 'house_delivery_time' => '', 'subscribe_date' => '', 'square' => '', 'no_visit_day' => '', 'sign_time' => '', 'add_wechat_time' => '', 'jiaoding_time' => '', 'qiandan_time' => '']);
- if (!empty($new_params['phone'])) {
- $list = Customer::where($condition)->column('id,phone');
- $keyCustomersId = [];
- $cypherphone = cypherphone($new_params['phone']);
- foreach ($list as $k => $v) {
- if ($cypherphone == $v['phone']) $keyCustomersId[] = $v['id'];
- // if (strpos($k, trim($new_params['phone'])) !== false) $keyCustomersId[] = $v;
- }
- unset($condition);
- $condition[] = ['id', 'in', $keyCustomersId];
- }
- //客户姓名
- if ($new_params['name']) $condition[] = ['name', 'like', '%' . $new_params['name'] . '%'];
- //小区名称
- if ($new_params['community_name']) $condition[] = ['community_name', 'like', '%' . $new_params['community_name'] . '%'];
- //客户种类 资源库,活动,经纪人,自建
- if ($new_params['customer_type']) {
- $gather = [
- 1 => [['crm_res_id', 'not null', null]], //'资源库'
- 2 => [['crm_res_id', 'null', null], ['remark', 'not like', '%活动报名建档%'], ['agents_id', 'null', null]], //'自建'
- 3 => [['remark', 'like', '%活动报名建档%']], //'活动'
- 4 => [['agents_id', '>', 0]] //'经纪人'
- ];
- $arr = explode(',', $new_params['customer_type']);
- $whereOr = [];
- foreach ($arr as $val) {
- if (isset($gather[$val])) {
- //$whereOr[] = array_merge($condition,$gather[$val]);
- $whereOr[] = $gather[$val];
- }
- }
- if (!empty($whereOr)) {
- $new_cids = Customer::where($condition)->where(function ($query) use ($whereOr) {
- $query->whereOr($whereOr);
- })->column('id');
- unset($condition);
- $condition[] = ['id', 'in', $new_cids];
- } else {
- //无效条件
- return json(['code' => 0, 'data' => [], 'count' => 0]);
- }
- }
- //加微时间
- if (!empty($new_params['add_wechat_time'])) {
- $add_wechat_time = explode(' - ', $new_params['add_wechat_time']);
- $start = str_replace('/', '-', $add_wechat_time[0] . ' 00:00:00');
- $end = str_replace('/', '-', $add_wechat_time[1] . ' 23:59:59');
- $condition[] = ['add_wechat_time', '>=', $start];
- $condition[] = ['add_wechat_time', '<=', $end];
- }
- //下次回访时间
- if (!empty($new_params['revisit_time'])) {
- $revisit_time = explode(' - ', $new_params['revisit_time']);
- $start = str_replace('/', '-', $revisit_time[0] . ' 00:00:00');
- $end = str_replace('/', '-', $revisit_time[1] . ' 23:59:59');
- $condition[] = ['revisit_time', '>', $start];
- $condition[] = ['revisit_time', '<', $end];
- }
- //交房时间
- if (!empty($new_params['house_delivery_time'])) {
- $house_delivery_time = explode(' - ', $new_params['house_delivery_time']);
- $start = $house_delivery_time[0];
- $end = $house_delivery_time[1] . ' 23:59:59';
- $condition[] = ['house_delivery_time', '>=', $start];
- $condition[] = ['house_delivery_time', '<=', $end];
- }
- //报名时间
- if (!empty($new_params['sign_time'])) {
- $sign_time = explode(' - ', $new_params['sign_time']);
- $start = str_replace('/', '-', $sign_time[0] . ' 00:00:00');
- $end = str_replace('/', '-', $sign_time[1] . ' 23:59:59');
- $condition[] = ['sign_time', '>=', $start];
- $condition[] = ['sign_time', '<=', $end];
- }
- //客户的跟进时间
- if (!empty($new_params['last_contact_date'])) {
- $last_contact = explode(' - ', $new_params['last_contact_date']);
- $start = str_replace('/', '-', $last_contact[0]);
- $end = str_replace('/', '-', $last_contact[1]);
- $condition[] = ['last_contact_date', '>=', $start];
- $condition[] = ['last_contact_date', '<=', $end];
- }
- //首次量房时间
- if (!empty($new_params['first_liangfang'])) {
- $first_liangfang = explode(' - ', $new_params['first_liangfang']);
- $start = str_replace('/', '-', $first_liangfang[0]);
- $end = date('Y-m-d', strtotime($first_liangfang[1]) + 86400);
- $customersIdList = Customer::where($condition)->order('addtime desc')->column('id');
- $lf_where[] = ['customer_id', 'in', $customersIdList];
- $lf_where[] = ['state', 'in', CustomerVisitLog::changeState('已量房', 'chaos')];
- $lf_where[] = ['addtime', '>=', $start];
- $lf_where[] = ['addtime', '<=', $end];
- $lflog = CustomerVisitLog::where($lf_where)->group('customer_id')->order('addtime asc')->column('customer_id');
- unset($condition);
- $condition[] = ['id', 'in', $lflog];
- }
- //首次到店时间
- if (!empty($new_params['fisttime'])) {
- $fisttime = explode(' - ', $new_params['fisttime']);
- $start = str_replace('/', '-', $fisttime[0]);
- $end = date('Y-m-d', strtotime($fisttime[1]) + 86400);
- $customersIdList = Customer::where($condition)->order('addtime desc')->column('id');
- $dd_where[] = ['customer_id', 'in', $customersIdList];
- $dd_where[] = ['state', 'in', CustomerVisitLog::changeState('已到店', 'chaos')];
- $dd_where[] = ['confirm_date', '>=', $start];
- $dd_where[] = ['confirm_date', '<=', $end];
- $ddlog = CustomerVisitLog::where($dd_where)->group('customer_id')->order('addtime asc')->column('customer_id');
- unset($condition);
- $condition[] = ['id', 'in', $ddlog];
- }
- //预计见面时间
- if (!empty($new_params['subscribe_date'])) {
- $subscribe_date = explode(' - ', $new_params['subscribe_date']);
- $start = $subscribe_date[0];
- $end = date('Y-m-d', strtotime($subscribe_date[1]) + 86400);
- $customersIdList = Customer::where($condition)->order('addtime desc')->column('id');
- $yj_where[] = ['customer_id', 'in', $customersIdList];
- $yj_where[] = ['state', '=', 0];
- $yj_where[] = ['subscribe_date', '>=', $start];
- $yj_where[] = ['subscribe_date', '<=', $end];
- $sublog = CustomersSubscribe::where($yj_where)->group('customer_id')->order('subscribe_date desc')->column('customer_id');
- unset($condition);
- $condition[] = ['id', 'in', $sublog];
- }
- //签单时间
- if (!empty($new_params['jiaoding_time'])) {
- $fisttime = explode(' - ', $new_params['jiaoding_time']);
- $start = str_replace('/', '-', $fisttime[0] . ' 00:00:00');
- $end = str_replace('/', '-', $fisttime[1] . ' 23:59:59');
- $customersIdList = Customer::where($condition)->order('addtime desc')->column('id');
- $dd_where[] = ['customer_id', 'in', $customersIdList];
- $dd_where[] = ['state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')];
- $ddlog = CustomerVisitLog::where($dd_where)->whereBetween('addtime', [$start, $end])->group('customer_id')->order('addtime asc')->column('customer_id');
- unset($condition);
- $condition[] = ['id', 'in', $ddlog];
- }
- //签单时间
- if (!empty($new_params['qiandan_time'])) {
- $fisttime = explode(' - ', $new_params['qiandan_time']);
- $start = str_replace('/', '-', $fisttime[0] . ' 00:00:00');
- $end = str_replace('/', '-', $fisttime[1] . ' 23:59:59');
- $customersIdList = Customer::where($condition)->order('addtime desc')->column('id');
- $dd_where[] = ['customer_id', 'in', $customersIdList];
- $dd_where[] = ['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')];
- $ddlog = CustomerVisitLog::where($dd_where)->whereBetween('addtime', [$start, $end])->group('customer_id')->order('addtime asc')->column('customer_id');
- unset($condition);
- $condition[] = ['id', 'in', $ddlog];
- }
- //客户经理
- if (!empty($new_params['assign_list'])) {
- $assign_employee = explode(',', $new_params['assign_list']);
- $customerIdList = Customer::where($condition)->where(function ($query) use ($assign_employee) {
- foreach ($assign_employee as $v) {
- $query->whereOr([['assigned_personnel', 'find in set', $v]]);
- }
- })->column('id');
- unset($condition);
- $condition[] = ['id', 'in', $customerIdList];
- }
- //未跟进天数
- if (!empty($new_params['no_visit_day'])) {
- $no_visit_day = explode(',', $new_params['no_visit_day']);
- $all_no_visit = [
- 1 => [3, 7],
- 2 => [8, 14],
- 3 => [15, 30],
- 4 => [31, 10000]
- ];
- $customersList = Customer::where($condition)->order('addtime desc')->column('id,addtime');
- $wg_where[] = ['customer_id', 'in', array_column($customersList, 'id')];
- $latest_visit_log = CustomerVisitLog::where($wg_where)->group('customer_id')->column('max(addtime) as addtime', 'customer_id'); //->select()->toArray();
- $no_visit_ids = [];
- foreach ($customersList as $k => $v) {
- if (empty($latest_visit_log[$v['id']])) {
- $no_visit_long = ceil((time() - strtotime($v['addtime'])) / 86400);
- } else {
- $no_visit_long = ceil((time() - strtotime($latest_visit_log[$v['id']])) / 86400);
- }
- // if($new_params['no_visit_day'] < 40){
- // if($no_visit_long <= $new_params['no_visit_day']) $no_visit_ids[] = $v['id'];
- // }
- // if($new_params['no_visit_day'] == 40){
- // if($no_visit_long > 30) $no_visit_ids[] = $v['id'];
- // }
- foreach ($no_visit_day as $m => $p) {
- if (empty($all_no_visit[$p])) continue;
- if ($no_visit_long >= $all_no_visit[$p][0] && $no_visit_long <= $all_no_visit[$p][1]) $no_visit_ids[] = $v['id'];
- }
- }
- unset($condition);
- $condition[] = ['id', 'in', $no_visit_ids];
- }
- // 房屋面积
- if (!empty($new_params['square'])) {
- $square = explode(',', $new_params['square']);
- $square_arr = [
- 1 => '(square > 1 and square <= 80)',
- 2 => '(square > 81 and square <= 100)',
- 3 => '(square > 101 and square <= 120)',
- 4 => '(square > 121 and square <= 200)',
- 5 => '(square > 201 and square <= 500)',
- 6 => '(square > 501 and square <= 10000)'
- ];
- $squ_whereOr = null;
- foreach ($square as $k => $v) {
- if (!empty($square_arr[$v])) $squ_whereOr .= $square_arr[$v] . ' OR';
- }
- $customersList = Customer::where($condition)->where(function ($query) use ($squ_whereOr) {
- $query->whereRaw(trim($squ_whereOr, ' OR'));
- })->order('addtime desc')->column('id');
- unset($condition);
- $condition[] = ['id', 'in', $customersList];
- }
- //业务员所属部门
- if (!empty($new_params['emporg'])) {
- $orgids = explode(',', $new_params['emporg']);
- $empids = Employee::where([['root_id', '=', $root_id], ['org_id', 'in', $orgids]])->column('id');
- $customersList = Customer::where($condition)->where('employee_id', 'in', $empids)->column('id');
- unset($condition);
- $condition[] = ['id', 'in', $customersList];
- }
- //设计师所属部门
- if (!empty($new_params['designer_org'])) {
- $orgids = explode(',', $new_params['designer_org']);
- $empids = Employee::where([['root_id', '=', $root_id], ['org_id', 'in', $orgids]])->column('id');
- $customersList = Customer::where($condition)->where('designer_id', 'in', $empids)->column('id');
- unset($condition);
- $condition[] = ['id', 'in', $customersList];
- }
- if ($search_type == 1) {
- if (!empty($param['addtime'])) {
- //录入时间
- $addtimeArr = explode(' - ', $param['addtime']);
- $start = $addtimeArr[0];
- $end = date('Y-m-d', strtotime($addtimeArr[1]) + 86400);
- $startTime = strtotime($start) - 1;
- $endTime = strtotime($end);
- }
- } else {
- if (!empty($param['followtime'])) {
- //录入时间
- $addtimeArr = explode(' - ', $param['followtime']);
- $start = $addtimeArr[0];
- $end = date('Y-m-d', strtotime($addtimeArr[1]) + 86400);
- $startTime = strtotime($start) - 1;
- $endTime = strtotime($end);
- }
- }
- // 状态筛选
- $c_ids = [];
- if (!empty($param['state'])) {
- $condition1[] = ['state', 'in', Customer::changeState('待确认', 'chaos')];
- $condition1[] = ['crm_res_id', 'NULL', null];
- $condition2[] = ['state', 'not in', Customer::changeState('待确认', 'chaos')];
- //从公海获取的客户是资源库的要显示在客户池
- $condition3[] = ['state', 'in', Customer::changeState('待确认', 'chaos')];
- $condition3[] = ['crm_res_id', 'NOTNULL', null];
- $condition3[] = ['valid_time', 'NOTNULL', null];
- $customersIdList = Customer::where($condition)->where(function ($query) use ($condition1, $condition2, $condition3) {
- $query->whereOr([$condition1, $condition2, $condition3]);
- })->order('addtime desc')->column('id');
- $stateall = explode(',', $param['state']);
- $allsta = [];
- foreach ($stateall as $k => $v) {
- if ($v == '已签单') $v = '已交定';
- if ($v == '已转单') $v = '已签单';
- if (in_array($v, ['待确认', '有效', '未到访'])) continue;
- $onestate = CustomerVisitLog::changeState($v, 'chaos');
- $allsta = array_merge($onestate, $allsta);
- }
- $visit_where[] = ['customer_id', 'in', $customersIdList];
- $visit_where[] = ['state', 'in', $allsta];
- $visit_where[] = ['addtime', '>', date('Y-m-d H:i:s', $startTime)];
- $visit_where[] = ['addtime', '<', date('Y-m-d H:i:s', $endTime)];
- $logCustomersIdList = CustomerVisitLog::where($visit_where)->group('customer_id')->column('customer_id');
- $c_ids = $logCustomersIdList;
- foreach ($stateall as $k => $v) {
- if ($v == '待确认') {
- $confirmedids = Customer::where($condition)->where(function ($query) use ($condition1, $condition3) {
- $query->whereOr([$condition1, $condition3]);
- })->column('id');
- }
- if ($v == '有效') {
- $validids = Customer::where($condition)->where('state', 'not in', Customer::changeState('待确认', 'chaos'))->column('id');
- }
- if ($v == '未到访') {
- $novisitids = Customer::where($condition)->where('state', 'in', Customer::changeState('未到访', 'chaos'))->column('id');
- }
- }
- unset($condition, $condition1, $condition2, $condition3);
- if (!empty($confirmedids)) $c_ids = array_merge($confirmedids, $c_ids);
- if (!empty($validids)) $c_ids = array_merge($validids, $c_ids);
- if (!empty($novisitids)) $c_ids = array_merge($novisitids, $c_ids);
- } else {
- //2023-02-10 客户有效状态 为排除待确认和无效, 或者 待确认并且不是资源库过来的
- $state1 = Customer::changeState('待确认', 'chaos');
- $state2 = Customer::changeState('无效', 'chaos');
- $query1 = array_merge($condition, [['state', 'not in', array_merge($state1, $state2)]]);
- $query2 = array_merge($condition, [['state', 'in', $state1], ['crm_res_id', 'NULL', NULL]]);
- $query3 = array_merge($condition, [['state', 'in', $state1], ['crm_res_id', 'NOTNULL', NULL], ['valid_time', 'NOTNULL', null]]);
- $ids = Customer::whereOr([$query1, $query2, $query3])->order('id desc')->column('id');
- unset($condition);
- $c_ids = $ids;
- }
- if ($array_intersect) {
- $c_ids = array_intersect($c_ids, $array_intersect_ids);
- $count = count($c_ids);
- $c_ids = array_slice($c_ids, ($param['page'] - 1) * $param['limit'], $param['limit']);
- $condition[] = ['id', 'in', $c_ids];
- } else {
- $count = count($c_ids);
- $c_ids = array_slice($c_ids, ($param['page'] - 1) * $param['limit'], $param['limit']);
- $condition[] = ['id', 'in', $c_ids];
- }
- $order = isset($param['order']) ? $param['order'] : 'id desc';
- $page = intval($param['page']);
- $limit = intval($param['limit']);
- //$data = $this->selCustomer($condition, $page, $limit, $order, $root_id);
- $data = $this->selCustomer($condition, 0, 0, $order, $root_id);
- $no_visit_state = ['未到访', '待确认'];
- if (!empty($stateall) && in_array('未到访', $stateall)) $no_visit_state = ['待确认'];
- $visit_state = ['已到店', '已量房', '已到场', '已签单', '已转单', '已卖卡'];
- foreach ($data as &$item) {
- //客户种类
- $item['customer_type'] = $item['crm_res_id'] ? '资源库' : ($item['remark'] == '活动报名建档' ? '活动' : ($item['agents_id'] ? '经纪人' : '自建'));
- // 有'已到店', '已量房', '已到场', '已签单', '已转单'状态的话不显示未到访、待确认
- // 有未到访状态不显示待确认
- $state = explode(',', $item['state']);
- $jiaoji = array_intersect($visit_state, $state);
- $no_jiaoji = array_intersect($no_visit_state, $state);
- if (count($no_jiaoji) == 2) { // 有未到访、待确认两个状态
- $state = ['未到访'];
- }
- if (count($state) > 1 && count($jiaoji) > 0) {
- $state = array_diff($state, $no_visit_state);
- }
- $item['state'] = implode(',', $state);
- $item['jiange_day'] = ceil((strtotime(date('Y-m-d')) - strtotime($item['last_contact_date'])) / 86400);
- $item['estimated_sign_money'] = $item['estimated_sign_money'] == 0 ? '' : $item['estimated_sign_money'];
- }
- //$count = Customer::where($condition)->count();
- return json(['code' => 0, 'data' => $data, 'count' => $count]);
- }
- //资源库客户列表
- public function resources_list()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $is_manager = $request->empcrm->is_manager;
- $team_orgs = [$request->empcrm->org_id];
- $org_employee = [$empid];
- // if($is_manager ==1){
- // $team_orgs = orgSubIds($request->empcrm->org_id);
- // $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
- // }
- if (!$request->isAjax()) {
- $label = Setting::where(['root_id' => $root_id, 'name' => 'crm_label'])->value('content');
- $label = empty($label) ? [] : explode(',', $label);
- View::assign('label', $label);
- //部门所有员工
- $employee_list = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->field('id,name')->select()->toArray();
- View::assign('employee_list', $employee_list);
- View::assign('empid', $empid);
- View::assign('is_manager', $is_manager);
- $source_list = CustomerSource::where('root_id', '=', $root_id)->select()->toArray();
- View::assign('source_list', $source_list);
- return View::fetch();
- }
- $param = $request->only(['page' => 1, 'limit' => 10, 'addtime', 'label', 'sign' => 0, 'employee_id', 'source_id' => '']);
- $order = isset($param['order']) ? $param['order'] : 'id desc';
- $page = intval($param['page']);
- $limit = intval($param['limit']);
- // if(!empty($param['employee_id']) && $is_manager ==1) $org_employee = [$param['employee_id']];
- $condition[] = ['employee_id', 'in', $org_employee];
- if (!empty($param['addtime'])) {
- //录入时间
- $addtimeArr = explode(' - ', $param['addtime']);
- $start = $addtimeArr[0];
- $end = date('Y-m-d', strtotime($addtimeArr[1]) + 86400);
- $condition[] = ['addtime', 'between', [$start, $end]];
- }
- if (!empty($param['label'])) $condition[] = ['remark', 'like', '%' . $param['label'] . '%'];
- if ($param['sign'] == 1) $condition[] = ['remark', '<>', ''];
- if ($param['sign'] == 2) $condition[] = ['remark', '=', ''];
- //$condition[] = ['transfer_info', 'null', ''];
- if (!empty($param['source_id'])) $condition[] = ['source_id', '=', $param['source_id']];
- $condition[] = ['state', 'not in', array_merge(Customer::changeState('无效', 'chaos'), Customer::changeState('签单', 'chaos'))];
- //查询资源库分配后待确认客户
- $condition[] = ['state', 'in', Customer::changeState('待确认', 'chaos')];
- $condition[] = ['crm_res_id', 'NOT NULL', null];
- $condition[] = ['valid_time', 'NULL', null];
- $allids = Customer::where($condition)->column('id');
- //$vslogid = CustomerVisitLog::where([['customer_employee_id','in',$org_employee],['state','in',CustomerVisitLog::changeState('已签单','chaos')]])->column('customer_id');
- $vslogid = CustomerVisitLog::where([['customer_id', 'in', $allids], ['state', 'not in', CustomerVisitLog::changeState('待确认', 'chaos')]])->group('customer_id')->column('customer_id');
- $condition[] = ['id', 'not in', $vslogid];
- $data = $this->selCustomer($condition, $page, $limit, $order, $root_id);
- foreach ($data as &$item) {
- //客户种类
- $item['customer_type'] = $item['crm_res_id'] ? '资源库' : ($item['remark'] == '活动报名建档' ? '活动' : ($item['agents_id'] ? '经纪人' : '自建'));
- $callnum = OutCallLog::where([
- ['customer_id', '=', $item['id']],
- ['employee_id', '=', $item['employee_id']],
- ['addtime', '>=', $item['employee_time']]
- ])->count();
- if (empty($callnum)) {
- $callnum = OutCallMbLog::where([
- ['customer_id', '=', $item['id']],
- ['employee_id', '=', $item['employee_id']],
- ['addtime', '>=', $item['employee_time']]
- ])->count();
- }
- $item['callnum'] = $callnum;
- }
- $count = Customer::where($condition)->count();
- return json(['code' => 0, 'data' => $data, 'count' => $count]);
- }
- //资源库标记客户
- public function resources_sign()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $is_manager = $request->empcrm->is_manager;
- $param = $request->only(['id', 'label', 'content' => '']);
- $where = [['id', '=', $param['id']], ['crm_res_id', 'NOTNULL', null], ['employee_id', '=', $empid], ['state', 'in', Customer::changeState('待确认', 'chaos')]];
- if ($is_manager) $where = [['id', '=', $param['id']], ['crm_res_id', 'NOTNULL', null], ['state', 'in', Customer::changeState('待确认', 'chaos')]];
- $customer = Customer::where($where)->find();
- if (empty($customer)) return json(['code' => 1, 'msg' => '客户不存在']);
- if (!$request->isAjax()) {
- $label = Setting::where(['root_id' => $root_id, 'name' => 'crm_label'])->value('content');
- $label = empty($label) ? [] : explode(',', $label);
- View::assign('label', $label);
- View::assign('data', $customer);
- $log = CustomerVisitLog::where(['employee_id' => $empid, 'customer_id' => $param['id'], 'data_type' => 'out_call'])->field('addtime,remark')->order('addtime desc')->select();
- View::assign('log', $log);
- // 判断是否有外呼系统
- $outcallSetting = OutCallSetting::where(['root_id' => $root_id])->find();
- if ($outcallSetting !== null) {
- // 如果有外呼系统则获取登陆人拨打的电话列表
- View::assign('outcall', 1);
- $phones = empty($request->empcrm['out_call_phone']) ? [] : explode(',', $request->empcrm['out_call_phone']);
- array_unshift($phones, $request->empcrm['phone']);
- View::assign('outcall_phone', $phones);
- } else {
- View::assign('outcall', 0);
- }
- $company = Company::where([['root_id', '=', $root_id]])->find();
- View::assign('fish', in_array($customer['source_id'], $company->channelSource['fish']) ? 1 : 0);
- return View::fetch();
- }
- if (empty($param['label']) || empty($param['content'])) return json(['code' => 1, 'msg' => '请选择或填写标记内容']);
- if (!empty($param['label'])) {
- if ($param['label'] !== '有效') {
- $customer->save(['remark' => '#' . $param['label'] . '#']);
- } else {
- // 同部门能否重复录入开关设置
- $empcrm_repeat[] = ['root_id', '=', $root_id];
- $empcrm_repeat[] = ['name', '=', 'empcrm_customer_repeat'];
- $repeat_setting = Setting::where($empcrm_repeat)->findOrEmpty();
- $emp_org = $request->empcrm->org_id;
- //判断同部门
- if (!$repeat_setting->isEmpty()) {
- $repeat_org = explode(',', $repeat_setting['content']);
- if (!in_array($emp_org, $repeat_org)) {
- $repeat_org = [$emp_org];
- }
- } else {
- $repeat_org = [$emp_org];
- }
- // 新建档,检测手机号是否重复
- $customerExist = Customer::where([
- ['phone|phone1|phone2', '=', cypherphone($customer['phone'])],
- ['org_id', 'in', $repeat_org],
- ['employee_id', '>', 0], // 员工客户
- ['id', '<>', $param['id']]
- ])->where(
- function ($query) {
- $not_sure = Customer::changeState('待确认', 'chaos');
- $or1[] = ['crm_res_id', 'null', null];
- $or2[] = ['crm_res_id', '>', 0];
- $or2[] = ['state', 'not in', $not_sure];
- $query->whereOr([$or1, $or2]);
- }
- )->field('id,employee_id,is_resource,org_id,source_id')->select()->toArray();
- if (!empty($customerExist)) {
- foreach ($customerExist as $ex) {
- if ($ex['employee_id'] != $customer['employee_id']) {
- //查询撞单客户渠道
- $source_name = '';
- if ($ex['source_id']) {
- $source = CustomerSource::where(['id' => $ex['source_id']])->value('source');
- $source_name = $source . '渠道客户';
- }
- $have_emp = Employee::where('id', '=', $ex['employee_id'])->value('name');
- return json(['code' => 1, 'msg' => '该客户与员工' . $have_emp . $source_name . '撞单,无法标记有效']);
- } else {
- return json(['code' => 1, 'msg' => '您名下已有此手机号客户,无法标记有效']);
- }
- }
- }
- $customer->save(['state' => 1, 'valid_time' => date('Y-m-d H:i:s')]);
- }
- }
- // 添加跟进记录
- CustomerVisitLog::Create([
- 'customer_id' => $customer->id,
- 'type' => 1,
- 'employee_id' => $empid,
- 'user_id' => $request->empcrm->uid,
- 'org_id' => $request->empcrm->org_id,
- 'customer_employee_id' => $customer->employee_id,
- 'customer_org_id' => $customer->org_id,
- 'data_type' => 'out_call',
- 'remark' => !empty($param['label']) ? '#' . $param['label'] . '#' . $param['content'] : $param['content']
- ]);
- return json(['code' => 0, 'msg' => '标识成功']);
- }
- //待确认
- public function stay_confirm_list()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $is_manager = $request->empcrm->is_manager;
- $org_employee = [$empid];
- if ($is_manager == 1) {
- $team_orgs = orgSubIds($request->empcrm->org_id);
- $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
- }
- if (!$request->isAjax()) {
- $this->screen();
- return View::fetch();
- }
- $param = $request->only(['page' => 1, 'limit' => 10, 'employee_id', 'level', 'source_id', 'designer_id', 'keyname', 'date']);
- //$condition[] = empty($param['date']) ? ['revisit_time', '=', date('Y-m-d') . ' 00:00:00'] : ['revisit_time', '=', $param['date'] . ' 00:00:00'];
- if (!empty($param['employee_id']) && $is_manager == 1 && in_array($param['employee_id'], $org_employee)) $org_employee = [$param['employee_id']];
- $condition[] = ['employee_id', 'in', $org_employee];
- if (!empty($param['date'])) {
- $newtime = explode(' - ', $param['date']);
- $condition[] = ['addtime', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']];
- }
- $condition[] = ['state', 'in', Customer::changeState('待确认', 'chaos')];
- //增加排除资源库分配的待确认客户
- $condition[] = ['crm_res_id', 'NULL', null];
- $order = isset($param['order']) ? $param['order'] : 'id desc';
- $page = intval($param['page']);
- $limit = intval($param['limit']);
- $data = $this->selCustomer($condition, $page, $limit, $order, $root_id);
- $count = Customer::where($condition)->count();
- return json(['code' => 0, 'data' => $data, 'count' => $count]);
- }
- //业绩页面(已签单)
- public function achment_list()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $is_manager = $request->empcrm->is_manager;
- $org_employee = [$empid];
- if ($is_manager == 1) {
- $team_orgs = orgSubIds($request->empcrm->org_id);
- $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
- }
- if (!$request->isAjax()) {
- $this->screen();
- return View::fetch();
- }
- $param = $request->only(['page' => 1, 'limit' => 10, 'employee_id', 'level', 'source_id', 'designer_id', 'keyname', 'date', 'customer_type']);
- if (!empty($param['employee_id']) && $is_manager == 1 && in_array($param['employee_id'], $org_employee)) $org_employee = [$param['employee_id']];
- $condition[] = ['employee_id', 'in', $org_employee];
- $condition[] = ['signed_money', '<>', 0];
- $condition[] = ['state', 'not in', Customer::changeState('无效', 'chaos')];
- if (!empty($param['level'])) $condition[] = ['level', '=', $param['level']];
- if (!empty($param['source_id'])) $condition[] = ['source_id', '=', $param['source_id']];
- if (!empty($param['designer_id'])) $condition[] = ['designer_id', '=', $param['designer_id']];
- if (!empty($param['keyname'])) $condition[] = ['name|community_name', 'like', '%' . $param['keyname'] . '%'];
- $order = isset($param['order']) ? $param['order'] : 'id desc';
- $page = intval($param['page']);
- $limit = intval($param['limit']);
- $vslogid = CustomerVisitLog::where([['customer_employee_id', 'in', $org_employee], ['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')]])->column('customer_id');
- if (!empty($param['date'])) {
- $time = explode(' - ', $param['date']);
- $vslogid = CustomerVisitLog::where([['customer_employee_id', 'in', $org_employee], ['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')], ['confirm_date', 'between', [$time[0] . ' 00:00:00', $time[1] . ' 23:59:59']]])->column('customer_id');
- }
- $condition[] = ['id', 'in', $vslogid];
- //客户种类 资源库,活动,经纪人,自建
- if (!empty($param['customer_type'])) {
- $gather = [
- 1 => [['crm_res_id', 'not null', null]], //'资源库'
- 2 => [['crm_res_id', 'null', null], ['remark', 'not like', '%活动报名建档%'], ['agents_id', 'null', null]], //'自建'
- 3 => [['remark', 'like', '%活动报名建档%']], //'活动'
- 4 => [['agents_id', '>', 0]] //'经纪人'
- ];
- $arr = explode(',', $param['customer_type']);
- $whereOr = [];
- foreach ($arr as $val) {
- if (isset($gather[$val])) {
- //$whereOr[] = array_merge($condition,$gather[$val]);
- $whereOr[] = $gather[$val];
- }
- }
- if (!empty($whereOr)) {
- $new_cids = Customer::where($condition)->where(function ($query) use ($whereOr) {
- $query->whereOr($whereOr);
- })->column('id');
- unset($condition);
- $condition[] = ['id', 'in', $new_cids];
- } else {
- //无效条件
- return json(['code' => 0, 'data' => [], 'count' => 0]);
- }
- }
- $data = $this->selCustomer($condition, $page, $limit, $order, $root_id);
- foreach ($data as $key => $val) {
- $sign = [];
- if (!empty($val['sign'])) $sign = $val['sign'];
- $data[$key]['dingqian_day'] = !empty($sign) && !empty($val['jiaoding_time']) ? round((strtotime($sign['confirm_date']) - strtotime($val['jiaoding_time'])) / 86400) : 0;
- $data[$key]['daoqian_day'] = !empty($sign) && !empty($val['fisttime']) ? round((strtotime($sign['confirm_date']) - strtotime($val['fisttime'])) / 86400) : 0;
- $data[$key]['all_money'] = !empty($sign) ? $sign['money'] : 0;
- $data[$key]['pf_money'] = !empty($val['square']) && !empty($sign) ? round(($sign['money']) / $val['square'], 2) : 0;
- $data[$key]['customer_type'] = $val['crm_res_id'] ? '资源库' : ($val['remark'] == '活动报名建档' ? '活动' : ($val['agents_id'] ? '装修推荐官' : '自建'));
- }
- $count = Customer::where($condition)->count();
- return json(['code' => 0, 'data' => $data, 'count' => $count]);
- }
- //更新已签单客户页面
- public function signcus_detail()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $org_employee = [$empid];
- $param = $request->only(['id']);
- $this->screen();
- //客户等级 隶属计划
- $fields = CustomerPortraitField::with(['select'])->where([['root_id', '=', $root_id], ['keyname', 'in', ['level', 'subjection_plan', 'housetype_arrow']]])->select()->toArray();
- View::assign('fields', $fields);
- //意向风格
- $decostyle_list = Decostyle::field('id,name,root_id as pid')->where(['root_id' => $root_id])->where([['type', '=', 0]])->order('id desc')->select()->toArray();
- View::assign('decostyle_list', $decostyle_list);
- //意向产品
- $product = CustomerPackage::field('id, name, total_price')->where(['root_id' => $root_id])->select();
- View::assign('product', $product);
- $data = $this->seLemp($param['id'], $empid);
- View::assign('data', $data);
- return View::fetch();
- }
- //日报点击弹出详情页面
- public function daily_detail()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $is_manager = $request->empcrm->is_manager;
- $org_employee = [$empid];
- if ($is_manager == 1) {
- $team_orgs = orgSubIds($request->empcrm->org_id);
- $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
- }
- $param = $this->request->only(['date', 'state', 'empid' => '', 'source_id' => '', 'page' => 1, 'limit' => 17]);
- if (!$request->isAjax()) {
- View::assign('date', $param['date']);
- View::assign('state', $param['state']);
- View::assign('empid', $param['empid']);
- View::assign('source_id', $param['source_id']);
- return View::fetch();
- }
- $order = isset($param['order']) ? $param['order'] : 'id desc';
- $page = intval($param['page']);
- $limit = intval($param['limit']);
- if ($is_manager == 1 && !empty($param['empid']) && $param['empid'] != 'undefined' && in_array($param['empid'], $org_employee)) $org_employee = [$param['empid']];
- $condition[] = ['employee_id', 'in', $org_employee];
- if (empty($param['date'])) return json(['code' => 1, 'msg' => '参数错误']);
- if (!empty($param['date'])) {
- $newtime = explode('到', $param['date']);
- }
- $condition[] = ['sign_time', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']];
- if (!empty($param['source_id'] && $param['source_id'] != 'undefined')) $condition[] = ['source_id', 'in', explode(',', $param['source_id'])];
- //无效条件
- $wx_where[] = ['root_id', '=', $root_id];
- $wx_where[] = ['employee_id', 'in', $org_employee];
- $wx_where[] = ['cus_addtime', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']];
- if (!empty($param['source_id'])) $wx_where[] = ['source_id', 'in', explode(',', $param['source_id'])];
- $vlst = '确认到店';
- if ($param['state'] == '到访') {
- $vlst = '确认到店';
- } elseif ($param['state'] == '交定') {
- $vlst = '交定';
- } elseif ($param['state'] == '签单') {
- $vlst = '签单';
- } elseif ($param['state'] == '线索') {
- $condition[] = ['state', 'not in', Customer::changeState('无效', 'chaos')];
- $clueids = Customer::where($condition)->column('id');
- //置为无效的客户
- $wuxiaoids = CustomerInvalidLog::where($wx_where)->column('customer_id');
- //去掉无效后又被人获取的客户
- //$cuswxids = Db::table('fl_customer')->where([['id','in',$wuxiaoids],['employee_id','NULL',null]])->column('id');
- $cuswxids = Customer::where([['id', 'in', $wuxiaoids], ['employee_id', 'NULL', null]])->withTrashed()->column('id');
- $recycleids = CustomerRecycle::where([['customer_id', 'in', $wuxiaoids]])->column('customer_id');
- $wuxiaoids = array_merge($cuswxids, $recycleids);
- if (!empty($wuxiaoids)) $clueids = array_merge($clueids, $wuxiaoids);
- unset($condition);
- $condition = implode(',', $clueids);
- $page = ($page - 1) * 17;
- $data = $this->cus_recycle($condition, $page, $limit, $order, $root_id);
- return json(['code' => 0, 'data' => $data['data'], 'count' => $data['count']]);
- } elseif ($param['state'] == '有效') {
- $condition[] = ['state', 'not in', array_merge(Customer::changeState('无效', 'chaos'), Customer::changeState('待确认', 'chaos'))];
- $clueids = Customer::where($condition)->column('id');
- $wx_where[] = ['status', '=', 1];
- $wuxiaoids = CustomerInvalidLog::where($wx_where)->column('customer_id');
- //去掉无效后又被人获取的客户
- $cuswxids = Customer::where([['id', 'in', $wuxiaoids], ['employee_id', 'NULL', null]])->withTrashed()->column('id');
- $recycleids = CustomerRecycle::where([['customer_id', 'in', $wuxiaoids]])->column('customer_id');
- $wuxiaoids = array_merge($cuswxids, $recycleids);
- if (!empty($wuxiaoids)) $clueids = array_merge($clueids, $wuxiaoids);
- unset($condition);
- $condition = implode(',', $clueids);
- $page = ($page - 1) * 17;
- $data = $this->cus_recycle($condition, $page, $limit, $order, $root_id);
- return json(['code' => 0, 'data' => $data['data'], 'count' => $data['count']]);
- } elseif ($param['state'] == '待确认') {
- $condition[] = ['state', 'in', Customer::changeState('待确认', 'chaos')];
- $data = $this->selCustomer($condition, $page, $limit, $order, $root_id);
- $count = Customer::where($condition)->count();
- return json(['code' => 0, 'data' => $data, 'count' => $count]);
- }
- $where[] = ['customer_employee_id', 'in', $org_employee];
- if ($vlst == '确认到店') {
- $where[] = ['state', 'in', array_merge(CustomerVisitLog::changeState($vlst, 'chaos'), CustomerVisitLog::changeState('已到场', 'chaos'), CustomerVisitLog::changeState('已量房', 'chaos'))];
- } else {
- $where[] = ['state', 'in', CustomerVisitLog::changeState($vlst, 'chaos')];
- }
- if (!empty($param['date'])) {
- $where[] = ['confirm_date', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']];
- }
- $vslogid = CustomerVisitLog::where($where)->column('customer_id');
- $vslogid = !empty($vslogid) ? array_unique($vslogid) : [];
- if ($vlst == '确认到店') {
- $vslogid = !empty($vslogid) ? $this->selBeforelog($vslogid, $newtime[0] . ' 00:00:00') : [];
- }
- if (!empty($param['source_id'])) {
- $vslogid = Customer::where([['id', 'in', $vslogid], ['source_id', 'in', explode(',', $param['source_id'])]])->column('id');
- }
- if ($param['state'] != '线索') {
- unset($condition);
- $condition[] = ['id', 'in', $vslogid];
- }
- $condition = implode(',', $vslogid);
- $page = ($page - 1) * 17;
- $data = $this->cus_recycle($condition, $page, $limit, $order, $root_id);
- return json(['code' => 0, 'data' => $data['data'], 'count' => $data['count']]);
- // $data = $this->selCustomer($condition,$page,$limit,$order,$root_id);
- // $count = Customer::where($condition)->count();
- // return json(['code' => 0, 'data' => $data, 'count' => $count]);
- }
- //日报点击弹出详情页面
- public function new_daily_detail()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $is_manager = $request->empcrm->is_manager;
- $org_employee = [$empid];
- if ($is_manager == 1) {
- $team_orgs = orgSubIds($request->empcrm->org_id);
- $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
- }
- $param = $this->request->only(['date', 'state', 'empid' => '', 'page' => 1, 'limit' => 17, 'orgid' => '']);
- if (!$request->isAjax()) {
- //$this->screen();
- View::assign('date', $param['date']);
- View::assign('state', $param['state']);
- View::assign('empid', $param['empid']);
- View::assign('orgid', $param['orgid']);
- return View::fetch();
- }
- $order = isset($param['order']) ? $param['order'] : 'id desc';
- $page = intval($param['page']);
- $limit = intval($param['limit']);
- if ($is_manager == 1 && !empty($param['empid']) && $param['empid'] != 'undefined' && in_array($param['empid'], $org_employee)) $org_employee = [$param['empid']];
- if ($is_manager == 1 && !empty($param['orgid']) && $param['orgid'] != 'undefined') {
- $org_employee = Employee::where([['org_id', 'in', orgSubIds($param['orgid'])], ['root_id', '=', $root_id], ['uid', '>', 0], ['state', '=', '在职']])->column('id');
- }
- if (empty($param['date'])) return json(['code' => 1, 'msg' => '参数错误']);
- if (!empty($param['date'])) {
- $newtime = explode('到', $param['date']);
- }
- $condition[] = ['employee_id', 'in', $org_employee];
- if ($param['state'] != 'addnew') $condition[] = ['died', '<>', 2];
- $condition[] = ['state', 'not in', Customer::changeState('无效', 'chaos')];
- $vlst = '确认到店';
- if ($param['state'] == '到访') {
- $vlst = '确认到店';
- } elseif ($param['state'] == '交定') {
- $vlst = '交定';
- } elseif ($param['state'] == '签单') {
- $vlst = '签单';
- } elseif ($param['state'] == 'clue' || $param['state'] == 'addnew') {
- $condition[] = ['sign_time', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']];
- $addnewids = Customer::where($condition)->column('id');
- //置为无效的客户
- $wuxiaoids = CustomerInvalidLog::where([['employee_id', 'in', $org_employee], ['root_id', '=', $root_id], ['cus_addtime', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']]])->column('customer_id');
- if (!empty($wuxiaoids)) $addnewids = array_merge($addnewids, $wuxiaoids);
- unset($condition);
- //$condition[] = ['id','in',$addnewids];
- $condition = implode(',', $addnewids);
- $page = ($page - 1) * 17;
- $data = $this->cus_recycle($condition, $page, $limit, $order, $root_id);
- return json(['code' => 0, 'data' => $data['data'], 'count' => $data['count']]);
- } elseif (in_array($param['state'], ['yixiang', 'liangfang', 'daodian'])) {
- $condition[] = ['assign_time', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']];
- $condition[] = ['assign_type', '=', $param['state']];
- } elseif ($param['state'] == 'valid') {
- $condition[] = ['sign_time', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']];
- $condition[] = ['state', 'not in', Customer::changeState('待确认', 'chaos')];
- $validids = Customer::where($condition)->column('id');
- //置为无效的客户
- $wuxiaoids = CustomerInvalidLog::where([['employee_id', 'in', $org_employee], ['status', '=', 1], ['root_id', '=', $root_id], ['cus_addtime', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']]])->column('customer_id');
- if (!empty($wuxiaoids)) $validids = array_merge($validids, $wuxiaoids);
- unset($condition);
- //$condition[] = ['id','in',$validids];
- $condition = implode(',', $validids);
- $page = ($page - 1) * 17;
- $data = $this->cus_recycle($condition, $page, $limit, $order, $root_id);
- return json(['code' => 0, 'data' => $data['data'], 'count' => $data['count']]);
- }
- $where[] = ['customer_employee_id', 'in', $org_employee];
- if ($vlst == '确认到店') {
- $where[] = ['state', 'in', array_merge(CustomerVisitLog::changeState($vlst, 'chaos'), CustomerVisitLog::changeState('已到场', 'chaos'), CustomerVisitLog::changeState('已量房', 'chaos'))];
- } else {
- $where[] = ['state', 'in', CustomerVisitLog::changeState($vlst, 'chaos')];
- }
- if (!empty($param['date'])) {
- $where[] = ['confirm_date', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']];
- }
- //$where[] =['customer_id','in',$customer_list];
- $vslogid = CustomerVisitLog::where($where)->group('customer_id')->column('customer_id');
- if ($vlst == '确认到店') {
- $vslogid = !empty(array_unique($vslogid)) ? $this->selBeforelog($vslogid, $newtime[0] . ' 00:00:00') : [];
- }
- if (!in_array($param['state'], ['clue', 'addnew', 'yixiang', 'liangfang', 'daodian', 'valid'])) {
- unset($condition);
- $condition[] = ['id', 'in', $vslogid];
- }
- $data = $this->selCustomer($condition, $page, $limit, $order, $root_id);
- $count = Customer::where($condition)->count();
- return json(['code' => 0, 'data' => $data, 'count' => $count]);
- }
- //员工日报页面
- public function emp_daily()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $is_manager = $request->empcrm->is_manager;
- $org_employee = [$empid];
- $team_orgs = [$request->empcrm->org_id];
- $param = $request->only(['date', 'source_id', 'org_id', 'page' => 1, 'limit' => 17]);
- // var_dump($param);
- // exit;
- if ($is_manager == 1) {
- $team_orgs = orgSubIds($request->empcrm->org_id);
- $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->page($param['page'], $param['limit'])->column('id');
- }
- if (!$request->isAjax()) {
- $data['company_name'] = Company::where('root_id', $root_id)->value('company_name');
- $data['empname'] = $request->empcrm->name;
- $data['date'] = !empty($param['date']) ? $param['date'] : date('Y-m');
- View::assign('data', $data);
- $soudata = CustomerSource::field('source as name,id as value')->where('root_id', $root_id)->select()->toArray();
- View::assign('soudata', json_encode($soudata));
- $org_data = Org::where('id', 'in', $team_orgs)->field('id,name')->select()->toArray();
- View::assign('org_data', $org_data);
- View::assign('is_manager', $is_manager);
- return View::fetch();
- }
- if (!empty($param['date'])) {
- $newtime = explode('到', $param['date']);
- }
- $where[] = ['sign_time', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']];
- if (!empty($param['org_id']) && $is_manager == 1) {
- $org_employee = Employee::where([['org_id', '=', $param['org_id']], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
- }
- $where[] = ['employee_id', 'in', $org_employee];
- $where[] = ['state', 'not in', Customer::changeState('无效', 'chaos')];
- $cuslist = Customer::where($where)->column('id,employee_id,state,add_wechat_time');
- if (!empty($param['source_id'])) {
- $cuslist = Customer::where($where)->where([['source_id', 'in', $param['source_id']]])->column('id,employee_id,state,add_wechat_time');
- }
- $state1 = CustomerVisitLog::changeState('已到店', 'chaos');
- $state2 = CustomerVisitLog::changeState('已交定', 'chaos');
- $state3 = CustomerVisitLog::changeState('已签单', 'chaos');
- $state4 = CustomerVisitLog::changeState('已量房', 'chaos');
- $state5 = CustomerVisitLog::changeState('无效', 'chaos');
- $state6 = Customer::changeState('待确认', 'chaos');
- $state7 = Customer::changeState('无效', 'chaos');
- $state8 = CustomerVisitLog::changeState('已到场', 'chaos');
- $wherev[] = ['customer_employee_id', 'in', $org_employee];
- $wherev[] = ['confirm_date', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']];
- $wherev[] = ['state', 'in', array_merge($state1, $state2, $state3, $state8, $state4, $state5)];
- //记录
- if (empty($param['source_id'])) {
- $visitlog_list = CustomerVisitLog::where($wherev)->group('state,customer_id')->column('customer_id,customer_employee_id,state,money');
- } else {
- $w = Customer::where([['source_id', 'in', $param['source_id']]]);
- $visitlog_list = CustomerVisitLog::hasWhere('customer', $w)
- ->where([['CustomerVisitLog.customer_employee_id', 'in', $org_employee], ['CustomerVisitLog.confirm_date', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']]])
- ->group('state,customer_id')->column('CustomerVisitLog.customer_id,CustomerVisitLog.customer_employee_id,CustomerVisitLog.state,CustomerVisitLog.money');
- }
- $metids = [];
- foreach ($visitlog_list as $key => $val) {
- if (in_array($val['state'], array_merge($state1, $state4, $state8))) $metids[] = $val['customer_id'];
- $newvslog[$val['customer_employee_id']][] = $val;
- }
- $yesids = !empty(array_unique($metids)) ? $this->selBeforelog(array_unique($metids), $newtime[0] . ' 00:00:00') : [];
- $wx_where[] = ['root_id', '=', $root_id];
- $wx_where[] = ['employee_id', 'in', $org_employee];
- $wx_where[] = ['cus_addtime', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']];
- if (!empty($param['source_id'])) $wx_where[] = ['source_id', 'in', $param['source_id']];
- $wuxiaolog_list = CustomerInvalidLog::where($wx_where)->column('customer_id,employee_id,status');
- //员工列表
- $emplist = Employee::where([['id', 'in', $org_employee]])->field('id,name')->select()->toArray();
- $t = ['name' => '求和', 'add_wechat' => 0, 'dai_confirmed' => 0, 'clue_cont' => 0, 'valid_cont' => 0, 'valid_percen' => 0, 'meet_cont' => 0, 'meet_percen' => 0, 'deposit_cont' => 0, 'deposit_percen' => 0, 'sign_cont' => 0, 'basic_money' => 0, 'other_money' => 0, 'all_money' => 0, 'invalid_no' => 0, 'invalid_yes' => 0, 'invalid_meet' => 0, 'invalid_deposit' => 0, 'valid_check_num' => 0];
- // 有效客户量
- $validState = CustomerStateCheck::where([['id', 'in', $org_employee], ['check_state', '=', 1]])->group('employee_id')->column('count(*) as num', 'employee_id');
- //获取所有的有效id
- $validids = [];
- foreach ($cuslist as $key => $val) {
- if (!in_array($val['state'], $state6, true)) {
- $validids[] = $val['id'];
- }
- }
- $wxcusids = array_column($cuslist, 'id');
- $meetids = [];
- foreach ($emplist as $key => $val) {
- $dai_confirmed = $add_wechat = $clue_cont = $valid = $meet = $deposit = $sign = $basic_money = $other_money = $invalid_no = $invalid_yes = $invalid_meet = $invalid_deposit = 0;
- $depositids = $cusids = $daystate = $wuxiao = [];
- foreach ($cuslist as $k => $v) {
- if ($v['employee_id'] == $val['id']) {
- $clue_cont++;
- if (!empty($v['add_wechat_time'])) $add_wechat++;
- if (in_array($v['state'], $state6, true)) $dai_confirmed++;
- if (!in_array($v['state'], $state6, true)) {
- $valid++;
- }
- $cusids[] = $v['id'];
- }
- }
- //处理无效的客户还原到员工身上为有效
- foreach ($wuxiaolog_list as $k => $v) {
- if ($v['employee_id'] == $val['id'] && !in_array($v['customer_id'], $validids) && $v['status'] == 1) {
- $valid++;
- $validids[] = $v['customer_id'];
- }
- if ($v['employee_id'] == $val['id'] && !in_array($v['customer_id'], $wxcusids)) {
- $clue_cont++;
- $wxcusids[] = $v['customer_id'];
- }
- }
- if (!empty($newvslog[$val['id']])) {
- foreach ($newvslog[$val['id']] as $k => $v) {
- if (in_array($v['state'], array_merge($state1, $state4, $state8), true) && !in_array($v['customer_id'], $meetids) && in_array($v['customer_id'], $yesids)) {
- $meet++;
- $meetids[] = $v['customer_id'];
- }
- if (in_array($v['state'], $state2, true)) {
- $deposit++;
- $depositids[] = $v['customer_id'];
- }
- if (in_array($v['state'], $state3, true)) {
- $basic_money += $v['money'];
- $sign++;
- }
- if (in_array($v['state'], $state5, true)) {
- $invalid_no++;
- $wuxiao[] = $v['customer_id'];
- }
- }
- unset($newvslog[$val['id']]);
- }
- if (!empty($newvslog[$val['id']])) {
- foreach ($wuxiao as $k => $v) {
- if (in_array($v, $meetids, true)) {
- $invalid_meet++;
- }
- if (in_array($v, $depositids, true)) {
- $invalid_deposit++;
- }
- }
- }
- unset($cusids, $depositids, $wuxiao);
- $emplist[$key]['source_id'] = !empty($param['source_id']) ? implode(',', $param['source_id']) : '';
- $emplist[$key]['clue_cont'] = $clue_cont;
- $emplist[$key]['add_wechat'] = $add_wechat;
- $emplist[$key]['dai_confirmed'] = $dai_confirmed;
- $emplist[$key]['valid_cont'] = $valid;
- $emplist[$key]['valid_percen'] = $valid && $clue_cont ? round($valid / $clue_cont * 100, 2) : 0;
- $emplist[$key]['meet_cont'] = $meet;
- $emplist[$key]['meet_percen'] = !empty($meet) && !empty($clue_cont) ? round($meet / $clue_cont * 100, 2) : 0;
- $emplist[$key]['deposit_cont'] = $deposit;
- $emplist[$key]['deposit_percen'] = !empty($deposit) && !empty($meet) ? round($deposit / $meet * 100, 2) : 0;
- $emplist[$key]['sign_cont'] = $sign;
- $emplist[$key]['basic_money'] = $basic_money;
- $emplist[$key]['other_money'] = $other_money;
- $emplist[$key]['all_money'] = $basic_money + $other_money;
- $emplist[$key]['invalid_no'] = $invalid_no;
- $emplist[$key]['invalid_yes'] = $invalid_yes;
- $emplist[$key]['invalid_meet'] = $invalid_meet;
- $emplist[$key]['invalid_deposit'] = $invalid_deposit;
- $emplist[$key]['valid_check_num'] = $validState[$val['id']] ?? 0;
- //总数
- $t['clue_cont'] += $clue_cont;
- $t['add_wechat'] += $add_wechat;
- $t['dai_confirmed'] += $dai_confirmed;
- $t['valid_cont'] += $valid;
- $t['meet_cont'] += $meet;
- $t['deposit_cont'] += $deposit;
- $t['sign_cont'] += $sign;
- $t['basic_money'] += $basic_money;
- $t['other_money'] += $other_money;
- $t['all_money'] += $other_money + $basic_money;
- $t['invalid_no'] += $invalid_no;
- $t['invalid_yes'] += $invalid_yes;
- $t['invalid_meet'] += $invalid_meet;
- $t['invalid_deposit'] += $invalid_deposit;
- $t['valid_check_num'] += $emplist[$key]['valid_check_num'];
- }
- if ($t['clue_cont'] !== 0) {
- $t['valid_percen'] = $t['valid_cont'] == 0 ? 0 : round($t['valid_cont'] / $t['clue_cont'] * 100, 2);
- $t['meet_percen'] = $t['meet_cont'] == 0 ? 0 : round($t['meet_cont'] / $t['clue_cont'] * 100, 2);
- $t['deposit_percen'] = ($t['deposit_cont'] == 0 || $t['meet_cont'] == 0) ? 0 : round($t['deposit_cont'] / $t['meet_cont'] * 100, 2);
- }
- array_push($emplist, $t);
- //var_dump($xin);
- $count = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->count();
- return json(['code' => 0, 'data' => $emplist, 'count' => $count, 'msg' => '获取成功']);
- }
- /**
- * 无效报表页面
- */
- public function invalid_report()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $is_manager = request()->empcrm->is_manager;
- // 管理员查看层级以下的
- if ($is_manager == 1) {
- $team_orgs = orgSubIds($request->empcrm->org_id);
- $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
- } else {
- $org_employee = [$empid];
- }
- $param = $request->only(['date', 'source_id']);
- if (!$request->isAjax()) {
- $data['company_name'] = Company::where('root_id', $root_id)->value('company_name');
- $data['empname'] = $request->empcrm->name;
- $data['date'] = !empty($param['date']) ? $param['date'] : date('Y-m');
- View::assign('data', $data);
- return View::fetch();
- }
- if (!empty($param['date'])) {
- $newtime = explode('到', $param['date']);
- }
- $where = [
- ['addtime', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']],
- ['employee_id', 'in', $org_employee]
- ];
- //来源列表
- $source_list = CustomerSource::field('id,source as name')->where('root_id', $root_id)->select()->toArray();
- $source_id = array_column($source_list, 'id');
- //跟踪记录
- $visitlog_list = CustomerVisitLog::where(array_merge($where, [['state', 'in', CustomerVisitLog::changeState('无效', 'chaos')]]))->column('id,customer_id,remark');
- //有效
- $yescuslist = Customer::where([['sign_time', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']], ['employee_id', 'in', $org_employee], ['source_id', 'in', $source_id]])->group('source_id')->column('count(source_id)', 'source_id');
- //无效
- $cusid = array_column($visitlog_list, 'customer_id');
- $nocuslist = Customer::where([['id', 'in', $cusid]])->group('source_id')->column('count(source_id)', 'source_id');
- $nocuslist_id = Customer::where([['id', 'in', $cusid], ['source_id', 'in', $source_id]])->column('source_id', 'id');
- $t = ['name' => '求和', 'clue_cont' => 0, 'invalid_no' => 0, 'remark1' => 0, 'remark2' => 0, 'remark3' => 0, 'remark4' => 0, 'remark5' => 0, 'remark6' => 0, 'remark7' => 0, 'remark8' => 0, 'remark9' => 0, 'remark10' => 0];
- foreach ($source_list as &$val) {
- $val['clue_cont'] = 0;
- $val['invalid_no'] = 0;
- $val['remark1'] = 0;
- $val['remark2'] = 0;
- $val['remark3'] = 0;
- $val['remark4'] = 0;
- $val['remark5'] = 0;
- $val['remark6'] = 0;
- $val['remark7'] = 0;
- $val['remark8'] = 0;
- $val['remark9'] = 0;
- $val['remark10'] = 0;
- //无效
- $val['invalid_no'] = isset($nocuslist[$val['id']]) ? $nocuslist[$val['id']] : 0;
- //线索
- $clue_cont = isset($yescuslist[$val['id']]) ? $yescuslist[$val['id']] : 0;
- $val['clue_cont'] = $clue_cont + $val['invalid_no'];
- $remark = ['同行' => 'remark1', '空错号' => 'remark2', '不是本人' => 'remark3', '局部装修' => 'remark4', '超服务区域' => 'remark5', '与内部撞单' => 'remark6', '多次联系不上' => 'remark7', '装修预算太低' => 'remark8', '没有装修需求' => 'remark9', '精装修' => 'remark10'];
- foreach ($visitlog_list as $k => $v) {
- if (isset($nocuslist_id[$v['customer_id']]) && $nocuslist_id[$v['customer_id']] == $val['id'] && isset($remark[$v['remark']])) {
- $val[$remark[$v['remark']]]++;
- }
- }
- $t['clue_cont'] += $val['clue_cont'];
- $t['invalid_no'] += $val['invalid_no'];
- $t['remark1'] += $val['remark1'];
- $t['remark2'] += $val['remark2'];
- $t['remark3'] += $val['remark3'];
- $t['remark4'] += $val['remark4'];
- $t['remark5'] += $val['remark5'];
- $t['remark6'] += $val['remark6'];
- $t['remark7'] += $val['remark7'];
- $t['remark8'] += $val['remark8'];
- $t['remark9'] += $val['remark9'];
- $t['remark10'] += $val['remark10'];
- }
- array_push($source_list, $t);
- return json(['code' => 0, 'data' => $source_list, 'count' => 0, 'msg' => '获取成功']);
- }
- //待回访
- public function repay_list()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $is_manager = $request->empcrm->is_manager;
- $org_employee = [$empid];
- if ($is_manager == 1) {
- $team_orgs = orgSubIds($request->empcrm->org_id);
- $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
- }
- if (!$request->isAjax()) {
- $this->screen();
- return View::fetch();
- }
- $param = $request->only(['page' => 1, 'limit' => 10, 'jiange_day', 'employee_name', 'level', 'source_name', 'designer_name', 'name', 'date', 'last_contact_date', 'community_name']);
- // 客户表revisit_time字段存在无法识别日期格式,查询需指定开始日期
- $condition[] = empty($param['date']) ? ['revisit_time', '=', date('Y-m-d') . ' 00:00:00'] : ['revisit_time', '=', $param['date'] . ' 00:00:00'];
- if (!empty($param['employee_id']) && $is_manager == 1 && in_array($param['employee_id'], $org_employee)) $org_employee = [$param['employee_id']];
- $condition[] = ['employee_id', 'in', $org_employee];
- //$condition[] = ['state', 'not in',Customer::changeState('已签单', 'chaos')];
- //客户姓名
- if (!empty($param['name'])) $condition[] = ['name', 'like', '%' . $param['name'] . '%'];
- //小区名称
- if (!empty($param['community_name'])) $condition[] = ['community_name', 'like', '%' . $param['community_name'] . '%'];
- //等级
- if (!empty($param['level'])) $condition[] = ['level', 'in', explode(',', $param['level'])];
- //来源
- if (!empty($param['source_name'])) $condition[] = ['source_id', 'in', explode(',', $param['source_name'])];
- //所属员工
- if (!empty($param['employee_name'])) $condition[] = ['employee_id', 'in', explode(',', $param['employee_name'])];
- //所属设计师
- if (!empty($param['designer_name'])) $condition[] = ['designer_id', 'in', explode(',', $param['designer_name'])];
- $order = isset($param['order']) ? $param['order'] : 'id desc';
- $page = intval($param['page']);
- $limit = intval($param['limit']);
- //客户的跟进时间
- if (!empty($param['last_contact_date'])) {
- $last_contact = explode(' - ', $param['last_contact_date']);
- $start = str_replace('/', '-', $last_contact[0]);
- $end = str_replace('/', '-', $last_contact[1]);
- $condition[] = ['last_contact_date', '>=', $start];
- $condition[] = ['last_contact_date', '<=', $end];
- }
- //未跟进天数
- if (!empty($param['jiange_day'])) {
- $no_visit_day = explode(',', $param['jiange_day']);
- $all_no_visit = [
- 1 => [3, 7],
- 2 => [8, 14],
- 3 => [15, 30],
- 4 => [31, 10000]
- ];
- $customersList = Customer::where($condition)->order('addtime desc')->column('id,addtime');
- $wg_where[] = ['customer_id', 'in', array_column($customersList, 'id')];
- $latest_visit_log = CustomerVisitLog::where($wg_where)->group('customer_id')->column('max(addtime) as addtime', 'customer_id'); //->select()->toArray();
- $no_visit_ids = [];
- foreach ($customersList as $k => $v) {
- if (empty($latest_visit_log[$v['id']])) {
- $no_visit_long = ceil((time() - strtotime($v['addtime'])) / 86400);
- } else {
- $no_visit_long = ceil((time() - strtotime($latest_visit_log[$v['id']])) / 86400);
- }
- foreach ($no_visit_day as $m => $p) {
- if (empty($all_no_visit[$p])) continue;
- if ($no_visit_long >= $all_no_visit[$p][0] && $no_visit_long <= $all_no_visit[$p][1]) $no_visit_ids[] = $v['id'];
- }
- }
- unset($condition);
- $condition[] = ['id', 'in', $no_visit_ids];
- }
- $data = $this->selCustomer($condition, $page, $limit, $order, $root_id);
- foreach ($data as $key => $val) {
- $data[$key]['jiange_day'] = ceil((strtotime(date('Y-m-d')) - strtotime($val['last_contact_date'])) / 86400);
- }
- $count = Customer::where($condition)->count();
- return json(['code' => 0, 'data' => $data, 'count' => $count]);
- }
- //到访(确认到场)列表 包含 到店、量房、到场、签单、转单
- public function confirm_visit_list()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $is_manager = $request->empcrm->is_manager;
- $org_employee = [$empid];
- $team_orgs = [$request->empcrm->org_id];
- if ($is_manager == 1) {
- $team_orgs = orgSubIds($request->empcrm->org_id);
- $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
- }
- if (!$request->isAjax()) {
- $this->screen();
- //指派经理
- $assignemp_list = Employee::where([['org_id', 'in', orgSubIds($root_id)], ['state', '=', '在职'], ['uid', '>', 0]])->column('id,name');
- View::assign('assignemp_list', $assignemp_list);
- //指派部门
- $assignorg_list = Org::where([['path', 'like', $root_id . '-%']])->column('id,name');
- View::assign('assignorg_list', $assignorg_list);
- $assign_type = [['id' => 1, 'name' => '意向指派'], ['id' => 2, 'name' => '量房指派'], ['id' => 3, 'name' => '到店指派']];
- View::assign('assign_type', $assign_type);
- return View::fetch();
- }
- $param = $request->only(['page' => 1, 'limit' => 10, 'last_contact_date', 'assign_org', 'assign_list', 'community_name', 'no_visit_day', 'liangfang_time', 'jiaoding_time', 'qiandan_time', 'jiaoding_money', 'qiandan_money', 'daodian_time' => '', 'square', 'level', 'source_name', 'name' => '', 'state', 'assign_time', 'phone', 'employee_name', 'assign_type_name', 'designer_name', 'times' => 0, 'customer_type']);
- if (!empty($param['employee_id']) && $is_manager == 1 && in_array($param['employee_id'], $org_employee)) $org_employee = [$param['employee_id']];
- //$condition[] = ['employee_id', 'in', $org_employee];
- //$condition[] = ['state', 'not in',Customer::changeState('无效','chaos')];
- $condition[] = ['org_id', 'in', $team_orgs];
- $tmp_condition = "";
- //客户姓名
- if (!empty($param['name'])) $condition[] = ['name', 'like', '%' . $param['name'] . '%'];
- //小区名称
- if (!empty($param['community_name'])) $condition[] = ['community_name', 'like', '%' . $param['community_name'] . '%'];
- //等级
- if (!empty($param['level'])) $condition[] = ['level', 'in', explode(',', $param['level'])];
- //来源
- if (!empty($param['source_name'])) $condition[] = ['source_id', 'in', explode(',', $param['source_name'])];
- //所属员工
- if (!empty($param['employee_name'])) $condition[] = ['employee_id', 'in', explode(',', $param['employee_name'])];
- //所属设计师
- if (!empty($param['designer_name'])) $condition[] = ['designer_id', 'in', explode(',', $param['designer_name'])];
- $order = isset($param['order']) ? $param['order'] : 'id desc';
- if ($param['times'] == 1) $order = 'assign_time asc';
- if ($param['times'] == 2) $order = 'assign_time desc';
- if ($param['times'] == 3) $order = 'last_contact_date asc';
- if ($param['times'] == 4) $order = 'last_contact_date desc';
- $page = intval($param['page']);
- $limit = intval($param['limit']);
- //客户的跟进时间
- if (!empty($param['last_contact_date'])) {
- $last_contact = explode(' - ', $param['last_contact_date']);
- $start = str_replace('/', '-', $last_contact[0]);
- $end = str_replace('/', '-', $last_contact[1]);
- $condition[] = ['last_contact_date', '>=', $start . ' 00:00:00'];
- $condition[] = ['last_contact_date', '<=', $end . ' 23:59:59'];
- }
- //客户种类 资源库,活动,经纪人,自建
- if (!empty($param['customer_type'])) {
- $gather = [
- 1 => [['crm_res_id', 'not null', null]], //'资源库'
- 2 => [['crm_res_id', 'null', null], ['remark', 'not like', '%活动报名建档%'], ['agents_id', 'null', null]], //'自建'
- 3 => [['remark', 'like', '%活动报名建档%']], //'活动'
- 4 => [['agents_id', '>', 0]] //'经纪人'
- ];
- $arr = explode(',', $param['customer_type']);
- $whereOr = [];
- foreach ($arr as $val) {
- if (isset($gather[$val])) {
- $whereOr[] = $gather[$val];
- }
- }
- if (!empty($whereOr)) {
- $new_cids = Customer::where($condition)->where(function ($query) use ($whereOr) {
- $query->whereOr($whereOr);
- })->column('id');
- unset($condition);
- $condition[] = ['id', 'in', $new_cids];
- } else {
- //无效条件
- return json(['code' => 0, 'data' => [], 'count' => 0]);
- }
- }
- //指派时间查询
- if (!empty($param['assign_time'])) {
- $assign_time = explode('-', $param['assign_time']);
- $start = $assign_time[0] . ' 00:00:00';
- $end = $assign_time[1] . ' 23:59:59';
- $condition[] = ['assign_time', '>', $start];
- $condition[] = ['assign_time', '<', $end];
- }
- //$cusids = Customer::where($condition)->column('id');
- //$vs_where[] = ['customer_id','in',$cusids];
- $vs_where[] = ['customer_employee_id', 'in', $org_employee];
- $vs_where[] = ['state', 'in', array_merge(CustomerVisitLog::changeState('确认到店', 'chaos'), CustomerVisitLog::changeState('已量房', 'chaos'), CustomerVisitLog::changeState('已到场', 'chaos'), CustomerVisitLog::changeState('已交定', 'chaos'), CustomerVisitLog::changeState('已签单', 'chaos'))];
- if (!empty($param['date'])) $vs_where[] = ['confirm_date', 'between', [$param['date'] . ' 00:00:00', $param['date'] . ' 23:59:59']];
- $vslogid = CustomerVisitLog::where($vs_where)->column('customer_id');
- $condition[] = ['id', 'in', array_unique($vslogid)];
- //手机号
- if (!empty($param['phone'])) {
- $list = Customer::where($condition)->order('addtime desc')->field('id,name,community_name,phone')->select()->toArray();
- $customerIdList = [];
- foreach ($list as $v) {
- if (strpos($v['phone'], trim($param['phone'])) !== false) $customerIdList[] = $v['id'];
- }
- unset($condition);
- $condition[] = ['id', 'in', $customerIdList];
- }
- //客户经理
- if (!empty($param['assign_list'])) {
- $assign_employee = explode(',', $param['assign_list']);
- $customerIdList = Customer::where($condition)->where(function ($query) use ($assign_employee) {
- foreach ($assign_employee as $v) {
- $query->whereOr([['assigned_personnel', 'find in set', $v]]);
- }
- })->column('id');
- unset($condition);
- $condition[] = ['id', 'in', $customerIdList];
- }
- //指派部门
- if (!empty($param['assign_org'])) {
- $assign_org = explode(',', $param['assign_org']);
- $all_assigids = [];
- $assigned_personnelList = Customer::where($condition)->column('assigned_personnel,id,designer_id');
- foreach ($assigned_personnelList as $k => $v) {
- $assigned_personal = !empty($v['assigned_personnel']) ? explode(',', $v['assigned_personnel']) : [];
- if (!empty($v['designer_id'])) $assigned_personal[] = $v['designer_id'];
- $assign_orgid = Employee::where([['id', 'in', $assigned_personal], ['org_id', 'in', $assign_org], ['root_id', '=', $root_id]])->column('org_id');
- if (!empty($assign_orgid)) $all_assigids[] = $v['id'];
- }
- unset($condition);
- $condition[] = ['id', 'in', $all_assigids];
- }
- //到店时间
- if (!empty($param['daodian_time'])) {
- $daodian_time = explode(' - ', $param['daodian_time']);
- if (empty($daodian_time)) return json(['code' => 1, 'msg' => '时间参数错误']);
- $start = str_replace('/', '-', $daodian_time[0]);
- $end = str_replace('/', '-', $daodian_time[1]);
- $customerIdList = Customer::where($condition)->column('id');
- $logwhere[] = ['confirm_date', '>=', $start . ' 00:00:00'];
- $logwhere[] = ['confirm_date', '<=', $end . ' 23:59:59'];
- $logwhere[] = ['state', 'in', CustomerVisitLog::changeState('已到店', 'chaos')];
- $logwhere[] = ['customer_id', 'in', $customerIdList];
- $customerIdList = CustomerVisitLog::where($logwhere)->group('customer_id')->column('customer_id');
- unset($condition);
- $condition[] = ['id', 'in', $customerIdList];
- }
- //量房时间
- if (!empty($param['liangfang_time'])) {
- $liangfang_time = explode(' - ', $param['liangfang_time']);
- if (empty($liangfang_time)) return json(['code' => 1, 'msg' => '时间参数错误']);
- $start = str_replace('/', '-', $liangfang_time[0]);
- $end = str_replace('/', '-', $liangfang_time[1]);
- $customerIdList = Customer::where($condition)->column('id');
- $logwhere[] = ['confirm_date', '>=', $start . ' 00:00:00'];
- $logwhere[] = ['confirm_date', '<=', $end . ' 23:59:59'];
- $logwhere[] = ['state', 'in', CustomerVisitLog::changeState('已量房', 'chaos')];
- $logwhere[] = ['customer_id', 'in', $customerIdList];
- $customerIdList = CustomerVisitLog::where($logwhere)->group('customer_id')->column('customer_id');
- unset($condition);
- $condition[] = ['id', 'in', $customerIdList];
- }
- //交定时间
- if (!empty($param['jiaoding_time'])) {
- $jiaoding_time = explode(' - ', $param['jiaoding_time']);
- if (empty($jiaoding_time)) return json(['code' => 1, 'msg' => '时间参数错误']);
- $start = str_replace('/', '-', $jiaoding_time[0]);
- $end = str_replace('/', '-', $jiaoding_time[1]);
- $customerIdList = Customer::where($condition)->column('id');
- $logwhere[] = ['confirm_date', '>=', $start . ' 00:00:00'];
- $logwhere[] = ['confirm_date', '<=', $end . ' 23:59:59'];
- $logwhere[] = ['state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')];
- $logwhere[] = ['customer_id', 'in', $customerIdList];
- $customerIdList = CustomerVisitLog::where($logwhere)->group('customer_id')->column('customer_id');
- unset($condition);
- $condition[] = ['id', 'in', $customerIdList];
- }
- //签单时间
- if (!empty($param['qiandan_time'])) {
- $qiandan_time = explode(' - ', $param['qiandan_time']);
- if (empty($qiandan_time)) return json(['code' => 1, 'msg' => '时间参数错误']);
- $start = str_replace('/', '-', $qiandan_time[0]);
- $end = str_replace('/', '-', $qiandan_time[1]);
- $customerIdList = Customer::where($condition)->column('id');
- $logwhere[] = ['confirm_date', '>=', $start . ' 00:00:00'];
- $logwhere[] = ['confirm_date', '<=', $end . ' 23:59:59'];
- $logwhere[] = ['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')];
- $logwhere[] = ['customer_id', 'in', $customerIdList];
- $customerIdList = CustomerVisitLog::where($logwhere)->group('customer_id')->column('customer_id');
- unset($condition);
- $condition[] = ['id', 'in', $customerIdList];
- }
- //交定金额
- if (!empty($param['jiaoding_money'])) {
- $jiaoding_money = explode('-', $param['jiaoding_money']);
- if (empty($jiaoding_money)) return json(['code' => 1, 'msg' => '参数错误']);
- $customerIdList = Customer::where($condition)->column('id');
- $logwhere[] = ['state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')];
- $logwhere[] = ['money', 'between', $jiaoding_money];
- $logwhere[] = ['customer_id', 'in', $customerIdList];
- $customerIdList = CustomerVisitLog::where($logwhere)->group('customer_id')->column('customer_id');
- unset($condition);
- $condition[] = ['id', 'in', $customerIdList];
- }
- //签单金额
- if (!empty($param['qiandan_money'])) {
- $sign_money = explode('-', $param['qiandan_money']);
- if (empty($sign_money)) return json(['code' => 1, 'msg' => '参数错误']);
- $customerIdList = Customer::where($condition)->column('id');
- $logwhere[] = ['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')];
- $logwhere[] = ['money', 'between', $sign_money];
- $logwhere[] = ['customer_id', 'in', $customerIdList];
- $customerIdList = CustomerVisitLog::where($logwhere)->group('customer_id')->column('customer_id');
- unset($condition);
- $condition[] = ['id', 'in', $customerIdList];
- }
- // 房屋面积
- if (!empty($param['square'])) {
- $square = explode(',', $param['square']);
- $square_arr = [
- 1 => '(square > 1 and square <= 80)',
- 2 => '(square > 81 and square <= 100)',
- 3 => '(square > 101 and square <= 120)',
- 4 => '(square > 121 and square <= 200)',
- 5 => '(square > 201 and square <= 500)',
- 6 => '(square > 501 and square <= 10000)'
- ];
- $squ_whereOr = null;
- foreach ($square as $k => $v) {
- if (!empty($square_arr[$v])) $squ_whereOr .= $square_arr[$v] . ' OR';
- }
- $customersList = Customer::where($condition)->where(function ($query) use ($squ_whereOr) {
- $query->whereRaw(trim($squ_whereOr, ' OR'));
- })->order('addtime desc')->column('id');
- unset($condition);
- $condition[] = ['id', 'in', $customersList];
- }
- //未跟进天数
- if (!empty($param['no_visit_day'])) {
- $no_visit_day = explode(',', $param['no_visit_day']);
- $all_no_visit = [
- 1 => [3, 7],
- 2 => [8, 14],
- 3 => [15, 30],
- 4 => [31, 10000]
- ];
- $customersList = Customer::where($condition)->order('addtime desc')->column('id,addtime');
- $wg_where[] = ['customer_id', 'in', array_column($customersList, 'id')];
- $latest_visit_log = CustomerVisitLog::where($wg_where)->group('customer_id')->column('max(addtime) as addtime', 'customer_id'); //->select()->toArray();
- $no_visit_ids = [];
- foreach ($customersList as $k => $v) {
- if (empty($latest_visit_log[$v['id']])) {
- $no_visit_long = ceil((time() - strtotime($v['addtime'])) / 86400);
- } else {
- $no_visit_long = ceil((time() - strtotime($latest_visit_log[$v['id']])) / 86400);
- }
- foreach ($no_visit_day as $m => $p) {
- if (empty($all_no_visit[$p])) continue;
- if ($no_visit_long >= $all_no_visit[$p][0] && $no_visit_long <= $all_no_visit[$p][1]) $no_visit_ids[] = $v['id'];
- }
- }
- unset($condition);
- $condition[] = ['id', 'in', $no_visit_ids];
- }
- if (!empty($param['state'])) {
- $customerIdList = Customer::where($condition)->column('id');
- $stateall = explode(',', $param['state']);
- $allsta = [];
- foreach ($stateall as $k => $v) {
- if ($v == '已签单') $v = '已交定';
- if ($v == '已转单') $v = '已签单';
- if (in_array($v, ['待确认', '有效'])) continue;
- $onestate = CustomerVisitLog::changeState($v, 'chaos');
- $allsta = array_merge($onestate, $allsta);
- }
- $visit_where[] = ['customer_id', 'in', $customerIdList];
- $visit_where[] = ['state', 'in', $allsta];
- // $visit_where[] = ['addtime', '>', date('Y-m-d H:i:s', $startTime)];
- // $visit_where[] = ['addtime', '<', date('Y-m-d H:i:s', $endTime)];
- $logCustomersIdList = CustomerVisitLog::where($visit_where)->group('customer_id')->column('customer_id');
- $c_ids = $logCustomersIdList;
- foreach ($stateall as $k => $v) {
- if ($v == '待确认') {
- $confirmedids = Customer::where($condition)->where([['state', 'in', Customer::changeState('待确认', 'chaos')], ['crm_res_id', 'NULL', null]])->column('id');
- }
- if ($v == '有效') {
- $validids = Customer::where($condition)->where('state', 'not in', Customer::changeState('待确认', 'chaos'))->column('id');
- }
- }
- unset($condition);
- if (!empty($confirmedids)) $c_ids = array_merge($confirmedids, $c_ids);
- if (!empty($validids)) $c_ids = array_merge($validids, $c_ids);
- unset($condition);
- $condition[] = ['id', 'in', $c_ids];
- }
- $vis_where = "if(CustomerVisitLog.state in (7,'已到店'), 1, 0) desc,confirm_date asc";
- $tmp_vis_where = 'confirm_date asc';
- if ($param['times'] > 4) {
- if ($param['times'] == 6) {
- $vis_where = "if(CustomerVisitLog.state in (7,'已到店'), 1, 0) desc,confirm_date desc";
- $tmp_vis_where = 'confirm_date desc';
- }
- $page = ($page - 1) * 17;
- $tmp_sql = CustomerVisitLog::hasWhere('customer', $condition)->field("if(CustomerVisitLog.state in (7,'已到店'),1,0) as tmp_state")->group('CustomerVisitLog.state,CustomerVisitLog.customer_id')->orderRaw($vis_where)->buildSql();
- $sql = "select customer_id from " . $tmp_sql . " a group by a.customer_id ORDER BY a.tmp_state desc," . $tmp_vis_where . " LIMIT " . $page . "," . $limit;
- $customer_id = Db::query($sql);
- $data = [];
- foreach ($customer_id as $k => $v) {
- $one = Customer::with(['employee', 'org', 'designer', 'source', 'visitLog' => function ($query) {
- $query->field('id,customer_id,addtime,state,confirm_date,money');
- }])->where($condition)->where(['id' => $v['customer_id']])->find();
- $data[$k] = $one->visible(['designer_id', 'assigned_personnel', 'assign_type', 'assign_time', 'id', 'employee_id', 'name', 'introduce', 'community_name', 'phone', 'level', 'state', 'square', 'revisit_time', 'addtime', 'last_contact_date', 'org_id', 'protected_to', 'is_resource', 'employee.name', 'org_name', 'designer.name', 'visit_log_count', 'activity_frequency_count', 'source_id', 'source.source', 'phone1', 'phone2', 'remark', 'crm_res_id', 'agents_id', 'house_delivery_time', 'first_visit_date', 'ext', 'transfer_info', 'deposit_money', 'signed_money', 'package_id'])->toArray();
- }
- } else {
- $data = Customer::with(['employee', 'org', 'designer', 'source', 'visitLog' => function ($query) {
- $query->field('id,customer_id,addtime,state,confirm_date,money');
- }])
- ->where($condition)
- ->page($page, $limit)->order($order)->select();
- $data = $data->visible(['designer_id', 'assigned_personnel', 'assign_type', 'assign_time', 'id', 'employee_id', 'name', 'introduce', 'community_name', 'phone', 'level', 'state', 'square', 'revisit_time', 'addtime', 'last_contact_date', 'org_id', 'protected_to', 'is_resource', 'employee.name', 'org_name', 'designer.name', 'visit_log_count', 'activity_frequency_count', 'source_id', 'source.source', 'phone1', 'phone2', 'remark', 'crm_res_id', 'agents_id', 'house_delivery_time', 'first_visit_date', 'ext', 'transfer_info', 'deposit_money', 'signed_money', 'package_id'])->toArray();
- }
- $setting = Setting::where([['name', '=', 'pubpool'], ['root_id', '=', $root_id]])->find();
- if (!empty($setting)) {
- $setting_content = json_decode($setting['content'], true);
- } else {
- $setting_content = [];
- }
- // 指派类型
- $assign_type_default = ['yixiang' => '意向客戶指派', 'liangfang' => '量房客戶指派', 'daodian' => '到店客戶指派'];
- foreach ($data as &$item) {
- $item['un_protected'] = false;
- if (!empty($item['protected_to']) && time() > strtotime($item['protected_to'])) {
- $state_n = Customer::changeState($item['state'], 'n');
- if (isset($setting_content[$state_n]['state']) && $setting_content[$state_n]['state'] == 1) {
- $item['un_protected'] = true;
- }
- }
- $item['customer_type'] = $item['crm_res_id'] ? '资源库' : ($item['remark'] == '活动报名建档' ? '活动' : ($item['agents_id'] ? '装修推荐官' : '自建'));
- $item['square'] = floatval($item['square']);
- $item['phone'] = substr_replace($item['phone'], '******', 3, 6);
- $item['phone1'] = $item['phone1'] ? substr_replace($item['phone1'], '******', 3, 6) : '';
- $item['phone2'] = $item['phone2'] ? substr_replace($item['phone2'], '******', 3, 6) : '';
- $item['jiaofang'] = !empty($item['house_delivery_time']) ? (str_replace('/', '-', $item['house_delivery_time']) <= date('Y-m-d', time()) ? '现房' : $item['house_delivery_time']) : '';
- $item['time_status'] = $item['last_contact_date'] ? $this->uc_time_ago($item['last_contact_date']) : '';
- $item['add_wechat'] = '否';
- $item['package_name'] = !empty($item['package_id']) ? CustomerPackage::where('id', $item['package_id'])->value('name') : '';
- $item['employee_name'] = !empty($item['employee']) ? $item['employee']['name'] : '';
- $item['designer_name'] = !empty($item['designer']) ? $item['designer']['name'] : '';
- $item['source_name'] = !empty($item['source']) ? $item['source']['source'] : '';
- if (!empty($item['ext'])) {
- $extdata = json_decode($item['ext'], true);
- foreach ($extdata as $key => $val) {
- if (isset($val['keyname']) && $val['keyname'] == 'wechat' && !empty($val['value'])) {
- $add_wechat = CustomerPortraitFieldSelect::where('id', $val['value'])->value('name');
- if ($add_wechat == '有') {
- $add_wechat = '是';
- }
- $item['add_wechat'] = $add_wechat;
- }
- }
- }
- $now_status = $item['state'];
- if ($now_status == '已签单') $now_status = '已转单';
- if ($now_status == '已交定') $now_status = '已签单';
- $assignIdList = !empty($item['assigned_personnel']) ? explode(',', $item['assigned_personnel']) : [];
- //$assignIdList[] = $item['employee_id']; // 所属销售
- if (!empty($item['designer_id'])) $assignIdList[] = $item['designer_id']; // 指派的设计师
- $assign_data = Employee::where([['id', 'in', $assignIdList], ['root_id', '=', $root_id]])->column('name,org_id');
- $item['assign_list'] = !empty($assign_data) ? implode(',', array_column($assign_data, 'name')) : '';
- $assign_org = Org::where([['id', 'in', array_column($assign_data, 'org_id')]])->group('id')->column('name');
- $item['assign_org'] = !empty($assign_org) ? implode(',', $assign_org) : '';
- $item['assign_type_name'] = $item['state'] = $item['daodian_time'] = $item['liangfang_time'] = $item['jiaoding_time'] = $item['qiandan_time'] = '';
- if (!empty($item['assign_type'])) {
- if (isset($assign_type_default[$item['assign_type']])) {
- $item['assign_type_name'] = $assign_type_default[$item['assign_type']];
- }
- }
- if (!empty($item['visitLog'])) {
- //取最新的一条为客户的跟进时间
- // $item['last_contact_date'] = $item['visitLog'][count($item['visitLog'])-1]['addtime'];
- //处理合并多个状态
- $stain = [$now_status];
- foreach ($item['visitLog'] as $k => $v) {
- if (!in_array($v['state'], ['已交定', '已签单', '已到店', '已量房', '已到场', '未到访', '回访'])) continue;
- if ($v['state'] == '回访') $v['state'] = '待确认';
- if ($v['state'] == '已签单') $v['state'] = '已转单';
- if ($v['state'] == '已交定') $v['state'] = '已签单';
- if (!in_array($v['state'], $stain)) {
- $stain[] = $v['state'];
- }
- }
- if (count($stain) > 1 && in_array('待确认', $stain)) $stain = array_diff($stain, ['待确认']);
- $visit_state = ['已到店', '已量房', '已到场', '已签单', '已转单', '已卖卡'];
- $no_visit_state = ['未到访', '待确认'];
- $jiaoji = array_intersect($visit_state, $stain);
- $no_jiaoji = array_intersect($no_visit_state, $stain);
- if (count($no_jiaoji) == 2) { // 有未到访、待确认两个状态
- $stain = ['未到访'];
- }
- if (count($stain) > 1 && count($jiaoji) > 0) {
- $stain = array_diff($stain, $no_visit_state);
- }
- $item['state'] = implode(',', $stain);
- if ($now_status == '待确认') $item['state'] = '待确认';
- //处理时间
- $state = [];
- foreach ($item['visitLog'] as $k => $v) {
- $state[$v['state']][] = $v;
- }
- foreach ($state as $k => $v) {
- if ($k == '已到店') $item['daodian_time'] = date('Y-m-d', strtotime($v[0]['confirm_date']));
- if ($k == '已量房') $item['liangfang_time'] = date('Y-m-d', strtotime($v[0]['confirm_date']));
- if ($k == '已交定') {
- $item['jiaoding_time'] = date('Y-m-d', strtotime($v[0]['confirm_date']));
- $item['jiaoding_money'] = $v[0]['money'];
- }
- if ($k == '已签单') {
- $item['qiandan_time'] = date('Y-m-d', strtotime($v[0]['confirm_date']));
- $item['qiandan_money'] = $v[0]['money'];
- //$item['sign'] = $v[0];
- }
- }
- }
- $item['now_state'] = $now_status;
- $item['no_visit_day'] = ceil((strtotime(date('Y-m-d')) - strtotime($item['last_contact_date'])) / 86400) > 0 ? ceil((strtotime(date('Y-m-d')) - strtotime($item['last_contact_date'])) / 86400) : 0;
- }
- $count = Customer::where($condition)->count();
- return json(['code' => 0, 'data' => $data, 'count' => $count]);
- }
- //已交定列表
- public function jiaoding_list()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $is_manager = $request->empcrm->is_manager;
- $org_employee = [$empid];
- if ($is_manager == 1) {
- $team_orgs = orgSubIds($request->empcrm->org_id);
- $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
- }
- if (!$request->isAjax()) {
- $this->screen();
- return View::fetch();
- }
- $param = $request->only(['page' => 1, 'limit' => 10, 'employee_id', 'level', 'source_id', 'designer_id', 'keyname', 'date', 'times' => 0, 'customer_type']);
- if (!empty($param['employee_id']) && $is_manager == 1 && in_array($param['employee_id'], $org_employee)) $org_employee = [$param['employee_id']];
- $condition[] = ['employee_id', 'in', $org_employee];
- $condition[] = ['state', 'not in', Customer::changeState('无效', 'chaos')];
- if (!empty($param['level'])) $condition[] = ['level', '=', $param['level']];
- if (!empty($param['source_id'])) $condition[] = ['source_id', '=', $param['source_id']];
- if (!empty($param['designer_id'])) $condition[] = ['designer_id', '=', $param['designer_id']];
- if (!empty($param['keyname'])) $condition[] = ['name|community_name', 'like', '%' . $param['keyname'] . '%'];
- $order = isset($param['order']) ? $param['order'] : 'id desc';
- if ($param['times'] == 1) $order = 'tmp_sign_time asc';
- if ($param['times'] == 2) $order = 'tmp_sign_time desc';
- $page = intval($param['page']);
- $limit = intval($param['limit']);
- $vslogid = CustomerVisitLog::where([['customer_employee_id', 'in', $org_employee], ['state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')]])->column('customer_id');
- if (!empty($param['date'])) {
- $time = explode(' - ', $param['date']);
- $vslogid = CustomerVisitLog::where([['customer_employee_id', 'in', $org_employee], ['state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')], ['confirm_date', 'between', [$time[0] . ' 00:00:00', $time[1] . ' 23:59:59']]])->column('customer_id');
- }
- $condition[] = ['id', 'in', $vslogid];
- //客户种类 资源库,活动,经纪人,自建
- if (!empty($param['customer_type'])) {
- $gather = [
- 1 => [['crm_res_id', 'not null', null]], //'资源库'
- 2 => [['crm_res_id', 'null', null], ['remark', 'not like', '%活动报名建档%'], ['agents_id', 'null', null]], //'自建'
- 3 => [['remark', 'like', '%活动报名建档%']], //'活动'
- 4 => [['agents_id', '>', 0]] //'经纪人'
- ];
- $arr = explode(',', $param['customer_type']);
- $whereOr = [];
- foreach ($arr as $val) {
- if (isset($gather[$val])) {
- //$whereOr[] = array_merge($condition,$gather[$val]);
- $whereOr[] = $gather[$val];
- }
- }
- if (!empty($whereOr)) {
- $new_cids = Customer::where($condition)->where(function ($query) use ($whereOr) {
- $query->whereOr($whereOr);
- })->column('id');
- unset($condition);
- $condition[] = ['id', 'in', $new_cids];
- } else {
- //无效条件
- return json(['code' => 0, 'data' => [], 'count' => 0]);
- }
- }
- $data = $this->selCustomerOrder($condition, $page, $limit, $order, $root_id);
- $no_visit_state = ['未到访', '待确认'];
- $visit_state = ['已到店', '已量房', '已到场', '已签单', '已转单', '已卖卡'];
- foreach ($data as $key => $val) {
- $data[$key]['customer_type'] = $val['crm_res_id'] ? '资源库' : ($val['remark'] == '活动报名建档' ? '活动' : ($val['agents_id'] ? '装修推荐官' : '自建'));
- $data[$key]['jiaoding_day'] = !empty($val['fisttime']) && strtotime($val['jiaoding_time']) > strtotime($val['fisttime']) ? ceil((strtotime($val['jiaoding_time']) - strtotime($val['fisttime'])) / 86400) : ceil((strtotime($val['jiaoding_time']) - strtotime($val['addtime'])) / 86400);
- $data[$key]['xinjushang'] = $request->empcrm['xinjushang'];
- // 有'已到店', '已量房', '已到场'状态的话不显示未到访
- $state = explode(',', $val['state']);
- $jiaoji = array_intersect($visit_state, $state);
- $no_jiaoji = array_intersect($no_visit_state, $state);
- if (count($no_jiaoji) == 2) { // 有未到访、待确认两个状态
- $state = ['未到访'];
- }
- if (count($state) > 1 && count($jiaoji) > 0) {
- $state = array_diff($state, $no_visit_state);
- }
- $data[$key]['state'] = implode(',', $state);
- }
- $count = Customer::where($condition)->count();
- return json(['code' => 0, 'data' => $data, 'count' => $count]);
- }
- //搜索
- public function search_list()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $is_manager = $request->empcrm->is_manager;
- $org_employee = [$empid];
- if (!$request->isAjax()) {
- //$this->screen();
- return View::fetch();
- }
- $param = $request->only(['page' => 1, 'limit' => 10, 'keyname']);
- if ($is_manager == 1) {
- $team_orgs = orgSubIds($request->empcrm->org_id);
- $condition[] = ['org_id', 'in', $team_orgs];
- } else {
- $condition[] = ['employee_id', '=', $empid];
- }
- $order = isset($param['order']) ? $param['order'] : 'id desc';
- $list = Customer::where($condition)->field('id,name,community_name,phone,employee_id,designer_id')->select()->toArray();
- $customersIdList = [];
- if (!empty($param['keyname'])) {
- foreach ($list as $key => $val) {
- $str = $val['name'] . $val['community_name'] . $val['phone'];
- if (strpos($str, trim($param['keyname'])) !== false) $customersIdList[] = $val['id'];
- }
- }
- $new = array_slice($customersIdList, ($param['page'] - 1) * $param['limit'], $param['limit']);
- $cond[] = ['id', 'in', $new];
- $data = $this->search_selone($cond, $order);
- $count = count($customersIdList);
- return json(['code' => 0, 'data' => $data, 'count' => $count]);
- }
- //搜索的单独查询
- public function search_selone($condition, $order)
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $data = Customer::with(['employee', 'org', 'designer', 'source'])
- ->withCount(['visitLog' => function ($query) {
- $query->where([[CustomerVisitLog::changeState(['state', '=', '未到访'])]]);
- }])
- ->where($condition)
- ->order($order)->select();
- $data = $data->visible(['id', 'employee_id', 'name', 'designer_id', 'community_name', 'phone', 'level', 'state', 'square', 'revisit_time', 'addtime', 'last_contact_date', 'org_id', 'protected_to', 'is_resource', 'employee.name', 'org_name', 'designer.name', 'visit_log_count', 'activity_frequency_count', 'source_id', 'source.source', 'phone1', 'phone2', 'remark', 'crm_res_id', 'agents_id', 'house_delivery_time', 'first_visit_date', 'ext', 'transfer_info'])->toArray();
- $setting = Setting::where([['name', '=', 'pubpool'], ['root_id', '=', $root_id]])->find();
- if (!empty($setting)) {
- $setting_content = json_decode($setting['content'], true);
- } else {
- $setting_content = [];
- }
- foreach ($data as &$item) {
- $item['un_protected'] = false;
- if (!empty($item['protected_to']) && time() > strtotime($item['protected_to'])) {
- $state_n = Customer::changeState($item['state'], 'n');
- if (isset($setting_content[$state_n]['state']) && $setting_content[$state_n]['state'] == 1) {
- $item['un_protected'] = true;
- }
- }
- $item['square'] = floatval($item['square']);
- $item['phone'] = substr_replace($item['phone'], '******', 3, 6);
- $item['phone1'] = $item['phone1'] ? substr_replace($item['phone1'], '******', 3, 6) : '';
- $item['phone2'] = $item['phone2'] ? substr_replace($item['phone2'], '******', 3, 6) : '';
- $item['jiaofang'] = !empty($item['house_delivery_time']) ? ($item['house_delivery_time'] <= date('Y/m/d', time()) ? '现房' : $item['house_delivery_time']) : '';
- $vlotime = CustomerVisitLog::where('customer_id', $item['id'])->order('addtime desc')->value('addtime');
- $fisttime = CustomerVisitLog::where([['customer_id', '=', $item['id']], ['customer_employee_id', '=', $item['employee_id']], [CustomerVisitLog::changeState(['state', '=', '确认到店'])]])->order('addtime asc')->value('addtime');
- $jiaoding_time = CustomerVisitLog::where([['customer_id', '=', $item['id']], ['customer_employee_id', '=', $item['employee_id']], [CustomerVisitLog::changeState(['state', '=', '已交定'])]])->order('addtime asc')->value('addtime');
- $signorder_time = CustomerVisitLog::where([['customer_id', '=', $item['id']], ['customer_employee_id', '=', $item['employee_id']], [CustomerVisitLog::changeState(['state', '=', '已签单'])]])->order('addtime asc')->value('addtime');
- $item['jiaoding_time'] = $jiaoding_time ? $jiaoding_time : '';
- $item['signorder_time'] = $signorder_time ? $signorder_time : '';
- $item['fisttime'] = $fisttime ? $fisttime : '';
- $item['time_status'] = $vlotime ? $this->uc_time_ago(date('Y-m-d', strtotime($vlotime))) : '';
- $item['add_wechat'] = '否';
- $item['sign_time'] = null;
- if (!empty($item['ext'])) {
- $extdata = json_decode($item['ext'], true);
- foreach ($extdata as $key => $val) {
- if (isset($val['keyname']) && $val['keyname'] == 'wechat' && !empty($val['value'])) {
- $add_wechat = CustomerPortraitFieldSelect::where('id', $val['value'])->value('name');
- if ($add_wechat == '有') {
- $add_wechat = '是';
- }
- $item['add_wechat'] = $add_wechat;
- }
- if (isset($val['keyname']) && $val['keyname'] == 'sign_time' && !empty($val['value'])) {
- $item['sign_time'] = $val['value'];
- }
- }
- }
- }
- return $data;
- }
- //处理查询客户之前的见面记录
- public function selBeforelog($metids, $firstday)
- {
- $statday = '2021-12-01 00:00:00';
- $state1 = CustomerVisitLog::changeState('确认到店', 'chaos');
- $state2 = CustomerVisitLog::changeState('已到场', 'chaos');
- $state3 = CustomerVisitLog::changeState('已量房', 'chaos');
- $q_where[] = ['customer_id', 'in', $metids];
- $q_where[] = ['state', 'in', array_merge($state1, $state2, $state3)];
- $q_where[] = ['addtime', 'between', [$statday, $firstday]];
- $qianvslog = CustomerVisitLog::where($q_where)->group('customer_id')->column('customer_id');
- $yesids = array_diff($metids, $qianvslog);
- return $yesids;
- }
- //日报
- public function daily()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $is_manager = $request->empcrm->is_manager;
- $org_employee = [$empid];
- $team_orgs = [$request->empcrm->org_id];
- if ($is_manager == 1) {
- $team_orgs = orgSubIds($request->empcrm->org_id);
- $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
- }
- $param = $request->only(['date', 'employee_id', 'org_id']);
- if (!$request->isAjax()) {
- $data['company_name'] = Company::where('root_id', $root_id)->value('company_name');
- $data['empname'] = $request->empcrm->name;
- $data['date'] = !empty($param['date']) ? $param['date'] : date('Y-m');
- $data['org_id'] = !empty($param['org_id']) ? $param['org_id'] : '';
- $data['org_name'] = !empty($param['org_id']) ? Org::where([['id', '=', $param['org_id']], ['root_id', '=', $root_id]])->value('name') : '';
- View::assign('data', $data);
- View::assign('is_manager', $is_manager);
- //部门所有员工
- $employee_list = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->field('id,name')->select()->toArray();
- View::assign('employee_list', $employee_list);
- //下级所有部门
- $org_list = Org::where([['id', 'in', $team_orgs], ['id', '<>', $request->empcrm->org_id]])->select()->toArray();
- View::assign('org_list', $org_list);
- return View::fetch();
- }
- $today = date('Y-m', time()) . '-01 00:00:00';
- $todaynum = date('d', time());
- $firstday = $param['date'] . '-' . '01 00:00:00';
- $lastday = date('Y-m-d 23:59:59', strtotime("$firstday +1 month -1 day"));
- $lastnum = date('d', strtotime("$firstday +1 month -1 day"));
- $nowempid = $empid;
- $noworgid = $request->empcrm->org_id;
- $sub_org = orgSubIds($noworgid);
- if ($is_manager == 1) $nowempid = $noworgid = '';
- if (!empty($param['employee_id']) && $is_manager == 1 && in_array($param['employee_id'], $org_employee)) $nowempid = $param['employee_id'];
- $where[] = ['confirm_date', 'between', [$firstday, $lastday]];
- if (!empty($param['employee_id']) && $is_manager == 1 && in_array($param['employee_id'], $org_employee)) $org_employee = [$param['employee_id']];
- if (!empty($param['org_id']) && $is_manager == 1) {
- $noworgid = $param['org_id'];
- $sub_org = orgSubIds($noworgid);
- $org_employee = Employee::where([['org_id', 'in', $sub_org], ['root_id', '=', $root_id], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
- }
- $where[] = ['customer_employee_id', 'in', $org_employee];
- //$list = Customer::where($where)->field('id,name,addtime,state,valid_time')->select()->toArray();
- //电话记录
- $hu_where = [];
- $hu_where[] = ['employee_id', 'in', $org_employee];
- $hu_where[] = ['root_id', '=', $root_id];
- $hu_where[] = ['addtime', '>=', $firstday];
- $hu_where[] = ['addtime', '<=', $lastday];
- $phonelist = OutCallLog::where($hu_where)->column('addtime,status,billsec');
- // 手机打电话次数获取
- $mbphlist = OutCallMbLog::where($hu_where)->column('addtime');
- //转发次数
- $shares_list = ShareLog::where([['employee_id', 'in', $org_employee], ['share_time', '>=', $firstday], ['share_time', '<=', $lastday]])->column('id,share_time');
- //新增加客户数
- $new_cuslist = Customer::where([['sign_time', '>=', $firstday], ['sign_time', '<=', $lastday], ['employee_id', 'in', $org_employee], ['state', 'not in', array_merge(Customer::changeState('无效', 'chaos'), Customer::changeState('待确认', 'chaos'))]])->column('id,sign_time,state,died'); //->select()->toArray();
- //待确认的客户
- $treat_cuslist = Customer::where([['sign_time', '>=', $firstday], ['sign_time', '<=', $lastday], ['employee_id', 'in', $org_employee], ['state', 'in', Customer::changeState('待确认', 'chaos')]])->column('sign_time', 'id');
- //客户派单数
- $assign_list = Customer::where([['assign_time', '>=', $firstday], ['assign_time', '<=', $lastday], ['employee_id', 'in', $org_employee]])->column('id,assign_time,assign_type');
- //置为无效的客户
- $invalidcus = CustomerInvalidLog::where([['root_id', '=', $root_id], ['employee_id', 'in', $org_employee], ['cus_addtime', 'between', [$firstday, $lastday]]])->column('cus_addtime,employee_id,customer_id,status');
- $state1 = Customer::changeState('待确认', 'chaos');
- $state2 = Customer::changeState('无效', 'chaos');
- $state3 = CustomerVisitLog::changeState('已交定', 'chaos');
- $state4 = CustomerVisitLog::changeState('已签单', 'chaos');
- $state5 = CustomerVisitLog::changeState('确认到店', 'chaos');
- $state6 = CustomerVisitLog::changeState('无效', 'chaos');
- $state7 = CustomerVisitLog::changeState('未到访', 'chaos');
- $state8 = CustomerVisitLog::changeState('已到场', 'chaos');
- $state9 = CustomerVisitLog::changeState('已量房', 'chaos');
- //记录
- $visitlog_list = CustomerVisitLog::where($where)
- ->where([['state', 'in', array_merge($state3, $state4, $state5, $state6, $state7, $state8, $state9)]])
- //->group('state,customer_id')
- ->order('addtime asc')
- ->column('customer_id,state,money,confirm_date');
- $metids = [];
- foreach ($visitlog_list as $key => $val) {
- if (in_array($val['state'], array_merge($state5, $state8, $state9))) $metids[] = $val['customer_id'];
- }
- $yesids = !empty($metids) ? $this->selBeforelog(array_unique($metids), $firstday) : [];
- $xin = [];
- $weekarray = array("日", "一", "二", "三", "四", "五", "六"); //先定义一个数组
- if ($today == $firstday) {
- for ($i = 0; $i < $todaynum; $i++) {
- $day = date('Y-m-d', strtotime("$firstday +$i days"));
- $a['day'] = $day;
- $a['week'] = $weekarray[date("w", strtotime("$firstday +$i days"))];
- $a['logdata'] = [];
- foreach ($visitlog_list as $key => $val) {
- if ($val['confirm_date'] >= $day . ' 00:00:00' && $val['confirm_date'] <= $day . ' 23:59:59') {
- $a['logdata'][] = $val;
- }
- }
- $xin[] = $a;
- }
- } elseif ($today > $firstday) {
- for ($i = 0; $i < $lastnum; $i++) {
- $day = date('Y-m-d', strtotime("$firstday +$i days"));
- $a['day'] = $day;
- $a['week'] = $weekarray[date("w", strtotime("$firstday +$i days"))];
- $a['logdata'] = [];
- foreach ($visitlog_list as $key => $val) {
- if ($val['confirm_date'] >= $day . ' 00:00:00' && $val['confirm_date'] <= $day . ' 23:59:59') {
- $a['logdata'][] = $val;
- }
- }
- $xin[] = $a;
- }
- }
- unset($visitlog_list);
- $meetids = [];
- $t = ['day' => '求和', 'week' => '', 'data' => [], 'log_customer_count' => 0, 'yixiang' => 0, 'liangfang' => 0, 'daodian' => 0, 'new_cus' => 0, 'shares_count' => 0, 'phone_count' => 0, 'on_phone_count' => 0, 'phone_time' => 0, 'clue_cont' => 0, 'valid_cont' => 0, 'valid_percen' => 0, 'meet_cont' => 0, 'meet_percen' => 0, 'deposit_cont' => 0, 'sign_cont' => 0, 'basic_money' => 0, 'other_money' => 0, 'all_money' => 0, 'invalid_no' => 0, 'invalid_yes' => 0, 'invalid_meet' => 0];
- foreach ($xin as $key => $val) {
- //$xin[$key]['clue_cont'] = count($new_cuslist);
- $xin[$key]['nowempid'] = $nowempid;
- $xin[$key]['noworgid'] = $noworgid;
- $yixiang = $liangfang = $daodian = $new_cus = $shares_count = $phone_count = $on_phone_count = $phone_time = $log_customer_count = $share_count = $valid = $meet = $deposit = $sign = $basic_money = $other_money = $invalid_no = $invalid_yes = $invalid_meet = 0;
- $depositids = $invalids = $daystate = $cusids = $validids = [];
- // 外呼系统统计
- $start = $val['day'] . ' 00:00:00';
- $end = $val['day'] . ' 23:59:59';
- foreach ($phonelist as $k => $v) {
- if ($v['addtime'] >= $start && $v['addtime'] <= $end) {
- if ($v['status'] == 1) $on_phone_count++;
- $phone_time += $v['billsec'];
- $phone_count++;
- }
- }
- foreach ($mbphlist as $k => $v) {
- if ($v >= $start && $v <= $end) {
- $phone_count++;
- }
- }
- //转发次数
- foreach ($shares_list as $k => $v) {
- if ($v['share_time'] >= $start && $v['share_time'] <= $end) {
- $shares_count++;
- }
- }
- //新增客户数
- foreach ($new_cuslist as $k => $v) {
- if ($v['sign_time'] >= $start && $v['sign_time'] <= $end) {
- $new_cus++;
- if ($v['died'] != 2 && !in_array($v['state'], array_merge($state1, $state2), true)) {
- $valid++;
- $validids[] = $v['id'];
- }
- $cusids[] = $v['id'];
- }
- }
- //待确认客户
- foreach ($treat_cuslist as $k => $v) {
- if ($v >= $start && $v <= $end) {
- $new_cus++;
- $cusids[] = $k;
- }
- }
- //无效客户数增加
- foreach ($invalidcus as $v) {
- if ($v['cus_addtime'] >= $start && $v['cus_addtime'] <= $end && !in_array($v['customer_id'], $cusids)) {
- $new_cus++;
- $cusids[] = $v['customer_id'];
- }
- if ($v['cus_addtime'] >= $start && $v['cus_addtime'] <= $end && !in_array($v['customer_id'], $validids)) {
- if ($v['status'] == 1) {
- $valid++;
- $validids[] = $v['customer_id'];
- }
- }
- }
- unset($cusids, $validids);
- //派单数
- foreach ($assign_list as $k => $v) {
- if ($v['assign_time'] >= $start && $v['assign_time'] <= $end) {
- if ($v['assign_type'] == 'yixiang') $yixiang++;
- if ($v['assign_type'] == 'liangfang') $liangfang++;
- if ($v['assign_type'] == 'daodian') $daodian++;
- }
- }
- foreach ($val['logdata'] as $ky => $v) {
- if (in_array($v['state'], $state3, true) && !in_array($v['customer_id'], $depositids)) {
- $deposit++;
- $depositids[] = $v['customer_id'];
- }
- if (in_array($v['state'], $state4, true)) {
- $sign++;
- $basic_money += $v['money'];
- }
- if (in_array($v['state'], array_merge($state5, $state8, $state9)) && !in_array($v['customer_id'], $meetids) && in_array($v['customer_id'], $yesids)) {
- $meet++;
- $meetids[] = $v['customer_id'];
- }
- if (in_array($v['state'], $state6)) {
- $invalid_yes++;
- $invalids[] = $v['customer_id'];
- }
- }
- foreach ($invalids as $k => $v) {
- if (in_array($v, $meetids)) $invalid_meet++;
- }
- //$xin[$key]['invalid_yesids'] = $invalid_yesids;
- $xin[$key]['valid_cont'] = $valid;
- $xin[$key]['valid_percen'] = $valid && !empty($new_cus) ? round($valid / $new_cus * 100, 2) : 0;
- $xin[$key]['meet_cont'] = $meet;
- $xin[$key]['meet_percen'] = !empty($meet) && !empty($new_cus) ? round($meet / $new_cus * 100, 2) : 0;
- $xin[$key]['deposit_cont'] = $deposit;
- $xin[$key]['sign_cont'] = $sign;
- $xin[$key]['basic_money'] = $basic_money;
- $xin[$key]['other_money'] = $other_money;
- $xin[$key]['all_money'] = $basic_money + $other_money;
- $xin[$key]['invalid_no'] = $invalid_no;
- $xin[$key]['invalid_yes'] = $invalid_yes;
- $xin[$key]['invalid_meet'] = $invalid_meet;
- $xin[$key]['log_customer_count'] = count($val['logdata']);
- $xin[$key]['phone_count'] = $phone_count;
- $xin[$key]['on_phone_count'] = $on_phone_count;
- $xin[$key]['phone_time'] = $phone_time;
- $xin[$key]['shares_count'] = $shares_count;
- $xin[$key]['new_cus'] = $new_cus;
- $xin[$key]['yixiang'] = $yixiang;
- $xin[$key]['liangfang'] = $liangfang;
- $xin[$key]['daodian'] = $daodian;
- //总数
- $t['clue_cont'] += $new_cus;
- $t['valid_cont'] += $valid;
- $t['meet_cont'] += $meet;
- $t['deposit_cont'] += $deposit;
- $t['sign_cont'] += $sign;
- $t['basic_money'] += $basic_money;
- $t['other_money'] += $other_money;
- $t['all_money'] += $other_money + $basic_money;
- $t['invalid_no'] += $invalid_no;
- $t['invalid_yes'] += $invalid_yes;
- $t['invalid_meet'] += $invalid_meet;
- $t['log_customer_count'] += count($val['logdata']);
- $t['phone_count'] += $phone_count;
- $t['on_phone_count'] += $on_phone_count;
- $t['phone_time'] += $phone_time;
- $t['shares_count'] += $shares_count;
- $t['new_cus'] += $new_cus;
- $t['yixiang'] += $yixiang;
- $t['liangfang'] += $liangfang;
- $t['daodian'] += $daodian;
- }
- if ($t['valid_cont'] !== 0) {
- $t['valid_percen'] = round($t['valid_cont'] / $t['new_cus'] * 100, 2);
- $t['meet_percen'] = round($t['meet_cont'] / $t['new_cus'] * 100, 2);
- }
- $xin = array_reverse($xin);
- array_push($xin, $t);
- return json(['code' => 0, 'data' => $xin, 'count' => 0, 'msg' => '获取成功']);
- }
- //分派
- public function assignment_list()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $is_manager = $request->empcrm->is_manager;
- $org_employee = [$empid];
- if ($is_manager == 1) {
- $team_orgs = orgSubIds($request->empcrm->org_id);
- $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
- }
- if (!$request->isAjax()) {
- $this->screen();
- $newteam_orgs = orgSubIds($request->empcrm->org_id);
- $assignemp_list = Employee::where([['org_id', 'in', $newteam_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->field('id,name')->select()->toArray();
- View::assign('assignemp_list', $assignemp_list);
- View::assign('is_manager', $is_manager);
- return View::fetch();
- }
- $param = $request->only(['page' => 1, 'limit' => 10, 'employee_id', 'level', 'source_id', 'designer_id', 'keyname', 'phone']);
- if (!empty($param['employee_id']) && $is_manager == 1) $org_employee = [$param['employee_id']];
- $condition[] = ['employee_id', 'in', $org_employee];
- $condition[] = ['transfer_info', '<>', ''];
- $order = isset($param['order']) ? $param['order'] : 'updatetime desc';
- $page = intval($param['page']);
- $limit = intval($param['limit']);
- if (!empty($param['level'])) $condition[] = ['level', '=', $param['level']];
- if (!empty($param['source_id'])) $condition[] = ['source_id', '=', $param['source_id']];
- if (!empty($param['designer_id'])) $condition[] = ['designer_id', '=', $param['designer_id']];
- if (!empty($param['keyname'])) $condition[] = ['name|community_name', 'like', '%' . $param['keyname'] . '%'];
- if (!empty($param['phone'])) {
- $list = Customer::where($condition)->column('id', 'phone');
- $keyCustomersId = [];
- foreach ($list as $k => $v) {
- if (cypherphone($param['phone']) == $k) $keyCustomersId[] = $v;
- }
- unset($condition);
- $condition[] = ['id', 'in', $keyCustomersId];
- }
- $data = $this->selCustomer($condition, $page, $limit, $order, $root_id);
- foreach ($data as $key => $val) {
- $transfer = json_decode($val['transfer_info'], true);
- $data[$key]['transfer_time'] = $transfer['transfer_time'];
- $data[$key]['transfer_name'] = $transfer['transfer_empname'];
- $data[$key]['jiange_day'] = ceil((strtotime(date('Y-m-d')) - strtotime($val['last_contact_date'])) / 86400);
- }
- $count = Customer::where($condition)->count();
- return json(['code' => 0, 'data' => $data, 'count' => $count]);
- }
- //公用列表筛选
- public function screen()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $is_manager = $request->empcrm->is_manager;
- $team_orgs = [$request->empcrm->org_id];
- if ($is_manager == 1) {
- $team_orgs = orgSubIds($request->empcrm->org_id);
- //$org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
- }
- $soudata = CustomerSource::field('id,source as name')->where('root_id', $root_id)->select()->toArray();
- View::assign('soudata', $soudata);
- $list = $this->get_designer();
- if (!empty($list)) {
- // $list = hanziInitsort($list, 'name');
- $sort = array_column($list, 's');
- sort($sort);
- array_multisort($sort, SORT_ASC, $list);
- $sort = array_unique($sort);
- $list = ['sort' => $sort, 'arr' => $list];
- } else {
- $list = ['sort' => [], 'arr' => []];
- }
- View::assign('designers', $list);
- //客户等级
- $level = CustomerPortraitField::with(['select'])->where([['root_id', '=', $root_id], ['keyname', '=', 'level']])->find()->toArray();
- View::assign('level', $level['select']);
- //部门所有员工
- $employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->field('id,name')->select()->toArray();
- View::assign('employee_list', $employee);
- View::assign('empid', $empid);
- $customer_type = [['id' => 1, 'name' => '资源库'], ['id' => 2, 'name' => '自建'], ['id' => 3, 'name' => '活动'], ['id' => 4, 'name' => '装修推荐官']];
- View::assign('customer_type', $customer_type);
- }
- /**
- * 有效客户联查(包含回收站)
- */
- public function selCustomerValid($condition, $con_union, $page, $limit, $order, $root_id)
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $is_manager = $request->empcrm->is_manager;
- $path = Org::where([['pid', '=', $request->empcrm->org_id]])->count();
- $prefix = env('database.prefix');
- if ($page && $limit) {
- $data = Db::table($prefix . 'customer')
- ->field('id,designer_id,employee_id,name,introduce,community_name,phone,level,state,square,revisit_time,addtime,last_contact_date,org_id,protected_to,is_resource,source_id,phone1,phone2,remark,crm_res_id,agents_id,house_delivery_time,first_visit_date,ext,transfer_info,deposit_money,signed_money,package_id,fresh,house_type,remark,assigned_personnel')
- ->where($condition)
- ->union(function ($query) use ($con_union) {
- $prefix = env('database.prefix');
- $query->field('customer_id id,designer_id,employee_id,name,introduce,community_name,phone,level,state,square,revisit_time,addtime,last_contact_date,org_id,protected_to,is_resource,source_id,phone1,phone2,remark,crm_res_id,agents_id,house_delivery_time,first_visit_date,ext,transfer_info,deposit_money,signed_money,package_id,fresh,house_type,remark,assigned_personnel')->where($con_union)->table($prefix . 'customer_recycle');
- })->order($order)->select()->toArray(); //,employee_time,add_wechat_time
- } else {
- $data = Db::table($prefix . 'customer')
- ->field('id,designer_id,employee_id,name,introduce,community_name,phone,level,state,square,revisit_time,addtime,last_contact_date,org_id,protected_to,is_resource,source_id,phone1,phone2,remark,crm_res_id,agents_id,house_delivery_time,first_visit_date,ext,transfer_info,deposit_money,signed_money,package_id,fresh,house_type,remark,assigned_personnel')
- ->where($condition)
- ->union(function ($query) use ($con_union) {
- $prefix = env('database.prefix');
- $query->field('customer_id id,designer_id,employee_id,name,introduce,community_name,phone,level,state,square,revisit_time,addtime,last_contact_date,org_id,protected_to,is_resource,source_id,phone1,phone2,remark,crm_res_id,agents_id,house_delivery_time,first_visit_date,ext,transfer_info,deposit_money,signed_money,package_id,fresh,house_type,remark,assigned_personnel')->where($con_union)->table($prefix . 'customer_recycle');
- })->page($page, $limit)->order($order)->select()->toArray(); //,employee_time,add_wechat_time
- }
- $aec = new Aec(config('app.aec_key'), config('app.aec_iv'));
- foreach ($data as $k => $v) {
- $data[$k]['state'] = Customer::changeState($v['state'], 'state');
- $phone = $aec->decrypt($v['phone']);
- $phone1 = !empty($v['phone1']) ? $aec->decrypt($v['phone1']) : '';
- $phone2 = !empty($v['phone2']) ? $aec->decrypt($v['phone2']) : '';
- $data[$k]['phone'] = substr_replace($phone, '******', 3, 6);
- $data[$k]['phone1'] = $phone1 ? substr_replace($phone1, '******', 3, 6) : '';
- $data[$k]['phone2'] = $phone2 ? substr_replace($phone2, '******', 3, 6) : '';
- $employee = Employee::find($v['employee_id']);
- $data[$k]['employee'] = null;
- if (!empty($employee)) {
- $data[$k]['employee']['name'] = $employee['name'];
- }
- $employee = Employee::find($v['designer_id']);
- $data[$k]['designer'] = null;
- if (!empty($employee)) {
- $data[$k]['designer']['name'] = $employee['name'];
- }
- $data[$k]['visit_log_count'] = CustomerVisitLog::where([['customer_id', '=', $v['id']], [CustomerVisitLog::changeState(['state', '=', '未到访'])]])->count();
- $data[$k]['source'] = null;
- if ($v['source_id']) {
- $source = CustomerSource::where('id', '=', $v['source_id'])->find();
- if (!empty($source)) {
- $data[$k]['source']['source'] = $source['source'];
- }
- }
- $data[$k]['org_name'] = [];
- if ($v['org_id']) {
- $org = Org::where('id', '=', $v['org_id'])->find();
- if (!empty($org)) {
- $data[$k]['org_name'] = $org['name'];
- }
- }
- $visit_log = CustomerVisitLog::where('customer_id', '=', $v['id'])->field('id,customer_id,state,addtime,confirm_date,money')->order('addtime asc')->select()->toArray();
- foreach ($visit_log as $k_log => $v_log) {
- $visit_log[$k_log]['state'] = CustomerVisitLog::changeState($v_log['state'], 'state');
- }
- $data[$k]['visitLog'] = $visit_log;
- }
- $setting = Setting::where([['name', '=', 'pubpool'], ['root_id', '=', $root_id]])->find();
- if (!empty($setting)) {
- $setting_content = json_decode($setting['content'], true);
- } else {
- $setting_content = [];
- }
- $empids = !empty($data) ? array_column($data, 'employee_id') : [];
- $emporg = Employee::with(['org' => function ($query) {
- $query->field('id,name');
- }])->where([['root_id', '=', $root_id], ['id', 'in', $empids]])->field('id,org_id')->select()->toArray();
- $state1 = CustomerVisitLog::changeState('已到店', 'chaos');
- $state2 = CustomerVisitLog::changeState('已交定', 'chaos');
- $state3 = CustomerVisitLog::changeState('已签单', 'chaos');
- $state4 = CustomerVisitLog::changeState('已量房', 'chaos');
- $state5 = CustomerVisitLog::changeState('无效', 'chaos');
- foreach ($data as &$item) {
- $item['emporg'] = $item['source_name'] = $item['designer_name'] = $item['employee_name'] = '';
- if (!empty($item['source'])) {
- $item['source_name'] = $item['source']['source'];
- }
- if (!empty($item['designer'])) {
- $item['designer_name'] = $item['designer']['name'];
- }
- if (!empty($item['employee'])) {
- $item['employee_name'] = $item['employee']['name'];
- foreach ($emporg as $p) {
- if ($p['id'] == $item['employee_id']) $item['emporg'] = $p['org']['name'];
- }
- }
- $item['un_protected'] = false;
- if (!empty($item['protected_to']) && time() > strtotime($item['protected_to'])) {
- $state_n = Customer::changeState($item['state'], 'n');
- if (isset($setting_content[$state_n]['state']) && $setting_content[$state_n]['state'] == 1) {
- $item['un_protected'] = true;
- }
- }
- $item['last_invalid_time'] = $item['first_liangfang'] = $item['jiaoding_time'] = $item['sign'] = $item['fisttime'] = '';
- if (!empty($item['visitLog'])) {
- //取最新的一条为客户的跟进时间
- $item['last_contact_date'] = $item['visitLog'][count($item['visitLog']) - 1]['addtime'];
- $state = [];
- foreach ($item['visitLog'] as $k => $v) {
- $state[$v['state']][] = $v;
- }
- foreach ($state as $k => $v) {
- if (in_array($k, $state1)) $item['fisttime'] = date('Y-m-d', strtotime($v[0]['confirm_date']));
- if (in_array($k, $state4)) $item['first_liangfang'] = date('Y-m-d', strtotime($v[0]['addtime']));
- if (in_array($k, $state2)) {
- $item['jiaoding_time'] = $v[0]['addtime'] ?? '';
- $item['jiaoding_money'] = $v[0]['money'] ?? '';
- }
- if (in_array($k, $state3)) {
- $item['qiandan_time'] = $v[0]['addtime'] ?? '';
- $item['qiandan_money'] = $v[0]['money'] ?? '';
- $item['sign'] = $v[0];
- }
- if (in_array($k, $state5)) $item['last_invalid_time'] = $v[0]['addtime'];
- }
- }
- $item['square'] = floatval($item['square']);
- $item['phone'] = substr_replace($item['phone'], '******', 3, 6);
- $item['phone1'] = $item['phone1'] ? substr_replace($item['phone1'], '******', 3, 6) : '';
- $item['phone2'] = $item['phone2'] ? substr_replace($item['phone2'], '******', 3, 6) : '';
- $houses = $item['house_type'];
- if (!empty($item['house_type']) && $item['house_type'] == '期房') $houses = !empty($item['house_delivery_time']) ? (str_replace('/', '-', $item['house_delivery_time']) <= date('Y-m-d', time()) ? '现房' : $item['house_delivery_time']) : '期房';
- $item['jiaofang'] = $houses;
- $item['time_status'] = $item['last_contact_date'] ? $this->uc_time_ago($item['last_contact_date']) : '';
- $item['add_wechat'] = '否';
- $item['sign_time'] = null;
- $item['allstate'] = null;
- $item['package_name'] = !empty($item['package_id']) ? CustomerPackage::where('id', $item['package_id'])->value('name') : '';
- if (!empty($item['ext'])) {
- $extdata = json_decode($item['ext'], true);
- foreach ($extdata as $key => $val) {
- if (isset($val['keyname']) && $val['keyname'] == 'wechat' && !empty($val['value'])) {
- $add_wechat = CustomerPortraitFieldSelect::where('id', $val['value'])->value('name');
- if ($add_wechat == '有') {
- $add_wechat = '是';
- }
- $item['add_wechat'] = $add_wechat;
- }
- if (isset($val['keyname']) && $val['keyname'] == 'sign_time' && !empty($val['value'])) {
- $item['sign_time'] = str_replace('/', '-', $val['value']);
- }
- }
- }
- $item['is_followup'] = 0;
- if ($is_manager == 1 && empty($path)) {
- $item['is_followup'] = 1;
- }
- if ($empid == $item['employee_id']) {
- $item['is_followup'] = 1;
- }
- $now_status = $item['state'];
- if ($now_status == '已签单') $now_status = '已转单';
- if ($now_status == '已交定') $now_status = '已签单';
- $item['now_status'] = $now_status;
- $item['state'] = '';
- $allstate = '';
- if (!empty($item['visitLog'])) {
- $stain = [$now_status];
- foreach ($item['visitLog'] as $k => $v) {
- if (!in_array($v['state'], ['已交定', '已签单', '已到店', '已量房', '已到场', '未到访', '回访'])) continue;
- if ($v['state'] == '回访') $v['state'] = '待确认';
- if ($v['state'] == '已签单') $v['state'] = '已转单';
- if ($v['state'] == '已交定') $v['state'] = '已签单';
- if (!in_array($v['state'], $stain)) {
- $stain[] = $v['state'];
- //$allstate.= $v['state'].',';
- }
- }
- if (count($stain) > 1 && in_array('待确认', $stain)) $stain = array_diff($stain, ['待确认']);
- //$item['state'] = trim($allstate,',');
- $item['state'] = implode(',', $stain);
- if ($now_status == '待确认') $item['state'] = '待确认';
- }
- // 未跟进时间
- //$latest_visit_log = CustomerVisitLog::where([['customer_id', '=', $item['id']]])->order('addtime desc')->find();
- $latest_visit_log = $item['last_contact_date'];
- if (empty($latest_visit_log)) {
- $no_visit_long = time() - strtotime($item['addtime']);
- } else {
- $no_visit_long = time() - strtotime($latest_visit_log);
- }
- $no_visit_day = ceil($no_visit_long / 24 / 3600);
- $item['no_visit_day'] = $no_visit_day;
- // 预计见面时间
- $subscribe = CustomersSubscribe::where([['customer_id', '=', $item['id']], ['employee_id', '=', $item['employee_id']], ['state', '=', 0]])->order('subscribe_date desc')->find();
- if (!empty($subscribe)) {
- $item['subscribe_date'] = $subscribe['subscribe_date'];
- } else {
- $item['subscribe_date'] = '';
- }
- $assignIdList = !empty($item['assigned_personnel']) ? explode(',', $item['assigned_personnel']) : [];
- if (!empty($item['designer_id']) && !empty($assignIdList)) $assignIdList = array_diff($assignIdList, [$item['designer_id']]);
- $assign_list = Employee::where('id', 'in', $assignIdList)->column('name');
- $item['assign_list'] = !empty($assign_list) ? implode(',', $assign_list) : '';
- }
- return $data;
- }
- public function selCustomerOrder($condition, $page, $limit, $order, $root_id)
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $is_manager = $request->empcrm->is_manager;
- $path = Org::where([['pid', '=', $request->empcrm->org_id]])->count();
- //创建临时查询报名时间-以便排序
- $sign_time = "if(((`ext` regexp '[0-9]{4}[/|-][0-9]{1,2}[/|-][0-9]{1,2} [0-9]{1,2}[:][0-9]{1,2}\", \"keyname\": \"sign_time\"') = 1),replace(replace(substr(`ext`,(locate('\", \"keyname\": \"sign_time\"}',`ext`) - 18),18),'\"',''),'/','-'),NULL) as tmp_sign_time";
- $data = Customer::with(['employee', 'org', 'designer', 'source', 'visitLog' => function ($query) {
- $query->field('id,customer_id,state,addtime,confirm_date,money')->order('addtime asc');
- }])
- ->withCount(['visitLog' => function ($query) {
- $query->where([[CustomerVisitLog::changeState(['state', '=', '未到访'])]]);
- }])
- ->field($sign_time)
- ->where($condition)
- ->page($page, $limit)
- ->order($order)->select();
- $data = $data->visible(['designer_id', 'id', 'employee_id', 'name', 'introduce', 'community_name', 'phone', 'level', 'state', 'square', 'revisit_time', 'addtime', 'last_contact_date', 'org_id', 'protected_to', 'is_resource', 'employee.name', 'org_name', 'designer.name', 'visit_log_count', 'activity_frequency_count', 'source_id', 'source.source', 'phone1', 'phone2', 'remark', 'crm_res_id', 'agents_id', 'sign_time', 'house_delivery_time', 'first_visit_date', 'ext', 'transfer_info', 'deposit_money', 'signed_money', 'package_id', 'fresh', 'house_type', 'remark', 'employee_time', 'add_wechat_time', 'assigned_personnel'])->toArray();
- $setting = Setting::where([['name', '=', 'pubpool'], ['root_id', '=', $root_id]])->find();
- if (!empty($setting)) {
- $setting_content = json_decode($setting['content'], true);
- } else {
- $setting_content = [];
- }
- $empids = !empty($data) ? array_column($data, 'employee_id') : [];
- $emporg = Employee::with(['org' => function ($query) {
- $query->field('id,name');
- }])->where([['root_id', '=', $root_id], ['id', 'in', $empids]])->field('id,org_id')->select()->toArray();
- $state1 = CustomerVisitLog::changeState('已到店', 'chaos');
- $state2 = CustomerVisitLog::changeState('已交定', 'chaos');
- $state3 = CustomerVisitLog::changeState('已签单', 'chaos');
- $state4 = CustomerVisitLog::changeState('已量房', 'chaos');
- $state5 = CustomerVisitLog::changeState('无效', 'chaos');
- foreach ($data as &$item) {
- $item['emporg'] = $item['source_name'] = $item['designer_name'] = $item['employee_name'] = '';
- if (!empty($item['source'])) {
- $item['source_name'] = $item['source']['source'];
- }
- if (!empty($item['designer'])) {
- $item['designer_name'] = $item['designer']['name'];
- }
- if (!empty($item['employee'])) {
- $item['employee_name'] = $item['employee']['name'];
- foreach ($emporg as $p) {
- if ($p['id'] == $item['employee_id']) $item['emporg'] = $p['org']['name'];
- }
- }
- $item['un_protected'] = false;
- if (!empty($item['protected_to']) && time() > strtotime($item['protected_to'])) {
- $state_n = Customer::changeState($item['state'], 'n');
- if (isset($setting_content[$state_n]['state']) && $setting_content[$state_n]['state'] == 1) {
- $item['un_protected'] = true;
- }
- }
- $item['last_invalid_time'] = $item['first_liangfang'] = $item['jiaoding_time'] = $item['sign'] = $item['fisttime'] = '';
- if (!empty($item['visitLog'])) {
- //取最新的一条为客户的跟进时间
- $item['last_contact_date'] = $item['visitLog'][count($item['visitLog']) - 1]['addtime'];
- $state = [];
- foreach ($item['visitLog'] as $k => $v) {
- $state[$v['state']][] = $v;
- }
- foreach ($state as $k => $v) {
- if (in_array($k, $state1)) $item['fisttime'] = date('Y-m-d', strtotime($v[0]['confirm_date']));
- if (in_array($k, $state4)) $item['first_liangfang'] = date('Y-m-d', strtotime($v[0]['addtime']));
- if (in_array($k, $state2)) {
- $item['jiaoding_time'] = $v[0]['addtime'];
- $item['jiaoding_money'] = $v[0]['money'];
- // $tmp_jiao_count = count($state['已交定']);
- // if ($tmp_jiao_count > 0) {
- // $item['jiaoding_time'] = $v[$tmp_jiao_count - 1]['addtime'];
- // $item['jiaoding_money'] = $v[$tmp_jiao_count - 1]['money'];
- // }
- }
- if (in_array($k, $state3)) {
- $item['qiandan_time'] = $v[0]['addtime'];
- $item['qiandan_money'] = $v[0]['money'];
- $item['sign'] = $v[0];
- // $tmp_sign_count = count($state['已签单']);
- // if ($tmp_sign_count > 0) {
- // $item['qiandan_time'] = $v[$tmp_sign_count - 1]['addtime'];
- // $item['qiandan_money'] = $v[$tmp_sign_count - 1]['money'];
- // $item['sign'] = $v[$tmp_sign_count - 1];
- // }
- }
- if (in_array($k, $state5)) $item['last_invalid_time'] = $v[0]['addtime'];
- }
- }
- $item['square'] = floatval($item['square']);
- //$item['visitLog'] = CustomerVisitLog::where([['customer_employee_id','=',$item['employee_id']],['customer_id','=',$item['id']],['state','in',array_merge(CustomerVisitLog::changeState('待确认','chaos'),CustomerVisitLog::changeState('已到店','chaos'),CustomerVisitLog::changeState('已到场','chaos'),CustomerVisitLog::changeState('已交定','chaos'),CustomerVisitLog::changeState('已签单','chaos'))]])->group('state,customer_id')->field('state,id,customer_id')->order('addtime asc')->select()->toArray();
- $item['phone'] = substr_replace($item['phone'], '******', 3, 6);
- $item['phone1'] = $item['phone1'] ? substr_replace($item['phone1'], '******', 3, 6) : '';
- $item['phone2'] = $item['phone2'] ? substr_replace($item['phone2'], '******', 3, 6) : '';
- $houses = $item['house_type'];
- if (!empty($item['house_type']) && $item['house_type'] == '期房') $houses = !empty($item['house_delivery_time']) ? (str_replace('/', '-', $item['house_delivery_time']) <= date('Y-m-d', time()) ? '现房' : $item['house_delivery_time']) : '期房';
- $item['jiaofang'] = $houses;
- $item['time_status'] = $item['last_contact_date'] ? $this->uc_time_ago($item['last_contact_date']) : '';
- $item['add_wechat'] = '否';
- $item['sign_time'] = null;
- $item['nowstate'] = $item['state'];
- $item['package_name'] = !empty($item['package_id']) ? CustomerPackage::where('id', $item['package_id'])->value('name') : '';
- if (!empty($item['ext'])) {
- $extdata = json_decode($item['ext'], true);
- foreach ($extdata as $key => $val) {
- if (isset($val['keyname']) && $val['keyname'] == 'wechat' && !empty($val['value'])) {
- $add_wechat = CustomerPortraitFieldSelect::where('id', $val['value'])->value('name');
- if ($add_wechat == '有') {
- $add_wechat = '是';
- }
- $item['add_wechat'] = $add_wechat;
- }
- if (isset($val['keyname']) && $val['keyname'] == 'sign_time' && !empty($val['value'])) {
- $item['sign_time'] = str_replace('/', '-', $val['value']);
- }
- }
- }
- $item['is_followup'] = 0;
- if ($is_manager == 1 && empty($path)) {
- $item['is_followup'] = 1;
- }
- if ($empid == $item['employee_id']) {
- $item['is_followup'] = 1;
- }
- $now_status = $item['state'];
- if ($now_status == '已签单' && $request->empcrm['xinjushang'] == 0) $now_status = '已转单';
- if ($now_status == '已交定' && $request->empcrm['xinjushang'] == 0) $now_status = '已签单';
- $item['now_status'] = $now_status;
- $item['state'] = '';
- //$allstate='';
- if (!empty($item['visitLog'])) {
- $stain = [$now_status];
- foreach ($item['visitLog'] as $k => $v) {
- if (!in_array($v['state'], ['已交定', '已签单', '已到店', '已量房', '已到场', '未到访', '回访'])) continue;
- if ($v['state'] == '回访') $v['state'] = '待确认';
- if ($v['state'] == '已签单' && $request->empcrm['xinjushang'] == 0) $v['state'] = '已转单';
- if ($v['state'] == '已交定' && $request->empcrm['xinjushang'] == 0) $v['state'] = '已签单';
- if (!in_array($v['state'], $stain)) {
- $stain[] = $v['state'];
- //$allstate.= $v['state'].',';
- }
- }
- if (count($stain) > 1 && in_array('待确认', $stain)) $stain = array_diff($stain, ['待确认']);
- //$item['state'] = trim($allstate,',');
- $item['state'] = implode(',', $stain);
- if ($now_status == '待确认') $item['state'] = '待确认';
- }
- // 未跟进时间
- //$latest_visit_log = CustomerVisitLog::where([['customer_id', '=', $item['id']]])->order('addtime desc')->find();
- $latest_visit_log = $item['last_contact_date'];
- if (empty($latest_visit_log)) {
- $no_visit_long = time() - strtotime($item['addtime']);
- } else {
- $no_visit_long = time() - strtotime($latest_visit_log);
- }
- $no_visit_day = ceil($no_visit_long / 24 / 3600);
- $item['no_visit_day'] = $no_visit_day;
- // 预计见面时间
- $subscribe = CustomersSubscribe::where([['customer_id', '=', $item['id']], ['employee_id', '=', $item['employee_id']], ['state', '=', 0]])->order('subscribe_date desc')->find();
- if (!empty($subscribe)) {
- $item['subscribe_date'] = $subscribe['subscribe_date'];
- } else {
- $item['subscribe_date'] = '';
- }
- $assignIdList = !empty($item['assigned_personnel']) ? explode(',', $item['assigned_personnel']) : [];
- if (!empty($item['designer_id']) && !empty($assignIdList)) $assignIdList = array_diff($assignIdList, [$item['designer_id']]);
- $assign_list = Employee::where('id', 'in', $assignIdList)->column('name');
- $item['assign_list'] = !empty($assign_list) ? implode(',', $assign_list) : '';
- }
- return $data;
- }
- //公用查询列表
- public function selCustomer($condition, $page, $limit, $order, $root_id)
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $is_manager = $request->empcrm->is_manager;
- $path = Org::where([['pid', '=', $request->empcrm->org_id]])->count();
- if ($page && $limit) {
- $data = Customer::with(['employee', 'org', 'designer', 'source', 'visitLog' => function ($query) {
- $query->field('id,customer_id,state,addtime,confirm_date,money')->order('addtime asc');
- }])->withCount(['visitLog' => function ($query) {
- $query->where([[CustomerVisitLog::changeState(['state', '=', '未到访'])]]);
- }])->where($condition)->page($page, $limit)->order($order)->select();
- } else {
- $data = Customer::with(['employee', 'org', 'designer', 'source', 'visitLog' => function ($query) {
- $query->field('id,customer_id,state,addtime,confirm_date,money')->order('addtime asc');
- }])->withCount(['visitLog' => function ($query) {
- $query->where([[CustomerVisitLog::changeState(['state', '=', '未到访'])]]);
- }])->where($condition)->order($order)->select();
- }
- $data = $data->visible(['designer.org_id', 'designer_id', 'id', 'employee_id', 'name', 'introduce', 'community_name', 'phone', 'level', 'state', 'square', 'revisit_time', 'addtime', 'last_contact_date', 'org_id', 'protected_to', 'is_resource', 'employee.name', 'org_name', 'designer.name', 'visit_log_count', 'activity_frequency_count', 'source_id', 'source.source', 'phone1', 'phone2', 'remark', 'crm_res_id', 'agents_id', 'sign_time', 'house_delivery_time', 'first_visit_date', 'ext', 'transfer_info', 'deposit_money', 'signed_money', 'package_id', 'fresh', 'house_type', 'remark', 'employee_time', 'add_wechat_time', 'assigned_personnel', 'sign_time', 'estimated_sign_money'])->toArray();
- $setting = Setting::where([['name', '=', 'pubpool'], ['root_id', '=', $root_id]])->find();
- if (!empty($setting)) {
- $setting_content = json_decode($setting['content'], true);
- } else {
- $setting_content = [];
- }
- $empids = !empty($data) ? array_column($data, 'employee_id') : [];
- $emporg = Employee::with(['org' => function ($query) {
- $query->field('id,name');
- }])->where([['root_id', '=', $root_id], ['id', 'in', $empids]])->field('id,org_id')->select()->toArray();
- $state1 = CustomerVisitLog::changeState('已到店', 'chaos');
- $state2 = CustomerVisitLog::changeState('已交定', 'chaos');
- $state3 = CustomerVisitLog::changeState('已签单', 'chaos');
- $state4 = CustomerVisitLog::changeState('已量房', 'chaos');
- $state5 = CustomerVisitLog::changeState('无效', 'chaos');
- $state15 = CustomerVisitLog::changeState('无忧', 'chaos');
- foreach ($data as &$item) {
- $item['emporg'] = $item['source_name'] = $item['designer_name'] = $item['employee_name'] = '';
- if (!empty($item['source'])) {
- $item['source_name'] = $item['source']['source'];
- }
- if (!empty($item['designer'])) {
- $item['designer_name'] = $item['designer']['name'];
- $item['designer_org'] = Org::where([['id', '=', $item['designer']['org_id']]])->value('name');
- }
- if (!empty($item['employee'])) {
- $item['employee_name'] = $item['employee']['name'];
- foreach ($emporg as $p) {
- if ($p['id'] == $item['employee_id']) $item['emporg'] = $p['org']['name'];
- }
- }
- $item['un_protected'] = false;
- if (!empty($item['protected_to']) && time() > strtotime($item['protected_to'])) {
- $state_n = Customer::changeState($item['state'], 'n');
- if (isset($setting_content[$state_n]['state']) && $setting_content[$state_n]['state'] == 1) {
- $item['un_protected'] = true;
- }
- }
- $item['last_invalid_time'] = $item['first_liangfang'] = $item['jiaoding_time'] = $item['sign'] = $item['fisttime'] = '';
- if (!empty($item['visitLog'])) {
- //取最新的一条为客户的跟进时间
- $item['last_contact_date'] = $item['visitLog'][count($item['visitLog']) - 1]['addtime'];
- $state = [];
- foreach ($item['visitLog'] as $k => $v) {
- $state[$v['state']][] = $v;
- }
- foreach ($state as $k => $v) {
- if (in_array($k, $state1)) $item['fisttime'] = date('Y-m-d', strtotime($v[0]['confirm_date']));
- if (in_array($k, $state4)) $item['first_liangfang'] = date('Y-m-d', strtotime($v[0]['addtime']));
- if (in_array($k, $state2)) {
- $item['jiaoding_time'] = $v[0]['addtime'];
- $item['jiaoding_money'] = $v[0]['money'];
- }
- if (in_array($k, $state3)) {
- $item['qiandan_time'] = $v[0]['addtime'];
- $item['qiandan_money'] = $v[0]['money'];
- $item['sign'] = $v[0];
- }
- if (in_array($k, $state5)) $item['last_invalid_time'] = $v[0]['addtime'];
- if (in_array($k, $state15)) $item['wuyou_money'] = $v[0]['money'];
- }
- }
- $item['square'] = floatval($item['square']);
- $item['phone'] = substr_replace($item['phone'], '******', 3, 6);
- $item['phone1'] = $item['phone1'] ? substr_replace($item['phone1'], '******', 3, 6) : '';
- $item['phone2'] = $item['phone2'] ? substr_replace($item['phone2'], '******', 3, 6) : '';
- $houses = $item['house_type'];
- if (!empty($item['house_type']) && $item['house_type'] == '期房') $houses = !empty($item['house_delivery_time']) ? (str_replace('/', '-', $item['house_delivery_time']) <= date('Y-m-d', time()) ? '现房' : $item['house_delivery_time']) : '期房';
- $item['jiaofang'] = $houses;
- $item['time_status'] = !empty($item['last_contact_date']) ? $this->uc_time_ago(date('Y-m-d', strtotime($item['last_contact_date']))) : '';
- $item['nowstate'] = $item['state'];
- $item['package_name'] = !empty($item['package_id']) ? CustomerPackage::where('id', $item['package_id'])->value('name') : '';
- $item['is_followup'] = 0;
- $item['lognum'] = count($item['visitLog']);
- if ($is_manager == 1 && empty($path)) {
- $item['is_followup'] = 1;
- }
- if ($empid == $item['employee_id']) {
- $item['is_followup'] = 1;
- }
- $now_status = $item['state'];
- if ($now_status == '已签单' && $request->empcrm['xinjushang'] == 0) $now_status = '已转单';
- if ($now_status == '已交定' && $request->empcrm['xinjushang'] == 0) $now_status = '已签单';
- $item['now_status'] = $now_status;
- $item['state'] = '';
- //$allstate='';
- if (!empty($item['visitLog'])) {
- $stain = [$now_status];
- foreach ($item['visitLog'] as $k => $v) {
- if (!in_array($v['state'], ['已交定', '已签单', '已到店', '已量房', '已到场', '未到访', '回访'])) continue;
- if ($v['state'] == '回访') $v['state'] = '待确认';
- if ($v['state'] == '已签单' && $request->empcrm['xinjushang'] == 0) $v['state'] = '已转单';
- if ($v['state'] == '已交定' && $request->empcrm['xinjushang'] == 0) $v['state'] = '已签单';
- if (!in_array($v['state'], $stain)) {
- $stain[] = $v['state'];
- }
- }
- if (count($stain) > 1 && in_array('待确认', $stain)) $stain = array_diff($stain, ['待确认']);
- $item['state'] = implode(',', $stain);
- if ($now_status == '待确认') $item['state'] = '待确认';
- }
- // 未跟进时间
- $latest_visit_log = $item['last_contact_date'];
- if (empty($latest_visit_log)) {
- $no_visit_long = time() - strtotime($item['addtime']);
- } else {
- $no_visit_long = time() - strtotime($latest_visit_log);
- }
- $no_visit_day = ceil($no_visit_long / 24 / 3600);
- $item['no_visit_day'] = $no_visit_day;
- // 预计见面时间
- $subscribe = CustomersSubscribe::where([['customer_id', '=', $item['id']], ['employee_id', '=', $item['employee_id']], ['state', '=', 0]])->order('subscribe_date desc')->find();
- if (!empty($subscribe)) {
- $item['subscribe_date'] = $subscribe['subscribe_date'];
- } else {
- $item['subscribe_date'] = '';
- }
- $assignIdList = !empty($item['assigned_personnel']) ? explode(',', $item['assigned_personnel']) : [];
- if (!empty($item['designer_id']) && !empty($assignIdList)) $assignIdList = array_diff($assignIdList, [$item['designer_id']]);
- $assign_list = Employee::where('id', 'in', $assignIdList)->column('name');
- $item['assign_list'] = !empty($assign_list) ? implode(',', $assign_list) : '';
- }
- return $data;
- }
- /**
- * 客户表加回收站查询
- */
- public function cus_recycle($condition, $page, $limit, $order, $root_id)
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $is_manager = $request->empcrm->is_manager;
- $path = Org::where([['pid', '=', $request->empcrm->org_id]])->count();
- $sql = "( SELECT id,name,community_name,square,phone,addtime,employee_id,designer_id,source_id,package_id,assigned_personnel,house_delivery_time,state,ext,last_contact_date FROM `fl_customer` WHERE `id` in (" . $condition . ")) UNION
- ( SELECT customer_id as id,name,community_name,phone,square,addtime,employee_id,designer_id,source_id,package_id,assigned_personnel,house_delivery_time,state,ext,last_contact_date FROM `fl_customer_recycle` WHERE `customer_id` in (" . $condition . "))
- LIMIT " . $page . "," . $limit;
- $data = Db::query($sql);
- $sqlcont = "( SELECT id FROM `fl_customer` WHERE `id` in (" . $condition . ")) UNION
- ( SELECT customer_id as id FROM `fl_customer_recycle` WHERE `customer_id` in (" . $condition . "))";
- $count = count(Db::query($sqlcont));
- $state1 = CustomerVisitLog::changeState('已到店', 'chaos');
- $state2 = CustomerVisitLog::changeState('已交定', 'chaos');
- $state3 = CustomerVisitLog::changeState('已签单', 'chaos');
- $state4 = CustomerVisitLog::changeState('已量房', 'chaos');
- $state5 = CustomerVisitLog::changeState('无效', 'chaos');
- foreach ($data as &$item) {
- $item['visitLog'] = $item['source_name'] = $item['designer_name'] = $item['employee_name'] = '';
- $item['source'] = !empty($item['source_id']) ? CustomerSource::where([['root_id', '=', $root_id], ['id', '=', $item['source_id']]])->field('id,source')->find() : '';
- $item['designer'] = !empty($item['designer_id']) ? Employee::where([['root_id', '=', $root_id], ['id', '=', $item['designer_id']]])->field('id,name')->find() : '';
- $item['employee'] = !empty($item['employee_id']) ? Employee::where([['root_id', '=', $root_id], ['id', '=', $item['employee_id']]])->field('id,name')->find() : '';
- $item['last_invalid_time'] = $item['first_liangfang'] = $item['jiaoding_time'] = $item['sign'] = $item['fisttime'] = '';
- $visitLog = CustomerVisitLog::where([['customer_id', '=', $item['id']], ['org_id', 'in', orgSubIds($root_id)]])->field('id,customer_id,state,addtime,confirm_date,money')->select()->toArray();
- $item['visitLog'] = !empty($visitLog) ? $visitLog : '';
- if (!empty($item['visitLog'])) {
- //取最新的一条为客户的跟进时间
- $item['last_contact_date'] = $item['visitLog'][count($item['visitLog']) - 1]['addtime'];
- $state = [];
- foreach ($item['visitLog'] as $k => $v) {
- $state[$v['state']][] = $v;
- }
- foreach ($state as $k => $v) {
- if (in_array($k, $state1)) $item['fisttime'] = date('Y-m-d', strtotime($v[0]['confirm_date']));
- if (in_array($k, $state4)) $item['first_liangfang'] = date('Y-m-d', strtotime($v[0]['addtime']));
- if (in_array($k, $state2)) {
- $item['jiaoding_time'] = $v[0]['addtime'];
- $item['jiaoding_money'] = $v[0]['money'];
- }
- if (in_array($k, $state3)) {
- $item['qiandan_time'] = $v[0]['addtime'];
- $item['qiandan_money'] = $v[0]['money'];
- $item['sign'] = $v[0];
- }
- if (in_array($k, $state5)) $item['last_invalid_time'] = $v[0]['addtime'];
- }
- }
- $item['square'] = floatval($item['square']);
- //$item['visitLog'] = CustomerVisitLog::where([['customer_employee_id','=',$item['employee_id']],['customer_id','=',$item['id']],['state','in',array_merge(CustomerVisitLog::changeState('待确认','chaos'),CustomerVisitLog::changeState('已到店','chaos'),CustomerVisitLog::changeState('已到场','chaos'),CustomerVisitLog::changeState('已交定','chaos'),CustomerVisitLog::changeState('已签单','chaos'))]])->group('state,customer_id')->field('state,id,customer_id')->order('addtime asc')->select()->toArray();
- $item['phone'] = substr_replace($this->getPhone($item['phone']), '******', 3, 6);
- $item['time_status'] = $item['last_contact_date'] ? $this->uc_time_ago($item['last_contact_date']) : '';
- $item['add_wechat'] = '否';
- $item['sign_time'] = date('Y-m-d H:i', strtotime($item['addtime']));
- $item['allstate'] = null;
- $item['package_name'] = !empty($item['package_id']) ? CustomerPackage::where('id', $item['package_id'])->value('name') : '';
- if (!empty($item['ext'])) {
- $extdata = json_decode($item['ext'], true);
- foreach ($extdata as $key => $val) {
- if (isset($val['keyname']) && $val['keyname'] == 'wechat' && !empty($val['value'])) {
- $add_wechat = CustomerPortraitFieldSelect::where('id', $val['value'])->value('name');
- if ($add_wechat == '有') {
- $add_wechat = '是';
- }
- $item['add_wechat'] = $add_wechat;
- }
- if (isset($val['keyname']) && $val['keyname'] == 'sign_time' && !empty($val['value'])) {
- $item['sign_time'] = str_replace('/', '-', $val['value']);
- }
- }
- }
- $item['is_followup'] = 0;
- if ($is_manager == 1 && empty($path)) {
- $item['is_followup'] = 1;
- }
- if ($empid == $item['employee_id']) {
- $item['is_followup'] = 1;
- }
- $now_status = $this->getState($item['state']);
- if ($now_status == '已签单' && $request->empcrm['xinjushang'] == 0) $now_status = '已转单';
- if ($now_status == '已交定' && $request->empcrm['xinjushang'] == 0) $now_status = '已签单';
- $item['now_status'] = $now_status;
- $item['state'] = '';
- $allstate = '';
- if (!empty($item['visitLog'])) {
- $stain = [$now_status];
- foreach ($item['visitLog'] as $k => $v) {
- if (!in_array($v['state'], ['已交定', '已签单', '已到店', '已量房', '已到场', '未到访', '回访'])) continue;
- if ($v['state'] == '回访') $v['state'] = '待确认';
- if ($v['state'] == '已签单' && $request->empcrm['xinjushang'] == 0) $v['state'] = '已转单';
- if ($v['state'] == '已交定' && $request->empcrm['xinjushang'] == 0) $v['state'] = '已签单';
- if (!in_array($v['state'], $stain)) {
- $stain[] = $v['state'];
- //$allstate.= $v['state'].',';
- }
- }
- if (count($stain) > 1 && in_array('待确认', $stain)) $stain = array_diff($stain, ['待确认']);
- //$item['state'] = trim($allstate,',');
- $item['state'] = implode(',', $stain);
- }
- if ($now_status == '待确认') $item['state'] = '待确认';
- // 未跟进时间
- //$latest_visit_log = CustomerVisitLog::where([['customer_id', '=', $item['id']]])->order('addtime desc')->find();
- $latest_visit_log = $item['last_contact_date'];
- if (empty($latest_visit_log)) {
- $no_visit_long = time() - strtotime($item['addtime']);
- } else {
- $no_visit_long = time() - strtotime($latest_visit_log);
- }
- $no_visit_day = ceil($no_visit_long / 24 / 3600);
- $item['no_visit_day'] = $no_visit_day;
- // 预计见面时间
- $subscribe = CustomersSubscribe::where([['customer_id', '=', $item['id']], ['employee_id', '=', $item['employee_id']], ['state', '=', 0]])->order('subscribe_date desc')->find();
- if (!empty($subscribe)) {
- $item['subscribe_date'] = $subscribe['subscribe_date'];
- } else {
- $item['subscribe_date'] = '';
- }
- $assignIdList = !empty($item['assigned_personnel']) ? explode(',', $item['assigned_personnel']) : [];
- if (!empty($item['designer_id']) && !empty($assignIdList)) $assignIdList = array_diff($assignIdList, [$item['designer_id']]);
- $assign_list = Employee::where('id', 'in', $assignIdList)->column('name');
- $item['assign_list'] = !empty($assign_list) ? implode(',', $assign_list) : '';
- }
- return ['data' => $data, 'count' => $count];
- }
- /**
- * 手机号解密
- */
- public function getPhone($value)
- {
- if (preg_match('/^1[\d]{10}$/', $value)) return $value;
- $aec = new Aec(config('app.aec_key'), config('app.aec_iv'));
- $value = $aec->decrypt($value);
- return $value;
- }
- public function getState($value)
- {
- $states = [
- ['n' => 0, 'chaos' => [0, '待确认', '0'], 'state' => '待确认'],
- ['n' => 1, 'chaos' => [1, '未到访', '1'], 'state' => '未到访'],
- ['n' => 2, 'chaos' => [2, '已到访', '确认到店', '确定到店', '已到店', '2'], 'state' => '已到店'],
- ['n' => 3, 'chaos' => [3, '确定到场', '确认到场', '已到场', '3'], 'state' => '已到场'],
- ['n' => 4, 'chaos' => [4, '已量房', '确定量房', '确认量房', '4'], 'state' => '已量房'],
- ['n' => 5, 'chaos' => [5, '定金', '交定', '已交定', '5'], 'state' => '已交定'],
- ['n' => 6, 'chaos' => [6, '签单', '已签单', '6'], 'state' => '已签单'],
- ['n' => 7, 'chaos' => [7, '无效', '7'], 'state' => '无效'],
- ['n' => 8, 'chaos' => [8, '已卖卡', '8'], 'state' => '已卖卡'],
- ];
- if (is_numeric($value)) $value = intval($value);
- foreach ($states as $s) {
- if ($value === $s['n'] || in_array($value, $s['chaos'], true)) return $s['state'];
- }
- return '';
- }
- //添加微信
- public function add_wetch()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $param = $request->only(['id']);
- $orgids = orgSubIds($root_id);
- $field = CustomerPortraitField::with(['select'])->where([['keyname', '=', 'wechat'], ['root_id', '=', $root_id]])->find()->toArray();
- foreach ($field['select'] as $key => $val) {
- if ($val['name'] == '是' || $val['name'] == '有') {
- $isadd = strval($val['id']);
- }
- }
- $extadd = ['id' => $field['id'], 'keyname' => $field['keyname'], 'value' => $isadd];
- $data = Customer::where([['id', '=', $param['id']], ['org_id', 'in', $orgids]])->find();
- if (empty($data)) return json(['code' => 1, 'msg' => '客户不存在']);
- if (!empty($data['ext']) && $data['ext'] != 'null') {
- $extdata = json_decode($data['ext'], true);
- $fd = array_column($extdata, 'keyname');
- if (in_array('wechat', $fd)) {
- foreach ($extdata as $key => $val) {
- if ($val['keyname'] == 'wechat') {
- $extdata[$key]['value'] = $isadd;
- }
- }
- } else {
- array_push($extdata, $extadd);
- }
- } else {
- $extdata = $extadd;
- }
- $data->ext = json_encode($extdata);
- $data->save();
- return json(['code' => 0, 'data' => '', 'msg' => '添加成功']);
- }
- //获取设计师列表
- private function get_designer()
- {
- //设计师获取
- $w[] = ['path', 'like', request()->empcrm->root_id . '-%'];
- $w[] = ['org_type', '=', 2];
- $orgs = Org::where($w)->column('id');
- $list = Employee::where([['org_id', 'in', $orgs], ['state', '=', '在职'], ['show', '=', 0]])->field('id,name,initials s')->order('s asc')->select()->toArray();
- return $list;
- }
- public function uc_time_ago($ptime)
- {
- $ptime = strtotime($ptime);
- $etime = strtotime(date('Y-m-d')) - $ptime;
- switch ($etime) {
- case $etime > 60 * 60 && $etime <= 24 * 60 * 60:
- $msg = date('Ymd', $ptime) == date('Ymd', time()) ? '今天 ' : '昨天 ';
- break;
- case $etime > 24 * 60 * 60 && $etime <= 2 * 24 * 60 * 60:
- $msg = date('Ymd', $ptime) + 1 == date('Ymd', time()) ? '昨天 ' : '前天 ';
- break;
- case $etime > 2 * 24 * 60 * 60:
- $msg = floor($etime / 86400) . '天+';
- break;
- default:
- $msg = date('Y-m-d H:i', $ptime);
- }
- return $msg;
- }
- /**
- * 客户报备
- */
- public function add_customer()
- {
- if (!request()->isAjax()) {
- return View::fetch();
- }
- $root_id = request()->empcrm->root_id;
- $employee_id = request()->empcrm->id;
- $is_manager = request()->empcrm->is_manager;
- $org_id = request()->empcrm->org_id;
- $orgids = orgSubIds($root_id);
- //编辑客户id
- $id = $this->request->param('id');
- if (empty($id)) {
- // 设计师能否报备、获取客户
- $de_where[] = ['root_id', '=', $root_id];
- $de_where[] = ['name', '=', 'designer_get_customer'];
- $info_de_where = Setting::where($de_where)->findOrEmpty();
- if (!$info_de_where->isEmpty() && request()->empcrm->org_type == 2) {
- if ((int)$info_de_where['content'] != 1) {
- return json(['code' => 1, 'msg' => '您的身份为设计师,无法报备客户']);
- }
- }
- }
- //管理员进行编辑客户信息
- if ($is_manager == 1 && $id) {
- $employee_id = Customer::where([['id', '=', $id], ['org_id', 'in', $orgids]])->value('employee_id');
- }
- $data = $this->request->only(['name', 'phone', 'sex', 'level', 'state' => '待确认', 'last_contact_date' => date('Y-m-d'), 'phone1', 'phone2', 'agents_id']);
- //扩展字段
- $ext = $this->request->param('ext');
- //去掉添加的备用手机号字段
- foreach ($ext as $key => $val) {
- if (in_array($val['keyname'], ['phone1', 'phone2'])) {
- unset($ext[$key]);
- }
- }
- $ext = array_values($ext);
- $orgids = orgSubIds($root_id);
- $pharr = [
- $data['phone'],
- !empty($data['phone1']) ? $data['phone1'] : '',
- !empty($data['phone2']) ? $data['phone2'] : ''
- ];
- $repeat = [];
- foreach ($pharr as $key => $val) {
- if (empty($val)) continue;
- if (!in_array($val, $repeat)) {
- $repeat[] = $val;
- } else {
- return json(['code' => 1, 'msg' => '手机号不能重复']);
- }
- }
- //判断手机号是否在公海,资源库,已建档,回收站
- // 增加同部门能否重复录入判断
- if (!$id) {
- $tips = $this->checkPepeat($pharr, $orgids, $employee_id);
- if (!empty($tips)) return $tips;
- } else {
- $tips = $this->checkPepeat($pharr, $orgids, $employee_id, $id);
- if (!empty($tips)) return $tips;
- }
- //验证接收的自定义字段是否开启必填
- $get_field = [
- isset($data['name']) ? 'name' : '',
- isset($data['phone']) ? 'phone' : '',
- isset($data['sex']) ? 'sex' : '',
- isset($data['level']) ? 'level' : '',
- ];
- $get_field = array_filter($get_field);
- $get_field = array_merge($get_field, array_column($ext, 'keyname'));
- $all_field = CustomerPortraitField::where([['root_id', '=', $root_id], ['status', '=', 0], ['keyname', 'in', $get_field]])->orderRaw('if(isnull(sort),1,0),sort asc')->select()->toArray();
- $validate_data = $data;
- $house_type_value = '';
- $live_broadcast_value = [];
- foreach ($ext as $t) {
- $validate_data[$t['keyname']] = !empty($t['value']) ? $t['value'] : '';
- if ($t['keyname'] == 'house_type' && !empty($t['value'])) {
- $house_type_value = CustomerPortraitFieldSelect::where([['pid', '=', $t['id']], ['id', '=', $t['value']]])->value('name');
- }
- if ($t['keyname'] == 'live_broadcast' && !empty($t['value'])) {
- $live_broadcast_value = CustomerPortraitFieldSelect::where([['pid', '=', $t['id']], ['id', 'in', explode(',', $t['value'])]])->column('name');
- }
- }
- foreach ($all_field as $item) {
- if ($house_type_value != '期房' && $item['keyname'] == 'house_delivery_time' && $item['is_must'] == 0 && empty($validate_data[$item['keyname']])) continue;
- if (!in_array('1对1业务直播', $live_broadcast_value) && $item['keyname'] == 'live_broadcast_business' && $item['is_must'] == 0 && empty($validate_data[$item['keyname']])) continue;
- if (!in_array('1对1设计直播', $live_broadcast_value) && $item['keyname'] == 'live_broadcast_design' && $item['is_must'] == 0 && empty($validate_data[$item['keyname']])) continue;
- if (!in_array('一对多直播', $live_broadcast_value) && $item['keyname'] == 'live_broadcast_personnel' && $item['is_must'] == 0 && empty($validate_data[$item['keyname']])) continue;
- if ($item['is_must'] == 0 && empty($validate_data[$item['keyname']])) {
- return json(['code' => 1, 'msg' => $item['name'] . '不能为空']);
- }
- }
- //扩展字段保存
- $add_customer_extfield = $this->add_customer_extfield($ext);
- $data = array_merge($data, $add_customer_extfield);
- $data['sex'] = isset($data['sex']) && !empty($data['sex']) ? $data['sex'] : 1; //数据库性别必须有默认值
- $customer = new Customer();
- if ($id) {
- unset($data['state']);
- unset($data['last_contact_date']);
- $customer = Customer::where(['id' => $id, 'employee_id|designer_id' => $employee_id])->find();
- $old_customer = $customer;
- if (empty($customer)) return json(['code' => 1, 'msg' => '数据不存在']);
- //增加检测修改的扩展字段信息
- if (!empty($customer['ext']) && $customer['ext'] != 'null') {
- $extdata = json_decode($customer['ext'], true);
- if (isset($extdata['ext1'])) {
- $new_ext = CustomerPortraitField::where([['root_id', '=', $root_id], ['pid', '<>', 0]])->select()->toArray();
- $this->check_old_updatefield($customer, $ext, $new_ext);
- } else {
- $this->check_new_updatefield($customer, $ext);
- }
- }
- } else {
- $data['employee_id'] = $employee_id;
- $data['org_id'] = $org_id;
- }
- Db::startTrans();
- try {
- $customer->save($data);
- // 更新客户保护期
- if (!$id) {
- $customer->save(['employee_time' => date('Y-m-d H:i:s')]); //保存员工获取客户时间
- Customer::changeProtectedTo($customer->id, $root_id);
- $this->check_not_updatefield($customer, $ext);
- }
- //建档后添加一条跟进记录
- if (empty($id)) $this->addFirstCustoemrVisitLog($customer->id);
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- return json(['code' => 1, 'msg' => $e->getMessage()]);
- }
- // 添加微爆积分
- if ($id) {
- // 老客户新加微
- $new_customer = Customer::find($id);
- if (!empty($new_customer['add_wechat_time']) && empty($old_customer['add_wechat_time'])) {
- $i_data['add_wechat_time'] = $new_customer['add_wechat_time'];
- $i_data['customer_id'] = $id;
- $i_data['employee_id'] = $new_customer['employee_id'];
- $i_data['type'] = 1;
- $i_data['org_id'] = $new_customer['org_id'];
- WechatActivityIntegral::addIntegral($i_data, $root_id);
- }
- } else {
- $new_customer = Customer::find($customer->id);
- if (!empty($new_customer['add_wechat_time'])) {
- $i_data['add_wechat_time'] = $new_customer['add_wechat_time'];
- $i_data['customer_id'] = $customer->id;
- $i_data['employee_id'] = $new_customer['employee_id'];
- $i_data['type'] = 1;
- $i_data['org_id'] = $new_customer['org_id'];
- WechatActivityIntegral::addIntegral($i_data, $root_id);
- }
- }
- $customer['name'] = !empty($customer['name']) ? $customer['name'] : '';
- $customer['phone'] = substr_replace($customer['phone'], '******', 3, 6);
- // 发送管理层模板消息
- $msg = ['first' => '行为通知', 'keyword1' => '客户信息编辑', 'keyword2' => "员工" . request()->empcrm->name . "对客户" . $customer['name'] . "(" . $customer['phone'] . ")信息编辑", 'keyword3' => date('Y/m/d'), 'remark' => '请悉知'];
- TmpMsg::sendMsgToLeader($msg, request()->empcrm->org_id);
- return json(['code' => 0, 'data' => $customer->id, 'msg' => '添加成功']);
- }
- //新添加字段时的处理
- public function check_not_updatefield($cusdata, $save_ext)
- {
- $yes_save = [];
- foreach ($save_ext as $key => $val) {
- if (!empty($val['value'])) {
- $yes_save[] = $val;
- }
- }
- if (!empty($yes_save)) $this->save_field_log($yes_save, $cusdata);
- }
- //查询修改值的字段
- public function check_new_updatefield($cusdata, $save_ext)
- {
- $old_data = json_decode($cusdata['ext'], true);
- $yes_save = [];
- foreach ($old_data as $k => $v) {
- foreach ($save_ext as $kk => $vv) {
- if ($v['id'] == $vv['id']) {
- //判断是否修改了数据
- if ($v['value'] != $vv['value']) {
- $yes_save[] = $vv;
- }
- unset($save_ext[$kk]);
- }
- }
- }
- //去除为空值的新字段
- foreach ($save_ext as $key => $val) {
- if (empty($val['value'])) {
- unset($save_ext[$key]);
- }
- }
- $save_ext = array_values($save_ext);
- $yes_data = $yes_save;
- if (!empty($save_ext)) {
- $yes_data = array_merge($yes_save, $save_ext);
- }
- if (!empty($yes_data)) $this->save_field_log($yes_data, $cusdata);
- }
- public function get_old_ext()
- {
- $extAttr = [
- 'ext1' => '年龄',
- 'ext2' => '是否首次装修',
- 'ext3' => '消费水平',
- 'ext4' => '预计装修时间',
- 'ext5' => '客户爱好',
- 'ext6' => '是否添加微信',
- 'ext7' => '客户空闲时间段',
- 'ext8' => '家庭结构',
- 'ext9' => '家庭成员意见',
- 'ext10' => '请选择房屋户型',
- 'ext11' => '决策人',
- 'ext12' => '客户喜欢的色调',
- 'ext13' => '房屋结构',
- 'ext14' => '装修方式',
- 'ext15' => '客户其它需求',
- 'ext16' => '客户痛点',
- 'ext17' => '重视环保',
- 'ext18' => '重视设计',
- 'ext19' => '关注的工艺',
- 'ext20' => '关注房屋的空间',
- 'ext21' => '汽车价格',
- 'ext22' => '房屋价格',
- 'ext23' => '买小区的原因',
- 'ext24' => '周边配置',
- 'ext25' => '设计师服务',
- 'ext26' => '方案满意度',
- 'ext27' => '报价满意度',
- 'ext28' => '装修意向',
- 'ext29' => '是否到店',
- 'ext30' => '预计到店时间',
- 'ext31' => '是否交定',
- 'ext32' => '客户其他需求',
- 'ext33' => '当前所在区域',
- 'ext34' => '加微类型',
- 'ext35' => '建群情况',
- 'ext36' => '直播情况',
- 'ext37' => '直播人员',
- 'ext38' => '房屋用途',
- 'ext39' => '房屋位置',
- 'ext40' => '其他房屋信息',
- 'ext41' => '谈单时长',
- 'ext42' => '门牌单元号',
- 'ext43' => '业务直播人员',
- 'ext44' => '设计直播人员'
- ];
- return $extAttr;
- }
- //老数据查询修改值的字段
- public function check_old_updatefield($cusdata, $save_ext, $new_ext)
- {
- $old_ext = $this->get_old_ext();
- $ext = json_decode($cusdata['ext'], true);
- // 数据的旧ext处理
- $xin = [];
- foreach ($ext as $key => $val) {
- foreach ($old_ext as $k => $v) {
- if ($k == $key) {
- $a['name'] = $v;
- $a['value'] = $val;
- }
- }
- $xin[] = $a;
- }
- //旧ext处理成新的ext结构
- $old_data = [];
- foreach ($xin as $k => $v) {
- foreach ($new_ext as $kk => $vv) {
- if ($v['name'] == $vv['name'] && !empty($v['value'])) {
- $old_data['id'] = $vv['id'];
- $old_data['keyname'] = $vv['keyname'];
- $old_data['value'] = $v['value'];
- }
- }
- }
- //$old_data = json_decode($cusdata['ext'],true);
- $yes_save = [];
- foreach ($old_data as $k => $v) {
- foreach ($save_ext as $kk => $vv) {
- if ($v['id'] == $vv['id']) {
- //判断是否修改了数据
- if ($v['value'] != $vv['value']) {
- $yes_save[] = $vv;
- }
- unset($save_ext[$kk]);
- }
- }
- }
- //去除为空值的新字段
- foreach ($save_ext as $key => $val) {
- if (empty($val['value'])) {
- unset($save_ext[$key]);
- }
- }
- $save_ext = array_values($save_ext);
- $yes_data = $yes_save;
- if (!empty($save_ext)) {
- $yes_data = array_merge($yes_save, $save_ext);
- }
- if (!empty($yes_data)) $this->save_field_log($yes_data, $cusdata);
- }
- //处理修改字段的记录
- public function save_field_log($yes_data, $cusdata)
- {
- $token = $this->get_token();
- $save_ids = array_column($yes_data, 'id');
- $is_down = 0;
- $new_ext = CustomerPortraitField::with(['select'])->where([['root_id', '=', $token['root_org']], ['pid', '<>', 0], ['id', 'in', $save_ids]])->select()->toArray();
- $city = Company::where('root_id', $token['root_org'])->value('city');
- foreach ($new_ext as $key => $val) {
- if ($val['keyname'] == 'current_region') {
- $new_ext[$key]['select'][] = ['id' => $token['root_org'], 'name' => $city, 'pid' => $val['id']];
- }
- if ($val['keyname'] == 'source_id') {
- $new_ext[$key]['select'] = CustomerSource::field('id,source as name')->where('root_id', $token['root_org'])->select()->toArray();
- }
- if ($val['keyname'] == 'deco_style') {
- $new_ext[$key]['select'] = Decostyle::field('id,name')->where([['root_id', '=', $token['root_org']], ['type', '=', 0]])->select()->toArray();
- }
- }
- foreach ($yes_data as $key => $val) {
- $selname1 = null;
- $yes_data[$key]['valname'] = $val['value'];
- foreach ($new_ext as $k => $v) {
- if ($v['id'] == $val['id']) {
- if ($v['type'] == 6) $is_down = 1;
- $yes_data[$key]['name'] = $v['name'];
- $yes_data[$key]['type'] = $v['type'];
- if (in_array($v['type'], [3, 4]) && !empty($val['value'])) {
- $sp = explode(',', $val['value']);
- foreach ($v['select'] as $w => $q) {
- if (in_array($q['id'], $sp)) {
- $selname1 .= $q['name'] . ',';
- }
- }
- $yes_data[$key]['valname'] = trim($selname1, ',');
- }
- }
- }
- }
- // 添加追踪记录
- $visitLog = [
- 'customer_id' => $cusdata->id,
- 'type' => '',
- 'next_contact_date' => null,
- 'employee_id' => $token['employee_id'],
- 'user_id' => $token['uid'],
- 'remark' => '修改客户扩展信息',
- 'state' => 1,
- 'org_id' => $token['org_id'],
- 'customer_employee_id' => $cusdata->employee_id,
- 'customer_org_id' => Employee::where('id', $cusdata->employee_id)->value('org_id'),
- 'save_portrait_field' => json_encode($yes_data),
- 'down_portrait_field_status' => 0
- ];
- // var_dump($visitLog);
- // exit;
- CustomerVisitLog::create($visitLog);
- }
- /**
- * 客户报备扩展字段
- */
- private function add_customer_extfield($ext)
- {
- $root_id = request()->empcrm->root_id;
- $seldata = CustomerPortraitField::with(['select'])->where([['root_id', '=', $root_id], ['status', '=', 0], ['type', 'in', [3, 4]]])->select()->toArray();
- $allselid = [];
- foreach ($seldata as $key => $val) {
- foreach ($val['select'] as $k => $v) {
- $allselid[] = ['id' => $v['id'], 'name' => $v['name']];
- }
- }
- $customer_data = [];
- //Log::record($ext)->save();
- foreach ($ext as $key => $val) {
- $selval = '';
- if ($val['keyname'] == 'community_name') {
- $customer_data['community_name'] = $val['value'];
- }
- if ($val['keyname'] == 'square') {
- $customer_data['square'] = $val['value'];
- }
- if ($val['keyname'] == 'house_type') {
- foreach ($allselid as $k => $v) {
- if ($val['value'] == $v['id']) {
- $selval = $v['name'];
- }
- }
- $customer_data['house_type'] = $selval;
- }
- if ($val['keyname'] == 'house_status') {
- foreach ($allselid as $k => $v) {
- if ($val['value'] == $v['id']) {
- $selval = $v['name'];
- }
- }
- $customer_data['house_status'] = $selval;
- }
- if ($val['keyname'] == 'level') {
- foreach ($allselid as $k => $v) {
- if ($val['value'] == $v['id']) {
- $selval = $v['name'];
- }
- }
- $customer_data['level'] = $selval;
- }
- if ($val['keyname'] == 'house_delivery_time') {
- $customer_data['house_delivery_time'] = $val['value'];
- }
- if ($val['keyname'] == 'plan_deco_time') {
- $customer_data['plan_deco_time'] = $val['value'];
- }
- if ($val['keyname'] == 'budget') {
- $customer_data['budget'] = $val['value'];
- }
- if ($val['keyname'] == 'deco_style') {
- $customer_data['deco_style'] = $val['value'];
- }
- if ($val['keyname'] == 'follow') {
- if (!empty($val['value'])) {
- $gza = explode(',', $val['value']);
- foreach ($allselid as $k => $v) {
- if (in_array($v['id'], $gza)) {
- $selval .= $v['name'] . ',';
- }
- }
- }
- $customer_data['product'] = trim($selval, ',');
- }
- if ($val['keyname'] == 'source_id') {
- $customer_data['source_id'] = $val['value'];
- }
- }
- $customer_data['ext'] = json_encode($ext);
- return $customer_data;
- }
- /**
- * 新建客户时添加一条跟进记录
- * $id 客户id
- */
- public function addFirstCustoemrVisitLog($id)
- {
- $token = request()->empcrm;
- $name = Employee::where('id', $token['id'])->value('name');
- $cname = Customer::where('id', $id)->value('name');
- // $type = $source ? '建档' : '报备';
- $type = '报备';
- $remark = '业务员' . $name . '对客户' . $cname . '进行' . $type;
- $save = [
- 'customer_id' => $id,
- 'type' => '',
- 'next_contact_date' => date('Y-m-d'),
- 'remark' => $remark,
- 'addtime' => date('Y-m-d H:i:s'),
- 'employee_id' => $token['id'],
- 'user_id' => $token['uid'],
- 'state' => 1,
- 'org_id' => $token['org_id'],
- 'customer_employee_id' => $token['id'],
- 'customer_org_id' => $token['org_id']
- ];
- CustomerVisitLog::insertGetId($save);
- return true;
- }
- /**
- * 添加客户检测手机号的重复
- * 1.客户录入不和资源库和回收站排重 (创艺)
- * 2.客户录入需查询是否和公海重复,如重复,提示可以获取该客户 (创艺) 工单id6077
- */
- public function checkPepeat($pharr, $org_id, $employee_id, $id = null)
- {
- // 同部门能否重复录入开关设置
- $root_id = request()->empcrm['root_id'];
- $empcrm_repeat[] = ['root_id', '=', $root_id];
- $empcrm_repeat[] = ['name', '=', 'empcrm_customer_repeat'];
- $repeat_setting = Setting::where($empcrm_repeat)->findOrEmpty();
- $emp_org = request()->empcrm['org_id'];
- foreach ($pharr as $val) {
- if (empty($val)) continue;
- // 先判断公海 只有添加时候判断
- if (empty($id)) {
- $poolExist = Customer::where([
- ['phone|phone1|phone2', '=', cypherphone(trim($val))],
- ['org_id', '=', $emp_org],
- ['employee_id', 'null', null],
- ['is_resource', '=', 0]
- ])->column('id');
- if (!empty($poolExist)) {
- $poolCrmIdStr = implode(',', $poolExist);
- return json(['code' => 3, 'data' => $poolCrmIdStr, 'msg' => '录入失败:客户信息已在公海中。']);
- }
- }
- // 新建档,检测手机号是否重复
- $customerExist = Customer::where([
- ['phone|phone1|phone2', '=', cypherphone(trim($val))],
- ['org_id', 'in', $org_id],
- ['employee_id', '>', 0] // 员工客户
- ])->field('id,employee_id,is_resource,org_id')->select()->toArray();
- $id_arr = array_column($customerExist, 'id');
- $employee_id_arr = array_column($customerExist, 'employee_id');
- if (empty($id)) {
- // 对非当前业务员名下重复客户不在判断
- if (in_array($employee_id, $employee_id_arr)) {
- // 是否已存在为自己的客户
- return json(['code' => 2, 'msg' => '录入失败。' . $val . '已经是您的客户。']);
- }
- } else {
- if (!in_array($id, $id_arr) && in_array($employee_id, $employee_id_arr)) {
- // 是否已存在为自己的客户
- return json(['code' => 2, 'msg' => '录入失败。' . $val . '已经是您的客户。']);
- }
- }
- //判断同部门
- if (!$repeat_setting->isEmpty()) {
- $repeat_org = explode(',', $repeat_setting['content']);
- if (in_array($emp_org, $repeat_org)) {
- // 需要验证
- if (!empty($customerExist)) {
- foreach ($customerExist as $ex) {
- if (in_array($ex['org_id'], $repeat_org) && $ex['employee_id'] != $employee_id) {
- // 为了避免开启撞单之前都已经重复的情况,所以要查一下这个id的现数据
- if ($id) {
- $this_customer = Customer::find($id);
- if ($this_customer) {
- $this_customer_phone = array_filter([$this_customer['phone'], $this_customer['phone1'], $this_customer['phone2']]);
- if (!in_array($val, $this_customer_phone)) { // 不是原来的客户手机号,编辑时候判断撞单
- $have_emp = Employee::where('id', '=', $ex['employee_id'])->value('name');
- return json(['code' => 2, 'msg' => '该客户与员工' . $have_emp . '撞单,无法录入']);
- }
- }
- } else {
- $have_emp = Employee::where('id', '=', $ex['employee_id'])->value('name');
- return json(['code' => 2, 'msg' => '该客户与员工' . $have_emp . '撞单,无法录入']);
- }
- }
- }
- }
- }
- }
- }
- }
- //编辑客户
- public function edit_customer()
- {
- $request = request();
- $param = $request->only(['id', 'empid', 'level', 'source_id', 'designer_id', 'name', 'phone', 'sex', 'source_position', 'subjection_plan' => '', 'community_name', 'square', 'house_delivery_time', 'housetype_arrow' => '', 'budget', 'unit_number' => '', 'package_id', 'deco_style', 'sign_time', 'employee_id', 'phone1' => '']);
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $org_employee = [$empid];
- if (!$request->isAjax()) {
- $soudata = CustomerSource::field('id,source as name')->where('root_id', $root_id)->select()->toArray();
- View::assign('soudata', $soudata);
- $list = $this->get_designer();
- if (!empty($list)) {
- // $list = hanziInitsort($list, 'name');
- $sort = array_column($list, 's');
- sort($sort);
- array_multisort($sort, SORT_ASC, $list);
- $sort = array_unique($sort);
- $list = ['sort' => $sort, 'arr' => $list];
- } else {
- $list = ['sort' => [], 'arr' => []];
- }
- View::assign('designers', $list);
- //客户等级 隶属计划
- $fields = CustomerPortraitField::with(['select'])->where([['root_id', '=', $root_id], ['keyname', 'in', ['level', 'subjection_plan', 'housetype_arrow']]])->select()->toArray();
- View::assign('fields', $fields);
- // var_dump($fields);
- // exit;
- //部门所有员工
- $employee_list = Employee::where([['root_id', '=', $root_id], ['org_id', '=', $request->empcrm->org_id], ['state', 'in', ['在职']]])->field('id,name')->select()->toArray();
- View::assign('employee_list', $employee_list);
- //意向风格
- $decostyle_list = Decostyle::field('id,name,root_id as pid')->where(['root_id' => $root_id])->where([['type', '=', 0]])->order('id desc')->select()->toArray();
- View::assign('decostyle_list', $decostyle_list);
- //意向产品
- $product = CustomerPackage::field('id, name, total_price')->where(['root_id' => $root_id])->select();
- View::assign('product', $product);
- View::assign('empid', $empid);
- //客户详情
- $customer = $this->seLemp($param['id'], $empid);
- // var_dump($customer->toArray());
- // exit;
- View::assign('customer', $customer);
- return View::fetch();
- }
- if (!empty($param['phone']) && !empty($param['phone1']) && $param['phone'] == $param['phone1']) {
- return json(['code' => 1, 'msg' => '手机号不能重复']);
- }
- $orgids = orgSubIds($root_id);
- $pharr = [
- $param['phone'],
- !empty($param['phone1']) ? $param['phone1'] : ''
- ];
- $tips = $this->checkPepeat($pharr, $orgids, $empid, $param['id']);
- if (!empty($tips)) return $tips;
- $data['employee_id'] = !empty($param['empid']) ? $param['empid'] : $empid;
- $data['org_id'] = request()->empcrm->org_id;
- $data['name'] = $param['name'];
- $data['phone'] = cypherphone(trim($param['phone']));
- $data['phone1'] = !empty($param['phone1']) ? cypherphone(trim($param['phone1'])) : '';
- //$data['sex'] = $param['sex'];
- $data['source_id'] = $param['source_id'];
- //$data['source_position'] = $param['source_position'];
- $data['level'] = $param['level'];
- $data['community_name'] = $param['community_name'];
- $data['square'] = $param['square'];
- $data['house_delivery_time'] = $param['house_delivery_time'] . '-01';
- $data['budget'] = !empty($param['budget']) ? $param['budget'] : '';
- $data['package_id'] = $param['package_id'];
- $data['deco_style'] = $param['deco_style'];
- //$data['sign_time'] = $param['sign_time'];
- $data['designer_id'] = $param['designer_id'];
- //处理客户的扩展字段
- $et = [];
- $kz = CustomerPortraitField::where([['root_id', '=', $root_id], ['keyname', 'in', ['subjection_plan', 'housetype_arrow', 'unit_number']]])->select();
- foreach ($kz as $key => $val) {
- $et[] = ['id' => $val['id'], 'keyname' => $val['keyname'], 'value' => $param[$val['keyname']]];
- }
- $data['ext'] = !empty($et) ? json_encode($et) : NULL;
- $ms = Customer::where('id', $param['id'])->update($data);
- // 更新客户保护期
- //Customer::changeProtectedTo($ms, $this->request->token['root_org']);
- //if($ms) $this->addFirstCustoemrVisitLog($ms);
- $data['name'] = !empty($data['name']) ? $data['name'] : '';
- $data['phone'] = substr_replace($data['phone'], '******', 3, 6);
- // 发送管理层模板消息
- $msg = ['first' => '行为通知', 'keyword1' => '客户信息编辑', 'keyword2' => "员工" . request()->empcrm->name . "对客户" . $data['name'] . "(" . $data['phone'] . ")信息编辑", 'keyword3' => date('Y/m/d'), 'remark' => '请悉知'];
- TmpMsg::sendMsgToLeader($msg, request()->empcrm->org_id);
- return json(['code' => 0, 'data' => $ms, 'msg' => '修改成功']);
- }
- //已签单客户修改客户信息
- public function sign_savecust()
- {
- $request = request();
- $param = $request->only(['id', 'designer_id', 'name', 'community_name', 'square', 'housetype_arrow' => '', 'package_id', 'deco_style']);
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $org_employee = [$empid];
- $orgids = orgSubIds($root_id);
- $data['name'] = $param['name'];
- $data['community_name'] = $param['community_name'];
- $data['square'] = $param['square'];
- $data['package_id'] = $param['package_id'];
- $data['deco_style'] = $param['deco_style'];
- //$data['designer_id'] = $param['designer_id'];
- //处理客户的扩展字段
- $et = [];
- $kz = CustomerPortraitField::where([['root_id', '=', $root_id], ['keyname', 'in', ['housetype_arrow']]])->select();
- foreach ($kz as $key => $val) {
- $et[] = ['id' => $val['id'], 'keyname' => $val['keyname'], 'value' => $param[$val['keyname']]];
- }
- $data['ext'] = !empty($et) ? json_encode($et) : NULL;
- $ms = Customer::where([['id', '=', $param['id']], ['org_id', 'in', $orgids]])->update($data);
- return json(['code' => 0, 'data' => $ms, 'msg' => '修改成功']);
- }
- //公海列表
- public function pool_list()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $is_manager = $request->empcrm->is_manager;
- $orgids = orgSubIds($root_id);
- if (!$request->isAjax()) {
- $this->screen();
- $week = date('w');
- View::assign('week', $week);
- View::assign('is_manager', $is_manager);
- return View::fetch();
- }
- $param = $request->only(['page' => 1, 'limit' => 20, 'order', 'org', 'keyname', 'state', 'date' => '', 'level', 'source_id', 'designer_id', 'addtime', 'name', 'phone', 'community_name', 'employee_name', 'fistDate']);
- $condition = [];
- $order = isset($param['order']) ? $param['order'] : 'id desc';
- $condition[] = ['employee_id', '=', NULL];
- $condition[] = ['is_resource', '=', 0];
- $condition[] = ['state', 'in', Customer::changeState('无效', 'chaos')];
- $condition[] = ['org_id', 'in', $orgids];
- if (isset($param['org']) && $param['org']) {
- $condition[] = ['org_id', '=', $param['org']];
- } else {
- $orgids = orgSubIds($request->empcrm->org_id);
- $condition[] = ['org_id', 'in', $orgids];
- }
- // if (isset($param['keyname']) && $param['keyname']) {
- // $condition[] = ['name|community_name', 'like', '%' . trim($param['keyname']) . '%'];
- // }
- if (isset($param['level']) && $param['level']) {
- $condition[] = ['level', 'in', explode(',', $param['level'])];
- }
- if (isset($param['source_id']) && $param['source_id']) {
- $condition[] = ['source_id', 'in', explode(',', $param['source_id'])];
- }
- if (isset($param['designer_id']) && $param['designer_id']) {
- $condition[] = ['designer_id', 'in', explode(',', $param['designer_id'])];
- }
- // 姓名筛选
- if (isset($param['name']) && $param['name']) {
- $condition[] = ['name', 'like', '%' . trim($param['name']) . '%'];
- }
- // 小区筛选
- if (isset($param['community_name']) && $param['community_name']) {
- $condition[] = ['community_name', 'like', '%' . trim($param['community_name']) . '%'];
- }
- // 手机号筛选
- if (!empty($param['phone'])) {
- $list = Customer::where($condition)->column('id', 'phone');
- $keyCustomersId = [];
- $aec = new Aec(config('app.aec_key'), config('app.aec_iv'));
- foreach ($list as $k => $v) {
- $v_phone = $aec->decrypt($k);
- if (strpos($v_phone, $param['phone']) !== false) $keyCustomersId[] = $v;
- }
- unset($condition);
- $condition[] = ['id', 'in', $keyCustomersId];
- }
- // 交房时间筛选
- if (!empty($param['house_delivery_time'])) {
- $house_delivery_time = explode(' - ', $param['house_delivery_time']);
- $start = $house_delivery_time[0];
- $end = $house_delivery_time[1] . ' 23:59:59';
- $condition[] = ['house_delivery_time', '>=', $start];
- $condition[] = ['house_delivery_time', '<=', $end];
- }
- // 面积筛选
- if (!empty($param['square'])) {
- $square = explode(',', $param['square']);
- $square_arr = [
- 1 => '(square > 1 and square <= 80)',
- 2 => '(square > 81 and square <= 100)',
- 3 => '(square > 101 and square <= 120)',
- 4 => '(square > 121 and square <= 200)',
- 5 => '(square > 201 and square <= 500)',
- 6 => '(square > 501 and square <= 10000)'
- ];
- $squ_whereOr = null;
- foreach ($square as $k => $v) {
- if (!empty($square_arr[$v])) $squ_whereOr .= $square_arr[$v] . ' OR';
- }
- $customersList = Customer::where($condition)->where(function ($query) use ($squ_whereOr) {
- $query->whereRaw(trim($squ_whereOr, ' OR'));
- })->order('addtime desc')->column('id');
- unset($condition);
- $condition[] = ['id', 'in', $customersList];
- }
- // 首次到店时间筛选
- if (!empty($param['fistDate'])) {
- $fistDate = explode(' - ', $param['fistDate']);
- $start = str_replace('/', '-', $fistDate[0]);
- $end = date('Y-m-d', strtotime($fistDate[1]) + 86400);
- $customersIdList = Customer::where($condition)->order('addtime desc')->column('id');
- $dd_where[] = ['customer_id', 'in', $customersIdList];
- $dd_where[] = ['state', 'in', CustomerVisitLog::changeState('已到店', 'chaos')];
- $dd_where[] = ['confirm_date', '>=', $start];
- $dd_where[] = ['confirm_date', '<=', $end];
- $ddlog = CustomerVisitLog::where($dd_where)->group('customer_id')->order('addtime asc')->column('customer_id');
- unset($condition);
- $condition[] = ['id', 'in', $ddlog];
- }
- // 最后一次置为公海的业务员筛选
- if (!empty($param['employee_name'])) {
- $customersIdList = Customer::where($condition)->column('id');
- $pool_employee_id = CustomerVisitLog::where([['employee_id', 'in', explode(',', $param['employee_name'])], ['customer_id', 'in', $customersIdList], ['state', 'in', CustomerVisitLog::changeState('无效', 'chaos')]])->group('customer_id')->column('customer_id');
- unset($condition);
- $condition[] = ['id', 'in', $pool_employee_id];
- }
- // 报备时间
- if (!empty($param['addtime'])) {
- //筛选区间范围
- $date_time = explode(' - ', $param['addtime']);
- $start = str_replace('/', '-', $date_time[0]);
- $end = str_replace('/', '-', $date_time[1]);
- $condition[] = [['addtime', 'between', [$start . ' 00:00:00', $end . ' 23:59:59']]];
- }
- // 查询id列表
- $customerIdList = Customer::where($condition)->column('id');
- if (isset($param['state']) && $param['state']) {
- // 筛查状态
- $customerIdList = CustomerVisitLog::where([['state', 'in', CustomerVisitLog::changeState($param['state'])], ['customer_id', 'in', $customerIdList]])->group('customer_id')->column('customer_id');
- }
- //回收时间
- if ($param['date']) {
- //筛选区间范围
- $date_time = explode(' - ', $param['date']);
- $start = str_replace('/', '-', $date_time[0]);
- $end = str_replace('/', '-', $date_time[1]);
- //$date = explode(' - ', $param['date']);
- //$date = $param['date'];
- $customerIdList = CustomerVisitLog::where([['addtime', 'between', [$start . ' 00:00:00', $end . ' 23:59:59']], ['customer_id', 'in', $customerIdList], CustomerVisitLog::changeState(['state', '=', '无效'])])->group('customer_id')->column('customer_id');
- }
- //$pageIdList = array_slice($customerIdList, ($param['page'] - 1) * $param['limit'], $param['limit']);
- $pageIdList = $customerIdList;
- $condition[] = ['id', 'in', $pageIdList];
- $customersStateList = CustomerVisitLog::where([['customer_id', 'in', $pageIdList]])->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray();
- $customersState = [];
- foreach ($customersStateList as $s) {
- if (!isset($customersState[$s['customer_id']]))
- $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
- $customersState[$s['customer_id']]['count'] += $s['num'];
- // 到店,量房,活动,定金,签单
- if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num'];
- elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num'];
- elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num'];
- elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num'];
- elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num'];
- }
- // 上方id已分页,此处分页参数传固定1
- $data = $this->poollist($condition, 1, $param['limit'], $order);
- $customer_id = array_column($data, 'id');
- $visitLogNum = CustomerVisitLog::where([['customer_id', 'in', $customer_id], CustomerVisitLog::changeState(['state', '=', '无效'])])->group('customer_id')->column('count(customer_id) as num', 'customer_id');
- $visitLogDate = CustomerVisitLog::where([['customer_id', 'in', $customer_id], CustomerVisitLog::changeState(['state', '=', '无效'])])->order('addtime')->column('addtime', 'customer_id');
- $visitLogMeet = CustomerVisitLog::where([['customer_id', 'in', $customer_id], CustomerVisitLog::changeState(['state', '=', '确认到店'])])->order('addtime')->column('addtime', 'customer_id');
- foreach ($data as &$value) {
- //回收次数
- $value['recoverNum'] = isset($visitLogNum[$value['id']]) ? $visitLogNum[$value['id']] : 0;
- //最近的回收时间
- $value['recoverLatelyDate'] = isset($visitLogDate[$value['id']]) ? $visitLogDate[$value['id']] : null;
- //最近的确认到店时间
- $value['fistDate'] = isset($visitLogMeet[$value['id']]) ? $visitLogMeet[$value['id']] : null;
- //上次跟进人信息
- $value['before_pool'] = !empty($value['before_pool']) ? json_decode($value['before_pool'], true) : null;
- $value[$value['id']]['stateNum'] = $customersState[$value['id']] ?? ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
- // 客户最后一次置为公海的关联员工
- $last_pool_employee = CustomerVisitLog::where([['customer_id', '=', $value['id']], ['state', 'in', CustomerVisitLog::changeState('无效', 'chaos')]])->with('employee')->order('addtime desc')->find();
- $value['employee'] = $last_pool_employee ? $last_pool_employee->employee->name : '';
- }
- $sort = array_column($data, 'recoverLatelyDate');
- array_multisort($sort, SORT_DESC, $data);
- foreach ($condition as $k => $v) {
- if ($v[0] == 'id') {
- if (isset($v[2])) {
- $condition[$k][2] = $customerIdList;
- }
- }
- }
- $data = array_slice($data, ($param['page'] - 1) * $param['limit'], $param['limit']);
- $count = CustomerLogic::count($condition);
- return json(['code' => 0, 'data' => $data, 'count' => $count]);
- }
- //公海列表
- public function pool_list_export()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $is_manager = $request->empcrm->is_manager;
- $orgids = orgSubIds($root_id);
- $param = $request->only(['page' => 1, 'limit' => 20, 'order', 'org', 'keyname', 'state', 'date' => '', 'level', 'source_id', 'designer_id', 'addtime', 'name', 'phone', 'community_name', 'employee_name', 'fistDate']);
- $condition = [];
- $order = isset($param['order']) ? $param['order'] : 'id desc';
- $condition[] = ['employee_id', '=', NULL];
- $condition[] = ['is_resource', '=', 0];
- $condition[] = ['state', 'in', Customer::changeState('无效', 'chaos')];
- $condition[] = ['org_id', 'in', $orgids];
- if (isset($param['org']) && $param['org']) {
- $condition[] = ['org_id', '=', $param['org']];
- } else {
- $orgids = orgSubIds($request->empcrm->org_id);
- $condition[] = ['org_id', 'in', $orgids];
- }
- // if (isset($param['keyname']) && $param['keyname']) {
- // $condition[] = ['name|community_name', 'like', '%' . trim($param['keyname']) . '%'];
- // }
- if (isset($param['level']) && $param['level']) {
- $condition[] = ['level', 'in', explode(',', $param['level'])];
- }
- if (isset($param['source_id']) && $param['source_id']) {
- $condition[] = ['source_id', 'in', explode(',', $param['source_id'])];
- }
- if (isset($param['designer_id']) && $param['designer_id']) {
- $condition[] = ['designer_id', 'in', explode(',', $param['designer_id'])];
- }
- // 姓名筛选
- if (isset($param['name']) && $param['name']) {
- $condition[] = ['name', 'like', '%' . trim($param['name']) . '%'];
- }
- // 小区筛选
- if (isset($param['community_name']) && $param['community_name']) {
- $condition[] = ['community_name', 'like', '%' . trim($param['community_name']) . '%'];
- }
- // 手机号筛选
- if (!empty($param['phone'])) {
- $list = Customer::where($condition)->column('id', 'phone');
- $keyCustomersId = [];
- $aec = new Aec(config('app.aec_key'), config('app.aec_iv'));
- foreach ($list as $k => $v) {
- $v_phone = $aec->decrypt($k);
- if (strpos($v_phone, $param['phone']) !== false) $keyCustomersId[] = $v;
- }
- unset($condition);
- $condition[] = ['id', 'in', $keyCustomersId];
- }
- // 交房时间筛选
- if (!empty($param['house_delivery_time'])) {
- $house_delivery_time = explode(' - ', $param['house_delivery_time']);
- $start = $house_delivery_time[0];
- $end = $house_delivery_time[1] . ' 23:59:59';
- $condition[] = ['house_delivery_time', '>=', $start];
- $condition[] = ['house_delivery_time', '<=', $end];
- }
- // 面积筛选
- if (!empty($param['square'])) {
- $square = explode(',', $param['square']);
- $square_arr = [
- 1 => '(square > 1 and square <= 80)',
- 2 => '(square > 81 and square <= 100)',
- 3 => '(square > 101 and square <= 120)',
- 4 => '(square > 121 and square <= 200)',
- 5 => '(square > 201 and square <= 500)',
- 6 => '(square > 501 and square <= 10000)'
- ];
- $squ_whereOr = null;
- foreach ($square as $k => $v) {
- if (!empty($square_arr[$v])) $squ_whereOr .= $square_arr[$v] . ' OR';
- }
- $customersList = Customer::where($condition)->where(function ($query) use ($squ_whereOr) {
- $query->whereRaw(trim($squ_whereOr, ' OR'));
- })->order('addtime desc')->column('id');
- unset($condition);
- $condition[] = ['id', 'in', $customersList];
- }
- // 首次到店时间筛选
- if (!empty($param['fistDate'])) {
- $fistDate = explode(' - ', $param['fistDate']);
- $start = str_replace('/', '-', $fistDate[0]);
- $end = date('Y-m-d', strtotime($fistDate[1]) + 86400);
- $customersIdList = Customer::where($condition)->order('addtime desc')->column('id');
- $dd_where[] = ['customer_id', 'in', $customersIdList];
- $dd_where[] = ['state', 'in', CustomerVisitLog::changeState('已到店', 'chaos')];
- $dd_where[] = ['confirm_date', '>=', $start];
- $dd_where[] = ['confirm_date', '<=', $end];
- $ddlog = CustomerVisitLog::where($dd_where)->group('customer_id')->order('addtime asc')->column('customer_id');
- unset($condition);
- $condition[] = ['id', 'in', $ddlog];
- }
- // 最后一次置为公海的业务员筛选
- if (!empty($param['employee_name'])) {
- $customersIdList = Customer::where($condition)->column('id');
- $pool_employee_id = CustomerVisitLog::where([['employee_id', 'in', explode(',', $param['employee_name'])], ['customer_id', 'in', $customersIdList], ['state', 'in', CustomerVisitLog::changeState('无效', 'chaos')]])->group('customer_id')->column('customer_id');
- unset($condition);
- $condition[] = ['id', 'in', $pool_employee_id];
- }
- // 报备时间
- if (!empty($param['addtime'])) {
- //筛选区间范围
- $date_time = explode(' - ', $param['addtime']);
- $start = str_replace('/', '-', $date_time[0]);
- $end = str_replace('/', '-', $date_time[1]);
- $condition[] = [['addtime', 'between', [$start . ' 00:00:00', $end . ' 23:59:59']]];
- }
- // 查询id列表
- $customerIdList = Customer::where($condition)->column('id');
- if (isset($param['state']) && $param['state']) {
- // 筛查状态
- $customerIdList = CustomerVisitLog::where([['state', 'in', CustomerVisitLog::changeState($param['state'])], ['customer_id', 'in', $customerIdList]])->group('customer_id')->column('customer_id');
- }
- //回收时间
- if ($param['date']) {
- //筛选区间范围
- $date_time = explode(' - ', $param['date']);
- $start = str_replace('/', '-', $date_time[0]);
- $end = str_replace('/', '-', $date_time[1]);
- //$date = explode(' - ', $param['date']);
- //$date = $param['date'];
- $customerIdList = CustomerVisitLog::where([['addtime', 'between', [$start . ' 00:00:00', $end . ' 23:59:59']], ['customer_id', 'in', $customerIdList], CustomerVisitLog::changeState(['state', '=', '无效'])])->group('customer_id')->column('customer_id');
- }
- //$pageIdList = array_slice($customerIdList, ($param['page'] - 1) * $param['limit'], $param['limit']);
- $pageIdList = $customerIdList;
- $condition[] = ['id', 'in', $pageIdList];
- $customersStateList = CustomerVisitLog::where([['customer_id', 'in', $pageIdList]])->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray();
- $customersState = [];
- foreach ($customersStateList as $s) {
- if (!isset($customersState[$s['customer_id']]))
- $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
- $customersState[$s['customer_id']]['count'] += $s['num'];
- // 到店,量房,活动,定金,签单
- if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num'];
- elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num'];
- elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num'];
- elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num'];
- elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num'];
- }
- // 上方id已分页,此处分页参数传固定1
- $data = $this->poollista($condition, 1, $param['limit'], $order);
- $customer_id = array_column($data, 'id');
- $visitLogNum = CustomerVisitLog::where([['customer_id', 'in', $customer_id], CustomerVisitLog::changeState(['state', '=', '无效'])])->group('customer_id')->column('count(customer_id) as num', 'customer_id');
- $visitLogDate = CustomerVisitLog::where([['customer_id', 'in', $customer_id], CustomerVisitLog::changeState(['state', '=', '无效'])])->order('addtime')->column('addtime', 'customer_id');
- $visitLogMeet = CustomerVisitLog::where([['customer_id', 'in', $customer_id], CustomerVisitLog::changeState(['state', '=', '确认到店'])])->order('addtime')->column('addtime', 'customer_id');
- foreach ($data as &$value) {
- //回收次数
- $value['recoverNum'] = isset($visitLogNum[$value['id']]) ? $visitLogNum[$value['id']] : 0;
- //最近的回收时间
- $value['recoverLatelyDate'] = isset($visitLogDate[$value['id']]) ? $visitLogDate[$value['id']] : null;
- //最近的确认到店时间
- $value['fistDate'] = isset($visitLogMeet[$value['id']]) ? $visitLogMeet[$value['id']] : null;
- //上次跟进人信息
- $value['before_pool'] = !empty($value['before_pool']) ? json_decode($value['before_pool'], true) : null;
- $value[$value['id']]['stateNum'] = $customersState[$value['id']] ?? ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
- // 客户最后一次置为公海的关联员工
- $last_pool_employee = CustomerVisitLog::where([['customer_id', '=', $value['id']], ['state', 'in', CustomerVisitLog::changeState('无效', 'chaos')]])->with('employee')->order('addtime desc')->find();
- $value['employee'] = $last_pool_employee ? $last_pool_employee->employee->name : '';
- }
- $sort = array_column($data, 'recoverLatelyDate');
- array_multisort($sort, SORT_DESC, $data);
- foreach ($condition as $k => $v) {
- if ($v[0] == 'id') {
- if (isset($v[2])) {
- $condition[$k][2] = $customerIdList;
- }
- }
- }
- //$data = array_slice($data, ($param['page'] - 1) * $param['limit'], $param['limit']);
- //$count = CustomerLogic::count($condition);
- $new = [];
- foreach ($data as $key => $val) {
- $new[] = ['name' => $val['name'], 'phone' => $val['phone'], 'community_name' => $val['community_name'], 'employee' => $val['employee'], 'level' => $val['level'], 'source' => isset($val['source']['source']) ? $val['source']['source'] : '', 'square' => $val['square'], 'designer_id' => $val['designer_id'], 'house_delivery_time' => $val['house_delivery_time'], 'recoverLatelyDate' => $val['recoverLatelyDate'], 'addtime' => $val['addtime'], 'state' => $val['state'], 'fistDate' => $val['fistDate']];
- }
- $header = ["A1" => "客户姓名", "B1" => "手机号", "C1" => "小区名称", "D1" => "所属员工", "E1" => "客户等级", "F1" => '来源渠道', "G1" => '面积', "H1" => '设计师', "I1" => '交房时间', "J1" => '注销时间', "K1" => '报备时间', "L1" => '状态', "M1" => '首次到店'];
- $fileName = date('Y-m-d H:i:s') . '公共库导出';
- $this->getExport($header, true, $new, $fileName);
- //return json(['code' => 0, 'data' => $data]);
- }
-
- // 导出
- public function getExport($header = [], $type = false, $data = [], $fileName)
- {
- // 实例化类
- $preadsheet = new Spreadsheet();
- // 创建sheet
- $sheet = $preadsheet->getActiveSheet();
- // 循环设置表头数据
- foreach ($header as $k => $v) {
- $sheet->setCellValue($k, $v);
- }
- // 生成数据
- $sheet->fromArray($data, null, "A2");
- // 样式设置
- $sheet->getDefaultColumnDimension()->setWidth(12);
- // 设置下载与后缀
- if ($type) {
- header("Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
- $type = "Xlsx";
- $suffix = "xlsx";
- } else {
- header("Content-Type:application/vnd.ms-excel");
- $type = "Xls";
- $suffix = "xls";
- }
- ob_end_clean(); //清楚缓存区
- // 激活浏览器窗口
- header("Content-Disposition:attachment;filename=$fileName.$suffix");
- //缓存控制
- header("Cache-Control:max-age=0");
- // 调用方法执行下载
- $writer = IOFactory::createWriter($preadsheet, $type);
- // 数据流
- $writer->save("php://output");
- }
- //查询公海
- public function poollista($condition = [], $page, $limit, $order)
- {
- $page = intval($page);
- $limit = intval($limit);
- $data = Customer::with(['source' => function ($query) {
- $query->field('id,source');
- }])->field('source_id,id, name, community_name, phone, level, designer_id,state, square, addtime, org_id, employee_id, is_resource, before_pool,first_visit_date,house_delivery_time')
- ->where($condition)
- ->select()->toArray();
- foreach ($data as &$item) {
- $item['pool_name'] = Org::where('id', $item['org_id'])->column('name');
- }
- return $data;
- }
- //查询公海
- public function poollist($condition = [], $page, $limit, $order)
- {
- $page = intval($page);
- $limit = intval($limit);
- $data = Customer::with(['source' => function ($query) {
- $query->field('id,source');
- }])->field('source_id,id, name, community_name, phone, level, designer_id,state, square, addtime, org_id, employee_id, is_resource, before_pool,first_visit_date,house_delivery_time')
- ->where($condition)
- ->select()->toArray();
- foreach ($data as &$item) {
- $item['pool_name'] = Org::where('id', $item['org_id'])->column('name');
- if (!$item['employee_id']) {
- $item['phone'] = substr_replace($item['phone'], '******', 3, 6);
- }
- }
- return $data;
- }
- //客户状态详情
- public function customer_detail()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $org_employee = [$empid];
- // if (!$request->isAjax()) {
- // $this->screen();
- // return View::fetch();
- // }
- $this->screen();
- $param = $request->only(['id']);
- $data = $this->seLemp($param['id'], $empid);
- //意向风格
- $decostyle_list = Decostyle::field('id,name,root_id as pid')->where(['root_id' => $root_id])->where([['type', '=', 0]])->order('id desc')->select()->toArray();
- View::assign('decostyle_list', $decostyle_list);
- //意向产品
- $product = CustomerPackage::field('id, name, total_price')->where(['root_id' => $root_id])->select();
- View::assign('product', $product);
- View::assign('data', $data);
- View::assign('empname', $request->empcrm->name);
- // var_dump($data->toArray());
- // exit;
- return View::fetch();
- }
- //公用查询客户详情
- public function seLemp($id, $empid)
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $orgids = orgSubIds($root_id);
- $is_manager = $request->empcrm->is_manager;
- $where = [
- ['id', '=', $id],
- ['state', '<>', '无效']
- ];
- // $condition1 = [
- // ['employee_id', '=', $empid],
- // ['id', '=', $id]
- // ];
- // $condition2 = [
- // ['designer_id', '=', $empid],
- // ['id', '=', $id]
- // ];
- // $condition3[] = ['assigned_personnel', 'find in set', $empid];
- // $condition3[] = ['id', '=', $id];
- // // 或者是公司领导看员工的客户
- // if (isset($request->empcrm->is_manager) && $request->empcrm->is_manager) {
- // $team_orgs = (new Org())->getChildOrg($request->empcrm->org_id);
- // $org_type = Org::where('id',$request->empcrm->org_id)->value('org_type');
- // if ($org_type == 1) {
- // $condition4 = [
- // ['org_id', 'in', $team_orgs],
- // ['id', '=', $id]
- // ];
- // $orCondition = [$condition1, $condition2, $condition3,$condition4];
- // } else {
- // $empIdList = Employee::where([['org_id', 'in', $team_orgs]])->column('id');
- // $condition4 = [
- // ['designer_id', 'in', $empIdList],
- // ['id', '=', $id]
- // ];
- // $condition5 = [
- // ['employee_id', 'in', $empIdList],
- // ['id', '=', $id]
- // ];
- // $condition6 = [
- // ['org_id', 'in', $team_orgs],
- // ['id', '=', $id]
- // ];
- // $orCondition = [$condition1, $condition2, $condition4, $condition5,$condition6];
- // }
- // } else {
- // $orCondition = [$condition1, $condition2,$condition3];
- // }
- // if(!empty($param['org_id']) && in_array($param['org_id'],$orgids)) $orCondition[] = [['org_id', 'in', $orgids],['id', '=', $id]];
- //$orCondition = [$condition1, $condition2];
- //增加查询是否要显示修改客户扩展字段信息功能
- $showEmpVistlog = Setting::where([['root_id', '=', $root_id], ['name', '=', 'showEmpVistlog']])->findOrEmpty();
- $visit = !$showEmpVistlog->isEmpty() ? (int)$showEmpVistlog->content : 0;
- $vlg_where[] = $visit ? ['save_portrait_field', 'NULL', null] : ['id', '>', 0];
- $data = Customer::with(['source', 'designer' => function ($query) {
- $query->field('id,name,headimgurl,org_id,uid');
- }, 'employee' => function ($query) {
- $query->withField(['id', 'name']);
- }, 'visitLog' => function ($query) use ($vlg_where) {
- $query->where($vlg_where)->field('id,customer_id,type,next_contact_date,remark,addtime,employee_id,state,customer_employee_id,img,img1,sign_img,delivery_img,save_portrait_field,data_type,data_id,number_of_visitors,talking_order_time,stay_length,document_field,document_name')->order('addtime desc');
- }])->where($where)->find();
- $empIdList = explode(',', $data->employee_id . ',' . $data->employee_id . ',' . $data->assigned_personnel);
- $empIdList = array_unique(array_filter($empIdList));
- if (!empty($data) && !in_array($empid, $empIdList)) {
- if ($is_manager == 1) {
- $team_orgs = orgSubIds($request->empcrm->org_id);
- $yuangong = Employee::where([['org_id', 'in', $team_orgs], ['uid', '>', 0], ['id', 'in', $empIdList]])->count();
- if ($yuangong == 0) $data = null;
- } else {
- $data = null;
- }
- }
- if (empty($data)) {
- echo json_encode(['code' => 1, 'msg' => '数据不存在']);
- exit;
- }
- $data['daochang'] = CustomerVisitLog::where([['customer_id', '=', $id], ['customer_employee_id', '=', $data['employee_id']], CustomerVisitLog::changeState(['state', '=', '已到访'])])->order('addtime desc')->find();
- $data['deposit'] = CustomerVisitLog::where([['customer_id', '=', $id], ['customer_employee_id', '=', $data['employee_id']], CustomerVisitLog::changeState(['state', '=', '交定'])])->order('addtime desc')->field("id,money,DATE_FORMAT(confirm_date, '%Y-%m-%d') as confirm_date")->find();
- $data['sign'] = CustomerVisitLog::where([['customer_id', '=', $id], ['customer_employee_id', '=', $data['employee_id']], CustomerVisitLog::changeState(['state', '=', '签单'])])->order('addtime desc')->field("id,money,DATE_FORMAT(confirm_date, '%Y-%m-%d') as confirm_date")->find();
- $data['dd'] = CustomerVisitLog::where([['customer_id', '=', $id], ['customer_employee_id', '=', $data['employee_id']], CustomerVisitLog::changeState(['state', '=', '已到店'])])->order('addtime desc')->value("DATE_FORMAT(confirm_date, '%Y-%m-%d')");
- $data['lf'] = CustomerVisitLog::where([['customer_id', '=', $id], ['customer_employee_id', '=', $data['employee_id']], CustomerVisitLog::changeState(['state', '=', '已量房'])])->order('addtime desc')->value("DATE_FORMAT(confirm_date, '%Y-%m-%d')");
- $data['wuyou'] = CustomerVisitLog::where([['customer_id', '=', $id], ['customer_employee_id', '=', $data['employee_id']], CustomerVisitLog::changeState(['state', '=', '无忧'])])->field("id,money,DATE_FORMAT(confirm_date, '%Y-%m-%d') as wuyou_date")->find();
- $data['package_name'] = $data['package_id'] ? CustomerPackage::where('id', $data['package_id'])->value('name') : '';
- $data['deco_style_name'] = $data['deco_style'] ? Decostyle::where('id', $data['deco_style'])->value('name') : '';
- $data['company_name'] = Company::where('root_id', $root_id)->value('company_name');
- $data['add_wechat'] = '否';
- $data['housetype'] = $data['unit_number'] = $data['subjection_plan'] = null;
- $data['sign_time'] = '';
- if (!empty($data['ext'])) {
- $field = $this->get_portrait_field($data);
- // var_dump($field);
- // exit;
- foreach ($field as $key => $val) {
- if ($val['keyname'] == 'wechat' && !empty($val['value'])) {
- $data['add_wechat'] = CustomerPortraitFieldSelect::where('id', $val['value'])->value('name');
- }
- if ($val['keyname'] == 'housetype_arrow' && !empty($val['value'])) {
- $data['housetype'] = CustomerPortraitFieldSelect::where('id', $val['value'])->value('name');
- }
- if ($val['keyname'] == 'subjection_plan' && !empty($val['value'])) {
- $data['subjection_plan'] = CustomerPortraitFieldSelect::where('id', $val['value'])->value('name');
- }
- if ($val['keyname'] == 'unit_number' && !empty($val['value'])) {
- $data['unit_number'] = $val['value'];
- }
- if ($val['keyname'] == 'sign_name' && !empty($val['value'])) {
- $data['sign_time'] = $val['value'];
- }
- }
- }
- $data['daochang'] = !empty($data['daochang']) ? json_decode($data['daochang'], true) : null;
- $data['deposit'] = !empty($data['deposit']) ? json_decode($data['deposit'], true) : null;
- $data['sign'] = !empty($data['sign']) ? json_decode($data['sign'], true) : null;
- $data['assigned_personnel'] = !empty($data['designer_id']) ? $data['assigned_personnel'] . ',' . $data['designer_id'] : $data['assigned_personnel'];
- return $data;
- }
- //新获取客户扩展字段
- public function get_portrait_field($cusdata)
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $w[] = ['root_id', '=', $root_id];
- $list = CustomerPortraitField::with(['select'])->where([['root_id', '=', $root_id], ['pid', '<>', 0]])->select()->toArray();
- $cuslog = new CustomerLogic;
- $new = $list;
- if (!empty($cusdata['ext']) && $cusdata['ext'] != 'null') {
- $extdata = json_decode($cusdata['ext'], true);
- if (isset($extdata['ext1'])) {
- $new = $cuslog->old_data_saves($cusdata, $list);
- } else {
- $new = $cuslog->new_data_saves($cusdata, $extdata, $list);
- }
- }
- return $new;
- //return json(['code' => 0, 'data' => $new]);
- }
- //添加客户的回访记录
- public function addVisitlog()
- {
- //$token = $this->request->token;
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $is_manager = $request->empcrm->is_manager;
- //防止后台切换部门
- $token['org_id'] = Employee::where('id', $empid)->value('org_id');
- $param = request()->only(['customer_id', 'name' => '', 'type' => '', 'next_contact_date', 'remark' => '', 'img' => '', 'state', 'money' => 0, 'aid' => 0, 'addtime' => date('Y-m-d H:i:s'), 'confirm_date' => '', 'weixin_media' => '', 'media_id' => '', 'starts' => 0, 'weixin_media1' => '', 'media_id1' => '', 'img1' => '', 'number_of_visitors' => 0, 'measure_room_img_type' => '', 'package' => '', 'deposit_mode' => 0, 'house_status' => '', 'other_money' => 0, 'house_delivery_time' => '', 'deco_style' => '', 'designer_id' => '', 'vlogid' => '', 'document_field' => '', 'document_name' => '']);
- $param['confirm_date'] = $param['confirm_date'] ? $param['confirm_date'] . ' ' . date('H:i:s') : date('Y-m-d H:i:s');
- $employeeTypeName = $this->request->empcrm['org_type'] == 2 ? '设计师' : '销售';
- $employeeType = $this->request->empcrm['org_type'] == 2 ? 'designer_id' : 'employee_id';
- if ($is_manager == 1) $employeeTypeName = '部门领导';
- //2022-10-04 逻辑修改,无效时无效原因必传remark
- //if ($param['state'] == '无效' && empty($param['remark'])) return json(['code' => 1, 'msg' => '请填写无效原因']);
- // 在跟进之前,增加了设计师必填判断,得先查一下设置
- $state = Customer::changeState($param['state']);
- $stateCheck = ['已到店' => 'daodian', '已到场' => 'daochang', '已量房' => 'liangfang', '已交定' => 'qiandan', '已签单' => 'zhuandan'];
- if (in_array($state, ['已到店', '已到场', '已量房', '已交定', '已签单'])) {
- $need_designer[] = ['root_id', '=', $root_id];
- $need_designer[] = ['name', '=', 'add_visit_log_need_designer'];
- $need_designer_setting = Setting::where($need_designer)->findOrEmpty();
- if (!$need_designer_setting->isEmpty()) {
- $need_state = explode(',', $need_designer_setting['content']);
- $input_state = $stateCheck[$state] ?? '';
- if (!empty($input_state) && in_array($input_state, $need_state)) {
- // 需要验证
- $customer_find = Customer::find($param['customer_id']);
- if (!empty($customer_find)) {
- if (empty($customer_find['assigned_personnel'])) {
- return json(['code' => 1, 'msg' => '请先指派设计师']);
- }
- $assigned_personnel = explode(',', $customer_find['assigned_personnel']);
- $assigned_personnel_employee_org = Employee::where('id', 'in', $assigned_personnel)->column('org_id');
- $assigned_personnel_org_type = Org::where('id', 'in', $assigned_personnel_employee_org)->column('org_type');
- if (!in_array(2, $assigned_personnel_org_type)) {
- return json(['code' => 1, 'msg' => '请先指派设计师']);
- }
- }
- }
- }
- }
- //2022-10-27 逻辑修改,跟进保护优化 如果后台设置了重复客户跟进保护规则 跟进到保护状态后其他业务员的此重复客户就不能在跟进了,但是可以置为无效
- $forbiddenState = $this->forbiddenState($param['customer_id'], $param['state']);
- if ($forbiddenState != false) return json(['code' => 1, 'msg' => $forbiddenState]);
- if ($is_manager == 1) {
- $team_orgs = orgSubIds($request->empcrm->org_id);
- $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
- } else {
- $org_employee = [$empid];
- }
- $where = [
- ['id', '=', $param['customer_id']],
- ['state', '<>', '无效']
- ];
- $had = Customer::where($where)->where(function ($query) use ($org_employee) {
- foreach ($org_employee as $k => $v) {
- $query->whereOr([['employee_id|designer_id|assigned_personnel', 'find in set', $v]]);
- }
- })->find();
- if (!$had) return json(['code' => 1, 'msg' => '添加失败,数据不存在']);
- $isAllManagerOperateAble = Setting::where(['root_id' => $root_id, 'name' => 'is_all_manager_oprate_able'])->value('content');
- $isEndOrg = Org::where([['pid', '=', $request->empcrm->org_id]])->count();
- if ($had['employee_id'] !== $empid) {
- if ($is_manager && in_array($had['org_id'], $team_orgs)) {
- if (!$isAllManagerOperateAble && $isEndOrg) return json(['code' => 1, 'msg' => '添加失败,无权限']);
- }
- }
- //签单之后不能置为无效
- if ($param['state'] == '无效') {
- $is_orders = CustomerVisitLog::where([['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')], ['customer_id', '=', $had->id]])->count();
- if ($is_orders) return json(['code' => 1, 'msg' => '该客户已签单,不能置为无效']);
- }
- //改为有效之后不能再改为待确认
- if ($param['state'] == '待确认') {
- if ($had->state != '待确认') return json(['code' => 1, 'msg' => '该客户已标记为有效,不能修改为待确认']);
- }
- // 撞单机制
- if ($param['state'] == '未到访') {
- // 同部门能否重复录入开关设置
- $empcrm_repeat[] = ['root_id', '=', $root_id];
- $empcrm_repeat[] = ['name', '=', 'empcrm_customer_repeat'];
- $repeat_setting = Setting::where($empcrm_repeat)->findOrEmpty();
- //判断同部门
- if (!$repeat_setting->isEmpty()) {
- $repeat_org = explode(',', $repeat_setting['content']);
- if (in_array($this->request->empcrm['org_id'], $repeat_org)) {
- // 需要验证
- $not_sure = Customer::changeState('待确认', 'chaos');
- $wuxiao = Customer::changeState('无效', 'chaos');
- $phones_arr = array_filter([$had['phone'], $had['phone1'], $had['phone2']]);
- $repeat_where[] = ['phone|phone1|phone2', 'in', $phones_arr];
- $repeat_where[] = ['org_id', 'in', $repeat_org];
- $repeat_where[] = ['state', 'not in', array_merge($not_sure, $wuxiao)];
- $customerExist = Customer::where($repeat_where)->field('id,employee_id');
- if (!empty($customerExist)) {
- foreach ($customerExist as $ex) {
- if ($ex['employee_id'] != $empid && $ex['id'] != $param['customer_id']) {
- $have_emp = Employee::where('id', '=', $ex['employee_id'])->value('name');
- return json(['code' => 2, 'msg' => '该客户与员工' . $have_emp . '撞单,无法标记有效']);
- }
- }
- }
- }
- }
- }
- // 如果是预约添加预约记录
- $stateType = [
- 1 => '到店',
- 2 => '到场',
- 3 => '量房'
- ];
- $yylf_state = CustomerVisitLog::changeState('预约量房', 'chaos');
- $lf_state = CustomerVisitLog::changeState('已量房', 'chaos');
- $yydc_state = CustomerVisitLog::changeState('预约活动', 'chaos');
- $dc_state = CustomerVisitLog::changeState('已到场', 'chaos');
- $yydd_state = CustomerVisitLog::changeState('预约到店', 'chaos');
- $dd_state = CustomerVisitLog::changeState('已到店', 'chaos');
- $check_state = array_merge($yylf_state, $lf_state, $yydc_state, $dc_state, $yydd_state, $dd_state);
- if (in_array($param['state'], $check_state)) {
- // 保护设置
- $sub_setting = Setting::where([['root_id', '=', $root_id], ['name', '=', 'subscribe_protected']])->findOrEmpty();
- if (!$sub_setting->isEmpty()) { // 有设置
- $setting_content = json_decode($sub_setting['content'], true);
- $org_ids = orgSubIds($root_id);
- foreach ($setting_content as $k_s => $v_s) {
- if (!empty($v_s['state']) && !empty($v_s['day']) && $v_s['state'] == 1) {
- $continue = false;
- $sub_state = 0;
- $errmsg = '该客户在保护期内,无法提交';
- $check_log = false;
- $check_where = [];
- switch ($k_s) {
- case 'liangfang':
- $sub_state = 3;
- $visit_state = 1;
- $check_log = true;
- $check_where[] = ['org_id', 'in', $org_ids];
- $check_where[] = ['addtime', '>', date('Y-m-d H:i:s', time() - 24 * 3600 * $v_s['day'])];
- $check_where[] = ['employee_id', '<>', $empid];
- $check_where[] = ['state', 'in', $lf_state];
- break;
- case 'yliangfang': //预约量房
- $sub_state = 3;
- $visit_state = 0;
- break;
- case 'daodian':
- $sub_state = 1;
- $visit_state = 1;
- $check_log = true;
- $check_where[] = ['org_id', 'in', $org_ids];
- $check_where[] = ['addtime', '>', date('Y-m-d H:i:s', time() - 24 * 3600 * $v_s['day'])];
- $check_where[] = ['employee_id', '<>', $empid];
- $check_where[] = ['state', 'in', $dd_state];
- break;
- case 'ydaodian':
- $sub_state = 1;
- $visit_state = 0;
- break;
- case 'daochang':
- $sub_state = 2;
- $visit_state = 1;
- $check_log = true;
- $check_where[] = ['org_id', 'in', $org_ids];
- $check_where[] = ['addtime', '>', date('Y-m-d H:i:s', time() - 24 * 3600 * $v_s['day'])];
- $check_where[] = ['employee_id', '<>', $empid];
- $check_where[] = ['state', 'in', $dc_state];
- break;
- case 'ydaochang':
- $sub_state = 2;
- $visit_state = 0;
- break;
- default:
- $continue = true;
- break;
- }
- if ($continue) continue;
- // “确认” 不受预约的限制
- if (in_array($k_s, ['yliangfang', 'ydaodian', 'ydaochang']) && in_array($param['state'], array_merge($lf_state, $dd_state, $dc_state))) {
- continue;
- }
- if (in_array($param['state'], array_merge($lf_state, $dd_state, $dc_state))) {
- $errmsg = '该客户在保护期内,无法提交确认';
- } elseif (in_array($param['state'], array_merge($yylf_state, $yydd_state, $yydc_state))) {
- $errmsg = '该客户在保护期内,无法预约';
- }
- // 查询保护时间段内添加的预约量房记录
- unset($v_where);
- $v_where[] = ['org_id', 'in', $org_ids];
- $v_where[] = ['addtime', '>', date('Y-m-d H:i:s', time() - 24 * 3600 * $v_s['day'])];
- $v_where[] = ['employee_id', '<>', $empid];
- $v_where[] = ['type', '=', $sub_state];
- $v_where[] = ['state', '=', $visit_state];
- $sub_list = CustomersSubscribe::where($v_where)->select()->toArray();
- if (!empty($sub_list)) {
- $sub_list_ids = array_column($sub_list, 'customer_id');
- $sub_employee_ids = array_column($sub_list, 'employee_id');
- $sub_customers = Customer::where([['id', 'in', $sub_list_ids], ['employee_id', 'in', $sub_employee_ids]])->select()->toArray();
- foreach ($sub_customers as $v) {
- if (in_array($had->phone, [$v['phone'], $v['phone1'], $v['phone2']]) && $v['id'] != $had['id']) {
- return json(['code' => 1, 'msg' => $errmsg]);
- }
- if (!empty($had->phone1) && in_array($had->phone1, [$v['phone'], $v['phone1'], $v['phone2']]) && $v['id'] != $had['id']) {
- return json(['code' => 1, 'msg' => $errmsg]);
- }
- if (!empty($had->phone2) && in_array($had->phone2, [$v['phone'], $v['phone1'], $v['phone2']]) && $v['id'] != $had['id']) {
- return json(['code' => 1, 'msg' => $errmsg]);
- }
- }
- }
- if ($check_log) {
- $log_list = CustomerVisitLog::where($check_where)->select()->toArray();
- $log_list_ids = array_column($log_list, 'customer_id');
- $log_employee_ids = array_column($log_list, 'employee_id');
- $log_customers = Customer::where([['id', 'in', $log_list_ids], ['employee_id', 'in', $log_employee_ids]])->select()->toArray();
- foreach ($log_customers as $v) {
- if (in_array($had->phone, [$v['phone'], $v['phone1'], $v['phone2']]) && $v['id'] != $had['id']) {
- return json(['code' => 1, 'msg' => $errmsg]);
- }
- if (!empty($had->phone1) && in_array($had->phone1, [$v['phone'], $v['phone1'], $v['phone2']]) && $v['id'] != $had['id']) {
- return json(['code' => 1, 'msg' => $errmsg]);
- }
- if (!empty($had->phone2) && in_array($had->phone2, [$v['phone'], $v['phone1'], $v['phone2']]) && $v['id'] != $had['id']) {
- return json(['code' => 1, 'msg' => $errmsg]);
- }
- }
- }
- }
- }
- }
- }
- // 更改客户信息
- $state = Customer::changeState($param['state']);
- $orders = false;
- //签单之后不能签单交定,可以跟进
- if ($state == '已签单') {
- $is = CustomerVisitLog::where([['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')], ['customer_id', '=', $had->id]])->count();
- if ($is) {
- return $this->updateMoney($param);
- // Db::rollback();
- // return json(['code' => 1, 'msg' => '该客户' . $state]);
- }
- $orders = true;
- } elseif ($state == '已交定') {
- $is = CustomerVisitLog::where([['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')], ['customer_id', '=', $had->id]])->count();
- if ($is) {
- Db::rollback();
- return json(['code' => 1, 'msg' => '该客户已签单']);
- }
- $orders = true;
- }
- Db::startTrans();
- //处理房屋状态
- if (!empty($param['house_status'])) {
- $had->house_status = $param['house_status'];
- }
- //如果是分派的客户第一次跟进直接修改分派信息为空
- if (!empty($had->transfer_info)) {
- $had->transfer_info = NULL;
- }
- //如果是第一次有效修改有效时间
- if ($state == '未到访' && $had->state == '待确认') {
- $had->valid_time = !empty($param['confirm_date']) ? $param['confirm_date'] : date('Y-m-d H:i:s');
- }
- if (!empty($state)) {
- //if(!in_array($had->state,Customer::changeState('待确认','chaos'),true)){
- $had->state = $state;
- //}
- }
- //客户名称
- if (!empty($param['name'])) {
- $had->name = $param['name'];
- }
- //是否设置客户转介绍新客户
- // if($param['introduce']){
- // $had->introduce = 1;
- // }
- //设计师
- if (!empty($param['designer_id']) && empty($had->designer_id)) {
- $limitNum = Setting::where(['root_id' => $root_id, 'name' => 'designerNum'])->value('content');
- if ($limitNum != 0) {
- //获取当月时间跨度
- $start_time = date('Y-m') . '-01 00:00:00';
- $end_time = date('Y-m-d', strtotime("$start_time +1 month -1 day")) . ' 23:59:59';
- $whered[] = ['addtime', 'between', [strtotime($start_time), strtotime($end_time)]];
- $whered[] = ['employee_id', '=', $param['designer_id']];
- $hads = CustomerSharing::field('employee_id,count(customer_id) as num')->group('employee_id')->where($whered)->having("num >= $limitNum")->select();
- if (!$hads->isEmpty()) {
- Db::rollback();
- return json(['code' => 1, 'msg' => '指派的设计师已无指派名额,请刷新后再试']);
- }
- }
- $had->designer_id = $param['designer_id'];
- }
- $had->revisit_time = !empty($param['next_contact_date']) ? $param['next_contact_date'] . ' 00:00:00' : null;
- $had->last_contact_date = date('Y-m-d', time());
- $had->updatetime = date('Y-m-d H:i:s', time());
- if ($param['aid'] && $param['state'] == '预约活动') $had->aid = $param['aid'];
- $had->fresh = 0;
- // 交定产品
- $dingMsg = '';
- if ($had->state == '已交定') {
- empty($param['money']) ?: $had->deposit_money = $param['money'];
- if (!empty($param['package'])) {
- $package = CustomerPackage::where([['root_id', '=', $root_id], ['id', 'in', $param['package']]])->column('name', 'id');
- if (!$package) return json(['code' => 1, 'msg' => '客户产品不存在']);
- $had->package_id = implode(',', array_keys($package));
- $dingMsg = '交定金额:' . $param['money'] . '元,选择产品:' . implode(',', $package);
- }
- }
- if ($had->state == '已签单' && !empty($param['money'])) $had->signed_money = $param['money'];
- $had->save();
- // 更新客户保护期
- if ($param['state'] !== '无效' && !empty($state)) {
- $log_state = CustomerVisitLog::changeState($state, 'chaos');
- $v_where[] = ['employee_id', '=', $empid];
- $v_where[] = ['state', 'in', $log_state];
- $v_where[] = ['customer_id', '=', $param['customer_id']];
- if (!empty($had['employee_time'])) {
- $v_where[] = ['addtime', '>', $had['employee_time']];
- }
- $v_count = CustomerVisitLog::where($v_where)->count();
- //查看此状态的跟踪记录,如果有数据,说明此客户在此员工这里已经经历过此状态,不再更新保护期
- if (!$v_count) {
- Customer::changeProtectedTo($had->id, $root_id);
- }
- }
- // 添加确定消息内容
- $confirmMsg = '';
- if (!empty($param['confirm_date'])) {
- $confirmMsg = ', 时间:' . $param['confirm_date'];
- }
- // 备注
- $employeeName = Employee::where(['id' => $empid])->value('name');
- $remark = $param['remark'];
- if (empty($remark)) {
- if (empty($state))
- $remark = $employeeTypeName . $employeeName . '对客户进行了回访';
- else
- $remark = $employeeTypeName . $employeeName . '更新客户状态为' . $state . $confirmMsg . ',' . $dingMsg;
- }
- $valid_status = NULL;
- //2022-10-04 逻辑修改,无效时增加无效原因
- if ($param['state'] == '无效' && !empty($param['remark'])) {
- // $remark = $employeeTypeName . $employeeName . '更新客户状态为' . $state . $confirmMsg . ',原因是:' . $param['remark'] . $dingMsg;
- $valid_status = !empty($had->valid_time) ? 2 : 1;
- }
- //如果置为无效时,查询客户的有效状态
- // 添加追踪记录
- $visitLog = [
- 'customer_id' => $had->id,
- 'type' => $param['type'],
- 'next_contact_date' => !empty($param['next_contact_date']) ? $param['next_contact_date'] : null,
- 'employee_id' => $empid,
- 'user_id' => $this->request->empcrm['uid'],
- 'remark' => $remark,
- 'state' => $param['state'],
- 'weixin_media' => $param['weixin_media'],
- 'media_id' => $param['media_id'],
- 'starts' => $param['starts'],
- 'money' => $param['money'],
- 'confirm_date' => $param['confirm_date'],
- 'org_id' => $this->request->empcrm['org_id'],
- 'aid' => $param['aid'] ?: $had->aid,
- 'weixin_media1' => $param['weixin_media1'],
- 'media_id1' => $param['media_id1'],
- 'customer_employee_id' => $had->employee_id,
- 'customer_org_id' => Employee::where('id', $had->employee_id)->value('org_id'),
- 'img' => $param['img'],
- 'img1' => $param['img1'],
- 'number_of_visitors' => $param['number_of_visitors'],
- 'measure_room_img_type' => $param['measure_room_img_type'],
- 'deposit_mode' => $param['deposit_mode'],
- 'document_field' => $param['document_field'],
- 'document_name' => $param['document_name']
- ];
- $returnvisit = ['customer_org_id' => $visitLog['customer_org_id']];
- //2022-10-28 增加交定和签单凭证 凭证为图片
- $deliverySignVoucher = request()->only(['delivery_media_id' => '', 'delivery_weixin_media' => '', 'sign_media_id' => '', 'sign_weixin_media' => '', 'delivery_img' => '', 'sign_img' => '']);
- $visitLog = $visitLog + $deliverySignVoucher;
- empty($param['img']) ?: $visitLog['img'] = $param['img'];
- if (!empty($param['aid'])) {
- // 检测活动是否存在
- $existActivity = Activity::where([['id', '=', $param['aid']], ['root_id', '=', $root_id]])->count();
- if ($existActivity == 0) {
- Db::rollback();
- return json(['code' => 1, 'msg' => '活动不存在']);
- }
- $visitLog['aid'] = $param['aid'];
- }
- $visitLog = CustomerVisitLog::create($visitLog);
- if ($orders) $this->preformance_tasks($state, $param['customer_id'], $param['money'], $visitLog->id); //计算业绩
- if (strpos($visitLog->state, '预约') !== false) {
- $state = str_replace('预约', '', $visitLog->state);
- // 查找是否已经预约
- if ($is_manager == 1) {
- $hadSubscribe = CustomersSubscribe::where([
- ['customer_id', '=', $had['id']],
- //['employee_id|designer_id', '=', $empid],
- ['subscribe_date', '=', date('Y-m-d', strtotime($param['confirm_date']))],
- ['state', '=', 0]
- ])->count();
- $typeSubscribe = CustomersSubscribe::where([
- ['customer_id', '=', $had['id']],
- //['employee_id|designer_id', '=', $empid],
- ['type', '=', array_search($state, $stateType)],
- ['state', '=', 0]
- ])->find();
- } else {
- $hadSubscribe = CustomersSubscribe::where([
- ['customer_id', '=', $had['id']],
- ['employee_id|designer_id', '=', $empid],
- ['subscribe_date', '=', date('Y-m-d', strtotime($param['confirm_date']))],
- ['state', '=', 0]
- ])->count();
- $typeSubscribe = CustomersSubscribe::where([
- ['customer_id', '=', $had['id']],
- ['employee_id|designer_id', '=', $empid],
- ['type', '=', array_search($state, $stateType)],
- ['state', '=', 0]
- ])->find();
- }
- $typeSubscribe = false; // 不是同一天的预约,同类型还可以预约
- if ($hadSubscribe) {
- Db::rollback();
- return json(['code' => 1, 'msg' => '该客户在当前日期已有预约']);
- } elseif ($typeSubscribe) {
- Db::rollback();
- return json(['code' => 1, 'msg' => '该客户已有' . $state . '预约, 预约日期' . $typeSubscribe->subscribe_date]);
- } else {
- $subscribe = [
- 'customer_id' => $had['id'],
- 'subscribe_date' => $param['confirm_date'],
- 'employee_id' => $had->employee_id,
- 'designer_id' => $had->designer_id,
- 'type' => $state,
- 'aid' => $param['aid'] ?: $had->aid,
- 'org_id' => $had->org_id
- ];
- CustomersSubscribe::create($subscribe);
- }
- }
- //当设为无效时,记录当前操作人跟进的最后一次状态,此处在公海列表处展示
- if ($param['state'] == '无效') {
- $last_cvl = CustomerVisitLog::where('customer_id', $param['customer_id'])->with('employee')->order('addtime desc')->find();
- //获取"无效"并进入公海之前的那次跟进记录的信息
- if ($last_cvl) {
- $before_pool = [
- 'name' => $last_cvl->employee->name,
- 'state' => $last_cvl->state,
- 'addtime' => $last_cvl->addtime
- ];
- } else {
- $before_pool = [
- 'name' => Employee::find($had['employee_id'])->value('name'),
- 'state' => '待确认',
- 'addtime' => $had->addtime
- ];
- }
- // 该客户预约中的状态置为无效
- CustomersSubscribe::where([['customer_id', '=', $param['customer_id']], ['state', '=', 0]])->save(['state' => -1]);
- //公海内是否存在该客户信息,存在则合并客户信息(phone、phone1、phone2)
- $orgids = orgSubIds($root_id);
- $phone_arr = [];
- !empty(trim($had['phone'])) ? $phone_arr[] = cypherphone(trim($had['phone'])) : '';
- !empty(trim($had['phone1'])) ? $phone_arr[] = cypherphone(trim($had['phone1'])) : '';
- !empty(trim($had['phone2'])) ? $phone_arr[] = cypherphone(trim($had['phone2'])) : '';
- $phone_arr = array_filter($phone_arr);
- $pool_condition = [
- ['employee_id', '=', NULL],
- ['is_resource', '=', 0],
- ['org_id', 'in', $orgids],
- ['phone|phone1|phone2', 'in', $phone_arr]
- ];
- $pool_crm = Customer::where($pool_condition)->column('id');
- if (!empty($pool_crm)) {
- //公海存在则删除
- Customer::where([['id', 'in', $pool_crm]])->update(['delete_time' => time()]);
- //关联回访记录修改
- CustomerVisitLog::where([['customer_id', 'in', $pool_crm]])->update(['is_merge' => 1, 'customer_id' => $had['id']]);
- CustomersSubscribe::where([['customer_id', 'in', $pool_crm]])->update(['is_merge' => 1, 'customer_id' => $had['id']]);
- }
- //增加无效记录
- $vlglist = CustomerVisitLog::where([['customer_id', '=', $param['customer_id']], ['customer_employee_id', '=', $had->employee_id], ['state', 'not in', array_merge(CustomerVisitLog::changeState('待确认', 'chaos'), CustomerVisitLog::changeState('无效', 'chaos'))], ['save_portrait_field', 'NULL', null]])->count();
- $invalidlog = [
- 'customer_id' => $param['customer_id'],
- 'employee_id' => $had->employee_id,
- 'designer_id' => $had->designer_id ? $had->designer_id : 0,
- 'org_id' => $had->org_id,
- 'root_id' => $request->empcrm->root_id,
- 'cus_addtime' => $had->sign_time,
- 'visitlog_id' => $visitLog->id,
- 'assigned_personnel' => $had->assigned_personnel ? $had->assigned_personnel : '',
- 'source_id' => $had->source_id ? $had->source_id : 0,
- 'status' => !empty($vlglist) ? 1 : 2,
- 'cus_employee_time' => $had->employee_time,
- 'is_resource' => $had->is_resource,
- 'valid_time' => $had->valid_time
- ];
- CustomerInvalidLog::create($invalidlog);
- Customer::where('id', $param['customer_id'])->update(['state' => 7, 'employee_id' => NULL, 'org_id' => $request->empcrm->org_id, 'is_resource' => 0, 'before_pool' => json_encode($before_pool), 'designer_id' => NULL, 'employee_time' => NULL]);
- // -------------------- 企业微信逻辑相关 start -----------------
- // 判断是否绑定有企业微信外部联系人关系,并做解绑操作
- $company_id = Company::where('root_id', '=', $root_id)->value('id');
- $company_setting = WeworksingleCompanySetting::where('company_id', '=', $company_id)->find();
- if (!empty($company_setting)) {
- $w_where['company_id'] = $company_id;
- $w_where['employee_id'] = $empid;
- $w_where['customer_id'] = $param['customer_id'];
- WeworksingleCustomer::where($w_where)->save(['customer_id' => 0]);
- }
- // -------------------- 企业微信逻辑相关 end -----------------
- }
- // //如果签单,默认交定
- // if (strpos($param['state'], "签单") !== false) {
- // trace(strpos($param['state'], "签单"));
- // $lw[] = CustomerVisitLog::changeState(['state', '=', '交定']);
- // $lw[] = ['customer_id', '=', $param['customer_id']];
- // $check = CustomerVisitLog::where($lw)->find();
- // if (!$check) {
- // $l_visitlog = $visitLog->toArray();
- // unset($l_visitlog['id']);
- // $l_visitlog['state'] = '交定';
- // $l_visitlog['remark'] = !empty($param['remark']) ? $param['remark'] : $employeeTypeName . $employeeName . '变更客户状态为交定';
- // $new_visitlog = array_merge($param, $l_visitlog);
- // $new_visitlog['money'] = 0; //直接签单默认交定金额为0
- // $mr = CustomerVisitLog::create($new_visitlog);
- // $this->preformance_tasks('已交定', $param['customer_id'], $param['money'], $mr->id); //计算业绩
- // }
- // }
- if (in_array($param['state'], ['已到店', '已量房', '已到场'])) {
- $state = str_replace('已', '', $param['state']);
- $subscribe = CustomersSubscribe::where([['customer_id', '=', $had['id']], ['type', '=', array_search($state, $stateType)], ['state', '=', 0]])->find();
- if (!empty($subscribe)) {
- $subscribe->state = 1;
- $subscribe->save();
- } elseif ($param['aid'] && in_array($param['state'], ['已到场'])) { //没有预约可以直接确认到场
- $this->activityState($param, $had);
- }
- }
- if (in_array($param['state'], ['未到店', '未量房', '未到场'])) {
- $state = str_replace('未', '', $param['state']);
- $subscribe = CustomersSubscribe::where([['customer_id', '=', $had['id']], ['type', '=', array_search($state, $stateType)], ['state', '=', 0]])->find();
- if (!empty($subscribe)) {
- $subscribe->state = -1;
- $subscribe->save();
- }
- $data = [
- 'eid' => $had->employee_id,
- 'org_id' => $returnvisit['customer_org_id'],
- 'designer_id' => $had->designer_id,
- 'confirm_date' => $param['confirm_date'],
- 'customer_id' => $had->id,
- 'return_visit' => $had->return_visit,
- 'name' => $had->name,
- 'state' => $param['state']
- ];
- $this->cancelReservation($data);
- //取消预约后,待回访状态也要取消
- $had->return_visit = 0;
- $had->save();
- }
- //增加经纪人送积分功能
- if ($had->agents_id && in_array($param['state'], ['已到店', '已交定', '已签单'])) {
- $this->agents_integral($had->agents_id, $param['state'], $had->id);
- }
- // 微爆活动
- if (in_array($param['state'], ['已到店', '已量房', '已到场', '已交定', '已签单'])) {
- $activity_data['customer_id'] = $param['customer_id'];
- $activity_data['employee_id'] = $had->employee_id;
- $activity_data['org_id'] = $had->org_id;
- switch ($param['state']) {
- case '已到店':
- $activity_data['type'] = 2;
- break;
- case '已量房':
- $activity_data['type'] = 3;
- break;
- case '已交定':
- $activity_data['type'] = 4;
- break;
- case '已签单':
- $activity_data['type'] = 5;
- break;
- case '已到场':
- $activity_data['type'] = 7;
- break;
- default:
- break;
- }
- WechatActivityIntegral::addIntegral($activity_data, $request->empcrm->root_id);
- }
- Db::commit();
- $from_wework = input('from_wework', '', 'trim');
- if ($from_wework) {
- Console::call('download', ['customer', 'wework', (string)$root_id]);
- Console::call('download', ['customer1', 'wework', (string)$root_id]);
- } else {
- Console::call('download', ['customer']);
- Console::call('download', ['customer1']);
- }
- //2022-10-28 更新交定/签单凭证
- if (in_array($param['state'], ['已交定', '已签单'])) {
- $deliveryType = $param['state'] == '已交定' ? 'delivery' : 'sign';
- Console::call('download', [$deliveryType]);
- }
- Console::call('medal', ['visit_log', (string)$empid, (string)$root_id]); //勋章
- if ($is_manager == 1) {
- //经理回访后修改客户的待回访的状态
- Customer::where('id', $had->id)->update(['return_visit' => 0]);
- $data = ['phone' => $had->phone, 'name' => $had->name];
- } else {
- //给业务经理和设计是发送提醒消息回访
- $data = ['phone' => $had->phone, 'org_id' => $returnvisit['customer_org_id'], 'state' => $param['state'], 'confirm_date' => $param['confirm_date'], 'name' => $had->name, 'designer_id' => $had->designer_id, 'eid' => $had->employee_id, 'customer_id' => $had->id];
- }
- //$this->returnVisit($data);
- $data['name'] = !empty($data['name']) ? $data['name'] : '';
- $data['phone'] = substr_replace($data['phone'], '******', 3, 6);
- if (in_array($param['state'], $dd_state)) {
- // 发送管理层模板消息
- $msg = ['first' => '行为通知', 'keyword1' => '客户确认到店', 'keyword2' => "客户" . $data['name'] . "(" . $data['phone'] . ")确认到店", 'keyword3' => date('Y/m/d'), 'remark' => '请悉知'];
- TmpMsg::sendMsgToLeader($msg, $token['org_id']);
- }
- if ($state == '已签单') {
- // 发送管理层模板消息
- $msg = ['first' => '行为通知', 'keyword1' => '客户签单', 'keyword2' => "客户" . $data['name'] . "(" . $data['phone'] . ")签单", 'keyword3' => date('Y/m/d'), 'remark' => '请悉知'];
- TmpMsg::sendMsgToLeader($msg, $token['org_id']);
- }
- return json(['code' => 0, 'msg' => '跟进成功']);
- }
- /**
- * 2022-10-27 逻辑修改,跟进保护优化 如果后台设置了重复客户跟进保护规则 跟进到保护状态后其他客户的此重复客户就不能在跟进了,但是可以置为无效
- */
- private function forbiddenState($id, $status)
- {
- if ($status == '无效') return false;
- //$token = $this->request->token;
- $root_id = $this->request->empcrm->root_id;
- //保护规则
- $provite = Setting::where([['name', '=', 'forbiddenState'], ['root_id', '=', $root_id]])->value('content');
- if (!$provite) return false;
- $info = Customer::where('id', $id)->column('phone,phone1,phone2');
- //查询是否是重复客户
- $orgs = Org::where([['path', 'like', $root_id . '-%']])->column('id');
- $query[] = ['employee_id', '>', 0];
- $query[] = ['org_id', 'in', $orgs];
- $query[] = ['phone|phone1|phone2', 'in', array_filter($info[0])];
- // $query[] = ['employee_id','<>',$token['employee_id']];
- $check = Customer::where($query)->column('id,employee_id');
- if (count($check) <= 1) return false;
- $provite = explode(',', $provite);
- $state = [];
- foreach ($provite as $v) {
- $state = array_merge($state, CustomerVisitLog::changeState($v, 'chaos'));
- }
- //查询该客户是否存在受保护的状态 只有第一个跟进的业务员可以继续跟进
- $where[] = ['state', 'in', $state];
- $where[] = ['customer_id', 'in', array_column($check, 'id')];
- $where[] = ['employee_id|customer_employee_id', 'in', array_column($check, 'employee_id')];
- $repeat = CustomerVisitLog::where($where)->order('id asc')->field('customer_id,state')->findOrEmpty();
- if ($repeat->isEmpty() || (int)$repeat->customer_id == (int)$id) return false;
- //该客户已被其他员工 确认到店/确认量房/确认到场/交定/签单 无法更近
- $state = str_replace('已', '', $repeat->state);
- $state = in_array(trim($state), ['交定', '签单']) ? trim($state) : '确认' . trim($state);
- return '该客户已被其他员工“' . $state . '”无法跟进';
- }
- /**
- * 计算业绩任务
- */
- public function preformance_tasks($state, $customer_id, $money = 0, $vislog_id)
- {
- $token = $this->get_token();
- //防止后台切换部门
- $token['org_id'] = Employee::where('id', $token['employee_id'])->value('org_id');
- if ($state == '已交定') {
- $w2[] = ['customer_id', '=', $customer_id];
- $w2[] = ['state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')];
- $w2[] = ['customer_employee_id', '=', $token['employee_id']];
- $w2[] = ['customer_org_id', '=', $token['org_id']];
- //切换部门重复交定仍然算业绩
- $check = CustomerVisitLog::where($w2)->count(); //没有交定过计算业绩
- if ($check == 1) {
- $time = date('Y-m-d H:i:s');
- //查询进行中的指派到所属部门的所有进度
- // $w[] = ['org_id','=',$token['org_id']];
- $w[] = ['root_id', '=', $token['root_org']];
- $w[] = ['start_date', '<=', $time];
- $w[] = ['end_date', '>=', $time];
- $w[] = ['is_deposit', '=', 1];
- $preformance_tasks = PreformanceTasksModel::where($w)->column('id');
- $w1[] = ['root_id', '=', $token['root_org']];
- $w1[] = ['org_id', '=', $token['org_id']];
- $w1[] = ['performance_tasks_id', 'in', $preformance_tasks];
- $w1[] = ['is_deposit', '=', 1];
- //指派到自己部门
- $pid = PreformanceTasksOrg::where($w1)->group('performance_tasks_id')->column('*');
- $w4[] = ['id', 'in', array_column($pid, 'performance_tasks_id')];
- $model = PreformanceTasksModel::where($w4)->column('*');
- foreach ($model as $k => $v) {
- $u = [];
- $u['ok_deposit'] = $v['ok_deposit'] + 1;
- $u['customer_visit_log_id'] = $v['customer_visit_log_id'] ? $v['customer_visit_log_id'] . ',' . $vislog_id : $vislog_id;
- PreformanceTasksModel::where('id', $v['id'])->update($u);
- unset($u);
- }
- PreformanceTasksOrg::where($w1)->inc('ok_deposit')->update();
- }
- } elseif ($state == '已签单') {
- $w2[] = ['customer_id', '=', $customer_id];
- $w2[] = ['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')];
- $w2[] = ['customer_employee_id', '=', $token['employee_id']];
- $check = CustomerVisitLog::where($w2)->count(); //没有交定过计算业绩
- if ($check == 1) {
- $time = date('Y-m-d H:i:s');
- //查询进行中的指派到所属部门的所有进度
- // $w[] = ['org_id','=',$token['org_id']];
- $w[] = ['root_id', '=', $token['root_org']];
- $w[] = ['start_date', '<=', $time];
- $w[] = ['end_date', '>=', $time];
- $w[] = ['is_money', '=', 1];
- $preformance_tasks = PreformanceTasksModel::where($w)->column('id');
- $w1[] = ['root_id', '=', $token['root_org']];
- $w1[] = ['org_id', '=', $token['org_id']];
- $w1[] = ['performance_tasks_id', 'in', $preformance_tasks];
- $w1[] = ['is_money', '=', 1];
- //指派到自己部门
- $pid = PreformanceTasksOrg::where($w1)->group('performance_tasks_id')->column('*');
- $w4[] = ['id', 'in', array_column($pid, 'performance_tasks_id')];
- $model = PreformanceTasksModel::where($w4)->column('*');
- foreach ($model as $k => $v) {
- $u = [];
- $u['ok_money'] = $v['ok_money'] + $money;
- $u['customer_visit_log_id'] = $v['customer_visit_log_id'] ? $v['customer_visit_log_id'] . ',' . $vislog_id : $vislog_id;
- PreformanceTasksModel::where('id', $v['id'])->update($u);
- unset($u);
- }
- PreformanceTasksOrg::where($w1)->inc('ok_money', (int)$money)->update();
- }
- }
- return 1;
- }
- /**
- * 没有预约活动,跟进时传了aid和state='预约活动'
- * 没有预约可以直接确认到场
- * 补全数据添加预约和的追踪记录
- */
- public function activityState($param, $had)
- {
- $token = $this->get_token();
- $employeeTypeName = $token['org_type'] == 2 ? '设计师' : '销售';
- // 备注
- $employeeName = Employee::where(['id' => $token['employee_id']])->value('name');
- $remark = $param['remark'];
- if (empty($remark)) {
- $remark = $employeeTypeName . $employeeName . '对客户进行了回访';
- }
- //预约记录
- $visitLog = [
- 'customer_id' => $had->id,
- 'type' => $param['type'],
- 'next_contact_date' => !empty($param['next_contact_date']) ? $param['next_contact_date'] : date('Y-m-d'),
- 'employee_id' => $token['employee_id'],
- 'user_id' => $token['uid'],
- 'remark' => $remark,
- 'state' => 6,
- 'weixin_media' => '',
- 'media_id' => '',
- 'starts' => $param['starts'],
- 'money' => $param['money'],
- 'confirm_date' => $param['confirm_date'],
- 'org_id' => $token['org_id'],
- 'aid' => $param['aid'],
- 'weixin_media1' => '',
- 'media_id1' => '',
- 'customer_employee_id' => $had->employee_id,
- 'customer_org_id' => Employee::where('id', $had->employee_id)->value('org_id')
- ];
- CustomerVisitLog::create($visitLog);
- //
- $subscribe = [
- 'customer_id' => $had['id'],
- 'subscribe_date' => $param['confirm_date'],
- 'employee_id' => $had->employee_id,
- 'designer_id' => $had->designer_id,
- 'type' => 2,
- 'aid' => $param['aid'],
- 'org_id' => $visitLog['customer_org_id'],
- 'state' => 1
- ];
- CustomersSubscribe::create($subscribe);
- return 1;
- }
- /**
- * 取消预约 给部门管理员和设计师发送消息提醒
- * 张三取消了客户李四2020年8月1日的量房/到店/到场预约
- */
- public function cancelReservation($data)
- {
- $token = $this->get_token();
- if ($data['return_visit'] == 1) {
- $token['name'] = Employee::where([['root_id', '=', $token['root_org']], ['id', '=', $data['eid']]])->value('name');
- $eids = Employee::where([['root_id', '=', $token['root_org']], ['org_id', '=', $data['org_id']], ['state', '=', '在职'], ['is_manager', '=', 1], ['uid', '>', 0]])->column('id');
- if ($data['designer_id']) array_push($eids, $data['designer_id']);
- $date = date('Y-m-d', strtotime($data['confirm_date']));
- $date = explode('-', $date);
- $state = str_replace('未', '', $data['state']);
- $msg = $token['name'] . '取消了客户' . $data['name'] . $date[0] . '年' . $date[1] . '月' . $date[2] . '日的' . $state;
- $eids = array_unique($eids);
- foreach ($eids as $key => $val) {
- event(new Msg($val, $msg, 'returnVisit', $data['customer_id']));
- }
- return true;
- }
- return false;
- }
- /**
- * 经纪人送积分功能
- */
- public function agents_integral($agentid, $state, $customer_id)
- {
- //$token = $this->request->token;
- $root_id = $this->request->empcrm->root_id;
- $agent_data = AgentUser::where('id', $agentid)->field('agent_name,type')->find();
- if ($state == '已到店') {
- $code = 'daodian_integral';
- $sta_type = 1;
- }
- if ($state == '已交定') {
- $code = 'jiaoding_integral';
- $sta_type = 2;
- }
- if ($state == '已签单') {
- $code = 'qiandan_integral';
- $sta_type = 3;
- }
- //添加与经纪人相关记录
- $you = AgentCustomerLog::where([['agent_id', '=', $agentid], ['type', '=', $sta_type], ['customer_id', '=', $customer_id]])->count();
- if (empty($you)) {
- $add = array(
- 'agent_id' => $agentid,
- 'type' => $sta_type,
- 'typename' => $state,
- 'addtime' => time(),
- 'customer_id' => $customer_id,
- 'agent_type' => $agent_data['type'],
- 'status' => 1
- );
- AgentCustomerLog::insert($add);
- }
- if ($agent_data['type'] == 2) return; //如果是网红经纪人直接返回
- $jifen = CreditsSetting::where(['code' => $code, 'root_id' => $root_id])->value('value');
- $ye = AgentIntegral::where(['customer_id' => $customer_id, 'agent_id' => $agentid])->column('state');
- if (!in_array($state, $ye) && $agent_data['type'] == 1) {
- $add = array(
- 'agent_id' => $agentid,
- 'type' => 1,
- 'integral' => $jifen ? $jifen : 1,
- 'addtime' => time(),
- 'state' => $state,
- 'customer_id' => $customer_id
- );
- AgentIntegral::insert($add);
- }
- if ($state == '已交定' && !in_array('已到店', $ye)) {
- $jifen = CreditsSetting::where(['code' => 'daodian_integral', 'root_id' => $root_id])->value('value');
- $add = array(
- 'agent_id' => $agentid,
- 'type' => 1,
- 'integral' => $jifen ? $jifen : 1,
- 'addtime' => time(),
- 'state' => '已到店',
- 'customer_id' => $customer_id
- );
- AgentIntegral::insert($add);
- }
- if ($state == '已签单' && !in_array('已到店', $ye)) {
- $jifen = CreditsSetting::where(['code' => 'daodian_integral', 'root_id' => $root_id])->value('value');
- $add = array(
- 'agent_id' => $agentid,
- 'type' => 1,
- 'integral' => $jifen ? $jifen : 1,
- 'addtime' => time(),
- 'state' => '已到店',
- 'customer_id' => $customer_id
- );
- AgentIntegral::insert($add);
- }
- if ($state == '已签单' && !in_array('已交定', $ye)) {
- $jifen = CreditsSetting::where(['code' => 'jiaoding_integral', 'root_id' => $root_id])->value('value');
- $add = array(
- 'agent_id' => $agentid,
- 'type' => 1,
- 'integral' => $jifen ? $jifen : 1,
- 'addtime' => time(),
- 'state' => '已交定',
- 'customer_id' => $customer_id
- );
- AgentIntegral::insert($add);
- }
- }
- /**
- * 消息提醒
- * eid 客户所属业务员id,
- * org_id 业务员部门id
- * state 跟进状态
- * name 业务员姓名
- * designer_id 设计师id
- * 01、客户邀约提醒管理层和指派的设计师。没指派设计师时,不提醒设计师,提醒管理层去指派设计师。在员工客户里面需要管理层回访的客户红色标签提示。
- * 02、业务员确认客户量房、到店、到场、交定、签单,提醒管理层回访客户。
- * a、管理层点击提醒,进入对应客户详情页面。进行客户跟踪。
- * b、在员工客户里面需要管理层回访的客户红色标签提示。
- */
- public function returnVisit($data)
- {
- $token = $this->get_token();
- $token['name'] = Employee::where([['id', '=', $data['eid']]])->value('name');
- $eids = Employee::where([['root_id', '=', $token['root_org']], ['org_id', '=', $data['org_id']], ['state', '=', '在职'], ['is_manager', '=', 1], ['uid', '>', 0]])->column('id');
- $state1 = CustomerVisitLog::changeState('确认到店', 'chaos');
- $state2 = CustomerVisitLog::changeState('确认量房', 'chaos');
- $state3 = CustomerVisitLog::changeState('确认到场', 'chaos');
- $state4 = CustomerVisitLog::changeState('交定', 'chaos');
- $state5 = CustomerVisitLog::changeState('签单', 'chaos');
- $make_state1 = array_merge($state1, $state2, $state3, $state4, $state5);
- $state6 = CustomerVisitLog::changeState('预约到店', 'chaos');
- $state7 = CustomerVisitLog::changeState('预约量房', 'chaos');
- $state8 = CustomerVisitLog::changeState('预约活动', 'chaos');
- $make_state2 = array_merge($state6, $state7, $state8);
- if (in_array($data['state'], $make_state2)) {
- $date = $office_date = date('Y-m-d', strtotime($data['confirm_date']));
- $date = explode('-', $date);
- $state = str_replace('预约', '', $data['state']);
- $msg = $token['name'] . '预约了客户' . $data['name'] . ',于' . $date[0] . '年' . $date[1] . '月' . $date[2] . '日' . $state;
- $msg = $data['designer_id'] ? $msg : $msg . ',该客户暂未指派设计师,请及时指派!!!';
- $type = '预约';
- } elseif (in_array($data['state'], $make_state1)) {
- $date = $office_date = date('Y-m-d');
- $date = explode('-', $date);
- $state = str_replace('已', '', $data['state']);
- $msg = $token['name'] . '确认客户' . $data['name'] . ',于' . $date[0] . '年' . $date[1] . '月' . $date[2] . '日' . $state;
- $type = '确认';
- } else {
- return false;
- }
- if ($data['designer_id']) array_push($eids, $data['designer_id']);
- $eids = array_unique($eids);
- $param = ['name' => $data['name'], 'sale_name' => $token['name'], 'type' => $type, 'state' => $state, 'phone' => $data['phone'], 'date' => $office_date];
- foreach ($eids as $key => $val) {
- event(new Msg($val, $msg, 'returnVisit', $data['customer_id']));
- //公众号消息
- $param['employee_id'] = $val;
- $param['cid'] = $data['customer_id'];
- $this->sendOfficialAccount($param);
- }
- Customer::where('id', $data['customer_id'])->update(['return_visit' => 1]);
- return true;
- }
- /**
- * 发送公众号消息
- * $data['name'] 客户姓名
- * $data['type'] 预约/确认
- * $data['state'] 到店,量房,到场
- * $data['phone'] 客户电话
- * $data['sale_name'] 业务员姓名
- * $data['employee_id] 业务员id
- * $data['date'] 日期
- */
- public function sendOfficialAccount($data)
- {
- //$token = $this->request->token;
- $token = $this->get_token();
- $dates = explode('-', $data['date']);
- $client_type = Miniprogram::where('root_id', $token['root_org'])->value('notify');
- if ($data['type'] == '预约') {
- //查询预约编号 今天的第多少条预约记录
- $state6 = CustomerVisitLog::changeState('预约到店', 'chaos');
- $state7 = CustomerVisitLog::changeState('预约量房', 'chaos');
- $state8 = CustomerVisitLog::changeState('预约活动', 'chaos');
- $make_state2 = array_merge($state6, $state7, $state8);
- $eids = Employee::where([['root_id', '=', $token['root_org']]])->column('id');
- $count = CustomerVisitLog::where([['addtime', 'like', '%' . date('Y-m-d') . '%'], ['state', 'in', $make_state2], ['employee_id', 'in', $eids]])->count();
- $count++;
- $number = $dates[0] . $dates[1] . $dates[2] . $count;
- $data['page'] = "/pages/index/index?office_type=cread&cid=" . $data['cid'] . '&clientype=' . $client_type;
- $data['param'] = [
- 'first' => '预约提醒', //标题
- 'keyword1' => $number, //预约编号
- 'keyword2' => $data['type'] . $data['name'] . $data['state'], //预约项目
- 'keyword3' => $data['date'], //预约时间
- 'keyword4' => substr_replace($data['phone'], '******', 3, 6), //预约电话
- 'keyword5' => $data['name'], //预约姓名
- 'remark' => '您好,业务员' . $data['sale_name'] . '新增一次预约记录,请及时回访' //电话
- ];
- $msg = [
- 'type' => 'returnVisit',
- 'employee_id' => $data['employee_id']
- ];
- EmployeeMsg::temploateMsg($msg, $data);
- } elseif ($data['type'] == '确认') {
- }
- return true;
- }
- //转换token
- public function get_token()
- {
- $token = [
- 'uid' => $this->request->empcrm->uid,
- 'employee_id' => $this->request->empcrm->id,
- 'org_id' => $this->request->empcrm->org_id,
- 'root_org' => $this->request->empcrm->root_id,
- 'name' => $this->request->empcrm->name,
- ];
- return $token;
- }
- /**
- * 客户迁移
- */
- public function customerTransfer()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $orgids = orgSubIds($root_id);
- $param = $this->request->only(['customer_ids' => '', 'employeeid']);
- $arr = explode(',', $param['customer_ids']);
- //查询手机号
- $phone = Customer::where([['employee_id', '=', $param['employeeid']], ['org_id', 'in', $orgids]])->column('phone,phone1,phone2');
- $phones = [];
- foreach ($phone as $v) {
- $phones = array_merge($phones, array_values($v));
- }
- $phones = array_filter($phones);
- $customers = Customer::where([['id', 'in', $arr], ['org_id', 'in', $orgids]])->column('id,name,phone,phone1,phone2');
- $repeat = $no_repeat = [];
- foreach ($customers as $v2) {
- $l_phone = array_filter([$v2['phone'], $v2['phone1'], $v2['phone2']]);
- empty(array_intersect($l_phone, $phones)) ? $no_repeat[] = $v2['id'] : $repeat[] = $v2['name'];
- }
- if ($no_repeat) {
- $org_id = Employee::where('id', $param['employeeid'])->value('org_id');
- Customer::where([['id', 'in', $no_repeat]])->update(['employee_id' => $param['employeeid'], 'org_id' => $org_id]);
- Customer::where([['id', 'in', $no_repeat]])->update(['transfer_info' => json_encode(['transfer_empid' => $empid, 'transfer_time' => date('Y-m-d H:i:s'), 'transfer_empname' => $request->empcrm->name])]);
- //修改转移的客户的跟进记录的customer_employee_id为新员工的id
- CustomerVisitLog::where([['customer_id', 'in', $no_repeat], ['org_id', 'in', $orgids]])->update(['customer_employee_id' => $param['employeeid'], 'customer_org_id' => $org_id]);
- }
- if ($repeat) return json(['code' => 1, 'msg' => '分派失败:员工名下已存在客户[' . implode(',', $repeat) . ']的联系方式']);
- return json(['code' => 0, 'msg' => '分派完成']);
- }
- //已签单或已交定客户详情
- public function deposit_sign_detail()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $param = $this->request->only(['cid']);
- $ms = $this->seLemp($param['cid'], $empid);
- $data = $ms->toArray();
- $visitlog = $data['visitLog'];
- if (!empty($visitlog)) {
- foreach ($visitlog as $key => $val) {
- $visitlog[$key]['empname'] = Employee::where('id', $val['employee_id'])->value('name');
- $parr[] = $val['addtime'];
- }
- array_multisort($parr, SORT_ASC, $visitlog);
- foreach ($visitlog as $key => $val) {
- if ((count($visitlog) - 1) > $key) {
- $visitlog[$key]['xt'] = round((strtotime($visitlog[$key + 1]['addtime']) - strtotime($val['addtime'])) / 86400);
- }
- if ((count($visitlog) - 1) == $key) {
- $visitlog[$key]['xt'] = 0;
- }
- }
- }
- $data['visitLog'] = $visitlog;
- View::assign('data', $data);
- return View::fetch();
- }
- //公用查询客户详情
- public function other_seLemp($id, $org_id)
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $orgids = orgSubIds($root_id);
- if (!in_array($org_id, $orgids)) echo json(['code' => 1, 'msg' => '数据不存在']);
- $condition = [
- ['org_id', '=', $org_id],
- ['id', '=', $id]
- ];
- $data = Customer::with(['source', 'designer' => function ($query) {
- $query->field('id,name,headimgurl,org_id,uid');
- }, 'employee' => function ($query) {
- $query->withField(['id', 'name']);
- }, 'visitLog' => function ($query) {
- $query->field('id,customer_id,type,next_contact_date,remark,addtime,employee_id,state,customer_employee_id,img,img1,sign_img,delivery_img,save_portrait_field,data_type,data_id,number_of_visitors,talking_order_time,stay_length,document_field,document_name')->order('addtime desc');
- }])->where($condition)->find();
- if (empty($data)) echo json(['code' => 1, 'msg' => '数据不存在']);
- //if (empty($data)) return false;
- $data['daochang'] = CustomerVisitLog::where([['customer_id', '=', $id], ['employee_id', '=', $data['employee_id']], CustomerVisitLog::changeState(['state', '=', '已到访'])])->order('addtime desc')->find();
- $data['deposit'] = CustomerVisitLog::where([['customer_id', '=', $id], ['employee_id', '=', $data['employee_id']], CustomerVisitLog::changeState(['state', '=', '交定'])])->order('addtime desc')->field("id,money,DATE_FORMAT(confirm_date, '%Y-%m-%d') as confirm_date")->find();
- $data['sign'] = CustomerVisitLog::where([['customer_id', '=', $id], ['employee_id', '=', $data['employee_id']], CustomerVisitLog::changeState(['state', '=', '签单'])])->order('addtime desc')->field("id,money,DATE_FORMAT(confirm_date, '%Y-%m-%d') as confirm_date")->find();
- $data['dd'] = CustomerVisitLog::where([['customer_id', '=', $id], ['customer_employee_id', '=', $data['employee_id']], CustomerVisitLog::changeState(['state', '=', '已到店'])])->order('addtime desc')->value("DATE_FORMAT(confirm_date, '%Y-%m-%d')");
- $data['lf'] = CustomerVisitLog::where([['customer_id', '=', $id], ['customer_employee_id', '=', $data['employee_id']], CustomerVisitLog::changeState(['state', '=', '已量房'])])->order('addtime desc')->value("DATE_FORMAT(confirm_date, '%Y-%m-%d')");
- $data['wuyou'] = CustomerVisitLog::where([['customer_id', '=', $id], ['employee_id', '=', $data['employee_id']], CustomerVisitLog::changeState(['state', '=', '买卡'])])->field("id,money,DATE_FORMAT(confirm_date, '%Y-%m-%d') as wuyou_date")->find();
- $data['package_name'] = $data['package_id'] ? CustomerPackage::where('id', $data['package_id'])->value('name') : '';
- $data['deco_style_name'] = $data['deco_style'] ? Decostyle::where('id', $data['deco_style'])->value('name') : '';
- //$data['company_name'] = Company::where('root_id',$root_id)->value('company_name');
- $data['add_wechat'] = '否';
- $data['housetype'] = $data['unit_number'] = $data['subjection_plan'] = null;
- if (!empty($data['ext'])) {
- $field = $this->get_portrait_field($data);
- foreach ($field as $key => $val) {
- if ($val['keyname'] == 'wechat' && !empty($val['value'])) {
- $data['add_wechat'] = CustomerPortraitFieldSelect::where('id', $val['value'])->value('name');
- }
- if ($val['keyname'] == 'housetype_arrow' && !empty($val['value'])) {
- $data['housetype'] = CustomerPortraitFieldSelect::where('id', $val['value'])->value('name');
- }
- if ($val['keyname'] == 'subjection_plan' && !empty($val['value'])) {
- $data['subjection_plan'] = CustomerPortraitFieldSelect::where('id', $val['value'])->value('name');
- }
- if ($val['keyname'] == 'unit_number' && !empty($val['value'])) {
- $data['unit_number'] = $val['value'];
- }
- }
- }
- $data['daochang'] = !empty($data['daochang']) ? json_decode($data['daochang'], true) : null;
- $data['deposit'] = !empty($data['deposit']) ? json_decode($data['deposit'], true) : null;
- $data['sign'] = !empty($data['sign']) ? json_decode($data['sign'], true) : null;
- return $data;
- }
- /**
- * 判断能否查看客户详情
- */
- public function checkCustomer()
- {
- $id = input('id', '', 'intval');
- $request = request();
- $empid = $request->empcrm->id;
- $is_manager = $request->empcrm->is_manager;
- $wuxiao_state = Customer::changeState('无效', 'chaos');
- $where = [
- ['id', '=', $id],
- ['state', 'not in', $wuxiao_state]
- ];
- $data = Customer::where($where)->find();
- if (empty($data)) {
- return json(['code' => 1, 'msg' => '无法查看此客户,请刷新后重试']);
- }
- $empIdList = explode(',', $data->employee_id . ',' . $data->employee_id . ',' . $data->assigned_personnel);
- $empIdList = array_unique(array_filter($empIdList));
- if (!empty($data) && !in_array($empid, $empIdList)) {
- if ($is_manager == 1) {
- $team_orgs = orgSubIds($request->empcrm->org_id);
- $yuangong = Employee::where([['org_id', 'in', $team_orgs], ['uid', '>', 0], ['id', 'in', $empIdList]])->count();
- if ($yuangong == 0) $data = null;
- } else {
- $data = null;
- }
- }
- if (empty($data)) {
- return json(['code' => 1, 'msg' => '无法查看此客户,请刷新后重试']);
- }
- return json(['code' => 0, 'msg' => 'success']);
- }
- //添加回访记录
- public function addlog()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $is_manager = $request->empcrm->is_manager;
- $team_orgs = orgSubIds($request->empcrm->org_id);
- $param = $this->request->only(['cid', 'org_id' => '']);
- $orgids = orgSubIds($root_id);
- //if(!empty($param['org_id']) && $is_manager == 1 && !in_array($param['org_id'],$team_orgs)){
- if (!empty($param['org_id']) && in_array($param['org_id'], $orgids)) {
- $ms = $this->other_seLemp($param['cid'], $param['org_id']);
- $is_sharecus = 1;
- } else {
- $ms = $this->seLemp($param['cid'], $empid);
- }
- $data = $ms->toArray();
- $stain = [];
- if (!empty($data['visitLog'])) {
- $state1 = CustomerVisitLog::changeState('已到店', 'chaos');
- $state2 = CustomerVisitLog::changeState('已到场', 'chaos');
- foreach ($data['visitLog'] as $key => $val) {
- if ($val['state'] == '已签单' && $request->empcrm['xinjushang'] == 0) $val['state'] = '已转单';
- if ($val['state'] == '已交定' && $request->empcrm['xinjushang'] == 0) $val['state'] = '已签单';
- if ($val['state'] == '已转单' && $request->empcrm['xinjushang'] == 0) $data['visitLog'][$key]['remark'] = str_replace('已签单', '已转单', $val['remark']);
- if ($val['state'] == '已签单' && $request->empcrm['xinjushang'] == 0) $data['visitLog'][$key]['remark'] = str_replace('已交定', '已签单', $val['remark']);
- $data['visitLog'][$key]['state'] = $val['state'];
- $data['visitLog'][$key]['empname'] = Employee::where('id', $val['employee_id'])->value('name');
- if (!in_array($val['state'], $stain)) {
- $stain[] = $val['state'];
- }
- if (!empty($val['save_portrait_field'])) {
- $save_portrait_field = json_decode($val['save_portrait_field'], true);
- foreach ($save_portrait_field as $k => $v) {
- if ($v['type'] == 6) {
- $save_portrait_field[$k]['valname'] = explode(',', $v['value']);
- }
- }
- } else {
- $save_portrait_field = '';
- }
- $data['visitLog'][$key]['save_portrait_field'] = $save_portrait_field;
- $data['visitLog'][$key]['isyes'] = 1;
- if ($key != 0 && $val['next_contact_date'] != '暂未设置下次回访时间') $data['visitLog'][$key]['isyes'] = 0;
- $data['visitLog'][$key]['wisdom_url'] = '';
- $data['visitLog'][$key]['is_wisdom'] = 0;
- if ($val['remark'] && strpos($val['remark'], '讲解智慧屏##') !== false) {
- //remark = 接待了客户:张三,讲解时长21分钟@讲解智慧屏## url;
- $data['visitLog'][$key]['is_wisdom'] = 1;
- $arr = explode('##', $val['remark']);
- if (count($arr) == 2) {
- $ali_oss_bindurl = config('app.ali_oss_bindurl');
- $data['visitLog'][$key]['wisdom_url'] = 'https://' . $ali_oss_bindurl . '/' . $arr[1];
- $title = explode('@', $arr[0]);
- $data['visitLog'][$key]['remark'] = $title[0];
- }
- }
- // 检测是否有录音
- if ($val['data_type'] == 'out_call') {
- $data['visitLog'][$key]['is_wisdom'] = 2;
- $data['visitLog'][$key]['wisdom_url'] = OutCallLog::where(['id' => $val['data_id']])->value('url');
- } elseif ($val['data_type'] == 'mobile_call' && strpos($val['remark'], '#') !== false) {
- $ali_oss_bindurl = config('app.ali_oss_bindurl');
- $arr = explode('#', $val['remark']);
- $data['visitLog'][$key]['wisdom_url'] = 'https://' . $ali_oss_bindurl . '/' . $arr[1];
- $data['visitLog'][$key]['remark'] = $arr[0];
- }
- if ($val['number_of_visitors'] > 0 && in_array($val['state'], $state1)) {
- $val['remark'] = $val['remark'] . ' 到店人数:' . $val['number_of_visitors'] . '人;';
- $data['visitLog'][$key]['remark'] = $val['remark'];
- }
- if ($val['talking_order_time'] && in_array($val['state'], $state1)) {
- $val['remark'] = $val['remark'] . ' 谈单时长:' . $val['talking_order_time'] . '分钟;';
- $data['visitLog'][$key]['remark'] = $val['remark'];
- }
- if ($val['number_of_visitors'] > 0 && in_array($val['state'], $state2)) {
- $val['remark'] = $val['remark'] . ' 到场人数:' . $val['number_of_visitors'] . '人;';
- $data['visitLog'][$key]['remark'] = $val['remark'];
- }
- if ($val['stay_length'] && in_array($val['state'], $state2)) {
- $val['remark'] = $val['remark'] . ' 停留时间:' . $val['stay_length'] . '分钟;';
- $data['visitLog'][$key]['remark'] = $val['remark'];
- }
- //检测是否有下载文件
- if (!empty($val['document_field']) && !empty($val['document_name'])) {
- $str = $root_id . '#' . $empid . '#' . $val['id'];
- $aec = new Aec(config('app.aec_key'), config('app.aec_iv'));
- $value = $aec->encrypt($str);
- $data['visitLog'][$key]['document_field'] = $request->domain() . '/downdocument/index.html?token=' . $value;
- }
- }
- }
- $data['allstate'] = $stain;
- //房屋状态
- $house_status = CustomerPortraitField::with(['select'])->where([['root_id', '=', $root_id], ['keyname', '=', 'house_status']])->find()->toArray();
- View::assign('house_status', $house_status['select']);
- View::assign('empname', $request->empcrm->name);
- //指派类型
- $assign_count = Setting::where([['root_id', '=', $root_id], ['name', '=', 'assign_type']])->findOrEmpty();
- $assign_name = !$assign_count->isEmpty() ? json_decode($assign_count->content, true) : [];
- $assign_list = [];
- foreach ($assign_name as $k => $v) {
- if ($v == 'yixiang') $assign_list[] = ['keyname' => $v, 'value' => '意向客户指派'];
- if ($v == 'liangfang') $assign_list[] = ['keyname' => $v, 'value' => '量房客户指派'];
- if ($v == 'daodian') $assign_list[] = ['keyname' => $v, 'value' => '到店客户指派'];
- }
- View::assign('assign_list', $assign_list);
- $isadd_log = 0;
- $isAllManagerOperateAble = Setting::where(['root_id' => $root_id, 'name' => 'is_all_manager_oprate_able'])->value('content');
- $path = Org::where([['pid', '=', $request->empcrm->org_id]])->count();
- if ($is_manager && in_array($data['org_id'], $team_orgs)) {
- if ($isAllManagerOperateAble || empty($path)) // 判断是否能跟进,开启所有以上领导能跟进,或直属领导跟进
- $isadd_log = 1;
- }
- if ($data['employee_id'] == $empid) {
- $isadd_log = 1;
- }
- // 加微信时间
- $data['add_wechat_time'] = '';
- if (!empty($data['ext']) && $data['ext'] != 'null') {
- $ext = json_decode($data['ext'], true);
- foreach ($ext as $i) {
- if (isset($i['keyname']) && $i['keyname'] == 'add_wechat_time') {
- $data['add_wechat_time'] = $i['value'];
- break;
- }
- }
- }
- if (!empty($is_sharecus) && $is_manager) $isadd_log = 1;
- View::assign('data', $data);
- View::assign('isadd_log', $isadd_log);
- View::assign('is_manager', $is_manager);
- // 判断是否有外呼系统
- $outcallSetting = OutCallSetting::where(['root_id' => $root_id, 'type' => 'yhuatong'])->find();
- if ($outcallSetting !== null) {
- // 如果有外呼系统则获取登陆人拨打的电话列表
- View::assign('outcall', 1);
- $phones = empty($request->empcrm['out_call_phone']) ? [] : explode(',', $request->empcrm['out_call_phone']);
- array_unshift($phones, $request->empcrm['phone']);
- View::assign('outcall_phone', $phones);
- } else {
- View::assign('outcall', 0);
- }
- //判断下次回访时间是否必传
- $next_where[] = ['root_id', '=', $root_id];
- $next_where[] = ['name', '=', 'nextVisitTimeIsOk'];
- $nextVisitTimeIsOk = Setting::where($next_where)->value('content');
- $fish = CustomerSource::where(['root_id' => $root_id, 'source' => '飞鱼线索'])->find();
- View::assign('fish', isset($fish) && $data['source_id'] == $fish['id'] ? 1 : 0);
- View::assign('nextVisitTimeIsOk', $nextVisitTimeIsOk);
- // 检测是否已经申请
- $rs = CustomerStateCheck::where(['customer_id' => $param['cid']])->find();
- View::assign('valid_state', $rs);
- return View::fetch();
- }
- //单独确认客户有效
- public function validation()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $orgids = orgSubIds($root_id);
- $param = $this->request->only(['customer_id']);
- $ms = Customer::where([['org_id', 'in', $orgids], ['id', '=', $param['customer_id']], ['state', 'in', Customer::changeState('待确认', 'chaos')]])->update(['state' => 1, 'valid_time' => date('Y-m-d H:i:s')]);
- if ($ms) {
- return json(['code' => 0, 'msg' => '确认完成']);
- } else {
- return json(['code' => 1, 'msg' => '确认失败']);
- }
- }
- //新增客户后跟进页面
- public function followup()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $param = $this->request->only(['cid']);
- $data = $this->seLemp($param['cid'], $empid);
- View::assign('data', $data);
- return View::fetch();
- }
- //设置客户介绍新客户标记
- public function save_introduce()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $orgids = orgSubIds($root_id);
- $param = $this->request->only(['cid']);
- $ms = Customer::where([['id', '=', $param['cid']], ['org_id', 'in', $orgids]])->find();
- if (!$ms) return json(['code' => 1, 'msg' => '客户不存在']);
- $ms->introduce = 1;
- $ms->save();
- return json(['code' => 0, 'msg' => '设置成功']);
- }
- //公共查询客户详情(没有所属员工id时)
- public function cusdet_noemp($id)
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $orgids = orgSubIds($root_id);
- $data = Customer::with(['visitLog'])->where([['id', '=', $id], ['org_id', 'in', $orgids]])->find();
- $data = $data->toArray();
- // echo json_encode($data['visitLog']);
- // exit;
- $data['package_name'] = $data['package_id'] ? CustomerPackage::where('id', $data['package_id'])->value('name') : '';
- $data['deco_style_name'] = $data['deco_style'] ? Decostyle::where('id', $data['deco_style'])->value('name') : '';
- $data['company_name'] = Company::where('root_id', $root_id)->value('company_name');
- $data['add_wechat'] = '否';
- $data['housetype'] = $data['unit_number'] = $data['subjection_plan'] = null;
- $data['jiaofang'] = !empty($data['house_delivery_time']) ? ($data['house_delivery_time'] <= date('Y/m/d', time()) ? '现房' : '期房') : '';
- $data['company_name'] = Company::where('root_id', $root_id)->value('company_name');
- //$vslog = CustomerVisitLog::where('customer_id',$id)->order('addtime desc')->find();
- $data['lrname'] = NULL;
- if (!empty($data['visitLog']) && !empty($data['visitLog'][0]['customer_employee_id'])) {
- $data['lrname'] = Employee::where('id', $data['visitLog'][0]['customer_employee_id'])->value('name');
- }
- if (!empty($data['ext'])) {
- $field = $this->get_portrait_field($data);
- foreach ($field as $key => $val) {
- if ($val['keyname'] == 'wechat' && !empty($val['value'])) {
- $data['add_wechat'] = CustomerPortraitFieldSelect::where('id', $val['value'])->value('name');
- }
- if ($val['keyname'] == 'housetype_arrow' && !empty($val['value'])) {
- $data['housetype'] = CustomerPortraitFieldSelect::where('id', $val['value'])->value('name');
- }
- if ($val['keyname'] == 'subjection_plan' && !empty($val['value'])) {
- $data['subjection_plan'] = CustomerPortraitFieldSelect::where('id', $val['value'])->value('name');
- }
- if ($val['keyname'] == 'unit_number' && !empty($val['value'])) {
- $data['unit_number'] = $val['value'];
- }
- if ($val['keyname'] == 'sign_name' && !empty($val['value'])) {
- $data['sign_time'] = $val['value'];
- }
- }
- }
- if (!empty($data['visitLog'])) {
- foreach ($data['visitLog'] as $key => $val) {
- if ($val['state'] == '已签单' && $request->empcrm['xinjushang'] == 0) $val['state'] = '已转单';
- if ($val['state'] == '已交定' && $request->empcrm['xinjushang'] == 0) $val['state'] = '已签单';
- if ($val['state'] == '已转单' && $request->empcrm['xinjushang'] == 0) $data['visitLog'][$key]['remark'] = str_replace('已签单', '已转单', $val['remark']);
- if ($val['state'] == '已签单' && $request->empcrm['xinjushang'] == 0) $data['visitLog'][$key]['remark'] = str_replace('已交定', '已签单', $val['remark']);
- $data['visitLog'][$key]['state'] = $val['state'];
- $data['visitLog'][$key]['empname'] = Employee::where('id', $val['employee_id'])->value('name');
- if (!empty($val['save_portrait_field'])) {
- $save_portrait_field = json_decode($val['save_portrait_field'], true);
- foreach ($save_portrait_field as $k => $v) {
- if ($v['type'] == 6) {
- $save_portrait_field[$k]['valname'] = explode(',', $v['value']);
- }
- }
- } else {
- $save_portrait_field = '';
- }
- $data['visitLog'][$key]['save_portrait_field'] = $save_portrait_field;
- $parr[] = $val['addtime'];
- if ($val['next_contact_date'] > date('Y-m-d H:i:s')) {
- $data['visitLog'][$key]['isyes'] = 1;
- } else {
- $data['visitLog'][$key]['isyes'] = 0;
- }
- $data['visitLog'][$key]['wisdom_url'] = '';
- $data['visitLog'][$key]['is_wisdom'] = 0;
- if ($val['remark'] && strpos($val['remark'], '讲解智慧屏##') !== false) {
- //remark = 接待了客户:张三,讲解时长21分钟@讲解智慧屏## url;
- $data['visitLog'][$key]['is_wisdom'] = 1;
- $arr = explode('##', $val['remark']);
- if (count($arr) == 2) {
- $ali_oss_bindurl = config('app.ali_oss_bindurl');
- $data['visitLog'][$key]['wisdom_url'] = 'https://' . $ali_oss_bindurl . '/' . $arr[1];
- $title = explode('@', $arr[0]);
- $data['visitLog'][$key]['remark'] = $title[0];
- }
- }
- // 检测是否有录音
- if ($val['data_type'] == 'out_call') {
- $data['visitLog'][$key]['is_wisdom'] = 2;
- $data['visitLog'][$key]['wisdom_url'] = OutCallLog::where(['id' => $val['data_id']])->value('url');
- }
- }
- array_multisort($parr, SORT_DESC, $data['visitLog']);
- }
- return $data;
- }
- /**
- * 公海获取客户
- * customer_id来源:业务员获取(int)、报备录入时查询公海存在自动获取(逗号分隔字符串)
- */
- public function fetchpoolitem()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $employee_id = $request->empcrm->id;
- $orgids = orgSubIds($root_id);
- $param = $this->request->only(['cid', 'designer_id']);
- if (!$request->isAjax()) {
- $this->screen();
- $data = $this->cusdet_noemp($param['cid']);
- View::assign('data', $data);
- return View::fetch();
- }
- // 设计师能否报备、获取客户
- $de_where[] = ['root_id', '=', $root_id];
- $de_where[] = ['name', '=', 'designer_get_customer'];
- $info_de_where = Setting::where($de_where)->findOrEmpty();
- if (!$info_de_where->isEmpty() && request()->empcrm->org_type == 2) {
- if ((int)$info_de_where['content'] != 1) {
- return json(['code' => 1, 'msg' => '您的身份为设计师,无法获取客户']);
- }
- }
- //$customer_id = explode(',', $param['customer_id']);
- $customer_id = $param['cid'];
- $otheremp = Customer::where([['org_id', 'in', $orgids], ['id', 'in', $customer_id], ['employee_id', '=', NULL]])->field('employee_id,phone,phone1,phone2')->select();
- if (empty($otheremp)) {
- return json(['code' => 1, 'msg' => '客户已被人抢先获取。记得下次抓住机会。']);
- }
- //判断名下是否存在该客户手机号,存在则不可获取
- foreach ($otheremp as $item) {
- $phone_arr = [];
- !empty($item['phone']) ? $phone_arr[] = cypherphone($item['phone']) : '';
- !empty($item['phone1']) ? $phone_arr[] = cypherphone($item['phone1']) : '';
- !empty($item['phone2']) ? $phone_arr[] = cypherphone($item['phone2']) : '';
- $phone_arr = array_filter($phone_arr);
- $haveCrm = Customer::where([['employee_id', '=', $employee_id], ['phone|phone1|phone2', 'in', $phone_arr]])->count();
- if ($haveCrm) {
- return json(['code' => 1, 'msg' => '您名下已存在该客户信息。']);
- }
- }
- if (Customer::where([['id', 'in', $customer_id]])->update(['employee_id' => $employee_id, 'org_id' => $this->request->empcrm->org_id, 'state' => 0, 'remark' => '公海获取', 'fresh' => 1, 'designer_id' => null, 'employee_time' => date('Y-m-d H:i:s')])) {
- $saveAll = [];
- $saveAll[] = [
- 'customer_id' => $customer_id,
- 'type' => 1,
- 'remark' => '公海获取',
- 'employee_id' => $employee_id,
- 'user_id' => $this->request->empcrm->uid,
- 'state' => 1
- ];
- if (CustomerVisitLog::insertAll($saveAll)) {
- return json(['code' => 0, 'msg' => '获取成功']);
- } else {
- return json(['code' => 1, 'msg' => '获取失败']);
- }
- } else {
- return json(['code' => 1, 'msg' => '获取失败']);
- }
- }
- //公海点击展示客户详情
- public function pollcus_detail()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $param = $this->request->only(['cid']);
- $data = $this->cusdet_noemp($param['cid']);
- View::assign('data', $data);
- return View::fetch();
- }
- //日报页面点击后列表页面
- public function daily_detlist()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $param = $this->request->only(['date', 'state']);
- if (!$request->isAjax()) {
- //$this->screen();
- View::assign('date', $param['date']);
- View::assign('state', $param['state']);
- return View::fetch();
- }
- $org_employee = [$empid];
- $condition[] = ['employee_id', 'in', $org_employee];
- $condition[] = ['transfer_info', 'null', ''];
- $condition[] = ['state', 'not in', Customer::changeState('无效', 'chaos')];
- //$condition[] = ['state', 'in',array_merge(Customer::changeState('已签单', 'chaos'),Customer::changeState('未到访', 'chaos'))];
- $vlst = '确认到店';
- if ($param['state'] == '到访') {
- $condition[] = ['signed_money', '<>', 0];
- $vlst = '确认到店';
- } elseif ($param['state'] == '交定') {
- $condition[] = ['deposit_money', '<>', 0];
- $condition[] = ['signed_money', 'null', ''];
- $vlst = '交定';
- } elseif ($param['state'] == '签单') {
- $condition[] = ['signed_money', '<>', 0];
- $vlst = '签单';
- }
- $order = isset($param['order']) ? $param['order'] : 'id desc';
- $page = intval($param['page']);
- $limit = intval($param['limit']);
- if (empty($param['date'])) return json(['code' => 1, 'msg' => '参数错误']);
- $where[] = ['employee_id', 'in', $org_employee];
- $where[] = ['state', 'in', CustomerVisitLog::changeState($vlst, 'chaos')];
- if (!empty($param['date'])) {
- $newtime = explode('-', $param['date']);
- $where[] = ['confirm_date', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']];
- }
- $vslogid = CustomerVisitLog::where($where)->column('customer_id');
- if ($param['state'] != '线索') {
- $condition[] = ['id', 'in', $vslogid];
- }
- $data = $this->selCustomer($condition, $page, $limit, $order, $root_id);
- $count = count($data);
- return json(['code' => 0, 'data' => $data, 'count' => $count]);
- }
- /**
- * 2022-11-11 业务修改 签单之后可以编辑签单金额,跟进记录增加 业务员修改签单金额为3000元
- */
- private function updateMoney($param)
- {
- $token = $this->get_token();
- $money = $param['money'] ?: 0;
- $orgids = orgSubIds($token['root_org']);
- $where[] = ['id', '=', $param['customer_id']];
- $where[] = ['org_id', 'in', $orgids];
- $cusdata = Customer::where($where)->find();
- $cusdata->signed_money = $money;
- $cusdata->save();
- //Customer::where($where)->update(['signed_money' => $money]);
- if (isset($param['vlogid']) && $param['vlogid']) CustomerVisitLog::where('id', $param['vlogid'])->update(['money' => $money]);
- $token['name'] = Employee::where([['id', '=', $token['employee_id']]])->value('name');
- $time = date('Y-m-d H:i:s');
- //增加跟进记录
- $save = [
- 'customer_id' => $param['customer_id'],
- 'type' => '',
- 'next_contact_date' => date('Y-m-d'),
- 'remark' => '业务员' . $token['name'] . '修改已签单金额为' . $money . '元',
- 'addtime' => $time,
- 'employee_id' => $token['employee_id'],
- 'user_id' => $token['uid'],
- // 'state' => 3,
- 'state' => 1,
- 'org_id' => $token['org_id'],
- 'customer_employee_id' => $cusdata['employee_id'],
- 'customer_org_id' => $token['org_id'],
- 'money' => $money
- ];
- //2022-10-28 增加交定和签单凭证 凭证为图片
- //$deliverySignVoucher = request()->only(['sign_media_id' => '', 'sign_weixin_media' => '', 'sign_img' => '']);
- //$save = $save + $deliverySignVoucher;
- CustomerVisitLog::insertGetId($save);
- //if ($deliverySignVoucher['sign_media_id']) Console::call('download', ['sign']);
- $msg = '转单金额修改成功.';
- if (request()->empcrm['xinjushang'] == 1) $msg = '签单金额修改成功.';
- return json(['code' => 0, 'msg' => $msg, 'data' => $msg]);
- }
- //客服报表
- public function allemp_report()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $param = $this->request->only(['cid']);
- }
- /**
- * 报备页面自定义字段
- */
- public function model_report()
- {
- $id = input('id', '', 'trim');
- $root_id = request()->empcrm->root_id;
- $setting = Setting::where([['name', '=', 'CustomerReportSettingForPc'], ['root_id', '=', $root_id]])->find();
- $where = [
- ['root_id', '=', $root_id],
- ['status', '=', 0]
- ];
- if ($setting && !empty($setting['content'])) {
- $all_content = json_decode($setting['content'], true);
- $field_id = array_column($all_content, 'id');
- $where[] = ['id', 'in', $field_id];
- } else {
- //默认值
- $where[] = ['keyname', 'in', ['name', 'phone', 'sex', 'age_range', 'intention', 'consumption_capacity', 'current_region', 'source_id', 'first', 'add_wechat_type', 'add_wechat_time', 'group_building', 'live_broadcast', 'group_building_date', 'follow', 'point', 'wechat', 'talking_about_single_time', 'community_name', 'unit_number', 'house_location', 'square', 'housetype_arrow', 'deco_style', 'house_type', 'house_status', 'plan_deco_time', 'budget', 'housing_use', 'peripheral_supporting', 'house_structure', 'decoration_mode', 'like_color', 'customer_demand', 'remarks_on_other_house_information', 'family_structure', 'decision_maker', 'family_opinion', 'hourse_price', 'car_price', 'buying_community', 'hobby', 'free_time', 'environmental_requirements', 'design', 'workmanship', 'space_design', 'service_satisfaction', 'quotation_satisfaction', 'program_satisfaction']];
- }
- //获取第二层
- $all = CustomerPortraitField::with(['select'])->where($where)->orderRaw('if(isnull(sort),1,0),sort asc')->select()->toArray();
- $city = Company::where('root_id', $root_id)->value('city');
- if ($setting && !empty($setting['content'])) {
- $all_new = [];
- foreach ($all_content as $c) {
- foreach ($all as $vals) {
- if ($c['id'] == $vals['id']) {
- $all_new[] = $vals;
- if ($c['keyname'] == 'phone') {
- $all_new[] = ['id' => 0, 'name' => '备用手机号1', 'keyname' => 'phone1', 'pid' => $vals['pid'], 'status' => 0, 'is_must' => 1, 'root_id' => $vals['root_id'], 'type' => 2, 'sort' => ''];
- $all_new[] = ['id' => 01, 'name' => '备用手机号2', 'keyname' => 'phone2', 'pid' => $vals['pid'], 'status' => 0, 'is_must' => 1, 'root_id' => $vals['root_id'], 'type' => 2, 'sort' => ''];
- }
- }
- }
- }
- $all = $all_new;
- }
- foreach ($all as &$val) {
- if ($val['keyname'] == 'current_region') {
- $val['select'][] = ['id' => $root_id, 'name' => $city, 'pid' => $val['id']];
- }
- if ($val['keyname'] == 'source_id') {
- $val['select'] = CustomerSource::field('id,source as name')->where('root_id', $root_id)->order('id desc')->select()->toArray();
- }
- if ($val['keyname'] == 'deco_style') {
- $val['select'] = Decostyle::field('id,name')->where([['root_id', '=', $root_id], ['type', '=', 0]])->select()->toArray();
- }
- if (in_array($val['keyname'], ['live_broadcast_business', 'live_broadcast_design', 'live_broadcast_personnel'])) {
- $val['select'] = $this->live_broadcast_personnel();
- }
- if ($val['keyname'] == 'phone') {
- $newadd = [
- ['id' => 0, 'name' => '备用手机号1', 'keyname' => 'phone1', 'pid' => $val['pid'], 'status' => 0, 'is_must' => 1, 'root_id' => $val['root_id'], 'type' => 2, 'sort' => $val['sort'] + 1],
- ['id' => 01, 'name' => '备用手机号2', 'keyname' => 'phone2', 'pid' => $val['pid'], 'status' => 0, 'is_must' => 1, 'root_id' => $val['root_id'], 'type' => 2, 'sort' => $val['sort'] + 1]
- ];
- }
- }
- if (empty($setting) && empty($setting['content'])) {
- $all = array_merge($all, $newadd);
- $sort = array_column($all, 'sort');
- array_multisort($sort, SORT_ASC, $all);
- }
- //获取第一层
- $pid = array_column($all, 'pid');
- $list = CustomerPortraitField::where([['id', 'in', $pid], ['status', '=', 0], ['pid', '=', 0]]);
- if ($setting && !empty($setting['content'])) {
- //如果是添加页面,把自定义字段追加到基本信息child下
- $list = $list->order('id')->limit(1)->select()->toArray();
- } else {
- $list = $list->orderRaw('if(isnull(sort),1,0),sort asc')->select()->toArray();
- }
- //组装数组
- foreach ($list as &$p) {
- foreach ($all as &$item) {
- $item['value'] = '';
- $item['valname'] = '';
- if ($setting && !empty($setting['content'])) {
- $p['child'][] = $item;
- } else {
- if ($p['id'] == $item['pid']) $p['child'][] = $item;
- }
- }
- }
- $cuslog = new CustomerLogic;
- if (isset($id) && !empty($id)) {
- $AllOrgId = orgSubIds($root_id);
- $cusdata = Customer::where([['org_id', 'in', $AllOrgId], ['id', '=', $id]])->find()->toArray();
- if (!empty($cusdata['ext']) && $cusdata['ext'] != 'null') {
- $extdata = json_decode($cusdata['ext'], true);
- if (isset($extdata['ext1'])) {
- $list = $cuslog->old_data_save($cusdata, $list);
- } else {
- $list = $cuslog->new_data_save($cusdata, $extdata, $list);
- }
- } else {
- $list = $cuslog->no_ext_save($cusdata, $list);
- }
- }
- return json(['code' => 0, 'msg' => '获取成功', 'data' => $list]);
- }
- /**
- * 获取直播人员列表(报备自定义页面选择展示)
- */
- private function live_broadcast_personnel()
- {
- $root_id = request()->empcrm->root_id;
- $param = Request()->only(['org_id' => 0, 'name']);
- $path = $param['org_id'] ? Org::where('id', $param['org_id'])->value('path') : $root_id . '-';
- $where[] = ['path', 'like', $path . '%'];
- $empwhere[] = ['e.state', '=', '在职'];
- if (!empty($param['name'])) {
- $empwhere[] = ['e.name', 'like', '%' . $param['name'] . '%'];
- }
- $orgs = Org::with(['employee' => function ($query) use ($empwhere) {
- $query->field('e.id,e.name,user.headimgurl,e.org_id')
- ->alias('e')
- ->where($empwhere)
- ->join('user', 'user.id=e.uid');
- }])->where($where)->order('level asc, id desc')->field('id,name,pid')->select()->toArray();
- $data = [];
- while (!empty($orgs)) {
- $org = array_pop($orgs);
- $childOrg = [
- 'id' => $org['id'],
- 'name' => $org['name'],
- 'pid' => $org['pid'],
- 'designer' => $org['employee'],
- 'designer_num' => count($org['employee'])
- ];
- if (isset($data[$org['id']])) {
- $childOrg['child_org'] = $data[$org['id']]['child_org'];
- $childOrg['designer_num'] += $data[$org['id']]['designer_num'];
- unset($data[$org['id']]);
- }
- $data[$org['pid']]['child_org'][] = $childOrg;
- $data[$org['pid']]['designer_num'] = isset($data[$org['pid']]['designer_num']) ? $data[$org['pid']]['designer_num'] + $childOrg['designer_num'] : $childOrg['designer_num'];
- }
- $data = array_pop($data);
- $result = [
- 'designer' => isset($data['designer']) ? $data['designer'] : [],
- 'child_org' => isset($data['child_org']) ? $data['child_org'] : []
- ];
- return $result;
- }
- /**
- * 客户报备扩展字段添加图片类型
- */
- public function upload_file()
- {
- $ali_oss_bindurl = config('app.ali_oss_bindurl');
- $url = request()->param('file');
- return json(['code' => 0, 'url' => $url]);
- }
- /**
- * 设计师指定
- * 2022-11-08 业务修改, 指派设计师改为指派人员 并且可以指派多人
- * $id 客户id
- * $designerId 指派的人员 1,2,3,4 //为空取消指派人员
- */
- public function reserveDesigner()
- {
- $request = request();
- $param = $this->request->only(['id', 'designerId', 'assign_type']);
- $id = $param['id'];
- $designerId = $param['designerId'];
- $root_id = $request->empcrm->root_id;
- if (isset($request->empcrm->is_manager) && $request->empcrm->is_manager) {
- $customer = Customer::where([['id', '=', $id], ['org_id', 'in', orgSubIds($request->empcrm['root_id'])]])->find();
- } else {
- $customer = Customer::where(['id' => $id, 'employee_id' => $request->empcrm['id']])->find();
- }
- if (empty($customer)) return json(['code' => 1, 'msg' => '客户不存在']);
- if (empty($designerId)) {
- $customer->designer_id = NULL;
- $customer->assigned_personnel = NULL;
- $customer->save();
- return json(['code' => 0, 'msg' => '取消指派成功!']);
- }
- $designerId = explode(',', $designerId);
- $designer = Employee::where([
- ['id', 'in', $designerId], ['root_id', '=', $root_id], ['state', '=', '在职']
- ])->column('org_id', 'id');
- if (empty($designer)) return json(['code' => 1, 'msg' => '指派人员不存在']);
- $orgs = Org::where([['id', 'in', array_values($designer)]])->column('org_type', 'id');
- //第一个选择的设计师
- $selectDesigner = [];
- foreach ($designerId as $k => $v) {
- if (empty($designer[$v]) || $orgs[$designer[$v]] !== 2) continue;
- $selectDesigner[] = $v;
- }
- $designer_id = isset($selectDesigner[0]) ? $selectDesigner[0] : null;
- // 检测是否有设计师已指派满了
- $limitNum = Setting::where(['root_id' => $request->empcrm['root_org'], 'name' => 'designerNum'])->value('content');
- if ($limitNum != 0) {
- //获取当月时间跨度
- $start_time = date('Y-m') . '-01 00:00:00';
- $end_time = date('Y-m-d', strtotime("$start_time +1 month -1 day")) . ' 23:59:59';
- $where[] = ['addtime', 'between', [strtotime($start_time), strtotime($end_time)]];
- $where[] = ['employee_id', 'in', $selectDesigner];
- $had = CustomerSharing::field('employee_id,count(customer_id) as num')->group('employee_id')->where($where)->having("num >= $limitNum")->select();
- // var_dump($had->toArray());
- // exit;
- if (!$had->isEmpty()) return json(['code' => 1, 'msg' => '指派的设计师已无指派名额,请刷新后再试']);
- }
- // 检测部门是否是设计师
- // $orgType = Org::where(['id' => $designer->org_id])->value('org_type');
- // if ($orgType != 2) return json(['code' => 1, 'msg' => '设计师不存在']);
- $customer->designer_id = $designer_id;
- $customer->assigned_personnel = implode(',', array_keys($designer));
- $customer->assign_time = date('Y-m-d H:i:s');
- if (!empty($param['assign_type']) && empty($customer->assign_type)) $customer->assign_type = $param['assign_type'];
- $customer->save();
- // 更新预约未确定的客户所属设计师id
- CustomersSubscribe::where(['customer_id' => $customer->id, 'state' => 0])->update(['designer_id' => $designer_id]);
- $all_assign_type = ['yixiang' => '意向客户指派', 'liangfang' => '量房客户指派', 'daodian' => '到店客户指派'];
- if (!empty($all_assign_type[$customer->assign_type])) {
- $empname_arr = Employee::where([['id', 'in', implode(',', array_keys($designer))], ['root_id', '=', $root_id]])->column('name');
- $remark = $all_assign_type[$customer->assign_type] . '指派至员工' . implode(',', $empname_arr);
- $visit_Log = [
- 'customer_id' => $id,
- 'type' => '',
- 'remark' => $remark,
- 'employee_id' => $request->empcrm->id,
- 'user_id' => $request->empcrm->uid,
- 'state' => 1,
- 'next_contact_date' => date('Y-m-d'),
- 'customer_employee_id' => $customer->employee_id,
- 'customer_org_id' => $customer->org_id,
- 'org_id' => $customer->org_id
- ];
- CustomerVisitLog::create($visit_Log);
- }
- return json(['code' => 0, 'msg' => '指派成功!']);
- }
- /*
- * 审核人员
- */
- public function get_person()
- {
- $ids = input('id', '');
- $root_id = request()->empcrm->root_id;
- $type = input('type', 0);
- //树形
- if ($type == 1) {
- //所有员工
- $w1[] = ['root_id', '=', $root_id];
- $w1[] = ['state', '=', '在职'];
- $w1[] = ['uid', '<>', 0];
- $w1[] = ['org_id', '>', 0];
- $w1[] = ['org_id', '<>', $root_id];
- $w1[] = ['id', '<>', request()->empcrm->id];
- $employee = Employee::where($w1)->field('id value,name,org_id')->select()->toArray();
- if ($ids) {
- $ids = explode(',', $ids);
- foreach ($employee as $k => &$v) {
- $v['selected'] = in_array($v['value'], $ids);
- }
- } else {
- foreach ($employee as $k => &$v) {
- $v['selected'] = false;
- }
- }
- // 查询限定数量
- // $limitNum = (int)Setting::where(['root_id' => $root_id, 'name' => 'designerNum'])->value('content');
- // // 查询员工已制定的人数
- // if ($limitNum != 0) {
- // //获取当月时间跨度
- // $start_time = date('Y-m').'-01 00:00:00';
- // $end_time = date('Y-m-d',strtotime("$start_time +1 month -1 day")).' 23:59:59';
- // $where_d[] = ['addtime', 'between', [strtotime($start_time),strtotime($end_time)]];
- // $where_d[] = ['root_id', '=', $root_id];
- // $empShareNum = CustomerSharing::where($where_d)->group('employee_id')->column('count(customer_id)', 'employee_id');
- // foreach ($employee as &$e) {
- // if (isset($empShareNum[$e['value']]) && $empShareNum[$e['value']] >= $limitNum) $e['disabled'] = true;
- // }
- // }
- $persons = [];
- foreach ($employee as $k => $v1) {
- $persons[$v1['org_id']][] = $v1;
- }
- $where = [
- ['path', 'like', $root_id . '-%'],
- ['status', '=', 1]
- ];
- $allnodes = Org::where($where)->field('id value,id,pid,name')->order('level asc, id asc')->select()->toArray();
- $tree = $this->tree($allnodes, 0, $persons);
- return json($tree);
- }
- $w[] = ['root_id', '=', $root_id];
- $w[] = ['state', '=', '在职'];
- $w[] = ['role', '=', '领导'];
- $w[] = ['uid', '<>', 0];
- $employee = Employee::where($w)->field('id value,name')->select()->toArray();
- if ($ids) {
- $ids = explode(',', $ids);
- foreach ($employee as $k => &$v) {
- $v['selected'] = in_array($v['value'], $ids);
- }
- } else {
- foreach ($employee as $k => &$v) {
- $v['selected'] = false;
- }
- }
- return json($employee);
- }
- public function tree($data, $pid = 0, $persons)
- {
- $new_arr = [];
- foreach ($data as $k => $v) {
- if ($v['pid'] == $pid) {
- $persions = isset($persons[$v['id']]) ? $persons[$v['id']] : [];
- $children = $this->tree($data, $v['id'], $persons);
- $v['children'] = array_merge_recursive($children, $persions);
- //if (empty($v['children'])) $v['disabled'] = true;
- if (empty($v['children'])) continue;
- $new_arr[] = $v;
- }
- }
- return $new_arr;
- }
- //获取客户所有扩展字段
- public function get_all_portrait_field()
- {
- $root_id = request()->empcrm->root_id;
- $w[] = ['root_id', '=', $root_id];
- $param = $this->request->only(['id']);
- $id = $param['id'];
- $AllOrgId = orgSubIds($root_id);
- $cusdata = Customer::where([['org_id', 'in', $AllOrgId], ['id', '=', $id]])->find();
- if (empty($cusdata)) return json(['code' => 1, 'msg' => '客户不存在']);
- $list = CustomerPortraitField::where([['root_id', '=', $root_id], ['pid', '=', 0], ['status', '=', 0]])->orderRaw('if(isnull(sort),1,0),sort asc')->select()->toArray();
- $city = Company::where('root_id', $root_id)->value('city');
- foreach ($list as $key => $val) {
- $child = CustomerPortraitField::with(['select'])->where([['root_id', '=', $root_id], ['pid', '<>', 0], ['pid', '=', $val['id']], ['status', '=', 0]])->orderRaw('if(isnull(sort),1,0),sort asc')->select()->toArray();
- foreach ($child as $k => $v) {
- if ($v['keyname'] == 'current_region') {
- $child[$k]['select'][] = ['id' => $root_id, 'name' => $city, 'pid' => $v['id']];
- }
- if ($v['keyname'] == 'source_id') {
- $soudata = CustomerSource::field('id,source as name')->where('root_id', $root_id)->select()->toArray();
- $child[$k]['select'] = $soudata;
- }
- if ($v['keyname'] == 'deco_style') {
- $decostyles = Decostyle::field('id,name')->where([['root_id', '=', $root_id], ['type', '=', 0]])->select()->toArray();
- $child[$k]['select'] = $decostyles;
- }
- $child[$k]['value'] = '';
- $child[$k]['valname'] = '';
- }
- $list[$key]['child'] = $child;
- }
- $new = $list;
- $cuslog = new CustomerLogic;
- if (!empty($cusdata['ext']) && $cusdata['ext'] != 'null') {
- $extdata = json_decode($cusdata['ext'], true);
- if (isset($extdata['ext1'])) {
- $new = $cuslog->old_data_save($cusdata, $list);
- } else {
- $new = $cuslog->new_data_save($cusdata, $extdata, $list);
- }
- }
- return json(['code' => 0, 'data' => $new, 'msg' => '获取成功']);
- }
- /**
- * 客户池展示字段
- */
- public function poolFields($type = 6)
- {
- // 管理层统计
- $type3 = [
- 'name' => ['name' => '员工姓名', 'show' => 1, 'width' => 100, 'sort' => 1],
- 'customer_count' => ['name' => '客户数量', 'show' => 1, 'width' => 100, 'sort' => 2],
- 'return_visit_count' => ['name' => '待回访客户数', 'show' => 1, 'width' => 150, 'sort' => 3],
- 'no_assigned_personnel' => ['name' => '待指派客户数', 'show' => 1, 'width' => 150, 'sort' => 4],
- 'no_visit_3' => ['name' => '3天内未跟进客户数', 'show' => 1, 'width' => 225, 'sort' => 5],
- 'no_visit_7' => ['name' => '7天内未跟进客户数', 'show' => 1, 'width' => 225, 'sort' => 6],
- 'no_visit_15' => ['name' => '15天内未跟进客户数', 'show' => 1, 'width' => 225, 'sort' => 7],
- 'no_visit_30' => ['name' => '一个月内未跟进客户数', 'show' => 1, 'width' => 250, 'sort' => 8],
- 'no_visit_31' => ['name' => '一个月以上未跟进客户数', 'show' => 1, 'width' => 275, 'sort' => 9],
- 'valid_count' => ['name' => '有效客户', 'show' => 1, 'width' => 100, 'sort' => 10],
- 'valid_grawth' => ['name' => '有效率', 'show' => 1, 'width' => 75, 'sort' => 11],
- 'phone_count' => ['name' => '电话数量', 'show' => 1, 'width' => 100, 'sort' => 12],
- 'on_phone_count' => ['name' => '接通数量', 'show' => 1, 'width' => 100, 'sort' => 13],
- 'on_phone_grawth' => ['name' => '接通率', 'show' => 1, 'width' => 75, 'sort' => 14],
- 'off_phone_count' => ['name' => '未接通', 'show' => 1, 'width' => 75, 'sort' => 15],
- 'avg_phone_time' => ['name' => '平均通话时长', 'show' => 1, 'width' => 150, 'sort' => 16],
- 'add_wechat_count' => ['name' => '加微数量', 'show' => 1, 'width' => 100, 'sort' => 17],
- 'add_wechat_grawth' => ['name' => '加微率', 'show' => 1, 'width' => 75, 'sort' => 18],
- 'room' => ['name' => '量房客户数', 'show' => 1, 'width' => 125, 'sort' => 19],
- 'room_grawth' => ['name' => '量房率', 'show' => 1, 'width' => 75, 'sort' => 20],
- 'store1' => ['name' => '一次到店客户数', 'show' => 1, 'width' => 175, 'sort' => 21],
- 'store1_grawth' => ['name' => '一次到店率', 'show' => 1, 'width' => 125, 'sort' => 22],
- 'store2' => ['name' => '二次到店客户数', 'show' => 1, 'width' => 175, 'sort' => 23],
- 'store2_grawth' => ['name' => '二次到店率', 'show' => 1, 'width' => 125, 'sort' => 24],
- 'store3' => ['name' => '三次及以上到店客户数', 'show' => 1, 'width' => 250, 'sort' => 25],
- 'store3_grawth' => ['name' => '三次及以上到店率', 'show' => 1, 'width' => 200, 'sort' => 26],
- 'store1_dep' => ['name' => '一次到店签单客户数', 'show' => 1, 'width' => 225, 'sort' => 27],
- 'store1_dep_grawth' => ['name' => '一次到店签单率', 'show' => 1, 'width' => 175, 'sort' => 28],
- 'store2_dep' => ['name' => '二次到店签单数', 'show' => 1, 'width' => 175, 'sort' => 29],
- 'store2_dep_grawth' => ['name' => '二次到店签单率', 'show' => 1, 'width' => 175, 'sort' => 30],
- 'store3_dep' => ['name' => '三次及以上到店签单数', 'show' => 1, 'width' => 250, 'sort' => 31],
- 'store3_dep_grawth' => ['name' => '三次及以上到店签单率', 'show' => 1, 'width' => 250, 'sort' => 32],
- 'dep' => ['name' => '总签单数', 'show' => 1, 'width' => 100, 'sort' => 33],
- 'dep_grawth' => ['name' => '总签单率', 'show' => 1, 'width' => 100, 'sort' => 34],
- 'drawing_date' => ['name' => '出方案客户数', 'show' => 1, 'width' => 150, 'sort' => 35],
- 'sign_count' => ['name' => '转单客户', 'show' => 1, 'width' => 100, 'sort' => 36],
- 'sign_grawth' => ['name' => '转单率', 'show' => 1, 'width' => 75, 'sort' => 37],
- // 'area'=> ['name'=> '面积分布', 'show'=> 1, 'width'=> 100, 'sort'=> 38],
- 'square_0_80' => ['name' => '80以下', 'show' => 1, 'width' => 100, 'parent' => 'area', 'sort' => 38],
- 'square_80_100' => ['name' => '81-100', 'show' => 1, 'width' => 100, 'parent' => 'area', 'sort' => 39],
- 'square_100_120' => ['name' => '100-120', 'show' => 1, 'width' => 100, 'parent' => 'area', 'sort' => 40],
- 'square_120_200' => ['name' => '121-200', 'show' => 1, 'width' => 100, 'parent' => 'area', 'sort' => 41],
- 'square_200_500' => ['name' => '200-500', 'show' => 1, 'width' => 100, 'parent' => 'area', 'sort' => 42],
- 'square_500' => ['name' => '500以上', 'show' => 1, 'width' => 100, 'parent' => 'area', 'sort' => 43],
- 'existing_homes_count' => ['name' => '现房数量', 'show' => 1, 'width' => 100, 'sort' => 44],
- 'forward_housing_count' => ['name' => '期房数量', 'show' => 1, 'width' => 100, 'sort' => 45],
- ];
- // 客户统计列表
- $type4 = [
- 'name' => ['name' => '客户姓名', 'show' => 1, 'width' => 100, 'sort' => 1],
- 'phone' => ['name' => '手机号', 'show' => 1, 'width' => 125, 'sort' => 2],
- 'community_name' => ['name' => '小区', 'show' => 1, 'width' => 150, 'sort' => 3],
- 'house_type' => ['name' => '房屋类型', 'show' => 1, 'width' => 100, 'sort' => 4],
- 'house_delivery_time' => ['name' => '交房时间', 'show' => 1, 'width' => 100, 'sort' => 5],
- 'square' => ['name' => '面积', 'show' => 1, 'width' => 75, 'sort' => 6],
- 'org_name' => ['name' => '部门', 'show' => 1, 'width' => 120, 'sort' => 7],
- 'employee_name' => ['name' => '业务员', 'show' => 1, 'width' => 90, 'sort' => 8],
- 'addtime' => ['name' => '添加时间', 'show' => 1, 'width' => 170, 'sort' => 9],
- 'source_name' => ['name' => '客户来源', 'show' => 1, 'width' => 100, 'sort' => 10],
- 'assigned_personal_manager' => ['name' => '指派客户经理', 'show' => 1, 'width' => 150, 'sort' => 11],
- 'assigned_personal_designer_org' => ['name' => '指派设计部', 'show' => 1, 'width' => 125, 'sort' => 12],
- 'assigned_personal_designer' => ['name' => '指派设计师', 'show' => 1, 'width' => 125, 'sort' => 13],
- 'no_visit_day' => ['name' => '未跟踪天数', 'show' => 1, 'width' => 125, 'sort' => 14],
- 'wechat' => ['name' => '是否加微信', 'show' => 1, 'width' => 125, 'sort' => 15],
- 'add_wechat_type' => ['name' => '加微类型', 'show' => 1, 'width' => 100, 'sort' => 16],
- 'add_wechat_time' => ['name' => '加微时间', 'show' => 1, 'width' => 120, 'sort' => 17],
- 'group_building_date' => ['name' => '建群时间', 'show' => 1, 'width' => 250, 'sort' => 18],
- 'is_liangfang' => ['name' => '是否量房', 'show' => 1, 'width' => 125, 'sort' => 19],
- 'liangfang_date' => ['name' => '量房时间', 'show' => 1, 'width' => 170, 'sort' => 20],
- 'once_liangfang_days' => ['name' => '量房周期', 'show' => 1, 'width' => 120, 'sort' => 21],
- 'plan_issuing_date' => ['name' => '计划出初步方案时间', 'show' => 1, 'width' => 225, 'sort' => 22],
- 'once_daodian' => ['name' => '是否一次到店', 'show' => 1, 'width' => 150, 'sort' => 23],
- 'daodian_date' => ['name' => '一次到店时间', 'show' => 1, 'width' => 170, 'sort' => 24],
- 'once_daodian_shop' => ['name' => '一次所到店面', 'show' => 1, 'width' => 150, 'sort' => 25],
- 'once_daodian_days' => ['name' => '一次到店周期', 'show' => 1, 'width' => 150, 'sort' => 26],
- 'liangfang_daodian_days' => ['name' => '量房到店周期', 'show' => 1, 'width' => 150, 'sort' => 27],
- 'twice_daodian_date' => ['name' => '二次到店时间', 'show' => 1, 'width' => 170, 'sort' => 28],
- 'twice_daodian_shop' => ['name' => '二次所到店面', 'show' => 1, 'width' => 150, 'sort' => 29],
- 'twice_daodian_days' => ['name' => '二次到店周期', 'show' => 1, 'width' => 150, 'sort' => 30],
- 'is_deposit' => ['name' => '是否交定/签单', 'show' => 1, 'width' => 170, 'sort' => 31],
- 'deposit_date' => ['name' => '交定/签单时间', 'show' => 1, 'width' => 170, 'sort' => 32],
- 'drawing_date' => ['name' => '出图时间', 'show' => 1, 'width' => 100, 'sort' => 33],
- 'sign_date' => ['name' => '转单时间', 'show' => 1, 'width' => 170, 'sort' => 34],
- 'sign_days' => ['name' => '交定转单周期', 'show' => 1, 'width' => 150, 'sort' => 35],
- 'signed_money_data' => ['name' => '合同金额(元)', 'show' => 1, 'width' => 125, 'sort' => 36],
- 'first_visit_no_sign' => ['name' => '一次到店未签单时间', 'show' => 1, 'width' => 200, 'sort' => 37],
- 'manager_visit_times' => ['name' => '客户经理回访次数', 'show' => 1, 'width' => 200, 'sort' => 38],
- 'manager_visit_cycle' => ['name' => '客户经理回访周期', 'show' => 1, 'width' => 100, 'sort' => 39],
- 'employee_visit_times' => ['name' => '经理回访次数', 'show' => 1, 'width' => 150, 'sort' => 40],
- 'employee_visit_cycle' => ['name' => '经理回访平均周期', 'show' => 1, 'width' => 200, 'sort' => 41],
- 'invalid' => ['name' => '无效客户', 'show' => 1, 'width' => 100, 'sort' => 42],
- 'invalid_remark' => ['name' => '无效原因', 'show' => 1, 'width' => 100, 'sort' => 43],
- 'died' => ['name' => '是否死单', 'show' => 1, 'width' => 100, 'sort' => 44],
- 'customer_type' => ['name' => '客户种类', 'show' => 1, 'width' => 100, 'sort' => 11],
- ];
- // 客户池字段
- $field6 = [
- 'name' => ['name' => '客户姓名', 'show' => 1, 'width' => 120, 'sort' => 1],
- 'phone' => ['name' => '手机号', 'show' => 1, 'width' => 100, 'sort' => 2],
- 'employee_name' => ['name' => '所属员工', 'show' => 1, 'width' => 120, 'sort' => 3],
- 'designer_name' => ['name' => '设计师', 'show' => 1, 'width' => 120, 'sort' => 4],
- 'source_name' => ['name' => '来源渠道', 'show' => 1, 'width' => 120, 'sort' => 5],
- 'add_wechat_time' => ['name' => '加微时间', 'show' => 1, 'width' => 120, 'sort' => 6],
- 'customer_type' => ['name' => '客户种类', 'show' => 1, 'width' => 120, 'sort' => 7],
- 'community_name' => ['name' => '小区名称', 'show' => 1, 'width' => 160, 'sort' => 8],
- 'level' => ['name' => '重要', 'show' => 1, 'width' => 160, 'sort' => 9],
- 'sign_time' => ['name' => '报名时间', 'show' => 1, 'width' => 160, 'sort' => 10],
- 'house_delivery_time' => ['name' => '交房时间', 'show' => 1, 'width' => 120, 'sort' => 11],
- 'state' => ['name' => '状态', 'show' => 1, 'width' => 120, 'sort' => 12],
- 'square' => ['name' => '面积', 'show' => 1, 'width' => 100, 'sort' => 13],
- 'last_contact_date' => ['name' => '跟进时间', 'show' => 1, 'width' => 160, 'sort' => 14],
- 'revisit_time' => ['name' => '下次回访时间', 'show' => 1, 'width' => 160, 'sort' => 15],
- 'addtime' => ['name' => '添加时间', 'show' => 1, 'width' => 160, 'sort' => 16],
- 'protected_to' => ['name' => '保护至', 'show' => 1, 'width' => 160, 'sort' => 17],
- 'visit_log_count' => ['name' => '预约次数', 'show' => 1, 'width' => 160, 'sort' => 18],
- 'activity_frequency_count' => ['name' => '参加活动次数', 'show' => 1, 'width' => 160, 'sort' => 19],
- 'lognum' => ['name' => '跟进次数', 'show' => 1, 'width' => 150, 'sort' => 20],
- 'no_visit_day' => ['name' => '未跟进天数', 'show' => 1, 'width' => 150, 'sort' => 21],
- 'subscribe_date' => ['name' => '预计见面时间', 'show' => 1, 'width' => 150, 'sort' => 22],
- 'first_liangfang' => ['name' => '首次量房时间', 'show' => 1, 'width' => 150, 'sort' => 23],
- 'fisttime' => ['name' => '首次到店时间', 'show' => 1, 'width' => 150, 'sort' => 24],
- 'wuyou_money' => ['name' => '无忧金额', 'show' => 1, 'width' => 120, 'sort' => 24],
- 'jiaoding_time' => ['name' => '签单时间', 'show' => 1, 'width' => 120, 'sort' => 25],
- 'jiaoding_money' => ['name' => '签单金额', 'show' => 1, 'width' => 120, 'sort' => 26],
- 'estimated_sign_money' => ['name' => '定金签单金额', 'show' => 1, 'width' => 120, 'sort' => 26],
- 'qiandan_time' => ['name' => '转单时间', 'show' => 1, 'width' => 120, 'sort' => 27],
- 'qiandan_money' => ['name' => '转单金额', 'show' => 1, 'width' => 120, 'sort' => 28],
- 'assign_list' => ['name' => '指派客户经理', 'show' => 1, 'width' => 160, 'sort' => 29],
- 'emporg' => ['name' => '业务员部门', 'show' => 1, 'width' => 160, 'sort' => 30],
- 'designer_org' => ['name' => '设计师部门', 'show' => 1, 'width' => 150, 'sort' => 31]
- ];
- // 员工统计
- $field7 = [
- 'name' => ['name' => '业务员名称', 'show' => 1, 'width' => 100, 'parent' => 'name', 'sort' => 1],
- 'org_name' => ['name' => '业务员所在部门', 'show' => 1, 'width' => 130, 'parent' => 'org_name', 'sort' => 2],
- 'customer_count' => ['name' => '线索量', 'show' => 1, 'width' => 120, 'parent' => 'customer_data', 'sort' => 3],
- 'connecting_count' => ['name' => '拨打电话数量', 'show' => 1, 'width' => 120, 'parent' => 'customer_data', 'sort' => 4],
- 'connecting_capacity' => ['name' => '接通数量', 'show' => 1, 'width' => 100, 'parent' => 'call_count', 'sort' => 5],
- 'connecting_capacity_grawth' => ['name' => '接通率', 'show' => 1, 'width' => 100, 'parent' => 'call_count', 'sort' => 6],
- 'connecting_no' => ['name' => '未接通数量', 'show' => 1, 'width' => 100, 'parent' => 'call_count', 'sort' => 7],
- 'phone_time' => ['name' => '总通话时长(分)', 'show' => 1, 'width' => 130, 'parent' => 'call_count', 'sort' => 8],
- 'average_duration' => ['name' => '平均通话时长(秒)', 'show' => 1, 'width' => 140, 'parent' => 'call_count', 'sort' => 9],
- 'follow_customer_count' => ['name' => '跟进客户数', 'show' => 1, 'width' => 100, 'parent' => 'customer_data', 'sort' => 10],
- 'add_wechat_count' => ['name' => '加微客户数量', 'show' => 1, 'width' => 120, 'parent' => 'behavior', 'sort' => 11],
- 'not_sure_count' => ['name' => '待确认客户数', 'show' => 1, 'width' => 120, 'parent' => 'behavior', 'sort' => 12],
- 'add_wechat_count_grawth' => ['name' => '加微率', 'show' => 1, 'width' => 100, 'parent' => 'behavior', 'sort' => 13],
- 'valid_count' => ['name' => '有效线索数', 'show' => 1, 'width' => 100, 'parent' => 'customer', 'sort' => 14],
- 'valid_grawth' => ['name' => '有效线索率(%)', 'show' => 1, 'width' => 100, 'parent' => 'customer', 'sort' => 15],
- 'valid_num' => ['name' => '有效客户数', 'show' => 1, 'width' => 100, 'parent' => 'customer', 'sort' => 15],
- //'not_following_up_count'=> ['name'=> '未跟进客户数', 'show'=> 1, 'width'=> 120, 'parent'=> 'customer_data', 'sort'=> 16],
- 'yixiang_assigned_count' => ['name' => '意向分派客户数', 'show' => 1, 'width' => 130, 'parent' => 'customer_data', 'sort' => 17],
- 'daodian_assigned_count' => ['name' => '到店分派客户数', 'show' => 1, 'width' => 130, 'parent' => 'customer_data', 'sort' => 18],
- 'measuring_assigned_count' => ['name' => '量房分派客户数', 'show' => 1, 'width' => 130, 'parent' => 'customer_data', 'sort' => 19],
- 'number_of_explanations' => ['name' => '智慧屏讲解次数', 'show' => 1, 'width' => 130, 'parent' => 'behavior', 'sort' => 20],
- 'measuring_room_customer' => ['name' => '量房客户数', 'show' => 1, 'width' => 100, 'parent' => 'customer', 'sort' => 21],
- 'measuring_room_zhouqi' => ['name' => '平均量房周期(天)', 'show' => 1, 'width' => 150, 'parent' => 'customer', 'sort' => 22],
- 'measuring_room_grawth' => ['name' => '量房率', 'show' => 1, 'width' => 100, 'parent' => 'customer', 'sort' => 23],
- 'to_the_store' => ['name' => '到店数', 'show' => 1, 'width' => 130, 'parent' => 'daodian_count', 'sort' => 24],
- 'strtore_2v1' => ['name' => '二访比率', 'show' => 1, 'width' => 130, 'parent' => 'daodian_count', 'sort' => 25],
- 'strtore_avg_days_1' => ['name' => '平均到店周期(天)', 'show' => 1, 'width' => 150, 'parent' => 'daodian_avg', 'sort' => 25],
- 'strtore_customer_count' => ['name' => '到店客户数', 'show' => 1, 'width' => 100, 'parent' => 'daodian_count', 'sort' => 26],
- 'strtore_customer_grawth' => ['name' => '到店率', 'show' => 1, 'width' => 100, 'parent' => 'daodian_grawth', 'sort' => 27],
- 'deposit_count' => ['name' => '签单客户数量', 'show' => 1, 'width' => 100, 'parent' => 'customer_data', 'sort' => 28],
- 'deposit_avg' => ['name' => '签单率', 'show' => 1, 'width' => 150, 'parent' => 'daodian_deposit', 'sort' => 29],
- 'deposit_avg_days' => ['name' => '平均签单周期', 'show' => 1, 'width' => 120, 'parent' => 'customer', 'sort' => 30],
- 'sign_count' => ['name' => '转单客户数量', 'show' => 1, 'width' => 100, 'parent' => 'customer', 'sort' => 31],
- 'sign_count_grawth' => ['name' => '转单率', 'show' => 1, 'width' => 100, 'parent' => 'customer', 'sort' => 32],
- 'avg_visit_count' => ['name' => '平均跟进次数', 'show' => 1, 'width' => 120, 'parent' => 'customer', 'sort' => 33],
- 'signed_money' => ['name' => '平均合同金额(元)', 'show' => 1, 'width' => 150, 'parent' => 'customer', 'sort' => 34],
- 'signed_money_all' => ['name' => '合同总额(元)', 'show' => 1, 'width' => 150, 'parent' => 'customer', 'sort' => 35],
- 'invalid_customer_count' => ['name' => '无效客户数', 'show' => 1, 'width' => 100, 'parent' => 'behavior', 'sort' => 35],
- 'square' => ['name' => '面积', 'show' => 1, 'width' => 100, 'parent' => 'area', 'sort' => 36],
- 'house_status' => ['name' => '房屋状态', 'show' => 1, 'width' => 100, 'parent' => 'area', 'sort' => 37],
- 'house_type' => ['name' => '房屋类型', 'show' => 1, 'width' => 100, 'parent' => 'area', 'sort' => 38],
- 'shares_count' => ['name' => '调用内容数量', 'show' => 1, 'width' => 110, 'parent' => 'behavior', 'sort' => 39],
- 'assessment_times' => ['name' => '考核次数', 'show' => 1, 'width' => 100, 'parent' => 'exam_data', 'sort' => 40],
- 'number_of_passes' => ['name' => '通过次数', 'show' => 1, 'width' => 100, 'parent' => 'exam_data', 'sort' => 41],
- ];
- // 指派客户字段
- $field8 = [
- 'name' => ['name' => '客户姓名', 'show' => 1, 'width' => 120, 'sort' => 1],
- 'phone' => ['name' => '手机号', 'show' => 1, 'width' => 100, 'sort' => 2],
- 'employee_name' => ['name' => '所属员工', 'show' => 1, 'width' => 120, 'sort' => 3],
- 'designer_name' => ['name' => '设计师', 'show' => 1, 'width' => 120, 'sort' => 4],
- 'source_name' => ['name' => '来源渠道', 'show' => 1, 'width' => 120, 'sort' => 5],
- 'community_name' => ['name' => '小区名称', 'show' => 1, 'width' => 160, 'sort' => 6],
- 'level' => ['name' => '重要', 'show' => 1, 'width' => 160, 'sort' => 7],
- 'square' => ['name' => '面积', 'show' => 1, 'width' => 100, 'sort' => 8],
- 'state' => ['name' => '状态', 'show' => 1, 'width' => 120, 'sort' => 9],
- 'assign_list' => ['name' => '指派客户经理', 'show' => 1, 'width' => 160, 'sort' => 10],
- 'assign_time' => ['name' => '指派时间', 'show' => 1, 'width' => 160, 'sort' => 11],
- 'assign_org' => ['name' => '指派部门', 'show' => 1, 'width' => 160, 'sort' => 12],
- 'assign_type_name' => ['name' => '指派类型', 'show' => 1, 'width' => 160, 'sort' => 13],
- 'last_contact_date' => ['name' => '跟进时间', 'show' => 1, 'width' => 160, 'sort' => 14],
- 'no_visit_day' => ['name' => '未跟进天数', 'show' => 1, 'width' => 150, 'sort' => 15],
- 'liangfang_time' => ['name' => '量房时间', 'show' => 1, 'width' => 150, 'sort' => 16],
- 'daodian_time' => ['name' => '到店时间', 'show' => 1, 'width' => 150, 'sort' => 17],
- 'jiaoding_time' => ['name' => '签单时间', 'show' => 1, 'width' => 120, 'sort' => 18],
- 'jiaoding_money' => ['name' => '签单金额', 'show' => 1, 'width' => 120, 'sort' => 19],
- 'qiandan_time' => ['name' => '转单时间', 'show' => 1, 'width' => 120, 'sort' => 20],
- 'qiandan_money' => ['name' => '转单金额', 'show' => 1, 'width' => 120, 'sort' => 21]
- ];
- // 到店客户字段
- $field9 = [
- 'name' => ['name' => '客户姓名', 'show' => 1, 'width' => 120, 'sort' => 1],
- 'phone' => ['name' => '手机号', 'show' => 1, 'width' => 100, 'sort' => 2],
- 'employee_name' => ['name' => '所属员工', 'show' => 1, 'width' => 120, 'sort' => 3],
- 'designer_name' => ['name' => '设计师', 'show' => 1, 'width' => 120, 'sort' => 4],
- 'source_name' => ['name' => '来源渠道', 'show' => 1, 'width' => 120, 'sort' => 5],
- 'community_name' => ['name' => '小区名称', 'show' => 1, 'width' => 160, 'sort' => 6],
- 'level' => ['name' => '重要', 'show' => 1, 'width' => 160, 'sort' => 7],
- 'square' => ['name' => '面积', 'show' => 1, 'width' => 100, 'sort' => 8],
- 'state' => ['name' => '状态', 'show' => 1, 'width' => 120, 'sort' => 9],
- 'assign_list' => ['name' => '指派客户经理', 'show' => 1, 'width' => 160, 'sort' => 10],
- 'assign_time' => ['name' => '指派时间', 'show' => 1, 'width' => 160, 'sort' => 11],
- 'assign_org' => ['name' => '指派部门', 'show' => 1, 'width' => 160, 'sort' => 12],
- 'assign_type_name' => ['name' => '指派类型', 'show' => 1, 'width' => 160, 'sort' => 13],
- 'last_contact_date' => ['name' => '跟进时间', 'show' => 1, 'width' => 160, 'sort' => 14],
- 'no_visit_day' => ['name' => '未跟进天数', 'show' => 1, 'width' => 150, 'sort' => 15],
- 'liangfang_time' => ['name' => '量房时间', 'show' => 1, 'width' => 150, 'sort' => 16],
- 'daodian_time' => ['name' => '到店时间', 'show' => 1, 'width' => 150, 'sort' => 17],
- 'jiaoding_time' => ['name' => '签单时间', 'show' => 1, 'width' => 120, 'sort' => 18],
- 'jiaoding_money' => ['name' => '签单金额', 'show' => 1, 'width' => 120, 'sort' => 19],
- 'qiandan_time' => ['name' => '转单时间', 'show' => 1, 'width' => 120, 'sort' => 20],
- 'qiandan_money' => ['name' => '转单金额', 'show' => 1, 'width' => 120, 'sort' => 21],
- 'customer_type' => ['name' => '客户种类', 'show' => 1, 'width' => 120, 'sort' => 22],
- ];
- // 待回访客户字段
- $field10 = [
- 'name' => ['name' => '客户姓名', 'show' => 1, 'width' => 120, 'sort' => 1],
- 'phone' => ['name' => '手机号', 'show' => 1, 'width' => 100, 'sort' => 2],
- 'employee_name' => ['name' => '所属员工', 'show' => 1, 'width' => 120, 'sort' => 3],
- 'designer_name' => ['name' => '设计师', 'show' => 1, 'width' => 120, 'sort' => 4],
- 'source_name' => ['name' => '来源渠道', 'show' => 1, 'width' => 120, 'sort' => 5],
- 'community_name' => ['name' => '小区名称', 'show' => 1, 'width' => 160, 'sort' => 6],
- 'level' => ['name' => '重要', 'show' => 1, 'width' => 160, 'sort' => 7],
- 'square' => ['name' => '面积', 'show' => 1, 'width' => 100, 'sort' => 8],
- 'last_contact_date' => ['name' => '上次回访', 'show' => 1, 'width' => 100, 'sort' => 9],
- 'jiange_day' => ['name' => '间隔', 'show' => 1, 'width' => 100, 'sort' => 10]
- ];
- $fileds12 = [
- 'name' => ['name' => '客户姓名', 'show' => 1, 'width' => 100, 'sort' => 1],
- 'telephone' => ['name' => '手机号', 'show' => 1, 'width' => 100, 'sort' => 2],
- 'weixin' => ['name' => '微信', 'show' => 1, 'width' => 150, 'sort' => 3],
- 'clue_owner_name' => ['name' => '所属员工', 'show' => 1, 'width' => 150, 'sort' => 4],
- 'org_id' => ['name' => '所属部门', 'show' => 1, 'width' => 150, 'sort' => 4],
- // 'follow_state_name'=> ['name'=> '通话状态', 'show'=> 0, 'width'=> 225, 'sort'=> 5],
- 'gender' => ['name' => '性别', 'show' => 1, 'width' => 225, 'sort' => 6],
- 'tags' => ['name' => '线索标签', 'show' => 1, 'width' => 225, 'sort' => 7],
- 'clue_state_name' => ['name' => '线索阶段', 'show' => 0, 'width' => 250, 'sort' => 8],
- 'convert_status' => ['name' => '转化状态', 'show' => 1, 'width' => 275, 'sort' => 9],
- 'external_url' => ['name' => '落地页链接', 'show' => 1, 'width' => 100, 'sort' => 10],
- 'create_time_detail' => ['name' => '线索创建时间', 'show' => 1, 'width' => 75, 'sort' => 11],
- 'clue_source' => ['name' => '线索来源', 'show' => 1, 'width' => 100, 'sort' => 12],
- 'allocation_status' => ['name' => '分配状态', 'show' => 0, 'width' => 100, 'sort' => 13],
- 'remark' => ['name' => '备注留言', 'show' => 1, 'width' => 75, 'sort' => 14],
- 'remark_dict' => ['name' => '面积-风格', 'show' => 1, 'width' => 100, 'sort' => 17],
- 'address' => ['name' => '详细地址', 'show' => 1, 'width' => 75, 'sort' => 15],
- 'advertiser_id' => ['name' => '广告主ID', 'show' => 1, 'width' => 150, 'sort' => 16],
- 'advertiser_name' => ['name' => '广告主名称', 'show' => 1, 'width' => 100, 'sort' => 17],
- // 'age'=> ['name'=> '线索ID', 'show'=> 0, 'width'=> 75, 'sort'=> 18],
- 'ad_id' => ['name' => '计划ID', 'show' => 0, 'width' => 125, 'sort' => 19],
- 'ad_name' => ['name' => '计划名', 'show' => 0, 'width' => 75, 'sort' => 20],
- 'creative_id' => ['name' => '广告创意id', 'show' => 0, 'width' => 175, 'sort' => 21],
- 'promotion_name' => ['name' => '升级版广告名称', 'show' => 0, 'width' => 125, 'sort' => 22],
- 'promotion_id' => ['name' => '升级版广告ID', 'show' => 0, 'width' => 175, 'sort' => 23],
- 'site_id' => ['name' => '站点ID', 'show' => 0, 'width' => 125, 'sort' => 24],
- 'site_name' => ['name' => '站点名称', 'show' => 0, 'width' => 250, 'sort' => 25],
- 'intention_estimation' => ['name' => '线索意向', 'show' => 0, 'width' => 200, 'sort' => 26],
- 'clue_type' => ['name' => '组件类型', 'show' => 0, 'width' => 225, 'sort' => 27],
- 'module_name' => ['name' => '组件名字', 'show' => 0, 'width' => 175, 'sort' => 28],
- 'email' => ['name' => '邮箱', 'show' => 0, 'width' => 175, 'sort' => 29],
- 'qq' => ['name' => 'QQ', 'show' => 0, 'width' => 175, 'sort' => 30],
- 'location' => ['name' => '地址', 'show' => 0, 'width' => 250, 'sort' => 31],
- 'app_name' => ['name' => '流量来源', 'show' => 0, 'width' => 250, 'sort' => 32],
- ];
- $root_id = request()->empcrm->root_id;
- //房屋状态是否开启
- $house_status_where = [
- ['root_id', '=', $root_id],
- ['keyname', '=', 'house_status'],
- ['status', '=', 0]
- ];
- $house_status_per = CustomerPortraitField::where($house_status_where)->findOrEmpty();
- $house_field = [];
- if (!$house_status_per->isEmpty()) {
- $field = CustomerPortraitFieldSelect::where('pid', $house_status_per->id)->column('name,id');
- $fields = [];
- foreach ($field as $v) {
- $fields['house_status_' . $v['id']] = [
- 'name' => $v['name'],
- 'show' => 1,
- 'width' => 100,
- 'parent' => 'house',
- 'sort' => 50
- ];
- }
- $house_field = $fields;
- }
- // 户型
- $housetype_where = [
- ['root_id', '=', $root_id],
- ['keyname', '=', 'housetype_arrow'],
- ['status', '=', 0]
- ];
- $housetype_per = CustomerPortraitField::where($housetype_where)->findOrEmpty();
- $housetype_field = [];
- $housetype_done_field = [];
- if (!$housetype_per->isEmpty()) {
- $field = CustomerPortraitFieldSelect::where('pid', $housetype_per->id)->column('name,id');
- $fields = [];
- $fields_done = [];
- foreach ($field as $v) {
- $fields['housetype_arrow_' . $v['id']] = [
- 'name' => $v['name'],
- 'show' => 1,
- 'width' => 100,
- 'parent' => 'house',
- 'sort' => 51
- ];
- $fields_done['housetype_arrow_lv_' . $v['id']] = [
- 'name' => $v['name'] . '成交率',
- 'show' => 1,
- 'width' => 150,
- 'parent' => 'house',
- 'sort' => 51
- ];
- }
- $housetype_field = $fields;
- $housetype_done_field = $fields_done;
- }
- $field7 = array_merge($field7, $housetype_field);
- $field7 = array_merge($field7, $housetype_done_field);
- // 管理层户型分布
- $new_type_3 = [];
- foreach ($type3 as $k => $v) {
- $new_type_3[$k] = $v;
- if ($k == 'area') {
- $new_type_3 = array_merge($new_type_3, $house_field);
- }
- }
- switch ($type) {
- case 3: // 管理层统计全部字段
- return $type3;
- break;
- case 4: // 客户统计全部字段
- return $type4;
- break;
- case 6: //客户池
- return $field6;
- break;
- case 7: // 总统计
- return $field7;
- break;
- case 8: // 指派客户
- return $field8;
- break;
- case 9: // 到店客户
- return $field9;
- break;
- case 10: //待回访客户
- return $field10;
- break;
- case 12:
- return $fileds12;
- break;
- default:
- break;
- }
- }
- /**
- * 客户池展示字段
- */
- public function poolFieldsList()
- {
- $type = input('type', '', 'intval');
- $is_org = input('is_org', '', 'intval'); // 是否是部门统计 目前只有总统计
- $root_id = request()->empcrm->root_id;
- $employee_id = request()->empcrm->id;
- $find = StatisticsSetting::where([['type', '=', $type], ['employee_id', '=', $employee_id], ['root_id', '=', $root_id]])->findOrEmpty();
- $default_fields = $this->poolFields($type);
- if (!$find->isEmpty()) {
- $set = json_decode($find['content'], true);
- if (!empty($set)) {
- foreach ($set as $k => $v) {
- if (isset($default_fields[$k])) {
- $default_fields[$k]['show'] = $v['show'];
- $default_fields[$k]['sort'] = $v['sort'];
- }
- }
- array_multisort(array_column($default_fields, 'sort'), SORT_ASC, $default_fields);
- }
- }
- if ($type == 6) {
- $field_6 = [];
- foreach ($default_fields as $k => $v) {
- if ($k == 'name') {
- unset($default_fields[$k]);
- $field_6[$k] = $v;
- }
- // if (!empty($field_6) && $k == 'phone') {
- // unset($default_fields[$k]);
- // $field_6[$k] = $v;
- // }
- }
- $field_6 = array_merge($field_6, $default_fields);
- $default_fields = $field_6;
- }
- if ($type == 7) {
- if ($is_org == 1) {
- $field_7['org_name'] = ['name' => '部门名称', 'show' => 1, 'width' => 100, 'sort' => 1];
- foreach ($default_fields as $k => $v) {
- if ($k == 'name' || $k == 'org_name' || $k == 'look_data') {
- unset($default_fields[$k]);
- }
- }
- $field_7 = array_merge($field_7, $default_fields);
- $default_fields = $field_7;
- } else {
- $field_7['name'] = [];
- $field_7['org_name'] = [];
- foreach ($default_fields as $k => $v) {
- if (isset($field_7[$k])) {
- $field_7[$k] = $v;
- unset($default_fields[$k]);
- }
- }
- $field_7 = array_merge($field_7, $default_fields);
- $default_fields = $field_7;
- }
- }
- //处理馨居尚交定签单显示文字
- if (request()->empcrm['xinjushang'] == 1) {
- foreach ($default_fields as $key => $val) {
- if (strpos($val['name'], '签单') !== false) $default_fields[$key]['name'] = str_replace('签单', '交定', $val['name']);
- if (strpos($val['name'], '转单') !== false) $default_fields[$key]['name'] = str_replace('转单', '签单', $val['name']);
- }
- }
- return json(['code' => 0, 'msg' => 'success', 'data' => $default_fields]);
- }
- /**
- * 设置客户池展示字段
- */
- public function poolFieldsSet()
- {
- $type = input('type', '', 'intval');
- $content = input('content', '', 'trim');
- $content = json_decode($content, true);
- $root_id = request()->empcrm->root_id;
- $employee_id = request()->empcrm->id;
- $find = StatisticsSetting::where([['type', '=', $type], ['employee_id', '=', $employee_id], ['root_id', '=', $root_id]])->findOrEmpty();
- $default_field = $this->poolFields($type);
- foreach ($content as $k => $v) {
- if (isset($v['name']) && isset($v['show']) && isset($v['sort']) && isset($default_field[$v['name']])) {
- $default_field[$v['name']]['show'] = $v['show'];
- $default_field[$v['name']]['sort'] = $v['sort'];
- }
- }
- if ($find->isEmpty()) {
- $save['employee_id'] = $employee_id;
- $save['root_id'] = $root_id;
- $save['content'] = json_encode($default_field);
- $save['type'] = $type;
- $result = StatisticsSetting::insert($save);
- } else {
- $find->content = json_encode($default_field);
- $find->save();
- }
- return json(['code' => 0, 'msg' => 'success']);
- }
- /**
- * 修改客户信息时展示所有字段
- */
- public function model_new_report()
- {
- $id = input('id', '', 'trim');
- $root_id = request()->empcrm->root_id;
- $where[] = ['pid', '<>', 0];
- $where[] = ['root_id', '=', $root_id];
- $where[] = ['status', '=', 0];
- //获取第二层
- $all = CustomerPortraitField::with(['select'])->where($where)->orderRaw('if(isnull(sort),1,0),sort asc')->select()->toArray();
- $city = Company::where('root_id', $root_id)->value('city');
- foreach ($all as $key => $val) {
- if (empty($val['sort'])) $all[$key]['sort'] = $val['id'];
- }
- foreach ($all as &$val) {
- if ($val['keyname'] == 'current_region') {
- $val['select'][] = ['id' => $root_id, 'name' => $city, 'pid' => $val['id']];
- }
- if ($val['keyname'] == 'source_id') {
- $val['select'] = CustomerSource::field('id,source as name')->where('root_id', $root_id)->select()->toArray();
- }
- if ($val['keyname'] == 'deco_style') {
- $val['select'] = Decostyle::field('id,name')->where([['root_id', '=', $root_id], ['type', '=', 0]])->select()->toArray();
- }
- if (in_array($val['keyname'], ['live_broadcast_business', 'live_broadcast_design', 'live_broadcast_personnel'])) {
- $val['select'] = $this->live_broadcast_personnel();
- }
- if ($val['keyname'] == 'phone') {
- $newadd = [
- ['id' => 0, 'name' => '备用手机号1', 'keyname' => 'phone1', 'pid' => $val['pid'], 'status' => 0, 'is_must' => 1, 'root_id' => $val['root_id'], 'type' => 2, 'sort' => $val['sort'] + 1],
- ['id' => 01, 'name' => '备用手机号2', 'keyname' => 'phone2', 'pid' => $val['pid'], 'status' => 0, 'is_must' => 1, 'root_id' => $val['root_id'], 'type' => 2, 'sort' => $val['sort'] + 1]
- ];
- }
- }
- $all = array_merge($all, $newadd);
- $sort = array_column($all, 'sort');
- array_multisort($sort, SORT_ASC, $all);
- //获取第一层
- $pid = array_column($all, 'pid');
- $list = CustomerPortraitField::where([['id', 'in', $pid], ['status', '=', 0], ['pid', '=', 0]])->orderRaw('if(isnull(sort),1,0),sort asc')->select()->toArray();
- //组装数组
- foreach ($list as &$p) {
- foreach ($all as &$item) {
- $item['value'] = '';
- $item['valname'] = '';
- if ($p['id'] == $item['pid']) {
- $p['child'][] = $item;
- }
- }
- }
- $cuslog = new CustomerLogic;
- if (isset($id) && !empty($id)) {
- $AllOrgId = orgSubIds($root_id);
- $cusdata = Customer::where([['org_id', 'in', $AllOrgId], ['id', '=', $id]])->find()->toArray();
- if (!empty($cusdata['ext']) && $cusdata['ext'] != 'null') {
- $extdata = json_decode($cusdata['ext'], true);
- if (isset($extdata['ext1'])) {
- $list = $cuslog->old_data_save($cusdata, $list);
- } else {
- $list = $cuslog->new_data_save($cusdata, $extdata, $list);
- }
- } else {
- $list = $cuslog->no_ext_save($cusdata, $list);
- }
- }
- return json(['code' => 0, 'msg' => '获取成功', 'data' => $list]);
- }
- /**
- * 总统计页面
- */
- public function generalStatistics()
- {
- $root_id = request()->empcrm->root_id;
- //返回组织列表
- $sub_orgs = orgSubIds(request()->empcrm->org_id);
- $org = Org::where('id', 'in', $sub_orgs)->select()->toArray();
- View::assign('org', $org);
- $type = input('type', '', 'intval');
- View::assign('type', $type);
- $org_id = input('org_id', '', 'intval');
- View::assign('org_id', $org_id);
- // 字段查询
- // 量房字段
- $liangfang_field = [
- 'measuring_room_customer' => '全部',
- 'measuring_room_customer_no_to_store' => '量房未到店客户数',
- 'measuring_room_customer_to_store' => '量房到店客户数'
- ];
- View::assign('liangfang_field', $liangfang_field);
- // 到店字段
- $daodian_field = [
- 'to_the_store' => '全部',
- 'to_the_store_1' => '一次到店',
- 'to_the_store_2' => '二次到店',
- 'to_the_store_3' => '三次及以上到店'
- ];
- View::assign('daodian_field', $daodian_field);
- // 到店周期字段
- $daodian_avg_days_field = [
- 'strtore_avg_days_1' => '一次到店平均周期',
- 'strtore_avg_days_2' => '二次到店平均周期',
- 'strtore_avg_days_3' => '三次及以上到店平均周期'
- ];
- View::assign('daodian_avg_days_field', $daodian_avg_days_field);
- // 无效字段
- $invalid_field = [
- 'invalid_customer_count' => '全部'
- ];
- $invalid = Setting::where([['name', '=', 'clueTag'], ['root_id', '=', $root_id]])->value('content');
- $invalid = $invalid ? explode(',', $invalid) : ['已装修', '无需求', '有需求', '超地域', '未交房', '在外地', '无购买力', '未接通电话'];
- foreach ($invalid as $k => $v) {
- $invalid_field['invalid_customer_' . $k] = $v;
- }
- View::assign('invalid_field', $invalid_field);
- // 面积字段
- $square_field = [
- 'square' => '全部',
- 'square_0_80' => '80以下',
- 'square_80_100' => '81-100',
- 'square_100_120' => '101-120',
- 'square_120_200' => '121-200',
- 'square_200_500' => '201-500',
- 'square_500' => '500以上',
- ];
- View::assign('square_field', $square_field);
- // 房屋状态
- // 房屋状态是否开启
- $house_status_where = [
- ['root_id', '=', $root_id],
- ['keyname', '=', 'house_status'],
- ['status', '=', 0]
- ];
- $house_status_per = CustomerPortraitField::where($house_status_where)->findOrEmpty();
- $house_status_set = [];
- if (!$house_status_per->isEmpty()) {
- $house_status_set = CustomerPortraitFieldSelect::where('pid', $house_status_per->id)->column('name,id');
- }
- $house_status_field = [
- 'house_status' => '全部'
- ];
- if (!empty($house_status_set)) {
- foreach ($house_status_set as $k => $v) {
- $house_status_field['house_status_' . $v['id']] = $v['name'];
- }
- }
- View::assign('house_status_field', $house_status_field);
- // 房屋类型
- $house_type = [
- 'house_type' => '全部',
- 'house_type_existing' => '现房数量',
- 'house_type_forward' => '期房数量'
- ];
- View::assign('house_type', $house_type);
- $source = CustomerSource::where('root_id', '=', $root_id)->select()->toArray();
- View::assign('source', $source);
- return View::fetch();
- }
- /**
- * 总统计数据
- */
- public function generalStatisticsData()
- {
- $param = request()->only(['page' => 1, 'limit' => 10, 'date' => '', 'keyword' => '', 'org_id' => '', 'start_date' => '', 'end_date' => '', 'type' => '', 'source_id' => '']);
- $root_id = request()->empcrm->root_id;
- //日期搜索//2023-02-15页面逻辑修改 传参方式改为 start_date=2023/2/15 - 2023/2/15
- if ($param['start_date']) {
- $count_dates = explode(' - ', $param['start_date']);
- $param['start_date'] = date('Y-m-d 00:00:00', strtotime($count_dates[0]));
- $param['end_date'] = date('Y-m-d 23:59:59', strtotime($count_dates[1]));
- } else {
- $param['start_date'] = '2010-01-01 00:00:00';
- $param['end_date'] = date('Y-m-d H:i:s', time());
- }
- $date_query = [['addtime', 'between', [$param['start_date'], $param['end_date']]]];
- $date_query1 = [['share_time', 'between', [$param['start_date'], $param['end_date']]]];
- //加微时间筛选
- $sdate = strtotime($param['start_date']);
- $edate = strtotime($param['end_date']);
- // 员工
- $where = [
- ['root_id', '=', $root_id],
- ['uid', '>', 0],
- ['state', '=', '在职']
- ];
- if ($param['keyword']) {
- $where[] = ['name', 'like', '%' . $param['keyword'] . '%'];
- }
- $org_id = $param['org_id'] ? $param['org_id'] : request()->empcrm->org_id;
- $sub_orgs = orgSubIds($org_id);
- $where[] = ['org_id', 'in', $sub_orgs];
- $list = Employee::with(['org' => function ($query) {
- $query->field('id, name');
- }])->where($where)->field('id,org_id,name')->page($param['page'], $param['limit'])->select()->toArray();
- $count = Employee::where($where)->count();
- $page_employee = array_column($list, 'id');
- $c_where[] = ['employee_id', 'in', $page_employee];
- $c_where[] = ['sign_time', 'between', [$param['start_date'], $param['end_date']]];
- $c_where[] = ['state', 'NOT NULL', null];
- $db_where[] = ['Customer.employee_id', 'in', $page_employee];
- // $db_where[] = ['Customer.sign_time', 'between', [$param['start_date'], $param['end_date']]];
- $db_where[] = ['Customer.state', 'NOT NULL', null];
- $fish_where[] = ['is_allocation', '=', '0'];
- $fish_where[] = ['create_time_detail', 'between', [$param['start_date'], $param['end_date']]];
- if ($param['source_id']) {
- $c_where[] = ['source_id', '=', $param['source_id']];
- $db_where[] = ['Customer.source_id', '=', $param['source_id']];
- $jiav_where[] = ['source_id', '=', $param['source_id']];
- $j_w_where[] = ['source_id', '=', $param['source_id']];
- $invalid_where[] = ['source_id', '=', $param['source_id']];
- $fish_source = CustomerSource::where(['root_id' => $root_id, 'id' => $param['source_id']])->value('source');
- if ($fish_source == '飞鱼线索') {
- $fish_where[] = ['type', 'in', [1, 4]];
- } elseif ($fish_source == '腾讯线索') {
- $fish_where[] = ['type', '=', 2];
- } elseif ($fish_source == '欧派线索') {
- $fish_where[] = ['type', '=', 3];
- } else {
- $fish_where[] = ['type', '=', 55];
- }
- }
- // 加微客户查询
- // 先查搜索时间段内部门客户的加微数据,再判断哪些客户是page_employee中员工的客户,再判断其中哪些是已经置为无效的客户,再把这些置为无效的客户拼接到page_empployee员工身上。
- $page_employee_org = array_unique(array_column($list, 'org_id'));
- $jiav_where[] = ['org_id', 'in', $page_employee_org];
- $jiav_where[] = ['add_wechat_time', 'between', [$param['start_date'], $param['end_date']]];
- $jiav_list = Customer::where($jiav_where)->column('id,employee_id,state');
- $db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
- if ($date_query) {
- $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$param['start_date'], $param['end_date']]];
- }
- $customer_list = Customer::where($c_where)->column('id,employee_id,state,ext,is_resource,source_id,addtime,signed_money,house_type,square,house_status,remark,crm_res_id,agents_id,died,assign_type,employee_time,sign_time');
- // 置为无效的客户 按报名时间
- $invalid_where[] = ['employee_id', 'in', $page_employee];
- $invalid_where[] = ['cus_addtime', 'between', [$param['start_date'], $param['end_date']]];
- $invalid_customer_ids = CustomerInvalidLog::where($invalid_where)->column('id,employee_id,status');
- $invalid_customer_employee = []; // 员工无效的客户 计算客资总和
- $invalid_customer_employee_valid = []; // 员工无效的客户曾经有效 计算有效客户总和
- foreach ($invalid_customer_ids as $k => $v) {
- $invalid_customer_employee[$v['employee_id']][] = $v['id'];
- if ($v['status'] == 1) {
- $invalid_customer_employee_valid[$v['employee_id']][] = $v['id'];
- }
- }
- $vislog_list = Db::view('CustomerVisitLog', 'id,customer_id,state,addtime,next_contact_date,remark,employee_id,confirm_date,customer_employee_id')
- ->view('Customer', [], 'Customer.id=CustomerVisitLog.customer_id')
- ->where($db_where)
- ->select()
- ->toArray();
- // 处理加微数据
- $jiav_employee = []; // 正常的客户
- $jiav_employee_wuxiao = []; // 无效的客户
- foreach ($jiav_list as $k => $v) {
- if (in_array($v['employee_id'], $page_employee) && !in_array($v['state'], Customer::changeState('无效', 'chaos', true))) {
- $jiav_employee[$v['employee_id']][] = $v['id'];
- } elseif (in_array($v['state'], Customer::changeState('无效', 'chaos', true))) {
- $jiav_employee_wuxiao[] = $v['id'];
- }
- }
- // 去查询这些置为无效的加微客户原先都是谁的
- $j_w_where[] = ['customer_id', 'in', $jiav_employee_wuxiao];
- $j_w_where[] = ['employee_id', 'in', $page_employee];
- $jiav_wuxiao_log = CustomerInvalidLog::where($j_w_where)->column('customer_id,employee_id');
- $jiav_wuxiao_log_deal = [];
- foreach ($jiav_wuxiao_log as $v) {
- $jiav_wuxiao_log_deal[$v['employee_id']][] = $v['customer_id'];
- }
- foreach ($jiav_employee as $k => $v) {
- if (isset($jiav_wuxiao_log_deal[$k])) {
- $jiav_employee[$k] = array_unique(array_merge($v, $jiav_wuxiao_log_deal[$k]));
- }
- }
- // 处理跟踪记录
- $customer_employee = []; // 员工下的客户
- $customer_ids_employee = []; // 员工下的客户id
- $customer_ids_key = []; // 客户所属员工
- foreach ($customer_list as $k => $v) {
- $customer_employee[$v['employee_id']][] = $v;
- $customer_ids_employee[$v['employee_id']][] = $v['id'];
- $customer_ids_key[$v['id']] = $v['employee_id'];
- }
- $state3 = CustomerVisitLog::changeState('已量房', 'chaos');
- $state4 = CustomerVisitLog::changeState('已到店', 'chaos');
- $state5 = CustomerVisitLog::changeState('已交定', 'chaos');
- $state6 = CustomerVisitLog::changeState('已签单', 'chaos');
- $state7 = CustomerVisitLog::changeState('已卖卡', 'chaos');
- $wuxiao_log_state = CustomerVisitLog::changeState('无效', 'chaos');
- //2023-03-18 3.3客户标记无效后不能影响数据统计内的有效数据、线索数据 https://kdocs.cn/l/cfx92KlU838H
- $no_where = [
- ['employee_id', 'in', array_column($list, 'id')],
- ['state', 'in', CustomerVisitLog::changeState('无效', 'chaos')],
- ['addtime', 'between', [$param['start_date'], $param['end_date']]]
- ];
- $no_valid_customer = CustomerVisitLog::where($no_where)->column('id,customer_id,state,addtime,next_contact_date,remark,employee_id,confirm_date');
- $vislog_list = array_merge($vislog_list, $no_valid_customer);
- $visit_log_employee = []; //员工跟进记录
- //$from_pool = []; // 来自公海的客户,不做统计
- foreach ($vislog_list as $k => $v) {
- /* if ($v['remark'] == '公海获取' || (isset($from_pool[$v['employee_id']]) && in_array($v['customer_id'], $from_pool[$v['employee_id']]))) {
- if (isset($from_pool[$v['employee_id']])){
- if (!in_array($v['customer_id'], $from_pool[$v['employee_id']])) {
- $from_pool[$v['employee_id']][] = $v['customer_id'];
- }
- } else {
- $from_pool[$v['employee_id']][] = $v['customer_id'];
- }
- unset($vislog_list[$k]);
- continue;
- } */
- $have_employee = $customer_ids_key[$v['customer_id']] ?? 0;
- if ($have_employee) {
- $v_customer_ids = $customer_ids_employee[$have_employee] ?? [];
- // 只保留还是员工客户的跟踪记录,或者是员工置为无效的客户记录
- if (!in_array($v['customer_id'], $v_customer_ids) && !in_array($v['state'], $wuxiao_log_state, true)) {
- unset($vislog_list[$k]);
- continue;
- }
- $visit_log_employee[$have_employee][] = $v;
- }
- }
- $vislog_list = array_values($vislog_list);
- foreach ($list as $k => $v) {
- $list[$k]['visit_log'] = $visit_log_employee[$v['id']] ?? [];
- $list[$k]['customer'] = $customer_employee[$v['id']] ?? [];
- }
- // 员工客户处理 去除公海获取的客户
- /* foreach ($list as $k => $v) {
- if (isset($customer_employee[$v['id']]) && isset($from_pool[$v['id']])) {
- $v_customer = $customer_employee[$v['id']];
- foreach ($v_customer as $kk => $vv) {
- if (in_array($vv['id'], $from_pool[$v['id']])) {
- unset($v_customer[$kk]);
- }
- }
- $list[$k]['customer'] = array_values($v_customer);
- } else {
- $list[$k]['customer'] = $customer_employee[$v['id']] ?? [];
- }
- } */
- $customer['sign_customer'] = []; //签单客户
- $customer['measuring_room_customer'] = []; //量房客户
- $customer['maika_customer'] = []; //卖卡客户
- $customer['measuring_room_time'] = []; //量房时间
- $customer['to_the_store'] = []; //到店客户id
- $customer['to_the_store_time'] = []; //到店时间
- $customer['deposit_customer'] = []; //交定客户
- $customer['deposit_date'] = []; // 交定时间
- foreach ($vislog_list as $v) {
- if (in_array($v['state'], $state6, true)) { //签单客户id
- if (empty($customer['sign_customer'][$v['customer_employee_id']])) {
- $customer['sign_customer'][$v['customer_employee_id']] = [];
- }
- $customer['sign_customer'][$v['customer_employee_id']][] = $v['customer_id'];
- } elseif (in_array($v['state'], $state3, true)) { //量房客户id
- if (empty($customer['measuring_room_customer'][$v['customer_employee_id']])) {
- $customer['measuring_room_customer'][$v['customer_employee_id']] = [];
- }
- $customer['measuring_room_customer'][$v['customer_employee_id']][] = $v['customer_id'];
- //量房时间 按照时间升序排列
- if (empty($customer['measuring_room_time'][$v['customer_id']])) {
- $customer['measuring_room_time'][$v['customer_id']] = [];
- $customer['measuring_room_time'][$v['customer_id']][] = $v['addtime'];
- } else {
- $customer['measuring_room_time'][$v['customer_id']][] = $v['addtime'];
- }
- } elseif (in_array($v['state'], $state4, true)) { //到店客户id
- if (empty($customer['to_the_store'][$v['customer_employee_id']])) {
- $customer['to_the_store'][$v['customer_employee_id']] = [];
- }
- $customer['to_the_store'][$v['customer_employee_id']][] = $v['customer_id'];
- //到店时间 按照时间升序排列
- if (empty($customer['to_the_store_time'][$v['customer_id']])) {
- $customer['to_the_store_time'][$v['customer_id']] = [];
- $customer['to_the_store_time'][$v['customer_id']][] = $v['addtime'];
- } else {
- $customer['to_the_store_time'][$v['customer_id']][] = $v['addtime'];
- }
- } elseif (in_array($v['state'], $state5, true)) { //交定客户
- if (empty($customer['deposit_customer'][$v['customer_employee_id']])) {
- $customer['deposit_customer'][$v['customer_employee_id']] = [];
- }
- $customer['deposit_customer'][$v['customer_employee_id']][] = $v['customer_id'];
- //交定时间 按照时间升序排列
- if (empty($customer['deposit_date'][$v['customer_id']])) {
- $customer['deposit_date'][$v['customer_id']] = [];
- $customer['deposit_date'][$v['customer_id']][] = $v['addtime'];
- } else {
- $customer['deposit_date'][$v['customer_id']][] = $v['addtime'];
- }
- } elseif (in_array($v['state'], $state7, true)) { //卖卡客户
- $customer['maika_customer'][] = $v['customer_id'];
- }
- }
- // 有跟进客户
- $log_customer_id = []; // 某个员工下的跟踪客户id数组
- //智慧屏讲解次数
- $zhihui = [];
- // 无效
- $wuxiao_visit_log = [];
- foreach ($vislog_list as $k => $v) {
- if (in_array($v['state'], $wuxiao_log_state, true)) {
- $wuxiao_visit_log[] = $v;
- continue;
- }
- $have_employee = $customer_ids_key[$v['customer_id']] ?? 0;
- if ($have_employee) {
- if (strpos($v['remark'], '讲解智慧屏##') !== false) {
- if (empty($zhihui[$have_employee])) {
- $zhihui[$have_employee] = 1;
- } else {
- $zhihui[$have_employee] += 1;
- }
- }
- }
- if (in_array($v['remark'], ['客户再分配', '资源库分配', '客户转移'], true) || (strpos($v['remark'], '业务员') !== false && strpos($v['remark'], '对客户') !== false && strpos($v['remark'], '进行报备') !== false)) {
- continue;
- }
- if ($have_employee) {
- if (isset($log_customer_id[$have_employee])) {
- $log_customer_id[$have_employee][] = $v['customer_id'];
- } else {
- $log_customer_id[$have_employee] = [];
- $log_customer_id[$have_employee][] = $v['customer_id'];
- }
- }
- }
- foreach ($log_customer_id as $k => $v) {
- $log_customer_id[$k] = array_values(array_unique($v));
- }
- // 外呼数据
- $outCallLog = OutCallLog::where([['root_id', '=', $root_id], ['employee_id', 'in', $page_employee]])->where($date_query)->field("count(id) as phone_count, sum(if(`status`>0,1,0)) as on_phone_count, sum(if(`status`<0,1,0)) as off_phone_count,sum(billsec) as phone_time,employee_id")->group('employee_id')->select()->toArray();
- $outCallLog = array_combine(array_column($outCallLog, 'employee_id'), $outCallLog);
- // 手机打电话次数获取
- $outCallMbLog = OutCallMbLog::where([
- ['root_id', '=', $root_id], ['employee_id', 'in', $page_employee]
- ])->where($date_query)->field("count(id) as phone_count,employee_id")->group('employee_id')->select()->toArray();
- $outCallMbLog = array_combine(array_column($outCallMbLog, 'employee_id'), $outCallMbLog);
- // 房屋状态设置项
- //房屋状态是否开启
- $house_status_where = [
- ['root_id', '=', $root_id],
- ['keyname', '=', 'house_status'],
- ['status', '=', 0]
- ];
- $house_status_per = CustomerPortraitField::where($house_status_where)->findOrEmpty();
- $house_status_field = [];
- if (!$house_status_per->isEmpty()) {
- $house_status_field = CustomerPortraitFieldSelect::where('pid', $house_status_per->id)->column('name,id');
- }
- // 户型是否开启
- $housetype_where = [
- ['root_id', '=', $root_id],
- ['keyname', '=', 'housetype_arrow'],
- ['status', '=', 0]
- ];
- $housetype_per = CustomerPortraitField::where($housetype_where)->findOrEmpty();
- $housetype_arrow = [];
- if (!$housetype_per->isEmpty()) {
- $housetype_arrow = CustomerPortraitFieldSelect::where('pid', $housetype_per->id)->column('name,id');
- }
- //分享内容次数
- $share_where = ['CompanyStrength', 'Article', 'Construction', 'Activity', 'MaterialEvidence', 'Video', 'MaterialCase', 'Building'];
- $share = ShareLog::where([['employee_id', 'in', $page_employee], ['type', 'in', $share_where]])->where($date_query1)->field('concat(employee_id,type,data_id) as str,employee_id')->select()->toArray();
- $shares = [];
- foreach ($share as $v) {
- if (empty($shares[$v['employee_id']])) {
- $shares[$v['employee_id']] = 1;
- } else {
- $shares[$v['employee_id']] += 1;
- }
- }
- //考核次数 通过次数
- $don = TrainDoneLog::where([['employee_id', 'in', $page_employee]])->where($date_query)->column('employee_id,done_percent');
- $don1 = $don2 = [];
- foreach ($don as $don_k => $don_v) {
- if (empty($don1[$don_v['employee_id']])) {
- $don1[$don_v['employee_id']] = 1;
- } else {
- $don1[$don_v['employee_id']] += 1;
- }
- if ($don_v['done_percent'] == 100) {
- if (empty($don2[$don_v['employee_id']])) {
- $don2[$don_v['employee_id']] = 1;
- } else {
- $don2[$don_v['employee_id']] += 1;
- }
- }
- }
- //无效客资
- $invalid = Setting::where([['name', '=', 'clueTag'], ['root_id', '=', $root_id]])->value('content');
- $invalid = $invalid ? explode(',', $invalid) : ['已装修', '无需求', '有需求', '超地域', '未交房', '在外地', '无购买力', '未接通电话'];
- $invalid_customer = [];
- // 按客户来源搜索处理
- $wuxiao_customer_id = array_values(array_unique(array_column($wuxiao_visit_log, 'customer_id')));
- $no_valid_customers = []; //2023-03-18 3.3客户标记无效后不能影响数据统计内的有效数据、线索数据 https://kdocs.cn/l/cfx92KlU838H 康
- if ($param['source_id']) {
- $wuxiao_source_customer_id = Customer::where([['id', 'in', $wuxiao_customer_id], ['source_id', '=', $param['source_id']]])->column('id');
- foreach ($wuxiao_visit_log as $k => $v) {
- if (in_array($v['customer_id'], $wuxiao_source_customer_id)) {
- $remark = '';
- if (in_array($v['remark'], $invalid)) {
- $remark = $v['remark'];
- } else {
- foreach ($invalid as $i_k => $i_v) {
- if (strpos($v['remark'], $i_v) !== false) {
- $remark = $i_v;
- break;
- }
- }
- }
- if (isset($invalid_customer[$v['employee_id']])) {
- $invalid_customer[$v['employee_id']][] = ['remark' => $remark, 'customer_id' => $v['customer_id']];
- } else {
- $invalid_customer[$v['employee_id']] = [];
- $invalid_customer[$v['employee_id']][] = ['remark' => $remark, 'customer_id' => $v['customer_id']];
- }
- if (isset($no_valid_customers[$v['employee_id']])) {
- $no_valid_customers[$v['employee_id']][] = $v['customer_id'];
- } else {
- $no_valid_customers[$v['employee_id']] = [];
- $no_valid_customers[$v['employee_id']][] = $v['customer_id'];
- }
- }
- }
- } else {
- foreach ($wuxiao_visit_log as $k => $v) {
- $remark = '';
- if (in_array($v['remark'], $invalid)) {
- $remark = $v['remark'];
- } else {
- foreach ($invalid as $i_k => $i_v) {
- if (strpos($v['remark'], $i_v) !== false) {
- $remark = $i_v;
- break;
- }
- }
- }
- if (isset($invalid_customer[$v['employee_id']])) {
- $invalid_customer[$v['employee_id']][] = ['remark' => $remark, 'customer_id' => $v['customer_id']];
- } else {
- $invalid_customer[$v['employee_id']] = [];
- $invalid_customer[$v['employee_id']][] = ['remark' => $remark, 'customer_id' => $v['customer_id']];
- }
- if (isset($no_valid_customers[$v['employee_id']])) {
- $no_valid_customers[$v['employee_id']][] = $v['customer_id'];
- } else {
- $no_valid_customers[$v['employee_id']] = [];
- $no_valid_customers[$v['employee_id']][] = $v['customer_id'];
- }
- }
- }
- // 无效客资来自资源分配
- $wuxiao_customer_res_id = Customer::where([['id', 'in', $wuxiao_customer_id], ['crm_res_id', '>', 0]])->column('id');
- $wuxiao_customer_res_employee = [];
- foreach ($invalid_customer as $k => $v) {
- $customer_id_v = array_column($v, 'customer_id');
- foreach ($customer_id_v as $vv) {
- if (in_array($vv, $wuxiao_customer_res_id)) {
- $wuxiao_customer_res_employee[$k][] = $vv;
- }
- }
- }
- $f_invalid = array_flip($invalid);
- foreach ($list as $k => $v) {
- //部门名称
- $list[$k]['org_name'] = $v['org']['name'];
- //客户id 所有的客户ID 包括资源,自创,无效等
- $all_customer_id = array_column($v['customer'], 'id');
- //数据处理
- $customer_data = $this->dealCustomerData($v['customer'], $sdate, $edate, $customer['measuring_room_time'], $customer['to_the_store_time'], $customer['deposit_date']);
- $cycle_data = $this->avgCycle($v['customer'], $v['visit_log']);
- // 分配资源数量 包含已置入公海的
- $resource_count = count($customer_data['is_resource']);
- $resource_wuxiao_ids = [];
- if (isset($wuxiao_customer_res_employee[$v['id']])) {
- $resource_wuxiao_ids = array_values(array_unique($wuxiao_customer_res_employee[$v['id']]));
- }
- $list[$k]['resource_count'] = count(array_unique(array_merge($customer_data['is_resource'], $resource_wuxiao_ids)));
- if (isset($outCallLog[$v['id']])) {
- // 拨打电话数量
- $list[$k]['connecting_count'] = $outCallLog[$v['id']]['phone_count'];
- // 接通数量
- $list[$k]['connecting_capacity'] = $outCallLog[$v['id']]['on_phone_count'];
- // 接通率
- $list[$k]['connecting_capacity_grawth'] = $outCallLog[$v['id']]['phone_count'] == 0 ? '0%' : round($outCallLog[$v['id']]['on_phone_count'] / $outCallLog[$v['id']]['phone_count'] * 100, 1) . '%';
- // 未接通数量
- $list[$k]['connecting_no'] = $outCallLog[$v['id']]['off_phone_count'];
- // 总通话时长(分)
- $list[$k]['phone_time'] = round($outCallLog[$v['id']]['phone_time'] / 60, 1);
- // 平均通话时长(秒)
- $list[$k]['average_duration'] = $outCallLog[$v['id']]['on_phone_count'] == 0 ? 0 : round($outCallLog[$v['id']]['phone_time'] / $outCallLog[$v['id']]['on_phone_count'], 0);
- } else {
- $list[$k]['connecting_count'] = $list[$k]['connecting_capacity'] = $list[$k]['connecting_no'] = $list[$k]['phone_time'] = $list[$k]['average_duration'] = 0;
- $list[$k]['connecting_capacity_grawth'] = '0%';
- }
- // 手机通话
- if (isset($outCallMbLog[$v['id']])) $list[$k]['connecting_count'] += $outCallMbLog[$v['id']]['phone_count'];
- // 客户总量
- if (isset($invalid_customer_employee[$v['id']])) {
- $customer_count = array_unique(array_merge($customer_data['customer_count'], $invalid_customer_employee[$v['id']]));
- } else {
- $customer_count = $customer_data['customer_count'];
- }
- //查询当前此人飞鱼腾讯的线索量-资源未转化的客户
- $fish_count = FishData::where($fish_where)->where(['employee_id' => $v['id']])->count();
- $list[$k]['customer_count'] = $fish_count + count($customer_count);
- // $list[$k]['customer_count'] = count($customer_count);
- $list[$k]['follow_customer_count'] = count($customer_data['follow_customer_count']);
- // 加微客户数量
- $v_wechat_count = $jiav_employee[$v['id']] ?? [];
- $list[$k]['add_wechat_count'] = count($v_wechat_count);
- // 待确认客户数
- $list[$k]['not_sure_count'] = count($customer_data['not_sure_count']);
- // 有效客户数
- if (isset($invalid_customer_employee_valid[$v['id']])) {
- $list[$k]['valid_count'] = count(array_unique(array_merge($customer_data['valid_count'], $invalid_customer_employee_valid[$v['id']])));
- } else {
- $list[$k]['valid_count'] = count($customer_data['valid_count']);
- }
- // 加微率
- $list[$k]['add_wechat_count_grawth'] = count($customer_count) == 0 ? '0%' : round(count($v_wechat_count) / count($customer_count) * 100, 2) . '%';
- // 无效客户数
- foreach ($invalid as $invalid_k => $invalid_v) {
- $list[$k]['invalid_customer_' . $invalid_k] = [];
- }
- if (isset($invalid_customer[$v['id']])) {
- $invalid_customer_all = array_unique(array_column($invalid_customer[$v['id']], 'customer_id'));
- $list[$k]['invalid_customer_count'] = count($invalid_customer_all);
- if ($invalid) {
- foreach ($invalid_customer[$v['id']] as $key => $value) {
- if ($value['remark']) {
- $key = $f_invalid[$value['remark']];
- $list[$k]['invalid_customer_' . $key][] = $value['customer_id'];
- }
- }
- }
- } else {
- $invalid_customer_all = [];
- $list[$k]['invalid_customer_count'] = 0;
- }
- foreach ($invalid as $invalid_k => $invalid_v) {
- $list[$k]['invalid_customer_' . $invalid_k] = count(array_unique($list[$k]['invalid_customer_' . $invalid_k]));
- }
- // 线索总量
- $v_customer = !empty($v['customer']) ? array_column($v['customer'], 'id') : [];
- $all_customer = array_unique(array_merge($v_customer, $invalid_customer_all));
- $list[$k]['all_customer_count'] = count($all_customer);
- // 有效率(%)
- $list[$k]['valid_grawth'] = count($all_customer) == 0 ? '0%' : round(count($customer_data['valid_count']) / count($all_customer) * 100, 2) . '%';
- // 未跟进客户数
- if (isset($log_customer_id[$v['id']])) {
- $following_up_count = $log_customer_id[$v['id']];
- } else {
- $following_up_count = [];
- }
- $list[$k]['not_following_up_count'] = count($customer_count) - count(array_intersect($customer_count, $following_up_count)); // 总的减去有跟进的
- // 意向分派客户数
- $list[$k]['yixiang_assigned_count'] = count($customer_data['yixiang_assigned_count']);
- // 到店分派客户数
- $list[$k]['daodian_assigned_count'] = count($customer_data['daodian_assigned_count']);
- // 量房分配客户数
- $list[$k]['measuring_assigned_count'] = count($customer_data['liangfang_assigned_count']);
- // 智慧屏讲解次数
- $list[$k]['number_of_explanations'] = isset($zhihui[$v['id']]) ? $zhihui[$v['id']] : 0;
- // 量房客户数
- $measuring_room_customer = $customer['measuring_room_customer'][$v['id']] ?? []; //量房客户ID
- $to_the_store_ids = $customer['to_the_store'][$v['id']] ?? []; // 到店客户ID
- $list[$k]['measuring_room_customer'] = count(array_unique($measuring_room_customer));
- // 量房未到店
- $measuring_room_customer_to_store = array_intersect($measuring_room_customer, $to_the_store_ids); // 量房 和 到店
- $measuring_room_customer_no_to_store = array_diff($measuring_room_customer, $measuring_room_customer_to_store);
- $list[$k]['measuring_room_customer_no_to_store'] = count($measuring_room_customer_no_to_store);
- // 量房已到店
- $list[$k]['measuring_room_customer_to_store'] = count($measuring_room_customer_to_store);
- // 平均量房周期(天)
- $list[$k]['measuring_room_zhouqi'] = $cycle_data['room_cycle'];
- // 量房率
- $list[$k]['measuring_room_grawth'] = count($customer_count) == 0 ? '0%' : round(count($measuring_room_customer) / count($customer_count) * 100, 2) . '%';
- // 到店数 次数
- $list[$k]['to_the_store'] = count($to_the_store_ids);
- // 一次到店数
- $list[$k]['to_the_store_1'] = $customer_data['strtore_count_0'];
- // 二次到店
- $list[$k]['to_the_store_2'] = $customer_data['strtore_count_1'];
- // 二次到店比率
- $list[$k]['strtore_2v1'] = $customer_data['strtore_count_1'] + $customer_data['strtore_count_0'] == 0 ? '0%' : round($customer_data['strtore_count_1'] / ($customer_data['strtore_count_1'] + $customer_data['strtore_count_0']) * 100, 2) . '%';
- // 三次及以上到店
- $list[$k]['to_the_store_3'] = $customer_data['strtore_count_2'];
- // 平均到店周期(天)
- $list[$k]['strtore_avg_days_1'] = ceil($customer_data['strtore_0'] / 86400); // 一次
- $list[$k]['strtore_avg_days_2'] = ceil($customer_data['strtore_1'] / 86400); // 二次
- $list[$k]['strtore_avg_days_3'] = ceil($customer_data['strtore_2'] / 86400); // 三次
- // 到店客户数
- $list[$k]['strtore_customer_count'] = count(array_unique($to_the_store_ids));
- // 到店率
- $list[$k]['strtore_customer_grawth'] = count($customer_count) == 0 ? '0%' : round(count($to_the_store_ids) / count($customer_count) * 100, 2) . '%';
- // 签单客户数量
- $deposit_count_ids = $customer['deposit_customer'][$v['id']] ?? [];
- $list[$k]['deposit_count'] = count(array_unique($deposit_count_ids));
- // 签单率 签单数/见面数
- $list[$k]['deposit_avg'] = count($cycle_data['visit_customer']) == 0 ? '0%' : round(count($deposit_count_ids) / count($cycle_data['visit_customer']) * 100, 2) . '%';
- // 平均签单周期
- $list[$k]['deposit_avg_days'] = $cycle_data['dep_cycle'];
- // 转单客户数量
- $sign_customer_ids = $customer['sign_customer'][$v['id']] ?? [];
- $list[$k]['sign_count'] = count(array_unique($sign_customer_ids));
- // 卖卡客户数量
- $maika_customer_ids = $customer['maika_customer'][$v['id']] ?? [];
- $list[$k]['maika_count'] = count(array_unique($maika_customer_ids));
- // 转单率 转单/见面数
- $list[$k]['sign_count_grawth'] = count($cycle_data['visit_customer']) == 0 ? '0%' : round(count($sign_customer_ids) / count($cycle_data['visit_customer']) * 100, 2) . '%';
- // 平均跟进次数
- $list[$k]['avg_visit_count'] = $cycle_data['next'];
- // 平均合同金额(元)
- $list[$k]['signed_money'] = $customer_data['signed_money'];
- // 合同总金额
- $list[$k]['signed_money_all'] = $customer_data['signed_money_all'];
- // 面积
- $list[$k]['square'] = $customer_data['square'];
- $list[$k]['square_0_80'] = $customer_data['square_0_80'];
- $list[$k]['square_80_100'] = $customer_data['square_80_100'];
- $list[$k]['square_100_120'] = $customer_data['square_100_120'];
- $list[$k]['square_120_200'] = $customer_data['square_120_200'];
- $list[$k]['square_200_500'] = $customer_data['square_200_500'];
- $list[$k]['square_500'] = $customer_data['square_500'];
- // 房屋状态
- $list[$k]['house_status'] = 0;
- if ($house_status_field) {
- $house_status = $this->getHouseStatus($customer_data['house_status'], $house_status_field);
- $list[$k] = array_merge($list[$k], $house_status);
- $list[$k]['house_status'] = array_sum($house_status);
- }
- //房屋户型 及 对应的转单率
- if ($housetype_arrow) {
- $housetype_arrow_data = $this->getHousetypeArrow($customer_data['housetype_arrow'], $housetype_arrow, $sign_customer_ids);
- $list[$k] = array_merge($list[$k], $housetype_arrow_data);
- }
- // 房屋类型
- $list[$k]['house_type'] = $customer_data['existing_homes_count'] + $customer_data['forward_housing_count'];
- $list[$k]['existing_homes_count'] = $customer_data['existing_homes_count'];
- $list[$k]['forward_housing_count'] = $customer_data['forward_housing_count'];
- // 调用内容数量
- // 分享内容次数
- $list[$k]['shares_count'] = isset($shares[$v['id']]) ? $shares[$v['id']] : 0;
- // 考核次数
- $list[$k]['assessment_times'] = isset($don1[$v['id']]) ? $don1[$v['id']] : 0;
- // 通过次数
- $list[$k]['number_of_passes'] = isset($don2[$v['id']]) ? $don2[$v['id']] : 0;
- unset($list[$k]['customer']);
- unset($list[$k]['visit_log']);
- $list[$k]['valid_num'] = CustomerStateCheck::where([
- ['employee_id', '=', $v['id']],
- ['check_state', '=', 1]
- ])->count();
- }
- return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
- }
- static function setForNot(&$a, $c, $addCount)
- {
- if (!isset($a[$c])) $a[$c] = 0;
- $a[$c] += $addCount;
- }
- /**
- * 处理客户数据
- * $list 业务员的客户数组
- * $sdate 开始时间筛选
- * $edate 结束时间筛选
- * $measuring_room_time 客户量房时间 计算平均量房时间
- * $to_the_store_time 客户到店时间 计算平均到店时间
- * $deposit_customer 客户交定时间 计算平均交定周期
- */
- static function dealCustomerData($list, $sdate, $edate, $measuring_room_time, $to_the_store_time, $deposit_customer)
- {
- $time = time();
- $data['not_sure_count'] = []; // 待确认客户id
- $data['customer_count'] = []; // 客户量(线索量) 除去资源、无效、死单
- $data['follow_customer_count'] = []; // 跟进客户数量
- $data['valid_count'] = []; // 有效客户 除去资源、无效、死单、待确认 (客户量的基础上减去待确认的)
- //$data['invalid_count'] = []; // 无效客户
- $data['yixiang_assigned_count'] = []; // 意向分配客户数
- $data['daodian_assigned_count'] = []; // 到店分配客户数
- $data['liangfang_assigned_count'] = []; // 量房分配客户数
- $data['wechat_count'] = []; //加微客户id
- $data['is_resource'] = []; //资源库分配客户id
- $data['drawing_date'] = []; //有户型图的客户
- $row['room_cycle_0'] = $row['room_cycle_1'] = $row['room_cycle_2'] = []; //一次 二次 三次量房周期
- $row['strtore_0'] = $row['strtore_1'] = $row['strtore_2'] = []; //一次 二次 三次到店周期
- $row['deposit_0'] = $row['deposit_1'] = $row['deposit_2'] = []; //一次 二次 三次交定周期
- $data['room_cycle_customer_0'] = $data['room_cycle_customer_1'] = $data['room_cycle_customer_2'] = []; //一次 两次 三次量房客户
- $data['strtore_customer_0'] = $data['strtore_customer_1'] = $data['strtore_customer_2'] = []; //一次 两次 三次到店客户
- $data['deposit_customer_0'] = $data['deposit_customer_1'] = $data['deposit_customer_2'] = []; //一次 两次 三次交定客户
- $row['signed_money'] = []; //合同金额
- $house_type = []; //房屋类型
- $housetype_arrow = []; // 房屋户型
- $square = []; //房屋面积
- $data['house_status'] = []; //房屋状态
- //报备客户
- $data['reported_quantity'] = 0;
- //死单客户
- $data['died'] = 0;
- $not_sure_state = Customer::changeState('待确认', 'chaos');
- $invalid_state = Customer::changeState('无效', 'chaos');
- $invalid_not_sure_state = array_values(array_merge($invalid_state, $not_sure_state));
- foreach ($list as $v) {
- // 线索量
- $data['customer_count'][] = $v['id'];
- // 待确认客户
- if (empty($v['crm_res_id']) && in_array($v['state'], $not_sure_state, true)) {
- $data['not_sure_count'][] = $v['id'];
- }
- // 客户数量
- if (intval($v['died']) != 2) {
- if ((empty($v['crm_res_id']) && !in_array($v['state'], $invalid_state, true)) || (!empty($v['crm_res_id']) && !in_array($v['state'], $invalid_not_sure_state, true))) {
- $data['follow_customer_count'][] = $v['id'];
- $square[] = $v['square']; //面积
- }
- }
- // 有效客户
- if (intval($v['died']) != 2 && !in_array($v['state'], $invalid_not_sure_state, true)) {
- $data['valid_count'][] = $v['id'];
- // 房屋类型
- $house_type[] = $v['house_type'];
- $row['signed_money'][] = $v['signed_money']; //合同金额
- $data['house_status'][] = $v['house_status'];
- // 房屋户型
- $ext = json_decode($v['ext'], true);
- if (!empty($ext) && is_array($ext)) {
- $ext = array_filter($ext);
- foreach ($ext as $e) {
- if (isset($e['keyname']) && $e['keyname'] == 'housetype_arrow' && !empty($e['value'])) {
- $housetype_arrow[$v['id']] = $e['value'];
- }
- }
- }
- }
- // 无效客户
- /* if (in_array($v['state'], $invalid_state, true)) {
- $data['invalid_count'][] = $v['id'];
- } */
- // 意向分派客户数
- if (!empty($v['assign_type']) && $v['assign_type'] == 'yixiang') {
- $data['yixiang_assigned_count'][] = $v['id'];
- }
- // 到店分派客户数
- if (!empty($v['assign_type']) && $v['assign_type'] == 'daodian') {
- $data['daodian_assigned_count'][] = $v['id'];
- }
- // 量房分派客户数
- if (!empty($v['assign_type']) && $v['assign_type'] == 'liangfang') {
- $data['liangfang_assigned_count'][] = $v['id'];
- }
- //客户报名时间
- $addtime = strtotime($v['sign_time']);
- //资源库分配数量
- if ($v['crm_res_id'] && strtotime($v['employee_time']) >= $sdate && strtotime($v['employee_time']) <= $edate) {
- $data['is_resource'][] = $v['id'];
- }
- //加微客户,有户型图客户
- $v2 = $v['ext'];
- $ext = json_decode($v2, true);
- if (!empty($ext) && is_array($ext)) {
- $ext = array_filter($ext);
- foreach ($ext as $e) {
- if (isset($e['keyname']) && $e['keyname'] == 'add_wechat_time' && !empty($e['value']) && strtotime($e['value']) >= $sdate && strtotime($e['value']) <= $edate) {
- $data['wechat_count'][] = $v['id'];
- } elseif (isset($e['keyname']) && $e['keyname'] == 'drawing_date' && !empty($e['value']) && strtotime($e['value']) <= $time) {
- $data['drawing_date'][] = $v['id'];
- }
- }
- }
- //一次 二次 三次量房周期
- if (isset($measuring_room_time[$v['id']])) {
- $count = count($measuring_room_time[$v['id']]);
- $start = $count >= 3 ? 2 : $count - 1;
- $measuring_room_cycle = [];
- $measuring_room_cycle[$start] = $measuring_room_time[$v['id']][$start];
- // $measuring_room_cycle = array_slice($measuring_room_time[$v['id']],0, 3);
- foreach ($measuring_room_cycle as $cycle_key => $cycle_item) {
- $cycle_addtime = strtotime($cycle_item);
- if ($addtime < $cycle_addtime) $row['room_cycle_' . $cycle_key][] = $cycle_addtime - $addtime;
- $data['room_cycle_customer_' . $cycle_key][] = $v['id'];
- }
- }
- //一次 二次 三次到店周期
- if (isset($to_the_store_time[$v['id']])) {
- $count = count($to_the_store_time[$v['id']]);
- $start = $count >= 3 ? 2 : $count - 1;
- $to_the_store_times = [];
- $to_the_store_times[$start] = $to_the_store_time[$v['id']][$start];
- // $to_the_store_times = array_slice($to_the_store_time[$v['id']],0, 3);
- foreach ($to_the_store_times as $store_key => $store_item) {
- $store_addtime = strtotime($store_item);
- if ($addtime < $store_addtime) $row['strtore_' . $store_key][] = $store_addtime - $addtime;
- $data['strtore_customer_' . $store_key][] = $v['id'];
- }
- }
- //一次 二次 三次交定周期
- if (isset($deposit_customer[$v['id']])) {
- $count = count($deposit_customer[$v['id']]);
- $start = $count >= 3 ? 2 : $count - 1;
- $deposit_customer_time = [];
- $deposit_customer_time[$start] = $deposit_customer[$v['id']][$start];
- // $deposit_customer_time = array_slice($deposit_customer[$v['id']],0, 3);
- foreach ($deposit_customer_time as $deposit_key => $deposit_item) {
- $deposit_addtime = strtotime($deposit_item);
- if ($addtime < $deposit_item) $row['deposit_' . $deposit_key][] = $deposit_addtime - $addtime;
- $data['deposit_customer_' . $deposit_key][] = $v['id'];
- }
- }
- //死单
- if (intval($v['died']) == 2) $data['died'] += 1;
- }
- $data['housetype_arrow'] = $housetype_arrow;
- foreach ([0, 1, 2] as $range) {
- //平均一次,二次,三次量房周期
- $data['room_cycle_' . $range] = $row['room_cycle_' . $range] ? ceil(array_sum($row['room_cycle_' . $range]) / count($row['room_cycle_' . $range])) : 0;
- //一次,二次,三次 量房人数
- $data['room_cycle_count_' . $range] = count($data['room_cycle_customer_' . $range]);
- //平均到店周期
- $data['strtore_' . $range] = $row['strtore_' . $range] ? ceil(array_sum($row['strtore_' . $range]) / count($row['strtore_' . $range])) : 0;
- //一次 二次 三次 到店人数
- $data['strtore_count_' . $range] = count($data['strtore_customer_' . $range]);
- //平均交定周期
- $data['deposit_' . $range] = $row['deposit_' . $range] ? ceil(array_sum($row['deposit_' . $range]) / count($row['deposit_' . $range])) : 0;
- //一次 二次 三次 交定人数
- $data['deposit_count_' . $range] = count($data['deposit_customer_' . $range]);
- }
- //平均合同金额
- $signed_money = array_filter($row['signed_money']);
- $data['signed_money'] = empty($signed_money) ? 0 : round(array_sum($signed_money) / count($signed_money), 2);
- $data['signed_money_all'] = array_sum($signed_money);
- //房屋类型
- $data['existing_homes_count'] = 0; //现房数量
- $data['forward_housing_count'] = 0; //期房数量
- foreach ($house_type as $house_type_item) {
- if (strpos($house_type_item, '现房') !== false) {
- $data['existing_homes_count'] += 1;
- } elseif (strpos($house_type_item, '期房') !== false) {
- $data['forward_housing_count'] += 1;
- }
- }
- //房屋面积
- $data['square'] = $data['square_0_80'] = $data['square_80_100'] = $data['square_100_120'] = $data['square_120_200'] = $data['square_200_500'] = $data['square_500'] = 0;
- foreach ($square as $square_item) {
- if (!$square_item) continue;
- $data['square'] += 1;
- if ($square_item <= 80) {
- $data['square_0_80'] += 1;
- } elseif ($square_item <= 100) {
- $data['square_80_100'] += 1;
- } elseif ($square_item <= 120) {
- $data['square_100_120'] += 1;
- } elseif ($square_item <= 200) {
- $data['square_120_200'] += 1;
- } elseif ($square_item <= 500) {
- $data['square_200_500'] += 1;
- } else {
- $data['square_500'] += 1;
- }
- }
- return $data;
- }
- /**
- * 平均周期
- */
- static function avgCycle($cus, $vis)
- {
- $state1 = CustomerVisitLog::changeState('已交定', 'chaos');
- $state2 = CustomerVisitLog::changeState('已量房', 'chaos');
- $state3 = CustomerVisitLog::changeState('已到店', 'chaos');
- $state4 = CustomerVisitLog::changeState('已到场', 'chaos');
- $dep_time = [];
- $room_time = [];
- $store_time = [];
- $next_date = [];
- $visit_customer = []; // 见面的客户id
- foreach ($vis as $v) {
- //交定日期
- if (in_array($v['state'], $state1)) {
- $dep_time[$v['customer_id']] = $v['addtime'];
- } elseif (in_array($v['state'], $state2)) {
- $room_time[$v['customer_id']] = $v['addtime'];
- } elseif (in_array($v['state'], $state3)) {
- $store_time[$v['customer_id']] = $v['addtime'];
- }
- //平均回访次数
- if (!isset($next_date[$v['customer_id']]['addtime'])) {
- $next_date[$v['customer_id']]['addtime'] = [];
- }
- if (!isset($next_date[$v['customer_id']]['next_date'])) {
- $next_date[$v['customer_id']]['next_date'] = [];
- }
- $next_date[$v['customer_id']]['addtime'][] = date('Y-m-d', strtotime($v['addtime']));
- $next_date[$v['customer_id']]['next_date'][] = date('Y-m-d', strtotime($v['next_contact_date']));
- $visit_state = array_merge($state2, $state3, $state4);
- if (in_array($v['state'], $visit_state)) {
- if (!in_array($v['customer_id'], $visit_customer)) {
- $visit_customer[] = $v['customer_id'];
- }
- }
- }
- $dep_cycle = $room_cycle = $room_store_cycle = $next = [];
- foreach ($cus as $v2) {
- if (isset($dep_time[$v2['id']])) {
- $dep_cycle[] = strtotime($dep_time[$v2['id']]) - strtotime($v2['addtime']);
- }
- if (isset($room_time[$v2['id']])) {
- $room_cycle[] = strtotime($room_time[$v2['id']]) - strtotime($v2['addtime']);
- }
- if (isset($room_time[$v2['id']]) && isset($store_time[$v2['id']])) {
- $room_store_cycle[] = abs(strtotime($room_time[$v2['id']]) - strtotime($store_time[$v2['id']]));
- }
- if (isset($next_date[$v2['id']]['addtime']) && $next_date[$v2['id']]['next_date']) {
- $next[] = count(array_filter(array_intersect($next_date[$v2['id']]['addtime'], $next_date[$v2['id']]['next_date'])));
- }
- }
- //平均交定周期
- $data['dep_cycle'] = empty($dep_cycle) ? 0 : ceil(count($dep_cycle) / array_sum($dep_cycle));
- //平均量房周期
- $data['room_cycle'] = empty($room_cycle) ? 0 : ceil(count($room_cycle) / array_sum($room_cycle));
- //平均量房到店周期
- $data['room_store_cycle'] = empty($room_store_cycle) ? 0 : ceil(count($room_store_cycle) / array_sum($room_store_cycle));
- //平均回访次数
- $data['next'] = !array_sum($next) ? 0 : ceil(count($next) / array_sum($next));
- // 见面量
- $data['visit_customer'] = $visit_customer;
- return $data;
- }
- /**
- * 房屋状态
- */
- static function getHouseStatus($house_status, $field)
- {
- $data = [];
- $house = [];
- foreach ($house_status as $v) {
- if (isset($house[$v])) {
- $house[$v]++;
- } else {
- $house[$v] = 1;
- }
- }
- foreach ($field as $k2 => $v2) {
- $data['house_status_' . $v2['id']] = isset($house[$v2['name']]) ? $house[$v2['name']] : 0;
- }
- return $data;
- }
- /**
- * 房屋户型
- */
- private function getHousetypeArrow($housetype, $field, $sign_ids)
- {
- $data = [];
- $house = [];
- foreach ($housetype as $k => $v) {
- @$house[$v][] = $k; // $v 是户型id,$k是客户id
- }
- // 每个户型的成交率
- foreach ($field as $k => $v) {
- $v_customer = $house[$v['id']] ?? [];
- $v_sign = array_intersect($v_customer, $sign_ids);
- $data['housetype_arrow_lv_' . $v['id']] = count($v_customer) > 0 ? round(count($v_sign) / count($v_customer) * 100, 2) . '%' : '0%';
- }
- // 每个户型的客户数
- foreach ($field as $k => $v) {
- $data['housetype_arrow_' . $v['id']] = isset($house[$v['id']]) ? count($house[$v['id']]) : 0;
- }
- return $data;
- }
- /**
- * 弹框页面
- */
- public function generalOpen()
- {
- $eid = input('eid',);
- View::assign('eid', $eid);
- $type = input('type', '');
- $start_date = input('start_date', '');
- $end_date = input('end_date', '');
- $source_id = input('source_id', '');
- if ($start_date) {
- $ls_date = explode(' - ', $start_date);
- if (count($ls_date) == 2) {
- $start_date = $ls_date[0];
- $end_date = $ls_date[1];
- }
- }
- $from = input('from', '', 'trim');
- $field = [
- ['field' => 'name', 'title' => '姓名'],
- ['field' => 'phone', 'title' => '电话']
- ];
- $url = url("empcrm/generalOpenData", ["eid" => $eid, 'type' => $type, 'end_date' => $end_date, 'start_date' => $start_date, 'from' => $from, 'source_id' => $source_id]);
- View::assign('fields', json_encode($field));
- View::assign('url', $url);
- $typehtml = 'add_wechat';
- if ($type == 'invalid_customer_count') $typehtml = 'invalid_popup';
- return View::fetch($typehtml);
- }
- /**
- * 弹框列表
- */
- public function generalOpenData()
- {
- $request = request();
- $param = $request->only(['page' => 1, 'limit' => 10, 'eid' => 0, 'type' => '', 'start_date' => '', 'end_date' => '', 'from' => '', 'source_id' => '']);
- if ($param['start_date'] && $param['end_date']) {
- $param['start_date'] = date('Y-m-d H:i:s', strtotime($param['start_date']));
- $param['end_date'] = date('Y-m-d 23:59:59', strtotime($param['end_date']));
- }/* else{
- $param['start_date'] = '2010-01-01 00:00:00';
- $param['end_date'] = date('Y-m-d H:i:s',time());
- } */
- $root_id = request()->empcrm->root_id;
- $where = [
- ['employee_id', '=', $param['eid']]
- ];
- //$where[] = ['org_id','in',$orgids];
- if ($param['start_date'] && $param['end_date']) {
- $s_date = date('Y-m-d 00:00:00', strtotime($param['start_date']));
- $e_date = date('Y-m-d 23:59:59', strtotime($param['end_date']));
- $date_where = [$s_date, $e_date];
- }
- if ($param['type'] == 'valid') {
- if (!empty($date_where)) {
- $where[] = ['sign_time', 'between', $date_where];
- }
- //有效客户数量
- $state1 = Customer::changeState('待确认', 'chaos');
- $state2 = Customer::changeState('无效', 'chaos');
- $where[] = ['state', 'not in', array_merge($state1, $state2)];
- $where[] = ['died', '<>', 2];
- $list = Customer::where($where)->column('id');
- //2023-03-18 3.3客户标记无效后不能影响数据统计内的有效数据、线索数据 https://kdocs.cn/l/cfx92KlU838H
- $no_where[] = ['employee_id', '=', $param['eid']];
- $no_where[] = ['status', '=', 1];
- $s_date = date('Y-m-d 00:00:00', strtotime($param['start_date']));
- $e_date = date('Y-m-d 23:59:59', strtotime($param['end_date']));
- if (!empty($date_where)) {
- $no_where[] = ['cus_addtime', 'between', $date_where];
- }
- $no_valid_customer = CustomerInvalidLog::where($no_where)->group('customer_id')->column('customer_id');
- $list = array_unique(array_merge($list, $no_valid_customer));
- } elseif ($param['type'] == 'follow_customer_count') {
- //在跟进客户数
- $where[] = ['died', '<>', 2];
- if (!empty($date_where)) {
- $where[] = ['sign_time', 'between', $date_where];
- }
- $countor1[] = ['crm_res_id', 'NULL', null];
- $countor1[] = ['state', 'not in', Customer::changeState('无效', 'chaos')];
- $countor2[] = ['crm_res_id', 'NOTNULL', null];
- $countor2[] = ['state', 'not in', array_merge(Customer::changeState('待确认', 'chaos'), Customer::changeState('无效', 'chaos'))];
- $list = Customer::where($where)->where(function ($query) use ($countor1, $countor2) {
- $query->whereOr([$countor1, $countor2]);
- })->column('id');
- } elseif ($param['type'] == 'add_wechat_count') {
- //加微客户数
- $cids = [];
- $where[] = ['died', '<>', 2];
- if (!empty($date_where)) {
- $where[] = ['add_wechat_time', 'between', $date_where];
- }
- $countor1[] = ['crm_res_id', 'NULL', null];
- $countor1[] = ['state', 'not in', Customer::changeState('无效', 'chaos')];
- $countor2[] = ['crm_res_id', 'NOTNULL', null];
- $countor2[] = ['state', 'not in', Customer::changeState('待确认', 'chaos')];
- $cids = Customer::where($where)->where(function ($query) use ($countor1, $countor2) {
- $query->whereOr([$countor1, $countor2]);
- })->column('id');
- $list = Customer::where([['id', 'in', $cids]])->column('id');
- } elseif ($param['type'] == 'not_sure_count') {
- //待确认客户数
- $where[] = ['died', '<>', 2];
- $where[] = ['crm_res_id', 'NULL', null];
- $where[] = ['state', 'in', Customer::changeState('待确认', 'chaos')];
- if (!empty($date_where)) {
- $where[] = ['sign_time', 'between', $date_where];
- }
- $list = Customer::where($where)->column('id');
- } elseif (in_array($param['type'], ['yixiang_assigned_count', 'liangfang_assigned_count', 'daodian_assigned_count'])) {
- //意向分派客户数
- $all_assign_type = ['yixiang_assigned_count' => 'yixiang', 'liangfang_assigned_count' => 'liangfang', 'daodian_assigned_count' => 'daodian'];
- if (empty($all_assign_type[$param['type']])) return json(['code' => 1, 'data' => '', 'count' => 0, 'msg' => '参数错误']);
- $where[] = ['died', '<>', 2];
- $where[] = ['assign_type', '=', $all_assign_type[$param['type']]];
- if (!empty($date_where)) {
- $where[] = ['sign_time', 'between', $date_where];
- }
- $list = Customer::where($where)->column('id');
- } elseif (in_array($param['type'], ['measuring_room_customer', 'strtore_customer_count', 'deposit_count', 'sign_count', 'invalid_customer_count'])) {
- //量房,到店,签单,转单客户数
- $cids = [];
- $all_state_type = ['measuring_room_customer' => '已量房', 'strtore_customer_count' => '已到店', 'deposit_count' => '已交定', 'sign_count' => '已签单', 'invalid_customer_count' => '无效'];
- if (empty($all_state_type[$param['type']])) return json(['code' => 1, 'data' => '', 'count' => 0, 'msg' => '参数错误']);
- if (!empty($date_where)) {
- if ($param['type'] == 'invalid_customer_count') {
- $invalid_where[] = ['confirm_date', 'between', $date_where];
- } else {
- $db_where[] = ['CustomerVisitLog.confirm_date', 'between', $date_where];
- }
- }
- switch ($param['type']) {
- case 'measuring_room_customer':
- $state = CustomerVisitLog::changeState('已量房', 'chaos');
- break;
- case 'strtore_customer_count':
- $state = CustomerVisitLog::changeState('已到店', 'chaos');
- break;
- case 'deposit_count':
- $state = CustomerVisitLog::changeState('已交定', 'chaos');
- break;
- case 'sign_count':
- $state = CustomerVisitLog::changeState('已签单', 'chaos');
- break;
- case 'invalid_customer_count':
- $state = CustomerVisitLog::changeState('无效', 'chaos');
- break;
- default:
- $state = '';
- break;
- }
- if ($state) {
- $db_where[] = ['CustomerVisitLog.state', 'in', $state];
- $invalid_where[] = ['state', 'in', $state];
- }
- if (in_array($param['type'], ['valid', 'invalid_customer_count'])) {
- $invalid_where[] = ['employee_id', '=', $param['eid']];
- $list = CustomerVisitLog::where($invalid_where)->group('customer_id')->column('customer_id');
- } else {
- $db_where[] = ['Customer.employee_id', '=', $param['eid']];
- $db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
- $cids = Db::view('Customer')
- ->view('CustomerVisitLog', '', 'Customer.id=CustomerVisitLog.customer_id')
- ->where($db_where)
- ->group('Customer.id')
- ->column('Customer.id');
- $list = Customer::where([['id', 'in', $cids]])->where($where)->column('id');
- }
- }
- //$list = $list->page($param['page'], $param['limit'])->select()->toArray();
- if (empty($list)) return json(['code' => 0, 'data' => [], 'count' => 0, 'msg' => '获取成功']);
- $count = count($list);
- $cuwhere = [['id', 'in', $list]];
- $page = intval($param['page']);
- $limit = intval($param['limit']);
- $order = isset($param['order']) ? $param['order'] : 'id desc';
- $con_union = [];
- // 如果搜索条件有来源
- if (!empty($param['source_id'])) {
- $cuwhere[] = ['source_id', '=', $param['source_id']];
- $con_union[] = ['source_id', '=', $param['source_id']];
- }
- if ($param['type'] == 'invalid_customer_count') {
- $con_union[] = ['customer_id', 'in', $list];
- $list = $this->selCustomerValid($cuwhere, $con_union, $page, $limit, $order, $root_id);
- } else {
- $list = $this->selCustomer($cuwhere, $page, $limit, $order, $root_id);
- }
- // foreach ($list as $k => $v) {
- // $list[$k]['phone'] = substr_replace($v['phone'], '******', 3, 6);
- // $list[$k]['name'] = empty($v['name']) ? '未知' : $v['name'];
- // }
- return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
- }
- /**
- * 外呼绑定
- */
- public function bind()
- {
- $params = request()->only(['phone', 'cid']);
- $emp = request()->empcrm;
- $where = [
- ['id', '=', $params['cid']],
- ['state', '<>', '无效'],
- ['employee_id', '=', $emp['id']]
- ];
- //指派的客户
- $where_or = [['assigned_personnel', 'find in set', $emp['id']], ['id', '=', $params['cid']]];
- $where_or1 = [['designer_id', '=', $emp['id']], ['id', '=', $params['cid']]];
- $had = Customer::whereOr([$where, $where_or, $where_or1])->find();
- if (!$had) return json(['code' => 1, 'msg' => '数据不存在']);
- if ($had->died == 2) {
- return json(['code' => 1, 'msg' => '客户死单,暂无法拨打电话']);
- }
- // 查询客户电话
- $customerPhone = $had->phone;
- // 查询员工电话
- $employee = $emp;
- if (!empty($params['phone'])) {
- $employeePhone = ($employee->phone == $params['phone'] || strpos($employee->out_call_phone, $params['phone']) !== false) ? $params['phone'] : $employee->phone;
- if ($employeePhone == '') return json(['code' => 1, 'msg' => '未绑定的手机号']);
- // } else {
- // $employeePhone = $employee->phone;
- }
- // 电话绑定
- $outCallSetting = OutCallSetting::where(['type' => 'yhuatong', 'root_id' => $emp['root_id']])->find();
- if (empty($outCallSetting)) return json(['code' => 1, 'msg' => '未配置外呼系统']);
- $yhuatong = new Yhuatong($outCallSetting->config['appid'], $outCallSetting->config['secretKey']);
- // 中间号获取
- $telXList = explode(',', $outCallSetting->config['tel_x']);
- $randKey = array_rand($telXList);
- $telX = $telXList[$randKey];
- // 获取行业id
- $industryId = $outCallSetting->config['industry'][0]['industry_id'];
- $rs = $yhuatong->bindv2($employeePhone, $customerPhone, $telX, $industryId);
- if ($rs['code'] !== 10000) {
- trace($rs['msg'], 'error');
- return json(['code' => 1, 'msg' => $rs['msg']]);
- } else {
- // 添加记录
- $log = OutCallLog::create([
- 'session_id' => $rs['data']['sessionId'],
- 'bind_num' => $rs['data']['telX'],
- 'caller_num' => $employeePhone,
- 'callee_num' => $customerPhone,
- 'employee_id' => $emp['id'],
- 'root_id' => $emp['root_id'],
- 'customer_id' => $params['cid']
- ]);
- }
- $had->updatetime = date('Y-m-d H:i:s');
- $had->save();
- if ($had->employee_id == $emp['id'])
- $customerEmployee = $emp;
- else
- $customerEmployee = Employee::find($had->employee_id);
- // 添加访问记录
- CustomerVisitLog::Create([
- 'customer_id' => $params['cid'],
- 'type' => 1,
- 'employee_id' => $emp['id'],
- 'user_id' => $employee->uid,
- 'org_id' => $employee->org_id,
- 'customer_employee_id' => $had->employee_id,
- 'customer_org_id' => $customerEmployee->org_id,
- 'data_type' => 'out_call',
- 'data_id' => $log->id,
- 'remark' => '回访跟踪了客户'
- ]);
- return json(['code' => 0, 'msg' => '拨打中,请接听']);
- }
- /**
- * 快速状态跟进
- */
- public function kb()
- {
- $request = request();
- $empcrm = $request->empcrm;
- $employeeTypeName = $empcrm->is_manager == 1 ? '部门领导' : ($empcrm->org_type == 2 ? '设计师' : '销售');
- // 请求参数设置
- $stateParam = [
- 3 => ['money', 'confirm_date'],
- 2 => ['money', 'confirm_date', 'estimated_sign_money'],
- 7 => ['confirm_date'],
- 8 => ['confirm_date'],
- 15 => ['money', 'confirm_date']
- ];
- $param = $request->only(['customer_id', 'state', 'money', 'add_wechat_time', 'vlogid']);
- //2022-10-27 逻辑修改,跟进保护优化 如果后台设置了重复客户跟进保护规则 跟进到保护状态后其他业务员的此重复客户就不能在跟进了,但是可以置为无效
- $forbiddenState = $this->forbiddenState($param['customer_id'], null);
- if ($forbiddenState != false) return json(['code' => 1, 'msg' => $forbiddenState]);
- // 数据查找
- $where = [
- ['id', '=', $param['customer_id']],
- ['state', '<>', '无效']
- ];
- if ($empcrm->is_manager == 1) {
- $team_orgs = orgSubIds($request->empcrm->org_id);
- $where[] = ['org_id', 'in', $team_orgs];
- } else {
- $where[] = ['employee_id|designer_id|assigned_personnel', 'find in set', $empcrm->id];
- }
- $had = Customer::where($where)->find();
- if ($had === null) return json(['code' => 1, 'msg' => '添加失败,数据不存在']);
- if (!empty($param['add_wechat_time'])) {
- $ext = $had->ext;
- $ext = json_decode($ext, true);
- $ei = false;
- foreach ($ext as &$e) {
- if ($e['keyname'] == 'add_wechat_time') {
- $e['value'] = date('Y-m-d', strtotime($param['add_wechat_time']));
- $ei = true;
- }
- }
- if ($ei === false) {
- $fid = CustomerPortraitField::where(['root_id' => $empcrm->root_id, 'keyname' => 'add_wechat_time'])->value('id');
- if ($fid) $ext[] = ['id' => $fid, 'keyname' => 'add_wechat_time', 'value' => date('Y-m-d', strtotime($param['add_wechat_time']))];
- }
- $had->ext = json_encode($ext);
- }
- if (empty($param['state']) || !in_array($param['state'], [2, 3, 7, 8, 15])) {
- $had->save();
- return json(['code' => 0, 'msg' => '保存成功']);
- } else {
- $customerVisitLogState = CustomerVisitLog::changeState($param['state'], 'n');
- $stateStr = CustomerVisitLog::changeState($param['state']);
- $customerState = Customer::changeState($stateStr, 'n');
- $stateParamValue = $request->only($stateParam[$customerVisitLogState]);
- }
- // 在跟进之前,增加了设计师必填判断,得先查一下设置
- $log_state = CustomerVisitLog::changeState($param['state'], 'state');
- $state = Customer::changeState($log_state, 'state');
- $stateCheck = ['已到店' => 'daodian', '已到场' => 'daochang', '已量房' => 'liangfang', '已交定' => 'qiandan', '已签单' => 'zhuandan'];
- if (in_array($state, ['已到店', '已到场', '已量房', '已交定', '已签单'])) {
- $need_designer[] = ['root_id', '=', $empcrm['root_id']];
- $need_designer[] = ['name', '=', 'add_visit_log_need_designer'];
- $need_designer_setting = Setting::where($need_designer)->findOrEmpty();
- if (!$need_designer_setting->isEmpty()) {
- $need_state = explode(',', $need_designer_setting['content']);
- $input_state = $stateCheck[$state] ?? '';
- if (!empty($input_state) && in_array($input_state, $need_state)) {
- // 需要验证
- $customer_find = Customer::find($param['customer_id']);
- if (!empty($customer_find)) {
- if (empty($customer_find['assigned_personnel'])) {
- return json(['code' => 1, 'msg' => '请先指派设计师']);
- }
- $assigned_personnel = explode(',', $customer_find['assigned_personnel']);
- $assigned_personnel_employee_org = Employee::where('id', 'in', $assigned_personnel)->column('org_id');
- $assigned_personnel_org_type = Org::where('id', 'in', $assigned_personnel_employee_org)->column('org_type');
- if (!in_array(2, $assigned_personnel_org_type)) {
- return json(['code' => 1, 'msg' => '请先指派设计师']);
- }
- }
- }
- }
- }
- // 量房到店保护机制
- $protectedDay = 0;
- if (7 == $customerVisitLogState || 8 == $customerVisitLogState) {
- // 保护设置
- $sub_setting = Setting::where([['root_id', '=', $empcrm->root_id], ['name', '=', 'subscribe_protected']])->findOrEmpty();
- if (!$sub_setting->isEmpty()) { // 有设置
- $setting_content = json_decode($sub_setting['content'], true);
- // 判断保护是否开启
- $bh = [];
- foreach ($setting_content as $k_s => $v_s) {
- if (!empty($v_s['state']) && !empty($v_s['day']) && $v_s['state'] == 1) {
- $bh[$k_s] = $v_s['day'];
- if (($k_s == 'ydaodian' && 7 == $customerVisitLogState) || ($k_s == 'yliangfang' && 8 == $customerVisitLogState)) $protectedDay = $v_s['day'];
- }
- }
- $phones = [$had->phone, $had->phone1, $had->phone2];
- $phones = array_filter($phones);
- // 如果设置了保护
- if (!empty($bh)) {
- foreach ($bh as $k => $d) {
- $condition = $this->protectCondition($k, $d);
- if (!empty($condition['log'])) {
- $e = CustomerVisitLog::withJoin('customer')->where([['Customer.id', '<>', $param['customer_id']]])->where($condition['log'])
- ->where(function ($query) use ($phones) {
- foreach ($phones as $p) {
- $c = [
- [['customer.phone', '=', $p]],
- [['customer.phone1', '=', $p]],
- [['customer.phone2', '=', $p]]
- ];
- $query->whereOr($c);
- }
- })->find();
- if ($e) return json(['code' => 1, 'msg' => '该客户在保护期内,无法提交']);
- }
- if (!empty($condition['subscribe'])) {
- $e = CustomersSubscribe::withJoin('customer')->where([['Customer.id', '<>', $param['customer_id']]])->where($condition['subscribe'])
- ->where(function ($query) use ($phones) {
- foreach ($phones as $p) {
- $c = [
- [['customer.phone', '=', $p]],
- [['customer.phone1', '=', $p]],
- [['customer.phone2', '=', $p]]
- ];
- $query->whereOr($c);
- }
- })
- ->count();
- if ($e > 0) return json(['code' => 1, 'msg' => '该客户在保护期内,无法提交']);
- }
- }
- }
- }
- }
- $otherMsg = '';
- // 更改客户信息
- $orders = false;
- //签单之后不能签单交定,可以跟进
- if (3 == $customerVisitLogState) {
- $is = CustomerVisitLog::where([['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')], ['customer_id', '=', $had->id]])->count();
- if ($is) return $this->updateMoney($param);
- $orders = true;
- $had->signed_money = $stateParamValue['money'];
- $otherMsg = ",签单金额:" . $stateParamValue['money'];
- }
- if (2 == $customerVisitLogState) {
- $is = CustomerVisitLog::where([['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')], ['customer_id', '=', $had->id]])->count();
- if ($is) return json(['code' => 1, 'msg' => '该客户已签单']);
- $orders = true;
- $had->deposit_money = $stateParamValue['money'];
- $had->estimated_sign_money = $stateParamValue['estimated_sign_money'];
- $otherMsg = ",定金金额:" . $stateParamValue['money'] . ', 预计签单金额' . $stateParamValue['estimated_sign_money'];
- }
- if (15 == $customerVisitLogState) $otherMsg = ",无忧金额:" . $stateParamValue['money'];
- Db::startTrans();
- //如果是分派的客户第一次跟进直接修改分派信息为空
- if (!empty($had->transfer_info)) $had->transfer_info = NULL;
- //如果是第一次有效修改有效时间
- if (in_array($param['state'], [2, 3, 7, 8]) && $had->state == '待确认') {
- $had->valid_time = !empty($stateParamValue['confirm_date']) ? $stateParamValue['confirm_date'] : date('Y-m-d H:i:s');
- }
- $had->state = $customerState;
- $had->last_contact_date = date('Y-m-d', time());
- $had->updatetime = date('Y-m-d H:i:s', time());
- $had->fresh = 0;
- // 更新客户保护期限
- if ($protectedDay) $had->protected_to = date('Y-m-d', time() + $protectedDay * 86400);
- $had->save();
- // 添加确定消息内容
- $confirmMsg = '';
- if (!empty($stateParamValue['confirm_date'])) {
- $confirmMsg = ', 时间:' . $stateParamValue['confirm_date'];
- }
- // 备注
- $employeeName = Employee::where(['id' => $empcrm->id])->value('name');
- $remark = '';
- $remark = $employeeTypeName . $employeeName . '更新客户状态为' . $stateStr . $confirmMsg . $otherMsg;
- $is_set = Setting::where([['root_id', '=', $empcrm->root_id], ['name', '=', 'nextVisitTimeIsOk']])->value('content');
- // 添加追踪记录
- $visitLog = [
- 'customer_id' => $had->id,
- 'type' => '',
- 'next_contact_date' => $is_set ? date('Y-m-d') : null,
- 'employee_id' => $empcrm->id,
- 'user_id' => $empcrm->uid,
- 'remark' => $remark,
- 'state' => $customerVisitLogState,
- 'money' => $stateParamValue['money'] ?? 0,
- 'confirm_date' => $stateParamValue['confirm_date'] ?? date('Y-m-d H:i:s'),
- 'org_id' => $empcrm['org_id'],
- 'customer_employee_id' => $had->employee_id,
- 'customer_org_id' => Employee::where('id', $had->employee_id)->value('org_id'),
- ];
- $visitLog = CustomerVisitLog::create($visitLog);
- if ($orders) $this->preformance_tasks($stateStr, $param['customer_id'], $stateParamValue['money'], $visitLog->id); //计算业绩
- // //如果签单,默认交定
- // if ($customerVisitLogState == 3) {
- // $lw[] = CustomerVisitLog::changeState(['state', '=', '交定']);
- // $lw[] = ['customer_id', '=', $param['customer_id']];
- // $check = CustomerVisitLog::where($lw)->find();
- // if (!$check) {
- // $l_visitlog = $visitLog->toArray();
- // unset($l_visitlog['id']);
- // $l_visitlog['state'] = '交定';
- // $l_visitlog['remark'] = $employeeTypeName . $employeeName . '变更客户状态为交定';
- // $new_visitlog = array_merge($param, $l_visitlog);
- // $new_visitlog['money'] = 0; //直接签单默认交定金额为0
- // $mr = CustomerVisitLog::create($new_visitlog);
- // $this->preformance_tasks('已交定', $param['customer_id'], $stateParamValue['money'], $mr->id); //计算业绩
- // }
- // }
- if ($customerVisitLogState == 7 || $customerVisitLogState == 8) {
- $type = $customerVisitLogState == 7 ? 1 : 3;
- $subscribe = CustomersSubscribe::where([['customer_id', '=', $had['id']], ['type', '=', $type], ['state', '=', 0]])->find();
- if (!empty($subscribe)) {
- $subscribe->state = 1;
- $subscribe->save();
- }
- }
- //增加经纪人送积分功能
- if ($had->agents_id && in_array($customerVisitLogState, [2, 3, 7])) {
- $this->agents_integral($had->agents_id, $customerVisitLogState, $had->id);
- }
- // 微爆活动
- if (in_array($param['state'], [2, 3, 7, 8])) {
- $activity_data['customer_id'] = $param['customer_id'];
- $activity_data['employee_id'] = $had->employee_id;
- $activity_data['org_id'] = $had->org_id;
- switch ($param['state']) {
- case 7: // 已到店
- $activity_data['type'] = 2;
- break;
- case 8: // 已量房
- $activity_data['type'] = 3;
- break;
- case 2: // 已交定
- $activity_data['type'] = 4;
- break;
- case 3: // 已签单
- $activity_data['type'] = 5;
- break;
- case 9: // 已到场
- $activity_data['type'] = 7;
- break;
- default:
- break;
- }
- WechatActivityIntegral::addIntegral($activity_data, $empcrm['root_id']);
- }
- Db::commit();
- Console::call('medal', ['visit_log', (string)$empcrm->id, (string)$empcrm->root_id]); //勋章
- if ($empcrm->is_manager == 1) {
- // 经理回访后修改客户的待回访的状态
- Customer::where('id', $had->id)->update(['return_visit' => 0]);
- }
- // if ($param['state'] == 2) {
- // // 发送管理层模板消息
- // $msg = ['first'=>'行为通知', 'keyword1'=>'客户确认到店', 'keyword2'=>"客户".$had['name']."(".$had['phone'].")确认到店", 'keyword3'=>date('Y/m/d'), 'remark'=>'请悉知'];
- // TmpMsg::sendMsgToLeader($msg, $empcrm['org_id']);
- // }
- $had['name'] = !empty($had['name']) ? $had['name'] : '';
- $had['phone'] = substr_replace($had['phone'], '******', 3, 6);
- if ($param['state'] == 3) {
- // 发送管理层模板消息
- $msg = ['first' => '行为通知', 'keyword1' => '客户签单', 'keyword2' => "客户" . $had['name'] . "(" . $had['phone'] . ")签单", 'keyword3' => date('Y/m/d'), 'remark' => '请悉知'];
- TmpMsg::sendMsgToLeader($msg, $empcrm['org_id']);
- }
- if ($param['state'] == 7) {
- // 发送管理层模板消息
- $msg = ['first' => '行为通知', 'keyword1' => '客户确认到店', 'keyword2' => "客户" . $had['name'] . "(" . $had['phone'] . ")确认到店", 'keyword3' => date('Y/m/d'), 'remark' => '请悉知'];
- TmpMsg::sendMsgToLeader($msg, $empcrm['org_id']);
- }
- return json(['code' => 0, 'msg' => '跟进成功']);
- }
- private function protectCondition($k_s, $day)
- {
- $empcrm = request()->empcrm;
- $org_ids = orgSubIds($empcrm->root_id);
- $check_where = [];
- $v_where = [];
- switch ($k_s) {
- case 'liangfang':
- $v_where[] = [
- ['customers_subscribe.org_id', 'in', $org_ids],
- ['customers_subscribe.addtime', '>', date('Y-m-d H:i:s', time() - 24 * 3600 * $day)],
- ['customers_subscribe.employee_id', '<>', $empcrm->id],
- ['customers_subscribe.type', '=', 3],
- ['customers_subscribe.state', '=', 1]
- ];
- $check_where = [
- ['customer_visit_log.org_id', 'in', $org_ids],
- ['customer_visit_log.addtime', '>', date('Y-m-d H:i:s', time() - 24 * 3600 * $day)],
- ['customer_visit_log.employee_id', '<>', $empcrm->id],
- ['customer_visit_log.state', 'in', CustomerVisitLog::changeState('已量房', 'chaos')]
- ];
- break;
- case 'yliangfang': //预约量房
- $v_where[] = [
- ['customers_subscribe.org_id', 'in', $org_ids],
- ['customers_subscribe.addtime', '>', date('Y-m-d H:i:s', time() - 24 * 3600 * $day)],
- ['customers_subscribe.employee_id', '<>', $empcrm->id],
- ['customers_subscribe.type', '=', 3],
- ['customers_subscribe.state', '=', 0]
- ];
- break;
- case 'daodian':
- $v_where[] = [
- ['customers_subscribe.org_id', 'in', $org_ids],
- ['customers_subscribe.addtime', '>', date('Y-m-d H:i:s', time() - 24 * 3600 * $day)],
- ['customers_subscribe.employee_id', '<>', $empcrm->id],
- ['customers_subscribe.type', '=', 1],
- ['customers_subscribe.state', '=', 1]
- ];
- $check_where = [
- ['customer_visit_log.org_id', 'in', $org_ids],
- ['customer_visit_log.addtime', '>', date('Y-m-d H:i:s', time() - 24 * 3600 * $day)],
- ['customer_visit_log.employee_id', '<>', $empcrm->id],
- ['customer_visit_log.state', 'in', CustomerVisitLog::changeState('已到店', 'chaos')]
- ];
- break;
- case 'ydaodian':
- $v_where[] = [
- ['customers_subscribe.org_id', 'in', $org_ids],
- ['customers_subscribe.addtime', '>', date('Y-m-d H:i:s', time() - 24 * 3600 * $day)],
- ['customers_subscribe.employee_id', '<>', $empcrm->id],
- ['customers_subscribe.type', '=', 1],
- ['customers_subscribe.state', '=', 0]
- ];
- break;
- case 'daochang':
- $v_where[] = [
- ['customers_subscribe.org_id', 'in', $org_ids],
- ['customers_subscribe.addtime', '>', date('Y-m-d H:i:s', time() - 24 * 3600 * $day)],
- ['customers_subscribe.employee_id', '<>', $empcrm->id],
- ['customers_subscribe.type', '=', 2],
- ['customers_subscribe.state', '=', 1]
- ];
- $check_where = [
- ['customer_visit_log.org_id', 'in', $org_ids],
- ['customer_visit_log.addtime', '>', date('Y-m-d H:i:s', time() - 24 * 3600 * $day)],
- ['customer_visit_log.employee_id', '<>', $empcrm->id],
- ['customer_visit_log.state', 'in', CustomerVisitLog::changeState('已到场', 'chaos')]
- ];
- break;
- case 'ydaochang':
- $v_where[] = [
- ['customers_subscribe.org_id', 'in', $org_ids],
- ['customers_subscribe.addtime', '>', date('Y-m-d H:i:s', time() - 24 * 3600 * $day)],
- ['customers_subscribe.employee_id', '<>', $empcrm->id],
- ['customers_subscribe.type', '=', 2],
- ['customers_subscribe.state', '=', 0]
- ];
- break;
- }
- return ['log' => $check_where, 'subscribe' => $v_where];
- }
- public function addwuxiao_logs()
- {
- $root_id = 1035;
- $orgids = orgSubIds($root_id);
- $empids = Employee::where([['org_id', 'in', $orgids], ['state', '=', '在职']])->column('id');
- $list = CustomerVisitLog::where([['employee_id', 'in', $empids], ['state', 'in', CustomerVisitLog::changeState('无效', 'chaos')]])->column('id,state,customer_id,employee_id,customer_employee_id,customer_org_id,org_id');
- foreach ($list as $key => $val) {
- $cus = Customer::where('id', $val['customer_id'])->field('addtime,org_id,source_id')->find();
- if (empty($cus)) {
- $cus = CustomerRecycle::where('customer_id', '=', $val['customer_id'])->field('addtime,org_id,source_id')->find();
- // var_dump($cus->toArray());
- // exit;
- }
- if (empty($cus)) continue;
- $org_id = !empty($val['customer_org_id']) ? $val['customer_org_id'] : $val['org_id'];
- $org_id = $org_id ? $org_id : $cus['org_id'];
- $employee_id = !empty($val['customer_employee_id']) ? $val['customer_employee_id'] : $val['employee_id'];
- $yes = CustomerInvalidLog::where([['customer_id', '=', $val['customer_id']], ['employee_id', '=', $employee_id]])->count();
- if ($yes) continue;
- $youxiao = CustomerVisitLog::where([['customer_id', '=', $val['customer_id']], ['employee_id', '=', $employee_id], ['state', 'not in', array_merge(CustomerVisitLog::changeState('待确认', 'chaos'), CustomerVisitLog::changeState('无效', 'chaos'))], ['save_portrait_field', 'NULL', null]])->count();
- $add[] = [
- 'customer_id' => $val['customer_id'],
- 'employee_id' => $employee_id,
- 'org_id' => $org_id,
- 'root_id' => $root_id,
- 'cus_addtime' => $cus['addtime'],
- 'visitlog_id' => $val['id'],
- 'assigned_personnel' => '',
- 'source_id' => !empty($cus['source_id']) ? $cus['source_id'] : 0,
- 'status' => $youxiao ? 1 : 2
- ];
- }
- // var_dump($add);
- // exit;
- $addlog = new CustomerInvalidLog;
- $addlog->saveAll($add);
- echo '添加完成';
- }
- public function save_log()
- {
- // $org_id = Org::where([['path','like','%1035-%']])->column('id');
- // $empids = Employee::where([['org_id','in',$org_id],['state','=','在职']])->column('id');
- // $where = [
- // ['employee_id','in',$empids],
- // ['state','in',['无效',11]],
- // ['org_id','=',0]
- // ];
- // $list = CustomerVisitLog::where($where)->group('customer_id')->field('max(addtime) as addtime,id,customer_id,state')->select()->toArray();
- // //var_dump($list);exit;
- // foreach($list as $key=>$val){
- // $chaild = CustomerVisitLog::where([['customer_id','=',$val['customer_id']],['state','in',['无效',11]]])->count();
- // if($chaild == 1) continue;
- // CustomerVisitLog::where([['customer_id','=',$val['customer_id']],['id','<>',$val['id']]])->update(['state'=>1]);
- // //var_dump($val['customer_id']);exit;
- // }
- }
- /**
- * 飞鱼线索列表
- */
- public function fish_list()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $emp_id = $request->empcrm->id;
- // $emp_name = $request->empcrm->name;
- $emp_org_id = $request->empcrm->org_id;
- $is_manager = $request->empcrm->is_manager;
- // $path = Org::where([['pid','=',$emp_org_id]])->count();
- $param = $request->only(['page' => 1, 'limit' => 10, 'name', 'clue_id', 'tags', 'address', 'advertiser_id', 'is_allocation', 'is_contact', 'ad_name', 'promotion_id', 'weixin', 'qq', 'times', 'module_id', 'module_name', 'email', 'location', 'promotion_name', 'ad_id', 'telephone', 'clue_owner_name', 'clue_source', 'create_time_detail', 'clue_state_name', 'follow_state_name', 'order_type' => 0, 'is_fenpei', 'is_repeat' => 0, 'org_id' => '']);
- $clue_source = [0 => '外部流量', 1 => '正常投放', 2 => '外部导入', 3 => '异常提交', 4 => '广告预览', 5 => '抖音私信', 6 => '鲁班线索'];
- $clue_state = [1 => '新线索', 2 => '有意向', 3 => '转商机', 4 => '无效', 5 => '已加微信', 6 => '待再次沟通'];
- $clue_type = [0 => '表单提交', 1 => '在线咨询', 2 => '智能电话', 3 => '网页回呼', 4 => '卡券', 5 => '抽奖'];
- $table_tags = [
- [
- 'title' => '高级意向',
- 'name' => 'all0',
- 'value' => 0,
- 'tags' =>
- [
- ['title' => '已签约合同', 'name' => 'all0_1'],
- ['title' => '已缴纳定金', 'name' => 'all0_2'],
- ['title' => '客户已到店', 'name' => 'all0_3'],
- ['title' => '预约上门量房', 'name' => 'all0_4'],
- ]
- ],
- [
- 'title' => '中级意向',
- 'name' => 'all1',
- 'value' => 1,
- 'tags' =>
- [
- ['title' => '能提供户型图', 'name' => 'all1_1'],
- ['title' => '有装修预算', 'name' => 'all1_2'],
- ['title' => '添加微信待通过', 'name' => 'all1_3'],
- ['title' => '添加微信已通过', 'name' => 'all1_4'],
- ]
- ],
- [
- 'title' => '初级意向',
- 'name' => 'all2',
- 'value' => 2,
- 'tags' =>
- [
- ['title' => '仅咨询了解', 'name' => 'all2_1'],
- ['title' => '无真实需求', 'name' => 'all2_2'],
- ['title' => '对比其他平台', 'name' => 'all2_3'],
- ['title' => '对价格不反感', 'name' => 'all2_4'],
- ['title' => '近期有装修需求', 'name' => 'all2_5'],
- ['title' => '添加微信', 'name' => 'all2_6'],
- ]
- ],
- [
- 'title' => '意向',
- 'name' => 'all3',
- 'value' => 3,
- 'tags' =>
- [
- ['title' => '低意向', 'name' => 'all3_1'],
- ['title' => '中意向', 'name' => 'all3_2'],
- ['title' => '高意向', 'name' => 'all3_3'],
- ]
- ],
- [
- 'title' => '无效',
- 'name' => 'all4',
- 'value' => 4,
- 'tags' =>
- [
- ['title' => '添加微信未通过', 'name' => 'all4_1'],
- ['title' => '多次联系不上', 'name' => 'all4_2'],
- ['title' => '拒绝加微信', 'name' => 'all4_3'],
- ['title' => '无需求', 'name' => 'all4_4'],
- ['title' => '错号', 'name' => 'all4_5'],
- ['title' => '关机', 'name' => 'all4_6'],
- ['title' => '空号', 'name' => 'all4_7'],
- ['title' => '联系不上', 'name' => 'all4_8'],
- ['title' => '无意愿', 'name' => 'all4_9'],
- ['title' => '非本人', 'name' => 'all4_10']
- ]
- ],
- [
- 'title' => '默认',
- 'name' => 'all5',
- 'value' => 5,
- 'tags' =>
- [
- ['title' => '加微信', 'name' => 'all5_1'],
- ['title' => '待确认', 'name' => 'all5_2'],
- ['title' => '无效', 'name' => 'all5_3'],
- ]
- ],
- ];
- if (!$request->isAjax()) {
- $this->screen();
- $advertiser_list[] = ['id' => 1, 'name' => '高潜客户'];
- $channel_setting = Company::where(['root_id' => $root_id])->field('root_id,channel_setting,channel_setting_wh')->find();
- if (isset($channel_setting['channel_setting_wh']) && $channel_setting['channel_setting_wh']) {
- $tmp_data = json_decode($channel_setting['channel_setting_wh'], true);
- $advertiser_id = explode(',', $tmp_data['fish_adv']);
- $fish_account = explode(',', $tmp_data['fish_account']);
- foreach ($advertiser_id as $key => $val) {
- $advertiser_list[] = [
- 'id' => $advertiser_id[$key],
- 'name' => isset($fish_account[$key]) ? $fish_account[$key] : '暂未设置名称',
- ];
- }
- }
- if (isset($channel_setting['channel_setting']) && $channel_setting['channel_setting']) {
- $tmp_data = json_decode($channel_setting['channel_setting'], true);
- $advertiser_id = explode(',', $tmp_data['fish_adv']);
- $fish_account = explode(',', $tmp_data['fish_account']);
- foreach ($advertiser_id as $key => $val) {
- $advertiser_list[] = [
- 'id' => $advertiser_id[$key],
- 'name' => isset($fish_account[$key]) ? $fish_account[$key] : '暂未设置名称',
- ];
- }
- }
- $team_orgs = $is_manager ? orgSubIds($emp_org_id) : [$emp_org_id];
- $emporg_list = Org::where([['id', 'in', $team_orgs]])->column('id,name');
- View::assign('emporg_list', $emporg_list);
- View::assign('advertiser_id', $advertiser_list);
- View::assign('is_manager', $is_manager);
- View::assign('soudata_now', $clue_source);
- View::assign('clue_state', $clue_state);
- View::assign('table_tags', $table_tags);
- // $this->synchronize_fish_data();
- //todo 同步直播数据
- // $this->synchronize_fish_live_data();
- return View::fetch();
- }
- $org_employee = [$emp_id];
- if ($is_manager == 1) {
- $team_orgs = orgSubIds($emp_org_id);
- // $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
- $condition[] = ['org_id', 'in', $team_orgs];
- } else {
- $condition[] = ['employee_id', 'in', $org_employee];
- }
- $condition[] = ['root_id', '=', $root_id];
- // $condition[] = ['allocation_status', '=', 1];
- $condition[] = ['type', 'in', [1, 4]];
- if (!empty($param['name'])) $condition[] = ['name', 'like', '%' . $param['name'] . '%'];
- if (!empty($param['clue_id'])) $condition[] = ['clue_id', 'like', '%' . $param['clue_id'] . '%'];
- if (!empty($param['telephone'])) $condition[] = ['telephone', '=', $param['telephone']];
- if (!empty($param['tags'])) {
- $tags = explode(',', $param['tags']);
- $tmp_tags = [];
- foreach ($tags as $val) {
- $tmp_tags[] = "%{$val}%";
- }
- $condition[] = ['tags', 'like', $tmp_tags, 'OR'];
- }
- if (!empty($param['address'])) $condition[] = ['address', 'like', '%' . $param['address'] . '%'];
- if (!empty($param['advertiser_id'])) {
- if ($param['advertiser_id'] == 1) {
- $condition[] = ['advertiser_id', '=', 0];
- } else {
- $condition[] = ['advertiser_id', '=', $param['advertiser_id']];
- }
- }
- if (!empty($param['ad_name'])) $condition[] = ['ad_name', 'like', '%' . $param['ad_name'] . '%'];
- if (!empty($param['ad_id'])) $condition[] = ['ad_id', 'like', '%' . $param['ad_id'] . '%'];
- if (!empty($param['promotion_id'])) $condition[] = ['promotion_id', 'like', '%' . $param['promotion_id'] . '%'];
- if (!empty($param['promotion_name'])) $condition[] = ['promotion_name', 'like', '%' . $param['promotion_name'] . '%'];
- if (!empty($param['weixin'])) $condition[] = ['weixin', 'like', '%' . $param['weixin'] . '%'];
- if (!empty($param['qq'])) $condition[] = ['qq', 'like', '%' . $param['qq'] . '%'];
- if (!empty($param['email'])) $condition[] = ['email', 'like', '%' . $param['email'] . '%'];
- if (!empty($param['location'])) $condition[] = ['location', 'like', '%' . $param['location'] . '%'];
- if (!empty($param['module_id'])) $condition[] = ['module_id', 'like', '%' . $param['module_id'] . '%'];
- if (!empty($param['module_name'])) $condition[] = ['module_name', 'like', '%' . $param['module_name'] . '%'];
- if (!empty($param['clue_owner_name'])) $condition[] = ['clue_owner_name', 'in', explode(',', $param['clue_owner_name'])];
- if (!empty($param['clue_source'])) {
- $aa = [];
- foreach (explode(',', $param['clue_source']) as $vv) {
- $aa[] = $vv - 1;
- }
- $condition[] = ['clue_source', 'in', $aa];
- }
- if (!empty($param['is_allocation'])) $condition[] = ['is_allocation', '=', $param['is_allocation'] - 1];
- if (!empty($param['is_contact'])) $condition[] = ['is_contact', '=', $param['is_contact'] - 1];
- if (!empty($param['is_fenpei'])) {
- if ($param['is_fenpei'] == 2) $condition[] = ['employee_id', '>', 0];
- if ($param['is_fenpei'] == 1) $condition[] = ['employee_id', '=', 0];
- }
- if (!empty($param['create_time_detail'])) {
- $last_contact = explode(' - ', $param['create_time_detail']);
- $start = str_replace('/', '-', $last_contact[0]);
- $end = str_replace('/', '-', $last_contact[1]);
- $condition[] = ['create_time_detail', '>=', $start . ' 00:00:00'];
- $condition[] = ['create_time_detail', '<=', $end . ' 23:59:59'];
- }
- if (!empty($param['times'])) {
- $last_contact = explode(' - ', $param['times']);
- $start = str_replace('/', '-', $last_contact[0]);
- $end = str_replace('/', '-', $last_contact[1]);
- $condition[] = ['create_time_detail', '>=', $start . ' 00:00:00'];
- $condition[] = ['create_time_detail', '<=', $end . ' 23:59:59'];
- }
- if (!empty($param['clue_state_name'])) $condition[] = ['clue_state', 'in', explode(',', $param['clue_state_name'])];
- if (!empty($param['org_id'])) $condition[] = ['org_id', 'in', explode(',', $param['org_id'])];
- if (!empty($param['follow_state_name'])) $condition[] = ['follow_state_name', 'like', '%' . $param['follow_state_name'] . '%'];
- if (!empty($param['is_repeat'])) {
- $tmp = FishData::where($condition)->field('telephone,count(*) as c')->group('telephone')->having('c>1')->select()->toArray();
- $condition[] = ['telephone', 'in', array_column($tmp, 'telephone')];
- }
- $order = 'id desc';
- if ($param['order_type'] == 1) $order = 'create_time_detail asc';
- if ($param['order_type'] == 2) $order = 'create_time_detail desc';
- $page = intval($param['page']);
- $limit = intval($param['limit']);
- $data = FishData::where($condition)->page($page, $limit)->order($order)->select();
- foreach ($data as $key => $val) {
- $data[$key]['clue_owner_name'] = empty($val['clue_owner_name']) ? ' ' : $val['clue_owner_name']; //Employee::where('id','=',$val['employee_id'])->value('name');
- $data[$key]['telephone'] = substr_replace($val['telephone'], '******', 3, 4);
- $data[$key]['gender'] = ['未知', '男', '女'][$val['gender']];
- $data[$key]['clue_source'] = $clue_source[$val['clue_source']];
- $data[$key]['allocation_status'] = $val['is_allocation'] == 1 ? '已分配' : '未分配';
- $data[$key]['clue_type'] = $clue_type[$val['clue_type']];
- $data[$key]['is_followup'] = 0;
- if ($is_manager == 1) {
- $data[$key]['is_followup'] = 1;
- }
- if ($is_manager == 1 && $emp_id == $data[$key]['employee_id']) {
- $data[$key]['is_followup'] = 3;
- }
- if ($is_manager == 0 && $emp_id == $data[$key]['employee_id']) {
- $data[$key]['is_followup'] = 2;
- }
- $tmp = json_decode($val['remark_dict'], true);
- $data[$key]['remark_dict'] = implode(',', $tmp);
- $data[$key]['org_id'] = Org::where(['id' => $val['org_id']])->value('name');
- }
- $count = FishData::where($condition)->count();
- return json(['code' => 0, 'data' => $data, 'count' => $count]);
- }
- /**
- * 飞鱼线索统计
- */
- public function fish_list_total()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- // $emp_id = $request->empcrm->id;
- // $emp_name = $request->empcrm->name;
- $emp_org_id = $request->empcrm->org_id;
- $is_manager = $request->empcrm->is_manager;
- // $path = Org::where([['pid','=',$emp_org_id]])->count();
- $param = $request->only(['advertiser_id', 'times']);
- $total = [];
- if ($is_manager == 1) {
- $orgids = orgSubIds($emp_org_id);
- $state1 = Customer::changeState('待确认', 'chaos');
- $state2 = Customer::changeState('无效', 'chaos');
- $state3 = CustomerVisitLog::changeState('已交定', 'chaos');
- $state4 = CustomerVisitLog::changeState('已签单', 'chaos');
- $state5 = CustomerVisitLog::changeState('确认到店', 'chaos');
- $state6 = CustomerVisitLog::changeState('已到场', 'chaos');
- $state7 = CustomerVisitLog::changeState('已量房', 'chaos');
- $FishCampaign[] = ['type', 'in', [1, 4]];
- $FishCampaign[] = ['root_id', '=', $root_id];
- $FishData[] = ['type', 'in', [1, 4]];
- $FishData[] = ['root_id', '=', $root_id];
- $FishData[] = ['org_id', 'in', $orgids];
- $sourceids = CustomerSource::where([['root_id', '=', $root_id], ['source', '=', '飞鱼线索']])->value('id');
- $wherev[] = ['CustomerVisitLog.customer_org_id', 'in', $orgids];
- $wherev[] = ['CustomerVisitLog.state', 'in', array_merge($state1, $state2, $state3, $state4, $state5, $state6, $state7)];
- if (!empty($param['advertiser_id'])) {
- $FishCampaign[] = ['advertiser_id', '=', $param['advertiser_id']];
- if ($param['advertiser_id'] == 1) {
- $FishData[] = ['advertiser_id', '=', 0];
- } else {
- $FishData[] = ['advertiser_id', '=', $param['advertiser_id']];
- }
- }
- if (!empty($param['times'])) {
- $last_contact = explode(' - ', $param['times']);
- $start = str_replace('/', '-', $last_contact[0]);
- $end = str_replace('/', '-', $last_contact[1]);
- // $FishCampaign[] = ['data_time', '>=', $start . ' 00:00:00'];
- $FishCampaign[] = ['data_time', 'between', [$start, $end]];
- $wherev[] = ['CustomerVisitLog.confirm_date', 'between', [$start, $end]];
- $FishData[] = ['create_time', 'between', [$start, $end]];
- }
- $fish_customer_id = FishData::where($FishData)->where(['is_allocation' => 1])->column('customer_id');
- $wherev[] = ['CustomerVisitLog.customer_id', 'in', $fish_customer_id];
- $total['totalNum2'] = FishData::where($FishData)->count(); //线索数
- $w = Customer::where('source_id', '=', $sourceids);
- $visitlog_list = CustomerVisitLog::hasWhere('customer', $w)->where($wherev)
- ->group('state,customer_id')
- ->column('CustomerVisitLog.customer_id,CustomerVisitLog.state,customer.deposit_money,customer.signed_money');
- $total['totalNum0'] = FishCampaign::where($FishCampaign)->sum('cost'); //总消耗
- $total['totalNum3'] = $total['totalNum0'] > 0 && $total['totalNum2'] > 0 ? round($total['totalNum0'] / $total['totalNum2'], 2) : 0;
- $total['totalNum4'] = count($fish_customer_id);
- $total['totalNum5'] = $total['totalNum0'] > 0 && $total['totalNum4'] > 0 ? round($total['totalNum0'] / $total['totalNum4'], 2) : 0;
- $total['totalNum6'] = $total['totalNum4'] > 0 && $total['totalNum2'] > 0 ? round($total['totalNum4'] / $total['totalNum2'], 2) * 100 . '%' : '0.00%';
- $sign_data = $reach_data = $shop_id = [];
- $reach = $sign = $shop = $house = $all_money = $basic_money = 0;
- foreach ($visitlog_list as $v) {
- //处理签单
- if (in_array($v['state'], $state4, true)) {
- $sign++;
- $basic_money += $v['signed_money'];
- $all_money += $v['signed_money'];
- $sign_data[] = $v['customer_id'];
- }
- //处理交定
- if (in_array($v['state'], $state3, true)) {
- $reach++;
- $reach_data[] = $v;
- }
- if (in_array($v['state'], $state5, true)) {
- $shop++;
- $shop_id[] = $v['customer_id'];
- }
- if (in_array($v['state'], $state7, true)) $house++;
- }
- foreach ($reach_data as $v) {
- if (!in_array($v['customer_id'], $sign_data)) {
- $all_money += $v['deposit_money'];
- }
- }
- $total['totalNum1'] = $all_money;
- $total['totalNum7'] = $house;
- $total['totalNum8'] = $house > 0 && $total['totalNum4'] > 0 ? round($house / $total['totalNum4'], 2) * 100 . '%' : '0.00%';
- $total['totalNum9'] = $shop;
- $total['totalNum10'] = $shop > 0 && $total['totalNum4'] > 0 ? round($shop / $total['totalNum4'], 2) * 100 . '%' : '0.00%';
- $total['totalNum11'] = $reach;
- $total['totalNum12'] = $reach > 0 && $total['totalNum4'] > 0 ? round($reach / $total['totalNum4'], 2) * 100 . '%' : '0.00%';
- $total['totalNum13'] = $sign;
- $total['totalNum14'] = $basic_money > 0 && $total['totalNum13'] > 0 ? round($basic_money / $total['totalNum13'], 2) : '0.00';
- $total['totalNum15'] = $sign > 0 && $total['totalNum4'] > 0 ? round($sign / $total['totalNum4'], 2) * 100 . '%' : '0.00%';
- $total['shop_id'] = $shop_id;
- }
- return json(['code' => 0, 'data' => $total]);
- }
- /**
- * 腾讯数据统计
- */
- public function tencent_list_total()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $emp_org_id = $request->empcrm->org_id;
- $is_manager = $request->empcrm->is_manager;
- $param = $request->only(['advertiser_id', 'times']);
- $total = [];
- if ($is_manager == 1) {
- $orgids = orgSubIds($emp_org_id);
- $state1 = Customer::changeState('待确认', 'chaos');
- $state2 = Customer::changeState('无效', 'chaos');
- $state3 = CustomerVisitLog::changeState('已交定', 'chaos');
- $state4 = CustomerVisitLog::changeState('已签单', 'chaos');
- $state5 = CustomerVisitLog::changeState('确认到店', 'chaos');
- $state6 = CustomerVisitLog::changeState('已到场', 'chaos');
- $state7 = CustomerVisitLog::changeState('已量房', 'chaos');
- $FishCampaign[] = ['type', '=', 2];
- $FishCampaign[] = ['root_id', '=', $root_id];
- $FishData[] = ['type', '=', 2];
- $FishData[] = ['root_id', '=', $root_id];
- $FishData[] = ['org_id', 'in', $orgids];
- $sourceids = CustomerSource::where([['root_id', '=', $root_id], ['source', '=', '腾讯线索']])->value('id');
- $wherev[] = ['CustomerVisitLog.customer_org_id', 'in', $orgids];
- $wherev[] = ['CustomerVisitLog.state', 'in', array_merge($state1, $state2, $state3, $state4, $state5, $state6, $state7)];
- if (!empty($param['advertiser_id'])) {
- $FishCampaign[] = ['advertiser_id', '=', $param['advertiser_id']];
- $FishData[] = ['advertiser_id', '=', $param['advertiser_id']];
- }
- if (!empty($param['times'])) {
- $last_contact = explode(' - ', $param['times']);
- $start = str_replace('/', '-', $last_contact[0]);
- $end = str_replace('/', '-', $last_contact[1]);
- $FishCampaign[] = ['data_time', 'between', [$start, $end]];
- $wherev[] = ['CustomerVisitLog.confirm_date', 'between', [$start, $end]];
- $FishData[] = ['create_time', 'between', [$start, $end]];
- }
- $fish_customer_id = FishData::where($FishData)->where(['is_allocation' => 1])->column('customer_id');
- if (!empty($fish_customer_id)) {
- $wherev[] = ['CustomerVisitLog.customer_id', 'in', $fish_customer_id];
- }
- $total['totalNum2'] = FishData::where($FishData)->count(); //线索数
- $w = Customer::where('source_id', '=', $sourceids);
- $visitlog_list = CustomerVisitLog::hasWhere('customer', $w)->where($wherev)
- ->group('state,customer_id')
- ->column('CustomerVisitLog.customer_id,CustomerVisitLog.state,customer.deposit_money,customer.signed_money');
- $total['totalNum0'] = FishCampaign::where($FishCampaign)->sum('cost'); //总消耗
- $total['totalNum3'] = $total['totalNum0'] > 0 && $total['totalNum2'] > 0 ? round($total['totalNum0'] / $total['totalNum2'], 2) : 0;
- $total['totalNum4'] = count($fish_customer_id);
- $total['totalNum5'] = $total['totalNum0'] > 0 && $total['totalNum4'] > 0 ? round($total['totalNum0'] / $total['totalNum4'], 2) : 0;
- $total['totalNum6'] = $total['totalNum4'] > 0 && $total['totalNum2'] > 0 ? round($total['totalNum4'] / $total['totalNum2'], 2) * 100 . '%' : '0.00%';
- $sign_data = $reach_data = [];
- $reach = $sign = $shop = $house = $all_money = $basic_money = 0;
- foreach ($visitlog_list as $v) {
- //处理签单
- if (in_array($v['state'], $state4, true)) {
- $sign++;
- $basic_money += $v['signed_money'];
- $all_money += $v['signed_money'];
- $sign_data[] = $v['customer_id'];
- }
- //处理交定
- if (in_array($v['state'], $state3, true)) {
- $reach++;
- $reach_data[] = $v;
- }
- if (in_array($v['state'], $state5, true)) $shop++;
- if (in_array($v['state'], $state7, true)) $house++;
- }
- foreach ($reach_data as $v) {
- if (!in_array($v['customer_id'], $sign_data)) {
- $all_money += $v['deposit_money'];
- }
- }
- $total['totalNum1'] = $all_money;
- $total['totalNum7'] = $house;
- $total['totalNum8'] = $house > 0 && $total['totalNum4'] > 0 ? round($house / $total['totalNum4'], 2) * 100 . '%' : '0.00%';
- $total['totalNum9'] = $shop;
- $total['totalNum10'] = $shop > 0 && $total['totalNum4'] > 0 ? round($shop / $total['totalNum4'], 2) * 100 . '%' : '0.00%';
- $total['totalNum11'] = $reach;
- $total['totalNum12'] = $reach > 0 && $total['totalNum4'] > 0 ? round($reach / $total['totalNum4'], 2) * 100 . '%' : '0.00%';
- $total['totalNum13'] = $sign;
- $total['totalNum14'] = $basic_money > 0 && $total['totalNum13'] > 0 ? round($basic_money / $total['totalNum13'], 2) : '0.00';
- $total['totalNum15'] = $sign > 0 && $total['totalNum4'] > 0 ? round($sign / $total['totalNum4'], 2) * 100 . '%' : '0.00%';
- }
- return json(['code' => 0, 'data' => $total]);
- }
- /**
- * 腾讯线索
- */
- public function tencent_list()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $emp_id = $request->empcrm->id;
- // $emp_name = $request->empcrm->name;
- $emp_org_id = $request->empcrm->org_id;
- $is_manager = $request->empcrm->is_manager;
- $path = Org::where([['pid', '=', $emp_org_id]])->count();
- $param = $request->only(['page' => 1, 'limit' => 10, 'name', 'clue_id', 'tags', 'address', 'advertiser_id', 'is_allocation', 'is_contact', 'ad_name', 'promotion_id', 'weixin', 'qq', 'times', 'module_id', 'module_name', 'email', 'location', 'promotion_name', 'ad_id', 'telephone', 'clue_owner_name', 'clue_source', 'create_time_detail', 'clue_state_name', 'follow_state_name', 'order_type' => 0, 'is_fenpei' => 0, 'is_repeat', 'org_id' => '']);
- $clue_source = [
- '0' => '未知',
- '1' => '其他',
- '2' => '外部导入',
- '3' => '广告创意',
- '4' => '蹊径落地页',
- '5' => '一叶智能',
- '6' => '微信原生推广页',
- '7' => '讯达小程序',
- '8' => '互动落地页',
- '9' => '教培通'
- ];
- $clue_state = [
- 'LEADS_FOLLOW_TAG_DEFAULT' => '未分配',
- 'LEADS_FOLLOW_TAG_ASSIGNED' => '已分配待跟进',
- 'LEADS_FOLLOW_TAG_FOLLOWING' => '跟进中',
- 'LEADS_FOLLOW_TAG_VALID' => '已转换-潜在客户',
- 'LEADS_FOLLOW_TAG_VALID_HIGH_INTENTION' => '已转换-高意向客户',
- 'LEADS_FOLLOW_TAG_VALID_CONVERTED' => '已转换-已成单',
- 'LEADS_FOLLOW_TAG_INVALID_REPEAT' => '无效-重复',
- 'LEADS_FOLLOW_TAG_INVALID_NOTSELF' => '无效-非本人',
- 'LEADS_FOLLOW_TAG_INVALID_NOTTOUCH' => '无效-未接通',
- 'LEADS_FOLLOW_TAG_INVALID_NOTINTENTION' => '无效-无意向',
- 'LEADS_FOLLOW_TAG_INVALID_REGION_MISMATCHED' => '无效-定向外',
- 'LEADS_FOLLOW_TAG_INVALID_OTHER' => '无效-其他',
- 'LEADS_INEFFECT_REASON_TEL_NOT_CONNECTED' => '推送',
- 'LEADS_INEFFECT_REASON_NO_INTENTION' => '其他',
- ];
- $app_name = [
- 'LEADS_TYPE_FORM' => '表单预约',
- 'LEADS_TYPE_ONLINE_CONSULT' => '在线咨询',
- 'LEADS_TYPE_MAKE_PHONE_CALL' => '普通电话',
- 'LEADS_TYPE_PHONE' => '智能电话',
- 'LEADS_TYPE_PROMOTION_COUPON' => '发券',
- 'LEADS_TYPE_INTELLIGENT_TOOL' => '智能咨询',
- 'LEADS_TYPE_LOTTERY' => '抽奖',
- 'LEADS_TYPE_LANDING_PAGE_CLICK' => '落地页点击',
- 'LEADS_TYPE_ONE_CLICK_AUTHORIZE' => '一键授权',
- 'LEADS_TYPE_PAGE_SCAN_CODE' => '加企业微信客服',
- 'LEADS_TYPE_PROMOTION_FOLLOW' => '微信关注',
- ];
- // $clue_type = [0 => '表单提交', 1 => '在线咨询', 2 => '智能电话', 3 => '网页回呼', 4 => '卡券', 5 => '抽奖'];
- $table_tags = [
- [
- 'title' => '高级意向',
- 'name' => 'all0',
- 'value' => 0,
- 'tags' =>
- [
- ['title' => '已签约合同', 'name' => 'all0_1'],
- ['title' => '已缴纳定金', 'name' => 'all0_2'],
- ['title' => '客户已到店', 'name' => 'all0_3'],
- ['title' => '预约上门量房', 'name' => 'all0_4'],
- ]
- ],
- [
- 'title' => '中级意向',
- 'name' => 'all1',
- 'value' => 1,
- 'tags' =>
- [
- ['title' => '能提供户型图', 'name' => 'all1_1'],
- ['title' => '有装修预算', 'name' => 'all1_2'],
- ['title' => '添加微信待通过', 'name' => 'all1_3'],
- ['title' => '添加微信已通过', 'name' => 'all1_4'],
- ]
- ],
- [
- 'title' => '初级意向',
- 'name' => 'all2',
- 'value' => 2,
- 'tags' =>
- [
- ['title' => '仅咨询了解', 'name' => 'all2_1'],
- ['title' => '无真实需求', 'name' => 'all2_2'],
- ['title' => '对比其他平台', 'name' => 'all2_3'],
- ['title' => '对价格不反感', 'name' => 'all2_4'],
- ['title' => '近期有装修需求', 'name' => 'all2_5'],
- ['title' => '添加微信', 'name' => 'all2_6'],
- ]
- ],
- [
- 'title' => '意向',
- 'name' => 'all3',
- 'value' => 3,
- 'tags' =>
- [
- ['title' => '低意向', 'name' => 'all3_1'],
- ['title' => '中意向', 'name' => 'all3_2'],
- ['title' => '高意向', 'name' => 'all3_3'],
- ]
- ],
- [
- 'title' => '无效',
- 'name' => 'all4',
- 'value' => 4,
- 'tags' =>
- [
- ['title' => '添加微信未通过', 'name' => 'all4_1'],
- ['title' => '多次联系不上', 'name' => 'all4_2'],
- ['title' => '拒绝加微信', 'name' => 'all4_3'],
- ['title' => '无需求', 'name' => 'all4_4'],
- ['title' => '错号', 'name' => 'all4_5'],
- ['title' => '关机', 'name' => 'all4_6'],
- ['title' => '空号', 'name' => 'all4_7'],
- ['title' => '联系不上', 'name' => 'all4_8'],
- ['title' => '无意愿', 'name' => 'all4_9'],
- ['title' => '非本人', 'name' => 'all4_10']
- ]
- ],
- [
- 'title' => '默认',
- 'name' => 'all5',
- 'value' => 5,
- 'tags' =>
- [
- ['title' => '加微信', 'name' => 'all5_1'],
- ['title' => '待确认', 'name' => 'all5_2'],
- ['title' => '无效', 'name' => 'all5_3'],
- ['title' => '参与赔付', 'name' => 'all5_4'],
- ]
- ],
- ];
- if (!$request->isAjax()) {
- $this->screen();
- $channel_setting = Company::where(['root_id' => $root_id])->value('channel_setting');
- $advertiser_list = [];
- if (isset($channel_setting) && $channel_setting) {
- $tmp_data = json_decode($channel_setting, true);
- $advertiser_id = explode(',', $tmp_data['tx_adv']);
- $fish_account = explode(',', $tmp_data['tx_account']);
- foreach ($advertiser_id as $key => $val) {
- $advertiser_list[] = [
- 'id' => $advertiser_id[$key],
- 'name' => isset($fish_account[$key]) ? $fish_account[$key] : '暂未设置名称',
- ];
- }
- }
- $team_orgs = $is_manager ? orgSubIds($emp_org_id) : [$emp_org_id];
- $emporg_list = Org::where([['id', 'in', $team_orgs]])->column('id,name');
- View::assign('emporg_list', $emporg_list);
- View::assign('advertiser_id', $advertiser_list);
- View::assign('is_manager', $is_manager);
- View::assign('soudata_now', $clue_source);
- View::assign('clue_state', $clue_state);
- View::assign('table_tags', $table_tags);
- // $this->synchronize_tx_data();
- return View::fetch();
- }
- $org_employee = [$emp_id];
- if ($is_manager == 1) {
- $team_orgs = orgSubIds($emp_org_id);
- // $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
- $condition[] = ['org_id', 'in', $team_orgs];
- } else {
- $condition[] = ['employee_id', 'in', $org_employee];
- }
- $condition[] = ['root_id', '=', $root_id];
- $condition[] = ['type', '=', 2];
- if (!empty($param['name'])) $condition[] = ['name', 'like', '%' . $param['name'] . '%'];
- if (!empty($param['clue_id'])) $condition[] = ['clue_id', 'like', '%' . $param['clue_id'] . '%'];
- if (!empty($param['telephone'])) $condition[] = ['telephone', '=', $param['telephone']];
- if (!empty($param['tags'])) {
- $tags = explode(',', $param['tags']);
- $tmp_tags = [];
- foreach ($tags as $val) {
- $tmp_tags[] = "%{$val}%";
- }
- $condition[] = ['tags', 'like', $tmp_tags, 'OR'];
- }
- if (!empty($param['address'])) $condition[] = ['address', 'like', '%' . $param['address'] . '%'];
- if (!empty($param['advertiser_id'])) $condition[] = ['advertiser_id', 'like', '%' . $param['advertiser_id'] . '%'];
- if (!empty($param['ad_name'])) $condition[] = ['ad_name', 'like', '%' . $param['ad_name'] . '%'];
- if (!empty($param['ad_id'])) $condition[] = ['ad_id', 'like', '%' . $param['ad_id'] . '%'];
- if (!empty($param['promotion_id'])) $condition[] = ['promotion_id', 'like', '%' . $param['promotion_id'] . '%'];
- if (!empty($param['promotion_name'])) $condition[] = ['promotion_name', 'like', '%' . $param['promotion_name'] . '%'];
- if (!empty($param['weixin'])) $condition[] = ['weixin', 'like', '%' . $param['weixin'] . '%'];
- if (!empty($param['qq'])) $condition[] = ['qq', 'like', '%' . $param['qq'] . '%'];
- if (!empty($param['email'])) $condition[] = ['email', 'like', '%' . $param['email'] . '%'];
- if (!empty($param['location'])) $condition[] = ['location', 'like', '%' . $param['location'] . '%'];
- if (!empty($param['module_id'])) $condition[] = ['module_id', 'like', '%' . $param['module_id'] . '%'];
- if (!empty($param['module_name'])) $condition[] = ['module_name', 'like', '%' . $param['module_name'] . '%'];
- if (!empty($param['clue_owner_name'])) $condition[] = ['clue_owner_name', 'in', explode(',', $param['clue_owner_name'])];
- if (!empty($param['org_id'])) $condition[] = ['org_id', 'in', explode(',', $param['org_id'])];
- if (!empty($param['clue_source'])) {
- $aa = [];
- foreach (explode(',', $param['clue_source']) as $vv) {
- $aa[] = $vv - 1;
- }
- $condition[] = ['clue_source', 'in', $aa];
- }
- if (!empty($param['is_allocation'])) $condition[] = ['is_allocation', '=', $param['is_allocation'] - 1];
- if (!empty($param['is_contact'])) $condition[] = ['is_contact', '=', $param['is_contact'] - 1];
- if (!empty($param['is_fenpei'])) {
- if ($param['is_fenpei'] == 2) $condition[] = ['employee_id', '>', 0];
- if ($param['is_fenpei'] == 1) $condition[] = ['employee_id', '=', 0];
- }
- if (!empty($param['create_time_detail'])) {
- $last_contact = explode(' - ', $param['create_time_detail']);
- $start = str_replace('/', '-', $last_contact[0]);
- $end = str_replace('/', '-', $last_contact[1]);
- $condition[] = ['create_time_detail', '>=', $start . ' 00:00:00'];
- $condition[] = ['create_time_detail', '<=', $end . ' 23:59:59'];
- }
- if (!empty($param['times'])) {
- $last_contact = explode(' - ', $param['times']);
- $start = str_replace('/', '-', $last_contact[0]);
- $end = str_replace('/', '-', $last_contact[1]);
- $condition[] = ['create_time_detail', '>=', $start . ' 00:00:00'];
- $condition[] = ['create_time_detail', '<=', $end . ' 23:59:59'];
- }
- if (!empty($param['clue_state_name'])) $condition[] = ['clue_state', 'in', explode(',', $param['clue_state_name'])];
- if (!empty($param['follow_state_name'])) $condition[] = ['follow_state_name', 'like', '%' . $param['follow_state_name'] . '%'];
- if (!empty($param['is_repeat'])) {
- $tmp = FishData::where($condition)->field('telephone,count(*) as c')->group('telephone')->having('c>1')->select()->toArray();
- $condition[] = ['telephone', 'in', array_column($tmp, 'telephone')];
- }
- $order = 'id desc';
- if ($param['order_type'] == 1) $order = 'create_time_detail asc';
- if ($param['order_type'] == 2) $order = 'create_time_detail desc';
- $page = intval($param['page']);
- $limit = intval($param['limit']);
- $data = FishData::where($condition)->page($page, $limit)->order($order)->select();
- foreach ($data as $key => $val) {
- $data[$key]['telephone'] = substr_replace($val['telephone'], '******', 3, 6);
- $data[$key]['gender'] = ['未知', '男', '女'][$val['gender']];
- $data[$key]['clue_source'] = isset($clue_source[$val['clue_source']]) ? $clue_source[$val['clue_source']] : '';
- $data[$key]['allocation_status'] = $val['is_allocation'] == 1 ? '已分配' : '未分配';
- $data[$key]['clue_state_name'] = isset($clue_state[$val['clue_state_name']]) ? $clue_state[$val['clue_state_name']] : '';
- $data[$key]['app_name'] = isset($app_name[$val['app_name']]) ? $app_name[$val['app_name']] : $app_name[$val['app_name']];
- $data[$key]['clue_type'] = '未知';
- $data[$key]['is_followup'] = 0;
- if ($is_manager == 1) {
- $data[$key]['is_followup'] = 1;
- }
- if ($is_manager == 1 && $emp_id == $data[$key]['employee_id']) {
- $data[$key]['is_followup'] = 3;
- }
- if ($is_manager == 0 && $emp_id == $data[$key]['employee_id']) {
- $data[$key]['is_followup'] = 2;
- }
- $data[$key]['org_id'] = Org::where(['id' => $val['org_id']])->value('name');
- }
- $count = FishData::where($condition)->count();
- return json(['code' => 0, 'data' => $data, 'count' => $count]);
- }
- /**
- * 飞鱼线索转化为有效客户
- */
- public function fish_validation()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $orgids = orgSubIds($root_id);
- $param = $this->request->only(['customer_id']);
- $customer_info = FishData::where(['id' => $param['customer_id']])->find();
- if (empty($customer_info)) return json(['code' => 1, 'msg' => '线索信息有误']);
- if ($customer_info['is_allocation'] == 1) return json(['code' => 1, 'msg' => '线索已转化,无法重复转化']);
- $customerExist = Customer::where([
- ['phone|phone1|phone2', '=', cypherphone($customer_info['telephone'])],
- ['org_id', 'in', $orgids],
- ['employee_id', '>', 0]
- ])->where(
- function ($query) {
- $not_sure = Customer::changeState('待确认', 'chaos');
- $or1[] = ['crm_res_id', 'null', null];
- $or2[] = ['crm_res_id', '>', 0];
- $or2[] = ['state', 'not in', $not_sure];
- $query->whereOr([$or1, $or2]);
- }
- )->field('id,employee_id,is_resource,org_id,source_id')->select()->toArray();
- if (!empty($customerExist)) {
- foreach ($customerExist as $ex) {
- if ($ex['employee_id'] != $empid) {
- //查询撞单客户渠道
- $source_name = '';
- if ($ex['source_id']) {
- $source = CustomerSource::where(['id' => $ex['source_id']])->value('source');
- $source_name = $source . '渠道客户';
- }
- $have_emp = Employee::where('id', '=', $ex['employee_id'])->value('name');
- return json(['code' => 1, 'msg' => '该客户与员工' . $have_emp . $source_name . '撞单,无法转化']);
- }
- }
- }
- //检查线索来源
- if ($customer_info['type'] == 1 || $customer_info['type'] == 4) {
- $fish_source = CustomerSource::where(['root_id' => $root_id, 'source' => '飞鱼线索'])->find();
- } else {
- $fish_source = CustomerSource::where(['root_id' => $root_id, 'source' => '腾讯线索'])->find();
- }
- //检查是否被分配
- if (empty($customer_info['employee_id']) || !isset($customer_info['employee_id'])) return json(['code' => 1, 'msg' => '请先分配线索']);
- $employee_info = Employee::where(['id' => $customer_info['employee_id'], 'root_id' => $root_id])->field('id,name,org_id')->find();
- if (!$employee_info) return json(['code' => 1, 'msg' => '员工信息有误']);
- // $aec = new Aec(config('app.aec_key'), config('app.aec_iv'));
- // $telephone = $aec->decrypt('RW0xbkt0WSsvcXJBWHdSSGdTdnhRdz09');
- // dump($telephone);die();//15838056141-宁/15565412648-豆
- $save['employee_id'] = $customer_info['employee_id'];
- $save['phone'] = $customer_info['telephone'];
- $save['name'] = $customer_info['name'];
- $save['is_distribution'] = 0;
- $save['is_resource'] = 0;
- $save['state'] = 1;
- $save['community_name'] = $customer_info['location'];
- $save['addtime'] = $customer_info['create_time_detail'];
- $save['updatetime'] = $customer_info['create_time_detail'];
- $save['org_id'] = $employee_info['org_id'];
- $save['sex'] = boolval($employee_info['gender']);
- $save['source_id'] = $fish_source['id'];
- $save['employee_time'] = date('Y-m-d H:i:s');
- $save['valid_time'] = date('Y-m-d H:i:s');
- $visitField_decode = [];
- $visit_default = CustomerPortraitField::where([['root_id', '=', $root_id], ['keyname', 'in', ['source_id', 'sign_time']]])->column('id', 'keyname');
- if (isset($visit_default['sign_time']) && $visit_default['sign_time']) {
- $visitField_decode[] = ['id' => $visit_default['sign_time'], 'keyname' => 'sign_time', 'value' => $customer_info['create_time_detail']];
- }
- if (isset($visit_default['source_id']) && $visit_default['source_id']) {
- $visitField_decode[] = ['id' => $visit_default['source_id'], 'keyname' => 'source_id', 'value' => $fish_source['id']];
- }
- $save['ext'] = !empty($visitField_decode) ? json_encode($visitField_decode) : NULL;
- $ms = Customer::create($save);
- if ($ms) {
- FishData::where(['id' => $param['customer_id']])->update(['is_allocation' => 1, 'customer_id' => $ms->id, 'is_contact' => 1]);
- //更新通话记录
- $log_list = OutCallLog::where(['call_id' => $param['customer_id']])->select()->toArray();
- OutCallLog::where(['call_id' => $param['customer_id']])->update(['customer_id' => $ms->id]);
- foreach ($log_list as $val) {
- CustomerVisitLog::where(['data_id' => $val['id'], 'data_type' => 'out_call'])->update(['customer_id' => $ms->id]);
- }
- if (!isset($log_list) || empty($log_list)) {
- CustomerVisitLog::Create([
- 'customer_id' => $ms->id,
- 'type' => '',
- 'employee_id' => $empid,
- 'user_id' => $request->empcrm->uid,
- 'org_id' => $request->empcrm->org_id,
- 'customer_employee_id' => 0,
- 'customer_org_id' => 0,
- 'data_type' => '',
- 'data_id' => '',
- 'remark' => '标记有效'
- ]);
- }
- return json(['code' => 0, 'msg' => '确认完成', 'data' => $log_list]);
- } else {
- return json(['code' => 1, 'msg' => '确认失败']);
- }
- }
- /**
- * 更新数据
- */
- public function fish_update_data()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $param = $this->request->only(['id', 'address', 'age', 'date', 'email', 'gender', 'location', 'name', 'qq', 'remark', 'weixin', 'employee_remark', 'tags']);
- $data = FishData::find($param['id']);
- if (empty($data)) return json(['code' => 1, 'msg' => '数据信息有误']);
- $param['gender'] = $param['gender'] == '男' ? 1 : ($param['gender'] == '女' ? 2 : 0);
- $data->save($param);
- return json(['code' => 0, 'msg' => '确认完成']);
- }
- /**
- * 同步数据
- */
- public function synchronize_fish_data()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $channel_setting = Company::where(['root_id' => $root_id])->value('channel_setting');
- if ($channel_setting) {
- $tmp_data = json_decode($channel_setting, true);
- if ($tmp_data['fish_adv']) {
- $appid = config('app.fish_clue_appid');
- $secret = config('app.fish_clue_secret');
- $fish_update_time = $tmp_data['fish_update_time'] ?: date('Y-m-d H:i:s', time() - 86400 * 180);
- $end_time = date('Y-m-d H:i:s');
- $page_siae = 100;
- $fish = new Fish($appid, $secret);
- $fish_refresh_time = $tmp_data['fish_refresh_time']; //上次token更新时间
- if ($fish_refresh_time + 80000 < time()) {
- $token = $fish->getRefreshToken($tmp_data['fish_refresh_token']);
- if ($token['code'] == 0) {
- $tmp_data['fish_access_token'] = $token['data']['access_token'];
- $tmp_data['fish_refresh_token'] = $token['data']['refresh_token'];
- $tmp_data['fish_refresh_time'] = time();
- Company::where(['root_id' => $root_id])->update(['channel_setting' => json_encode($tmp_data)]);
- } else {
- $tmp_data['fish_access_token'] = '';
- $tmp_data['fish_refresh_token'] = '';
- Company::where(['root_id' => $root_id])->update(['channel_setting' => json_encode($tmp_data)]);
- }
- }
- $advertiser_id = explode(',', $tmp_data['fish_adv']);
- foreach ($advertiser_id as $adv) {
- $data = $fish->getClueListData($tmp_data['fish_access_token'], $adv, $fish_update_time, $end_time, 1, $page_siae);
- if (isset($data) && $data['code'] == 0) {
- $total_number = $data['data']['page_info']['total_number'];
- $page = intval(ceil($total_number / $page_siae));
- $list = $data['data']['list'];
- // Db::startTrans();
- // try {
- $this->processing_fish_data($list, $root_id, 1);
- for ($i = 1; $i < $page; $i++) {
- $tmp = $fish->getClueListData($tmp_data['fish_access_token'], $adv, $fish_update_time, $end_time, $i + 1, $page_siae);
- $this->processing_fish_data($tmp['data']['list'], $root_id, 1);
- }
- $tmp_data['fish_update_time'] = $end_time;
- Company::where(['root_id' => $root_id])->update(['channel_setting' => json_encode($tmp_data)]);
- // Db::commit();
- // } catch (\Exception $e) {
- // trace($e->getMessage(), 'error');
- // Db::rollback();
- // }
- }
- }
- }
- }
- }
- /**
- * todo 同步直播飞鱼数据
- */
- public function synchronize_fish_live_data()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $channel_setting = Company::where(['root_id' => $root_id])->value('channel_setting_wh');
- if ($channel_setting) {
- $tmp_data = json_decode($channel_setting, true);
- if ($tmp_data['fish_adv']) {
- $appid = config('app.fish_clue_appid_wh');
- $secret = config('app.fish_clue_secret_wh');
- $fish_update_time = $tmp_data['fish_update_time'] ?: date('Y-m-d H:i:s', time() - 86400 * 180);
- $end_time = date('Y-m-d H:i:s');
- $page_siae = 100;
- $fish = new Fish($appid, $secret);
- $fish_refresh_time = $tmp_data['fish_refresh_time']; //上次token更新时间
- if ($fish_refresh_time + 80000 < time()) {
- $token = $fish->getRefreshToken($tmp_data['fish_refresh_token']);
- if ($token['code'] == 0) {
- $tmp_data['fish_access_token'] = $token['data']['access_token'];
- $tmp_data['fish_refresh_token'] = $token['data']['refresh_token'];
- $tmp_data['fish_refresh_time'] = time();
- Company::where(['root_id' => $root_id])->update(['channel_setting_wh' => json_encode($tmp_data)]);
- } else {
- $tmp_data['fish_access_token'] = '';
- $tmp_data['fish_refresh_token'] = '';
- Company::where(['root_id' => $root_id])->update(['channel_setting_wh' => json_encode($tmp_data)]);
- }
- }
- $advertiser_id = explode(',', $tmp_data['fish_adv']);
- foreach ($advertiser_id as $adv) {
- $data = $fish->getClueListData($tmp_data['fish_access_token'], $adv, $fish_update_time, $end_time, 1, $page_siae);
- if (isset($data) && $data['code'] == 0) {
- $total_number = $data['data']['page_info']['total_number'];
- $page = intval(ceil($total_number / $page_siae));
- $list = $data['data']['list'];
- // Db::startTrans();
- // try {
- $this->processing_fish_data($list, $root_id, 4);
- for ($i = 1; $i < $page; $i++) {
- $tmp = $fish->getClueListData($tmp_data['fish_access_token'], $adv, $fish_update_time, $end_time, $i + 1, $page_siae);
- $this->processing_fish_data($tmp['data']['list'], $root_id, 4);
- }
- $tmp_data['fish_update_time'] = $end_time;
- Company::where(['root_id' => $root_id])->update(['channel_setting_wh' => json_encode($tmp_data)]);
- // Db::commit();
- // } catch (\Exception $e) {
- // trace($e->getMessage(), 'error');
- // Db::rollback();
- // }
- }
- }
- }
- }
- }
- /**
- * 腾讯同步数据
- */
- public function synchronize_tx_data()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $channel_setting = Company::where(['root_id' => $root_id])->value('channel_setting');
- if ($channel_setting) {
- $tmp_data = json_decode($channel_setting, true);
- if ($tmp_data['tx_adv']) {
- $fish_update_time = $tmp_data['tx_update_time'] ?: time() - 86400; //默认往前一天
- $end_time = time();
- $page_siae = 200;
- $fish = new Tencent(config('app.tx_clue_appid'), config('app.tx_clue_secret'));
- $advertiser_id = explode(',', $tmp_data['tx_adv']);
- foreach ($advertiser_id as $adv) {
- $data = $fish->getClueListData($tmp_data['tx_access_token'], $adv, $fish_update_time, $end_time, 1, $page_siae);
- if (isset($data) && $data['code'] == 0) {
- $total_number = $data['data']['page_info']['total_number'];
- $page = intval(ceil($total_number / $page_siae));
- $list = $data['data']['leads_info'];
- // Db::startTrans();
- // try {
- $this->processing_tx_data($list, $root_id);
- for ($i = 1; $i < $page; $i++) {
- $tmp = $fish->getClueListData($tmp_data['tx_access_token'], $adv, $fish_update_time, $end_time, $i + 1, $page_siae);
- $this->processing_tx_data($tmp['data']['leads_info'], $root_id);
- }
- $tmp_data['tx_update_time'] = $end_time;
- Company::where(['root_id' => $root_id])->update(['channel_setting' => json_encode($tmp_data)]);
- // Db::commit();
- // } catch (\Exception $e) {
- // trace($e->getMessage(), 'error');
- // Db::rollback();
- // }
- }
- }
- }
- }
- }
- //保存飞鱼线索
- public function processing_fish_data($list, $root_id, $type)
- {
- $customer = [];
- foreach ($list as $val) {
- //查找所属员工-只有员工名称对应上才允许保持到本地库
- $val['extra_info'] = implode(',', $val['extra_info']);
- $val['remark_dict'] = json_encode($val['remark_dict']);
- $val['store'] = json_encode($val['store']);
- $val['tags'] = implode(';', $val['tags']);
- $val['root_id'] = $root_id;
- $val['type'] = $type;
- $info = Employee::where(['name' => $val['clue_owner_name'], 'root_id' => $root_id])->field('id,org_id')->find();
- //检查线索是否存在
- $fid = FishData::where(['clue_id' => $val['clue_id'], 'type' => $type])->find();
- if (!isset($fid) && isset($info) && $info) {
- $val['employee_id'] = $info['id'];
- $val['org_id'] = $info['org_id'];
- $customer[] = $val;
- }
- //检查线索是否被分配
- if (isset($fid) && empty($fid['clue_owner_name'])) {
- if (isset($info) && $info) {
- $val['employee_id'] = $info['id'];
- $val['org_id'] = $info['org_id'];
- FishData::where(['clue_id' => $val['clue_id'], 'type' => $type])->update($val);
- }
- }
- }
- FishData::insertAll($customer);
- }
- //保存腾讯线索
- public function processing_tx_data($list, $root_id)
- {
- $leads_source = [
- 'OUTER_TRAFFIC' => 1,
- 'OFFLINE' => 2,
- 'AD_PREVIEW' => 3,
- 'XJ' => 4,
- 'ONE_LEAF' => 5,
- 'WECHAT_CANVAS' => 6,
- 'XUNDA' => 7,
- 'INTERACTIVEPAGE' => 8,
- 'JIAOPEITONG' => 9,
- ];
- $leads_gender = [
- 'GENDER_TYPE_FEMALE' => 2,
- 'GENDER_TYPE_MALE' => 1,
- 'GENDER_TYPE_UNKNOWN' => 0,
- ];
- foreach ($list as $val) {
- $data = [];
- //检查线索是否存在
- $fid = FishData::where(['clue_id' => $val['leads_id'], 'type' => 2])->value('id');
- $info = Employee::where(['name' => $val['owner_name'], 'root_id' => $root_id])->field('id,org_id')->find();
- if (!isset($fid) && isset($info) && $info) {
- //查找所属员工-只有员工名称对应上才允许保持到本地库
- $data['clue_id'] = $val['leads_id'];
- $data['advertiser_id'] = $val['account_id'];
- $data['advertiser_name'] = $val['agency_name'];
- $data['ad_id'] = $val['ad_id'];
- $data['ad_name'] = $val['ad_name'];
- $data['creative_id'] = $val['adcreative_id'];
- $data['promotion_id'] = $val['adgroup_id'];
- $data['mid_info'] = $val['adgroup_name'];
- $data['site_id'] = 0; //空
- $data['site_name'] = ''; //空
- $data['intention_estimation'] = ''; //空
- $data['external_url'] = $val['page_url'];
- $data['clue_type'] = 0; //空
- $data['module_name'] = $val['component_name'];
- $data['module_id'] = $val['component_id'];
- $data['create_time'] = date('Y-m-d', strtotime($val['leads_create_time']));
- $data['create_time_detail'] = $val['leads_create_time'];
- // $data['date'] = ''; //空
- $data['clue_source'] = isset($leads_source[$val['leads_source']]) ? $leads_source[$val['leads_source']] : 0;
- $data['name'] = (isset($val['leads_name']) && $val['leads_name']) ? $val['leads_name'] : '未命名';
- $data['telephone'] = $val['leads_telephone'];
- $data['gender'] = isset($leads_gender[$val['leads_gender']]) ? $leads_gender[$val['leads_gender']] : 0;
- $data['age'] = $val['age'];
- $data['email'] = $val['leads_email'];
- $data['weixin'] = $val['leads_wechat'];
- $data['qq'] = $val['leads_qq'];
- $data['province_name'] = ''; //空
- $data['city_name'] = $val['leads_area']; //空
- $data['address'] = $val['address'];
- $data['location'] = $val['address'];
- $data['remark'] = ''; //空
- $data['remark_dict'] = ''; //空
- $data['form_remark'] = $val['bundle'];
- $data['app_name'] = $val['leads_type'];
- $data['convert_status'] = ''; //转化状态-空
- $data['req_id'] = '';
- $data['clue_state'] = 0; //线索状态值-空
- $data['clue_state_name'] = $val['leads_follow_tag'];
- $data['clue_owner_name'] = $val['owner_name'];
- $data['follow_state_name'] = ''; //空
- $data['country_name'] = $val['leads_area'];
- $data['system_tags'] = ''; //空
- $data['tags'] = $val['leads_tags'];
- $data['allocation_status'] = 0; //空
- $data['store'] = json(['all_follow_records' => $val['all_follow_records']]);
- $data['extra_info'] = ''; //空
- $data['employee_id'] = $info['id'];
- $data['org_id'] = $info['org_id'];
- $data['root_id'] = $root_id;
- $data['type'] = 2;
- FishData::insert($data);
- }
- }
- }
- /**
- * 发起语音双呼
- */
- public function create_bridge_call_customer()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $employee_id = $request->empcrm->id;
- $param = $this->request->only(['customer_id']);
- $fish_data = FishData::where(['customer_id' => $param['customer_id']])->find();
- if (empty($fish_data)) return json(['code' => 1, 'msg' => '线索信息有误']);
- if ($fish_data['type'] == 1) {
- $fish = new Fish(config('app.fish_clue_appid'), config('app.fish_clue_secret'));
- $channel_setting = Company::where(['root_id' => $root_id])->value('channel_setting');
- } elseif ($fish_data['type'] == 4) {
- $fish = new Fish(config('app.fish_clue_appid_wh'), config('app.fish_clue_secret_wh'));
- $channel_setting = Company::where(['root_id' => $root_id])->value('channel_setting_wh');
- } else {
- $fish = new Fish(config('app.fish_clue_appid'), config('app.fish_clue_secret'));
- $channel_setting = Company::where(['root_id' => $root_id])->value('channel_setting');
- }
- $tmp_data = json_decode($channel_setting, true);
- $employeePhone = $request->empcrm->phone;
- $customerPhone = $fish_data['telephone'];
- $tmp_info = [
- //广告主id
- 'advertiser_id' => intval($fish_data['advertiser_id']),
- //线索id
- 'clue_id' => intval($fish_data['clue_id']),
- // 主叫号码,必须为11位手机号码,否则呼叫失败
- 'caller_number' => $employeePhone, //'15713803101',//
- // 被叫号码,即线索号码
- 'callee_number' => $customerPhone,
- ];
- $rs = $fish->createBridgeCall($tmp_data['fish_access_token'], $tmp_info);
- if ($rs['code'] != 0) {
- return json(['code' => 1, 'msg' => '呼叫失败']);
- } else {
- if ($rs['data']['call_result_code'] != 101) {
- return json(['code' => 1, 'msg' => $rs['data']['call_result_message']]);
- }
- // 添加记录
- $logData = [
- 'session_id' => $rs['data']['contact_id'],
- 'bind_num' => '',
- 'caller_num' => $employeePhone,
- 'callee_num' => $customerPhone,
- 'employee_id' => $employee_id,
- 'root_id' => $root_id,
- 'customer_id' => $param['customer_id'],
- 'call_id' => $fish_data['id'] // 存储飞鱼线索id
- ];
- $log = OutCallLog::create($logData);
- }
- // 添加访问记录
- CustomerVisitLog::Create([
- 'customer_id' => $param['customer_id'],
- 'type' => 1,
- 'employee_id' => $employee_id,
- 'user_id' => $request->empcrm->uid,
- 'org_id' => $request->empcrm->org_id,
- 'customer_employee_id' => 0,
- 'customer_org_id' => 0,
- 'data_type' => 'out_call',
- 'data_id' => $log->id,
- 'remark' => '回访跟踪了飞鱼线索客户'
- ]);
- $fish_data->save(['is_contact' => 1, 'update_time_detail' => date('Y-m-d H:i:s')]);
- return json(['code' => 0, 'msg' => '拨打中,请接听']);
- }
- /**
- * 发起语音双呼
- */
- public function create_bridge_call()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $employee_id = $request->empcrm->id;
- $param = $this->request->only(['customer_id']);
- $fish_data = FishData::where(['id' => $param['customer_id']])->find(); //customer_id
- if (empty($fish_data)) return json(['code' => 1, 'msg' => '线索信息有误']);
- if ($fish_data['type'] == 1) {
- $fish = new Fish(config('app.fish_clue_appid'), config('app.fish_clue_secret'));
- $channel_setting = Company::where(['root_id' => $root_id])->value('channel_setting');
- } else {
- $fish = new Fish(config('app.fish_clue_appid_wh'), config('app.fish_clue_secret_wh'));
- $channel_setting = Company::where(['root_id' => $root_id])->value('channel_setting_wh');
- }
- $tmp_data = json_decode($channel_setting, true);
- $employeePhone = $request->empcrm->phone;
- $customerPhone = $fish_data['telephone'];
- $tmp_info = [
- //广告主id
- 'advertiser_id' => intval($fish_data['advertiser_id']),
- //线索id
- 'clue_id' => intval($fish_data['clue_id']),
- // 主叫号码,必须为11位手机号码,否则呼叫失败
- 'caller_number' => $employeePhone, //'15713803101',//
- // 被叫号码,即线索号码
- 'callee_number' => $customerPhone,
- ];
- $rs = $fish->createBridgeCall($tmp_data['fish_access_token'], $tmp_info);
- if ($rs['code'] != 0) {
- return json(['code' => 1, 'msg' => '呼叫失败']);
- } else {
- if ($rs['data']['call_result_code'] != 101) {
- return json(['code' => 1, 'msg' => $rs['data']['call_result_message']]);
- }
- // 添加记录
- $logData = [
- 'session_id' => $rs['data']['contact_id'],
- 'bind_num' => '',
- 'caller_num' => $employeePhone,
- 'callee_num' => $customerPhone,
- 'employee_id' => $employee_id,
- 'root_id' => $root_id,
- 'customer_id' => 0, //$param['customer_id']
- 'call_id' => $param['customer_id'] // 存储飞鱼线索id
- ];
- $log = OutCallLog::create($logData);
- }
- // 添加访问记录
- CustomerVisitLog::Create([
- 'customer_id' => 0,
- 'type' => 1,
- 'employee_id' => $employee_id,
- 'user_id' => $request->empcrm->uid,
- 'org_id' => $request->empcrm->org_id,
- 'customer_employee_id' => 0,
- 'customer_org_id' => 0,
- 'data_type' => 'out_call',
- 'data_id' => $log->id,
- 'remark' => '回访跟踪了飞鱼线索客户'
- ]);
- $fish_data->save(['is_contact' => 1, 'update_time_detail' => date('Y-m-d H:i:s')]);
- return json(['code' => 0, 'msg' => '拨打中,请接听']);
- }
- /**
- * 腾讯发起语音双呼
- */
- public function create_bridge_call_tx()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $employee_id = $request->empcrm->id;
- $param = $this->request->only(['customer_id']);
- $fish_data = FishData::where(['id' => $param['customer_id']])->find(); //customer_id
- if (empty($fish_data)) return json(['code' => 1, 'msg' => '线索信息有误']);
- $tencent = new Tencent(config('app.tx_clue_appid'), config('app.tx_clue_secret'));
- $channel_setting = Company::where(['root_id' => $root_id])->value('channel_setting');
- $tmp_data = json_decode($channel_setting, true);
- $employeePhone = $request->empcrm->phone;
- $customerPhone = $fish_data['telephone'];
- $tmp_info = [
- //广告主id
- 'advertiser_id' => intval($fish_data['advertiser_id']),
- //线索id
- 'clue_id' => intval($fish_data['clue_id']),
- // 主叫号码,必须为11位手机号码,否则呼叫失败
- 'caller_number' => $employeePhone, //'15713803101'
- // 被叫号码,即线索号码
- 'callee_number' => $customerPhone,
- // 被叫号码,即线索号码
- 'request_id' => str_rand(20),
- ];
- $rs = $tencent->getCallVirtualNumber($tmp_data['tx_access_token'], $tmp_info);
- if ($rs['code'] != 0) {
- return json(['code' => 1, 'msg' => '呼叫失败']);
- } else {
- // 添加记录
- $logData = [
- 'session_id' => $rs['data']['contact_id'],
- 'bind_num' => '',
- 'caller_num' => $employeePhone,
- 'callee_num' => $customerPhone,
- 'employee_id' => $employee_id,
- 'root_id' => $root_id,
- 'customer_id' => 0, //$param['customer_id']
- 'call_id' => $param['customer_id'] // 存储飞鱼线索id
- ];
- $log = OutCallLog::create($logData);
- }
- // 添加访问记录
- CustomerVisitLog::Create([
- 'customer_id' => 0,
- 'type' => 1,
- 'employee_id' => $employee_id,
- 'user_id' => $request->empcrm->uid,
- 'org_id' => $request->empcrm->org_id,
- 'customer_employee_id' => 0,
- 'customer_org_id' => 0,
- 'data_type' => 'out_call',
- 'data_id' => $log->id,
- 'remark' => '回访跟踪了飞鱼线索客户'
- ]);
- $fish_data->save(['is_contact' => 1, 'update_time_detail' => date('Y-m-d H:i:s')]);
- return json(['code' => 0, 'msg' => '拨打中,请接听']);
- }
- /**
- * 分配员工
- */
- public function fish_validation_view()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $emp_org_id = $request->empcrm->org_id;
- $param = $this->request->only(['id']);
- $customer_info = FishData::where(['id' => $param['id'], 'root_id' => $root_id])->find();
- $orgids = orgSubIds($emp_org_id);
- $employee = Employee::where([['org_id', 'in', $orgids], ['state', '=', '在职'], ['uid', '>', 0]])->field('id,name')->select()->toArray();
- View::assign('employee_list', $employee);
- View::assign('data', $customer_info);
- return View::fetch();
- }
- /**
- * 重新分配员工
- */
- public function fish_validation_again()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $param = $this->request->only(['customer_id', 'employee_id']);
- if (empty($param['customer_id']) || !isset($param['customer_id'])) return json(['code' => 1, 'msg' => '请先选择客户']);
- $customer_id = explode(',', $param['customer_id']);
- $data = FishData::where([['id', 'in', $customer_id], ['is_allocation', '=', 1]])->column('id');
- if (!empty($data)) return json(['code' => 1, 'msg' => '存在已转化的客户']);
- $employee_info = Employee::where(['id' => $param['employee_id'], 'root_id' => $root_id])->field('id,name,org_id')->find();
- if (!$employee_info) return json(['code' => 1, 'msg' => '员工信息有误']);
- FishData::where([['id', 'in', $customer_id], ['is_allocation', '=', 0]])->update(['allocation_status' => 1, 'org_id' => $employee_info['org_id'], 'employee_id' => $employee_info['id'], 'clue_owner_name' => $employee_info['name']]);
- return json(['code' => 0, 'msg' => '确认完成']);
- }
- /**
- * 渠道统计
- */
- public function fish_total()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- $empid = $request->empcrm->id;
- $team_orgs = orgSubIds($request->empcrm->org_id);
- $param = $this->request->only(['date', 'employee_id', 'org_id']);
- if (!$request->isAjax()) {
- $emplist = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->field('id,name')->select()->toArray();
- View::assign('emplist', $emplist);
- $org_list = Org::where([['id', 'in', $team_orgs]])->select()->toArray();
- View::assign('org_list', $org_list);
- $data['company_name'] = Company::where('root_id', $root_id)->value('company_name');
- $data['empname'] = $request->empcrm->name;
- View::assign('data', $data);
- $source_list = CustomerSource::where('root_id', '=', $root_id)->select()->toArray();
- View::assign('source_list', $source_list);
- return View::fetch();
- }
- $state1 = Customer::changeState('待确认', 'chaos');
- $state2 = Customer::changeState('无效', 'chaos');
- $state3 = CustomerVisitLog::changeState('已交定', 'chaos');
- $state4 = CustomerVisitLog::changeState('已签单', 'chaos');
- $state5 = CustomerVisitLog::changeState('确认到店', 'chaos');
- $state6 = CustomerVisitLog::changeState('已到场', 'chaos');
- $state7 = CustomerVisitLog::changeState('已量房', 'chaos');
- if (!empty($param['date'])) {
- $newtime = explode('到', $param['date']);
- } else {
- return json(['code' => 1, 'msg' => '日期为空']);
- }
- $fish_campaign_where[] = ['data_time', 'between', $newtime];
- $fish_data_where[] = ['create_time', 'between', $newtime];
- $customer_where[] = ['sign_time', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']];
- $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id'); //默认部门
- $wherev_where = []; // 查询渠道下所有的客户用
- if (!empty($param['employee_id'])) {
- $org_employee = [$param['employee_id']];
- $fish_data_where[] = ['employee_id', '=', $param['employee_id']];
- $wherev_where[] = ['employee_id', '=', $param['employee_id']];
- }
- if (!empty($param['org_id']) && empty($param['employee_id'])) {
- $org_employee = Employee::where([['root_id', '=', $root_id], ['org_id', 'in', orgSubIds($param['org_id'])], ['uid', '>', 0], ['state', '=', '在职']])->column('id');
- $fish_data_where[] = ['org_id', 'in', orgSubIds($param['org_id'])];
- $wherev_where[] = ['org_id', 'in', orgSubIds($param['org_id'])];
- }
- $customer_where[] = ['employee_id', 'in', $org_employee];
- $wherev[] = ['confirm_date', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']];
- $wherev[] = ['customer_employee_id', 'in', $org_employee];
- $wherev[] = ['customer_org_id', 'in', $team_orgs];
- $wherev[] = ['state', 'in', array_merge($state1, $state2, $state3, $state4, $state5, $state6, $state7)];
- //计算广告主id
- $advertiser_list[] = ['id' => 0, 'name' => '高潜客户'];
- $channel_setting = Company::where(['root_id' => $root_id])->field('root_id,channel_setting,channel_setting_wh')->find();
- if (isset($channel_setting['channel_setting_wh']) && $channel_setting['channel_setting_wh']) {
- $tmp_data = json_decode($channel_setting['channel_setting_wh'], true);
- $advertiser_id = explode(',', $tmp_data['fish_adv']);
- $fish_account = explode(',', $tmp_data['fish_account']);
- foreach ($advertiser_id as $key => $val) {
- $advertiser_list[] = [
- 'id' => $advertiser_id[$key],
- 'name' => isset($fish_account[$key]) ? $fish_account[$key] : '暂未设置名称',
- ];
- }
- }
- if (isset($channel_setting['channel_setting']) && $channel_setting['channel_setting']) {
- $tmp_data = json_decode($channel_setting['channel_setting'], true);
- $advertiser_id = explode(',', $tmp_data['fish_adv']);
- $fish_account = explode(',', $tmp_data['fish_account']);
- foreach ($advertiser_id as $key => $val) {
- $advertiser_list[] = [
- 'id' => $advertiser_id[$key],
- 'name' => isset($fish_account[$key]) ? $fish_account[$key] : '暂未设置名称',
- ];
- }
- if (isset($tmp_data['tx_adv']) && !empty($tmp_data['tx_adv'])) {
- $tx_advertiser_id = explode(',', $tmp_data['tx_adv']);
- $tx_account = explode(',', $tmp_data['tx_account']);
- foreach ($tx_advertiser_id as $key => $val) {
- $advertiser_list[] = [
- 'id' => $tx_advertiser_id[$key],
- 'name' => isset($tx_account[$key]) ? $tx_account[$key] : '暂未设置名称',
- ];
- }
- }
- }
- //计算广告主id
- $t = ['name' => '求和', 'deposit_percen' => 0, 'exten_money' => 0, 'exten_show' => 0, 'exten_click' => 0, 'click_percen' => 0, 'clue_conversion' => 0, 'clue_money' => 0, 'valid_money' => 0, 'meet_money' => 0, 'sign_agv_money' => 0, 'ROI' => 0, 'week' => '', 'data' => [], 'clue_cont' => 0, 'valid_cont' => 0, 'valid_percen' => 0, 'meet_cont' => 0, 'meet_percen' => 0, 'deposit_cont' => 0, 'sign_cont' => 0, 'basic_money' => 0, 'other_money' => 0, 'all_money' => 0, 'invalid_no' => 0, 'invalid_yes' => 0, 'invalid_meet' => 0, 'invalid_deposit' => 0, 'invalid_sign' => 0];
- foreach ($advertiser_list as $key => $val) {
- //查询广告主当前的消耗
- $fish_campaign = FishCampaign::where(['advertiser_id' => $val['id'], 'root_id' => $root_id])->where($fish_campaign_where)->field('sum(`cost`) as exten_money,sum(`show`) as exten_show,sum(`click`) as exten_click')->find();
- $fish_data = FishData::where(['advertiser_id' => $val['id'], 'root_id' => $root_id])->where($fish_data_where)->column('id,is_allocation,customer_id');
- $all_fish_data = FishData::where(['advertiser_id' => $val['id'], 'root_id' => $root_id])->where($wherev_where)->column('id,customer_id');
- $customer_id = array_column($fish_data, 'customer_id');
- $all_customer_id = array_column($all_fish_data, 'customer_id');
- $customer_data = Customer::where($customer_where)->where([['id', 'in', $customer_id]])->field('id,deposit_money as deposit_moneys,signed_money as signed_moneys')->select()->toArray();
- $valid_cont = FishData::where([['advertiser_id', '=', $val['id']], ['root_id', '=', $root_id], ['customer_id', '>', 0]])->where($fish_data_where)->count(); //有效线索
- $advertiser_list[$key]['fish_sql'] = FishData::where([['advertiser_id', '=', $val['id']], ['root_id', '=', $root_id], ['customer_id', '>', 0]])->where($fish_data_where)->buildSql();
- $count = count($fish_data);
- $exten_money = $fish_campaign['exten_money'] ?? 0; //消费
- $exten_show = $fish_campaign['exten_show'] ?? 0; //展现
- $exten_click = $fish_campaign['exten_click'] ?? 0; //点击
- // $valid_cont = count($customer_data);//有效线索
- $advertiser_list[$key]['exten_money'] = $exten_money;
- $advertiser_list[$key]['exten_show'] = $exten_show;
- $advertiser_list[$key]['exten_click'] = $exten_click;
- $advertiser_list[$key]['click_percen'] = $exten_show > 0 ? round($exten_click / $exten_show * 100, 2) . '%' : '0%';
- $advertiser_list[$key]['clue_conversion'] = $exten_click > 0 ? round($count / $exten_click * 100, 2) . '%' : '0%';
- $advertiser_list[$key]['clue_cont'] = $count;
- $advertiser_list[$key]['clue_money'] = $count > 0 ? round($exten_money / $count, 2) : 0;
- $advertiser_list[$key]['valid_cont'] = $valid_cont;
- $advertiser_list[$key]['valid_money'] = $valid_cont > 0 ? round($exten_money / $valid_cont, 2) : 0; //有效单价
- $advertiser_list[$key]['valid_percen'] = $count > 0 ? round($valid_cont / $count * 100, 2) : 0; //有效率
- $visitlog_list = CustomerVisitLog::where($wherev)->whereIn('customer_id', $all_customer_id)->column('customer_id,state');
- $meet_data = $reach_data = $sign_data = [];
- $meet = $reach = $sign = $all_money = $basic_money = 0;
- foreach ($visitlog_list as $v) {
- //处理签单
- if (in_array($v['state'], array_merge($state3, $state4, $state5, $state6, $state7), true) && !in_array($v['customer_id'], $meet_data, true)) {
- $meet++;
- $meet_data[] = $v['customer_id'];
- }
- //处理交定
- if (in_array($v['state'], $state3, true) && !in_array($v['customer_id'], $reach_data, true)) {
- $reach++;
- $reach_data[] = $v['customer_id'];
- }
- if (in_array($v['state'], $state4, true) && !in_array($v['customer_id'], $sign_data, true)) {
- $sign++;
- $sign_data[] = $v['customer_id'];
- }
- }
- foreach ($customer_data as $val) {
- if (isset($val['signed_moneys']) && $val['signed_moneys']) {
- $all_money += $val['signed_moneys'];
- $basic_money += $val['signed_moneys'];
- }
- if (!isset($val['signed_moneys']) && isset($val['deposit_moneys'])) {
- $all_money += $val['deposit_moneys'];
- }
- }
- $advertiser_list[$key]['meet_cont'] = $meet;
- $advertiser_list[$key]['meet_percen'] = $count > 0 ? round($meet / $count * 100, 2) : 0;
- $advertiser_list[$key]['meet_money'] = $meet > 0 ? round($exten_money / $meet, 2) : 0;
- $advertiser_list[$key]['deposit_cont'] = $reach;
- $advertiser_list[$key]['deposit_percen'] = $meet > 0 ? round($reach / $meet * 100, 2) : 0;
- $advertiser_list[$key]['sign_cont'] = $sign;
- $advertiser_list[$key]['basic_money'] = $all_money;
- $advertiser_list[$key]['sign_agv_money'] = $sign > 0 ? round($basic_money / $sign, 2) : 0;
- $advertiser_list[$key]['ROI'] = !empty($exten_money) && !empty($basic_money) ? round($exten_money / $basic_money * 100, 2) : 0;
- $t['exten_money'] += !empty($exten_money) ? round($exten_money, 2) : 0;
- $t['exten_show'] += $exten_show;
- $t['exten_click'] += $exten_click;
- $t['clue_cont'] += $count;
- $t['valid_cont'] += $valid_cont;
- $t['meet_cont'] += $meet;
- $t['deposit_cont'] += $reach;
- $t['sign_cont'] += $sign;
- $t['basic_money'] += $all_money;
- $t['other_money'] += $basic_money;
- }
- $t['exten_money'] = round($t['exten_money'], 2);
- $t['clue_money'] = $t['exten_money'] == 0 || $t['clue_cont'] == 0 ? 0 : round($t['exten_money'] / $t['clue_cont'], 2);
- $t['valid_money'] = $t['exten_money'] == 0 || $t['valid_cont'] == 0 ? 0 : round($t['exten_money'] / $t['valid_cont'], 2);
- $t['click_percen'] = $t['exten_show'] == 0 || $t['exten_click'] == 0 ? 0 : round($t['exten_click'] / $t['exten_show'] * 100, 2);
- $t['valid_percen'] = $t['clue_cont'] == 0 || $t['valid_cont'] == 0 ? 0 : round($t['valid_cont'] / $t['clue_cont'] * 100, 2);
- $t['meet_percen'] = $t['clue_cont'] == 0 || $t['meet_cont'] == 0 ? 0 : round($t['meet_cont'] / $t['clue_cont'] * 100, 2);
- $t['deposit_percen'] = $t['meet_cont'] == 0 || $t['deposit_cont'] == 0 ? 0 : round($t['deposit_cont'] / $t['meet_cont'] * 100, 2);
- $t['meet_money'] = $t['exten_money'] == 0 || $t['meet_cont'] == 0 ? 0 : round($t['exten_money'] / $t['meet_cont'], 2);
- $t['sign_agv_money'] = $t['sign_cont'] == 0 || $t['other_money'] == 0 ? 0 : round($t['other_money'] / $t['sign_cont'], 2);
- $t['ROI'] = $t['exten_money'] == 0 || $t['basic_money'] == 0 ? 0 : round($t['exten_money'] / $t['basic_money'] * 100, 2);
- array_push($advertiser_list, $t);
- return json(['code' => 0, 'data' => $advertiser_list, 'count' => 0, 'msg' => '获取成功']);
- }
- public function fish_detail()
- {
- $request = request();
- $root_id = $request->empcrm->root_id;
- // $team_orgs = orgSubIds($request->empcrm->org_id);
- $param = $this->request->only(['date', 'source_id', 'org_id', 'employee_id', 'state', 'page' => 1, 'limit' => 17]);
- if (!$request->isAjax()) {
- View::assign('date', $param['date']);
- View::assign('state', $param['state']);
- View::assign('source_id', !empty($param['source_id']) ? $param['source_id'] : '');
- View::assign('employee_id', !empty($param['employee_id']) ? $param['employee_id'] : '');
- View::assign('org_id', !empty($param['org_id']) ? $param['org_id'] : '');
- return View::fetch();
- }
- $order = isset($param['order']) ? $param['order'] : 'id desc';
- // $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
- if (!empty($param['employee_id'])) {
- $org_employee = [$param['employee_id']];
- $condition[] = ['employee_id', 'in', $org_employee];
- }
- if (!empty($param['org_id']) && empty($param['employee_id'])) {
- $org_employee = Employee::where([['root_id', '=', $root_id], ['org_id', 'in', orgSubIds($param['org_id'])], ['uid', '>', 0], ['state', '=', '在职']])->column('id');
- if (!empty($param['source_id'])) {
- $condition[] = ['employee_id', 'in', $org_employee];
- } else {
- $condition[] = ['employee_id', 'in', implode(',', $org_employee) . ',0'];
- }
- }
- if (empty($param['org_id']) && empty($param['employee_id'])) {
- $condition[] = ['employee_id', '>', '-1'];
- }
- $condition[] = ['root_id', '=', $root_id];
- if (!empty($param['source_id'])) {
- $condition[] = ['advertiser_id', '=', $param['source_id']];
- } else {
- $condition[] = ['advertiser_id', '=', 0];
- }
- if (empty($param['date'])) return json(['code' => 1, 'msg' => '参数错误']);
- if (!empty($param['date'])) {
- $newtime = explode('到', $param['date']);
- }
- $condition[] = ['create_time', 'between', [$newtime[0], $newtime[1]]];
- $list = FishData::where($condition)->order($order)->page($param['page'], $param['limit'])->select();
- foreach ($list as $key => $val) {
- $list[$key]['telephone'] = substr_replace($val['telephone'], '******', 3, 6);
- $list[$key]['gender'] = ['未知', '男', '女'][$val['gender']];
- $list[$key]['is_contact'] = $val['is_contact'] == 1 ? '已联系' : '未联系';
- $list[$key]['is_allocation'] = $val['is_allocation'] == 1 ? '已转化' : '未转化';
- }
- $count = FishData::where($condition)->count();
- $sql = FishData::where($condition)->buildSql();
- return json(['code' => 0, 'data' => $list, 'count' => $count, 'where' => $condition, 'param' => $param, 'sql' => $sql]);
- }
- }
|