StatisticsService.php 154 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956
  1. <?php
  2. namespace App\Service;
  3. use App\Model\BasicType;
  4. use App\Model\BasicTypeAllUse;
  5. use App\Model\Customer;
  6. use App\Model\CustomerPerFormance;
  7. use App\Model\CustomerReport;
  8. use App\Model\CustomerReportDepart;
  9. use App\Model\Depart;
  10. use App\Model\DepartIndex;
  11. use App\Model\Employee;
  12. use App\Model\EmployeeDepartPermission;
  13. use App\Model\InOutRecord;
  14. use App\Model\InvoiceOrder;
  15. use App\Model\InvoiceOrderInfo;
  16. use App\Model\LastJc;
  17. use App\Model\MonthlyPerFormance;
  18. use App\Model\Product;
  19. use App\Model\ProductAdjustment;
  20. use App\Model\ProductCategory;
  21. use App\Model\PurchaseOrder;
  22. use App\Model\PurchaseOrderInfo;
  23. use App\Model\ReturnExchangeOrder;
  24. use App\Model\ReturnExchangeOrderProductInfo;
  25. use App\Model\SalesOrder;
  26. use App\Model\SalesOrderProductInfo;
  27. use App\Model\SalesOrderReport;
  28. use App\Model\SeeRange;
  29. use App\Model\Setting;
  30. use Carbon\Carbon;
  31. use Illuminate\Support\Facades\DB;
  32. class StatisticsService extends Service
  33. {
  34. public function statisticsCustomer($data,$user){
  35. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择客资创建时间区间'];
  36. $model = Customer::Clear($user,$data);
  37. $model = $model->where('del_time',0)
  38. ->select('id','model_type','customer_from','top_depart_id')
  39. ->orderby('id', 'desc');
  40. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  41. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  42. $model->where('crt_time','>=',$return[0]);
  43. $model->where('crt_time','<=',$return[1]);
  44. }
  45. $list = $model->get()->toArray();
  46. $list1 = $this->fillStatisticsCustomer($list);
  47. $list2 = $this->fillStatisticsCustomer2($list);
  48. $list3 = $this->fillStatisticsCustomer3($list);
  49. return [true, ['bt' => $list1, 'zz' => $list2, 'table' => $list3]];
  50. }
  51. public function fillStatisticsCustomer($data){
  52. if(empty($data)) return $data;
  53. $customer_from = array_unique(array_column($data,'customer_from'));
  54. $basic_type = BasicType::where('del_time',0)
  55. ->whereIn('id',$customer_from)
  56. ->where('type',2)
  57. ->pluck('title','id')
  58. ->toArray();
  59. $return = [];
  60. foreach ($data as $value){
  61. $tmp = $basic_type[$value['customer_from']] ?? "";
  62. if(! $tmp) continue;
  63. if(isset($return[$tmp])){
  64. $return[$tmp]['total'] += 1;
  65. }else{
  66. $return[$tmp] = [
  67. 'total' => 1,
  68. 'title' => $tmp
  69. ];
  70. }
  71. }
  72. return array_values($return);
  73. }
  74. public function fillStatisticsCustomer2($data){
  75. if(empty($data)) return $data;
  76. $depart = Depart::where('del_time',0)
  77. ->where('parent_id',0)
  78. ->where('is_main',0)
  79. ->pluck('title','id')
  80. ->toArray();
  81. $return = [];
  82. foreach ($depart as $key => $value){
  83. $return[$key] = [
  84. 'title' => $value,
  85. 'total' => 0
  86. ];
  87. }
  88. $top_depart_id = SeeRange::where('del_time',0)
  89. ->where('data_type',SeeRange::type_one)
  90. ->where('type',SeeRange::data_three)
  91. ->whereIn('data_id', array_column($data,'id'))
  92. ->select('param_id as fp_top_depart_id')
  93. ->get()->toArray();
  94. $top_depart_map = [];
  95. foreach ($top_depart_id as $value){
  96. if(isset($top_depart_map[$value['fp_top_depart_id']])){
  97. $top_depart_map[$value['fp_top_depart_id']] += 1;
  98. }else{
  99. $top_depart_map[$value['fp_top_depart_id']] = 1;
  100. }
  101. }
  102. foreach ($return as $key => $value){
  103. if(isset($top_depart_map[$key])) $return[$key]['total'] = $top_depart_map[$key];
  104. }
  105. $return = array_values($return);
  106. usort($return, function($a, $b) {
  107. return $b['total'] - $a['total'];
  108. });
  109. return $return;
  110. }
  111. public function fillStatisticsCustomer3($data){
  112. if(empty($data)) return $data;
  113. $array = array_unique(array_column($data,'customer_from'));
  114. $basic_map = BasicType::whereIn('id',$array)
  115. ->pluck('title','id')
  116. ->toArray();
  117. $return_first = $customer_from_map = $header = [];
  118. foreach ($data as $value){
  119. $title = $basic_map[$value['customer_from']] ?? "";
  120. if(! $title) continue;
  121. if(isset($return_first[$title])){
  122. $return_first[$title]['total'] += 1;
  123. }else{
  124. $return_first[$title] = [
  125. 'title' => $title,
  126. 'total' => 1,
  127. ];
  128. }
  129. $customer_from_map[$value['id']] = $value['customer_from'];
  130. if(! in_array($title, $header)) $header[] = $title;
  131. }
  132. $header_tmp = $header_return = [];
  133. foreach ($header as $value){
  134. $header_tmp[] = [
  135. "title" => $value,
  136. "total" => 0,
  137. ];
  138. $header_return[] = [
  139. "title" => $value
  140. ];
  141. }
  142. $return_first = array_values($return_first);
  143. $depart = Depart::where('del_time',0)
  144. ->where('parent_id',0)
  145. ->where('is_main',0)
  146. ->pluck('title','id')
  147. ->toArray();
  148. $return = [];
  149. foreach ($depart as $key => $value){
  150. $return[$key] = [
  151. 'title' => $value,
  152. 'total' => 0,
  153. 'customer_from' => $header_tmp,
  154. ];
  155. }
  156. $top_depart_id = SeeRange::where('del_time',0)
  157. ->where('data_type',SeeRange::type_one)
  158. ->where('type',SeeRange::data_three)
  159. ->whereIn('data_id', array_column($data,'id'))
  160. ->select('param_id as fp_top_depart_id','data_id as customer_id')
  161. ->get()->toArray();
  162. $top_depart_map = $top_depart_map2 = [];
  163. foreach ($top_depart_id as $value){
  164. if(isset($top_depart_map[$value['fp_top_depart_id']])){
  165. $top_depart_map[$value['fp_top_depart_id']] += 1;
  166. }else{
  167. $top_depart_map[$value['fp_top_depart_id']] = 1;
  168. }
  169. $customer_from_tmp = $customer_from_map[$value['customer_id']] ?? 0;
  170. if($customer_from_tmp){
  171. if(isset($top_depart_map2[$value['fp_top_depart_id']][$customer_from_tmp])){
  172. $top_depart_map2[$value['fp_top_depart_id']][$customer_from_tmp] += 1;
  173. }else{
  174. $top_depart_map2[$value['fp_top_depart_id']][$customer_from_tmp] = 1;
  175. }
  176. }
  177. }
  178. foreach ($return as $key => $value){
  179. if(isset($top_depart_map[$key])) {
  180. $num = $top_depart_map[$key];
  181. if($num <= 0) {
  182. unset($return[$key]);
  183. continue;
  184. }else{
  185. $return[$key]['total'] = $top_depart_map[$key];
  186. }
  187. }else{
  188. unset($return[$key]);
  189. continue;
  190. }
  191. if(isset($top_depart_map2[$key])) {
  192. $tmp = $top_depart_map2[$key];
  193. foreach ($tmp as $k => $v){
  194. $title = $basic_map[$k] ?? "";
  195. if(! $title) continue;
  196. foreach ($return[$key]['customer_from'] as $kk => $vv){
  197. if($vv['title'] == $title){
  198. $return[$key]['customer_from'][$kk]['total'] = $v;
  199. }
  200. }
  201. }
  202. }
  203. }
  204. $return = array_values($return);
  205. usort($return, function($a, $b) {
  206. return $b['total'] - $a['total'];
  207. });
  208. return ['header' => $header_return,'left' => $return_first, 'right' => $return];
  209. }
  210. //销售统计饼图 根据合同类型区分(例如线上合同、线下合同)
  211. public function statisticsBt($data,$user){
  212. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择销售订单时间区间'];
  213. //总社id
  214. $head = $user['head']['id'] ?? 0;
  215. //当前门店
  216. $current_top_depart_id = $this->getMyTopDepart($user);
  217. //需要所有数据的门店
  218. $setting = Setting::where('setting_name','bt_top_depart_id')->first();
  219. $bt_top_depart_id = $setting['setting_value'] ?? [];
  220. $bt_top_depart_id = json_decode($bt_top_depart_id,true);
  221. //当前门店是否在设置门店中
  222. $bool = in_array($current_top_depart_id,$bt_top_depart_id);
  223. $model = SalesOrder::where('del_time',0)
  224. ->when(! $bool, function ($query) use ($current_top_depart_id) {
  225. return $query->where('top_depart_id', $current_top_depart_id);
  226. })
  227. ->select('model_type','contract_fee as total','top_depart_id');
  228. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  229. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  230. $model->where('crt_time','>=',$return[0]);
  231. $model->where('crt_time','<=',$return[1]);
  232. }
  233. $list = $model->get()->toArray();
  234. $list = $this->fillStatisticsBt($list,$bt_top_depart_id,$bool);
  235. return [true, $list];
  236. }
  237. public function fillStatisticsBt($data,$bt_top_depart_id,$bool){
  238. if(empty($data)) return $data;
  239. $return = [];
  240. foreach ($data as $value){
  241. $model_type_title = SalesOrder::$model_type_title[$value['model_type']] ?? "";
  242. if($bool){
  243. if($value['model_type'] != SalesOrder::Model_type_two){
  244. if(in_array($value['top_depart_id'], $bt_top_depart_id)){
  245. $this->makeThis($return,$value,$model_type_title);
  246. }
  247. }else{
  248. $this->makeThis($return,$value,$model_type_title);
  249. }
  250. }else{
  251. $this->makeThis($return,$value,$model_type_title);
  252. }
  253. }
  254. return array_values($return);
  255. }
  256. public function makeThis(&$return,$value,$model_type_title){
  257. if(isset($return[$value['model_type']])){
  258. $total = bcadd($value['total'], $return[$value['model_type']]['total'],2);
  259. $return[$value['model_type']]['total'] = $total;
  260. }else{
  261. $return[$value['model_type']] = [
  262. 'model_type' => $value['model_type'],
  263. 'model_type_title' => $model_type_title,
  264. 'total' => $value['total'],
  265. ];
  266. }
  267. }
  268. //省 订单类型细分统计
  269. public function statisticsProvince($data,$user){
  270. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
  271. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  272. //总社id
  273. $head = $user['head']['id'] ?? 0;
  274. //当前门店
  275. $current_top_depart_id = $this->getMyTopDepart($user);
  276. //销售订单类型
  277. $model_type = 0;
  278. if(! empty($data['model_type'])) $model_type = $data['model_type'];
  279. //门店设置的指标 按照区域汇总
  280. $index_map = DepartIndex::where('del_time',0)
  281. ->pluck('param_one','top_depart_id')
  282. ->toArray();
  283. //分社所属省
  284. $depart_map = $province_map = [];
  285. $depart = Depart::where('parent_id',0)
  286. ->where('province','<>','')
  287. ->where('del_time',0)
  288. ->select('province','id')
  289. ->get()->toArray();
  290. foreach ($depart as $value){
  291. $depart_map[$value['id']] = $value['province'];
  292. $province_map[$value['province']][] = $value['id'];
  293. }
  294. $address_map = config('address');
  295. $address_map = array_column($address_map,'label','value');
  296. //省
  297. $final_address_map = [];
  298. foreach ($address_map as $key => $value){
  299. $tmp = [
  300. 'key' => $key,
  301. 'title' => $value,
  302. 'total' => 0,
  303. 'index' => 0
  304. ];
  305. $top_depart_id = $province_map[$key] ?? [];
  306. if(! empty($top_depart_id)){
  307. foreach ($top_depart_id as $depart_id){
  308. $index = $index_map[$depart_id] ?? 0;
  309. $tmp['index'] = bcadd($tmp['index'], $index,2);
  310. }
  311. }
  312. $final_address_map[] = $tmp;
  313. }
  314. //特殊的门店
  315. $setting = Setting::where('setting_name','bt_top_depart_id')->first();
  316. $bt_top_depart_id = $setting['setting_value'] ?? [];
  317. $bt_top_depart_id = json_decode($bt_top_depart_id,true);
  318. //当前门店是否在设置门店中
  319. $bool = in_array($current_top_depart_id,$bt_top_depart_id);
  320. $sale_order = SalesOrder::where("del_time",0)
  321. ->where('crt_time','>=',$return[0])
  322. ->where('crt_time','<=',$return[1])
  323. ->when(! $bool, function ($query) use ($current_top_depart_id) {
  324. return $query->where('top_depart_id', $current_top_depart_id);
  325. })
  326. ->when(! empty($model_type), function ($query) use ($model_type) {
  327. return $query->where('model_type',$model_type);
  328. })
  329. ->select('id','top_depart_id','contract_fee','model_type')
  330. ->get()->toArray();
  331. //合同
  332. $purchase_map = $sale_order_id = [];
  333. foreach ($sale_order as $value){
  334. $area = $depart_map[$value['top_depart_id']] ?? 0;
  335. if(! $area) continue;
  336. if($value['model_type'] != SalesOrder::Model_type_two){
  337. if(in_array($value['top_depart_id'], $bt_top_depart_id)){
  338. $sale_order_id[] = $value['id'];
  339. $this->makeData($purchase_map,$value,$area);
  340. }
  341. }else{
  342. $sale_order_id[] = $value['id'];
  343. $this->makeData($purchase_map,$value,$area);
  344. }
  345. }
  346. //退货的差异
  347. $returnExchange_map = [];
  348. $returnExchange = ReturnExchangeOrder::where('del_time',0)
  349. ->where('type',ReturnExchangeOrder::Order_type)
  350. ->whereIn('data_id',array_unique($sale_order_id))
  351. ->select('top_depart_id','difference_amount')
  352. ->get()->toArray();
  353. foreach ($returnExchange as $value){
  354. $area = $depart_map[$value['top_depart_id']] ?? 0;
  355. if(! $area) continue;
  356. if(isset($returnExchange_map[$area])){
  357. $total = bcadd($returnExchange_map[$area], $value['difference_amount'],2);
  358. $returnExchange_map[$area] = $total;
  359. }else{
  360. $returnExchange_map[$area] = $value['difference_amount'];
  361. }
  362. }
  363. foreach ($final_address_map as $key => $value){
  364. $purchase_tmp = $purchase_map[$value['key']] ?? 0;
  365. $return_tmp = $returnExchange_map[$value['key']] ?? 0;
  366. $final_address_map[$key]['total'] = bcsub($purchase_tmp, $return_tmp,2);
  367. }
  368. usort($final_address_map, function($a, $b) {
  369. return $b['total'] - $a['total'];
  370. });
  371. return [true, $final_address_map];
  372. }
  373. public function makeData(&$purchase_map, $value,$area){
  374. if(isset($purchase_map[$area])){
  375. $total = bcadd($purchase_map[$area], $value['contract_fee'],2);
  376. $purchase_map[$area] = $total;
  377. }else{
  378. $purchase_map[$area] = $value['contract_fee'];
  379. }
  380. }
  381. //大区 订单类型细分统计
  382. public function statisticsArea($data,$user){
  383. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
  384. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  385. //总社id
  386. $head = $user['head']['id'] ?? 0;
  387. //当前门店
  388. $current_top_depart_id = $this->getMyTopDepart($user);
  389. //销售订单类型
  390. $model_type = 0;
  391. if(! empty($data['model_type'])) $model_type = $data['model_type'];
  392. //门店设置的指标 按照区域汇总
  393. $index_map = DepartIndex::where('del_time',0)
  394. ->pluck('param_one','top_depart_id')
  395. ->toArray();
  396. //分社所属大区
  397. $depart_map = $area_map = [];
  398. $depart = Depart::where('parent_id',0)
  399. ->where('area','>',0)
  400. ->where('del_time',0)
  401. ->select('area','id')
  402. ->get()->toArray();
  403. foreach ($depart as $value){
  404. $depart_map[$value['id']] = $value['area'];
  405. $area_map[$value['area']][] = $value['id'];
  406. }
  407. //大区
  408. $area = Depart::$area;
  409. $area_map = [];
  410. foreach ($area as $key => $value){
  411. $tmp = [
  412. 'key' => $key,
  413. 'title' => $value,
  414. 'total' => 0,
  415. 'index' => 0
  416. ];
  417. $top_depart_id = $area_map[$key] ?? [];
  418. if(! empty($top_depart_id)){
  419. foreach ($top_depart_id as $depart_id){
  420. $index = $index_map[$depart_id] ?? 0;
  421. $tmp['index'] = bcadd($tmp['index'], $index,2);
  422. }
  423. }
  424. $area_map[] = $tmp;
  425. }
  426. //特殊的门店
  427. $setting = Setting::where('setting_name','bt_top_depart_id')->first();
  428. $bt_top_depart_id = $setting['setting_value'] ?? [];
  429. $bt_top_depart_id = json_decode($bt_top_depart_id,true);
  430. //当前门店是否在设置门店中
  431. $bool = in_array($current_top_depart_id,$bt_top_depart_id);
  432. $sale_order = SalesOrder::where("del_time",0)
  433. ->where('crt_time','>=',$return[0])
  434. ->where('crt_time','<=',$return[1])
  435. ->when(! $bool, function ($query) use ($current_top_depart_id) {
  436. return $query->where('top_depart_id', $current_top_depart_id);
  437. })
  438. ->when(! empty($model_type), function ($query) use ($model_type) {
  439. return $query->where('model_type',$model_type);
  440. })
  441. ->select('top_depart_id','contract_fee','model_type','id')
  442. ->get()->toArray();
  443. $purchase_map = $sale_order_id = [];
  444. foreach ($sale_order as $value){
  445. $area = $depart_map[$value['top_depart_id']] ?? 0;
  446. if(! $area) continue;
  447. if($value['model_type'] != SalesOrder::Model_type_two){
  448. if(in_array($value['top_depart_id'], $bt_top_depart_id)){
  449. $sale_order_id[] = $value['id'];
  450. $this->makeData($purchase_map,$value,$area);
  451. }
  452. }else{
  453. $sale_order_id[] = $value['id'];
  454. $this->makeData($purchase_map,$value,$area);
  455. }
  456. }
  457. //退货的差异
  458. $returnExchange_map = [];
  459. $returnExchange = ReturnExchangeOrder::where('del_time',0)
  460. ->where('type',ReturnExchangeOrder::Order_type)
  461. ->whereIn('data_id',array_unique($sale_order_id))
  462. ->select('top_depart_id','difference_amount')
  463. ->get()->toArray();
  464. foreach ($returnExchange as $value){
  465. $area = $depart_map[$value['top_depart_id']] ?? 0;
  466. if(! $area) continue;
  467. if(isset($returnExchange_map[$area])){
  468. $total = bcadd($returnExchange_map[$area], $value['difference_amount'],2);
  469. $returnExchange_map[$area] = $total;
  470. }else{
  471. $returnExchange_map[$area] = $value['difference_amount'];
  472. }
  473. }
  474. foreach ($area_map as $key => $value){
  475. $purchase_tmp = $purchase_map[$value['key']] ?? 0;
  476. $return_tmp = $returnExchange_map[$value['key']] ?? 0;
  477. $area_map[$key]['total'] = bcsub($purchase_tmp, $return_tmp,2);
  478. }
  479. usort($area_map, function($a, $b) {
  480. return $b['total'] - $a['total'];
  481. });
  482. return [true, $area_map];
  483. }
  484. // 省|大区下的门店 订货统计
  485. public function statisticsAreaDepart($data,$user){
  486. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
  487. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  488. if(empty($data['area']) && empty($data['province'])) return [false, '大区或省份必须选择一个'];
  489. if(! empty($data['area']) && ! empty($data['province']))return [false, '大区或省份有且只能有一个'];
  490. if(! empty($data['area']) && ! isset(Depart::$area[$data['area']])) return [false, '该大区不存在'];
  491. $address_map = config('address');
  492. $address_map = array_column($address_map,'label','value');
  493. if(! empty($data['province']) && ! isset($address_map[$data['province']])) return [false, '该省不存在'];
  494. //总社id
  495. $head = $user['head']['id'] ?? 0;
  496. //当前门店
  497. $current_top_depart_id = $this->getMyTopDepart($user);
  498. //销售订单类型
  499. $model_type = 0;
  500. if(! empty($data['model_type'])) $model_type = $data['model_type'];
  501. if(! empty($data['area'])){
  502. $depart = Depart::where('parent_id',0)
  503. ->where('del_time',0)
  504. ->where('area',$data['area'])
  505. ->select('id','title')
  506. ->get()
  507. ->toArray();
  508. if(empty($depart)) return [false, '该大区下不存在门店'];
  509. }else{
  510. $depart = Depart::where('parent_id',0)
  511. ->where('del_time',0)
  512. ->where('province',$data['province'])
  513. ->select('id','title')
  514. ->get()
  515. ->toArray();
  516. if(empty($depart)) return [false, '该省下不存在门店'];
  517. }
  518. //门店设置的指标
  519. $index_map = DepartIndex::where('del_time',0)
  520. ->whereIn('top_depart_id',array_column($depart,'id'))
  521. ->pluck('param_one','top_depart_id')
  522. ->toArray();
  523. //特殊的门店
  524. $setting = Setting::where('setting_name','bt_top_depart_id')->first();
  525. $bt_top_depart_id = $setting['setting_value'] ?? [];
  526. $bt_top_depart_id = json_decode($bt_top_depart_id,true);
  527. //当前门店是否在设置门店中
  528. $bool = in_array($current_top_depart_id,$bt_top_depart_id);
  529. //合同
  530. $sale_order = SalesOrder::where("del_time",0)
  531. ->where('crt_time','>=',$return[0])
  532. ->where('crt_time','<=',$return[1])
  533. ->when(! $bool, function ($query) use ($current_top_depart_id) {
  534. return $query->where('top_depart_id', $current_top_depart_id);
  535. })
  536. ->when(! empty($model_type), function ($query) use ($model_type) {
  537. return $query->where('model_type',$model_type);
  538. })
  539. ->select('top_depart_id','contract_fee','id','model_type')
  540. ->get()->toArray();
  541. //向总社采购的钱
  542. $purchase_map = $sale_order_id = [];
  543. foreach ($sale_order as $value){
  544. if($value['model_type'] != SalesOrder::Model_type_two){
  545. if(in_array($value['top_depart_id'], $bt_top_depart_id)){
  546. $sale_order_id[] = $value['id'];
  547. $this->makeData($purchase_map,$value,$value['top_depart_id']);
  548. }
  549. }else{
  550. $sale_order_id[] = $value['id'];
  551. $this->makeData($purchase_map,$value,$value['top_depart_id']);
  552. }
  553. }
  554. //退货的差异
  555. $returnExchange_map = [];
  556. $returnExchange = ReturnExchangeOrder::where('del_time',0)
  557. ->whereIn('top_depart_id',array_column($depart,'id'))
  558. ->where('type',ReturnExchangeOrder::Order_type)
  559. ->whereIn('data_id',array_unique($sale_order_id))
  560. ->select('top_depart_id','difference_amount')
  561. ->get()->toArray();
  562. foreach ($returnExchange as $value){
  563. if(isset($returnExchange_map[$value['top_depart_id']])){
  564. $total = bcadd($returnExchange_map[$value['top_depart_id']], $value['difference_amount'],2);
  565. $returnExchange_map[$value['top_depart_id']] = $total;
  566. }else{
  567. $returnExchange_map[$value['top_depart_id']] = $value['difference_amount'];
  568. }
  569. }
  570. foreach ($depart as $key => $value){
  571. $purchase_tmp = $purchase_map[$value['id']] ?? 0;
  572. $return_tmp = $returnExchange_map[$value['id']] ?? 0;
  573. $depart[$key]['total'] = bcsub($purchase_tmp, $return_tmp,2);
  574. $depart[$key]['index'] = $index_map[$value['id']] ?? 0;
  575. }
  576. return [true, $depart];
  577. }
  578. //某个门店 关于产品以及分类的统计 订货统计
  579. public function statisticsAreaDepartProduct($data,$user){
  580. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
  581. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  582. if(empty($data['top_depart_id'])) return [false, '请选择门店'];
  583. //总社id
  584. $head = $user['head']['id'] ?? 0;
  585. //当前门店
  586. $current_top_depart_id = $this->getMyTopDepart($user);
  587. //特殊的门店
  588. $setting = Setting::where('setting_name','bt_top_depart_id')->first();
  589. $bt_top_depart_id = $setting['setting_value'] ?? [];
  590. $bt_top_depart_id = json_decode($bt_top_depart_id,true);
  591. //当前门店是否在设置门店中
  592. $bool = in_array($current_top_depart_id,$bt_top_depart_id);
  593. //销售订单类型
  594. $model_type = 0;
  595. if(! empty($data['model_type'])) $model_type = $data['model_type'];
  596. //时间 =》 钱
  597. $purchase_map1 = $this->getTime($return);
  598. //产品 =》 数量
  599. $purchase_category_map = $purchase_map = [];
  600. $sale_order = SalesOrder::where("del_time",0)
  601. ->where('top_depart_id',$data['top_depart_id'])
  602. ->where('crt_time','>=',$return[0])
  603. ->where('crt_time','<=',$return[1])
  604. ->when(! $bool, function ($query) use ($current_top_depart_id) {
  605. return $query->where('top_depart_id', $current_top_depart_id);
  606. })
  607. ->when(! empty($model_type), function ($query) use ($model_type) {
  608. return $query->where('model_type',$model_type);
  609. })
  610. ->select('id','crt_time')
  611. ->get()->toArray();
  612. $purchase_for_time = array_column($sale_order,'crt_time','id');
  613. $sale_order_id = array_column($sale_order,'id');
  614. $purchase_product = SalesOrderProductInfo::where("del_time",0)
  615. ->whereIn('sales_order_id',$sale_order_id)
  616. ->select('sales_order_id','product_id','number','price','final_amount','sports_bag_id')
  617. ->get()->toArray();
  618. //退换货
  619. list($returnExchange_map,$returnExchange_map_2) = $this->returnExchange($data,$sale_order_id);
  620. //产品
  621. $product_list = Product::whereIn('id',array_unique(array_merge_recursive(array_column($purchase_product,'product_id'),array_keys($returnExchange_map,'product_id'))))
  622. ->select('id','product_category','title','code')
  623. ->get()->toArray();
  624. $product_list_map = array_column($product_list,null,'id');
  625. $category_return = ProductCategory::where('del_time',0)
  626. ->where('parent_id',0)
  627. ->pluck('title','id')
  628. ->toArray();
  629. foreach ($purchase_product as $value){
  630. if($value['sports_bag_id'] > 0){
  631. $money = $value['final_amount'];
  632. }elseif($value['final_amount'] > 0){
  633. $money = $value['final_amount'];
  634. }else{
  635. $money = bcmul($value['price'],$value['number'],2);
  636. }
  637. $crt_time = $purchase_for_time[$value['sales_order_id']];
  638. $crt_time = date("Y-m-d",$crt_time);
  639. //产品信息
  640. $product_tmp = $product_list_map[$value['product_id']] ?? [];
  641. $time_money = 0;
  642. if(isset($returnExchange_map_2[$crt_time])) {
  643. $time_money = $returnExchange_map_2[$crt_time] ?? 0;
  644. unset($returnExchange_map_2[$crt_time]);
  645. }
  646. //钱
  647. if(isset($purchase_map1[$crt_time])){
  648. $time_total = bcadd($purchase_map1[$crt_time]['total'],$money,2);
  649. $time_total = bcsub($time_total,$time_money,2);
  650. $purchase_map1[$crt_time]['total'] = $time_total;
  651. }
  652. $return_number = $return_total = 0;
  653. $return_category_number = $return_category_total = 0;
  654. if(isset($returnExchange_map[$value['product_id']])){
  655. $tmp = $returnExchange_map[$value['product_id']];
  656. $return_number = bcsub($value['number'], $tmp['number'],2);
  657. $return_total = bcsub($money, $tmp['total'],2);
  658. $return_category_number = $return_number;
  659. $return_category_total = $return_total;
  660. }
  661. //-------根据产品
  662. //数量
  663. if(isset($purchase_map[$value['product_id']])){
  664. $tmp_number = bcadd($purchase_map[$value['product_id']]['number'], $value['number'],2);
  665. $tmp_money = bcadd($purchase_map[$value['product_id']]['total'], $money,2);
  666. $purchase_map[$value['product_id']]['number'] = $tmp_number;
  667. $purchase_map[$value['product_id']]['total'] = $tmp_money;
  668. }else{
  669. //减去退换货
  670. $number = bcsub($value['number'], $return_number,2);
  671. $total = bcsub($money, $return_total,2);
  672. $purchase_map[$value['product_id']] = [
  673. 'title' => $product_tmp['title'] . "(" . $product_tmp['code'] .")",
  674. 'number' => $number,
  675. 'total' => $total
  676. ];
  677. }
  678. //-------根据产品
  679. //-------根据产品大类
  680. //数量
  681. $category_tmp = json_decode($product_tmp['product_category']);
  682. $category_tmp = min($category_tmp);
  683. //退换货
  684. $number_2 = bcsub($value['number'], $return_category_number,2);
  685. $total_2 = bcsub($money, $return_category_total,2);
  686. if(isset($purchase_category_map[$category_tmp])){
  687. $tmp_number = bcadd($purchase_map[$value['product_id']]['number'], $value['number'],2);
  688. $tmp_number = bcsub($tmp_number,$number_2,2);
  689. $tmp_money = bcadd($purchase_map[$value['product_id']]['total'], $money,2);
  690. $tmp_money = bcsub($tmp_money,$total_2,2);
  691. $purchase_category_map[$category_tmp]['number'] = $tmp_number;
  692. $purchase_category_map[$category_tmp]['total'] = $tmp_money;
  693. }else{
  694. $num = bcsub($value['number'],$number_2,2);
  695. $tol = bcsub($money,$total_2,2);
  696. $purchase_category_map[$category_tmp] = [
  697. 'number' => $num,
  698. 'title' => $category_return[$category_tmp] ?? "",
  699. 'total' => $tol
  700. ];
  701. }
  702. }
  703. return [true, ['money' => array_values($purchase_map1), 'product_num' => array_values($purchase_map), 'category_num' => array_values($purchase_category_map)]];
  704. }
  705. public function getTime($data){
  706. $startTimestamp = $data[0]; // 起始时间戳
  707. $endTimestamp = $data[1]; // 结束时间戳
  708. // 创建 DateTime 对象
  709. $startDate = new \DateTime();
  710. $endDate = new \DateTime();
  711. $startDate->setTimestamp($startTimestamp);
  712. $endDate->setTimestamp($endTimestamp);
  713. // 创建 DatePeriod 对象
  714. $interval = new \DateInterval('P1D'); // 每天的间隔
  715. $dateRange = new \DatePeriod($startDate, $interval, $endDate);
  716. // 遍历日期范围并输出每个日期
  717. $return = [];
  718. foreach ($dateRange as $date) {
  719. $day = $date->format('Y-m-d');
  720. $return[$day] = [
  721. 'day' => $day,
  722. 'total' => 0
  723. ];
  724. }
  725. return $return;
  726. }
  727. public function returnExchange($data,$sale_order_id){
  728. $returnExchange_map = $returnExchange_map_2 = [];
  729. $returnExchange = ReturnExchangeOrder::where('del_time',0)
  730. ->where('type',ReturnExchangeOrder::Order_type)
  731. ->whereIn('data_id', $sale_order_id)
  732. ->select('id')
  733. ->get()->toArray();
  734. $returnExchange_product = ReturnExchangeOrderProductInfo::where("del_time",0)
  735. ->whereIn('return_exchange_id',array_column($returnExchange,'id'))
  736. ->select('return_exchange_id','product_id','number','return_exchange_price as price','crt_time')
  737. ->get()->toArray();
  738. foreach ($returnExchange_product as $value){
  739. $money = bcmul($value['price'],$value['number'],2);
  740. if(isset($returnExchange_map[$value['product_id']])){
  741. $tmp_money = bcadd($returnExchange_map[$value['product_id']]['total'], $money,2);
  742. $tmp_number = bcadd($returnExchange_map[$value['product_id']]['number'], $value['number'],2);
  743. $returnExchange_map[$value['product_id']] = [
  744. 'number' => $tmp_number,
  745. 'total' => $tmp_money
  746. ];
  747. }else{
  748. $returnExchange_map[$value['product_id']] = [
  749. 'number' => $value['number'],
  750. 'total' => $money
  751. ];
  752. }
  753. $crt_time = date("Y-m-d",$value['crt_time']);
  754. if(isset($returnExchange_map_2[$crt_time])){
  755. $tmp_money_2 = bcadd($returnExchange_map_2[$crt_time], $money,2);
  756. $returnExchange_map_2[$crt_time] = $tmp_money_2;
  757. }else{
  758. $returnExchange_map_2[$crt_time] = $money;
  759. }
  760. }
  761. return [$returnExchange_map,$returnExchange_map_2];
  762. }
  763. //线下订单
  764. public function statisticsModelTypeOne($data,$user){
  765. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
  766. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  767. if(empty($data['x'])) return [false, '请选择X轴维度'];
  768. //总社id
  769. $head = $user['head']['id'] ?? 0;
  770. //当前门店
  771. $current_top_depart_id = $this->getMyTopDepart($user);
  772. //线下 销售订单类型
  773. $model_type = SalesOrder::Model_type_one;
  774. $sale_order = SalesOrder::where("del_time",0)
  775. ->where('crt_time','>=',$return[0])
  776. ->where('crt_time','<=',$return[1])
  777. ->where('model_type',$model_type)
  778. ->when(! empty($current_top_depart_id) && $current_top_depart_id != $head, function ($query) use ($current_top_depart_id) {
  779. return $query->where('top_depart_id', $current_top_depart_id);
  780. })
  781. ->select('id','top_depart_id','contract_fee','crt_id')
  782. ->get()->toArray();
  783. //合同
  784. $sale_order_id = array_column($sale_order,'id');
  785. $statistics = [];
  786. if($data['x'] == "crt_id"){
  787. $emp = Employee::whereIn('id', array_unique(array_column($sale_order,'crt_id')))
  788. ->pluck('emp_name','id')
  789. ->toArray();
  790. //退货的差异
  791. $returnExchange_map = [];
  792. $returnExchange = ReturnExchangeOrder::where('del_time',0)
  793. ->where('type',ReturnExchangeOrder::Order_type)
  794. ->whereIn('data_id',array_unique($sale_order_id))
  795. ->select('top_depart_id','difference_amount','data_id')
  796. ->get()->toArray();
  797. foreach ($returnExchange as $value){
  798. if(isset($returnExchange_map[$value['data_id']])){
  799. $total = bcadd($returnExchange_map[$value['data_id']], $value['difference_amount'],2);
  800. $returnExchange_map[$value['data_id']] = $total;
  801. }else{
  802. $returnExchange_map[$value['data_id']] = $value['difference_amount'];
  803. }
  804. }
  805. foreach ($sale_order as $value){
  806. $crt_name = $emp[$value['crt_id']] ?? "";
  807. if(! $crt_name) continue;
  808. $return_money = $returnExchange_map[$value['id']] ?? 0;
  809. $tmp = bcsub($value['contract_fee'], $return_money,2);
  810. if(isset($statistics[$value['crt_id']])){
  811. $total = bcadd($tmp,$statistics[$value['crt_id']]['total'],2);
  812. $statistics[$value['crt_id']]['total'] = $total;
  813. }else{
  814. $statistics[$value['crt_id']] = [
  815. 'title' => $crt_name,
  816. 'total' => $tmp,
  817. ];
  818. }
  819. }
  820. }elseif($data['x'] == "product"){
  821. $sales_product = SalesOrderProductInfo::where('del_time',0)
  822. ->whereIn('sales_order_id',$sale_order_id)
  823. ->select('product_id','number','retail_price','price')
  824. ->get()->toArray();
  825. $product = Product::whereIn('id',array_unique(array_column($sales_product,'product_id')))
  826. ->select('id','title','code')
  827. ->get()->toArray();
  828. $product_map = [];
  829. foreach ($product as $value){
  830. $product_map[$value['id']] = $value['title'] . "(" . $value['code']. ")";
  831. }
  832. //退货的差异
  833. $returnExchange_map = [];
  834. $returnExchange = ReturnExchangeOrder::where('del_time',0)
  835. ->where('type',ReturnExchangeOrder::Order_type)
  836. ->whereIn('data_id',array_unique($sale_order_id))
  837. ->select('id')
  838. ->get()->toArray();
  839. $return_product = ReturnExchangeOrderProductInfo::where('del_time',0)
  840. ->whereIn('return_exchange_id', array_column($returnExchange,'id'))
  841. ->select('product_id','number','return_exchange_price')
  842. ->get()->toArray();
  843. foreach ($return_product as $value){
  844. $tmp = bcmul($value['number'],$value['return_exchange_price'],2);
  845. if(isset($returnExchange_map[$value['product_id']])){
  846. $total = bcadd($returnExchange_map[$value['product_id']], $tmp,2);
  847. $returnExchange_map[$value['product_id']] = $total;
  848. }else{
  849. $returnExchange_map[$value['product_id']] = $tmp;
  850. }
  851. }
  852. foreach ($sales_product as $value){
  853. $product_tmp = $product_map[$value['product_id']] ?? "";
  854. if(! $product_tmp) continue;
  855. if($value['price'] > 0){
  856. $tmp = bcmul($value['price'], $value['number'],2);
  857. }else{
  858. $tmp = bcmul($value['retail_price'], $value['number'],2);
  859. }
  860. $return_tmp = 0;
  861. if(isset($returnExchange_map[$value['product_id']])){
  862. $return_tmp = $returnExchange_map[$value['product_id']] ?? 0;
  863. unset($returnExchange_map[$value['product_id']]);
  864. }
  865. $tmp = bcsub($tmp,$return_tmp,2);
  866. if(isset($statistics[$value['product_id']])){
  867. $total = bcadd($tmp,$statistics[$value['product_id']]['total'],2);
  868. $statistics[$value['product_id']]['total'] = $total;
  869. }else{
  870. $statistics[$value['product_id']] = [
  871. 'title' => $product_tmp,
  872. 'total' => $tmp,
  873. ];
  874. }
  875. }
  876. }
  877. foreach ($statistics as $key => $value){
  878. if(floatval($value['total']) <= 0) unset($statistics[$key]);
  879. }
  880. $statistics = array_values($statistics);
  881. usort($statistics, function($a, $b) {
  882. return $b['total'] - $a['total'];
  883. });
  884. return [true, $statistics];
  885. }
  886. //线上订单
  887. public function statisticsModelTypeFour($data,$user){
  888. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
  889. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  890. if(empty($data['x'])) return [false, '请选择X轴维度'];
  891. //总社id
  892. $head = $user['head']['id'] ?? 0;
  893. //当前门店
  894. $current_top_depart_id = $this->getMyTopDepart($user);
  895. //线下 销售订单类型
  896. $model_type = SalesOrder::Model_type_four;
  897. $sale_order = SalesOrder::where("del_time",0)
  898. ->where('crt_time','>=',$return[0])
  899. ->where('crt_time','<=',$return[1])
  900. ->where('model_type',$model_type)
  901. ->when(! empty($current_top_depart_id) && $current_top_depart_id != $head, function ($query) use ($current_top_depart_id) {
  902. return $query->where('top_depart_id', $current_top_depart_id);
  903. })
  904. ->select('id','top_depart_id','contract_fee','plat_type')
  905. ->get()->toArray();
  906. //合同
  907. $sale_order_id = array_column($sale_order,'id');
  908. $statistics = [];
  909. if($data['x'] == "order_from"){
  910. $array = array_unique(array_column($sale_order,'plat_type'));
  911. $basic_map = BasicType::whereIn('id',$array)
  912. ->pluck('title','id')
  913. ->toArray();
  914. //退货的差异
  915. $returnExchange_map = [];
  916. $returnExchange = ReturnExchangeOrder::where('del_time',0)
  917. ->where('type',ReturnExchangeOrder::Order_type)
  918. ->whereIn('data_id',array_unique($sale_order_id))
  919. ->select('top_depart_id','difference_amount','data_id')
  920. ->get()->toArray();
  921. foreach ($returnExchange as $value){
  922. if(isset($returnExchange_map[$value['data_id']])){
  923. $total = bcadd($returnExchange_map[$value['data_id']], $value['difference_amount'],2);
  924. $returnExchange_map[$value['data_id']] = $total;
  925. }else{
  926. $returnExchange_map[$value['data_id']] = $value['difference_amount'];
  927. }
  928. }
  929. foreach ($sale_order as $value){
  930. $plat_type = $basic_map[$value['plat_type']] ?? "";
  931. if(! $plat_type) continue;
  932. $return_money = $returnExchange_map[$value['id']] ?? 0;
  933. $tmp = bcsub($value['contract_fee'], $return_money,2);
  934. if(isset($statistics[$value['plat_type']])){
  935. $total = bcadd($tmp,$statistics[$value['plat_type']]['total'],2);
  936. $statistics[$value['plat_type']]['total'] = $total;
  937. }else{
  938. $statistics[$value['plat_type']] = [
  939. 'title' => $plat_type,
  940. 'total' => $tmp,
  941. ];
  942. }
  943. }
  944. }elseif($data['x'] == "product"){
  945. $sales_product = SalesOrderProductInfo::where('del_time',0)
  946. ->whereIn('sales_order_id',$sale_order_id)
  947. ->select('product_id','number','retail_price','price')
  948. ->get()->toArray();
  949. $product = Product::whereIn('id',array_unique(array_column($sales_product,'product_id')))
  950. ->select('id','title','code')
  951. ->get()->toArray();
  952. $product_map = [];
  953. foreach ($product as $value){
  954. $product_map[$value['id']] = $value['title'] . "(" . $value['code']. ")";
  955. }
  956. //退货的差异
  957. $returnExchange_map = [];
  958. $returnExchange = ReturnExchangeOrder::where('del_time',0)
  959. ->where('type',ReturnExchangeOrder::Order_type)
  960. ->whereIn('data_id',array_unique($sale_order_id))
  961. ->select('id')
  962. ->get()->toArray();
  963. $return_product = ReturnExchangeOrderProductInfo::where('del_time',0)
  964. ->whereIn('return_exchange_id', array_column($returnExchange,'id'))
  965. ->select('product_id','number','return_exchange_price')
  966. ->get()->toArray();
  967. foreach ($return_product as $value){
  968. $tmp = bcmul($value['number'],$value['return_exchange_price'],2);
  969. if(isset($returnExchange_map[$value['product_id']])){
  970. $total = bcadd($returnExchange_map[$value['product_id']], $tmp,2);
  971. $returnExchange_map[$value['product_id']] = $total;
  972. }else{
  973. $returnExchange_map[$value['product_id']] = $tmp;
  974. }
  975. }
  976. foreach ($sales_product as $value){
  977. $product_tmp = $product_map[$value['product_id']] ?? "";
  978. if(! $product_tmp) continue;
  979. if($value['price'] > 0){
  980. $tmp = bcmul($value['price'], $value['number'],2);
  981. }else{
  982. $tmp = bcmul($value['retail_price'], $value['number'],2);
  983. }
  984. $return_tmp = 0;
  985. if(isset($returnExchange_map[$value['product_id']])){
  986. $return_tmp = $returnExchange_map[$value['product_id']] ?? 0;
  987. unset($returnExchange_map[$value['product_id']]);
  988. }
  989. $tmp = bcsub($tmp,$return_tmp,2);
  990. if(isset($statistics[$value['product_id']])){
  991. $total = bcadd($tmp,$statistics[$value['product_id']]['total'],2);
  992. $statistics[$value['product_id']]['total'] = $total;
  993. }else{
  994. $statistics[$value['product_id']] = [
  995. 'title' => $product_tmp,
  996. 'total' => $tmp,
  997. ];
  998. }
  999. }
  1000. }
  1001. foreach ($statistics as $key => $value){
  1002. if(floatval($value['total']) <= 0) unset($statistics[$key]);
  1003. }
  1004. $statistics = array_values($statistics);
  1005. usort($statistics, function($a, $b) {
  1006. return $b['total'] - $a['total'];
  1007. });
  1008. return [true, $statistics];
  1009. }
  1010. public function statisticsJc($data,$user){
  1011. $model = Product::ProductClear2($user,$data);
  1012. $model = $model->where('del_time',0)
  1013. ->select('title','id','code','depart_id','top_depart_id','product_attribute')
  1014. ->orderby('product_attribute', 'desc')
  1015. ->orderby('id', 'desc');
  1016. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  1017. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  1018. if(isset($data['product_attribute'])) $model->where('product_attribute', $data['product_attribute']);
  1019. if(! empty($data['product_category_id'])) $model->where('product_category_id', $data['product_category_id']);
  1020. if(! empty($data['product_category'])) {
  1021. $product_category = ProductCategory::where('del_time',0)
  1022. ->where('title', 'LIKE', '%'.$data['product_category'].'%')
  1023. ->select('id')
  1024. ->get()->toArray();
  1025. $model->whereIn('product_category_id',array_unique(array_column($product_category,'id')));
  1026. }
  1027. $list = $this->limit($model,'',$data);
  1028. $list = $this->fillData($list,$user,$data);
  1029. return [true, $list];
  1030. }
  1031. public function fillData($data, $user, $search){
  1032. if(empty($data['data'])) return $data;
  1033. //产品
  1034. $product = array_column($data['data'],'id');
  1035. //本月入库 本月出库
  1036. list($in, $out) = $this->getThisMonthData($product,$user,$search);
  1037. //上月结存 汇总这个月之前的出入
  1038. list($last_in,$last_out) = $this->getLastMonthBalance($product,$user,$search);
  1039. foreach ($data['data'] as $key => $value){
  1040. $last_in_tmp = $last_in[$value['id']] ?? [];
  1041. $last_in_money = $last_in_tmp['total'] ?? 0;//本月之前入的金额
  1042. $last_in_number = $last_in_tmp['number'] ?? 0;//本月之前入的数量
  1043. $last_out_tmp = $last_out[$value['id']] ?? [];
  1044. $last_out_money = $last_out_tmp['total'] ?? 0;//本月之前出的金额
  1045. $last_out_number = $last_out_tmp['number'] ?? 0;//本月之前出的数量
  1046. //上月结存
  1047. $last_jc_money = bcsub($last_in_money,$last_out_money,2);//结存金额
  1048. $last_jc_number = bcsub($last_in_number,$last_out_number,2);//结存数量
  1049. $last_jc_price = floatval($last_jc_number) ? bcdiv($last_jc_money,$last_jc_number,2) : 0;//结存单价
  1050. $data['data'][$key]['last_jc_money'] = $last_jc_money;
  1051. $data['data'][$key]['last_jc_number'] = $last_jc_number;
  1052. $data['data'][$key]['last_jc_price'] = $last_jc_price;
  1053. //本月入库
  1054. $this_in_tmp = $in[$value['id']] ?? [];
  1055. $this_in_money = $this_in_tmp['total'] ?? 0;//本月入的金额
  1056. $this_in_number = $this_in_tmp['number'] ?? 0;//本月入的数量
  1057. $this_in_price = floatval($this_in_number) ? bcdiv($this_in_money,$this_in_number,2) : 0;//本月入单价
  1058. $data['data'][$key]['this_in_money'] = $this_in_money;
  1059. $data['data'][$key]['this_in_number'] = $this_in_number;
  1060. $data['data'][$key]['this_in_price'] = $this_in_price;
  1061. //本月出库
  1062. $this_out_tmp = $out[$value['id']] ?? [];
  1063. $this_out_money = $this_out_tmp['total'] ?? 0;//本月出的金额
  1064. $this_out_number = $this_out_tmp['number'] ?? 0;//本月出的数量
  1065. //单价= (上月结存金额+本月入库金额) /上月结存数量+本月入库数量
  1066. $amount = bcadd($last_jc_money, $this_in_money,2);
  1067. $number = bcadd($last_jc_number, $this_in_number,2);
  1068. $this_out_price = floatval($number) ? bcdiv($amount,$number,2) : 0;//本月出单价
  1069. $data['data'][$key]['this_out_money'] = $this_out_money;
  1070. $data['data'][$key]['this_out_number'] = $this_out_number;
  1071. $data['data'][$key]['this_out_price'] = $this_out_price;
  1072. //本月结存
  1073. //本月结存 数量/金额=本月入库+上月结存+(-本月出库)
  1074. $this_jc_money = bcsub(bcadd($this_in_money,$last_jc_money,2),$this_out_money,2);
  1075. $this_jc_number = bcadd(bcadd($this_in_number,$last_jc_number,2),$this_out_number,2);
  1076. $this_jc_price = floatval($this_jc_number) ? bcdiv($this_jc_money,$this_jc_number,2) : 0;//本月结存单价
  1077. $data['data'][$key]['this_jc_money'] = $this_jc_money;
  1078. $data['data'][$key]['this_jc_number'] = $this_jc_number;
  1079. $data['data'][$key]['this_jc_price'] = $this_jc_price;
  1080. }
  1081. return $data;
  1082. }
  1083. //本月入库 出库(库存流水)
  1084. public function getThisMonthData($product = [], $user = [], $search = []){
  1085. $in = $out = [];
  1086. $startStamp = strtotime(date("Y-m-01 00:00:00"));
  1087. $endStamp = strtotime(date("Y-m-t 23:59:59"));
  1088. //本月出和入的数据
  1089. $model = InOutRecord::TopClear($user,$search);
  1090. $list = $model->where('del_time',0)
  1091. ->where('crt_time','>=',$startStamp)
  1092. ->where('crt_time','<=',$endStamp)
  1093. ->whereIn('product_id',$product)
  1094. ->select('product_id','number','price')
  1095. ->get()->toArray();
  1096. foreach ($list as $value){
  1097. if($value['number'] >= 0){
  1098. $tmp_total = bcmul($value['number'], $value['price'], 2);
  1099. if(isset($in[$value['product_id']])){
  1100. $number = bcadd($in[$value['product_id']]['number'], $value['number'],2);
  1101. $total = bcadd($in[$value['product_id']]['total'], $tmp_total,2);
  1102. $in[$value['product_id']]['number'] = $number;
  1103. $in[$value['product_id']]['total'] = $total;
  1104. }else{
  1105. $in[$value['product_id']] = [
  1106. 'number' => $value['number'],
  1107. 'total' => $tmp_total,
  1108. ];
  1109. }
  1110. }else{
  1111. $number = abs($value['number']);
  1112. $tmp_total = bcmul($number, $value['price'], 2);
  1113. if(isset($out[$value['product_id']])){
  1114. $number = bcadd($out[$value['product_id']]['number'], $number,2);
  1115. $total = bcadd($out[$value['product_id']]['total'], $tmp_total,2);
  1116. $out[$value['product_id']]['number'] = $number;
  1117. $out[$value['product_id']]['total'] = $total;
  1118. }else{
  1119. $out[$value['product_id']] = [
  1120. 'number' => $number,
  1121. 'total' => $tmp_total,
  1122. ];
  1123. }
  1124. }
  1125. }
  1126. return [$in, $out];
  1127. }
  1128. //上月结存(汇总这个月之前的出入)(库存流水)
  1129. public function getLastMonthBalance($product = [], $user = [], $search = []){
  1130. //用户提交的上月结存
  1131. $lastData = $this->getLastMonthJc($product,$user,$search);
  1132. $startStamp = strtotime(date("Y-m-01 00:00:00"));
  1133. $model = InOutRecord::TopClear($user,$search);
  1134. $list = $model->where('del_time',0)
  1135. ->where('crt_time','<',$startStamp)
  1136. ->whereIn('product_id',$product)
  1137. ->select('product_id','number','price','top_depart_id')
  1138. ->get()->toArray();
  1139. $map = [];
  1140. foreach ($list as $val){
  1141. $map[$val['product_id'] . $val['top_depart_id']] = $val;
  1142. }
  1143. //先结存表
  1144. $in = $out = [];
  1145. foreach ($lastData as $value){
  1146. $key = $value['product_id'] . '|' . $value['top_depart_id'];
  1147. if($value['number'] >= 0){
  1148. if(isset($in[$key])){
  1149. $number = bcadd($in[$key]['number'], $value['number'],2);
  1150. $total = bcadd($in[$key]['total'], $value['total'],2);
  1151. $in[$key]['number'] = $number;
  1152. $in[$key]['total'] = $total;
  1153. }else{
  1154. $in[$key] = [
  1155. 'number' => $value['number'],
  1156. 'total' => $value['total'],
  1157. ];
  1158. }
  1159. }else{
  1160. $number = abs($value['number']);
  1161. $total = abs($value['total']);
  1162. if(isset($out[$key])){
  1163. $number = bcadd($out[$key]['number'], $number,2);
  1164. $total = bcadd($out[$key]['total'], $total,2);
  1165. $out[$key]['number'] = $number;
  1166. $out[$key]['total'] = $total;
  1167. }else{
  1168. $out[$key] = [
  1169. 'number' => $number,
  1170. 'total' => $total,
  1171. ];
  1172. }
  1173. }
  1174. }
  1175. //后库存流水
  1176. foreach ($list as $value){
  1177. $key = $value['product_id'] . '|' . $value['top_depart_id'];
  1178. if($value['number'] >= 0){
  1179. if(isset($in[$key])) continue;
  1180. $tmp_total = bcmul($value['number'], $value['price'], 2);
  1181. if(isset($in[$value['product_id']])){
  1182. $number = bcadd($in[$value['product_id']]['number'], $value['number'],2);
  1183. $total = bcadd($in[$value['product_id']]['total'], $tmp_total,2);
  1184. $in[$value['product_id']]['number'] = $number;
  1185. $in[$value['product_id']]['total'] = $total;
  1186. }else{
  1187. $in[$value['product_id']] = [
  1188. 'number' => $value['number'],
  1189. 'total' => $tmp_total,
  1190. ];
  1191. }
  1192. }else{
  1193. if(isset($out[$key])) continue;
  1194. $number = abs($value['number']);
  1195. $tmp_total = bcmul($number, $value['price'], 2);
  1196. if(isset($out[$value['product_id']])){
  1197. $number = bcadd($out[$value['product_id']]['number'], $number,2);
  1198. $total = bcadd($out[$value['product_id']]['total'], $tmp_total,2);
  1199. $out[$value['product_id']]['number'] = $number;
  1200. $out[$value['product_id']]['total'] = $total;
  1201. }else{
  1202. $out[$value['product_id']] = [
  1203. 'number' => $number,
  1204. 'total' => $tmp_total,
  1205. ];
  1206. }
  1207. }
  1208. }
  1209. //两个数组组合过滤
  1210. $new_in = $new_out = [];
  1211. foreach ($in as $key => $value){
  1212. $tmp = explode('|', $key);
  1213. $product_id = $tmp[0];
  1214. if(isset($new_in[$product_id])){
  1215. $number = bcadd($new_in[$product_id]['number'], $value['number'],2);
  1216. $total = bcadd($new_in[$product_id]['total'], $value['total'],2);
  1217. $new_in[$product_id] = [
  1218. 'number' => $number,
  1219. 'total' => $total,
  1220. ];
  1221. }else{
  1222. $new_in[$product_id] = [
  1223. 'number' => $value['number'],
  1224. 'total' => $value['total'],
  1225. ];
  1226. }
  1227. }
  1228. foreach ($out as $key => $value){
  1229. $tmp = explode('|', $key);
  1230. $product_id = $tmp[0];
  1231. if(isset($new_out[$product_id])){
  1232. $number = bcadd($new_out[$product_id]['number'], $value['number'],2);
  1233. $total = bcadd($new_out[$product_id]['total'], $value['total'],2);
  1234. $new_out[$product_id] = [
  1235. 'number' => $number,
  1236. 'total' => $total,
  1237. ];
  1238. }else{
  1239. $new_out[$product_id] = [
  1240. 'number' => $value['number'],
  1241. 'total' => $value['total'],
  1242. ];
  1243. }
  1244. }
  1245. return [$new_in, $new_out];
  1246. }
  1247. public function getLastMonthJc($product = [], $user = [], $search = []){
  1248. // 如果存在上月结存数据则使用这个
  1249. $top_depart_id = 0;
  1250. if(! empty($search['top_depart_id'])) $top_depart_id = $search['top_depart_id'];
  1251. $startStamp = strtotime(date("Y-m-01 00:00:00"));
  1252. $result = LastJc::where('del_time',0)
  1253. ->whereIn('product_id',$product)
  1254. ->where('time','<',$startStamp)
  1255. ->when(! empty($top_depart_id), function ($query) use ($top_depart_id) {
  1256. return $query->where('top_depart_id',$top_depart_id);
  1257. })
  1258. ->select('product_id','top_depart_id','total','number','price')
  1259. ->orderBy('time','desc')
  1260. ->get()->toArray();
  1261. $return = [];
  1262. $tmp = [];
  1263. foreach ($result as $value){
  1264. $key = $value['product_id'] . $value['top_depart_id'];
  1265. if(in_array($key, $tmp)) continue;
  1266. $return[] = $value;
  1267. $tmp[] = $key;
  1268. }
  1269. return $return;
  1270. }
  1271. //本月入库(单据)暂时没用
  1272. public function getThisMonthIn1($product = [], $user = [], $search = []){
  1273. $return = [];
  1274. $startStamp = strtotime(date("Y-m-01 00:00:00"));
  1275. $endStamp = strtotime(date("Y-m-t 23:59:59"));
  1276. //本月采购单
  1277. $model = PurchaseOrder::Clear($user,$search);
  1278. $list = $model->where('del_time',0)
  1279. ->where('state', PurchaseOrder::STATE_Four)
  1280. ->where('crt_time','>=',$startStamp)
  1281. ->where('crt_time','<=',$endStamp)
  1282. ->select('id')
  1283. ->get()->toArray();
  1284. if(empty($list)) return $return;
  1285. //本月采购产品
  1286. $purchase_product_array = [];
  1287. $purchase_product = PurchaseOrderInfo::where('del_time',0)
  1288. ->whereIn('product_id',$product)
  1289. ->whereIn('purchase_order_id',array_column($list,'id'))
  1290. ->select('product_id','number','price')
  1291. ->get()->toArray();
  1292. foreach ($purchase_product as $value){
  1293. $total = bcmul($value['number'],$value['price'],2);
  1294. if(isset($purchase_product_array[$value['product_id']])){
  1295. $purchase_product_array[$value['product_id']]['number'] += $value['number'];
  1296. $total_tmp = bcadd($purchase_product_array[$value['product_id']]['total'], $total, 2);
  1297. $purchase_product_array[$value['product_id']]['total'] = $total_tmp;
  1298. }else{
  1299. $purchase_product_array[$value['product_id']] = [
  1300. 'number' => $value['number'],
  1301. 'total' => $total,
  1302. ];
  1303. }
  1304. }
  1305. //本月退货(采购)
  1306. $model2 = ReturnExchangeOrder::Clear($user, $search);
  1307. $return_list = $model2->where('del_time',0)
  1308. ->where('state', ReturnExchangeOrder::State_two)
  1309. ->where('type',ReturnExchangeOrder::Order_type2)
  1310. ->where('crt_time','>=',$startStamp)
  1311. ->where('crt_time','<=',$endStamp)
  1312. ->select('id')
  1313. ->get()->toArray();
  1314. //本月退货产品
  1315. $return_product_array = [];
  1316. if(! empty($return_list)){
  1317. $return_product = ReturnExchangeOrderProductInfo::where('del_time',0)
  1318. ->where('return_exchange_id', array_column($return_list, 'id'))
  1319. ->whereIn('product_id',$product)
  1320. ->where('return_or_exchange',ReturnExchangeOrderProductInfo::type_one)
  1321. ->select('product_id','number','return_or_exchange')
  1322. ->get()->toArray();
  1323. foreach ($return_product as $value){
  1324. $total = bcmul($value['number'],$value['return_or_exchange'],2);
  1325. if(isset($return_product_array[$value['product_id']])){
  1326. $return_product_array[$value['product_id']]['number'] += $value['number'];
  1327. $total_tmp = bcadd($return_product_array[$value['product_id']]['total'], $total, 2);
  1328. $return_product_array[$value['product_id']]['total'] = $total_tmp;
  1329. }else{
  1330. $return_product_array[$value['product_id']] = [
  1331. 'number' => $value['number'],
  1332. 'total' => $total,
  1333. ];
  1334. }
  1335. }
  1336. }
  1337. foreach ($return_product_array as $p => $n){
  1338. $number_tmp = -$n['number'];
  1339. $total_tmp = -$n['total'];
  1340. $purchase_product_tmp = $purchase_product_array[$p] ?? [];
  1341. if(empty($purchase_product_tmp)){
  1342. $purchase_product_array[$p] = [
  1343. 'number' => $number_tmp,
  1344. 'total' => $total_tmp,
  1345. ];
  1346. }else{
  1347. $purchase_product_array[$p]['number'] += $number_tmp;
  1348. $total_tmp2 = bcadd($purchase_product_array[$p]['total'], $total_tmp, 2);
  1349. $purchase_product_array[$p]['total'] += $total_tmp2;
  1350. }
  1351. }
  1352. return $purchase_product_array;
  1353. }
  1354. //新的进销存统计通用搜索底层抽象
  1355. public function statisticsJcNewCommonOrigin($data,$user,$field = []){
  1356. if(! empty($data['count_month'])) {
  1357. $startStamp = $this->changeDateToDate($data['count_month']);
  1358. }else{
  1359. $startStamp = strtotime(date("Y-m-01 00:00:00"));
  1360. }
  1361. $endTimeStamp = strtotime('first day of next month', $startStamp) - 1;
  1362. //-------- 结存数量汇总 ---------- //
  1363. //上月结存
  1364. $last_month_stock = "SUM(CASE WHEN crt_time < $startStamp THEN number ELSE 0 END)";
  1365. //本月入库
  1366. $this_month_in = "SUM(CASE WHEN crt_time >= $startStamp AND crt_time <= $endTimeStamp AND number > 0 THEN number ELSE 0 END)";
  1367. //本月出库
  1368. $this_month_out = "SUM(CASE WHEN crt_time >= $startStamp AND crt_time <= $endTimeStamp AND number < 0 THEN number ELSE 0 END)";
  1369. //本月结存
  1370. $this_month_stock = "($last_month_stock + $this_month_in + $this_month_out)";
  1371. //-------- 结存数量汇总 ---------- //
  1372. //-------- 结存金额汇总 ---------- //
  1373. $order_type = ProductAdjustment::prefix;
  1374. //上月结存
  1375. $last_month_stock_m = "ROUND(
  1376. SUM(
  1377. CASE
  1378. WHEN crt_time < $startStamp and number <> 0 THEN (number * price)
  1379. WHEN crt_time < $startStamp and number = 0 and order_type = '$order_type' THEN price
  1380. ELSE 0
  1381. END
  1382. ),
  1383. 2)";
  1384. //本月入库
  1385. $this_month_in_m = "ROUND(
  1386. SUM(
  1387. CASE
  1388. WHEN crt_time >= $startStamp and crt_time <= $endTimeStamp and number > 0 THEN (number * price)
  1389. WHEN crt_time >= $startStamp and crt_time <= $endTimeStamp and number = 0 and price >= 0 and order_type = '$order_type' THEN price
  1390. ELSE 0
  1391. END
  1392. ),
  1393. 2)";
  1394. //本月出库
  1395. $this_month_out_m = "ROUND(
  1396. SUM(
  1397. CASE
  1398. WHEN crt_time >= $startStamp and crt_time <= $endTimeStamp and number < 0 THEN (number * price)
  1399. WHEN crt_time >= $startStamp and crt_time <= $endTimeStamp and number = 0 and price < 0 and order_type = '$order_type' THEN price
  1400. ELSE 0
  1401. END
  1402. ),
  1403. 2)";
  1404. //上月结存
  1405. // $last_month_stock_m = "ROUND(SUM(CASE WHEN crt_time < $startStamp THEN (number * price) ELSE 0 END), 2)";
  1406. // //本月入库
  1407. // $this_month_in_m = "ROUND(SUM(CASE WHEN crt_time >= $startStamp AND number > 0 THEN (number * price) ELSE 0 END), 2)";
  1408. // //本月出库
  1409. // $this_month_out_m = "ROUND(SUM(CASE WHEN crt_time >= $startStamp AND number < 0 THEN (number * price) ELSE 0 END), 2)";
  1410. //-------- 结存金额汇总 ---------- //
  1411. if(empty($field)){
  1412. $field = ['product_id as id',
  1413. DB::raw("$last_month_stock as last_jc_number"),
  1414. DB::raw("$last_month_stock_m as last_jc_money"),
  1415. DB::raw("$this_month_in as this_in_number"),
  1416. DB::raw("$this_month_in_m as this_in_money"),
  1417. DB::raw("$this_month_out as this_out_number"),
  1418. DB::raw("$this_month_out_m as this_out_money"),
  1419. // DB::raw("$this_month_stock as this_jc_number")];
  1420. ];
  1421. }
  1422. $model = InOutRecord::TopClear($user,$data);
  1423. $model = $model->where('del_time',0)
  1424. ->select($field)
  1425. ->groupBy('product_id')
  1426. ->havingRaw("$last_month_stock != 0 OR $this_month_in != 0 OR $this_month_out != 0 OR $this_month_stock != 0");
  1427. return $model;
  1428. }
  1429. //新的进销存统计通用搜索
  1430. public function statisticsJcNewCommon($data,$user, $field = []){
  1431. $model = $this->statisticsJcNewCommonOrigin($data, $user, $field);
  1432. if(! empty($data['product_name']) || ! empty($data['product_category_name']) || ! empty($data['code'])) {
  1433. $id = $this->searchForProduct($data, $user);
  1434. $model->whereIn('product_id', $id);
  1435. }
  1436. return $model;
  1437. }
  1438. //新的进销存统计
  1439. public function statisticsJcNew($data,$user){
  1440. $model = $this->statisticsJcNewCommon($data, $user);
  1441. $model->orderBy('product_id','desc');
  1442. $list = $this->limit($model,'',$data);
  1443. $list = $this->fillDataNew($list);
  1444. return [true, $list];
  1445. }
  1446. public function fillDataNew($data){
  1447. if(empty($data['data'])) return $data;
  1448. //产品
  1449. $map = (new ProductService())->getProductDetail(array_column($data['data'],'id'));
  1450. foreach ($data['data'] as $key => $value){
  1451. //上月结存
  1452. $last_jc_price = floatval($value['last_jc_number']) ? bcdiv($value['last_jc_money'],$value['last_jc_number'],2) : 0;//结存单价
  1453. $data['data'][$key]['last_jc_price'] = $last_jc_price;
  1454. //本月入库
  1455. $this_in_price = floatval($value['this_in_number']) ? bcdiv($value['this_in_money'],$value['this_in_number'],2) : 0;//本月入单价
  1456. $data['data'][$key]['this_in_price'] = $this_in_price;
  1457. //本月出库
  1458. //单价= (上月结存金额+本月入库金额) /上月结存数量+本月入库数量
  1459. $amount = bcadd($value['last_jc_money'], $value['this_in_money'],2);
  1460. $number = bcadd($value['last_jc_number'], $value['this_in_number'],2);
  1461. $this_out_price = floatval($number) ? bcdiv($amount,$number,2) : 0;//本月出单价
  1462. $data['data'][$key]['this_out_price'] = $this_out_price;
  1463. //本月结存
  1464. //本月结存 数量/金额=本月入库+上月结存+(-本月出库)
  1465. $this_jc_money = bcadd(bcadd($value['this_in_money'],$value['last_jc_money'],2),$value['this_out_money'],2);
  1466. $this_jc_number = bcadd(bcadd($value['this_in_number'],$value['last_jc_number'],2),$value['this_out_number'],2);
  1467. $this_jc_price = floatval($this_jc_number) ? bcdiv($this_jc_money,$this_jc_number,2) : 0;//本月结存单价
  1468. $data['data'][$key]['this_jc_money'] = $this_jc_money;
  1469. $data['data'][$key]['this_jc_number'] = $this_jc_number;
  1470. $data['data'][$key]['this_jc_price'] = $this_jc_price;
  1471. //产品
  1472. $tmp = $map[$value['id']] ?? [];
  1473. $data['data'][$key]['title'] = $tmp['title'] ?? "";
  1474. $data['data'][$key]['code'] = $tmp['code'] ?? "";
  1475. //负数改为正数
  1476. $data['data'][$key]['this_out_number'] = abs($value['this_out_number']);
  1477. $data['data'][$key]['this_out_money'] = abs($value['this_out_money']);
  1478. }
  1479. return $data;
  1480. }
  1481. public function searchForProduct($data, $user){
  1482. $search = [];
  1483. if(! empty($data['product_name'])) $search['title'] = $data['product_name'];
  1484. if(! empty($data['product_category_name'])) $search['product_category'] = $data['product_category_name'];
  1485. if(! empty($data['code'])) $search['code'] = $data['code'];
  1486. $model = (new ProductService())->productCommon($search, $user, ['id']);
  1487. $product = $model->get()->toArray();
  1488. return array_column($product,'id');
  1489. }
  1490. public function statisticsAreaDepartProduct222($data,$user){
  1491. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
  1492. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  1493. if(empty($data['top_depart_id'])) return [false, '请选择门店'];
  1494. //向总社采购的钱
  1495. $purchase_map = [];
  1496. $purchase = PurchaseOrder::where('del_time',0)
  1497. ->where('top_depart_id',$data['top_depart_id'])
  1498. ->where('order_type',[PurchaseOrder::Order_type_three,PurchaseOrder::Order_type_four])
  1499. ->where('crt_time','>=',$return[0])
  1500. ->where('crt_time','<=',$return[1])
  1501. ->select('id')
  1502. ->get()->toArray();
  1503. $purchase_product = PurchaseOrderInfo::where("del_time",0)
  1504. ->whereIn('purchase_order_id',array_column($purchase,'id'))
  1505. ->select('product_id','number','price','final_amount','sports_bag_id')
  1506. ->get()->toArray();
  1507. foreach ($purchase_product as $value){
  1508. if($value['sports_bag_id'] > 0){
  1509. $money = $value['final_amount'];
  1510. }elseif($value['final_amount'] > 0){
  1511. $money = $value['final_amount'];
  1512. }else{
  1513. $money = bcmul($value['price'],$value['number'],2);
  1514. }
  1515. if(isset($purchase_map[$value['product_id']])){
  1516. $tmp_money = bcadd($purchase_map[$value['product_id']]['total'], $money,2);
  1517. $tmp_number = bcadd($purchase_map[$value['product_id']]['number'], $value['number'],2);
  1518. $purchase_map[$value['product_id']] = [
  1519. 'number' => $tmp_number,
  1520. 'total' => $tmp_money
  1521. ];
  1522. }else{
  1523. $purchase_map[$value['product_id']] = [
  1524. 'number' => $value['number'],
  1525. 'total' => $money
  1526. ];
  1527. }
  1528. }
  1529. //退货的差异
  1530. // $returnExchange_map = $this->returnExchange($data,$return);
  1531. //产品
  1532. $product_list = Product::whereIn('id',array_unique(array_merge_recursive(array_column($purchase_product,'product_id'),array_keys([],'product_id'))))
  1533. ->select('id','product_category','title')
  1534. ->get()->toArray();
  1535. $product_list_map = array_column($product_list,null,'id');
  1536. $category = $category_sum = [];
  1537. foreach ($purchase_map as $key => $value){
  1538. // if(isset($returnExchange_map[$key])){
  1539. // $tmp = $returnExchange_map[$key];
  1540. // $number = bcsub($value['number'], $tmp['number'],2);
  1541. // $total = bcsub($value['total'], $tmp['total'],2);
  1542. // $purchase_map[$key] = [
  1543. // 'number' => $number,
  1544. // 'total' => $total,
  1545. // ];
  1546. // }
  1547. $product_tmp = $product_list_map[$key] ?? [];
  1548. $purchase_map[$key]['title'] = $product_tmp['title'];
  1549. $category_tmp = json_decode($product_tmp['product_category']);
  1550. $category_tmp = $category_tmp[0];
  1551. if(! in_array($category_tmp,$category)) $category[] = $category_tmp;
  1552. if(isset($category_sum[$category_tmp])){
  1553. $tmp_number = bcadd($category_sum[$category_tmp]['number'], $purchase_map[$key]['number'],2);
  1554. $tmp_total = bcadd($category_sum[$category_tmp]['total'], $purchase_map[$key]['total'],2);
  1555. $category_sum[$category_tmp] = [
  1556. 'number' => $tmp_number,
  1557. 'total' => $tmp_total,
  1558. ];
  1559. }else{
  1560. $category_sum[$category_tmp] = [
  1561. 'number' => $purchase_map[$key]['number'],
  1562. 'total' => $purchase_map[$key]['total'],
  1563. ];
  1564. }
  1565. }
  1566. //根据产品大类 $category_sum
  1567. $category_return = ProductCategory::whereIn('id',$category)
  1568. ->select('id','title')
  1569. ->get()->toArray();
  1570. foreach ($category_return as $key => $value){
  1571. $tmp = $category_sum[$value['id']] ?? [];
  1572. $category_return[$key]['number'] = $tmp['number'];
  1573. $category_return[$key]['total'] = $tmp['total'];
  1574. }
  1575. //根据产品 $purchase_map
  1576. dd($purchase_map);
  1577. return [true, ''];
  1578. }
  1579. public function statisticsProvince2222($data,$user){
  1580. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
  1581. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  1582. //门店设置的指标 按照区域汇总
  1583. $index_map = DepartIndex::where('del_time',0)
  1584. ->pluck('param_one','top_depart_id')
  1585. ->toArray();
  1586. //分社所属省
  1587. $depart_map = $province_map = [];
  1588. $depart = Depart::where('parent_id',0)
  1589. ->where('province','<>','')
  1590. ->where('del_time',0)
  1591. ->select('province','id')
  1592. ->get()->toArray();
  1593. foreach ($depart as $value){
  1594. $depart_map[$value['id']] = $value['province'];
  1595. $province_map[$value['province']][] = $value['id'];
  1596. }
  1597. $address_map = config('address');
  1598. $address_map = array_column($address_map,'label','value');
  1599. //大区
  1600. $final_address_map = [];
  1601. foreach ($address_map as $key => $value){
  1602. $tmp = [
  1603. 'key' => $key,
  1604. 'title' => $value,
  1605. 'total' => 0,
  1606. 'index' => 0
  1607. ];
  1608. $top_depart_id = $province_map[$key] ?? [];
  1609. if(! empty($top_depart_id)){
  1610. foreach ($top_depart_id as $depart_id){
  1611. $index = $index_map[$depart_id] ?? 0;
  1612. $tmp['index'] = bcadd($tmp['index'], $index,2);
  1613. }
  1614. }
  1615. $final_address_map[] = $tmp;
  1616. }
  1617. //向总社采购的钱
  1618. $purchase_map = [];
  1619. $purchase = PurchaseOrder::where('del_time',0)
  1620. ->where('order_type',[PurchaseOrder::Order_type_three,PurchaseOrder::Order_type_four])
  1621. ->where('crt_time','>=',$return[0])
  1622. ->where('crt_time','<=',$return[1])
  1623. ->select('top_depart_id','purchase_total')
  1624. ->get()->toArray();
  1625. foreach ($purchase as $value){
  1626. $area = $depart_map[$value['top_depart_id']] ?? 0;
  1627. if(! $area) continue;
  1628. if(isset($purchase_map[$area])){
  1629. $total = bcadd($purchase_map[$area], $value['purchase_total'],2);
  1630. $purchase_map[$area] = $total;
  1631. }else{
  1632. $purchase_map[$area] = $value['purchase_total'];
  1633. }
  1634. }
  1635. //退货的差异
  1636. $returnExchange_map = [];
  1637. $returnExchange = ReturnExchangeOrder::where('del_time',0)
  1638. ->where('type',ReturnExchangeOrder::Order_type2)
  1639. ->where('crt_time','>=',$return[0])
  1640. ->where('crt_time','<=',$return[1])
  1641. ->where('crt_time','<=',$return[1])
  1642. ->select('top_depart_id','difference_amount')
  1643. ->get()->toArray();
  1644. foreach ($returnExchange as $value){
  1645. $area = $depart_map[$value['top_depart_id']] ?? 0;
  1646. if(! $area) continue;
  1647. if(isset($returnExchange_map[$area])){
  1648. $total = bcadd($returnExchange_map[$area], $value['difference_amount'],2);
  1649. $returnExchange_map[$area] = $total;
  1650. }else{
  1651. $returnExchange_map[$area] = $value['difference_amount'];
  1652. }
  1653. }
  1654. foreach ($final_address_map as $key => $value){
  1655. $purchase_tmp = $purchase_map[$value['key']] ?? 0;
  1656. $return_tmp = $returnExchange_map[$value['key']] ?? 0;
  1657. $final_address_map[$key]['total'] = bcsub($purchase_tmp, $return_tmp,2);
  1658. }
  1659. return [true, $final_address_map];
  1660. }
  1661. public function fillStatisticsBt1($data){
  1662. if(empty($data)) return $data;
  1663. $total = floatval(array_sum(array_column($data,'total')));
  1664. // 设置一个最小显示阈值,比如0.5%
  1665. $threshold = 0.5;
  1666. // 用于存储调整后的数据
  1667. $adjustedData = [];
  1668. $otherTotal = 0;
  1669. $other = [];
  1670. foreach ($data as $value) {
  1671. $model_type_title = SalesOrder::$model_type_title[$value['model_type']] ?? "";
  1672. $rate = $total ? $value['total'] / $total : 0;
  1673. // 检查是否低于阈值
  1674. if ($rate * 100 < $threshold) {
  1675. // 小于阈值的部分加到"其他"中
  1676. $otherTotal += $value['total'];
  1677. $other[] = $model_type_title;
  1678. } else {
  1679. // 保留更多小数位数
  1680. $adjustedData[] = [
  1681. 'model_type' => $value['model_type'],
  1682. 'total' => $value['total'],
  1683. 'model_type_title' => $model_type_title,
  1684. 'rate' => round($rate * 100, 2)
  1685. ];
  1686. }
  1687. }
  1688. // 如果有"其他"值,则添加到数据集中
  1689. if ($otherTotal > 0) {
  1690. $other_title = implode(',',$other);
  1691. $adjustedData[] = [
  1692. 'model_type' => -1, // 自定义一个类型标识
  1693. 'total' => $otherTotal,
  1694. 'model_type_title' => '其他(' . $other_title . ')',
  1695. 'rate' => round($otherTotal / $total * 100, 2)
  1696. ];
  1697. }
  1698. return $adjustedData;
  1699. }
  1700. private function aboutTime($return){
  1701. $day = $this->returnDays($return);
  1702. if($day > 60) return [false, '查询时间仅支持范围区间在两月(60天)内'];
  1703. return [true ,''];
  1704. }
  1705. //改装 客户模板 t9改装 不是分配的客户 是所有的 =》 点击省 展示市 只有这个有产品
  1706. //分社 客户模板 t9改装 不是分配的客户 是所有的 =》 点击省 展示分社
  1707. //加盟 客户模板 t9分社 不是分配的客户 是所有的 =》 点击省 展示市
  1708. //销售 不是分配的客户 是所有的 =》 点击分社 展示销售人员(客户的负责人)
  1709. public function customerReportStepOfFirst($data, $user){
  1710. if(empty($data['enter_time'][0]) || empty($data['enter_time'][1])) return [false, '请选择日期'];
  1711. $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);
  1712. list($status, $msg) = $this->aboutTime($return);
  1713. if(! $status) return [false, $msg];
  1714. if(empty($data['type'])) return [false, '请选择报表查看类型'];
  1715. $type = $data['type'];
  1716. $result = [];
  1717. if($type == 1){
  1718. $type_detail = 1;
  1719. if(! empty($data['type_detail'])) $type_detail = $data['type_detail'];
  1720. if($type_detail == 1){
  1721. $result = $this->customerReportStepOneForProvince($return,$type);
  1722. }else{
  1723. $result = $this->customerReportStepOneForProduct($return);
  1724. }
  1725. }elseif ($type == 2){
  1726. $result = $this->customerReportStepOneForProvinceFs($return);
  1727. }elseif ($type == 3){
  1728. $result = $this->customerReportStepOneForProvince($return,$type);
  1729. }elseif ($type == 4){
  1730. $result = $this->customerReportStepOneForProvinceXs($return);
  1731. }else{
  1732. return [false ,'报表查看类型错误'];
  1733. }
  1734. return [true, ['list' => $result]];
  1735. }
  1736. private function customerReportStepOneForProduct($time){
  1737. //基础产品数据
  1738. $basic_all_data = BasicTypeAllUse::where('del_time',0)
  1739. ->where('type', BasicTypeAllUse::type_one)
  1740. ->select('title','id')
  1741. ->get()->toArray();
  1742. $basic_all = [];
  1743. foreach ($basic_all_data as $value){
  1744. $basic_all[] = [
  1745. 'label' => $value['title'],
  1746. 'value' => $value['id'],
  1747. 'all_num' => 0
  1748. ];
  1749. }
  1750. $start = $time[0];
  1751. $end = $time[1];
  1752. $model_type = Customer::Model_type_one;
  1753. $label = "所有产品";
  1754. $total = [
  1755. 'label' => $label,
  1756. 'value' => "",
  1757. 'all_num' => 0
  1758. ];
  1759. $list = CustomerReport::where('del_time',0)
  1760. ->where('enter_time','>=', $start)
  1761. ->where('enter_time','<=', $end)
  1762. ->where('model_type',$model_type)
  1763. ->select('consulting_product_new')
  1764. ->get()->toArray();
  1765. $map = [];
  1766. foreach ($list as $value) {
  1767. if(isset($map[$value['consulting_product_new']])) {
  1768. $map[$value['consulting_product_new']] += 1;
  1769. }else{
  1770. $map[$value['consulting_product_new']] = 1;
  1771. }
  1772. $total['all_num'] += 1;
  1773. }
  1774. foreach ($basic_all as $key => $value){
  1775. if(isset($map[$value['value']])){
  1776. $tmp = $map[$value['value']];
  1777. $basic_all[$key]['all_num'] = $tmp;
  1778. }
  1779. }
  1780. usort($basic_all, function($a, $b) {
  1781. return $b['all_num'] - $a['all_num'];
  1782. });
  1783. array_unshift($basic_all, $total);
  1784. return $basic_all;
  1785. }
  1786. private function customerReportStepOneForProvince($time, $type){
  1787. //省市数据
  1788. $addressData = config('address3');
  1789. if(is_string($addressData)) $addressData = json_decode($addressData, true);
  1790. $province = [];
  1791. foreach ($addressData as $value){
  1792. $province[] = [
  1793. 'label' => $value['label'],
  1794. 'value' => $value['value'],
  1795. 'follow_num' => 0,
  1796. 'all_num' => 0
  1797. ];
  1798. }
  1799. $start = $time[0];
  1800. $end = $time[1];
  1801. $model_type = Customer::Model_type_one;
  1802. if($type == 3) $model_type = Customer::Model_type_three;
  1803. $label = "所有客资";
  1804. $total = [
  1805. 'label' => $label,
  1806. 'value' => "",
  1807. 'follow_num' => 0,
  1808. 'all_num' => 0
  1809. ];
  1810. $list = CustomerReport::where('del_time',0)
  1811. ->where('enter_time','>=', $start)
  1812. ->where('enter_time','<=', $end)
  1813. ->where('model_type',$model_type)
  1814. ->where('province_code','<>','')
  1815. ->select('province_code','follow_num')
  1816. ->get()->toArray();
  1817. $map = [];
  1818. foreach ($list as $value) {
  1819. if(isset($map[$value['province_code']])) {
  1820. $tmp = bcadd($value['follow_num'], $map[$value['province_code']]['num_1']);
  1821. $map[$value['province_code']]['num_1'] = $tmp;
  1822. $map[$value['province_code']]['num_2'] += 1;
  1823. }else{
  1824. $map[$value['province_code']] = [
  1825. 'num_1' => $value['follow_num'],
  1826. 'num_2' => 1,
  1827. ];
  1828. }
  1829. $tmp_t = bcadd($total['follow_num'], $value['follow_num']);
  1830. $total['follow_num'] = $tmp_t;
  1831. $total['all_num'] += 1;
  1832. }
  1833. foreach ($province as $key => $value){
  1834. if(isset($map[$value['value']])){
  1835. $tmp = $map[$value['value']];
  1836. $province[$key]['follow_num'] = intval($tmp['num_1']);
  1837. $province[$key]['all_num'] = $tmp['num_2'];
  1838. }
  1839. }
  1840. usort($province, function($a, $b) {
  1841. // 先比较 all_num
  1842. if ($b['all_num'] !== $a['all_num']) {
  1843. return $b['all_num'] - $a['all_num']; // all_num 降序
  1844. }
  1845. // 如果 all_num 相同,再比较 value
  1846. return $a['value'] - $b['value']; // value 正序
  1847. });
  1848. array_unshift($province, $total);
  1849. return $province;
  1850. }
  1851. private function customerReportStepOneForProvinceFs($time){
  1852. //省市数据
  1853. $addressData = config('address3');
  1854. if(is_string($addressData)) $addressData = json_decode($addressData, true);
  1855. $province = [];
  1856. foreach ($addressData as $value){
  1857. $province[] = [
  1858. 'label' => $value['label'],
  1859. 'value' => $value['value'],
  1860. 'follow_num' => 0,
  1861. 'all_num' => 0
  1862. ];
  1863. }
  1864. //门店归属省
  1865. $depart = Depart::where('del_time',0)
  1866. ->where('parent_id',0)
  1867. ->where('province','<>','')
  1868. ->select('id','province as province_code')
  1869. ->get()->toArray();
  1870. $depart_map = [];
  1871. foreach ($depart as $value){
  1872. $depart_map[$value['id']] = $value['province_code'];
  1873. }
  1874. $start = $time[0];
  1875. $end = $time[1];
  1876. $model_type = Customer::Model_type_one;
  1877. $label = "全国分社";
  1878. $total = [
  1879. 'label' => $label,
  1880. 'value' => "",
  1881. 'follow_num' => 0,
  1882. 'all_num' => 0
  1883. ];
  1884. $list = CustomerReport::where('del_time',0)
  1885. ->where('enter_time','>=', $start)
  1886. ->where('enter_time','<=', $end)
  1887. ->where('model_type',$model_type)
  1888. ->where('province_code','<>','')
  1889. ->select('province_code','follow_num','customer_id')
  1890. ->get()->toArray();
  1891. $detail = CustomerReportDepart::where('del_time',0)
  1892. ->whereIn('customer_id',array_unique(array_column($list,'customer_id')))
  1893. ->where('type',CustomerReportDepart::type_one)
  1894. ->select('customer_id','top_depart_id')
  1895. ->get()->toArray();
  1896. $detail_map = [];
  1897. foreach ($detail as $value){
  1898. $detail_map[$value['customer_id']][] = $value['top_depart_id'];
  1899. }
  1900. $map = [];
  1901. foreach ($list as $value) {
  1902. //客资所属门店
  1903. $c_belong_id = $detail_map[$value['customer_id']] ?? "";
  1904. if(empty($c_belong_id)) continue;
  1905. foreach ($c_belong_id as $id){
  1906. //客资所属门店所属省
  1907. $province_code = $depart_map[$id] ?? "";
  1908. if(empty($province_code)) continue;
  1909. if(isset($map[$province_code])) {
  1910. $tmp = bcadd($value['follow_num'], $map[$province_code]['num_1']);
  1911. $map[$province_code]['num_1'] = $tmp;
  1912. $map[$province_code]['num_2'] += 1;
  1913. }else{
  1914. $map[$province_code] = [
  1915. 'num_1' => $value['follow_num'],
  1916. 'num_2' => 1,
  1917. ];
  1918. }
  1919. $tmp_t = bcadd($total['follow_num'], $value['follow_num']);
  1920. $total['follow_num'] = $tmp_t;
  1921. $total['all_num'] += 1;
  1922. }
  1923. }
  1924. foreach ($province as $key => $value){
  1925. if(isset($map[$value['value']])){
  1926. $tmp = $map[$value['value']];
  1927. $province[$key]['follow_num'] = intval($tmp['num_1']);
  1928. $province[$key]['all_num'] = $tmp['num_2'];
  1929. }
  1930. }
  1931. usort($province, function($a, $b) {
  1932. // 先比较 all_num
  1933. if ($b['all_num'] !== $a['all_num']) {
  1934. return $b['all_num'] - $a['all_num']; // all_num 降序
  1935. }
  1936. // 如果 all_num 相同,再比较 value
  1937. return $a['value'] - $b['value']; // value 正序
  1938. });
  1939. array_unshift($province, $total);
  1940. return $province;
  1941. }
  1942. private function customerReportStepOneForProvinceXs($time){
  1943. //门店
  1944. $depart = Depart::where('del_time',0)
  1945. ->where('parent_id',0)
  1946. ->select('id','title')
  1947. ->get()->toArray();
  1948. $departArray = [];
  1949. foreach ($depart as $value){
  1950. $departArray[]= [
  1951. 'label' => $value['title'],
  1952. 'value' => $value['id'],
  1953. 'follow_num' => 0,
  1954. 'all_num' => 0
  1955. ];
  1956. }
  1957. $start = $time[0];
  1958. $end = $time[1];
  1959. $label = "所有门店";
  1960. $total = [
  1961. 'label' => $label,
  1962. 'value' => "",
  1963. 'follow_num' => 0,
  1964. 'all_num' => 0
  1965. ];
  1966. $list = CustomerReport::where('del_time',0)
  1967. ->where('enter_time','>=', $start)
  1968. ->where('enter_time','<=', $end)
  1969. ->select('follow_num','customer_id')
  1970. ->get()->toArray();
  1971. $detail = CustomerReportDepart::where('del_time',0)
  1972. ->whereIn('customer_id',array_unique(array_column($list,'customer_id')))
  1973. ->where('type',CustomerReportDepart::type_one)
  1974. ->select('customer_id','top_depart_id')
  1975. ->get()->toArray();
  1976. $detail_map = [];
  1977. foreach ($detail as $value){
  1978. $detail_map[$value['customer_id']][] = $value['top_depart_id'];
  1979. }
  1980. $map = [];
  1981. foreach ($list as $value) {
  1982. if(isset($detail_map[$value['customer_id']])){
  1983. //客资所属门店
  1984. $c_belong_id = $detail_map[$value['customer_id']];
  1985. if(empty($c_belong_id)) continue;
  1986. foreach ($c_belong_id as $id){
  1987. if(isset($map[$id])) {
  1988. $tmp = bcadd($value['follow_num'], $map[$id]['num_1']);
  1989. $map[$id]['num_1'] = $tmp;
  1990. $map[$id]['num_2'] += 1;
  1991. }else{
  1992. $map[$id] = [
  1993. 'num_1' => $value['follow_num'],
  1994. 'num_2' => 1,
  1995. ];
  1996. }
  1997. $tmp_t = bcadd($total['follow_num'], $value['follow_num']);
  1998. $total['follow_num'] = $tmp_t;
  1999. $total['all_num'] += 1;
  2000. }
  2001. }
  2002. }
  2003. foreach ($departArray as $key => $value){
  2004. if(isset($map[$value['value']])){
  2005. $tmp = $map[$value['value']];
  2006. $departArray[$key]['follow_num'] = intval($tmp['num_1']);
  2007. $departArray[$key]['all_num'] = $tmp['num_2'];
  2008. }
  2009. }
  2010. usort($departArray, function($a, $b) {
  2011. // 先比较 all_num
  2012. if ($b['all_num'] !== $a['all_num']) {
  2013. return $b['all_num'] - $a['all_num']; // all_num 降序
  2014. }
  2015. // 如果 all_num 相同,再比较 value
  2016. return $a['value'] - $b['value']; // value 正序
  2017. });
  2018. array_unshift($departArray, $total);
  2019. return $departArray;
  2020. }
  2021. public function customerReportStepOfSecond($data, $user){
  2022. if(empty($data['enter_time'][0]) || empty($data['enter_time'][1])) return [false, '请选择日期'];
  2023. $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);
  2024. list($status, $msg) = $this->aboutTime($return);
  2025. if(! $status) return [false, $msg];
  2026. if(empty($data['type'])) return [false, '请选择报表查看类型'];
  2027. if(empty($data['value'])) return [false, 'value不能为空'];
  2028. $type = $data['type'];
  2029. if($type == 1){
  2030. list($list, $total) = $this->customerReportStepSecondForCity($return,$data,$type);
  2031. }elseif ($type == 2){
  2032. list($list, $total) = $this->customerReportStepSecondForFs($return, $data);
  2033. }elseif ($type == 3){
  2034. list($list, $total) = $this->customerReportStepSecondForCity($return,$data,$type);
  2035. }elseif ($type == 4){
  2036. list($list, $total) = $this->customerReportStepSecondForProvinceXs($return,$data);
  2037. }else{
  2038. return [false ,'报表查看类型错误'];
  2039. }
  2040. return [true, ['list' => $list, 'total' => $total]];
  2041. }
  2042. private function customerReportStepSecondForCity($time, $data, $type){
  2043. $province_code = $data['value'];
  2044. //市数据
  2045. $addressData = config('address3');
  2046. if(is_string($addressData)) $addressData = json_decode($addressData, true);
  2047. $city = [];
  2048. foreach ($addressData as $value){
  2049. if($value['value'] != $province_code) continue;
  2050. foreach ($value['children'] as $val){
  2051. $city[] = [
  2052. 'label' => $val['label'],
  2053. 'value' => $val['value'],
  2054. 'follow_num' => 0,
  2055. 'all_num' => 0
  2056. ];
  2057. }
  2058. }
  2059. $start = $time[0];
  2060. $end = $time[1];
  2061. $model_type = Customer::Model_type_one;
  2062. if($type == 3) $model_type = Customer::Model_type_three;
  2063. // $label = "所有客资";
  2064. // $total = [
  2065. // 'label' => $label,
  2066. // 'value' => "",
  2067. // 'follow_num' => 0,
  2068. // 'all_num' => 0
  2069. // ];
  2070. $total = [
  2071. 'follow_num' => 0,
  2072. 'all_num' => 0
  2073. ];
  2074. $list = CustomerReport::where('del_time',0)
  2075. ->where('enter_time','>=', $start)
  2076. ->where('enter_time','<=', $end)
  2077. ->where('model_type',$model_type)
  2078. ->where('province_code', $province_code)
  2079. ->select('city_code','follow_num')
  2080. ->get()->toArray();
  2081. $map = [];
  2082. $first_city = $city[0];
  2083. foreach ($list as $value) {
  2084. if(empty($value['city_code'])) $value['city_code'] = $first_city['value'];
  2085. if(isset($map[$value['city_code']])) {
  2086. $tmp = bcadd($value['follow_num'], $map[$value['city_code']]['num_1']);
  2087. $map[$value['city_code']]['num_1'] = $tmp;
  2088. $map[$value['city_code']]['num_2'] += 1;
  2089. }else{
  2090. $map[$value['city_code']] = [
  2091. 'num_1' => $value['follow_num'],
  2092. 'num_2' => 1,
  2093. ];
  2094. }
  2095. $tmp_t = bcadd($total['follow_num'], $value['follow_num']);
  2096. $total['follow_num'] = $tmp_t;
  2097. $total['all_num'] += 1;
  2098. }
  2099. foreach ($city as $key => $value){
  2100. if(isset($map[$value['value']])){
  2101. $tmp = $map[$value['value']];
  2102. $city[$key]['follow_num'] = intval($tmp['num_1']);
  2103. $city[$key]['all_num'] = $tmp['num_2'];
  2104. }
  2105. }
  2106. usort($city, function($a, $b) {
  2107. // 先比较 all_num
  2108. if ($b['all_num'] !== $a['all_num']) {
  2109. return $b['all_num'] - $a['all_num']; // all_num 降序
  2110. }
  2111. // 如果 all_num 相同,再比较 value
  2112. return $a['value'] - $b['value']; // value 正序
  2113. });
  2114. return [$city, $total];
  2115. }
  2116. private function customerReportStepSecondForFs($time, $data){
  2117. $province_code = $data['value'];
  2118. //省下的所有门店
  2119. $depart = Depart::where('del_time',0)
  2120. ->where('parent_id',0)
  2121. ->where('province', $province_code)
  2122. ->select('id','title')
  2123. ->get()->toArray();
  2124. $depart_id = array_column($depart,'id');
  2125. $depart_array = [];
  2126. foreach ($depart as $value){
  2127. $depart_array[] = [
  2128. 'label' => $value['title'],
  2129. 'value' => $value['id'],
  2130. 'follow_num' => 0,
  2131. 'all_num' => 0
  2132. ];
  2133. }
  2134. $start = $time[0];
  2135. $end = $time[1];
  2136. $model_type = Customer::Model_type_one;
  2137. $list = CustomerReport::where('del_time',0)
  2138. ->where('enter_time','>=', $start)
  2139. ->where('enter_time','<=', $end)
  2140. ->where('model_type',$model_type)
  2141. ->where('province','<>','')
  2142. ->select('province_code','follow_num','customer_id')
  2143. ->get()->toArray();
  2144. $detail = CustomerReportDepart::where('del_time',0)
  2145. ->whereIn('customer_id',array_unique(array_column($list,'customer_id')))
  2146. ->where('type',CustomerReportDepart::type_one)
  2147. ->whereIn('top_depart_id',$depart_id)
  2148. ->select('customer_id','top_depart_id')
  2149. ->get()->toArray();
  2150. $detail_map = [];
  2151. foreach ($detail as $value){
  2152. $detail_map[$value['customer_id']][] = $value['top_depart_id'];
  2153. }
  2154. $total = [
  2155. 'follow_num' => 0,
  2156. 'all_num' => 0
  2157. ];
  2158. $map = [];
  2159. foreach ($list as $value) {
  2160. if(isset($detail_map[$value['customer_id']])){
  2161. //客资所属门店
  2162. $c_belong_id = $detail_map[$value['customer_id']] ?? [];
  2163. if(empty($c_belong_id)) continue;
  2164. foreach ($c_belong_id as $id){
  2165. if(isset($map[$id])) {
  2166. $tmp = bcadd($value['follow_num'], $map[$id]['num_1']);
  2167. $map[$id]['num_1'] = $tmp;
  2168. $map[$id]['num_2'] += 1;
  2169. }else{
  2170. $map[$id] = [
  2171. 'num_1' => $value['follow_num'],
  2172. 'num_2' => 1,
  2173. ];
  2174. }
  2175. $tmp_t = bcadd($total['follow_num'], $value['follow_num']);
  2176. $total['follow_num'] = $tmp_t;
  2177. $total['all_num'] += 1;
  2178. }
  2179. }
  2180. }
  2181. foreach ($depart_array as $key => $value){
  2182. if(isset($map[$value['value']])){
  2183. $tmp = $map[$value['value']];
  2184. $depart_array[$key]['follow_num'] = intval($tmp['num_1']);
  2185. $depart_array[$key]['all_num'] = $tmp['num_2'];
  2186. }
  2187. }
  2188. usort($depart_array, function($a, $b) {
  2189. // 先比较 all_num
  2190. if ($b['all_num'] !== $a['all_num']) {
  2191. return $b['all_num'] - $a['all_num']; // all_num 降序
  2192. }
  2193. // 如果 all_num 相同,再比较 value
  2194. return $a['value'] - $b['value']; // value 正序
  2195. });
  2196. return [$depart_array, $total];
  2197. }
  2198. private function customerReportStepSecondForProvinceXs($time,$data){
  2199. $top_depart_id = intval($data['value']);
  2200. //门店下所有部门
  2201. $all_depart_id = DB::select("
  2202. WITH RECURSIVE sub_departments AS (
  2203. SELECT id FROM depart WHERE parent_id = ? AND del_time = 0
  2204. UNION ALL
  2205. SELECT d.id FROM depart d
  2206. INNER JOIN sub_departments s ON d.parent_id = s.id
  2207. WHERE d.del_time = 0
  2208. )
  2209. SELECT id FROM sub_departments
  2210. ", [$top_depart_id]);
  2211. $allDepartIds = collect($all_depart_id)->pluck('id')->toArray();
  2212. $allDepartIds[] = $top_depart_id;
  2213. //所属于这个门店下的人
  2214. $man = EmployeeDepartPermission::whereIn('depart_id', $allDepartIds)
  2215. ->select('employee_id')
  2216. ->get()->toArray();
  2217. $man = array_column($man,'employee_id');
  2218. $employee = Employee::where('del_time',0)
  2219. ->whereIn('id', $man)
  2220. ->select('id', 'emp_name', 'number')
  2221. ->get()->toArray();
  2222. $employee_map = array_column($employee,null,'id');
  2223. $start = $time[0];
  2224. $end = $time[1];
  2225. $total = [
  2226. 'follow_num' => 0,
  2227. 'all_num' => 0
  2228. ];
  2229. $list = CustomerReport::where('del_time',0)
  2230. ->where('enter_time','>=', $start)
  2231. ->where('enter_time','<=', $end)
  2232. ->select('follow_num','customer_id')
  2233. ->get()->toArray();
  2234. $detail = CustomerReportDepart::where('del_time',0)
  2235. ->whereIn('customer_id',array_unique(array_column($list,'customer_id')))
  2236. ->select('customer_id','man_id','type','top_depart_id')
  2237. ->get()->toArray();
  2238. $manArray = $man_flag = $detail_array = $man_map = [];
  2239. foreach ($detail as $value){
  2240. if($value['type'] == CustomerReportDepart::type_two){
  2241. if(isset($employee_map[$value['man_id']])){
  2242. if(in_array($value['man_id'], $man_flag)) continue;
  2243. if(! in_array($value['man_id'], $man)) continue;
  2244. $man_flag[] = $value['man_id'];
  2245. $e_t = $employee_map[$value['man_id']];
  2246. $manArray[] = [
  2247. 'label' => $e_t['emp_name'],
  2248. 'value' => $e_t['id'],
  2249. 'follow_num' => 0,
  2250. 'all_num' => 0
  2251. ];
  2252. }
  2253. }elseif ($value['type'] == CustomerReportDepart::type_one && $value['top_depart_id'] == $top_depart_id){
  2254. if(! in_array($value['customer_id'], $detail_array)) $detail_array[] = $value['customer_id'];
  2255. }elseif ($value['type'] == CustomerReportDepart::type_three){
  2256. if(isset($man_map[$value['customer_id']][$value['man_id']])){
  2257. $man_map[$value['customer_id']][$value['man_id']] += 1;
  2258. }else{
  2259. $man_map[$value['customer_id']][$value['man_id']] = 1;
  2260. }
  2261. }
  2262. }unset($man_flag);
  2263. $map = [];
  2264. foreach ($list as $value) {
  2265. //这个客户是否属于这个门店
  2266. if(in_array($value['customer_id'], $detail_array)){
  2267. if(isset($man_map[$value['customer_id']])) {
  2268. //客户由谁创建跟进记录
  2269. $tmp = $man_map[$value['customer_id']];
  2270. foreach ($tmp as $m_id => $val){
  2271. if(isset($map[$m_id])) {
  2272. $tmp = bcadd($value['follow_num'], $map[$m_id]['num_1']);
  2273. $map[$m_id]['num_1'] = $tmp;
  2274. $map[$m_id]['num_2'] += 1;
  2275. }else{
  2276. $map[$m_id] = [
  2277. 'num_1' => $val,
  2278. 'num_2' => 1,
  2279. ];
  2280. }
  2281. $tmp_t = bcadd($total['follow_num'], $val);
  2282. $total['follow_num'] = $tmp_t;
  2283. }
  2284. }
  2285. $total['all_num'] += 1;
  2286. }
  2287. }
  2288. foreach ($manArray as $key => $value){
  2289. if(isset($map[$value['value']])){
  2290. $tmp = $map[$value['value']];
  2291. $manArray[$key]['follow_num'] = intval($tmp['num_1']);
  2292. $manArray[$key]['all_num'] = $tmp['num_2'];
  2293. }
  2294. }
  2295. usort($manArray, function($a, $b) {
  2296. // 先比较 all_num
  2297. if ($b['all_num'] !== $a['all_num']) {
  2298. return $b['all_num'] - $a['all_num']; // all_num 降序
  2299. }
  2300. // 如果 all_num 相同,再比较 value
  2301. return $a['value'] - $b['value']; // value 正序
  2302. });
  2303. return [$manArray, $total];
  2304. }
  2305. public function customerReportStepOfThird($data, $user){
  2306. if(empty($data['enter_time'][0]) || empty($data['enter_time'][1])) return [false, '请选择日期'];
  2307. $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);
  2308. list($status, $msg) = $this->aboutTime($return);
  2309. if(! $status) return [false, $msg];
  2310. if(empty($data['type'])) return [false, '请选择报表查看类型'];
  2311. if(empty($data['value'])) return [false, 'value不能为空'];
  2312. if(empty($data['value2'])) return [false, 'value2不能为空'];
  2313. $type = $data['type'];
  2314. if($type == 1){
  2315. $result = $this->customerReportStepThirdForDetail($return,$data,$type);
  2316. }elseif ($type == 2){
  2317. $result = $this->customerReportStepThirdForFsDetail($return, $data);
  2318. }elseif ($type == 3){
  2319. $result = $this->customerReportStepThirdForDetail($return,$data,$type);
  2320. }elseif ($type == 4){
  2321. $result = $this->customerReportStepThirdForProvinceXsDetail($return,$data);
  2322. }else{
  2323. return [false ,'报表查看类型错误'];
  2324. }
  2325. return [true, ['list' => $result]];
  2326. }
  2327. private function customerReportStepThirdForDetail($time, $data, $type){
  2328. $province_code = $data['value'];
  2329. $city_code = $data['value2'];
  2330. //市数据
  2331. $addressData = config('address3');
  2332. if(is_string($addressData)) $addressData = json_decode($addressData, true);
  2333. $first_city = "";
  2334. foreach ($addressData as $value){
  2335. if($value['value'] != $province_code && ! empty($city)) continue;
  2336. foreach ($value['children'] as $val){
  2337. if(! empty($city)) continue;
  2338. $first_city = $val['value'];
  2339. }
  2340. }
  2341. $customer = [];
  2342. foreach (CustomerReportDepart::type_list as $key => $value){
  2343. $customer[] = [
  2344. 'label' => $value,
  2345. 'value' => $key,
  2346. 'list' => [],
  2347. 'count' => 0
  2348. ];
  2349. }
  2350. $start = $time[0];
  2351. $end = $time[1];
  2352. $model_type = Customer::Model_type_one;
  2353. if($type == 3) $model_type = Customer::Model_type_three;
  2354. $list = CustomerReport::where('del_time',0)
  2355. ->where('enter_time','>=', $start)
  2356. ->where('enter_time','<=', $end)
  2357. ->where('model_type',$model_type)
  2358. ->where('province_code', $province_code)
  2359. ->select('city_code','type','customer_id','title','customer_from','car_type','consulting_product_new_title','enter_time','contact_info')
  2360. ->get()->toArray();
  2361. $detail = CustomerReportDepart::where('del_time',0)
  2362. ->whereIn('customer_id',array_unique(array_column($list,'customer_id')))
  2363. ->where('type',CustomerReportDepart::type_one)
  2364. ->select('customer_id','top_depart_id')
  2365. ->get()->toArray();
  2366. $detail_map = [];
  2367. foreach ($detail as $value){
  2368. $detail_map[$value['customer_id']][] = $value['top_depart_id'];
  2369. }
  2370. $depart = Depart::whereIn('id',array_unique(array_column($detail,'top_depart_id')))
  2371. ->pluck('title','id')
  2372. ->toArray();
  2373. $map = [];
  2374. foreach ($list as $value) {
  2375. if(empty($value['city_code']) && $first_city != $city_code) continue;
  2376. $depart_title = "";
  2377. if(isset($detail_map[$value['customer_id']])){
  2378. foreach ($detail_map[$value['customer_id']] as $val){
  2379. $t = $depart[$val];
  2380. if(! $t) continue;
  2381. $depart_title .= $t . ',';
  2382. }
  2383. }
  2384. $depart_title = rtrim($depart_title,',');
  2385. $tmp = [
  2386. 'depart_title' => $depart_title,
  2387. 'title' => $value['title'],
  2388. 'customer_from' => $value['customer_from'],
  2389. 'car_type' => $value['car_type'],
  2390. 'consulting_product_new_title' => $value['consulting_product_new_title'],
  2391. 'enter_time' => $value['enter_time'] ? date("Y-m-d H:i:s", $value['enter_time']) : "",
  2392. 'contact_info' => $value['contact_info'],
  2393. ];
  2394. $map[$value['type']][] = $tmp;
  2395. }
  2396. foreach ($customer as $key => $value){
  2397. if(isset($map[$value['value']])){
  2398. $tmp = $map[$value['value']];
  2399. $customer[$key]['list'] = $tmp;
  2400. $customer[$key]['count'] = count($tmp);
  2401. }
  2402. }
  2403. return $customer;
  2404. }
  2405. private function customerReportStepThirdForFsDetail($time, $data){
  2406. $province_code = $data['value'];
  2407. $top_depart_id = $data['value2'];
  2408. $customer = [];
  2409. foreach (CustomerReportDepart::type_list as $key => $value){
  2410. $customer[] = [
  2411. 'label' => $value,
  2412. 'value' => $key,
  2413. 'list' => [],
  2414. 'count' => 0
  2415. ];
  2416. }
  2417. $start = $time[0];
  2418. $end = $time[1];
  2419. $model_type = Customer::Model_type_one;
  2420. $list = CustomerReport::where('del_time',0)
  2421. ->where('enter_time','>=', $start)
  2422. ->where('enter_time','<=', $end)
  2423. ->where('model_type',$model_type)
  2424. ->where('province','<>','')
  2425. ->select('type','customer_id','title','customer_from','car_type','consulting_product_new_title','enter_time','contact_info')
  2426. ->get()->toArray();
  2427. $detail = CustomerReportDepart::where('del_time',0)
  2428. ->whereIn('customer_id',array_unique(array_column($list,'customer_id')))
  2429. ->where('type',CustomerReportDepart::type_one)
  2430. ->where('top_depart_id', $top_depart_id)
  2431. ->select('customer_id')
  2432. ->get()->toArray();
  2433. $detail_array = array_column($detail,'customer_id');
  2434. $depart = Depart::where('id',$top_depart_id)->value('title');
  2435. $map = [];
  2436. foreach ($list as $value) {
  2437. if(in_array($value['customer_id'], $detail_array)){
  2438. $tmp = [
  2439. 'depart_title' => $depart,
  2440. 'title' => $value['title'],
  2441. 'customer_from' => $value['customer_from'],
  2442. 'car_type' => $value['car_type'],
  2443. 'consulting_product_new_title' => $value['consulting_product_new_title'],
  2444. 'enter_time' => $value['enter_time'] ? date("Y-m-d H:i:s", $value['enter_time']) : "",
  2445. 'contact_info' => $value['contact_info'],
  2446. ];
  2447. $map[$value['type']][] = $tmp;
  2448. }
  2449. }
  2450. foreach ($customer as $key => $value){
  2451. if(isset($map[$value['value']])){
  2452. $tmp = $map[$value['value']];
  2453. $customer[$key]['list'] = $tmp;
  2454. $customer[$key]['count'] = count($tmp);
  2455. }
  2456. }
  2457. return $customer;
  2458. }
  2459. private function customerReportStepThirdForProvinceXsDetail($time, $data){
  2460. $top_depart_id = intval($data['value']);
  2461. $man_id = $data['value2'];
  2462. $customer = [];
  2463. foreach (CustomerReportDepart::type_list as $key => $value){
  2464. $customer[] = [
  2465. 'label' => $value,
  2466. 'value' => $key,
  2467. 'list' => [],
  2468. 'count' => 0
  2469. ];
  2470. }
  2471. //门店下所有部门
  2472. $all_depart_id = DB::select("
  2473. WITH RECURSIVE sub_departments AS (
  2474. SELECT id FROM depart WHERE parent_id = ? AND del_time = 0
  2475. UNION ALL
  2476. SELECT d.id FROM depart d
  2477. INNER JOIN sub_departments s ON d.parent_id = s.id
  2478. WHERE d.del_time = 0
  2479. )
  2480. SELECT id FROM sub_departments
  2481. ", [$top_depart_id]);
  2482. $allDepartIds = collect($all_depart_id)->pluck('id')->toArray();
  2483. $allDepartIds[] = $top_depart_id;
  2484. //所属于这个门店下的人
  2485. $exists = EmployeeDepartPermission::whereIn('depart_id', $allDepartIds)
  2486. ->where('employee_id',$man_id)
  2487. ->exists();
  2488. if(! $exists) return [];
  2489. $start = $time[0];
  2490. $end = $time[1];
  2491. $list = CustomerReport::where('del_time',0)
  2492. ->where('enter_time','>=', $start)
  2493. ->where('enter_time','<=', $end)
  2494. ->select('type','customer_id','title','customer_from','car_type','consulting_product_new_title','enter_time','contact_info')
  2495. ->get()->toArray();
  2496. $detail = CustomerReportDepart::where('del_time',0)
  2497. ->whereIn('customer_id',array_unique(array_column($list,'customer_id')))
  2498. ->select('customer_id','man_id','type','top_depart_id')
  2499. ->get()->toArray();
  2500. $detail_map = [];
  2501. $detail_array = $man_map = [];
  2502. foreach ($detail as $value){
  2503. if($value['type'] == CustomerReportDepart::type_two){
  2504. }elseif ($value['type'] == CustomerReportDepart::type_one && $value['top_depart_id'] == $top_depart_id){
  2505. $detail_map[$value['customer_id']][] = $value['top_depart_id'];
  2506. if(! in_array($value['customer_id'], $detail_array)) $detail_array[] = $value['customer_id'];
  2507. }elseif ($value['type'] == CustomerReportDepart::type_three){
  2508. if($value['man_id'] == $man_id) $man_map[$value['customer_id']] = 1;
  2509. }
  2510. }
  2511. $depart = Depart::whereIn('id',array_unique(array_column($detail,'top_depart_id')))
  2512. ->pluck('title','id')
  2513. ->toArray();
  2514. $map = [];
  2515. foreach ($list as $value) {
  2516. //这个客户是否属于当前这个门店
  2517. if(in_array($value['customer_id'], $detail_array)){
  2518. $depart_title = "";
  2519. if(isset($detail_map[$value['customer_id']])){
  2520. foreach ($detail_map[$value['customer_id']] as $val){
  2521. $t = $depart[$val];
  2522. if(! $t) continue;
  2523. $depart_title .= $t . ',';
  2524. }
  2525. }
  2526. $depart_title = rtrim($depart_title,',');
  2527. if(isset($man_map[$value['customer_id']])) {
  2528. $tmp = [
  2529. 'depart_title' => $depart_title,
  2530. 'title' => $value['title'],
  2531. 'customer_from' => $value['customer_from'],
  2532. 'car_type' => $value['car_type'],
  2533. 'consulting_product_new_title' => $value['consulting_product_new_title'],
  2534. 'enter_time' => $value['enter_time'] ? date("Y-m-d H:i:s", $value['enter_time']) : "",
  2535. 'contact_info' => $value['contact_info'],
  2536. ];
  2537. $map[$value['type']][] = $tmp;
  2538. }
  2539. }
  2540. }
  2541. foreach ($customer as $key => $value){
  2542. if(isset($map[$value['value']])){
  2543. $tmp = $map[$value['value']];
  2544. $customer[$key]['list'] = $tmp;
  2545. $customer[$key]['count'] = count($tmp);
  2546. }
  2547. }
  2548. return $customer;
  2549. }
  2550. //门店零售:统计各个门店所有的订单总金额
  2551. //私域营销:统计T9品牌中心和杭州旗舰店的营销部部门下所有的人员的线下订单成交金额的总计
  2552. //线上销售:来自T9品牌中心所有的线上订单的合同金额总计
  2553. //门店业绩:来自分社采购单转成的订单的合同金额汇总
  2554. //客资业绩:导入的数据
  2555. public function saleReportStepOfFirst($data,$user){
  2556. if(empty($data['enter_time'][0]) || empty($data['enter_time'][1])) return [false, '请选择日期'];
  2557. $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);
  2558. list($status, $msg) = $this->aboutTime($return);
  2559. if(! $status) return [false, $msg];
  2560. $result = $this->saleReportStepOfFirstStart($return,$user);
  2561. return [true, ['list' => $result]];
  2562. }
  2563. private function saleReportStepOfFirstStart($time,$user){
  2564. $result = $type = [];
  2565. foreach (SalesOrderReport::$all_list as $key => $value){
  2566. $result[] = [
  2567. 'label' => $value,
  2568. 'value' => $key,
  2569. 'all' => ""
  2570. ];
  2571. $type[$key] = 0;
  2572. }
  2573. $setting = Setting::where('setting_name','bt_top_depart_id')->first();
  2574. $bt_top_depart_id = $setting['setting_value'] ?? [];
  2575. $bt_top_depart_id = json_decode($bt_top_depart_id,true);
  2576. $id = [];
  2577. $depart_list = Depart::where('del_time',0)->select('id','parent_id','title')->get()->toArray();
  2578. foreach ($depart_list as $value){
  2579. if(in_array($value['parent_id'], $bt_top_depart_id) && $value['title'] == "营销部"){
  2580. $id[] = $value['id'];
  2581. }
  2582. }
  2583. // 查找所有部门id
  2584. $all_depart_id = [];
  2585. foreach ($id as $value){
  2586. $childIds = $this->findChildIds($value, $depart_list);
  2587. $all_depart_id_tmp = array_merge($childIds, [$value]);
  2588. $all_depart_id = array_merge($all_depart_id, $all_depart_id_tmp);
  2589. }
  2590. // 获取部门下的人
  2591. $employee = EmployeeDepartPermission::whereIn('depart_id', $all_depart_id)
  2592. ->select('employee_id')
  2593. ->get()->toArray();
  2594. $employee = array_unique(array_column($employee,'employee_id'));
  2595. $start = $time[0];
  2596. $end = $time[1];
  2597. $list = SalesOrderReport::where('del_time',0)
  2598. ->where('crt_time','>=', $start)
  2599. ->where('crt_time','<=', $end)
  2600. ->get()->toArray();
  2601. $head = $user['head']['id'] ?? 0;
  2602. foreach ($list as $value){
  2603. //门店零售
  2604. $type[SalesOrderReport::type_one] = bcadd($type[SalesOrderReport::type_one], $value['contract_fee'],2);
  2605. //私域营销
  2606. $bool = in_array($value['top_depart_id'], $bt_top_depart_id);
  2607. 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);
  2608. //线上销售
  2609. 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);
  2610. //门店业绩
  2611. 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);
  2612. }
  2613. //客资业绩
  2614. $start_time = strtotime(date("Y-m-01 00:00:00", $start));
  2615. $type_five = CustomerPerFormance::where('del_time',0)
  2616. ->where('crt_time','>=', $start_time)
  2617. ->where('crt_time','<=', $end)
  2618. ->select(DB::raw('sum(current_month_payment) as current_month_payment'))
  2619. ->first()->toArray();
  2620. $type[SalesOrderReport::type_five] = $type_five['current_month_payment'] ?? 0;
  2621. foreach ($result as $key => $value){
  2622. if(isset($type[$value['value']])){
  2623. $result[$key]['all'] = $type[$value['value']];
  2624. }
  2625. }
  2626. return $result;
  2627. }
  2628. public function saleReportStepOfSecond($data, $user){
  2629. if(empty($data['enter_time'][0]) || empty($data['enter_time'][1])) return [false, '请选择日期'];
  2630. $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);
  2631. list($status, $msg) = $this->aboutTime($return);
  2632. if(! $status) return [false, $msg];
  2633. if(empty($data['type'])) return [false, '请选择报表查看类型'];
  2634. $type = $data['type'];
  2635. if($type == 1){
  2636. $result = $this->saleReportStepOfSecondTypeOne($return);
  2637. }elseif ($type == 2){
  2638. $result = $this->saleReportStepOfSecondTypeTwo($return);
  2639. }elseif ($type == 3){
  2640. $result = $this->saleReportStepOfSecondTypeThree($return, $user);
  2641. }elseif ($type == 4){
  2642. $result = $this->saleReportStepOfSecondTypeFour($return, $user);
  2643. }elseif ($type == 5){
  2644. $result = $this->saleReportStepOfSecondTypeFive($return);
  2645. }else{
  2646. return [false ,'报表查看类型错误'];
  2647. }
  2648. return [true, ['list' => $result]];
  2649. }
  2650. private function saleReportStepOfSecondTypeOne($time){
  2651. //大区
  2652. $area = [];
  2653. foreach (Depart::$area as $key => $value){
  2654. $area[] = [
  2655. 'label' => $value,
  2656. 'value' => $key,
  2657. 'receipt' => 0,
  2658. 'all' => 0
  2659. ];
  2660. }
  2661. $label = "全部";
  2662. $total = [
  2663. 'label' => $label,
  2664. 'value' => "",
  2665. 'receipt' => 0,
  2666. 'all' => 0
  2667. ];
  2668. $start = $time[0];
  2669. $end = $time[1];
  2670. $list = SalesOrderReport::where('del_time',0)
  2671. ->where('crt_time','>=', $start)
  2672. ->where('crt_time','<=', $end)
  2673. ->get()->toArray();
  2674. $map = [];
  2675. foreach ($list as $value) {
  2676. if(isset($map[$value['area']])) {
  2677. $tmp = bcadd($value['receipt'], $map[$value['area']]['num_1']);
  2678. $tmp2 = bcadd($value['contract_fee'], $map[$value['area']]['num_2']);
  2679. $map[$value['area']]['num_1'] = $tmp;
  2680. $map[$value['area']]['num_2'] = $tmp2;
  2681. }else{
  2682. $map[$value['area']] = [
  2683. 'num_1' => $value['receipt'],
  2684. 'num_2' => $value['contract_fee'],
  2685. ];
  2686. }
  2687. $tmp_t = bcadd($total['receipt'], $value['receipt'],2);
  2688. $tmp_t1 = bcadd($total['all'], $value['contract_fee'],2);
  2689. $total['receipt'] = $tmp_t;
  2690. $total['all'] = $tmp_t1;
  2691. }
  2692. foreach ($area as $key => $value){
  2693. if(isset($map[$value['value']])){
  2694. $tmp = $map[$value['value']];
  2695. $area[$key]['receipt'] = $tmp['num_1'];
  2696. $area[$key]['all'] = $tmp['num_2'];
  2697. }
  2698. }
  2699. usort($area, function($a, $b) {
  2700. // 先比较 all_num
  2701. if ($b['all'] !== $a['all']) {
  2702. return $b['all'] - $a['all']; // all_num 降序
  2703. }
  2704. // 如果 all_num 相同,再比较 value
  2705. return $a['receipt'] - $b['receipt']; // value 正序
  2706. });
  2707. array_unshift($area, $total);
  2708. return $area;
  2709. }
  2710. private function saleReportStepOfSecondTypeTwo($time){
  2711. $setting = Setting::where('setting_name','bt_top_depart_id')->first();
  2712. $bt_top_depart_id = $setting['setting_value'] ?? [];
  2713. $bt_top_depart_id = json_decode($bt_top_depart_id,true);
  2714. $id = [];
  2715. $depart_list = Depart::where('del_time',0)->select('id','parent_id','title')->get()->toArray();
  2716. foreach ($depart_list as $value){
  2717. if(in_array($value['parent_id'], $bt_top_depart_id) && $value['title'] == "营销部"){
  2718. $id[] = $value['id'];
  2719. }
  2720. }
  2721. // 查找所有部门id
  2722. $all_depart_id = [];
  2723. foreach ($id as $value){
  2724. $childIds = $this->findChildIds($value, $depart_list);
  2725. $all_depart_id_tmp = array_merge($childIds, [$value]);
  2726. $all_depart_id = array_merge($all_depart_id, $all_depart_id_tmp);
  2727. }
  2728. // 获取部门下的人
  2729. $employee = EmployeeDepartPermission::whereIn('depart_id', $all_depart_id)
  2730. ->select('employee_id')
  2731. ->get()->toArray();
  2732. $employee = array_unique(array_column($employee,'employee_id'));
  2733. $empList = Employee::whereIn('id', $employee)
  2734. ->select('id','emp_name as title')
  2735. ->get()->toArray();
  2736. $man = [];
  2737. foreach ($empList as $value){
  2738. $man[] = [
  2739. 'label' => $value['title'],
  2740. 'value' => $value['id'],
  2741. 'receipt' => 0,
  2742. 'all' => 0,
  2743. 'goal' => 0,
  2744. ];
  2745. }
  2746. $start = $time[0];
  2747. $end = $time[1];
  2748. $list = SalesOrderReport::where('del_time',0)
  2749. ->where('crt_time','>=', $start)
  2750. ->where('crt_time','<=', $end)
  2751. ->whereIn('top_depart_id', $bt_top_depart_id)
  2752. ->where('model_type',SalesOrder::Model_type_one)
  2753. ->whereIn('crt_id',$employee)
  2754. ->get()->toArray();
  2755. $map = [];
  2756. foreach ($list as $value) {
  2757. if(isset($map[$value['crt_id']])) {
  2758. $tmp = bcadd($value['receipt'], $map[$value['crt_id']]['num_1']);
  2759. $tmp2 = bcadd($value['contract_fee'], $map[$value['crt_id']]['num_2']);
  2760. $map[$value['crt_id']]['num_1'] = $tmp;
  2761. $map[$value['crt_id']]['num_2'] = $tmp2;
  2762. }else{
  2763. $map[$value['crt_id']] = [
  2764. 'num_1' => $value['receipt'],
  2765. 'num_2' => $value['contract_fee'],
  2766. ];
  2767. }
  2768. }
  2769. $start_time = strtotime(date("Y-m-01 00:00:00", $start));
  2770. $month = MonthlyPerFormance::where('del_time',0)
  2771. ->where('crt_time',">=",$start_time)
  2772. ->where('crt_time','<=',$end)
  2773. ->select('employee_id','sales_index')
  2774. ->get()->toArray();
  2775. $month_map = [];
  2776. foreach ($month as $value){
  2777. $month_map[$value['employee_id']] = $value['sales_index'];
  2778. }
  2779. foreach ($man as $key => $value){
  2780. if(isset($map[$value['value']])){
  2781. $tmp = $map[$value['value']];
  2782. $man[$key]['receipt'] = $tmp['num_1'];
  2783. $man[$key]['all'] = $tmp['num_2'];
  2784. }
  2785. $man[$key]['goal'] = $month_map[$value['value']] ?? 0;
  2786. }
  2787. usort($man, function($a, $b) {
  2788. // 先比较 all_num
  2789. if ($b['all'] !== $a['all']) {
  2790. return $b['all'] - $a['all']; // all_num 降序
  2791. }
  2792. // 如果 all_num 相同,再比较 value
  2793. return $a['receipt'] - $b['receipt']; // value 正序
  2794. });
  2795. return $man;
  2796. }
  2797. private function saleReportStepOfSecondTypeThree($time, $user){
  2798. $head = $user['head']['id'] ?? 0;
  2799. $list = BasicType::where('del_time',0)
  2800. ->where('type',24)
  2801. ->where('top_depart_id', $head)
  2802. ->select('title')
  2803. ->get()->toArray();
  2804. //大区
  2805. $area = [];
  2806. foreach ($list as $value){
  2807. $area[] = [
  2808. 'label' => $value['title'],
  2809. 'value' => $value['title'],
  2810. 'all' => 0
  2811. ];
  2812. }
  2813. $label = "无平台来源";
  2814. $start = $time[0];
  2815. $end = $time[1];
  2816. $list = SalesOrderReport::where('del_time',0)
  2817. ->where('crt_time','>=', $start)
  2818. ->where('crt_time','<=', $end)
  2819. ->where('model_type', SalesOrder::Model_type_four)
  2820. ->where('top_depart_id', $head)
  2821. ->get()->toArray();
  2822. $map = [];
  2823. foreach ($list as $value) {
  2824. if(empty($value['plat_type_title'])) $value['plat_type_title'] = $label;
  2825. if(isset($map[$value['plat_type_title']])) {
  2826. $tmp = bcadd($value['contract_fee'], $map[$value['plat_type_title']]['num_1']);
  2827. $map[$value['plat_type_title']]['num_1'] = $tmp;
  2828. }else{
  2829. $map[$value['plat_type_title']] = [
  2830. 'num_1' => $value['contract_fee'],
  2831. ];
  2832. }
  2833. }
  2834. if(isset($map[$label])){
  2835. $area[] = [
  2836. 'label' => $label,
  2837. 'value' => $label,
  2838. 'all' => 0
  2839. ];
  2840. }
  2841. foreach ($area as $key => $value){
  2842. if(isset($map[$value['value']])){
  2843. $tmp = $map[$value['value']];
  2844. $area[$key]['all'] = $tmp['num_1'];
  2845. }
  2846. }
  2847. usort($area, function($a, $b) {
  2848. // 先比较 all_num
  2849. if ($b['all'] !== $a['all']) {
  2850. return $b['all'] - $a['all']; // all_num 降序
  2851. }
  2852. });
  2853. return $area;
  2854. }
  2855. private function saleReportStepOfSecondTypeFour($time, $user){
  2856. $head = $user['head']['id'] ?? 0;
  2857. //大区
  2858. $area = [];
  2859. foreach (Depart::$area as $key => $value){
  2860. $area[] = [
  2861. 'label' => $value,
  2862. 'value' => $key,
  2863. 'receipt' => 0,
  2864. 'all' => 0
  2865. ];
  2866. }
  2867. $label = "全部";
  2868. $total = [
  2869. 'label' => $label,
  2870. 'value' => "",
  2871. 'receipt' => 0,
  2872. 'all' => 0
  2873. ];
  2874. $start = $time[0];
  2875. $end = $time[1];
  2876. $list = SalesOrderReport::where('del_time',0)
  2877. ->where('crt_time','>=', $start)
  2878. ->where('crt_time','<=', $end)
  2879. ->where('model_type', SalesOrder::Model_type_two)
  2880. ->where('from_top_depart_id', '>', 0)
  2881. ->get()->toArray();
  2882. $map = [];
  2883. foreach ($list as $value) {
  2884. if(isset($map[$value['area']])) {
  2885. $tmp = bcadd($value['receipt'], $map[$value['area']]['num_1']);
  2886. $tmp2 = bcadd($value['contract_fee'], $map[$value['area']]['num_2']);
  2887. $map[$value['area']]['num_1'] = $tmp;
  2888. $map[$value['area']]['num_2'] = $tmp2;
  2889. }else{
  2890. $map[$value['area']] = [
  2891. 'num_1' => $value['receipt'],
  2892. 'num_2' => $value['contract_fee'],
  2893. ];
  2894. }
  2895. $tmp_t = bcadd($total['receipt'], $value['receipt'],2);
  2896. $tmp_t1 = bcadd($total['all'], $value['contract_fee'],2);
  2897. $total['receipt'] = $tmp_t;
  2898. $total['all'] = $tmp_t1;
  2899. }
  2900. foreach ($area as $key => $value){
  2901. if(isset($map[$value['value']])){
  2902. $tmp = $map[$value['value']];
  2903. $area[$key]['receipt'] = $tmp['num_1'];
  2904. $area[$key]['all'] = $tmp['num_2'];
  2905. }
  2906. }
  2907. usort($area, function($a, $b) {
  2908. // 先比较 all_num
  2909. if ($b['all'] !== $a['all']) {
  2910. return $b['all'] - $a['all']; // all_num 降序
  2911. }
  2912. // 如果 all_num 相同,再比较 value
  2913. return $a['receipt'] - $b['receipt']; // value 正序
  2914. });
  2915. array_unshift($area, $total);
  2916. return $area;
  2917. }
  2918. private function saleReportStepOfSecondTypeFive($time){
  2919. $label = "全部";
  2920. $total = [
  2921. 'label' => $label,
  2922. 'value' => "",
  2923. 'all' => 0
  2924. ];
  2925. $start = $time[0];
  2926. $end = $time[1];
  2927. $start_time = strtotime(date("Y-m-01 00:00:00", $start));
  2928. $list = CustomerPerFormance::where('del_time',0)
  2929. ->where('crt_time','>=', $start_time)
  2930. ->where('crt_time','<=', $end)
  2931. ->get()->toArray();
  2932. $map = [];
  2933. foreach ($list as $value) {
  2934. if(isset($map[$value['area']])) {
  2935. $tmp = bcadd($value['current_month_payment'], $map[$value['area']]['all']);
  2936. $map[$value['area']]['all'] = $tmp;
  2937. }else{
  2938. $map[$value['area']] = [
  2939. 'label' => $value['area'],
  2940. 'value' => $value['area'],
  2941. 'all' => $value['current_month_payment']
  2942. ];
  2943. }
  2944. $tmp_t1 = bcadd($total['all'], $value['current_month_payment'],2);
  2945. $total['all'] = $tmp_t1;
  2946. }
  2947. $map = array_values($map);
  2948. usort($map, function($a, $b) {
  2949. // 先比较 all_num
  2950. if ($b['all'] !== $a['all']) {
  2951. return $b['all'] - $a['all']; // all_num 降序
  2952. }
  2953. });
  2954. array_unshift($map, $total);
  2955. return $map;
  2956. }
  2957. public function saleReportStepOfThird($data, $user){
  2958. if(empty($data['enter_time'][0]) || empty($data['enter_time'][1])) return [false, '请选择日期'];
  2959. $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);
  2960. list($status, $msg) = $this->aboutTime($return);
  2961. if(! $status) return [false, $msg];
  2962. if(empty($data['type'])) return [false, '请选择报表查看类型'];
  2963. if(empty($data['value'])) return [false, 'value不能为空'];
  2964. $type = $data['type'];
  2965. if($type == 1){
  2966. $result = $this->saleReportStepOfThirdTypeOne($return, $data);
  2967. }elseif ($type == 2){
  2968. $result = $this->saleReportStepOfThirdTypeTwo($return, $data);
  2969. }elseif ($type == 3){
  2970. $result = $this->saleReportStepOfThirdTypeThree($return, $data, $user);
  2971. }elseif ($type == 4){
  2972. $result = $this->saleReportStepOfThirdTypeFour($return, $data, $user);
  2973. }elseif ($type == 5){
  2974. $result = $this->saleReportStepOfThreeTypeFive($return,$data);
  2975. }else{
  2976. return [false ,'报表查看类型错误'];
  2977. }
  2978. return [true, ['list' => $result]];
  2979. }
  2980. private function saleReportStepOfThirdTypeOne($time,$data){
  2981. $area_value = $data['value'];
  2982. $depart = Depart::where('del_time',0)
  2983. ->where('parent_id',0)
  2984. ->where('area',$area_value)
  2985. ->select('id','title')
  2986. ->get()->toArray();
  2987. //大区
  2988. $area = [];
  2989. foreach ($depart as $value){
  2990. $area[] = [
  2991. 'label' => $value['title'],
  2992. 'value' => $value['id'],
  2993. 'receipt' => 0,
  2994. 'all' => 0
  2995. ];
  2996. }
  2997. $label = "全部";
  2998. $total = [
  2999. 'label' => $label,
  3000. 'value' => "",
  3001. 'receipt' => 0,
  3002. 'all' => 0
  3003. ];
  3004. $start = $time[0];
  3005. $end = $time[1];
  3006. $list = SalesOrderReport::where('del_time',0)
  3007. ->where('crt_time','>=', $start)
  3008. ->where('crt_time','<=', $end)
  3009. ->where('area',$area_value)
  3010. ->get()->toArray();
  3011. $map = [];
  3012. foreach ($list as $value) {
  3013. if(isset($map[$value['top_depart_id']])) {
  3014. $tmp = bcadd($value['receipt'], $map[$value['top_depart_id']]['num_1']);
  3015. $tmp2 = bcadd($value['contract_fee'], $map[$value['top_depart_id']]['num_2']);
  3016. $map[$value['top_depart_id']]['num_1'] = $tmp;
  3017. $map[$value['top_depart_id']]['num_2'] = $tmp2;
  3018. }else{
  3019. $map[$value['top_depart_id']] = [
  3020. 'num_1' => $value['receipt'],
  3021. 'num_2' => $value['contract_fee'],
  3022. ];
  3023. }
  3024. $tmp_t = bcadd($total['receipt'], $value['receipt'],2);
  3025. $tmp_t1 = bcadd($total['all'], $value['contract_fee'],2);
  3026. $total['receipt'] = $tmp_t;
  3027. $total['all'] = $tmp_t1;
  3028. }
  3029. foreach ($area as $key => $value){
  3030. if(isset($map[$value['value']])){
  3031. $tmp = $map[$value['value']];
  3032. $area[$key]['receipt'] = $tmp['num_1'];
  3033. $area[$key]['all'] = $tmp['num_2'];
  3034. }
  3035. }
  3036. usort($area, function($a, $b) {
  3037. // 先比较 all_num
  3038. if ($b['all'] !== $a['all']) {
  3039. return $b['all'] - $a['all']; // all_num 降序
  3040. }
  3041. // 如果 all_num 相同,再比较 value
  3042. return $a['receipt'] - $b['receipt']; // value 正序
  3043. });
  3044. array_unshift($area, $total);
  3045. return $area;
  3046. }
  3047. private function saleReportStepOfThirdTypeTwo($time,$data){
  3048. $man_value = $data['value'];
  3049. $area = [];
  3050. foreach (SalesOrderReport::$all_state_list as $key => $value){
  3051. $area[] = [
  3052. 'label' => $value,
  3053. 'value' => $key,
  3054. 'list' => [],
  3055. 'count' => 0
  3056. ];
  3057. }
  3058. $setting = Setting::where('setting_name','bt_top_depart_id')->first();
  3059. $bt_top_depart_id = $setting['setting_value'] ?? [];
  3060. $bt_top_depart_id = json_decode($bt_top_depart_id,true);
  3061. $start = $time[0];
  3062. $end = $time[1];
  3063. $list = SalesOrderReport::where('del_time',0)
  3064. ->where('crt_time','>=', $start)
  3065. ->where('crt_time','<=', $end)
  3066. ->whereIn('top_depart_id', $bt_top_depart_id)
  3067. ->where('model_type',SalesOrder::Model_type_one)
  3068. ->where('crt_id', $man_value)
  3069. ->orderBy('crt_time','desc')
  3070. ->get()->toArray();
  3071. $map = [];
  3072. foreach ($list as $value) {
  3073. $crt_time = $value['crt_time'] ? date("Y-m-d H:i:s", $value['crt_time']) : "";
  3074. $model_type_title = SalesOrder::$model_type_title[$value['model_type']] ?? "";
  3075. $tmp = [
  3076. 'crt_time' => $crt_time,
  3077. 'model_type_title' => $model_type_title,
  3078. 'customer' => $value['customer_title'],
  3079. 'contact_product' => $value['contact_product'],
  3080. 'fz' => $value['fz_man'],
  3081. 'contract_fee' => $value['contract_fee'],
  3082. ];
  3083. $map[SalesOrderReport::state_zero][] = $tmp;
  3084. if($value['state'] == 1){
  3085. $map[SalesOrderReport::state_one][] = $tmp;
  3086. }elseif ($value['state'] == 2){
  3087. $map[SalesOrderReport::state_two][] = $tmp;
  3088. }elseif ($value['state'] == 3){
  3089. $map[SalesOrderReport::state_three][] = $tmp;
  3090. }elseif ($value['state'] == 4){
  3091. $map[SalesOrderReport::state_four][] = $tmp;
  3092. }
  3093. }
  3094. foreach ($area as $key => $value){
  3095. if(isset($map[$value['value']])){
  3096. $tmp = $map[$value['value']];
  3097. $area[$key]['list'] = $tmp;
  3098. $area[$key]['count'] = count($tmp);
  3099. }
  3100. }
  3101. return $area;
  3102. }
  3103. private function saleReportStepOfThirdTypeThree($time,$data, $user){
  3104. $head = $user['head']['id'] ?? 0;
  3105. $basic_value = $data['value'];
  3106. $area = [];
  3107. foreach (SalesOrderReport::$all_state_list as $key => $value){
  3108. $area[] = [
  3109. 'label' => $value,
  3110. 'value' => $key,
  3111. 'list' => []
  3112. ];
  3113. }
  3114. $start = $time[0];
  3115. $end = $time[1];
  3116. $list = SalesOrderReport::where('del_time',0)
  3117. ->where('crt_time','>=', $start)
  3118. ->where('crt_time','<=', $end)
  3119. ->where('model_type', SalesOrder::Model_type_four)
  3120. ->where('top_depart_id', $head)
  3121. ->where('plat_type_title', $basic_value)
  3122. ->orderBy('crt_time','desc')
  3123. ->get()->toArray();
  3124. $map = [];
  3125. foreach ($list as $value) {
  3126. $crt_time = $value['crt_time'] ? date("Y-m-d H:i:s", $value['crt_time']) : "";
  3127. $model_type_title = SalesOrder::$model_type_title[$value['model_type']] ?? "";
  3128. $tmp = [
  3129. 'crt_time' => $crt_time,
  3130. 'model_type_title' => $model_type_title,
  3131. 'customer' => $value['customer_title'],
  3132. 'contact_product' => $value['contact_product'],
  3133. 'fz' => $value['fz_man'],
  3134. 'contract_fee' => $value['contract_fee'],
  3135. ];
  3136. $map[SalesOrderReport::state_zero][] = $tmp;
  3137. if($value['state'] == 1){
  3138. $map[SalesOrderReport::state_one][] = $tmp;
  3139. }elseif ($value['state'] == 2){
  3140. $map[SalesOrderReport::state_two][] = $tmp;
  3141. }elseif ($value['state'] == 3){
  3142. $map[SalesOrderReport::state_three][] = $tmp;
  3143. }elseif ($value['state'] == 4){
  3144. $map[SalesOrderReport::state_four][] = $tmp;
  3145. }
  3146. }
  3147. foreach ($area as $key => $value){
  3148. if(isset($map[$value['value']])){
  3149. $tmp = $map[$value['value']];
  3150. $area[$key]['list'] = $tmp;
  3151. $area[$key]['count'] = count($tmp);
  3152. }
  3153. }
  3154. return $area;
  3155. }
  3156. private function saleReportStepOfThirdTypeFour($time,$data, $user){
  3157. $head = $user['head']['id'] ?? 0;
  3158. $area_value = $data['value'];
  3159. $depart = Depart::where('del_time',0)
  3160. ->where('parent_id',0)
  3161. ->where('area', $area_value)
  3162. ->select('id','title')
  3163. ->get()->toArray();
  3164. //大区
  3165. $area = [];
  3166. foreach ($depart as $value){
  3167. $area[] = [
  3168. 'label' => $value['title'],
  3169. 'value' => $value['id'],
  3170. 'receipt' => 0,
  3171. 'all' => 0
  3172. ];
  3173. }
  3174. $label = "全部";
  3175. $total = [
  3176. 'label' => $label,
  3177. 'value' => "",
  3178. 'receipt' => 0,
  3179. 'all' => 0
  3180. ];
  3181. $start = $time[0];
  3182. $end = $time[1];
  3183. $list = SalesOrderReport::where('del_time',0)
  3184. ->where('crt_time','>=', $start)
  3185. ->where('crt_time','<=', $end)
  3186. ->where('area',$area_value)
  3187. ->where('model_type', SalesOrder::Model_type_two)
  3188. ->where('from_top_depart_id', '>',0)
  3189. ->get()->toArray();
  3190. $map = [];
  3191. foreach ($list as $value) {
  3192. if(isset($map[$value['top_depart_id']])) {
  3193. $tmp = bcadd($value['receipt'], $map[$value['top_depart_id']]['num_1']);
  3194. $tmp2 = bcadd($value['contract_fee'], $map[$value['top_depart_id']]['num_2']);
  3195. $map[$value['top_depart_id']]['num_1'] = $tmp;
  3196. $map[$value['top_depart_id']]['num_2'] = $tmp2;
  3197. }else{
  3198. $map[$value['top_depart_id']] = [
  3199. 'num_1' => $value['receipt'],
  3200. 'num_2' => $value['contract_fee'],
  3201. ];
  3202. }
  3203. $tmp_t = bcadd($total['receipt'], $value['receipt'],2);
  3204. $tmp_t1 = bcadd($total['all'], $value['contract_fee'],2);
  3205. $total['receipt'] = $tmp_t;
  3206. $total['all'] = $tmp_t1;
  3207. }
  3208. foreach ($area as $key => $value){
  3209. if(isset($map[$value['value']])){
  3210. $tmp = $map[$value['value']];
  3211. $area[$key]['receipt'] = $tmp['num_1'];
  3212. $area[$key]['all'] = $tmp['num_2'];
  3213. }
  3214. }
  3215. usort($area, function($a, $b) {
  3216. // 先比较 all_num
  3217. if ($b['all'] !== $a['all']) {
  3218. return $b['all'] - $a['all']; // all_num 降序
  3219. }
  3220. // 如果 all_num 相同,再比较 value
  3221. return $a['receipt'] - $b['receipt']; // value 正序
  3222. });
  3223. array_unshift($area, $total);
  3224. return $area;
  3225. }
  3226. private function saleReportStepOfThreeTypeFive($time,$data){
  3227. $area_value = $data['value'];
  3228. $label = "全部";
  3229. $total = [
  3230. 'label' => $label,
  3231. 'value' => "",
  3232. 'all' => 0
  3233. ];
  3234. $start = $time[0];
  3235. $end = $time[1];
  3236. $start_time = strtotime(date("Y-m-01 00:00:00", $start));
  3237. $list = CustomerPerFormance::where('del_time',0)
  3238. ->where('crt_time','>=', $start_time)
  3239. ->where('crt_time','<=', $end)
  3240. ->where('area', $area_value)
  3241. ->get()->toArray();
  3242. $map = [];
  3243. foreach ($list as $value) {
  3244. if(isset($map[$value['title']])) {
  3245. $tmp = bcadd($value['current_month_payment'], $map[$value['title']]['all']);
  3246. $map[$value['title']]['all'] = $tmp;
  3247. }else{
  3248. $map[$value['title']] = [
  3249. 'label' => $value['title'],
  3250. 'value' => $value['title'],
  3251. 'all' => $value['current_month_payment']
  3252. ];
  3253. }
  3254. $tmp_t1 = bcadd($total['all'], $value['current_month_payment'],2);
  3255. $total['all'] = $tmp_t1;
  3256. }
  3257. $map = array_values($map);
  3258. usort($map, function($a, $b) {
  3259. // 先比较 all_num
  3260. if ($b['all'] !== $a['all']) {
  3261. return $b['all'] - $a['all']; // all 降序
  3262. }
  3263. });
  3264. array_unshift($map, $total);
  3265. return $map;
  3266. }
  3267. public function saleReportStepOfFour($data, $user){
  3268. if(empty($data['enter_time'][0]) || empty($data['enter_time'][1])) return [false, '请选择日期'];
  3269. $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);
  3270. list($status, $msg) = $this->aboutTime($return);
  3271. if(! $status) return [false, $msg];
  3272. if(empty($data['type'])) return [false, '请选择报表查看类型'];
  3273. if(empty($data['value'])) return [false, 'value不能为空'];
  3274. if(empty($data['value2'])) return [false, 'value2不能为空'];
  3275. $type = $data['type'];
  3276. if($type == 1){
  3277. $result = $this->saleReportStepOfFourTypeOne($return, $data);
  3278. }elseif ($type == 4){
  3279. $result = $this->saleReportStepOfFourTypeFour($return, $data, $user);
  3280. }else{
  3281. return [false ,'报表查看类型错误'];
  3282. }
  3283. return [true, ['list' => $result]];
  3284. }
  3285. private function saleReportStepOfFourTypeOne($time,$data){
  3286. $area_value = $data['value'];
  3287. $depart_value = $data['value2'];
  3288. $depart_list = Depart::where('del_time',0)->select('id','parent_id','title')->get()->toArray();
  3289. // 查找所有部门id
  3290. $childIds = $this->findChildIds($depart_value, $depart_list);
  3291. $all_depart_id = array_merge($childIds, [$depart_value]);
  3292. // 获取部门下的人
  3293. $employee = EmployeeDepartPermission::whereIn('depart_id', $all_depart_id)
  3294. ->select('employee_id')
  3295. ->get()->toArray();
  3296. $employee = array_unique(array_column($employee,'employee_id'));
  3297. $empMap = Employee::whereIn('id', $employee)
  3298. ->pluck('emp_name as title','id')
  3299. ->toArray();
  3300. $area = [];
  3301. foreach ($empMap as $key => $value){
  3302. $area[$key] = [
  3303. 'label' => $value,
  3304. 'value' => $key,
  3305. 'receipt' => 0,
  3306. 'all' => 0,
  3307. 'goal' => 0,
  3308. ];
  3309. }
  3310. $label = "全部";
  3311. $total = [
  3312. 'label' => $label,
  3313. 'value' => "",
  3314. 'receipt' => 0,
  3315. 'all' => 0,
  3316. 'goal' => 0,
  3317. ];
  3318. $start = $time[0];
  3319. $end = $time[1];
  3320. $list = SalesOrderReport::where('del_time',0)
  3321. ->where('crt_time','>=', $start)
  3322. ->where('crt_time','<=', $end)
  3323. ->where('area',$area_value)
  3324. ->where('top_depart_id',$depart_value)
  3325. ->get()->toArray();
  3326. $map = [];
  3327. foreach ($list as $value) {
  3328. if(isset($map[$value['crt_id']])) {
  3329. $tmp = bcadd($value['receipt'], $map[$value['crt_id']]['num_1']);
  3330. $tmp2 = bcadd($value['contract_fee'], $map[$value['crt_id']]['num_2']);
  3331. $map[$value['crt_id']]['num_1'] = $tmp;
  3332. $map[$value['crt_id']]['num_2'] = $tmp2;
  3333. }else{
  3334. $map[$value['crt_id']] = [
  3335. 'num_1' => $value['receipt'],
  3336. 'num_2' => $value['contract_fee'],
  3337. ];
  3338. }
  3339. $tmp_t = bcadd($total['receipt'], $value['receipt'],2);
  3340. $tmp_t1 = bcadd($total['all'], $value['contract_fee'],2);
  3341. $total['receipt'] = $tmp_t;
  3342. $total['all'] = $tmp_t1;
  3343. }
  3344. $start_time = strtotime(date("Y-m-01 00:00:00", $start));
  3345. $month = MonthlyPerFormance::where('del_time',0)
  3346. ->where('crt_time',">=",$start_time)
  3347. ->where('crt_time','<=',$end)
  3348. ->select('employee_id','sales_index')
  3349. ->get()->toArray();
  3350. $month_map = [];
  3351. foreach ($month as $value){
  3352. $month_map[$value['employee_id']] = $value['sales_index'];
  3353. }
  3354. foreach ($area as $key => $value){
  3355. if(isset($map[$value['value']])){
  3356. $tmp = $map[$value['value']];
  3357. $area[$key]['receipt'] = $tmp['num_1'];
  3358. $area[$key]['all'] = $tmp['num_2'];
  3359. $goal = $month_map[$value['value']] ?? 0;
  3360. $area[$key]['goal'] = $goal;
  3361. $total['goal'] = bcadd($goal, $total['goal'],2);
  3362. }else{
  3363. unset($area[$key]);
  3364. }
  3365. }
  3366. $area = array_values($area);
  3367. usort($area, function($a, $b) {
  3368. // 先比较 all_num
  3369. if ($b['all'] !== $a['all']) {
  3370. return $b['all'] - $a['all']; // all_num 降序
  3371. }
  3372. // 如果 all_num 相同,再比较 value
  3373. return $a['receipt'] - $b['receipt']; // value 正序
  3374. });
  3375. array_unshift($area, $total);
  3376. return $area;
  3377. }
  3378. private function saleReportStepOfFourTypeFour($time,$data, $user){
  3379. $head = $user['head']['id'] ?? 0;
  3380. $area_value = $data['value'];
  3381. $depart_value = $data['value2'];
  3382. $area = [];
  3383. foreach (SalesOrderReport::$all_state_list as $key => $value){
  3384. $area[] = [
  3385. 'label' => $value,
  3386. 'value' => $key,
  3387. 'list' => []
  3388. ];
  3389. }
  3390. $start = $time[0];
  3391. $end = $time[1];
  3392. $list = SalesOrderReport::where('del_time',0)
  3393. ->where('crt_time','>=', $start)
  3394. ->where('crt_time','<=', $end)
  3395. ->where('area',$area_value)
  3396. ->where('model_type', SalesOrder::Model_type_two)
  3397. ->where('from_top_depart_id', $depart_value)
  3398. ->get()->toArray();
  3399. $map = [];
  3400. foreach ($list as $value) {
  3401. $crt_time = $value['crt_time'] ? date("Y-m-d H:i:s", $value['crt_time']) : "";
  3402. $model_type_title = SalesOrder::$model_type_title[$value['model_type']] ?? "";
  3403. $tmp = [
  3404. 'crt_time' => $crt_time,
  3405. 'model_type_title' => $model_type_title,
  3406. 'customer' => $value['customer_title'],
  3407. 'contact_product' => $value['contact_product'],
  3408. 'fz' => $value['fz_man'],
  3409. 'contract_fee' => $value['contract_fee'],
  3410. ];
  3411. $map[SalesOrderReport::state_zero][] = $tmp;
  3412. if($value['state'] == 1){
  3413. $map[SalesOrderReport::state_one][] = $tmp;
  3414. }elseif ($value['state'] == 2){
  3415. $map[SalesOrderReport::state_two][] = $tmp;
  3416. }elseif ($value['state'] == 3){
  3417. $map[SalesOrderReport::state_three][] = $tmp;
  3418. }elseif ($value['state'] == 4){
  3419. $map[SalesOrderReport::state_four][] = $tmp;
  3420. }
  3421. }
  3422. foreach ($area as $key => $value){
  3423. if(isset($map[$value['value']])){
  3424. $tmp = $map[$value['value']];
  3425. $area[$key]['list'] = $tmp;
  3426. $area[$key]['count'] = count($tmp);
  3427. }
  3428. }
  3429. return $area;
  3430. }
  3431. public function saleReportStepOfFive($data, $user){
  3432. if(empty($data['enter_time'][0]) || empty($data['enter_time'][1])) return [false, '请选择日期'];
  3433. $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);
  3434. list($status, $msg) = $this->aboutTime($return);
  3435. if(! $status) return [false, $msg];
  3436. if(empty($data['type'])) return [false, '请选择报表查看类型'];
  3437. if(empty($data['value'])) return [false, 'value不能为空'];
  3438. if(empty($data['value2'])) return [false, 'value2不能为空'];
  3439. if(empty($data['value3'])) return [false, 'value3不能为空'];
  3440. $type = $data['type'];
  3441. if($type == 1){
  3442. $result = $this->saleReportStepOfFiveTypeOne($return, $data);
  3443. }else{
  3444. return [false ,'报表查看类型错误'];
  3445. }
  3446. return [true, ['list' => $result]];
  3447. }
  3448. private function saleReportStepOfFiveTypeOne($time,$data){
  3449. $area_value = $data['value'];
  3450. $depart_value = $data['value2'];
  3451. $man_value = $data['value3'];
  3452. $area = [];
  3453. foreach (SalesOrderReport::$all_state_list as $key => $value){
  3454. $area[] = [
  3455. 'label' => $value,
  3456. 'value' => $key,
  3457. 'list' => [],
  3458. 'count' => 0
  3459. ];
  3460. }
  3461. $start = $time[0];
  3462. $end = $time[1];
  3463. $list = SalesOrderReport::where('del_time',0)
  3464. ->where('crt_time','>=', $start)
  3465. ->where('crt_time','<=', $end)
  3466. ->where('area',$area_value)
  3467. ->where('top_depart_id',$depart_value)
  3468. ->where('crt_id', $man_value)
  3469. ->get()->toArray();
  3470. $map = [];
  3471. foreach ($list as $value) {
  3472. $crt_time = $value['crt_time'] ? date("Y-m-d H:i:s", $value['crt_time']) : "";
  3473. $model_type_title = SalesOrder::$model_type_title[$value['model_type']] ?? "";
  3474. $tmp = [
  3475. 'crt_time' => $crt_time,
  3476. 'model_type_title' => $model_type_title,
  3477. 'customer' => $value['customer_title'],
  3478. 'contact_product' => $value['contact_product'],
  3479. 'fz' => $value['fz_man'],
  3480. 'contract_fee' => $value['contract_fee'],
  3481. ];
  3482. $map[SalesOrderReport::state_zero][] = $tmp;
  3483. if($value['state'] == 1){
  3484. $map[SalesOrderReport::state_one][] = $tmp;
  3485. }elseif ($value['state'] == 2){
  3486. $map[SalesOrderReport::state_two][] = $tmp;
  3487. }elseif ($value['state'] == 3){
  3488. $map[SalesOrderReport::state_three][] = $tmp;
  3489. }elseif ($value['state'] == 4){
  3490. $map[SalesOrderReport::state_four][] = $tmp;
  3491. }
  3492. }
  3493. foreach ($area as $key => $value){
  3494. if(isset($map[$value['value']])){
  3495. $tmp = $map[$value['value']];
  3496. $area[$key]['list'] = $tmp;
  3497. $area[$key]['count'] = count($tmp);
  3498. }
  3499. }
  3500. return $area;
  3501. }
  3502. }