SettlementBillStoreOutFeeReportController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Interfaces\SettlementBillControllerInterface;
  4. use App\Owner;
  5. use App\OwnerBillReport;
  6. use App\OwnerBillReportArchive;
  7. use App\OwnerStoreOutFeeReport;
  8. use App\Services\OwnerBillReportArchiveService;
  9. use App\Services\OwnerStoreOutFeeReportService;
  10. use App\Traits\SettlementBillTrait;
  11. use Illuminate\Http\RedirectResponse;
  12. use Illuminate\Http\Request;
  13. use Oursdreams\Export\Export;
  14. class SettlementBillStoreOutFeeReportController extends Controller implements SettlementBillControllerInterface
  15. {
  16. use SettlementBillTrait;
  17. //TODO 缺少总计费用
  18. /* @var OwnerStoreOutFeeReportService $service */
  19. private $service;
  20. /** @var $archiveService OwnerBillReportArchiveService */
  21. private $archiveService;
  22. /**
  23. * OwnerStoreOutFeeReportController constructor.
  24. */
  25. public function __construct()
  26. {
  27. $this->service = app('OwnerStoreOutFeeReportService');
  28. $this->archiveService = app('OwnerBillReportArchiveService');
  29. }
  30. public function index(Request $request)
  31. {
  32. list($permittingOwnerIds, $counting_month, $owner_id) = $this->getRequestParams($request->year, $request->month, $request->owner_id);
  33. list($reports, $work_name_fee_total, $fee_total) = $this->service->get([
  34. 'owner_id' => $owner_id,
  35. 'counting_month' => $counting_month,
  36. 'type' => $this->service::TYPE,
  37. ]);
  38. $owner = Owner::query()->selectRaw("name,id")->find($owner_id);
  39. $owners = Owner::query()->selectRaw("id,name")->whereIn('id', $permittingOwnerIds)->get();
  40. $isArchived = $this->archiveService->isArchived($counting_month, $owner_id, $this->service::TYPE);
  41. $request = $this->buildRequest($request, $counting_month);
  42. $work_name_fee_total = collect($this->buildWorkNameFeeTotal($reports, $work_name_fee_total));
  43. return view('finance.settlementBills.storeOutFee.report.index',
  44. compact('owners', 'owner', 'isArchived', 'request', 'work_name_fee_total', 'fee_total'));
  45. }
  46. public function confirmBill(Request $request): RedirectResponse
  47. {
  48. list($permittingOwnerIds, $counting_month, $owner_id) = $this->getRequestParams($request->year, $request->month, $request->owner_id);
  49. $billReport = OwnerBillReport::query()
  50. ->select('storage_fee', 'id')
  51. ->where('owner_id', $owner_id)
  52. ->where('counting_month', $counting_month)
  53. ->firstOr(function () {
  54. return new OwnerBillReport();
  55. });
  56. list($reports, $work_name_fee_total, $fee_total) = $this->service->get([
  57. 'owner_id' => $owner_id,
  58. 'counting_month' => $counting_month,
  59. 'type' => $this->service::TYPE,
  60. ]);
  61. OwnerBillReportArchive::query()->create([
  62. 'owner_bill_report_id' => $billReport->id ?? null,
  63. 'owner_id' => $owner_id,
  64. 'counting_month' => $counting_month,
  65. 'type' => $this->service::TYPE,
  66. 'archiver_id' => auth()->id(),
  67. 'archived_at' => now(),
  68. 'information' => [
  69. 'reports' => $reports,
  70. 'work_name_fee_total' => $work_name_fee_total,
  71. 'fee_total' => $fee_total,
  72. ],
  73. ]);
  74. return back()->with('success', '确认成功');
  75. }
  76. public function export(Request $request)
  77. {
  78. list($permittingOwnerIds, $counting_month, $owner_id) = $this->getRequestParams($request->year, $request->month, $request->owner_id);
  79. list($reports, $work_name_fee_total, $fee_total) = $this->service->get([
  80. 'owner_id' => $owner_id,
  81. 'counting_month' => $counting_month,
  82. 'type' => $this->service::TYPE,
  83. ]);
  84. $json = [];
  85. $work_name_fee_total = collect($this->buildWorkNameFeeTotal($reports, $work_name_fee_total));
  86. foreach ($work_name_fee_total as $work_name_fee_total_item) {
  87. foreach ($work_name_fee_total_item['data'] as $data_item) {
  88. $json[] = [
  89. $work_name_fee_total_item['name'],
  90. $data_item['step'],
  91. $data_item['unit_price'],
  92. $data_item['amount'],
  93. $work_name_fee_total_item['fee'],
  94. ];
  95. }
  96. }
  97. $row = ['作业名称', '阶梯', '单价', '数量', '合计',];
  98. return Export::make($row, $json, "出库费合计");
  99. }
  100. private function buildWorkNameFeeTotal($reports, $work_name_fee_total)
  101. {
  102. $result = [];
  103. $reports_grouped = $reports->groupBy('owner_price_operation_id');
  104. foreach ($work_name_fee_total as $work_name_fee_total_item) {
  105. $work_name_fee_total_item['data'] = $reports_grouped[$work_name_fee_total_item['id']];
  106. $result[] = $work_name_fee_total_item;
  107. }
  108. return $result;
  109. }
  110. }