| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- <?php
- namespace App\Http\Controllers;
- use App\Owner;
- use App\OwnerBillReport;
- use App\OwnerBillReportArchive;
- use App\Services\OwnerBillReportArchiveService;
- use App\Services\OwnerLogisticFeeReportService;
- use App\Services\UserService;
- use Illuminate\Http\RedirectResponse;
- use Illuminate\Http\Request;
- use Illuminate\Pagination\LengthAwarePaginator;
- use Oursdreams\Export\Export;
- class OwnerLogisticFeeReportController extends Controller
- {
- /* @var OwnerLogisticFeeReportService $service */
- private $service;
- /* @var UserService $userService */
- private $userService;
- /** @var $archiveService OwnerBillReportArchiveService */
- private $archiveService;
- /**
- * OwnerLogisticFeeReportController constructor.
- */
- public function __construct()
- {
- $this->middleware('auth');
- }
- /**
- * Display a listing of the resource.
- */
- public function index(Request $request)
- {
- $paginateParams = $request->input();
- list($permittingOwnerIds, $counting_month, $owner_id) = $this->getRequestParams($request->year, $request->month, $request->owner_id);
- list($reports, $recordTotal) = $this->service->get([
- 'owner_id' => $owner_id,
- 'counting_month' => $counting_month,
- 'paginateParams' => $paginateParams,
- 'type' => $this->service::TYPE,
- ]);
- $reportPaginator = null;
- if ($reports instanceof LengthAwarePaginator) {
- $reportPaginator = $reports;
- $reports = collect($reportPaginator->items());
- }
- $owner = Owner::query()->selectRaw("name,id")->find($owner_id);
- $owners = Owner::query()->selectRaw("id,name")->whereIn('id', $permittingOwnerIds)->get();
- $this->archiveService = app('OwnerBillReportArchiveService');
- $isArchived = $this->archiveService->isArchived($counting_month, $owner_id, OwnerBillReportArchive::$enums['type']['快递费-合计']);
- $request = $request->all();
- $request['year'] = \Carbon\Carbon::parse($counting_month)->year;
- $request['month'] = \Carbon\Carbon::parse($counting_month)->month;
- $request = collect($request);
- return view('finance.settlementBills.logisticFee.report.index', compact('reports', 'recordTotal', 'paginateParams', 'owners', 'owner', 'isArchived', 'request', 'reportPaginator'));
- }
- public function export(Request $request)
- {
- $this->archiveService = app('OwnerBillReportArchiveService');
- list($permittingOwnerIds, $counting_month, $owner_id) = $this->getRequestParams($request->year, $request->month, $request->owner_id);
- if ($this->archiveService->isArchived($counting_month, $owner_id, $this->service::TYPE) == 1) {
- //已确认账单导出
- list($reports, $recordTotal) = $this->service->get([
- 'owner_id' => $owner_id,
- 'counting_month' => $counting_month,
- 'type' => $this->service::TYPE,
- ]);
- } else {
- //未确认账单导出
- $query = $this->service->getSql($owner_id, $counting_month);
- if (!$request->exists('checkAllSign')) {
- $query->whereIn('id', explode(',', $request['data']));
- }
- $reports = $query->get();
- }
- $json = [];
- foreach ($reports as $report) {
- $json[] = [
- $report['logistic']['name'] ?? '',
- $report['province'],
- $report['initial_weight'],
- $report['initial_amount'],
- $report['additional_weight'],
- $report['additional_amount'],
- $report['fee'],
- ];
- }
- $row = ['快递公司', '地区', '首重', '订单数', '续重', '续重合计', '(省份)合计'];
- return Export::make($row, $json, "快递费用合计");
- }
- /**
- * @param $year
- * @param $month
- * @param $owner_id
- * @return array
- */
- private 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 Request $request
- * @return RedirectResponse
- */
- public function confirmBill(Request $request)
- {
- $this->service = app('OwnerLogisticFeeReportService');
- $this->archiveService = app('OwnerBillReportArchiveService');
- list($permittingOwnerIds, $counting_month, $owner_id) = $this->getRequestParams($request->year, $request->month, $request->owner_id);
- $billReport = OwnerBillReport::query()
- ->select('storage_fee', 'id')
- ->where('owner_id', $owner_id)
- ->where('counting_month', $counting_month)
- ->firstOr(function () {
- return new OwnerBillReport();
- });
- $reports = $this->service->getRecords($owner_id, $counting_month);
- $recordTotal = $this->service->getRecordTotal($owner_id, $counting_month);
- OwnerBillReportArchive::query()->create([
- 'owner_bill_report_id' => $billReport->id ?? null,
- 'owner_id' => $owner_id,
- 'counting_mouth' => $counting_month,
- 'type' => $this->service::TYPE,
- 'archiver_id' => auth()->id(),
- 'archived_at' => now(),
- 'information' => [
- 'reports' => $reports,
- 'recordTotal' => $recordTotal,
- ],
- ]);
- return back()->with('success', '确认成功');
- }
- }
|