SettlementBillStoreFeeReportController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Owner;
  4. use App\OwnerBillReport;
  5. use App\OwnerBillReportArchive;
  6. use App\Services\OwnerBillReportArchiveService;
  7. use App\Services\OwnerStoreFeeReportService;
  8. use App\Traits\SettlementBillTrait;
  9. use Illuminate\Http\RedirectResponse;
  10. use Illuminate\Http\Request;
  11. use Oursdreams\Export\Export;
  12. use Tightenco\Collect\Support\Collection;
  13. class SettlementBillStoreFeeReportController extends Controller
  14. {
  15. use SettlementBillTrait;
  16. /* @var OwnerStoreFeeReportService $service */
  17. private $service;
  18. /** @var $archiveService OwnerBillReportArchiveService */
  19. private $archiveService;
  20. /**
  21. * SettlementBillStoreFeeReportController constructor.
  22. */
  23. public function __construct()
  24. {
  25. $this->service = app('OwnerStoreFeeReportService');
  26. $this->archiveService = app('OwnerBillReportArchiveService');
  27. }
  28. public function index(Request $request)
  29. {
  30. list($permittingOwnerIds, $counting_month, $owner_id) = $this->getRequestParams($request->year, $request->month, $request->owner_id);
  31. list($reports, $totalAmount, $totalFee, $owner_price_operation_fees) = $this->service->get([
  32. 'owner_id' => $owner_id,
  33. 'counting_month' => $counting_month,
  34. 'type' => $this->service::TYPE,
  35. ]);
  36. $owner_price_operation_fees = $this->buildOwnerPriceOperationFees($reports, $owner_price_operation_fees);
  37. $owner = Owner::query()->selectRaw("name,id")->find($owner_id);
  38. $owners = Owner::query()->selectRaw("id,name")->whereIn('id', $permittingOwnerIds)->get();
  39. $this->archiveService = app('OwnerBillReportArchiveService');
  40. $isArchived = $this->archiveService->isArchived($counting_month, $owner_id, $this->service::TYPE);
  41. $request = $this->buildRequest($request, $counting_month, $owner_id);
  42. return view('finance.settlementBills.storeFee.report.index',
  43. compact('owners', 'owner', 'isArchived', 'request', 'totalAmount', 'totalFee', 'owner_price_operation_fees'));
  44. }
  45. public function export(Request $request)
  46. {
  47. $this->service = app('OwnerStoreFeeReportService');
  48. $this->archiveService = app('OwnerBillReportArchiveService');
  49. list($permittingOwnerIds, $counting_month, $owner_id) = $this->getRequestParams($request->year, $request->month, $request->owner_id);
  50. list($reports, $totalAmount, $totalFee, $owner_price_operation_fees) = $this->service->get([
  51. 'owner_id' => $owner_id,
  52. 'counting_month' => $counting_month,
  53. 'type' => $this->service::TYPE,
  54. ]);
  55. $json = [];
  56. foreach ($reports as $report) {
  57. $operation = $owner_price_operation_fees->firstWhere('work_name', $report['work_name']);
  58. $json[] = [
  59. $report['work_name'],
  60. $report['unit_price'],
  61. $report['amount'],
  62. $operation['fee'],
  63. ];
  64. }
  65. $row = ['名称', '单价', '数量', '合计'];
  66. return Export::make($row, $json, "入库费合计");
  67. }
  68. /**
  69. * @param $reports
  70. * @param $owner_price_operation_fees
  71. * @return \Illuminate\Support\Collection|Collection
  72. */
  73. private function buildOwnerPriceOperationFees($reports, $owner_price_operation_fees)
  74. {
  75. $result = [];
  76. $reports = $reports->groupBy('work_name');
  77. foreach ($owner_price_operation_fees as $price_operation) {
  78. $price_operation['data'] = $reports[$price_operation['work_name']];
  79. $result[] = $price_operation;
  80. }
  81. return collect($result);
  82. }
  83. }