12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959 |
- <?php
- namespace App\Service;
- use App\Model\BasicType;
- use App\Model\BasicTypeAllUse;
- use App\Model\Customer;
- use App\Model\CustomerPerFormance;
- use App\Model\CustomerReport;
- use App\Model\CustomerReportDepart;
- use App\Model\Depart;
- use App\Model\DepartIndex;
- use App\Model\Employee;
- use App\Model\EmployeeDepartPermission;
- use App\Model\InOutRecord;
- use App\Model\InvoiceOrder;
- use App\Model\InvoiceOrderInfo;
- use App\Model\LastJc;
- use App\Model\MonthlyPerFormance;
- use App\Model\Product;
- use App\Model\ProductAdjustment;
- use App\Model\ProductCategory;
- use App\Model\PurchaseOrder;
- use App\Model\PurchaseOrderInfo;
- use App\Model\ReturnExchangeOrder;
- use App\Model\ReturnExchangeOrderProductInfo;
- use App\Model\SalesOrder;
- use App\Model\SalesOrderProductInfo;
- use App\Model\SalesOrderReport;
- use App\Model\SeeRange;
- use App\Model\Setting;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\DB;
- class StatisticsService extends Service
- {
- public function statisticsCustomer($data,$user){
- if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择客资创建时间区间'];
- $model = Customer::Clear($user,$data);
- $model = $model->where('del_time',0)
- ->select('id','model_type','customer_from','top_depart_id')
- ->orderby('id', 'desc');
- if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
- $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
- $model->where('crt_time','>=',$return[0]);
- $model->where('crt_time','<=',$return[1]);
- }
- $list = $model->get()->toArray();
- $list1 = $this->fillStatisticsCustomer($list);
- $list2 = $this->fillStatisticsCustomer2($list);
- $list3 = $this->fillStatisticsCustomer3($list);
- return [true, ['bt' => $list1, 'zz' => $list2, 'table' => $list3]];
- }
- public function fillStatisticsCustomer($data){
- if(empty($data)) return $data;
- $customer_from = array_unique(array_column($data,'customer_from'));
- $basic_type = BasicType::where('del_time',0)
- ->whereIn('id',$customer_from)
- ->where('type',2)
- ->pluck('title','id')
- ->toArray();
- $return = [];
- foreach ($data as $value){
- $tmp = $basic_type[$value['customer_from']] ?? "";
- if(! $tmp) continue;
- if(isset($return[$tmp])){
- $return[$tmp]['total'] += 1;
- }else{
- $return[$tmp] = [
- 'total' => 1,
- 'title' => $tmp
- ];
- }
- }
- return array_values($return);
- }
- public function fillStatisticsCustomer2($data){
- if(empty($data)) return $data;
- $depart = Depart::where('del_time',0)
- ->where('parent_id',0)
- ->where('is_main',0)
- ->pluck('title','id')
- ->toArray();
- $return = [];
- foreach ($depart as $key => $value){
- $return[$key] = [
- 'title' => $value,
- 'total' => 0
- ];
- }
- $top_depart_id = SeeRange::where('del_time',0)
- ->where('data_type',SeeRange::type_one)
- ->where('type',SeeRange::data_three)
- ->whereIn('data_id', array_column($data,'id'))
- ->select('param_id as fp_top_depart_id')
- ->get()->toArray();
- $top_depart_map = [];
- foreach ($top_depart_id as $value){
- if(isset($top_depart_map[$value['fp_top_depart_id']])){
- $top_depart_map[$value['fp_top_depart_id']] += 1;
- }else{
- $top_depart_map[$value['fp_top_depart_id']] = 1;
- }
- }
- foreach ($return as $key => $value){
- if(isset($top_depart_map[$key])) $return[$key]['total'] = $top_depart_map[$key];
- }
- $return = array_values($return);
- usort($return, function($a, $b) {
- return $b['total'] - $a['total'];
- });
- return $return;
- }
- public function fillStatisticsCustomer3($data){
- if(empty($data)) return $data;
- $array = array_unique(array_column($data,'customer_from'));
- $basic_map = BasicType::whereIn('id',$array)
- ->pluck('title','id')
- ->toArray();
- $return_first = $customer_from_map = $header = [];
- foreach ($data as $value){
- $title = $basic_map[$value['customer_from']] ?? "";
- if(! $title) continue;
- if(isset($return_first[$title])){
- $return_first[$title]['total'] += 1;
- }else{
- $return_first[$title] = [
- 'title' => $title,
- 'total' => 1,
- ];
- }
- $customer_from_map[$value['id']] = $value['customer_from'];
- if(! in_array($title, $header)) $header[] = $title;
- }
- $header_tmp = $header_return = [];
- foreach ($header as $value){
- $header_tmp[] = [
- "title" => $value,
- "total" => 0,
- ];
- $header_return[] = [
- "title" => $value
- ];
- }
- $return_first = array_values($return_first);
- $depart = Depart::where('del_time',0)
- ->where('parent_id',0)
- ->where('is_main',0)
- ->pluck('title','id')
- ->toArray();
- $return = [];
- foreach ($depart as $key => $value){
- $return[$key] = [
- 'title' => $value,
- 'total' => 0,
- 'customer_from' => $header_tmp,
- ];
- }
- $top_depart_id = SeeRange::where('del_time',0)
- ->where('data_type',SeeRange::type_one)
- ->where('type',SeeRange::data_three)
- ->whereIn('data_id', array_column($data,'id'))
- ->select('param_id as fp_top_depart_id','data_id as customer_id')
- ->get()->toArray();
- $top_depart_map = $top_depart_map2 = [];
- foreach ($top_depart_id as $value){
- if(isset($top_depart_map[$value['fp_top_depart_id']])){
- $top_depart_map[$value['fp_top_depart_id']] += 1;
- }else{
- $top_depart_map[$value['fp_top_depart_id']] = 1;
- }
- $customer_from_tmp = $customer_from_map[$value['customer_id']] ?? 0;
- if($customer_from_tmp){
- if(isset($top_depart_map2[$value['fp_top_depart_id']][$customer_from_tmp])){
- $top_depart_map2[$value['fp_top_depart_id']][$customer_from_tmp] += 1;
- }else{
- $top_depart_map2[$value['fp_top_depart_id']][$customer_from_tmp] = 1;
- }
- }
- }
- foreach ($return as $key => $value){
- if(isset($top_depart_map[$key])) {
- $num = $top_depart_map[$key];
- if($num <= 0) {
- unset($return[$key]);
- continue;
- }else{
- $return[$key]['total'] = $top_depart_map[$key];
- }
- }else{
- unset($return[$key]);
- continue;
- }
- if(isset($top_depart_map2[$key])) {
- $tmp = $top_depart_map2[$key];
- foreach ($tmp as $k => $v){
- $title = $basic_map[$k] ?? "";
- if(! $title) continue;
- foreach ($return[$key]['customer_from'] as $kk => $vv){
- if($vv['title'] == $title){
- $return[$key]['customer_from'][$kk]['total'] = $v;
- }
- }
- }
- }
- }
- $return = array_values($return);
- usort($return, function($a, $b) {
- return $b['total'] - $a['total'];
- });
- return ['header' => $header_return,'left' => $return_first, 'right' => $return];
- }
- //销售统计饼图 根据合同类型区分(例如线上合同、线下合同)
- public function statisticsBt($data,$user){
- if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择销售订单时间区间'];
- //总社id
- $head = $user['head']['id'] ?? 0;
- //当前门店
- $current_top_depart_id = $this->getMyTopDepart($user);
- //需要所有数据的门店
- $setting = Setting::where('setting_name','bt_top_depart_id')->first();
- $bt_top_depart_id = $setting['setting_value'] ?? [];
- $bt_top_depart_id = json_decode($bt_top_depart_id,true);
- //当前门店是否在设置门店中
- $bool = in_array($current_top_depart_id,$bt_top_depart_id);
- $model = SalesOrder::where('del_time',0)
- ->when(! $bool, function ($query) use ($current_top_depart_id) {
- return $query->where('top_depart_id', $current_top_depart_id);
- })
- ->select('model_type','contract_fee as total','top_depart_id');
- if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
- $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
- $model->where('crt_time','>=',$return[0]);
- $model->where('crt_time','<=',$return[1]);
- }
- $list = $model->get()->toArray();
- $list = $this->fillStatisticsBt($list,$bt_top_depart_id,$bool);
- return [true, $list];
- }
- public function fillStatisticsBt($data,$bt_top_depart_id,$bool){
- if(empty($data)) return $data;
- $return = [];
- foreach ($data as $value){
- $model_type_title = SalesOrder::$model_type_title[$value['model_type']] ?? "";
- if($bool){
- if($value['model_type'] != SalesOrder::Model_type_two){
- if(in_array($value['top_depart_id'], $bt_top_depart_id)){
- $this->makeThis($return,$value,$model_type_title);
- }
- }else{
- $this->makeThis($return,$value,$model_type_title);
- }
- }else{
- $this->makeThis($return,$value,$model_type_title);
- }
- }
- return array_values($return);
- }
- public function makeThis(&$return,$value,$model_type_title){
- if(isset($return[$value['model_type']])){
- $total = bcadd($value['total'], $return[$value['model_type']]['total'],2);
- $return[$value['model_type']]['total'] = $total;
- }else{
- $return[$value['model_type']] = [
- 'model_type' => $value['model_type'],
- 'model_type_title' => $model_type_title,
- 'total' => $value['total'],
- ];
- }
- }
- //省 订单类型细分统计
- public function statisticsProvince($data,$user){
- if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
- $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
- //总社id
- $head = $user['head']['id'] ?? 0;
- //当前门店
- $current_top_depart_id = $this->getMyTopDepart($user);
- //销售订单类型
- $model_type = 0;
- if(! empty($data['model_type'])) $model_type = $data['model_type'];
- //门店设置的指标 按照区域汇总
- $index_map = DepartIndex::where('del_time',0)
- ->pluck('param_one','top_depart_id')
- ->toArray();
- //分社所属省
- $depart_map = $province_map = [];
- $depart = Depart::where('parent_id',0)
- ->where('province','<>','')
- ->where('del_time',0)
- ->select('province','id')
- ->get()->toArray();
- foreach ($depart as $value){
- $depart_map[$value['id']] = $value['province'];
- $province_map[$value['province']][] = $value['id'];
- }
- $address_map = config('address');
- $address_map = array_column($address_map,'label','value');
- //省
- $final_address_map = [];
- foreach ($address_map as $key => $value){
- $tmp = [
- 'key' => $key,
- 'title' => $value,
- 'total' => 0,
- 'index' => 0
- ];
- $top_depart_id = $province_map[$key] ?? [];
- if(! empty($top_depart_id)){
- foreach ($top_depart_id as $depart_id){
- $index = $index_map[$depart_id] ?? 0;
- $tmp['index'] = bcadd($tmp['index'], $index,2);
- }
- }
- $final_address_map[] = $tmp;
- }
- //特殊的门店
- $setting = Setting::where('setting_name','bt_top_depart_id')->first();
- $bt_top_depart_id = $setting['setting_value'] ?? [];
- $bt_top_depart_id = json_decode($bt_top_depart_id,true);
- //当前门店是否在设置门店中
- $bool = in_array($current_top_depart_id,$bt_top_depart_id);
- $sale_order = SalesOrder::where("del_time",0)
- ->where('crt_time','>=',$return[0])
- ->where('crt_time','<=',$return[1])
- ->when(! $bool, function ($query) use ($current_top_depart_id) {
- return $query->where('top_depart_id', $current_top_depart_id);
- })
- ->when(! empty($model_type), function ($query) use ($model_type) {
- return $query->where('model_type',$model_type);
- })
- ->select('id','top_depart_id','contract_fee','model_type')
- ->get()->toArray();
- //合同
- $purchase_map = $sale_order_id = [];
- foreach ($sale_order as $value){
- $area = $depart_map[$value['top_depart_id']] ?? 0;
- if(! $area) continue;
- if($value['model_type'] != SalesOrder::Model_type_two){
- if(in_array($value['top_depart_id'], $bt_top_depart_id)){
- $sale_order_id[] = $value['id'];
- $this->makeData($purchase_map,$value,$area);
- }
- }else{
- $sale_order_id[] = $value['id'];
- $this->makeData($purchase_map,$value,$area);
- }
- }
- //退货的差异
- $returnExchange_map = [];
- $returnExchange = ReturnExchangeOrder::where('del_time',0)
- ->where('type',ReturnExchangeOrder::Order_type)
- ->whereIn('data_id',array_unique($sale_order_id))
- ->select('top_depart_id','difference_amount')
- ->get()->toArray();
- foreach ($returnExchange as $value){
- $area = $depart_map[$value['top_depart_id']] ?? 0;
- if(! $area) continue;
- if(isset($returnExchange_map[$area])){
- $total = bcadd($returnExchange_map[$area], $value['difference_amount'],2);
- $returnExchange_map[$area] = $total;
- }else{
- $returnExchange_map[$area] = $value['difference_amount'];
- }
- }
- foreach ($final_address_map as $key => $value){
- $purchase_tmp = $purchase_map[$value['key']] ?? 0;
- $return_tmp = $returnExchange_map[$value['key']] ?? 0;
- $final_address_map[$key]['total'] = bcsub($purchase_tmp, $return_tmp,2);
- }
- usort($final_address_map, function($a, $b) {
- return $b['total'] - $a['total'];
- });
- return [true, $final_address_map];
- }
- public function makeData(&$purchase_map, $value,$area){
- if(isset($purchase_map[$area])){
- $total = bcadd($purchase_map[$area], $value['contract_fee'],2);
- $purchase_map[$area] = $total;
- }else{
- $purchase_map[$area] = $value['contract_fee'];
- }
- }
- //大区 订单类型细分统计
- public function statisticsArea($data,$user){
- if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
- $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
- //总社id
- $head = $user['head']['id'] ?? 0;
- //当前门店
- $current_top_depart_id = $this->getMyTopDepart($user);
- //销售订单类型
- $model_type = 0;
- if(! empty($data['model_type'])) $model_type = $data['model_type'];
- //门店设置的指标 按照区域汇总
- $index_map = DepartIndex::where('del_time',0)
- ->pluck('param_one','top_depart_id')
- ->toArray();
- //分社所属大区
- $depart_map = $area_map = [];
- $depart = Depart::where('parent_id',0)
- ->where('area','>',0)
- ->where('del_time',0)
- ->select('area','id')
- ->get()->toArray();
- foreach ($depart as $value){
- $depart_map[$value['id']] = $value['area'];
- $area_map[$value['area']][] = $value['id'];
- }
- //大区
- $area = Depart::$area;
- $area_map = [];
- foreach ($area as $key => $value){
- $tmp = [
- 'key' => $key,
- 'title' => $value,
- 'total' => 0,
- 'index' => 0
- ];
- $top_depart_id = $area_map[$key] ?? [];
- if(! empty($top_depart_id)){
- foreach ($top_depart_id as $depart_id){
- $index = $index_map[$depart_id] ?? 0;
- $tmp['index'] = bcadd($tmp['index'], $index,2);
- }
- }
- $area_map[] = $tmp;
- }
- //特殊的门店
- $setting = Setting::where('setting_name','bt_top_depart_id')->first();
- $bt_top_depart_id = $setting['setting_value'] ?? [];
- $bt_top_depart_id = json_decode($bt_top_depart_id,true);
- //当前门店是否在设置门店中
- $bool = in_array($current_top_depart_id,$bt_top_depart_id);
- $sale_order = SalesOrder::where("del_time",0)
- ->where('crt_time','>=',$return[0])
- ->where('crt_time','<=',$return[1])
- ->when(! $bool, function ($query) use ($current_top_depart_id) {
- return $query->where('top_depart_id', $current_top_depart_id);
- })
- ->when(! empty($model_type), function ($query) use ($model_type) {
- return $query->where('model_type',$model_type);
- })
- ->select('top_depart_id','contract_fee','model_type','id')
- ->get()->toArray();
- $purchase_map = $sale_order_id = [];
- foreach ($sale_order as $value){
- $area = $depart_map[$value['top_depart_id']] ?? 0;
- if(! $area) continue;
- if($value['model_type'] != SalesOrder::Model_type_two){
- if(in_array($value['top_depart_id'], $bt_top_depart_id)){
- $sale_order_id[] = $value['id'];
- $this->makeData($purchase_map,$value,$area);
- }
- }else{
- $sale_order_id[] = $value['id'];
- $this->makeData($purchase_map,$value,$area);
- }
- }
- //退货的差异
- $returnExchange_map = [];
- $returnExchange = ReturnExchangeOrder::where('del_time',0)
- ->where('type',ReturnExchangeOrder::Order_type)
- ->whereIn('data_id',array_unique($sale_order_id))
- ->select('top_depart_id','difference_amount')
- ->get()->toArray();
- foreach ($returnExchange as $value){
- $area = $depart_map[$value['top_depart_id']] ?? 0;
- if(! $area) continue;
- if(isset($returnExchange_map[$area])){
- $total = bcadd($returnExchange_map[$area], $value['difference_amount'],2);
- $returnExchange_map[$area] = $total;
- }else{
- $returnExchange_map[$area] = $value['difference_amount'];
- }
- }
- foreach ($area_map as $key => $value){
- $purchase_tmp = $purchase_map[$value['key']] ?? 0;
- $return_tmp = $returnExchange_map[$value['key']] ?? 0;
- $area_map[$key]['total'] = bcsub($purchase_tmp, $return_tmp,2);
- }
- usort($area_map, function($a, $b) {
- return $b['total'] - $a['total'];
- });
- return [true, $area_map];
- }
- // 省|大区下的门店 订货统计
- public function statisticsAreaDepart($data,$user){
- if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
- $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
- if(empty($data['area']) && empty($data['province'])) return [false, '大区或省份必须选择一个'];
- if(! empty($data['area']) && ! empty($data['province']))return [false, '大区或省份有且只能有一个'];
- if(! empty($data['area']) && ! isset(Depart::$area[$data['area']])) return [false, '该大区不存在'];
- $address_map = config('address');
- $address_map = array_column($address_map,'label','value');
- if(! empty($data['province']) && ! isset($address_map[$data['province']])) return [false, '该省不存在'];
- //总社id
- $head = $user['head']['id'] ?? 0;
- //当前门店
- $current_top_depart_id = $this->getMyTopDepart($user);
- //销售订单类型
- $model_type = 0;
- if(! empty($data['model_type'])) $model_type = $data['model_type'];
- if(! empty($data['area'])){
- $depart = Depart::where('parent_id',0)
- ->where('del_time',0)
- ->where('area',$data['area'])
- ->select('id','title')
- ->get()
- ->toArray();
- if(empty($depart)) return [false, '该大区下不存在门店'];
- }else{
- $depart = Depart::where('parent_id',0)
- ->where('del_time',0)
- ->where('province',$data['province'])
- ->select('id','title')
- ->get()
- ->toArray();
- if(empty($depart)) return [false, '该省下不存在门店'];
- }
- //门店设置的指标
- $index_map = DepartIndex::where('del_time',0)
- ->whereIn('top_depart_id',array_column($depart,'id'))
- ->pluck('param_one','top_depart_id')
- ->toArray();
- //特殊的门店
- $setting = Setting::where('setting_name','bt_top_depart_id')->first();
- $bt_top_depart_id = $setting['setting_value'] ?? [];
- $bt_top_depart_id = json_decode($bt_top_depart_id,true);
- //当前门店是否在设置门店中
- $bool = in_array($current_top_depart_id,$bt_top_depart_id);
- //合同
- $sale_order = SalesOrder::where("del_time",0)
- ->where('crt_time','>=',$return[0])
- ->where('crt_time','<=',$return[1])
- ->when(! $bool, function ($query) use ($current_top_depart_id) {
- return $query->where('top_depart_id', $current_top_depart_id);
- })
- ->when(! empty($model_type), function ($query) use ($model_type) {
- return $query->where('model_type',$model_type);
- })
- ->select('top_depart_id','contract_fee','id','model_type')
- ->get()->toArray();
- //向总社采购的钱
- $purchase_map = $sale_order_id = [];
- foreach ($sale_order as $value){
- if($value['model_type'] != SalesOrder::Model_type_two){
- if(in_array($value['top_depart_id'], $bt_top_depart_id)){
- $sale_order_id[] = $value['id'];
- $this->makeData($purchase_map,$value,$value['top_depart_id']);
- }
- }else{
- $sale_order_id[] = $value['id'];
- $this->makeData($purchase_map,$value,$value['top_depart_id']);
- }
- }
- //退货的差异
- $returnExchange_map = [];
- $returnExchange = ReturnExchangeOrder::where('del_time',0)
- ->whereIn('top_depart_id',array_column($depart,'id'))
- ->where('type',ReturnExchangeOrder::Order_type)
- ->whereIn('data_id',array_unique($sale_order_id))
- ->select('top_depart_id','difference_amount')
- ->get()->toArray();
- foreach ($returnExchange as $value){
- if(isset($returnExchange_map[$value['top_depart_id']])){
- $total = bcadd($returnExchange_map[$value['top_depart_id']], $value['difference_amount'],2);
- $returnExchange_map[$value['top_depart_id']] = $total;
- }else{
- $returnExchange_map[$value['top_depart_id']] = $value['difference_amount'];
- }
- }
- foreach ($depart as $key => $value){
- $purchase_tmp = $purchase_map[$value['id']] ?? 0;
- $return_tmp = $returnExchange_map[$value['id']] ?? 0;
- $depart[$key]['total'] = bcsub($purchase_tmp, $return_tmp,2);
- $depart[$key]['index'] = $index_map[$value['id']] ?? 0;
- }
- return [true, $depart];
- }
- //某个门店 关于产品以及分类的统计 订货统计
- public function statisticsAreaDepartProduct($data,$user){
- if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
- $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
- if(empty($data['top_depart_id'])) return [false, '请选择门店'];
- //总社id
- $head = $user['head']['id'] ?? 0;
- //当前门店
- $current_top_depart_id = $this->getMyTopDepart($user);
- //特殊的门店
- $setting = Setting::where('setting_name','bt_top_depart_id')->first();
- $bt_top_depart_id = $setting['setting_value'] ?? [];
- $bt_top_depart_id = json_decode($bt_top_depart_id,true);
- //当前门店是否在设置门店中
- $bool = in_array($current_top_depart_id,$bt_top_depart_id);
- //销售订单类型
- $model_type = 0;
- if(! empty($data['model_type'])) $model_type = $data['model_type'];
- //时间 =》 钱
- $purchase_map1 = $this->getTime($return);
- //产品 =》 数量
- $purchase_category_map = $purchase_map = [];
- $sale_order = SalesOrder::where("del_time",0)
- ->where('top_depart_id',$data['top_depart_id'])
- ->where('crt_time','>=',$return[0])
- ->where('crt_time','<=',$return[1])
- ->when(! $bool, function ($query) use ($current_top_depart_id) {
- return $query->where('top_depart_id', $current_top_depart_id);
- })
- ->when(! empty($model_type), function ($query) use ($model_type) {
- return $query->where('model_type',$model_type);
- })
- ->select('id','crt_time')
- ->get()->toArray();
- $purchase_for_time = array_column($sale_order,'crt_time','id');
- $sale_order_id = array_column($sale_order,'id');
- $purchase_product = SalesOrderProductInfo::where("del_time",0)
- ->whereIn('sales_order_id',$sale_order_id)
- ->select('sales_order_id','product_id','number','price','final_amount','sports_bag_id')
- ->get()->toArray();
- //退换货
- list($returnExchange_map,$returnExchange_map_2) = $this->returnExchange($data,$sale_order_id);
- //产品
- $product_list = Product::whereIn('id',array_unique(array_merge_recursive(array_column($purchase_product,'product_id'),array_keys($returnExchange_map,'product_id'))))
- ->select('id','product_category','title','code')
- ->get()->toArray();
- $product_list_map = array_column($product_list,null,'id');
- $category_return = ProductCategory::where('del_time',0)
- ->where('parent_id',0)
- ->pluck('title','id')
- ->toArray();
- foreach ($purchase_product as $value){
- if($value['sports_bag_id'] > 0){
- $money = $value['final_amount'];
- }elseif($value['final_amount'] > 0){
- $money = $value['final_amount'];
- }else{
- $money = bcmul($value['price'],$value['number'],2);
- }
- $crt_time = $purchase_for_time[$value['sales_order_id']];
- $crt_time = date("Y-m-d",$crt_time);
- //产品信息
- $product_tmp = $product_list_map[$value['product_id']] ?? [];
- $time_money = 0;
- if(isset($returnExchange_map_2[$crt_time])) {
- $time_money = $returnExchange_map_2[$crt_time] ?? 0;
- unset($returnExchange_map_2[$crt_time]);
- }
- //钱
- if(isset($purchase_map1[$crt_time])){
- $time_total = bcadd($purchase_map1[$crt_time]['total'],$money,2);
- $time_total = bcsub($time_total,$time_money,2);
- $purchase_map1[$crt_time]['total'] = $time_total;
- }
- $return_number = $return_total = 0;
- $return_category_number = $return_category_total = 0;
- if(isset($returnExchange_map[$value['product_id']])){
- $tmp = $returnExchange_map[$value['product_id']];
- $return_number = bcsub($value['number'], $tmp['number'],2);
- $return_total = bcsub($money, $tmp['total'],2);
- $return_category_number = $return_number;
- $return_category_total = $return_total;
- }
- //-------根据产品
- //数量
- if(isset($purchase_map[$value['product_id']])){
- $tmp_number = bcadd($purchase_map[$value['product_id']]['number'], $value['number'],2);
- $tmp_money = bcadd($purchase_map[$value['product_id']]['total'], $money,2);
- $purchase_map[$value['product_id']]['number'] = $tmp_number;
- $purchase_map[$value['product_id']]['total'] = $tmp_money;
- }else{
- //减去退换货
- $number = bcsub($value['number'], $return_number,2);
- $total = bcsub($money, $return_total,2);
- $purchase_map[$value['product_id']] = [
- 'title' => $product_tmp['title'] . "(" . $product_tmp['code'] .")",
- 'number' => $number,
- 'total' => $total
- ];
- }
- //-------根据产品
- //-------根据产品大类
- //数量
- $category_tmp = json_decode($product_tmp['product_category']);
- $category_tmp = min($category_tmp);
- //退换货
- $number_2 = bcsub($value['number'], $return_category_number,2);
- $total_2 = bcsub($money, $return_category_total,2);
- if(isset($purchase_category_map[$category_tmp])){
- $tmp_number = bcadd($purchase_map[$value['product_id']]['number'], $value['number'],2);
- $tmp_number = bcsub($tmp_number,$number_2,2);
- $tmp_money = bcadd($purchase_map[$value['product_id']]['total'], $money,2);
- $tmp_money = bcsub($tmp_money,$total_2,2);
- $purchase_category_map[$category_tmp]['number'] = $tmp_number;
- $purchase_category_map[$category_tmp]['total'] = $tmp_money;
- }else{
- $num = bcsub($value['number'],$number_2,2);
- $tol = bcsub($money,$total_2,2);
- $purchase_category_map[$category_tmp] = [
- 'number' => $num,
- 'title' => $category_return[$category_tmp] ?? "",
- 'total' => $tol
- ];
- }
- }
- return [true, ['money' => array_values($purchase_map1), 'product_num' => array_values($purchase_map), 'category_num' => array_values($purchase_category_map)]];
- }
- public function getTime($data){
- $startTimestamp = $data[0]; // 起始时间戳
- $endTimestamp = $data[1]; // 结束时间戳
- // 创建 DateTime 对象
- $startDate = new \DateTime();
- $endDate = new \DateTime();
- $startDate->setTimestamp($startTimestamp);
- $endDate->setTimestamp($endTimestamp);
- // 创建 DatePeriod 对象
- $interval = new \DateInterval('P1D'); // 每天的间隔
- $dateRange = new \DatePeriod($startDate, $interval, $endDate);
- // 遍历日期范围并输出每个日期
- $return = [];
- foreach ($dateRange as $date) {
- $day = $date->format('Y-m-d');
- $return[$day] = [
- 'day' => $day,
- 'total' => 0
- ];
- }
- return $return;
- }
- public function returnExchange($data,$sale_order_id){
- $returnExchange_map = $returnExchange_map_2 = [];
- $returnExchange = ReturnExchangeOrder::where('del_time',0)
- ->where('type',ReturnExchangeOrder::Order_type)
- ->whereIn('data_id', $sale_order_id)
- ->select('id')
- ->get()->toArray();
- $returnExchange_product = ReturnExchangeOrderProductInfo::where("del_time",0)
- ->whereIn('return_exchange_id',array_column($returnExchange,'id'))
- ->select('return_exchange_id','product_id','number','return_exchange_price as price','crt_time')
- ->get()->toArray();
- foreach ($returnExchange_product as $value){
- $money = bcmul($value['price'],$value['number'],2);
- if(isset($returnExchange_map[$value['product_id']])){
- $tmp_money = bcadd($returnExchange_map[$value['product_id']]['total'], $money,2);
- $tmp_number = bcadd($returnExchange_map[$value['product_id']]['number'], $value['number'],2);
- $returnExchange_map[$value['product_id']] = [
- 'number' => $tmp_number,
- 'total' => $tmp_money
- ];
- }else{
- $returnExchange_map[$value['product_id']] = [
- 'number' => $value['number'],
- 'total' => $money
- ];
- }
- $crt_time = date("Y-m-d",$value['crt_time']);
- if(isset($returnExchange_map_2[$crt_time])){
- $tmp_money_2 = bcadd($returnExchange_map_2[$crt_time], $money,2);
- $returnExchange_map_2[$crt_time] = $tmp_money_2;
- }else{
- $returnExchange_map_2[$crt_time] = $money;
- }
- }
- return [$returnExchange_map,$returnExchange_map_2];
- }
- //线下订单
- public function statisticsModelTypeOne($data,$user){
- if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
- $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
- if(empty($data['x'])) return [false, '请选择X轴维度'];
- //总社id
- $head = $user['head']['id'] ?? 0;
- //当前门店
- $current_top_depart_id = $this->getMyTopDepart($user);
- //线下 销售订单类型
- $model_type = SalesOrder::Model_type_one;
- $sale_order = SalesOrder::where("del_time",0)
- ->where('crt_time','>=',$return[0])
- ->where('crt_time','<=',$return[1])
- ->where('model_type',$model_type)
- ->when(! empty($current_top_depart_id) && $current_top_depart_id != $head, function ($query) use ($current_top_depart_id) {
- return $query->where('top_depart_id', $current_top_depart_id);
- })
- ->select('id','top_depart_id','contract_fee','crt_id')
- ->get()->toArray();
- //合同
- $sale_order_id = array_column($sale_order,'id');
- $statistics = [];
- if($data['x'] == "crt_id"){
- $emp = Employee::whereIn('id', array_unique(array_column($sale_order,'crt_id')))
- ->pluck('emp_name','id')
- ->toArray();
- //退货的差异
- $returnExchange_map = [];
- $returnExchange = ReturnExchangeOrder::where('del_time',0)
- ->where('type',ReturnExchangeOrder::Order_type)
- ->whereIn('data_id',array_unique($sale_order_id))
- ->select('top_depart_id','difference_amount','data_id')
- ->get()->toArray();
- foreach ($returnExchange as $value){
- if(isset($returnExchange_map[$value['data_id']])){
- $total = bcadd($returnExchange_map[$value['data_id']], $value['difference_amount'],2);
- $returnExchange_map[$value['data_id']] = $total;
- }else{
- $returnExchange_map[$value['data_id']] = $value['difference_amount'];
- }
- }
- foreach ($sale_order as $value){
- $crt_name = $emp[$value['crt_id']] ?? "";
- if(! $crt_name) continue;
- $return_money = $returnExchange_map[$value['id']] ?? 0;
- $tmp = bcsub($value['contract_fee'], $return_money,2);
- if(isset($statistics[$value['crt_id']])){
- $total = bcadd($tmp,$statistics[$value['crt_id']]['total'],2);
- $statistics[$value['crt_id']]['total'] = $total;
- }else{
- $statistics[$value['crt_id']] = [
- 'title' => $crt_name,
- 'total' => $tmp,
- ];
- }
- }
- }elseif($data['x'] == "product"){
- $sales_product = SalesOrderProductInfo::where('del_time',0)
- ->whereIn('sales_order_id',$sale_order_id)
- ->select('product_id','number','retail_price','price')
- ->get()->toArray();
- $product = Product::whereIn('id',array_unique(array_column($sales_product,'product_id')))
- ->select('id','title','code')
- ->get()->toArray();
- $product_map = [];
- foreach ($product as $value){
- $product_map[$value['id']] = $value['title'] . "(" . $value['code']. ")";
- }
- //退货的差异
- $returnExchange_map = [];
- $returnExchange = ReturnExchangeOrder::where('del_time',0)
- ->where('type',ReturnExchangeOrder::Order_type)
- ->whereIn('data_id',array_unique($sale_order_id))
- ->select('id')
- ->get()->toArray();
- $return_product = ReturnExchangeOrderProductInfo::where('del_time',0)
- ->whereIn('return_exchange_id', array_column($returnExchange,'id'))
- ->select('product_id','number','return_exchange_price')
- ->get()->toArray();
- foreach ($return_product as $value){
- $tmp = bcmul($value['number'],$value['return_exchange_price'],2);
- if(isset($returnExchange_map[$value['product_id']])){
- $total = bcadd($returnExchange_map[$value['product_id']], $tmp,2);
- $returnExchange_map[$value['product_id']] = $total;
- }else{
- $returnExchange_map[$value['product_id']] = $tmp;
- }
- }
- foreach ($sales_product as $value){
- $product_tmp = $product_map[$value['product_id']] ?? "";
- if(! $product_tmp) continue;
- if($value['price'] > 0){
- $tmp = bcmul($value['price'], $value['number'],2);
- }else{
- $tmp = bcmul($value['retail_price'], $value['number'],2);
- }
- $return_tmp = 0;
- if(isset($returnExchange_map[$value['product_id']])){
- $return_tmp = $returnExchange_map[$value['product_id']] ?? 0;
- unset($returnExchange_map[$value['product_id']]);
- }
- $tmp = bcsub($tmp,$return_tmp,2);
- if(isset($statistics[$value['product_id']])){
- $total = bcadd($tmp,$statistics[$value['product_id']]['total'],2);
- $statistics[$value['product_id']]['total'] = $total;
- }else{
- $statistics[$value['product_id']] = [
- 'title' => $product_tmp,
- 'total' => $tmp,
- ];
- }
- }
- }
- foreach ($statistics as $key => $value){
- if(floatval($value['total']) <= 0) unset($statistics[$key]);
- }
- $statistics = array_values($statistics);
- usort($statistics, function($a, $b) {
- return $b['total'] - $a['total'];
- });
- return [true, $statistics];
- }
- //线上订单
- public function statisticsModelTypeFour($data,$user){
- if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
- $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
- if(empty($data['x'])) return [false, '请选择X轴维度'];
- //总社id
- $head = $user['head']['id'] ?? 0;
- //当前门店
- $current_top_depart_id = $this->getMyTopDepart($user);
- //线下 销售订单类型
- $model_type = SalesOrder::Model_type_four;
- $sale_order = SalesOrder::where("del_time",0)
- ->where('crt_time','>=',$return[0])
- ->where('crt_time','<=',$return[1])
- ->where('model_type',$model_type)
- ->when(! empty($current_top_depart_id) && $current_top_depart_id != $head, function ($query) use ($current_top_depart_id) {
- return $query->where('top_depart_id', $current_top_depart_id);
- })
- ->select('id','top_depart_id','contract_fee','plat_type')
- ->get()->toArray();
- //合同
- $sale_order_id = array_column($sale_order,'id');
- $statistics = [];
- if($data['x'] == "order_from"){
- $array = array_unique(array_column($sale_order,'plat_type'));
- $basic_map = BasicType::whereIn('id',$array)
- ->pluck('title','id')
- ->toArray();
- //退货的差异
- $returnExchange_map = [];
- $returnExchange = ReturnExchangeOrder::where('del_time',0)
- ->where('type',ReturnExchangeOrder::Order_type)
- ->whereIn('data_id',array_unique($sale_order_id))
- ->select('top_depart_id','difference_amount','data_id')
- ->get()->toArray();
- foreach ($returnExchange as $value){
- if(isset($returnExchange_map[$value['data_id']])){
- $total = bcadd($returnExchange_map[$value['data_id']], $value['difference_amount'],2);
- $returnExchange_map[$value['data_id']] = $total;
- }else{
- $returnExchange_map[$value['data_id']] = $value['difference_amount'];
- }
- }
- foreach ($sale_order as $value){
- $plat_type = $basic_map[$value['plat_type']] ?? "";
- if(! $plat_type) continue;
- $return_money = $returnExchange_map[$value['id']] ?? 0;
- $tmp = bcsub($value['contract_fee'], $return_money,2);
- if(isset($statistics[$value['plat_type']])){
- $total = bcadd($tmp,$statistics[$value['plat_type']]['total'],2);
- $statistics[$value['plat_type']]['total'] = $total;
- }else{
- $statistics[$value['plat_type']] = [
- 'title' => $plat_type,
- 'total' => $tmp,
- ];
- }
- }
- }elseif($data['x'] == "product"){
- $sales_product = SalesOrderProductInfo::where('del_time',0)
- ->whereIn('sales_order_id',$sale_order_id)
- ->select('product_id','number','retail_price','price')
- ->get()->toArray();
- $product = Product::whereIn('id',array_unique(array_column($sales_product,'product_id')))
- ->select('id','title','code')
- ->get()->toArray();
- $product_map = [];
- foreach ($product as $value){
- $product_map[$value['id']] = $value['title'] . "(" . $value['code']. ")";
- }
- //退货的差异
- $returnExchange_map = [];
- $returnExchange = ReturnExchangeOrder::where('del_time',0)
- ->where('type',ReturnExchangeOrder::Order_type)
- ->whereIn('data_id',array_unique($sale_order_id))
- ->select('id')
- ->get()->toArray();
- $return_product = ReturnExchangeOrderProductInfo::where('del_time',0)
- ->whereIn('return_exchange_id', array_column($returnExchange,'id'))
- ->select('product_id','number','return_exchange_price')
- ->get()->toArray();
- foreach ($return_product as $value){
- $tmp = bcmul($value['number'],$value['return_exchange_price'],2);
- if(isset($returnExchange_map[$value['product_id']])){
- $total = bcadd($returnExchange_map[$value['product_id']], $tmp,2);
- $returnExchange_map[$value['product_id']] = $total;
- }else{
- $returnExchange_map[$value['product_id']] = $tmp;
- }
- }
- foreach ($sales_product as $value){
- $product_tmp = $product_map[$value['product_id']] ?? "";
- if(! $product_tmp) continue;
- if($value['price'] > 0){
- $tmp = bcmul($value['price'], $value['number'],2);
- }else{
- $tmp = bcmul($value['retail_price'], $value['number'],2);
- }
- $return_tmp = 0;
- if(isset($returnExchange_map[$value['product_id']])){
- $return_tmp = $returnExchange_map[$value['product_id']] ?? 0;
- unset($returnExchange_map[$value['product_id']]);
- }
- $tmp = bcsub($tmp,$return_tmp,2);
- if(isset($statistics[$value['product_id']])){
- $total = bcadd($tmp,$statistics[$value['product_id']]['total'],2);
- $statistics[$value['product_id']]['total'] = $total;
- }else{
- $statistics[$value['product_id']] = [
- 'title' => $product_tmp,
- 'total' => $tmp,
- ];
- }
- }
- }
- foreach ($statistics as $key => $value){
- if(floatval($value['total']) <= 0) unset($statistics[$key]);
- }
- $statistics = array_values($statistics);
- usort($statistics, function($a, $b) {
- return $b['total'] - $a['total'];
- });
- return [true, $statistics];
- }
- public function statisticsJc($data,$user){
- $model = Product::ProductClear2($user,$data);
- $model = $model->where('del_time',0)
- ->select('title','id','code','depart_id','top_depart_id','product_attribute')
- ->orderby('product_attribute', 'desc')
- ->orderby('id', 'desc');
- if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
- if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
- if(isset($data['product_attribute'])) $model->where('product_attribute', $data['product_attribute']);
- if(! empty($data['product_category_id'])) $model->where('product_category_id', $data['product_category_id']);
- if(! empty($data['product_category'])) {
- $product_category = ProductCategory::where('del_time',0)
- ->where('title', 'LIKE', '%'.$data['product_category'].'%')
- ->select('id')
- ->get()->toArray();
- $model->whereIn('product_category_id',array_unique(array_column($product_category,'id')));
- }
- $list = $this->limit($model,'',$data);
- $list = $this->fillData($list,$user,$data);
- return [true, $list];
- }
- public function fillData($data, $user, $search){
- if(empty($data['data'])) return $data;
- //产品
- $product = array_column($data['data'],'id');
- //本月入库 本月出库
- list($in, $out) = $this->getThisMonthData($product,$user,$search);
- //上月结存 汇总这个月之前的出入
- list($last_in,$last_out) = $this->getLastMonthBalance($product,$user,$search);
- foreach ($data['data'] as $key => $value){
- $last_in_tmp = $last_in[$value['id']] ?? [];
- $last_in_money = $last_in_tmp['total'] ?? 0;//本月之前入的金额
- $last_in_number = $last_in_tmp['number'] ?? 0;//本月之前入的数量
- $last_out_tmp = $last_out[$value['id']] ?? [];
- $last_out_money = $last_out_tmp['total'] ?? 0;//本月之前出的金额
- $last_out_number = $last_out_tmp['number'] ?? 0;//本月之前出的数量
- //上月结存
- $last_jc_money = bcsub($last_in_money,$last_out_money,2);//结存金额
- $last_jc_number = bcsub($last_in_number,$last_out_number,2);//结存数量
- $last_jc_price = floatval($last_jc_number) ? bcdiv($last_jc_money,$last_jc_number,2) : 0;//结存单价
- $data['data'][$key]['last_jc_money'] = $last_jc_money;
- $data['data'][$key]['last_jc_number'] = $last_jc_number;
- $data['data'][$key]['last_jc_price'] = $last_jc_price;
- //本月入库
- $this_in_tmp = $in[$value['id']] ?? [];
- $this_in_money = $this_in_tmp['total'] ?? 0;//本月入的金额
- $this_in_number = $this_in_tmp['number'] ?? 0;//本月入的数量
- $this_in_price = floatval($this_in_number) ? bcdiv($this_in_money,$this_in_number,2) : 0;//本月入单价
- $data['data'][$key]['this_in_money'] = $this_in_money;
- $data['data'][$key]['this_in_number'] = $this_in_number;
- $data['data'][$key]['this_in_price'] = $this_in_price;
- //本月出库
- $this_out_tmp = $out[$value['id']] ?? [];
- $this_out_money = $this_out_tmp['total'] ?? 0;//本月出的金额
- $this_out_number = $this_out_tmp['number'] ?? 0;//本月出的数量
- //单价= (上月结存金额+本月入库金额) /上月结存数量+本月入库数量
- $amount = bcadd($last_jc_money, $this_in_money,2);
- $number = bcadd($last_jc_number, $this_in_number,2);
- $this_out_price = floatval($number) ? bcdiv($amount,$number,2) : 0;//本月出单价
- $data['data'][$key]['this_out_money'] = $this_out_money;
- $data['data'][$key]['this_out_number'] = $this_out_number;
- $data['data'][$key]['this_out_price'] = $this_out_price;
- //本月结存
- //本月结存 数量/金额=本月入库+上月结存+(-本月出库)
- $this_jc_money = bcsub(bcadd($this_in_money,$last_jc_money,2),$this_out_money,2);
- $this_jc_number = bcadd(bcadd($this_in_number,$last_jc_number,2),$this_out_number,2);
- $this_jc_price = floatval($this_jc_number) ? bcdiv($this_jc_money,$this_jc_number,2) : 0;//本月结存单价
- $data['data'][$key]['this_jc_money'] = $this_jc_money;
- $data['data'][$key]['this_jc_number'] = $this_jc_number;
- $data['data'][$key]['this_jc_price'] = $this_jc_price;
- }
- return $data;
- }
- //本月入库 出库(库存流水)
- public function getThisMonthData($product = [], $user = [], $search = []){
- $in = $out = [];
- $startStamp = strtotime(date("Y-m-01 00:00:00"));
- $endStamp = strtotime(date("Y-m-t 23:59:59"));
- //本月出和入的数据
- $model = InOutRecord::TopClear($user,$search);
- $list = $model->where('del_time',0)
- ->where('crt_time','>=',$startStamp)
- ->where('crt_time','<=',$endStamp)
- ->whereIn('product_id',$product)
- ->select('product_id','number','price')
- ->get()->toArray();
- foreach ($list as $value){
- if($value['number'] >= 0){
- $tmp_total = bcmul($value['number'], $value['price'], 2);
- if(isset($in[$value['product_id']])){
- $number = bcadd($in[$value['product_id']]['number'], $value['number'],2);
- $total = bcadd($in[$value['product_id']]['total'], $tmp_total,2);
- $in[$value['product_id']]['number'] = $number;
- $in[$value['product_id']]['total'] = $total;
- }else{
- $in[$value['product_id']] = [
- 'number' => $value['number'],
- 'total' => $tmp_total,
- ];
- }
- }else{
- $number = abs($value['number']);
- $tmp_total = bcmul($number, $value['price'], 2);
- if(isset($out[$value['product_id']])){
- $number = bcadd($out[$value['product_id']]['number'], $number,2);
- $total = bcadd($out[$value['product_id']]['total'], $tmp_total,2);
- $out[$value['product_id']]['number'] = $number;
- $out[$value['product_id']]['total'] = $total;
- }else{
- $out[$value['product_id']] = [
- 'number' => $number,
- 'total' => $tmp_total,
- ];
- }
- }
- }
- return [$in, $out];
- }
- //上月结存(汇总这个月之前的出入)(库存流水)
- public function getLastMonthBalance($product = [], $user = [], $search = []){
- //用户提交的上月结存
- $lastData = $this->getLastMonthJc($product,$user,$search);
- $startStamp = strtotime(date("Y-m-01 00:00:00"));
- $model = InOutRecord::TopClear($user,$search);
- $list = $model->where('del_time',0)
- ->where('crt_time','<',$startStamp)
- ->whereIn('product_id',$product)
- ->select('product_id','number','price','top_depart_id')
- ->get()->toArray();
- $map = [];
- foreach ($list as $val){
- $map[$val['product_id'] . $val['top_depart_id']] = $val;
- }
- //先结存表
- $in = $out = [];
- foreach ($lastData as $value){
- $key = $value['product_id'] . '|' . $value['top_depart_id'];
- if($value['number'] >= 0){
- if(isset($in[$key])){
- $number = bcadd($in[$key]['number'], $value['number'],2);
- $total = bcadd($in[$key]['total'], $value['total'],2);
- $in[$key]['number'] = $number;
- $in[$key]['total'] = $total;
- }else{
- $in[$key] = [
- 'number' => $value['number'],
- 'total' => $value['total'],
- ];
- }
- }else{
- $number = abs($value['number']);
- $total = abs($value['total']);
- if(isset($out[$key])){
- $number = bcadd($out[$key]['number'], $number,2);
- $total = bcadd($out[$key]['total'], $total,2);
- $out[$key]['number'] = $number;
- $out[$key]['total'] = $total;
- }else{
- $out[$key] = [
- 'number' => $number,
- 'total' => $total,
- ];
- }
- }
- }
- //后库存流水
- foreach ($list as $value){
- $key = $value['product_id'] . '|' . $value['top_depart_id'];
- if($value['number'] >= 0){
- if(isset($in[$key])) continue;
- $tmp_total = bcmul($value['number'], $value['price'], 2);
- if(isset($in[$value['product_id']])){
- $number = bcadd($in[$value['product_id']]['number'], $value['number'],2);
- $total = bcadd($in[$value['product_id']]['total'], $tmp_total,2);
- $in[$value['product_id']]['number'] = $number;
- $in[$value['product_id']]['total'] = $total;
- }else{
- $in[$value['product_id']] = [
- 'number' => $value['number'],
- 'total' => $tmp_total,
- ];
- }
- }else{
- if(isset($out[$key])) continue;
- $number = abs($value['number']);
- $tmp_total = bcmul($number, $value['price'], 2);
- if(isset($out[$value['product_id']])){
- $number = bcadd($out[$value['product_id']]['number'], $number,2);
- $total = bcadd($out[$value['product_id']]['total'], $tmp_total,2);
- $out[$value['product_id']]['number'] = $number;
- $out[$value['product_id']]['total'] = $total;
- }else{
- $out[$value['product_id']] = [
- 'number' => $number,
- 'total' => $tmp_total,
- ];
- }
- }
- }
- //两个数组组合过滤
- $new_in = $new_out = [];
- foreach ($in as $key => $value){
- $tmp = explode('|', $key);
- $product_id = $tmp[0];
- if(isset($new_in[$product_id])){
- $number = bcadd($new_in[$product_id]['number'], $value['number'],2);
- $total = bcadd($new_in[$product_id]['total'], $value['total'],2);
- $new_in[$product_id] = [
- 'number' => $number,
- 'total' => $total,
- ];
- }else{
- $new_in[$product_id] = [
- 'number' => $value['number'],
- 'total' => $value['total'],
- ];
- }
- }
- foreach ($out as $key => $value){
- $tmp = explode('|', $key);
- $product_id = $tmp[0];
- if(isset($new_out[$product_id])){
- $number = bcadd($new_out[$product_id]['number'], $value['number'],2);
- $total = bcadd($new_out[$product_id]['total'], $value['total'],2);
- $new_out[$product_id] = [
- 'number' => $number,
- 'total' => $total,
- ];
- }else{
- $new_out[$product_id] = [
- 'number' => $value['number'],
- 'total' => $value['total'],
- ];
- }
- }
- return [$new_in, $new_out];
- }
- public function getLastMonthJc($product = [], $user = [], $search = []){
- // 如果存在上月结存数据则使用这个
- $top_depart_id = 0;
- if(! empty($search['top_depart_id'])) $top_depart_id = $search['top_depart_id'];
- $startStamp = strtotime(date("Y-m-01 00:00:00"));
- $result = LastJc::where('del_time',0)
- ->whereIn('product_id',$product)
- ->where('time','<',$startStamp)
- ->when(! empty($top_depart_id), function ($query) use ($top_depart_id) {
- return $query->where('top_depart_id',$top_depart_id);
- })
- ->select('product_id','top_depart_id','total','number','price')
- ->orderBy('time','desc')
- ->get()->toArray();
- $return = [];
- $tmp = [];
- foreach ($result as $value){
- $key = $value['product_id'] . $value['top_depart_id'];
- if(in_array($key, $tmp)) continue;
- $return[] = $value;
- $tmp[] = $key;
- }
- return $return;
- }
- //本月入库(单据)暂时没用
- public function getThisMonthIn1($product = [], $user = [], $search = []){
- $return = [];
- $startStamp = strtotime(date("Y-m-01 00:00:00"));
- $endStamp = strtotime(date("Y-m-t 23:59:59"));
- //本月采购单
- $model = PurchaseOrder::Clear($user,$search);
- $list = $model->where('del_time',0)
- ->where('state', PurchaseOrder::STATE_Four)
- ->where('crt_time','>=',$startStamp)
- ->where('crt_time','<=',$endStamp)
- ->select('id')
- ->get()->toArray();
- if(empty($list)) return $return;
- //本月采购产品
- $purchase_product_array = [];
- $purchase_product = PurchaseOrderInfo::where('del_time',0)
- ->whereIn('product_id',$product)
- ->whereIn('purchase_order_id',array_column($list,'id'))
- ->select('product_id','number','price')
- ->get()->toArray();
- foreach ($purchase_product as $value){
- $total = bcmul($value['number'],$value['price'],2);
- if(isset($purchase_product_array[$value['product_id']])){
- $purchase_product_array[$value['product_id']]['number'] += $value['number'];
- $total_tmp = bcadd($purchase_product_array[$value['product_id']]['total'], $total, 2);
- $purchase_product_array[$value['product_id']]['total'] = $total_tmp;
- }else{
- $purchase_product_array[$value['product_id']] = [
- 'number' => $value['number'],
- 'total' => $total,
- ];
- }
- }
- //本月退货(采购)
- $model2 = ReturnExchangeOrder::Clear($user, $search);
- $return_list = $model2->where('del_time',0)
- ->where('state', ReturnExchangeOrder::State_two)
- ->where('type',ReturnExchangeOrder::Order_type2)
- ->where('crt_time','>=',$startStamp)
- ->where('crt_time','<=',$endStamp)
- ->select('id')
- ->get()->toArray();
- //本月退货产品
- $return_product_array = [];
- if(! empty($return_list)){
- $return_product = ReturnExchangeOrderProductInfo::where('del_time',0)
- ->where('return_exchange_id', array_column($return_list, 'id'))
- ->whereIn('product_id',$product)
- ->where('return_or_exchange',ReturnExchangeOrderProductInfo::type_one)
- ->select('product_id','number','return_or_exchange')
- ->get()->toArray();
- foreach ($return_product as $value){
- $total = bcmul($value['number'],$value['return_or_exchange'],2);
- if(isset($return_product_array[$value['product_id']])){
- $return_product_array[$value['product_id']]['number'] += $value['number'];
- $total_tmp = bcadd($return_product_array[$value['product_id']]['total'], $total, 2);
- $return_product_array[$value['product_id']]['total'] = $total_tmp;
- }else{
- $return_product_array[$value['product_id']] = [
- 'number' => $value['number'],
- 'total' => $total,
- ];
- }
- }
- }
- foreach ($return_product_array as $p => $n){
- $number_tmp = -$n['number'];
- $total_tmp = -$n['total'];
- $purchase_product_tmp = $purchase_product_array[$p] ?? [];
- if(empty($purchase_product_tmp)){
- $purchase_product_array[$p] = [
- 'number' => $number_tmp,
- 'total' => $total_tmp,
- ];
- }else{
- $purchase_product_array[$p]['number'] += $number_tmp;
- $total_tmp2 = bcadd($purchase_product_array[$p]['total'], $total_tmp, 2);
- $purchase_product_array[$p]['total'] += $total_tmp2;
- }
- }
- return $purchase_product_array;
- }
- //新的进销存统计通用搜索底层抽象
- public function statisticsJcNewCommonOrigin($data,$user,$field = []){
- if(! empty($data['count_month'])) {
- $startStamp = $this->changeDateToDate($data['count_month']);
- }else{
- $startStamp = strtotime(date("Y-m-01 00:00:00"));
- }
- $endTimeStamp = strtotime('first day of next month', $startStamp) - 1;
- //-------- 结存数量汇总 ---------- //
- //上月结存
- $last_month_stock = "SUM(CASE WHEN crt_time < $startStamp THEN number ELSE 0 END)";
- //本月入库
- $this_month_in = "SUM(CASE WHEN crt_time >= $startStamp AND crt_time <= $endTimeStamp AND number > 0 THEN number ELSE 0 END)";
- //本月出库
- $this_month_out = "SUM(CASE WHEN crt_time >= $startStamp AND crt_time <= $endTimeStamp AND number < 0 THEN number ELSE 0 END)";
- //本月结存
- $this_month_stock = "($last_month_stock + $this_month_in + $this_month_out)";
- //-------- 结存数量汇总 ---------- //
- //-------- 结存金额汇总 ---------- //
- $order_type = ProductAdjustment::prefix;
- //上月结存
- $last_month_stock_m = "ROUND(
- SUM(
- CASE
- WHEN crt_time < $startStamp and number <> 0 THEN (number * price)
- WHEN crt_time < $startStamp and number = 0 and order_type = '$order_type' THEN price
- ELSE 0
- END
- ),
- 2)";
- //本月入库
- $this_month_in_m = "ROUND(
- SUM(
- CASE
- WHEN crt_time >= $startStamp and crt_time <= $endTimeStamp and number > 0 THEN (number * price)
- WHEN crt_time >= $startStamp and crt_time <= $endTimeStamp and number = 0 and price >= 0 and order_type = '$order_type' THEN price
- ELSE 0
- END
- ),
- 2)";
- //本月出库
- $this_month_out_m = "ROUND(
- SUM(
- CASE
- WHEN crt_time >= $startStamp and crt_time <= $endTimeStamp and number < 0 THEN (number * price)
- WHEN crt_time >= $startStamp and crt_time <= $endTimeStamp and number = 0 and price < 0 and order_type = '$order_type' THEN price
- ELSE 0
- END
- ),
- 2)";
- //上月结存
- // $last_month_stock_m = "ROUND(SUM(CASE WHEN crt_time < $startStamp THEN (number * price) ELSE 0 END), 2)";
- // //本月入库
- // $this_month_in_m = "ROUND(SUM(CASE WHEN crt_time >= $startStamp AND number > 0 THEN (number * price) ELSE 0 END), 2)";
- // //本月出库
- // $this_month_out_m = "ROUND(SUM(CASE WHEN crt_time >= $startStamp AND number < 0 THEN (number * price) ELSE 0 END), 2)";
- //-------- 结存金额汇总 ---------- //
- if(empty($field)){
- $field = ['product_id as id',
- DB::raw("$last_month_stock as last_jc_number"),
- DB::raw("$last_month_stock_m as last_jc_money"),
- DB::raw("$this_month_in as this_in_number"),
- DB::raw("$this_month_in_m as this_in_money"),
- DB::raw("$this_month_out as this_out_number"),
- DB::raw("$this_month_out_m as this_out_money"),
- // DB::raw("$this_month_stock as this_jc_number")];
- ];
- }
- $model = InOutRecord::TopClear($user,$data);
- $model = $model->where('del_time',0)
- ->select($field)
- ->groupBy('product_id')
- ->havingRaw("$last_month_stock != 0 OR $this_month_in != 0 OR $this_month_out != 0 OR $this_month_stock != 0");
- return $model;
- }
- //新的进销存统计通用搜索
- public function statisticsJcNewCommon($data,$user, $field = []){
- $model = $this->statisticsJcNewCommonOrigin($data, $user, $field);
- if(! empty($data['product_name']) || ! empty($data['product_category_name']) || ! empty($data['code'])) {
- $id = $this->searchForProduct($data, $user);
- $model->whereIn('product_id', $id);
- }
- return $model;
- }
- //新的进销存统计
- public function statisticsJcNew($data,$user){
- $model = $this->statisticsJcNewCommon($data, $user);
- $model->orderBy('product_id','desc');
- $list = $this->limit($model,'',$data);
- $list = $this->fillDataNew($list);
- return [true, $list];
- }
- public function fillDataNew($data){
- if(empty($data['data'])) return $data;
- //产品
- $map = (new ProductService())->getProductDetail(array_column($data['data'],'id'));
- foreach ($data['data'] as $key => $value){
- //上月结存
- $last_jc_price = floatval($value['last_jc_number']) ? bcdiv($value['last_jc_money'],$value['last_jc_number'],2) : 0;//结存单价
- $data['data'][$key]['last_jc_price'] = $last_jc_price;
- //本月入库
- $this_in_price = floatval($value['this_in_number']) ? bcdiv($value['this_in_money'],$value['this_in_number'],2) : 0;//本月入单价
- $data['data'][$key]['this_in_price'] = $this_in_price;
- //本月出库
- //单价= (上月结存金额+本月入库金额) /上月结存数量+本月入库数量
- $amount = bcadd($value['last_jc_money'], $value['this_in_money'],2);
- $number = bcadd($value['last_jc_number'], $value['this_in_number'],2);
- $this_out_price = floatval($number) ? bcdiv($amount,$number,2) : 0;//本月出单价
- $data['data'][$key]['this_out_price'] = $this_out_price;
- //本月结存
- //本月结存 数量/金额=本月入库+上月结存+(-本月出库)
- $this_jc_money = bcadd(bcadd($value['this_in_money'],$value['last_jc_money'],2),$value['this_out_money'],2);
- $this_jc_number = bcadd(bcadd($value['this_in_number'],$value['last_jc_number'],2),$value['this_out_number'],2);
- $this_jc_price = floatval($this_jc_number) ? bcdiv($this_jc_money,$this_jc_number,2) : 0;//本月结存单价
- $data['data'][$key]['this_jc_money'] = $this_jc_money;
- $data['data'][$key]['this_jc_number'] = $this_jc_number;
- $data['data'][$key]['this_jc_price'] = $this_jc_price;
- //产品
- $tmp = $map[$value['id']] ?? [];
- $data['data'][$key]['title'] = $tmp['title'] ?? "";
- $data['data'][$key]['code'] = $tmp['code'] ?? "";
- //负数改为正数
- $data['data'][$key]['this_out_number'] = abs($value['this_out_number']);
- $data['data'][$key]['this_out_money'] = abs($value['this_out_money']);
- }
- return $data;
- }
- public function searchForProduct($data, $user){
- $search = [];
- if(! empty($data['product_name'])) $search['title'] = $data['product_name'];
- if(! empty($data['product_category_name'])) $search['product_category'] = $data['product_category_name'];
- if(! empty($data['code'])) $search['code'] = $data['code'];
- $model = (new ProductService())->productCommon($search, $user, ['id']);
- $product = $model->get()->toArray();
- return array_column($product,'id');
- }
- public function statisticsAreaDepartProduct222($data,$user){
- if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
- $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
- if(empty($data['top_depart_id'])) return [false, '请选择门店'];
- //向总社采购的钱
- $purchase_map = [];
- $purchase = PurchaseOrder::where('del_time',0)
- ->where('top_depart_id',$data['top_depart_id'])
- ->where('order_type',[PurchaseOrder::Order_type_three,PurchaseOrder::Order_type_four])
- ->where('crt_time','>=',$return[0])
- ->where('crt_time','<=',$return[1])
- ->select('id')
- ->get()->toArray();
- $purchase_product = PurchaseOrderInfo::where("del_time",0)
- ->whereIn('purchase_order_id',array_column($purchase,'id'))
- ->select('product_id','number','price','final_amount','sports_bag_id')
- ->get()->toArray();
- foreach ($purchase_product as $value){
- if($value['sports_bag_id'] > 0){
- $money = $value['final_amount'];
- }elseif($value['final_amount'] > 0){
- $money = $value['final_amount'];
- }else{
- $money = bcmul($value['price'],$value['number'],2);
- }
- if(isset($purchase_map[$value['product_id']])){
- $tmp_money = bcadd($purchase_map[$value['product_id']]['total'], $money,2);
- $tmp_number = bcadd($purchase_map[$value['product_id']]['number'], $value['number'],2);
- $purchase_map[$value['product_id']] = [
- 'number' => $tmp_number,
- 'total' => $tmp_money
- ];
- }else{
- $purchase_map[$value['product_id']] = [
- 'number' => $value['number'],
- 'total' => $money
- ];
- }
- }
- //退货的差异
- // $returnExchange_map = $this->returnExchange($data,$return);
- //产品
- $product_list = Product::whereIn('id',array_unique(array_merge_recursive(array_column($purchase_product,'product_id'),array_keys([],'product_id'))))
- ->select('id','product_category','title')
- ->get()->toArray();
- $product_list_map = array_column($product_list,null,'id');
- $category = $category_sum = [];
- foreach ($purchase_map as $key => $value){
- // if(isset($returnExchange_map[$key])){
- // $tmp = $returnExchange_map[$key];
- // $number = bcsub($value['number'], $tmp['number'],2);
- // $total = bcsub($value['total'], $tmp['total'],2);
- // $purchase_map[$key] = [
- // 'number' => $number,
- // 'total' => $total,
- // ];
- // }
- $product_tmp = $product_list_map[$key] ?? [];
- $purchase_map[$key]['title'] = $product_tmp['title'];
- $category_tmp = json_decode($product_tmp['product_category']);
- $category_tmp = $category_tmp[0];
- if(! in_array($category_tmp,$category)) $category[] = $category_tmp;
- if(isset($category_sum[$category_tmp])){
- $tmp_number = bcadd($category_sum[$category_tmp]['number'], $purchase_map[$key]['number'],2);
- $tmp_total = bcadd($category_sum[$category_tmp]['total'], $purchase_map[$key]['total'],2);
- $category_sum[$category_tmp] = [
- 'number' => $tmp_number,
- 'total' => $tmp_total,
- ];
- }else{
- $category_sum[$category_tmp] = [
- 'number' => $purchase_map[$key]['number'],
- 'total' => $purchase_map[$key]['total'],
- ];
- }
- }
- //根据产品大类 $category_sum
- $category_return = ProductCategory::whereIn('id',$category)
- ->select('id','title')
- ->get()->toArray();
- foreach ($category_return as $key => $value){
- $tmp = $category_sum[$value['id']] ?? [];
- $category_return[$key]['number'] = $tmp['number'];
- $category_return[$key]['total'] = $tmp['total'];
- }
- //根据产品 $purchase_map
- dd($purchase_map);
- return [true, ''];
- }
- public function statisticsProvince2222($data,$user){
- if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
- $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
- //门店设置的指标 按照区域汇总
- $index_map = DepartIndex::where('del_time',0)
- ->pluck('param_one','top_depart_id')
- ->toArray();
- //分社所属省
- $depart_map = $province_map = [];
- $depart = Depart::where('parent_id',0)
- ->where('province','<>','')
- ->where('del_time',0)
- ->select('province','id')
- ->get()->toArray();
- foreach ($depart as $value){
- $depart_map[$value['id']] = $value['province'];
- $province_map[$value['province']][] = $value['id'];
- }
- $address_map = config('address');
- $address_map = array_column($address_map,'label','value');
- //大区
- $final_address_map = [];
- foreach ($address_map as $key => $value){
- $tmp = [
- 'key' => $key,
- 'title' => $value,
- 'total' => 0,
- 'index' => 0
- ];
- $top_depart_id = $province_map[$key] ?? [];
- if(! empty($top_depart_id)){
- foreach ($top_depart_id as $depart_id){
- $index = $index_map[$depart_id] ?? 0;
- $tmp['index'] = bcadd($tmp['index'], $index,2);
- }
- }
- $final_address_map[] = $tmp;
- }
- //向总社采购的钱
- $purchase_map = [];
- $purchase = PurchaseOrder::where('del_time',0)
- ->where('order_type',[PurchaseOrder::Order_type_three,PurchaseOrder::Order_type_four])
- ->where('crt_time','>=',$return[0])
- ->where('crt_time','<=',$return[1])
- ->select('top_depart_id','purchase_total')
- ->get()->toArray();
- foreach ($purchase as $value){
- $area = $depart_map[$value['top_depart_id']] ?? 0;
- if(! $area) continue;
- if(isset($purchase_map[$area])){
- $total = bcadd($purchase_map[$area], $value['purchase_total'],2);
- $purchase_map[$area] = $total;
- }else{
- $purchase_map[$area] = $value['purchase_total'];
- }
- }
- //退货的差异
- $returnExchange_map = [];
- $returnExchange = ReturnExchangeOrder::where('del_time',0)
- ->where('type',ReturnExchangeOrder::Order_type2)
- ->where('crt_time','>=',$return[0])
- ->where('crt_time','<=',$return[1])
- ->where('crt_time','<=',$return[1])
- ->select('top_depart_id','difference_amount')
- ->get()->toArray();
- foreach ($returnExchange as $value){
- $area = $depart_map[$value['top_depart_id']] ?? 0;
- if(! $area) continue;
- if(isset($returnExchange_map[$area])){
- $total = bcadd($returnExchange_map[$area], $value['difference_amount'],2);
- $returnExchange_map[$area] = $total;
- }else{
- $returnExchange_map[$area] = $value['difference_amount'];
- }
- }
- foreach ($final_address_map as $key => $value){
- $purchase_tmp = $purchase_map[$value['key']] ?? 0;
- $return_tmp = $returnExchange_map[$value['key']] ?? 0;
- $final_address_map[$key]['total'] = bcsub($purchase_tmp, $return_tmp,2);
- }
- return [true, $final_address_map];
- }
- public function fillStatisticsBt1($data){
- if(empty($data)) return $data;
- $total = floatval(array_sum(array_column($data,'total')));
- // 设置一个最小显示阈值,比如0.5%
- $threshold = 0.5;
- // 用于存储调整后的数据
- $adjustedData = [];
- $otherTotal = 0;
- $other = [];
- foreach ($data as $value) {
- $model_type_title = SalesOrder::$model_type_title[$value['model_type']] ?? "";
- $rate = $total ? $value['total'] / $total : 0;
- // 检查是否低于阈值
- if ($rate * 100 < $threshold) {
- // 小于阈值的部分加到"其他"中
- $otherTotal += $value['total'];
- $other[] = $model_type_title;
- } else {
- // 保留更多小数位数
- $adjustedData[] = [
- 'model_type' => $value['model_type'],
- 'total' => $value['total'],
- 'model_type_title' => $model_type_title,
- 'rate' => round($rate * 100, 2)
- ];
- }
- }
- // 如果有"其他"值,则添加到数据集中
- if ($otherTotal > 0) {
- $other_title = implode(',',$other);
- $adjustedData[] = [
- 'model_type' => -1, // 自定义一个类型标识
- 'total' => $otherTotal,
- 'model_type_title' => '其他(' . $other_title . ')',
- 'rate' => round($otherTotal / $total * 100, 2)
- ];
- }
- return $adjustedData;
- }
- private function aboutTime($return){
- $day = $this->returnDays($return);
- if($day > 60) return [false, '查询时间仅支持范围区间在两月(60天)内'];
- return [true ,''];
- }
- //改装 客户模板 t9改装 不是分配的客户 是所有的 =》 点击省 展示市 只有这个有产品
- //分社 客户模板 t9改装 不是分配的客户 是所有的 =》 点击省 展示分社
- //加盟 客户模板 t9分社 不是分配的客户 是所有的 =》 点击省 展示市
- //销售 不是分配的客户 是所有的 =》 点击分社 展示销售人员(客户的负责人)
- public function customerReportStepOfFirst($data, $user){
- if(empty($data['enter_time'][0]) || empty($data['enter_time'][1])) return [false, '请选择日期'];
- $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);
- list($status, $msg) = $this->aboutTime($return);
- if(! $status) return [false, $msg];
- if(empty($data['type'])) return [false, '请选择报表查看类型'];
- $type = $data['type'];
- $result = [];
- if($type == 1){
- $type_detail = 1;
- if(! empty($data['type_detail'])) $type_detail = $data['type_detail'];
- if($type_detail == 1){
- $result = $this->customerReportStepOneForProvince($return,$type);
- }else{
- $result = $this->customerReportStepOneForProduct($return);
- }
- }elseif ($type == 2){
- $result = $this->customerReportStepOneForProvinceFs($return);
- }elseif ($type == 3){
- $result = $this->customerReportStepOneForProvince($return,$type);
- }elseif ($type == 4){
- $result = $this->customerReportStepOneForProvinceXs($return);
- }else{
- return [false ,'报表查看类型错误'];
- }
- return [true, ['list' => $result]];
- }
- private function customerReportStepOneForProduct($time){
- //基础产品数据
- $basic_all_data = BasicTypeAllUse::where('del_time',0)
- ->where('type', BasicTypeAllUse::type_one)
- ->select('title','id')
- ->get()->toArray();
- $basic_all = [];
- foreach ($basic_all_data as $value){
- $basic_all[] = [
- 'label' => $value['title'],
- 'value' => $value['id'],
- 'all_num' => 0
- ];
- }
- $start = $time[0];
- $end = $time[1];
- $model_type = Customer::Model_type_one;
- $label = "所有产品";
- $total = [
- 'label' => $label,
- 'value' => "",
- 'all_num' => 0
- ];
- $list = CustomerReport::where('del_time',0)
- ->where('enter_time','>=', $start)
- ->where('enter_time','<=', $end)
- ->where('model_type',$model_type)
- ->select('consulting_product_new')
- ->get()->toArray();
- $map = [];
- foreach ($list as $value) {
- if(isset($map[$value['consulting_product_new']])) {
- $map[$value['consulting_product_new']] += 1;
- }else{
- $map[$value['consulting_product_new']] = 1;
- }
- $total['all_num'] += 1;
- }
- foreach ($basic_all as $key => $value){
- if(isset($map[$value['value']])){
- $tmp = $map[$value['value']];
- $basic_all[$key]['all_num'] = $tmp;
- }
- }
- usort($basic_all, function($a, $b) {
- return $b['all_num'] - $a['all_num'];
- });
- array_unshift($basic_all, $total);
- return $basic_all;
- }
- private function customerReportStepOneForProvince($time, $type){
- //省市数据
- $addressData = config('address3');
- if(is_string($addressData)) $addressData = json_decode($addressData, true);
- $province = [];
- foreach ($addressData as $value){
- $province[] = [
- 'label' => $value['label'],
- 'value' => $value['value'],
- 'follow_num' => 0,
- 'all_num' => 0
- ];
- }
- $start = $time[0];
- $end = $time[1];
- $model_type = Customer::Model_type_one;
- if($type == 3) $model_type = Customer::Model_type_three;
- $label = "所有客资";
- $total = [
- 'label' => $label,
- 'value' => "",
- 'follow_num' => 0,
- 'all_num' => 0
- ];
- $list = CustomerReport::where('del_time',0)
- ->where('enter_time','>=', $start)
- ->where('enter_time','<=', $end)
- ->where('model_type',$model_type)
- ->where('province_code','<>','')
- ->select('province_code','follow_num')
- ->get()->toArray();
- $map = [];
- foreach ($list as $value) {
- if(isset($map[$value['province_code']])) {
- $tmp = bcadd($value['follow_num'], $map[$value['province_code']]['num_1']);
- $map[$value['province_code']]['num_1'] = $tmp;
- $map[$value['province_code']]['num_2'] += 1;
- }else{
- $map[$value['province_code']] = [
- 'num_1' => $value['follow_num'],
- 'num_2' => 1,
- ];
- }
- $tmp_t = bcadd($total['follow_num'], $value['follow_num']);
- $total['follow_num'] = $tmp_t;
- $total['all_num'] += 1;
- }
- foreach ($province as $key => $value){
- if(isset($map[$value['value']])){
- $tmp = $map[$value['value']];
- $province[$key]['follow_num'] = intval($tmp['num_1']);
- $province[$key]['all_num'] = $tmp['num_2'];
- }
- }
- usort($province, function($a, $b) {
- // 先比较 all_num
- if ($b['all_num'] !== $a['all_num']) {
- return $b['all_num'] - $a['all_num']; // all_num 降序
- }
- // 如果 all_num 相同,再比较 value
- return $a['value'] - $b['value']; // value 正序
- });
- array_unshift($province, $total);
- return $province;
- }
- private function customerReportStepOneForProvinceFs($time){
- //省市数据
- $addressData = config('address3');
- if(is_string($addressData)) $addressData = json_decode($addressData, true);
- $province = [];
- foreach ($addressData as $value){
- $province[] = [
- 'label' => $value['label'],
- 'value' => $value['value'],
- 'follow_num' => 0,
- 'all_num' => 0
- ];
- }
- //门店归属省
- $depart = Depart::where('del_time',0)
- ->where('parent_id',0)
- ->where('province','<>','')
- ->select('id','province as province_code')
- ->get()->toArray();
- $depart_map = [];
- foreach ($depart as $value){
- $depart_map[$value['id']] = $value['province_code'];
- }
- $start = $time[0];
- $end = $time[1];
- $model_type = Customer::Model_type_one;
- $label = "全国分社";
- $total = [
- 'label' => $label,
- 'value' => "",
- 'follow_num' => 0,
- 'all_num' => 0
- ];
- $list = CustomerReport::where('del_time',0)
- ->where('enter_time','>=', $start)
- ->where('enter_time','<=', $end)
- ->where('model_type',$model_type)
- ->where('province_code','<>','')
- ->select('province_code','follow_num','customer_id')
- ->get()->toArray();
- $detail = CustomerReportDepart::where('del_time',0)
- ->whereIn('customer_id',array_unique(array_column($list,'customer_id')))
- ->where('type',CustomerReportDepart::type_one)
- ->select('customer_id','top_depart_id')
- ->get()->toArray();
- $detail_map = [];
- foreach ($detail as $value){
- $detail_map[$value['customer_id']][] = $value['top_depart_id'];
- }
- $map = [];
- foreach ($list as $value) {
- //客资所属门店
- $c_belong_id = $detail_map[$value['customer_id']] ?? "";
- if(empty($c_belong_id)) continue;
- foreach ($c_belong_id as $id){
- //客资所属门店所属省
- $province_code = $depart_map[$id] ?? "";
- if(empty($province_code)) continue;
- if(isset($map[$province_code])) {
- $tmp = bcadd($value['follow_num'], $map[$province_code]['num_1']);
- $map[$province_code]['num_1'] = $tmp;
- $map[$province_code]['num_2'] += 1;
- }else{
- $map[$province_code] = [
- 'num_1' => $value['follow_num'],
- 'num_2' => 1,
- ];
- }
- $tmp_t = bcadd($total['follow_num'], $value['follow_num']);
- $total['follow_num'] = $tmp_t;
- $total['all_num'] += 1;
- }
- }
- foreach ($province as $key => $value){
- if(isset($map[$value['value']])){
- $tmp = $map[$value['value']];
- $province[$key]['follow_num'] = intval($tmp['num_1']);
- $province[$key]['all_num'] = $tmp['num_2'];
- }
- }
- usort($province, function($a, $b) {
- // 先比较 all_num
- if ($b['all_num'] !== $a['all_num']) {
- return $b['all_num'] - $a['all_num']; // all_num 降序
- }
- // 如果 all_num 相同,再比较 value
- return $a['value'] - $b['value']; // value 正序
- });
- array_unshift($province, $total);
- return $province;
- }
- private function customerReportStepOneForProvinceXs($time){
- //门店
- $depart = Depart::where('del_time',0)
- ->where('parent_id',0)
- ->select('id','title')
- ->get()->toArray();
- $departArray = [];
- foreach ($depart as $value){
- $departArray[]= [
- 'label' => $value['title'],
- 'value' => $value['id'],
- 'follow_num' => 0,
- 'all_num' => 0
- ];
- }
- $start = $time[0];
- $end = $time[1];
- $label = "所有门店";
- $total = [
- 'label' => $label,
- 'value' => "",
- 'follow_num' => 0,
- 'all_num' => 0
- ];
- $list = CustomerReport::where('del_time',0)
- ->where('enter_time','>=', $start)
- ->where('enter_time','<=', $end)
- ->select('follow_num','customer_id')
- ->get()->toArray();
- $detail = CustomerReportDepart::where('del_time',0)
- ->whereIn('customer_id',array_unique(array_column($list,'customer_id')))
- ->where('type',CustomerReportDepart::type_one)
- ->select('customer_id','top_depart_id')
- ->get()->toArray();
- $detail_map = [];
- foreach ($detail as $value){
- $detail_map[$value['customer_id']][] = $value['top_depart_id'];
- }
- $map = [];
- foreach ($list as $value) {
- if(isset($detail_map[$value['customer_id']])){
- //客资所属门店
- $c_belong_id = $detail_map[$value['customer_id']];
- if(empty($c_belong_id)) continue;
- foreach ($c_belong_id as $id){
- if(isset($map[$id])) {
- $tmp = bcadd($value['follow_num'], $map[$id]['num_1']);
- $map[$id]['num_1'] = $tmp;
- $map[$id]['num_2'] += 1;
- }else{
- $map[$id] = [
- 'num_1' => $value['follow_num'],
- 'num_2' => 1,
- ];
- }
- $tmp_t = bcadd($total['follow_num'], $value['follow_num']);
- $total['follow_num'] = $tmp_t;
- $total['all_num'] += 1;
- }
- }
- }
- foreach ($departArray as $key => $value){
- if(isset($map[$value['value']])){
- $tmp = $map[$value['value']];
- $departArray[$key]['follow_num'] = intval($tmp['num_1']);
- $departArray[$key]['all_num'] = $tmp['num_2'];
- }
- }
- usort($departArray, function($a, $b) {
- // 先比较 all_num
- if ($b['all_num'] !== $a['all_num']) {
- return $b['all_num'] - $a['all_num']; // all_num 降序
- }
- // 如果 all_num 相同,再比较 value
- return $a['value'] - $b['value']; // value 正序
- });
- array_unshift($departArray, $total);
- return $departArray;
- }
- public function customerReportStepOfSecond($data, $user){
- if(empty($data['enter_time'][0]) || empty($data['enter_time'][1])) return [false, '请选择日期'];
- $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);
- list($status, $msg) = $this->aboutTime($return);
- if(! $status) return [false, $msg];
- if(empty($data['type'])) return [false, '请选择报表查看类型'];
- if(empty($data['value'])) return [false, 'value不能为空'];
- $type = $data['type'];
- if($type == 1){
- list($list, $total) = $this->customerReportStepSecondForCity($return,$data,$type);
- }elseif ($type == 2){
- list($list, $total) = $this->customerReportStepSecondForFs($return, $data);
- }elseif ($type == 3){
- list($list, $total) = $this->customerReportStepSecondForCity($return,$data,$type);
- }elseif ($type == 4){
- list($list, $total) = $this->customerReportStepSecondForProvinceXs($return,$data);
- }else{
- return [false ,'报表查看类型错误'];
- }
- return [true, ['list' => $list, 'total' => $total]];
- }
- private function customerReportStepSecondForCity($time, $data, $type){
- $province_code = $data['value'];
- //市数据
- $addressData = config('address3');
- if(is_string($addressData)) $addressData = json_decode($addressData, true);
- $city = [];
- foreach ($addressData as $value){
- if($value['value'] != $province_code) continue;
- foreach ($value['children'] as $val){
- $city[] = [
- 'label' => $val['label'],
- 'value' => $val['value'],
- 'follow_num' => 0,
- 'all_num' => 0
- ];
- }
- }
- $start = $time[0];
- $end = $time[1];
- $model_type = Customer::Model_type_one;
- if($type == 3) $model_type = Customer::Model_type_three;
- // $label = "所有客资";
- // $total = [
- // 'label' => $label,
- // 'value' => "",
- // 'follow_num' => 0,
- // 'all_num' => 0
- // ];
- $total = [
- 'follow_num' => 0,
- 'all_num' => 0
- ];
- $list = CustomerReport::where('del_time',0)
- ->where('enter_time','>=', $start)
- ->where('enter_time','<=', $end)
- ->where('model_type',$model_type)
- ->where('province_code', $province_code)
- ->select('city_code','follow_num')
- ->get()->toArray();
- $map = [];
- $first_city = $city[0];
- foreach ($list as $value) {
- if(empty($value['city_code'])) $value['city_code'] = $first_city['value'];
- if(isset($map[$value['city_code']])) {
- $tmp = bcadd($value['follow_num'], $map[$value['city_code']]['num_1']);
- $map[$value['city_code']]['num_1'] = $tmp;
- $map[$value['city_code']]['num_2'] += 1;
- }else{
- $map[$value['city_code']] = [
- 'num_1' => $value['follow_num'],
- 'num_2' => 1,
- ];
- }
- $tmp_t = bcadd($total['follow_num'], $value['follow_num']);
- $total['follow_num'] = $tmp_t;
- $total['all_num'] += 1;
- }
- foreach ($city as $key => $value){
- if(isset($map[$value['value']])){
- $tmp = $map[$value['value']];
- $city[$key]['follow_num'] = intval($tmp['num_1']);
- $city[$key]['all_num'] = $tmp['num_2'];
- }
- }
- usort($city, function($a, $b) {
- // 先比较 all_num
- if ($b['all_num'] !== $a['all_num']) {
- return $b['all_num'] - $a['all_num']; // all_num 降序
- }
- // 如果 all_num 相同,再比较 value
- return $a['value'] - $b['value']; // value 正序
- });
- return [$city, $total];
- }
- private function customerReportStepSecondForFs($time, $data){
- $province_code = $data['value'];
- //省下的所有门店
- $depart = Depart::where('del_time',0)
- ->where('parent_id',0)
- ->where('province', $province_code)
- ->select('id','title')
- ->get()->toArray();
- $depart_id = array_column($depart,'id');
- $depart_array = [];
- foreach ($depart as $value){
- $depart_array[] = [
- 'label' => $value['title'],
- 'value' => $value['id'],
- 'follow_num' => 0,
- 'all_num' => 0
- ];
- }
- $start = $time[0];
- $end = $time[1];
- $model_type = Customer::Model_type_one;
- $list = CustomerReport::where('del_time',0)
- ->where('enter_time','>=', $start)
- ->where('enter_time','<=', $end)
- ->where('model_type',$model_type)
- ->where('province','<>','')
- ->select('province_code','follow_num','customer_id')
- ->get()->toArray();
- $detail = CustomerReportDepart::where('del_time',0)
- ->whereIn('customer_id',array_unique(array_column($list,'customer_id')))
- ->where('type',CustomerReportDepart::type_one)
- ->whereIn('top_depart_id',$depart_id)
- ->select('customer_id','top_depart_id')
- ->get()->toArray();
- $detail_map = [];
- foreach ($detail as $value){
- $detail_map[$value['customer_id']][] = $value['top_depart_id'];
- }
- $total = [
- 'follow_num' => 0,
- 'all_num' => 0
- ];
- $map = [];
- foreach ($list as $value) {
- if(isset($detail_map[$value['customer_id']])){
- //客资所属门店
- $c_belong_id = $detail_map[$value['customer_id']] ?? [];
- if(empty($c_belong_id)) continue;
- foreach ($c_belong_id as $id){
- if(isset($map[$id])) {
- $tmp = bcadd($value['follow_num'], $map[$id]['num_1']);
- $map[$id]['num_1'] = $tmp;
- $map[$id]['num_2'] += 1;
- }else{
- $map[$id] = [
- 'num_1' => $value['follow_num'],
- 'num_2' => 1,
- ];
- }
- $tmp_t = bcadd($total['follow_num'], $value['follow_num']);
- $total['follow_num'] = $tmp_t;
- $total['all_num'] += 1;
- }
- }
- }
- foreach ($depart_array as $key => $value){
- if(isset($map[$value['value']])){
- $tmp = $map[$value['value']];
- $depart_array[$key]['follow_num'] = intval($tmp['num_1']);
- $depart_array[$key]['all_num'] = $tmp['num_2'];
- }
- }
- usort($depart_array, function($a, $b) {
- // 先比较 all_num
- if ($b['all_num'] !== $a['all_num']) {
- return $b['all_num'] - $a['all_num']; // all_num 降序
- }
- // 如果 all_num 相同,再比较 value
- return $a['value'] - $b['value']; // value 正序
- });
- return [$depart_array, $total];
- }
- private function customerReportStepSecondForProvinceXs($time,$data){
- $top_depart_id = intval($data['value']);
- //门店下所有部门
- $all_depart_id = DB::select("
- WITH RECURSIVE sub_departments AS (
- SELECT id FROM depart WHERE parent_id = ? AND del_time = 0
- UNION ALL
- SELECT d.id FROM depart d
- INNER JOIN sub_departments s ON d.parent_id = s.id
- WHERE d.del_time = 0
- )
- SELECT id FROM sub_departments
- ", [$top_depart_id]);
- $allDepartIds = collect($all_depart_id)->pluck('id')->toArray();
- $allDepartIds[] = $top_depart_id;
- //所属于这个门店下的人
- $man = EmployeeDepartPermission::whereIn('depart_id', $allDepartIds)
- ->select('employee_id')
- ->get()->toArray();
- $man = array_column($man,'employee_id');
- $employee = Employee::where('del_time',0)
- ->whereIn('id', $man)
- ->select('id', 'emp_name', 'number')
- ->get()->toArray();
- $employee_map = array_column($employee,null,'id');
- $start = $time[0];
- $end = $time[1];
- $total = [
- 'follow_num' => 0,
- 'all_num' => 0
- ];
- $list = CustomerReport::where('del_time',0)
- ->where('enter_time','>=', $start)
- ->where('enter_time','<=', $end)
- ->select('follow_num','customer_id')
- ->get()->toArray();
- $detail = CustomerReportDepart::where('del_time',0)
- ->whereIn('customer_id',array_unique(array_column($list,'customer_id')))
- ->select('customer_id','man_id','type','top_depart_id')
- ->get()->toArray();
- $manArray = $man_flag = $detail_array = $man_map = [];
- foreach ($detail as $value){
- if($value['type'] == CustomerReportDepart::type_two){
- if(isset($employee_map[$value['man_id']])){
- if(in_array($value['man_id'], $man_flag)) continue;
- if(! in_array($value['man_id'], $man)) continue;
- $man_flag[] = $value['man_id'];
- $e_t = $employee_map[$value['man_id']];
- $manArray[] = [
- 'label' => $e_t['emp_name'],
- 'value' => $e_t['id'],
- 'follow_num' => 0,
- 'all_num' => 0
- ];
- }
- }elseif ($value['type'] == CustomerReportDepart::type_one && $value['top_depart_id'] == $top_depart_id){
- if(! in_array($value['customer_id'], $detail_array)) $detail_array[] = $value['customer_id'];
- }elseif ($value['type'] == CustomerReportDepart::type_three){
- if(isset($man_map[$value['customer_id']][$value['man_id']])){
- $man_map[$value['customer_id']][$value['man_id']] += 1;
- }else{
- $man_map[$value['customer_id']][$value['man_id']] = 1;
- }
- }
- }unset($man_flag);
- $map = [];
- foreach ($list as $value) {
- //这个客户是否属于这个门店
- if(in_array($value['customer_id'], $detail_array)){
- if(isset($man_map[$value['customer_id']])) {
- //客户由谁创建跟进记录
- $tmp = $man_map[$value['customer_id']];
- foreach ($tmp as $m_id => $val){
- if(isset($map[$m_id])) {
- $tmp = bcadd($value['follow_num'], $map[$m_id]['num_1']);
- $map[$m_id]['num_1'] = $tmp;
- $map[$m_id]['num_2'] += 1;
- }else{
- $map[$m_id] = [
- 'num_1' => $val,
- 'num_2' => 1,
- ];
- }
- $tmp_t = bcadd($total['follow_num'], $val);
- $total['follow_num'] = $tmp_t;
- }
- }
- $total['all_num'] += 1;
- }
- }
- foreach ($manArray as $key => $value){
- if(isset($map[$value['value']])){
- $tmp = $map[$value['value']];
- $manArray[$key]['follow_num'] = intval($tmp['num_1']);
- $manArray[$key]['all_num'] = $tmp['num_2'];
- }
- }
- usort($manArray, function($a, $b) {
- // 先比较 all_num
- if ($b['all_num'] !== $a['all_num']) {
- return $b['all_num'] - $a['all_num']; // all_num 降序
- }
- // 如果 all_num 相同,再比较 value
- return $a['value'] - $b['value']; // value 正序
- });
- return [$manArray, $total];
- }
- public function customerReportStepOfThird($data, $user){
- if(empty($data['enter_time'][0]) || empty($data['enter_time'][1])) return [false, '请选择日期'];
- $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);
- list($status, $msg) = $this->aboutTime($return);
- if(! $status) return [false, $msg];
- if(empty($data['type'])) return [false, '请选择报表查看类型'];
- if(empty($data['value'])) return [false, 'value不能为空'];
- if(empty($data['value2'])) return [false, 'value2不能为空'];
- $type = $data['type'];
- if($type == 1){
- $result = $this->customerReportStepThirdForDetail($return,$data,$type);
- }elseif ($type == 2){
- $result = $this->customerReportStepThirdForFsDetail($return, $data);
- }elseif ($type == 3){
- $result = $this->customerReportStepThirdForDetail($return,$data,$type);
- }elseif ($type == 4){
- $result = $this->customerReportStepThirdForProvinceXsDetail($return,$data);
- }else{
- return [false ,'报表查看类型错误'];
- }
- return [true, ['list' => $result]];
- }
- private function customerReportStepThirdForDetail($time, $data, $type){
- $province_code = $data['value'];
- $city_code = $data['value2'];
- //市数据
- $addressData = config('address3');
- if(is_string($addressData)) $addressData = json_decode($addressData, true);
- $first_city = "";
- foreach ($addressData as $value){
- if($value['value'] != $province_code && ! empty($city)) continue;
- foreach ($value['children'] as $val){
- if(! empty($city)) continue;
- $first_city = $val['value'];
- }
- }
- $customer = [];
- foreach (CustomerReportDepart::type_list as $key => $value){
- $customer[] = [
- 'label' => $value,
- 'value' => $key,
- 'list' => [],
- 'count' => 0
- ];
- }
- $start = $time[0];
- $end = $time[1];
- $model_type = Customer::Model_type_one;
- if($type == 3) $model_type = Customer::Model_type_three;
- $list = CustomerReport::where('del_time',0)
- ->where('enter_time','>=', $start)
- ->where('enter_time','<=', $end)
- ->where('model_type',$model_type)
- ->where('province_code', $province_code)
- ->select('city_code','type','customer_id','title','customer_from','car_type','consulting_product_new_title','enter_time','contact_info')
- ->get()->toArray();
- $detail = CustomerReportDepart::where('del_time',0)
- ->whereIn('customer_id',array_unique(array_column($list,'customer_id')))
- ->where('type',CustomerReportDepart::type_one)
- ->select('customer_id','top_depart_id')
- ->get()->toArray();
- $detail_map = [];
- foreach ($detail as $value){
- $detail_map[$value['customer_id']][] = $value['top_depart_id'];
- }
- $depart = Depart::whereIn('id',array_unique(array_column($detail,'top_depart_id')))
- ->pluck('title','id')
- ->toArray();
- $map = [];
- foreach ($list as $value) {
- if(empty($value['city_code']) && $first_city != $city_code) continue;
- if($value['city_code'] != $city_code) continue;
- $depart_title = "";
- if(isset($detail_map[$value['customer_id']])){
- foreach ($detail_map[$value['customer_id']] as $val){
- $t = $depart[$val];
- if(! $t) continue;
- $depart_title .= $t . ',';
- }
- }
- $depart_title = rtrim($depart_title,',');
- $tmp = [
- 'depart_title' => $depart_title,
- 'title' => $value['title'],
- 'customer_from' => $value['customer_from'],
- 'car_type' => $value['car_type'],
- 'consulting_product_new_title' => $value['consulting_product_new_title'],
- 'enter_time' => $value['enter_time'] ? date("Y-m-d H:i:s", $value['enter_time']) : "",
- 'contact_info' => $value['contact_info'],
- ];
- $map[$value['type']][] = $tmp;
- }
- foreach ($customer as $key => $value){
- if(isset($map[$value['value']])){
- $tmp = $map[$value['value']];
- $customer[$key]['list'] = $tmp;
- $customer[$key]['count'] = count($tmp);
- }
- }
- return $customer;
- }
- private function customerReportStepThirdForFsDetail($time, $data){
- $province_code = $data['value'];
- $top_depart_id = $data['value2'];
- $customer = [];
- foreach (CustomerReportDepart::type_list as $key => $value){
- $customer[] = [
- 'label' => $value,
- 'value' => $key,
- 'list' => [],
- 'count' => 0
- ];
- }
- $start = $time[0];
- $end = $time[1];
- $model_type = Customer::Model_type_one;
- $list = CustomerReport::where('del_time',0)
- ->where('enter_time','>=', $start)
- ->where('enter_time','<=', $end)
- ->where('model_type',$model_type)
- ->where('province','<>','')
- ->select('type','customer_id','title','customer_from','car_type','consulting_product_new_title','enter_time','contact_info')
- ->get()->toArray();
- $detail = CustomerReportDepart::where('del_time',0)
- ->whereIn('customer_id',array_unique(array_column($list,'customer_id')))
- ->where('type',CustomerReportDepart::type_one)
- ->where('top_depart_id', $top_depart_id)
- ->select('customer_id')
- ->get()->toArray();
- $detail_array = array_column($detail,'customer_id');
- $depart = Depart::where('id',$top_depart_id)->value('title');
- $map = [];
- foreach ($list as $value) {
- if(in_array($value['customer_id'], $detail_array)){
- $tmp = [
- 'depart_title' => $depart,
- 'title' => $value['title'],
- 'customer_from' => $value['customer_from'],
- 'car_type' => $value['car_type'],
- 'consulting_product_new_title' => $value['consulting_product_new_title'],
- 'enter_time' => $value['enter_time'] ? date("Y-m-d H:i:s", $value['enter_time']) : "",
- 'contact_info' => $value['contact_info'],
- ];
- $map[$value['type']][] = $tmp;
- }
- }
- foreach ($customer as $key => $value){
- if(isset($map[$value['value']])){
- $tmp = $map[$value['value']];
- $customer[$key]['list'] = $tmp;
- $customer[$key]['count'] = count($tmp);
- }
- }
- return $customer;
- }
- private function customerReportStepThirdForProvinceXsDetail($time, $data){
- $top_depart_id = intval($data['value']);
- $man_id = $data['value2'];
- $customer = [];
- foreach (CustomerReportDepart::type_list as $key => $value){
- $customer[] = [
- 'label' => $value,
- 'value' => $key,
- 'list' => [],
- 'count' => 0
- ];
- }
- //门店下所有部门
- $all_depart_id = DB::select("
- WITH RECURSIVE sub_departments AS (
- SELECT id FROM depart WHERE parent_id = ? AND del_time = 0
- UNION ALL
- SELECT d.id FROM depart d
- INNER JOIN sub_departments s ON d.parent_id = s.id
- WHERE d.del_time = 0
- )
- SELECT id FROM sub_departments
- ", [$top_depart_id]);
- $allDepartIds = collect($all_depart_id)->pluck('id')->toArray();
- $allDepartIds[] = $top_depart_id;
- //所属于这个门店下的人
- $exists = EmployeeDepartPermission::whereIn('depart_id', $allDepartIds)
- ->where('employee_id',$man_id)
- ->exists();
- if(! $exists) return [];
- $start = $time[0];
- $end = $time[1];
- $list = CustomerReport::where('del_time',0)
- ->where('enter_time','>=', $start)
- ->where('enter_time','<=', $end)
- ->select('type','customer_id','title','customer_from','car_type','consulting_product_new_title','enter_time','contact_info')
- ->get()->toArray();
- $detail = CustomerReportDepart::where('del_time',0)
- ->whereIn('customer_id',array_unique(array_column($list,'customer_id')))
- ->select('customer_id','man_id','type','top_depart_id')
- ->get()->toArray();
- $detail_map = [];
- $detail_array = $man_map = [];
- foreach ($detail as $value){
- if($value['type'] == CustomerReportDepart::type_two){
- }elseif ($value['type'] == CustomerReportDepart::type_one && $value['top_depart_id'] == $top_depart_id){
- $detail_map[$value['customer_id']][] = $value['top_depart_id'];
- if(! in_array($value['customer_id'], $detail_array)) $detail_array[] = $value['customer_id'];
- }elseif ($value['type'] == CustomerReportDepart::type_three){
- if($value['man_id'] == $man_id) $man_map[$value['customer_id']] = 1;
- }
- }
- $depart = Depart::whereIn('id',array_unique(array_column($detail,'top_depart_id')))
- ->pluck('title','id')
- ->toArray();
- $map = [];
- foreach ($list as $value) {
- //这个客户是否属于当前这个门店
- if(in_array($value['customer_id'], $detail_array)){
- $depart_title = "";
- if(isset($detail_map[$value['customer_id']])){
- foreach ($detail_map[$value['customer_id']] as $val){
- $t = $depart[$val];
- if(! $t) continue;
- $depart_title .= $t . ',';
- }
- }
- $depart_title = rtrim($depart_title,',');
- if(isset($man_map[$value['customer_id']])) {
- $tmp = [
- 'depart_title' => $depart_title,
- 'title' => $value['title'],
- 'customer_from' => $value['customer_from'],
- 'car_type' => $value['car_type'],
- 'consulting_product_new_title' => $value['consulting_product_new_title'],
- 'enter_time' => $value['enter_time'] ? date("Y-m-d H:i:s", $value['enter_time']) : "",
- 'contact_info' => $value['contact_info'],
- ];
- $map[$value['type']][] = $tmp;
- }
- }
- }
- foreach ($customer as $key => $value){
- if(isset($map[$value['value']])){
- $tmp = $map[$value['value']];
- $customer[$key]['list'] = $tmp;
- $customer[$key]['count'] = count($tmp);
- }
- }
- return $customer;
- }
- //门店零售:统计各个门店所有的订单总金额
- //私域营销:统计T9品牌中心和杭州旗舰店的营销部部门下所有的人员的线下订单成交金额的总计
- //线上销售:来自T9品牌中心所有的线上订单的合同金额总计
- //门店业绩:来自分社采购单转成的订单的合同金额汇总
- //客资业绩:导入的数据
- public function saleReportStepOfFirst($data,$user){
- if(empty($data['enter_time'][0]) || empty($data['enter_time'][1])) return [false, '请选择日期'];
- $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);
- list($status, $msg) = $this->aboutTime($return);
- if(! $status) return [false, $msg];
- $result = $this->saleReportStepOfFirstStart($return,$user);
- return [true, ['list' => $result]];
- }
- private function saleReportStepOfFirstStart($time,$user){
- $result = $type = [];
- foreach (SalesOrderReport::$all_list as $key => $value){
- $result[] = [
- 'label' => $value,
- 'value' => $key,
- 'all' => ""
- ];
- $type[$key] = 0;
- }
- $setting = Setting::where('setting_name','bt_top_depart_id')->first();
- $bt_top_depart_id = $setting['setting_value'] ?? [];
- $bt_top_depart_id = json_decode($bt_top_depart_id,true);
- $id = [];
- $depart_list = Depart::where('del_time',0)->select('id','parent_id','title')->get()->toArray();
- foreach ($depart_list as $value){
- if(in_array($value['parent_id'], $bt_top_depart_id) && $value['title'] == "营销部"){
- $id[] = $value['id'];
- }
- }
- // 查找所有部门id
- $all_depart_id = [];
- foreach ($id as $value){
- $childIds = $this->findChildIds($value, $depart_list);
- $all_depart_id_tmp = array_merge($childIds, [$value]);
- $all_depart_id = array_merge($all_depart_id, $all_depart_id_tmp);
- }
- // 获取部门下的人
- $employee = EmployeeDepartPermission::whereIn('depart_id', $all_depart_id)
- ->select('employee_id')
- ->get()->toArray();
- $employee = array_unique(array_column($employee,'employee_id'));
- $start = $time[0];
- $end = $time[1];
- $list = SalesOrderReport::where('del_time',0)
- ->where('crt_time','>=', $start)
- ->where('crt_time','<=', $end)
- ->get()->toArray();
- $head = $user['head']['id'] ?? 0;
- foreach ($list as $value){
- //门店零售
- $type[SalesOrderReport::type_one] = bcadd($type[SalesOrderReport::type_one], $value['contract_fee'],2);
- //私域营销
- $bool = in_array($value['top_depart_id'], $bt_top_depart_id);
- if($bool && in_array($value['crt_id'], $employee) && $value['model_type'] == SalesOrder::Model_type_one) $type[SalesOrderReport::type_two] = bcadd($type[SalesOrderReport::type_two], $value['contract_fee'],2);
- //线上销售
- if($value['top_depart_id'] == $head && $value['model_type'] == SalesOrder::Model_type_four) $type[SalesOrderReport::type_three] = bcadd($type[SalesOrderReport::type_three], $value['contract_fee'],2);
- //门店业绩
- if($value['from_top_depart_id'] > 0 && $value['model_type'] == SalesOrder::Model_type_two) $type[SalesOrderReport::type_four] = bcadd($type[SalesOrderReport::type_four], $value['contract_fee'],2);
- }
- //客资业绩
- $start_time = strtotime(date("Y-m-01 00:00:00", $start));
- $type_five = CustomerPerFormance::where('del_time',0)
- ->where('crt_time','>=', $start_time)
- ->where('crt_time','<=', $end)
- ->select(DB::raw('sum(current_month_payment) as current_month_payment'))
- ->first()->toArray();
- $type[SalesOrderReport::type_five] = $type_five['current_month_payment'] ?? 0;
- foreach ($result as $key => $value){
- if(isset($type[$value['value']])){
- $result[$key]['all'] = $type[$value['value']];
- }
- }
- return $result;
- }
- public function saleReportStepOfSecond($data, $user){
- if(empty($data['enter_time'][0]) || empty($data['enter_time'][1])) return [false, '请选择日期'];
- $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);
- list($status, $msg) = $this->aboutTime($return);
- if(! $status) return [false, $msg];
- if(empty($data['type'])) return [false, '请选择报表查看类型'];
- $type = $data['type'];
- if($type == 1){
- $result = $this->saleReportStepOfSecondTypeOne($return);
- }elseif ($type == 2){
- $result = $this->saleReportStepOfSecondTypeTwo($return);
- }elseif ($type == 3){
- $result = $this->saleReportStepOfSecondTypeThree($return, $user);
- }elseif ($type == 4){
- $result = $this->saleReportStepOfSecondTypeFour($return, $user);
- }elseif ($type == 5){
- $result = $this->saleReportStepOfSecondTypeFive($return);
- }else{
- return [false ,'报表查看类型错误'];
- }
- return [true, ['list' => $result]];
- }
- private function saleReportStepOfSecondTypeOne($time){
- //大区
- $area = [];
- foreach (Depart::$area as $key => $value){
- $area[] = [
- 'label' => $value,
- 'value' => $key,
- 'receipt' => 0,
- 'all' => 0
- ];
- }
- $label = "全部";
- $total = [
- 'label' => $label,
- 'value' => "",
- 'receipt' => 0,
- 'all' => 0
- ];
- $start = $time[0];
- $end = $time[1];
- $list = SalesOrderReport::where('del_time',0)
- ->where('crt_time','>=', $start)
- ->where('crt_time','<=', $end)
- ->get()->toArray();
- $map = [];
- foreach ($list as $value) {
- if(isset($map[$value['area']])) {
- $tmp = bcadd($value['receipt'], $map[$value['area']]['num_1']);
- $tmp2 = bcadd($value['contract_fee'], $map[$value['area']]['num_2']);
- $map[$value['area']]['num_1'] = $tmp;
- $map[$value['area']]['num_2'] = $tmp2;
- }else{
- $map[$value['area']] = [
- 'num_1' => $value['receipt'],
- 'num_2' => $value['contract_fee'],
- ];
- }
- $tmp_t = bcadd($total['receipt'], $value['receipt'],2);
- $tmp_t1 = bcadd($total['all'], $value['contract_fee'],2);
- $total['receipt'] = $tmp_t;
- $total['all'] = $tmp_t1;
- }
- foreach ($area as $key => $value){
- if(isset($map[$value['value']])){
- $tmp = $map[$value['value']];
- $area[$key]['receipt'] = $tmp['num_1'];
- $area[$key]['all'] = $tmp['num_2'];
- }
- }
- usort($area, function($a, $b) {
- // 先比较 all_num
- if ($b['all'] !== $a['all']) {
- return $b['all'] - $a['all']; // all_num 降序
- }
- // 如果 all_num 相同,再比较 value
- return $a['receipt'] - $b['receipt']; // value 正序
- });
- array_unshift($area, $total);
- return $area;
- }
- private function saleReportStepOfSecondTypeTwo($time){
- $setting = Setting::where('setting_name','bt_top_depart_id')->first();
- $bt_top_depart_id = $setting['setting_value'] ?? [];
- $bt_top_depart_id = json_decode($bt_top_depart_id,true);
- $id = [];
- $depart_list = Depart::where('del_time',0)->select('id','parent_id','title')->get()->toArray();
- foreach ($depart_list as $value){
- if(in_array($value['parent_id'], $bt_top_depart_id) && $value['title'] == "营销部"){
- $id[] = $value['id'];
- }
- }
- // 查找所有部门id
- $all_depart_id = [];
- foreach ($id as $value){
- $childIds = $this->findChildIds($value, $depart_list);
- $all_depart_id_tmp = array_merge($childIds, [$value]);
- $all_depart_id = array_merge($all_depart_id, $all_depart_id_tmp);
- }
- // 获取部门下的人
- $employee = EmployeeDepartPermission::whereIn('depart_id', $all_depart_id)
- ->select('employee_id')
- ->get()->toArray();
- $employee = array_unique(array_column($employee,'employee_id'));
- $empList = Employee::whereIn('id', $employee)
- ->select('id','emp_name as title')
- ->get()->toArray();
- $man = [];
- foreach ($empList as $value){
- $man[] = [
- 'label' => $value['title'],
- 'value' => $value['id'],
- 'receipt' => 0,
- 'all' => 0,
- 'goal' => 0,
- ];
- }
- $start = $time[0];
- $end = $time[1];
- $list = SalesOrderReport::where('del_time',0)
- ->where('crt_time','>=', $start)
- ->where('crt_time','<=', $end)
- ->whereIn('top_depart_id', $bt_top_depart_id)
- ->where('model_type',SalesOrder::Model_type_one)
- ->whereIn('crt_id',$employee)
- ->get()->toArray();
- $map = [];
- foreach ($list as $value) {
- if(isset($map[$value['crt_id']])) {
- $tmp = bcadd($value['receipt'], $map[$value['crt_id']]['num_1']);
- $tmp2 = bcadd($value['contract_fee'], $map[$value['crt_id']]['num_2']);
- $map[$value['crt_id']]['num_1'] = $tmp;
- $map[$value['crt_id']]['num_2'] = $tmp2;
- }else{
- $map[$value['crt_id']] = [
- 'num_1' => $value['receipt'],
- 'num_2' => $value['contract_fee'],
- ];
- }
- }
- $start_time = strtotime(date("Y-m-01 00:00:00", $start));
- $month = MonthlyPerFormance::where('del_time',0)
- ->where('crt_time',">=",$start_time)
- ->where('crt_time','<=',$end)
- ->select('employee_id','sales_index')
- ->get()->toArray();
- $month_map = [];
- foreach ($month as $value){
- $month_map[$value['employee_id']] = $value['sales_index'];
- }
- foreach ($man as $key => $value){
- if(isset($map[$value['value']])){
- $tmp = $map[$value['value']];
- $man[$key]['receipt'] = $tmp['num_1'];
- $man[$key]['all'] = $tmp['num_2'];
- }
- $man[$key]['goal'] = $month_map[$value['value']] ?? 0;
- }
- usort($man, function($a, $b) {
- // 先比较 all_num
- if ($b['all'] !== $a['all']) {
- return $b['all'] - $a['all']; // all_num 降序
- }
- // 如果 all_num 相同,再比较 value
- return $a['receipt'] - $b['receipt']; // value 正序
- });
- return $man;
- }
- private function saleReportStepOfSecondTypeThree($time, $user){
- $head = $user['head']['id'] ?? 0;
- $list = BasicType::where('del_time',0)
- ->where('type',24)
- ->where('top_depart_id', $head)
- ->select('title')
- ->get()->toArray();
- //大区
- $area = [];
- foreach ($list as $value){
- $area[] = [
- 'label' => $value['title'],
- 'value' => $value['title'],
- 'all' => 0
- ];
- }
- $label = "无平台来源";
- $start = $time[0];
- $end = $time[1];
- $list = SalesOrderReport::where('del_time',0)
- ->where('crt_time','>=', $start)
- ->where('crt_time','<=', $end)
- ->where('model_type', SalesOrder::Model_type_four)
- ->where('top_depart_id', $head)
- ->get()->toArray();
- $map = [];
- foreach ($list as $value) {
- if(empty($value['plat_type_title'])) $value['plat_type_title'] = $label;
- if(isset($map[$value['plat_type_title']])) {
- $tmp = bcadd($value['contract_fee'], $map[$value['plat_type_title']]['num_1']);
- $map[$value['plat_type_title']]['num_1'] = $tmp;
- }else{
- $map[$value['plat_type_title']] = [
- 'num_1' => $value['contract_fee'],
- ];
- }
- }
- if(isset($map[$label])){
- $area[] = [
- 'label' => $label,
- 'value' => $label,
- 'all' => 0
- ];
- }
- foreach ($area as $key => $value){
- if(isset($map[$value['value']])){
- $tmp = $map[$value['value']];
- $area[$key]['all'] = $tmp['num_1'];
- }
- }
- usort($area, function($a, $b) {
- // 先比较 all_num
- if ($b['all'] !== $a['all']) {
- return $b['all'] - $a['all']; // all_num 降序
- }
- });
- return $area;
- }
- private function saleReportStepOfSecondTypeFour($time, $user){
- $head = $user['head']['id'] ?? 0;
- //大区
- $area = [];
- foreach (Depart::$area as $key => $value){
- $area[] = [
- 'label' => $value,
- 'value' => $key,
- 'receipt' => 0,
- 'all' => 0
- ];
- }
- $label = "全部";
- $total = [
- 'label' => $label,
- 'value' => "",
- 'receipt' => 0,
- 'all' => 0
- ];
- $start = $time[0];
- $end = $time[1];
- $list = SalesOrderReport::where('del_time',0)
- ->where('crt_time','>=', $start)
- ->where('crt_time','<=', $end)
- ->where('model_type', SalesOrder::Model_type_two)
- ->where('from_top_depart_id', '>', 0)
- ->get()->toArray();
- $map = [];
- foreach ($list as $value) {
- if(isset($map[$value['area']])) {
- $tmp = bcadd($value['receipt'], $map[$value['area']]['num_1']);
- $tmp2 = bcadd($value['contract_fee'], $map[$value['area']]['num_2']);
- $map[$value['area']]['num_1'] = $tmp;
- $map[$value['area']]['num_2'] = $tmp2;
- }else{
- $map[$value['area']] = [
- 'num_1' => $value['receipt'],
- 'num_2' => $value['contract_fee'],
- ];
- }
- $tmp_t = bcadd($total['receipt'], $value['receipt'],2);
- $tmp_t1 = bcadd($total['all'], $value['contract_fee'],2);
- $total['receipt'] = $tmp_t;
- $total['all'] = $tmp_t1;
- }
- foreach ($area as $key => $value){
- if(isset($map[$value['value']])){
- $tmp = $map[$value['value']];
- $area[$key]['receipt'] = $tmp['num_1'];
- $area[$key]['all'] = $tmp['num_2'];
- }
- }
- usort($area, function($a, $b) {
- // 先比较 all_num
- if ($b['all'] !== $a['all']) {
- return $b['all'] - $a['all']; // all_num 降序
- }
- // 如果 all_num 相同,再比较 value
- return $a['receipt'] - $b['receipt']; // value 正序
- });
- array_unshift($area, $total);
- return $area;
- }
- private function saleReportStepOfSecondTypeFive($time){
- $label = "全部";
- $total = [
- 'label' => $label,
- 'value' => "",
- 'all' => 0
- ];
- $start = $time[0];
- $end = $time[1];
- $start_time = strtotime(date("Y-m-01 00:00:00", $start));
- $list = CustomerPerFormance::where('del_time',0)
- ->where('crt_time','>=', $start_time)
- ->where('crt_time','<=', $end)
- ->get()->toArray();
- $map = [];
- foreach ($list as $value) {
- if(isset($map[$value['area']])) {
- $tmp = bcadd($value['current_month_payment'], $map[$value['area']]['all']);
- $map[$value['area']]['all'] = $tmp;
- }else{
- $map[$value['area']] = [
- 'label' => $value['area'],
- 'value' => $value['area'],
- 'all' => $value['current_month_payment']
- ];
- }
- $tmp_t1 = bcadd($total['all'], $value['current_month_payment'],2);
- $total['all'] = $tmp_t1;
- }
- $map = array_values($map);
- usort($map, function($a, $b) {
- // 先比较 all_num
- if ($b['all'] !== $a['all']) {
- return $b['all'] - $a['all']; // all_num 降序
- }
- });
- array_unshift($map, $total);
- return $map;
- }
- public function saleReportStepOfThird($data, $user){
- if(empty($data['enter_time'][0]) || empty($data['enter_time'][1])) return [false, '请选择日期'];
- $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);
- list($status, $msg) = $this->aboutTime($return);
- if(! $status) return [false, $msg];
- if(empty($data['type'])) return [false, '请选择报表查看类型'];
- if(empty($data['value'])) return [false, 'value不能为空'];
- $type = $data['type'];
- if($type == 1){
- $result = $this->saleReportStepOfThirdTypeOne($return, $data);
- }elseif ($type == 2){
- $result = $this->saleReportStepOfThirdTypeTwo($return, $data);
- }elseif ($type == 3){
- $result = $this->saleReportStepOfThirdTypeThree($return, $data, $user);
- }elseif ($type == 4){
- $result = $this->saleReportStepOfThirdTypeFour($return, $data, $user);
- }elseif ($type == 5){
- $result = $this->saleReportStepOfThreeTypeFive($return,$data);
- }else{
- return [false ,'报表查看类型错误'];
- }
- return [true, ['list' => $result]];
- }
- private function saleReportStepOfThirdTypeOne($time,$data){
- $area_value = $data['value'];
- $depart = Depart::where('del_time',0)
- ->where('parent_id',0)
- ->where('area',$area_value)
- ->select('id','title')
- ->get()->toArray();
- //大区
- $area = [];
- foreach ($depart as $value){
- $area[] = [
- 'label' => $value['title'],
- 'value' => $value['id'],
- 'receipt' => 0,
- 'all' => 0
- ];
- }
- $label = "全部";
- $total = [
- 'label' => $label,
- 'value' => "",
- 'receipt' => 0,
- 'all' => 0
- ];
- $start = $time[0];
- $end = $time[1];
- $list = SalesOrderReport::where('del_time',0)
- ->where('crt_time','>=', $start)
- ->where('crt_time','<=', $end)
- ->where('area',$area_value)
- ->get()->toArray();
- $map = [];
- foreach ($list as $value) {
- if(isset($map[$value['top_depart_id']])) {
- $tmp = bcadd($value['receipt'], $map[$value['top_depart_id']]['num_1']);
- $tmp2 = bcadd($value['contract_fee'], $map[$value['top_depart_id']]['num_2']);
- $map[$value['top_depart_id']]['num_1'] = $tmp;
- $map[$value['top_depart_id']]['num_2'] = $tmp2;
- }else{
- $map[$value['top_depart_id']] = [
- 'num_1' => $value['receipt'],
- 'num_2' => $value['contract_fee'],
- ];
- }
- $tmp_t = bcadd($total['receipt'], $value['receipt'],2);
- $tmp_t1 = bcadd($total['all'], $value['contract_fee'],2);
- $total['receipt'] = $tmp_t;
- $total['all'] = $tmp_t1;
- }
- foreach ($area as $key => $value){
- if(isset($map[$value['value']])){
- $tmp = $map[$value['value']];
- $area[$key]['receipt'] = $tmp['num_1'];
- $area[$key]['all'] = $tmp['num_2'];
- }
- }
- usort($area, function($a, $b) {
- // 先比较 all_num
- if ($b['all'] !== $a['all']) {
- return $b['all'] - $a['all']; // all_num 降序
- }
- // 如果 all_num 相同,再比较 value
- return $a['receipt'] - $b['receipt']; // value 正序
- });
- array_unshift($area, $total);
- return $area;
- }
- private function saleReportStepOfThirdTypeTwo($time,$data){
- $man_value = $data['value'];
- $area = [];
- foreach (SalesOrderReport::$all_state_list as $key => $value){
- $area[] = [
- 'label' => $value,
- 'value' => $key,
- 'list' => [],
- 'count' => 0
- ];
- }
- $setting = Setting::where('setting_name','bt_top_depart_id')->first();
- $bt_top_depart_id = $setting['setting_value'] ?? [];
- $bt_top_depart_id = json_decode($bt_top_depart_id,true);
- $start = $time[0];
- $end = $time[1];
- $list = SalesOrderReport::where('del_time',0)
- ->where('crt_time','>=', $start)
- ->where('crt_time','<=', $end)
- ->whereIn('top_depart_id', $bt_top_depart_id)
- ->where('model_type',SalesOrder::Model_type_one)
- ->where('crt_id', $man_value)
- ->orderBy('crt_time','desc')
- ->get()->toArray();
- $map = [];
- foreach ($list as $value) {
- $crt_time = $value['crt_time'] ? date("Y-m-d H:i:s", $value['crt_time']) : "";
- $model_type_title = SalesOrder::$model_type_title[$value['model_type']] ?? "";
- $tmp = [
- 'crt_time' => $crt_time,
- 'model_type_title' => $model_type_title,
- 'customer' => $value['customer_title'],
- 'contact_product' => $value['contact_product'],
- 'fz' => $value['fz_man'],
- 'contract_fee' => $value['contract_fee'],
- ];
- $map[SalesOrderReport::state_zero][] = $tmp;
- if($value['state'] == 1){
- $map[SalesOrderReport::state_one][] = $tmp;
- }elseif ($value['state'] == 2){
- $map[SalesOrderReport::state_two][] = $tmp;
- }elseif ($value['state'] == 3){
- $map[SalesOrderReport::state_three][] = $tmp;
- }elseif ($value['state'] == 4){
- $map[SalesOrderReport::state_four][] = $tmp;
- }
- }
- foreach ($area as $key => $value){
- if(isset($map[$value['value']])){
- $tmp = $map[$value['value']];
- $area[$key]['list'] = $tmp;
- $area[$key]['count'] = count($tmp);
- }
- }
- return $area;
- }
- private function saleReportStepOfThirdTypeThree($time,$data, $user){
- $head = $user['head']['id'] ?? 0;
- $basic_value = $data['value'];
- $area = [];
- foreach (SalesOrderReport::$all_state_list as $key => $value){
- $area[] = [
- 'label' => $value,
- 'value' => $key,
- 'list' => [],
- 'count' => 0
- ];
- }
- $start = $time[0];
- $end = $time[1];
- $list = SalesOrderReport::where('del_time',0)
- ->where('crt_time','>=', $start)
- ->where('crt_time','<=', $end)
- ->where('model_type', SalesOrder::Model_type_four)
- ->where('top_depart_id', $head)
- ->where('plat_type_title', $basic_value)
- ->orderBy('crt_time','desc')
- ->get()->toArray();
- $map = [];
- foreach ($list as $value) {
- $crt_time = $value['crt_time'] ? date("Y-m-d H:i:s", $value['crt_time']) : "";
- $model_type_title = SalesOrder::$model_type_title[$value['model_type']] ?? "";
- $tmp = [
- 'crt_time' => $crt_time,
- 'model_type_title' => $model_type_title,
- 'customer' => $value['customer_title'],
- 'contact_product' => $value['contact_product'],
- 'fz' => $value['fz_man'],
- 'contract_fee' => $value['contract_fee'],
- ];
- $map[SalesOrderReport::state_zero][] = $tmp;
- if($value['state'] == 1){
- $map[SalesOrderReport::state_one][] = $tmp;
- }elseif ($value['state'] == 2){
- $map[SalesOrderReport::state_two][] = $tmp;
- }elseif ($value['state'] == 3){
- $map[SalesOrderReport::state_three][] = $tmp;
- }elseif ($value['state'] == 4){
- $map[SalesOrderReport::state_four][] = $tmp;
- }
- }
- foreach ($area as $key => $value){
- if(isset($map[$value['value']])){
- $tmp = $map[$value['value']];
- $area[$key]['list'] = $tmp;
- $area[$key]['count'] = count($tmp);
- }
- }
- return $area;
- }
- private function saleReportStepOfThirdTypeFour($time,$data, $user){
- $head = $user['head']['id'] ?? 0;
- $area_value = $data['value'];
- $depart = Depart::where('del_time',0)
- ->where('parent_id',0)
- ->where('area', $area_value)
- ->select('id','title')
- ->get()->toArray();
- //大区
- $area = [];
- foreach ($depart as $value){
- $area[] = [
- 'label' => $value['title'],
- 'value' => $value['id'],
- 'receipt' => 0,
- 'all' => 0
- ];
- }
- $label = "全部";
- $total = [
- 'label' => $label,
- 'value' => "",
- 'receipt' => 0,
- 'all' => 0
- ];
- $start = $time[0];
- $end = $time[1];
- $list = SalesOrderReport::where('del_time',0)
- ->where('crt_time','>=', $start)
- ->where('crt_time','<=', $end)
- ->where('area',$area_value)
- ->where('model_type', SalesOrder::Model_type_two)
- ->where('from_top_depart_id', '>',0)
- ->get()->toArray();
- $map = [];
- foreach ($list as $value) {
- if(isset($map[$value['from_top_depart_id']])) {
- $tmp = bcadd($value['receipt'], $map[$value['from_top_depart_id']]['num_1']);
- $tmp2 = bcadd($value['contract_fee'], $map[$value['from_top_depart_id']]['num_2']);
- $map[$value['from_top_depart_id']]['num_1'] = $tmp;
- $map[$value['from_top_depart_id']]['num_2'] = $tmp2;
- }else{
- $map[$value['from_top_depart_id']] = [
- 'num_1' => $value['receipt'],
- 'num_2' => $value['contract_fee'],
- ];
- }
- $tmp_t = bcadd($total['receipt'], $value['receipt'],2);
- $tmp_t1 = bcadd($total['all'], $value['contract_fee'],2);
- $total['receipt'] = $tmp_t;
- $total['all'] = $tmp_t1;
- }
- foreach ($area as $key => $value){
- if(isset($map[$value['value']])){
- $tmp = $map[$value['value']];
- $area[$key]['receipt'] = $tmp['num_1'];
- $area[$key]['all'] = $tmp['num_2'];
- }
- }
- usort($area, function($a, $b) {
- // 先比较 all_num
- if ($b['all'] !== $a['all']) {
- return $b['all'] - $a['all']; // all_num 降序
- }
- // 如果 all_num 相同,再比较 value
- return $a['receipt'] - $b['receipt']; // value 正序
- });
- array_unshift($area, $total);
- return $area;
- }
- private function saleReportStepOfThreeTypeFive($time,$data){
- $area_value = $data['value'];
- $label = "全部";
- $total = [
- 'label' => $label,
- 'value' => "",
- 'all' => 0
- ];
- $start = $time[0];
- $end = $time[1];
- $start_time = strtotime(date("Y-m-01 00:00:00", $start));
- $list = CustomerPerFormance::where('del_time',0)
- ->where('crt_time','>=', $start_time)
- ->where('crt_time','<=', $end)
- ->where('area', $area_value)
- ->get()->toArray();
- $map = [];
- foreach ($list as $value) {
- if(isset($map[$value['title']])) {
- $tmp = bcadd($value['current_month_payment'], $map[$value['title']]['all']);
- $map[$value['title']]['all'] = $tmp;
- }else{
- $map[$value['title']] = [
- 'label' => $value['title'],
- 'value' => $value['title'],
- 'all' => $value['current_month_payment']
- ];
- }
- $tmp_t1 = bcadd($total['all'], $value['current_month_payment'],2);
- $total['all'] = $tmp_t1;
- }
- $map = array_values($map);
- usort($map, function($a, $b) {
- // 先比较 all_num
- if ($b['all'] !== $a['all']) {
- return $b['all'] - $a['all']; // all 降序
- }
- });
- array_unshift($map, $total);
- return $map;
- }
- public function saleReportStepOfFour($data, $user){
- if(empty($data['enter_time'][0]) || empty($data['enter_time'][1])) return [false, '请选择日期'];
- $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);
- list($status, $msg) = $this->aboutTime($return);
- if(! $status) return [false, $msg];
- if(empty($data['type'])) return [false, '请选择报表查看类型'];
- if(empty($data['value'])) return [false, 'value不能为空'];
- if(empty($data['value2'])) return [false, 'value2不能为空'];
- $type = $data['type'];
- if($type == 1){
- $result = $this->saleReportStepOfFourTypeOne($return, $data);
- }elseif ($type == 4){
- $result = $this->saleReportStepOfFourTypeFour($return, $data, $user);
- }else{
- return [false ,'报表查看类型错误'];
- }
- return [true, ['list' => $result]];
- }
- private function saleReportStepOfFourTypeOne($time,$data){
- $area_value = $data['value'];
- $depart_value = $data['value2'];
- $depart_list = Depart::where('del_time',0)->select('id','parent_id','title')->get()->toArray();
- // 查找所有部门id
- $childIds = $this->findChildIds($depart_value, $depart_list);
- $all_depart_id = array_merge($childIds, [$depart_value]);
- // 获取部门下的人
- $employee = EmployeeDepartPermission::whereIn('depart_id', $all_depart_id)
- ->select('employee_id')
- ->get()->toArray();
- $employee = array_unique(array_column($employee,'employee_id'));
- $empMap = Employee::whereIn('id', $employee)
- ->pluck('emp_name as title','id')
- ->toArray();
- $area = [];
- foreach ($empMap as $key => $value){
- $area[$key] = [
- 'label' => $value,
- 'value' => $key,
- 'receipt' => 0,
- 'all' => 0,
- 'goal' => 0,
- ];
- }
- $label = "全部";
- $total = [
- 'label' => $label,
- 'value' => "",
- 'receipt' => 0,
- 'all' => 0,
- 'goal' => 0,
- ];
- $start = $time[0];
- $end = $time[1];
- $list = SalesOrderReport::where('del_time',0)
- ->where('crt_time','>=', $start)
- ->where('crt_time','<=', $end)
- ->where('area',$area_value)
- ->where('top_depart_id',$depart_value)
- ->get()->toArray();
- $map = [];
- foreach ($list as $value) {
- if(isset($map[$value['crt_id']])) {
- $tmp = bcadd($value['receipt'], $map[$value['crt_id']]['num_1']);
- $tmp2 = bcadd($value['contract_fee'], $map[$value['crt_id']]['num_2']);
- $map[$value['crt_id']]['num_1'] = $tmp;
- $map[$value['crt_id']]['num_2'] = $tmp2;
- }else{
- $map[$value['crt_id']] = [
- 'num_1' => $value['receipt'],
- 'num_2' => $value['contract_fee'],
- ];
- }
- $tmp_t = bcadd($total['receipt'], $value['receipt'],2);
- $tmp_t1 = bcadd($total['all'], $value['contract_fee'],2);
- $total['receipt'] = $tmp_t;
- $total['all'] = $tmp_t1;
- }
- $start_time = strtotime(date("Y-m-01 00:00:00", $start));
- $month = MonthlyPerFormance::where('del_time',0)
- ->where('crt_time',">=",$start_time)
- ->where('crt_time','<=',$end)
- ->select('employee_id','sales_index')
- ->get()->toArray();
- $month_map = [];
- foreach ($month as $value){
- $month_map[$value['employee_id']] = $value['sales_index'];
- }
- foreach ($area as $key => $value){
- if(isset($map[$value['value']])){
- $tmp = $map[$value['value']];
- $area[$key]['receipt'] = $tmp['num_1'];
- $area[$key]['all'] = $tmp['num_2'];
- $goal = $month_map[$value['value']] ?? 0;
- $area[$key]['goal'] = $goal;
- $total['goal'] = bcadd($goal, $total['goal'],2);
- }else{
- unset($area[$key]);
- }
- }
- $area = array_values($area);
- usort($area, function($a, $b) {
- // 先比较 all_num
- if ($b['all'] !== $a['all']) {
- return $b['all'] - $a['all']; // all_num 降序
- }
- // 如果 all_num 相同,再比较 value
- return $a['receipt'] - $b['receipt']; // value 正序
- });
- array_unshift($area, $total);
- return $area;
- }
- private function saleReportStepOfFourTypeFour($time,$data, $user){
- $head = $user['head']['id'] ?? 0;
- $area_value = $data['value'];
- $depart_value = $data['value2'];
- $area = [];
- foreach (SalesOrderReport::$all_state_list as $key => $value){
- $area[] = [
- 'label' => $value,
- 'value' => $key,
- 'list' => [],
- 'count' => 0,
- ];
- }
- $start = $time[0];
- $end = $time[1];
- $list = SalesOrderReport::where('del_time',0)
- ->where('crt_time','>=', $start)
- ->where('crt_time','<=', $end)
- ->where('area',$area_value)
- ->where('model_type', SalesOrder::Model_type_two)
- ->where('from_top_depart_id', $depart_value)
- ->get()->toArray();
- $map = [];
- foreach ($list as $value) {
- $crt_time = $value['crt_time'] ? date("Y-m-d H:i:s", $value['crt_time']) : "";
- $model_type_title = SalesOrder::$model_type_title[$value['model_type']] ?? "";
- $tmp = [
- 'crt_time' => $crt_time,
- 'model_type_title' => $model_type_title,
- 'customer' => $value['customer_title'],
- 'contact_product' => $value['contact_product'],
- 'fz' => $value['fz_man'],
- 'contract_fee' => $value['contract_fee'],
- ];
- $map[SalesOrderReport::state_zero][] = $tmp;
- if($value['state'] == 1){
- $map[SalesOrderReport::state_one][] = $tmp;
- }elseif ($value['state'] == 2){
- $map[SalesOrderReport::state_two][] = $tmp;
- }elseif ($value['state'] == 3){
- $map[SalesOrderReport::state_three][] = $tmp;
- }elseif ($value['state'] == 4){
- $map[SalesOrderReport::state_four][] = $tmp;
- }
- }
- foreach ($area as $key => $value){
- if(isset($map[$value['value']])){
- $tmp = $map[$value['value']];
- $area[$key]['list'] = $tmp;
- $area[$key]['count'] = count($tmp);
- }
- }
- return $area;
- }
- public function saleReportStepOfFive($data, $user){
- if(empty($data['enter_time'][0]) || empty($data['enter_time'][1])) return [false, '请选择日期'];
- $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);
- list($status, $msg) = $this->aboutTime($return);
- if(! $status) return [false, $msg];
- if(empty($data['type'])) return [false, '请选择报表查看类型'];
- if(empty($data['value'])) return [false, 'value不能为空'];
- if(empty($data['value2'])) return [false, 'value2不能为空'];
- if(empty($data['value3'])) return [false, 'value3不能为空'];
- $type = $data['type'];
- if($type == 1){
- $result = $this->saleReportStepOfFiveTypeOne($return, $data);
- }else{
- return [false ,'报表查看类型错误'];
- }
- return [true, ['list' => $result]];
- }
- private function saleReportStepOfFiveTypeOne($time,$data){
- $area_value = $data['value'];
- $depart_value = $data['value2'];
- $man_value = $data['value3'];
- $area = [];
- foreach (SalesOrderReport::$all_state_list as $key => $value){
- $area[] = [
- 'label' => $value,
- 'value' => $key,
- 'list' => [],
- 'count' => 0
- ];
- }
- $start = $time[0];
- $end = $time[1];
- $list = SalesOrderReport::where('del_time',0)
- ->where('crt_time','>=', $start)
- ->where('crt_time','<=', $end)
- ->where('area',$area_value)
- ->where('top_depart_id',$depart_value)
- ->where('crt_id', $man_value)
- ->get()->toArray();
- $map = [];
- foreach ($list as $value) {
- $crt_time = $value['crt_time'] ? date("Y-m-d H:i:s", $value['crt_time']) : "";
- $model_type_title = SalesOrder::$model_type_title[$value['model_type']] ?? "";
- $tmp = [
- 'crt_time' => $crt_time,
- 'model_type_title' => $model_type_title,
- 'customer' => $value['customer_title'],
- 'contact_product' => $value['contact_product'],
- 'fz' => $value['fz_man'],
- 'contract_fee' => $value['contract_fee'],
- ];
- $map[SalesOrderReport::state_zero][] = $tmp;
- if($value['state'] == 1){
- $map[SalesOrderReport::state_one][] = $tmp;
- }elseif ($value['state'] == 2){
- $map[SalesOrderReport::state_two][] = $tmp;
- }elseif ($value['state'] == 3){
- $map[SalesOrderReport::state_three][] = $tmp;
- }elseif ($value['state'] == 4){
- $map[SalesOrderReport::state_four][] = $tmp;
- }
- }
- foreach ($area as $key => $value){
- if(isset($map[$value['value']])){
- $tmp = $map[$value['value']];
- $area[$key]['list'] = $tmp;
- $area[$key]['count'] = count($tmp);
- }
- }
- return $area;
- }
- }
|