SettlementBillStoreFeeReportController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. public function index(Request $request)
  21. {
  22. $this->service = app('OwnerStoreFeeReportService');
  23. $this->archiveService = app('OwnerBillReportArchiveService');
  24. list($permittingOwnerIds, $counting_month, $owner_id) = $this->service->getRequestParams($request->year, $request->month, $request->owner_id);
  25. list($reports, $totalAmount, $totalFee, $owner_price_operation_fees) = $this->service->get([
  26. 'owner_id' => $owner_id,
  27. 'counting_month' => $counting_month,
  28. 'type' => $this->service::TYPE,
  29. ]);
  30. $owner_price_operation_fees = $this->buildOwnerPriceOperationFees($reports, $owner_price_operation_fees);
  31. $owner = Owner::query()->selectRaw("name,id")->find($owner_id);
  32. $owners = Owner::query()->selectRaw("id,name")->whereIn('id', $permittingOwnerIds)->get();
  33. $this->archiveService = app('OwnerBillReportArchiveService');
  34. $isArchived = $this->archiveService->isArchived($counting_month, $owner_id, $this->service::TYPE);
  35. $request = $this->buildRequest($request, $counting_month);
  36. return view('finance.settlementBills.storeFee.report.index',
  37. compact('owners', 'owner', 'isArchived', 'request', 'reports', 'totalAmount', 'totalFee', 'owner_price_operation_fees'));
  38. }
  39. public function confirmBill(Request $request): RedirectResponse
  40. {
  41. $this->service = app('OwnerStoreFeeReportService');
  42. $this->archiveService = app('OwnerBillReportArchiveService');
  43. list($permittingOwnerIds, $counting_month, $owner_id) = $this->service->getRequestParams($request->year, $request->month, $request->owner_id);
  44. $billReport = OwnerBillReport::query()
  45. ->select('storage_fee', 'id')
  46. ->where('owner_id', $owner_id)
  47. ->where('counting_month', $counting_month)
  48. ->firstOr(function () {
  49. return new OwnerBillReport();
  50. });
  51. list($reports, $totalAmount, $totalFee, $owner_price_operation_fees) = $this->service->get([
  52. 'owner_id' => $owner_id,
  53. 'counting_month' => $counting_month,
  54. 'type' => $this->service::TYPE,
  55. ]);
  56. OwnerBillReportArchive::query()->create([
  57. 'owner_bill_report_id' => $billReport->id ?? null,
  58. 'owner_id' => $owner_id,
  59. 'counting_month' => $counting_month,
  60. 'type' => $this->service::TYPE,
  61. 'archiver_id' => auth()->id(),
  62. 'archived_at' => now(),
  63. 'information' => [
  64. 'reports' => $reports,
  65. 'totalAmount' => $totalAmount,
  66. 'totalFee' => $totalFee,
  67. 'owner_price_operation_fees' => $owner_price_operation_fees
  68. ],
  69. ]);
  70. return back()->with('success', '确认成功');
  71. }
  72. public function export(Request $request)
  73. {
  74. $this->service = app('OwnerStoreFeeReportService');
  75. $this->archiveService = app('OwnerBillReportArchiveService');
  76. list($permittingOwnerIds, $counting_month, $owner_id) = $this->service->getRequestParams($request->year, $request->month, $request->owner_id);
  77. list($reports, $totalAmount, $totalFee, $owner_price_operation_fees) = $this->service->get([
  78. 'owner_id' => $owner_id,
  79. 'counting_month' => $counting_month,
  80. 'type' => $this->service::TYPE,
  81. ]);
  82. $json = [];
  83. foreach ($reports as $report) {
  84. $operation = $owner_price_operation_fees->firstWhere('owner_price_operation_id',$report['owner_price_operation_id']);
  85. $json[] = [
  86. $operation['owner_price_operation']['name']??$operation['ownerPriceOperation']['name'],
  87. $report['unit_price'],
  88. $report['amount'],
  89. $operation['fee'],
  90. ];
  91. }
  92. $row = ['名称', '单价', '数量', '合计'];
  93. return Export::make($row, $json, "入库费合计");
  94. }
  95. /**
  96. * @param $reports
  97. * @param $owner_price_operation_fees
  98. * @return \Illuminate\Support\Collection|Collection
  99. */
  100. private function buildOwnerPriceOperationFees($reports, $owner_price_operation_fees)
  101. {
  102. $result = [];
  103. $reports = $reports->groupBy('owner_price_operation_id');
  104. foreach ($owner_price_operation_fees as $price_operation) {
  105. $price_operation['data'] = $reports[$price_operation['owner_price_operation_id']];
  106. $result[] = $price_operation;
  107. }
  108. return collect($result);
  109. }
  110. }