| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?php
- namespace App\Services;
- use App\OwnerAreaReport;
- use App\OwnerBillReport;
- use App\Traits\ServiceAppAop;
- class SettlementBillsAreaFeeService
- {
- /** @var $archiveService OwnerBillReportArchiveService */
- private $archiveService;
- const TYPE = '仓储费';
- use ServiceAppAop;
- /**
- * @param $year
- * @param $month
- * @param $owner_id
- * @return array
- */
- public function getRequestParams($year, $month, $owner_id): array
- {
- $this->service = app('OwnerLogisticFeeReportService');
- $this->userService = app('UserService');
- $permittingOwnerIds = $this->userService->getPermittingOwnerIds(auth()->user());
- if (is_null($year)) {
- $year = now()->subMonth()->year;
- }
- if (is_null($month)) {
- $month = now()->subMonth()->month;
- }
- $counting_month = $year . '-' . $month . '-' . '01';
- if (is_null($owner_id)) {
- $owner_id = $permittingOwnerIds[0];
- }
- return array($permittingOwnerIds, $counting_month, $owner_id);
- }
- /**
- * @param $owner_id
- * @param $counting_month
- * @return array
- */
- public function get($owner_id, $counting_month): array
- {
- $this->archiveService = app('OwnerBillReportArchiveService');
- $archived = $this->archiveService->getSql(\Carbon\Carbon::parse($counting_month)->toDateString(), $owner_id, \App\OwnerBillReportArchive::$enums['type']['仓储费'])->first();
- if ($archived ?? false) {
- $areaReports = collect($archived->information['areaReports']);
- $billReport = collect($archived->information['billReport']);
- $price = $archived->information['price'];
- } else {
- $areaReports = OwnerAreaReport::query()
- ->with('ownerStoragePriceModel:id,using_type,price')
- ->where('owner_id', $owner_id)
- ->where('counting_month', $counting_month)
- ->get();
- $billReport = OwnerBillReport::query()
- ->select(['storage_fee', 'id'])
- ->where('owner_id', $owner_id)
- ->where('counting_month', $counting_month)
- ->firstOr(function () {
- return new OwnerBillReport();
- });
- $totalArea = $areaReports->reduce(function ($carry, $areaReport) {
- return $carry + $areaReport->accounting_area;
- }, 0);
- try {
- $price = number_format(($billReport->storage_fee ?? 0) / $totalArea, 3);
- } catch (\Exception $e) {
- $price = 0;
- }
- }
- return array($areaReports, $billReport, $price);
- }
- }
|