|
|
@@ -2,7 +2,10 @@
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
+use App\Owner;
|
|
|
use App\OwnerAreaReport;
|
|
|
+use App\OwnerBillReport;
|
|
|
+use App\OwnerBillReportArchive;
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
/**
|
|
|
@@ -22,25 +25,17 @@ class SettlementBillOwnerAreaFeeController extends Controller
|
|
|
|
|
|
public function index(Request $request)
|
|
|
{
|
|
|
- list($permittingOwnerIds, $date, $owner_id) = $this->getRequestParams($request);
|
|
|
- $owners = \App\Owner::query()->find($permittingOwnerIds);
|
|
|
- $owner = \App\Owner::query()->find($owner_id);
|
|
|
- $ownerAreas = OwnerAreaReport::query()
|
|
|
- ->with('ownerStoragePriceModel:id,using_type')
|
|
|
- ->where('owner_id', $owner_id)
|
|
|
- ->where('counting_month', $date)
|
|
|
- ->get();
|
|
|
-
|
|
|
- $ownerBillReport = \App\OwnerBillReport::query()
|
|
|
- ->selectRaw('storage_fee')
|
|
|
- ->where('owner_id', $owner_id)
|
|
|
- ->where('counting_month', $date)
|
|
|
- ->first();
|
|
|
-
|
|
|
+ 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
|
|
|
+ * @param Request $request year month owner_id
|
|
|
* @return array
|
|
|
*/
|
|
|
private function getRequestParams(Request $request): array
|
|
|
@@ -49,15 +44,84 @@ class SettlementBillOwnerAreaFeeController extends Controller
|
|
|
$this->userService = app('UserService');
|
|
|
$permittingOwnerIds = $this->userService->getPermittingOwnerIds(auth()->user());
|
|
|
if (is_null($request->year) || is_null($request->month)) {
|
|
|
- $date = now()->subMonth()->startOfMonth()->toDateString();
|
|
|
+ $counting_month = now()->subMonth()->startOfMonth()->toDateString();
|
|
|
} else {
|
|
|
- $date = $request->year . '-' . $request->month . '-' . '01';
|
|
|
+ $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, $date, $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', '仓储费');
|
|
|
}
|
|
|
}
|