ReportController.php 15 KB

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