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()->pluck('shortname', 'id')->toArray();
  161. foreach ($districts as $districtId => $district) {
  162. $totalOrders = $this->getOrderCount($districtId);
  163. $totalMafs = $this->getMafCount($districtId);
  164. $readyOrders = $this->getOrderCount($districtId, 4);
  165. $readyMafs = $this->getMafCount($districtId, 4);
  166. $mountOrders = $this->getOrderCount($districtId, 5);
  167. $mountMafs = $this->getMafCount($districtId, 5);
  168. $readyDoneOrders = $this->getOrderCount($districtId, 7);
  169. $readyDoneMafs = $this->getMafCount($districtId, 7);
  170. $doneOrders = $this->getOrderCount($districtId, $doneStatuses);
  171. $doneMafs = $this->getMafCount($districtId, $doneStatuses);
  172. $statusOstOrders = $totalOrders - $mountOrders - $readyDoneOrders - $doneOrders;
  173. $statusOstMafs = $totalMafs - $mountMafs - $readyDoneMafs - $doneMafs;
  174. $totalYardOrders = $this->getOrderCount($districtId, null, 1);
  175. $totalYardMafs = $this->getMafCount($districtId, null, 1);
  176. $mountYardOrders = $this->getOrderCount($districtId, $mountStatuses, 1);
  177. $mountYardMafs = $this->getMafCount($districtId, $mountStatuses, 1);
  178. $ostYardOrders = $totalYardOrders - $mountYardOrders;
  179. $ostYardMafs = $totalYardMafs - $mountYardMafs;
  180. $totalEduOrders = $this->getOrderCount($districtId, null, 2);
  181. $totalEduMafs = $this->getMafCount($districtId, null, 2);
  182. $mountEduOrders = $this->getOrderCount($districtId, $mountStatuses, 2);
  183. $mountEduMafs = $this->getMafCount($districtId, $mountStatuses, 2);
  184. $ostEduOrders = $totalYardOrders - $mountYardOrders;
  185. $ostEduMafs = $totalYardMafs - $mountYardMafs;
  186. $totalZnakOrders = $this->getOrderCount($districtId, null, 3);
  187. $totalZnakMafs = $this->getMafCount($districtId, null, 3);
  188. $mountZnakOrders = $this->getOrderCount($districtId, $mountStatuses, 3);
  189. $mountZnakMafs = $this->getMafCount($districtId, $mountStatuses, 3);
  190. $ostZnakOrders = $totalYardOrders - $mountYardOrders;
  191. $ostZnakMafs = $totalYardMafs - $mountYardMafs;
  192. $this->data['byDistrict'][$district] = [
  193. 'name' => $district,
  194. 'totalSum' => $this->getDistrictSum($districtId),
  195. 'doneSum' => $this->getDistrictSum($districtId, [$handedOverStatus]),
  196. 'totalOrders' => $totalOrders,
  197. 'totalMafs' => $totalMafs,
  198. 'readyOrders' => $readyOrders,
  199. 'readyMafs' => $readyMafs,
  200. 'mountOrders' => $mountOrders,
  201. 'mountMafs' => $mountMafs,
  202. 'readyDoneOrders' => $readyDoneOrders,
  203. 'readyDoneMafs' => $readyDoneMafs,
  204. 'doneOrders' => $doneOrders,
  205. 'doneMafs' => $doneMafs,
  206. 'statusOstOrders' => $statusOstOrders,
  207. 'statusOstMafs' => $statusOstMafs,
  208. 'totalYardOrders' => $totalYardOrders,
  209. 'totalYardMafs' => $totalYardMafs,
  210. 'mountYardOrders' => $mountYardOrders,
  211. 'mountYardMafs' => $mountYardMafs,
  212. 'ostYardOrders' => $ostYardOrders,
  213. 'ostYardMafs' => $ostYardMafs,
  214. 'totalEduOrders' => $totalEduOrders,
  215. 'totalEduMafs' => $totalEduMafs,
  216. 'mountEduOrders' => $mountEduOrders,
  217. 'mountEduMafs' => $mountEduMafs,
  218. 'ostEduOrders' => $ostEduOrders,
  219. 'ostEduMafs' => $ostEduMafs,
  220. 'totalZnakOrders' => $totalZnakOrders,
  221. 'totalZnakMafs' => $totalZnakMafs,
  222. 'mountZnakOrders' => $mountZnakOrders,
  223. 'mountZnakMafs' => $mountZnakMafs,
  224. 'ostZnakOrders' => $ostZnakOrders,
  225. 'ostZnakMafs' => $ostZnakMafs,
  226. ];
  227. }
  228. return view('reports.index', $this->data);
  229. }
  230. private function appendReclamationSparePartsToReport(Collection $reclamations): void
  231. {
  232. foreach ($reclamations as $reclamation) {
  233. foreach ($reclamation->spareParts as $sparePart) {
  234. $article = trim((string) $sparePart->article);
  235. $quantity = (int) ($sparePart->pivot?->quantity ?? 0);
  236. if ($article === '' || $quantity < 1) {
  237. continue;
  238. }
  239. if (isset($this->data['reclamationDetails'][$article])) {
  240. $this->data['reclamationDetails'][$article] += $quantity;
  241. } else {
  242. $this->data['reclamationDetails'][$article] = $quantity;
  243. }
  244. }
  245. }
  246. }
  247. private function getOrderCount($districtId, $status = null, $type = null)
  248. {
  249. $q = Order::query()->where('district_id', '=', $districtId);
  250. if($status) {
  251. if(!is_array($status)) $status = [$status];
  252. $q->whereIn('order_status_id', $status);
  253. }
  254. if($type) {
  255. if(!is_array($type)) $type = [$type];
  256. $q->whereIn('object_type_id', $type);
  257. }
  258. return $q->count();
  259. }
  260. private function getMafCount($districtId, $status = null, $type = null)
  261. {
  262. return ProductSKU::query()->
  263. whereHas('order', function ($query) use ($districtId, $status, $type) {
  264. $query->where('district_id', '=', $districtId);
  265. if($status) {
  266. if(!is_array($status)) $status = [$status];
  267. $query->whereIn('order_status_id', $status);
  268. }
  269. if($type) {
  270. if(!is_array($type)) $type = [$type];
  271. $query->whereIn('object_type_id', $type);
  272. }
  273. })->count();
  274. }
  275. private function getDistrictSum($districtId, $done = false)
  276. {
  277. return Price::format(
  278. $this->getDoneSumByStatus($done ?: null, $districtId)
  279. );
  280. }
  281. private function getDoneSumByStatus(array|int|null $statuses = null, ?int $districtId = null): float
  282. {
  283. $query = ProductSKU::query()
  284. ->join('orders', 'orders.id', '=', 'products_sku.order_id')
  285. ->join('products', 'products.id', '=', 'products_sku.product_id')
  286. ->where('orders.year', year())
  287. ->where('products.year', year());
  288. if ($districtId !== null) {
  289. $query->where('orders.district_id', $districtId);
  290. }
  291. if ($statuses !== null && $statuses !== false) {
  292. $statuses = is_array($statuses) ? $statuses : [$statuses];
  293. $query->whereIn('orders.order_status_id', $statuses);
  294. }
  295. return ((float) $query->sum('products.total_price')) / 100;
  296. }
  297. }