SettlementBillStoreOutFeeReportController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Interfaces\SettlementBillControllerInterface;
  4. use App\Owner;
  5. use App\Services\OwnerBillReportArchiveService;
  6. use App\Services\OwnerStoreOutFeeReportService;
  7. use App\Traits\SettlementBillTrait;
  8. use Illuminate\Http\Request;
  9. use Oursdreams\Export\Export;
  10. class SettlementBillStoreOutFeeReportController extends Controller implements SettlementBillControllerInterface
  11. {
  12. use SettlementBillTrait;
  13. /* @var OwnerStoreOutFeeReportService $service */
  14. private $service;
  15. /** @var $archiveService OwnerBillReportArchiveService */
  16. private $archiveService;
  17. /**
  18. * OwnerStoreOutFeeReportController constructor.
  19. */
  20. public function __construct()
  21. {
  22. $this->service = app('OwnerStoreOutFeeReportService');
  23. $this->archiveService = app('OwnerBillReportArchiveService');
  24. }
  25. public function index(Request $request)
  26. {
  27. list($permittingOwnerIds, $counting_month, $owner_id) = $this->getRequestParams($request->year, $request->month, $request->owner_id);
  28. list($reports, $work_name_fee_total, $fee_total) = $this->service->get([
  29. 'owner_id' => $owner_id,
  30. 'counting_month' => $counting_month,
  31. 'type' => $this->service::TYPE,
  32. ]);
  33. $owner = Owner::query()->selectRaw("name,id")->find($owner_id);
  34. $owners = Owner::query()->selectRaw("id,name")->whereIn('id', $permittingOwnerIds)->get();
  35. $isArchived = $this->archiveService->isArchived($counting_month, $owner_id, $this->service::TYPE);
  36. $request = $this->buildRequest($request, $counting_month, $owner_id);
  37. $work_name_fee_total = collect($this->buildWorkNameFeeTotal($reports, $work_name_fee_total));
  38. return view('finance.settlementBills.storeOutFee.report.index',
  39. compact('owners', 'owner', 'isArchived', 'request', 'work_name_fee_total', 'fee_total'));
  40. }
  41. public function export(Request $request)
  42. {
  43. list($permittingOwnerIds, $counting_month, $owner_id) = $this->getRequestParams($request->year, $request->month, $request->owner_id);
  44. list($reports, $work_name_fee_total, $fee_total) = $this->service->get([
  45. 'owner_id' => $owner_id,
  46. 'counting_month' => $counting_month,
  47. 'type' => $this->service::TYPE,
  48. ]);
  49. $json = [];
  50. $work_name_fee_total = collect($this->buildWorkNameFeeTotal($reports, $work_name_fee_total));
  51. foreach ($work_name_fee_total as $work_name_fee_total_item) {
  52. foreach ($work_name_fee_total_item['data'] as $data_item) {
  53. $json[] = [
  54. $work_name_fee_total_item['work_name'],
  55. $data_item['step'],
  56. $data_item['unit_price'] . '/' . $data_item['unit']['name'] ?? '',
  57. $data_item['amount'],
  58. $work_name_fee_total_item['fee'],
  59. ];
  60. }
  61. }
  62. $row = ['作业名称', '阶梯', '单价', '数量', '合计',];
  63. return Export::make($row, $json, "出库费合计");
  64. }
  65. private function buildWorkNameFeeTotal($reports, $work_name_fee_total): array
  66. {
  67. $result = [];
  68. $reports_grouped = $reports->groupBy('work_name');
  69. foreach ($work_name_fee_total as $work_name_fee_total_item) {
  70. $work_name_fee_total_item['data'] = $reports_grouped[$work_name_fee_total_item['work_name']] ?? [];
  71. $result[] = $work_name_fee_total_item;
  72. }
  73. return $result;
  74. }
  75. }