| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php
- namespace App\Http\Controllers;
- use App\Owner;
- use App\OwnerAreaReport;
- use App\OwnerBillReport;
- use App\OwnerBillReportArchive;
- use Illuminate\Http\Request;
- /**
- * 结算管理-结算账单-仓储费
- * Class SettlementBillOwnerAreaFeeController
- * @package App\Http\Controllers
- */
- class SettlementBillOwnerAreaFeeController extends Controller
- {
- /**
- * SettlementBillOwnerAreaFeeController constructor.
- */
- public function __construct()
- {
- $this->middleware('auth');
- }
- public function index(Request $request)
- {
- list($permittingOwnerIds, $counting_month, $owner_id) = $this->getRequestParams($request);
- $isArchived = $this->ownerBillReportArchiveQuery($counting_month, $owner_id)->exists();
- $isArchived = $isArchived ? 1 : 2;
- list($areaReports, $billReport, $price) = $this->get($owner_id, $counting_month);
- $owners = Owner::query()->find($permittingOwnerIds);
- $owner = Owner::query()->find($owner_id);
- return view('finance.settlementBills.areaFee.index', compact('owner', 'owners', 'areaReports', 'billReport', 'price', 'request', 'isArchived'));
- }
- /**
- * @param Request $request year month owner_id
- * @return array
- */
- private function getRequestParams(Request $request): array
- {
- $this->service = app('OwnerLogisticFeeReportService');
- $this->userService = app('UserService');
- $permittingOwnerIds = $this->userService->getPermittingOwnerIds(auth()->user());
- if (is_null($request->year) || is_null($request->month)) {
- $counting_month = now()->subMonth()->startOfMonth()->toDateString();
- } else {
- $counting_month = $request->year . '-' . $request->month . '-' . '01';
- }
- if (is_null($request->owner_id)) {
- $owner_id = $permittingOwnerIds[0];
- } else {
- $owner_id = $request->owner_id;
- }
- return array($permittingOwnerIds, $counting_month, $owner_id);
- }
- public function confirmBill(Request $request)
- {
- list($permittingOwnerIds, $counting_month, $owner_id) = $this->getRequestParams($request);
- list($areaReports, $billReport, $price) = $this->get($owner_id, $counting_month);
- $information = [
- 'areaReports' => $areaReports,
- 'billReport' => $billReport,
- 'price' => $price,
- ];
- OwnerBillReportArchive::query()->create([
- 'owner_bill_report_id' => $billReport->id ?? null,
- 'owner_id' => $owner_id,
- 'counting_mouth' => $counting_month,
- 'type' => '仓储费',
- 'archiver_id' => auth()->id(),
- 'archived_at' => now(),
- 'information' => $information,
- ]);
- return back();
- }
- /**
- * @param $owner_id
- * @param $counting_month
- * @return array
- */
- private function get($owner_id, $counting_month): array
- {
- $archived = $this->ownerBillReportArchiveQuery($counting_month, $owner_id)->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()
- ->selectRaw('storage_fee')
- ->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 = $billReport->storage_fee ?? 0 / $totalArea;
- } catch (\Exception $e) {
- $price = 0;
- }
- }
- return array($areaReports, $billReport, $price);
- }
- /**
- * @param $counting_month
- * @param $owner_id
- * @return \Illuminate\Database\Eloquent\Builder
- */
- private function ownerBillReportArchiveQuery($counting_month, $owner_id): \Illuminate\Database\Eloquent\Builder
- {
- return OwnerBillReportArchive::query()->where('counting_mouth', $counting_month)
- ->where('owner_id', $owner_id)->where('type', '仓储费');
- }
- }
|