ReportController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Helpers\Price;
  4. use App\Models\Dictionary\District;
  5. use App\Models\ObjectType;
  6. use App\Models\Order;
  7. use App\Models\OrderStatus;
  8. use App\Models\Product;
  9. use App\Models\ProductSKU;
  10. use App\Models\Reclamation;
  11. use App\Models\ReclamationStatus;
  12. use App\Models\User;
  13. use Illuminate\Database\Eloquent\Collection;
  14. use Illuminate\Http\Request;
  15. use Illuminate\Support\Str;
  16. class ReportController extends Controller
  17. {
  18. protected array $data = [
  19. 'active' => 'reports',
  20. 'title' => 'Отчёты',
  21. 'id' => 'reports',
  22. ];
  23. public function index()
  24. {
  25. $installedStatuses = [
  26. Order::STATUS_READY_TO_HAND_OVER,
  27. Order::STATUS_NOT_HANDED_OVER_WITH_NOTES,
  28. Order::STATUS_HANDED_OVER_WITH_NOTES,
  29. Order::STATUS_HANDED_OVER,
  30. ];
  31. $doneStatuses = [9, 10];
  32. $handedOverStatus = Order::STATUS_HANDED_OVER;
  33. $objectTypes = ObjectType::query()->get()->pluck('name', 'id')->toArray();
  34. $this->data['objectTypes'] = $objectTypes;
  35. $user_ids = Order::query()->distinct()->get('user_id')->pluck('user_id')->toArray();
  36. $managers = User::query()->whereIn('id', $user_ids)->get()->pluck('name', 'id')->toArray();
  37. $this->data['managers'] = $managers ?? [];
  38. $this->data['doneMafsManager'] = $this->data['notDoneMafsManager'] = [];
  39. // всего заказов, выполнено заказов
  40. $this->data['totalOrders'] = Order::all()->count();
  41. $this->data['doneOrders'] = Order::query()->whereIn('order_status_id', $doneStatuses)->count();
  42. $this->data['mountOrders'] = Order::query()->whereIn('order_status_id', $installedStatuses)->count();
  43. $this->data['installedOrders'] = Order::query()->whereIn('order_status_id', $installedStatuses)->count();
  44. // всего маф / завершено маф / установлено маф
  45. $this->data['totalMafs'] = ProductSKU::all()->count();
  46. $this->data['doneMafs'] = ProductSKU::query()->
  47. whereHas('order', function ($query) use ($doneStatuses) {
  48. $query->whereIn('order_status_id', $doneStatuses);
  49. })->count();
  50. $this->data['mountMafs'] = ProductSKU::query()->
  51. whereHas('order', function ($query) use ($installedStatuses){
  52. $query->whereIn('order_status_id', $installedStatuses);
  53. })->count();
  54. $this->data['installedMafs'] = ProductSKU::query()
  55. ->whereHas('order', function ($query) use ($installedStatuses) {
  56. $query->whereIn('order_status_id', $installedStatuses);
  57. })
  58. ->count();
  59. // сумма сданных МАФ по площадкам со статусом "Сдана"
  60. $this->data['totalHandedOverSum'] = Price::format(
  61. $this->getDoneSumByStatus($handedOverStatus)
  62. );
  63. // done by managers
  64. foreach ($managers as $userId => $userName) {
  65. $this->data['totalOrderManager'] = Order::where('user_id', $userId)->count();
  66. $this->data['totalMafsManager'][$userId] = ProductSKU::query()->
  67. whereHas('order', function ($query) use ($userId) {
  68. $query->where('user_id', '=', $userId);
  69. })->count();
  70. $this->data['doneOrdersManager'][$userId] = Order::query()
  71. ->where('user_id', '=', $userId)
  72. ->whereIn('order_status_id', $doneStatuses)
  73. ->count();
  74. $this->data['doneMafsManager'][$userId] = ProductSKU::query()->
  75. whereHas('order', function ($query) use ($userId, $doneStatuses) {
  76. $query->where('user_id', '=', $userId)
  77. ->whereIn('order_status_id', $doneStatuses);
  78. })->count();
  79. $this->data['notDoneOrdersManager'][$userId] = Order::query()
  80. ->where('user_id', '=', $userId)
  81. ->whereNotIn('order_status_id', $doneStatuses)
  82. ->count();
  83. $this->data['notDoneMafsManager'][$userId] = ProductSKU::query()->
  84. whereHas('order', function ($query) use ($userId, $doneStatuses) {
  85. $query->where('user_id', '=', $userId)
  86. ->whereNotIn('order_status_id', $doneStatuses);
  87. })->count();
  88. }
  89. // total mount by types
  90. foreach ($objectTypes as $objectTypeId => $objectType) {
  91. // total by types
  92. $this->data['totalOrdersType'][$objectTypeId] = Order::where('object_type_id', '=', $objectTypeId)->count();
  93. $this->data['totalMafsType'][$objectTypeId] = ProductSKU::query()->
  94. whereHas('order', function ($query) use ($objectTypeId) {
  95. $query->where('object_type_id', '=', $objectTypeId);
  96. })->count();
  97. // Установленные: статусы после выхода из "В монтаже".
  98. $this->data['mountOrdersType'][$objectTypeId] = Order::query()
  99. ->where('object_type_id', '=', $objectTypeId)
  100. ->whereIn('order_status_id', $installedStatuses)
  101. ->count();
  102. $this->data['mountMafsType'][$objectTypeId] = ProductSKU::query()->
  103. whereHas('order', function ($query) use ($objectTypeId, $installedStatuses) {
  104. $query->where('object_type_id', '=', $objectTypeId)
  105. ->whereIn('order_status_id', $installedStatuses);
  106. })->count();
  107. // остальные - не готовы
  108. $this->data['notMountOrdersType'][$objectTypeId] = Order::query()
  109. ->where('object_type_id', '=', $objectTypeId)
  110. ->whereNotIn('order_status_id', $installedStatuses)
  111. ->count();
  112. $this->data['notMountMafsType'][$objectTypeId] = ProductSKU::query()->
  113. whereHas('order', function ($query) use ($objectTypeId, $installedStatuses) {
  114. $query->where('object_type_id', '=', $objectTypeId)
  115. ->whereNotIn('order_status_id', $installedStatuses);
  116. })->count();
  117. }
  118. // рекламации
  119. $this->data['totalReclamations'] = Reclamation::query()->count();
  120. $this->data['reclamationStatuses'] = ReclamationStatus::query()->get()->pluck('name', 'id')->toArray();
  121. foreach ($this->data['reclamationStatuses'] as $reclamationStatusId => $reclamationStatus) {
  122. $this->data['reclamations'][$reclamationStatus] = Reclamation::query()->where('status_id', '=', $reclamationStatusId)->count();
  123. }
  124. $this->data['reclamationMafs'] = $this->data['reclamationDetails'] = [];
  125. $reclamations = Reclamation::query()
  126. ->with([
  127. 'skus.product',
  128. 'details',
  129. 'spareParts',
  130. ])
  131. ->get();
  132. foreach ($reclamations as $reclamation) {
  133. foreach ($reclamation->skus as $sku) {
  134. $a = $sku->product->article;
  135. if(isset($this->data['reclamationMafs'][$a])){
  136. $this->data['reclamationMafs'][$a]++;
  137. } else {
  138. $this->data['reclamationMafs'][$a] = 1;
  139. }
  140. }
  141. foreach ($reclamation->details as $detail) {
  142. $a = $detail->name;
  143. if(isset($this->data['reclamationDetails'][$a])){
  144. $this->data['reclamationDetails'][$a] += $detail->quantity;
  145. } else {
  146. $this->data['reclamationDetails'][$a] = $detail->quantity;
  147. }
  148. }
  149. }
  150. $this->appendReclamationSparePartsToReport($reclamations);
  151. // svod
  152. $orderStatuses = OrderStatus::query()->get()->pluck('name', 'id')->toArray();
  153. foreach ($orderStatuses as $orderStatusId => $orderStatus) {
  154. $this->data['orderStatuses'][$orderStatus] = Order::query()->where('order_status_id', '=', $orderStatusId)->count();
  155. $this->data['orderMafStatuses'][$orderStatus] = ProductSKU::query()->
  156. whereHas('order', function ($query) use ($orderStatusId) {
  157. $query->where('order_status_id', $orderStatusId);
  158. })->count();
  159. }
  160. // общая сумма
  161. $this->data['totalSum'] = Price::format(
  162. ProductSKU::query()
  163. ->withSum('product', 'total_price')
  164. ->get()
  165. ->sum('product_sum_total_price') / 100
  166. );
  167. // общая сумма done
  168. $this->data['totalDoneSum'] = Price::format(
  169. $this->getDoneSumByStatus($handedOverStatus)
  170. );
  171. $districts = District::query()->get(['id', 'shortname']);
  172. foreach ($districts as $districtModel) {
  173. $districtId = $districtModel->id;
  174. $district = $districtModel->shortname;
  175. $totalOrders = $this->getOrderCount($districtId);
  176. $totalMafs = $this->getMafCount($districtId);
  177. $readyOrders = $this->getOrderCount($districtId, 4);
  178. $readyMafs = $this->getMafCount($districtId, 4);
  179. $mountOrders = $this->getOrderCount($districtId, 5);
  180. $mountMafs = $this->getMafCount($districtId, 5);
  181. $readyDoneOrders = $this->getOrderCount($districtId, 7);
  182. $readyDoneMafs = $this->getMafCount($districtId, 7);
  183. $doneOrders = $this->getOrderCount($districtId, $doneStatuses);
  184. $doneMafs = $this->getMafCount($districtId, $doneStatuses);
  185. $statusOstOrders = $totalOrders - $mountOrders - $readyDoneOrders - $doneOrders;
  186. $statusOstMafs = $totalMafs - $mountMafs - $readyDoneMafs - $doneMafs;
  187. $totalYardOrders = $this->getOrderCount($districtId, null, 1);
  188. $totalYardMafs = $this->getMafCount($districtId, null, 1);
  189. $mountYardOrders = $this->getOrderCount($districtId, $installedStatuses, 1);
  190. $mountYardMafs = $this->getMafCount($districtId, $installedStatuses, 1);
  191. $ostYardOrders = $totalYardOrders - $mountYardOrders;
  192. $ostYardMafs = $totalYardMafs - $mountYardMafs;
  193. $totalEduOrders = $this->getOrderCount($districtId, null, 2);
  194. $totalEduMafs = $this->getMafCount($districtId, null, 2);
  195. $mountEduOrders = $this->getOrderCount($districtId, $installedStatuses, 2);
  196. $mountEduMafs = $this->getMafCount($districtId, $installedStatuses, 2);
  197. $ostEduOrders = $totalEduOrders - $mountEduOrders;
  198. $ostEduMafs = $totalEduMafs - $mountEduMafs;
  199. $totalZnakOrders = $this->getOrderCount($districtId, null, 3);
  200. $totalZnakMafs = $this->getMafCount($districtId, null, 3);
  201. $mountZnakOrders = $this->getOrderCount($districtId, $installedStatuses, 3);
  202. $mountZnakMafs = $this->getMafCount($districtId, $installedStatuses, 3);
  203. $ostZnakOrders = $totalZnakOrders - $mountZnakOrders;
  204. $ostZnakMafs = $totalZnakMafs - $mountZnakMafs;
  205. $this->data['byDistrict'][$districtId] = [
  206. 'name' => $district,
  207. 'totalSum' => $this->getDistrictSum($districtId),
  208. 'doneSum' => $this->getDistrictSum($districtId, [$handedOverStatus]),
  209. 'totalOrders' => $totalOrders,
  210. 'totalMafs' => $totalMafs,
  211. 'readyOrders' => $readyOrders,
  212. 'readyMafs' => $readyMafs,
  213. 'mountOrders' => $mountOrders,
  214. 'mountMafs' => $mountMafs,
  215. 'readyDoneOrders' => $readyDoneOrders,
  216. 'readyDoneMafs' => $readyDoneMafs,
  217. 'doneOrders' => $doneOrders,
  218. 'doneMafs' => $doneMafs,
  219. 'statusOstOrders' => $statusOstOrders,
  220. 'statusOstMafs' => $statusOstMafs,
  221. 'totalYardOrders' => $totalYardOrders,
  222. 'totalYardMafs' => $totalYardMafs,
  223. 'mountYardOrders' => $mountYardOrders,
  224. 'mountYardMafs' => $mountYardMafs,
  225. 'ostYardOrders' => $ostYardOrders,
  226. 'ostYardMafs' => $ostYardMafs,
  227. 'totalEduOrders' => $totalEduOrders,
  228. 'totalEduMafs' => $totalEduMafs,
  229. 'mountEduOrders' => $mountEduOrders,
  230. 'mountEduMafs' => $mountEduMafs,
  231. 'ostEduOrders' => $ostEduOrders,
  232. 'ostEduMafs' => $ostEduMafs,
  233. 'totalZnakOrders' => $totalZnakOrders,
  234. 'totalZnakMafs' => $totalZnakMafs,
  235. 'mountZnakOrders' => $mountZnakOrders,
  236. 'mountZnakMafs' => $mountZnakMafs,
  237. 'ostZnakOrders' => $ostZnakOrders,
  238. 'ostZnakMafs' => $ostZnakMafs,
  239. ];
  240. }
  241. return view('reports.index', $this->data);
  242. }
  243. private function appendReclamationSparePartsToReport(Collection $reclamations): void
  244. {
  245. foreach ($reclamations as $reclamation) {
  246. foreach ($reclamation->spareParts as $sparePart) {
  247. $article = trim((string) $sparePart->article);
  248. $quantity = (int) ($sparePart->pivot?->quantity ?? 0);
  249. if ($article === '' || $quantity < 1) {
  250. continue;
  251. }
  252. if (isset($this->data['reclamationDetails'][$article])) {
  253. $this->data['reclamationDetails'][$article] += $quantity;
  254. } else {
  255. $this->data['reclamationDetails'][$article] = $quantity;
  256. }
  257. }
  258. }
  259. }
  260. private function getOrderCount($districtId, $status = null, $type = null)
  261. {
  262. $q = Order::query()->where('district_id', '=', $districtId);
  263. if($status) {
  264. if(!is_array($status)) $status = [$status];
  265. $q->whereIn('order_status_id', $status);
  266. }
  267. if($type) {
  268. if(!is_array($type)) $type = [$type];
  269. $q->whereIn('object_type_id', $type);
  270. }
  271. return $q->count();
  272. }
  273. private function getMafCount($districtId, $status = null, $type = null)
  274. {
  275. return ProductSKU::query()->
  276. whereHas('order', function ($query) use ($districtId, $status, $type) {
  277. $query->where('district_id', '=', $districtId);
  278. if($status) {
  279. if(!is_array($status)) $status = [$status];
  280. $query->whereIn('order_status_id', $status);
  281. }
  282. if($type) {
  283. if(!is_array($type)) $type = [$type];
  284. $query->whereIn('object_type_id', $type);
  285. }
  286. })->count();
  287. }
  288. private function getDistrictSum($districtId, $done = false)
  289. {
  290. return Price::format(
  291. $this->getDoneSumByStatus($done ?: null, $districtId)
  292. );
  293. }
  294. private function getDoneSumByStatus(array|int|null $statuses = null, ?int $districtId = null): float
  295. {
  296. $query = ProductSKU::query()
  297. ->join('orders', 'orders.id', '=', 'products_sku.order_id')
  298. ->join('products', 'products.id', '=', 'products_sku.product_id')
  299. ->where('orders.year', year())
  300. ->where('products.year', year());
  301. if ($districtId !== null) {
  302. $query->where('orders.district_id', $districtId);
  303. }
  304. if ($statuses !== null && $statuses !== false) {
  305. $statuses = is_array($statuses) ? $statuses : [$statuses];
  306. $query->whereIn('orders.order_status_id', $statuses);
  307. }
  308. return ((float) $query->sum('products.total_price')) / 100;
  309. }
  310. }