SettlementBillsAreaFeeService.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace App\Services;
  3. use App\OwnerAreaReport;
  4. use App\OwnerBillReport;
  5. use App\Traits\ServiceAppAop;
  6. class SettlementBillsAreaFeeService
  7. {
  8. /** @var $archiveService OwnerBillReportArchiveService */
  9. private $archiveService;
  10. const TYPE = '仓储费';
  11. use ServiceAppAop;
  12. /**
  13. * @param $year
  14. * @param $month
  15. * @param $owner_id
  16. * @return array
  17. */
  18. public function getRequestParams($year, $month, $owner_id): array
  19. {
  20. $this->service = app('OwnerLogisticFeeReportService');
  21. $this->userService = app('UserService');
  22. $permittingOwnerIds = $this->userService->getPermittingOwnerIds(auth()->user());
  23. if (is_null($year)) {
  24. $year = now()->subMonth()->year;
  25. }
  26. if (is_null($month)) {
  27. $month = now()->subMonth()->month;
  28. }
  29. $counting_month = $year . '-' . $month . '-' . '01';
  30. if (is_null($owner_id)) {
  31. $owner_id = $permittingOwnerIds[0];
  32. }
  33. return array($permittingOwnerIds, $counting_month, $owner_id);
  34. }
  35. /**
  36. * @param $owner_id
  37. * @param $counting_month
  38. * @return array
  39. */
  40. public function get($owner_id, $counting_month): array
  41. {
  42. $this->archiveService = app('OwnerBillReportArchiveService');
  43. $archived = $this->archiveService->getSql(\Carbon\Carbon::parse($counting_month)->toDateString(), $owner_id, \App\OwnerBillReportArchive::$enums['type']['仓储费'])->first();
  44. if ($archived ?? false) {
  45. $areaReports = collect($archived->information['areaReports']);
  46. $billReport = collect($archived->information['billReport']);
  47. $price = $archived->information['price'];
  48. } else {
  49. $areaReports = OwnerAreaReport::query()
  50. ->with('ownerStoragePriceModel:id,using_type,price')
  51. ->where('owner_id', $owner_id)
  52. ->where('counting_month', $counting_month)
  53. ->get();
  54. $billReport = OwnerBillReport::query()
  55. ->select(['storage_fee', 'id'])
  56. ->where('owner_id', $owner_id)
  57. ->where('counting_month', $counting_month)
  58. ->firstOr(function () {
  59. return new OwnerBillReport();
  60. });
  61. $totalArea = $areaReports->reduce(function ($carry, $areaReport) {
  62. return $carry + $areaReport->accounting_area;
  63. }, 0);
  64. try {
  65. $price = number_format(($billReport->storage_fee ?? 0) / $totalArea, 3);
  66. } catch (\Exception $e) {
  67. $price = 0;
  68. }
  69. }
  70. return array($areaReports, $billReport, $price);
  71. }
  72. }