Empcrm.php 616 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706
  1. <?php
  2. namespace app\empcrm\controller;
  3. use app\BaseController;
  4. use app\model\FishData;
  5. use app\model\User;
  6. use app\model\Admin;
  7. use think\facade\View;
  8. use think\facade\Session;
  9. use think\facade\Log;
  10. use app\model\Employee;
  11. use app\model\Company;
  12. use app\model\CustomerVisitLog;
  13. use app\model\Customer;
  14. use app\logics\OrgLogic;
  15. use app\model\CustomerSource;
  16. use app\model\Org;
  17. use app\model\CustomerPortraitField;
  18. use app\model\CustomerPortraitFieldSelect;
  19. use app\model\Setting;
  20. use app\logics\CustomerLogic;
  21. use app\model\Decostyle;
  22. use app\model\CustomerPackage;
  23. use app\model\CustomerRecycle;
  24. use app\model\CustomersSubscribe;
  25. use think\facade\Db;
  26. use app\model\Activity;
  27. use app\model\WeworksingleCompanySetting;
  28. use app\model\WeworksingleCustomer;
  29. use think\facade\Console;
  30. use app\model\PreformanceTasksOrg;
  31. use app\event\Msg;
  32. use app\model\CreditsSetting;
  33. use app\model\AgentIntegral;
  34. use app\model\AgentUser;
  35. use app\model\AgentCustomerLog;
  36. use app\model\CustomerImport;
  37. use app\model\EmployeeMsg;
  38. use app\model\Miniprogram;
  39. use app\model\PreformanceTasks as PreformanceTasksModel;
  40. use app\model\CustomerSharing;
  41. use app\model\OutCallLog;
  42. use app\model\OutCallMbLog;
  43. use app\model\ShareLog;
  44. use app\model\StatisticsSetting;
  45. use app\model\TrainCourseView;
  46. use app\model\TrainDoneLog;
  47. use app\model\OutCallSetting;
  48. use call\Yhuatong;
  49. use app\model\CustomerInvalidLog;
  50. use app\model\CustomerPortrait;
  51. use app\model\CustomerStateCheck;
  52. use app\model\WechatActivityIntegral;
  53. use toolkits\Aec;
  54. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  55. use PhpOffice\PhpSpreadsheet\IOFactory;
  56. use wx\offiaccount\client\TmpMsg;
  57. use clue\Fish;
  58. use clue\Tencent;
  59. use app\model\FishCampaign;
  60. class Empcrm extends BaseController
  61. {
  62. //首页
  63. public function index()
  64. {
  65. $request = request();
  66. $root_id = $request->empcrm->root_id;
  67. $today = date('Y-m-d H:i:s', time());
  68. $firstday = $today;
  69. $lastday = date('Y-m-d 00:00:00', strtotime("$firstday -2 day"));
  70. $where[] = ['org_id', '=', $request->empcrm->org_id];
  71. $where[] = ['addtime', 'between', [$lastday, $firstday]];
  72. $where[] = ['state', 'in', array_merge(CustomerVisitLog::changeState('已交定', 'chaos'), CustomerVisitLog::changeState('已签单', 'chaos'))];
  73. $list = CustomerVisitLog::with(['employee' => function ($query) {
  74. $query->field('id,name');
  75. }])->where($where)->order('addtime desc')->select()->toArray();
  76. foreach ($list as $key => $val) {
  77. $list[$key]['package_name'] = !empty($val['package_id']) ? CustomerPackage::where('id', $val['package_id'])->value('name') : '';
  78. $cusdata = Customer::with(['designer'])->where('id', $val['customer_id'])->field('name,community_name,deposit_money,signed_money,addtime,designer_id')->find();
  79. //如果客户不存在直接销毁跳过本次循环
  80. if (empty($cusdata)) {
  81. unset($list[$key]);
  82. continue;
  83. }
  84. $cusdata['deposit_moneys'] = !empty($cusdata->getData('deposit_money')) ? number_format($cusdata->getData('deposit_money'), 2, '.', ',') : 0;
  85. $cusdata['signed_moneys'] = !empty($cusdata->getData('signed_money')) ? number_format($cusdata->getData('signed_money'), 2, '.', ',') : 0;
  86. $list[$key]['cusdata'] = $cusdata;
  87. }
  88. $company_name = Company::where('root_id', $root_id)->value('company_name');
  89. View::assign('company_name', $company_name);
  90. View::assign('list', $list);
  91. return View::fetch();
  92. }
  93. /************************************************* 客户池仿手机端团队客户搜索 start ***************************************/
  94. public function getCondition()
  95. {
  96. $param = $this->request->only(['addtime' => date('Y-m-d'), 'source' => '', 'employee_id' => '', 'org_id' => '', 'type' => '']);
  97. $is_manager = request()->empcrm->is_manager;
  98. $employee_id = request()->empcrm->id;
  99. $org_id = request()->empcrm->org_id;
  100. $startTime = strtotime('2015-01-01');
  101. $endTime = strtotime(date('Y-m-d')) + 86400;
  102. if (!empty($param['addtime'])) {
  103. //录入时间
  104. $addtimeArr = explode(' - ', $param['addtime']);
  105. $start = $addtimeArr[0];
  106. $end = date('Y-m-d', strtotime($addtimeArr[1]) + 86400);
  107. $startTime = strtotime($start) - 1;
  108. $endTime = strtotime($end);
  109. }
  110. // 查询条件
  111. // 设计师部门管理员,需要查询指派给下属设计师的客户信息
  112. $org_type = Org::where('id', '=', $org_id)->value('org_type');
  113. if ($org_type == 2) {
  114. if ($is_manager == 1) {
  115. if ($param['org_id']) {
  116. $param_sub_org = orgSubIds($param['org_id']);
  117. } else {
  118. $param_sub_org = orgSubIds($org_id);
  119. }
  120. $designer_employee = Employee::where([['org_id', 'in', $param_sub_org], ['uid', '<>', 0], ['state', '=', '在职']])->column('id');
  121. $query_condition[] = ['designer_id', 'in', $designer_employee];
  122. $condition[] = ['Customer.designer_id', 'in', $designer_employee];
  123. // 是哪个员工
  124. setCondition($param['employee_id'], 'designer_id', '=', $condition);
  125. setCondition($param['employee_id'], 'Customer.designer_id', '=', $condition_log);
  126. } else {
  127. $query_condition[] = ['designer_id', '=', $employee_id];
  128. $condition[] = ['Customer.designer_id', '=', $employee_id];
  129. }
  130. } else {
  131. if ($is_manager == 1) {
  132. if ($param['org_id']) {
  133. $param_sub_org = orgSubIds($param['org_id']);
  134. } else {
  135. $param_sub_org = orgSubIds($org_id);
  136. }
  137. $query_condition[] = ['org_id', 'in', $param_sub_org];
  138. $query_condition[] = ['employee_id', '>', 0];
  139. $condition[] = ['Customer.org_id', 'in', $param_sub_org];
  140. $condition[] = ['Customer.employee_id', '>', 0];
  141. setCondition($param['employee_id'], 'employee_id', '=', $condition);
  142. setCondition($param['employee_id'], 'Customer.employee_id', '=', $condition_log);
  143. } else {
  144. $query_condition[] = ['employee_id', '=', $employee_id];
  145. $condition[] = ['Customer.employee_id', '=', $employee_id];
  146. }
  147. }
  148. $query_condition[] = ['died', '<>', 2];
  149. $query_condition[] = ['state', 'not in', Customer::changeState('无效', 'chaos')];
  150. $condition[] = ['Customer.died', '<>', 2];
  151. $condition[] = ['Customer.state', 'not in', Customer::changeState('无效', 'chaos')];
  152. $state_n = Customer::changeState('待确认', 'chaos');
  153. $state_w = Customer::changeState('无效', 'chaos');
  154. $query1 = array_merge($query_condition, [['state', 'not in', array_merge($state_n, $state_w)]]);
  155. $query2 = array_merge($query_condition, [['state', 'in', $state_n], ['crm_res_id', 'NULL', NULL]]);
  156. $query3 = array_merge($query_condition, [['state', 'in', $state_n], ['crm_res_id', 'NOTNULL', NULL], ['valid_time', 'NOTNULL', NULL]]);
  157. $ids = Customer::whereOr([$query1, $query2, $query3])->column('id');
  158. // 有效
  159. $state1 = CustomerVisitLog::changeState('未到访', 'chaos');
  160. $state2 = CustomerVisitLog::changeState('已到访', 'chaos');
  161. $state3 = CustomerVisitLog::changeState('已量房', 'chaos');
  162. $state4 = CustomerVisitLog::changeState('已到店', 'chaos');
  163. $state5 = CustomerVisitLog::changeState('交定', 'chaos');
  164. $state6 = CustomerVisitLog::changeState('签单', 'chaos');
  165. $state7 = CustomerVisitLog::changeState('已到场', 'chaos');
  166. // 查询条件
  167. switch ($param['type']) {
  168. case 'jiav':
  169. $condition[] = ['Customer.add_wechat_time', '>', date('Y-m-d H:i:s', $startTime)];
  170. $condition[] = ['Customer.add_wechat_time', '<', date('Y-m-d H:i:s', $endTime)];
  171. break;
  172. case 'jiaofang':
  173. $condition[] = ['Customer.house_delivery_time', '>', date('Y/m/d H:i:s', $startTime)];
  174. $condition[] = ['Customer.house_delivery_time', '<', date('Y/m/d H:i:s', $endTime)];
  175. break;
  176. case 'yjlf': // 预计量房
  177. $s_where[] = ['org_id', 'in', $param_sub_org];
  178. $s_where[] = ['state', '=', 0];
  179. $s_where[] = ['type', '=', 3];
  180. $s_where[] = ['addtime', '>', date('Y-m-d H:i:s', $startTime)];
  181. $s_where[] = ['addtime', '<', date('Y-m-d H:i:s', $endTime)];
  182. $customer_ids = CustomersSubscribe::where($s_where)->group('customer_id')->column('customer_id');
  183. $condition[] = ['Customer.id', 'in', $customer_ids];
  184. break;
  185. case 'liangfang':
  186. $condition[] = ['CustomerVisitLog.state', 'in', $state3];
  187. $condition[] = ['CustomerVisitLog.addtime', '>', date('Y-m-d H:i:s', $startTime)];
  188. $condition[] = ['CustomerVisitLog.addtime', '<', date('Y-m-d H:i:s', $endTime)];
  189. break;
  190. case 'yjdd': // 预计到店
  191. $s_where[] = ['org_id', 'in', $param_sub_org];
  192. $s_where[] = ['state', '=', 0];
  193. $s_where[] = ['type', '=', 1];
  194. $s_where[] = ['addtime', '>', date('Y-m-d H:i:s', $startTime)];
  195. $s_where[] = ['addtime', '<', date('Y-m-d H:i:s', $endTime)];
  196. $customer_ids = CustomersSubscribe::where($s_where)->group('customer_id')->column('customer_id');
  197. $condition[] = ['Customer.id', 'in', $customer_ids];
  198. break;
  199. case 'daodian':
  200. $condition[] = ['CustomerVisitLog.state', 'in', $state4];
  201. $condition[] = ['CustomerVisitLog.addtime', '>', date('Y-m-d H:i:s', $startTime)];
  202. $condition[] = ['CustomerVisitLog.addtime', '<', date('Y-m-d H:i:s', $endTime)];
  203. break;
  204. case 'jiaoding':
  205. $condition[] = ['CustomerVisitLog.state', 'in', $state5];
  206. $condition[] = ['CustomerVisitLog.addtime', '>', date('Y-m-d H:i:s', $startTime)];
  207. $condition[] = ['CustomerVisitLog.addtime', '<', date('Y-m-d H:i:s', $endTime)];
  208. break;
  209. case 'qiandan':
  210. $condition[] = ['CustomerVisitLog.state', 'in', $state6];
  211. $condition[] = ['CustomerVisitLog.addtime', '>', date('Y-m-d H:i:s', $startTime)];
  212. $condition[] = ['CustomerVisitLog.addtime', '<', date('Y-m-d H:i:s', $endTime)];
  213. break;
  214. case 'jianqun':
  215. $condition[] = ['Customer.group_building_date', '>', date('Y-m-d H:i:s', $startTime)];
  216. $condition[] = ['Customer.group_building_date', '<', date('Y-m-d H:i:s', $endTime)];
  217. break;
  218. case 'zhibo':
  219. $condition[] = ['Customer.live_broadcast_date', '>', date('Y-m-d H:i:s', $startTime)];
  220. $condition[] = ['Customer.live_broadcast_date', '<', date('Y-m-d H:i:s', $endTime)];
  221. break;
  222. case 'yydc':
  223. $s_where[] = ['org_id', 'in', $param_sub_org];
  224. $s_where[] = ['state', '=', 0];
  225. $s_where[] = ['type', '=', 2];
  226. $s_where[] = ['addtime', '>', date('Y-m-d H:i:s', $startTime)];
  227. $s_where[] = ['addtime', '<', date('Y-m-d H:i:s', $endTime)];
  228. $customer_ids = CustomersSubscribe::where($s_where)->group('customer_id')->column('customer_id');
  229. $condition[] = ['Customer.id', 'in', $customer_ids];
  230. break;
  231. case 'daochang':
  232. $condition[] = ['CustomerVisitLog.state', 'in', $state7];
  233. $condition[] = ['CustomerVisitLog.addtime', '>', date('Y-m-d H:i:s', $startTime)];
  234. $condition[] = ['CustomerVisitLog.addtime', '<', date('Y-m-d H:i:s', $endTime)];
  235. break;
  236. case 'yygswjm':
  237. $condition[] = ['Customer.subscribe_meet_outside', '>', date('Y-m-d H:i:s', $startTime)];
  238. $condition[] = ['Customer.subscribe_meet_outside', '<', date('Y-m-d H:i:s', $endTime)];
  239. break;
  240. case 'gswjm':
  241. $condition[] = ['Customer.meet_outside', '>', date('Y-m-d H:i:s', $startTime)];
  242. $condition[] = ['Customer.meet_outside', '<', date('Y-m-d H:i:s', $endTime)];
  243. break;
  244. default:
  245. //$condition[] = ['Customer.addtime', '>', date('Y-m-d H:i:s', $startTime)];
  246. //$condition[] = ['Customer.addtime', '<', date('Y-m-d H:i:s', $endTime)];
  247. break;
  248. }
  249. return ['condition' => $condition, 'ids' => $ids];
  250. }
  251. /**
  252. * 以客户跟进为主的查询条件
  253. *
  254. */
  255. public function logCondition()
  256. {
  257. $param = $this->request->only(['followtime' => '', 'source' => '', 'employee_id' => '', 'org_id' => '', 'type' => '']);
  258. $is_manager = request()->empcrm->is_manager;
  259. $employee_id = request()->empcrm->id;
  260. $org_id = request()->empcrm->org_id;
  261. $startTime = strtotime('2015-01-01');
  262. $endTime = strtotime(date('Y-m-d')) + 86400;
  263. if (!empty($param['followtime'])) {
  264. //录入时间
  265. $addtimeArr = explode(' - ', $param['followtime']);
  266. $start = $addtimeArr[0];
  267. $end = date('Y-m-d', strtotime($addtimeArr[1]) + 86400);
  268. $startTime = strtotime($start) - 1;
  269. $endTime = strtotime($end);
  270. }
  271. // 查询条件
  272. // 设计师部门管理员,需要查询指派给下属设计师的客户信息
  273. $org_type = Org::where('id', '=', $org_id)->value('org_type');
  274. if ($param['org_id']) {
  275. $param_sub_org = orgSubIds($param['org_id']);
  276. } else {
  277. $param_sub_org = orgSubIds($org_id);
  278. }
  279. if ($org_type == 2) {
  280. if ($is_manager == 1) {
  281. $designer_employee = Employee::where([['org_id', 'in', $param_sub_org], ['uid', '<>', 0], ['state', '=', '在职']])->column('id');
  282. $condition[] = ['designer_id', 'in', $designer_employee];
  283. $condition_log[] = ['Customer.designer_id', 'in', $designer_employee];
  284. $s_where[] = ['employee_id', 'in', $designer_employee];
  285. // 是哪个员工
  286. setCondition($param['employee_id'], 'designer_id', '=', $condition);
  287. setCondition($param['employee_id'], 'Customer.designer_id', '=', $condition_log);
  288. setCondition($param['employee_id'], 'employee_id', '=', $s_where);
  289. } else {
  290. $condition[] = ['designer_id', '=', $employee_id];
  291. $condition_log[] = ['Customer.designer_id', '=', $employee_id];
  292. $s_where[] = ['employee_id', '=', $employee_id];
  293. }
  294. } else {
  295. if ($is_manager == 1) {
  296. $condition[] = ['org_id', 'in', $param_sub_org];
  297. $condition[] = ['employee_id', '>', 0];
  298. $condition_log[] = ['Customer.org_id', 'in', $param_sub_org];
  299. $condition_log[] = ['Customer.employee_id', '>', 0];
  300. $s_where[] = ['org_id', 'in', $param_sub_org];
  301. $s_where[] = ['employee_id', '>', 0];
  302. // 是哪个员工
  303. setCondition($param['employee_id'], 'employee_id', '=', $condition);
  304. setCondition($param['employee_id'], 'Customer.employee_id', '=', $condition_log);
  305. setCondition($param['employee_id'], 'employee_id', '=', $s_where);
  306. } else {
  307. $condition[] = ['employee_id', '=', $employee_id];
  308. $condition_log[] = ['Customer.employee_id', '=', $employee_id];
  309. $s_where[] = ['employee_id', '=', $employee_id];
  310. }
  311. }
  312. $condition[] = ['died', '<>', 2];
  313. $condition[] = ['state', 'not in', Customer::changeState('无效', 'chaos')];
  314. $condition_log[] = ['Customer.died', '<>', 2];
  315. $condition_log[] = ['Customer.state', 'not in', Customer::changeState('无效', 'chaos')];
  316. $state_n = Customer::changeState('待确认', 'chaos');
  317. $state_w = Customer::changeState('无效', 'chaos');
  318. $query1 = array_merge($condition, [['state', 'not in', array_merge($state_n, $state_w)]]);
  319. $query2 = array_merge($condition, [['state', 'in', $state_n], ['crm_res_id', 'NULL', NULL]]);
  320. $ids = Customer::whereOr([$query1, $query2])->column('id');
  321. // 模块
  322. // 加v
  323. $condition_jiav = $condition;
  324. $condition_jiav[] = ['add_wechat_time', '>', date('Y-m-d H:i:s', $startTime)];
  325. $condition_jiav[] = ['add_wechat_time', '<', date('Y-m-d H:i:s', $endTime)];
  326. $jiav_ids_find = Customer::where($condition_jiav)->column('id');
  327. $jiav_ids = array_intersect($jiav_ids_find, $ids);
  328. // 交房
  329. $condition_jiaofang = $condition;
  330. $condition_jiaofang[] = ['house_delivery_time', '>', date('Y/m/d H:i:s', $startTime)];
  331. $condition_jiaofang[] = ['house_delivery_time', '<', date('Y/m/d H:i:s', $endTime)];
  332. $jiaofang_ids_find = Customer::where($condition_jiaofang)->column('id');
  333. $jiaofang_ids = array_intersect($jiaofang_ids_find, $ids);
  334. //预计量房 预计到店
  335. if ($is_manager) {
  336. $s_where[] = ['org_id', 'in', $param_sub_org];
  337. } else {
  338. $s_where[] = ['employee_id', '=', $employee_id];
  339. }
  340. $s_where[] = ['state', '=', 0];
  341. $s_where[] = ['addtime', '>', date('Y-m-d H:i:s', $startTime)];
  342. $s_where[] = ['addtime', '<', date('Y-m-d H:i:s', $endTime)];
  343. $s_where[] = ['type', 'in', [1, 3]];
  344. $yuji_ids_find = CustomersSubscribe::where($s_where)->group('customer_id')->column('customer_id');
  345. $yuji_ids = array_intersect($yuji_ids_find, $ids);
  346. $customer_ids_1 = array_values(array_unique(array_merge($jiav_ids, $jiaofang_ids, $yuji_ids)));
  347. // 时间段内有跟踪
  348. $condition_log[] = ['CustomerVisitLog.addtime', '>', date('Y-m-d H:i:s', $startTime)];
  349. $condition_log[] = ['CustomerVisitLog.addtime', '<', date('Y-m-d H:i:s', $endTime)];
  350. $customer_ids_2_find = Db::view('Customer')
  351. ->view('Org', '', 'Org.id=Customer.org_id')
  352. ->view('CustomerVisitLog', '', 'Customer.id=CustomerVisitLog.customer_id')
  353. ->where($condition_log)
  354. ->group('Customer.id')
  355. ->column('Customer.id');
  356. $customer_ids_2 = array_intersect($customer_ids_2_find, $ids);
  357. $customer_ids = array_values(array_unique(array_merge($customer_ids_1, $customer_ids_2)));
  358. return $customer_ids;
  359. }
  360. /**
  361. * 团队统计数据
  362. * @param string $start_date
  363. * @param string $end_date
  364. * @return string
  365. */
  366. public function statistics()
  367. {
  368. $type = input('type', '', 'trim');
  369. $search_type = input('search_type', 1, 'intval'); //数据查询方式 1,以客户为主导 2,以满足的搜索状态为主导,合并出所有客户
  370. $param = $this->request->only(['addtime' => '', 'followtime' => '', 'source' => '', 'employee_id' => '', 'org_id' => '', 'type' => '']);
  371. $startTime = strtotime('2015-01-01');
  372. $endTime = strtotime(date('Y-m-d')) + 86400;
  373. if ($search_type == 1) {
  374. if (!empty($param['addtime'])) {
  375. //录入时间
  376. $addtimeArr = explode(' - ', $param['addtime']);
  377. $start = $addtimeArr[0];
  378. $end = date('Y-m-d', strtotime($addtimeArr[1]) + 86400);
  379. $startTime = strtotime($start) - 1;
  380. $endTime = strtotime($end);
  381. }
  382. } else {
  383. if (!empty($param['followtime'])) {
  384. //录入时间
  385. $addtimeArr = explode(' - ', $param['followtime']);
  386. $start = $addtimeArr[0];
  387. $end = date('Y-m-d', strtotime($addtimeArr[1]) + 86400);
  388. $startTime = strtotime($start) - 1;
  389. $endTime = strtotime($end);
  390. }
  391. }
  392. $is_manager = request()->empcrm->is_manager;
  393. $employee_id = request()->empcrm->id;
  394. $org_id = request()->empcrm->org_id;
  395. if ($type) {
  396. $condition_res = $this->getCondition();
  397. $condition = $condition_res['condition'];
  398. $condition_ids = $condition_res['ids'];
  399. // 客户总数
  400. $customer_ids = Db::view('Customer')
  401. ->view('Org', '', 'Org.id=Customer.org_id')
  402. ->view('CustomerVisitLog', '', 'Customer.id=CustomerVisitLog.customer_id')
  403. ->where($condition)
  404. ->group('Customer.id')
  405. ->column('Customer.id');
  406. $customer_ids = array_intersect($customer_ids, $condition_ids);
  407. $customer_num = count($customer_ids);
  408. if ($type == 'jiav') {
  409. $jiawei = $customer_num;
  410. } else {
  411. $jiav_condition = $condition;
  412. $jiav_condition[] = ['Customer.add_wechat_time', '<>', ''];
  413. $jiaweiIds = Db::view('Customer')
  414. ->view('Org', '', 'Org.id=Customer.org_id')
  415. ->view('CustomerVisitLog', '', 'Customer.id=CustomerVisitLog.customer_id')
  416. ->where($jiav_condition)
  417. ->group('Customer.id')
  418. ->column('Customer.id');
  419. $jiaweiIds = array_intersect($jiaweiIds, $condition_ids);
  420. $jiawei = count($jiaweiIds);
  421. }
  422. $signed_money = Customer::where('id', 'in', $customer_ids)->sum('signed_money');
  423. $customerData['signed_money'] = $signed_money ? round($signed_money / 10000, 2) : 0;
  424. } else {
  425. if ($search_type == 1) {
  426. $org_type = Org::where('id', '=', $org_id)->value('org_type');
  427. if ($org_type == 2) {
  428. if ($is_manager == 1) {
  429. if ($param['org_id']) {
  430. $param_sub_org = orgSubIds($param['org_id']);
  431. } else {
  432. $param_sub_org = orgSubIds($org_id);
  433. }
  434. $designer_employee = Employee::where([['org_id', 'in', $param_sub_org], ['uid', '<>', 0], ['state', '=', '在职']])->column('id');
  435. $condition[] = ['designer_id', 'in', $designer_employee];
  436. // 是哪个员工
  437. setCondition($param['employee_id'], 'designer_id', '=', $condition);
  438. } else {
  439. $condition[] = ['designer_id', '=', $employee_id];
  440. }
  441. } else {
  442. if ($is_manager == 1) {
  443. if ($param['org_id']) {
  444. $param_sub_org = orgSubIds($param['org_id']);
  445. } else {
  446. $param_sub_org = orgSubIds($org_id);
  447. }
  448. $condition[] = ['org_id', 'in', $param_sub_org];
  449. $condition[] = ['employee_id', '>', 0];
  450. // 是哪个员工
  451. setCondition($param['employee_id'], 'employee_id', '=', $condition);
  452. } else {
  453. $condition[] = ['employee_id', '=', $employee_id];
  454. }
  455. }
  456. $condition[] = ['died', '<>', 2];
  457. $condition[] = ['state', 'not in', Customer::changeState('无效', 'chaos')];
  458. $condition[] = ['addtime', '>', date('Y-m-d H:i:s', $startTime)];
  459. $condition[] = ['addtime', '<', date('Y-m-d H:i:s', $endTime)];
  460. $state_n = Customer::changeState('待确认', 'chaos');
  461. $state_w = Customer::changeState('无效', 'chaos');
  462. $query1 = array_merge($condition, [['state', 'not in', array_merge($state_n, $state_w)]]);
  463. $query2 = array_merge($condition, [['state', 'in', $state_n], ['crm_res_id', 'NULL', NULL]]);
  464. $query3 = array_merge($condition, [['state', 'in', $state_n], ['crm_res_id', 'NOTNULL', NULL], ['valid_time', 'NOTNULL', NULL]]);
  465. $query_ids = Customer::whereOr([$query1, $query2, $query3])->column('id');
  466. $customer_num = count($query_ids);
  467. // 加微数量
  468. $condition1 = $condition;
  469. $condition1[] = ['add_wechat_time', '<>', ''];
  470. $jiawei_ids_find = Customer::where($condition1)->column('id');
  471. $jiawei_ids = array_intersect($jiawei_ids_find, $query_ids);
  472. $jiawei = count($jiawei_ids);
  473. $signed_money = Customer::where('id', 'in', $query_ids)->sum('signed_money');
  474. $customerData['signed_money'] = $signed_money ? round($signed_money / 10000, 2) : 0;
  475. } else {
  476. $customer_ids = $this->logCondition();
  477. $customer_num = count($customer_ids);
  478. // 加微数量
  479. $condition1[] = ['id', 'in', $customer_ids];
  480. $condition1[] = ['add_wechat_time', '<>', ''];
  481. $jiawei = Customer::where($condition1)->count();
  482. $signed_money = Customer::where($condition1)->sum('signed_money');
  483. $customerData['signed_money'] = $signed_money ? round($signed_money / 10000, 2) : 0;
  484. }
  485. }
  486. $customerData['customer_num'] = $customer_num;
  487. $customerData['visit_num'] = $jiawei;
  488. return json(['code' => 0, 'data' => $customerData]);
  489. }
  490. /**
  491. * 团队统计数据
  492. * @param string $start_date
  493. * @param string $end_date
  494. * @return string
  495. */
  496. public function statistics_more()
  497. {
  498. $type = input('type', '', 'trim');
  499. $search_type = input('search_type', 1, 'intval'); //数据查询方式 1,以客户为主导 2,以满足的搜索状态为主导,合并出所有客户
  500. $param = $this->request->only(['addtime' => '', 'followtime' => '', 'source' => '', 'employee_id' => '', 'org_id' => '', 'type' => '']);
  501. $is_manager = request()->empcrm->is_manager;
  502. $employee_id = request()->empcrm->id;
  503. $org_id = request()->empcrm->org_id;
  504. $startTime = strtotime('2015-01-01');
  505. $endTime = strtotime(date('Y-m-d')) + 86400;
  506. if ($search_type == 1) {
  507. if (!empty($param['addtime'])) {
  508. //录入时间
  509. $addtimeArr = explode(' - ', $param['addtime']);
  510. $start = $addtimeArr[0];
  511. $end = date('Y-m-d', strtotime($addtimeArr[1]) + 86400);
  512. $startTime = strtotime($start) - 1;
  513. $endTime = strtotime($end);
  514. }
  515. } else {
  516. if (!empty($param['followtime'])) {
  517. //录入时间
  518. $addtimeArr = explode(' - ', $param['followtime']);
  519. $start = $addtimeArr[0];
  520. $end = date('Y-m-d', strtotime($addtimeArr[1]) + 86400);
  521. $startTime = strtotime($start) - 1;
  522. $endTime = strtotime($end);
  523. }
  524. }
  525. $org_type = Org::where('id', '=', $org_id)->value('org_type');
  526. if ($type) {
  527. $condition_res = $this->getCondition();
  528. $condition = $condition_res['condition'];
  529. $condition_ids = $condition_res['ids'];
  530. // 客户总数
  531. $customerIdList = Db::view('Customer')
  532. ->view('Org', '', 'Org.id=Customer.org_id')
  533. ->view('CustomerVisitLog', '', 'Customer.id=CustomerVisitLog.customer_id')
  534. ->where($condition)
  535. ->group('Customer.id')
  536. ->column('Customer.id');
  537. $customerIdList = array_intersect($customerIdList, $condition_ids);
  538. } else {
  539. if ($search_type == 1) {
  540. // 设计师部门管理员,需要查询指派给下属设计师的客户信息
  541. if ($org_type == 2) {
  542. if ($is_manager == 1) {
  543. if ($param['org_id']) {
  544. $param_sub_org = orgSubIds($param['org_id']);
  545. } else {
  546. $param_sub_org = orgSubIds($org_id);
  547. }
  548. $designer_employee = Employee::where([['org_id', 'in', $param_sub_org], ['uid', '<>', 0], ['state', '=', '在职']])->column('id');
  549. $condition[] = ['designer_id', 'in', $designer_employee];
  550. // 是哪个员工
  551. setCondition($param['employee_id'], 'designer_id', '=', $condition);
  552. } else {
  553. $condition[] = ['designer_id', '=', $employee_id];
  554. }
  555. } else {
  556. if ($is_manager == 1) {
  557. if ($param['org_id']) {
  558. $param_sub_org = orgSubIds($param['org_id']);
  559. } else {
  560. $param_sub_org = orgSubIds($org_id);
  561. }
  562. $condition[] = ['org_id', 'in', $param_sub_org];
  563. $condition[] = ['employee_id', '>', 0];
  564. // 是哪个员工
  565. setCondition($param['employee_id'], 'employee_id', '=', $condition);
  566. } else {
  567. $condition[] = ['employee_id', '=', $employee_id];
  568. }
  569. }
  570. $condition[] = ['died', '<>', 2];
  571. $condition[] = ['state', 'not in', Customer::changeState('无效', 'chaos')];
  572. $condition[] = ['addtime', 'between', [date('Y-m-d H:i:s', $startTime), date('Y-m-d H:i:s', $endTime)]];
  573. $state_n = Customer::changeState('待确认', 'chaos');
  574. $state_w = Customer::changeState('无效', 'chaos');
  575. $query1 = array_merge($condition, [['state', 'not in', array_merge($state_n, $state_w)]]);
  576. $query2 = array_merge($condition, [['state', 'in', $state_n], ['crm_res_id', 'NULL', NULL]]);
  577. $customerIdList = Customer::whereOr([$query1, $query2])->column('id');
  578. } else {
  579. $customerIdList = $this->logCondition();
  580. }
  581. }
  582. // 有效
  583. $state3 = CustomerVisitLog::changeState('已量房', 'chaos');
  584. $state4 = CustomerVisitLog::changeState('已到店', 'chaos');
  585. $state5 = CustomerVisitLog::changeState('交定', 'chaos');
  586. $state6 = CustomerVisitLog::changeState('签单', 'chaos');
  587. $state7 = CustomerVisitLog::changeState('已到场', 'chaos');
  588. $state8 = CustomerVisitLog::changeState('预约量房', 'chaos');
  589. $state9 = CustomerVisitLog::changeState('预约到店', 'chaos');
  590. $state10 = CustomerVisitLog::changeState('预约活动', 'chaos');
  591. // 签单
  592. if ($param['org_id']) {
  593. $condition_org_path = Org::where('id', '=', $param['org_id'])->value('path');
  594. } else {
  595. $condition_org_path = Org::where('id', '=', $org_id)->value('path');
  596. }
  597. // 设计师部门管理员,需要查询指派给下属设计师的客户信息
  598. if ($org_type == 2) {
  599. $org_id = Org::where([['path', 'like', $condition_org_path . '%']])->column('id');
  600. $designer_employee = Employee::where([['org_id', 'in', $org_id], ['uid', '<>', 0], ['state', '=', '在职']])->column('id');
  601. $condition_all[] = ['Customer.employee_id|Customer.designer_id', 'in', $designer_employee];
  602. } else {
  603. $condition_all[] = ['Org.path', 'like', $condition_org_path . '%'];
  604. }
  605. // 签单 交定 预计量房 量房 预计到店 预约活动 已到店 已到场
  606. $condition_state = array_merge($state3, $state4, $state5, $state6, $state7, $state8, $state9, $state10);
  607. $condition_all[] = ['CustomerVisitLog.state', 'in', $condition_state];
  608. if (empty($type)) {
  609. $condition_all[] = ['CustomerVisitLog.addtime', 'between', [date('Y-m-d H:i:s', $startTime), date('Y-m-d H:i:s', $endTime)]];
  610. }
  611. $visit_list = Db::view('CustomerVisitLog')
  612. ->view('Customer', '', 'Customer.id=CustomerVisitLog.customer_id')
  613. ->view('Org', '', 'Org.id=Customer.org_id')
  614. ->where($condition_all)
  615. ->column('CustomerVisitLog.customer_id,CustomerVisitLog.state,CustomerVisitLog.addtime');
  616. $qiandanIds = []; // 签单
  617. $jiaodingIds = []; // 交定
  618. $yylfIds = []; //预约量房
  619. $lfIds = []; // 量房
  620. $yyddIds = []; // 预约到店
  621. $yyhdIds = []; // 预约活动
  622. $yddIds = []; // 已到店
  623. $ydcIds = []; // 已到场
  624. $ydc_list = []; // 已到场记录
  625. $ydd_list = []; // 已到店记录
  626. foreach ($visit_list as $k => $v) {
  627. if (in_array($v['state'], $state6)) {
  628. $qiandanIds[] = $v['customer_id'];
  629. }
  630. if (in_array($v['state'], $state5)) {
  631. $jiaodingIds[] = $v['customer_id'];
  632. }
  633. if (in_array($v['state'], $state8)) {
  634. $yylfIds[] = $v['customer_id'];
  635. }
  636. if (in_array($v['state'], $state3)) {
  637. $lfIds[] = $v['customer_id'];
  638. }
  639. if (in_array($v['state'], $state9)) {
  640. $yyddIds[] = $v['customer_id'];
  641. }
  642. if (in_array($v['state'], $state10)) {
  643. $yyhdIds[] = $v['customer_id'];
  644. }
  645. if (in_array($v['state'], $state7)) {
  646. $ydcIds[] = $v['customer_id'];
  647. $ydc_list[] = $v;
  648. }
  649. if (in_array($v['state'], $state4)) {
  650. $yddIds[] = $v['customer_id'];
  651. $ydd_list[] = $v;
  652. }
  653. }
  654. $qiandan = array_intersect($customerIdList, $qiandanIds);
  655. $jiaoding = array_intersect($customerIdList, $jiaodingIds);
  656. $yylf_ing = CustomersSubscribe::where([['state', '=', 0], ['type', '=', 3], ['customer_id', 'in', $yylfIds]])->column('customer_id');
  657. $yylf = array_intersect($customerIdList, $yylf_ing);
  658. // 量房
  659. $lf = array_intersect($customerIdList, $lfIds);
  660. // 预计到店
  661. $yydd_ing = CustomersSubscribe::where([['state', '=', 0], ['type', '=', 1], ['customer_id', 'in', $yyddIds]])->column('customer_id');
  662. $yydd = array_intersect($customerIdList, $yydd_ing);
  663. // 预约活动
  664. $yyhd_ing = CustomersSubscribe::where([['state', '=', 0], ['type', '=', 2], ['customer_id', 'in', $yyhdIds]])->column('customer_id');
  665. $yyhd = array_intersect($customerIdList, $yyhd_ing);
  666. // 已到店
  667. // 客户5号加微信,10号到店,则10号手机端的到店数据要体现,15号再次到店,则15号手机端不计二次到店的数据,第二月5号再次到店,则5号的数据手机端计1次到店数据 小胖提
  668. if (empty($type)) {
  669. $ydd = [];
  670. $year_s = date('Y', $startTime);
  671. $year_e = date('Y', $endTime);
  672. $month_s = intval(date('m', $startTime));
  673. $month_e = intval(date('m', $endTime));
  674. if ($year_s != $year_e || $month_s != $month_e) {
  675. // 跨月
  676. if ($year_s == $year_e) { // 同一年
  677. for ($i = 0; $month_s <= $month_e; $i++) {
  678. if ($i == 0) {
  679. $startTimeFor = date('Y-m-d H:i:s', $startTime); //首次等于搜索的开始时间
  680. } else {
  681. $startTimeFor = $year_s . '-' . $month_s . '-01 00:00:00'; // 后面月份的开始时间
  682. }
  683. if ($month_s == $month_e) { // 等于搜索的结束月份
  684. $endTimeFor = date('Y-m-d H:i:s', $endTime);
  685. } else { //中间月份的结束时间
  686. $next_month = strtotime(($year_s . '-' . ($month_s + 1) . '-01 00:00:00')) - 1;
  687. $endTimeFor = date('Y-m-d 23:59:59', $next_month);
  688. }
  689. $month_ids = [];
  690. $ids_for_sel = [];
  691. $strtotime_start = strtotime($startTimeFor);
  692. $strtotime_end = strtotime($endTimeFor);
  693. foreach ($ydd_list as $k => $v) {
  694. if (strtotime($v['addtime']) > $strtotime_start && strtotime($v['addtime']) < $strtotime_end) {
  695. if (!in_array($v['customer_id'], $month_ids)) {
  696. $ids_for_sel[] = $v['customer_id'];
  697. }
  698. unset($ydd_list[$k]);
  699. }
  700. }
  701. $ids_for_one = array_intersect($customerIdList, $ids_for_sel);
  702. $ydd = array_merge($ydd, $ids_for_one);
  703. $month_s++; // 月份增加
  704. }
  705. } else {
  706. for ($y = 0; $year_s <= $year_e; $y++) {
  707. if ($year_s == $year_e) {
  708. $month_s_for = 1;
  709. $month_e_for = $month_e;
  710. for ($i = 0; $month_s_for <= $month_e_for; $i++) {
  711. $startTimeFor = $year_s . '-' . $month_s_for . '-01 00:00:00';
  712. if ($month_s_for == $month_e_for) {
  713. $endTimeFor = date('Y-m-d H:i:s', $endTime);
  714. } else {
  715. $next_month = strtotime(($year_s . '-' . ($month_s_for + 1) . '-01 00:00:00')) - 1;
  716. $endTimeFor = date('Y-m-d 23:59:59', $next_month);
  717. }
  718. $month_ids = [];
  719. $ids_for_sel = [];
  720. $strtotime_start = strtotime($startTimeFor);
  721. $strtotime_end = strtotime($endTimeFor);
  722. foreach ($ydd_list as $k => $v) {
  723. if (strtotime($v['addtime']) > $strtotime_start && strtotime($v['addtime']) < $strtotime_end) {
  724. if (!in_array($v['customer_id'], $month_ids)) {
  725. $ids_for_sel[] = $v['customer_id'];
  726. }
  727. unset($ydd_list[$k]);
  728. }
  729. }
  730. $ids_for_one = array_intersect($customerIdList, $ids_for_sel);
  731. $ydd = array_merge($ydd, $ids_for_one);
  732. $month_s_for++;
  733. }
  734. } else {
  735. if ($y == 0) {
  736. $month_s_for = $month_s;
  737. } else {
  738. $month_s_for = 1;
  739. }
  740. for ($i = 0; $month_s_for <= 12; $i++) {
  741. if ($y == 0 && $i == 0) {
  742. $startTimeFor = date('Y-m-d H:i:s', $startTime);
  743. } else {
  744. $startTimeFor = $year_s . '-' . $month_s_for . '-01 00:00:00';
  745. }
  746. if ($month_s_for == 12) {
  747. $next_month = strtotime($year_s . '-12-31 23:59:59');
  748. } else {
  749. $next_month = strtotime(($year_s . '-' . ($month_s_for + 1) . '-01 00:00:00')) - 1;
  750. }
  751. $endTimeFor = date('Y-m-d 23:59:59', $next_month);
  752. $month_ids = [];
  753. $ids_for_sel = [];
  754. $strtotime_start = strtotime($startTimeFor);
  755. $strtotime_end = strtotime($endTimeFor);
  756. foreach ($ydd_list as $k => $v) {
  757. if (strtotime($v['addtime']) > $strtotime_start && strtotime($v['addtime']) < $strtotime_end) {
  758. if (!in_array($v['customer_id'], $month_ids)) {
  759. $ids_for_sel[] = $v['customer_id'];
  760. }
  761. unset($ydd_list[$k]);
  762. }
  763. }
  764. $ids_for_one = array_intersect($customerIdList, $ids_for_sel);
  765. $ydd = array_merge($ydd, $ids_for_one);
  766. $month_s_for++;
  767. }
  768. }
  769. $year_s++; // 下一年
  770. }
  771. }
  772. } else {
  773. $ydd = array_intersect($customerIdList, $yddIds);
  774. }
  775. } else {
  776. $ydd = array_values(array_intersect($customerIdList, $yddIds));
  777. }
  778. // 已到场
  779. if (empty($type)) {
  780. $ydc = [];
  781. $year_s = date('Y', $startTime);
  782. $year_e = date('Y', $endTime);
  783. $month_s = intval(date('m', $startTime));
  784. $month_e = intval(date('m', $endTime));
  785. if ($year_s != $year_e || $month_s != $month_e) {
  786. // 跨月
  787. if ($year_s == $year_e) { // 同一年
  788. for ($i = 0; $month_s <= $month_e; $i++) {
  789. if ($i == 0) {
  790. $startTimeFor = date('Y-m-d H:i:s', $startTime); //首次等于搜索的开始时间
  791. } else {
  792. $startTimeFor = $year_s . '-' . $month_s . '-01 00:00:00'; // 后面月份的开始时间
  793. }
  794. if ($month_s == $month_e) { // 等于搜索的结束月份
  795. $endTimeFor = date('Y-m-d H:i:s', $endTime);
  796. } else { //中间月份的结束时间
  797. $next_month = strtotime(($year_s . '-' . ($month_s + 1) . '-01 00:00:00')) - 1;
  798. $endTimeFor = date('Y-m-d 23:59:59', $next_month);
  799. }
  800. $month_ids = [];
  801. $ids_for_sel = [];
  802. $strtotime_start = strtotime($startTimeFor);
  803. $strtotime_end = strtotime($endTimeFor);
  804. foreach ($ydc_list as $k => $v) {
  805. if (strtotime($v['addtime']) > $strtotime_start && strtotime($v['addtime']) < $strtotime_end) {
  806. if (!in_array($v['customer_id'], $month_ids)) {
  807. $ids_for_sel[] = $v['customer_id'];
  808. }
  809. unset($ydc_list[$k]);
  810. }
  811. }
  812. $ids_for_one = array_intersect($customerIdList, $ids_for_sel);
  813. $ydc = array_merge($ydc, $ids_for_one);
  814. $month_s++; // 月份增加
  815. }
  816. } else {
  817. for ($y = 0; $year_s <= $year_e; $y++) {
  818. if ($year_s == $year_e) {
  819. $month_s_for = 1;
  820. $month_e_for = $month_e;
  821. for ($i = 0; $month_s_for <= $month_e_for; $i++) {
  822. $startTimeFor = $year_s . '-' . $month_s_for . '-01 00:00:00';
  823. if ($month_s_for == $month_e_for) {
  824. $endTimeFor = date('Y-m-d H:i:s', $endTime);
  825. } else {
  826. $next_month = strtotime(($year_s . '-' . ($month_s_for + 1) . '-01 00:00:00')) - 1;
  827. $endTimeFor = date('Y-m-d 23:59:59', $next_month);
  828. }
  829. $month_ids = [];
  830. $ids_for_sel = [];
  831. $strtotime_start = strtotime($startTimeFor);
  832. $strtotime_end = strtotime($endTimeFor);
  833. foreach ($ydc_list as $k => $v) {
  834. if (strtotime($v['addtime']) > $strtotime_start && strtotime($v['addtime']) < $strtotime_end) {
  835. if (!in_array($v['customer_id'], $month_ids)) {
  836. $ids_for_sel[] = $v['customer_id'];
  837. }
  838. unset($ydc_list[$k]);
  839. }
  840. }
  841. $ids_for_one = array_intersect($customerIdList, $ids_for_sel);
  842. $ydc = array_merge($ydc, $ids_for_one);
  843. $month_s_for++;
  844. }
  845. } else {
  846. if ($y == 0) {
  847. $month_s_for = $month_s;
  848. } else {
  849. $month_s_for = 1;
  850. }
  851. for ($i = 0; $month_s_for <= 12; $i++) {
  852. if ($y == 0 && $i == 0) {
  853. $startTimeFor = date('Y-m-d H:i:s', $startTime);
  854. } else {
  855. $startTimeFor = $year_s . '-' . $month_s_for . '-01 00:00:00';
  856. }
  857. if ($month_s_for == 12) {
  858. $next_month = strtotime($year_s . '-12-31 23:59:59');
  859. } else {
  860. $next_month = strtotime(($year_s . '-' . ($month_s + 1) . '-01 00:00:00')) - 1;
  861. }
  862. $endTimeFor = date('Y-m-d 23:59:59', $next_month);
  863. $month_ids = [];
  864. $ids_for_sel = [];
  865. $strtotime_start = strtotime($startTimeFor);
  866. $strtotime_end = strtotime($endTimeFor);
  867. foreach ($ydc_list as $k => $v) {
  868. if (strtotime($v['addtime']) > $strtotime_start && strtotime($v['addtime']) < $strtotime_end) {
  869. if (!in_array($v['customer_id'], $month_ids)) {
  870. $ids_for_sel[] = $v['customer_id'];
  871. }
  872. unset($ydc_list[$k]);
  873. }
  874. }
  875. $ids_for_one = array_intersect($customerIdList, $ids_for_sel);
  876. $ydc = array_merge($ydc, $ids_for_one);
  877. $month_s_for++;
  878. }
  879. }
  880. $year_s++; // 下一年
  881. }
  882. }
  883. } else {
  884. $ydc = array_intersect($customerIdList, $ydcIds);
  885. }
  886. } else {
  887. $ydc = array_intersect($customerIdList, $ydcIds);
  888. }
  889. $customerData['signed_num'] = count($qiandan);
  890. $customerData['deposit_num'] = count($jiaoding);
  891. $customerData['yylf_num'] = count($yylf);
  892. $customerData['lf_num'] = count($lf);
  893. $customerData['yydd_num'] = count($yydd);
  894. $customerData['ydd_num'] = count($ydd);
  895. $customerData['yyhd_num'] = count($yyhd);
  896. $customerData['ydc_num'] = count($ydc);
  897. return json(['code' => 0, 'data' => $customerData]);
  898. }
  899. /************************************************* 客户池仿手机端团队客户搜索 end ***************************************/
  900. /**
  901. * 客户池
  902. */
  903. public function list()
  904. {
  905. $request = request();
  906. $root_id = $request->empcrm->root_id;
  907. $empid = $request->empcrm->id;
  908. $is_manager = $request->empcrm->is_manager;
  909. $sub_orgs = orgSubIds($request->empcrm->org_id);
  910. $org_id = input('org_id', $request->empcrm->org_id, 'intval');
  911. if (!in_array($org_id, $sub_orgs)) {
  912. $org_id = $request->empcrm->org_id;
  913. }
  914. if ($is_manager) {
  915. $team_orgs = orgSubIds($org_id);
  916. } else {
  917. $team_orgs = [$org_id];
  918. }
  919. if (!$request->isAjax()) {
  920. //来源
  921. $soudata = CustomerSource::field('id,source as name')->where('root_id', $root_id)->select()->toArray();
  922. View::assign('soudata', $soudata);
  923. //设计师
  924. $list = $this->get_designer();
  925. if (!empty($list)) {
  926. $sort = array_column($list, 's');
  927. sort($sort);
  928. array_multisort($sort, SORT_ASC, $list);
  929. $sort = array_unique($sort);
  930. $list = ['sort' => $sort, 'arr' => $list];
  931. } else {
  932. $list = ['sort' => [], 'arr' => []];
  933. }
  934. View::assign('designers', $list);
  935. //客户等级
  936. $level = CustomerPortraitField::with(['select'])->where([['root_id', '=', $root_id], ['keyname', '=', 'level']])->find()->toArray();
  937. View::assign('level', $level['select']);
  938. //部门所有员工
  939. $employee_list = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->field('id,name')->select()->toArray();
  940. View::assign('employee_list', $employee_list);
  941. View::assign('empid', $empid);
  942. View::assign('is_manager', $is_manager);
  943. //指派经理
  944. $assignemp_list = Employee::where([['org_id', 'in', orgSubIds($root_id)], ['id', 'not in', array_column($list['arr'], 'id')], ['state', '=', '在职'], ['uid', '>', 0]])->column('id,name');
  945. View::assign('assignemp_list', $assignemp_list);
  946. // 馨居上
  947. //View::assign('xinjushang', $request->empcrm['xinjushang']);
  948. //员工所属部门
  949. $emporg_list = Org::where([['id', 'in', $team_orgs]])->column('id,name');
  950. View::assign('emporg_list', $emporg_list);
  951. //设计师所属部门
  952. $designer_org_list = Org::where([['path', 'like', $root_id . '-%'], ['org_type', '=', 2]])->column('name,id');
  953. View::assign('designer_org_list', $designer_org_list);
  954. // 统计模块的控制
  955. $statistics_setting = Setting::where([['name', '=', 'empcrm_pool_statistics'], ['root_id', '=', $root_id]])->findOrEmpty();
  956. $statistics_setting_content = !$statistics_setting->isEmpty() ? $statistics_setting['content'] : 0;
  957. View::assign('statistics_setting', $statistics_setting_content);
  958. //统计字段展示控制权限
  959. $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];
  960. $setting = Setting::where([['name', '=', 'teamStatisticsJurisdiction'], ['root_id', '=', $root_id]])->value('content');
  961. $setting = $setting ? explode(',', $setting) : [];
  962. foreach ($type_arr as $k => $v) {
  963. $type_arr[$k] = in_array($k, $setting) ? 1 : 0;
  964. }
  965. $sum = array_sum(array_values($type_arr));
  966. 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];
  967. View::assign('setting', $type_arr);
  968. return View::fetch();
  969. }
  970. $param = $request->only(['page' => 1, 'limit' => 10, 'addtime' => '', 'level' => '', 'source_name' => '', 'designer_name' => '', 'keyname' => '', 'followtime' => '', 'state' => '', 'employee_name' => '', 'type' => '', 'is_assign']);
  971. $search_type = input('search_type', 1, 'intval'); //数据查询方式 1,以客户为主导 2,以满足的搜索状态为主导,合并出所有客户
  972. $startTime = strtotime('2015-01-01');
  973. $endTime = strtotime(date('Y-m-d')) + 86400;
  974. $array_intersect = false;
  975. $array_intersect_ids = [];
  976. if ($param['type']) {
  977. $condition_res = $this->getCondition();
  978. $condition_get = $condition_res['condition'];
  979. $condition_ids = $condition_res['ids'];
  980. // 客户总数
  981. $customerIdList = Db::view('Customer')
  982. ->view('Org', '', 'Org.id=Customer.org_id')
  983. ->view('CustomerVisitLog', '', 'Customer.id=CustomerVisitLog.customer_id')
  984. ->where($condition_get)
  985. ->group('Customer.id')
  986. ->column('Customer.id');
  987. $customerIdList = array_intersect($customerIdList, $condition_ids);
  988. $condition[] = ['id', 'in', $customerIdList];
  989. } else {
  990. if ($search_type == 1) {
  991. $org_type = Org::where('id', '=', $org_id)->value('org_type');
  992. // 设计师部门管理员,需要查询指派给下属设计师的客户信息
  993. if ($org_type == 2) {
  994. if ($is_manager == 1) {
  995. $designer_employee = Employee::where([['org_id', 'in', $team_orgs], ['uid', '<>', 0], ['state', '=', '在职']])->column('id');
  996. $condition[] = ['designer_id', 'in', $designer_employee];
  997. // 是哪个员工
  998. // setCondition($param['employee_id'], 'designer_id', '=', $condition);
  999. if ($param['employee_name']) $condition[] = ['employee_id', 'in', explode(',', $param['employee_name'])];
  1000. } else {
  1001. $condition[] = ['designer_id', '=', $empid];
  1002. }
  1003. } else {
  1004. if ($is_manager == 1) {
  1005. $condition[] = ['org_id', 'in', $team_orgs];
  1006. $condition[] = ['employee_id', '>', 0];
  1007. // 是哪个员工
  1008. // setCondition($param['employee_id'], 'employee_id', '=', $condition);
  1009. if ($param['employee_name']) $condition[] = ['employee_id', 'in', explode(',', $param['employee_name'])];
  1010. } else {
  1011. $condition[] = ['employee_id', '=', $empid];
  1012. }
  1013. }
  1014. $condition[] = ['state', 'not in', Customer::changeState('无效', 'chaos')];
  1015. $condition[] = ['died', '<>', 2];
  1016. if (!empty($param['addtime'])) {
  1017. //录入时间
  1018. $addtimeArr = explode(' - ', $param['addtime']);
  1019. $start = $addtimeArr[0];
  1020. $end = date('Y-m-d', strtotime($addtimeArr[1]) + 86400);
  1021. $startTime = strtotime($start) - 1;
  1022. $endTime = strtotime($end);
  1023. }
  1024. $condition[] = ['addtime', '>', date('Y-m-d H:i:s', $startTime)];
  1025. $condition[] = ['addtime', '<', date('Y-m-d H:i:s', $endTime)];
  1026. } else {
  1027. $array_intersect_ids = $this->logCondition();
  1028. $array_intersect = true;
  1029. $org_type = Org::where('id', '=', $org_id)->value('org_type');
  1030. // 设计师部门管理员,需要查询指派给下属设计师的客户信息
  1031. if ($org_type == 2) {
  1032. if ($is_manager == 1) {
  1033. $designer_employee = Employee::where([['org_id', 'in', $team_orgs], ['uid', '<>', 0], ['state', '=', '在职']])->column('id');
  1034. $condition[] = ['designer_id', 'in', $designer_employee];
  1035. // 是哪个员工
  1036. setCondition($param['employee_name'], 'designer_id', '=', $condition);
  1037. } else {
  1038. $condition[] = ['designer_id', '=', $empid];
  1039. }
  1040. } else {
  1041. if ($is_manager == 1) {
  1042. $condition[] = ['org_id', 'in', $team_orgs];
  1043. $condition[] = ['employee_id', '>', 0];
  1044. // 是哪个员工
  1045. setCondition($param['employee_name'], 'employee_id', '=', $condition);
  1046. } else {
  1047. $condition[] = ['employee_id', '=', $empid];
  1048. }
  1049. }
  1050. $condition[] = ['state', 'not in', Customer::changeState('无效', 'chaos')];
  1051. $condition[] = ['died', '<>', 2];
  1052. }
  1053. }
  1054. //$condition[] = ['transfer_info', 'null', ''];
  1055. //$condition[] = ['state', 'not in', array_merge(Customer::changeState('无效', 'chaos'), Customer::changeState('签单', 'chaos'))];
  1056. if (!empty($param['level'])) $condition[] = ['level', 'in', explode(',', $param['level'])];
  1057. if (!empty($param['source_name'])) $condition[] = ['source_id', 'in', explode(',', $param['source_name'])];
  1058. if (!empty($param['designer_name'])) $condition[] = ['designer_id', 'in', explode(',', $param['designer_name'])];
  1059. if (!empty($param['is_assign']) && $param['is_assign'] == 1) $condition[] = ['assigned_personnel', 'NOTNULL', null];
  1060. if (!empty($param['is_assign']) && $param['is_assign'] == 2) $condition[] = ['assigned_personnel', 'NULL', null];
  1061. //2023-02-22 搜索条件修改
  1062. $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' => '']);
  1063. if (!empty($new_params['phone'])) {
  1064. $list = Customer::where($condition)->column('id,phone');
  1065. $keyCustomersId = [];
  1066. $cypherphone = cypherphone($new_params['phone']);
  1067. foreach ($list as $k => $v) {
  1068. if ($cypherphone == $v['phone']) $keyCustomersId[] = $v['id'];
  1069. // if (strpos($k, trim($new_params['phone'])) !== false) $keyCustomersId[] = $v;
  1070. }
  1071. unset($condition);
  1072. $condition[] = ['id', 'in', $keyCustomersId];
  1073. }
  1074. //客户姓名
  1075. if ($new_params['name']) $condition[] = ['name', 'like', '%' . $new_params['name'] . '%'];
  1076. //小区名称
  1077. if ($new_params['community_name']) $condition[] = ['community_name', 'like', '%' . $new_params['community_name'] . '%'];
  1078. //客户种类 资源库,活动,经纪人,自建
  1079. if ($new_params['customer_type']) {
  1080. $gather = [
  1081. 1 => [['crm_res_id', 'not null', null]], //'资源库'
  1082. 2 => [['crm_res_id', 'null', null], ['remark', 'not like', '%活动报名建档%'], ['agents_id', 'null', null]], //'自建'
  1083. 3 => [['remark', 'like', '%活动报名建档%']], //'活动'
  1084. 4 => [['agents_id', '>', 0]] //'经纪人'
  1085. ];
  1086. $arr = explode(',', $new_params['customer_type']);
  1087. $whereOr = [];
  1088. foreach ($arr as $val) {
  1089. if (isset($gather[$val])) {
  1090. //$whereOr[] = array_merge($condition,$gather[$val]);
  1091. $whereOr[] = $gather[$val];
  1092. }
  1093. }
  1094. if (!empty($whereOr)) {
  1095. $new_cids = Customer::where($condition)->where(function ($query) use ($whereOr) {
  1096. $query->whereOr($whereOr);
  1097. })->column('id');
  1098. unset($condition);
  1099. $condition[] = ['id', 'in', $new_cids];
  1100. } else {
  1101. //无效条件
  1102. return json(['code' => 0, 'data' => [], 'count' => 0]);
  1103. }
  1104. }
  1105. //加微时间
  1106. if (!empty($new_params['add_wechat_time'])) {
  1107. $add_wechat_time = explode(' - ', $new_params['add_wechat_time']);
  1108. $start = str_replace('/', '-', $add_wechat_time[0] . ' 00:00:00');
  1109. $end = str_replace('/', '-', $add_wechat_time[1] . ' 23:59:59');
  1110. $condition[] = ['add_wechat_time', '>=', $start];
  1111. $condition[] = ['add_wechat_time', '<=', $end];
  1112. }
  1113. //下次回访时间
  1114. if (!empty($new_params['revisit_time'])) {
  1115. $revisit_time = explode(' - ', $new_params['revisit_time']);
  1116. $start = str_replace('/', '-', $revisit_time[0] . ' 00:00:00');
  1117. $end = str_replace('/', '-', $revisit_time[1] . ' 23:59:59');
  1118. $condition[] = ['revisit_time', '>', $start];
  1119. $condition[] = ['revisit_time', '<', $end];
  1120. }
  1121. //交房时间
  1122. if (!empty($new_params['house_delivery_time'])) {
  1123. $house_delivery_time = explode(' - ', $new_params['house_delivery_time']);
  1124. $start = $house_delivery_time[0];
  1125. $end = $house_delivery_time[1] . ' 23:59:59';
  1126. $condition[] = ['house_delivery_time', '>=', $start];
  1127. $condition[] = ['house_delivery_time', '<=', $end];
  1128. }
  1129. //报名时间
  1130. if (!empty($new_params['sign_time'])) {
  1131. $sign_time = explode(' - ', $new_params['sign_time']);
  1132. $start = str_replace('/', '-', $sign_time[0] . ' 00:00:00');
  1133. $end = str_replace('/', '-', $sign_time[1] . ' 23:59:59');
  1134. $condition[] = ['sign_time', '>=', $start];
  1135. $condition[] = ['sign_time', '<=', $end];
  1136. }
  1137. //客户的跟进时间
  1138. if (!empty($new_params['last_contact_date'])) {
  1139. $last_contact = explode(' - ', $new_params['last_contact_date']);
  1140. $start = str_replace('/', '-', $last_contact[0]);
  1141. $end = str_replace('/', '-', $last_contact[1]);
  1142. $condition[] = ['last_contact_date', '>=', $start];
  1143. $condition[] = ['last_contact_date', '<=', $end];
  1144. }
  1145. //首次量房时间
  1146. if (!empty($new_params['first_liangfang'])) {
  1147. $first_liangfang = explode(' - ', $new_params['first_liangfang']);
  1148. $start = str_replace('/', '-', $first_liangfang[0]);
  1149. $end = date('Y-m-d', strtotime($first_liangfang[1]) + 86400);
  1150. $customersIdList = Customer::where($condition)->order('addtime desc')->column('id');
  1151. $lf_where[] = ['customer_id', 'in', $customersIdList];
  1152. $lf_where[] = ['state', 'in', CustomerVisitLog::changeState('已量房', 'chaos')];
  1153. $lf_where[] = ['addtime', '>=', $start];
  1154. $lf_where[] = ['addtime', '<=', $end];
  1155. $lflog = CustomerVisitLog::where($lf_where)->group('customer_id')->order('addtime asc')->column('customer_id');
  1156. unset($condition);
  1157. $condition[] = ['id', 'in', $lflog];
  1158. }
  1159. //首次到店时间
  1160. if (!empty($new_params['fisttime'])) {
  1161. $fisttime = explode(' - ', $new_params['fisttime']);
  1162. $start = str_replace('/', '-', $fisttime[0]);
  1163. $end = date('Y-m-d', strtotime($fisttime[1]) + 86400);
  1164. $customersIdList = Customer::where($condition)->order('addtime desc')->column('id');
  1165. $dd_where[] = ['customer_id', 'in', $customersIdList];
  1166. $dd_where[] = ['state', 'in', CustomerVisitLog::changeState('已到店', 'chaos')];
  1167. $dd_where[] = ['confirm_date', '>=', $start];
  1168. $dd_where[] = ['confirm_date', '<=', $end];
  1169. $ddlog = CustomerVisitLog::where($dd_where)->group('customer_id')->order('addtime asc')->column('customer_id');
  1170. unset($condition);
  1171. $condition[] = ['id', 'in', $ddlog];
  1172. }
  1173. //预计见面时间
  1174. if (!empty($new_params['subscribe_date'])) {
  1175. $subscribe_date = explode(' - ', $new_params['subscribe_date']);
  1176. $start = $subscribe_date[0];
  1177. $end = date('Y-m-d', strtotime($subscribe_date[1]) + 86400);
  1178. $customersIdList = Customer::where($condition)->order('addtime desc')->column('id');
  1179. $yj_where[] = ['customer_id', 'in', $customersIdList];
  1180. $yj_where[] = ['state', '=', 0];
  1181. $yj_where[] = ['subscribe_date', '>=', $start];
  1182. $yj_where[] = ['subscribe_date', '<=', $end];
  1183. $sublog = CustomersSubscribe::where($yj_where)->group('customer_id')->order('subscribe_date desc')->column('customer_id');
  1184. unset($condition);
  1185. $condition[] = ['id', 'in', $sublog];
  1186. }
  1187. //签单时间
  1188. if (!empty($new_params['jiaoding_time'])) {
  1189. $fisttime = explode(' - ', $new_params['jiaoding_time']);
  1190. $start = str_replace('/', '-', $fisttime[0] . ' 00:00:00');
  1191. $end = str_replace('/', '-', $fisttime[1] . ' 23:59:59');
  1192. $customersIdList = Customer::where($condition)->order('addtime desc')->column('id');
  1193. $dd_where[] = ['customer_id', 'in', $customersIdList];
  1194. $dd_where[] = ['state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')];
  1195. $ddlog = CustomerVisitLog::where($dd_where)->whereBetween('addtime', [$start, $end])->group('customer_id')->order('addtime asc')->column('customer_id');
  1196. unset($condition);
  1197. $condition[] = ['id', 'in', $ddlog];
  1198. }
  1199. //签单时间
  1200. if (!empty($new_params['qiandan_time'])) {
  1201. $fisttime = explode(' - ', $new_params['qiandan_time']);
  1202. $start = str_replace('/', '-', $fisttime[0] . ' 00:00:00');
  1203. $end = str_replace('/', '-', $fisttime[1] . ' 23:59:59');
  1204. $customersIdList = Customer::where($condition)->order('addtime desc')->column('id');
  1205. $dd_where[] = ['customer_id', 'in', $customersIdList];
  1206. $dd_where[] = ['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')];
  1207. $ddlog = CustomerVisitLog::where($dd_where)->whereBetween('addtime', [$start, $end])->group('customer_id')->order('addtime asc')->column('customer_id');
  1208. unset($condition);
  1209. $condition[] = ['id', 'in', $ddlog];
  1210. }
  1211. //客户经理
  1212. if (!empty($new_params['assign_list'])) {
  1213. $assign_employee = explode(',', $new_params['assign_list']);
  1214. $customerIdList = Customer::where($condition)->where(function ($query) use ($assign_employee) {
  1215. foreach ($assign_employee as $v) {
  1216. $query->whereOr([['assigned_personnel', 'find in set', $v]]);
  1217. }
  1218. })->column('id');
  1219. unset($condition);
  1220. $condition[] = ['id', 'in', $customerIdList];
  1221. }
  1222. //未跟进天数
  1223. if (!empty($new_params['no_visit_day'])) {
  1224. $no_visit_day = explode(',', $new_params['no_visit_day']);
  1225. $all_no_visit = [
  1226. 1 => [3, 7],
  1227. 2 => [8, 14],
  1228. 3 => [15, 30],
  1229. 4 => [31, 10000]
  1230. ];
  1231. $customersList = Customer::where($condition)->order('addtime desc')->column('id,addtime');
  1232. $wg_where[] = ['customer_id', 'in', array_column($customersList, 'id')];
  1233. $latest_visit_log = CustomerVisitLog::where($wg_where)->group('customer_id')->column('max(addtime) as addtime', 'customer_id'); //->select()->toArray();
  1234. $no_visit_ids = [];
  1235. foreach ($customersList as $k => $v) {
  1236. if (empty($latest_visit_log[$v['id']])) {
  1237. $no_visit_long = ceil((time() - strtotime($v['addtime'])) / 86400);
  1238. } else {
  1239. $no_visit_long = ceil((time() - strtotime($latest_visit_log[$v['id']])) / 86400);
  1240. }
  1241. // if($new_params['no_visit_day'] < 40){
  1242. // if($no_visit_long <= $new_params['no_visit_day']) $no_visit_ids[] = $v['id'];
  1243. // }
  1244. // if($new_params['no_visit_day'] == 40){
  1245. // if($no_visit_long > 30) $no_visit_ids[] = $v['id'];
  1246. // }
  1247. foreach ($no_visit_day as $m => $p) {
  1248. if (empty($all_no_visit[$p])) continue;
  1249. if ($no_visit_long >= $all_no_visit[$p][0] && $no_visit_long <= $all_no_visit[$p][1]) $no_visit_ids[] = $v['id'];
  1250. }
  1251. }
  1252. unset($condition);
  1253. $condition[] = ['id', 'in', $no_visit_ids];
  1254. }
  1255. // 房屋面积
  1256. if (!empty($new_params['square'])) {
  1257. $square = explode(',', $new_params['square']);
  1258. $square_arr = [
  1259. 1 => '(square > 1 and square <= 80)',
  1260. 2 => '(square > 81 and square <= 100)',
  1261. 3 => '(square > 101 and square <= 120)',
  1262. 4 => '(square > 121 and square <= 200)',
  1263. 5 => '(square > 201 and square <= 500)',
  1264. 6 => '(square > 501 and square <= 10000)'
  1265. ];
  1266. $squ_whereOr = null;
  1267. foreach ($square as $k => $v) {
  1268. if (!empty($square_arr[$v])) $squ_whereOr .= $square_arr[$v] . ' OR';
  1269. }
  1270. $customersList = Customer::where($condition)->where(function ($query) use ($squ_whereOr) {
  1271. $query->whereRaw(trim($squ_whereOr, ' OR'));
  1272. })->order('addtime desc')->column('id');
  1273. unset($condition);
  1274. $condition[] = ['id', 'in', $customersList];
  1275. }
  1276. //业务员所属部门
  1277. if (!empty($new_params['emporg'])) {
  1278. $orgids = explode(',', $new_params['emporg']);
  1279. $empids = Employee::where([['root_id', '=', $root_id], ['org_id', 'in', $orgids]])->column('id');
  1280. $customersList = Customer::where($condition)->where('employee_id', 'in', $empids)->column('id');
  1281. unset($condition);
  1282. $condition[] = ['id', 'in', $customersList];
  1283. }
  1284. //设计师所属部门
  1285. if (!empty($new_params['designer_org'])) {
  1286. $orgids = explode(',', $new_params['designer_org']);
  1287. $empids = Employee::where([['root_id', '=', $root_id], ['org_id', 'in', $orgids]])->column('id');
  1288. $customersList = Customer::where($condition)->where('designer_id', 'in', $empids)->column('id');
  1289. unset($condition);
  1290. $condition[] = ['id', 'in', $customersList];
  1291. }
  1292. if ($search_type == 1) {
  1293. if (!empty($param['addtime'])) {
  1294. //录入时间
  1295. $addtimeArr = explode(' - ', $param['addtime']);
  1296. $start = $addtimeArr[0];
  1297. $end = date('Y-m-d', strtotime($addtimeArr[1]) + 86400);
  1298. $startTime = strtotime($start) - 1;
  1299. $endTime = strtotime($end);
  1300. }
  1301. } else {
  1302. if (!empty($param['followtime'])) {
  1303. //录入时间
  1304. $addtimeArr = explode(' - ', $param['followtime']);
  1305. $start = $addtimeArr[0];
  1306. $end = date('Y-m-d', strtotime($addtimeArr[1]) + 86400);
  1307. $startTime = strtotime($start) - 1;
  1308. $endTime = strtotime($end);
  1309. }
  1310. }
  1311. // 状态筛选
  1312. $c_ids = [];
  1313. if (!empty($param['state'])) {
  1314. $condition1[] = ['state', 'in', Customer::changeState('待确认', 'chaos')];
  1315. $condition1[] = ['crm_res_id', 'NULL', null];
  1316. $condition2[] = ['state', 'not in', Customer::changeState('待确认', 'chaos')];
  1317. //从公海获取的客户是资源库的要显示在客户池
  1318. $condition3[] = ['state', 'in', Customer::changeState('待确认', 'chaos')];
  1319. $condition3[] = ['crm_res_id', 'NOTNULL', null];
  1320. $condition3[] = ['valid_time', 'NOTNULL', null];
  1321. $customersIdList = Customer::where($condition)->where(function ($query) use ($condition1, $condition2, $condition3) {
  1322. $query->whereOr([$condition1, $condition2, $condition3]);
  1323. })->order('addtime desc')->column('id');
  1324. $stateall = explode(',', $param['state']);
  1325. $allsta = [];
  1326. foreach ($stateall as $k => $v) {
  1327. if ($v == '已签单') $v = '已交定';
  1328. if ($v == '已转单') $v = '已签单';
  1329. if (in_array($v, ['待确认', '有效', '未到访'])) continue;
  1330. $onestate = CustomerVisitLog::changeState($v, 'chaos');
  1331. $allsta = array_merge($onestate, $allsta);
  1332. }
  1333. $visit_where[] = ['customer_id', 'in', $customersIdList];
  1334. $visit_where[] = ['state', 'in', $allsta];
  1335. $visit_where[] = ['addtime', '>', date('Y-m-d H:i:s', $startTime)];
  1336. $visit_where[] = ['addtime', '<', date('Y-m-d H:i:s', $endTime)];
  1337. $logCustomersIdList = CustomerVisitLog::where($visit_where)->group('customer_id')->column('customer_id');
  1338. $c_ids = $logCustomersIdList;
  1339. foreach ($stateall as $k => $v) {
  1340. if ($v == '待确认') {
  1341. $confirmedids = Customer::where($condition)->where(function ($query) use ($condition1, $condition3) {
  1342. $query->whereOr([$condition1, $condition3]);
  1343. })->column('id');
  1344. }
  1345. if ($v == '有效') {
  1346. $validids = Customer::where($condition)->where('state', 'not in', Customer::changeState('待确认', 'chaos'))->column('id');
  1347. }
  1348. if ($v == '未到访') {
  1349. $novisitids = Customer::where($condition)->where('state', 'in', Customer::changeState('未到访', 'chaos'))->column('id');
  1350. }
  1351. }
  1352. unset($condition, $condition1, $condition2, $condition3);
  1353. if (!empty($confirmedids)) $c_ids = array_merge($confirmedids, $c_ids);
  1354. if (!empty($validids)) $c_ids = array_merge($validids, $c_ids);
  1355. if (!empty($novisitids)) $c_ids = array_merge($novisitids, $c_ids);
  1356. } else {
  1357. //2023-02-10 客户有效状态 为排除待确认和无效, 或者 待确认并且不是资源库过来的
  1358. $state1 = Customer::changeState('待确认', 'chaos');
  1359. $state2 = Customer::changeState('无效', 'chaos');
  1360. $query1 = array_merge($condition, [['state', 'not in', array_merge($state1, $state2)]]);
  1361. $query2 = array_merge($condition, [['state', 'in', $state1], ['crm_res_id', 'NULL', NULL]]);
  1362. $query3 = array_merge($condition, [['state', 'in', $state1], ['crm_res_id', 'NOTNULL', NULL], ['valid_time', 'NOTNULL', null]]);
  1363. $ids = Customer::whereOr([$query1, $query2, $query3])->order('id desc')->column('id');
  1364. unset($condition);
  1365. $c_ids = $ids;
  1366. }
  1367. if ($array_intersect) {
  1368. $c_ids = array_intersect($c_ids, $array_intersect_ids);
  1369. $count = count($c_ids);
  1370. $c_ids = array_slice($c_ids, ($param['page'] - 1) * $param['limit'], $param['limit']);
  1371. $condition[] = ['id', 'in', $c_ids];
  1372. } else {
  1373. $count = count($c_ids);
  1374. $c_ids = array_slice($c_ids, ($param['page'] - 1) * $param['limit'], $param['limit']);
  1375. $condition[] = ['id', 'in', $c_ids];
  1376. }
  1377. $order = isset($param['order']) ? $param['order'] : 'id desc';
  1378. $page = intval($param['page']);
  1379. $limit = intval($param['limit']);
  1380. //$data = $this->selCustomer($condition, $page, $limit, $order, $root_id);
  1381. $data = $this->selCustomer($condition, 0, 0, $order, $root_id);
  1382. $no_visit_state = ['未到访', '待确认'];
  1383. if (!empty($stateall) && in_array('未到访', $stateall)) $no_visit_state = ['待确认'];
  1384. $visit_state = ['已到店', '已量房', '已到场', '已签单', '已转单', '已卖卡'];
  1385. foreach ($data as &$item) {
  1386. //客户种类
  1387. $item['customer_type'] = $item['crm_res_id'] ? '资源库' : ($item['remark'] == '活动报名建档' ? '活动' : ($item['agents_id'] ? '经纪人' : '自建'));
  1388. // 有'已到店', '已量房', '已到场', '已签单', '已转单'状态的话不显示未到访、待确认
  1389. // 有未到访状态不显示待确认
  1390. $state = explode(',', $item['state']);
  1391. $jiaoji = array_intersect($visit_state, $state);
  1392. $no_jiaoji = array_intersect($no_visit_state, $state);
  1393. if (count($no_jiaoji) == 2) { // 有未到访、待确认两个状态
  1394. $state = ['未到访'];
  1395. }
  1396. if (count($state) > 1 && count($jiaoji) > 0) {
  1397. $state = array_diff($state, $no_visit_state);
  1398. }
  1399. $item['state'] = implode(',', $state);
  1400. $item['jiange_day'] = ceil((strtotime(date('Y-m-d')) - strtotime($item['last_contact_date'])) / 86400);
  1401. $item['estimated_sign_money'] = $item['estimated_sign_money'] == 0 ? '' : $item['estimated_sign_money'];
  1402. }
  1403. //$count = Customer::where($condition)->count();
  1404. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  1405. }
  1406. //资源库客户列表
  1407. public function resources_list()
  1408. {
  1409. $request = request();
  1410. $root_id = $request->empcrm->root_id;
  1411. $empid = $request->empcrm->id;
  1412. $is_manager = $request->empcrm->is_manager;
  1413. $team_orgs = [$request->empcrm->org_id];
  1414. $org_employee = [$empid];
  1415. // if($is_manager ==1){
  1416. // $team_orgs = orgSubIds($request->empcrm->org_id);
  1417. // $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
  1418. // }
  1419. if (!$request->isAjax()) {
  1420. $label = Setting::where(['root_id' => $root_id, 'name' => 'crm_label'])->value('content');
  1421. $label = empty($label) ? [] : explode(',', $label);
  1422. View::assign('label', $label);
  1423. //部门所有员工
  1424. $employee_list = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->field('id,name')->select()->toArray();
  1425. View::assign('employee_list', $employee_list);
  1426. View::assign('empid', $empid);
  1427. View::assign('is_manager', $is_manager);
  1428. $source_list = CustomerSource::where('root_id', '=', $root_id)->select()->toArray();
  1429. View::assign('source_list', $source_list);
  1430. return View::fetch();
  1431. }
  1432. $param = $request->only(['page' => 1, 'limit' => 10, 'addtime', 'label', 'sign' => 0, 'employee_id', 'source_id' => '']);
  1433. $order = isset($param['order']) ? $param['order'] : 'id desc';
  1434. $page = intval($param['page']);
  1435. $limit = intval($param['limit']);
  1436. // if(!empty($param['employee_id']) && $is_manager ==1) $org_employee = [$param['employee_id']];
  1437. $condition[] = ['employee_id', 'in', $org_employee];
  1438. if (!empty($param['addtime'])) {
  1439. //录入时间
  1440. $addtimeArr = explode(' - ', $param['addtime']);
  1441. $start = $addtimeArr[0];
  1442. $end = date('Y-m-d', strtotime($addtimeArr[1]) + 86400);
  1443. $condition[] = ['addtime', 'between', [$start, $end]];
  1444. }
  1445. if (!empty($param['label'])) $condition[] = ['remark', 'like', '%' . $param['label'] . '%'];
  1446. if ($param['sign'] == 1) $condition[] = ['remark', '<>', ''];
  1447. if ($param['sign'] == 2) $condition[] = ['remark', '=', ''];
  1448. //$condition[] = ['transfer_info', 'null', ''];
  1449. if (!empty($param['source_id'])) $condition[] = ['source_id', '=', $param['source_id']];
  1450. $condition[] = ['state', 'not in', array_merge(Customer::changeState('无效', 'chaos'), Customer::changeState('签单', 'chaos'))];
  1451. //查询资源库分配后待确认客户
  1452. $condition[] = ['state', 'in', Customer::changeState('待确认', 'chaos')];
  1453. $condition[] = ['crm_res_id', 'NOT NULL', null];
  1454. $condition[] = ['valid_time', 'NULL', null];
  1455. $allids = Customer::where($condition)->column('id');
  1456. //$vslogid = CustomerVisitLog::where([['customer_employee_id','in',$org_employee],['state','in',CustomerVisitLog::changeState('已签单','chaos')]])->column('customer_id');
  1457. $vslogid = CustomerVisitLog::where([['customer_id', 'in', $allids], ['state', 'not in', CustomerVisitLog::changeState('待确认', 'chaos')]])->group('customer_id')->column('customer_id');
  1458. $condition[] = ['id', 'not in', $vslogid];
  1459. $data = $this->selCustomer($condition, $page, $limit, $order, $root_id);
  1460. foreach ($data as &$item) {
  1461. //客户种类
  1462. $item['customer_type'] = $item['crm_res_id'] ? '资源库' : ($item['remark'] == '活动报名建档' ? '活动' : ($item['agents_id'] ? '经纪人' : '自建'));
  1463. $callnum = OutCallLog::where([
  1464. ['customer_id', '=', $item['id']],
  1465. ['employee_id', '=', $item['employee_id']],
  1466. ['addtime', '>=', $item['employee_time']]
  1467. ])->count();
  1468. if (empty($callnum)) {
  1469. $callnum = OutCallMbLog::where([
  1470. ['customer_id', '=', $item['id']],
  1471. ['employee_id', '=', $item['employee_id']],
  1472. ['addtime', '>=', $item['employee_time']]
  1473. ])->count();
  1474. }
  1475. $item['callnum'] = $callnum;
  1476. }
  1477. $count = Customer::where($condition)->count();
  1478. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  1479. }
  1480. //资源库标记客户
  1481. public function resources_sign()
  1482. {
  1483. $request = request();
  1484. $root_id = $request->empcrm->root_id;
  1485. $empid = $request->empcrm->id;
  1486. $is_manager = $request->empcrm->is_manager;
  1487. $param = $request->only(['id', 'label', 'content' => '']);
  1488. $where = [['id', '=', $param['id']], ['crm_res_id', 'NOTNULL', null], ['employee_id', '=', $empid], ['state', 'in', Customer::changeState('待确认', 'chaos')]];
  1489. if ($is_manager) $where = [['id', '=', $param['id']], ['crm_res_id', 'NOTNULL', null], ['state', 'in', Customer::changeState('待确认', 'chaos')]];
  1490. $customer = Customer::where($where)->find();
  1491. if (empty($customer)) return json(['code' => 1, 'msg' => '客户不存在']);
  1492. if (!$request->isAjax()) {
  1493. $label = Setting::where(['root_id' => $root_id, 'name' => 'crm_label'])->value('content');
  1494. $label = empty($label) ? [] : explode(',', $label);
  1495. View::assign('label', $label);
  1496. View::assign('data', $customer);
  1497. $log = CustomerVisitLog::where(['employee_id' => $empid, 'customer_id' => $param['id'], 'data_type' => 'out_call'])->field('addtime,remark')->order('addtime desc')->select();
  1498. View::assign('log', $log);
  1499. // 判断是否有外呼系统
  1500. $outcallSetting = OutCallSetting::where(['root_id' => $root_id])->find();
  1501. if ($outcallSetting !== null) {
  1502. // 如果有外呼系统则获取登陆人拨打的电话列表
  1503. View::assign('outcall', 1);
  1504. $phones = empty($request->empcrm['out_call_phone']) ? [] : explode(',', $request->empcrm['out_call_phone']);
  1505. array_unshift($phones, $request->empcrm['phone']);
  1506. View::assign('outcall_phone', $phones);
  1507. } else {
  1508. View::assign('outcall', 0);
  1509. }
  1510. $company = Company::where([['root_id', '=', $root_id]])->find();
  1511. View::assign('fish', in_array($customer['source_id'], $company->channelSource['fish']) ? 1 : 0);
  1512. return View::fetch();
  1513. }
  1514. if (empty($param['label']) || empty($param['content'])) return json(['code' => 1, 'msg' => '请选择或填写标记内容']);
  1515. if (!empty($param['label'])) {
  1516. if ($param['label'] !== '有效') {
  1517. $customer->save(['remark' => '#' . $param['label'] . '#']);
  1518. } else {
  1519. // 同部门能否重复录入开关设置
  1520. $empcrm_repeat[] = ['root_id', '=', $root_id];
  1521. $empcrm_repeat[] = ['name', '=', 'empcrm_customer_repeat'];
  1522. $repeat_setting = Setting::where($empcrm_repeat)->findOrEmpty();
  1523. $emp_org = $request->empcrm->org_id;
  1524. //判断同部门
  1525. if (!$repeat_setting->isEmpty()) {
  1526. $repeat_org = explode(',', $repeat_setting['content']);
  1527. if (!in_array($emp_org, $repeat_org)) {
  1528. $repeat_org = [$emp_org];
  1529. }
  1530. } else {
  1531. $repeat_org = [$emp_org];
  1532. }
  1533. // 新建档,检测手机号是否重复
  1534. $customerExist = Customer::where([
  1535. ['phone|phone1|phone2', '=', cypherphone($customer['phone'])],
  1536. ['org_id', 'in', $repeat_org],
  1537. ['employee_id', '>', 0], // 员工客户
  1538. ['id', '<>', $param['id']]
  1539. ])->where(
  1540. function ($query) {
  1541. $not_sure = Customer::changeState('待确认', 'chaos');
  1542. $or1[] = ['crm_res_id', 'null', null];
  1543. $or2[] = ['crm_res_id', '>', 0];
  1544. $or2[] = ['state', 'not in', $not_sure];
  1545. $query->whereOr([$or1, $or2]);
  1546. }
  1547. )->field('id,employee_id,is_resource,org_id,source_id')->select()->toArray();
  1548. if (!empty($customerExist)) {
  1549. foreach ($customerExist as $ex) {
  1550. if ($ex['employee_id'] != $customer['employee_id']) {
  1551. //查询撞单客户渠道
  1552. $source_name = '';
  1553. if ($ex['source_id']) {
  1554. $source = CustomerSource::where(['id' => $ex['source_id']])->value('source');
  1555. $source_name = $source . '渠道客户';
  1556. }
  1557. $have_emp = Employee::where('id', '=', $ex['employee_id'])->value('name');
  1558. return json(['code' => 1, 'msg' => '该客户与员工' . $have_emp . $source_name . '撞单,无法标记有效']);
  1559. } else {
  1560. return json(['code' => 1, 'msg' => '您名下已有此手机号客户,无法标记有效']);
  1561. }
  1562. }
  1563. }
  1564. $customer->save(['state' => 1, 'valid_time' => date('Y-m-d H:i:s')]);
  1565. }
  1566. }
  1567. // 添加跟进记录
  1568. CustomerVisitLog::Create([
  1569. 'customer_id' => $customer->id,
  1570. 'type' => 1,
  1571. 'employee_id' => $empid,
  1572. 'user_id' => $request->empcrm->uid,
  1573. 'org_id' => $request->empcrm->org_id,
  1574. 'customer_employee_id' => $customer->employee_id,
  1575. 'customer_org_id' => $customer->org_id,
  1576. 'data_type' => 'out_call',
  1577. 'remark' => !empty($param['label']) ? '#' . $param['label'] . '#' . $param['content'] : $param['content']
  1578. ]);
  1579. return json(['code' => 0, 'msg' => '标识成功']);
  1580. }
  1581. //待确认
  1582. public function stay_confirm_list()
  1583. {
  1584. $request = request();
  1585. $root_id = $request->empcrm->root_id;
  1586. $empid = $request->empcrm->id;
  1587. $is_manager = $request->empcrm->is_manager;
  1588. $org_employee = [$empid];
  1589. if ($is_manager == 1) {
  1590. $team_orgs = orgSubIds($request->empcrm->org_id);
  1591. $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
  1592. }
  1593. if (!$request->isAjax()) {
  1594. $this->screen();
  1595. return View::fetch();
  1596. }
  1597. $param = $request->only(['page' => 1, 'limit' => 10, 'employee_id', 'level', 'source_id', 'designer_id', 'keyname', 'date']);
  1598. //$condition[] = empty($param['date']) ? ['revisit_time', '=', date('Y-m-d') . ' 00:00:00'] : ['revisit_time', '=', $param['date'] . ' 00:00:00'];
  1599. if (!empty($param['employee_id']) && $is_manager == 1 && in_array($param['employee_id'], $org_employee)) $org_employee = [$param['employee_id']];
  1600. $condition[] = ['employee_id', 'in', $org_employee];
  1601. if (!empty($param['date'])) {
  1602. $newtime = explode(' - ', $param['date']);
  1603. $condition[] = ['addtime', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']];
  1604. }
  1605. $condition[] = ['state', 'in', Customer::changeState('待确认', 'chaos')];
  1606. //增加排除资源库分配的待确认客户
  1607. $condition[] = ['crm_res_id', 'NULL', null];
  1608. $order = isset($param['order']) ? $param['order'] : 'id desc';
  1609. $page = intval($param['page']);
  1610. $limit = intval($param['limit']);
  1611. $data = $this->selCustomer($condition, $page, $limit, $order, $root_id);
  1612. $count = Customer::where($condition)->count();
  1613. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  1614. }
  1615. //业绩页面(已签单)
  1616. public function achment_list()
  1617. {
  1618. $request = request();
  1619. $root_id = $request->empcrm->root_id;
  1620. $empid = $request->empcrm->id;
  1621. $is_manager = $request->empcrm->is_manager;
  1622. $org_employee = [$empid];
  1623. if ($is_manager == 1) {
  1624. $team_orgs = orgSubIds($request->empcrm->org_id);
  1625. $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
  1626. }
  1627. if (!$request->isAjax()) {
  1628. $this->screen();
  1629. return View::fetch();
  1630. }
  1631. $param = $request->only(['page' => 1, 'limit' => 10, 'employee_id', 'level', 'source_id', 'designer_id', 'keyname', 'date', 'customer_type']);
  1632. if (!empty($param['employee_id']) && $is_manager == 1 && in_array($param['employee_id'], $org_employee)) $org_employee = [$param['employee_id']];
  1633. $condition[] = ['employee_id', 'in', $org_employee];
  1634. $condition[] = ['signed_money', '<>', 0];
  1635. $condition[] = ['state', 'not in', Customer::changeState('无效', 'chaos')];
  1636. if (!empty($param['level'])) $condition[] = ['level', '=', $param['level']];
  1637. if (!empty($param['source_id'])) $condition[] = ['source_id', '=', $param['source_id']];
  1638. if (!empty($param['designer_id'])) $condition[] = ['designer_id', '=', $param['designer_id']];
  1639. if (!empty($param['keyname'])) $condition[] = ['name|community_name', 'like', '%' . $param['keyname'] . '%'];
  1640. $order = isset($param['order']) ? $param['order'] : 'id desc';
  1641. $page = intval($param['page']);
  1642. $limit = intval($param['limit']);
  1643. $vslogid = CustomerVisitLog::where([['customer_employee_id', 'in', $org_employee], ['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')]])->column('customer_id');
  1644. if (!empty($param['date'])) {
  1645. $time = explode(' - ', $param['date']);
  1646. $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');
  1647. }
  1648. $condition[] = ['id', 'in', $vslogid];
  1649. //客户种类 资源库,活动,经纪人,自建
  1650. if (!empty($param['customer_type'])) {
  1651. $gather = [
  1652. 1 => [['crm_res_id', 'not null', null]], //'资源库'
  1653. 2 => [['crm_res_id', 'null', null], ['remark', 'not like', '%活动报名建档%'], ['agents_id', 'null', null]], //'自建'
  1654. 3 => [['remark', 'like', '%活动报名建档%']], //'活动'
  1655. 4 => [['agents_id', '>', 0]] //'经纪人'
  1656. ];
  1657. $arr = explode(',', $param['customer_type']);
  1658. $whereOr = [];
  1659. foreach ($arr as $val) {
  1660. if (isset($gather[$val])) {
  1661. //$whereOr[] = array_merge($condition,$gather[$val]);
  1662. $whereOr[] = $gather[$val];
  1663. }
  1664. }
  1665. if (!empty($whereOr)) {
  1666. $new_cids = Customer::where($condition)->where(function ($query) use ($whereOr) {
  1667. $query->whereOr($whereOr);
  1668. })->column('id');
  1669. unset($condition);
  1670. $condition[] = ['id', 'in', $new_cids];
  1671. } else {
  1672. //无效条件
  1673. return json(['code' => 0, 'data' => [], 'count' => 0]);
  1674. }
  1675. }
  1676. $data = $this->selCustomer($condition, $page, $limit, $order, $root_id);
  1677. foreach ($data as $key => $val) {
  1678. $sign = [];
  1679. if (!empty($val['sign'])) $sign = $val['sign'];
  1680. $data[$key]['dingqian_day'] = !empty($sign) && !empty($val['jiaoding_time']) ? round((strtotime($sign['confirm_date']) - strtotime($val['jiaoding_time'])) / 86400) : 0;
  1681. $data[$key]['daoqian_day'] = !empty($sign) && !empty($val['fisttime']) ? round((strtotime($sign['confirm_date']) - strtotime($val['fisttime'])) / 86400) : 0;
  1682. $data[$key]['all_money'] = !empty($sign) ? $sign['money'] : 0;
  1683. $data[$key]['pf_money'] = !empty($val['square']) && !empty($sign) ? round(($sign['money']) / $val['square'], 2) : 0;
  1684. $data[$key]['customer_type'] = $val['crm_res_id'] ? '资源库' : ($val['remark'] == '活动报名建档' ? '活动' : ($val['agents_id'] ? '装修推荐官' : '自建'));
  1685. }
  1686. $count = Customer::where($condition)->count();
  1687. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  1688. }
  1689. //更新已签单客户页面
  1690. public function signcus_detail()
  1691. {
  1692. $request = request();
  1693. $root_id = $request->empcrm->root_id;
  1694. $empid = $request->empcrm->id;
  1695. $org_employee = [$empid];
  1696. $param = $request->only(['id']);
  1697. $this->screen();
  1698. //客户等级 隶属计划
  1699. $fields = CustomerPortraitField::with(['select'])->where([['root_id', '=', $root_id], ['keyname', 'in', ['level', 'subjection_plan', 'housetype_arrow']]])->select()->toArray();
  1700. View::assign('fields', $fields);
  1701. //意向风格
  1702. $decostyle_list = Decostyle::field('id,name,root_id as pid')->where(['root_id' => $root_id])->where([['type', '=', 0]])->order('id desc')->select()->toArray();
  1703. View::assign('decostyle_list', $decostyle_list);
  1704. //意向产品
  1705. $product = CustomerPackage::field('id, name, total_price')->where(['root_id' => $root_id])->select();
  1706. View::assign('product', $product);
  1707. $data = $this->seLemp($param['id'], $empid);
  1708. View::assign('data', $data);
  1709. return View::fetch();
  1710. }
  1711. //日报点击弹出详情页面
  1712. public function daily_detail()
  1713. {
  1714. $request = request();
  1715. $root_id = $request->empcrm->root_id;
  1716. $empid = $request->empcrm->id;
  1717. $is_manager = $request->empcrm->is_manager;
  1718. $org_employee = [$empid];
  1719. if ($is_manager == 1) {
  1720. $team_orgs = orgSubIds($request->empcrm->org_id);
  1721. $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
  1722. }
  1723. $param = $this->request->only(['date', 'state', 'empid' => '', 'source_id' => '', 'page' => 1, 'limit' => 17]);
  1724. if (!$request->isAjax()) {
  1725. View::assign('date', $param['date']);
  1726. View::assign('state', $param['state']);
  1727. View::assign('empid', $param['empid']);
  1728. View::assign('source_id', $param['source_id']);
  1729. return View::fetch();
  1730. }
  1731. $order = isset($param['order']) ? $param['order'] : 'id desc';
  1732. $page = intval($param['page']);
  1733. $limit = intval($param['limit']);
  1734. if ($is_manager == 1 && !empty($param['empid']) && $param['empid'] != 'undefined' && in_array($param['empid'], $org_employee)) $org_employee = [$param['empid']];
  1735. $condition[] = ['employee_id', 'in', $org_employee];
  1736. if (empty($param['date'])) return json(['code' => 1, 'msg' => '参数错误']);
  1737. if (!empty($param['date'])) {
  1738. $newtime = explode('到', $param['date']);
  1739. }
  1740. $condition[] = ['sign_time', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']];
  1741. if (!empty($param['source_id'] && $param['source_id'] != 'undefined')) $condition[] = ['source_id', 'in', explode(',', $param['source_id'])];
  1742. //无效条件
  1743. $wx_where[] = ['root_id', '=', $root_id];
  1744. $wx_where[] = ['employee_id', 'in', $org_employee];
  1745. $wx_where[] = ['cus_addtime', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']];
  1746. if (!empty($param['source_id'])) $wx_where[] = ['source_id', 'in', explode(',', $param['source_id'])];
  1747. $vlst = '确认到店';
  1748. if ($param['state'] == '到访') {
  1749. $vlst = '确认到店';
  1750. } elseif ($param['state'] == '交定') {
  1751. $vlst = '交定';
  1752. } elseif ($param['state'] == '签单') {
  1753. $vlst = '签单';
  1754. } elseif ($param['state'] == '线索') {
  1755. $condition[] = ['state', 'not in', Customer::changeState('无效', 'chaos')];
  1756. $clueids = Customer::where($condition)->column('id');
  1757. //置为无效的客户
  1758. $wuxiaoids = CustomerInvalidLog::where($wx_where)->column('customer_id');
  1759. //去掉无效后又被人获取的客户
  1760. //$cuswxids = Db::table('fl_customer')->where([['id','in',$wuxiaoids],['employee_id','NULL',null]])->column('id');
  1761. $cuswxids = Customer::where([['id', 'in', $wuxiaoids], ['employee_id', 'NULL', null]])->withTrashed()->column('id');
  1762. $recycleids = CustomerRecycle::where([['customer_id', 'in', $wuxiaoids]])->column('customer_id');
  1763. $wuxiaoids = array_merge($cuswxids, $recycleids);
  1764. if (!empty($wuxiaoids)) $clueids = array_merge($clueids, $wuxiaoids);
  1765. unset($condition);
  1766. $condition = implode(',', $clueids);
  1767. $page = ($page - 1) * 17;
  1768. $data = $this->cus_recycle($condition, $page, $limit, $order, $root_id);
  1769. return json(['code' => 0, 'data' => $data['data'], 'count' => $data['count']]);
  1770. } elseif ($param['state'] == '有效') {
  1771. $condition[] = ['state', 'not in', array_merge(Customer::changeState('无效', 'chaos'), Customer::changeState('待确认', 'chaos'))];
  1772. $clueids = Customer::where($condition)->column('id');
  1773. $wx_where[] = ['status', '=', 1];
  1774. $wuxiaoids = CustomerInvalidLog::where($wx_where)->column('customer_id');
  1775. //去掉无效后又被人获取的客户
  1776. $cuswxids = Customer::where([['id', 'in', $wuxiaoids], ['employee_id', 'NULL', null]])->withTrashed()->column('id');
  1777. $recycleids = CustomerRecycle::where([['customer_id', 'in', $wuxiaoids]])->column('customer_id');
  1778. $wuxiaoids = array_merge($cuswxids, $recycleids);
  1779. if (!empty($wuxiaoids)) $clueids = array_merge($clueids, $wuxiaoids);
  1780. unset($condition);
  1781. $condition = implode(',', $clueids);
  1782. $page = ($page - 1) * 17;
  1783. $data = $this->cus_recycle($condition, $page, $limit, $order, $root_id);
  1784. return json(['code' => 0, 'data' => $data['data'], 'count' => $data['count']]);
  1785. } elseif ($param['state'] == '待确认') {
  1786. $condition[] = ['state', 'in', Customer::changeState('待确认', 'chaos')];
  1787. $data = $this->selCustomer($condition, $page, $limit, $order, $root_id);
  1788. $count = Customer::where($condition)->count();
  1789. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  1790. }
  1791. $where[] = ['customer_employee_id', 'in', $org_employee];
  1792. if ($vlst == '确认到店') {
  1793. $where[] = ['state', 'in', array_merge(CustomerVisitLog::changeState($vlst, 'chaos'), CustomerVisitLog::changeState('已到场', 'chaos'), CustomerVisitLog::changeState('已量房', 'chaos'))];
  1794. } else {
  1795. $where[] = ['state', 'in', CustomerVisitLog::changeState($vlst, 'chaos')];
  1796. }
  1797. if (!empty($param['date'])) {
  1798. $where[] = ['confirm_date', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']];
  1799. }
  1800. $vslogid = CustomerVisitLog::where($where)->column('customer_id');
  1801. $vslogid = !empty($vslogid) ? array_unique($vslogid) : [];
  1802. if ($vlst == '确认到店') {
  1803. $vslogid = !empty($vslogid) ? $this->selBeforelog($vslogid, $newtime[0] . ' 00:00:00') : [];
  1804. }
  1805. if (!empty($param['source_id'])) {
  1806. $vslogid = Customer::where([['id', 'in', $vslogid], ['source_id', 'in', explode(',', $param['source_id'])]])->column('id');
  1807. }
  1808. if ($param['state'] != '线索') {
  1809. unset($condition);
  1810. $condition[] = ['id', 'in', $vslogid];
  1811. }
  1812. $condition = implode(',', $vslogid);
  1813. $page = ($page - 1) * 17;
  1814. $data = $this->cus_recycle($condition, $page, $limit, $order, $root_id);
  1815. return json(['code' => 0, 'data' => $data['data'], 'count' => $data['count']]);
  1816. // $data = $this->selCustomer($condition,$page,$limit,$order,$root_id);
  1817. // $count = Customer::where($condition)->count();
  1818. // return json(['code' => 0, 'data' => $data, 'count' => $count]);
  1819. }
  1820. //日报点击弹出详情页面
  1821. public function new_daily_detail()
  1822. {
  1823. $request = request();
  1824. $root_id = $request->empcrm->root_id;
  1825. $empid = $request->empcrm->id;
  1826. $is_manager = $request->empcrm->is_manager;
  1827. $org_employee = [$empid];
  1828. if ($is_manager == 1) {
  1829. $team_orgs = orgSubIds($request->empcrm->org_id);
  1830. $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
  1831. }
  1832. $param = $this->request->only(['date', 'state', 'empid' => '', 'page' => 1, 'limit' => 17, 'orgid' => '']);
  1833. if (!$request->isAjax()) {
  1834. //$this->screen();
  1835. View::assign('date', $param['date']);
  1836. View::assign('state', $param['state']);
  1837. View::assign('empid', $param['empid']);
  1838. View::assign('orgid', $param['orgid']);
  1839. return View::fetch();
  1840. }
  1841. $order = isset($param['order']) ? $param['order'] : 'id desc';
  1842. $page = intval($param['page']);
  1843. $limit = intval($param['limit']);
  1844. if ($is_manager == 1 && !empty($param['empid']) && $param['empid'] != 'undefined' && in_array($param['empid'], $org_employee)) $org_employee = [$param['empid']];
  1845. if ($is_manager == 1 && !empty($param['orgid']) && $param['orgid'] != 'undefined') {
  1846. $org_employee = Employee::where([['org_id', 'in', orgSubIds($param['orgid'])], ['root_id', '=', $root_id], ['uid', '>', 0], ['state', '=', '在职']])->column('id');
  1847. }
  1848. if (empty($param['date'])) return json(['code' => 1, 'msg' => '参数错误']);
  1849. if (!empty($param['date'])) {
  1850. $newtime = explode('到', $param['date']);
  1851. }
  1852. $condition[] = ['employee_id', 'in', $org_employee];
  1853. if ($param['state'] != 'addnew') $condition[] = ['died', '<>', 2];
  1854. $condition[] = ['state', 'not in', Customer::changeState('无效', 'chaos')];
  1855. $vlst = '确认到店';
  1856. if ($param['state'] == '到访') {
  1857. $vlst = '确认到店';
  1858. } elseif ($param['state'] == '交定') {
  1859. $vlst = '交定';
  1860. } elseif ($param['state'] == '签单') {
  1861. $vlst = '签单';
  1862. } elseif ($param['state'] == 'clue' || $param['state'] == 'addnew') {
  1863. $condition[] = ['sign_time', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']];
  1864. $addnewids = Customer::where($condition)->column('id');
  1865. //置为无效的客户
  1866. $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');
  1867. if (!empty($wuxiaoids)) $addnewids = array_merge($addnewids, $wuxiaoids);
  1868. unset($condition);
  1869. //$condition[] = ['id','in',$addnewids];
  1870. $condition = implode(',', $addnewids);
  1871. $page = ($page - 1) * 17;
  1872. $data = $this->cus_recycle($condition, $page, $limit, $order, $root_id);
  1873. return json(['code' => 0, 'data' => $data['data'], 'count' => $data['count']]);
  1874. } elseif (in_array($param['state'], ['yixiang', 'liangfang', 'daodian'])) {
  1875. $condition[] = ['assign_time', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']];
  1876. $condition[] = ['assign_type', '=', $param['state']];
  1877. } elseif ($param['state'] == 'valid') {
  1878. $condition[] = ['sign_time', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']];
  1879. $condition[] = ['state', 'not in', Customer::changeState('待确认', 'chaos')];
  1880. $validids = Customer::where($condition)->column('id');
  1881. //置为无效的客户
  1882. $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');
  1883. if (!empty($wuxiaoids)) $validids = array_merge($validids, $wuxiaoids);
  1884. unset($condition);
  1885. //$condition[] = ['id','in',$validids];
  1886. $condition = implode(',', $validids);
  1887. $page = ($page - 1) * 17;
  1888. $data = $this->cus_recycle($condition, $page, $limit, $order, $root_id);
  1889. return json(['code' => 0, 'data' => $data['data'], 'count' => $data['count']]);
  1890. }
  1891. $where[] = ['customer_employee_id', 'in', $org_employee];
  1892. if ($vlst == '确认到店') {
  1893. $where[] = ['state', 'in', array_merge(CustomerVisitLog::changeState($vlst, 'chaos'), CustomerVisitLog::changeState('已到场', 'chaos'), CustomerVisitLog::changeState('已量房', 'chaos'))];
  1894. } else {
  1895. $where[] = ['state', 'in', CustomerVisitLog::changeState($vlst, 'chaos')];
  1896. }
  1897. if (!empty($param['date'])) {
  1898. $where[] = ['confirm_date', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']];
  1899. }
  1900. //$where[] =['customer_id','in',$customer_list];
  1901. $vslogid = CustomerVisitLog::where($where)->group('customer_id')->column('customer_id');
  1902. if ($vlst == '确认到店') {
  1903. $vslogid = !empty(array_unique($vslogid)) ? $this->selBeforelog($vslogid, $newtime[0] . ' 00:00:00') : [];
  1904. }
  1905. if (!in_array($param['state'], ['clue', 'addnew', 'yixiang', 'liangfang', 'daodian', 'valid'])) {
  1906. unset($condition);
  1907. $condition[] = ['id', 'in', $vslogid];
  1908. }
  1909. $data = $this->selCustomer($condition, $page, $limit, $order, $root_id);
  1910. $count = Customer::where($condition)->count();
  1911. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  1912. }
  1913. //员工日报页面
  1914. public function emp_daily()
  1915. {
  1916. $request = request();
  1917. $root_id = $request->empcrm->root_id;
  1918. $empid = $request->empcrm->id;
  1919. $is_manager = $request->empcrm->is_manager;
  1920. $org_employee = [$empid];
  1921. $team_orgs = [$request->empcrm->org_id];
  1922. $param = $request->only(['date', 'source_id', 'org_id', 'page' => 1, 'limit' => 17]);
  1923. // var_dump($param);
  1924. // exit;
  1925. if ($is_manager == 1) {
  1926. $team_orgs = orgSubIds($request->empcrm->org_id);
  1927. $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->page($param['page'], $param['limit'])->column('id');
  1928. }
  1929. if (!$request->isAjax()) {
  1930. $data['company_name'] = Company::where('root_id', $root_id)->value('company_name');
  1931. $data['empname'] = $request->empcrm->name;
  1932. $data['date'] = !empty($param['date']) ? $param['date'] : date('Y-m');
  1933. View::assign('data', $data);
  1934. $soudata = CustomerSource::field('source as name,id as value')->where('root_id', $root_id)->select()->toArray();
  1935. View::assign('soudata', json_encode($soudata));
  1936. $org_data = Org::where('id', 'in', $team_orgs)->field('id,name')->select()->toArray();
  1937. View::assign('org_data', $org_data);
  1938. View::assign('is_manager', $is_manager);
  1939. return View::fetch();
  1940. }
  1941. if (!empty($param['date'])) {
  1942. $newtime = explode('到', $param['date']);
  1943. }
  1944. $where[] = ['sign_time', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']];
  1945. if (!empty($param['org_id']) && $is_manager == 1) {
  1946. $org_employee = Employee::where([['org_id', '=', $param['org_id']], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
  1947. }
  1948. $where[] = ['employee_id', 'in', $org_employee];
  1949. $where[] = ['state', 'not in', Customer::changeState('无效', 'chaos')];
  1950. $cuslist = Customer::where($where)->column('id,employee_id,state,add_wechat_time');
  1951. if (!empty($param['source_id'])) {
  1952. $cuslist = Customer::where($where)->where([['source_id', 'in', $param['source_id']]])->column('id,employee_id,state,add_wechat_time');
  1953. }
  1954. $state1 = CustomerVisitLog::changeState('已到店', 'chaos');
  1955. $state2 = CustomerVisitLog::changeState('已交定', 'chaos');
  1956. $state3 = CustomerVisitLog::changeState('已签单', 'chaos');
  1957. $state4 = CustomerVisitLog::changeState('已量房', 'chaos');
  1958. $state5 = CustomerVisitLog::changeState('无效', 'chaos');
  1959. $state6 = Customer::changeState('待确认', 'chaos');
  1960. $state7 = Customer::changeState('无效', 'chaos');
  1961. $state8 = CustomerVisitLog::changeState('已到场', 'chaos');
  1962. $wherev[] = ['customer_employee_id', 'in', $org_employee];
  1963. $wherev[] = ['confirm_date', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']];
  1964. $wherev[] = ['state', 'in', array_merge($state1, $state2, $state3, $state8, $state4, $state5)];
  1965. //记录
  1966. if (empty($param['source_id'])) {
  1967. $visitlog_list = CustomerVisitLog::where($wherev)->group('state,customer_id')->column('customer_id,customer_employee_id,state,money');
  1968. } else {
  1969. $w = Customer::where([['source_id', 'in', $param['source_id']]]);
  1970. $visitlog_list = CustomerVisitLog::hasWhere('customer', $w)
  1971. ->where([['CustomerVisitLog.customer_employee_id', 'in', $org_employee], ['CustomerVisitLog.confirm_date', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']]])
  1972. ->group('state,customer_id')->column('CustomerVisitLog.customer_id,CustomerVisitLog.customer_employee_id,CustomerVisitLog.state,CustomerVisitLog.money');
  1973. }
  1974. $metids = [];
  1975. foreach ($visitlog_list as $key => $val) {
  1976. if (in_array($val['state'], array_merge($state1, $state4, $state8))) $metids[] = $val['customer_id'];
  1977. $newvslog[$val['customer_employee_id']][] = $val;
  1978. }
  1979. $yesids = !empty(array_unique($metids)) ? $this->selBeforelog(array_unique($metids), $newtime[0] . ' 00:00:00') : [];
  1980. $wx_where[] = ['root_id', '=', $root_id];
  1981. $wx_where[] = ['employee_id', 'in', $org_employee];
  1982. $wx_where[] = ['cus_addtime', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']];
  1983. if (!empty($param['source_id'])) $wx_where[] = ['source_id', 'in', $param['source_id']];
  1984. $wuxiaolog_list = CustomerInvalidLog::where($wx_where)->column('customer_id,employee_id,status');
  1985. //员工列表
  1986. $emplist = Employee::where([['id', 'in', $org_employee]])->field('id,name')->select()->toArray();
  1987. $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];
  1988. // 有效客户量
  1989. $validState = CustomerStateCheck::where([['id', 'in', $org_employee], ['check_state', '=', 1]])->group('employee_id')->column('count(*) as num', 'employee_id');
  1990. //获取所有的有效id
  1991. $validids = [];
  1992. foreach ($cuslist as $key => $val) {
  1993. if (!in_array($val['state'], $state6, true)) {
  1994. $validids[] = $val['id'];
  1995. }
  1996. }
  1997. $wxcusids = array_column($cuslist, 'id');
  1998. $meetids = [];
  1999. foreach ($emplist as $key => $val) {
  2000. $dai_confirmed = $add_wechat = $clue_cont = $valid = $meet = $deposit = $sign = $basic_money = $other_money = $invalid_no = $invalid_yes = $invalid_meet = $invalid_deposit = 0;
  2001. $depositids = $cusids = $daystate = $wuxiao = [];
  2002. foreach ($cuslist as $k => $v) {
  2003. if ($v['employee_id'] == $val['id']) {
  2004. $clue_cont++;
  2005. if (!empty($v['add_wechat_time'])) $add_wechat++;
  2006. if (in_array($v['state'], $state6, true)) $dai_confirmed++;
  2007. if (!in_array($v['state'], $state6, true)) {
  2008. $valid++;
  2009. }
  2010. $cusids[] = $v['id'];
  2011. }
  2012. }
  2013. //处理无效的客户还原到员工身上为有效
  2014. foreach ($wuxiaolog_list as $k => $v) {
  2015. if ($v['employee_id'] == $val['id'] && !in_array($v['customer_id'], $validids) && $v['status'] == 1) {
  2016. $valid++;
  2017. $validids[] = $v['customer_id'];
  2018. }
  2019. if ($v['employee_id'] == $val['id'] && !in_array($v['customer_id'], $wxcusids)) {
  2020. $clue_cont++;
  2021. $wxcusids[] = $v['customer_id'];
  2022. }
  2023. }
  2024. if (!empty($newvslog[$val['id']])) {
  2025. foreach ($newvslog[$val['id']] as $k => $v) {
  2026. if (in_array($v['state'], array_merge($state1, $state4, $state8), true) && !in_array($v['customer_id'], $meetids) && in_array($v['customer_id'], $yesids)) {
  2027. $meet++;
  2028. $meetids[] = $v['customer_id'];
  2029. }
  2030. if (in_array($v['state'], $state2, true)) {
  2031. $deposit++;
  2032. $depositids[] = $v['customer_id'];
  2033. }
  2034. if (in_array($v['state'], $state3, true)) {
  2035. $basic_money += $v['money'];
  2036. $sign++;
  2037. }
  2038. if (in_array($v['state'], $state5, true)) {
  2039. $invalid_no++;
  2040. $wuxiao[] = $v['customer_id'];
  2041. }
  2042. }
  2043. unset($newvslog[$val['id']]);
  2044. }
  2045. if (!empty($newvslog[$val['id']])) {
  2046. foreach ($wuxiao as $k => $v) {
  2047. if (in_array($v, $meetids, true)) {
  2048. $invalid_meet++;
  2049. }
  2050. if (in_array($v, $depositids, true)) {
  2051. $invalid_deposit++;
  2052. }
  2053. }
  2054. }
  2055. unset($cusids, $depositids, $wuxiao);
  2056. $emplist[$key]['source_id'] = !empty($param['source_id']) ? implode(',', $param['source_id']) : '';
  2057. $emplist[$key]['clue_cont'] = $clue_cont;
  2058. $emplist[$key]['add_wechat'] = $add_wechat;
  2059. $emplist[$key]['dai_confirmed'] = $dai_confirmed;
  2060. $emplist[$key]['valid_cont'] = $valid;
  2061. $emplist[$key]['valid_percen'] = $valid && $clue_cont ? round($valid / $clue_cont * 100, 2) : 0;
  2062. $emplist[$key]['meet_cont'] = $meet;
  2063. $emplist[$key]['meet_percen'] = !empty($meet) && !empty($clue_cont) ? round($meet / $clue_cont * 100, 2) : 0;
  2064. $emplist[$key]['deposit_cont'] = $deposit;
  2065. $emplist[$key]['deposit_percen'] = !empty($deposit) && !empty($meet) ? round($deposit / $meet * 100, 2) : 0;
  2066. $emplist[$key]['sign_cont'] = $sign;
  2067. $emplist[$key]['basic_money'] = $basic_money;
  2068. $emplist[$key]['other_money'] = $other_money;
  2069. $emplist[$key]['all_money'] = $basic_money + $other_money;
  2070. $emplist[$key]['invalid_no'] = $invalid_no;
  2071. $emplist[$key]['invalid_yes'] = $invalid_yes;
  2072. $emplist[$key]['invalid_meet'] = $invalid_meet;
  2073. $emplist[$key]['invalid_deposit'] = $invalid_deposit;
  2074. $emplist[$key]['valid_check_num'] = $validState[$val['id']] ?? 0;
  2075. //总数
  2076. $t['clue_cont'] += $clue_cont;
  2077. $t['add_wechat'] += $add_wechat;
  2078. $t['dai_confirmed'] += $dai_confirmed;
  2079. $t['valid_cont'] += $valid;
  2080. $t['meet_cont'] += $meet;
  2081. $t['deposit_cont'] += $deposit;
  2082. $t['sign_cont'] += $sign;
  2083. $t['basic_money'] += $basic_money;
  2084. $t['other_money'] += $other_money;
  2085. $t['all_money'] += $other_money + $basic_money;
  2086. $t['invalid_no'] += $invalid_no;
  2087. $t['invalid_yes'] += $invalid_yes;
  2088. $t['invalid_meet'] += $invalid_meet;
  2089. $t['invalid_deposit'] += $invalid_deposit;
  2090. $t['valid_check_num'] += $emplist[$key]['valid_check_num'];
  2091. }
  2092. if ($t['clue_cont'] !== 0) {
  2093. $t['valid_percen'] = $t['valid_cont'] == 0 ? 0 : round($t['valid_cont'] / $t['clue_cont'] * 100, 2);
  2094. $t['meet_percen'] = $t['meet_cont'] == 0 ? 0 : round($t['meet_cont'] / $t['clue_cont'] * 100, 2);
  2095. $t['deposit_percen'] = ($t['deposit_cont'] == 0 || $t['meet_cont'] == 0) ? 0 : round($t['deposit_cont'] / $t['meet_cont'] * 100, 2);
  2096. }
  2097. array_push($emplist, $t);
  2098. //var_dump($xin);
  2099. $count = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->count();
  2100. return json(['code' => 0, 'data' => $emplist, 'count' => $count, 'msg' => '获取成功']);
  2101. }
  2102. /**
  2103. * 无效报表页面
  2104. */
  2105. public function invalid_report()
  2106. {
  2107. $request = request();
  2108. $root_id = $request->empcrm->root_id;
  2109. $empid = $request->empcrm->id;
  2110. $is_manager = request()->empcrm->is_manager;
  2111. // 管理员查看层级以下的
  2112. if ($is_manager == 1) {
  2113. $team_orgs = orgSubIds($request->empcrm->org_id);
  2114. $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
  2115. } else {
  2116. $org_employee = [$empid];
  2117. }
  2118. $param = $request->only(['date', 'source_id']);
  2119. if (!$request->isAjax()) {
  2120. $data['company_name'] = Company::where('root_id', $root_id)->value('company_name');
  2121. $data['empname'] = $request->empcrm->name;
  2122. $data['date'] = !empty($param['date']) ? $param['date'] : date('Y-m');
  2123. View::assign('data', $data);
  2124. return View::fetch();
  2125. }
  2126. if (!empty($param['date'])) {
  2127. $newtime = explode('到', $param['date']);
  2128. }
  2129. $where = [
  2130. ['addtime', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']],
  2131. ['employee_id', 'in', $org_employee]
  2132. ];
  2133. //来源列表
  2134. $source_list = CustomerSource::field('id,source as name')->where('root_id', $root_id)->select()->toArray();
  2135. $source_id = array_column($source_list, 'id');
  2136. //跟踪记录
  2137. $visitlog_list = CustomerVisitLog::where(array_merge($where, [['state', 'in', CustomerVisitLog::changeState('无效', 'chaos')]]))->column('id,customer_id,remark');
  2138. //有效
  2139. $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');
  2140. //无效
  2141. $cusid = array_column($visitlog_list, 'customer_id');
  2142. $nocuslist = Customer::where([['id', 'in', $cusid]])->group('source_id')->column('count(source_id)', 'source_id');
  2143. $nocuslist_id = Customer::where([['id', 'in', $cusid], ['source_id', 'in', $source_id]])->column('source_id', 'id');
  2144. $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];
  2145. foreach ($source_list as &$val) {
  2146. $val['clue_cont'] = 0;
  2147. $val['invalid_no'] = 0;
  2148. $val['remark1'] = 0;
  2149. $val['remark2'] = 0;
  2150. $val['remark3'] = 0;
  2151. $val['remark4'] = 0;
  2152. $val['remark5'] = 0;
  2153. $val['remark6'] = 0;
  2154. $val['remark7'] = 0;
  2155. $val['remark8'] = 0;
  2156. $val['remark9'] = 0;
  2157. $val['remark10'] = 0;
  2158. //无效
  2159. $val['invalid_no'] = isset($nocuslist[$val['id']]) ? $nocuslist[$val['id']] : 0;
  2160. //线索
  2161. $clue_cont = isset($yescuslist[$val['id']]) ? $yescuslist[$val['id']] : 0;
  2162. $val['clue_cont'] = $clue_cont + $val['invalid_no'];
  2163. $remark = ['同行' => 'remark1', '空错号' => 'remark2', '不是本人' => 'remark3', '局部装修' => 'remark4', '超服务区域' => 'remark5', '与内部撞单' => 'remark6', '多次联系不上' => 'remark7', '装修预算太低' => 'remark8', '没有装修需求' => 'remark9', '精装修' => 'remark10'];
  2164. foreach ($visitlog_list as $k => $v) {
  2165. if (isset($nocuslist_id[$v['customer_id']]) && $nocuslist_id[$v['customer_id']] == $val['id'] && isset($remark[$v['remark']])) {
  2166. $val[$remark[$v['remark']]]++;
  2167. }
  2168. }
  2169. $t['clue_cont'] += $val['clue_cont'];
  2170. $t['invalid_no'] += $val['invalid_no'];
  2171. $t['remark1'] += $val['remark1'];
  2172. $t['remark2'] += $val['remark2'];
  2173. $t['remark3'] += $val['remark3'];
  2174. $t['remark4'] += $val['remark4'];
  2175. $t['remark5'] += $val['remark5'];
  2176. $t['remark6'] += $val['remark6'];
  2177. $t['remark7'] += $val['remark7'];
  2178. $t['remark8'] += $val['remark8'];
  2179. $t['remark9'] += $val['remark9'];
  2180. $t['remark10'] += $val['remark10'];
  2181. }
  2182. array_push($source_list, $t);
  2183. return json(['code' => 0, 'data' => $source_list, 'count' => 0, 'msg' => '获取成功']);
  2184. }
  2185. //待回访
  2186. public function repay_list()
  2187. {
  2188. $request = request();
  2189. $root_id = $request->empcrm->root_id;
  2190. $empid = $request->empcrm->id;
  2191. $is_manager = $request->empcrm->is_manager;
  2192. $org_employee = [$empid];
  2193. if ($is_manager == 1) {
  2194. $team_orgs = orgSubIds($request->empcrm->org_id);
  2195. $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
  2196. }
  2197. if (!$request->isAjax()) {
  2198. $this->screen();
  2199. return View::fetch();
  2200. }
  2201. $param = $request->only(['page' => 1, 'limit' => 10, 'jiange_day', 'employee_name', 'level', 'source_name', 'designer_name', 'name', 'date', 'last_contact_date', 'community_name']);
  2202. // 客户表revisit_time字段存在无法识别日期格式,查询需指定开始日期
  2203. $condition[] = empty($param['date']) ? ['revisit_time', '=', date('Y-m-d') . ' 00:00:00'] : ['revisit_time', '=', $param['date'] . ' 00:00:00'];
  2204. if (!empty($param['employee_id']) && $is_manager == 1 && in_array($param['employee_id'], $org_employee)) $org_employee = [$param['employee_id']];
  2205. $condition[] = ['employee_id', 'in', $org_employee];
  2206. //$condition[] = ['state', 'not in',Customer::changeState('已签单', 'chaos')];
  2207. //客户姓名
  2208. if (!empty($param['name'])) $condition[] = ['name', 'like', '%' . $param['name'] . '%'];
  2209. //小区名称
  2210. if (!empty($param['community_name'])) $condition[] = ['community_name', 'like', '%' . $param['community_name'] . '%'];
  2211. //等级
  2212. if (!empty($param['level'])) $condition[] = ['level', 'in', explode(',', $param['level'])];
  2213. //来源
  2214. if (!empty($param['source_name'])) $condition[] = ['source_id', 'in', explode(',', $param['source_name'])];
  2215. //所属员工
  2216. if (!empty($param['employee_name'])) $condition[] = ['employee_id', 'in', explode(',', $param['employee_name'])];
  2217. //所属设计师
  2218. if (!empty($param['designer_name'])) $condition[] = ['designer_id', 'in', explode(',', $param['designer_name'])];
  2219. $order = isset($param['order']) ? $param['order'] : 'id desc';
  2220. $page = intval($param['page']);
  2221. $limit = intval($param['limit']);
  2222. //客户的跟进时间
  2223. if (!empty($param['last_contact_date'])) {
  2224. $last_contact = explode(' - ', $param['last_contact_date']);
  2225. $start = str_replace('/', '-', $last_contact[0]);
  2226. $end = str_replace('/', '-', $last_contact[1]);
  2227. $condition[] = ['last_contact_date', '>=', $start];
  2228. $condition[] = ['last_contact_date', '<=', $end];
  2229. }
  2230. //未跟进天数
  2231. if (!empty($param['jiange_day'])) {
  2232. $no_visit_day = explode(',', $param['jiange_day']);
  2233. $all_no_visit = [
  2234. 1 => [3, 7],
  2235. 2 => [8, 14],
  2236. 3 => [15, 30],
  2237. 4 => [31, 10000]
  2238. ];
  2239. $customersList = Customer::where($condition)->order('addtime desc')->column('id,addtime');
  2240. $wg_where[] = ['customer_id', 'in', array_column($customersList, 'id')];
  2241. $latest_visit_log = CustomerVisitLog::where($wg_where)->group('customer_id')->column('max(addtime) as addtime', 'customer_id'); //->select()->toArray();
  2242. $no_visit_ids = [];
  2243. foreach ($customersList as $k => $v) {
  2244. if (empty($latest_visit_log[$v['id']])) {
  2245. $no_visit_long = ceil((time() - strtotime($v['addtime'])) / 86400);
  2246. } else {
  2247. $no_visit_long = ceil((time() - strtotime($latest_visit_log[$v['id']])) / 86400);
  2248. }
  2249. foreach ($no_visit_day as $m => $p) {
  2250. if (empty($all_no_visit[$p])) continue;
  2251. if ($no_visit_long >= $all_no_visit[$p][0] && $no_visit_long <= $all_no_visit[$p][1]) $no_visit_ids[] = $v['id'];
  2252. }
  2253. }
  2254. unset($condition);
  2255. $condition[] = ['id', 'in', $no_visit_ids];
  2256. }
  2257. $data = $this->selCustomer($condition, $page, $limit, $order, $root_id);
  2258. foreach ($data as $key => $val) {
  2259. $data[$key]['jiange_day'] = ceil((strtotime(date('Y-m-d')) - strtotime($val['last_contact_date'])) / 86400);
  2260. }
  2261. $count = Customer::where($condition)->count();
  2262. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  2263. }
  2264. //到访(确认到场)列表 包含 到店、量房、到场、签单、转单
  2265. public function confirm_visit_list()
  2266. {
  2267. $request = request();
  2268. $root_id = $request->empcrm->root_id;
  2269. $empid = $request->empcrm->id;
  2270. $is_manager = $request->empcrm->is_manager;
  2271. $org_employee = [$empid];
  2272. $team_orgs = [$request->empcrm->org_id];
  2273. if ($is_manager == 1) {
  2274. $team_orgs = orgSubIds($request->empcrm->org_id);
  2275. $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
  2276. }
  2277. if (!$request->isAjax()) {
  2278. $this->screen();
  2279. //指派经理
  2280. $assignemp_list = Employee::where([['org_id', 'in', orgSubIds($root_id)], ['state', '=', '在职'], ['uid', '>', 0]])->column('id,name');
  2281. View::assign('assignemp_list', $assignemp_list);
  2282. //指派部门
  2283. $assignorg_list = Org::where([['path', 'like', $root_id . '-%']])->column('id,name');
  2284. View::assign('assignorg_list', $assignorg_list);
  2285. $assign_type = [['id' => 1, 'name' => '意向指派'], ['id' => 2, 'name' => '量房指派'], ['id' => 3, 'name' => '到店指派']];
  2286. View::assign('assign_type', $assign_type);
  2287. return View::fetch();
  2288. }
  2289. $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']);
  2290. if (!empty($param['employee_id']) && $is_manager == 1 && in_array($param['employee_id'], $org_employee)) $org_employee = [$param['employee_id']];
  2291. //$condition[] = ['employee_id', 'in', $org_employee];
  2292. //$condition[] = ['state', 'not in',Customer::changeState('无效','chaos')];
  2293. $condition[] = ['org_id', 'in', $team_orgs];
  2294. $tmp_condition = "";
  2295. //客户姓名
  2296. if (!empty($param['name'])) $condition[] = ['name', 'like', '%' . $param['name'] . '%'];
  2297. //小区名称
  2298. if (!empty($param['community_name'])) $condition[] = ['community_name', 'like', '%' . $param['community_name'] . '%'];
  2299. //等级
  2300. if (!empty($param['level'])) $condition[] = ['level', 'in', explode(',', $param['level'])];
  2301. //来源
  2302. if (!empty($param['source_name'])) $condition[] = ['source_id', 'in', explode(',', $param['source_name'])];
  2303. //所属员工
  2304. if (!empty($param['employee_name'])) $condition[] = ['employee_id', 'in', explode(',', $param['employee_name'])];
  2305. //所属设计师
  2306. if (!empty($param['designer_name'])) $condition[] = ['designer_id', 'in', explode(',', $param['designer_name'])];
  2307. $order = isset($param['order']) ? $param['order'] : 'id desc';
  2308. if ($param['times'] == 1) $order = 'assign_time asc';
  2309. if ($param['times'] == 2) $order = 'assign_time desc';
  2310. if ($param['times'] == 3) $order = 'last_contact_date asc';
  2311. if ($param['times'] == 4) $order = 'last_contact_date desc';
  2312. $page = intval($param['page']);
  2313. $limit = intval($param['limit']);
  2314. //客户的跟进时间
  2315. if (!empty($param['last_contact_date'])) {
  2316. $last_contact = explode(' - ', $param['last_contact_date']);
  2317. $start = str_replace('/', '-', $last_contact[0]);
  2318. $end = str_replace('/', '-', $last_contact[1]);
  2319. $condition[] = ['last_contact_date', '>=', $start . ' 00:00:00'];
  2320. $condition[] = ['last_contact_date', '<=', $end . ' 23:59:59'];
  2321. }
  2322. //客户种类 资源库,活动,经纪人,自建
  2323. if (!empty($param['customer_type'])) {
  2324. $gather = [
  2325. 1 => [['crm_res_id', 'not null', null]], //'资源库'
  2326. 2 => [['crm_res_id', 'null', null], ['remark', 'not like', '%活动报名建档%'], ['agents_id', 'null', null]], //'自建'
  2327. 3 => [['remark', 'like', '%活动报名建档%']], //'活动'
  2328. 4 => [['agents_id', '>', 0]] //'经纪人'
  2329. ];
  2330. $arr = explode(',', $param['customer_type']);
  2331. $whereOr = [];
  2332. foreach ($arr as $val) {
  2333. if (isset($gather[$val])) {
  2334. $whereOr[] = $gather[$val];
  2335. }
  2336. }
  2337. if (!empty($whereOr)) {
  2338. $new_cids = Customer::where($condition)->where(function ($query) use ($whereOr) {
  2339. $query->whereOr($whereOr);
  2340. })->column('id');
  2341. unset($condition);
  2342. $condition[] = ['id', 'in', $new_cids];
  2343. } else {
  2344. //无效条件
  2345. return json(['code' => 0, 'data' => [], 'count' => 0]);
  2346. }
  2347. }
  2348. //指派时间查询
  2349. if (!empty($param['assign_time'])) {
  2350. $assign_time = explode('-', $param['assign_time']);
  2351. $start = $assign_time[0] . ' 00:00:00';
  2352. $end = $assign_time[1] . ' 23:59:59';
  2353. $condition[] = ['assign_time', '>', $start];
  2354. $condition[] = ['assign_time', '<', $end];
  2355. }
  2356. //$cusids = Customer::where($condition)->column('id');
  2357. //$vs_where[] = ['customer_id','in',$cusids];
  2358. $vs_where[] = ['customer_employee_id', 'in', $org_employee];
  2359. $vs_where[] = ['state', 'in', array_merge(CustomerVisitLog::changeState('确认到店', 'chaos'), CustomerVisitLog::changeState('已量房', 'chaos'), CustomerVisitLog::changeState('已到场', 'chaos'), CustomerVisitLog::changeState('已交定', 'chaos'), CustomerVisitLog::changeState('已签单', 'chaos'))];
  2360. if (!empty($param['date'])) $vs_where[] = ['confirm_date', 'between', [$param['date'] . ' 00:00:00', $param['date'] . ' 23:59:59']];
  2361. $vslogid = CustomerVisitLog::where($vs_where)->column('customer_id');
  2362. $condition[] = ['id', 'in', array_unique($vslogid)];
  2363. //手机号
  2364. if (!empty($param['phone'])) {
  2365. $list = Customer::where($condition)->order('addtime desc')->field('id,name,community_name,phone')->select()->toArray();
  2366. $customerIdList = [];
  2367. foreach ($list as $v) {
  2368. if (strpos($v['phone'], trim($param['phone'])) !== false) $customerIdList[] = $v['id'];
  2369. }
  2370. unset($condition);
  2371. $condition[] = ['id', 'in', $customerIdList];
  2372. }
  2373. //客户经理
  2374. if (!empty($param['assign_list'])) {
  2375. $assign_employee = explode(',', $param['assign_list']);
  2376. $customerIdList = Customer::where($condition)->where(function ($query) use ($assign_employee) {
  2377. foreach ($assign_employee as $v) {
  2378. $query->whereOr([['assigned_personnel', 'find in set', $v]]);
  2379. }
  2380. })->column('id');
  2381. unset($condition);
  2382. $condition[] = ['id', 'in', $customerIdList];
  2383. }
  2384. //指派部门
  2385. if (!empty($param['assign_org'])) {
  2386. $assign_org = explode(',', $param['assign_org']);
  2387. $all_assigids = [];
  2388. $assigned_personnelList = Customer::where($condition)->column('assigned_personnel,id,designer_id');
  2389. foreach ($assigned_personnelList as $k => $v) {
  2390. $assigned_personal = !empty($v['assigned_personnel']) ? explode(',', $v['assigned_personnel']) : [];
  2391. if (!empty($v['designer_id'])) $assigned_personal[] = $v['designer_id'];
  2392. $assign_orgid = Employee::where([['id', 'in', $assigned_personal], ['org_id', 'in', $assign_org], ['root_id', '=', $root_id]])->column('org_id');
  2393. if (!empty($assign_orgid)) $all_assigids[] = $v['id'];
  2394. }
  2395. unset($condition);
  2396. $condition[] = ['id', 'in', $all_assigids];
  2397. }
  2398. //到店时间
  2399. if (!empty($param['daodian_time'])) {
  2400. $daodian_time = explode(' - ', $param['daodian_time']);
  2401. if (empty($daodian_time)) return json(['code' => 1, 'msg' => '时间参数错误']);
  2402. $start = str_replace('/', '-', $daodian_time[0]);
  2403. $end = str_replace('/', '-', $daodian_time[1]);
  2404. $customerIdList = Customer::where($condition)->column('id');
  2405. $logwhere[] = ['confirm_date', '>=', $start . ' 00:00:00'];
  2406. $logwhere[] = ['confirm_date', '<=', $end . ' 23:59:59'];
  2407. $logwhere[] = ['state', 'in', CustomerVisitLog::changeState('已到店', 'chaos')];
  2408. $logwhere[] = ['customer_id', 'in', $customerIdList];
  2409. $customerIdList = CustomerVisitLog::where($logwhere)->group('customer_id')->column('customer_id');
  2410. unset($condition);
  2411. $condition[] = ['id', 'in', $customerIdList];
  2412. }
  2413. //量房时间
  2414. if (!empty($param['liangfang_time'])) {
  2415. $liangfang_time = explode(' - ', $param['liangfang_time']);
  2416. if (empty($liangfang_time)) return json(['code' => 1, 'msg' => '时间参数错误']);
  2417. $start = str_replace('/', '-', $liangfang_time[0]);
  2418. $end = str_replace('/', '-', $liangfang_time[1]);
  2419. $customerIdList = Customer::where($condition)->column('id');
  2420. $logwhere[] = ['confirm_date', '>=', $start . ' 00:00:00'];
  2421. $logwhere[] = ['confirm_date', '<=', $end . ' 23:59:59'];
  2422. $logwhere[] = ['state', 'in', CustomerVisitLog::changeState('已量房', 'chaos')];
  2423. $logwhere[] = ['customer_id', 'in', $customerIdList];
  2424. $customerIdList = CustomerVisitLog::where($logwhere)->group('customer_id')->column('customer_id');
  2425. unset($condition);
  2426. $condition[] = ['id', 'in', $customerIdList];
  2427. }
  2428. //交定时间
  2429. if (!empty($param['jiaoding_time'])) {
  2430. $jiaoding_time = explode(' - ', $param['jiaoding_time']);
  2431. if (empty($jiaoding_time)) return json(['code' => 1, 'msg' => '时间参数错误']);
  2432. $start = str_replace('/', '-', $jiaoding_time[0]);
  2433. $end = str_replace('/', '-', $jiaoding_time[1]);
  2434. $customerIdList = Customer::where($condition)->column('id');
  2435. $logwhere[] = ['confirm_date', '>=', $start . ' 00:00:00'];
  2436. $logwhere[] = ['confirm_date', '<=', $end . ' 23:59:59'];
  2437. $logwhere[] = ['state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')];
  2438. $logwhere[] = ['customer_id', 'in', $customerIdList];
  2439. $customerIdList = CustomerVisitLog::where($logwhere)->group('customer_id')->column('customer_id');
  2440. unset($condition);
  2441. $condition[] = ['id', 'in', $customerIdList];
  2442. }
  2443. //签单时间
  2444. if (!empty($param['qiandan_time'])) {
  2445. $qiandan_time = explode(' - ', $param['qiandan_time']);
  2446. if (empty($qiandan_time)) return json(['code' => 1, 'msg' => '时间参数错误']);
  2447. $start = str_replace('/', '-', $qiandan_time[0]);
  2448. $end = str_replace('/', '-', $qiandan_time[1]);
  2449. $customerIdList = Customer::where($condition)->column('id');
  2450. $logwhere[] = ['confirm_date', '>=', $start . ' 00:00:00'];
  2451. $logwhere[] = ['confirm_date', '<=', $end . ' 23:59:59'];
  2452. $logwhere[] = ['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')];
  2453. $logwhere[] = ['customer_id', 'in', $customerIdList];
  2454. $customerIdList = CustomerVisitLog::where($logwhere)->group('customer_id')->column('customer_id');
  2455. unset($condition);
  2456. $condition[] = ['id', 'in', $customerIdList];
  2457. }
  2458. //交定金额
  2459. if (!empty($param['jiaoding_money'])) {
  2460. $jiaoding_money = explode('-', $param['jiaoding_money']);
  2461. if (empty($jiaoding_money)) return json(['code' => 1, 'msg' => '参数错误']);
  2462. $customerIdList = Customer::where($condition)->column('id');
  2463. $logwhere[] = ['state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')];
  2464. $logwhere[] = ['money', 'between', $jiaoding_money];
  2465. $logwhere[] = ['customer_id', 'in', $customerIdList];
  2466. $customerIdList = CustomerVisitLog::where($logwhere)->group('customer_id')->column('customer_id');
  2467. unset($condition);
  2468. $condition[] = ['id', 'in', $customerIdList];
  2469. }
  2470. //签单金额
  2471. if (!empty($param['qiandan_money'])) {
  2472. $sign_money = explode('-', $param['qiandan_money']);
  2473. if (empty($sign_money)) return json(['code' => 1, 'msg' => '参数错误']);
  2474. $customerIdList = Customer::where($condition)->column('id');
  2475. $logwhere[] = ['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')];
  2476. $logwhere[] = ['money', 'between', $sign_money];
  2477. $logwhere[] = ['customer_id', 'in', $customerIdList];
  2478. $customerIdList = CustomerVisitLog::where($logwhere)->group('customer_id')->column('customer_id');
  2479. unset($condition);
  2480. $condition[] = ['id', 'in', $customerIdList];
  2481. }
  2482. // 房屋面积
  2483. if (!empty($param['square'])) {
  2484. $square = explode(',', $param['square']);
  2485. $square_arr = [
  2486. 1 => '(square > 1 and square <= 80)',
  2487. 2 => '(square > 81 and square <= 100)',
  2488. 3 => '(square > 101 and square <= 120)',
  2489. 4 => '(square > 121 and square <= 200)',
  2490. 5 => '(square > 201 and square <= 500)',
  2491. 6 => '(square > 501 and square <= 10000)'
  2492. ];
  2493. $squ_whereOr = null;
  2494. foreach ($square as $k => $v) {
  2495. if (!empty($square_arr[$v])) $squ_whereOr .= $square_arr[$v] . ' OR';
  2496. }
  2497. $customersList = Customer::where($condition)->where(function ($query) use ($squ_whereOr) {
  2498. $query->whereRaw(trim($squ_whereOr, ' OR'));
  2499. })->order('addtime desc')->column('id');
  2500. unset($condition);
  2501. $condition[] = ['id', 'in', $customersList];
  2502. }
  2503. //未跟进天数
  2504. if (!empty($param['no_visit_day'])) {
  2505. $no_visit_day = explode(',', $param['no_visit_day']);
  2506. $all_no_visit = [
  2507. 1 => [3, 7],
  2508. 2 => [8, 14],
  2509. 3 => [15, 30],
  2510. 4 => [31, 10000]
  2511. ];
  2512. $customersList = Customer::where($condition)->order('addtime desc')->column('id,addtime');
  2513. $wg_where[] = ['customer_id', 'in', array_column($customersList, 'id')];
  2514. $latest_visit_log = CustomerVisitLog::where($wg_where)->group('customer_id')->column('max(addtime) as addtime', 'customer_id'); //->select()->toArray();
  2515. $no_visit_ids = [];
  2516. foreach ($customersList as $k => $v) {
  2517. if (empty($latest_visit_log[$v['id']])) {
  2518. $no_visit_long = ceil((time() - strtotime($v['addtime'])) / 86400);
  2519. } else {
  2520. $no_visit_long = ceil((time() - strtotime($latest_visit_log[$v['id']])) / 86400);
  2521. }
  2522. foreach ($no_visit_day as $m => $p) {
  2523. if (empty($all_no_visit[$p])) continue;
  2524. if ($no_visit_long >= $all_no_visit[$p][0] && $no_visit_long <= $all_no_visit[$p][1]) $no_visit_ids[] = $v['id'];
  2525. }
  2526. }
  2527. unset($condition);
  2528. $condition[] = ['id', 'in', $no_visit_ids];
  2529. }
  2530. if (!empty($param['state'])) {
  2531. $customerIdList = Customer::where($condition)->column('id');
  2532. $stateall = explode(',', $param['state']);
  2533. $allsta = [];
  2534. foreach ($stateall as $k => $v) {
  2535. if ($v == '已签单') $v = '已交定';
  2536. if ($v == '已转单') $v = '已签单';
  2537. if (in_array($v, ['待确认', '有效'])) continue;
  2538. $onestate = CustomerVisitLog::changeState($v, 'chaos');
  2539. $allsta = array_merge($onestate, $allsta);
  2540. }
  2541. $visit_where[] = ['customer_id', 'in', $customerIdList];
  2542. $visit_where[] = ['state', 'in', $allsta];
  2543. // $visit_where[] = ['addtime', '>', date('Y-m-d H:i:s', $startTime)];
  2544. // $visit_where[] = ['addtime', '<', date('Y-m-d H:i:s', $endTime)];
  2545. $logCustomersIdList = CustomerVisitLog::where($visit_where)->group('customer_id')->column('customer_id');
  2546. $c_ids = $logCustomersIdList;
  2547. foreach ($stateall as $k => $v) {
  2548. if ($v == '待确认') {
  2549. $confirmedids = Customer::where($condition)->where([['state', 'in', Customer::changeState('待确认', 'chaos')], ['crm_res_id', 'NULL', null]])->column('id');
  2550. }
  2551. if ($v == '有效') {
  2552. $validids = Customer::where($condition)->where('state', 'not in', Customer::changeState('待确认', 'chaos'))->column('id');
  2553. }
  2554. }
  2555. unset($condition);
  2556. if (!empty($confirmedids)) $c_ids = array_merge($confirmedids, $c_ids);
  2557. if (!empty($validids)) $c_ids = array_merge($validids, $c_ids);
  2558. unset($condition);
  2559. $condition[] = ['id', 'in', $c_ids];
  2560. }
  2561. $vis_where = "if(CustomerVisitLog.state in (7,'已到店'), 1, 0) desc,confirm_date asc";
  2562. $tmp_vis_where = 'confirm_date asc';
  2563. if ($param['times'] > 4) {
  2564. if ($param['times'] == 6) {
  2565. $vis_where = "if(CustomerVisitLog.state in (7,'已到店'), 1, 0) desc,confirm_date desc";
  2566. $tmp_vis_where = 'confirm_date desc';
  2567. }
  2568. $page = ($page - 1) * 17;
  2569. $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();
  2570. $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;
  2571. $customer_id = Db::query($sql);
  2572. $data = [];
  2573. foreach ($customer_id as $k => $v) {
  2574. $one = Customer::with(['employee', 'org', 'designer', 'source', 'visitLog' => function ($query) {
  2575. $query->field('id,customer_id,addtime,state,confirm_date,money');
  2576. }])->where($condition)->where(['id' => $v['customer_id']])->find();
  2577. $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();
  2578. }
  2579. } else {
  2580. $data = Customer::with(['employee', 'org', 'designer', 'source', 'visitLog' => function ($query) {
  2581. $query->field('id,customer_id,addtime,state,confirm_date,money');
  2582. }])
  2583. ->where($condition)
  2584. ->page($page, $limit)->order($order)->select();
  2585. $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();
  2586. }
  2587. $setting = Setting::where([['name', '=', 'pubpool'], ['root_id', '=', $root_id]])->find();
  2588. if (!empty($setting)) {
  2589. $setting_content = json_decode($setting['content'], true);
  2590. } else {
  2591. $setting_content = [];
  2592. }
  2593. // 指派类型
  2594. $assign_type_default = ['yixiang' => '意向客戶指派', 'liangfang' => '量房客戶指派', 'daodian' => '到店客戶指派'];
  2595. foreach ($data as &$item) {
  2596. $item['un_protected'] = false;
  2597. if (!empty($item['protected_to']) && time() > strtotime($item['protected_to'])) {
  2598. $state_n = Customer::changeState($item['state'], 'n');
  2599. if (isset($setting_content[$state_n]['state']) && $setting_content[$state_n]['state'] == 1) {
  2600. $item['un_protected'] = true;
  2601. }
  2602. }
  2603. $item['customer_type'] = $item['crm_res_id'] ? '资源库' : ($item['remark'] == '活动报名建档' ? '活动' : ($item['agents_id'] ? '装修推荐官' : '自建'));
  2604. $item['square'] = floatval($item['square']);
  2605. $item['phone'] = substr_replace($item['phone'], '******', 3, 6);
  2606. $item['phone1'] = $item['phone1'] ? substr_replace($item['phone1'], '******', 3, 6) : '';
  2607. $item['phone2'] = $item['phone2'] ? substr_replace($item['phone2'], '******', 3, 6) : '';
  2608. $item['jiaofang'] = !empty($item['house_delivery_time']) ? (str_replace('/', '-', $item['house_delivery_time']) <= date('Y-m-d', time()) ? '现房' : $item['house_delivery_time']) : '';
  2609. $item['time_status'] = $item['last_contact_date'] ? $this->uc_time_ago($item['last_contact_date']) : '';
  2610. $item['add_wechat'] = '否';
  2611. $item['package_name'] = !empty($item['package_id']) ? CustomerPackage::where('id', $item['package_id'])->value('name') : '';
  2612. $item['employee_name'] = !empty($item['employee']) ? $item['employee']['name'] : '';
  2613. $item['designer_name'] = !empty($item['designer']) ? $item['designer']['name'] : '';
  2614. $item['source_name'] = !empty($item['source']) ? $item['source']['source'] : '';
  2615. if (!empty($item['ext'])) {
  2616. $extdata = json_decode($item['ext'], true);
  2617. foreach ($extdata as $key => $val) {
  2618. if (isset($val['keyname']) && $val['keyname'] == 'wechat' && !empty($val['value'])) {
  2619. $add_wechat = CustomerPortraitFieldSelect::where('id', $val['value'])->value('name');
  2620. if ($add_wechat == '有') {
  2621. $add_wechat = '是';
  2622. }
  2623. $item['add_wechat'] = $add_wechat;
  2624. }
  2625. }
  2626. }
  2627. $now_status = $item['state'];
  2628. if ($now_status == '已签单') $now_status = '已转单';
  2629. if ($now_status == '已交定') $now_status = '已签单';
  2630. $assignIdList = !empty($item['assigned_personnel']) ? explode(',', $item['assigned_personnel']) : [];
  2631. //$assignIdList[] = $item['employee_id']; // 所属销售
  2632. if (!empty($item['designer_id'])) $assignIdList[] = $item['designer_id']; // 指派的设计师
  2633. $assign_data = Employee::where([['id', 'in', $assignIdList], ['root_id', '=', $root_id]])->column('name,org_id');
  2634. $item['assign_list'] = !empty($assign_data) ? implode(',', array_column($assign_data, 'name')) : '';
  2635. $assign_org = Org::where([['id', 'in', array_column($assign_data, 'org_id')]])->group('id')->column('name');
  2636. $item['assign_org'] = !empty($assign_org) ? implode(',', $assign_org) : '';
  2637. $item['assign_type_name'] = $item['state'] = $item['daodian_time'] = $item['liangfang_time'] = $item['jiaoding_time'] = $item['qiandan_time'] = '';
  2638. if (!empty($item['assign_type'])) {
  2639. if (isset($assign_type_default[$item['assign_type']])) {
  2640. $item['assign_type_name'] = $assign_type_default[$item['assign_type']];
  2641. }
  2642. }
  2643. if (!empty($item['visitLog'])) {
  2644. //取最新的一条为客户的跟进时间
  2645. // $item['last_contact_date'] = $item['visitLog'][count($item['visitLog'])-1]['addtime'];
  2646. //处理合并多个状态
  2647. $stain = [$now_status];
  2648. foreach ($item['visitLog'] as $k => $v) {
  2649. if (!in_array($v['state'], ['已交定', '已签单', '已到店', '已量房', '已到场', '未到访', '回访'])) continue;
  2650. if ($v['state'] == '回访') $v['state'] = '待确认';
  2651. if ($v['state'] == '已签单') $v['state'] = '已转单';
  2652. if ($v['state'] == '已交定') $v['state'] = '已签单';
  2653. if (!in_array($v['state'], $stain)) {
  2654. $stain[] = $v['state'];
  2655. }
  2656. }
  2657. if (count($stain) > 1 && in_array('待确认', $stain)) $stain = array_diff($stain, ['待确认']);
  2658. $visit_state = ['已到店', '已量房', '已到场', '已签单', '已转单', '已卖卡'];
  2659. $no_visit_state = ['未到访', '待确认'];
  2660. $jiaoji = array_intersect($visit_state, $stain);
  2661. $no_jiaoji = array_intersect($no_visit_state, $stain);
  2662. if (count($no_jiaoji) == 2) { // 有未到访、待确认两个状态
  2663. $stain = ['未到访'];
  2664. }
  2665. if (count($stain) > 1 && count($jiaoji) > 0) {
  2666. $stain = array_diff($stain, $no_visit_state);
  2667. }
  2668. $item['state'] = implode(',', $stain);
  2669. if ($now_status == '待确认') $item['state'] = '待确认';
  2670. //处理时间
  2671. $state = [];
  2672. foreach ($item['visitLog'] as $k => $v) {
  2673. $state[$v['state']][] = $v;
  2674. }
  2675. foreach ($state as $k => $v) {
  2676. if ($k == '已到店') $item['daodian_time'] = date('Y-m-d', strtotime($v[0]['confirm_date']));
  2677. if ($k == '已量房') $item['liangfang_time'] = date('Y-m-d', strtotime($v[0]['confirm_date']));
  2678. if ($k == '已交定') {
  2679. $item['jiaoding_time'] = date('Y-m-d', strtotime($v[0]['confirm_date']));
  2680. $item['jiaoding_money'] = $v[0]['money'];
  2681. }
  2682. if ($k == '已签单') {
  2683. $item['qiandan_time'] = date('Y-m-d', strtotime($v[0]['confirm_date']));
  2684. $item['qiandan_money'] = $v[0]['money'];
  2685. //$item['sign'] = $v[0];
  2686. }
  2687. }
  2688. }
  2689. $item['now_state'] = $now_status;
  2690. $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;
  2691. }
  2692. $count = Customer::where($condition)->count();
  2693. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  2694. }
  2695. //已交定列表
  2696. public function jiaoding_list()
  2697. {
  2698. $request = request();
  2699. $root_id = $request->empcrm->root_id;
  2700. $empid = $request->empcrm->id;
  2701. $is_manager = $request->empcrm->is_manager;
  2702. $org_employee = [$empid];
  2703. if ($is_manager == 1) {
  2704. $team_orgs = orgSubIds($request->empcrm->org_id);
  2705. $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
  2706. }
  2707. if (!$request->isAjax()) {
  2708. $this->screen();
  2709. return View::fetch();
  2710. }
  2711. $param = $request->only(['page' => 1, 'limit' => 10, 'employee_id', 'level', 'source_id', 'designer_id', 'keyname', 'date', 'times' => 0, 'customer_type']);
  2712. if (!empty($param['employee_id']) && $is_manager == 1 && in_array($param['employee_id'], $org_employee)) $org_employee = [$param['employee_id']];
  2713. $condition[] = ['employee_id', 'in', $org_employee];
  2714. $condition[] = ['state', 'not in', Customer::changeState('无效', 'chaos')];
  2715. if (!empty($param['level'])) $condition[] = ['level', '=', $param['level']];
  2716. if (!empty($param['source_id'])) $condition[] = ['source_id', '=', $param['source_id']];
  2717. if (!empty($param['designer_id'])) $condition[] = ['designer_id', '=', $param['designer_id']];
  2718. if (!empty($param['keyname'])) $condition[] = ['name|community_name', 'like', '%' . $param['keyname'] . '%'];
  2719. $order = isset($param['order']) ? $param['order'] : 'id desc';
  2720. if ($param['times'] == 1) $order = 'tmp_sign_time asc';
  2721. if ($param['times'] == 2) $order = 'tmp_sign_time desc';
  2722. $page = intval($param['page']);
  2723. $limit = intval($param['limit']);
  2724. $vslogid = CustomerVisitLog::where([['customer_employee_id', 'in', $org_employee], ['state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')]])->column('customer_id');
  2725. if (!empty($param['date'])) {
  2726. $time = explode(' - ', $param['date']);
  2727. $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');
  2728. }
  2729. $condition[] = ['id', 'in', $vslogid];
  2730. //客户种类 资源库,活动,经纪人,自建
  2731. if (!empty($param['customer_type'])) {
  2732. $gather = [
  2733. 1 => [['crm_res_id', 'not null', null]], //'资源库'
  2734. 2 => [['crm_res_id', 'null', null], ['remark', 'not like', '%活动报名建档%'], ['agents_id', 'null', null]], //'自建'
  2735. 3 => [['remark', 'like', '%活动报名建档%']], //'活动'
  2736. 4 => [['agents_id', '>', 0]] //'经纪人'
  2737. ];
  2738. $arr = explode(',', $param['customer_type']);
  2739. $whereOr = [];
  2740. foreach ($arr as $val) {
  2741. if (isset($gather[$val])) {
  2742. //$whereOr[] = array_merge($condition,$gather[$val]);
  2743. $whereOr[] = $gather[$val];
  2744. }
  2745. }
  2746. if (!empty($whereOr)) {
  2747. $new_cids = Customer::where($condition)->where(function ($query) use ($whereOr) {
  2748. $query->whereOr($whereOr);
  2749. })->column('id');
  2750. unset($condition);
  2751. $condition[] = ['id', 'in', $new_cids];
  2752. } else {
  2753. //无效条件
  2754. return json(['code' => 0, 'data' => [], 'count' => 0]);
  2755. }
  2756. }
  2757. $data = $this->selCustomerOrder($condition, $page, $limit, $order, $root_id);
  2758. $no_visit_state = ['未到访', '待确认'];
  2759. $visit_state = ['已到店', '已量房', '已到场', '已签单', '已转单', '已卖卡'];
  2760. foreach ($data as $key => $val) {
  2761. $data[$key]['customer_type'] = $val['crm_res_id'] ? '资源库' : ($val['remark'] == '活动报名建档' ? '活动' : ($val['agents_id'] ? '装修推荐官' : '自建'));
  2762. $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);
  2763. $data[$key]['xinjushang'] = $request->empcrm['xinjushang'];
  2764. // 有'已到店', '已量房', '已到场'状态的话不显示未到访
  2765. $state = explode(',', $val['state']);
  2766. $jiaoji = array_intersect($visit_state, $state);
  2767. $no_jiaoji = array_intersect($no_visit_state, $state);
  2768. if (count($no_jiaoji) == 2) { // 有未到访、待确认两个状态
  2769. $state = ['未到访'];
  2770. }
  2771. if (count($state) > 1 && count($jiaoji) > 0) {
  2772. $state = array_diff($state, $no_visit_state);
  2773. }
  2774. $data[$key]['state'] = implode(',', $state);
  2775. }
  2776. $count = Customer::where($condition)->count();
  2777. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  2778. }
  2779. //搜索
  2780. public function search_list()
  2781. {
  2782. $request = request();
  2783. $root_id = $request->empcrm->root_id;
  2784. $empid = $request->empcrm->id;
  2785. $is_manager = $request->empcrm->is_manager;
  2786. $org_employee = [$empid];
  2787. if (!$request->isAjax()) {
  2788. //$this->screen();
  2789. return View::fetch();
  2790. }
  2791. $param = $request->only(['page' => 1, 'limit' => 10, 'keyname']);
  2792. if ($is_manager == 1) {
  2793. $team_orgs = orgSubIds($request->empcrm->org_id);
  2794. $condition[] = ['org_id', 'in', $team_orgs];
  2795. } else {
  2796. $condition[] = ['employee_id', '=', $empid];
  2797. }
  2798. $order = isset($param['order']) ? $param['order'] : 'id desc';
  2799. $list = Customer::where($condition)->field('id,name,community_name,phone,employee_id,designer_id')->select()->toArray();
  2800. $customersIdList = [];
  2801. if (!empty($param['keyname'])) {
  2802. foreach ($list as $key => $val) {
  2803. $str = $val['name'] . $val['community_name'] . $val['phone'];
  2804. if (strpos($str, trim($param['keyname'])) !== false) $customersIdList[] = $val['id'];
  2805. }
  2806. }
  2807. $new = array_slice($customersIdList, ($param['page'] - 1) * $param['limit'], $param['limit']);
  2808. $cond[] = ['id', 'in', $new];
  2809. $data = $this->search_selone($cond, $order);
  2810. $count = count($customersIdList);
  2811. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  2812. }
  2813. //搜索的单独查询
  2814. public function search_selone($condition, $order)
  2815. {
  2816. $request = request();
  2817. $root_id = $request->empcrm->root_id;
  2818. $empid = $request->empcrm->id;
  2819. $data = Customer::with(['employee', 'org', 'designer', 'source'])
  2820. ->withCount(['visitLog' => function ($query) {
  2821. $query->where([[CustomerVisitLog::changeState(['state', '=', '未到访'])]]);
  2822. }])
  2823. ->where($condition)
  2824. ->order($order)->select();
  2825. $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();
  2826. $setting = Setting::where([['name', '=', 'pubpool'], ['root_id', '=', $root_id]])->find();
  2827. if (!empty($setting)) {
  2828. $setting_content = json_decode($setting['content'], true);
  2829. } else {
  2830. $setting_content = [];
  2831. }
  2832. foreach ($data as &$item) {
  2833. $item['un_protected'] = false;
  2834. if (!empty($item['protected_to']) && time() > strtotime($item['protected_to'])) {
  2835. $state_n = Customer::changeState($item['state'], 'n');
  2836. if (isset($setting_content[$state_n]['state']) && $setting_content[$state_n]['state'] == 1) {
  2837. $item['un_protected'] = true;
  2838. }
  2839. }
  2840. $item['square'] = floatval($item['square']);
  2841. $item['phone'] = substr_replace($item['phone'], '******', 3, 6);
  2842. $item['phone1'] = $item['phone1'] ? substr_replace($item['phone1'], '******', 3, 6) : '';
  2843. $item['phone2'] = $item['phone2'] ? substr_replace($item['phone2'], '******', 3, 6) : '';
  2844. $item['jiaofang'] = !empty($item['house_delivery_time']) ? ($item['house_delivery_time'] <= date('Y/m/d', time()) ? '现房' : $item['house_delivery_time']) : '';
  2845. $vlotime = CustomerVisitLog::where('customer_id', $item['id'])->order('addtime desc')->value('addtime');
  2846. $fisttime = CustomerVisitLog::where([['customer_id', '=', $item['id']], ['customer_employee_id', '=', $item['employee_id']], [CustomerVisitLog::changeState(['state', '=', '确认到店'])]])->order('addtime asc')->value('addtime');
  2847. $jiaoding_time = CustomerVisitLog::where([['customer_id', '=', $item['id']], ['customer_employee_id', '=', $item['employee_id']], [CustomerVisitLog::changeState(['state', '=', '已交定'])]])->order('addtime asc')->value('addtime');
  2848. $signorder_time = CustomerVisitLog::where([['customer_id', '=', $item['id']], ['customer_employee_id', '=', $item['employee_id']], [CustomerVisitLog::changeState(['state', '=', '已签单'])]])->order('addtime asc')->value('addtime');
  2849. $item['jiaoding_time'] = $jiaoding_time ? $jiaoding_time : '';
  2850. $item['signorder_time'] = $signorder_time ? $signorder_time : '';
  2851. $item['fisttime'] = $fisttime ? $fisttime : '';
  2852. $item['time_status'] = $vlotime ? $this->uc_time_ago(date('Y-m-d', strtotime($vlotime))) : '';
  2853. $item['add_wechat'] = '否';
  2854. $item['sign_time'] = null;
  2855. if (!empty($item['ext'])) {
  2856. $extdata = json_decode($item['ext'], true);
  2857. foreach ($extdata as $key => $val) {
  2858. if (isset($val['keyname']) && $val['keyname'] == 'wechat' && !empty($val['value'])) {
  2859. $add_wechat = CustomerPortraitFieldSelect::where('id', $val['value'])->value('name');
  2860. if ($add_wechat == '有') {
  2861. $add_wechat = '是';
  2862. }
  2863. $item['add_wechat'] = $add_wechat;
  2864. }
  2865. if (isset($val['keyname']) && $val['keyname'] == 'sign_time' && !empty($val['value'])) {
  2866. $item['sign_time'] = $val['value'];
  2867. }
  2868. }
  2869. }
  2870. }
  2871. return $data;
  2872. }
  2873. //处理查询客户之前的见面记录
  2874. public function selBeforelog($metids, $firstday)
  2875. {
  2876. $statday = '2021-12-01 00:00:00';
  2877. $state1 = CustomerVisitLog::changeState('确认到店', 'chaos');
  2878. $state2 = CustomerVisitLog::changeState('已到场', 'chaos');
  2879. $state3 = CustomerVisitLog::changeState('已量房', 'chaos');
  2880. $q_where[] = ['customer_id', 'in', $metids];
  2881. $q_where[] = ['state', 'in', array_merge($state1, $state2, $state3)];
  2882. $q_where[] = ['addtime', 'between', [$statday, $firstday]];
  2883. $qianvslog = CustomerVisitLog::where($q_where)->group('customer_id')->column('customer_id');
  2884. $yesids = array_diff($metids, $qianvslog);
  2885. return $yesids;
  2886. }
  2887. //日报
  2888. public function daily()
  2889. {
  2890. $request = request();
  2891. $root_id = $request->empcrm->root_id;
  2892. $empid = $request->empcrm->id;
  2893. $is_manager = $request->empcrm->is_manager;
  2894. $org_employee = [$empid];
  2895. $team_orgs = [$request->empcrm->org_id];
  2896. if ($is_manager == 1) {
  2897. $team_orgs = orgSubIds($request->empcrm->org_id);
  2898. $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
  2899. }
  2900. $param = $request->only(['date', 'employee_id', 'org_id']);
  2901. if (!$request->isAjax()) {
  2902. $data['company_name'] = Company::where('root_id', $root_id)->value('company_name');
  2903. $data['empname'] = $request->empcrm->name;
  2904. $data['date'] = !empty($param['date']) ? $param['date'] : date('Y-m');
  2905. $data['org_id'] = !empty($param['org_id']) ? $param['org_id'] : '';
  2906. $data['org_name'] = !empty($param['org_id']) ? Org::where([['id', '=', $param['org_id']], ['root_id', '=', $root_id]])->value('name') : '';
  2907. View::assign('data', $data);
  2908. View::assign('is_manager', $is_manager);
  2909. //部门所有员工
  2910. $employee_list = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->field('id,name')->select()->toArray();
  2911. View::assign('employee_list', $employee_list);
  2912. //下级所有部门
  2913. $org_list = Org::where([['id', 'in', $team_orgs], ['id', '<>', $request->empcrm->org_id]])->select()->toArray();
  2914. View::assign('org_list', $org_list);
  2915. return View::fetch();
  2916. }
  2917. $today = date('Y-m', time()) . '-01 00:00:00';
  2918. $todaynum = date('d', time());
  2919. $firstday = $param['date'] . '-' . '01 00:00:00';
  2920. $lastday = date('Y-m-d 23:59:59', strtotime("$firstday +1 month -1 day"));
  2921. $lastnum = date('d', strtotime("$firstday +1 month -1 day"));
  2922. $nowempid = $empid;
  2923. $noworgid = $request->empcrm->org_id;
  2924. $sub_org = orgSubIds($noworgid);
  2925. if ($is_manager == 1) $nowempid = $noworgid = '';
  2926. if (!empty($param['employee_id']) && $is_manager == 1 && in_array($param['employee_id'], $org_employee)) $nowempid = $param['employee_id'];
  2927. $where[] = ['confirm_date', 'between', [$firstday, $lastday]];
  2928. if (!empty($param['employee_id']) && $is_manager == 1 && in_array($param['employee_id'], $org_employee)) $org_employee = [$param['employee_id']];
  2929. if (!empty($param['org_id']) && $is_manager == 1) {
  2930. $noworgid = $param['org_id'];
  2931. $sub_org = orgSubIds($noworgid);
  2932. $org_employee = Employee::where([['org_id', 'in', $sub_org], ['root_id', '=', $root_id], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
  2933. }
  2934. $where[] = ['customer_employee_id', 'in', $org_employee];
  2935. //$list = Customer::where($where)->field('id,name,addtime,state,valid_time')->select()->toArray();
  2936. //电话记录
  2937. $hu_where = [];
  2938. $hu_where[] = ['employee_id', 'in', $org_employee];
  2939. $hu_where[] = ['root_id', '=', $root_id];
  2940. $hu_where[] = ['addtime', '>=', $firstday];
  2941. $hu_where[] = ['addtime', '<=', $lastday];
  2942. $phonelist = OutCallLog::where($hu_where)->column('addtime,status,billsec');
  2943. // 手机打电话次数获取
  2944. $mbphlist = OutCallMbLog::where($hu_where)->column('addtime');
  2945. //转发次数
  2946. $shares_list = ShareLog::where([['employee_id', 'in', $org_employee], ['share_time', '>=', $firstday], ['share_time', '<=', $lastday]])->column('id,share_time');
  2947. //新增加客户数
  2948. $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();
  2949. //待确认的客户
  2950. $treat_cuslist = Customer::where([['sign_time', '>=', $firstday], ['sign_time', '<=', $lastday], ['employee_id', 'in', $org_employee], ['state', 'in', Customer::changeState('待确认', 'chaos')]])->column('sign_time', 'id');
  2951. //客户派单数
  2952. $assign_list = Customer::where([['assign_time', '>=', $firstday], ['assign_time', '<=', $lastday], ['employee_id', 'in', $org_employee]])->column('id,assign_time,assign_type');
  2953. //置为无效的客户
  2954. $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');
  2955. $state1 = Customer::changeState('待确认', 'chaos');
  2956. $state2 = Customer::changeState('无效', 'chaos');
  2957. $state3 = CustomerVisitLog::changeState('已交定', 'chaos');
  2958. $state4 = CustomerVisitLog::changeState('已签单', 'chaos');
  2959. $state5 = CustomerVisitLog::changeState('确认到店', 'chaos');
  2960. $state6 = CustomerVisitLog::changeState('无效', 'chaos');
  2961. $state7 = CustomerVisitLog::changeState('未到访', 'chaos');
  2962. $state8 = CustomerVisitLog::changeState('已到场', 'chaos');
  2963. $state9 = CustomerVisitLog::changeState('已量房', 'chaos');
  2964. //记录
  2965. $visitlog_list = CustomerVisitLog::where($where)
  2966. ->where([['state', 'in', array_merge($state3, $state4, $state5, $state6, $state7, $state8, $state9)]])
  2967. //->group('state,customer_id')
  2968. ->order('addtime asc')
  2969. ->column('customer_id,state,money,confirm_date');
  2970. $metids = [];
  2971. foreach ($visitlog_list as $key => $val) {
  2972. if (in_array($val['state'], array_merge($state5, $state8, $state9))) $metids[] = $val['customer_id'];
  2973. }
  2974. $yesids = !empty($metids) ? $this->selBeforelog(array_unique($metids), $firstday) : [];
  2975. $xin = [];
  2976. $weekarray = array("日", "一", "二", "三", "四", "五", "六"); //先定义一个数组
  2977. if ($today == $firstday) {
  2978. for ($i = 0; $i < $todaynum; $i++) {
  2979. $day = date('Y-m-d', strtotime("$firstday +$i days"));
  2980. $a['day'] = $day;
  2981. $a['week'] = $weekarray[date("w", strtotime("$firstday +$i days"))];
  2982. $a['logdata'] = [];
  2983. foreach ($visitlog_list as $key => $val) {
  2984. if ($val['confirm_date'] >= $day . ' 00:00:00' && $val['confirm_date'] <= $day . ' 23:59:59') {
  2985. $a['logdata'][] = $val;
  2986. }
  2987. }
  2988. $xin[] = $a;
  2989. }
  2990. } elseif ($today > $firstday) {
  2991. for ($i = 0; $i < $lastnum; $i++) {
  2992. $day = date('Y-m-d', strtotime("$firstday +$i days"));
  2993. $a['day'] = $day;
  2994. $a['week'] = $weekarray[date("w", strtotime("$firstday +$i days"))];
  2995. $a['logdata'] = [];
  2996. foreach ($visitlog_list as $key => $val) {
  2997. if ($val['confirm_date'] >= $day . ' 00:00:00' && $val['confirm_date'] <= $day . ' 23:59:59') {
  2998. $a['logdata'][] = $val;
  2999. }
  3000. }
  3001. $xin[] = $a;
  3002. }
  3003. }
  3004. unset($visitlog_list);
  3005. $meetids = [];
  3006. $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];
  3007. foreach ($xin as $key => $val) {
  3008. //$xin[$key]['clue_cont'] = count($new_cuslist);
  3009. $xin[$key]['nowempid'] = $nowempid;
  3010. $xin[$key]['noworgid'] = $noworgid;
  3011. $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;
  3012. $depositids = $invalids = $daystate = $cusids = $validids = [];
  3013. // 外呼系统统计
  3014. $start = $val['day'] . ' 00:00:00';
  3015. $end = $val['day'] . ' 23:59:59';
  3016. foreach ($phonelist as $k => $v) {
  3017. if ($v['addtime'] >= $start && $v['addtime'] <= $end) {
  3018. if ($v['status'] == 1) $on_phone_count++;
  3019. $phone_time += $v['billsec'];
  3020. $phone_count++;
  3021. }
  3022. }
  3023. foreach ($mbphlist as $k => $v) {
  3024. if ($v >= $start && $v <= $end) {
  3025. $phone_count++;
  3026. }
  3027. }
  3028. //转发次数
  3029. foreach ($shares_list as $k => $v) {
  3030. if ($v['share_time'] >= $start && $v['share_time'] <= $end) {
  3031. $shares_count++;
  3032. }
  3033. }
  3034. //新增客户数
  3035. foreach ($new_cuslist as $k => $v) {
  3036. if ($v['sign_time'] >= $start && $v['sign_time'] <= $end) {
  3037. $new_cus++;
  3038. if ($v['died'] != 2 && !in_array($v['state'], array_merge($state1, $state2), true)) {
  3039. $valid++;
  3040. $validids[] = $v['id'];
  3041. }
  3042. $cusids[] = $v['id'];
  3043. }
  3044. }
  3045. //待确认客户
  3046. foreach ($treat_cuslist as $k => $v) {
  3047. if ($v >= $start && $v <= $end) {
  3048. $new_cus++;
  3049. $cusids[] = $k;
  3050. }
  3051. }
  3052. //无效客户数增加
  3053. foreach ($invalidcus as $v) {
  3054. if ($v['cus_addtime'] >= $start && $v['cus_addtime'] <= $end && !in_array($v['customer_id'], $cusids)) {
  3055. $new_cus++;
  3056. $cusids[] = $v['customer_id'];
  3057. }
  3058. if ($v['cus_addtime'] >= $start && $v['cus_addtime'] <= $end && !in_array($v['customer_id'], $validids)) {
  3059. if ($v['status'] == 1) {
  3060. $valid++;
  3061. $validids[] = $v['customer_id'];
  3062. }
  3063. }
  3064. }
  3065. unset($cusids, $validids);
  3066. //派单数
  3067. foreach ($assign_list as $k => $v) {
  3068. if ($v['assign_time'] >= $start && $v['assign_time'] <= $end) {
  3069. if ($v['assign_type'] == 'yixiang') $yixiang++;
  3070. if ($v['assign_type'] == 'liangfang') $liangfang++;
  3071. if ($v['assign_type'] == 'daodian') $daodian++;
  3072. }
  3073. }
  3074. foreach ($val['logdata'] as $ky => $v) {
  3075. if (in_array($v['state'], $state3, true) && !in_array($v['customer_id'], $depositids)) {
  3076. $deposit++;
  3077. $depositids[] = $v['customer_id'];
  3078. }
  3079. if (in_array($v['state'], $state4, true)) {
  3080. $sign++;
  3081. $basic_money += $v['money'];
  3082. }
  3083. if (in_array($v['state'], array_merge($state5, $state8, $state9)) && !in_array($v['customer_id'], $meetids) && in_array($v['customer_id'], $yesids)) {
  3084. $meet++;
  3085. $meetids[] = $v['customer_id'];
  3086. }
  3087. if (in_array($v['state'], $state6)) {
  3088. $invalid_yes++;
  3089. $invalids[] = $v['customer_id'];
  3090. }
  3091. }
  3092. foreach ($invalids as $k => $v) {
  3093. if (in_array($v, $meetids)) $invalid_meet++;
  3094. }
  3095. //$xin[$key]['invalid_yesids'] = $invalid_yesids;
  3096. $xin[$key]['valid_cont'] = $valid;
  3097. $xin[$key]['valid_percen'] = $valid && !empty($new_cus) ? round($valid / $new_cus * 100, 2) : 0;
  3098. $xin[$key]['meet_cont'] = $meet;
  3099. $xin[$key]['meet_percen'] = !empty($meet) && !empty($new_cus) ? round($meet / $new_cus * 100, 2) : 0;
  3100. $xin[$key]['deposit_cont'] = $deposit;
  3101. $xin[$key]['sign_cont'] = $sign;
  3102. $xin[$key]['basic_money'] = $basic_money;
  3103. $xin[$key]['other_money'] = $other_money;
  3104. $xin[$key]['all_money'] = $basic_money + $other_money;
  3105. $xin[$key]['invalid_no'] = $invalid_no;
  3106. $xin[$key]['invalid_yes'] = $invalid_yes;
  3107. $xin[$key]['invalid_meet'] = $invalid_meet;
  3108. $xin[$key]['log_customer_count'] = count($val['logdata']);
  3109. $xin[$key]['phone_count'] = $phone_count;
  3110. $xin[$key]['on_phone_count'] = $on_phone_count;
  3111. $xin[$key]['phone_time'] = $phone_time;
  3112. $xin[$key]['shares_count'] = $shares_count;
  3113. $xin[$key]['new_cus'] = $new_cus;
  3114. $xin[$key]['yixiang'] = $yixiang;
  3115. $xin[$key]['liangfang'] = $liangfang;
  3116. $xin[$key]['daodian'] = $daodian;
  3117. //总数
  3118. $t['clue_cont'] += $new_cus;
  3119. $t['valid_cont'] += $valid;
  3120. $t['meet_cont'] += $meet;
  3121. $t['deposit_cont'] += $deposit;
  3122. $t['sign_cont'] += $sign;
  3123. $t['basic_money'] += $basic_money;
  3124. $t['other_money'] += $other_money;
  3125. $t['all_money'] += $other_money + $basic_money;
  3126. $t['invalid_no'] += $invalid_no;
  3127. $t['invalid_yes'] += $invalid_yes;
  3128. $t['invalid_meet'] += $invalid_meet;
  3129. $t['log_customer_count'] += count($val['logdata']);
  3130. $t['phone_count'] += $phone_count;
  3131. $t['on_phone_count'] += $on_phone_count;
  3132. $t['phone_time'] += $phone_time;
  3133. $t['shares_count'] += $shares_count;
  3134. $t['new_cus'] += $new_cus;
  3135. $t['yixiang'] += $yixiang;
  3136. $t['liangfang'] += $liangfang;
  3137. $t['daodian'] += $daodian;
  3138. }
  3139. if ($t['valid_cont'] !== 0) {
  3140. $t['valid_percen'] = round($t['valid_cont'] / $t['new_cus'] * 100, 2);
  3141. $t['meet_percen'] = round($t['meet_cont'] / $t['new_cus'] * 100, 2);
  3142. }
  3143. $xin = array_reverse($xin);
  3144. array_push($xin, $t);
  3145. return json(['code' => 0, 'data' => $xin, 'count' => 0, 'msg' => '获取成功']);
  3146. }
  3147. //分派
  3148. public function assignment_list()
  3149. {
  3150. $request = request();
  3151. $root_id = $request->empcrm->root_id;
  3152. $empid = $request->empcrm->id;
  3153. $is_manager = $request->empcrm->is_manager;
  3154. $org_employee = [$empid];
  3155. if ($is_manager == 1) {
  3156. $team_orgs = orgSubIds($request->empcrm->org_id);
  3157. $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
  3158. }
  3159. if (!$request->isAjax()) {
  3160. $this->screen();
  3161. $newteam_orgs = orgSubIds($request->empcrm->org_id);
  3162. $assignemp_list = Employee::where([['org_id', 'in', $newteam_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->field('id,name')->select()->toArray();
  3163. View::assign('assignemp_list', $assignemp_list);
  3164. View::assign('is_manager', $is_manager);
  3165. return View::fetch();
  3166. }
  3167. $param = $request->only(['page' => 1, 'limit' => 10, 'employee_id', 'level', 'source_id', 'designer_id', 'keyname', 'phone']);
  3168. if (!empty($param['employee_id']) && $is_manager == 1) $org_employee = [$param['employee_id']];
  3169. $condition[] = ['employee_id', 'in', $org_employee];
  3170. $condition[] = ['transfer_info', '<>', ''];
  3171. $order = isset($param['order']) ? $param['order'] : 'updatetime desc';
  3172. $page = intval($param['page']);
  3173. $limit = intval($param['limit']);
  3174. if (!empty($param['level'])) $condition[] = ['level', '=', $param['level']];
  3175. if (!empty($param['source_id'])) $condition[] = ['source_id', '=', $param['source_id']];
  3176. if (!empty($param['designer_id'])) $condition[] = ['designer_id', '=', $param['designer_id']];
  3177. if (!empty($param['keyname'])) $condition[] = ['name|community_name', 'like', '%' . $param['keyname'] . '%'];
  3178. if (!empty($param['phone'])) {
  3179. $list = Customer::where($condition)->column('id', 'phone');
  3180. $keyCustomersId = [];
  3181. foreach ($list as $k => $v) {
  3182. if (cypherphone($param['phone']) == $k) $keyCustomersId[] = $v;
  3183. }
  3184. unset($condition);
  3185. $condition[] = ['id', 'in', $keyCustomersId];
  3186. }
  3187. $data = $this->selCustomer($condition, $page, $limit, $order, $root_id);
  3188. foreach ($data as $key => $val) {
  3189. $transfer = json_decode($val['transfer_info'], true);
  3190. $data[$key]['transfer_time'] = $transfer['transfer_time'];
  3191. $data[$key]['transfer_name'] = $transfer['transfer_empname'];
  3192. $data[$key]['jiange_day'] = ceil((strtotime(date('Y-m-d')) - strtotime($val['last_contact_date'])) / 86400);
  3193. }
  3194. $count = Customer::where($condition)->count();
  3195. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  3196. }
  3197. //公用列表筛选
  3198. public function screen()
  3199. {
  3200. $request = request();
  3201. $root_id = $request->empcrm->root_id;
  3202. $empid = $request->empcrm->id;
  3203. $is_manager = $request->empcrm->is_manager;
  3204. $team_orgs = [$request->empcrm->org_id];
  3205. if ($is_manager == 1) {
  3206. $team_orgs = orgSubIds($request->empcrm->org_id);
  3207. //$org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
  3208. }
  3209. $soudata = CustomerSource::field('id,source as name')->where('root_id', $root_id)->select()->toArray();
  3210. View::assign('soudata', $soudata);
  3211. $list = $this->get_designer();
  3212. if (!empty($list)) {
  3213. // $list = hanziInitsort($list, 'name');
  3214. $sort = array_column($list, 's');
  3215. sort($sort);
  3216. array_multisort($sort, SORT_ASC, $list);
  3217. $sort = array_unique($sort);
  3218. $list = ['sort' => $sort, 'arr' => $list];
  3219. } else {
  3220. $list = ['sort' => [], 'arr' => []];
  3221. }
  3222. View::assign('designers', $list);
  3223. //客户等级
  3224. $level = CustomerPortraitField::with(['select'])->where([['root_id', '=', $root_id], ['keyname', '=', 'level']])->find()->toArray();
  3225. View::assign('level', $level['select']);
  3226. //部门所有员工
  3227. $employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->field('id,name')->select()->toArray();
  3228. View::assign('employee_list', $employee);
  3229. View::assign('empid', $empid);
  3230. $customer_type = [['id' => 1, 'name' => '资源库'], ['id' => 2, 'name' => '自建'], ['id' => 3, 'name' => '活动'], ['id' => 4, 'name' => '装修推荐官']];
  3231. View::assign('customer_type', $customer_type);
  3232. }
  3233. /**
  3234. * 有效客户联查(包含回收站)
  3235. */
  3236. public function selCustomerValid($condition, $con_union, $page, $limit, $order, $root_id)
  3237. {
  3238. $request = request();
  3239. $root_id = $request->empcrm->root_id;
  3240. $empid = $request->empcrm->id;
  3241. $is_manager = $request->empcrm->is_manager;
  3242. $path = Org::where([['pid', '=', $request->empcrm->org_id]])->count();
  3243. $prefix = env('database.prefix');
  3244. if ($page && $limit) {
  3245. $data = Db::table($prefix . 'customer')
  3246. ->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')
  3247. ->where($condition)
  3248. ->union(function ($query) use ($con_union) {
  3249. $prefix = env('database.prefix');
  3250. $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');
  3251. })->order($order)->select()->toArray(); //,employee_time,add_wechat_time
  3252. } else {
  3253. $data = Db::table($prefix . 'customer')
  3254. ->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')
  3255. ->where($condition)
  3256. ->union(function ($query) use ($con_union) {
  3257. $prefix = env('database.prefix');
  3258. $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');
  3259. })->page($page, $limit)->order($order)->select()->toArray(); //,employee_time,add_wechat_time
  3260. }
  3261. $aec = new Aec(config('app.aec_key'), config('app.aec_iv'));
  3262. foreach ($data as $k => $v) {
  3263. $data[$k]['state'] = Customer::changeState($v['state'], 'state');
  3264. $phone = $aec->decrypt($v['phone']);
  3265. $phone1 = !empty($v['phone1']) ? $aec->decrypt($v['phone1']) : '';
  3266. $phone2 = !empty($v['phone2']) ? $aec->decrypt($v['phone2']) : '';
  3267. $data[$k]['phone'] = substr_replace($phone, '******', 3, 6);
  3268. $data[$k]['phone1'] = $phone1 ? substr_replace($phone1, '******', 3, 6) : '';
  3269. $data[$k]['phone2'] = $phone2 ? substr_replace($phone2, '******', 3, 6) : '';
  3270. $employee = Employee::find($v['employee_id']);
  3271. $data[$k]['employee'] = null;
  3272. if (!empty($employee)) {
  3273. $data[$k]['employee']['name'] = $employee['name'];
  3274. }
  3275. $employee = Employee::find($v['designer_id']);
  3276. $data[$k]['designer'] = null;
  3277. if (!empty($employee)) {
  3278. $data[$k]['designer']['name'] = $employee['name'];
  3279. }
  3280. $data[$k]['visit_log_count'] = CustomerVisitLog::where([['customer_id', '=', $v['id']], [CustomerVisitLog::changeState(['state', '=', '未到访'])]])->count();
  3281. $data[$k]['source'] = null;
  3282. if ($v['source_id']) {
  3283. $source = CustomerSource::where('id', '=', $v['source_id'])->find();
  3284. if (!empty($source)) {
  3285. $data[$k]['source']['source'] = $source['source'];
  3286. }
  3287. }
  3288. $data[$k]['org_name'] = [];
  3289. if ($v['org_id']) {
  3290. $org = Org::where('id', '=', $v['org_id'])->find();
  3291. if (!empty($org)) {
  3292. $data[$k]['org_name'] = $org['name'];
  3293. }
  3294. }
  3295. $visit_log = CustomerVisitLog::where('customer_id', '=', $v['id'])->field('id,customer_id,state,addtime,confirm_date,money')->order('addtime asc')->select()->toArray();
  3296. foreach ($visit_log as $k_log => $v_log) {
  3297. $visit_log[$k_log]['state'] = CustomerVisitLog::changeState($v_log['state'], 'state');
  3298. }
  3299. $data[$k]['visitLog'] = $visit_log;
  3300. }
  3301. $setting = Setting::where([['name', '=', 'pubpool'], ['root_id', '=', $root_id]])->find();
  3302. if (!empty($setting)) {
  3303. $setting_content = json_decode($setting['content'], true);
  3304. } else {
  3305. $setting_content = [];
  3306. }
  3307. $empids = !empty($data) ? array_column($data, 'employee_id') : [];
  3308. $emporg = Employee::with(['org' => function ($query) {
  3309. $query->field('id,name');
  3310. }])->where([['root_id', '=', $root_id], ['id', 'in', $empids]])->field('id,org_id')->select()->toArray();
  3311. $state1 = CustomerVisitLog::changeState('已到店', 'chaos');
  3312. $state2 = CustomerVisitLog::changeState('已交定', 'chaos');
  3313. $state3 = CustomerVisitLog::changeState('已签单', 'chaos');
  3314. $state4 = CustomerVisitLog::changeState('已量房', 'chaos');
  3315. $state5 = CustomerVisitLog::changeState('无效', 'chaos');
  3316. foreach ($data as &$item) {
  3317. $item['emporg'] = $item['source_name'] = $item['designer_name'] = $item['employee_name'] = '';
  3318. if (!empty($item['source'])) {
  3319. $item['source_name'] = $item['source']['source'];
  3320. }
  3321. if (!empty($item['designer'])) {
  3322. $item['designer_name'] = $item['designer']['name'];
  3323. }
  3324. if (!empty($item['employee'])) {
  3325. $item['employee_name'] = $item['employee']['name'];
  3326. foreach ($emporg as $p) {
  3327. if ($p['id'] == $item['employee_id']) $item['emporg'] = $p['org']['name'];
  3328. }
  3329. }
  3330. $item['un_protected'] = false;
  3331. if (!empty($item['protected_to']) && time() > strtotime($item['protected_to'])) {
  3332. $state_n = Customer::changeState($item['state'], 'n');
  3333. if (isset($setting_content[$state_n]['state']) && $setting_content[$state_n]['state'] == 1) {
  3334. $item['un_protected'] = true;
  3335. }
  3336. }
  3337. $item['last_invalid_time'] = $item['first_liangfang'] = $item['jiaoding_time'] = $item['sign'] = $item['fisttime'] = '';
  3338. if (!empty($item['visitLog'])) {
  3339. //取最新的一条为客户的跟进时间
  3340. $item['last_contact_date'] = $item['visitLog'][count($item['visitLog']) - 1]['addtime'];
  3341. $state = [];
  3342. foreach ($item['visitLog'] as $k => $v) {
  3343. $state[$v['state']][] = $v;
  3344. }
  3345. foreach ($state as $k => $v) {
  3346. if (in_array($k, $state1)) $item['fisttime'] = date('Y-m-d', strtotime($v[0]['confirm_date']));
  3347. if (in_array($k, $state4)) $item['first_liangfang'] = date('Y-m-d', strtotime($v[0]['addtime']));
  3348. if (in_array($k, $state2)) {
  3349. $item['jiaoding_time'] = $v[0]['addtime'] ?? '';
  3350. $item['jiaoding_money'] = $v[0]['money'] ?? '';
  3351. }
  3352. if (in_array($k, $state3)) {
  3353. $item['qiandan_time'] = $v[0]['addtime'] ?? '';
  3354. $item['qiandan_money'] = $v[0]['money'] ?? '';
  3355. $item['sign'] = $v[0];
  3356. }
  3357. if (in_array($k, $state5)) $item['last_invalid_time'] = $v[0]['addtime'];
  3358. }
  3359. }
  3360. $item['square'] = floatval($item['square']);
  3361. $item['phone'] = substr_replace($item['phone'], '******', 3, 6);
  3362. $item['phone1'] = $item['phone1'] ? substr_replace($item['phone1'], '******', 3, 6) : '';
  3363. $item['phone2'] = $item['phone2'] ? substr_replace($item['phone2'], '******', 3, 6) : '';
  3364. $houses = $item['house_type'];
  3365. 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']) : '期房';
  3366. $item['jiaofang'] = $houses;
  3367. $item['time_status'] = $item['last_contact_date'] ? $this->uc_time_ago($item['last_contact_date']) : '';
  3368. $item['add_wechat'] = '否';
  3369. $item['sign_time'] = null;
  3370. $item['allstate'] = null;
  3371. $item['package_name'] = !empty($item['package_id']) ? CustomerPackage::where('id', $item['package_id'])->value('name') : '';
  3372. if (!empty($item['ext'])) {
  3373. $extdata = json_decode($item['ext'], true);
  3374. foreach ($extdata as $key => $val) {
  3375. if (isset($val['keyname']) && $val['keyname'] == 'wechat' && !empty($val['value'])) {
  3376. $add_wechat = CustomerPortraitFieldSelect::where('id', $val['value'])->value('name');
  3377. if ($add_wechat == '有') {
  3378. $add_wechat = '是';
  3379. }
  3380. $item['add_wechat'] = $add_wechat;
  3381. }
  3382. if (isset($val['keyname']) && $val['keyname'] == 'sign_time' && !empty($val['value'])) {
  3383. $item['sign_time'] = str_replace('/', '-', $val['value']);
  3384. }
  3385. }
  3386. }
  3387. $item['is_followup'] = 0;
  3388. if ($is_manager == 1 && empty($path)) {
  3389. $item['is_followup'] = 1;
  3390. }
  3391. if ($empid == $item['employee_id']) {
  3392. $item['is_followup'] = 1;
  3393. }
  3394. $now_status = $item['state'];
  3395. if ($now_status == '已签单') $now_status = '已转单';
  3396. if ($now_status == '已交定') $now_status = '已签单';
  3397. $item['now_status'] = $now_status;
  3398. $item['state'] = '';
  3399. $allstate = '';
  3400. if (!empty($item['visitLog'])) {
  3401. $stain = [$now_status];
  3402. foreach ($item['visitLog'] as $k => $v) {
  3403. if (!in_array($v['state'], ['已交定', '已签单', '已到店', '已量房', '已到场', '未到访', '回访'])) continue;
  3404. if ($v['state'] == '回访') $v['state'] = '待确认';
  3405. if ($v['state'] == '已签单') $v['state'] = '已转单';
  3406. if ($v['state'] == '已交定') $v['state'] = '已签单';
  3407. if (!in_array($v['state'], $stain)) {
  3408. $stain[] = $v['state'];
  3409. //$allstate.= $v['state'].',';
  3410. }
  3411. }
  3412. if (count($stain) > 1 && in_array('待确认', $stain)) $stain = array_diff($stain, ['待确认']);
  3413. //$item['state'] = trim($allstate,',');
  3414. $item['state'] = implode(',', $stain);
  3415. if ($now_status == '待确认') $item['state'] = '待确认';
  3416. }
  3417. // 未跟进时间
  3418. //$latest_visit_log = CustomerVisitLog::where([['customer_id', '=', $item['id']]])->order('addtime desc')->find();
  3419. $latest_visit_log = $item['last_contact_date'];
  3420. if (empty($latest_visit_log)) {
  3421. $no_visit_long = time() - strtotime($item['addtime']);
  3422. } else {
  3423. $no_visit_long = time() - strtotime($latest_visit_log);
  3424. }
  3425. $no_visit_day = ceil($no_visit_long / 24 / 3600);
  3426. $item['no_visit_day'] = $no_visit_day;
  3427. // 预计见面时间
  3428. $subscribe = CustomersSubscribe::where([['customer_id', '=', $item['id']], ['employee_id', '=', $item['employee_id']], ['state', '=', 0]])->order('subscribe_date desc')->find();
  3429. if (!empty($subscribe)) {
  3430. $item['subscribe_date'] = $subscribe['subscribe_date'];
  3431. } else {
  3432. $item['subscribe_date'] = '';
  3433. }
  3434. $assignIdList = !empty($item['assigned_personnel']) ? explode(',', $item['assigned_personnel']) : [];
  3435. if (!empty($item['designer_id']) && !empty($assignIdList)) $assignIdList = array_diff($assignIdList, [$item['designer_id']]);
  3436. $assign_list = Employee::where('id', 'in', $assignIdList)->column('name');
  3437. $item['assign_list'] = !empty($assign_list) ? implode(',', $assign_list) : '';
  3438. }
  3439. return $data;
  3440. }
  3441. public function selCustomerOrder($condition, $page, $limit, $order, $root_id)
  3442. {
  3443. $request = request();
  3444. $root_id = $request->empcrm->root_id;
  3445. $empid = $request->empcrm->id;
  3446. $is_manager = $request->empcrm->is_manager;
  3447. $path = Org::where([['pid', '=', $request->empcrm->org_id]])->count();
  3448. //创建临时查询报名时间-以便排序
  3449. $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";
  3450. $data = Customer::with(['employee', 'org', 'designer', 'source', 'visitLog' => function ($query) {
  3451. $query->field('id,customer_id,state,addtime,confirm_date,money')->order('addtime asc');
  3452. }])
  3453. ->withCount(['visitLog' => function ($query) {
  3454. $query->where([[CustomerVisitLog::changeState(['state', '=', '未到访'])]]);
  3455. }])
  3456. ->field($sign_time)
  3457. ->where($condition)
  3458. ->page($page, $limit)
  3459. ->order($order)->select();
  3460. $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();
  3461. $setting = Setting::where([['name', '=', 'pubpool'], ['root_id', '=', $root_id]])->find();
  3462. if (!empty($setting)) {
  3463. $setting_content = json_decode($setting['content'], true);
  3464. } else {
  3465. $setting_content = [];
  3466. }
  3467. $empids = !empty($data) ? array_column($data, 'employee_id') : [];
  3468. $emporg = Employee::with(['org' => function ($query) {
  3469. $query->field('id,name');
  3470. }])->where([['root_id', '=', $root_id], ['id', 'in', $empids]])->field('id,org_id')->select()->toArray();
  3471. $state1 = CustomerVisitLog::changeState('已到店', 'chaos');
  3472. $state2 = CustomerVisitLog::changeState('已交定', 'chaos');
  3473. $state3 = CustomerVisitLog::changeState('已签单', 'chaos');
  3474. $state4 = CustomerVisitLog::changeState('已量房', 'chaos');
  3475. $state5 = CustomerVisitLog::changeState('无效', 'chaos');
  3476. foreach ($data as &$item) {
  3477. $item['emporg'] = $item['source_name'] = $item['designer_name'] = $item['employee_name'] = '';
  3478. if (!empty($item['source'])) {
  3479. $item['source_name'] = $item['source']['source'];
  3480. }
  3481. if (!empty($item['designer'])) {
  3482. $item['designer_name'] = $item['designer']['name'];
  3483. }
  3484. if (!empty($item['employee'])) {
  3485. $item['employee_name'] = $item['employee']['name'];
  3486. foreach ($emporg as $p) {
  3487. if ($p['id'] == $item['employee_id']) $item['emporg'] = $p['org']['name'];
  3488. }
  3489. }
  3490. $item['un_protected'] = false;
  3491. if (!empty($item['protected_to']) && time() > strtotime($item['protected_to'])) {
  3492. $state_n = Customer::changeState($item['state'], 'n');
  3493. if (isset($setting_content[$state_n]['state']) && $setting_content[$state_n]['state'] == 1) {
  3494. $item['un_protected'] = true;
  3495. }
  3496. }
  3497. $item['last_invalid_time'] = $item['first_liangfang'] = $item['jiaoding_time'] = $item['sign'] = $item['fisttime'] = '';
  3498. if (!empty($item['visitLog'])) {
  3499. //取最新的一条为客户的跟进时间
  3500. $item['last_contact_date'] = $item['visitLog'][count($item['visitLog']) - 1]['addtime'];
  3501. $state = [];
  3502. foreach ($item['visitLog'] as $k => $v) {
  3503. $state[$v['state']][] = $v;
  3504. }
  3505. foreach ($state as $k => $v) {
  3506. if (in_array($k, $state1)) $item['fisttime'] = date('Y-m-d', strtotime($v[0]['confirm_date']));
  3507. if (in_array($k, $state4)) $item['first_liangfang'] = date('Y-m-d', strtotime($v[0]['addtime']));
  3508. if (in_array($k, $state2)) {
  3509. $item['jiaoding_time'] = $v[0]['addtime'];
  3510. $item['jiaoding_money'] = $v[0]['money'];
  3511. // $tmp_jiao_count = count($state['已交定']);
  3512. // if ($tmp_jiao_count > 0) {
  3513. // $item['jiaoding_time'] = $v[$tmp_jiao_count - 1]['addtime'];
  3514. // $item['jiaoding_money'] = $v[$tmp_jiao_count - 1]['money'];
  3515. // }
  3516. }
  3517. if (in_array($k, $state3)) {
  3518. $item['qiandan_time'] = $v[0]['addtime'];
  3519. $item['qiandan_money'] = $v[0]['money'];
  3520. $item['sign'] = $v[0];
  3521. // $tmp_sign_count = count($state['已签单']);
  3522. // if ($tmp_sign_count > 0) {
  3523. // $item['qiandan_time'] = $v[$tmp_sign_count - 1]['addtime'];
  3524. // $item['qiandan_money'] = $v[$tmp_sign_count - 1]['money'];
  3525. // $item['sign'] = $v[$tmp_sign_count - 1];
  3526. // }
  3527. }
  3528. if (in_array($k, $state5)) $item['last_invalid_time'] = $v[0]['addtime'];
  3529. }
  3530. }
  3531. $item['square'] = floatval($item['square']);
  3532. //$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();
  3533. $item['phone'] = substr_replace($item['phone'], '******', 3, 6);
  3534. $item['phone1'] = $item['phone1'] ? substr_replace($item['phone1'], '******', 3, 6) : '';
  3535. $item['phone2'] = $item['phone2'] ? substr_replace($item['phone2'], '******', 3, 6) : '';
  3536. $houses = $item['house_type'];
  3537. 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']) : '期房';
  3538. $item['jiaofang'] = $houses;
  3539. $item['time_status'] = $item['last_contact_date'] ? $this->uc_time_ago($item['last_contact_date']) : '';
  3540. $item['add_wechat'] = '否';
  3541. $item['sign_time'] = null;
  3542. $item['nowstate'] = $item['state'];
  3543. $item['package_name'] = !empty($item['package_id']) ? CustomerPackage::where('id', $item['package_id'])->value('name') : '';
  3544. if (!empty($item['ext'])) {
  3545. $extdata = json_decode($item['ext'], true);
  3546. foreach ($extdata as $key => $val) {
  3547. if (isset($val['keyname']) && $val['keyname'] == 'wechat' && !empty($val['value'])) {
  3548. $add_wechat = CustomerPortraitFieldSelect::where('id', $val['value'])->value('name');
  3549. if ($add_wechat == '有') {
  3550. $add_wechat = '是';
  3551. }
  3552. $item['add_wechat'] = $add_wechat;
  3553. }
  3554. if (isset($val['keyname']) && $val['keyname'] == 'sign_time' && !empty($val['value'])) {
  3555. $item['sign_time'] = str_replace('/', '-', $val['value']);
  3556. }
  3557. }
  3558. }
  3559. $item['is_followup'] = 0;
  3560. if ($is_manager == 1 && empty($path)) {
  3561. $item['is_followup'] = 1;
  3562. }
  3563. if ($empid == $item['employee_id']) {
  3564. $item['is_followup'] = 1;
  3565. }
  3566. $now_status = $item['state'];
  3567. if ($now_status == '已签单' && $request->empcrm['xinjushang'] == 0) $now_status = '已转单';
  3568. if ($now_status == '已交定' && $request->empcrm['xinjushang'] == 0) $now_status = '已签单';
  3569. $item['now_status'] = $now_status;
  3570. $item['state'] = '';
  3571. //$allstate='';
  3572. if (!empty($item['visitLog'])) {
  3573. $stain = [$now_status];
  3574. foreach ($item['visitLog'] as $k => $v) {
  3575. if (!in_array($v['state'], ['已交定', '已签单', '已到店', '已量房', '已到场', '未到访', '回访'])) continue;
  3576. if ($v['state'] == '回访') $v['state'] = '待确认';
  3577. if ($v['state'] == '已签单' && $request->empcrm['xinjushang'] == 0) $v['state'] = '已转单';
  3578. if ($v['state'] == '已交定' && $request->empcrm['xinjushang'] == 0) $v['state'] = '已签单';
  3579. if (!in_array($v['state'], $stain)) {
  3580. $stain[] = $v['state'];
  3581. //$allstate.= $v['state'].',';
  3582. }
  3583. }
  3584. if (count($stain) > 1 && in_array('待确认', $stain)) $stain = array_diff($stain, ['待确认']);
  3585. //$item['state'] = trim($allstate,',');
  3586. $item['state'] = implode(',', $stain);
  3587. if ($now_status == '待确认') $item['state'] = '待确认';
  3588. }
  3589. // 未跟进时间
  3590. //$latest_visit_log = CustomerVisitLog::where([['customer_id', '=', $item['id']]])->order('addtime desc')->find();
  3591. $latest_visit_log = $item['last_contact_date'];
  3592. if (empty($latest_visit_log)) {
  3593. $no_visit_long = time() - strtotime($item['addtime']);
  3594. } else {
  3595. $no_visit_long = time() - strtotime($latest_visit_log);
  3596. }
  3597. $no_visit_day = ceil($no_visit_long / 24 / 3600);
  3598. $item['no_visit_day'] = $no_visit_day;
  3599. // 预计见面时间
  3600. $subscribe = CustomersSubscribe::where([['customer_id', '=', $item['id']], ['employee_id', '=', $item['employee_id']], ['state', '=', 0]])->order('subscribe_date desc')->find();
  3601. if (!empty($subscribe)) {
  3602. $item['subscribe_date'] = $subscribe['subscribe_date'];
  3603. } else {
  3604. $item['subscribe_date'] = '';
  3605. }
  3606. $assignIdList = !empty($item['assigned_personnel']) ? explode(',', $item['assigned_personnel']) : [];
  3607. if (!empty($item['designer_id']) && !empty($assignIdList)) $assignIdList = array_diff($assignIdList, [$item['designer_id']]);
  3608. $assign_list = Employee::where('id', 'in', $assignIdList)->column('name');
  3609. $item['assign_list'] = !empty($assign_list) ? implode(',', $assign_list) : '';
  3610. }
  3611. return $data;
  3612. }
  3613. //公用查询列表
  3614. public function selCustomer($condition, $page, $limit, $order, $root_id)
  3615. {
  3616. $request = request();
  3617. $root_id = $request->empcrm->root_id;
  3618. $empid = $request->empcrm->id;
  3619. $is_manager = $request->empcrm->is_manager;
  3620. $path = Org::where([['pid', '=', $request->empcrm->org_id]])->count();
  3621. if ($page && $limit) {
  3622. $data = Customer::with(['employee', 'org', 'designer', 'source', 'visitLog' => function ($query) {
  3623. $query->field('id,customer_id,state,addtime,confirm_date,money')->order('addtime asc');
  3624. }])->withCount(['visitLog' => function ($query) {
  3625. $query->where([[CustomerVisitLog::changeState(['state', '=', '未到访'])]]);
  3626. }])->where($condition)->page($page, $limit)->order($order)->select();
  3627. } else {
  3628. $data = Customer::with(['employee', 'org', 'designer', 'source', 'visitLog' => function ($query) {
  3629. $query->field('id,customer_id,state,addtime,confirm_date,money')->order('addtime asc');
  3630. }])->withCount(['visitLog' => function ($query) {
  3631. $query->where([[CustomerVisitLog::changeState(['state', '=', '未到访'])]]);
  3632. }])->where($condition)->order($order)->select();
  3633. }
  3634. $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();
  3635. $setting = Setting::where([['name', '=', 'pubpool'], ['root_id', '=', $root_id]])->find();
  3636. if (!empty($setting)) {
  3637. $setting_content = json_decode($setting['content'], true);
  3638. } else {
  3639. $setting_content = [];
  3640. }
  3641. $empids = !empty($data) ? array_column($data, 'employee_id') : [];
  3642. $emporg = Employee::with(['org' => function ($query) {
  3643. $query->field('id,name');
  3644. }])->where([['root_id', '=', $root_id], ['id', 'in', $empids]])->field('id,org_id')->select()->toArray();
  3645. $state1 = CustomerVisitLog::changeState('已到店', 'chaos');
  3646. $state2 = CustomerVisitLog::changeState('已交定', 'chaos');
  3647. $state3 = CustomerVisitLog::changeState('已签单', 'chaos');
  3648. $state4 = CustomerVisitLog::changeState('已量房', 'chaos');
  3649. $state5 = CustomerVisitLog::changeState('无效', 'chaos');
  3650. $state15 = CustomerVisitLog::changeState('无忧', 'chaos');
  3651. foreach ($data as &$item) {
  3652. $item['emporg'] = $item['source_name'] = $item['designer_name'] = $item['employee_name'] = '';
  3653. if (!empty($item['source'])) {
  3654. $item['source_name'] = $item['source']['source'];
  3655. }
  3656. if (!empty($item['designer'])) {
  3657. $item['designer_name'] = $item['designer']['name'];
  3658. $item['designer_org'] = Org::where([['id', '=', $item['designer']['org_id']]])->value('name');
  3659. }
  3660. if (!empty($item['employee'])) {
  3661. $item['employee_name'] = $item['employee']['name'];
  3662. foreach ($emporg as $p) {
  3663. if ($p['id'] == $item['employee_id']) $item['emporg'] = $p['org']['name'];
  3664. }
  3665. }
  3666. $item['un_protected'] = false;
  3667. if (!empty($item['protected_to']) && time() > strtotime($item['protected_to'])) {
  3668. $state_n = Customer::changeState($item['state'], 'n');
  3669. if (isset($setting_content[$state_n]['state']) && $setting_content[$state_n]['state'] == 1) {
  3670. $item['un_protected'] = true;
  3671. }
  3672. }
  3673. $item['last_invalid_time'] = $item['first_liangfang'] = $item['jiaoding_time'] = $item['sign'] = $item['fisttime'] = '';
  3674. if (!empty($item['visitLog'])) {
  3675. //取最新的一条为客户的跟进时间
  3676. $item['last_contact_date'] = $item['visitLog'][count($item['visitLog']) - 1]['addtime'];
  3677. $state = [];
  3678. foreach ($item['visitLog'] as $k => $v) {
  3679. $state[$v['state']][] = $v;
  3680. }
  3681. foreach ($state as $k => $v) {
  3682. if (in_array($k, $state1)) $item['fisttime'] = date('Y-m-d', strtotime($v[0]['confirm_date']));
  3683. if (in_array($k, $state4)) $item['first_liangfang'] = date('Y-m-d', strtotime($v[0]['addtime']));
  3684. if (in_array($k, $state2)) {
  3685. $item['jiaoding_time'] = $v[0]['addtime'];
  3686. $item['jiaoding_money'] = $v[0]['money'];
  3687. }
  3688. if (in_array($k, $state3)) {
  3689. $item['qiandan_time'] = $v[0]['addtime'];
  3690. $item['qiandan_money'] = $v[0]['money'];
  3691. $item['sign'] = $v[0];
  3692. }
  3693. if (in_array($k, $state5)) $item['last_invalid_time'] = $v[0]['addtime'];
  3694. if (in_array($k, $state15)) $item['wuyou_money'] = $v[0]['money'];
  3695. }
  3696. }
  3697. $item['square'] = floatval($item['square']);
  3698. $item['phone'] = substr_replace($item['phone'], '******', 3, 6);
  3699. $item['phone1'] = $item['phone1'] ? substr_replace($item['phone1'], '******', 3, 6) : '';
  3700. $item['phone2'] = $item['phone2'] ? substr_replace($item['phone2'], '******', 3, 6) : '';
  3701. $houses = $item['house_type'];
  3702. 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']) : '期房';
  3703. $item['jiaofang'] = $houses;
  3704. $item['time_status'] = !empty($item['last_contact_date']) ? $this->uc_time_ago(date('Y-m-d', strtotime($item['last_contact_date']))) : '';
  3705. $item['nowstate'] = $item['state'];
  3706. $item['package_name'] = !empty($item['package_id']) ? CustomerPackage::where('id', $item['package_id'])->value('name') : '';
  3707. $item['is_followup'] = 0;
  3708. $item['lognum'] = count($item['visitLog']);
  3709. if ($is_manager == 1 && empty($path)) {
  3710. $item['is_followup'] = 1;
  3711. }
  3712. if ($empid == $item['employee_id']) {
  3713. $item['is_followup'] = 1;
  3714. }
  3715. $now_status = $item['state'];
  3716. if ($now_status == '已签单' && $request->empcrm['xinjushang'] == 0) $now_status = '已转单';
  3717. if ($now_status == '已交定' && $request->empcrm['xinjushang'] == 0) $now_status = '已签单';
  3718. $item['now_status'] = $now_status;
  3719. $item['state'] = '';
  3720. //$allstate='';
  3721. if (!empty($item['visitLog'])) {
  3722. $stain = [$now_status];
  3723. foreach ($item['visitLog'] as $k => $v) {
  3724. if (!in_array($v['state'], ['已交定', '已签单', '已到店', '已量房', '已到场', '未到访', '回访'])) continue;
  3725. if ($v['state'] == '回访') $v['state'] = '待确认';
  3726. if ($v['state'] == '已签单' && $request->empcrm['xinjushang'] == 0) $v['state'] = '已转单';
  3727. if ($v['state'] == '已交定' && $request->empcrm['xinjushang'] == 0) $v['state'] = '已签单';
  3728. if (!in_array($v['state'], $stain)) {
  3729. $stain[] = $v['state'];
  3730. }
  3731. }
  3732. if (count($stain) > 1 && in_array('待确认', $stain)) $stain = array_diff($stain, ['待确认']);
  3733. $item['state'] = implode(',', $stain);
  3734. if ($now_status == '待确认') $item['state'] = '待确认';
  3735. }
  3736. // 未跟进时间
  3737. $latest_visit_log = $item['last_contact_date'];
  3738. if (empty($latest_visit_log)) {
  3739. $no_visit_long = time() - strtotime($item['addtime']);
  3740. } else {
  3741. $no_visit_long = time() - strtotime($latest_visit_log);
  3742. }
  3743. $no_visit_day = ceil($no_visit_long / 24 / 3600);
  3744. $item['no_visit_day'] = $no_visit_day;
  3745. // 预计见面时间
  3746. $subscribe = CustomersSubscribe::where([['customer_id', '=', $item['id']], ['employee_id', '=', $item['employee_id']], ['state', '=', 0]])->order('subscribe_date desc')->find();
  3747. if (!empty($subscribe)) {
  3748. $item['subscribe_date'] = $subscribe['subscribe_date'];
  3749. } else {
  3750. $item['subscribe_date'] = '';
  3751. }
  3752. $assignIdList = !empty($item['assigned_personnel']) ? explode(',', $item['assigned_personnel']) : [];
  3753. if (!empty($item['designer_id']) && !empty($assignIdList)) $assignIdList = array_diff($assignIdList, [$item['designer_id']]);
  3754. $assign_list = Employee::where('id', 'in', $assignIdList)->column('name');
  3755. $item['assign_list'] = !empty($assign_list) ? implode(',', $assign_list) : '';
  3756. }
  3757. return $data;
  3758. }
  3759. /**
  3760. * 客户表加回收站查询
  3761. */
  3762. public function cus_recycle($condition, $page, $limit, $order, $root_id)
  3763. {
  3764. $request = request();
  3765. $root_id = $request->empcrm->root_id;
  3766. $empid = $request->empcrm->id;
  3767. $is_manager = $request->empcrm->is_manager;
  3768. $path = Org::where([['pid', '=', $request->empcrm->org_id]])->count();
  3769. $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
  3770. ( 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 . "))
  3771. LIMIT " . $page . "," . $limit;
  3772. $data = Db::query($sql);
  3773. $sqlcont = "( SELECT id FROM `fl_customer` WHERE `id` in (" . $condition . ")) UNION
  3774. ( SELECT customer_id as id FROM `fl_customer_recycle` WHERE `customer_id` in (" . $condition . "))";
  3775. $count = count(Db::query($sqlcont));
  3776. $state1 = CustomerVisitLog::changeState('已到店', 'chaos');
  3777. $state2 = CustomerVisitLog::changeState('已交定', 'chaos');
  3778. $state3 = CustomerVisitLog::changeState('已签单', 'chaos');
  3779. $state4 = CustomerVisitLog::changeState('已量房', 'chaos');
  3780. $state5 = CustomerVisitLog::changeState('无效', 'chaos');
  3781. foreach ($data as &$item) {
  3782. $item['visitLog'] = $item['source_name'] = $item['designer_name'] = $item['employee_name'] = '';
  3783. $item['source'] = !empty($item['source_id']) ? CustomerSource::where([['root_id', '=', $root_id], ['id', '=', $item['source_id']]])->field('id,source')->find() : '';
  3784. $item['designer'] = !empty($item['designer_id']) ? Employee::where([['root_id', '=', $root_id], ['id', '=', $item['designer_id']]])->field('id,name')->find() : '';
  3785. $item['employee'] = !empty($item['employee_id']) ? Employee::where([['root_id', '=', $root_id], ['id', '=', $item['employee_id']]])->field('id,name')->find() : '';
  3786. $item['last_invalid_time'] = $item['first_liangfang'] = $item['jiaoding_time'] = $item['sign'] = $item['fisttime'] = '';
  3787. $visitLog = CustomerVisitLog::where([['customer_id', '=', $item['id']], ['org_id', 'in', orgSubIds($root_id)]])->field('id,customer_id,state,addtime,confirm_date,money')->select()->toArray();
  3788. $item['visitLog'] = !empty($visitLog) ? $visitLog : '';
  3789. if (!empty($item['visitLog'])) {
  3790. //取最新的一条为客户的跟进时间
  3791. $item['last_contact_date'] = $item['visitLog'][count($item['visitLog']) - 1]['addtime'];
  3792. $state = [];
  3793. foreach ($item['visitLog'] as $k => $v) {
  3794. $state[$v['state']][] = $v;
  3795. }
  3796. foreach ($state as $k => $v) {
  3797. if (in_array($k, $state1)) $item['fisttime'] = date('Y-m-d', strtotime($v[0]['confirm_date']));
  3798. if (in_array($k, $state4)) $item['first_liangfang'] = date('Y-m-d', strtotime($v[0]['addtime']));
  3799. if (in_array($k, $state2)) {
  3800. $item['jiaoding_time'] = $v[0]['addtime'];
  3801. $item['jiaoding_money'] = $v[0]['money'];
  3802. }
  3803. if (in_array($k, $state3)) {
  3804. $item['qiandan_time'] = $v[0]['addtime'];
  3805. $item['qiandan_money'] = $v[0]['money'];
  3806. $item['sign'] = $v[0];
  3807. }
  3808. if (in_array($k, $state5)) $item['last_invalid_time'] = $v[0]['addtime'];
  3809. }
  3810. }
  3811. $item['square'] = floatval($item['square']);
  3812. //$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();
  3813. $item['phone'] = substr_replace($this->getPhone($item['phone']), '******', 3, 6);
  3814. $item['time_status'] = $item['last_contact_date'] ? $this->uc_time_ago($item['last_contact_date']) : '';
  3815. $item['add_wechat'] = '否';
  3816. $item['sign_time'] = date('Y-m-d H:i', strtotime($item['addtime']));
  3817. $item['allstate'] = null;
  3818. $item['package_name'] = !empty($item['package_id']) ? CustomerPackage::where('id', $item['package_id'])->value('name') : '';
  3819. if (!empty($item['ext'])) {
  3820. $extdata = json_decode($item['ext'], true);
  3821. foreach ($extdata as $key => $val) {
  3822. if (isset($val['keyname']) && $val['keyname'] == 'wechat' && !empty($val['value'])) {
  3823. $add_wechat = CustomerPortraitFieldSelect::where('id', $val['value'])->value('name');
  3824. if ($add_wechat == '有') {
  3825. $add_wechat = '是';
  3826. }
  3827. $item['add_wechat'] = $add_wechat;
  3828. }
  3829. if (isset($val['keyname']) && $val['keyname'] == 'sign_time' && !empty($val['value'])) {
  3830. $item['sign_time'] = str_replace('/', '-', $val['value']);
  3831. }
  3832. }
  3833. }
  3834. $item['is_followup'] = 0;
  3835. if ($is_manager == 1 && empty($path)) {
  3836. $item['is_followup'] = 1;
  3837. }
  3838. if ($empid == $item['employee_id']) {
  3839. $item['is_followup'] = 1;
  3840. }
  3841. $now_status = $this->getState($item['state']);
  3842. if ($now_status == '已签单' && $request->empcrm['xinjushang'] == 0) $now_status = '已转单';
  3843. if ($now_status == '已交定' && $request->empcrm['xinjushang'] == 0) $now_status = '已签单';
  3844. $item['now_status'] = $now_status;
  3845. $item['state'] = '';
  3846. $allstate = '';
  3847. if (!empty($item['visitLog'])) {
  3848. $stain = [$now_status];
  3849. foreach ($item['visitLog'] as $k => $v) {
  3850. if (!in_array($v['state'], ['已交定', '已签单', '已到店', '已量房', '已到场', '未到访', '回访'])) continue;
  3851. if ($v['state'] == '回访') $v['state'] = '待确认';
  3852. if ($v['state'] == '已签单' && $request->empcrm['xinjushang'] == 0) $v['state'] = '已转单';
  3853. if ($v['state'] == '已交定' && $request->empcrm['xinjushang'] == 0) $v['state'] = '已签单';
  3854. if (!in_array($v['state'], $stain)) {
  3855. $stain[] = $v['state'];
  3856. //$allstate.= $v['state'].',';
  3857. }
  3858. }
  3859. if (count($stain) > 1 && in_array('待确认', $stain)) $stain = array_diff($stain, ['待确认']);
  3860. //$item['state'] = trim($allstate,',');
  3861. $item['state'] = implode(',', $stain);
  3862. }
  3863. if ($now_status == '待确认') $item['state'] = '待确认';
  3864. // 未跟进时间
  3865. //$latest_visit_log = CustomerVisitLog::where([['customer_id', '=', $item['id']]])->order('addtime desc')->find();
  3866. $latest_visit_log = $item['last_contact_date'];
  3867. if (empty($latest_visit_log)) {
  3868. $no_visit_long = time() - strtotime($item['addtime']);
  3869. } else {
  3870. $no_visit_long = time() - strtotime($latest_visit_log);
  3871. }
  3872. $no_visit_day = ceil($no_visit_long / 24 / 3600);
  3873. $item['no_visit_day'] = $no_visit_day;
  3874. // 预计见面时间
  3875. $subscribe = CustomersSubscribe::where([['customer_id', '=', $item['id']], ['employee_id', '=', $item['employee_id']], ['state', '=', 0]])->order('subscribe_date desc')->find();
  3876. if (!empty($subscribe)) {
  3877. $item['subscribe_date'] = $subscribe['subscribe_date'];
  3878. } else {
  3879. $item['subscribe_date'] = '';
  3880. }
  3881. $assignIdList = !empty($item['assigned_personnel']) ? explode(',', $item['assigned_personnel']) : [];
  3882. if (!empty($item['designer_id']) && !empty($assignIdList)) $assignIdList = array_diff($assignIdList, [$item['designer_id']]);
  3883. $assign_list = Employee::where('id', 'in', $assignIdList)->column('name');
  3884. $item['assign_list'] = !empty($assign_list) ? implode(',', $assign_list) : '';
  3885. }
  3886. return ['data' => $data, 'count' => $count];
  3887. }
  3888. /**
  3889. * 手机号解密
  3890. */
  3891. public function getPhone($value)
  3892. {
  3893. if (preg_match('/^1[\d]{10}$/', $value)) return $value;
  3894. $aec = new Aec(config('app.aec_key'), config('app.aec_iv'));
  3895. $value = $aec->decrypt($value);
  3896. return $value;
  3897. }
  3898. public function getState($value)
  3899. {
  3900. $states = [
  3901. ['n' => 0, 'chaos' => [0, '待确认', '0'], 'state' => '待确认'],
  3902. ['n' => 1, 'chaos' => [1, '未到访', '1'], 'state' => '未到访'],
  3903. ['n' => 2, 'chaos' => [2, '已到访', '确认到店', '确定到店', '已到店', '2'], 'state' => '已到店'],
  3904. ['n' => 3, 'chaos' => [3, '确定到场', '确认到场', '已到场', '3'], 'state' => '已到场'],
  3905. ['n' => 4, 'chaos' => [4, '已量房', '确定量房', '确认量房', '4'], 'state' => '已量房'],
  3906. ['n' => 5, 'chaos' => [5, '定金', '交定', '已交定', '5'], 'state' => '已交定'],
  3907. ['n' => 6, 'chaos' => [6, '签单', '已签单', '6'], 'state' => '已签单'],
  3908. ['n' => 7, 'chaos' => [7, '无效', '7'], 'state' => '无效'],
  3909. ['n' => 8, 'chaos' => [8, '已卖卡', '8'], 'state' => '已卖卡'],
  3910. ];
  3911. if (is_numeric($value)) $value = intval($value);
  3912. foreach ($states as $s) {
  3913. if ($value === $s['n'] || in_array($value, $s['chaos'], true)) return $s['state'];
  3914. }
  3915. return '';
  3916. }
  3917. //添加微信
  3918. public function add_wetch()
  3919. {
  3920. $request = request();
  3921. $root_id = $request->empcrm->root_id;
  3922. $empid = $request->empcrm->id;
  3923. $param = $request->only(['id']);
  3924. $orgids = orgSubIds($root_id);
  3925. $field = CustomerPortraitField::with(['select'])->where([['keyname', '=', 'wechat'], ['root_id', '=', $root_id]])->find()->toArray();
  3926. foreach ($field['select'] as $key => $val) {
  3927. if ($val['name'] == '是' || $val['name'] == '有') {
  3928. $isadd = strval($val['id']);
  3929. }
  3930. }
  3931. $extadd = ['id' => $field['id'], 'keyname' => $field['keyname'], 'value' => $isadd];
  3932. $data = Customer::where([['id', '=', $param['id']], ['org_id', 'in', $orgids]])->find();
  3933. if (empty($data)) return json(['code' => 1, 'msg' => '客户不存在']);
  3934. if (!empty($data['ext']) && $data['ext'] != 'null') {
  3935. $extdata = json_decode($data['ext'], true);
  3936. $fd = array_column($extdata, 'keyname');
  3937. if (in_array('wechat', $fd)) {
  3938. foreach ($extdata as $key => $val) {
  3939. if ($val['keyname'] == 'wechat') {
  3940. $extdata[$key]['value'] = $isadd;
  3941. }
  3942. }
  3943. } else {
  3944. array_push($extdata, $extadd);
  3945. }
  3946. } else {
  3947. $extdata = $extadd;
  3948. }
  3949. $data->ext = json_encode($extdata);
  3950. $data->save();
  3951. return json(['code' => 0, 'data' => '', 'msg' => '添加成功']);
  3952. }
  3953. //获取设计师列表
  3954. private function get_designer()
  3955. {
  3956. //设计师获取
  3957. $w[] = ['path', 'like', request()->empcrm->root_id . '-%'];
  3958. $w[] = ['org_type', '=', 2];
  3959. $orgs = Org::where($w)->column('id');
  3960. $list = Employee::where([['org_id', 'in', $orgs], ['state', '=', '在职'], ['show', '=', 0]])->field('id,name,initials s')->order('s asc')->select()->toArray();
  3961. return $list;
  3962. }
  3963. public function uc_time_ago($ptime)
  3964. {
  3965. $ptime = strtotime($ptime);
  3966. $etime = strtotime(date('Y-m-d')) - $ptime;
  3967. switch ($etime) {
  3968. case $etime > 60 * 60 && $etime <= 24 * 60 * 60:
  3969. $msg = date('Ymd', $ptime) == date('Ymd', time()) ? '今天 ' : '昨天 ';
  3970. break;
  3971. case $etime > 24 * 60 * 60 && $etime <= 2 * 24 * 60 * 60:
  3972. $msg = date('Ymd', $ptime) + 1 == date('Ymd', time()) ? '昨天 ' : '前天 ';
  3973. break;
  3974. case $etime > 2 * 24 * 60 * 60:
  3975. $msg = floor($etime / 86400) . '天+';
  3976. break;
  3977. default:
  3978. $msg = date('Y-m-d H:i', $ptime);
  3979. }
  3980. return $msg;
  3981. }
  3982. /**
  3983. * 客户报备
  3984. */
  3985. public function add_customer()
  3986. {
  3987. if (!request()->isAjax()) {
  3988. return View::fetch();
  3989. }
  3990. $root_id = request()->empcrm->root_id;
  3991. $employee_id = request()->empcrm->id;
  3992. $is_manager = request()->empcrm->is_manager;
  3993. $org_id = request()->empcrm->org_id;
  3994. $orgids = orgSubIds($root_id);
  3995. //编辑客户id
  3996. $id = $this->request->param('id');
  3997. if (empty($id)) {
  3998. // 设计师能否报备、获取客户
  3999. $de_where[] = ['root_id', '=', $root_id];
  4000. $de_where[] = ['name', '=', 'designer_get_customer'];
  4001. $info_de_where = Setting::where($de_where)->findOrEmpty();
  4002. if (!$info_de_where->isEmpty() && request()->empcrm->org_type == 2) {
  4003. if ((int)$info_de_where['content'] != 1) {
  4004. return json(['code' => 1, 'msg' => '您的身份为设计师,无法报备客户']);
  4005. }
  4006. }
  4007. }
  4008. //管理员进行编辑客户信息
  4009. if ($is_manager == 1 && $id) {
  4010. $employee_id = Customer::where([['id', '=', $id], ['org_id', 'in', $orgids]])->value('employee_id');
  4011. }
  4012. $data = $this->request->only(['name', 'phone', 'sex', 'level', 'state' => '待确认', 'last_contact_date' => date('Y-m-d'), 'phone1', 'phone2', 'agents_id']);
  4013. //扩展字段
  4014. $ext = $this->request->param('ext');
  4015. //去掉添加的备用手机号字段
  4016. foreach ($ext as $key => $val) {
  4017. if (in_array($val['keyname'], ['phone1', 'phone2'])) {
  4018. unset($ext[$key]);
  4019. }
  4020. }
  4021. $ext = array_values($ext);
  4022. $orgids = orgSubIds($root_id);
  4023. $pharr = [
  4024. $data['phone'],
  4025. !empty($data['phone1']) ? $data['phone1'] : '',
  4026. !empty($data['phone2']) ? $data['phone2'] : ''
  4027. ];
  4028. $repeat = [];
  4029. foreach ($pharr as $key => $val) {
  4030. if (empty($val)) continue;
  4031. if (!in_array($val, $repeat)) {
  4032. $repeat[] = $val;
  4033. } else {
  4034. return json(['code' => 1, 'msg' => '手机号不能重复']);
  4035. }
  4036. }
  4037. //判断手机号是否在公海,资源库,已建档,回收站
  4038. // 增加同部门能否重复录入判断
  4039. if (!$id) {
  4040. $tips = $this->checkPepeat($pharr, $orgids, $employee_id);
  4041. if (!empty($tips)) return $tips;
  4042. } else {
  4043. $tips = $this->checkPepeat($pharr, $orgids, $employee_id, $id);
  4044. if (!empty($tips)) return $tips;
  4045. }
  4046. //验证接收的自定义字段是否开启必填
  4047. $get_field = [
  4048. isset($data['name']) ? 'name' : '',
  4049. isset($data['phone']) ? 'phone' : '',
  4050. isset($data['sex']) ? 'sex' : '',
  4051. isset($data['level']) ? 'level' : '',
  4052. ];
  4053. $get_field = array_filter($get_field);
  4054. $get_field = array_merge($get_field, array_column($ext, 'keyname'));
  4055. $all_field = CustomerPortraitField::where([['root_id', '=', $root_id], ['status', '=', 0], ['keyname', 'in', $get_field]])->orderRaw('if(isnull(sort),1,0),sort asc')->select()->toArray();
  4056. $validate_data = $data;
  4057. $house_type_value = '';
  4058. $live_broadcast_value = [];
  4059. foreach ($ext as $t) {
  4060. $validate_data[$t['keyname']] = !empty($t['value']) ? $t['value'] : '';
  4061. if ($t['keyname'] == 'house_type' && !empty($t['value'])) {
  4062. $house_type_value = CustomerPortraitFieldSelect::where([['pid', '=', $t['id']], ['id', '=', $t['value']]])->value('name');
  4063. }
  4064. if ($t['keyname'] == 'live_broadcast' && !empty($t['value'])) {
  4065. $live_broadcast_value = CustomerPortraitFieldSelect::where([['pid', '=', $t['id']], ['id', 'in', explode(',', $t['value'])]])->column('name');
  4066. }
  4067. }
  4068. foreach ($all_field as $item) {
  4069. if ($house_type_value != '期房' && $item['keyname'] == 'house_delivery_time' && $item['is_must'] == 0 && empty($validate_data[$item['keyname']])) continue;
  4070. if (!in_array('1对1业务直播', $live_broadcast_value) && $item['keyname'] == 'live_broadcast_business' && $item['is_must'] == 0 && empty($validate_data[$item['keyname']])) continue;
  4071. if (!in_array('1对1设计直播', $live_broadcast_value) && $item['keyname'] == 'live_broadcast_design' && $item['is_must'] == 0 && empty($validate_data[$item['keyname']])) continue;
  4072. if (!in_array('一对多直播', $live_broadcast_value) && $item['keyname'] == 'live_broadcast_personnel' && $item['is_must'] == 0 && empty($validate_data[$item['keyname']])) continue;
  4073. if ($item['is_must'] == 0 && empty($validate_data[$item['keyname']])) {
  4074. return json(['code' => 1, 'msg' => $item['name'] . '不能为空']);
  4075. }
  4076. }
  4077. //扩展字段保存
  4078. $add_customer_extfield = $this->add_customer_extfield($ext);
  4079. $data = array_merge($data, $add_customer_extfield);
  4080. $data['sex'] = isset($data['sex']) && !empty($data['sex']) ? $data['sex'] : 1; //数据库性别必须有默认值
  4081. $customer = new Customer();
  4082. if ($id) {
  4083. unset($data['state']);
  4084. unset($data['last_contact_date']);
  4085. $customer = Customer::where(['id' => $id, 'employee_id|designer_id' => $employee_id])->find();
  4086. $old_customer = $customer;
  4087. if (empty($customer)) return json(['code' => 1, 'msg' => '数据不存在']);
  4088. //增加检测修改的扩展字段信息
  4089. if (!empty($customer['ext']) && $customer['ext'] != 'null') {
  4090. $extdata = json_decode($customer['ext'], true);
  4091. if (isset($extdata['ext1'])) {
  4092. $new_ext = CustomerPortraitField::where([['root_id', '=', $root_id], ['pid', '<>', 0]])->select()->toArray();
  4093. $this->check_old_updatefield($customer, $ext, $new_ext);
  4094. } else {
  4095. $this->check_new_updatefield($customer, $ext);
  4096. }
  4097. }
  4098. } else {
  4099. $data['employee_id'] = $employee_id;
  4100. $data['org_id'] = $org_id;
  4101. }
  4102. Db::startTrans();
  4103. try {
  4104. $customer->save($data);
  4105. // 更新客户保护期
  4106. if (!$id) {
  4107. $customer->save(['employee_time' => date('Y-m-d H:i:s')]); //保存员工获取客户时间
  4108. Customer::changeProtectedTo($customer->id, $root_id);
  4109. $this->check_not_updatefield($customer, $ext);
  4110. }
  4111. //建档后添加一条跟进记录
  4112. if (empty($id)) $this->addFirstCustoemrVisitLog($customer->id);
  4113. Db::commit();
  4114. } catch (\Exception $e) {
  4115. Db::rollback();
  4116. return json(['code' => 1, 'msg' => $e->getMessage()]);
  4117. }
  4118. // 添加微爆积分
  4119. if ($id) {
  4120. // 老客户新加微
  4121. $new_customer = Customer::find($id);
  4122. if (!empty($new_customer['add_wechat_time']) && empty($old_customer['add_wechat_time'])) {
  4123. $i_data['add_wechat_time'] = $new_customer['add_wechat_time'];
  4124. $i_data['customer_id'] = $id;
  4125. $i_data['employee_id'] = $new_customer['employee_id'];
  4126. $i_data['type'] = 1;
  4127. $i_data['org_id'] = $new_customer['org_id'];
  4128. WechatActivityIntegral::addIntegral($i_data, $root_id);
  4129. }
  4130. } else {
  4131. $new_customer = Customer::find($customer->id);
  4132. if (!empty($new_customer['add_wechat_time'])) {
  4133. $i_data['add_wechat_time'] = $new_customer['add_wechat_time'];
  4134. $i_data['customer_id'] = $customer->id;
  4135. $i_data['employee_id'] = $new_customer['employee_id'];
  4136. $i_data['type'] = 1;
  4137. $i_data['org_id'] = $new_customer['org_id'];
  4138. WechatActivityIntegral::addIntegral($i_data, $root_id);
  4139. }
  4140. }
  4141. $customer['name'] = !empty($customer['name']) ? $customer['name'] : '';
  4142. $customer['phone'] = substr_replace($customer['phone'], '******', 3, 6);
  4143. // 发送管理层模板消息
  4144. $msg = ['first' => '行为通知', 'keyword1' => '客户信息编辑', 'keyword2' => "员工" . request()->empcrm->name . "对客户" . $customer['name'] . "(" . $customer['phone'] . ")信息编辑", 'keyword3' => date('Y/m/d'), 'remark' => '请悉知'];
  4145. TmpMsg::sendMsgToLeader($msg, request()->empcrm->org_id);
  4146. return json(['code' => 0, 'data' => $customer->id, 'msg' => '添加成功']);
  4147. }
  4148. //新添加字段时的处理
  4149. public function check_not_updatefield($cusdata, $save_ext)
  4150. {
  4151. $yes_save = [];
  4152. foreach ($save_ext as $key => $val) {
  4153. if (!empty($val['value'])) {
  4154. $yes_save[] = $val;
  4155. }
  4156. }
  4157. if (!empty($yes_save)) $this->save_field_log($yes_save, $cusdata);
  4158. }
  4159. //查询修改值的字段
  4160. public function check_new_updatefield($cusdata, $save_ext)
  4161. {
  4162. $old_data = json_decode($cusdata['ext'], true);
  4163. $yes_save = [];
  4164. foreach ($old_data as $k => $v) {
  4165. foreach ($save_ext as $kk => $vv) {
  4166. if ($v['id'] == $vv['id']) {
  4167. //判断是否修改了数据
  4168. if ($v['value'] != $vv['value']) {
  4169. $yes_save[] = $vv;
  4170. }
  4171. unset($save_ext[$kk]);
  4172. }
  4173. }
  4174. }
  4175. //去除为空值的新字段
  4176. foreach ($save_ext as $key => $val) {
  4177. if (empty($val['value'])) {
  4178. unset($save_ext[$key]);
  4179. }
  4180. }
  4181. $save_ext = array_values($save_ext);
  4182. $yes_data = $yes_save;
  4183. if (!empty($save_ext)) {
  4184. $yes_data = array_merge($yes_save, $save_ext);
  4185. }
  4186. if (!empty($yes_data)) $this->save_field_log($yes_data, $cusdata);
  4187. }
  4188. public function get_old_ext()
  4189. {
  4190. $extAttr = [
  4191. 'ext1' => '年龄',
  4192. 'ext2' => '是否首次装修',
  4193. 'ext3' => '消费水平',
  4194. 'ext4' => '预计装修时间',
  4195. 'ext5' => '客户爱好',
  4196. 'ext6' => '是否添加微信',
  4197. 'ext7' => '客户空闲时间段',
  4198. 'ext8' => '家庭结构',
  4199. 'ext9' => '家庭成员意见',
  4200. 'ext10' => '请选择房屋户型',
  4201. 'ext11' => '决策人',
  4202. 'ext12' => '客户喜欢的色调',
  4203. 'ext13' => '房屋结构',
  4204. 'ext14' => '装修方式',
  4205. 'ext15' => '客户其它需求',
  4206. 'ext16' => '客户痛点',
  4207. 'ext17' => '重视环保',
  4208. 'ext18' => '重视设计',
  4209. 'ext19' => '关注的工艺',
  4210. 'ext20' => '关注房屋的空间',
  4211. 'ext21' => '汽车价格',
  4212. 'ext22' => '房屋价格',
  4213. 'ext23' => '买小区的原因',
  4214. 'ext24' => '周边配置',
  4215. 'ext25' => '设计师服务',
  4216. 'ext26' => '方案满意度',
  4217. 'ext27' => '报价满意度',
  4218. 'ext28' => '装修意向',
  4219. 'ext29' => '是否到店',
  4220. 'ext30' => '预计到店时间',
  4221. 'ext31' => '是否交定',
  4222. 'ext32' => '客户其他需求',
  4223. 'ext33' => '当前所在区域',
  4224. 'ext34' => '加微类型',
  4225. 'ext35' => '建群情况',
  4226. 'ext36' => '直播情况',
  4227. 'ext37' => '直播人员',
  4228. 'ext38' => '房屋用途',
  4229. 'ext39' => '房屋位置',
  4230. 'ext40' => '其他房屋信息',
  4231. 'ext41' => '谈单时长',
  4232. 'ext42' => '门牌单元号',
  4233. 'ext43' => '业务直播人员',
  4234. 'ext44' => '设计直播人员'
  4235. ];
  4236. return $extAttr;
  4237. }
  4238. //老数据查询修改值的字段
  4239. public function check_old_updatefield($cusdata, $save_ext, $new_ext)
  4240. {
  4241. $old_ext = $this->get_old_ext();
  4242. $ext = json_decode($cusdata['ext'], true);
  4243. // 数据的旧ext处理
  4244. $xin = [];
  4245. foreach ($ext as $key => $val) {
  4246. foreach ($old_ext as $k => $v) {
  4247. if ($k == $key) {
  4248. $a['name'] = $v;
  4249. $a['value'] = $val;
  4250. }
  4251. }
  4252. $xin[] = $a;
  4253. }
  4254. //旧ext处理成新的ext结构
  4255. $old_data = [];
  4256. foreach ($xin as $k => $v) {
  4257. foreach ($new_ext as $kk => $vv) {
  4258. if ($v['name'] == $vv['name'] && !empty($v['value'])) {
  4259. $old_data['id'] = $vv['id'];
  4260. $old_data['keyname'] = $vv['keyname'];
  4261. $old_data['value'] = $v['value'];
  4262. }
  4263. }
  4264. }
  4265. //$old_data = json_decode($cusdata['ext'],true);
  4266. $yes_save = [];
  4267. foreach ($old_data as $k => $v) {
  4268. foreach ($save_ext as $kk => $vv) {
  4269. if ($v['id'] == $vv['id']) {
  4270. //判断是否修改了数据
  4271. if ($v['value'] != $vv['value']) {
  4272. $yes_save[] = $vv;
  4273. }
  4274. unset($save_ext[$kk]);
  4275. }
  4276. }
  4277. }
  4278. //去除为空值的新字段
  4279. foreach ($save_ext as $key => $val) {
  4280. if (empty($val['value'])) {
  4281. unset($save_ext[$key]);
  4282. }
  4283. }
  4284. $save_ext = array_values($save_ext);
  4285. $yes_data = $yes_save;
  4286. if (!empty($save_ext)) {
  4287. $yes_data = array_merge($yes_save, $save_ext);
  4288. }
  4289. if (!empty($yes_data)) $this->save_field_log($yes_data, $cusdata);
  4290. }
  4291. //处理修改字段的记录
  4292. public function save_field_log($yes_data, $cusdata)
  4293. {
  4294. $token = $this->get_token();
  4295. $save_ids = array_column($yes_data, 'id');
  4296. $is_down = 0;
  4297. $new_ext = CustomerPortraitField::with(['select'])->where([['root_id', '=', $token['root_org']], ['pid', '<>', 0], ['id', 'in', $save_ids]])->select()->toArray();
  4298. $city = Company::where('root_id', $token['root_org'])->value('city');
  4299. foreach ($new_ext as $key => $val) {
  4300. if ($val['keyname'] == 'current_region') {
  4301. $new_ext[$key]['select'][] = ['id' => $token['root_org'], 'name' => $city, 'pid' => $val['id']];
  4302. }
  4303. if ($val['keyname'] == 'source_id') {
  4304. $new_ext[$key]['select'] = CustomerSource::field('id,source as name')->where('root_id', $token['root_org'])->select()->toArray();
  4305. }
  4306. if ($val['keyname'] == 'deco_style') {
  4307. $new_ext[$key]['select'] = Decostyle::field('id,name')->where([['root_id', '=', $token['root_org']], ['type', '=', 0]])->select()->toArray();
  4308. }
  4309. }
  4310. foreach ($yes_data as $key => $val) {
  4311. $selname1 = null;
  4312. $yes_data[$key]['valname'] = $val['value'];
  4313. foreach ($new_ext as $k => $v) {
  4314. if ($v['id'] == $val['id']) {
  4315. if ($v['type'] == 6) $is_down = 1;
  4316. $yes_data[$key]['name'] = $v['name'];
  4317. $yes_data[$key]['type'] = $v['type'];
  4318. if (in_array($v['type'], [3, 4]) && !empty($val['value'])) {
  4319. $sp = explode(',', $val['value']);
  4320. foreach ($v['select'] as $w => $q) {
  4321. if (in_array($q['id'], $sp)) {
  4322. $selname1 .= $q['name'] . ',';
  4323. }
  4324. }
  4325. $yes_data[$key]['valname'] = trim($selname1, ',');
  4326. }
  4327. }
  4328. }
  4329. }
  4330. // 添加追踪记录
  4331. $visitLog = [
  4332. 'customer_id' => $cusdata->id,
  4333. 'type' => '',
  4334. 'next_contact_date' => null,
  4335. 'employee_id' => $token['employee_id'],
  4336. 'user_id' => $token['uid'],
  4337. 'remark' => '修改客户扩展信息',
  4338. 'state' => 1,
  4339. 'org_id' => $token['org_id'],
  4340. 'customer_employee_id' => $cusdata->employee_id,
  4341. 'customer_org_id' => Employee::where('id', $cusdata->employee_id)->value('org_id'),
  4342. 'save_portrait_field' => json_encode($yes_data),
  4343. 'down_portrait_field_status' => 0
  4344. ];
  4345. // var_dump($visitLog);
  4346. // exit;
  4347. CustomerVisitLog::create($visitLog);
  4348. }
  4349. /**
  4350. * 客户报备扩展字段
  4351. */
  4352. private function add_customer_extfield($ext)
  4353. {
  4354. $root_id = request()->empcrm->root_id;
  4355. $seldata = CustomerPortraitField::with(['select'])->where([['root_id', '=', $root_id], ['status', '=', 0], ['type', 'in', [3, 4]]])->select()->toArray();
  4356. $allselid = [];
  4357. foreach ($seldata as $key => $val) {
  4358. foreach ($val['select'] as $k => $v) {
  4359. $allselid[] = ['id' => $v['id'], 'name' => $v['name']];
  4360. }
  4361. }
  4362. $customer_data = [];
  4363. //Log::record($ext)->save();
  4364. foreach ($ext as $key => $val) {
  4365. $selval = '';
  4366. if ($val['keyname'] == 'community_name') {
  4367. $customer_data['community_name'] = $val['value'];
  4368. }
  4369. if ($val['keyname'] == 'square') {
  4370. $customer_data['square'] = $val['value'];
  4371. }
  4372. if ($val['keyname'] == 'house_type') {
  4373. foreach ($allselid as $k => $v) {
  4374. if ($val['value'] == $v['id']) {
  4375. $selval = $v['name'];
  4376. }
  4377. }
  4378. $customer_data['house_type'] = $selval;
  4379. }
  4380. if ($val['keyname'] == 'house_status') {
  4381. foreach ($allselid as $k => $v) {
  4382. if ($val['value'] == $v['id']) {
  4383. $selval = $v['name'];
  4384. }
  4385. }
  4386. $customer_data['house_status'] = $selval;
  4387. }
  4388. if ($val['keyname'] == 'level') {
  4389. foreach ($allselid as $k => $v) {
  4390. if ($val['value'] == $v['id']) {
  4391. $selval = $v['name'];
  4392. }
  4393. }
  4394. $customer_data['level'] = $selval;
  4395. }
  4396. if ($val['keyname'] == 'house_delivery_time') {
  4397. $customer_data['house_delivery_time'] = $val['value'];
  4398. }
  4399. if ($val['keyname'] == 'plan_deco_time') {
  4400. $customer_data['plan_deco_time'] = $val['value'];
  4401. }
  4402. if ($val['keyname'] == 'budget') {
  4403. $customer_data['budget'] = $val['value'];
  4404. }
  4405. if ($val['keyname'] == 'deco_style') {
  4406. $customer_data['deco_style'] = $val['value'];
  4407. }
  4408. if ($val['keyname'] == 'follow') {
  4409. if (!empty($val['value'])) {
  4410. $gza = explode(',', $val['value']);
  4411. foreach ($allselid as $k => $v) {
  4412. if (in_array($v['id'], $gza)) {
  4413. $selval .= $v['name'] . ',';
  4414. }
  4415. }
  4416. }
  4417. $customer_data['product'] = trim($selval, ',');
  4418. }
  4419. if ($val['keyname'] == 'source_id') {
  4420. $customer_data['source_id'] = $val['value'];
  4421. }
  4422. }
  4423. $customer_data['ext'] = json_encode($ext);
  4424. return $customer_data;
  4425. }
  4426. /**
  4427. * 新建客户时添加一条跟进记录
  4428. * $id 客户id
  4429. */
  4430. public function addFirstCustoemrVisitLog($id)
  4431. {
  4432. $token = request()->empcrm;
  4433. $name = Employee::where('id', $token['id'])->value('name');
  4434. $cname = Customer::where('id', $id)->value('name');
  4435. // $type = $source ? '建档' : '报备';
  4436. $type = '报备';
  4437. $remark = '业务员' . $name . '对客户' . $cname . '进行' . $type;
  4438. $save = [
  4439. 'customer_id' => $id,
  4440. 'type' => '',
  4441. 'next_contact_date' => date('Y-m-d'),
  4442. 'remark' => $remark,
  4443. 'addtime' => date('Y-m-d H:i:s'),
  4444. 'employee_id' => $token['id'],
  4445. 'user_id' => $token['uid'],
  4446. 'state' => 1,
  4447. 'org_id' => $token['org_id'],
  4448. 'customer_employee_id' => $token['id'],
  4449. 'customer_org_id' => $token['org_id']
  4450. ];
  4451. CustomerVisitLog::insertGetId($save);
  4452. return true;
  4453. }
  4454. /**
  4455. * 添加客户检测手机号的重复
  4456. * 1.客户录入不和资源库和回收站排重 (创艺)
  4457. * 2.客户录入需查询是否和公海重复,如重复,提示可以获取该客户 (创艺) 工单id6077
  4458. */
  4459. public function checkPepeat($pharr, $org_id, $employee_id, $id = null)
  4460. {
  4461. // 同部门能否重复录入开关设置
  4462. $root_id = request()->empcrm['root_id'];
  4463. $empcrm_repeat[] = ['root_id', '=', $root_id];
  4464. $empcrm_repeat[] = ['name', '=', 'empcrm_customer_repeat'];
  4465. $repeat_setting = Setting::where($empcrm_repeat)->findOrEmpty();
  4466. $emp_org = request()->empcrm['org_id'];
  4467. foreach ($pharr as $val) {
  4468. if (empty($val)) continue;
  4469. // 先判断公海 只有添加时候判断
  4470. if (empty($id)) {
  4471. $poolExist = Customer::where([
  4472. ['phone|phone1|phone2', '=', cypherphone(trim($val))],
  4473. ['org_id', '=', $emp_org],
  4474. ['employee_id', 'null', null],
  4475. ['is_resource', '=', 0]
  4476. ])->column('id');
  4477. if (!empty($poolExist)) {
  4478. $poolCrmIdStr = implode(',', $poolExist);
  4479. return json(['code' => 3, 'data' => $poolCrmIdStr, 'msg' => '录入失败:客户信息已在公海中。']);
  4480. }
  4481. }
  4482. // 新建档,检测手机号是否重复
  4483. $customerExist = Customer::where([
  4484. ['phone|phone1|phone2', '=', cypherphone(trim($val))],
  4485. ['org_id', 'in', $org_id],
  4486. ['employee_id', '>', 0] // 员工客户
  4487. ])->field('id,employee_id,is_resource,org_id')->select()->toArray();
  4488. $id_arr = array_column($customerExist, 'id');
  4489. $employee_id_arr = array_column($customerExist, 'employee_id');
  4490. if (empty($id)) {
  4491. // 对非当前业务员名下重复客户不在判断
  4492. if (in_array($employee_id, $employee_id_arr)) {
  4493. // 是否已存在为自己的客户
  4494. return json(['code' => 2, 'msg' => '录入失败。' . $val . '已经是您的客户。']);
  4495. }
  4496. } else {
  4497. if (!in_array($id, $id_arr) && in_array($employee_id, $employee_id_arr)) {
  4498. // 是否已存在为自己的客户
  4499. return json(['code' => 2, 'msg' => '录入失败。' . $val . '已经是您的客户。']);
  4500. }
  4501. }
  4502. //判断同部门
  4503. if (!$repeat_setting->isEmpty()) {
  4504. $repeat_org = explode(',', $repeat_setting['content']);
  4505. if (in_array($emp_org, $repeat_org)) {
  4506. // 需要验证
  4507. if (!empty($customerExist)) {
  4508. foreach ($customerExist as $ex) {
  4509. if (in_array($ex['org_id'], $repeat_org) && $ex['employee_id'] != $employee_id) {
  4510. // 为了避免开启撞单之前都已经重复的情况,所以要查一下这个id的现数据
  4511. if ($id) {
  4512. $this_customer = Customer::find($id);
  4513. if ($this_customer) {
  4514. $this_customer_phone = array_filter([$this_customer['phone'], $this_customer['phone1'], $this_customer['phone2']]);
  4515. if (!in_array($val, $this_customer_phone)) { // 不是原来的客户手机号,编辑时候判断撞单
  4516. $have_emp = Employee::where('id', '=', $ex['employee_id'])->value('name');
  4517. return json(['code' => 2, 'msg' => '该客户与员工' . $have_emp . '撞单,无法录入']);
  4518. }
  4519. }
  4520. } else {
  4521. $have_emp = Employee::where('id', '=', $ex['employee_id'])->value('name');
  4522. return json(['code' => 2, 'msg' => '该客户与员工' . $have_emp . '撞单,无法录入']);
  4523. }
  4524. }
  4525. }
  4526. }
  4527. }
  4528. }
  4529. }
  4530. }
  4531. //编辑客户
  4532. public function edit_customer()
  4533. {
  4534. $request = request();
  4535. $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' => '']);
  4536. $root_id = $request->empcrm->root_id;
  4537. $empid = $request->empcrm->id;
  4538. $org_employee = [$empid];
  4539. if (!$request->isAjax()) {
  4540. $soudata = CustomerSource::field('id,source as name')->where('root_id', $root_id)->select()->toArray();
  4541. View::assign('soudata', $soudata);
  4542. $list = $this->get_designer();
  4543. if (!empty($list)) {
  4544. // $list = hanziInitsort($list, 'name');
  4545. $sort = array_column($list, 's');
  4546. sort($sort);
  4547. array_multisort($sort, SORT_ASC, $list);
  4548. $sort = array_unique($sort);
  4549. $list = ['sort' => $sort, 'arr' => $list];
  4550. } else {
  4551. $list = ['sort' => [], 'arr' => []];
  4552. }
  4553. View::assign('designers', $list);
  4554. //客户等级 隶属计划
  4555. $fields = CustomerPortraitField::with(['select'])->where([['root_id', '=', $root_id], ['keyname', 'in', ['level', 'subjection_plan', 'housetype_arrow']]])->select()->toArray();
  4556. View::assign('fields', $fields);
  4557. // var_dump($fields);
  4558. // exit;
  4559. //部门所有员工
  4560. $employee_list = Employee::where([['root_id', '=', $root_id], ['org_id', '=', $request->empcrm->org_id], ['state', 'in', ['在职']]])->field('id,name')->select()->toArray();
  4561. View::assign('employee_list', $employee_list);
  4562. //意向风格
  4563. $decostyle_list = Decostyle::field('id,name,root_id as pid')->where(['root_id' => $root_id])->where([['type', '=', 0]])->order('id desc')->select()->toArray();
  4564. View::assign('decostyle_list', $decostyle_list);
  4565. //意向产品
  4566. $product = CustomerPackage::field('id, name, total_price')->where(['root_id' => $root_id])->select();
  4567. View::assign('product', $product);
  4568. View::assign('empid', $empid);
  4569. //客户详情
  4570. $customer = $this->seLemp($param['id'], $empid);
  4571. // var_dump($customer->toArray());
  4572. // exit;
  4573. View::assign('customer', $customer);
  4574. return View::fetch();
  4575. }
  4576. if (!empty($param['phone']) && !empty($param['phone1']) && $param['phone'] == $param['phone1']) {
  4577. return json(['code' => 1, 'msg' => '手机号不能重复']);
  4578. }
  4579. $orgids = orgSubIds($root_id);
  4580. $pharr = [
  4581. $param['phone'],
  4582. !empty($param['phone1']) ? $param['phone1'] : ''
  4583. ];
  4584. $tips = $this->checkPepeat($pharr, $orgids, $empid, $param['id']);
  4585. if (!empty($tips)) return $tips;
  4586. $data['employee_id'] = !empty($param['empid']) ? $param['empid'] : $empid;
  4587. $data['org_id'] = request()->empcrm->org_id;
  4588. $data['name'] = $param['name'];
  4589. $data['phone'] = cypherphone(trim($param['phone']));
  4590. $data['phone1'] = !empty($param['phone1']) ? cypherphone(trim($param['phone1'])) : '';
  4591. //$data['sex'] = $param['sex'];
  4592. $data['source_id'] = $param['source_id'];
  4593. //$data['source_position'] = $param['source_position'];
  4594. $data['level'] = $param['level'];
  4595. $data['community_name'] = $param['community_name'];
  4596. $data['square'] = $param['square'];
  4597. $data['house_delivery_time'] = $param['house_delivery_time'] . '-01';
  4598. $data['budget'] = !empty($param['budget']) ? $param['budget'] : '';
  4599. $data['package_id'] = $param['package_id'];
  4600. $data['deco_style'] = $param['deco_style'];
  4601. //$data['sign_time'] = $param['sign_time'];
  4602. $data['designer_id'] = $param['designer_id'];
  4603. //处理客户的扩展字段
  4604. $et = [];
  4605. $kz = CustomerPortraitField::where([['root_id', '=', $root_id], ['keyname', 'in', ['subjection_plan', 'housetype_arrow', 'unit_number']]])->select();
  4606. foreach ($kz as $key => $val) {
  4607. $et[] = ['id' => $val['id'], 'keyname' => $val['keyname'], 'value' => $param[$val['keyname']]];
  4608. }
  4609. $data['ext'] = !empty($et) ? json_encode($et) : NULL;
  4610. $ms = Customer::where('id', $param['id'])->update($data);
  4611. // 更新客户保护期
  4612. //Customer::changeProtectedTo($ms, $this->request->token['root_org']);
  4613. //if($ms) $this->addFirstCustoemrVisitLog($ms);
  4614. $data['name'] = !empty($data['name']) ? $data['name'] : '';
  4615. $data['phone'] = substr_replace($data['phone'], '******', 3, 6);
  4616. // 发送管理层模板消息
  4617. $msg = ['first' => '行为通知', 'keyword1' => '客户信息编辑', 'keyword2' => "员工" . request()->empcrm->name . "对客户" . $data['name'] . "(" . $data['phone'] . ")信息编辑", 'keyword3' => date('Y/m/d'), 'remark' => '请悉知'];
  4618. TmpMsg::sendMsgToLeader($msg, request()->empcrm->org_id);
  4619. return json(['code' => 0, 'data' => $ms, 'msg' => '修改成功']);
  4620. }
  4621. //已签单客户修改客户信息
  4622. public function sign_savecust()
  4623. {
  4624. $request = request();
  4625. $param = $request->only(['id', 'designer_id', 'name', 'community_name', 'square', 'housetype_arrow' => '', 'package_id', 'deco_style']);
  4626. $root_id = $request->empcrm->root_id;
  4627. $empid = $request->empcrm->id;
  4628. $org_employee = [$empid];
  4629. $orgids = orgSubIds($root_id);
  4630. $data['name'] = $param['name'];
  4631. $data['community_name'] = $param['community_name'];
  4632. $data['square'] = $param['square'];
  4633. $data['package_id'] = $param['package_id'];
  4634. $data['deco_style'] = $param['deco_style'];
  4635. //$data['designer_id'] = $param['designer_id'];
  4636. //处理客户的扩展字段
  4637. $et = [];
  4638. $kz = CustomerPortraitField::where([['root_id', '=', $root_id], ['keyname', 'in', ['housetype_arrow']]])->select();
  4639. foreach ($kz as $key => $val) {
  4640. $et[] = ['id' => $val['id'], 'keyname' => $val['keyname'], 'value' => $param[$val['keyname']]];
  4641. }
  4642. $data['ext'] = !empty($et) ? json_encode($et) : NULL;
  4643. $ms = Customer::where([['id', '=', $param['id']], ['org_id', 'in', $orgids]])->update($data);
  4644. return json(['code' => 0, 'data' => $ms, 'msg' => '修改成功']);
  4645. }
  4646. //公海列表
  4647. public function pool_list()
  4648. {
  4649. $request = request();
  4650. $root_id = $request->empcrm->root_id;
  4651. $empid = $request->empcrm->id;
  4652. $is_manager = $request->empcrm->is_manager;
  4653. $orgids = orgSubIds($root_id);
  4654. if (!$request->isAjax()) {
  4655. $this->screen();
  4656. $week = date('w');
  4657. View::assign('week', $week);
  4658. View::assign('is_manager', $is_manager);
  4659. return View::fetch();
  4660. }
  4661. $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']);
  4662. $condition = [];
  4663. $order = isset($param['order']) ? $param['order'] : 'id desc';
  4664. $condition[] = ['employee_id', '=', NULL];
  4665. $condition[] = ['is_resource', '=', 0];
  4666. $condition[] = ['state', 'in', Customer::changeState('无效', 'chaos')];
  4667. $condition[] = ['org_id', 'in', $orgids];
  4668. if (isset($param['org']) && $param['org']) {
  4669. $condition[] = ['org_id', '=', $param['org']];
  4670. } else {
  4671. $orgids = orgSubIds($request->empcrm->org_id);
  4672. $condition[] = ['org_id', 'in', $orgids];
  4673. }
  4674. // if (isset($param['keyname']) && $param['keyname']) {
  4675. // $condition[] = ['name|community_name', 'like', '%' . trim($param['keyname']) . '%'];
  4676. // }
  4677. if (isset($param['level']) && $param['level']) {
  4678. $condition[] = ['level', 'in', explode(',', $param['level'])];
  4679. }
  4680. if (isset($param['source_id']) && $param['source_id']) {
  4681. $condition[] = ['source_id', 'in', explode(',', $param['source_id'])];
  4682. }
  4683. if (isset($param['designer_id']) && $param['designer_id']) {
  4684. $condition[] = ['designer_id', 'in', explode(',', $param['designer_id'])];
  4685. }
  4686. // 姓名筛选
  4687. if (isset($param['name']) && $param['name']) {
  4688. $condition[] = ['name', 'like', '%' . trim($param['name']) . '%'];
  4689. }
  4690. // 小区筛选
  4691. if (isset($param['community_name']) && $param['community_name']) {
  4692. $condition[] = ['community_name', 'like', '%' . trim($param['community_name']) . '%'];
  4693. }
  4694. // 手机号筛选
  4695. if (!empty($param['phone'])) {
  4696. $list = Customer::where($condition)->column('id', 'phone');
  4697. $keyCustomersId = [];
  4698. $aec = new Aec(config('app.aec_key'), config('app.aec_iv'));
  4699. foreach ($list as $k => $v) {
  4700. $v_phone = $aec->decrypt($k);
  4701. if (strpos($v_phone, $param['phone']) !== false) $keyCustomersId[] = $v;
  4702. }
  4703. unset($condition);
  4704. $condition[] = ['id', 'in', $keyCustomersId];
  4705. }
  4706. // 交房时间筛选
  4707. if (!empty($param['house_delivery_time'])) {
  4708. $house_delivery_time = explode(' - ', $param['house_delivery_time']);
  4709. $start = $house_delivery_time[0];
  4710. $end = $house_delivery_time[1] . ' 23:59:59';
  4711. $condition[] = ['house_delivery_time', '>=', $start];
  4712. $condition[] = ['house_delivery_time', '<=', $end];
  4713. }
  4714. // 面积筛选
  4715. if (!empty($param['square'])) {
  4716. $square = explode(',', $param['square']);
  4717. $square_arr = [
  4718. 1 => '(square > 1 and square <= 80)',
  4719. 2 => '(square > 81 and square <= 100)',
  4720. 3 => '(square > 101 and square <= 120)',
  4721. 4 => '(square > 121 and square <= 200)',
  4722. 5 => '(square > 201 and square <= 500)',
  4723. 6 => '(square > 501 and square <= 10000)'
  4724. ];
  4725. $squ_whereOr = null;
  4726. foreach ($square as $k => $v) {
  4727. if (!empty($square_arr[$v])) $squ_whereOr .= $square_arr[$v] . ' OR';
  4728. }
  4729. $customersList = Customer::where($condition)->where(function ($query) use ($squ_whereOr) {
  4730. $query->whereRaw(trim($squ_whereOr, ' OR'));
  4731. })->order('addtime desc')->column('id');
  4732. unset($condition);
  4733. $condition[] = ['id', 'in', $customersList];
  4734. }
  4735. // 首次到店时间筛选
  4736. if (!empty($param['fistDate'])) {
  4737. $fistDate = explode(' - ', $param['fistDate']);
  4738. $start = str_replace('/', '-', $fistDate[0]);
  4739. $end = date('Y-m-d', strtotime($fistDate[1]) + 86400);
  4740. $customersIdList = Customer::where($condition)->order('addtime desc')->column('id');
  4741. $dd_where[] = ['customer_id', 'in', $customersIdList];
  4742. $dd_where[] = ['state', 'in', CustomerVisitLog::changeState('已到店', 'chaos')];
  4743. $dd_where[] = ['confirm_date', '>=', $start];
  4744. $dd_where[] = ['confirm_date', '<=', $end];
  4745. $ddlog = CustomerVisitLog::where($dd_where)->group('customer_id')->order('addtime asc')->column('customer_id');
  4746. unset($condition);
  4747. $condition[] = ['id', 'in', $ddlog];
  4748. }
  4749. // 最后一次置为公海的业务员筛选
  4750. if (!empty($param['employee_name'])) {
  4751. $customersIdList = Customer::where($condition)->column('id');
  4752. $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');
  4753. unset($condition);
  4754. $condition[] = ['id', 'in', $pool_employee_id];
  4755. }
  4756. // 报备时间
  4757. if (!empty($param['addtime'])) {
  4758. //筛选区间范围
  4759. $date_time = explode(' - ', $param['addtime']);
  4760. $start = str_replace('/', '-', $date_time[0]);
  4761. $end = str_replace('/', '-', $date_time[1]);
  4762. $condition[] = [['addtime', 'between', [$start . ' 00:00:00', $end . ' 23:59:59']]];
  4763. }
  4764. // 查询id列表
  4765. $customerIdList = Customer::where($condition)->column('id');
  4766. if (isset($param['state']) && $param['state']) {
  4767. // 筛查状态
  4768. $customerIdList = CustomerVisitLog::where([['state', 'in', CustomerVisitLog::changeState($param['state'])], ['customer_id', 'in', $customerIdList]])->group('customer_id')->column('customer_id');
  4769. }
  4770. //回收时间
  4771. if ($param['date']) {
  4772. //筛选区间范围
  4773. $date_time = explode(' - ', $param['date']);
  4774. $start = str_replace('/', '-', $date_time[0]);
  4775. $end = str_replace('/', '-', $date_time[1]);
  4776. //$date = explode(' - ', $param['date']);
  4777. //$date = $param['date'];
  4778. $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');
  4779. }
  4780. //$pageIdList = array_slice($customerIdList, ($param['page'] - 1) * $param['limit'], $param['limit']);
  4781. $pageIdList = $customerIdList;
  4782. $condition[] = ['id', 'in', $pageIdList];
  4783. $customersStateList = CustomerVisitLog::where([['customer_id', 'in', $pageIdList]])->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray();
  4784. $customersState = [];
  4785. foreach ($customersStateList as $s) {
  4786. if (!isset($customersState[$s['customer_id']]))
  4787. $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
  4788. $customersState[$s['customer_id']]['count'] += $s['num'];
  4789. // 到店,量房,活动,定金,签单
  4790. if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num'];
  4791. elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num'];
  4792. elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num'];
  4793. elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num'];
  4794. elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num'];
  4795. }
  4796. // 上方id已分页,此处分页参数传固定1
  4797. $data = $this->poollist($condition, 1, $param['limit'], $order);
  4798. $customer_id = array_column($data, 'id');
  4799. $visitLogNum = CustomerVisitLog::where([['customer_id', 'in', $customer_id], CustomerVisitLog::changeState(['state', '=', '无效'])])->group('customer_id')->column('count(customer_id) as num', 'customer_id');
  4800. $visitLogDate = CustomerVisitLog::where([['customer_id', 'in', $customer_id], CustomerVisitLog::changeState(['state', '=', '无效'])])->order('addtime')->column('addtime', 'customer_id');
  4801. $visitLogMeet = CustomerVisitLog::where([['customer_id', 'in', $customer_id], CustomerVisitLog::changeState(['state', '=', '确认到店'])])->order('addtime')->column('addtime', 'customer_id');
  4802. foreach ($data as &$value) {
  4803. //回收次数
  4804. $value['recoverNum'] = isset($visitLogNum[$value['id']]) ? $visitLogNum[$value['id']] : 0;
  4805. //最近的回收时间
  4806. $value['recoverLatelyDate'] = isset($visitLogDate[$value['id']]) ? $visitLogDate[$value['id']] : null;
  4807. //最近的确认到店时间
  4808. $value['fistDate'] = isset($visitLogMeet[$value['id']]) ? $visitLogMeet[$value['id']] : null;
  4809. //上次跟进人信息
  4810. $value['before_pool'] = !empty($value['before_pool']) ? json_decode($value['before_pool'], true) : null;
  4811. $value[$value['id']]['stateNum'] = $customersState[$value['id']] ?? ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
  4812. // 客户最后一次置为公海的关联员工
  4813. $last_pool_employee = CustomerVisitLog::where([['customer_id', '=', $value['id']], ['state', 'in', CustomerVisitLog::changeState('无效', 'chaos')]])->with('employee')->order('addtime desc')->find();
  4814. $value['employee'] = $last_pool_employee ? $last_pool_employee->employee->name : '';
  4815. }
  4816. $sort = array_column($data, 'recoverLatelyDate');
  4817. array_multisort($sort, SORT_DESC, $data);
  4818. foreach ($condition as $k => $v) {
  4819. if ($v[0] == 'id') {
  4820. if (isset($v[2])) {
  4821. $condition[$k][2] = $customerIdList;
  4822. }
  4823. }
  4824. }
  4825. $data = array_slice($data, ($param['page'] - 1) * $param['limit'], $param['limit']);
  4826. $count = CustomerLogic::count($condition);
  4827. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  4828. }
  4829. //公海列表
  4830. public function pool_list_export()
  4831. {
  4832. $request = request();
  4833. $root_id = $request->empcrm->root_id;
  4834. $empid = $request->empcrm->id;
  4835. $is_manager = $request->empcrm->is_manager;
  4836. $orgids = orgSubIds($root_id);
  4837. $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']);
  4838. $condition = [];
  4839. $order = isset($param['order']) ? $param['order'] : 'id desc';
  4840. $condition[] = ['employee_id', '=', NULL];
  4841. $condition[] = ['is_resource', '=', 0];
  4842. $condition[] = ['state', 'in', Customer::changeState('无效', 'chaos')];
  4843. $condition[] = ['org_id', 'in', $orgids];
  4844. if (isset($param['org']) && $param['org']) {
  4845. $condition[] = ['org_id', '=', $param['org']];
  4846. } else {
  4847. $orgids = orgSubIds($request->empcrm->org_id);
  4848. $condition[] = ['org_id', 'in', $orgids];
  4849. }
  4850. // if (isset($param['keyname']) && $param['keyname']) {
  4851. // $condition[] = ['name|community_name', 'like', '%' . trim($param['keyname']) . '%'];
  4852. // }
  4853. if (isset($param['level']) && $param['level']) {
  4854. $condition[] = ['level', 'in', explode(',', $param['level'])];
  4855. }
  4856. if (isset($param['source_id']) && $param['source_id']) {
  4857. $condition[] = ['source_id', 'in', explode(',', $param['source_id'])];
  4858. }
  4859. if (isset($param['designer_id']) && $param['designer_id']) {
  4860. $condition[] = ['designer_id', 'in', explode(',', $param['designer_id'])];
  4861. }
  4862. // 姓名筛选
  4863. if (isset($param['name']) && $param['name']) {
  4864. $condition[] = ['name', 'like', '%' . trim($param['name']) . '%'];
  4865. }
  4866. // 小区筛选
  4867. if (isset($param['community_name']) && $param['community_name']) {
  4868. $condition[] = ['community_name', 'like', '%' . trim($param['community_name']) . '%'];
  4869. }
  4870. // 手机号筛选
  4871. if (!empty($param['phone'])) {
  4872. $list = Customer::where($condition)->column('id', 'phone');
  4873. $keyCustomersId = [];
  4874. $aec = new Aec(config('app.aec_key'), config('app.aec_iv'));
  4875. foreach ($list as $k => $v) {
  4876. $v_phone = $aec->decrypt($k);
  4877. if (strpos($v_phone, $param['phone']) !== false) $keyCustomersId[] = $v;
  4878. }
  4879. unset($condition);
  4880. $condition[] = ['id', 'in', $keyCustomersId];
  4881. }
  4882. // 交房时间筛选
  4883. if (!empty($param['house_delivery_time'])) {
  4884. $house_delivery_time = explode(' - ', $param['house_delivery_time']);
  4885. $start = $house_delivery_time[0];
  4886. $end = $house_delivery_time[1] . ' 23:59:59';
  4887. $condition[] = ['house_delivery_time', '>=', $start];
  4888. $condition[] = ['house_delivery_time', '<=', $end];
  4889. }
  4890. // 面积筛选
  4891. if (!empty($param['square'])) {
  4892. $square = explode(',', $param['square']);
  4893. $square_arr = [
  4894. 1 => '(square > 1 and square <= 80)',
  4895. 2 => '(square > 81 and square <= 100)',
  4896. 3 => '(square > 101 and square <= 120)',
  4897. 4 => '(square > 121 and square <= 200)',
  4898. 5 => '(square > 201 and square <= 500)',
  4899. 6 => '(square > 501 and square <= 10000)'
  4900. ];
  4901. $squ_whereOr = null;
  4902. foreach ($square as $k => $v) {
  4903. if (!empty($square_arr[$v])) $squ_whereOr .= $square_arr[$v] . ' OR';
  4904. }
  4905. $customersList = Customer::where($condition)->where(function ($query) use ($squ_whereOr) {
  4906. $query->whereRaw(trim($squ_whereOr, ' OR'));
  4907. })->order('addtime desc')->column('id');
  4908. unset($condition);
  4909. $condition[] = ['id', 'in', $customersList];
  4910. }
  4911. // 首次到店时间筛选
  4912. if (!empty($param['fistDate'])) {
  4913. $fistDate = explode(' - ', $param['fistDate']);
  4914. $start = str_replace('/', '-', $fistDate[0]);
  4915. $end = date('Y-m-d', strtotime($fistDate[1]) + 86400);
  4916. $customersIdList = Customer::where($condition)->order('addtime desc')->column('id');
  4917. $dd_where[] = ['customer_id', 'in', $customersIdList];
  4918. $dd_where[] = ['state', 'in', CustomerVisitLog::changeState('已到店', 'chaos')];
  4919. $dd_where[] = ['confirm_date', '>=', $start];
  4920. $dd_where[] = ['confirm_date', '<=', $end];
  4921. $ddlog = CustomerVisitLog::where($dd_where)->group('customer_id')->order('addtime asc')->column('customer_id');
  4922. unset($condition);
  4923. $condition[] = ['id', 'in', $ddlog];
  4924. }
  4925. // 最后一次置为公海的业务员筛选
  4926. if (!empty($param['employee_name'])) {
  4927. $customersIdList = Customer::where($condition)->column('id');
  4928. $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');
  4929. unset($condition);
  4930. $condition[] = ['id', 'in', $pool_employee_id];
  4931. }
  4932. // 报备时间
  4933. if (!empty($param['addtime'])) {
  4934. //筛选区间范围
  4935. $date_time = explode(' - ', $param['addtime']);
  4936. $start = str_replace('/', '-', $date_time[0]);
  4937. $end = str_replace('/', '-', $date_time[1]);
  4938. $condition[] = [['addtime', 'between', [$start . ' 00:00:00', $end . ' 23:59:59']]];
  4939. }
  4940. // 查询id列表
  4941. $customerIdList = Customer::where($condition)->column('id');
  4942. if (isset($param['state']) && $param['state']) {
  4943. // 筛查状态
  4944. $customerIdList = CustomerVisitLog::where([['state', 'in', CustomerVisitLog::changeState($param['state'])], ['customer_id', 'in', $customerIdList]])->group('customer_id')->column('customer_id');
  4945. }
  4946. //回收时间
  4947. if ($param['date']) {
  4948. //筛选区间范围
  4949. $date_time = explode(' - ', $param['date']);
  4950. $start = str_replace('/', '-', $date_time[0]);
  4951. $end = str_replace('/', '-', $date_time[1]);
  4952. //$date = explode(' - ', $param['date']);
  4953. //$date = $param['date'];
  4954. $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');
  4955. }
  4956. //$pageIdList = array_slice($customerIdList, ($param['page'] - 1) * $param['limit'], $param['limit']);
  4957. $pageIdList = $customerIdList;
  4958. $condition[] = ['id', 'in', $pageIdList];
  4959. $customersStateList = CustomerVisitLog::where([['customer_id', 'in', $pageIdList]])->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray();
  4960. $customersState = [];
  4961. foreach ($customersStateList as $s) {
  4962. if (!isset($customersState[$s['customer_id']]))
  4963. $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
  4964. $customersState[$s['customer_id']]['count'] += $s['num'];
  4965. // 到店,量房,活动,定金,签单
  4966. if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num'];
  4967. elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num'];
  4968. elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num'];
  4969. elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num'];
  4970. elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num'];
  4971. }
  4972. // 上方id已分页,此处分页参数传固定1
  4973. $data = $this->poollista($condition, 1, $param['limit'], $order);
  4974. $customer_id = array_column($data, 'id');
  4975. $visitLogNum = CustomerVisitLog::where([['customer_id', 'in', $customer_id], CustomerVisitLog::changeState(['state', '=', '无效'])])->group('customer_id')->column('count(customer_id) as num', 'customer_id');
  4976. $visitLogDate = CustomerVisitLog::where([['customer_id', 'in', $customer_id], CustomerVisitLog::changeState(['state', '=', '无效'])])->order('addtime')->column('addtime', 'customer_id');
  4977. $visitLogMeet = CustomerVisitLog::where([['customer_id', 'in', $customer_id], CustomerVisitLog::changeState(['state', '=', '确认到店'])])->order('addtime')->column('addtime', 'customer_id');
  4978. foreach ($data as &$value) {
  4979. //回收次数
  4980. $value['recoverNum'] = isset($visitLogNum[$value['id']]) ? $visitLogNum[$value['id']] : 0;
  4981. //最近的回收时间
  4982. $value['recoverLatelyDate'] = isset($visitLogDate[$value['id']]) ? $visitLogDate[$value['id']] : null;
  4983. //最近的确认到店时间
  4984. $value['fistDate'] = isset($visitLogMeet[$value['id']]) ? $visitLogMeet[$value['id']] : null;
  4985. //上次跟进人信息
  4986. $value['before_pool'] = !empty($value['before_pool']) ? json_decode($value['before_pool'], true) : null;
  4987. $value[$value['id']]['stateNum'] = $customersState[$value['id']] ?? ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
  4988. // 客户最后一次置为公海的关联员工
  4989. $last_pool_employee = CustomerVisitLog::where([['customer_id', '=', $value['id']], ['state', 'in', CustomerVisitLog::changeState('无效', 'chaos')]])->with('employee')->order('addtime desc')->find();
  4990. $value['employee'] = $last_pool_employee ? $last_pool_employee->employee->name : '';
  4991. }
  4992. $sort = array_column($data, 'recoverLatelyDate');
  4993. array_multisort($sort, SORT_DESC, $data);
  4994. foreach ($condition as $k => $v) {
  4995. if ($v[0] == 'id') {
  4996. if (isset($v[2])) {
  4997. $condition[$k][2] = $customerIdList;
  4998. }
  4999. }
  5000. }
  5001. //$data = array_slice($data, ($param['page'] - 1) * $param['limit'], $param['limit']);
  5002. //$count = CustomerLogic::count($condition);
  5003. $new = [];
  5004. foreach ($data as $key => $val) {
  5005. $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']];
  5006. }
  5007. $header = ["A1" => "客户姓名", "B1" => "手机号", "C1" => "小区名称", "D1" => "所属员工", "E1" => "客户等级", "F1" => '来源渠道', "G1" => '面积', "H1" => '设计师', "I1" => '交房时间', "J1" => '注销时间', "K1" => '报备时间', "L1" => '状态', "M1" => '首次到店'];
  5008. $fileName = date('Y-m-d H:i:s') . '公共库导出';
  5009. $this->getExport($header, true, $new, $fileName);
  5010. //return json(['code' => 0, 'data' => $data]);
  5011. }
  5012. // 导出
  5013. public function getExport($header = [], $type = false, $data = [], $fileName)
  5014. {
  5015. // 实例化类
  5016. $preadsheet = new Spreadsheet();
  5017. // 创建sheet
  5018. $sheet = $preadsheet->getActiveSheet();
  5019. // 循环设置表头数据
  5020. foreach ($header as $k => $v) {
  5021. $sheet->setCellValue($k, $v);
  5022. }
  5023. // 生成数据
  5024. $sheet->fromArray($data, null, "A2");
  5025. // 样式设置
  5026. $sheet->getDefaultColumnDimension()->setWidth(12);
  5027. // 设置下载与后缀
  5028. if ($type) {
  5029. header("Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  5030. $type = "Xlsx";
  5031. $suffix = "xlsx";
  5032. } else {
  5033. header("Content-Type:application/vnd.ms-excel");
  5034. $type = "Xls";
  5035. $suffix = "xls";
  5036. }
  5037. ob_end_clean(); //清楚缓存区
  5038. // 激活浏览器窗口
  5039. header("Content-Disposition:attachment;filename=$fileName.$suffix");
  5040. //缓存控制
  5041. header("Cache-Control:max-age=0");
  5042. // 调用方法执行下载
  5043. $writer = IOFactory::createWriter($preadsheet, $type);
  5044. // 数据流
  5045. $writer->save("php://output");
  5046. }
  5047. //查询公海
  5048. public function poollista($condition = [], $page, $limit, $order)
  5049. {
  5050. $page = intval($page);
  5051. $limit = intval($limit);
  5052. $data = Customer::with(['source' => function ($query) {
  5053. $query->field('id,source');
  5054. }])->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')
  5055. ->where($condition)
  5056. ->select()->toArray();
  5057. foreach ($data as &$item) {
  5058. $item['pool_name'] = Org::where('id', $item['org_id'])->column('name');
  5059. }
  5060. return $data;
  5061. }
  5062. //查询公海
  5063. public function poollist($condition = [], $page, $limit, $order)
  5064. {
  5065. $page = intval($page);
  5066. $limit = intval($limit);
  5067. $data = Customer::with(['source' => function ($query) {
  5068. $query->field('id,source');
  5069. }])->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')
  5070. ->where($condition)
  5071. ->select()->toArray();
  5072. foreach ($data as &$item) {
  5073. $item['pool_name'] = Org::where('id', $item['org_id'])->column('name');
  5074. if (!$item['employee_id']) {
  5075. $item['phone'] = substr_replace($item['phone'], '******', 3, 6);
  5076. }
  5077. }
  5078. return $data;
  5079. }
  5080. //客户状态详情
  5081. public function customer_detail()
  5082. {
  5083. $request = request();
  5084. $root_id = $request->empcrm->root_id;
  5085. $empid = $request->empcrm->id;
  5086. $org_employee = [$empid];
  5087. // if (!$request->isAjax()) {
  5088. // $this->screen();
  5089. // return View::fetch();
  5090. // }
  5091. $this->screen();
  5092. $param = $request->only(['id']);
  5093. $data = $this->seLemp($param['id'], $empid);
  5094. //意向风格
  5095. $decostyle_list = Decostyle::field('id,name,root_id as pid')->where(['root_id' => $root_id])->where([['type', '=', 0]])->order('id desc')->select()->toArray();
  5096. View::assign('decostyle_list', $decostyle_list);
  5097. //意向产品
  5098. $product = CustomerPackage::field('id, name, total_price')->where(['root_id' => $root_id])->select();
  5099. View::assign('product', $product);
  5100. View::assign('data', $data);
  5101. View::assign('empname', $request->empcrm->name);
  5102. // var_dump($data->toArray());
  5103. // exit;
  5104. return View::fetch();
  5105. }
  5106. //公用查询客户详情
  5107. public function seLemp($id, $empid)
  5108. {
  5109. $request = request();
  5110. $root_id = $request->empcrm->root_id;
  5111. $empid = $request->empcrm->id;
  5112. $orgids = orgSubIds($root_id);
  5113. $is_manager = $request->empcrm->is_manager;
  5114. $where = [
  5115. ['id', '=', $id],
  5116. ['state', '<>', '无效']
  5117. ];
  5118. // $condition1 = [
  5119. // ['employee_id', '=', $empid],
  5120. // ['id', '=', $id]
  5121. // ];
  5122. // $condition2 = [
  5123. // ['designer_id', '=', $empid],
  5124. // ['id', '=', $id]
  5125. // ];
  5126. // $condition3[] = ['assigned_personnel', 'find in set', $empid];
  5127. // $condition3[] = ['id', '=', $id];
  5128. // // 或者是公司领导看员工的客户
  5129. // if (isset($request->empcrm->is_manager) && $request->empcrm->is_manager) {
  5130. // $team_orgs = (new Org())->getChildOrg($request->empcrm->org_id);
  5131. // $org_type = Org::where('id',$request->empcrm->org_id)->value('org_type');
  5132. // if ($org_type == 1) {
  5133. // $condition4 = [
  5134. // ['org_id', 'in', $team_orgs],
  5135. // ['id', '=', $id]
  5136. // ];
  5137. // $orCondition = [$condition1, $condition2, $condition3,$condition4];
  5138. // } else {
  5139. // $empIdList = Employee::where([['org_id', 'in', $team_orgs]])->column('id');
  5140. // $condition4 = [
  5141. // ['designer_id', 'in', $empIdList],
  5142. // ['id', '=', $id]
  5143. // ];
  5144. // $condition5 = [
  5145. // ['employee_id', 'in', $empIdList],
  5146. // ['id', '=', $id]
  5147. // ];
  5148. // $condition6 = [
  5149. // ['org_id', 'in', $team_orgs],
  5150. // ['id', '=', $id]
  5151. // ];
  5152. // $orCondition = [$condition1, $condition2, $condition4, $condition5,$condition6];
  5153. // }
  5154. // } else {
  5155. // $orCondition = [$condition1, $condition2,$condition3];
  5156. // }
  5157. // if(!empty($param['org_id']) && in_array($param['org_id'],$orgids)) $orCondition[] = [['org_id', 'in', $orgids],['id', '=', $id]];
  5158. //$orCondition = [$condition1, $condition2];
  5159. //增加查询是否要显示修改客户扩展字段信息功能
  5160. $showEmpVistlog = Setting::where([['root_id', '=', $root_id], ['name', '=', 'showEmpVistlog']])->findOrEmpty();
  5161. $visit = !$showEmpVistlog->isEmpty() ? (int)$showEmpVistlog->content : 0;
  5162. $vlg_where[] = $visit ? ['save_portrait_field', 'NULL', null] : ['id', '>', 0];
  5163. $data = Customer::with(['source', 'designer' => function ($query) {
  5164. $query->field('id,name,headimgurl,org_id,uid');
  5165. }, 'employee' => function ($query) {
  5166. $query->withField(['id', 'name']);
  5167. }, 'visitLog' => function ($query) use ($vlg_where) {
  5168. $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');
  5169. }])->where($where)->find();
  5170. $empIdList = explode(',', $data->employee_id . ',' . $data->employee_id . ',' . $data->assigned_personnel);
  5171. $empIdList = array_unique(array_filter($empIdList));
  5172. if (!empty($data) && !in_array($empid, $empIdList)) {
  5173. if ($is_manager == 1) {
  5174. $team_orgs = orgSubIds($request->empcrm->org_id);
  5175. $yuangong = Employee::where([['org_id', 'in', $team_orgs], ['uid', '>', 0], ['id', 'in', $empIdList]])->count();
  5176. if ($yuangong == 0) $data = null;
  5177. } else {
  5178. $data = null;
  5179. }
  5180. }
  5181. if (empty($data)) {
  5182. echo json_encode(['code' => 1, 'msg' => '数据不存在']);
  5183. exit;
  5184. }
  5185. $data['daochang'] = CustomerVisitLog::where([['customer_id', '=', $id], ['customer_employee_id', '=', $data['employee_id']], CustomerVisitLog::changeState(['state', '=', '已到访'])])->order('addtime desc')->find();
  5186. $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();
  5187. $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();
  5188. $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')");
  5189. $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')");
  5190. $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();
  5191. $data['package_name'] = $data['package_id'] ? CustomerPackage::where('id', $data['package_id'])->value('name') : '';
  5192. $data['deco_style_name'] = $data['deco_style'] ? Decostyle::where('id', $data['deco_style'])->value('name') : '';
  5193. $data['company_name'] = Company::where('root_id', $root_id)->value('company_name');
  5194. $data['add_wechat'] = '否';
  5195. $data['housetype'] = $data['unit_number'] = $data['subjection_plan'] = null;
  5196. $data['sign_time'] = '';
  5197. if (!empty($data['ext'])) {
  5198. $field = $this->get_portrait_field($data);
  5199. // var_dump($field);
  5200. // exit;
  5201. foreach ($field as $key => $val) {
  5202. if ($val['keyname'] == 'wechat' && !empty($val['value'])) {
  5203. $data['add_wechat'] = CustomerPortraitFieldSelect::where('id', $val['value'])->value('name');
  5204. }
  5205. if ($val['keyname'] == 'housetype_arrow' && !empty($val['value'])) {
  5206. $data['housetype'] = CustomerPortraitFieldSelect::where('id', $val['value'])->value('name');
  5207. }
  5208. if ($val['keyname'] == 'subjection_plan' && !empty($val['value'])) {
  5209. $data['subjection_plan'] = CustomerPortraitFieldSelect::where('id', $val['value'])->value('name');
  5210. }
  5211. if ($val['keyname'] == 'unit_number' && !empty($val['value'])) {
  5212. $data['unit_number'] = $val['value'];
  5213. }
  5214. if ($val['keyname'] == 'sign_name' && !empty($val['value'])) {
  5215. $data['sign_time'] = $val['value'];
  5216. }
  5217. }
  5218. }
  5219. $data['daochang'] = !empty($data['daochang']) ? json_decode($data['daochang'], true) : null;
  5220. $data['deposit'] = !empty($data['deposit']) ? json_decode($data['deposit'], true) : null;
  5221. $data['sign'] = !empty($data['sign']) ? json_decode($data['sign'], true) : null;
  5222. $data['assigned_personnel'] = !empty($data['designer_id']) ? $data['assigned_personnel'] . ',' . $data['designer_id'] : $data['assigned_personnel'];
  5223. return $data;
  5224. }
  5225. //新获取客户扩展字段
  5226. public function get_portrait_field($cusdata)
  5227. {
  5228. $request = request();
  5229. $root_id = $request->empcrm->root_id;
  5230. $empid = $request->empcrm->id;
  5231. $w[] = ['root_id', '=', $root_id];
  5232. $list = CustomerPortraitField::with(['select'])->where([['root_id', '=', $root_id], ['pid', '<>', 0]])->select()->toArray();
  5233. $cuslog = new CustomerLogic;
  5234. $new = $list;
  5235. if (!empty($cusdata['ext']) && $cusdata['ext'] != 'null') {
  5236. $extdata = json_decode($cusdata['ext'], true);
  5237. if (isset($extdata['ext1'])) {
  5238. $new = $cuslog->old_data_saves($cusdata, $list);
  5239. } else {
  5240. $new = $cuslog->new_data_saves($cusdata, $extdata, $list);
  5241. }
  5242. }
  5243. return $new;
  5244. //return json(['code' => 0, 'data' => $new]);
  5245. }
  5246. //添加客户的回访记录
  5247. public function addVisitlog()
  5248. {
  5249. //$token = $this->request->token;
  5250. $request = request();
  5251. $root_id = $request->empcrm->root_id;
  5252. $empid = $request->empcrm->id;
  5253. $is_manager = $request->empcrm->is_manager;
  5254. //防止后台切换部门
  5255. $token['org_id'] = Employee::where('id', $empid)->value('org_id');
  5256. $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' => '']);
  5257. $param['confirm_date'] = $param['confirm_date'] ? $param['confirm_date'] . ' ' . date('H:i:s') : date('Y-m-d H:i:s');
  5258. $employeeTypeName = $this->request->empcrm['org_type'] == 2 ? '设计师' : '销售';
  5259. $employeeType = $this->request->empcrm['org_type'] == 2 ? 'designer_id' : 'employee_id';
  5260. if ($is_manager == 1) $employeeTypeName = '部门领导';
  5261. //2022-10-04 逻辑修改,无效时无效原因必传remark
  5262. //if ($param['state'] == '无效' && empty($param['remark'])) return json(['code' => 1, 'msg' => '请填写无效原因']);
  5263. // 在跟进之前,增加了设计师必填判断,得先查一下设置
  5264. $state = Customer::changeState($param['state']);
  5265. $stateCheck = ['已到店' => 'daodian', '已到场' => 'daochang', '已量房' => 'liangfang', '已交定' => 'qiandan', '已签单' => 'zhuandan'];
  5266. if (in_array($state, ['已到店', '已到场', '已量房', '已交定', '已签单'])) {
  5267. $need_designer[] = ['root_id', '=', $root_id];
  5268. $need_designer[] = ['name', '=', 'add_visit_log_need_designer'];
  5269. $need_designer_setting = Setting::where($need_designer)->findOrEmpty();
  5270. if (!$need_designer_setting->isEmpty()) {
  5271. $need_state = explode(',', $need_designer_setting['content']);
  5272. $input_state = $stateCheck[$state] ?? '';
  5273. if (!empty($input_state) && in_array($input_state, $need_state)) {
  5274. // 需要验证
  5275. $customer_find = Customer::find($param['customer_id']);
  5276. if (!empty($customer_find)) {
  5277. if (empty($customer_find['assigned_personnel'])) {
  5278. return json(['code' => 1, 'msg' => '请先指派设计师']);
  5279. }
  5280. $assigned_personnel = explode(',', $customer_find['assigned_personnel']);
  5281. $assigned_personnel_employee_org = Employee::where('id', 'in', $assigned_personnel)->column('org_id');
  5282. $assigned_personnel_org_type = Org::where('id', 'in', $assigned_personnel_employee_org)->column('org_type');
  5283. if (!in_array(2, $assigned_personnel_org_type)) {
  5284. return json(['code' => 1, 'msg' => '请先指派设计师']);
  5285. }
  5286. }
  5287. }
  5288. }
  5289. }
  5290. //2022-10-27 逻辑修改,跟进保护优化 如果后台设置了重复客户跟进保护规则 跟进到保护状态后其他业务员的此重复客户就不能在跟进了,但是可以置为无效
  5291. $forbiddenState = $this->forbiddenState($param['customer_id'], $param['state']);
  5292. if ($forbiddenState != false) return json(['code' => 1, 'msg' => $forbiddenState]);
  5293. if ($is_manager == 1) {
  5294. $team_orgs = orgSubIds($request->empcrm->org_id);
  5295. $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
  5296. } else {
  5297. $org_employee = [$empid];
  5298. }
  5299. $where = [
  5300. ['id', '=', $param['customer_id']],
  5301. ['state', '<>', '无效']
  5302. ];
  5303. $had = Customer::where($where)->where(function ($query) use ($org_employee) {
  5304. foreach ($org_employee as $k => $v) {
  5305. $query->whereOr([['employee_id|designer_id|assigned_personnel', 'find in set', $v]]);
  5306. }
  5307. })->find();
  5308. if (!$had) return json(['code' => 1, 'msg' => '添加失败,数据不存在']);
  5309. $isAllManagerOperateAble = Setting::where(['root_id' => $root_id, 'name' => 'is_all_manager_oprate_able'])->value('content');
  5310. $isEndOrg = Org::where([['pid', '=', $request->empcrm->org_id]])->count();
  5311. if ($had['employee_id'] !== $empid) {
  5312. if ($is_manager && in_array($had['org_id'], $team_orgs)) {
  5313. if (!$isAllManagerOperateAble && $isEndOrg) return json(['code' => 1, 'msg' => '添加失败,无权限']);
  5314. }
  5315. }
  5316. //签单之后不能置为无效
  5317. if ($param['state'] == '无效') {
  5318. $is_orders = CustomerVisitLog::where([['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')], ['customer_id', '=', $had->id]])->count();
  5319. if ($is_orders) return json(['code' => 1, 'msg' => '该客户已签单,不能置为无效']);
  5320. }
  5321. //改为有效之后不能再改为待确认
  5322. if ($param['state'] == '待确认') {
  5323. if ($had->state != '待确认') return json(['code' => 1, 'msg' => '该客户已标记为有效,不能修改为待确认']);
  5324. }
  5325. // 撞单机制
  5326. if ($param['state'] == '未到访') {
  5327. // 同部门能否重复录入开关设置
  5328. $empcrm_repeat[] = ['root_id', '=', $root_id];
  5329. $empcrm_repeat[] = ['name', '=', 'empcrm_customer_repeat'];
  5330. $repeat_setting = Setting::where($empcrm_repeat)->findOrEmpty();
  5331. //判断同部门
  5332. if (!$repeat_setting->isEmpty()) {
  5333. $repeat_org = explode(',', $repeat_setting['content']);
  5334. if (in_array($this->request->empcrm['org_id'], $repeat_org)) {
  5335. // 需要验证
  5336. $not_sure = Customer::changeState('待确认', 'chaos');
  5337. $wuxiao = Customer::changeState('无效', 'chaos');
  5338. $phones_arr = array_filter([$had['phone'], $had['phone1'], $had['phone2']]);
  5339. $repeat_where[] = ['phone|phone1|phone2', 'in', $phones_arr];
  5340. $repeat_where[] = ['org_id', 'in', $repeat_org];
  5341. $repeat_where[] = ['state', 'not in', array_merge($not_sure, $wuxiao)];
  5342. $customerExist = Customer::where($repeat_where)->field('id,employee_id');
  5343. if (!empty($customerExist)) {
  5344. foreach ($customerExist as $ex) {
  5345. if ($ex['employee_id'] != $empid && $ex['id'] != $param['customer_id']) {
  5346. $have_emp = Employee::where('id', '=', $ex['employee_id'])->value('name');
  5347. return json(['code' => 2, 'msg' => '该客户与员工' . $have_emp . '撞单,无法标记有效']);
  5348. }
  5349. }
  5350. }
  5351. }
  5352. }
  5353. }
  5354. // 如果是预约添加预约记录
  5355. $stateType = [
  5356. 1 => '到店',
  5357. 2 => '到场',
  5358. 3 => '量房'
  5359. ];
  5360. $yylf_state = CustomerVisitLog::changeState('预约量房', 'chaos');
  5361. $lf_state = CustomerVisitLog::changeState('已量房', 'chaos');
  5362. $yydc_state = CustomerVisitLog::changeState('预约活动', 'chaos');
  5363. $dc_state = CustomerVisitLog::changeState('已到场', 'chaos');
  5364. $yydd_state = CustomerVisitLog::changeState('预约到店', 'chaos');
  5365. $dd_state = CustomerVisitLog::changeState('已到店', 'chaos');
  5366. $check_state = array_merge($yylf_state, $lf_state, $yydc_state, $dc_state, $yydd_state, $dd_state);
  5367. if (in_array($param['state'], $check_state)) {
  5368. // 保护设置
  5369. $sub_setting = Setting::where([['root_id', '=', $root_id], ['name', '=', 'subscribe_protected']])->findOrEmpty();
  5370. if (!$sub_setting->isEmpty()) { // 有设置
  5371. $setting_content = json_decode($sub_setting['content'], true);
  5372. $org_ids = orgSubIds($root_id);
  5373. foreach ($setting_content as $k_s => $v_s) {
  5374. if (!empty($v_s['state']) && !empty($v_s['day']) && $v_s['state'] == 1) {
  5375. $continue = false;
  5376. $sub_state = 0;
  5377. $errmsg = '该客户在保护期内,无法提交';
  5378. $check_log = false;
  5379. $check_where = [];
  5380. switch ($k_s) {
  5381. case 'liangfang':
  5382. $sub_state = 3;
  5383. $visit_state = 1;
  5384. $check_log = true;
  5385. $check_where[] = ['org_id', 'in', $org_ids];
  5386. $check_where[] = ['addtime', '>', date('Y-m-d H:i:s', time() - 24 * 3600 * $v_s['day'])];
  5387. $check_where[] = ['employee_id', '<>', $empid];
  5388. $check_where[] = ['state', 'in', $lf_state];
  5389. break;
  5390. case 'yliangfang': //预约量房
  5391. $sub_state = 3;
  5392. $visit_state = 0;
  5393. break;
  5394. case 'daodian':
  5395. $sub_state = 1;
  5396. $visit_state = 1;
  5397. $check_log = true;
  5398. $check_where[] = ['org_id', 'in', $org_ids];
  5399. $check_where[] = ['addtime', '>', date('Y-m-d H:i:s', time() - 24 * 3600 * $v_s['day'])];
  5400. $check_where[] = ['employee_id', '<>', $empid];
  5401. $check_where[] = ['state', 'in', $dd_state];
  5402. break;
  5403. case 'ydaodian':
  5404. $sub_state = 1;
  5405. $visit_state = 0;
  5406. break;
  5407. case 'daochang':
  5408. $sub_state = 2;
  5409. $visit_state = 1;
  5410. $check_log = true;
  5411. $check_where[] = ['org_id', 'in', $org_ids];
  5412. $check_where[] = ['addtime', '>', date('Y-m-d H:i:s', time() - 24 * 3600 * $v_s['day'])];
  5413. $check_where[] = ['employee_id', '<>', $empid];
  5414. $check_where[] = ['state', 'in', $dc_state];
  5415. break;
  5416. case 'ydaochang':
  5417. $sub_state = 2;
  5418. $visit_state = 0;
  5419. break;
  5420. default:
  5421. $continue = true;
  5422. break;
  5423. }
  5424. if ($continue) continue;
  5425. // “确认” 不受预约的限制
  5426. if (in_array($k_s, ['yliangfang', 'ydaodian', 'ydaochang']) && in_array($param['state'], array_merge($lf_state, $dd_state, $dc_state))) {
  5427. continue;
  5428. }
  5429. if (in_array($param['state'], array_merge($lf_state, $dd_state, $dc_state))) {
  5430. $errmsg = '该客户在保护期内,无法提交确认';
  5431. } elseif (in_array($param['state'], array_merge($yylf_state, $yydd_state, $yydc_state))) {
  5432. $errmsg = '该客户在保护期内,无法预约';
  5433. }
  5434. // 查询保护时间段内添加的预约量房记录
  5435. unset($v_where);
  5436. $v_where[] = ['org_id', 'in', $org_ids];
  5437. $v_where[] = ['addtime', '>', date('Y-m-d H:i:s', time() - 24 * 3600 * $v_s['day'])];
  5438. $v_where[] = ['employee_id', '<>', $empid];
  5439. $v_where[] = ['type', '=', $sub_state];
  5440. $v_where[] = ['state', '=', $visit_state];
  5441. $sub_list = CustomersSubscribe::where($v_where)->select()->toArray();
  5442. if (!empty($sub_list)) {
  5443. $sub_list_ids = array_column($sub_list, 'customer_id');
  5444. $sub_employee_ids = array_column($sub_list, 'employee_id');
  5445. $sub_customers = Customer::where([['id', 'in', $sub_list_ids], ['employee_id', 'in', $sub_employee_ids]])->select()->toArray();
  5446. foreach ($sub_customers as $v) {
  5447. if (in_array($had->phone, [$v['phone'], $v['phone1'], $v['phone2']]) && $v['id'] != $had['id']) {
  5448. return json(['code' => 1, 'msg' => $errmsg]);
  5449. }
  5450. if (!empty($had->phone1) && in_array($had->phone1, [$v['phone'], $v['phone1'], $v['phone2']]) && $v['id'] != $had['id']) {
  5451. return json(['code' => 1, 'msg' => $errmsg]);
  5452. }
  5453. if (!empty($had->phone2) && in_array($had->phone2, [$v['phone'], $v['phone1'], $v['phone2']]) && $v['id'] != $had['id']) {
  5454. return json(['code' => 1, 'msg' => $errmsg]);
  5455. }
  5456. }
  5457. }
  5458. if ($check_log) {
  5459. $log_list = CustomerVisitLog::where($check_where)->select()->toArray();
  5460. $log_list_ids = array_column($log_list, 'customer_id');
  5461. $log_employee_ids = array_column($log_list, 'employee_id');
  5462. $log_customers = Customer::where([['id', 'in', $log_list_ids], ['employee_id', 'in', $log_employee_ids]])->select()->toArray();
  5463. foreach ($log_customers as $v) {
  5464. if (in_array($had->phone, [$v['phone'], $v['phone1'], $v['phone2']]) && $v['id'] != $had['id']) {
  5465. return json(['code' => 1, 'msg' => $errmsg]);
  5466. }
  5467. if (!empty($had->phone1) && in_array($had->phone1, [$v['phone'], $v['phone1'], $v['phone2']]) && $v['id'] != $had['id']) {
  5468. return json(['code' => 1, 'msg' => $errmsg]);
  5469. }
  5470. if (!empty($had->phone2) && in_array($had->phone2, [$v['phone'], $v['phone1'], $v['phone2']]) && $v['id'] != $had['id']) {
  5471. return json(['code' => 1, 'msg' => $errmsg]);
  5472. }
  5473. }
  5474. }
  5475. }
  5476. }
  5477. }
  5478. }
  5479. // 更改客户信息
  5480. $state = Customer::changeState($param['state']);
  5481. $orders = false;
  5482. //签单之后不能签单交定,可以跟进
  5483. if ($state == '已签单') {
  5484. $is = CustomerVisitLog::where([['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')], ['customer_id', '=', $had->id]])->count();
  5485. if ($is) {
  5486. return $this->updateMoney($param);
  5487. // Db::rollback();
  5488. // return json(['code' => 1, 'msg' => '该客户' . $state]);
  5489. }
  5490. $orders = true;
  5491. } elseif ($state == '已交定') {
  5492. $is = CustomerVisitLog::where([['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')], ['customer_id', '=', $had->id]])->count();
  5493. if ($is) {
  5494. Db::rollback();
  5495. return json(['code' => 1, 'msg' => '该客户已签单']);
  5496. }
  5497. $orders = true;
  5498. }
  5499. Db::startTrans();
  5500. //处理房屋状态
  5501. if (!empty($param['house_status'])) {
  5502. $had->house_status = $param['house_status'];
  5503. }
  5504. //如果是分派的客户第一次跟进直接修改分派信息为空
  5505. if (!empty($had->transfer_info)) {
  5506. $had->transfer_info = NULL;
  5507. }
  5508. //如果是第一次有效修改有效时间
  5509. if ($state == '未到访' && $had->state == '待确认') {
  5510. $had->valid_time = !empty($param['confirm_date']) ? $param['confirm_date'] : date('Y-m-d H:i:s');
  5511. }
  5512. if (!empty($state)) {
  5513. //if(!in_array($had->state,Customer::changeState('待确认','chaos'),true)){
  5514. $had->state = $state;
  5515. //}
  5516. }
  5517. //客户名称
  5518. if (!empty($param['name'])) {
  5519. $had->name = $param['name'];
  5520. }
  5521. //是否设置客户转介绍新客户
  5522. // if($param['introduce']){
  5523. // $had->introduce = 1;
  5524. // }
  5525. //设计师
  5526. if (!empty($param['designer_id']) && empty($had->designer_id)) {
  5527. $limitNum = Setting::where(['root_id' => $root_id, 'name' => 'designerNum'])->value('content');
  5528. if ($limitNum != 0) {
  5529. //获取当月时间跨度
  5530. $start_time = date('Y-m') . '-01 00:00:00';
  5531. $end_time = date('Y-m-d', strtotime("$start_time +1 month -1 day")) . ' 23:59:59';
  5532. $whered[] = ['addtime', 'between', [strtotime($start_time), strtotime($end_time)]];
  5533. $whered[] = ['employee_id', '=', $param['designer_id']];
  5534. $hads = CustomerSharing::field('employee_id,count(customer_id) as num')->group('employee_id')->where($whered)->having("num >= $limitNum")->select();
  5535. if (!$hads->isEmpty()) {
  5536. Db::rollback();
  5537. return json(['code' => 1, 'msg' => '指派的设计师已无指派名额,请刷新后再试']);
  5538. }
  5539. }
  5540. $had->designer_id = $param['designer_id'];
  5541. }
  5542. $had->revisit_time = !empty($param['next_contact_date']) ? $param['next_contact_date'] . ' 00:00:00' : null;
  5543. $had->last_contact_date = date('Y-m-d', time());
  5544. $had->updatetime = date('Y-m-d H:i:s', time());
  5545. if ($param['aid'] && $param['state'] == '预约活动') $had->aid = $param['aid'];
  5546. $had->fresh = 0;
  5547. // 交定产品
  5548. $dingMsg = '';
  5549. if ($had->state == '已交定') {
  5550. empty($param['money']) ?: $had->deposit_money = $param['money'];
  5551. if (!empty($param['package'])) {
  5552. $package = CustomerPackage::where([['root_id', '=', $root_id], ['id', 'in', $param['package']]])->column('name', 'id');
  5553. if (!$package) return json(['code' => 1, 'msg' => '客户产品不存在']);
  5554. $had->package_id = implode(',', array_keys($package));
  5555. $dingMsg = '交定金额:' . $param['money'] . '元,选择产品:' . implode(',', $package);
  5556. }
  5557. }
  5558. if ($had->state == '已签单' && !empty($param['money'])) $had->signed_money = $param['money'];
  5559. $had->save();
  5560. // 更新客户保护期
  5561. if ($param['state'] !== '无效' && !empty($state)) {
  5562. $log_state = CustomerVisitLog::changeState($state, 'chaos');
  5563. $v_where[] = ['employee_id', '=', $empid];
  5564. $v_where[] = ['state', 'in', $log_state];
  5565. $v_where[] = ['customer_id', '=', $param['customer_id']];
  5566. if (!empty($had['employee_time'])) {
  5567. $v_where[] = ['addtime', '>', $had['employee_time']];
  5568. }
  5569. $v_count = CustomerVisitLog::where($v_where)->count();
  5570. //查看此状态的跟踪记录,如果有数据,说明此客户在此员工这里已经经历过此状态,不再更新保护期
  5571. if (!$v_count) {
  5572. Customer::changeProtectedTo($had->id, $root_id);
  5573. }
  5574. }
  5575. // 添加确定消息内容
  5576. $confirmMsg = '';
  5577. if (!empty($param['confirm_date'])) {
  5578. $confirmMsg = ', 时间:' . $param['confirm_date'];
  5579. }
  5580. // 备注
  5581. $employeeName = Employee::where(['id' => $empid])->value('name');
  5582. $remark = $param['remark'];
  5583. if (empty($remark)) {
  5584. if (empty($state))
  5585. $remark = $employeeTypeName . $employeeName . '对客户进行了回访';
  5586. else
  5587. $remark = $employeeTypeName . $employeeName . '更新客户状态为' . $state . $confirmMsg . ',' . $dingMsg;
  5588. }
  5589. $valid_status = NULL;
  5590. //2022-10-04 逻辑修改,无效时增加无效原因
  5591. if ($param['state'] == '无效' && !empty($param['remark'])) {
  5592. // $remark = $employeeTypeName . $employeeName . '更新客户状态为' . $state . $confirmMsg . ',原因是:' . $param['remark'] . $dingMsg;
  5593. $valid_status = !empty($had->valid_time) ? 2 : 1;
  5594. }
  5595. //如果置为无效时,查询客户的有效状态
  5596. // 添加追踪记录
  5597. $visitLog = [
  5598. 'customer_id' => $had->id,
  5599. 'type' => $param['type'],
  5600. 'next_contact_date' => !empty($param['next_contact_date']) ? $param['next_contact_date'] : null,
  5601. 'employee_id' => $empid,
  5602. 'user_id' => $this->request->empcrm['uid'],
  5603. 'remark' => $remark,
  5604. 'state' => $param['state'],
  5605. 'weixin_media' => $param['weixin_media'],
  5606. 'media_id' => $param['media_id'],
  5607. 'starts' => $param['starts'],
  5608. 'money' => $param['money'],
  5609. 'confirm_date' => $param['confirm_date'],
  5610. 'org_id' => $this->request->empcrm['org_id'],
  5611. 'aid' => $param['aid'] ?: $had->aid,
  5612. 'weixin_media1' => $param['weixin_media1'],
  5613. 'media_id1' => $param['media_id1'],
  5614. 'customer_employee_id' => $had->employee_id,
  5615. 'customer_org_id' => Employee::where('id', $had->employee_id)->value('org_id'),
  5616. 'img' => $param['img'],
  5617. 'img1' => $param['img1'],
  5618. 'number_of_visitors' => $param['number_of_visitors'],
  5619. 'measure_room_img_type' => $param['measure_room_img_type'],
  5620. 'deposit_mode' => $param['deposit_mode'],
  5621. 'document_field' => $param['document_field'],
  5622. 'document_name' => $param['document_name']
  5623. ];
  5624. $returnvisit = ['customer_org_id' => $visitLog['customer_org_id']];
  5625. //2022-10-28 增加交定和签单凭证 凭证为图片
  5626. $deliverySignVoucher = request()->only(['delivery_media_id' => '', 'delivery_weixin_media' => '', 'sign_media_id' => '', 'sign_weixin_media' => '', 'delivery_img' => '', 'sign_img' => '']);
  5627. $visitLog = $visitLog + $deliverySignVoucher;
  5628. empty($param['img']) ?: $visitLog['img'] = $param['img'];
  5629. if (!empty($param['aid'])) {
  5630. // 检测活动是否存在
  5631. $existActivity = Activity::where([['id', '=', $param['aid']], ['root_id', '=', $root_id]])->count();
  5632. if ($existActivity == 0) {
  5633. Db::rollback();
  5634. return json(['code' => 1, 'msg' => '活动不存在']);
  5635. }
  5636. $visitLog['aid'] = $param['aid'];
  5637. }
  5638. $visitLog = CustomerVisitLog::create($visitLog);
  5639. if ($orders) $this->preformance_tasks($state, $param['customer_id'], $param['money'], $visitLog->id); //计算业绩
  5640. if (strpos($visitLog->state, '预约') !== false) {
  5641. $state = str_replace('预约', '', $visitLog->state);
  5642. // 查找是否已经预约
  5643. if ($is_manager == 1) {
  5644. $hadSubscribe = CustomersSubscribe::where([
  5645. ['customer_id', '=', $had['id']],
  5646. //['employee_id|designer_id', '=', $empid],
  5647. ['subscribe_date', '=', date('Y-m-d', strtotime($param['confirm_date']))],
  5648. ['state', '=', 0]
  5649. ])->count();
  5650. $typeSubscribe = CustomersSubscribe::where([
  5651. ['customer_id', '=', $had['id']],
  5652. //['employee_id|designer_id', '=', $empid],
  5653. ['type', '=', array_search($state, $stateType)],
  5654. ['state', '=', 0]
  5655. ])->find();
  5656. } else {
  5657. $hadSubscribe = CustomersSubscribe::where([
  5658. ['customer_id', '=', $had['id']],
  5659. ['employee_id|designer_id', '=', $empid],
  5660. ['subscribe_date', '=', date('Y-m-d', strtotime($param['confirm_date']))],
  5661. ['state', '=', 0]
  5662. ])->count();
  5663. $typeSubscribe = CustomersSubscribe::where([
  5664. ['customer_id', '=', $had['id']],
  5665. ['employee_id|designer_id', '=', $empid],
  5666. ['type', '=', array_search($state, $stateType)],
  5667. ['state', '=', 0]
  5668. ])->find();
  5669. }
  5670. $typeSubscribe = false; // 不是同一天的预约,同类型还可以预约
  5671. if ($hadSubscribe) {
  5672. Db::rollback();
  5673. return json(['code' => 1, 'msg' => '该客户在当前日期已有预约']);
  5674. } elseif ($typeSubscribe) {
  5675. Db::rollback();
  5676. return json(['code' => 1, 'msg' => '该客户已有' . $state . '预约, 预约日期' . $typeSubscribe->subscribe_date]);
  5677. } else {
  5678. $subscribe = [
  5679. 'customer_id' => $had['id'],
  5680. 'subscribe_date' => $param['confirm_date'],
  5681. 'employee_id' => $had->employee_id,
  5682. 'designer_id' => $had->designer_id,
  5683. 'type' => $state,
  5684. 'aid' => $param['aid'] ?: $had->aid,
  5685. 'org_id' => $had->org_id
  5686. ];
  5687. CustomersSubscribe::create($subscribe);
  5688. }
  5689. }
  5690. //当设为无效时,记录当前操作人跟进的最后一次状态,此处在公海列表处展示
  5691. if ($param['state'] == '无效') {
  5692. $last_cvl = CustomerVisitLog::where('customer_id', $param['customer_id'])->with('employee')->order('addtime desc')->find();
  5693. //获取"无效"并进入公海之前的那次跟进记录的信息
  5694. if ($last_cvl) {
  5695. $before_pool = [
  5696. 'name' => $last_cvl->employee->name,
  5697. 'state' => $last_cvl->state,
  5698. 'addtime' => $last_cvl->addtime
  5699. ];
  5700. } else {
  5701. $before_pool = [
  5702. 'name' => Employee::find($had['employee_id'])->value('name'),
  5703. 'state' => '待确认',
  5704. 'addtime' => $had->addtime
  5705. ];
  5706. }
  5707. // 该客户预约中的状态置为无效
  5708. CustomersSubscribe::where([['customer_id', '=', $param['customer_id']], ['state', '=', 0]])->save(['state' => -1]);
  5709. //公海内是否存在该客户信息,存在则合并客户信息(phone、phone1、phone2)
  5710. $orgids = orgSubIds($root_id);
  5711. $phone_arr = [];
  5712. !empty(trim($had['phone'])) ? $phone_arr[] = cypherphone(trim($had['phone'])) : '';
  5713. !empty(trim($had['phone1'])) ? $phone_arr[] = cypherphone(trim($had['phone1'])) : '';
  5714. !empty(trim($had['phone2'])) ? $phone_arr[] = cypherphone(trim($had['phone2'])) : '';
  5715. $phone_arr = array_filter($phone_arr);
  5716. $pool_condition = [
  5717. ['employee_id', '=', NULL],
  5718. ['is_resource', '=', 0],
  5719. ['org_id', 'in', $orgids],
  5720. ['phone|phone1|phone2', 'in', $phone_arr]
  5721. ];
  5722. $pool_crm = Customer::where($pool_condition)->column('id');
  5723. if (!empty($pool_crm)) {
  5724. //公海存在则删除
  5725. Customer::where([['id', 'in', $pool_crm]])->update(['delete_time' => time()]);
  5726. //关联回访记录修改
  5727. CustomerVisitLog::where([['customer_id', 'in', $pool_crm]])->update(['is_merge' => 1, 'customer_id' => $had['id']]);
  5728. CustomersSubscribe::where([['customer_id', 'in', $pool_crm]])->update(['is_merge' => 1, 'customer_id' => $had['id']]);
  5729. }
  5730. //增加无效记录
  5731. $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();
  5732. $invalidlog = [
  5733. 'customer_id' => $param['customer_id'],
  5734. 'employee_id' => $had->employee_id,
  5735. 'designer_id' => $had->designer_id ? $had->designer_id : 0,
  5736. 'org_id' => $had->org_id,
  5737. 'root_id' => $request->empcrm->root_id,
  5738. 'cus_addtime' => $had->sign_time,
  5739. 'visitlog_id' => $visitLog->id,
  5740. 'assigned_personnel' => $had->assigned_personnel ? $had->assigned_personnel : '',
  5741. 'source_id' => $had->source_id ? $had->source_id : 0,
  5742. 'status' => !empty($vlglist) ? 1 : 2,
  5743. 'cus_employee_time' => $had->employee_time,
  5744. 'is_resource' => $had->is_resource,
  5745. 'valid_time' => $had->valid_time
  5746. ];
  5747. CustomerInvalidLog::create($invalidlog);
  5748. 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]);
  5749. // -------------------- 企业微信逻辑相关 start -----------------
  5750. // 判断是否绑定有企业微信外部联系人关系,并做解绑操作
  5751. $company_id = Company::where('root_id', '=', $root_id)->value('id');
  5752. $company_setting = WeworksingleCompanySetting::where('company_id', '=', $company_id)->find();
  5753. if (!empty($company_setting)) {
  5754. $w_where['company_id'] = $company_id;
  5755. $w_where['employee_id'] = $empid;
  5756. $w_where['customer_id'] = $param['customer_id'];
  5757. WeworksingleCustomer::where($w_where)->save(['customer_id' => 0]);
  5758. }
  5759. // -------------------- 企业微信逻辑相关 end -----------------
  5760. }
  5761. // //如果签单,默认交定
  5762. // if (strpos($param['state'], "签单") !== false) {
  5763. // trace(strpos($param['state'], "签单"));
  5764. // $lw[] = CustomerVisitLog::changeState(['state', '=', '交定']);
  5765. // $lw[] = ['customer_id', '=', $param['customer_id']];
  5766. // $check = CustomerVisitLog::where($lw)->find();
  5767. // if (!$check) {
  5768. // $l_visitlog = $visitLog->toArray();
  5769. // unset($l_visitlog['id']);
  5770. // $l_visitlog['state'] = '交定';
  5771. // $l_visitlog['remark'] = !empty($param['remark']) ? $param['remark'] : $employeeTypeName . $employeeName . '变更客户状态为交定';
  5772. // $new_visitlog = array_merge($param, $l_visitlog);
  5773. // $new_visitlog['money'] = 0; //直接签单默认交定金额为0
  5774. // $mr = CustomerVisitLog::create($new_visitlog);
  5775. // $this->preformance_tasks('已交定', $param['customer_id'], $param['money'], $mr->id); //计算业绩
  5776. // }
  5777. // }
  5778. if (in_array($param['state'], ['已到店', '已量房', '已到场'])) {
  5779. $state = str_replace('已', '', $param['state']);
  5780. $subscribe = CustomersSubscribe::where([['customer_id', '=', $had['id']], ['type', '=', array_search($state, $stateType)], ['state', '=', 0]])->find();
  5781. if (!empty($subscribe)) {
  5782. $subscribe->state = 1;
  5783. $subscribe->save();
  5784. } elseif ($param['aid'] && in_array($param['state'], ['已到场'])) { //没有预约可以直接确认到场
  5785. $this->activityState($param, $had);
  5786. }
  5787. }
  5788. if (in_array($param['state'], ['未到店', '未量房', '未到场'])) {
  5789. $state = str_replace('未', '', $param['state']);
  5790. $subscribe = CustomersSubscribe::where([['customer_id', '=', $had['id']], ['type', '=', array_search($state, $stateType)], ['state', '=', 0]])->find();
  5791. if (!empty($subscribe)) {
  5792. $subscribe->state = -1;
  5793. $subscribe->save();
  5794. }
  5795. $data = [
  5796. 'eid' => $had->employee_id,
  5797. 'org_id' => $returnvisit['customer_org_id'],
  5798. 'designer_id' => $had->designer_id,
  5799. 'confirm_date' => $param['confirm_date'],
  5800. 'customer_id' => $had->id,
  5801. 'return_visit' => $had->return_visit,
  5802. 'name' => $had->name,
  5803. 'state' => $param['state']
  5804. ];
  5805. $this->cancelReservation($data);
  5806. //取消预约后,待回访状态也要取消
  5807. $had->return_visit = 0;
  5808. $had->save();
  5809. }
  5810. //增加经纪人送积分功能
  5811. if ($had->agents_id && in_array($param['state'], ['已到店', '已交定', '已签单'])) {
  5812. $this->agents_integral($had->agents_id, $param['state'], $had->id);
  5813. }
  5814. // 微爆活动
  5815. if (in_array($param['state'], ['已到店', '已量房', '已到场', '已交定', '已签单'])) {
  5816. $activity_data['customer_id'] = $param['customer_id'];
  5817. $activity_data['employee_id'] = $had->employee_id;
  5818. $activity_data['org_id'] = $had->org_id;
  5819. switch ($param['state']) {
  5820. case '已到店':
  5821. $activity_data['type'] = 2;
  5822. break;
  5823. case '已量房':
  5824. $activity_data['type'] = 3;
  5825. break;
  5826. case '已交定':
  5827. $activity_data['type'] = 4;
  5828. break;
  5829. case '已签单':
  5830. $activity_data['type'] = 5;
  5831. break;
  5832. case '已到场':
  5833. $activity_data['type'] = 7;
  5834. break;
  5835. default:
  5836. break;
  5837. }
  5838. WechatActivityIntegral::addIntegral($activity_data, $request->empcrm->root_id);
  5839. }
  5840. Db::commit();
  5841. $from_wework = input('from_wework', '', 'trim');
  5842. if ($from_wework) {
  5843. Console::call('download', ['customer', 'wework', (string)$root_id]);
  5844. Console::call('download', ['customer1', 'wework', (string)$root_id]);
  5845. } else {
  5846. Console::call('download', ['customer']);
  5847. Console::call('download', ['customer1']);
  5848. }
  5849. //2022-10-28 更新交定/签单凭证
  5850. if (in_array($param['state'], ['已交定', '已签单'])) {
  5851. $deliveryType = $param['state'] == '已交定' ? 'delivery' : 'sign';
  5852. Console::call('download', [$deliveryType]);
  5853. }
  5854. Console::call('medal', ['visit_log', (string)$empid, (string)$root_id]); //勋章
  5855. if ($is_manager == 1) {
  5856. //经理回访后修改客户的待回访的状态
  5857. Customer::where('id', $had->id)->update(['return_visit' => 0]);
  5858. $data = ['phone' => $had->phone, 'name' => $had->name];
  5859. } else {
  5860. //给业务经理和设计是发送提醒消息回访
  5861. $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];
  5862. }
  5863. //$this->returnVisit($data);
  5864. $data['name'] = !empty($data['name']) ? $data['name'] : '';
  5865. $data['phone'] = substr_replace($data['phone'], '******', 3, 6);
  5866. if (in_array($param['state'], $dd_state)) {
  5867. // 发送管理层模板消息
  5868. $msg = ['first' => '行为通知', 'keyword1' => '客户确认到店', 'keyword2' => "客户" . $data['name'] . "(" . $data['phone'] . ")确认到店", 'keyword3' => date('Y/m/d'), 'remark' => '请悉知'];
  5869. TmpMsg::sendMsgToLeader($msg, $token['org_id']);
  5870. }
  5871. if ($state == '已签单') {
  5872. // 发送管理层模板消息
  5873. $msg = ['first' => '行为通知', 'keyword1' => '客户签单', 'keyword2' => "客户" . $data['name'] . "(" . $data['phone'] . ")签单", 'keyword3' => date('Y/m/d'), 'remark' => '请悉知'];
  5874. TmpMsg::sendMsgToLeader($msg, $token['org_id']);
  5875. }
  5876. return json(['code' => 0, 'msg' => '跟进成功']);
  5877. }
  5878. /**
  5879. * 2022-10-27 逻辑修改,跟进保护优化 如果后台设置了重复客户跟进保护规则 跟进到保护状态后其他客户的此重复客户就不能在跟进了,但是可以置为无效
  5880. */
  5881. private function forbiddenState($id, $status)
  5882. {
  5883. if ($status == '无效') return false;
  5884. //$token = $this->request->token;
  5885. $root_id = $this->request->empcrm->root_id;
  5886. //保护规则
  5887. $provite = Setting::where([['name', '=', 'forbiddenState'], ['root_id', '=', $root_id]])->value('content');
  5888. if (!$provite) return false;
  5889. $info = Customer::where('id', $id)->column('phone,phone1,phone2');
  5890. //查询是否是重复客户
  5891. $orgs = Org::where([['path', 'like', $root_id . '-%']])->column('id');
  5892. $query[] = ['employee_id', '>', 0];
  5893. $query[] = ['org_id', 'in', $orgs];
  5894. $query[] = ['phone|phone1|phone2', 'in', array_filter($info[0])];
  5895. // $query[] = ['employee_id','<>',$token['employee_id']];
  5896. $check = Customer::where($query)->column('id,employee_id');
  5897. if (count($check) <= 1) return false;
  5898. $provite = explode(',', $provite);
  5899. $state = [];
  5900. foreach ($provite as $v) {
  5901. $state = array_merge($state, CustomerVisitLog::changeState($v, 'chaos'));
  5902. }
  5903. //查询该客户是否存在受保护的状态 只有第一个跟进的业务员可以继续跟进
  5904. $where[] = ['state', 'in', $state];
  5905. $where[] = ['customer_id', 'in', array_column($check, 'id')];
  5906. $where[] = ['employee_id|customer_employee_id', 'in', array_column($check, 'employee_id')];
  5907. $repeat = CustomerVisitLog::where($where)->order('id asc')->field('customer_id,state')->findOrEmpty();
  5908. if ($repeat->isEmpty() || (int)$repeat->customer_id == (int)$id) return false;
  5909. //该客户已被其他员工 确认到店/确认量房/确认到场/交定/签单 无法更近
  5910. $state = str_replace('已', '', $repeat->state);
  5911. $state = in_array(trim($state), ['交定', '签单']) ? trim($state) : '确认' . trim($state);
  5912. return '该客户已被其他员工“' . $state . '”无法跟进';
  5913. }
  5914. /**
  5915. * 计算业绩任务
  5916. */
  5917. public function preformance_tasks($state, $customer_id, $money = 0, $vislog_id)
  5918. {
  5919. $token = $this->get_token();
  5920. //防止后台切换部门
  5921. $token['org_id'] = Employee::where('id', $token['employee_id'])->value('org_id');
  5922. if ($state == '已交定') {
  5923. $w2[] = ['customer_id', '=', $customer_id];
  5924. $w2[] = ['state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')];
  5925. $w2[] = ['customer_employee_id', '=', $token['employee_id']];
  5926. $w2[] = ['customer_org_id', '=', $token['org_id']];
  5927. //切换部门重复交定仍然算业绩
  5928. $check = CustomerVisitLog::where($w2)->count(); //没有交定过计算业绩
  5929. if ($check == 1) {
  5930. $time = date('Y-m-d H:i:s');
  5931. //查询进行中的指派到所属部门的所有进度
  5932. // $w[] = ['org_id','=',$token['org_id']];
  5933. $w[] = ['root_id', '=', $token['root_org']];
  5934. $w[] = ['start_date', '<=', $time];
  5935. $w[] = ['end_date', '>=', $time];
  5936. $w[] = ['is_deposit', '=', 1];
  5937. $preformance_tasks = PreformanceTasksModel::where($w)->column('id');
  5938. $w1[] = ['root_id', '=', $token['root_org']];
  5939. $w1[] = ['org_id', '=', $token['org_id']];
  5940. $w1[] = ['performance_tasks_id', 'in', $preformance_tasks];
  5941. $w1[] = ['is_deposit', '=', 1];
  5942. //指派到自己部门
  5943. $pid = PreformanceTasksOrg::where($w1)->group('performance_tasks_id')->column('*');
  5944. $w4[] = ['id', 'in', array_column($pid, 'performance_tasks_id')];
  5945. $model = PreformanceTasksModel::where($w4)->column('*');
  5946. foreach ($model as $k => $v) {
  5947. $u = [];
  5948. $u['ok_deposit'] = $v['ok_deposit'] + 1;
  5949. $u['customer_visit_log_id'] = $v['customer_visit_log_id'] ? $v['customer_visit_log_id'] . ',' . $vislog_id : $vislog_id;
  5950. PreformanceTasksModel::where('id', $v['id'])->update($u);
  5951. unset($u);
  5952. }
  5953. PreformanceTasksOrg::where($w1)->inc('ok_deposit')->update();
  5954. }
  5955. } elseif ($state == '已签单') {
  5956. $w2[] = ['customer_id', '=', $customer_id];
  5957. $w2[] = ['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')];
  5958. $w2[] = ['customer_employee_id', '=', $token['employee_id']];
  5959. $check = CustomerVisitLog::where($w2)->count(); //没有交定过计算业绩
  5960. if ($check == 1) {
  5961. $time = date('Y-m-d H:i:s');
  5962. //查询进行中的指派到所属部门的所有进度
  5963. // $w[] = ['org_id','=',$token['org_id']];
  5964. $w[] = ['root_id', '=', $token['root_org']];
  5965. $w[] = ['start_date', '<=', $time];
  5966. $w[] = ['end_date', '>=', $time];
  5967. $w[] = ['is_money', '=', 1];
  5968. $preformance_tasks = PreformanceTasksModel::where($w)->column('id');
  5969. $w1[] = ['root_id', '=', $token['root_org']];
  5970. $w1[] = ['org_id', '=', $token['org_id']];
  5971. $w1[] = ['performance_tasks_id', 'in', $preformance_tasks];
  5972. $w1[] = ['is_money', '=', 1];
  5973. //指派到自己部门
  5974. $pid = PreformanceTasksOrg::where($w1)->group('performance_tasks_id')->column('*');
  5975. $w4[] = ['id', 'in', array_column($pid, 'performance_tasks_id')];
  5976. $model = PreformanceTasksModel::where($w4)->column('*');
  5977. foreach ($model as $k => $v) {
  5978. $u = [];
  5979. $u['ok_money'] = $v['ok_money'] + $money;
  5980. $u['customer_visit_log_id'] = $v['customer_visit_log_id'] ? $v['customer_visit_log_id'] . ',' . $vislog_id : $vislog_id;
  5981. PreformanceTasksModel::where('id', $v['id'])->update($u);
  5982. unset($u);
  5983. }
  5984. PreformanceTasksOrg::where($w1)->inc('ok_money', (int)$money)->update();
  5985. }
  5986. }
  5987. return 1;
  5988. }
  5989. /**
  5990. * 没有预约活动,跟进时传了aid和state='预约活动'
  5991. * 没有预约可以直接确认到场
  5992. * 补全数据添加预约和的追踪记录
  5993. */
  5994. public function activityState($param, $had)
  5995. {
  5996. $token = $this->get_token();
  5997. $employeeTypeName = $token['org_type'] == 2 ? '设计师' : '销售';
  5998. // 备注
  5999. $employeeName = Employee::where(['id' => $token['employee_id']])->value('name');
  6000. $remark = $param['remark'];
  6001. if (empty($remark)) {
  6002. $remark = $employeeTypeName . $employeeName . '对客户进行了回访';
  6003. }
  6004. //预约记录
  6005. $visitLog = [
  6006. 'customer_id' => $had->id,
  6007. 'type' => $param['type'],
  6008. 'next_contact_date' => !empty($param['next_contact_date']) ? $param['next_contact_date'] : date('Y-m-d'),
  6009. 'employee_id' => $token['employee_id'],
  6010. 'user_id' => $token['uid'],
  6011. 'remark' => $remark,
  6012. 'state' => 6,
  6013. 'weixin_media' => '',
  6014. 'media_id' => '',
  6015. 'starts' => $param['starts'],
  6016. 'money' => $param['money'],
  6017. 'confirm_date' => $param['confirm_date'],
  6018. 'org_id' => $token['org_id'],
  6019. 'aid' => $param['aid'],
  6020. 'weixin_media1' => '',
  6021. 'media_id1' => '',
  6022. 'customer_employee_id' => $had->employee_id,
  6023. 'customer_org_id' => Employee::where('id', $had->employee_id)->value('org_id')
  6024. ];
  6025. CustomerVisitLog::create($visitLog);
  6026. //
  6027. $subscribe = [
  6028. 'customer_id' => $had['id'],
  6029. 'subscribe_date' => $param['confirm_date'],
  6030. 'employee_id' => $had->employee_id,
  6031. 'designer_id' => $had->designer_id,
  6032. 'type' => 2,
  6033. 'aid' => $param['aid'],
  6034. 'org_id' => $visitLog['customer_org_id'],
  6035. 'state' => 1
  6036. ];
  6037. CustomersSubscribe::create($subscribe);
  6038. return 1;
  6039. }
  6040. /**
  6041. * 取消预约 给部门管理员和设计师发送消息提醒
  6042. * 张三取消了客户李四2020年8月1日的量房/到店/到场预约
  6043. */
  6044. public function cancelReservation($data)
  6045. {
  6046. $token = $this->get_token();
  6047. if ($data['return_visit'] == 1) {
  6048. $token['name'] = Employee::where([['root_id', '=', $token['root_org']], ['id', '=', $data['eid']]])->value('name');
  6049. $eids = Employee::where([['root_id', '=', $token['root_org']], ['org_id', '=', $data['org_id']], ['state', '=', '在职'], ['is_manager', '=', 1], ['uid', '>', 0]])->column('id');
  6050. if ($data['designer_id']) array_push($eids, $data['designer_id']);
  6051. $date = date('Y-m-d', strtotime($data['confirm_date']));
  6052. $date = explode('-', $date);
  6053. $state = str_replace('未', '', $data['state']);
  6054. $msg = $token['name'] . '取消了客户' . $data['name'] . $date[0] . '年' . $date[1] . '月' . $date[2] . '日的' . $state;
  6055. $eids = array_unique($eids);
  6056. foreach ($eids as $key => $val) {
  6057. event(new Msg($val, $msg, 'returnVisit', $data['customer_id']));
  6058. }
  6059. return true;
  6060. }
  6061. return false;
  6062. }
  6063. /**
  6064. * 经纪人送积分功能
  6065. */
  6066. public function agents_integral($agentid, $state, $customer_id)
  6067. {
  6068. //$token = $this->request->token;
  6069. $root_id = $this->request->empcrm->root_id;
  6070. $agent_data = AgentUser::where('id', $agentid)->field('agent_name,type')->find();
  6071. if ($state == '已到店') {
  6072. $code = 'daodian_integral';
  6073. $sta_type = 1;
  6074. }
  6075. if ($state == '已交定') {
  6076. $code = 'jiaoding_integral';
  6077. $sta_type = 2;
  6078. }
  6079. if ($state == '已签单') {
  6080. $code = 'qiandan_integral';
  6081. $sta_type = 3;
  6082. }
  6083. //添加与经纪人相关记录
  6084. $you = AgentCustomerLog::where([['agent_id', '=', $agentid], ['type', '=', $sta_type], ['customer_id', '=', $customer_id]])->count();
  6085. if (empty($you)) {
  6086. $add = array(
  6087. 'agent_id' => $agentid,
  6088. 'type' => $sta_type,
  6089. 'typename' => $state,
  6090. 'addtime' => time(),
  6091. 'customer_id' => $customer_id,
  6092. 'agent_type' => $agent_data['type'],
  6093. 'status' => 1
  6094. );
  6095. AgentCustomerLog::insert($add);
  6096. }
  6097. if ($agent_data['type'] == 2) return; //如果是网红经纪人直接返回
  6098. $jifen = CreditsSetting::where(['code' => $code, 'root_id' => $root_id])->value('value');
  6099. $ye = AgentIntegral::where(['customer_id' => $customer_id, 'agent_id' => $agentid])->column('state');
  6100. if (!in_array($state, $ye) && $agent_data['type'] == 1) {
  6101. $add = array(
  6102. 'agent_id' => $agentid,
  6103. 'type' => 1,
  6104. 'integral' => $jifen ? $jifen : 1,
  6105. 'addtime' => time(),
  6106. 'state' => $state,
  6107. 'customer_id' => $customer_id
  6108. );
  6109. AgentIntegral::insert($add);
  6110. }
  6111. if ($state == '已交定' && !in_array('已到店', $ye)) {
  6112. $jifen = CreditsSetting::where(['code' => 'daodian_integral', 'root_id' => $root_id])->value('value');
  6113. $add = array(
  6114. 'agent_id' => $agentid,
  6115. 'type' => 1,
  6116. 'integral' => $jifen ? $jifen : 1,
  6117. 'addtime' => time(),
  6118. 'state' => '已到店',
  6119. 'customer_id' => $customer_id
  6120. );
  6121. AgentIntegral::insert($add);
  6122. }
  6123. if ($state == '已签单' && !in_array('已到店', $ye)) {
  6124. $jifen = CreditsSetting::where(['code' => 'daodian_integral', 'root_id' => $root_id])->value('value');
  6125. $add = array(
  6126. 'agent_id' => $agentid,
  6127. 'type' => 1,
  6128. 'integral' => $jifen ? $jifen : 1,
  6129. 'addtime' => time(),
  6130. 'state' => '已到店',
  6131. 'customer_id' => $customer_id
  6132. );
  6133. AgentIntegral::insert($add);
  6134. }
  6135. if ($state == '已签单' && !in_array('已交定', $ye)) {
  6136. $jifen = CreditsSetting::where(['code' => 'jiaoding_integral', 'root_id' => $root_id])->value('value');
  6137. $add = array(
  6138. 'agent_id' => $agentid,
  6139. 'type' => 1,
  6140. 'integral' => $jifen ? $jifen : 1,
  6141. 'addtime' => time(),
  6142. 'state' => '已交定',
  6143. 'customer_id' => $customer_id
  6144. );
  6145. AgentIntegral::insert($add);
  6146. }
  6147. }
  6148. /**
  6149. * 消息提醒
  6150. * eid 客户所属业务员id,
  6151. * org_id 业务员部门id
  6152. * state 跟进状态
  6153. * name 业务员姓名
  6154. * designer_id 设计师id
  6155. * 01、客户邀约提醒管理层和指派的设计师。没指派设计师时,不提醒设计师,提醒管理层去指派设计师。在员工客户里面需要管理层回访的客户红色标签提示。
  6156. * 02、业务员确认客户量房、到店、到场、交定、签单,提醒管理层回访客户。
  6157. * a、管理层点击提醒,进入对应客户详情页面。进行客户跟踪。
  6158. * b、在员工客户里面需要管理层回访的客户红色标签提示。
  6159. */
  6160. public function returnVisit($data)
  6161. {
  6162. $token = $this->get_token();
  6163. $token['name'] = Employee::where([['id', '=', $data['eid']]])->value('name');
  6164. $eids = Employee::where([['root_id', '=', $token['root_org']], ['org_id', '=', $data['org_id']], ['state', '=', '在职'], ['is_manager', '=', 1], ['uid', '>', 0]])->column('id');
  6165. $state1 = CustomerVisitLog::changeState('确认到店', 'chaos');
  6166. $state2 = CustomerVisitLog::changeState('确认量房', 'chaos');
  6167. $state3 = CustomerVisitLog::changeState('确认到场', 'chaos');
  6168. $state4 = CustomerVisitLog::changeState('交定', 'chaos');
  6169. $state5 = CustomerVisitLog::changeState('签单', 'chaos');
  6170. $make_state1 = array_merge($state1, $state2, $state3, $state4, $state5);
  6171. $state6 = CustomerVisitLog::changeState('预约到店', 'chaos');
  6172. $state7 = CustomerVisitLog::changeState('预约量房', 'chaos');
  6173. $state8 = CustomerVisitLog::changeState('预约活动', 'chaos');
  6174. $make_state2 = array_merge($state6, $state7, $state8);
  6175. if (in_array($data['state'], $make_state2)) {
  6176. $date = $office_date = date('Y-m-d', strtotime($data['confirm_date']));
  6177. $date = explode('-', $date);
  6178. $state = str_replace('预约', '', $data['state']);
  6179. $msg = $token['name'] . '预约了客户' . $data['name'] . ',于' . $date[0] . '年' . $date[1] . '月' . $date[2] . '日' . $state;
  6180. $msg = $data['designer_id'] ? $msg : $msg . ',该客户暂未指派设计师,请及时指派!!!';
  6181. $type = '预约';
  6182. } elseif (in_array($data['state'], $make_state1)) {
  6183. $date = $office_date = date('Y-m-d');
  6184. $date = explode('-', $date);
  6185. $state = str_replace('已', '', $data['state']);
  6186. $msg = $token['name'] . '确认客户' . $data['name'] . ',于' . $date[0] . '年' . $date[1] . '月' . $date[2] . '日' . $state;
  6187. $type = '确认';
  6188. } else {
  6189. return false;
  6190. }
  6191. if ($data['designer_id']) array_push($eids, $data['designer_id']);
  6192. $eids = array_unique($eids);
  6193. $param = ['name' => $data['name'], 'sale_name' => $token['name'], 'type' => $type, 'state' => $state, 'phone' => $data['phone'], 'date' => $office_date];
  6194. foreach ($eids as $key => $val) {
  6195. event(new Msg($val, $msg, 'returnVisit', $data['customer_id']));
  6196. //公众号消息
  6197. $param['employee_id'] = $val;
  6198. $param['cid'] = $data['customer_id'];
  6199. $this->sendOfficialAccount($param);
  6200. }
  6201. Customer::where('id', $data['customer_id'])->update(['return_visit' => 1]);
  6202. return true;
  6203. }
  6204. /**
  6205. * 发送公众号消息
  6206. * $data['name'] 客户姓名
  6207. * $data['type'] 预约/确认
  6208. * $data['state'] 到店,量房,到场
  6209. * $data['phone'] 客户电话
  6210. * $data['sale_name'] 业务员姓名
  6211. * $data['employee_id] 业务员id
  6212. * $data['date'] 日期
  6213. */
  6214. public function sendOfficialAccount($data)
  6215. {
  6216. //$token = $this->request->token;
  6217. $token = $this->get_token();
  6218. $dates = explode('-', $data['date']);
  6219. $client_type = Miniprogram::where('root_id', $token['root_org'])->value('notify');
  6220. if ($data['type'] == '预约') {
  6221. //查询预约编号 今天的第多少条预约记录
  6222. $state6 = CustomerVisitLog::changeState('预约到店', 'chaos');
  6223. $state7 = CustomerVisitLog::changeState('预约量房', 'chaos');
  6224. $state8 = CustomerVisitLog::changeState('预约活动', 'chaos');
  6225. $make_state2 = array_merge($state6, $state7, $state8);
  6226. $eids = Employee::where([['root_id', '=', $token['root_org']]])->column('id');
  6227. $count = CustomerVisitLog::where([['addtime', 'like', '%' . date('Y-m-d') . '%'], ['state', 'in', $make_state2], ['employee_id', 'in', $eids]])->count();
  6228. $count++;
  6229. $number = $dates[0] . $dates[1] . $dates[2] . $count;
  6230. $data['page'] = "/pages/index/index?office_type=cread&cid=" . $data['cid'] . '&clientype=' . $client_type;
  6231. $data['param'] = [
  6232. 'first' => '预约提醒', //标题
  6233. 'keyword1' => $number, //预约编号
  6234. 'keyword2' => $data['type'] . $data['name'] . $data['state'], //预约项目
  6235. 'keyword3' => $data['date'], //预约时间
  6236. 'keyword4' => substr_replace($data['phone'], '******', 3, 6), //预约电话
  6237. 'keyword5' => $data['name'], //预约姓名
  6238. 'remark' => '您好,业务员' . $data['sale_name'] . '新增一次预约记录,请及时回访' //电话
  6239. ];
  6240. $msg = [
  6241. 'type' => 'returnVisit',
  6242. 'employee_id' => $data['employee_id']
  6243. ];
  6244. EmployeeMsg::temploateMsg($msg, $data);
  6245. } elseif ($data['type'] == '确认') {
  6246. }
  6247. return true;
  6248. }
  6249. //转换token
  6250. public function get_token()
  6251. {
  6252. $token = [
  6253. 'uid' => $this->request->empcrm->uid,
  6254. 'employee_id' => $this->request->empcrm->id,
  6255. 'org_id' => $this->request->empcrm->org_id,
  6256. 'root_org' => $this->request->empcrm->root_id,
  6257. 'name' => $this->request->empcrm->name,
  6258. ];
  6259. return $token;
  6260. }
  6261. /**
  6262. * 客户迁移
  6263. */
  6264. public function customerTransfer()
  6265. {
  6266. $request = request();
  6267. $root_id = $request->empcrm->root_id;
  6268. $empid = $request->empcrm->id;
  6269. $orgids = orgSubIds($root_id);
  6270. $param = $this->request->only(['customer_ids' => '', 'employeeid']);
  6271. $arr = explode(',', $param['customer_ids']);
  6272. //查询手机号
  6273. $phone = Customer::where([['employee_id', '=', $param['employeeid']], ['org_id', 'in', $orgids]])->column('phone,phone1,phone2');
  6274. $phones = [];
  6275. foreach ($phone as $v) {
  6276. $phones = array_merge($phones, array_values($v));
  6277. }
  6278. $phones = array_filter($phones);
  6279. $customers = Customer::where([['id', 'in', $arr], ['org_id', 'in', $orgids]])->column('id,name,phone,phone1,phone2');
  6280. $repeat = $no_repeat = [];
  6281. foreach ($customers as $v2) {
  6282. $l_phone = array_filter([$v2['phone'], $v2['phone1'], $v2['phone2']]);
  6283. empty(array_intersect($l_phone, $phones)) ? $no_repeat[] = $v2['id'] : $repeat[] = $v2['name'];
  6284. }
  6285. if ($no_repeat) {
  6286. $org_id = Employee::where('id', $param['employeeid'])->value('org_id');
  6287. Customer::where([['id', 'in', $no_repeat]])->update(['employee_id' => $param['employeeid'], 'org_id' => $org_id]);
  6288. 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])]);
  6289. //修改转移的客户的跟进记录的customer_employee_id为新员工的id
  6290. CustomerVisitLog::where([['customer_id', 'in', $no_repeat], ['org_id', 'in', $orgids]])->update(['customer_employee_id' => $param['employeeid'], 'customer_org_id' => $org_id]);
  6291. }
  6292. if ($repeat) return json(['code' => 1, 'msg' => '分派失败:员工名下已存在客户[' . implode(',', $repeat) . ']的联系方式']);
  6293. return json(['code' => 0, 'msg' => '分派完成']);
  6294. }
  6295. //已签单或已交定客户详情
  6296. public function deposit_sign_detail()
  6297. {
  6298. $request = request();
  6299. $root_id = $request->empcrm->root_id;
  6300. $empid = $request->empcrm->id;
  6301. $param = $this->request->only(['cid']);
  6302. $ms = $this->seLemp($param['cid'], $empid);
  6303. $data = $ms->toArray();
  6304. $visitlog = $data['visitLog'];
  6305. if (!empty($visitlog)) {
  6306. foreach ($visitlog as $key => $val) {
  6307. $visitlog[$key]['empname'] = Employee::where('id', $val['employee_id'])->value('name');
  6308. $parr[] = $val['addtime'];
  6309. }
  6310. array_multisort($parr, SORT_ASC, $visitlog);
  6311. foreach ($visitlog as $key => $val) {
  6312. if ((count($visitlog) - 1) > $key) {
  6313. $visitlog[$key]['xt'] = round((strtotime($visitlog[$key + 1]['addtime']) - strtotime($val['addtime'])) / 86400);
  6314. }
  6315. if ((count($visitlog) - 1) == $key) {
  6316. $visitlog[$key]['xt'] = 0;
  6317. }
  6318. }
  6319. }
  6320. $data['visitLog'] = $visitlog;
  6321. View::assign('data', $data);
  6322. return View::fetch();
  6323. }
  6324. //公用查询客户详情
  6325. public function other_seLemp($id, $org_id)
  6326. {
  6327. $request = request();
  6328. $root_id = $request->empcrm->root_id;
  6329. $orgids = orgSubIds($root_id);
  6330. if (!in_array($org_id, $orgids)) echo json(['code' => 1, 'msg' => '数据不存在']);
  6331. $condition = [
  6332. ['org_id', '=', $org_id],
  6333. ['id', '=', $id]
  6334. ];
  6335. $data = Customer::with(['source', 'designer' => function ($query) {
  6336. $query->field('id,name,headimgurl,org_id,uid');
  6337. }, 'employee' => function ($query) {
  6338. $query->withField(['id', 'name']);
  6339. }, 'visitLog' => function ($query) {
  6340. $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');
  6341. }])->where($condition)->find();
  6342. if (empty($data)) echo json(['code' => 1, 'msg' => '数据不存在']);
  6343. //if (empty($data)) return false;
  6344. $data['daochang'] = CustomerVisitLog::where([['customer_id', '=', $id], ['employee_id', '=', $data['employee_id']], CustomerVisitLog::changeState(['state', '=', '已到访'])])->order('addtime desc')->find();
  6345. $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();
  6346. $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();
  6347. $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')");
  6348. $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')");
  6349. $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();
  6350. $data['package_name'] = $data['package_id'] ? CustomerPackage::where('id', $data['package_id'])->value('name') : '';
  6351. $data['deco_style_name'] = $data['deco_style'] ? Decostyle::where('id', $data['deco_style'])->value('name') : '';
  6352. //$data['company_name'] = Company::where('root_id',$root_id)->value('company_name');
  6353. $data['add_wechat'] = '否';
  6354. $data['housetype'] = $data['unit_number'] = $data['subjection_plan'] = null;
  6355. if (!empty($data['ext'])) {
  6356. $field = $this->get_portrait_field($data);
  6357. foreach ($field as $key => $val) {
  6358. if ($val['keyname'] == 'wechat' && !empty($val['value'])) {
  6359. $data['add_wechat'] = CustomerPortraitFieldSelect::where('id', $val['value'])->value('name');
  6360. }
  6361. if ($val['keyname'] == 'housetype_arrow' && !empty($val['value'])) {
  6362. $data['housetype'] = CustomerPortraitFieldSelect::where('id', $val['value'])->value('name');
  6363. }
  6364. if ($val['keyname'] == 'subjection_plan' && !empty($val['value'])) {
  6365. $data['subjection_plan'] = CustomerPortraitFieldSelect::where('id', $val['value'])->value('name');
  6366. }
  6367. if ($val['keyname'] == 'unit_number' && !empty($val['value'])) {
  6368. $data['unit_number'] = $val['value'];
  6369. }
  6370. }
  6371. }
  6372. $data['daochang'] = !empty($data['daochang']) ? json_decode($data['daochang'], true) : null;
  6373. $data['deposit'] = !empty($data['deposit']) ? json_decode($data['deposit'], true) : null;
  6374. $data['sign'] = !empty($data['sign']) ? json_decode($data['sign'], true) : null;
  6375. return $data;
  6376. }
  6377. /**
  6378. * 判断能否查看客户详情
  6379. */
  6380. public function checkCustomer()
  6381. {
  6382. $id = input('id', '', 'intval');
  6383. $request = request();
  6384. $empid = $request->empcrm->id;
  6385. $is_manager = $request->empcrm->is_manager;
  6386. $wuxiao_state = Customer::changeState('无效', 'chaos');
  6387. $where = [
  6388. ['id', '=', $id],
  6389. ['state', 'not in', $wuxiao_state]
  6390. ];
  6391. $data = Customer::where($where)->find();
  6392. if (empty($data)) {
  6393. return json(['code' => 1, 'msg' => '无法查看此客户,请刷新后重试']);
  6394. }
  6395. $empIdList = explode(',', $data->employee_id . ',' . $data->employee_id . ',' . $data->assigned_personnel);
  6396. $empIdList = array_unique(array_filter($empIdList));
  6397. if (!empty($data) && !in_array($empid, $empIdList)) {
  6398. if ($is_manager == 1) {
  6399. $team_orgs = orgSubIds($request->empcrm->org_id);
  6400. $yuangong = Employee::where([['org_id', 'in', $team_orgs], ['uid', '>', 0], ['id', 'in', $empIdList]])->count();
  6401. if ($yuangong == 0) $data = null;
  6402. } else {
  6403. $data = null;
  6404. }
  6405. }
  6406. if (empty($data)) {
  6407. return json(['code' => 1, 'msg' => '无法查看此客户,请刷新后重试']);
  6408. }
  6409. return json(['code' => 0, 'msg' => 'success']);
  6410. }
  6411. //添加回访记录
  6412. public function addlog()
  6413. {
  6414. $request = request();
  6415. $root_id = $request->empcrm->root_id;
  6416. $empid = $request->empcrm->id;
  6417. $is_manager = $request->empcrm->is_manager;
  6418. $team_orgs = orgSubIds($request->empcrm->org_id);
  6419. $param = $this->request->only(['cid', 'org_id' => '']);
  6420. $orgids = orgSubIds($root_id);
  6421. //if(!empty($param['org_id']) && $is_manager == 1 && !in_array($param['org_id'],$team_orgs)){
  6422. if (!empty($param['org_id']) && in_array($param['org_id'], $orgids)) {
  6423. $ms = $this->other_seLemp($param['cid'], $param['org_id']);
  6424. $is_sharecus = 1;
  6425. } else {
  6426. $ms = $this->seLemp($param['cid'], $empid);
  6427. }
  6428. $data = $ms->toArray();
  6429. $stain = [];
  6430. if (!empty($data['visitLog'])) {
  6431. $state1 = CustomerVisitLog::changeState('已到店', 'chaos');
  6432. $state2 = CustomerVisitLog::changeState('已到场', 'chaos');
  6433. foreach ($data['visitLog'] as $key => $val) {
  6434. if ($val['state'] == '已签单' && $request->empcrm['xinjushang'] == 0) $val['state'] = '已转单';
  6435. if ($val['state'] == '已交定' && $request->empcrm['xinjushang'] == 0) $val['state'] = '已签单';
  6436. if ($val['state'] == '已转单' && $request->empcrm['xinjushang'] == 0) $data['visitLog'][$key]['remark'] = str_replace('已签单', '已转单', $val['remark']);
  6437. if ($val['state'] == '已签单' && $request->empcrm['xinjushang'] == 0) $data['visitLog'][$key]['remark'] = str_replace('已交定', '已签单', $val['remark']);
  6438. $data['visitLog'][$key]['state'] = $val['state'];
  6439. $data['visitLog'][$key]['empname'] = Employee::where('id', $val['employee_id'])->value('name');
  6440. if (!in_array($val['state'], $stain)) {
  6441. $stain[] = $val['state'];
  6442. }
  6443. if (!empty($val['save_portrait_field'])) {
  6444. $save_portrait_field = json_decode($val['save_portrait_field'], true);
  6445. foreach ($save_portrait_field as $k => $v) {
  6446. if ($v['type'] == 6) {
  6447. $save_portrait_field[$k]['valname'] = explode(',', $v['value']);
  6448. }
  6449. }
  6450. } else {
  6451. $save_portrait_field = '';
  6452. }
  6453. $data['visitLog'][$key]['save_portrait_field'] = $save_portrait_field;
  6454. $data['visitLog'][$key]['isyes'] = 1;
  6455. if ($key != 0 && $val['next_contact_date'] != '暂未设置下次回访时间') $data['visitLog'][$key]['isyes'] = 0;
  6456. $data['visitLog'][$key]['wisdom_url'] = '';
  6457. $data['visitLog'][$key]['is_wisdom'] = 0;
  6458. if ($val['remark'] && strpos($val['remark'], '讲解智慧屏##') !== false) {
  6459. //remark = 接待了客户:张三,讲解时长21分钟@讲解智慧屏## url;
  6460. $data['visitLog'][$key]['is_wisdom'] = 1;
  6461. $arr = explode('##', $val['remark']);
  6462. if (count($arr) == 2) {
  6463. $ali_oss_bindurl = config('app.ali_oss_bindurl');
  6464. $data['visitLog'][$key]['wisdom_url'] = 'https://' . $ali_oss_bindurl . '/' . $arr[1];
  6465. $title = explode('@', $arr[0]);
  6466. $data['visitLog'][$key]['remark'] = $title[0];
  6467. }
  6468. }
  6469. // 检测是否有录音
  6470. if ($val['data_type'] == 'out_call') {
  6471. $data['visitLog'][$key]['is_wisdom'] = 2;
  6472. $data['visitLog'][$key]['wisdom_url'] = OutCallLog::where(['id' => $val['data_id']])->value('url');
  6473. } elseif ($val['data_type'] == 'mobile_call' && strpos($val['remark'], '#') !== false) {
  6474. $ali_oss_bindurl = config('app.ali_oss_bindurl');
  6475. $arr = explode('#', $val['remark']);
  6476. $data['visitLog'][$key]['wisdom_url'] = 'https://' . $ali_oss_bindurl . '/' . $arr[1];
  6477. $data['visitLog'][$key]['remark'] = $arr[0];
  6478. }
  6479. if ($val['number_of_visitors'] > 0 && in_array($val['state'], $state1)) {
  6480. $val['remark'] = $val['remark'] . ' 到店人数:' . $val['number_of_visitors'] . '人;';
  6481. $data['visitLog'][$key]['remark'] = $val['remark'];
  6482. }
  6483. if ($val['talking_order_time'] && in_array($val['state'], $state1)) {
  6484. $val['remark'] = $val['remark'] . ' 谈单时长:' . $val['talking_order_time'] . '分钟;';
  6485. $data['visitLog'][$key]['remark'] = $val['remark'];
  6486. }
  6487. if ($val['number_of_visitors'] > 0 && in_array($val['state'], $state2)) {
  6488. $val['remark'] = $val['remark'] . ' 到场人数:' . $val['number_of_visitors'] . '人;';
  6489. $data['visitLog'][$key]['remark'] = $val['remark'];
  6490. }
  6491. if ($val['stay_length'] && in_array($val['state'], $state2)) {
  6492. $val['remark'] = $val['remark'] . ' 停留时间:' . $val['stay_length'] . '分钟;';
  6493. $data['visitLog'][$key]['remark'] = $val['remark'];
  6494. }
  6495. //检测是否有下载文件
  6496. if (!empty($val['document_field']) && !empty($val['document_name'])) {
  6497. $str = $root_id . '#' . $empid . '#' . $val['id'];
  6498. $aec = new Aec(config('app.aec_key'), config('app.aec_iv'));
  6499. $value = $aec->encrypt($str);
  6500. $data['visitLog'][$key]['document_field'] = $request->domain() . '/downdocument/index.html?token=' . $value;
  6501. }
  6502. }
  6503. }
  6504. $data['allstate'] = $stain;
  6505. //房屋状态
  6506. $house_status = CustomerPortraitField::with(['select'])->where([['root_id', '=', $root_id], ['keyname', '=', 'house_status']])->find()->toArray();
  6507. View::assign('house_status', $house_status['select']);
  6508. View::assign('empname', $request->empcrm->name);
  6509. //指派类型
  6510. $assign_count = Setting::where([['root_id', '=', $root_id], ['name', '=', 'assign_type']])->findOrEmpty();
  6511. $assign_name = !$assign_count->isEmpty() ? json_decode($assign_count->content, true) : [];
  6512. $assign_list = [];
  6513. foreach ($assign_name as $k => $v) {
  6514. if ($v == 'yixiang') $assign_list[] = ['keyname' => $v, 'value' => '意向客户指派'];
  6515. if ($v == 'liangfang') $assign_list[] = ['keyname' => $v, 'value' => '量房客户指派'];
  6516. if ($v == 'daodian') $assign_list[] = ['keyname' => $v, 'value' => '到店客户指派'];
  6517. }
  6518. View::assign('assign_list', $assign_list);
  6519. $isadd_log = 0;
  6520. $isAllManagerOperateAble = Setting::where(['root_id' => $root_id, 'name' => 'is_all_manager_oprate_able'])->value('content');
  6521. $path = Org::where([['pid', '=', $request->empcrm->org_id]])->count();
  6522. if ($is_manager && in_array($data['org_id'], $team_orgs)) {
  6523. if ($isAllManagerOperateAble || empty($path)) // 判断是否能跟进,开启所有以上领导能跟进,或直属领导跟进
  6524. $isadd_log = 1;
  6525. }
  6526. if ($data['employee_id'] == $empid) {
  6527. $isadd_log = 1;
  6528. }
  6529. // 加微信时间
  6530. $data['add_wechat_time'] = '';
  6531. if (!empty($data['ext']) && $data['ext'] != 'null') {
  6532. $ext = json_decode($data['ext'], true);
  6533. foreach ($ext as $i) {
  6534. if (isset($i['keyname']) && $i['keyname'] == 'add_wechat_time') {
  6535. $data['add_wechat_time'] = $i['value'];
  6536. break;
  6537. }
  6538. }
  6539. }
  6540. if (!empty($is_sharecus) && $is_manager) $isadd_log = 1;
  6541. View::assign('data', $data);
  6542. View::assign('isadd_log', $isadd_log);
  6543. View::assign('is_manager', $is_manager);
  6544. // 判断是否有外呼系统
  6545. $outcallSetting = OutCallSetting::where(['root_id' => $root_id, 'type' => 'yhuatong'])->find();
  6546. if ($outcallSetting !== null) {
  6547. // 如果有外呼系统则获取登陆人拨打的电话列表
  6548. View::assign('outcall', 1);
  6549. $phones = empty($request->empcrm['out_call_phone']) ? [] : explode(',', $request->empcrm['out_call_phone']);
  6550. array_unshift($phones, $request->empcrm['phone']);
  6551. View::assign('outcall_phone', $phones);
  6552. } else {
  6553. View::assign('outcall', 0);
  6554. }
  6555. //判断下次回访时间是否必传
  6556. $next_where[] = ['root_id', '=', $root_id];
  6557. $next_where[] = ['name', '=', 'nextVisitTimeIsOk'];
  6558. $nextVisitTimeIsOk = Setting::where($next_where)->value('content');
  6559. $fish = CustomerSource::where(['root_id' => $root_id, 'source' => '飞鱼线索'])->find();
  6560. View::assign('fish', isset($fish) && $data['source_id'] == $fish['id'] ? 1 : 0);
  6561. View::assign('nextVisitTimeIsOk', $nextVisitTimeIsOk);
  6562. // 检测是否已经申请
  6563. $rs = CustomerStateCheck::where(['customer_id' => $param['cid']])->find();
  6564. View::assign('valid_state', $rs);
  6565. return View::fetch();
  6566. }
  6567. //单独确认客户有效
  6568. public function validation()
  6569. {
  6570. $request = request();
  6571. $root_id = $request->empcrm->root_id;
  6572. $empid = $request->empcrm->id;
  6573. $orgids = orgSubIds($root_id);
  6574. $param = $this->request->only(['customer_id']);
  6575. $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')]);
  6576. if ($ms) {
  6577. return json(['code' => 0, 'msg' => '确认完成']);
  6578. } else {
  6579. return json(['code' => 1, 'msg' => '确认失败']);
  6580. }
  6581. }
  6582. //新增客户后跟进页面
  6583. public function followup()
  6584. {
  6585. $request = request();
  6586. $root_id = $request->empcrm->root_id;
  6587. $empid = $request->empcrm->id;
  6588. $param = $this->request->only(['cid']);
  6589. $data = $this->seLemp($param['cid'], $empid);
  6590. View::assign('data', $data);
  6591. return View::fetch();
  6592. }
  6593. //设置客户介绍新客户标记
  6594. public function save_introduce()
  6595. {
  6596. $request = request();
  6597. $root_id = $request->empcrm->root_id;
  6598. $empid = $request->empcrm->id;
  6599. $orgids = orgSubIds($root_id);
  6600. $param = $this->request->only(['cid']);
  6601. $ms = Customer::where([['id', '=', $param['cid']], ['org_id', 'in', $orgids]])->find();
  6602. if (!$ms) return json(['code' => 1, 'msg' => '客户不存在']);
  6603. $ms->introduce = 1;
  6604. $ms->save();
  6605. return json(['code' => 0, 'msg' => '设置成功']);
  6606. }
  6607. //公共查询客户详情(没有所属员工id时)
  6608. public function cusdet_noemp($id)
  6609. {
  6610. $request = request();
  6611. $root_id = $request->empcrm->root_id;
  6612. $orgids = orgSubIds($root_id);
  6613. $data = Customer::with(['visitLog'])->where([['id', '=', $id], ['org_id', 'in', $orgids]])->find();
  6614. $data = $data->toArray();
  6615. // echo json_encode($data['visitLog']);
  6616. // exit;
  6617. $data['package_name'] = $data['package_id'] ? CustomerPackage::where('id', $data['package_id'])->value('name') : '';
  6618. $data['deco_style_name'] = $data['deco_style'] ? Decostyle::where('id', $data['deco_style'])->value('name') : '';
  6619. $data['company_name'] = Company::where('root_id', $root_id)->value('company_name');
  6620. $data['add_wechat'] = '否';
  6621. $data['housetype'] = $data['unit_number'] = $data['subjection_plan'] = null;
  6622. $data['jiaofang'] = !empty($data['house_delivery_time']) ? ($data['house_delivery_time'] <= date('Y/m/d', time()) ? '现房' : '期房') : '';
  6623. $data['company_name'] = Company::where('root_id', $root_id)->value('company_name');
  6624. //$vslog = CustomerVisitLog::where('customer_id',$id)->order('addtime desc')->find();
  6625. $data['lrname'] = NULL;
  6626. if (!empty($data['visitLog']) && !empty($data['visitLog'][0]['customer_employee_id'])) {
  6627. $data['lrname'] = Employee::where('id', $data['visitLog'][0]['customer_employee_id'])->value('name');
  6628. }
  6629. if (!empty($data['ext'])) {
  6630. $field = $this->get_portrait_field($data);
  6631. foreach ($field as $key => $val) {
  6632. if ($val['keyname'] == 'wechat' && !empty($val['value'])) {
  6633. $data['add_wechat'] = CustomerPortraitFieldSelect::where('id', $val['value'])->value('name');
  6634. }
  6635. if ($val['keyname'] == 'housetype_arrow' && !empty($val['value'])) {
  6636. $data['housetype'] = CustomerPortraitFieldSelect::where('id', $val['value'])->value('name');
  6637. }
  6638. if ($val['keyname'] == 'subjection_plan' && !empty($val['value'])) {
  6639. $data['subjection_plan'] = CustomerPortraitFieldSelect::where('id', $val['value'])->value('name');
  6640. }
  6641. if ($val['keyname'] == 'unit_number' && !empty($val['value'])) {
  6642. $data['unit_number'] = $val['value'];
  6643. }
  6644. if ($val['keyname'] == 'sign_name' && !empty($val['value'])) {
  6645. $data['sign_time'] = $val['value'];
  6646. }
  6647. }
  6648. }
  6649. if (!empty($data['visitLog'])) {
  6650. foreach ($data['visitLog'] as $key => $val) {
  6651. if ($val['state'] == '已签单' && $request->empcrm['xinjushang'] == 0) $val['state'] = '已转单';
  6652. if ($val['state'] == '已交定' && $request->empcrm['xinjushang'] == 0) $val['state'] = '已签单';
  6653. if ($val['state'] == '已转单' && $request->empcrm['xinjushang'] == 0) $data['visitLog'][$key]['remark'] = str_replace('已签单', '已转单', $val['remark']);
  6654. if ($val['state'] == '已签单' && $request->empcrm['xinjushang'] == 0) $data['visitLog'][$key]['remark'] = str_replace('已交定', '已签单', $val['remark']);
  6655. $data['visitLog'][$key]['state'] = $val['state'];
  6656. $data['visitLog'][$key]['empname'] = Employee::where('id', $val['employee_id'])->value('name');
  6657. if (!empty($val['save_portrait_field'])) {
  6658. $save_portrait_field = json_decode($val['save_portrait_field'], true);
  6659. foreach ($save_portrait_field as $k => $v) {
  6660. if ($v['type'] == 6) {
  6661. $save_portrait_field[$k]['valname'] = explode(',', $v['value']);
  6662. }
  6663. }
  6664. } else {
  6665. $save_portrait_field = '';
  6666. }
  6667. $data['visitLog'][$key]['save_portrait_field'] = $save_portrait_field;
  6668. $parr[] = $val['addtime'];
  6669. if ($val['next_contact_date'] > date('Y-m-d H:i:s')) {
  6670. $data['visitLog'][$key]['isyes'] = 1;
  6671. } else {
  6672. $data['visitLog'][$key]['isyes'] = 0;
  6673. }
  6674. $data['visitLog'][$key]['wisdom_url'] = '';
  6675. $data['visitLog'][$key]['is_wisdom'] = 0;
  6676. if ($val['remark'] && strpos($val['remark'], '讲解智慧屏##') !== false) {
  6677. //remark = 接待了客户:张三,讲解时长21分钟@讲解智慧屏## url;
  6678. $data['visitLog'][$key]['is_wisdom'] = 1;
  6679. $arr = explode('##', $val['remark']);
  6680. if (count($arr) == 2) {
  6681. $ali_oss_bindurl = config('app.ali_oss_bindurl');
  6682. $data['visitLog'][$key]['wisdom_url'] = 'https://' . $ali_oss_bindurl . '/' . $arr[1];
  6683. $title = explode('@', $arr[0]);
  6684. $data['visitLog'][$key]['remark'] = $title[0];
  6685. }
  6686. }
  6687. // 检测是否有录音
  6688. if ($val['data_type'] == 'out_call') {
  6689. $data['visitLog'][$key]['is_wisdom'] = 2;
  6690. $data['visitLog'][$key]['wisdom_url'] = OutCallLog::where(['id' => $val['data_id']])->value('url');
  6691. }
  6692. }
  6693. array_multisort($parr, SORT_DESC, $data['visitLog']);
  6694. }
  6695. return $data;
  6696. }
  6697. /**
  6698. * 公海获取客户
  6699. * customer_id来源:业务员获取(int)、报备录入时查询公海存在自动获取(逗号分隔字符串)
  6700. */
  6701. public function fetchpoolitem()
  6702. {
  6703. $request = request();
  6704. $root_id = $request->empcrm->root_id;
  6705. $employee_id = $request->empcrm->id;
  6706. $orgids = orgSubIds($root_id);
  6707. $param = $this->request->only(['cid', 'designer_id']);
  6708. if (!$request->isAjax()) {
  6709. $this->screen();
  6710. $data = $this->cusdet_noemp($param['cid']);
  6711. View::assign('data', $data);
  6712. return View::fetch();
  6713. }
  6714. // 设计师能否报备、获取客户
  6715. $de_where[] = ['root_id', '=', $root_id];
  6716. $de_where[] = ['name', '=', 'designer_get_customer'];
  6717. $info_de_where = Setting::where($de_where)->findOrEmpty();
  6718. if (!$info_de_where->isEmpty() && request()->empcrm->org_type == 2) {
  6719. if ((int)$info_de_where['content'] != 1) {
  6720. return json(['code' => 1, 'msg' => '您的身份为设计师,无法获取客户']);
  6721. }
  6722. }
  6723. //$customer_id = explode(',', $param['customer_id']);
  6724. $customer_id = $param['cid'];
  6725. $otheremp = Customer::where([['org_id', 'in', $orgids], ['id', 'in', $customer_id], ['employee_id', '=', NULL]])->field('employee_id,phone,phone1,phone2')->select();
  6726. if (empty($otheremp)) {
  6727. return json(['code' => 1, 'msg' => '客户已被人抢先获取。记得下次抓住机会。']);
  6728. }
  6729. //判断名下是否存在该客户手机号,存在则不可获取
  6730. foreach ($otheremp as $item) {
  6731. $phone_arr = [];
  6732. !empty($item['phone']) ? $phone_arr[] = cypherphone($item['phone']) : '';
  6733. !empty($item['phone1']) ? $phone_arr[] = cypherphone($item['phone1']) : '';
  6734. !empty($item['phone2']) ? $phone_arr[] = cypherphone($item['phone2']) : '';
  6735. $phone_arr = array_filter($phone_arr);
  6736. $haveCrm = Customer::where([['employee_id', '=', $employee_id], ['phone|phone1|phone2', 'in', $phone_arr]])->count();
  6737. if ($haveCrm) {
  6738. return json(['code' => 1, 'msg' => '您名下已存在该客户信息。']);
  6739. }
  6740. }
  6741. 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')])) {
  6742. $saveAll = [];
  6743. $saveAll[] = [
  6744. 'customer_id' => $customer_id,
  6745. 'type' => 1,
  6746. 'remark' => '公海获取',
  6747. 'employee_id' => $employee_id,
  6748. 'user_id' => $this->request->empcrm->uid,
  6749. 'state' => 1
  6750. ];
  6751. if (CustomerVisitLog::insertAll($saveAll)) {
  6752. return json(['code' => 0, 'msg' => '获取成功']);
  6753. } else {
  6754. return json(['code' => 1, 'msg' => '获取失败']);
  6755. }
  6756. } else {
  6757. return json(['code' => 1, 'msg' => '获取失败']);
  6758. }
  6759. }
  6760. //公海点击展示客户详情
  6761. public function pollcus_detail()
  6762. {
  6763. $request = request();
  6764. $root_id = $request->empcrm->root_id;
  6765. $empid = $request->empcrm->id;
  6766. $param = $this->request->only(['cid']);
  6767. $data = $this->cusdet_noemp($param['cid']);
  6768. View::assign('data', $data);
  6769. return View::fetch();
  6770. }
  6771. //日报页面点击后列表页面
  6772. public function daily_detlist()
  6773. {
  6774. $request = request();
  6775. $root_id = $request->empcrm->root_id;
  6776. $empid = $request->empcrm->id;
  6777. $param = $this->request->only(['date', 'state']);
  6778. if (!$request->isAjax()) {
  6779. //$this->screen();
  6780. View::assign('date', $param['date']);
  6781. View::assign('state', $param['state']);
  6782. return View::fetch();
  6783. }
  6784. $org_employee = [$empid];
  6785. $condition[] = ['employee_id', 'in', $org_employee];
  6786. $condition[] = ['transfer_info', 'null', ''];
  6787. $condition[] = ['state', 'not in', Customer::changeState('无效', 'chaos')];
  6788. //$condition[] = ['state', 'in',array_merge(Customer::changeState('已签单', 'chaos'),Customer::changeState('未到访', 'chaos'))];
  6789. $vlst = '确认到店';
  6790. if ($param['state'] == '到访') {
  6791. $condition[] = ['signed_money', '<>', 0];
  6792. $vlst = '确认到店';
  6793. } elseif ($param['state'] == '交定') {
  6794. $condition[] = ['deposit_money', '<>', 0];
  6795. $condition[] = ['signed_money', 'null', ''];
  6796. $vlst = '交定';
  6797. } elseif ($param['state'] == '签单') {
  6798. $condition[] = ['signed_money', '<>', 0];
  6799. $vlst = '签单';
  6800. }
  6801. $order = isset($param['order']) ? $param['order'] : 'id desc';
  6802. $page = intval($param['page']);
  6803. $limit = intval($param['limit']);
  6804. if (empty($param['date'])) return json(['code' => 1, 'msg' => '参数错误']);
  6805. $where[] = ['employee_id', 'in', $org_employee];
  6806. $where[] = ['state', 'in', CustomerVisitLog::changeState($vlst, 'chaos')];
  6807. if (!empty($param['date'])) {
  6808. $newtime = explode('-', $param['date']);
  6809. $where[] = ['confirm_date', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']];
  6810. }
  6811. $vslogid = CustomerVisitLog::where($where)->column('customer_id');
  6812. if ($param['state'] != '线索') {
  6813. $condition[] = ['id', 'in', $vslogid];
  6814. }
  6815. $data = $this->selCustomer($condition, $page, $limit, $order, $root_id);
  6816. $count = count($data);
  6817. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  6818. }
  6819. /**
  6820. * 2022-11-11 业务修改 签单之后可以编辑签单金额,跟进记录增加 业务员修改签单金额为3000元
  6821. */
  6822. private function updateMoney($param)
  6823. {
  6824. $token = $this->get_token();
  6825. $money = $param['money'] ?: 0;
  6826. $orgids = orgSubIds($token['root_org']);
  6827. $where[] = ['id', '=', $param['customer_id']];
  6828. $where[] = ['org_id', 'in', $orgids];
  6829. $cusdata = Customer::where($where)->find();
  6830. $cusdata->signed_money = $money;
  6831. $cusdata->save();
  6832. //Customer::where($where)->update(['signed_money' => $money]);
  6833. if (isset($param['vlogid']) && $param['vlogid']) CustomerVisitLog::where('id', $param['vlogid'])->update(['money' => $money]);
  6834. $token['name'] = Employee::where([['id', '=', $token['employee_id']]])->value('name');
  6835. $time = date('Y-m-d H:i:s');
  6836. //增加跟进记录
  6837. $save = [
  6838. 'customer_id' => $param['customer_id'],
  6839. 'type' => '',
  6840. 'next_contact_date' => date('Y-m-d'),
  6841. 'remark' => '业务员' . $token['name'] . '修改已签单金额为' . $money . '元',
  6842. 'addtime' => $time,
  6843. 'employee_id' => $token['employee_id'],
  6844. 'user_id' => $token['uid'],
  6845. // 'state' => 3,
  6846. 'state' => 1,
  6847. 'org_id' => $token['org_id'],
  6848. 'customer_employee_id' => $cusdata['employee_id'],
  6849. 'customer_org_id' => $token['org_id'],
  6850. 'money' => $money
  6851. ];
  6852. //2022-10-28 增加交定和签单凭证 凭证为图片
  6853. //$deliverySignVoucher = request()->only(['sign_media_id' => '', 'sign_weixin_media' => '', 'sign_img' => '']);
  6854. //$save = $save + $deliverySignVoucher;
  6855. CustomerVisitLog::insertGetId($save);
  6856. //if ($deliverySignVoucher['sign_media_id']) Console::call('download', ['sign']);
  6857. $msg = '转单金额修改成功.';
  6858. if (request()->empcrm['xinjushang'] == 1) $msg = '签单金额修改成功.';
  6859. return json(['code' => 0, 'msg' => $msg, 'data' => $msg]);
  6860. }
  6861. //客服报表
  6862. public function allemp_report()
  6863. {
  6864. $request = request();
  6865. $root_id = $request->empcrm->root_id;
  6866. $empid = $request->empcrm->id;
  6867. $param = $this->request->only(['cid']);
  6868. }
  6869. /**
  6870. * 报备页面自定义字段
  6871. */
  6872. public function model_report()
  6873. {
  6874. $id = input('id', '', 'trim');
  6875. $root_id = request()->empcrm->root_id;
  6876. $setting = Setting::where([['name', '=', 'CustomerReportSettingForPc'], ['root_id', '=', $root_id]])->find();
  6877. $where = [
  6878. ['root_id', '=', $root_id],
  6879. ['status', '=', 0]
  6880. ];
  6881. if ($setting && !empty($setting['content'])) {
  6882. $all_content = json_decode($setting['content'], true);
  6883. $field_id = array_column($all_content, 'id');
  6884. $where[] = ['id', 'in', $field_id];
  6885. } else {
  6886. //默认值
  6887. $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']];
  6888. }
  6889. //获取第二层
  6890. $all = CustomerPortraitField::with(['select'])->where($where)->orderRaw('if(isnull(sort),1,0),sort asc')->select()->toArray();
  6891. $city = Company::where('root_id', $root_id)->value('city');
  6892. if ($setting && !empty($setting['content'])) {
  6893. $all_new = [];
  6894. foreach ($all_content as $c) {
  6895. foreach ($all as $vals) {
  6896. if ($c['id'] == $vals['id']) {
  6897. $all_new[] = $vals;
  6898. if ($c['keyname'] == 'phone') {
  6899. $all_new[] = ['id' => 0, 'name' => '备用手机号1', 'keyname' => 'phone1', 'pid' => $vals['pid'], 'status' => 0, 'is_must' => 1, 'root_id' => $vals['root_id'], 'type' => 2, 'sort' => ''];
  6900. $all_new[] = ['id' => 01, 'name' => '备用手机号2', 'keyname' => 'phone2', 'pid' => $vals['pid'], 'status' => 0, 'is_must' => 1, 'root_id' => $vals['root_id'], 'type' => 2, 'sort' => ''];
  6901. }
  6902. }
  6903. }
  6904. }
  6905. $all = $all_new;
  6906. }
  6907. foreach ($all as &$val) {
  6908. if ($val['keyname'] == 'current_region') {
  6909. $val['select'][] = ['id' => $root_id, 'name' => $city, 'pid' => $val['id']];
  6910. }
  6911. if ($val['keyname'] == 'source_id') {
  6912. $val['select'] = CustomerSource::field('id,source as name')->where('root_id', $root_id)->order('id desc')->select()->toArray();
  6913. }
  6914. if ($val['keyname'] == 'deco_style') {
  6915. $val['select'] = Decostyle::field('id,name')->where([['root_id', '=', $root_id], ['type', '=', 0]])->select()->toArray();
  6916. }
  6917. if (in_array($val['keyname'], ['live_broadcast_business', 'live_broadcast_design', 'live_broadcast_personnel'])) {
  6918. $val['select'] = $this->live_broadcast_personnel();
  6919. }
  6920. if ($val['keyname'] == 'phone') {
  6921. $newadd = [
  6922. ['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],
  6923. ['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]
  6924. ];
  6925. }
  6926. }
  6927. if (empty($setting) && empty($setting['content'])) {
  6928. $all = array_merge($all, $newadd);
  6929. $sort = array_column($all, 'sort');
  6930. array_multisort($sort, SORT_ASC, $all);
  6931. }
  6932. //获取第一层
  6933. $pid = array_column($all, 'pid');
  6934. $list = CustomerPortraitField::where([['id', 'in', $pid], ['status', '=', 0], ['pid', '=', 0]]);
  6935. if ($setting && !empty($setting['content'])) {
  6936. //如果是添加页面,把自定义字段追加到基本信息child下
  6937. $list = $list->order('id')->limit(1)->select()->toArray();
  6938. } else {
  6939. $list = $list->orderRaw('if(isnull(sort),1,0),sort asc')->select()->toArray();
  6940. }
  6941. //组装数组
  6942. foreach ($list as &$p) {
  6943. foreach ($all as &$item) {
  6944. $item['value'] = '';
  6945. $item['valname'] = '';
  6946. if ($setting && !empty($setting['content'])) {
  6947. $p['child'][] = $item;
  6948. } else {
  6949. if ($p['id'] == $item['pid']) $p['child'][] = $item;
  6950. }
  6951. }
  6952. }
  6953. $cuslog = new CustomerLogic;
  6954. if (isset($id) && !empty($id)) {
  6955. $AllOrgId = orgSubIds($root_id);
  6956. $cusdata = Customer::where([['org_id', 'in', $AllOrgId], ['id', '=', $id]])->find()->toArray();
  6957. if (!empty($cusdata['ext']) && $cusdata['ext'] != 'null') {
  6958. $extdata = json_decode($cusdata['ext'], true);
  6959. if (isset($extdata['ext1'])) {
  6960. $list = $cuslog->old_data_save($cusdata, $list);
  6961. } else {
  6962. $list = $cuslog->new_data_save($cusdata, $extdata, $list);
  6963. }
  6964. } else {
  6965. $list = $cuslog->no_ext_save($cusdata, $list);
  6966. }
  6967. }
  6968. return json(['code' => 0, 'msg' => '获取成功', 'data' => $list]);
  6969. }
  6970. /**
  6971. * 获取直播人员列表(报备自定义页面选择展示)
  6972. */
  6973. private function live_broadcast_personnel()
  6974. {
  6975. $root_id = request()->empcrm->root_id;
  6976. $param = Request()->only(['org_id' => 0, 'name']);
  6977. $path = $param['org_id'] ? Org::where('id', $param['org_id'])->value('path') : $root_id . '-';
  6978. $where[] = ['path', 'like', $path . '%'];
  6979. $empwhere[] = ['e.state', '=', '在职'];
  6980. if (!empty($param['name'])) {
  6981. $empwhere[] = ['e.name', 'like', '%' . $param['name'] . '%'];
  6982. }
  6983. $orgs = Org::with(['employee' => function ($query) use ($empwhere) {
  6984. $query->field('e.id,e.name,user.headimgurl,e.org_id')
  6985. ->alias('e')
  6986. ->where($empwhere)
  6987. ->join('user', 'user.id=e.uid');
  6988. }])->where($where)->order('level asc, id desc')->field('id,name,pid')->select()->toArray();
  6989. $data = [];
  6990. while (!empty($orgs)) {
  6991. $org = array_pop($orgs);
  6992. $childOrg = [
  6993. 'id' => $org['id'],
  6994. 'name' => $org['name'],
  6995. 'pid' => $org['pid'],
  6996. 'designer' => $org['employee'],
  6997. 'designer_num' => count($org['employee'])
  6998. ];
  6999. if (isset($data[$org['id']])) {
  7000. $childOrg['child_org'] = $data[$org['id']]['child_org'];
  7001. $childOrg['designer_num'] += $data[$org['id']]['designer_num'];
  7002. unset($data[$org['id']]);
  7003. }
  7004. $data[$org['pid']]['child_org'][] = $childOrg;
  7005. $data[$org['pid']]['designer_num'] = isset($data[$org['pid']]['designer_num']) ? $data[$org['pid']]['designer_num'] + $childOrg['designer_num'] : $childOrg['designer_num'];
  7006. }
  7007. $data = array_pop($data);
  7008. $result = [
  7009. 'designer' => isset($data['designer']) ? $data['designer'] : [],
  7010. 'child_org' => isset($data['child_org']) ? $data['child_org'] : []
  7011. ];
  7012. return $result;
  7013. }
  7014. /**
  7015. * 客户报备扩展字段添加图片类型
  7016. */
  7017. public function upload_file()
  7018. {
  7019. $ali_oss_bindurl = config('app.ali_oss_bindurl');
  7020. $url = request()->param('file');
  7021. return json(['code' => 0, 'url' => $url]);
  7022. }
  7023. /**
  7024. * 设计师指定
  7025. * 2022-11-08 业务修改, 指派设计师改为指派人员 并且可以指派多人
  7026. * $id 客户id
  7027. * $designerId 指派的人员 1,2,3,4 //为空取消指派人员
  7028. */
  7029. public function reserveDesigner()
  7030. {
  7031. $request = request();
  7032. $param = $this->request->only(['id', 'designerId', 'assign_type']);
  7033. $id = $param['id'];
  7034. $designerId = $param['designerId'];
  7035. $root_id = $request->empcrm->root_id;
  7036. if (isset($request->empcrm->is_manager) && $request->empcrm->is_manager) {
  7037. $customer = Customer::where([['id', '=', $id], ['org_id', 'in', orgSubIds($request->empcrm['root_id'])]])->find();
  7038. } else {
  7039. $customer = Customer::where(['id' => $id, 'employee_id' => $request->empcrm['id']])->find();
  7040. }
  7041. if (empty($customer)) return json(['code' => 1, 'msg' => '客户不存在']);
  7042. if (empty($designerId)) {
  7043. $customer->designer_id = NULL;
  7044. $customer->assigned_personnel = NULL;
  7045. $customer->save();
  7046. return json(['code' => 0, 'msg' => '取消指派成功!']);
  7047. }
  7048. $designerId = explode(',', $designerId);
  7049. $designer = Employee::where([
  7050. ['id', 'in', $designerId], ['root_id', '=', $root_id], ['state', '=', '在职']
  7051. ])->column('org_id', 'id');
  7052. if (empty($designer)) return json(['code' => 1, 'msg' => '指派人员不存在']);
  7053. $orgs = Org::where([['id', 'in', array_values($designer)]])->column('org_type', 'id');
  7054. //第一个选择的设计师
  7055. $selectDesigner = [];
  7056. foreach ($designerId as $k => $v) {
  7057. if (empty($designer[$v]) || $orgs[$designer[$v]] !== 2) continue;
  7058. $selectDesigner[] = $v;
  7059. }
  7060. $designer_id = isset($selectDesigner[0]) ? $selectDesigner[0] : null;
  7061. // 检测是否有设计师已指派满了
  7062. $limitNum = Setting::where(['root_id' => $request->empcrm['root_org'], 'name' => 'designerNum'])->value('content');
  7063. if ($limitNum != 0) {
  7064. //获取当月时间跨度
  7065. $start_time = date('Y-m') . '-01 00:00:00';
  7066. $end_time = date('Y-m-d', strtotime("$start_time +1 month -1 day")) . ' 23:59:59';
  7067. $where[] = ['addtime', 'between', [strtotime($start_time), strtotime($end_time)]];
  7068. $where[] = ['employee_id', 'in', $selectDesigner];
  7069. $had = CustomerSharing::field('employee_id,count(customer_id) as num')->group('employee_id')->where($where)->having("num >= $limitNum")->select();
  7070. // var_dump($had->toArray());
  7071. // exit;
  7072. if (!$had->isEmpty()) return json(['code' => 1, 'msg' => '指派的设计师已无指派名额,请刷新后再试']);
  7073. }
  7074. // 检测部门是否是设计师
  7075. // $orgType = Org::where(['id' => $designer->org_id])->value('org_type');
  7076. // if ($orgType != 2) return json(['code' => 1, 'msg' => '设计师不存在']);
  7077. $customer->designer_id = $designer_id;
  7078. $customer->assigned_personnel = implode(',', array_keys($designer));
  7079. $customer->assign_time = date('Y-m-d H:i:s');
  7080. if (!empty($param['assign_type']) && empty($customer->assign_type)) $customer->assign_type = $param['assign_type'];
  7081. $customer->save();
  7082. // 更新预约未确定的客户所属设计师id
  7083. CustomersSubscribe::where(['customer_id' => $customer->id, 'state' => 0])->update(['designer_id' => $designer_id]);
  7084. $all_assign_type = ['yixiang' => '意向客户指派', 'liangfang' => '量房客户指派', 'daodian' => '到店客户指派'];
  7085. if (!empty($all_assign_type[$customer->assign_type])) {
  7086. $empname_arr = Employee::where([['id', 'in', implode(',', array_keys($designer))], ['root_id', '=', $root_id]])->column('name');
  7087. $remark = $all_assign_type[$customer->assign_type] . '指派至员工' . implode(',', $empname_arr);
  7088. $visit_Log = [
  7089. 'customer_id' => $id,
  7090. 'type' => '',
  7091. 'remark' => $remark,
  7092. 'employee_id' => $request->empcrm->id,
  7093. 'user_id' => $request->empcrm->uid,
  7094. 'state' => 1,
  7095. 'next_contact_date' => date('Y-m-d'),
  7096. 'customer_employee_id' => $customer->employee_id,
  7097. 'customer_org_id' => $customer->org_id,
  7098. 'org_id' => $customer->org_id
  7099. ];
  7100. CustomerVisitLog::create($visit_Log);
  7101. }
  7102. return json(['code' => 0, 'msg' => '指派成功!']);
  7103. }
  7104. /*
  7105. * 审核人员
  7106. */
  7107. public function get_person()
  7108. {
  7109. $ids = input('id', '');
  7110. $root_id = request()->empcrm->root_id;
  7111. $type = input('type', 0);
  7112. //树形
  7113. if ($type == 1) {
  7114. //所有员工
  7115. $w1[] = ['root_id', '=', $root_id];
  7116. $w1[] = ['state', '=', '在职'];
  7117. $w1[] = ['uid', '<>', 0];
  7118. $w1[] = ['org_id', '>', 0];
  7119. $w1[] = ['org_id', '<>', $root_id];
  7120. $w1[] = ['id', '<>', request()->empcrm->id];
  7121. $employee = Employee::where($w1)->field('id value,name,org_id')->select()->toArray();
  7122. if ($ids) {
  7123. $ids = explode(',', $ids);
  7124. foreach ($employee as $k => &$v) {
  7125. $v['selected'] = in_array($v['value'], $ids);
  7126. }
  7127. } else {
  7128. foreach ($employee as $k => &$v) {
  7129. $v['selected'] = false;
  7130. }
  7131. }
  7132. // 查询限定数量
  7133. // $limitNum = (int)Setting::where(['root_id' => $root_id, 'name' => 'designerNum'])->value('content');
  7134. // // 查询员工已制定的人数
  7135. // if ($limitNum != 0) {
  7136. // //获取当月时间跨度
  7137. // $start_time = date('Y-m').'-01 00:00:00';
  7138. // $end_time = date('Y-m-d',strtotime("$start_time +1 month -1 day")).' 23:59:59';
  7139. // $where_d[] = ['addtime', 'between', [strtotime($start_time),strtotime($end_time)]];
  7140. // $where_d[] = ['root_id', '=', $root_id];
  7141. // $empShareNum = CustomerSharing::where($where_d)->group('employee_id')->column('count(customer_id)', 'employee_id');
  7142. // foreach ($employee as &$e) {
  7143. // if (isset($empShareNum[$e['value']]) && $empShareNum[$e['value']] >= $limitNum) $e['disabled'] = true;
  7144. // }
  7145. // }
  7146. $persons = [];
  7147. foreach ($employee as $k => $v1) {
  7148. $persons[$v1['org_id']][] = $v1;
  7149. }
  7150. $where = [
  7151. ['path', 'like', $root_id . '-%'],
  7152. ['status', '=', 1]
  7153. ];
  7154. $allnodes = Org::where($where)->field('id value,id,pid,name')->order('level asc, id asc')->select()->toArray();
  7155. $tree = $this->tree($allnodes, 0, $persons);
  7156. return json($tree);
  7157. }
  7158. $w[] = ['root_id', '=', $root_id];
  7159. $w[] = ['state', '=', '在职'];
  7160. $w[] = ['role', '=', '领导'];
  7161. $w[] = ['uid', '<>', 0];
  7162. $employee = Employee::where($w)->field('id value,name')->select()->toArray();
  7163. if ($ids) {
  7164. $ids = explode(',', $ids);
  7165. foreach ($employee as $k => &$v) {
  7166. $v['selected'] = in_array($v['value'], $ids);
  7167. }
  7168. } else {
  7169. foreach ($employee as $k => &$v) {
  7170. $v['selected'] = false;
  7171. }
  7172. }
  7173. return json($employee);
  7174. }
  7175. public function tree($data, $pid = 0, $persons)
  7176. {
  7177. $new_arr = [];
  7178. foreach ($data as $k => $v) {
  7179. if ($v['pid'] == $pid) {
  7180. $persions = isset($persons[$v['id']]) ? $persons[$v['id']] : [];
  7181. $children = $this->tree($data, $v['id'], $persons);
  7182. $v['children'] = array_merge_recursive($children, $persions);
  7183. //if (empty($v['children'])) $v['disabled'] = true;
  7184. if (empty($v['children'])) continue;
  7185. $new_arr[] = $v;
  7186. }
  7187. }
  7188. return $new_arr;
  7189. }
  7190. //获取客户所有扩展字段
  7191. public function get_all_portrait_field()
  7192. {
  7193. $root_id = request()->empcrm->root_id;
  7194. $w[] = ['root_id', '=', $root_id];
  7195. $param = $this->request->only(['id']);
  7196. $id = $param['id'];
  7197. $AllOrgId = orgSubIds($root_id);
  7198. $cusdata = Customer::where([['org_id', 'in', $AllOrgId], ['id', '=', $id]])->find();
  7199. if (empty($cusdata)) return json(['code' => 1, 'msg' => '客户不存在']);
  7200. $list = CustomerPortraitField::where([['root_id', '=', $root_id], ['pid', '=', 0], ['status', '=', 0]])->orderRaw('if(isnull(sort),1,0),sort asc')->select()->toArray();
  7201. $city = Company::where('root_id', $root_id)->value('city');
  7202. foreach ($list as $key => $val) {
  7203. $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();
  7204. foreach ($child as $k => $v) {
  7205. if ($v['keyname'] == 'current_region') {
  7206. $child[$k]['select'][] = ['id' => $root_id, 'name' => $city, 'pid' => $v['id']];
  7207. }
  7208. if ($v['keyname'] == 'source_id') {
  7209. $soudata = CustomerSource::field('id,source as name')->where('root_id', $root_id)->select()->toArray();
  7210. $child[$k]['select'] = $soudata;
  7211. }
  7212. if ($v['keyname'] == 'deco_style') {
  7213. $decostyles = Decostyle::field('id,name')->where([['root_id', '=', $root_id], ['type', '=', 0]])->select()->toArray();
  7214. $child[$k]['select'] = $decostyles;
  7215. }
  7216. $child[$k]['value'] = '';
  7217. $child[$k]['valname'] = '';
  7218. }
  7219. $list[$key]['child'] = $child;
  7220. }
  7221. $new = $list;
  7222. $cuslog = new CustomerLogic;
  7223. if (!empty($cusdata['ext']) && $cusdata['ext'] != 'null') {
  7224. $extdata = json_decode($cusdata['ext'], true);
  7225. if (isset($extdata['ext1'])) {
  7226. $new = $cuslog->old_data_save($cusdata, $list);
  7227. } else {
  7228. $new = $cuslog->new_data_save($cusdata, $extdata, $list);
  7229. }
  7230. }
  7231. return json(['code' => 0, 'data' => $new, 'msg' => '获取成功']);
  7232. }
  7233. /**
  7234. * 客户池展示字段
  7235. */
  7236. public function poolFields($type = 6)
  7237. {
  7238. // 管理层统计
  7239. $type3 = [
  7240. 'name' => ['name' => '员工姓名', 'show' => 1, 'width' => 100, 'sort' => 1],
  7241. 'customer_count' => ['name' => '客户数量', 'show' => 1, 'width' => 100, 'sort' => 2],
  7242. 'return_visit_count' => ['name' => '待回访客户数', 'show' => 1, 'width' => 150, 'sort' => 3],
  7243. 'no_assigned_personnel' => ['name' => '待指派客户数', 'show' => 1, 'width' => 150, 'sort' => 4],
  7244. 'no_visit_3' => ['name' => '3天内未跟进客户数', 'show' => 1, 'width' => 225, 'sort' => 5],
  7245. 'no_visit_7' => ['name' => '7天内未跟进客户数', 'show' => 1, 'width' => 225, 'sort' => 6],
  7246. 'no_visit_15' => ['name' => '15天内未跟进客户数', 'show' => 1, 'width' => 225, 'sort' => 7],
  7247. 'no_visit_30' => ['name' => '一个月内未跟进客户数', 'show' => 1, 'width' => 250, 'sort' => 8],
  7248. 'no_visit_31' => ['name' => '一个月以上未跟进客户数', 'show' => 1, 'width' => 275, 'sort' => 9],
  7249. 'valid_count' => ['name' => '有效客户', 'show' => 1, 'width' => 100, 'sort' => 10],
  7250. 'valid_grawth' => ['name' => '有效率', 'show' => 1, 'width' => 75, 'sort' => 11],
  7251. 'phone_count' => ['name' => '电话数量', 'show' => 1, 'width' => 100, 'sort' => 12],
  7252. 'on_phone_count' => ['name' => '接通数量', 'show' => 1, 'width' => 100, 'sort' => 13],
  7253. 'on_phone_grawth' => ['name' => '接通率', 'show' => 1, 'width' => 75, 'sort' => 14],
  7254. 'off_phone_count' => ['name' => '未接通', 'show' => 1, 'width' => 75, 'sort' => 15],
  7255. 'avg_phone_time' => ['name' => '平均通话时长', 'show' => 1, 'width' => 150, 'sort' => 16],
  7256. 'add_wechat_count' => ['name' => '加微数量', 'show' => 1, 'width' => 100, 'sort' => 17],
  7257. 'add_wechat_grawth' => ['name' => '加微率', 'show' => 1, 'width' => 75, 'sort' => 18],
  7258. 'room' => ['name' => '量房客户数', 'show' => 1, 'width' => 125, 'sort' => 19],
  7259. 'room_grawth' => ['name' => '量房率', 'show' => 1, 'width' => 75, 'sort' => 20],
  7260. 'store1' => ['name' => '一次到店客户数', 'show' => 1, 'width' => 175, 'sort' => 21],
  7261. 'store1_grawth' => ['name' => '一次到店率', 'show' => 1, 'width' => 125, 'sort' => 22],
  7262. 'store2' => ['name' => '二次到店客户数', 'show' => 1, 'width' => 175, 'sort' => 23],
  7263. 'store2_grawth' => ['name' => '二次到店率', 'show' => 1, 'width' => 125, 'sort' => 24],
  7264. 'store3' => ['name' => '三次及以上到店客户数', 'show' => 1, 'width' => 250, 'sort' => 25],
  7265. 'store3_grawth' => ['name' => '三次及以上到店率', 'show' => 1, 'width' => 200, 'sort' => 26],
  7266. 'store1_dep' => ['name' => '一次到店签单客户数', 'show' => 1, 'width' => 225, 'sort' => 27],
  7267. 'store1_dep_grawth' => ['name' => '一次到店签单率', 'show' => 1, 'width' => 175, 'sort' => 28],
  7268. 'store2_dep' => ['name' => '二次到店签单数', 'show' => 1, 'width' => 175, 'sort' => 29],
  7269. 'store2_dep_grawth' => ['name' => '二次到店签单率', 'show' => 1, 'width' => 175, 'sort' => 30],
  7270. 'store3_dep' => ['name' => '三次及以上到店签单数', 'show' => 1, 'width' => 250, 'sort' => 31],
  7271. 'store3_dep_grawth' => ['name' => '三次及以上到店签单率', 'show' => 1, 'width' => 250, 'sort' => 32],
  7272. 'dep' => ['name' => '总签单数', 'show' => 1, 'width' => 100, 'sort' => 33],
  7273. 'dep_grawth' => ['name' => '总签单率', 'show' => 1, 'width' => 100, 'sort' => 34],
  7274. 'drawing_date' => ['name' => '出方案客户数', 'show' => 1, 'width' => 150, 'sort' => 35],
  7275. 'sign_count' => ['name' => '转单客户', 'show' => 1, 'width' => 100, 'sort' => 36],
  7276. 'sign_grawth' => ['name' => '转单率', 'show' => 1, 'width' => 75, 'sort' => 37],
  7277. // 'area'=> ['name'=> '面积分布', 'show'=> 1, 'width'=> 100, 'sort'=> 38],
  7278. 'square_0_80' => ['name' => '80以下', 'show' => 1, 'width' => 100, 'parent' => 'area', 'sort' => 38],
  7279. 'square_80_100' => ['name' => '81-100', 'show' => 1, 'width' => 100, 'parent' => 'area', 'sort' => 39],
  7280. 'square_100_120' => ['name' => '100-120', 'show' => 1, 'width' => 100, 'parent' => 'area', 'sort' => 40],
  7281. 'square_120_200' => ['name' => '121-200', 'show' => 1, 'width' => 100, 'parent' => 'area', 'sort' => 41],
  7282. 'square_200_500' => ['name' => '200-500', 'show' => 1, 'width' => 100, 'parent' => 'area', 'sort' => 42],
  7283. 'square_500' => ['name' => '500以上', 'show' => 1, 'width' => 100, 'parent' => 'area', 'sort' => 43],
  7284. 'existing_homes_count' => ['name' => '现房数量', 'show' => 1, 'width' => 100, 'sort' => 44],
  7285. 'forward_housing_count' => ['name' => '期房数量', 'show' => 1, 'width' => 100, 'sort' => 45],
  7286. ];
  7287. // 客户统计列表
  7288. $type4 = [
  7289. 'name' => ['name' => '客户姓名', 'show' => 1, 'width' => 100, 'sort' => 1],
  7290. 'phone' => ['name' => '手机号', 'show' => 1, 'width' => 125, 'sort' => 2],
  7291. 'community_name' => ['name' => '小区', 'show' => 1, 'width' => 150, 'sort' => 3],
  7292. 'house_type' => ['name' => '房屋类型', 'show' => 1, 'width' => 100, 'sort' => 4],
  7293. 'house_delivery_time' => ['name' => '交房时间', 'show' => 1, 'width' => 100, 'sort' => 5],
  7294. 'square' => ['name' => '面积', 'show' => 1, 'width' => 75, 'sort' => 6],
  7295. 'org_name' => ['name' => '部门', 'show' => 1, 'width' => 120, 'sort' => 7],
  7296. 'employee_name' => ['name' => '业务员', 'show' => 1, 'width' => 90, 'sort' => 8],
  7297. 'addtime' => ['name' => '添加时间', 'show' => 1, 'width' => 170, 'sort' => 9],
  7298. 'source_name' => ['name' => '客户来源', 'show' => 1, 'width' => 100, 'sort' => 10],
  7299. 'assigned_personal_manager' => ['name' => '指派客户经理', 'show' => 1, 'width' => 150, 'sort' => 11],
  7300. 'assigned_personal_designer_org' => ['name' => '指派设计部', 'show' => 1, 'width' => 125, 'sort' => 12],
  7301. 'assigned_personal_designer' => ['name' => '指派设计师', 'show' => 1, 'width' => 125, 'sort' => 13],
  7302. 'no_visit_day' => ['name' => '未跟踪天数', 'show' => 1, 'width' => 125, 'sort' => 14],
  7303. 'wechat' => ['name' => '是否加微信', 'show' => 1, 'width' => 125, 'sort' => 15],
  7304. 'add_wechat_type' => ['name' => '加微类型', 'show' => 1, 'width' => 100, 'sort' => 16],
  7305. 'add_wechat_time' => ['name' => '加微时间', 'show' => 1, 'width' => 120, 'sort' => 17],
  7306. 'group_building_date' => ['name' => '建群时间', 'show' => 1, 'width' => 250, 'sort' => 18],
  7307. 'is_liangfang' => ['name' => '是否量房', 'show' => 1, 'width' => 125, 'sort' => 19],
  7308. 'liangfang_date' => ['name' => '量房时间', 'show' => 1, 'width' => 170, 'sort' => 20],
  7309. 'once_liangfang_days' => ['name' => '量房周期', 'show' => 1, 'width' => 120, 'sort' => 21],
  7310. 'plan_issuing_date' => ['name' => '计划出初步方案时间', 'show' => 1, 'width' => 225, 'sort' => 22],
  7311. 'once_daodian' => ['name' => '是否一次到店', 'show' => 1, 'width' => 150, 'sort' => 23],
  7312. 'daodian_date' => ['name' => '一次到店时间', 'show' => 1, 'width' => 170, 'sort' => 24],
  7313. 'once_daodian_shop' => ['name' => '一次所到店面', 'show' => 1, 'width' => 150, 'sort' => 25],
  7314. 'once_daodian_days' => ['name' => '一次到店周期', 'show' => 1, 'width' => 150, 'sort' => 26],
  7315. 'liangfang_daodian_days' => ['name' => '量房到店周期', 'show' => 1, 'width' => 150, 'sort' => 27],
  7316. 'twice_daodian_date' => ['name' => '二次到店时间', 'show' => 1, 'width' => 170, 'sort' => 28],
  7317. 'twice_daodian_shop' => ['name' => '二次所到店面', 'show' => 1, 'width' => 150, 'sort' => 29],
  7318. 'twice_daodian_days' => ['name' => '二次到店周期', 'show' => 1, 'width' => 150, 'sort' => 30],
  7319. 'is_deposit' => ['name' => '是否交定/签单', 'show' => 1, 'width' => 170, 'sort' => 31],
  7320. 'deposit_date' => ['name' => '交定/签单时间', 'show' => 1, 'width' => 170, 'sort' => 32],
  7321. 'drawing_date' => ['name' => '出图时间', 'show' => 1, 'width' => 100, 'sort' => 33],
  7322. 'sign_date' => ['name' => '转单时间', 'show' => 1, 'width' => 170, 'sort' => 34],
  7323. 'sign_days' => ['name' => '交定转单周期', 'show' => 1, 'width' => 150, 'sort' => 35],
  7324. 'signed_money_data' => ['name' => '合同金额(元)', 'show' => 1, 'width' => 125, 'sort' => 36],
  7325. 'first_visit_no_sign' => ['name' => '一次到店未签单时间', 'show' => 1, 'width' => 200, 'sort' => 37],
  7326. 'manager_visit_times' => ['name' => '客户经理回访次数', 'show' => 1, 'width' => 200, 'sort' => 38],
  7327. 'manager_visit_cycle' => ['name' => '客户经理回访周期', 'show' => 1, 'width' => 100, 'sort' => 39],
  7328. 'employee_visit_times' => ['name' => '经理回访次数', 'show' => 1, 'width' => 150, 'sort' => 40],
  7329. 'employee_visit_cycle' => ['name' => '经理回访平均周期', 'show' => 1, 'width' => 200, 'sort' => 41],
  7330. 'invalid' => ['name' => '无效客户', 'show' => 1, 'width' => 100, 'sort' => 42],
  7331. 'invalid_remark' => ['name' => '无效原因', 'show' => 1, 'width' => 100, 'sort' => 43],
  7332. 'died' => ['name' => '是否死单', 'show' => 1, 'width' => 100, 'sort' => 44],
  7333. 'customer_type' => ['name' => '客户种类', 'show' => 1, 'width' => 100, 'sort' => 11],
  7334. ];
  7335. // 客户池字段
  7336. $field6 = [
  7337. 'name' => ['name' => '客户姓名', 'show' => 1, 'width' => 120, 'sort' => 1],
  7338. 'phone' => ['name' => '手机号', 'show' => 1, 'width' => 100, 'sort' => 2],
  7339. 'employee_name' => ['name' => '所属员工', 'show' => 1, 'width' => 120, 'sort' => 3],
  7340. 'designer_name' => ['name' => '设计师', 'show' => 1, 'width' => 120, 'sort' => 4],
  7341. 'source_name' => ['name' => '来源渠道', 'show' => 1, 'width' => 120, 'sort' => 5],
  7342. 'add_wechat_time' => ['name' => '加微时间', 'show' => 1, 'width' => 120, 'sort' => 6],
  7343. 'customer_type' => ['name' => '客户种类', 'show' => 1, 'width' => 120, 'sort' => 7],
  7344. 'community_name' => ['name' => '小区名称', 'show' => 1, 'width' => 160, 'sort' => 8],
  7345. 'level' => ['name' => '重要', 'show' => 1, 'width' => 160, 'sort' => 9],
  7346. 'sign_time' => ['name' => '报名时间', 'show' => 1, 'width' => 160, 'sort' => 10],
  7347. 'house_delivery_time' => ['name' => '交房时间', 'show' => 1, 'width' => 120, 'sort' => 11],
  7348. 'state' => ['name' => '状态', 'show' => 1, 'width' => 120, 'sort' => 12],
  7349. 'square' => ['name' => '面积', 'show' => 1, 'width' => 100, 'sort' => 13],
  7350. 'last_contact_date' => ['name' => '跟进时间', 'show' => 1, 'width' => 160, 'sort' => 14],
  7351. 'revisit_time' => ['name' => '下次回访时间', 'show' => 1, 'width' => 160, 'sort' => 15],
  7352. 'addtime' => ['name' => '添加时间', 'show' => 1, 'width' => 160, 'sort' => 16],
  7353. 'protected_to' => ['name' => '保护至', 'show' => 1, 'width' => 160, 'sort' => 17],
  7354. 'visit_log_count' => ['name' => '预约次数', 'show' => 1, 'width' => 160, 'sort' => 18],
  7355. 'activity_frequency_count' => ['name' => '参加活动次数', 'show' => 1, 'width' => 160, 'sort' => 19],
  7356. 'lognum' => ['name' => '跟进次数', 'show' => 1, 'width' => 150, 'sort' => 20],
  7357. 'no_visit_day' => ['name' => '未跟进天数', 'show' => 1, 'width' => 150, 'sort' => 21],
  7358. 'subscribe_date' => ['name' => '预计见面时间', 'show' => 1, 'width' => 150, 'sort' => 22],
  7359. 'first_liangfang' => ['name' => '首次量房时间', 'show' => 1, 'width' => 150, 'sort' => 23],
  7360. 'fisttime' => ['name' => '首次到店时间', 'show' => 1, 'width' => 150, 'sort' => 24],
  7361. 'wuyou_money' => ['name' => '无忧金额', 'show' => 1, 'width' => 120, 'sort' => 24],
  7362. 'jiaoding_time' => ['name' => '签单时间', 'show' => 1, 'width' => 120, 'sort' => 25],
  7363. 'jiaoding_money' => ['name' => '签单金额', 'show' => 1, 'width' => 120, 'sort' => 26],
  7364. 'estimated_sign_money' => ['name' => '定金签单金额', 'show' => 1, 'width' => 120, 'sort' => 26],
  7365. 'qiandan_time' => ['name' => '转单时间', 'show' => 1, 'width' => 120, 'sort' => 27],
  7366. 'qiandan_money' => ['name' => '转单金额', 'show' => 1, 'width' => 120, 'sort' => 28],
  7367. 'assign_list' => ['name' => '指派客户经理', 'show' => 1, 'width' => 160, 'sort' => 29],
  7368. 'emporg' => ['name' => '业务员部门', 'show' => 1, 'width' => 160, 'sort' => 30],
  7369. 'designer_org' => ['name' => '设计师部门', 'show' => 1, 'width' => 150, 'sort' => 31]
  7370. ];
  7371. // 员工统计
  7372. $field7 = [
  7373. 'name' => ['name' => '业务员名称', 'show' => 1, 'width' => 100, 'parent' => 'name', 'sort' => 1],
  7374. 'org_name' => ['name' => '业务员所在部门', 'show' => 1, 'width' => 130, 'parent' => 'org_name', 'sort' => 2],
  7375. 'customer_count' => ['name' => '线索量', 'show' => 1, 'width' => 120, 'parent' => 'customer_data', 'sort' => 3],
  7376. 'connecting_count' => ['name' => '拨打电话数量', 'show' => 1, 'width' => 120, 'parent' => 'customer_data', 'sort' => 4],
  7377. 'connecting_capacity' => ['name' => '接通数量', 'show' => 1, 'width' => 100, 'parent' => 'call_count', 'sort' => 5],
  7378. 'connecting_capacity_grawth' => ['name' => '接通率', 'show' => 1, 'width' => 100, 'parent' => 'call_count', 'sort' => 6],
  7379. 'connecting_no' => ['name' => '未接通数量', 'show' => 1, 'width' => 100, 'parent' => 'call_count', 'sort' => 7],
  7380. 'phone_time' => ['name' => '总通话时长(分)', 'show' => 1, 'width' => 130, 'parent' => 'call_count', 'sort' => 8],
  7381. 'average_duration' => ['name' => '平均通话时长(秒)', 'show' => 1, 'width' => 140, 'parent' => 'call_count', 'sort' => 9],
  7382. 'follow_customer_count' => ['name' => '跟进客户数', 'show' => 1, 'width' => 100, 'parent' => 'customer_data', 'sort' => 10],
  7383. 'add_wechat_count' => ['name' => '加微客户数量', 'show' => 1, 'width' => 120, 'parent' => 'behavior', 'sort' => 11],
  7384. 'not_sure_count' => ['name' => '待确认客户数', 'show' => 1, 'width' => 120, 'parent' => 'behavior', 'sort' => 12],
  7385. 'add_wechat_count_grawth' => ['name' => '加微率', 'show' => 1, 'width' => 100, 'parent' => 'behavior', 'sort' => 13],
  7386. 'valid_count' => ['name' => '有效线索数', 'show' => 1, 'width' => 100, 'parent' => 'customer', 'sort' => 14],
  7387. 'valid_grawth' => ['name' => '有效线索率(%)', 'show' => 1, 'width' => 100, 'parent' => 'customer', 'sort' => 15],
  7388. 'valid_num' => ['name' => '有效客户数', 'show' => 1, 'width' => 100, 'parent' => 'customer', 'sort' => 15],
  7389. //'not_following_up_count'=> ['name'=> '未跟进客户数', 'show'=> 1, 'width'=> 120, 'parent'=> 'customer_data', 'sort'=> 16],
  7390. 'yixiang_assigned_count' => ['name' => '意向分派客户数', 'show' => 1, 'width' => 130, 'parent' => 'customer_data', 'sort' => 17],
  7391. 'daodian_assigned_count' => ['name' => '到店分派客户数', 'show' => 1, 'width' => 130, 'parent' => 'customer_data', 'sort' => 18],
  7392. 'measuring_assigned_count' => ['name' => '量房分派客户数', 'show' => 1, 'width' => 130, 'parent' => 'customer_data', 'sort' => 19],
  7393. 'number_of_explanations' => ['name' => '智慧屏讲解次数', 'show' => 1, 'width' => 130, 'parent' => 'behavior', 'sort' => 20],
  7394. 'measuring_room_customer' => ['name' => '量房客户数', 'show' => 1, 'width' => 100, 'parent' => 'customer', 'sort' => 21],
  7395. 'measuring_room_zhouqi' => ['name' => '平均量房周期(天)', 'show' => 1, 'width' => 150, 'parent' => 'customer', 'sort' => 22],
  7396. 'measuring_room_grawth' => ['name' => '量房率', 'show' => 1, 'width' => 100, 'parent' => 'customer', 'sort' => 23],
  7397. 'to_the_store' => ['name' => '到店数', 'show' => 1, 'width' => 130, 'parent' => 'daodian_count', 'sort' => 24],
  7398. 'strtore_2v1' => ['name' => '二访比率', 'show' => 1, 'width' => 130, 'parent' => 'daodian_count', 'sort' => 25],
  7399. 'strtore_avg_days_1' => ['name' => '平均到店周期(天)', 'show' => 1, 'width' => 150, 'parent' => 'daodian_avg', 'sort' => 25],
  7400. 'strtore_customer_count' => ['name' => '到店客户数', 'show' => 1, 'width' => 100, 'parent' => 'daodian_count', 'sort' => 26],
  7401. 'strtore_customer_grawth' => ['name' => '到店率', 'show' => 1, 'width' => 100, 'parent' => 'daodian_grawth', 'sort' => 27],
  7402. 'deposit_count' => ['name' => '签单客户数量', 'show' => 1, 'width' => 100, 'parent' => 'customer_data', 'sort' => 28],
  7403. 'deposit_avg' => ['name' => '签单率', 'show' => 1, 'width' => 150, 'parent' => 'daodian_deposit', 'sort' => 29],
  7404. 'deposit_avg_days' => ['name' => '平均签单周期', 'show' => 1, 'width' => 120, 'parent' => 'customer', 'sort' => 30],
  7405. 'sign_count' => ['name' => '转单客户数量', 'show' => 1, 'width' => 100, 'parent' => 'customer', 'sort' => 31],
  7406. 'sign_count_grawth' => ['name' => '转单率', 'show' => 1, 'width' => 100, 'parent' => 'customer', 'sort' => 32],
  7407. 'avg_visit_count' => ['name' => '平均跟进次数', 'show' => 1, 'width' => 120, 'parent' => 'customer', 'sort' => 33],
  7408. 'signed_money' => ['name' => '平均合同金额(元)', 'show' => 1, 'width' => 150, 'parent' => 'customer', 'sort' => 34],
  7409. 'signed_money_all' => ['name' => '合同总额(元)', 'show' => 1, 'width' => 150, 'parent' => 'customer', 'sort' => 35],
  7410. 'invalid_customer_count' => ['name' => '无效客户数', 'show' => 1, 'width' => 100, 'parent' => 'behavior', 'sort' => 35],
  7411. 'square' => ['name' => '面积', 'show' => 1, 'width' => 100, 'parent' => 'area', 'sort' => 36],
  7412. 'house_status' => ['name' => '房屋状态', 'show' => 1, 'width' => 100, 'parent' => 'area', 'sort' => 37],
  7413. 'house_type' => ['name' => '房屋类型', 'show' => 1, 'width' => 100, 'parent' => 'area', 'sort' => 38],
  7414. 'shares_count' => ['name' => '调用内容数量', 'show' => 1, 'width' => 110, 'parent' => 'behavior', 'sort' => 39],
  7415. 'assessment_times' => ['name' => '考核次数', 'show' => 1, 'width' => 100, 'parent' => 'exam_data', 'sort' => 40],
  7416. 'number_of_passes' => ['name' => '通过次数', 'show' => 1, 'width' => 100, 'parent' => 'exam_data', 'sort' => 41],
  7417. ];
  7418. // 指派客户字段
  7419. $field8 = [
  7420. 'name' => ['name' => '客户姓名', 'show' => 1, 'width' => 120, 'sort' => 1],
  7421. 'phone' => ['name' => '手机号', 'show' => 1, 'width' => 100, 'sort' => 2],
  7422. 'employee_name' => ['name' => '所属员工', 'show' => 1, 'width' => 120, 'sort' => 3],
  7423. 'designer_name' => ['name' => '设计师', 'show' => 1, 'width' => 120, 'sort' => 4],
  7424. 'source_name' => ['name' => '来源渠道', 'show' => 1, 'width' => 120, 'sort' => 5],
  7425. 'community_name' => ['name' => '小区名称', 'show' => 1, 'width' => 160, 'sort' => 6],
  7426. 'level' => ['name' => '重要', 'show' => 1, 'width' => 160, 'sort' => 7],
  7427. 'square' => ['name' => '面积', 'show' => 1, 'width' => 100, 'sort' => 8],
  7428. 'state' => ['name' => '状态', 'show' => 1, 'width' => 120, 'sort' => 9],
  7429. 'assign_list' => ['name' => '指派客户经理', 'show' => 1, 'width' => 160, 'sort' => 10],
  7430. 'assign_time' => ['name' => '指派时间', 'show' => 1, 'width' => 160, 'sort' => 11],
  7431. 'assign_org' => ['name' => '指派部门', 'show' => 1, 'width' => 160, 'sort' => 12],
  7432. 'assign_type_name' => ['name' => '指派类型', 'show' => 1, 'width' => 160, 'sort' => 13],
  7433. 'last_contact_date' => ['name' => '跟进时间', 'show' => 1, 'width' => 160, 'sort' => 14],
  7434. 'no_visit_day' => ['name' => '未跟进天数', 'show' => 1, 'width' => 150, 'sort' => 15],
  7435. 'liangfang_time' => ['name' => '量房时间', 'show' => 1, 'width' => 150, 'sort' => 16],
  7436. 'daodian_time' => ['name' => '到店时间', 'show' => 1, 'width' => 150, 'sort' => 17],
  7437. 'jiaoding_time' => ['name' => '签单时间', 'show' => 1, 'width' => 120, 'sort' => 18],
  7438. 'jiaoding_money' => ['name' => '签单金额', 'show' => 1, 'width' => 120, 'sort' => 19],
  7439. 'qiandan_time' => ['name' => '转单时间', 'show' => 1, 'width' => 120, 'sort' => 20],
  7440. 'qiandan_money' => ['name' => '转单金额', 'show' => 1, 'width' => 120, 'sort' => 21]
  7441. ];
  7442. // 到店客户字段
  7443. $field9 = [
  7444. 'name' => ['name' => '客户姓名', 'show' => 1, 'width' => 120, 'sort' => 1],
  7445. 'phone' => ['name' => '手机号', 'show' => 1, 'width' => 100, 'sort' => 2],
  7446. 'employee_name' => ['name' => '所属员工', 'show' => 1, 'width' => 120, 'sort' => 3],
  7447. 'designer_name' => ['name' => '设计师', 'show' => 1, 'width' => 120, 'sort' => 4],
  7448. 'source_name' => ['name' => '来源渠道', 'show' => 1, 'width' => 120, 'sort' => 5],
  7449. 'community_name' => ['name' => '小区名称', 'show' => 1, 'width' => 160, 'sort' => 6],
  7450. 'level' => ['name' => '重要', 'show' => 1, 'width' => 160, 'sort' => 7],
  7451. 'square' => ['name' => '面积', 'show' => 1, 'width' => 100, 'sort' => 8],
  7452. 'state' => ['name' => '状态', 'show' => 1, 'width' => 120, 'sort' => 9],
  7453. 'assign_list' => ['name' => '指派客户经理', 'show' => 1, 'width' => 160, 'sort' => 10],
  7454. 'assign_time' => ['name' => '指派时间', 'show' => 1, 'width' => 160, 'sort' => 11],
  7455. 'assign_org' => ['name' => '指派部门', 'show' => 1, 'width' => 160, 'sort' => 12],
  7456. 'assign_type_name' => ['name' => '指派类型', 'show' => 1, 'width' => 160, 'sort' => 13],
  7457. 'last_contact_date' => ['name' => '跟进时间', 'show' => 1, 'width' => 160, 'sort' => 14],
  7458. 'no_visit_day' => ['name' => '未跟进天数', 'show' => 1, 'width' => 150, 'sort' => 15],
  7459. 'liangfang_time' => ['name' => '量房时间', 'show' => 1, 'width' => 150, 'sort' => 16],
  7460. 'daodian_time' => ['name' => '到店时间', 'show' => 1, 'width' => 150, 'sort' => 17],
  7461. 'jiaoding_time' => ['name' => '签单时间', 'show' => 1, 'width' => 120, 'sort' => 18],
  7462. 'jiaoding_money' => ['name' => '签单金额', 'show' => 1, 'width' => 120, 'sort' => 19],
  7463. 'qiandan_time' => ['name' => '转单时间', 'show' => 1, 'width' => 120, 'sort' => 20],
  7464. 'qiandan_money' => ['name' => '转单金额', 'show' => 1, 'width' => 120, 'sort' => 21],
  7465. 'customer_type' => ['name' => '客户种类', 'show' => 1, 'width' => 120, 'sort' => 22],
  7466. ];
  7467. // 待回访客户字段
  7468. $field10 = [
  7469. 'name' => ['name' => '客户姓名', 'show' => 1, 'width' => 120, 'sort' => 1],
  7470. 'phone' => ['name' => '手机号', 'show' => 1, 'width' => 100, 'sort' => 2],
  7471. 'employee_name' => ['name' => '所属员工', 'show' => 1, 'width' => 120, 'sort' => 3],
  7472. 'designer_name' => ['name' => '设计师', 'show' => 1, 'width' => 120, 'sort' => 4],
  7473. 'source_name' => ['name' => '来源渠道', 'show' => 1, 'width' => 120, 'sort' => 5],
  7474. 'community_name' => ['name' => '小区名称', 'show' => 1, 'width' => 160, 'sort' => 6],
  7475. 'level' => ['name' => '重要', 'show' => 1, 'width' => 160, 'sort' => 7],
  7476. 'square' => ['name' => '面积', 'show' => 1, 'width' => 100, 'sort' => 8],
  7477. 'last_contact_date' => ['name' => '上次回访', 'show' => 1, 'width' => 100, 'sort' => 9],
  7478. 'jiange_day' => ['name' => '间隔', 'show' => 1, 'width' => 100, 'sort' => 10]
  7479. ];
  7480. $fileds12 = [
  7481. 'name' => ['name' => '客户姓名', 'show' => 1, 'width' => 100, 'sort' => 1],
  7482. 'telephone' => ['name' => '手机号', 'show' => 1, 'width' => 100, 'sort' => 2],
  7483. 'weixin' => ['name' => '微信', 'show' => 1, 'width' => 150, 'sort' => 3],
  7484. 'clue_owner_name' => ['name' => '所属员工', 'show' => 1, 'width' => 150, 'sort' => 4],
  7485. 'org_id' => ['name' => '所属部门', 'show' => 1, 'width' => 150, 'sort' => 4],
  7486. // 'follow_state_name'=> ['name'=> '通话状态', 'show'=> 0, 'width'=> 225, 'sort'=> 5],
  7487. 'gender' => ['name' => '性别', 'show' => 1, 'width' => 225, 'sort' => 6],
  7488. 'tags' => ['name' => '线索标签', 'show' => 1, 'width' => 225, 'sort' => 7],
  7489. 'clue_state_name' => ['name' => '线索阶段', 'show' => 0, 'width' => 250, 'sort' => 8],
  7490. 'convert_status' => ['name' => '转化状态', 'show' => 1, 'width' => 275, 'sort' => 9],
  7491. 'external_url' => ['name' => '落地页链接', 'show' => 1, 'width' => 100, 'sort' => 10],
  7492. 'create_time_detail' => ['name' => '线索创建时间', 'show' => 1, 'width' => 75, 'sort' => 11],
  7493. 'clue_source' => ['name' => '线索来源', 'show' => 1, 'width' => 100, 'sort' => 12],
  7494. 'allocation_status' => ['name' => '分配状态', 'show' => 0, 'width' => 100, 'sort' => 13],
  7495. 'remark' => ['name' => '备注留言', 'show' => 1, 'width' => 75, 'sort' => 14],
  7496. 'remark_dict' => ['name' => '面积-风格', 'show' => 1, 'width' => 100, 'sort' => 17],
  7497. 'address' => ['name' => '详细地址', 'show' => 1, 'width' => 75, 'sort' => 15],
  7498. 'advertiser_id' => ['name' => '广告主ID', 'show' => 1, 'width' => 150, 'sort' => 16],
  7499. 'advertiser_name' => ['name' => '广告主名称', 'show' => 1, 'width' => 100, 'sort' => 17],
  7500. // 'age'=> ['name'=> '线索ID', 'show'=> 0, 'width'=> 75, 'sort'=> 18],
  7501. 'ad_id' => ['name' => '计划ID', 'show' => 0, 'width' => 125, 'sort' => 19],
  7502. 'ad_name' => ['name' => '计划名', 'show' => 0, 'width' => 75, 'sort' => 20],
  7503. 'creative_id' => ['name' => '广告创意id', 'show' => 0, 'width' => 175, 'sort' => 21],
  7504. 'promotion_name' => ['name' => '升级版广告名称', 'show' => 0, 'width' => 125, 'sort' => 22],
  7505. 'promotion_id' => ['name' => '升级版广告ID', 'show' => 0, 'width' => 175, 'sort' => 23],
  7506. 'site_id' => ['name' => '站点ID', 'show' => 0, 'width' => 125, 'sort' => 24],
  7507. 'site_name' => ['name' => '站点名称', 'show' => 0, 'width' => 250, 'sort' => 25],
  7508. 'intention_estimation' => ['name' => '线索意向', 'show' => 0, 'width' => 200, 'sort' => 26],
  7509. 'clue_type' => ['name' => '组件类型', 'show' => 0, 'width' => 225, 'sort' => 27],
  7510. 'module_name' => ['name' => '组件名字', 'show' => 0, 'width' => 175, 'sort' => 28],
  7511. 'email' => ['name' => '邮箱', 'show' => 0, 'width' => 175, 'sort' => 29],
  7512. 'qq' => ['name' => 'QQ', 'show' => 0, 'width' => 175, 'sort' => 30],
  7513. 'location' => ['name' => '地址', 'show' => 0, 'width' => 250, 'sort' => 31],
  7514. 'app_name' => ['name' => '流量来源', 'show' => 0, 'width' => 250, 'sort' => 32],
  7515. ];
  7516. $root_id = request()->empcrm->root_id;
  7517. //房屋状态是否开启
  7518. $house_status_where = [
  7519. ['root_id', '=', $root_id],
  7520. ['keyname', '=', 'house_status'],
  7521. ['status', '=', 0]
  7522. ];
  7523. $house_status_per = CustomerPortraitField::where($house_status_where)->findOrEmpty();
  7524. $house_field = [];
  7525. if (!$house_status_per->isEmpty()) {
  7526. $field = CustomerPortraitFieldSelect::where('pid', $house_status_per->id)->column('name,id');
  7527. $fields = [];
  7528. foreach ($field as $v) {
  7529. $fields['house_status_' . $v['id']] = [
  7530. 'name' => $v['name'],
  7531. 'show' => 1,
  7532. 'width' => 100,
  7533. 'parent' => 'house',
  7534. 'sort' => 50
  7535. ];
  7536. }
  7537. $house_field = $fields;
  7538. }
  7539. // 户型
  7540. $housetype_where = [
  7541. ['root_id', '=', $root_id],
  7542. ['keyname', '=', 'housetype_arrow'],
  7543. ['status', '=', 0]
  7544. ];
  7545. $housetype_per = CustomerPortraitField::where($housetype_where)->findOrEmpty();
  7546. $housetype_field = [];
  7547. $housetype_done_field = [];
  7548. if (!$housetype_per->isEmpty()) {
  7549. $field = CustomerPortraitFieldSelect::where('pid', $housetype_per->id)->column('name,id');
  7550. $fields = [];
  7551. $fields_done = [];
  7552. foreach ($field as $v) {
  7553. $fields['housetype_arrow_' . $v['id']] = [
  7554. 'name' => $v['name'],
  7555. 'show' => 1,
  7556. 'width' => 100,
  7557. 'parent' => 'house',
  7558. 'sort' => 51
  7559. ];
  7560. $fields_done['housetype_arrow_lv_' . $v['id']] = [
  7561. 'name' => $v['name'] . '成交率',
  7562. 'show' => 1,
  7563. 'width' => 150,
  7564. 'parent' => 'house',
  7565. 'sort' => 51
  7566. ];
  7567. }
  7568. $housetype_field = $fields;
  7569. $housetype_done_field = $fields_done;
  7570. }
  7571. $field7 = array_merge($field7, $housetype_field);
  7572. $field7 = array_merge($field7, $housetype_done_field);
  7573. // 管理层户型分布
  7574. $new_type_3 = [];
  7575. foreach ($type3 as $k => $v) {
  7576. $new_type_3[$k] = $v;
  7577. if ($k == 'area') {
  7578. $new_type_3 = array_merge($new_type_3, $house_field);
  7579. }
  7580. }
  7581. switch ($type) {
  7582. case 3: // 管理层统计全部字段
  7583. return $type3;
  7584. break;
  7585. case 4: // 客户统计全部字段
  7586. return $type4;
  7587. break;
  7588. case 6: //客户池
  7589. return $field6;
  7590. break;
  7591. case 7: // 总统计
  7592. return $field7;
  7593. break;
  7594. case 8: // 指派客户
  7595. return $field8;
  7596. break;
  7597. case 9: // 到店客户
  7598. return $field9;
  7599. break;
  7600. case 10: //待回访客户
  7601. return $field10;
  7602. break;
  7603. case 12:
  7604. return $fileds12;
  7605. break;
  7606. default:
  7607. break;
  7608. }
  7609. }
  7610. /**
  7611. * 客户池展示字段
  7612. */
  7613. public function poolFieldsList()
  7614. {
  7615. $type = input('type', '', 'intval');
  7616. $is_org = input('is_org', '', 'intval'); // 是否是部门统计 目前只有总统计
  7617. $root_id = request()->empcrm->root_id;
  7618. $employee_id = request()->empcrm->id;
  7619. $find = StatisticsSetting::where([['type', '=', $type], ['employee_id', '=', $employee_id], ['root_id', '=', $root_id]])->findOrEmpty();
  7620. $default_fields = $this->poolFields($type);
  7621. if (!$find->isEmpty()) {
  7622. $set = json_decode($find['content'], true);
  7623. if (!empty($set)) {
  7624. foreach ($set as $k => $v) {
  7625. if (isset($default_fields[$k])) {
  7626. $default_fields[$k]['show'] = $v['show'];
  7627. $default_fields[$k]['sort'] = $v['sort'];
  7628. }
  7629. }
  7630. array_multisort(array_column($default_fields, 'sort'), SORT_ASC, $default_fields);
  7631. }
  7632. }
  7633. if ($type == 6) {
  7634. $field_6 = [];
  7635. foreach ($default_fields as $k => $v) {
  7636. if ($k == 'name') {
  7637. unset($default_fields[$k]);
  7638. $field_6[$k] = $v;
  7639. }
  7640. // if (!empty($field_6) && $k == 'phone') {
  7641. // unset($default_fields[$k]);
  7642. // $field_6[$k] = $v;
  7643. // }
  7644. }
  7645. $field_6 = array_merge($field_6, $default_fields);
  7646. $default_fields = $field_6;
  7647. }
  7648. if ($type == 7) {
  7649. if ($is_org == 1) {
  7650. $field_7['org_name'] = ['name' => '部门名称', 'show' => 1, 'width' => 100, 'sort' => 1];
  7651. foreach ($default_fields as $k => $v) {
  7652. if ($k == 'name' || $k == 'org_name' || $k == 'look_data') {
  7653. unset($default_fields[$k]);
  7654. }
  7655. }
  7656. $field_7 = array_merge($field_7, $default_fields);
  7657. $default_fields = $field_7;
  7658. } else {
  7659. $field_7['name'] = [];
  7660. $field_7['org_name'] = [];
  7661. foreach ($default_fields as $k => $v) {
  7662. if (isset($field_7[$k])) {
  7663. $field_7[$k] = $v;
  7664. unset($default_fields[$k]);
  7665. }
  7666. }
  7667. $field_7 = array_merge($field_7, $default_fields);
  7668. $default_fields = $field_7;
  7669. }
  7670. }
  7671. //处理馨居尚交定签单显示文字
  7672. if (request()->empcrm['xinjushang'] == 1) {
  7673. foreach ($default_fields as $key => $val) {
  7674. if (strpos($val['name'], '签单') !== false) $default_fields[$key]['name'] = str_replace('签单', '交定', $val['name']);
  7675. if (strpos($val['name'], '转单') !== false) $default_fields[$key]['name'] = str_replace('转单', '签单', $val['name']);
  7676. }
  7677. }
  7678. return json(['code' => 0, 'msg' => 'success', 'data' => $default_fields]);
  7679. }
  7680. /**
  7681. * 设置客户池展示字段
  7682. */
  7683. public function poolFieldsSet()
  7684. {
  7685. $type = input('type', '', 'intval');
  7686. $content = input('content', '', 'trim');
  7687. $content = json_decode($content, true);
  7688. $root_id = request()->empcrm->root_id;
  7689. $employee_id = request()->empcrm->id;
  7690. $find = StatisticsSetting::where([['type', '=', $type], ['employee_id', '=', $employee_id], ['root_id', '=', $root_id]])->findOrEmpty();
  7691. $default_field = $this->poolFields($type);
  7692. foreach ($content as $k => $v) {
  7693. if (isset($v['name']) && isset($v['show']) && isset($v['sort']) && isset($default_field[$v['name']])) {
  7694. $default_field[$v['name']]['show'] = $v['show'];
  7695. $default_field[$v['name']]['sort'] = $v['sort'];
  7696. }
  7697. }
  7698. if ($find->isEmpty()) {
  7699. $save['employee_id'] = $employee_id;
  7700. $save['root_id'] = $root_id;
  7701. $save['content'] = json_encode($default_field);
  7702. $save['type'] = $type;
  7703. $result = StatisticsSetting::insert($save);
  7704. } else {
  7705. $find->content = json_encode($default_field);
  7706. $find->save();
  7707. }
  7708. return json(['code' => 0, 'msg' => 'success']);
  7709. }
  7710. /**
  7711. * 修改客户信息时展示所有字段
  7712. */
  7713. public function model_new_report()
  7714. {
  7715. $id = input('id', '', 'trim');
  7716. $root_id = request()->empcrm->root_id;
  7717. $where[] = ['pid', '<>', 0];
  7718. $where[] = ['root_id', '=', $root_id];
  7719. $where[] = ['status', '=', 0];
  7720. //获取第二层
  7721. $all = CustomerPortraitField::with(['select'])->where($where)->orderRaw('if(isnull(sort),1,0),sort asc')->select()->toArray();
  7722. $city = Company::where('root_id', $root_id)->value('city');
  7723. foreach ($all as $key => $val) {
  7724. if (empty($val['sort'])) $all[$key]['sort'] = $val['id'];
  7725. }
  7726. foreach ($all as &$val) {
  7727. if ($val['keyname'] == 'current_region') {
  7728. $val['select'][] = ['id' => $root_id, 'name' => $city, 'pid' => $val['id']];
  7729. }
  7730. if ($val['keyname'] == 'source_id') {
  7731. $val['select'] = CustomerSource::field('id,source as name')->where('root_id', $root_id)->select()->toArray();
  7732. }
  7733. if ($val['keyname'] == 'deco_style') {
  7734. $val['select'] = Decostyle::field('id,name')->where([['root_id', '=', $root_id], ['type', '=', 0]])->select()->toArray();
  7735. }
  7736. if (in_array($val['keyname'], ['live_broadcast_business', 'live_broadcast_design', 'live_broadcast_personnel'])) {
  7737. $val['select'] = $this->live_broadcast_personnel();
  7738. }
  7739. if ($val['keyname'] == 'phone') {
  7740. $newadd = [
  7741. ['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],
  7742. ['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]
  7743. ];
  7744. }
  7745. }
  7746. $all = array_merge($all, $newadd);
  7747. $sort = array_column($all, 'sort');
  7748. array_multisort($sort, SORT_ASC, $all);
  7749. //获取第一层
  7750. $pid = array_column($all, 'pid');
  7751. $list = CustomerPortraitField::where([['id', 'in', $pid], ['status', '=', 0], ['pid', '=', 0]])->orderRaw('if(isnull(sort),1,0),sort asc')->select()->toArray();
  7752. //组装数组
  7753. foreach ($list as &$p) {
  7754. foreach ($all as &$item) {
  7755. $item['value'] = '';
  7756. $item['valname'] = '';
  7757. if ($p['id'] == $item['pid']) {
  7758. $p['child'][] = $item;
  7759. }
  7760. }
  7761. }
  7762. $cuslog = new CustomerLogic;
  7763. if (isset($id) && !empty($id)) {
  7764. $AllOrgId = orgSubIds($root_id);
  7765. $cusdata = Customer::where([['org_id', 'in', $AllOrgId], ['id', '=', $id]])->find()->toArray();
  7766. if (!empty($cusdata['ext']) && $cusdata['ext'] != 'null') {
  7767. $extdata = json_decode($cusdata['ext'], true);
  7768. if (isset($extdata['ext1'])) {
  7769. $list = $cuslog->old_data_save($cusdata, $list);
  7770. } else {
  7771. $list = $cuslog->new_data_save($cusdata, $extdata, $list);
  7772. }
  7773. } else {
  7774. $list = $cuslog->no_ext_save($cusdata, $list);
  7775. }
  7776. }
  7777. return json(['code' => 0, 'msg' => '获取成功', 'data' => $list]);
  7778. }
  7779. /**
  7780. * 总统计页面
  7781. */
  7782. public function generalStatistics()
  7783. {
  7784. $root_id = request()->empcrm->root_id;
  7785. //返回组织列表
  7786. $sub_orgs = orgSubIds(request()->empcrm->org_id);
  7787. $org = Org::where('id', 'in', $sub_orgs)->select()->toArray();
  7788. View::assign('org', $org);
  7789. $type = input('type', '', 'intval');
  7790. View::assign('type', $type);
  7791. $org_id = input('org_id', '', 'intval');
  7792. View::assign('org_id', $org_id);
  7793. // 字段查询
  7794. // 量房字段
  7795. $liangfang_field = [
  7796. 'measuring_room_customer' => '全部',
  7797. 'measuring_room_customer_no_to_store' => '量房未到店客户数',
  7798. 'measuring_room_customer_to_store' => '量房到店客户数'
  7799. ];
  7800. View::assign('liangfang_field', $liangfang_field);
  7801. // 到店字段
  7802. $daodian_field = [
  7803. 'to_the_store' => '全部',
  7804. 'to_the_store_1' => '一次到店',
  7805. 'to_the_store_2' => '二次到店',
  7806. 'to_the_store_3' => '三次及以上到店'
  7807. ];
  7808. View::assign('daodian_field', $daodian_field);
  7809. // 到店周期字段
  7810. $daodian_avg_days_field = [
  7811. 'strtore_avg_days_1' => '一次到店平均周期',
  7812. 'strtore_avg_days_2' => '二次到店平均周期',
  7813. 'strtore_avg_days_3' => '三次及以上到店平均周期'
  7814. ];
  7815. View::assign('daodian_avg_days_field', $daodian_avg_days_field);
  7816. // 无效字段
  7817. $invalid_field = [
  7818. 'invalid_customer_count' => '全部'
  7819. ];
  7820. $invalid = Setting::where([['name', '=', 'clueTag'], ['root_id', '=', $root_id]])->value('content');
  7821. $invalid = $invalid ? explode(',', $invalid) : ['已装修', '无需求', '有需求', '超地域', '未交房', '在外地', '无购买力', '未接通电话'];
  7822. foreach ($invalid as $k => $v) {
  7823. $invalid_field['invalid_customer_' . $k] = $v;
  7824. }
  7825. View::assign('invalid_field', $invalid_field);
  7826. // 面积字段
  7827. $square_field = [
  7828. 'square' => '全部',
  7829. 'square_0_80' => '80以下',
  7830. 'square_80_100' => '81-100',
  7831. 'square_100_120' => '101-120',
  7832. 'square_120_200' => '121-200',
  7833. 'square_200_500' => '201-500',
  7834. 'square_500' => '500以上',
  7835. ];
  7836. View::assign('square_field', $square_field);
  7837. // 房屋状态
  7838. // 房屋状态是否开启
  7839. $house_status_where = [
  7840. ['root_id', '=', $root_id],
  7841. ['keyname', '=', 'house_status'],
  7842. ['status', '=', 0]
  7843. ];
  7844. $house_status_per = CustomerPortraitField::where($house_status_where)->findOrEmpty();
  7845. $house_status_set = [];
  7846. if (!$house_status_per->isEmpty()) {
  7847. $house_status_set = CustomerPortraitFieldSelect::where('pid', $house_status_per->id)->column('name,id');
  7848. }
  7849. $house_status_field = [
  7850. 'house_status' => '全部'
  7851. ];
  7852. if (!empty($house_status_set)) {
  7853. foreach ($house_status_set as $k => $v) {
  7854. $house_status_field['house_status_' . $v['id']] = $v['name'];
  7855. }
  7856. }
  7857. View::assign('house_status_field', $house_status_field);
  7858. // 房屋类型
  7859. $house_type = [
  7860. 'house_type' => '全部',
  7861. 'house_type_existing' => '现房数量',
  7862. 'house_type_forward' => '期房数量'
  7863. ];
  7864. View::assign('house_type', $house_type);
  7865. $source = CustomerSource::where('root_id', '=', $root_id)->select()->toArray();
  7866. View::assign('source', $source);
  7867. return View::fetch();
  7868. }
  7869. /**
  7870. * 总统计数据
  7871. */
  7872. public function generalStatisticsData()
  7873. {
  7874. $param = request()->only(['page' => 1, 'limit' => 10, 'date' => '', 'keyword' => '', 'org_id' => '', 'start_date' => '', 'end_date' => '', 'type' => '', 'source_id' => '']);
  7875. $root_id = request()->empcrm->root_id;
  7876. //日期搜索//2023-02-15页面逻辑修改 传参方式改为 start_date=2023/2/15 - 2023/2/15
  7877. if ($param['start_date']) {
  7878. $count_dates = explode(' - ', $param['start_date']);
  7879. $param['start_date'] = date('Y-m-d 00:00:00', strtotime($count_dates[0]));
  7880. $param['end_date'] = date('Y-m-d 23:59:59', strtotime($count_dates[1]));
  7881. } else {
  7882. $param['start_date'] = '2010-01-01 00:00:00';
  7883. $param['end_date'] = date('Y-m-d H:i:s', time());
  7884. }
  7885. $date_query = [['addtime', 'between', [$param['start_date'], $param['end_date']]]];
  7886. $date_query1 = [['share_time', 'between', [$param['start_date'], $param['end_date']]]];
  7887. //加微时间筛选
  7888. $sdate = strtotime($param['start_date']);
  7889. $edate = strtotime($param['end_date']);
  7890. // 员工
  7891. $where = [
  7892. ['root_id', '=', $root_id],
  7893. ['uid', '>', 0],
  7894. ['state', '=', '在职']
  7895. ];
  7896. if ($param['keyword']) {
  7897. $where[] = ['name', 'like', '%' . $param['keyword'] . '%'];
  7898. }
  7899. $org_id = $param['org_id'] ? $param['org_id'] : request()->empcrm->org_id;
  7900. $sub_orgs = orgSubIds($org_id);
  7901. $where[] = ['org_id', 'in', $sub_orgs];
  7902. $list = Employee::with(['org' => function ($query) {
  7903. $query->field('id, name');
  7904. }])->where($where)->field('id,org_id,name')->page($param['page'], $param['limit'])->select()->toArray();
  7905. $count = Employee::where($where)->count();
  7906. $page_employee = array_column($list, 'id');
  7907. $c_where[] = ['employee_id', 'in', $page_employee];
  7908. $c_where[] = ['sign_time', 'between', [$param['start_date'], $param['end_date']]];
  7909. $c_where[] = ['state', 'NOT NULL', null];
  7910. $db_where[] = ['Customer.employee_id', 'in', $page_employee];
  7911. // $db_where[] = ['Customer.sign_time', 'between', [$param['start_date'], $param['end_date']]];
  7912. $db_where[] = ['Customer.state', 'NOT NULL', null];
  7913. $fish_where[] = ['is_allocation', '=', '0'];
  7914. $fish_where[] = ['create_time_detail', 'between', [$param['start_date'], $param['end_date']]];
  7915. if ($param['source_id']) {
  7916. $c_where[] = ['source_id', '=', $param['source_id']];
  7917. $db_where[] = ['Customer.source_id', '=', $param['source_id']];
  7918. $jiav_where[] = ['source_id', '=', $param['source_id']];
  7919. $j_w_where[] = ['source_id', '=', $param['source_id']];
  7920. $invalid_where[] = ['source_id', '=', $param['source_id']];
  7921. $fish_source = CustomerSource::where(['root_id' => $root_id, 'id' => $param['source_id']])->value('source');
  7922. if ($fish_source == '飞鱼线索') {
  7923. $fish_where[] = ['type', 'in', [1, 4]];
  7924. } elseif ($fish_source == '腾讯线索') {
  7925. $fish_where[] = ['type', '=', 2];
  7926. } elseif ($fish_source == '欧派线索') {
  7927. $fish_where[] = ['type', '=', 3];
  7928. } else {
  7929. $fish_where[] = ['type', '=', 55];
  7930. }
  7931. }
  7932. // 加微客户查询
  7933. // 先查搜索时间段内部门客户的加微数据,再判断哪些客户是page_employee中员工的客户,再判断其中哪些是已经置为无效的客户,再把这些置为无效的客户拼接到page_empployee员工身上。
  7934. $page_employee_org = array_unique(array_column($list, 'org_id'));
  7935. $jiav_where[] = ['org_id', 'in', $page_employee_org];
  7936. $jiav_where[] = ['add_wechat_time', 'between', [$param['start_date'], $param['end_date']]];
  7937. $jiav_list = Customer::where($jiav_where)->column('id,employee_id,state');
  7938. $db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
  7939. if ($date_query) {
  7940. $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$param['start_date'], $param['end_date']]];
  7941. }
  7942. $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');
  7943. // 置为无效的客户 按报名时间
  7944. $invalid_where[] = ['employee_id', 'in', $page_employee];
  7945. $invalid_where[] = ['cus_addtime', 'between', [$param['start_date'], $param['end_date']]];
  7946. $invalid_customer_ids = CustomerInvalidLog::where($invalid_where)->column('id,employee_id,status');
  7947. $invalid_customer_employee = []; // 员工无效的客户 计算客资总和
  7948. $invalid_customer_employee_valid = []; // 员工无效的客户曾经有效 计算有效客户总和
  7949. foreach ($invalid_customer_ids as $k => $v) {
  7950. $invalid_customer_employee[$v['employee_id']][] = $v['id'];
  7951. if ($v['status'] == 1) {
  7952. $invalid_customer_employee_valid[$v['employee_id']][] = $v['id'];
  7953. }
  7954. }
  7955. $vislog_list = Db::view('CustomerVisitLog', 'id,customer_id,state,addtime,next_contact_date,remark,employee_id,confirm_date,customer_employee_id')
  7956. ->view('Customer', [], 'Customer.id=CustomerVisitLog.customer_id')
  7957. ->where($db_where)
  7958. ->select()
  7959. ->toArray();
  7960. // 处理加微数据
  7961. $jiav_employee = []; // 正常的客户
  7962. $jiav_employee_wuxiao = []; // 无效的客户
  7963. foreach ($jiav_list as $k => $v) {
  7964. if (in_array($v['employee_id'], $page_employee) && !in_array($v['state'], Customer::changeState('无效', 'chaos', true))) {
  7965. $jiav_employee[$v['employee_id']][] = $v['id'];
  7966. } elseif (in_array($v['state'], Customer::changeState('无效', 'chaos', true))) {
  7967. $jiav_employee_wuxiao[] = $v['id'];
  7968. }
  7969. }
  7970. // 去查询这些置为无效的加微客户原先都是谁的
  7971. $j_w_where[] = ['customer_id', 'in', $jiav_employee_wuxiao];
  7972. $j_w_where[] = ['employee_id', 'in', $page_employee];
  7973. $jiav_wuxiao_log = CustomerInvalidLog::where($j_w_where)->column('customer_id,employee_id');
  7974. $jiav_wuxiao_log_deal = [];
  7975. foreach ($jiav_wuxiao_log as $v) {
  7976. $jiav_wuxiao_log_deal[$v['employee_id']][] = $v['customer_id'];
  7977. }
  7978. foreach ($jiav_employee as $k => $v) {
  7979. if (isset($jiav_wuxiao_log_deal[$k])) {
  7980. $jiav_employee[$k] = array_unique(array_merge($v, $jiav_wuxiao_log_deal[$k]));
  7981. }
  7982. }
  7983. // 处理跟踪记录
  7984. $customer_employee = []; // 员工下的客户
  7985. $customer_ids_employee = []; // 员工下的客户id
  7986. $customer_ids_key = []; // 客户所属员工
  7987. foreach ($customer_list as $k => $v) {
  7988. $customer_employee[$v['employee_id']][] = $v;
  7989. $customer_ids_employee[$v['employee_id']][] = $v['id'];
  7990. $customer_ids_key[$v['id']] = $v['employee_id'];
  7991. }
  7992. $state3 = CustomerVisitLog::changeState('已量房', 'chaos');
  7993. $state4 = CustomerVisitLog::changeState('已到店', 'chaos');
  7994. $state5 = CustomerVisitLog::changeState('已交定', 'chaos');
  7995. $state6 = CustomerVisitLog::changeState('已签单', 'chaos');
  7996. $state7 = CustomerVisitLog::changeState('已卖卡', 'chaos');
  7997. $wuxiao_log_state = CustomerVisitLog::changeState('无效', 'chaos');
  7998. //2023-03-18 3.3客户标记无效后不能影响数据统计内的有效数据、线索数据 https://kdocs.cn/l/cfx92KlU838H
  7999. $no_where = [
  8000. ['employee_id', 'in', array_column($list, 'id')],
  8001. ['state', 'in', CustomerVisitLog::changeState('无效', 'chaos')],
  8002. ['addtime', 'between', [$param['start_date'], $param['end_date']]]
  8003. ];
  8004. $no_valid_customer = CustomerVisitLog::where($no_where)->column('id,customer_id,state,addtime,next_contact_date,remark,employee_id,confirm_date');
  8005. $vislog_list = array_merge($vislog_list, $no_valid_customer);
  8006. $visit_log_employee = []; //员工跟进记录
  8007. //$from_pool = []; // 来自公海的客户,不做统计
  8008. foreach ($vislog_list as $k => $v) {
  8009. /* if ($v['remark'] == '公海获取' || (isset($from_pool[$v['employee_id']]) && in_array($v['customer_id'], $from_pool[$v['employee_id']]))) {
  8010. if (isset($from_pool[$v['employee_id']])){
  8011. if (!in_array($v['customer_id'], $from_pool[$v['employee_id']])) {
  8012. $from_pool[$v['employee_id']][] = $v['customer_id'];
  8013. }
  8014. } else {
  8015. $from_pool[$v['employee_id']][] = $v['customer_id'];
  8016. }
  8017. unset($vislog_list[$k]);
  8018. continue;
  8019. } */
  8020. $have_employee = $customer_ids_key[$v['customer_id']] ?? 0;
  8021. if ($have_employee) {
  8022. $v_customer_ids = $customer_ids_employee[$have_employee] ?? [];
  8023. // 只保留还是员工客户的跟踪记录,或者是员工置为无效的客户记录
  8024. if (!in_array($v['customer_id'], $v_customer_ids) && !in_array($v['state'], $wuxiao_log_state, true)) {
  8025. unset($vislog_list[$k]);
  8026. continue;
  8027. }
  8028. $visit_log_employee[$have_employee][] = $v;
  8029. }
  8030. }
  8031. $vislog_list = array_values($vislog_list);
  8032. foreach ($list as $k => $v) {
  8033. $list[$k]['visit_log'] = $visit_log_employee[$v['id']] ?? [];
  8034. $list[$k]['customer'] = $customer_employee[$v['id']] ?? [];
  8035. }
  8036. // 员工客户处理 去除公海获取的客户
  8037. /* foreach ($list as $k => $v) {
  8038. if (isset($customer_employee[$v['id']]) && isset($from_pool[$v['id']])) {
  8039. $v_customer = $customer_employee[$v['id']];
  8040. foreach ($v_customer as $kk => $vv) {
  8041. if (in_array($vv['id'], $from_pool[$v['id']])) {
  8042. unset($v_customer[$kk]);
  8043. }
  8044. }
  8045. $list[$k]['customer'] = array_values($v_customer);
  8046. } else {
  8047. $list[$k]['customer'] = $customer_employee[$v['id']] ?? [];
  8048. }
  8049. } */
  8050. $customer['sign_customer'] = []; //签单客户
  8051. $customer['measuring_room_customer'] = []; //量房客户
  8052. $customer['maika_customer'] = []; //卖卡客户
  8053. $customer['measuring_room_time'] = []; //量房时间
  8054. $customer['to_the_store'] = []; //到店客户id
  8055. $customer['to_the_store_time'] = []; //到店时间
  8056. $customer['deposit_customer'] = []; //交定客户
  8057. $customer['deposit_date'] = []; // 交定时间
  8058. foreach ($vislog_list as $v) {
  8059. if (in_array($v['state'], $state6, true)) { //签单客户id
  8060. if (empty($customer['sign_customer'][$v['customer_employee_id']])) {
  8061. $customer['sign_customer'][$v['customer_employee_id']] = [];
  8062. }
  8063. $customer['sign_customer'][$v['customer_employee_id']][] = $v['customer_id'];
  8064. } elseif (in_array($v['state'], $state3, true)) { //量房客户id
  8065. if (empty($customer['measuring_room_customer'][$v['customer_employee_id']])) {
  8066. $customer['measuring_room_customer'][$v['customer_employee_id']] = [];
  8067. }
  8068. $customer['measuring_room_customer'][$v['customer_employee_id']][] = $v['customer_id'];
  8069. //量房时间 按照时间升序排列
  8070. if (empty($customer['measuring_room_time'][$v['customer_id']])) {
  8071. $customer['measuring_room_time'][$v['customer_id']] = [];
  8072. $customer['measuring_room_time'][$v['customer_id']][] = $v['addtime'];
  8073. } else {
  8074. $customer['measuring_room_time'][$v['customer_id']][] = $v['addtime'];
  8075. }
  8076. } elseif (in_array($v['state'], $state4, true)) { //到店客户id
  8077. if (empty($customer['to_the_store'][$v['customer_employee_id']])) {
  8078. $customer['to_the_store'][$v['customer_employee_id']] = [];
  8079. }
  8080. $customer['to_the_store'][$v['customer_employee_id']][] = $v['customer_id'];
  8081. //到店时间 按照时间升序排列
  8082. if (empty($customer['to_the_store_time'][$v['customer_id']])) {
  8083. $customer['to_the_store_time'][$v['customer_id']] = [];
  8084. $customer['to_the_store_time'][$v['customer_id']][] = $v['addtime'];
  8085. } else {
  8086. $customer['to_the_store_time'][$v['customer_id']][] = $v['addtime'];
  8087. }
  8088. } elseif (in_array($v['state'], $state5, true)) { //交定客户
  8089. if (empty($customer['deposit_customer'][$v['customer_employee_id']])) {
  8090. $customer['deposit_customer'][$v['customer_employee_id']] = [];
  8091. }
  8092. $customer['deposit_customer'][$v['customer_employee_id']][] = $v['customer_id'];
  8093. //交定时间 按照时间升序排列
  8094. if (empty($customer['deposit_date'][$v['customer_id']])) {
  8095. $customer['deposit_date'][$v['customer_id']] = [];
  8096. $customer['deposit_date'][$v['customer_id']][] = $v['addtime'];
  8097. } else {
  8098. $customer['deposit_date'][$v['customer_id']][] = $v['addtime'];
  8099. }
  8100. } elseif (in_array($v['state'], $state7, true)) { //卖卡客户
  8101. $customer['maika_customer'][] = $v['customer_id'];
  8102. }
  8103. }
  8104. // 有跟进客户
  8105. $log_customer_id = []; // 某个员工下的跟踪客户id数组
  8106. //智慧屏讲解次数
  8107. $zhihui = [];
  8108. // 无效
  8109. $wuxiao_visit_log = [];
  8110. foreach ($vislog_list as $k => $v) {
  8111. if (in_array($v['state'], $wuxiao_log_state, true)) {
  8112. $wuxiao_visit_log[] = $v;
  8113. continue;
  8114. }
  8115. $have_employee = $customer_ids_key[$v['customer_id']] ?? 0;
  8116. if ($have_employee) {
  8117. if (strpos($v['remark'], '讲解智慧屏##') !== false) {
  8118. if (empty($zhihui[$have_employee])) {
  8119. $zhihui[$have_employee] = 1;
  8120. } else {
  8121. $zhihui[$have_employee] += 1;
  8122. }
  8123. }
  8124. }
  8125. if (in_array($v['remark'], ['客户再分配', '资源库分配', '客户转移'], true) || (strpos($v['remark'], '业务员') !== false && strpos($v['remark'], '对客户') !== false && strpos($v['remark'], '进行报备') !== false)) {
  8126. continue;
  8127. }
  8128. if ($have_employee) {
  8129. if (isset($log_customer_id[$have_employee])) {
  8130. $log_customer_id[$have_employee][] = $v['customer_id'];
  8131. } else {
  8132. $log_customer_id[$have_employee] = [];
  8133. $log_customer_id[$have_employee][] = $v['customer_id'];
  8134. }
  8135. }
  8136. }
  8137. foreach ($log_customer_id as $k => $v) {
  8138. $log_customer_id[$k] = array_values(array_unique($v));
  8139. }
  8140. // 外呼数据
  8141. $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();
  8142. $outCallLog = array_combine(array_column($outCallLog, 'employee_id'), $outCallLog);
  8143. // 手机打电话次数获取
  8144. $outCallMbLog = OutCallMbLog::where([
  8145. ['root_id', '=', $root_id], ['employee_id', 'in', $page_employee]
  8146. ])->where($date_query)->field("count(id) as phone_count,employee_id")->group('employee_id')->select()->toArray();
  8147. $outCallMbLog = array_combine(array_column($outCallMbLog, 'employee_id'), $outCallMbLog);
  8148. // 房屋状态设置项
  8149. //房屋状态是否开启
  8150. $house_status_where = [
  8151. ['root_id', '=', $root_id],
  8152. ['keyname', '=', 'house_status'],
  8153. ['status', '=', 0]
  8154. ];
  8155. $house_status_per = CustomerPortraitField::where($house_status_where)->findOrEmpty();
  8156. $house_status_field = [];
  8157. if (!$house_status_per->isEmpty()) {
  8158. $house_status_field = CustomerPortraitFieldSelect::where('pid', $house_status_per->id)->column('name,id');
  8159. }
  8160. // 户型是否开启
  8161. $housetype_where = [
  8162. ['root_id', '=', $root_id],
  8163. ['keyname', '=', 'housetype_arrow'],
  8164. ['status', '=', 0]
  8165. ];
  8166. $housetype_per = CustomerPortraitField::where($housetype_where)->findOrEmpty();
  8167. $housetype_arrow = [];
  8168. if (!$housetype_per->isEmpty()) {
  8169. $housetype_arrow = CustomerPortraitFieldSelect::where('pid', $housetype_per->id)->column('name,id');
  8170. }
  8171. //分享内容次数
  8172. $share_where = ['CompanyStrength', 'Article', 'Construction', 'Activity', 'MaterialEvidence', 'Video', 'MaterialCase', 'Building'];
  8173. $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();
  8174. $shares = [];
  8175. foreach ($share as $v) {
  8176. if (empty($shares[$v['employee_id']])) {
  8177. $shares[$v['employee_id']] = 1;
  8178. } else {
  8179. $shares[$v['employee_id']] += 1;
  8180. }
  8181. }
  8182. //考核次数 通过次数
  8183. $don = TrainDoneLog::where([['employee_id', 'in', $page_employee]])->where($date_query)->column('employee_id,done_percent');
  8184. $don1 = $don2 = [];
  8185. foreach ($don as $don_k => $don_v) {
  8186. if (empty($don1[$don_v['employee_id']])) {
  8187. $don1[$don_v['employee_id']] = 1;
  8188. } else {
  8189. $don1[$don_v['employee_id']] += 1;
  8190. }
  8191. if ($don_v['done_percent'] == 100) {
  8192. if (empty($don2[$don_v['employee_id']])) {
  8193. $don2[$don_v['employee_id']] = 1;
  8194. } else {
  8195. $don2[$don_v['employee_id']] += 1;
  8196. }
  8197. }
  8198. }
  8199. //无效客资
  8200. $invalid = Setting::where([['name', '=', 'clueTag'], ['root_id', '=', $root_id]])->value('content');
  8201. $invalid = $invalid ? explode(',', $invalid) : ['已装修', '无需求', '有需求', '超地域', '未交房', '在外地', '无购买力', '未接通电话'];
  8202. $invalid_customer = [];
  8203. // 按客户来源搜索处理
  8204. $wuxiao_customer_id = array_values(array_unique(array_column($wuxiao_visit_log, 'customer_id')));
  8205. $no_valid_customers = []; //2023-03-18 3.3客户标记无效后不能影响数据统计内的有效数据、线索数据 https://kdocs.cn/l/cfx92KlU838H 康
  8206. if ($param['source_id']) {
  8207. $wuxiao_source_customer_id = Customer::where([['id', 'in', $wuxiao_customer_id], ['source_id', '=', $param['source_id']]])->column('id');
  8208. foreach ($wuxiao_visit_log as $k => $v) {
  8209. if (in_array($v['customer_id'], $wuxiao_source_customer_id)) {
  8210. $remark = '';
  8211. if (in_array($v['remark'], $invalid)) {
  8212. $remark = $v['remark'];
  8213. } else {
  8214. foreach ($invalid as $i_k => $i_v) {
  8215. if (strpos($v['remark'], $i_v) !== false) {
  8216. $remark = $i_v;
  8217. break;
  8218. }
  8219. }
  8220. }
  8221. if (isset($invalid_customer[$v['employee_id']])) {
  8222. $invalid_customer[$v['employee_id']][] = ['remark' => $remark, 'customer_id' => $v['customer_id']];
  8223. } else {
  8224. $invalid_customer[$v['employee_id']] = [];
  8225. $invalid_customer[$v['employee_id']][] = ['remark' => $remark, 'customer_id' => $v['customer_id']];
  8226. }
  8227. if (isset($no_valid_customers[$v['employee_id']])) {
  8228. $no_valid_customers[$v['employee_id']][] = $v['customer_id'];
  8229. } else {
  8230. $no_valid_customers[$v['employee_id']] = [];
  8231. $no_valid_customers[$v['employee_id']][] = $v['customer_id'];
  8232. }
  8233. }
  8234. }
  8235. } else {
  8236. foreach ($wuxiao_visit_log as $k => $v) {
  8237. $remark = '';
  8238. if (in_array($v['remark'], $invalid)) {
  8239. $remark = $v['remark'];
  8240. } else {
  8241. foreach ($invalid as $i_k => $i_v) {
  8242. if (strpos($v['remark'], $i_v) !== false) {
  8243. $remark = $i_v;
  8244. break;
  8245. }
  8246. }
  8247. }
  8248. if (isset($invalid_customer[$v['employee_id']])) {
  8249. $invalid_customer[$v['employee_id']][] = ['remark' => $remark, 'customer_id' => $v['customer_id']];
  8250. } else {
  8251. $invalid_customer[$v['employee_id']] = [];
  8252. $invalid_customer[$v['employee_id']][] = ['remark' => $remark, 'customer_id' => $v['customer_id']];
  8253. }
  8254. if (isset($no_valid_customers[$v['employee_id']])) {
  8255. $no_valid_customers[$v['employee_id']][] = $v['customer_id'];
  8256. } else {
  8257. $no_valid_customers[$v['employee_id']] = [];
  8258. $no_valid_customers[$v['employee_id']][] = $v['customer_id'];
  8259. }
  8260. }
  8261. }
  8262. // 无效客资来自资源分配
  8263. $wuxiao_customer_res_id = Customer::where([['id', 'in', $wuxiao_customer_id], ['crm_res_id', '>', 0]])->column('id');
  8264. $wuxiao_customer_res_employee = [];
  8265. foreach ($invalid_customer as $k => $v) {
  8266. $customer_id_v = array_column($v, 'customer_id');
  8267. foreach ($customer_id_v as $vv) {
  8268. if (in_array($vv, $wuxiao_customer_res_id)) {
  8269. $wuxiao_customer_res_employee[$k][] = $vv;
  8270. }
  8271. }
  8272. }
  8273. $f_invalid = array_flip($invalid);
  8274. foreach ($list as $k => $v) {
  8275. //部门名称
  8276. $list[$k]['org_name'] = $v['org']['name'];
  8277. //客户id 所有的客户ID 包括资源,自创,无效等
  8278. $all_customer_id = array_column($v['customer'], 'id');
  8279. //数据处理
  8280. $customer_data = $this->dealCustomerData($v['customer'], $sdate, $edate, $customer['measuring_room_time'], $customer['to_the_store_time'], $customer['deposit_date']);
  8281. $cycle_data = $this->avgCycle($v['customer'], $v['visit_log']);
  8282. // 分配资源数量 包含已置入公海的
  8283. $resource_count = count($customer_data['is_resource']);
  8284. $resource_wuxiao_ids = [];
  8285. if (isset($wuxiao_customer_res_employee[$v['id']])) {
  8286. $resource_wuxiao_ids = array_values(array_unique($wuxiao_customer_res_employee[$v['id']]));
  8287. }
  8288. $list[$k]['resource_count'] = count(array_unique(array_merge($customer_data['is_resource'], $resource_wuxiao_ids)));
  8289. if (isset($outCallLog[$v['id']])) {
  8290. // 拨打电话数量
  8291. $list[$k]['connecting_count'] = $outCallLog[$v['id']]['phone_count'];
  8292. // 接通数量
  8293. $list[$k]['connecting_capacity'] = $outCallLog[$v['id']]['on_phone_count'];
  8294. // 接通率
  8295. $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) . '%';
  8296. // 未接通数量
  8297. $list[$k]['connecting_no'] = $outCallLog[$v['id']]['off_phone_count'];
  8298. // 总通话时长(分)
  8299. $list[$k]['phone_time'] = round($outCallLog[$v['id']]['phone_time'] / 60, 1);
  8300. // 平均通话时长(秒)
  8301. $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);
  8302. } else {
  8303. $list[$k]['connecting_count'] = $list[$k]['connecting_capacity'] = $list[$k]['connecting_no'] = $list[$k]['phone_time'] = $list[$k]['average_duration'] = 0;
  8304. $list[$k]['connecting_capacity_grawth'] = '0%';
  8305. }
  8306. // 手机通话
  8307. if (isset($outCallMbLog[$v['id']])) $list[$k]['connecting_count'] += $outCallMbLog[$v['id']]['phone_count'];
  8308. // 客户总量
  8309. if (isset($invalid_customer_employee[$v['id']])) {
  8310. $customer_count = array_unique(array_merge($customer_data['customer_count'], $invalid_customer_employee[$v['id']]));
  8311. } else {
  8312. $customer_count = $customer_data['customer_count'];
  8313. }
  8314. //查询当前此人飞鱼腾讯的线索量-资源未转化的客户
  8315. $fish_count = FishData::where($fish_where)->where(['employee_id' => $v['id']])->count();
  8316. $list[$k]['customer_count'] = $fish_count + count($customer_count);
  8317. // $list[$k]['customer_count'] = count($customer_count);
  8318. $list[$k]['follow_customer_count'] = count($customer_data['follow_customer_count']);
  8319. // 加微客户数量
  8320. $v_wechat_count = $jiav_employee[$v['id']] ?? [];
  8321. $list[$k]['add_wechat_count'] = count($v_wechat_count);
  8322. // 待确认客户数
  8323. $list[$k]['not_sure_count'] = count($customer_data['not_sure_count']);
  8324. // 有效客户数
  8325. if (isset($invalid_customer_employee_valid[$v['id']])) {
  8326. $list[$k]['valid_count'] = count(array_unique(array_merge($customer_data['valid_count'], $invalid_customer_employee_valid[$v['id']])));
  8327. } else {
  8328. $list[$k]['valid_count'] = count($customer_data['valid_count']);
  8329. }
  8330. // 加微率
  8331. $list[$k]['add_wechat_count_grawth'] = count($customer_count) == 0 ? '0%' : round(count($v_wechat_count) / count($customer_count) * 100, 2) . '%';
  8332. // 无效客户数
  8333. foreach ($invalid as $invalid_k => $invalid_v) {
  8334. $list[$k]['invalid_customer_' . $invalid_k] = [];
  8335. }
  8336. if (isset($invalid_customer[$v['id']])) {
  8337. $invalid_customer_all = array_unique(array_column($invalid_customer[$v['id']], 'customer_id'));
  8338. $list[$k]['invalid_customer_count'] = count($invalid_customer_all);
  8339. if ($invalid) {
  8340. foreach ($invalid_customer[$v['id']] as $key => $value) {
  8341. if ($value['remark']) {
  8342. $key = $f_invalid[$value['remark']];
  8343. $list[$k]['invalid_customer_' . $key][] = $value['customer_id'];
  8344. }
  8345. }
  8346. }
  8347. } else {
  8348. $invalid_customer_all = [];
  8349. $list[$k]['invalid_customer_count'] = 0;
  8350. }
  8351. foreach ($invalid as $invalid_k => $invalid_v) {
  8352. $list[$k]['invalid_customer_' . $invalid_k] = count(array_unique($list[$k]['invalid_customer_' . $invalid_k]));
  8353. }
  8354. // 线索总量
  8355. $v_customer = !empty($v['customer']) ? array_column($v['customer'], 'id') : [];
  8356. $all_customer = array_unique(array_merge($v_customer, $invalid_customer_all));
  8357. $list[$k]['all_customer_count'] = count($all_customer);
  8358. // 有效率(%)
  8359. $list[$k]['valid_grawth'] = count($all_customer) == 0 ? '0%' : round(count($customer_data['valid_count']) / count($all_customer) * 100, 2) . '%';
  8360. // 未跟进客户数
  8361. if (isset($log_customer_id[$v['id']])) {
  8362. $following_up_count = $log_customer_id[$v['id']];
  8363. } else {
  8364. $following_up_count = [];
  8365. }
  8366. $list[$k]['not_following_up_count'] = count($customer_count) - count(array_intersect($customer_count, $following_up_count)); // 总的减去有跟进的
  8367. // 意向分派客户数
  8368. $list[$k]['yixiang_assigned_count'] = count($customer_data['yixiang_assigned_count']);
  8369. // 到店分派客户数
  8370. $list[$k]['daodian_assigned_count'] = count($customer_data['daodian_assigned_count']);
  8371. // 量房分配客户数
  8372. $list[$k]['measuring_assigned_count'] = count($customer_data['liangfang_assigned_count']);
  8373. // 智慧屏讲解次数
  8374. $list[$k]['number_of_explanations'] = isset($zhihui[$v['id']]) ? $zhihui[$v['id']] : 0;
  8375. // 量房客户数
  8376. $measuring_room_customer = $customer['measuring_room_customer'][$v['id']] ?? []; //量房客户ID
  8377. $to_the_store_ids = $customer['to_the_store'][$v['id']] ?? []; // 到店客户ID
  8378. $list[$k]['measuring_room_customer'] = count(array_unique($measuring_room_customer));
  8379. // 量房未到店
  8380. $measuring_room_customer_to_store = array_intersect($measuring_room_customer, $to_the_store_ids); // 量房 和 到店
  8381. $measuring_room_customer_no_to_store = array_diff($measuring_room_customer, $measuring_room_customer_to_store);
  8382. $list[$k]['measuring_room_customer_no_to_store'] = count($measuring_room_customer_no_to_store);
  8383. // 量房已到店
  8384. $list[$k]['measuring_room_customer_to_store'] = count($measuring_room_customer_to_store);
  8385. // 平均量房周期(天)
  8386. $list[$k]['measuring_room_zhouqi'] = $cycle_data['room_cycle'];
  8387. // 量房率
  8388. $list[$k]['measuring_room_grawth'] = count($customer_count) == 0 ? '0%' : round(count($measuring_room_customer) / count($customer_count) * 100, 2) . '%';
  8389. // 到店数 次数
  8390. $list[$k]['to_the_store'] = count($to_the_store_ids);
  8391. // 一次到店数
  8392. $list[$k]['to_the_store_1'] = $customer_data['strtore_count_0'];
  8393. // 二次到店
  8394. $list[$k]['to_the_store_2'] = $customer_data['strtore_count_1'];
  8395. // 二次到店比率
  8396. $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) . '%';
  8397. // 三次及以上到店
  8398. $list[$k]['to_the_store_3'] = $customer_data['strtore_count_2'];
  8399. // 平均到店周期(天)
  8400. $list[$k]['strtore_avg_days_1'] = ceil($customer_data['strtore_0'] / 86400); // 一次
  8401. $list[$k]['strtore_avg_days_2'] = ceil($customer_data['strtore_1'] / 86400); // 二次
  8402. $list[$k]['strtore_avg_days_3'] = ceil($customer_data['strtore_2'] / 86400); // 三次
  8403. // 到店客户数
  8404. $list[$k]['strtore_customer_count'] = count(array_unique($to_the_store_ids));
  8405. // 到店率
  8406. $list[$k]['strtore_customer_grawth'] = count($customer_count) == 0 ? '0%' : round(count($to_the_store_ids) / count($customer_count) * 100, 2) . '%';
  8407. // 签单客户数量
  8408. $deposit_count_ids = $customer['deposit_customer'][$v['id']] ?? [];
  8409. $list[$k]['deposit_count'] = count(array_unique($deposit_count_ids));
  8410. // 签单率 签单数/见面数
  8411. $list[$k]['deposit_avg'] = count($cycle_data['visit_customer']) == 0 ? '0%' : round(count($deposit_count_ids) / count($cycle_data['visit_customer']) * 100, 2) . '%';
  8412. // 平均签单周期
  8413. $list[$k]['deposit_avg_days'] = $cycle_data['dep_cycle'];
  8414. // 转单客户数量
  8415. $sign_customer_ids = $customer['sign_customer'][$v['id']] ?? [];
  8416. $list[$k]['sign_count'] = count(array_unique($sign_customer_ids));
  8417. // 卖卡客户数量
  8418. $maika_customer_ids = $customer['maika_customer'][$v['id']] ?? [];
  8419. $list[$k]['maika_count'] = count(array_unique($maika_customer_ids));
  8420. // 转单率 转单/见面数
  8421. $list[$k]['sign_count_grawth'] = count($cycle_data['visit_customer']) == 0 ? '0%' : round(count($sign_customer_ids) / count($cycle_data['visit_customer']) * 100, 2) . '%';
  8422. // 平均跟进次数
  8423. $list[$k]['avg_visit_count'] = $cycle_data['next'];
  8424. // 平均合同金额(元)
  8425. $list[$k]['signed_money'] = $customer_data['signed_money'];
  8426. // 合同总金额
  8427. $list[$k]['signed_money_all'] = $customer_data['signed_money_all'];
  8428. // 面积
  8429. $list[$k]['square'] = $customer_data['square'];
  8430. $list[$k]['square_0_80'] = $customer_data['square_0_80'];
  8431. $list[$k]['square_80_100'] = $customer_data['square_80_100'];
  8432. $list[$k]['square_100_120'] = $customer_data['square_100_120'];
  8433. $list[$k]['square_120_200'] = $customer_data['square_120_200'];
  8434. $list[$k]['square_200_500'] = $customer_data['square_200_500'];
  8435. $list[$k]['square_500'] = $customer_data['square_500'];
  8436. // 房屋状态
  8437. $list[$k]['house_status'] = 0;
  8438. if ($house_status_field) {
  8439. $house_status = $this->getHouseStatus($customer_data['house_status'], $house_status_field);
  8440. $list[$k] = array_merge($list[$k], $house_status);
  8441. $list[$k]['house_status'] = array_sum($house_status);
  8442. }
  8443. //房屋户型 及 对应的转单率
  8444. if ($housetype_arrow) {
  8445. $housetype_arrow_data = $this->getHousetypeArrow($customer_data['housetype_arrow'], $housetype_arrow, $sign_customer_ids);
  8446. $list[$k] = array_merge($list[$k], $housetype_arrow_data);
  8447. }
  8448. // 房屋类型
  8449. $list[$k]['house_type'] = $customer_data['existing_homes_count'] + $customer_data['forward_housing_count'];
  8450. $list[$k]['existing_homes_count'] = $customer_data['existing_homes_count'];
  8451. $list[$k]['forward_housing_count'] = $customer_data['forward_housing_count'];
  8452. // 调用内容数量
  8453. // 分享内容次数
  8454. $list[$k]['shares_count'] = isset($shares[$v['id']]) ? $shares[$v['id']] : 0;
  8455. // 考核次数
  8456. $list[$k]['assessment_times'] = isset($don1[$v['id']]) ? $don1[$v['id']] : 0;
  8457. // 通过次数
  8458. $list[$k]['number_of_passes'] = isset($don2[$v['id']]) ? $don2[$v['id']] : 0;
  8459. unset($list[$k]['customer']);
  8460. unset($list[$k]['visit_log']);
  8461. $list[$k]['valid_num'] = CustomerStateCheck::where([
  8462. ['employee_id', '=', $v['id']],
  8463. ['check_state', '=', 1]
  8464. ])->count();
  8465. }
  8466. return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
  8467. }
  8468. static function setForNot(&$a, $c, $addCount)
  8469. {
  8470. if (!isset($a[$c])) $a[$c] = 0;
  8471. $a[$c] += $addCount;
  8472. }
  8473. /**
  8474. * 处理客户数据
  8475. * $list 业务员的客户数组
  8476. * $sdate 开始时间筛选
  8477. * $edate 结束时间筛选
  8478. * $measuring_room_time 客户量房时间 计算平均量房时间
  8479. * $to_the_store_time 客户到店时间 计算平均到店时间
  8480. * $deposit_customer 客户交定时间 计算平均交定周期
  8481. */
  8482. static function dealCustomerData($list, $sdate, $edate, $measuring_room_time, $to_the_store_time, $deposit_customer)
  8483. {
  8484. $time = time();
  8485. $data['not_sure_count'] = []; // 待确认客户id
  8486. $data['customer_count'] = []; // 客户量(线索量) 除去资源、无效、死单
  8487. $data['follow_customer_count'] = []; // 跟进客户数量
  8488. $data['valid_count'] = []; // 有效客户 除去资源、无效、死单、待确认 (客户量的基础上减去待确认的)
  8489. //$data['invalid_count'] = []; // 无效客户
  8490. $data['yixiang_assigned_count'] = []; // 意向分配客户数
  8491. $data['daodian_assigned_count'] = []; // 到店分配客户数
  8492. $data['liangfang_assigned_count'] = []; // 量房分配客户数
  8493. $data['wechat_count'] = []; //加微客户id
  8494. $data['is_resource'] = []; //资源库分配客户id
  8495. $data['drawing_date'] = []; //有户型图的客户
  8496. $row['room_cycle_0'] = $row['room_cycle_1'] = $row['room_cycle_2'] = []; //一次 二次 三次量房周期
  8497. $row['strtore_0'] = $row['strtore_1'] = $row['strtore_2'] = []; //一次 二次 三次到店周期
  8498. $row['deposit_0'] = $row['deposit_1'] = $row['deposit_2'] = []; //一次 二次 三次交定周期
  8499. $data['room_cycle_customer_0'] = $data['room_cycle_customer_1'] = $data['room_cycle_customer_2'] = []; //一次 两次 三次量房客户
  8500. $data['strtore_customer_0'] = $data['strtore_customer_1'] = $data['strtore_customer_2'] = []; //一次 两次 三次到店客户
  8501. $data['deposit_customer_0'] = $data['deposit_customer_1'] = $data['deposit_customer_2'] = []; //一次 两次 三次交定客户
  8502. $row['signed_money'] = []; //合同金额
  8503. $house_type = []; //房屋类型
  8504. $housetype_arrow = []; // 房屋户型
  8505. $square = []; //房屋面积
  8506. $data['house_status'] = []; //房屋状态
  8507. //报备客户
  8508. $data['reported_quantity'] = 0;
  8509. //死单客户
  8510. $data['died'] = 0;
  8511. $not_sure_state = Customer::changeState('待确认', 'chaos');
  8512. $invalid_state = Customer::changeState('无效', 'chaos');
  8513. $invalid_not_sure_state = array_values(array_merge($invalid_state, $not_sure_state));
  8514. foreach ($list as $v) {
  8515. // 线索量
  8516. $data['customer_count'][] = $v['id'];
  8517. // 待确认客户
  8518. if (empty($v['crm_res_id']) && in_array($v['state'], $not_sure_state, true)) {
  8519. $data['not_sure_count'][] = $v['id'];
  8520. }
  8521. // 客户数量
  8522. if (intval($v['died']) != 2) {
  8523. 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))) {
  8524. $data['follow_customer_count'][] = $v['id'];
  8525. $square[] = $v['square']; //面积
  8526. }
  8527. }
  8528. // 有效客户
  8529. if (intval($v['died']) != 2 && !in_array($v['state'], $invalid_not_sure_state, true)) {
  8530. $data['valid_count'][] = $v['id'];
  8531. // 房屋类型
  8532. $house_type[] = $v['house_type'];
  8533. $row['signed_money'][] = $v['signed_money']; //合同金额
  8534. $data['house_status'][] = $v['house_status'];
  8535. // 房屋户型
  8536. $ext = json_decode($v['ext'], true);
  8537. if (!empty($ext) && is_array($ext)) {
  8538. $ext = array_filter($ext);
  8539. foreach ($ext as $e) {
  8540. if (isset($e['keyname']) && $e['keyname'] == 'housetype_arrow' && !empty($e['value'])) {
  8541. $housetype_arrow[$v['id']] = $e['value'];
  8542. }
  8543. }
  8544. }
  8545. }
  8546. // 无效客户
  8547. /* if (in_array($v['state'], $invalid_state, true)) {
  8548. $data['invalid_count'][] = $v['id'];
  8549. } */
  8550. // 意向分派客户数
  8551. if (!empty($v['assign_type']) && $v['assign_type'] == 'yixiang') {
  8552. $data['yixiang_assigned_count'][] = $v['id'];
  8553. }
  8554. // 到店分派客户数
  8555. if (!empty($v['assign_type']) && $v['assign_type'] == 'daodian') {
  8556. $data['daodian_assigned_count'][] = $v['id'];
  8557. }
  8558. // 量房分派客户数
  8559. if (!empty($v['assign_type']) && $v['assign_type'] == 'liangfang') {
  8560. $data['liangfang_assigned_count'][] = $v['id'];
  8561. }
  8562. //客户报名时间
  8563. $addtime = strtotime($v['sign_time']);
  8564. //资源库分配数量
  8565. if ($v['crm_res_id'] && strtotime($v['employee_time']) >= $sdate && strtotime($v['employee_time']) <= $edate) {
  8566. $data['is_resource'][] = $v['id'];
  8567. }
  8568. //加微客户,有户型图客户
  8569. $v2 = $v['ext'];
  8570. $ext = json_decode($v2, true);
  8571. if (!empty($ext) && is_array($ext)) {
  8572. $ext = array_filter($ext);
  8573. foreach ($ext as $e) {
  8574. if (isset($e['keyname']) && $e['keyname'] == 'add_wechat_time' && !empty($e['value']) && strtotime($e['value']) >= $sdate && strtotime($e['value']) <= $edate) {
  8575. $data['wechat_count'][] = $v['id'];
  8576. } elseif (isset($e['keyname']) && $e['keyname'] == 'drawing_date' && !empty($e['value']) && strtotime($e['value']) <= $time) {
  8577. $data['drawing_date'][] = $v['id'];
  8578. }
  8579. }
  8580. }
  8581. //一次 二次 三次量房周期
  8582. if (isset($measuring_room_time[$v['id']])) {
  8583. $count = count($measuring_room_time[$v['id']]);
  8584. $start = $count >= 3 ? 2 : $count - 1;
  8585. $measuring_room_cycle = [];
  8586. $measuring_room_cycle[$start] = $measuring_room_time[$v['id']][$start];
  8587. // $measuring_room_cycle = array_slice($measuring_room_time[$v['id']],0, 3);
  8588. foreach ($measuring_room_cycle as $cycle_key => $cycle_item) {
  8589. $cycle_addtime = strtotime($cycle_item);
  8590. if ($addtime < $cycle_addtime) $row['room_cycle_' . $cycle_key][] = $cycle_addtime - $addtime;
  8591. $data['room_cycle_customer_' . $cycle_key][] = $v['id'];
  8592. }
  8593. }
  8594. //一次 二次 三次到店周期
  8595. if (isset($to_the_store_time[$v['id']])) {
  8596. $count = count($to_the_store_time[$v['id']]);
  8597. $start = $count >= 3 ? 2 : $count - 1;
  8598. $to_the_store_times = [];
  8599. $to_the_store_times[$start] = $to_the_store_time[$v['id']][$start];
  8600. // $to_the_store_times = array_slice($to_the_store_time[$v['id']],0, 3);
  8601. foreach ($to_the_store_times as $store_key => $store_item) {
  8602. $store_addtime = strtotime($store_item);
  8603. if ($addtime < $store_addtime) $row['strtore_' . $store_key][] = $store_addtime - $addtime;
  8604. $data['strtore_customer_' . $store_key][] = $v['id'];
  8605. }
  8606. }
  8607. //一次 二次 三次交定周期
  8608. if (isset($deposit_customer[$v['id']])) {
  8609. $count = count($deposit_customer[$v['id']]);
  8610. $start = $count >= 3 ? 2 : $count - 1;
  8611. $deposit_customer_time = [];
  8612. $deposit_customer_time[$start] = $deposit_customer[$v['id']][$start];
  8613. // $deposit_customer_time = array_slice($deposit_customer[$v['id']],0, 3);
  8614. foreach ($deposit_customer_time as $deposit_key => $deposit_item) {
  8615. $deposit_addtime = strtotime($deposit_item);
  8616. if ($addtime < $deposit_item) $row['deposit_' . $deposit_key][] = $deposit_addtime - $addtime;
  8617. $data['deposit_customer_' . $deposit_key][] = $v['id'];
  8618. }
  8619. }
  8620. //死单
  8621. if (intval($v['died']) == 2) $data['died'] += 1;
  8622. }
  8623. $data['housetype_arrow'] = $housetype_arrow;
  8624. foreach ([0, 1, 2] as $range) {
  8625. //平均一次,二次,三次量房周期
  8626. $data['room_cycle_' . $range] = $row['room_cycle_' . $range] ? ceil(array_sum($row['room_cycle_' . $range]) / count($row['room_cycle_' . $range])) : 0;
  8627. //一次,二次,三次 量房人数
  8628. $data['room_cycle_count_' . $range] = count($data['room_cycle_customer_' . $range]);
  8629. //平均到店周期
  8630. $data['strtore_' . $range] = $row['strtore_' . $range] ? ceil(array_sum($row['strtore_' . $range]) / count($row['strtore_' . $range])) : 0;
  8631. //一次 二次 三次 到店人数
  8632. $data['strtore_count_' . $range] = count($data['strtore_customer_' . $range]);
  8633. //平均交定周期
  8634. $data['deposit_' . $range] = $row['deposit_' . $range] ? ceil(array_sum($row['deposit_' . $range]) / count($row['deposit_' . $range])) : 0;
  8635. //一次 二次 三次 交定人数
  8636. $data['deposit_count_' . $range] = count($data['deposit_customer_' . $range]);
  8637. }
  8638. //平均合同金额
  8639. $signed_money = array_filter($row['signed_money']);
  8640. $data['signed_money'] = empty($signed_money) ? 0 : round(array_sum($signed_money) / count($signed_money), 2);
  8641. $data['signed_money_all'] = array_sum($signed_money);
  8642. //房屋类型
  8643. $data['existing_homes_count'] = 0; //现房数量
  8644. $data['forward_housing_count'] = 0; //期房数量
  8645. foreach ($house_type as $house_type_item) {
  8646. if (strpos($house_type_item, '现房') !== false) {
  8647. $data['existing_homes_count'] += 1;
  8648. } elseif (strpos($house_type_item, '期房') !== false) {
  8649. $data['forward_housing_count'] += 1;
  8650. }
  8651. }
  8652. //房屋面积
  8653. $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;
  8654. foreach ($square as $square_item) {
  8655. if (!$square_item) continue;
  8656. $data['square'] += 1;
  8657. if ($square_item <= 80) {
  8658. $data['square_0_80'] += 1;
  8659. } elseif ($square_item <= 100) {
  8660. $data['square_80_100'] += 1;
  8661. } elseif ($square_item <= 120) {
  8662. $data['square_100_120'] += 1;
  8663. } elseif ($square_item <= 200) {
  8664. $data['square_120_200'] += 1;
  8665. } elseif ($square_item <= 500) {
  8666. $data['square_200_500'] += 1;
  8667. } else {
  8668. $data['square_500'] += 1;
  8669. }
  8670. }
  8671. return $data;
  8672. }
  8673. /**
  8674. * 平均周期
  8675. */
  8676. static function avgCycle($cus, $vis)
  8677. {
  8678. $state1 = CustomerVisitLog::changeState('已交定', 'chaos');
  8679. $state2 = CustomerVisitLog::changeState('已量房', 'chaos');
  8680. $state3 = CustomerVisitLog::changeState('已到店', 'chaos');
  8681. $state4 = CustomerVisitLog::changeState('已到场', 'chaos');
  8682. $dep_time = [];
  8683. $room_time = [];
  8684. $store_time = [];
  8685. $next_date = [];
  8686. $visit_customer = []; // 见面的客户id
  8687. foreach ($vis as $v) {
  8688. //交定日期
  8689. if (in_array($v['state'], $state1)) {
  8690. $dep_time[$v['customer_id']] = $v['addtime'];
  8691. } elseif (in_array($v['state'], $state2)) {
  8692. $room_time[$v['customer_id']] = $v['addtime'];
  8693. } elseif (in_array($v['state'], $state3)) {
  8694. $store_time[$v['customer_id']] = $v['addtime'];
  8695. }
  8696. //平均回访次数
  8697. if (!isset($next_date[$v['customer_id']]['addtime'])) {
  8698. $next_date[$v['customer_id']]['addtime'] = [];
  8699. }
  8700. if (!isset($next_date[$v['customer_id']]['next_date'])) {
  8701. $next_date[$v['customer_id']]['next_date'] = [];
  8702. }
  8703. $next_date[$v['customer_id']]['addtime'][] = date('Y-m-d', strtotime($v['addtime']));
  8704. $next_date[$v['customer_id']]['next_date'][] = date('Y-m-d', strtotime($v['next_contact_date']));
  8705. $visit_state = array_merge($state2, $state3, $state4);
  8706. if (in_array($v['state'], $visit_state)) {
  8707. if (!in_array($v['customer_id'], $visit_customer)) {
  8708. $visit_customer[] = $v['customer_id'];
  8709. }
  8710. }
  8711. }
  8712. $dep_cycle = $room_cycle = $room_store_cycle = $next = [];
  8713. foreach ($cus as $v2) {
  8714. if (isset($dep_time[$v2['id']])) {
  8715. $dep_cycle[] = strtotime($dep_time[$v2['id']]) - strtotime($v2['addtime']);
  8716. }
  8717. if (isset($room_time[$v2['id']])) {
  8718. $room_cycle[] = strtotime($room_time[$v2['id']]) - strtotime($v2['addtime']);
  8719. }
  8720. if (isset($room_time[$v2['id']]) && isset($store_time[$v2['id']])) {
  8721. $room_store_cycle[] = abs(strtotime($room_time[$v2['id']]) - strtotime($store_time[$v2['id']]));
  8722. }
  8723. if (isset($next_date[$v2['id']]['addtime']) && $next_date[$v2['id']]['next_date']) {
  8724. $next[] = count(array_filter(array_intersect($next_date[$v2['id']]['addtime'], $next_date[$v2['id']]['next_date'])));
  8725. }
  8726. }
  8727. //平均交定周期
  8728. $data['dep_cycle'] = empty($dep_cycle) ? 0 : ceil(count($dep_cycle) / array_sum($dep_cycle));
  8729. //平均量房周期
  8730. $data['room_cycle'] = empty($room_cycle) ? 0 : ceil(count($room_cycle) / array_sum($room_cycle));
  8731. //平均量房到店周期
  8732. $data['room_store_cycle'] = empty($room_store_cycle) ? 0 : ceil(count($room_store_cycle) / array_sum($room_store_cycle));
  8733. //平均回访次数
  8734. $data['next'] = !array_sum($next) ? 0 : ceil(count($next) / array_sum($next));
  8735. // 见面量
  8736. $data['visit_customer'] = $visit_customer;
  8737. return $data;
  8738. }
  8739. /**
  8740. * 房屋状态
  8741. */
  8742. static function getHouseStatus($house_status, $field)
  8743. {
  8744. $data = [];
  8745. $house = [];
  8746. foreach ($house_status as $v) {
  8747. if (isset($house[$v])) {
  8748. $house[$v]++;
  8749. } else {
  8750. $house[$v] = 1;
  8751. }
  8752. }
  8753. foreach ($field as $k2 => $v2) {
  8754. $data['house_status_' . $v2['id']] = isset($house[$v2['name']]) ? $house[$v2['name']] : 0;
  8755. }
  8756. return $data;
  8757. }
  8758. /**
  8759. * 房屋户型
  8760. */
  8761. private function getHousetypeArrow($housetype, $field, $sign_ids)
  8762. {
  8763. $data = [];
  8764. $house = [];
  8765. foreach ($housetype as $k => $v) {
  8766. @$house[$v][] = $k; // $v 是户型id,$k是客户id
  8767. }
  8768. // 每个户型的成交率
  8769. foreach ($field as $k => $v) {
  8770. $v_customer = $house[$v['id']] ?? [];
  8771. $v_sign = array_intersect($v_customer, $sign_ids);
  8772. $data['housetype_arrow_lv_' . $v['id']] = count($v_customer) > 0 ? round(count($v_sign) / count($v_customer) * 100, 2) . '%' : '0%';
  8773. }
  8774. // 每个户型的客户数
  8775. foreach ($field as $k => $v) {
  8776. $data['housetype_arrow_' . $v['id']] = isset($house[$v['id']]) ? count($house[$v['id']]) : 0;
  8777. }
  8778. return $data;
  8779. }
  8780. /**
  8781. * 弹框页面
  8782. */
  8783. public function generalOpen()
  8784. {
  8785. $eid = input('eid',);
  8786. View::assign('eid', $eid);
  8787. $type = input('type', '');
  8788. $start_date = input('start_date', '');
  8789. $end_date = input('end_date', '');
  8790. $source_id = input('source_id', '');
  8791. if ($start_date) {
  8792. $ls_date = explode(' - ', $start_date);
  8793. if (count($ls_date) == 2) {
  8794. $start_date = $ls_date[0];
  8795. $end_date = $ls_date[1];
  8796. }
  8797. }
  8798. $from = input('from', '', 'trim');
  8799. $field = [
  8800. ['field' => 'name', 'title' => '姓名'],
  8801. ['field' => 'phone', 'title' => '电话']
  8802. ];
  8803. $url = url("empcrm/generalOpenData", ["eid" => $eid, 'type' => $type, 'end_date' => $end_date, 'start_date' => $start_date, 'from' => $from, 'source_id' => $source_id]);
  8804. View::assign('fields', json_encode($field));
  8805. View::assign('url', $url);
  8806. $typehtml = 'add_wechat';
  8807. if ($type == 'invalid_customer_count') $typehtml = 'invalid_popup';
  8808. return View::fetch($typehtml);
  8809. }
  8810. /**
  8811. * 弹框列表
  8812. */
  8813. public function generalOpenData()
  8814. {
  8815. $request = request();
  8816. $param = $request->only(['page' => 1, 'limit' => 10, 'eid' => 0, 'type' => '', 'start_date' => '', 'end_date' => '', 'from' => '', 'source_id' => '']);
  8817. if ($param['start_date'] && $param['end_date']) {
  8818. $param['start_date'] = date('Y-m-d H:i:s', strtotime($param['start_date']));
  8819. $param['end_date'] = date('Y-m-d 23:59:59', strtotime($param['end_date']));
  8820. }/* else{
  8821. $param['start_date'] = '2010-01-01 00:00:00';
  8822. $param['end_date'] = date('Y-m-d H:i:s',time());
  8823. } */
  8824. $root_id = request()->empcrm->root_id;
  8825. $where = [
  8826. ['employee_id', '=', $param['eid']]
  8827. ];
  8828. //$where[] = ['org_id','in',$orgids];
  8829. if ($param['start_date'] && $param['end_date']) {
  8830. $s_date = date('Y-m-d 00:00:00', strtotime($param['start_date']));
  8831. $e_date = date('Y-m-d 23:59:59', strtotime($param['end_date']));
  8832. $date_where = [$s_date, $e_date];
  8833. }
  8834. if ($param['type'] == 'valid') {
  8835. if (!empty($date_where)) {
  8836. $where[] = ['sign_time', 'between', $date_where];
  8837. }
  8838. //有效客户数量
  8839. $state1 = Customer::changeState('待确认', 'chaos');
  8840. $state2 = Customer::changeState('无效', 'chaos');
  8841. $where[] = ['state', 'not in', array_merge($state1, $state2)];
  8842. $where[] = ['died', '<>', 2];
  8843. $list = Customer::where($where)->column('id');
  8844. //2023-03-18 3.3客户标记无效后不能影响数据统计内的有效数据、线索数据 https://kdocs.cn/l/cfx92KlU838H
  8845. $no_where[] = ['employee_id', '=', $param['eid']];
  8846. $no_where[] = ['status', '=', 1];
  8847. $s_date = date('Y-m-d 00:00:00', strtotime($param['start_date']));
  8848. $e_date = date('Y-m-d 23:59:59', strtotime($param['end_date']));
  8849. if (!empty($date_where)) {
  8850. $no_where[] = ['cus_addtime', 'between', $date_where];
  8851. }
  8852. $no_valid_customer = CustomerInvalidLog::where($no_where)->group('customer_id')->column('customer_id');
  8853. $list = array_unique(array_merge($list, $no_valid_customer));
  8854. } elseif ($param['type'] == 'follow_customer_count') {
  8855. //在跟进客户数
  8856. $where[] = ['died', '<>', 2];
  8857. if (!empty($date_where)) {
  8858. $where[] = ['sign_time', 'between', $date_where];
  8859. }
  8860. $countor1[] = ['crm_res_id', 'NULL', null];
  8861. $countor1[] = ['state', 'not in', Customer::changeState('无效', 'chaos')];
  8862. $countor2[] = ['crm_res_id', 'NOTNULL', null];
  8863. $countor2[] = ['state', 'not in', array_merge(Customer::changeState('待确认', 'chaos'), Customer::changeState('无效', 'chaos'))];
  8864. $list = Customer::where($where)->where(function ($query) use ($countor1, $countor2) {
  8865. $query->whereOr([$countor1, $countor2]);
  8866. })->column('id');
  8867. } elseif ($param['type'] == 'add_wechat_count') {
  8868. //加微客户数
  8869. $cids = [];
  8870. $where[] = ['died', '<>', 2];
  8871. if (!empty($date_where)) {
  8872. $where[] = ['add_wechat_time', 'between', $date_where];
  8873. }
  8874. $countor1[] = ['crm_res_id', 'NULL', null];
  8875. $countor1[] = ['state', 'not in', Customer::changeState('无效', 'chaos')];
  8876. $countor2[] = ['crm_res_id', 'NOTNULL', null];
  8877. $countor2[] = ['state', 'not in', Customer::changeState('待确认', 'chaos')];
  8878. $cids = Customer::where($where)->where(function ($query) use ($countor1, $countor2) {
  8879. $query->whereOr([$countor1, $countor2]);
  8880. })->column('id');
  8881. $list = Customer::where([['id', 'in', $cids]])->column('id');
  8882. } elseif ($param['type'] == 'not_sure_count') {
  8883. //待确认客户数
  8884. $where[] = ['died', '<>', 2];
  8885. $where[] = ['crm_res_id', 'NULL', null];
  8886. $where[] = ['state', 'in', Customer::changeState('待确认', 'chaos')];
  8887. if (!empty($date_where)) {
  8888. $where[] = ['sign_time', 'between', $date_where];
  8889. }
  8890. $list = Customer::where($where)->column('id');
  8891. } elseif (in_array($param['type'], ['yixiang_assigned_count', 'liangfang_assigned_count', 'daodian_assigned_count'])) {
  8892. //意向分派客户数
  8893. $all_assign_type = ['yixiang_assigned_count' => 'yixiang', 'liangfang_assigned_count' => 'liangfang', 'daodian_assigned_count' => 'daodian'];
  8894. if (empty($all_assign_type[$param['type']])) return json(['code' => 1, 'data' => '', 'count' => 0, 'msg' => '参数错误']);
  8895. $where[] = ['died', '<>', 2];
  8896. $where[] = ['assign_type', '=', $all_assign_type[$param['type']]];
  8897. if (!empty($date_where)) {
  8898. $where[] = ['sign_time', 'between', $date_where];
  8899. }
  8900. $list = Customer::where($where)->column('id');
  8901. } elseif (in_array($param['type'], ['measuring_room_customer', 'strtore_customer_count', 'deposit_count', 'sign_count', 'invalid_customer_count'])) {
  8902. //量房,到店,签单,转单客户数
  8903. $cids = [];
  8904. $all_state_type = ['measuring_room_customer' => '已量房', 'strtore_customer_count' => '已到店', 'deposit_count' => '已交定', 'sign_count' => '已签单', 'invalid_customer_count' => '无效'];
  8905. if (empty($all_state_type[$param['type']])) return json(['code' => 1, 'data' => '', 'count' => 0, 'msg' => '参数错误']);
  8906. if (!empty($date_where)) {
  8907. if ($param['type'] == 'invalid_customer_count') {
  8908. $invalid_where[] = ['confirm_date', 'between', $date_where];
  8909. } else {
  8910. $db_where[] = ['CustomerVisitLog.confirm_date', 'between', $date_where];
  8911. }
  8912. }
  8913. switch ($param['type']) {
  8914. case 'measuring_room_customer':
  8915. $state = CustomerVisitLog::changeState('已量房', 'chaos');
  8916. break;
  8917. case 'strtore_customer_count':
  8918. $state = CustomerVisitLog::changeState('已到店', 'chaos');
  8919. break;
  8920. case 'deposit_count':
  8921. $state = CustomerVisitLog::changeState('已交定', 'chaos');
  8922. break;
  8923. case 'sign_count':
  8924. $state = CustomerVisitLog::changeState('已签单', 'chaos');
  8925. break;
  8926. case 'invalid_customer_count':
  8927. $state = CustomerVisitLog::changeState('无效', 'chaos');
  8928. break;
  8929. default:
  8930. $state = '';
  8931. break;
  8932. }
  8933. if ($state) {
  8934. $db_where[] = ['CustomerVisitLog.state', 'in', $state];
  8935. $invalid_where[] = ['state', 'in', $state];
  8936. }
  8937. if (in_array($param['type'], ['valid', 'invalid_customer_count'])) {
  8938. $invalid_where[] = ['employee_id', '=', $param['eid']];
  8939. $list = CustomerVisitLog::where($invalid_where)->group('customer_id')->column('customer_id');
  8940. } else {
  8941. $db_where[] = ['Customer.employee_id', '=', $param['eid']];
  8942. $db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
  8943. $cids = Db::view('Customer')
  8944. ->view('CustomerVisitLog', '', 'Customer.id=CustomerVisitLog.customer_id')
  8945. ->where($db_where)
  8946. ->group('Customer.id')
  8947. ->column('Customer.id');
  8948. $list = Customer::where([['id', 'in', $cids]])->where($where)->column('id');
  8949. }
  8950. }
  8951. //$list = $list->page($param['page'], $param['limit'])->select()->toArray();
  8952. if (empty($list)) return json(['code' => 0, 'data' => [], 'count' => 0, 'msg' => '获取成功']);
  8953. $count = count($list);
  8954. $cuwhere = [['id', 'in', $list]];
  8955. $page = intval($param['page']);
  8956. $limit = intval($param['limit']);
  8957. $order = isset($param['order']) ? $param['order'] : 'id desc';
  8958. $con_union = [];
  8959. // 如果搜索条件有来源
  8960. if (!empty($param['source_id'])) {
  8961. $cuwhere[] = ['source_id', '=', $param['source_id']];
  8962. $con_union[] = ['source_id', '=', $param['source_id']];
  8963. }
  8964. if ($param['type'] == 'invalid_customer_count') {
  8965. $con_union[] = ['customer_id', 'in', $list];
  8966. $list = $this->selCustomerValid($cuwhere, $con_union, $page, $limit, $order, $root_id);
  8967. } else {
  8968. $list = $this->selCustomer($cuwhere, $page, $limit, $order, $root_id);
  8969. }
  8970. // foreach ($list as $k => $v) {
  8971. // $list[$k]['phone'] = substr_replace($v['phone'], '******', 3, 6);
  8972. // $list[$k]['name'] = empty($v['name']) ? '未知' : $v['name'];
  8973. // }
  8974. return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
  8975. }
  8976. /**
  8977. * 外呼绑定
  8978. */
  8979. public function bind()
  8980. {
  8981. $params = request()->only(['phone', 'cid']);
  8982. $emp = request()->empcrm;
  8983. $where = [
  8984. ['id', '=', $params['cid']],
  8985. ['state', '<>', '无效'],
  8986. ['employee_id', '=', $emp['id']]
  8987. ];
  8988. //指派的客户
  8989. $where_or = [['assigned_personnel', 'find in set', $emp['id']], ['id', '=', $params['cid']]];
  8990. $where_or1 = [['designer_id', '=', $emp['id']], ['id', '=', $params['cid']]];
  8991. $had = Customer::whereOr([$where, $where_or, $where_or1])->find();
  8992. if (!$had) return json(['code' => 1, 'msg' => '数据不存在']);
  8993. if ($had->died == 2) {
  8994. return json(['code' => 1, 'msg' => '客户死单,暂无法拨打电话']);
  8995. }
  8996. // 查询客户电话
  8997. $customerPhone = $had->phone;
  8998. // 查询员工电话
  8999. $employee = $emp;
  9000. if (!empty($params['phone'])) {
  9001. $employeePhone = ($employee->phone == $params['phone'] || strpos($employee->out_call_phone, $params['phone']) !== false) ? $params['phone'] : $employee->phone;
  9002. if ($employeePhone == '') return json(['code' => 1, 'msg' => '未绑定的手机号']);
  9003. // } else {
  9004. // $employeePhone = $employee->phone;
  9005. }
  9006. // 电话绑定
  9007. $outCallSetting = OutCallSetting::where(['type' => 'yhuatong', 'root_id' => $emp['root_id']])->find();
  9008. if (empty($outCallSetting)) return json(['code' => 1, 'msg' => '未配置外呼系统']);
  9009. $yhuatong = new Yhuatong($outCallSetting->config['appid'], $outCallSetting->config['secretKey']);
  9010. // 中间号获取
  9011. $telXList = explode(',', $outCallSetting->config['tel_x']);
  9012. $randKey = array_rand($telXList);
  9013. $telX = $telXList[$randKey];
  9014. // 获取行业id
  9015. $industryId = $outCallSetting->config['industry'][0]['industry_id'];
  9016. $rs = $yhuatong->bindv2($employeePhone, $customerPhone, $telX, $industryId);
  9017. if ($rs['code'] !== 10000) {
  9018. trace($rs['msg'], 'error');
  9019. return json(['code' => 1, 'msg' => $rs['msg']]);
  9020. } else {
  9021. // 添加记录
  9022. $log = OutCallLog::create([
  9023. 'session_id' => $rs['data']['sessionId'],
  9024. 'bind_num' => $rs['data']['telX'],
  9025. 'caller_num' => $employeePhone,
  9026. 'callee_num' => $customerPhone,
  9027. 'employee_id' => $emp['id'],
  9028. 'root_id' => $emp['root_id'],
  9029. 'customer_id' => $params['cid']
  9030. ]);
  9031. }
  9032. $had->updatetime = date('Y-m-d H:i:s');
  9033. $had->save();
  9034. if ($had->employee_id == $emp['id'])
  9035. $customerEmployee = $emp;
  9036. else
  9037. $customerEmployee = Employee::find($had->employee_id);
  9038. // 添加访问记录
  9039. CustomerVisitLog::Create([
  9040. 'customer_id' => $params['cid'],
  9041. 'type' => 1,
  9042. 'employee_id' => $emp['id'],
  9043. 'user_id' => $employee->uid,
  9044. 'org_id' => $employee->org_id,
  9045. 'customer_employee_id' => $had->employee_id,
  9046. 'customer_org_id' => $customerEmployee->org_id,
  9047. 'data_type' => 'out_call',
  9048. 'data_id' => $log->id,
  9049. 'remark' => '回访跟踪了客户'
  9050. ]);
  9051. return json(['code' => 0, 'msg' => '拨打中,请接听']);
  9052. }
  9053. /**
  9054. * 快速状态跟进
  9055. */
  9056. public function kb()
  9057. {
  9058. $request = request();
  9059. $empcrm = $request->empcrm;
  9060. $employeeTypeName = $empcrm->is_manager == 1 ? '部门领导' : ($empcrm->org_type == 2 ? '设计师' : '销售');
  9061. // 请求参数设置
  9062. $stateParam = [
  9063. 3 => ['money', 'confirm_date'],
  9064. 2 => ['money', 'confirm_date', 'estimated_sign_money'],
  9065. 7 => ['confirm_date'],
  9066. 8 => ['confirm_date'],
  9067. 15 => ['money', 'confirm_date']
  9068. ];
  9069. $param = $request->only(['customer_id', 'state', 'money', 'add_wechat_time', 'vlogid']);
  9070. //2022-10-27 逻辑修改,跟进保护优化 如果后台设置了重复客户跟进保护规则 跟进到保护状态后其他业务员的此重复客户就不能在跟进了,但是可以置为无效
  9071. $forbiddenState = $this->forbiddenState($param['customer_id'], null);
  9072. if ($forbiddenState != false) return json(['code' => 1, 'msg' => $forbiddenState]);
  9073. // 数据查找
  9074. $where = [
  9075. ['id', '=', $param['customer_id']],
  9076. ['state', '<>', '无效']
  9077. ];
  9078. if ($empcrm->is_manager == 1) {
  9079. $team_orgs = orgSubIds($request->empcrm->org_id);
  9080. $where[] = ['org_id', 'in', $team_orgs];
  9081. } else {
  9082. $where[] = ['employee_id|designer_id|assigned_personnel', 'find in set', $empcrm->id];
  9083. }
  9084. $had = Customer::where($where)->find();
  9085. if ($had === null) return json(['code' => 1, 'msg' => '添加失败,数据不存在']);
  9086. if (!empty($param['add_wechat_time'])) {
  9087. $ext = $had->ext;
  9088. $ext = json_decode($ext, true);
  9089. $ei = false;
  9090. foreach ($ext as &$e) {
  9091. if ($e['keyname'] == 'add_wechat_time') {
  9092. $e['value'] = date('Y-m-d', strtotime($param['add_wechat_time']));
  9093. $ei = true;
  9094. }
  9095. }
  9096. if ($ei === false) {
  9097. $fid = CustomerPortraitField::where(['root_id' => $empcrm->root_id, 'keyname' => 'add_wechat_time'])->value('id');
  9098. if ($fid) $ext[] = ['id' => $fid, 'keyname' => 'add_wechat_time', 'value' => date('Y-m-d', strtotime($param['add_wechat_time']))];
  9099. }
  9100. $had->ext = json_encode($ext);
  9101. }
  9102. if (empty($param['state']) || !in_array($param['state'], [2, 3, 7, 8, 15])) {
  9103. $had->save();
  9104. return json(['code' => 0, 'msg' => '保存成功']);
  9105. } else {
  9106. $customerVisitLogState = CustomerVisitLog::changeState($param['state'], 'n');
  9107. $stateStr = CustomerVisitLog::changeState($param['state']);
  9108. $customerState = Customer::changeState($stateStr, 'n');
  9109. $stateParamValue = $request->only($stateParam[$customerVisitLogState]);
  9110. }
  9111. // 在跟进之前,增加了设计师必填判断,得先查一下设置
  9112. $log_state = CustomerVisitLog::changeState($param['state'], 'state');
  9113. $state = Customer::changeState($log_state, 'state');
  9114. $stateCheck = ['已到店' => 'daodian', '已到场' => 'daochang', '已量房' => 'liangfang', '已交定' => 'qiandan', '已签单' => 'zhuandan'];
  9115. if (in_array($state, ['已到店', '已到场', '已量房', '已交定', '已签单'])) {
  9116. $need_designer[] = ['root_id', '=', $empcrm['root_id']];
  9117. $need_designer[] = ['name', '=', 'add_visit_log_need_designer'];
  9118. $need_designer_setting = Setting::where($need_designer)->findOrEmpty();
  9119. if (!$need_designer_setting->isEmpty()) {
  9120. $need_state = explode(',', $need_designer_setting['content']);
  9121. $input_state = $stateCheck[$state] ?? '';
  9122. if (!empty($input_state) && in_array($input_state, $need_state)) {
  9123. // 需要验证
  9124. $customer_find = Customer::find($param['customer_id']);
  9125. if (!empty($customer_find)) {
  9126. if (empty($customer_find['assigned_personnel'])) {
  9127. return json(['code' => 1, 'msg' => '请先指派设计师']);
  9128. }
  9129. $assigned_personnel = explode(',', $customer_find['assigned_personnel']);
  9130. $assigned_personnel_employee_org = Employee::where('id', 'in', $assigned_personnel)->column('org_id');
  9131. $assigned_personnel_org_type = Org::where('id', 'in', $assigned_personnel_employee_org)->column('org_type');
  9132. if (!in_array(2, $assigned_personnel_org_type)) {
  9133. return json(['code' => 1, 'msg' => '请先指派设计师']);
  9134. }
  9135. }
  9136. }
  9137. }
  9138. }
  9139. // 量房到店保护机制
  9140. $protectedDay = 0;
  9141. if (7 == $customerVisitLogState || 8 == $customerVisitLogState) {
  9142. // 保护设置
  9143. $sub_setting = Setting::where([['root_id', '=', $empcrm->root_id], ['name', '=', 'subscribe_protected']])->findOrEmpty();
  9144. if (!$sub_setting->isEmpty()) { // 有设置
  9145. $setting_content = json_decode($sub_setting['content'], true);
  9146. // 判断保护是否开启
  9147. $bh = [];
  9148. foreach ($setting_content as $k_s => $v_s) {
  9149. if (!empty($v_s['state']) && !empty($v_s['day']) && $v_s['state'] == 1) {
  9150. $bh[$k_s] = $v_s['day'];
  9151. if (($k_s == 'ydaodian' && 7 == $customerVisitLogState) || ($k_s == 'yliangfang' && 8 == $customerVisitLogState)) $protectedDay = $v_s['day'];
  9152. }
  9153. }
  9154. $phones = [$had->phone, $had->phone1, $had->phone2];
  9155. $phones = array_filter($phones);
  9156. // 如果设置了保护
  9157. if (!empty($bh)) {
  9158. foreach ($bh as $k => $d) {
  9159. $condition = $this->protectCondition($k, $d);
  9160. if (!empty($condition['log'])) {
  9161. $e = CustomerVisitLog::withJoin('customer')->where([['Customer.id', '<>', $param['customer_id']]])->where($condition['log'])
  9162. ->where(function ($query) use ($phones) {
  9163. foreach ($phones as $p) {
  9164. $c = [
  9165. [['customer.phone', '=', $p]],
  9166. [['customer.phone1', '=', $p]],
  9167. [['customer.phone2', '=', $p]]
  9168. ];
  9169. $query->whereOr($c);
  9170. }
  9171. })->find();
  9172. if ($e) return json(['code' => 1, 'msg' => '该客户在保护期内,无法提交']);
  9173. }
  9174. if (!empty($condition['subscribe'])) {
  9175. $e = CustomersSubscribe::withJoin('customer')->where([['Customer.id', '<>', $param['customer_id']]])->where($condition['subscribe'])
  9176. ->where(function ($query) use ($phones) {
  9177. foreach ($phones as $p) {
  9178. $c = [
  9179. [['customer.phone', '=', $p]],
  9180. [['customer.phone1', '=', $p]],
  9181. [['customer.phone2', '=', $p]]
  9182. ];
  9183. $query->whereOr($c);
  9184. }
  9185. })
  9186. ->count();
  9187. if ($e > 0) return json(['code' => 1, 'msg' => '该客户在保护期内,无法提交']);
  9188. }
  9189. }
  9190. }
  9191. }
  9192. }
  9193. $otherMsg = '';
  9194. // 更改客户信息
  9195. $orders = false;
  9196. //签单之后不能签单交定,可以跟进
  9197. if (3 == $customerVisitLogState) {
  9198. $is = CustomerVisitLog::where([['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')], ['customer_id', '=', $had->id]])->count();
  9199. if ($is) return $this->updateMoney($param);
  9200. $orders = true;
  9201. $had->signed_money = $stateParamValue['money'];
  9202. $otherMsg = ",签单金额:" . $stateParamValue['money'];
  9203. }
  9204. if (2 == $customerVisitLogState) {
  9205. $is = CustomerVisitLog::where([['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')], ['customer_id', '=', $had->id]])->count();
  9206. if ($is) return json(['code' => 1, 'msg' => '该客户已签单']);
  9207. $orders = true;
  9208. $had->deposit_money = $stateParamValue['money'];
  9209. $had->estimated_sign_money = $stateParamValue['estimated_sign_money'];
  9210. $otherMsg = ",定金金额:" . $stateParamValue['money'] . ', 预计签单金额' . $stateParamValue['estimated_sign_money'];
  9211. }
  9212. if (15 == $customerVisitLogState) $otherMsg = ",无忧金额:" . $stateParamValue['money'];
  9213. Db::startTrans();
  9214. //如果是分派的客户第一次跟进直接修改分派信息为空
  9215. if (!empty($had->transfer_info)) $had->transfer_info = NULL;
  9216. //如果是第一次有效修改有效时间
  9217. if (in_array($param['state'], [2, 3, 7, 8]) && $had->state == '待确认') {
  9218. $had->valid_time = !empty($stateParamValue['confirm_date']) ? $stateParamValue['confirm_date'] : date('Y-m-d H:i:s');
  9219. }
  9220. $had->state = $customerState;
  9221. $had->last_contact_date = date('Y-m-d', time());
  9222. $had->updatetime = date('Y-m-d H:i:s', time());
  9223. $had->fresh = 0;
  9224. // 更新客户保护期限
  9225. if ($protectedDay) $had->protected_to = date('Y-m-d', time() + $protectedDay * 86400);
  9226. $had->save();
  9227. // 添加确定消息内容
  9228. $confirmMsg = '';
  9229. if (!empty($stateParamValue['confirm_date'])) {
  9230. $confirmMsg = ', 时间:' . $stateParamValue['confirm_date'];
  9231. }
  9232. // 备注
  9233. $employeeName = Employee::where(['id' => $empcrm->id])->value('name');
  9234. $remark = '';
  9235. $remark = $employeeTypeName . $employeeName . '更新客户状态为' . $stateStr . $confirmMsg . $otherMsg;
  9236. $is_set = Setting::where([['root_id', '=', $empcrm->root_id], ['name', '=', 'nextVisitTimeIsOk']])->value('content');
  9237. // 添加追踪记录
  9238. $visitLog = [
  9239. 'customer_id' => $had->id,
  9240. 'type' => '',
  9241. 'next_contact_date' => $is_set ? date('Y-m-d') : null,
  9242. 'employee_id' => $empcrm->id,
  9243. 'user_id' => $empcrm->uid,
  9244. 'remark' => $remark,
  9245. 'state' => $customerVisitLogState,
  9246. 'money' => $stateParamValue['money'] ?? 0,
  9247. 'confirm_date' => $stateParamValue['confirm_date'] ?? date('Y-m-d H:i:s'),
  9248. 'org_id' => $empcrm['org_id'],
  9249. 'customer_employee_id' => $had->employee_id,
  9250. 'customer_org_id' => Employee::where('id', $had->employee_id)->value('org_id'),
  9251. ];
  9252. $visitLog = CustomerVisitLog::create($visitLog);
  9253. if ($orders) $this->preformance_tasks($stateStr, $param['customer_id'], $stateParamValue['money'], $visitLog->id); //计算业绩
  9254. // //如果签单,默认交定
  9255. // if ($customerVisitLogState == 3) {
  9256. // $lw[] = CustomerVisitLog::changeState(['state', '=', '交定']);
  9257. // $lw[] = ['customer_id', '=', $param['customer_id']];
  9258. // $check = CustomerVisitLog::where($lw)->find();
  9259. // if (!$check) {
  9260. // $l_visitlog = $visitLog->toArray();
  9261. // unset($l_visitlog['id']);
  9262. // $l_visitlog['state'] = '交定';
  9263. // $l_visitlog['remark'] = $employeeTypeName . $employeeName . '变更客户状态为交定';
  9264. // $new_visitlog = array_merge($param, $l_visitlog);
  9265. // $new_visitlog['money'] = 0; //直接签单默认交定金额为0
  9266. // $mr = CustomerVisitLog::create($new_visitlog);
  9267. // $this->preformance_tasks('已交定', $param['customer_id'], $stateParamValue['money'], $mr->id); //计算业绩
  9268. // }
  9269. // }
  9270. if ($customerVisitLogState == 7 || $customerVisitLogState == 8) {
  9271. $type = $customerVisitLogState == 7 ? 1 : 3;
  9272. $subscribe = CustomersSubscribe::where([['customer_id', '=', $had['id']], ['type', '=', $type], ['state', '=', 0]])->find();
  9273. if (!empty($subscribe)) {
  9274. $subscribe->state = 1;
  9275. $subscribe->save();
  9276. }
  9277. }
  9278. //增加经纪人送积分功能
  9279. if ($had->agents_id && in_array($customerVisitLogState, [2, 3, 7])) {
  9280. $this->agents_integral($had->agents_id, $customerVisitLogState, $had->id);
  9281. }
  9282. // 微爆活动
  9283. if (in_array($param['state'], [2, 3, 7, 8])) {
  9284. $activity_data['customer_id'] = $param['customer_id'];
  9285. $activity_data['employee_id'] = $had->employee_id;
  9286. $activity_data['org_id'] = $had->org_id;
  9287. switch ($param['state']) {
  9288. case 7: // 已到店
  9289. $activity_data['type'] = 2;
  9290. break;
  9291. case 8: // 已量房
  9292. $activity_data['type'] = 3;
  9293. break;
  9294. case 2: // 已交定
  9295. $activity_data['type'] = 4;
  9296. break;
  9297. case 3: // 已签单
  9298. $activity_data['type'] = 5;
  9299. break;
  9300. case 9: // 已到场
  9301. $activity_data['type'] = 7;
  9302. break;
  9303. default:
  9304. break;
  9305. }
  9306. WechatActivityIntegral::addIntegral($activity_data, $empcrm['root_id']);
  9307. }
  9308. Db::commit();
  9309. Console::call('medal', ['visit_log', (string)$empcrm->id, (string)$empcrm->root_id]); //勋章
  9310. if ($empcrm->is_manager == 1) {
  9311. // 经理回访后修改客户的待回访的状态
  9312. Customer::where('id', $had->id)->update(['return_visit' => 0]);
  9313. }
  9314. // if ($param['state'] == 2) {
  9315. // // 发送管理层模板消息
  9316. // $msg = ['first'=>'行为通知', 'keyword1'=>'客户确认到店', 'keyword2'=>"客户".$had['name']."(".$had['phone'].")确认到店", 'keyword3'=>date('Y/m/d'), 'remark'=>'请悉知'];
  9317. // TmpMsg::sendMsgToLeader($msg, $empcrm['org_id']);
  9318. // }
  9319. $had['name'] = !empty($had['name']) ? $had['name'] : '';
  9320. $had['phone'] = substr_replace($had['phone'], '******', 3, 6);
  9321. if ($param['state'] == 3) {
  9322. // 发送管理层模板消息
  9323. $msg = ['first' => '行为通知', 'keyword1' => '客户签单', 'keyword2' => "客户" . $had['name'] . "(" . $had['phone'] . ")签单", 'keyword3' => date('Y/m/d'), 'remark' => '请悉知'];
  9324. TmpMsg::sendMsgToLeader($msg, $empcrm['org_id']);
  9325. }
  9326. if ($param['state'] == 7) {
  9327. // 发送管理层模板消息
  9328. $msg = ['first' => '行为通知', 'keyword1' => '客户确认到店', 'keyword2' => "客户" . $had['name'] . "(" . $had['phone'] . ")确认到店", 'keyword3' => date('Y/m/d'), 'remark' => '请悉知'];
  9329. TmpMsg::sendMsgToLeader($msg, $empcrm['org_id']);
  9330. }
  9331. return json(['code' => 0, 'msg' => '跟进成功']);
  9332. }
  9333. private function protectCondition($k_s, $day)
  9334. {
  9335. $empcrm = request()->empcrm;
  9336. $org_ids = orgSubIds($empcrm->root_id);
  9337. $check_where = [];
  9338. $v_where = [];
  9339. switch ($k_s) {
  9340. case 'liangfang':
  9341. $v_where[] = [
  9342. ['customers_subscribe.org_id', 'in', $org_ids],
  9343. ['customers_subscribe.addtime', '>', date('Y-m-d H:i:s', time() - 24 * 3600 * $day)],
  9344. ['customers_subscribe.employee_id', '<>', $empcrm->id],
  9345. ['customers_subscribe.type', '=', 3],
  9346. ['customers_subscribe.state', '=', 1]
  9347. ];
  9348. $check_where = [
  9349. ['customer_visit_log.org_id', 'in', $org_ids],
  9350. ['customer_visit_log.addtime', '>', date('Y-m-d H:i:s', time() - 24 * 3600 * $day)],
  9351. ['customer_visit_log.employee_id', '<>', $empcrm->id],
  9352. ['customer_visit_log.state', 'in', CustomerVisitLog::changeState('已量房', 'chaos')]
  9353. ];
  9354. break;
  9355. case 'yliangfang': //预约量房
  9356. $v_where[] = [
  9357. ['customers_subscribe.org_id', 'in', $org_ids],
  9358. ['customers_subscribe.addtime', '>', date('Y-m-d H:i:s', time() - 24 * 3600 * $day)],
  9359. ['customers_subscribe.employee_id', '<>', $empcrm->id],
  9360. ['customers_subscribe.type', '=', 3],
  9361. ['customers_subscribe.state', '=', 0]
  9362. ];
  9363. break;
  9364. case 'daodian':
  9365. $v_where[] = [
  9366. ['customers_subscribe.org_id', 'in', $org_ids],
  9367. ['customers_subscribe.addtime', '>', date('Y-m-d H:i:s', time() - 24 * 3600 * $day)],
  9368. ['customers_subscribe.employee_id', '<>', $empcrm->id],
  9369. ['customers_subscribe.type', '=', 1],
  9370. ['customers_subscribe.state', '=', 1]
  9371. ];
  9372. $check_where = [
  9373. ['customer_visit_log.org_id', 'in', $org_ids],
  9374. ['customer_visit_log.addtime', '>', date('Y-m-d H:i:s', time() - 24 * 3600 * $day)],
  9375. ['customer_visit_log.employee_id', '<>', $empcrm->id],
  9376. ['customer_visit_log.state', 'in', CustomerVisitLog::changeState('已到店', 'chaos')]
  9377. ];
  9378. break;
  9379. case 'ydaodian':
  9380. $v_where[] = [
  9381. ['customers_subscribe.org_id', 'in', $org_ids],
  9382. ['customers_subscribe.addtime', '>', date('Y-m-d H:i:s', time() - 24 * 3600 * $day)],
  9383. ['customers_subscribe.employee_id', '<>', $empcrm->id],
  9384. ['customers_subscribe.type', '=', 1],
  9385. ['customers_subscribe.state', '=', 0]
  9386. ];
  9387. break;
  9388. case 'daochang':
  9389. $v_where[] = [
  9390. ['customers_subscribe.org_id', 'in', $org_ids],
  9391. ['customers_subscribe.addtime', '>', date('Y-m-d H:i:s', time() - 24 * 3600 * $day)],
  9392. ['customers_subscribe.employee_id', '<>', $empcrm->id],
  9393. ['customers_subscribe.type', '=', 2],
  9394. ['customers_subscribe.state', '=', 1]
  9395. ];
  9396. $check_where = [
  9397. ['customer_visit_log.org_id', 'in', $org_ids],
  9398. ['customer_visit_log.addtime', '>', date('Y-m-d H:i:s', time() - 24 * 3600 * $day)],
  9399. ['customer_visit_log.employee_id', '<>', $empcrm->id],
  9400. ['customer_visit_log.state', 'in', CustomerVisitLog::changeState('已到场', 'chaos')]
  9401. ];
  9402. break;
  9403. case 'ydaochang':
  9404. $v_where[] = [
  9405. ['customers_subscribe.org_id', 'in', $org_ids],
  9406. ['customers_subscribe.addtime', '>', date('Y-m-d H:i:s', time() - 24 * 3600 * $day)],
  9407. ['customers_subscribe.employee_id', '<>', $empcrm->id],
  9408. ['customers_subscribe.type', '=', 2],
  9409. ['customers_subscribe.state', '=', 0]
  9410. ];
  9411. break;
  9412. }
  9413. return ['log' => $check_where, 'subscribe' => $v_where];
  9414. }
  9415. public function addwuxiao_logs()
  9416. {
  9417. $root_id = 1035;
  9418. $orgids = orgSubIds($root_id);
  9419. $empids = Employee::where([['org_id', 'in', $orgids], ['state', '=', '在职']])->column('id');
  9420. $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');
  9421. foreach ($list as $key => $val) {
  9422. $cus = Customer::where('id', $val['customer_id'])->field('addtime,org_id,source_id')->find();
  9423. if (empty($cus)) {
  9424. $cus = CustomerRecycle::where('customer_id', '=', $val['customer_id'])->field('addtime,org_id,source_id')->find();
  9425. // var_dump($cus->toArray());
  9426. // exit;
  9427. }
  9428. if (empty($cus)) continue;
  9429. $org_id = !empty($val['customer_org_id']) ? $val['customer_org_id'] : $val['org_id'];
  9430. $org_id = $org_id ? $org_id : $cus['org_id'];
  9431. $employee_id = !empty($val['customer_employee_id']) ? $val['customer_employee_id'] : $val['employee_id'];
  9432. $yes = CustomerInvalidLog::where([['customer_id', '=', $val['customer_id']], ['employee_id', '=', $employee_id]])->count();
  9433. if ($yes) continue;
  9434. $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();
  9435. $add[] = [
  9436. 'customer_id' => $val['customer_id'],
  9437. 'employee_id' => $employee_id,
  9438. 'org_id' => $org_id,
  9439. 'root_id' => $root_id,
  9440. 'cus_addtime' => $cus['addtime'],
  9441. 'visitlog_id' => $val['id'],
  9442. 'assigned_personnel' => '',
  9443. 'source_id' => !empty($cus['source_id']) ? $cus['source_id'] : 0,
  9444. 'status' => $youxiao ? 1 : 2
  9445. ];
  9446. }
  9447. // var_dump($add);
  9448. // exit;
  9449. $addlog = new CustomerInvalidLog;
  9450. $addlog->saveAll($add);
  9451. echo '添加完成';
  9452. }
  9453. public function save_log()
  9454. {
  9455. // $org_id = Org::where([['path','like','%1035-%']])->column('id');
  9456. // $empids = Employee::where([['org_id','in',$org_id],['state','=','在职']])->column('id');
  9457. // $where = [
  9458. // ['employee_id','in',$empids],
  9459. // ['state','in',['无效',11]],
  9460. // ['org_id','=',0]
  9461. // ];
  9462. // $list = CustomerVisitLog::where($where)->group('customer_id')->field('max(addtime) as addtime,id,customer_id,state')->select()->toArray();
  9463. // //var_dump($list);exit;
  9464. // foreach($list as $key=>$val){
  9465. // $chaild = CustomerVisitLog::where([['customer_id','=',$val['customer_id']],['state','in',['无效',11]]])->count();
  9466. // if($chaild == 1) continue;
  9467. // CustomerVisitLog::where([['customer_id','=',$val['customer_id']],['id','<>',$val['id']]])->update(['state'=>1]);
  9468. // //var_dump($val['customer_id']);exit;
  9469. // }
  9470. }
  9471. /**
  9472. * 飞鱼线索列表
  9473. */
  9474. public function fish_list()
  9475. {
  9476. $request = request();
  9477. $root_id = $request->empcrm->root_id;
  9478. $emp_id = $request->empcrm->id;
  9479. // $emp_name = $request->empcrm->name;
  9480. $emp_org_id = $request->empcrm->org_id;
  9481. $is_manager = $request->empcrm->is_manager;
  9482. // $path = Org::where([['pid','=',$emp_org_id]])->count();
  9483. $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' => '']);
  9484. $clue_source = [0 => '外部流量', 1 => '正常投放', 2 => '外部导入', 3 => '异常提交', 4 => '广告预览', 5 => '抖音私信', 6 => '鲁班线索'];
  9485. $clue_state = [1 => '新线索', 2 => '有意向', 3 => '转商机', 4 => '无效', 5 => '已加微信', 6 => '待再次沟通'];
  9486. $clue_type = [0 => '表单提交', 1 => '在线咨询', 2 => '智能电话', 3 => '网页回呼', 4 => '卡券', 5 => '抽奖'];
  9487. $table_tags = [
  9488. [
  9489. 'title' => '高级意向',
  9490. 'name' => 'all0',
  9491. 'value' => 0,
  9492. 'tags' =>
  9493. [
  9494. ['title' => '已签约合同', 'name' => 'all0_1'],
  9495. ['title' => '已缴纳定金', 'name' => 'all0_2'],
  9496. ['title' => '客户已到店', 'name' => 'all0_3'],
  9497. ['title' => '预约上门量房', 'name' => 'all0_4'],
  9498. ]
  9499. ],
  9500. [
  9501. 'title' => '中级意向',
  9502. 'name' => 'all1',
  9503. 'value' => 1,
  9504. 'tags' =>
  9505. [
  9506. ['title' => '能提供户型图', 'name' => 'all1_1'],
  9507. ['title' => '有装修预算', 'name' => 'all1_2'],
  9508. ['title' => '添加微信待通过', 'name' => 'all1_3'],
  9509. ['title' => '添加微信已通过', 'name' => 'all1_4'],
  9510. ]
  9511. ],
  9512. [
  9513. 'title' => '初级意向',
  9514. 'name' => 'all2',
  9515. 'value' => 2,
  9516. 'tags' =>
  9517. [
  9518. ['title' => '仅咨询了解', 'name' => 'all2_1'],
  9519. ['title' => '无真实需求', 'name' => 'all2_2'],
  9520. ['title' => '对比其他平台', 'name' => 'all2_3'],
  9521. ['title' => '对价格不反感', 'name' => 'all2_4'],
  9522. ['title' => '近期有装修需求', 'name' => 'all2_5'],
  9523. ['title' => '添加微信', 'name' => 'all2_6'],
  9524. ]
  9525. ],
  9526. [
  9527. 'title' => '意向',
  9528. 'name' => 'all3',
  9529. 'value' => 3,
  9530. 'tags' =>
  9531. [
  9532. ['title' => '低意向', 'name' => 'all3_1'],
  9533. ['title' => '中意向', 'name' => 'all3_2'],
  9534. ['title' => '高意向', 'name' => 'all3_3'],
  9535. ]
  9536. ],
  9537. [
  9538. 'title' => '无效',
  9539. 'name' => 'all4',
  9540. 'value' => 4,
  9541. 'tags' =>
  9542. [
  9543. ['title' => '添加微信未通过', 'name' => 'all4_1'],
  9544. ['title' => '多次联系不上', 'name' => 'all4_2'],
  9545. ['title' => '拒绝加微信', 'name' => 'all4_3'],
  9546. ['title' => '无需求', 'name' => 'all4_4'],
  9547. ['title' => '错号', 'name' => 'all4_5'],
  9548. ['title' => '关机', 'name' => 'all4_6'],
  9549. ['title' => '空号', 'name' => 'all4_7'],
  9550. ['title' => '联系不上', 'name' => 'all4_8'],
  9551. ['title' => '无意愿', 'name' => 'all4_9'],
  9552. ['title' => '非本人', 'name' => 'all4_10']
  9553. ]
  9554. ],
  9555. [
  9556. 'title' => '默认',
  9557. 'name' => 'all5',
  9558. 'value' => 5,
  9559. 'tags' =>
  9560. [
  9561. ['title' => '加微信', 'name' => 'all5_1'],
  9562. ['title' => '待确认', 'name' => 'all5_2'],
  9563. ['title' => '无效', 'name' => 'all5_3'],
  9564. ]
  9565. ],
  9566. ];
  9567. if (!$request->isAjax()) {
  9568. $this->screen();
  9569. $advertiser_list[] = ['id' => 1, 'name' => '高潜客户'];
  9570. $channel_setting = Company::where(['root_id' => $root_id])->field('root_id,channel_setting,channel_setting_wh')->find();
  9571. if (isset($channel_setting['channel_setting_wh']) && $channel_setting['channel_setting_wh']) {
  9572. $tmp_data = json_decode($channel_setting['channel_setting_wh'], true);
  9573. $advertiser_id = explode(',', $tmp_data['fish_adv']);
  9574. $fish_account = explode(',', $tmp_data['fish_account']);
  9575. foreach ($advertiser_id as $key => $val) {
  9576. $advertiser_list[] = [
  9577. 'id' => $advertiser_id[$key],
  9578. 'name' => isset($fish_account[$key]) ? $fish_account[$key] : '暂未设置名称',
  9579. ];
  9580. }
  9581. }
  9582. if (isset($channel_setting['channel_setting']) && $channel_setting['channel_setting']) {
  9583. $tmp_data = json_decode($channel_setting['channel_setting'], true);
  9584. $advertiser_id = explode(',', $tmp_data['fish_adv']);
  9585. $fish_account = explode(',', $tmp_data['fish_account']);
  9586. foreach ($advertiser_id as $key => $val) {
  9587. $advertiser_list[] = [
  9588. 'id' => $advertiser_id[$key],
  9589. 'name' => isset($fish_account[$key]) ? $fish_account[$key] : '暂未设置名称',
  9590. ];
  9591. }
  9592. }
  9593. $team_orgs = $is_manager ? orgSubIds($emp_org_id) : [$emp_org_id];
  9594. $emporg_list = Org::where([['id', 'in', $team_orgs]])->column('id,name');
  9595. View::assign('emporg_list', $emporg_list);
  9596. View::assign('advertiser_id', $advertiser_list);
  9597. View::assign('is_manager', $is_manager);
  9598. View::assign('soudata_now', $clue_source);
  9599. View::assign('clue_state', $clue_state);
  9600. View::assign('table_tags', $table_tags);
  9601. // $this->synchronize_fish_data();
  9602. //todo 同步直播数据
  9603. // $this->synchronize_fish_live_data();
  9604. return View::fetch();
  9605. }
  9606. $org_employee = [$emp_id];
  9607. if ($is_manager == 1) {
  9608. $team_orgs = orgSubIds($emp_org_id);
  9609. // $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
  9610. $condition[] = ['org_id', 'in', $team_orgs];
  9611. } else {
  9612. $condition[] = ['employee_id', 'in', $org_employee];
  9613. }
  9614. $condition[] = ['root_id', '=', $root_id];
  9615. // $condition[] = ['allocation_status', '=', 1];
  9616. $condition[] = ['type', 'in', [1, 4]];
  9617. if (!empty($param['name'])) $condition[] = ['name', 'like', '%' . $param['name'] . '%'];
  9618. if (!empty($param['clue_id'])) $condition[] = ['clue_id', 'like', '%' . $param['clue_id'] . '%'];
  9619. if (!empty($param['telephone'])) $condition[] = ['telephone', '=', $param['telephone']];
  9620. if (!empty($param['tags'])) {
  9621. $tags = explode(',', $param['tags']);
  9622. $tmp_tags = [];
  9623. foreach ($tags as $val) {
  9624. $tmp_tags[] = "%{$val}%";
  9625. }
  9626. $condition[] = ['tags', 'like', $tmp_tags, 'OR'];
  9627. }
  9628. if (!empty($param['address'])) $condition[] = ['address', 'like', '%' . $param['address'] . '%'];
  9629. if (!empty($param['advertiser_id'])) {
  9630. if ($param['advertiser_id'] == 1) {
  9631. $condition[] = ['advertiser_id', '=', 0];
  9632. } else {
  9633. $condition[] = ['advertiser_id', '=', $param['advertiser_id']];
  9634. }
  9635. }
  9636. if (!empty($param['ad_name'])) $condition[] = ['ad_name', 'like', '%' . $param['ad_name'] . '%'];
  9637. if (!empty($param['ad_id'])) $condition[] = ['ad_id', 'like', '%' . $param['ad_id'] . '%'];
  9638. if (!empty($param['promotion_id'])) $condition[] = ['promotion_id', 'like', '%' . $param['promotion_id'] . '%'];
  9639. if (!empty($param['promotion_name'])) $condition[] = ['promotion_name', 'like', '%' . $param['promotion_name'] . '%'];
  9640. if (!empty($param['weixin'])) $condition[] = ['weixin', 'like', '%' . $param['weixin'] . '%'];
  9641. if (!empty($param['qq'])) $condition[] = ['qq', 'like', '%' . $param['qq'] . '%'];
  9642. if (!empty($param['email'])) $condition[] = ['email', 'like', '%' . $param['email'] . '%'];
  9643. if (!empty($param['location'])) $condition[] = ['location', 'like', '%' . $param['location'] . '%'];
  9644. if (!empty($param['module_id'])) $condition[] = ['module_id', 'like', '%' . $param['module_id'] . '%'];
  9645. if (!empty($param['module_name'])) $condition[] = ['module_name', 'like', '%' . $param['module_name'] . '%'];
  9646. if (!empty($param['clue_owner_name'])) $condition[] = ['clue_owner_name', 'in', explode(',', $param['clue_owner_name'])];
  9647. if (!empty($param['clue_source'])) {
  9648. $aa = [];
  9649. foreach (explode(',', $param['clue_source']) as $vv) {
  9650. $aa[] = $vv - 1;
  9651. }
  9652. $condition[] = ['clue_source', 'in', $aa];
  9653. }
  9654. if (!empty($param['is_allocation'])) $condition[] = ['is_allocation', '=', $param['is_allocation'] - 1];
  9655. if (!empty($param['is_contact'])) $condition[] = ['is_contact', '=', $param['is_contact'] - 1];
  9656. if (!empty($param['is_fenpei'])) {
  9657. if ($param['is_fenpei'] == 2) $condition[] = ['employee_id', '>', 0];
  9658. if ($param['is_fenpei'] == 1) $condition[] = ['employee_id', '=', 0];
  9659. }
  9660. if (!empty($param['create_time_detail'])) {
  9661. $last_contact = explode(' - ', $param['create_time_detail']);
  9662. $start = str_replace('/', '-', $last_contact[0]);
  9663. $end = str_replace('/', '-', $last_contact[1]);
  9664. $condition[] = ['create_time_detail', '>=', $start . ' 00:00:00'];
  9665. $condition[] = ['create_time_detail', '<=', $end . ' 23:59:59'];
  9666. }
  9667. if (!empty($param['times'])) {
  9668. $last_contact = explode(' - ', $param['times']);
  9669. $start = str_replace('/', '-', $last_contact[0]);
  9670. $end = str_replace('/', '-', $last_contact[1]);
  9671. $condition[] = ['create_time_detail', '>=', $start . ' 00:00:00'];
  9672. $condition[] = ['create_time_detail', '<=', $end . ' 23:59:59'];
  9673. }
  9674. if (!empty($param['clue_state_name'])) $condition[] = ['clue_state', 'in', explode(',', $param['clue_state_name'])];
  9675. if (!empty($param['org_id'])) $condition[] = ['org_id', 'in', explode(',', $param['org_id'])];
  9676. if (!empty($param['follow_state_name'])) $condition[] = ['follow_state_name', 'like', '%' . $param['follow_state_name'] . '%'];
  9677. if (!empty($param['is_repeat'])) {
  9678. $tmp = FishData::where($condition)->field('telephone,count(*) as c')->group('telephone')->having('c>1')->select()->toArray();
  9679. $condition[] = ['telephone', 'in', array_column($tmp, 'telephone')];
  9680. }
  9681. $order = 'id desc';
  9682. if ($param['order_type'] == 1) $order = 'create_time_detail asc';
  9683. if ($param['order_type'] == 2) $order = 'create_time_detail desc';
  9684. $page = intval($param['page']);
  9685. $limit = intval($param['limit']);
  9686. $data = FishData::where($condition)->page($page, $limit)->order($order)->select();
  9687. foreach ($data as $key => $val) {
  9688. $data[$key]['clue_owner_name'] = empty($val['clue_owner_name']) ? ' ' : $val['clue_owner_name']; //Employee::where('id','=',$val['employee_id'])->value('name');
  9689. $data[$key]['telephone'] = substr_replace($val['telephone'], '******', 3, 4);
  9690. $data[$key]['gender'] = ['未知', '男', '女'][$val['gender']];
  9691. $data[$key]['clue_source'] = $clue_source[$val['clue_source']];
  9692. $data[$key]['allocation_status'] = $val['is_allocation'] == 1 ? '已分配' : '未分配';
  9693. $data[$key]['clue_type'] = $clue_type[$val['clue_type']];
  9694. $data[$key]['is_followup'] = 0;
  9695. if ($is_manager == 1) {
  9696. $data[$key]['is_followup'] = 1;
  9697. }
  9698. if ($is_manager == 1 && $emp_id == $data[$key]['employee_id']) {
  9699. $data[$key]['is_followup'] = 3;
  9700. }
  9701. if ($is_manager == 0 && $emp_id == $data[$key]['employee_id']) {
  9702. $data[$key]['is_followup'] = 2;
  9703. }
  9704. $tmp = json_decode($val['remark_dict'], true);
  9705. $data[$key]['remark_dict'] = implode(',', $tmp);
  9706. $data[$key]['org_id'] = Org::where(['id' => $val['org_id']])->value('name');
  9707. }
  9708. $count = FishData::where($condition)->count();
  9709. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  9710. }
  9711. /**
  9712. * 飞鱼线索统计
  9713. */
  9714. public function fish_list_total()
  9715. {
  9716. $request = request();
  9717. $root_id = $request->empcrm->root_id;
  9718. // $emp_id = $request->empcrm->id;
  9719. // $emp_name = $request->empcrm->name;
  9720. $emp_org_id = $request->empcrm->org_id;
  9721. $is_manager = $request->empcrm->is_manager;
  9722. // $path = Org::where([['pid','=',$emp_org_id]])->count();
  9723. $param = $request->only(['advertiser_id', 'times']);
  9724. $total = [];
  9725. if ($is_manager == 1) {
  9726. $orgids = orgSubIds($emp_org_id);
  9727. $state1 = Customer::changeState('待确认', 'chaos');
  9728. $state2 = Customer::changeState('无效', 'chaos');
  9729. $state3 = CustomerVisitLog::changeState('已交定', 'chaos');
  9730. $state4 = CustomerVisitLog::changeState('已签单', 'chaos');
  9731. $state5 = CustomerVisitLog::changeState('确认到店', 'chaos');
  9732. $state6 = CustomerVisitLog::changeState('已到场', 'chaos');
  9733. $state7 = CustomerVisitLog::changeState('已量房', 'chaos');
  9734. $FishCampaign[] = ['type', 'in', [1, 4]];
  9735. $FishCampaign[] = ['root_id', '=', $root_id];
  9736. $FishData[] = ['type', 'in', [1, 4]];
  9737. $FishData[] = ['root_id', '=', $root_id];
  9738. $FishData[] = ['org_id', 'in', $orgids];
  9739. $sourceids = CustomerSource::where([['root_id', '=', $root_id], ['source', '=', '飞鱼线索']])->value('id');
  9740. $wherev[] = ['CustomerVisitLog.customer_org_id', 'in', $orgids];
  9741. $wherev[] = ['CustomerVisitLog.state', 'in', array_merge($state1, $state2, $state3, $state4, $state5, $state6, $state7)];
  9742. if (!empty($param['advertiser_id'])) {
  9743. $FishCampaign[] = ['advertiser_id', '=', $param['advertiser_id']];
  9744. if ($param['advertiser_id'] == 1) {
  9745. $FishData[] = ['advertiser_id', '=', 0];
  9746. } else {
  9747. $FishData[] = ['advertiser_id', '=', $param['advertiser_id']];
  9748. }
  9749. }
  9750. if (!empty($param['times'])) {
  9751. $last_contact = explode(' - ', $param['times']);
  9752. $start = str_replace('/', '-', $last_contact[0]);
  9753. $end = str_replace('/', '-', $last_contact[1]);
  9754. // $FishCampaign[] = ['data_time', '>=', $start . ' 00:00:00'];
  9755. $FishCampaign[] = ['data_time', 'between', [$start, $end]];
  9756. $wherev[] = ['CustomerVisitLog.confirm_date', 'between', [$start, $end]];
  9757. $FishData[] = ['create_time', 'between', [$start, $end]];
  9758. }
  9759. $fish_customer_id = FishData::where($FishData)->where(['is_allocation' => 1])->column('customer_id');
  9760. $wherev[] = ['CustomerVisitLog.customer_id', 'in', $fish_customer_id];
  9761. $total['totalNum2'] = FishData::where($FishData)->count(); //线索数
  9762. $w = Customer::where('source_id', '=', $sourceids);
  9763. $visitlog_list = CustomerVisitLog::hasWhere('customer', $w)->where($wherev)
  9764. ->group('state,customer_id')
  9765. ->column('CustomerVisitLog.customer_id,CustomerVisitLog.state,customer.deposit_money,customer.signed_money');
  9766. $total['totalNum0'] = FishCampaign::where($FishCampaign)->sum('cost'); //总消耗
  9767. $total['totalNum3'] = $total['totalNum0'] > 0 && $total['totalNum2'] > 0 ? round($total['totalNum0'] / $total['totalNum2'], 2) : 0;
  9768. $total['totalNum4'] = count($fish_customer_id);
  9769. $total['totalNum5'] = $total['totalNum0'] > 0 && $total['totalNum4'] > 0 ? round($total['totalNum0'] / $total['totalNum4'], 2) : 0;
  9770. $total['totalNum6'] = $total['totalNum4'] > 0 && $total['totalNum2'] > 0 ? round($total['totalNum4'] / $total['totalNum2'], 2) * 100 . '%' : '0.00%';
  9771. $sign_data = $reach_data = $shop_id = [];
  9772. $reach = $sign = $shop = $house = $all_money = $basic_money = 0;
  9773. foreach ($visitlog_list as $v) {
  9774. //处理签单
  9775. if (in_array($v['state'], $state4, true)) {
  9776. $sign++;
  9777. $basic_money += $v['signed_money'];
  9778. $all_money += $v['signed_money'];
  9779. $sign_data[] = $v['customer_id'];
  9780. }
  9781. //处理交定
  9782. if (in_array($v['state'], $state3, true)) {
  9783. $reach++;
  9784. $reach_data[] = $v;
  9785. }
  9786. if (in_array($v['state'], $state5, true)) {
  9787. $shop++;
  9788. $shop_id[] = $v['customer_id'];
  9789. }
  9790. if (in_array($v['state'], $state7, true)) $house++;
  9791. }
  9792. foreach ($reach_data as $v) {
  9793. if (!in_array($v['customer_id'], $sign_data)) {
  9794. $all_money += $v['deposit_money'];
  9795. }
  9796. }
  9797. $total['totalNum1'] = $all_money;
  9798. $total['totalNum7'] = $house;
  9799. $total['totalNum8'] = $house > 0 && $total['totalNum4'] > 0 ? round($house / $total['totalNum4'], 2) * 100 . '%' : '0.00%';
  9800. $total['totalNum9'] = $shop;
  9801. $total['totalNum10'] = $shop > 0 && $total['totalNum4'] > 0 ? round($shop / $total['totalNum4'], 2) * 100 . '%' : '0.00%';
  9802. $total['totalNum11'] = $reach;
  9803. $total['totalNum12'] = $reach > 0 && $total['totalNum4'] > 0 ? round($reach / $total['totalNum4'], 2) * 100 . '%' : '0.00%';
  9804. $total['totalNum13'] = $sign;
  9805. $total['totalNum14'] = $basic_money > 0 && $total['totalNum13'] > 0 ? round($basic_money / $total['totalNum13'], 2) : '0.00';
  9806. $total['totalNum15'] = $sign > 0 && $total['totalNum4'] > 0 ? round($sign / $total['totalNum4'], 2) * 100 . '%' : '0.00%';
  9807. $total['shop_id'] = $shop_id;
  9808. }
  9809. return json(['code' => 0, 'data' => $total]);
  9810. }
  9811. /**
  9812. * 腾讯数据统计
  9813. */
  9814. public function tencent_list_total()
  9815. {
  9816. $request = request();
  9817. $root_id = $request->empcrm->root_id;
  9818. $emp_org_id = $request->empcrm->org_id;
  9819. $is_manager = $request->empcrm->is_manager;
  9820. $param = $request->only(['advertiser_id', 'times']);
  9821. $total = [];
  9822. if ($is_manager == 1) {
  9823. $orgids = orgSubIds($emp_org_id);
  9824. $state1 = Customer::changeState('待确认', 'chaos');
  9825. $state2 = Customer::changeState('无效', 'chaos');
  9826. $state3 = CustomerVisitLog::changeState('已交定', 'chaos');
  9827. $state4 = CustomerVisitLog::changeState('已签单', 'chaos');
  9828. $state5 = CustomerVisitLog::changeState('确认到店', 'chaos');
  9829. $state6 = CustomerVisitLog::changeState('已到场', 'chaos');
  9830. $state7 = CustomerVisitLog::changeState('已量房', 'chaos');
  9831. $FishCampaign[] = ['type', '=', 2];
  9832. $FishCampaign[] = ['root_id', '=', $root_id];
  9833. $FishData[] = ['type', '=', 2];
  9834. $FishData[] = ['root_id', '=', $root_id];
  9835. $FishData[] = ['org_id', 'in', $orgids];
  9836. $sourceids = CustomerSource::where([['root_id', '=', $root_id], ['source', '=', '腾讯线索']])->value('id');
  9837. $wherev[] = ['CustomerVisitLog.customer_org_id', 'in', $orgids];
  9838. $wherev[] = ['CustomerVisitLog.state', 'in', array_merge($state1, $state2, $state3, $state4, $state5, $state6, $state7)];
  9839. if (!empty($param['advertiser_id'])) {
  9840. $FishCampaign[] = ['advertiser_id', '=', $param['advertiser_id']];
  9841. $FishData[] = ['advertiser_id', '=', $param['advertiser_id']];
  9842. }
  9843. if (!empty($param['times'])) {
  9844. $last_contact = explode(' - ', $param['times']);
  9845. $start = str_replace('/', '-', $last_contact[0]);
  9846. $end = str_replace('/', '-', $last_contact[1]);
  9847. $FishCampaign[] = ['data_time', 'between', [$start, $end]];
  9848. $wherev[] = ['CustomerVisitLog.confirm_date', 'between', [$start, $end]];
  9849. $FishData[] = ['create_time', 'between', [$start, $end]];
  9850. }
  9851. $fish_customer_id = FishData::where($FishData)->where(['is_allocation' => 1])->column('customer_id');
  9852. if (!empty($fish_customer_id)) {
  9853. $wherev[] = ['CustomerVisitLog.customer_id', 'in', $fish_customer_id];
  9854. }
  9855. $total['totalNum2'] = FishData::where($FishData)->count(); //线索数
  9856. $w = Customer::where('source_id', '=', $sourceids);
  9857. $visitlog_list = CustomerVisitLog::hasWhere('customer', $w)->where($wherev)
  9858. ->group('state,customer_id')
  9859. ->column('CustomerVisitLog.customer_id,CustomerVisitLog.state,customer.deposit_money,customer.signed_money');
  9860. $total['totalNum0'] = FishCampaign::where($FishCampaign)->sum('cost'); //总消耗
  9861. $total['totalNum3'] = $total['totalNum0'] > 0 && $total['totalNum2'] > 0 ? round($total['totalNum0'] / $total['totalNum2'], 2) : 0;
  9862. $total['totalNum4'] = count($fish_customer_id);
  9863. $total['totalNum5'] = $total['totalNum0'] > 0 && $total['totalNum4'] > 0 ? round($total['totalNum0'] / $total['totalNum4'], 2) : 0;
  9864. $total['totalNum6'] = $total['totalNum4'] > 0 && $total['totalNum2'] > 0 ? round($total['totalNum4'] / $total['totalNum2'], 2) * 100 . '%' : '0.00%';
  9865. $sign_data = $reach_data = [];
  9866. $reach = $sign = $shop = $house = $all_money = $basic_money = 0;
  9867. foreach ($visitlog_list as $v) {
  9868. //处理签单
  9869. if (in_array($v['state'], $state4, true)) {
  9870. $sign++;
  9871. $basic_money += $v['signed_money'];
  9872. $all_money += $v['signed_money'];
  9873. $sign_data[] = $v['customer_id'];
  9874. }
  9875. //处理交定
  9876. if (in_array($v['state'], $state3, true)) {
  9877. $reach++;
  9878. $reach_data[] = $v;
  9879. }
  9880. if (in_array($v['state'], $state5, true)) $shop++;
  9881. if (in_array($v['state'], $state7, true)) $house++;
  9882. }
  9883. foreach ($reach_data as $v) {
  9884. if (!in_array($v['customer_id'], $sign_data)) {
  9885. $all_money += $v['deposit_money'];
  9886. }
  9887. }
  9888. $total['totalNum1'] = $all_money;
  9889. $total['totalNum7'] = $house;
  9890. $total['totalNum8'] = $house > 0 && $total['totalNum4'] > 0 ? round($house / $total['totalNum4'], 2) * 100 . '%' : '0.00%';
  9891. $total['totalNum9'] = $shop;
  9892. $total['totalNum10'] = $shop > 0 && $total['totalNum4'] > 0 ? round($shop / $total['totalNum4'], 2) * 100 . '%' : '0.00%';
  9893. $total['totalNum11'] = $reach;
  9894. $total['totalNum12'] = $reach > 0 && $total['totalNum4'] > 0 ? round($reach / $total['totalNum4'], 2) * 100 . '%' : '0.00%';
  9895. $total['totalNum13'] = $sign;
  9896. $total['totalNum14'] = $basic_money > 0 && $total['totalNum13'] > 0 ? round($basic_money / $total['totalNum13'], 2) : '0.00';
  9897. $total['totalNum15'] = $sign > 0 && $total['totalNum4'] > 0 ? round($sign / $total['totalNum4'], 2) * 100 . '%' : '0.00%';
  9898. }
  9899. return json(['code' => 0, 'data' => $total]);
  9900. }
  9901. /**
  9902. * 腾讯线索
  9903. */
  9904. public function tencent_list()
  9905. {
  9906. $request = request();
  9907. $root_id = $request->empcrm->root_id;
  9908. $emp_id = $request->empcrm->id;
  9909. // $emp_name = $request->empcrm->name;
  9910. $emp_org_id = $request->empcrm->org_id;
  9911. $is_manager = $request->empcrm->is_manager;
  9912. $path = Org::where([['pid', '=', $emp_org_id]])->count();
  9913. $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' => '']);
  9914. $clue_source = [
  9915. '0' => '未知',
  9916. '1' => '其他',
  9917. '2' => '外部导入',
  9918. '3' => '广告创意',
  9919. '4' => '蹊径落地页',
  9920. '5' => '一叶智能',
  9921. '6' => '微信原生推广页',
  9922. '7' => '讯达小程序',
  9923. '8' => '互动落地页',
  9924. '9' => '教培通'
  9925. ];
  9926. $clue_state = [
  9927. 'LEADS_FOLLOW_TAG_DEFAULT' => '未分配',
  9928. 'LEADS_FOLLOW_TAG_ASSIGNED' => '已分配待跟进',
  9929. 'LEADS_FOLLOW_TAG_FOLLOWING' => '跟进中',
  9930. 'LEADS_FOLLOW_TAG_VALID' => '已转换-潜在客户',
  9931. 'LEADS_FOLLOW_TAG_VALID_HIGH_INTENTION' => '已转换-高意向客户',
  9932. 'LEADS_FOLLOW_TAG_VALID_CONVERTED' => '已转换-已成单',
  9933. 'LEADS_FOLLOW_TAG_INVALID_REPEAT' => '无效-重复',
  9934. 'LEADS_FOLLOW_TAG_INVALID_NOTSELF' => '无效-非本人',
  9935. 'LEADS_FOLLOW_TAG_INVALID_NOTTOUCH' => '无效-未接通',
  9936. 'LEADS_FOLLOW_TAG_INVALID_NOTINTENTION' => '无效-无意向',
  9937. 'LEADS_FOLLOW_TAG_INVALID_REGION_MISMATCHED' => '无效-定向外',
  9938. 'LEADS_FOLLOW_TAG_INVALID_OTHER' => '无效-其他',
  9939. 'LEADS_INEFFECT_REASON_TEL_NOT_CONNECTED' => '推送',
  9940. 'LEADS_INEFFECT_REASON_NO_INTENTION' => '其他',
  9941. ];
  9942. $app_name = [
  9943. 'LEADS_TYPE_FORM' => '表单预约',
  9944. 'LEADS_TYPE_ONLINE_CONSULT' => '在线咨询',
  9945. 'LEADS_TYPE_MAKE_PHONE_CALL' => '普通电话',
  9946. 'LEADS_TYPE_PHONE' => '智能电话',
  9947. 'LEADS_TYPE_PROMOTION_COUPON' => '发券',
  9948. 'LEADS_TYPE_INTELLIGENT_TOOL' => '智能咨询',
  9949. 'LEADS_TYPE_LOTTERY' => '抽奖',
  9950. 'LEADS_TYPE_LANDING_PAGE_CLICK' => '落地页点击',
  9951. 'LEADS_TYPE_ONE_CLICK_AUTHORIZE' => '一键授权',
  9952. 'LEADS_TYPE_PAGE_SCAN_CODE' => '加企业微信客服',
  9953. 'LEADS_TYPE_PROMOTION_FOLLOW' => '微信关注',
  9954. ];
  9955. // $clue_type = [0 => '表单提交', 1 => '在线咨询', 2 => '智能电话', 3 => '网页回呼', 4 => '卡券', 5 => '抽奖'];
  9956. $table_tags = [
  9957. [
  9958. 'title' => '高级意向',
  9959. 'name' => 'all0',
  9960. 'value' => 0,
  9961. 'tags' =>
  9962. [
  9963. ['title' => '已签约合同', 'name' => 'all0_1'],
  9964. ['title' => '已缴纳定金', 'name' => 'all0_2'],
  9965. ['title' => '客户已到店', 'name' => 'all0_3'],
  9966. ['title' => '预约上门量房', 'name' => 'all0_4'],
  9967. ]
  9968. ],
  9969. [
  9970. 'title' => '中级意向',
  9971. 'name' => 'all1',
  9972. 'value' => 1,
  9973. 'tags' =>
  9974. [
  9975. ['title' => '能提供户型图', 'name' => 'all1_1'],
  9976. ['title' => '有装修预算', 'name' => 'all1_2'],
  9977. ['title' => '添加微信待通过', 'name' => 'all1_3'],
  9978. ['title' => '添加微信已通过', 'name' => 'all1_4'],
  9979. ]
  9980. ],
  9981. [
  9982. 'title' => '初级意向',
  9983. 'name' => 'all2',
  9984. 'value' => 2,
  9985. 'tags' =>
  9986. [
  9987. ['title' => '仅咨询了解', 'name' => 'all2_1'],
  9988. ['title' => '无真实需求', 'name' => 'all2_2'],
  9989. ['title' => '对比其他平台', 'name' => 'all2_3'],
  9990. ['title' => '对价格不反感', 'name' => 'all2_4'],
  9991. ['title' => '近期有装修需求', 'name' => 'all2_5'],
  9992. ['title' => '添加微信', 'name' => 'all2_6'],
  9993. ]
  9994. ],
  9995. [
  9996. 'title' => '意向',
  9997. 'name' => 'all3',
  9998. 'value' => 3,
  9999. 'tags' =>
  10000. [
  10001. ['title' => '低意向', 'name' => 'all3_1'],
  10002. ['title' => '中意向', 'name' => 'all3_2'],
  10003. ['title' => '高意向', 'name' => 'all3_3'],
  10004. ]
  10005. ],
  10006. [
  10007. 'title' => '无效',
  10008. 'name' => 'all4',
  10009. 'value' => 4,
  10010. 'tags' =>
  10011. [
  10012. ['title' => '添加微信未通过', 'name' => 'all4_1'],
  10013. ['title' => '多次联系不上', 'name' => 'all4_2'],
  10014. ['title' => '拒绝加微信', 'name' => 'all4_3'],
  10015. ['title' => '无需求', 'name' => 'all4_4'],
  10016. ['title' => '错号', 'name' => 'all4_5'],
  10017. ['title' => '关机', 'name' => 'all4_6'],
  10018. ['title' => '空号', 'name' => 'all4_7'],
  10019. ['title' => '联系不上', 'name' => 'all4_8'],
  10020. ['title' => '无意愿', 'name' => 'all4_9'],
  10021. ['title' => '非本人', 'name' => 'all4_10']
  10022. ]
  10023. ],
  10024. [
  10025. 'title' => '默认',
  10026. 'name' => 'all5',
  10027. 'value' => 5,
  10028. 'tags' =>
  10029. [
  10030. ['title' => '加微信', 'name' => 'all5_1'],
  10031. ['title' => '待确认', 'name' => 'all5_2'],
  10032. ['title' => '无效', 'name' => 'all5_3'],
  10033. ['title' => '参与赔付', 'name' => 'all5_4'],
  10034. ]
  10035. ],
  10036. ];
  10037. if (!$request->isAjax()) {
  10038. $this->screen();
  10039. $channel_setting = Company::where(['root_id' => $root_id])->value('channel_setting');
  10040. $advertiser_list = [];
  10041. if (isset($channel_setting) && $channel_setting) {
  10042. $tmp_data = json_decode($channel_setting, true);
  10043. $advertiser_id = explode(',', $tmp_data['tx_adv']);
  10044. $fish_account = explode(',', $tmp_data['tx_account']);
  10045. foreach ($advertiser_id as $key => $val) {
  10046. $advertiser_list[] = [
  10047. 'id' => $advertiser_id[$key],
  10048. 'name' => isset($fish_account[$key]) ? $fish_account[$key] : '暂未设置名称',
  10049. ];
  10050. }
  10051. }
  10052. $team_orgs = $is_manager ? orgSubIds($emp_org_id) : [$emp_org_id];
  10053. $emporg_list = Org::where([['id', 'in', $team_orgs]])->column('id,name');
  10054. View::assign('emporg_list', $emporg_list);
  10055. View::assign('advertiser_id', $advertiser_list);
  10056. View::assign('is_manager', $is_manager);
  10057. View::assign('soudata_now', $clue_source);
  10058. View::assign('clue_state', $clue_state);
  10059. View::assign('table_tags', $table_tags);
  10060. // $this->synchronize_tx_data();
  10061. return View::fetch();
  10062. }
  10063. $org_employee = [$emp_id];
  10064. if ($is_manager == 1) {
  10065. $team_orgs = orgSubIds($emp_org_id);
  10066. // $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
  10067. $condition[] = ['org_id', 'in', $team_orgs];
  10068. } else {
  10069. $condition[] = ['employee_id', 'in', $org_employee];
  10070. }
  10071. $condition[] = ['root_id', '=', $root_id];
  10072. $condition[] = ['type', '=', 2];
  10073. if (!empty($param['name'])) $condition[] = ['name', 'like', '%' . $param['name'] . '%'];
  10074. if (!empty($param['clue_id'])) $condition[] = ['clue_id', 'like', '%' . $param['clue_id'] . '%'];
  10075. if (!empty($param['telephone'])) $condition[] = ['telephone', '=', $param['telephone']];
  10076. if (!empty($param['tags'])) {
  10077. $tags = explode(',', $param['tags']);
  10078. $tmp_tags = [];
  10079. foreach ($tags as $val) {
  10080. $tmp_tags[] = "%{$val}%";
  10081. }
  10082. $condition[] = ['tags', 'like', $tmp_tags, 'OR'];
  10083. }
  10084. if (!empty($param['address'])) $condition[] = ['address', 'like', '%' . $param['address'] . '%'];
  10085. if (!empty($param['advertiser_id'])) $condition[] = ['advertiser_id', 'like', '%' . $param['advertiser_id'] . '%'];
  10086. if (!empty($param['ad_name'])) $condition[] = ['ad_name', 'like', '%' . $param['ad_name'] . '%'];
  10087. if (!empty($param['ad_id'])) $condition[] = ['ad_id', 'like', '%' . $param['ad_id'] . '%'];
  10088. if (!empty($param['promotion_id'])) $condition[] = ['promotion_id', 'like', '%' . $param['promotion_id'] . '%'];
  10089. if (!empty($param['promotion_name'])) $condition[] = ['promotion_name', 'like', '%' . $param['promotion_name'] . '%'];
  10090. if (!empty($param['weixin'])) $condition[] = ['weixin', 'like', '%' . $param['weixin'] . '%'];
  10091. if (!empty($param['qq'])) $condition[] = ['qq', 'like', '%' . $param['qq'] . '%'];
  10092. if (!empty($param['email'])) $condition[] = ['email', 'like', '%' . $param['email'] . '%'];
  10093. if (!empty($param['location'])) $condition[] = ['location', 'like', '%' . $param['location'] . '%'];
  10094. if (!empty($param['module_id'])) $condition[] = ['module_id', 'like', '%' . $param['module_id'] . '%'];
  10095. if (!empty($param['module_name'])) $condition[] = ['module_name', 'like', '%' . $param['module_name'] . '%'];
  10096. if (!empty($param['clue_owner_name'])) $condition[] = ['clue_owner_name', 'in', explode(',', $param['clue_owner_name'])];
  10097. if (!empty($param['org_id'])) $condition[] = ['org_id', 'in', explode(',', $param['org_id'])];
  10098. if (!empty($param['clue_source'])) {
  10099. $aa = [];
  10100. foreach (explode(',', $param['clue_source']) as $vv) {
  10101. $aa[] = $vv - 1;
  10102. }
  10103. $condition[] = ['clue_source', 'in', $aa];
  10104. }
  10105. if (!empty($param['is_allocation'])) $condition[] = ['is_allocation', '=', $param['is_allocation'] - 1];
  10106. if (!empty($param['is_contact'])) $condition[] = ['is_contact', '=', $param['is_contact'] - 1];
  10107. if (!empty($param['is_fenpei'])) {
  10108. if ($param['is_fenpei'] == 2) $condition[] = ['employee_id', '>', 0];
  10109. if ($param['is_fenpei'] == 1) $condition[] = ['employee_id', '=', 0];
  10110. }
  10111. if (!empty($param['create_time_detail'])) {
  10112. $last_contact = explode(' - ', $param['create_time_detail']);
  10113. $start = str_replace('/', '-', $last_contact[0]);
  10114. $end = str_replace('/', '-', $last_contact[1]);
  10115. $condition[] = ['create_time_detail', '>=', $start . ' 00:00:00'];
  10116. $condition[] = ['create_time_detail', '<=', $end . ' 23:59:59'];
  10117. }
  10118. if (!empty($param['times'])) {
  10119. $last_contact = explode(' - ', $param['times']);
  10120. $start = str_replace('/', '-', $last_contact[0]);
  10121. $end = str_replace('/', '-', $last_contact[1]);
  10122. $condition[] = ['create_time_detail', '>=', $start . ' 00:00:00'];
  10123. $condition[] = ['create_time_detail', '<=', $end . ' 23:59:59'];
  10124. }
  10125. if (!empty($param['clue_state_name'])) $condition[] = ['clue_state', 'in', explode(',', $param['clue_state_name'])];
  10126. if (!empty($param['follow_state_name'])) $condition[] = ['follow_state_name', 'like', '%' . $param['follow_state_name'] . '%'];
  10127. if (!empty($param['is_repeat'])) {
  10128. $tmp = FishData::where($condition)->field('telephone,count(*) as c')->group('telephone')->having('c>1')->select()->toArray();
  10129. $condition[] = ['telephone', 'in', array_column($tmp, 'telephone')];
  10130. }
  10131. $order = 'id desc';
  10132. if ($param['order_type'] == 1) $order = 'create_time_detail asc';
  10133. if ($param['order_type'] == 2) $order = 'create_time_detail desc';
  10134. $page = intval($param['page']);
  10135. $limit = intval($param['limit']);
  10136. $data = FishData::where($condition)->page($page, $limit)->order($order)->select();
  10137. foreach ($data as $key => $val) {
  10138. $data[$key]['telephone'] = substr_replace($val['telephone'], '******', 3, 6);
  10139. $data[$key]['gender'] = ['未知', '男', '女'][$val['gender']];
  10140. $data[$key]['clue_source'] = isset($clue_source[$val['clue_source']]) ? $clue_source[$val['clue_source']] : '';
  10141. $data[$key]['allocation_status'] = $val['is_allocation'] == 1 ? '已分配' : '未分配';
  10142. $data[$key]['clue_state_name'] = isset($clue_state[$val['clue_state_name']]) ? $clue_state[$val['clue_state_name']] : '';
  10143. $data[$key]['app_name'] = isset($app_name[$val['app_name']]) ? $app_name[$val['app_name']] : $app_name[$val['app_name']];
  10144. $data[$key]['clue_type'] = '未知';
  10145. $data[$key]['is_followup'] = 0;
  10146. if ($is_manager == 1) {
  10147. $data[$key]['is_followup'] = 1;
  10148. }
  10149. if ($is_manager == 1 && $emp_id == $data[$key]['employee_id']) {
  10150. $data[$key]['is_followup'] = 3;
  10151. }
  10152. if ($is_manager == 0 && $emp_id == $data[$key]['employee_id']) {
  10153. $data[$key]['is_followup'] = 2;
  10154. }
  10155. $data[$key]['org_id'] = Org::where(['id' => $val['org_id']])->value('name');
  10156. }
  10157. $count = FishData::where($condition)->count();
  10158. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  10159. }
  10160. /**
  10161. * 飞鱼线索转化为有效客户
  10162. */
  10163. public function fish_validation()
  10164. {
  10165. $request = request();
  10166. $root_id = $request->empcrm->root_id;
  10167. $empid = $request->empcrm->id;
  10168. $orgids = orgSubIds($root_id);
  10169. $param = $this->request->only(['customer_id']);
  10170. $customer_info = FishData::where(['id' => $param['customer_id']])->find();
  10171. if (empty($customer_info)) return json(['code' => 1, 'msg' => '线索信息有误']);
  10172. if ($customer_info['is_allocation'] == 1) return json(['code' => 1, 'msg' => '线索已转化,无法重复转化']);
  10173. $customerExist = Customer::where([
  10174. ['phone|phone1|phone2', '=', cypherphone($customer_info['telephone'])],
  10175. ['org_id', 'in', $orgids],
  10176. ['employee_id', '>', 0]
  10177. ])->where(
  10178. function ($query) {
  10179. $not_sure = Customer::changeState('待确认', 'chaos');
  10180. $or1[] = ['crm_res_id', 'null', null];
  10181. $or2[] = ['crm_res_id', '>', 0];
  10182. $or2[] = ['state', 'not in', $not_sure];
  10183. $query->whereOr([$or1, $or2]);
  10184. }
  10185. )->field('id,employee_id,is_resource,org_id,source_id')->select()->toArray();
  10186. if (!empty($customerExist)) {
  10187. foreach ($customerExist as $ex) {
  10188. if ($ex['employee_id'] != $empid) {
  10189. //查询撞单客户渠道
  10190. $source_name = '';
  10191. if ($ex['source_id']) {
  10192. $source = CustomerSource::where(['id' => $ex['source_id']])->value('source');
  10193. $source_name = $source . '渠道客户';
  10194. }
  10195. $have_emp = Employee::where('id', '=', $ex['employee_id'])->value('name');
  10196. return json(['code' => 1, 'msg' => '该客户与员工' . $have_emp . $source_name . '撞单,无法转化']);
  10197. }
  10198. }
  10199. }
  10200. //检查线索来源
  10201. if ($customer_info['type'] == 1 || $customer_info['type'] == 4) {
  10202. $fish_source = CustomerSource::where(['root_id' => $root_id, 'source' => '飞鱼线索'])->find();
  10203. } else {
  10204. $fish_source = CustomerSource::where(['root_id' => $root_id, 'source' => '腾讯线索'])->find();
  10205. }
  10206. //检查是否被分配
  10207. if (empty($customer_info['employee_id']) || !isset($customer_info['employee_id'])) return json(['code' => 1, 'msg' => '请先分配线索']);
  10208. $employee_info = Employee::where(['id' => $customer_info['employee_id'], 'root_id' => $root_id])->field('id,name,org_id')->find();
  10209. if (!$employee_info) return json(['code' => 1, 'msg' => '员工信息有误']);
  10210. // $aec = new Aec(config('app.aec_key'), config('app.aec_iv'));
  10211. // $telephone = $aec->decrypt('RW0xbkt0WSsvcXJBWHdSSGdTdnhRdz09');
  10212. // dump($telephone);die();//15838056141-宁/15565412648-豆
  10213. $save['employee_id'] = $customer_info['employee_id'];
  10214. $save['phone'] = $customer_info['telephone'];
  10215. $save['name'] = $customer_info['name'];
  10216. $save['is_distribution'] = 0;
  10217. $save['is_resource'] = 0;
  10218. $save['state'] = 1;
  10219. $save['community_name'] = $customer_info['location'];
  10220. $save['addtime'] = $customer_info['create_time_detail'];
  10221. $save['updatetime'] = $customer_info['create_time_detail'];
  10222. $save['org_id'] = $employee_info['org_id'];
  10223. $save['sex'] = boolval($employee_info['gender']);
  10224. $save['source_id'] = $fish_source['id'];
  10225. $save['employee_time'] = date('Y-m-d H:i:s');
  10226. $save['valid_time'] = date('Y-m-d H:i:s');
  10227. $visitField_decode = [];
  10228. $visit_default = CustomerPortraitField::where([['root_id', '=', $root_id], ['keyname', 'in', ['source_id', 'sign_time']]])->column('id', 'keyname');
  10229. if (isset($visit_default['sign_time']) && $visit_default['sign_time']) {
  10230. $visitField_decode[] = ['id' => $visit_default['sign_time'], 'keyname' => 'sign_time', 'value' => $customer_info['create_time_detail']];
  10231. }
  10232. if (isset($visit_default['source_id']) && $visit_default['source_id']) {
  10233. $visitField_decode[] = ['id' => $visit_default['source_id'], 'keyname' => 'source_id', 'value' => $fish_source['id']];
  10234. }
  10235. $save['ext'] = !empty($visitField_decode) ? json_encode($visitField_decode) : NULL;
  10236. $ms = Customer::create($save);
  10237. if ($ms) {
  10238. FishData::where(['id' => $param['customer_id']])->update(['is_allocation' => 1, 'customer_id' => $ms->id, 'is_contact' => 1]);
  10239. //更新通话记录
  10240. $log_list = OutCallLog::where(['call_id' => $param['customer_id']])->select()->toArray();
  10241. OutCallLog::where(['call_id' => $param['customer_id']])->update(['customer_id' => $ms->id]);
  10242. foreach ($log_list as $val) {
  10243. CustomerVisitLog::where(['data_id' => $val['id'], 'data_type' => 'out_call'])->update(['customer_id' => $ms->id]);
  10244. }
  10245. if (!isset($log_list) || empty($log_list)) {
  10246. CustomerVisitLog::Create([
  10247. 'customer_id' => $ms->id,
  10248. 'type' => '',
  10249. 'employee_id' => $empid,
  10250. 'user_id' => $request->empcrm->uid,
  10251. 'org_id' => $request->empcrm->org_id,
  10252. 'customer_employee_id' => 0,
  10253. 'customer_org_id' => 0,
  10254. 'data_type' => '',
  10255. 'data_id' => '',
  10256. 'remark' => '标记有效'
  10257. ]);
  10258. }
  10259. return json(['code' => 0, 'msg' => '确认完成', 'data' => $log_list]);
  10260. } else {
  10261. return json(['code' => 1, 'msg' => '确认失败']);
  10262. }
  10263. }
  10264. /**
  10265. * 更新数据
  10266. */
  10267. public function fish_update_data()
  10268. {
  10269. $request = request();
  10270. $root_id = $request->empcrm->root_id;
  10271. $param = $this->request->only(['id', 'address', 'age', 'date', 'email', 'gender', 'location', 'name', 'qq', 'remark', 'weixin', 'employee_remark', 'tags']);
  10272. $data = FishData::find($param['id']);
  10273. if (empty($data)) return json(['code' => 1, 'msg' => '数据信息有误']);
  10274. $param['gender'] = $param['gender'] == '男' ? 1 : ($param['gender'] == '女' ? 2 : 0);
  10275. $data->save($param);
  10276. return json(['code' => 0, 'msg' => '确认完成']);
  10277. }
  10278. /**
  10279. * 同步数据
  10280. */
  10281. public function synchronize_fish_data()
  10282. {
  10283. $request = request();
  10284. $root_id = $request->empcrm->root_id;
  10285. $channel_setting = Company::where(['root_id' => $root_id])->value('channel_setting');
  10286. if ($channel_setting) {
  10287. $tmp_data = json_decode($channel_setting, true);
  10288. if ($tmp_data['fish_adv']) {
  10289. $appid = config('app.fish_clue_appid');
  10290. $secret = config('app.fish_clue_secret');
  10291. $fish_update_time = $tmp_data['fish_update_time'] ?: date('Y-m-d H:i:s', time() - 86400 * 180);
  10292. $end_time = date('Y-m-d H:i:s');
  10293. $page_siae = 100;
  10294. $fish = new Fish($appid, $secret);
  10295. $fish_refresh_time = $tmp_data['fish_refresh_time']; //上次token更新时间
  10296. if ($fish_refresh_time + 80000 < time()) {
  10297. $token = $fish->getRefreshToken($tmp_data['fish_refresh_token']);
  10298. if ($token['code'] == 0) {
  10299. $tmp_data['fish_access_token'] = $token['data']['access_token'];
  10300. $tmp_data['fish_refresh_token'] = $token['data']['refresh_token'];
  10301. $tmp_data['fish_refresh_time'] = time();
  10302. Company::where(['root_id' => $root_id])->update(['channel_setting' => json_encode($tmp_data)]);
  10303. } else {
  10304. $tmp_data['fish_access_token'] = '';
  10305. $tmp_data['fish_refresh_token'] = '';
  10306. Company::where(['root_id' => $root_id])->update(['channel_setting' => json_encode($tmp_data)]);
  10307. }
  10308. }
  10309. $advertiser_id = explode(',', $tmp_data['fish_adv']);
  10310. foreach ($advertiser_id as $adv) {
  10311. $data = $fish->getClueListData($tmp_data['fish_access_token'], $adv, $fish_update_time, $end_time, 1, $page_siae);
  10312. if (isset($data) && $data['code'] == 0) {
  10313. $total_number = $data['data']['page_info']['total_number'];
  10314. $page = intval(ceil($total_number / $page_siae));
  10315. $list = $data['data']['list'];
  10316. // Db::startTrans();
  10317. // try {
  10318. $this->processing_fish_data($list, $root_id, 1);
  10319. for ($i = 1; $i < $page; $i++) {
  10320. $tmp = $fish->getClueListData($tmp_data['fish_access_token'], $adv, $fish_update_time, $end_time, $i + 1, $page_siae);
  10321. $this->processing_fish_data($tmp['data']['list'], $root_id, 1);
  10322. }
  10323. $tmp_data['fish_update_time'] = $end_time;
  10324. Company::where(['root_id' => $root_id])->update(['channel_setting' => json_encode($tmp_data)]);
  10325. // Db::commit();
  10326. // } catch (\Exception $e) {
  10327. // trace($e->getMessage(), 'error');
  10328. // Db::rollback();
  10329. // }
  10330. }
  10331. }
  10332. }
  10333. }
  10334. }
  10335. /**
  10336. * todo 同步直播飞鱼数据
  10337. */
  10338. public function synchronize_fish_live_data()
  10339. {
  10340. $request = request();
  10341. $root_id = $request->empcrm->root_id;
  10342. $channel_setting = Company::where(['root_id' => $root_id])->value('channel_setting_wh');
  10343. if ($channel_setting) {
  10344. $tmp_data = json_decode($channel_setting, true);
  10345. if ($tmp_data['fish_adv']) {
  10346. $appid = config('app.fish_clue_appid_wh');
  10347. $secret = config('app.fish_clue_secret_wh');
  10348. $fish_update_time = $tmp_data['fish_update_time'] ?: date('Y-m-d H:i:s', time() - 86400 * 180);
  10349. $end_time = date('Y-m-d H:i:s');
  10350. $page_siae = 100;
  10351. $fish = new Fish($appid, $secret);
  10352. $fish_refresh_time = $tmp_data['fish_refresh_time']; //上次token更新时间
  10353. if ($fish_refresh_time + 80000 < time()) {
  10354. $token = $fish->getRefreshToken($tmp_data['fish_refresh_token']);
  10355. if ($token['code'] == 0) {
  10356. $tmp_data['fish_access_token'] = $token['data']['access_token'];
  10357. $tmp_data['fish_refresh_token'] = $token['data']['refresh_token'];
  10358. $tmp_data['fish_refresh_time'] = time();
  10359. Company::where(['root_id' => $root_id])->update(['channel_setting_wh' => json_encode($tmp_data)]);
  10360. } else {
  10361. $tmp_data['fish_access_token'] = '';
  10362. $tmp_data['fish_refresh_token'] = '';
  10363. Company::where(['root_id' => $root_id])->update(['channel_setting_wh' => json_encode($tmp_data)]);
  10364. }
  10365. }
  10366. $advertiser_id = explode(',', $tmp_data['fish_adv']);
  10367. foreach ($advertiser_id as $adv) {
  10368. $data = $fish->getClueListData($tmp_data['fish_access_token'], $adv, $fish_update_time, $end_time, 1, $page_siae);
  10369. if (isset($data) && $data['code'] == 0) {
  10370. $total_number = $data['data']['page_info']['total_number'];
  10371. $page = intval(ceil($total_number / $page_siae));
  10372. $list = $data['data']['list'];
  10373. // Db::startTrans();
  10374. // try {
  10375. $this->processing_fish_data($list, $root_id, 4);
  10376. for ($i = 1; $i < $page; $i++) {
  10377. $tmp = $fish->getClueListData($tmp_data['fish_access_token'], $adv, $fish_update_time, $end_time, $i + 1, $page_siae);
  10378. $this->processing_fish_data($tmp['data']['list'], $root_id, 4);
  10379. }
  10380. $tmp_data['fish_update_time'] = $end_time;
  10381. Company::where(['root_id' => $root_id])->update(['channel_setting_wh' => json_encode($tmp_data)]);
  10382. // Db::commit();
  10383. // } catch (\Exception $e) {
  10384. // trace($e->getMessage(), 'error');
  10385. // Db::rollback();
  10386. // }
  10387. }
  10388. }
  10389. }
  10390. }
  10391. }
  10392. /**
  10393. * 腾讯同步数据
  10394. */
  10395. public function synchronize_tx_data()
  10396. {
  10397. $request = request();
  10398. $root_id = $request->empcrm->root_id;
  10399. $channel_setting = Company::where(['root_id' => $root_id])->value('channel_setting');
  10400. if ($channel_setting) {
  10401. $tmp_data = json_decode($channel_setting, true);
  10402. if ($tmp_data['tx_adv']) {
  10403. $fish_update_time = $tmp_data['tx_update_time'] ?: time() - 86400; //默认往前一天
  10404. $end_time = time();
  10405. $page_siae = 200;
  10406. $fish = new Tencent(config('app.tx_clue_appid'), config('app.tx_clue_secret'));
  10407. $advertiser_id = explode(',', $tmp_data['tx_adv']);
  10408. foreach ($advertiser_id as $adv) {
  10409. $data = $fish->getClueListData($tmp_data['tx_access_token'], $adv, $fish_update_time, $end_time, 1, $page_siae);
  10410. if (isset($data) && $data['code'] == 0) {
  10411. $total_number = $data['data']['page_info']['total_number'];
  10412. $page = intval(ceil($total_number / $page_siae));
  10413. $list = $data['data']['leads_info'];
  10414. // Db::startTrans();
  10415. // try {
  10416. $this->processing_tx_data($list, $root_id);
  10417. for ($i = 1; $i < $page; $i++) {
  10418. $tmp = $fish->getClueListData($tmp_data['tx_access_token'], $adv, $fish_update_time, $end_time, $i + 1, $page_siae);
  10419. $this->processing_tx_data($tmp['data']['leads_info'], $root_id);
  10420. }
  10421. $tmp_data['tx_update_time'] = $end_time;
  10422. Company::where(['root_id' => $root_id])->update(['channel_setting' => json_encode($tmp_data)]);
  10423. // Db::commit();
  10424. // } catch (\Exception $e) {
  10425. // trace($e->getMessage(), 'error');
  10426. // Db::rollback();
  10427. // }
  10428. }
  10429. }
  10430. }
  10431. }
  10432. }
  10433. //保存飞鱼线索
  10434. public function processing_fish_data($list, $root_id, $type)
  10435. {
  10436. $customer = [];
  10437. foreach ($list as $val) {
  10438. //查找所属员工-只有员工名称对应上才允许保持到本地库
  10439. $val['extra_info'] = implode(',', $val['extra_info']);
  10440. $val['remark_dict'] = json_encode($val['remark_dict']);
  10441. $val['store'] = json_encode($val['store']);
  10442. $val['tags'] = implode(';', $val['tags']);
  10443. $val['root_id'] = $root_id;
  10444. $val['type'] = $type;
  10445. $info = Employee::where(['name' => $val['clue_owner_name'], 'root_id' => $root_id])->field('id,org_id')->find();
  10446. //检查线索是否存在
  10447. $fid = FishData::where(['clue_id' => $val['clue_id'], 'type' => $type])->find();
  10448. if (!isset($fid) && isset($info) && $info) {
  10449. $val['employee_id'] = $info['id'];
  10450. $val['org_id'] = $info['org_id'];
  10451. $customer[] = $val;
  10452. }
  10453. //检查线索是否被分配
  10454. if (isset($fid) && empty($fid['clue_owner_name'])) {
  10455. if (isset($info) && $info) {
  10456. $val['employee_id'] = $info['id'];
  10457. $val['org_id'] = $info['org_id'];
  10458. FishData::where(['clue_id' => $val['clue_id'], 'type' => $type])->update($val);
  10459. }
  10460. }
  10461. }
  10462. FishData::insertAll($customer);
  10463. }
  10464. //保存腾讯线索
  10465. public function processing_tx_data($list, $root_id)
  10466. {
  10467. $leads_source = [
  10468. 'OUTER_TRAFFIC' => 1,
  10469. 'OFFLINE' => 2,
  10470. 'AD_PREVIEW' => 3,
  10471. 'XJ' => 4,
  10472. 'ONE_LEAF' => 5,
  10473. 'WECHAT_CANVAS' => 6,
  10474. 'XUNDA' => 7,
  10475. 'INTERACTIVEPAGE' => 8,
  10476. 'JIAOPEITONG' => 9,
  10477. ];
  10478. $leads_gender = [
  10479. 'GENDER_TYPE_FEMALE' => 2,
  10480. 'GENDER_TYPE_MALE' => 1,
  10481. 'GENDER_TYPE_UNKNOWN' => 0,
  10482. ];
  10483. foreach ($list as $val) {
  10484. $data = [];
  10485. //检查线索是否存在
  10486. $fid = FishData::where(['clue_id' => $val['leads_id'], 'type' => 2])->value('id');
  10487. $info = Employee::where(['name' => $val['owner_name'], 'root_id' => $root_id])->field('id,org_id')->find();
  10488. if (!isset($fid) && isset($info) && $info) {
  10489. //查找所属员工-只有员工名称对应上才允许保持到本地库
  10490. $data['clue_id'] = $val['leads_id'];
  10491. $data['advertiser_id'] = $val['account_id'];
  10492. $data['advertiser_name'] = $val['agency_name'];
  10493. $data['ad_id'] = $val['ad_id'];
  10494. $data['ad_name'] = $val['ad_name'];
  10495. $data['creative_id'] = $val['adcreative_id'];
  10496. $data['promotion_id'] = $val['adgroup_id'];
  10497. $data['mid_info'] = $val['adgroup_name'];
  10498. $data['site_id'] = 0; //空
  10499. $data['site_name'] = ''; //空
  10500. $data['intention_estimation'] = ''; //空
  10501. $data['external_url'] = $val['page_url'];
  10502. $data['clue_type'] = 0; //空
  10503. $data['module_name'] = $val['component_name'];
  10504. $data['module_id'] = $val['component_id'];
  10505. $data['create_time'] = date('Y-m-d', strtotime($val['leads_create_time']));
  10506. $data['create_time_detail'] = $val['leads_create_time'];
  10507. // $data['date'] = ''; //空
  10508. $data['clue_source'] = isset($leads_source[$val['leads_source']]) ? $leads_source[$val['leads_source']] : 0;
  10509. $data['name'] = (isset($val['leads_name']) && $val['leads_name']) ? $val['leads_name'] : '未命名';
  10510. $data['telephone'] = $val['leads_telephone'];
  10511. $data['gender'] = isset($leads_gender[$val['leads_gender']]) ? $leads_gender[$val['leads_gender']] : 0;
  10512. $data['age'] = $val['age'];
  10513. $data['email'] = $val['leads_email'];
  10514. $data['weixin'] = $val['leads_wechat'];
  10515. $data['qq'] = $val['leads_qq'];
  10516. $data['province_name'] = ''; //空
  10517. $data['city_name'] = $val['leads_area']; //空
  10518. $data['address'] = $val['address'];
  10519. $data['location'] = $val['address'];
  10520. $data['remark'] = ''; //空
  10521. $data['remark_dict'] = ''; //空
  10522. $data['form_remark'] = $val['bundle'];
  10523. $data['app_name'] = $val['leads_type'];
  10524. $data['convert_status'] = ''; //转化状态-空
  10525. $data['req_id'] = '';
  10526. $data['clue_state'] = 0; //线索状态值-空
  10527. $data['clue_state_name'] = $val['leads_follow_tag'];
  10528. $data['clue_owner_name'] = $val['owner_name'];
  10529. $data['follow_state_name'] = ''; //空
  10530. $data['country_name'] = $val['leads_area'];
  10531. $data['system_tags'] = ''; //空
  10532. $data['tags'] = $val['leads_tags'];
  10533. $data['allocation_status'] = 0; //空
  10534. $data['store'] = json(['all_follow_records' => $val['all_follow_records']]);
  10535. $data['extra_info'] = ''; //空
  10536. $data['employee_id'] = $info['id'];
  10537. $data['org_id'] = $info['org_id'];
  10538. $data['root_id'] = $root_id;
  10539. $data['type'] = 2;
  10540. FishData::insert($data);
  10541. }
  10542. }
  10543. }
  10544. /**
  10545. * 发起语音双呼
  10546. */
  10547. public function create_bridge_call_customer()
  10548. {
  10549. $request = request();
  10550. $root_id = $request->empcrm->root_id;
  10551. $employee_id = $request->empcrm->id;
  10552. $param = $this->request->only(['customer_id']);
  10553. $fish_data = FishData::where(['customer_id' => $param['customer_id']])->find();
  10554. if (empty($fish_data)) return json(['code' => 1, 'msg' => '线索信息有误']);
  10555. if ($fish_data['type'] == 1) {
  10556. $fish = new Fish(config('app.fish_clue_appid'), config('app.fish_clue_secret'));
  10557. $channel_setting = Company::where(['root_id' => $root_id])->value('channel_setting');
  10558. } elseif ($fish_data['type'] == 4) {
  10559. $fish = new Fish(config('app.fish_clue_appid_wh'), config('app.fish_clue_secret_wh'));
  10560. $channel_setting = Company::where(['root_id' => $root_id])->value('channel_setting_wh');
  10561. } else {
  10562. $fish = new Fish(config('app.fish_clue_appid'), config('app.fish_clue_secret'));
  10563. $channel_setting = Company::where(['root_id' => $root_id])->value('channel_setting');
  10564. }
  10565. $tmp_data = json_decode($channel_setting, true);
  10566. $employeePhone = $request->empcrm->phone;
  10567. $customerPhone = $fish_data['telephone'];
  10568. $tmp_info = [
  10569. //广告主id
  10570. 'advertiser_id' => intval($fish_data['advertiser_id']),
  10571. //线索id
  10572. 'clue_id' => intval($fish_data['clue_id']),
  10573. // 主叫号码,必须为11位手机号码,否则呼叫失败
  10574. 'caller_number' => $employeePhone, //'15713803101',//
  10575. // 被叫号码,即线索号码
  10576. 'callee_number' => $customerPhone,
  10577. ];
  10578. $rs = $fish->createBridgeCall($tmp_data['fish_access_token'], $tmp_info);
  10579. if ($rs['code'] != 0) {
  10580. return json(['code' => 1, 'msg' => '呼叫失败']);
  10581. } else {
  10582. if ($rs['data']['call_result_code'] != 101) {
  10583. return json(['code' => 1, 'msg' => $rs['data']['call_result_message']]);
  10584. }
  10585. // 添加记录
  10586. $logData = [
  10587. 'session_id' => $rs['data']['contact_id'],
  10588. 'bind_num' => '',
  10589. 'caller_num' => $employeePhone,
  10590. 'callee_num' => $customerPhone,
  10591. 'employee_id' => $employee_id,
  10592. 'root_id' => $root_id,
  10593. 'customer_id' => $param['customer_id'],
  10594. 'call_id' => $fish_data['id'] // 存储飞鱼线索id
  10595. ];
  10596. $log = OutCallLog::create($logData);
  10597. }
  10598. // 添加访问记录
  10599. CustomerVisitLog::Create([
  10600. 'customer_id' => $param['customer_id'],
  10601. 'type' => 1,
  10602. 'employee_id' => $employee_id,
  10603. 'user_id' => $request->empcrm->uid,
  10604. 'org_id' => $request->empcrm->org_id,
  10605. 'customer_employee_id' => 0,
  10606. 'customer_org_id' => 0,
  10607. 'data_type' => 'out_call',
  10608. 'data_id' => $log->id,
  10609. 'remark' => '回访跟踪了飞鱼线索客户'
  10610. ]);
  10611. $fish_data->save(['is_contact' => 1, 'update_time_detail' => date('Y-m-d H:i:s')]);
  10612. return json(['code' => 0, 'msg' => '拨打中,请接听']);
  10613. }
  10614. /**
  10615. * 发起语音双呼
  10616. */
  10617. public function create_bridge_call()
  10618. {
  10619. $request = request();
  10620. $root_id = $request->empcrm->root_id;
  10621. $employee_id = $request->empcrm->id;
  10622. $param = $this->request->only(['customer_id']);
  10623. $fish_data = FishData::where(['id' => $param['customer_id']])->find(); //customer_id
  10624. if (empty($fish_data)) return json(['code' => 1, 'msg' => '线索信息有误']);
  10625. if ($fish_data['type'] == 1) {
  10626. $fish = new Fish(config('app.fish_clue_appid'), config('app.fish_clue_secret'));
  10627. $channel_setting = Company::where(['root_id' => $root_id])->value('channel_setting');
  10628. } else {
  10629. $fish = new Fish(config('app.fish_clue_appid_wh'), config('app.fish_clue_secret_wh'));
  10630. $channel_setting = Company::where(['root_id' => $root_id])->value('channel_setting_wh');
  10631. }
  10632. $tmp_data = json_decode($channel_setting, true);
  10633. $employeePhone = $request->empcrm->phone;
  10634. $customerPhone = $fish_data['telephone'];
  10635. $tmp_info = [
  10636. //广告主id
  10637. 'advertiser_id' => intval($fish_data['advertiser_id']),
  10638. //线索id
  10639. 'clue_id' => intval($fish_data['clue_id']),
  10640. // 主叫号码,必须为11位手机号码,否则呼叫失败
  10641. 'caller_number' => $employeePhone, //'15713803101',//
  10642. // 被叫号码,即线索号码
  10643. 'callee_number' => $customerPhone,
  10644. ];
  10645. $rs = $fish->createBridgeCall($tmp_data['fish_access_token'], $tmp_info);
  10646. if ($rs['code'] != 0) {
  10647. return json(['code' => 1, 'msg' => '呼叫失败']);
  10648. } else {
  10649. if ($rs['data']['call_result_code'] != 101) {
  10650. return json(['code' => 1, 'msg' => $rs['data']['call_result_message']]);
  10651. }
  10652. // 添加记录
  10653. $logData = [
  10654. 'session_id' => $rs['data']['contact_id'],
  10655. 'bind_num' => '',
  10656. 'caller_num' => $employeePhone,
  10657. 'callee_num' => $customerPhone,
  10658. 'employee_id' => $employee_id,
  10659. 'root_id' => $root_id,
  10660. 'customer_id' => 0, //$param['customer_id']
  10661. 'call_id' => $param['customer_id'] // 存储飞鱼线索id
  10662. ];
  10663. $log = OutCallLog::create($logData);
  10664. }
  10665. // 添加访问记录
  10666. CustomerVisitLog::Create([
  10667. 'customer_id' => 0,
  10668. 'type' => 1,
  10669. 'employee_id' => $employee_id,
  10670. 'user_id' => $request->empcrm->uid,
  10671. 'org_id' => $request->empcrm->org_id,
  10672. 'customer_employee_id' => 0,
  10673. 'customer_org_id' => 0,
  10674. 'data_type' => 'out_call',
  10675. 'data_id' => $log->id,
  10676. 'remark' => '回访跟踪了飞鱼线索客户'
  10677. ]);
  10678. $fish_data->save(['is_contact' => 1, 'update_time_detail' => date('Y-m-d H:i:s')]);
  10679. return json(['code' => 0, 'msg' => '拨打中,请接听']);
  10680. }
  10681. /**
  10682. * 腾讯发起语音双呼
  10683. */
  10684. public function create_bridge_call_tx()
  10685. {
  10686. $request = request();
  10687. $root_id = $request->empcrm->root_id;
  10688. $employee_id = $request->empcrm->id;
  10689. $param = $this->request->only(['customer_id']);
  10690. $fish_data = FishData::where(['id' => $param['customer_id']])->find(); //customer_id
  10691. if (empty($fish_data)) return json(['code' => 1, 'msg' => '线索信息有误']);
  10692. $tencent = new Tencent(config('app.tx_clue_appid'), config('app.tx_clue_secret'));
  10693. $channel_setting = Company::where(['root_id' => $root_id])->value('channel_setting');
  10694. $tmp_data = json_decode($channel_setting, true);
  10695. $employeePhone = $request->empcrm->phone;
  10696. $customerPhone = $fish_data['telephone'];
  10697. $tmp_info = [
  10698. //广告主id
  10699. 'advertiser_id' => intval($fish_data['advertiser_id']),
  10700. //线索id
  10701. 'clue_id' => intval($fish_data['clue_id']),
  10702. // 主叫号码,必须为11位手机号码,否则呼叫失败
  10703. 'caller_number' => $employeePhone, //'15713803101'
  10704. // 被叫号码,即线索号码
  10705. 'callee_number' => $customerPhone,
  10706. // 被叫号码,即线索号码
  10707. 'request_id' => str_rand(20),
  10708. ];
  10709. $rs = $tencent->getCallVirtualNumber($tmp_data['tx_access_token'], $tmp_info);
  10710. if ($rs['code'] != 0) {
  10711. return json(['code' => 1, 'msg' => '呼叫失败']);
  10712. } else {
  10713. // 添加记录
  10714. $logData = [
  10715. 'session_id' => $rs['data']['contact_id'],
  10716. 'bind_num' => '',
  10717. 'caller_num' => $employeePhone,
  10718. 'callee_num' => $customerPhone,
  10719. 'employee_id' => $employee_id,
  10720. 'root_id' => $root_id,
  10721. 'customer_id' => 0, //$param['customer_id']
  10722. 'call_id' => $param['customer_id'] // 存储飞鱼线索id
  10723. ];
  10724. $log = OutCallLog::create($logData);
  10725. }
  10726. // 添加访问记录
  10727. CustomerVisitLog::Create([
  10728. 'customer_id' => 0,
  10729. 'type' => 1,
  10730. 'employee_id' => $employee_id,
  10731. 'user_id' => $request->empcrm->uid,
  10732. 'org_id' => $request->empcrm->org_id,
  10733. 'customer_employee_id' => 0,
  10734. 'customer_org_id' => 0,
  10735. 'data_type' => 'out_call',
  10736. 'data_id' => $log->id,
  10737. 'remark' => '回访跟踪了飞鱼线索客户'
  10738. ]);
  10739. $fish_data->save(['is_contact' => 1, 'update_time_detail' => date('Y-m-d H:i:s')]);
  10740. return json(['code' => 0, 'msg' => '拨打中,请接听']);
  10741. }
  10742. /**
  10743. * 分配员工
  10744. */
  10745. public function fish_validation_view()
  10746. {
  10747. $request = request();
  10748. $root_id = $request->empcrm->root_id;
  10749. $emp_org_id = $request->empcrm->org_id;
  10750. $param = $this->request->only(['id']);
  10751. $customer_info = FishData::where(['id' => $param['id'], 'root_id' => $root_id])->find();
  10752. $orgids = orgSubIds($emp_org_id);
  10753. $employee = Employee::where([['org_id', 'in', $orgids], ['state', '=', '在职'], ['uid', '>', 0]])->field('id,name')->select()->toArray();
  10754. View::assign('employee_list', $employee);
  10755. View::assign('data', $customer_info);
  10756. return View::fetch();
  10757. }
  10758. /**
  10759. * 重新分配员工
  10760. */
  10761. public function fish_validation_again()
  10762. {
  10763. $request = request();
  10764. $root_id = $request->empcrm->root_id;
  10765. $param = $this->request->only(['customer_id', 'employee_id']);
  10766. if (empty($param['customer_id']) || !isset($param['customer_id'])) return json(['code' => 1, 'msg' => '请先选择客户']);
  10767. $customer_id = explode(',', $param['customer_id']);
  10768. $data = FishData::where([['id', 'in', $customer_id], ['is_allocation', '=', 1]])->column('id');
  10769. if (!empty($data)) return json(['code' => 1, 'msg' => '存在已转化的客户']);
  10770. $employee_info = Employee::where(['id' => $param['employee_id'], 'root_id' => $root_id])->field('id,name,org_id')->find();
  10771. if (!$employee_info) return json(['code' => 1, 'msg' => '员工信息有误']);
  10772. 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']]);
  10773. return json(['code' => 0, 'msg' => '确认完成']);
  10774. }
  10775. /**
  10776. * 渠道统计
  10777. */
  10778. public function fish_total()
  10779. {
  10780. $request = request();
  10781. $root_id = $request->empcrm->root_id;
  10782. $empid = $request->empcrm->id;
  10783. $team_orgs = orgSubIds($request->empcrm->org_id);
  10784. $param = $this->request->only(['date', 'employee_id', 'org_id']);
  10785. if (!$request->isAjax()) {
  10786. $emplist = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->field('id,name')->select()->toArray();
  10787. View::assign('emplist', $emplist);
  10788. $org_list = Org::where([['id', 'in', $team_orgs]])->select()->toArray();
  10789. View::assign('org_list', $org_list);
  10790. $data['company_name'] = Company::where('root_id', $root_id)->value('company_name');
  10791. $data['empname'] = $request->empcrm->name;
  10792. View::assign('data', $data);
  10793. $source_list = CustomerSource::where('root_id', '=', $root_id)->select()->toArray();
  10794. View::assign('source_list', $source_list);
  10795. return View::fetch();
  10796. }
  10797. $state1 = Customer::changeState('待确认', 'chaos');
  10798. $state2 = Customer::changeState('无效', 'chaos');
  10799. $state3 = CustomerVisitLog::changeState('已交定', 'chaos');
  10800. $state4 = CustomerVisitLog::changeState('已签单', 'chaos');
  10801. $state5 = CustomerVisitLog::changeState('确认到店', 'chaos');
  10802. $state6 = CustomerVisitLog::changeState('已到场', 'chaos');
  10803. $state7 = CustomerVisitLog::changeState('已量房', 'chaos');
  10804. if (!empty($param['date'])) {
  10805. $newtime = explode('到', $param['date']);
  10806. } else {
  10807. return json(['code' => 1, 'msg' => '日期为空']);
  10808. }
  10809. $fish_campaign_where[] = ['data_time', 'between', $newtime];
  10810. $fish_data_where[] = ['create_time', 'between', $newtime];
  10811. $customer_where[] = ['sign_time', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']];
  10812. $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id'); //默认部门
  10813. $wherev_where = []; // 查询渠道下所有的客户用
  10814. if (!empty($param['employee_id'])) {
  10815. $org_employee = [$param['employee_id']];
  10816. $fish_data_where[] = ['employee_id', '=', $param['employee_id']];
  10817. $wherev_where[] = ['employee_id', '=', $param['employee_id']];
  10818. }
  10819. if (!empty($param['org_id']) && empty($param['employee_id'])) {
  10820. $org_employee = Employee::where([['root_id', '=', $root_id], ['org_id', 'in', orgSubIds($param['org_id'])], ['uid', '>', 0], ['state', '=', '在职']])->column('id');
  10821. $fish_data_where[] = ['org_id', 'in', orgSubIds($param['org_id'])];
  10822. $wherev_where[] = ['org_id', 'in', orgSubIds($param['org_id'])];
  10823. }
  10824. $customer_where[] = ['employee_id', 'in', $org_employee];
  10825. $wherev[] = ['confirm_date', 'between', [$newtime[0] . ' 00:00:00', $newtime[1] . ' 23:59:59']];
  10826. $wherev[] = ['customer_employee_id', 'in', $org_employee];
  10827. $wherev[] = ['customer_org_id', 'in', $team_orgs];
  10828. $wherev[] = ['state', 'in', array_merge($state1, $state2, $state3, $state4, $state5, $state6, $state7)];
  10829. //计算广告主id
  10830. $advertiser_list[] = ['id' => 0, 'name' => '高潜客户'];
  10831. $channel_setting = Company::where(['root_id' => $root_id])->field('root_id,channel_setting,channel_setting_wh')->find();
  10832. if (isset($channel_setting['channel_setting_wh']) && $channel_setting['channel_setting_wh']) {
  10833. $tmp_data = json_decode($channel_setting['channel_setting_wh'], true);
  10834. $advertiser_id = explode(',', $tmp_data['fish_adv']);
  10835. $fish_account = explode(',', $tmp_data['fish_account']);
  10836. foreach ($advertiser_id as $key => $val) {
  10837. $advertiser_list[] = [
  10838. 'id' => $advertiser_id[$key],
  10839. 'name' => isset($fish_account[$key]) ? $fish_account[$key] : '暂未设置名称',
  10840. ];
  10841. }
  10842. }
  10843. if (isset($channel_setting['channel_setting']) && $channel_setting['channel_setting']) {
  10844. $tmp_data = json_decode($channel_setting['channel_setting'], true);
  10845. $advertiser_id = explode(',', $tmp_data['fish_adv']);
  10846. $fish_account = explode(',', $tmp_data['fish_account']);
  10847. foreach ($advertiser_id as $key => $val) {
  10848. $advertiser_list[] = [
  10849. 'id' => $advertiser_id[$key],
  10850. 'name' => isset($fish_account[$key]) ? $fish_account[$key] : '暂未设置名称',
  10851. ];
  10852. }
  10853. if (isset($tmp_data['tx_adv']) && !empty($tmp_data['tx_adv'])) {
  10854. $tx_advertiser_id = explode(',', $tmp_data['tx_adv']);
  10855. $tx_account = explode(',', $tmp_data['tx_account']);
  10856. foreach ($tx_advertiser_id as $key => $val) {
  10857. $advertiser_list[] = [
  10858. 'id' => $tx_advertiser_id[$key],
  10859. 'name' => isset($tx_account[$key]) ? $tx_account[$key] : '暂未设置名称',
  10860. ];
  10861. }
  10862. }
  10863. }
  10864. //计算广告主id
  10865. $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];
  10866. foreach ($advertiser_list as $key => $val) {
  10867. //查询广告主当前的消耗
  10868. $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();
  10869. $fish_data = FishData::where(['advertiser_id' => $val['id'], 'root_id' => $root_id])->where($fish_data_where)->column('id,is_allocation,customer_id');
  10870. $all_fish_data = FishData::where(['advertiser_id' => $val['id'], 'root_id' => $root_id])->where($wherev_where)->column('id,customer_id');
  10871. $customer_id = array_column($fish_data, 'customer_id');
  10872. $all_customer_id = array_column($all_fish_data, 'customer_id');
  10873. $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();
  10874. $valid_cont = FishData::where([['advertiser_id', '=', $val['id']], ['root_id', '=', $root_id], ['customer_id', '>', 0]])->where($fish_data_where)->count(); //有效线索
  10875. $advertiser_list[$key]['fish_sql'] = FishData::where([['advertiser_id', '=', $val['id']], ['root_id', '=', $root_id], ['customer_id', '>', 0]])->where($fish_data_where)->buildSql();
  10876. $count = count($fish_data);
  10877. $exten_money = $fish_campaign['exten_money'] ?? 0; //消费
  10878. $exten_show = $fish_campaign['exten_show'] ?? 0; //展现
  10879. $exten_click = $fish_campaign['exten_click'] ?? 0; //点击
  10880. // $valid_cont = count($customer_data);//有效线索
  10881. $advertiser_list[$key]['exten_money'] = $exten_money;
  10882. $advertiser_list[$key]['exten_show'] = $exten_show;
  10883. $advertiser_list[$key]['exten_click'] = $exten_click;
  10884. $advertiser_list[$key]['click_percen'] = $exten_show > 0 ? round($exten_click / $exten_show * 100, 2) . '%' : '0%';
  10885. $advertiser_list[$key]['clue_conversion'] = $exten_click > 0 ? round($count / $exten_click * 100, 2) . '%' : '0%';
  10886. $advertiser_list[$key]['clue_cont'] = $count;
  10887. $advertiser_list[$key]['clue_money'] = $count > 0 ? round($exten_money / $count, 2) : 0;
  10888. $advertiser_list[$key]['valid_cont'] = $valid_cont;
  10889. $advertiser_list[$key]['valid_money'] = $valid_cont > 0 ? round($exten_money / $valid_cont, 2) : 0; //有效单价
  10890. $advertiser_list[$key]['valid_percen'] = $count > 0 ? round($valid_cont / $count * 100, 2) : 0; //有效率
  10891. $visitlog_list = CustomerVisitLog::where($wherev)->whereIn('customer_id', $all_customer_id)->column('customer_id,state');
  10892. $meet_data = $reach_data = $sign_data = [];
  10893. $meet = $reach = $sign = $all_money = $basic_money = 0;
  10894. foreach ($visitlog_list as $v) {
  10895. //处理签单
  10896. if (in_array($v['state'], array_merge($state3, $state4, $state5, $state6, $state7), true) && !in_array($v['customer_id'], $meet_data, true)) {
  10897. $meet++;
  10898. $meet_data[] = $v['customer_id'];
  10899. }
  10900. //处理交定
  10901. if (in_array($v['state'], $state3, true) && !in_array($v['customer_id'], $reach_data, true)) {
  10902. $reach++;
  10903. $reach_data[] = $v['customer_id'];
  10904. }
  10905. if (in_array($v['state'], $state4, true) && !in_array($v['customer_id'], $sign_data, true)) {
  10906. $sign++;
  10907. $sign_data[] = $v['customer_id'];
  10908. }
  10909. }
  10910. foreach ($customer_data as $val) {
  10911. if (isset($val['signed_moneys']) && $val['signed_moneys']) {
  10912. $all_money += $val['signed_moneys'];
  10913. $basic_money += $val['signed_moneys'];
  10914. }
  10915. if (!isset($val['signed_moneys']) && isset($val['deposit_moneys'])) {
  10916. $all_money += $val['deposit_moneys'];
  10917. }
  10918. }
  10919. $advertiser_list[$key]['meet_cont'] = $meet;
  10920. $advertiser_list[$key]['meet_percen'] = $count > 0 ? round($meet / $count * 100, 2) : 0;
  10921. $advertiser_list[$key]['meet_money'] = $meet > 0 ? round($exten_money / $meet, 2) : 0;
  10922. $advertiser_list[$key]['deposit_cont'] = $reach;
  10923. $advertiser_list[$key]['deposit_percen'] = $meet > 0 ? round($reach / $meet * 100, 2) : 0;
  10924. $advertiser_list[$key]['sign_cont'] = $sign;
  10925. $advertiser_list[$key]['basic_money'] = $all_money;
  10926. $advertiser_list[$key]['sign_agv_money'] = $sign > 0 ? round($basic_money / $sign, 2) : 0;
  10927. $advertiser_list[$key]['ROI'] = !empty($exten_money) && !empty($basic_money) ? round($exten_money / $basic_money * 100, 2) : 0;
  10928. $t['exten_money'] += !empty($exten_money) ? round($exten_money, 2) : 0;
  10929. $t['exten_show'] += $exten_show;
  10930. $t['exten_click'] += $exten_click;
  10931. $t['clue_cont'] += $count;
  10932. $t['valid_cont'] += $valid_cont;
  10933. $t['meet_cont'] += $meet;
  10934. $t['deposit_cont'] += $reach;
  10935. $t['sign_cont'] += $sign;
  10936. $t['basic_money'] += $all_money;
  10937. $t['other_money'] += $basic_money;
  10938. }
  10939. $t['exten_money'] = round($t['exten_money'], 2);
  10940. $t['clue_money'] = $t['exten_money'] == 0 || $t['clue_cont'] == 0 ? 0 : round($t['exten_money'] / $t['clue_cont'], 2);
  10941. $t['valid_money'] = $t['exten_money'] == 0 || $t['valid_cont'] == 0 ? 0 : round($t['exten_money'] / $t['valid_cont'], 2);
  10942. $t['click_percen'] = $t['exten_show'] == 0 || $t['exten_click'] == 0 ? 0 : round($t['exten_click'] / $t['exten_show'] * 100, 2);
  10943. $t['valid_percen'] = $t['clue_cont'] == 0 || $t['valid_cont'] == 0 ? 0 : round($t['valid_cont'] / $t['clue_cont'] * 100, 2);
  10944. $t['meet_percen'] = $t['clue_cont'] == 0 || $t['meet_cont'] == 0 ? 0 : round($t['meet_cont'] / $t['clue_cont'] * 100, 2);
  10945. $t['deposit_percen'] = $t['meet_cont'] == 0 || $t['deposit_cont'] == 0 ? 0 : round($t['deposit_cont'] / $t['meet_cont'] * 100, 2);
  10946. $t['meet_money'] = $t['exten_money'] == 0 || $t['meet_cont'] == 0 ? 0 : round($t['exten_money'] / $t['meet_cont'], 2);
  10947. $t['sign_agv_money'] = $t['sign_cont'] == 0 || $t['other_money'] == 0 ? 0 : round($t['other_money'] / $t['sign_cont'], 2);
  10948. $t['ROI'] = $t['exten_money'] == 0 || $t['basic_money'] == 0 ? 0 : round($t['exten_money'] / $t['basic_money'] * 100, 2);
  10949. array_push($advertiser_list, $t);
  10950. return json(['code' => 0, 'data' => $advertiser_list, 'count' => 0, 'msg' => '获取成功']);
  10951. }
  10952. public function fish_detail()
  10953. {
  10954. $request = request();
  10955. $root_id = $request->empcrm->root_id;
  10956. // $team_orgs = orgSubIds($request->empcrm->org_id);
  10957. $param = $this->request->only(['date', 'source_id', 'org_id', 'employee_id', 'state', 'page' => 1, 'limit' => 17]);
  10958. if (!$request->isAjax()) {
  10959. View::assign('date', $param['date']);
  10960. View::assign('state', $param['state']);
  10961. View::assign('source_id', !empty($param['source_id']) ? $param['source_id'] : '');
  10962. View::assign('employee_id', !empty($param['employee_id']) ? $param['employee_id'] : '');
  10963. View::assign('org_id', !empty($param['org_id']) ? $param['org_id'] : '');
  10964. return View::fetch();
  10965. }
  10966. $order = isset($param['order']) ? $param['order'] : 'id desc';
  10967. // $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
  10968. if (!empty($param['employee_id'])) {
  10969. $org_employee = [$param['employee_id']];
  10970. $condition[] = ['employee_id', 'in', $org_employee];
  10971. }
  10972. if (!empty($param['org_id']) && empty($param['employee_id'])) {
  10973. $org_employee = Employee::where([['root_id', '=', $root_id], ['org_id', 'in', orgSubIds($param['org_id'])], ['uid', '>', 0], ['state', '=', '在职']])->column('id');
  10974. if (!empty($param['source_id'])) {
  10975. $condition[] = ['employee_id', 'in', $org_employee];
  10976. } else {
  10977. $condition[] = ['employee_id', 'in', implode(',', $org_employee) . ',0'];
  10978. }
  10979. }
  10980. if (empty($param['org_id']) && empty($param['employee_id'])) {
  10981. $condition[] = ['employee_id', '>', '-1'];
  10982. }
  10983. $condition[] = ['root_id', '=', $root_id];
  10984. if (!empty($param['source_id'])) {
  10985. $condition[] = ['advertiser_id', '=', $param['source_id']];
  10986. } else {
  10987. $condition[] = ['advertiser_id', '=', 0];
  10988. }
  10989. if (empty($param['date'])) return json(['code' => 1, 'msg' => '参数错误']);
  10990. if (!empty($param['date'])) {
  10991. $newtime = explode('到', $param['date']);
  10992. }
  10993. $condition[] = ['create_time', 'between', [$newtime[0], $newtime[1]]];
  10994. $list = FishData::where($condition)->order($order)->page($param['page'], $param['limit'])->select();
  10995. foreach ($list as $key => $val) {
  10996. $list[$key]['telephone'] = substr_replace($val['telephone'], '******', 3, 6);
  10997. $list[$key]['gender'] = ['未知', '男', '女'][$val['gender']];
  10998. $list[$key]['is_contact'] = $val['is_contact'] == 1 ? '已联系' : '未联系';
  10999. $list[$key]['is_allocation'] = $val['is_allocation'] == 1 ? '已转化' : '未转化';
  11000. }
  11001. $count = FishData::where($condition)->count();
  11002. $sql = FishData::where($condition)->buildSql();
  11003. return json(['code' => 0, 'data' => $list, 'count' => $count, 'where' => $condition, 'param' => $param, 'sql' => $sql]);
  11004. }
  11005. }