| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- <?php
- namespace App\Services;
- use App\OwnerBillReport;
- use App\Traits\ServiceAppAop;
- use App\OwnerStoreFeeReport;
- use Carbon\Carbon;
- use Illuminate\Contracts\Pagination\LengthAwarePaginator;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Support\Facades\DB;
- class OwnerStoreFeeReportService
- {
- const TYPE = '入库费-合计';
- use ServiceAppAop;
- use \App\Traits\SettlementBillTrait;
- protected $modelClass = OwnerStoreFeeReport::class;
- /** @var $archiveService OwnerBillReportArchiveService */
- private $archiveService;
- /** @var $detailService OwnerStoreFeeDetailService */
- private $detailService;
- /**
- * 生成报表数据
- * 如果参数$counting_month为空 统计上一个月的
- * 如果参数$counting_month为2021-01-01 则统计2021-01-01 -- 2021-01-31之间的数据
- * @param null $counting_month 统计月份,默认统计上个月的 2021-05-01
- */
- public function recordReport($counting_month = null)
- {
- $this->detailService = app('OwnerStoreFeeDetailService');
- if (is_null($counting_month)) {
- //默认统计上个月的数据
- $counting_month = now()->subMonth()->startOfMonth()->toDateString();
- }
- $this->reportDate = $counting_month;
- $start = $this->reportDate;
- $end = Carbon::parse($this->reportDate)->endOfMonth()->toDateString();
- $details =
- DB::table('owner_store_fee_details')
- ->leftJoin('owner_fee_details', 'owner_fee_detail_id', '=', 'owner_fee_details.id')
- ->selectRaw("DATE_FORMAT(owner_store_fee_details.created_at,'%Y-%m-%d') as counting_month,
- owner_store_fee_details.type,unit_id,unit_price,sum(amount) as amounts ,
- owner_store_fee_details.owner_id,owner_fee_detail_id,
- sum(owner_fee_details.work_fee) as work_fee")
- ->whereBetween('owner_store_fee_details.created_at', [$start, $end])
- ->groupBy('counting_month', 'owner_store_fee_details.owner_id', 'owner_store_fee_details.type', 'unit_id', 'unit_price')
- ->get();
- $reports = [];
- foreach ($details as $detail) {
- $counting_month = Carbon::parse($detail->counting_month)->startOfMonth()->toDateString();
- $ownerBillReport = OwnerBillReport::query()
- ->selectRaw("id")
- ->where('owner_id', $detail->owner_id)
- ->where('counting_month', $counting_month)->first();
- $reports[] = [
- 'owner_bill_report_id' => $ownerBillReport->id ?? null,
- 'owner_price_operation_id' => null,//$detail->ownerFeeDetail->ownerPriceOperation->id??null,
- 'owner_id' => $detail->owner_id,
- 'counting_month' => $counting_month,
- 'unit_id' => $detail->unit_id,
- 'unit_price' => $detail->unit_price,
- 'amount' => $detail->amounts,
- 'type' => $this->detailService->switchType($detail->type),
- 'fee' => $detail->work_fee,
- ];
- }
- OwnerStoreFeeReport::query()->insertOrIgnore($reports);
- }
- public function get(array $kvPairs): array
- {
- $this->archiveService = app('OwnerBillReportArchiveService');
- $this->detailService = app('OwnerStoreFeeDetailService');
- if ($this->archiveService->isArchived($kvPairs['counting_month'], $kvPairs['owner_id'], $kvPairs['type']) == 1) {
- //查询存档数据
- $archived = $this->archiveService->get($kvPairs);
- $reports = collect($archived->information['reports']);
- $totalAmount = $archived->information['totalAmount'];
- $totalFee = $archived->information['totalFee'];
- $newFee = $archived->information['newFee'];
- $backFee = $archived->information['backFee'];
- } else {
- $reports = $this->getReports($kvPairs['owner_id'], $kvPairs['counting_month']);
- $totalAmount = $this->getSql($kvPairs['owner_id'], $kvPairs['counting_month'])->sum('amount');
- $totalFee = $this->getSql($kvPairs['owner_id'], $kvPairs['counting_month'])->sum('fee');
- $newFee = $this->getSql($kvPairs['owner_id'], $kvPairs['counting_month'])->where('type', $this->detailService->switchType('新品入库'))->sum('fee');
- $backFee = $this->getSql($kvPairs['owner_id'], $kvPairs['counting_month'])->where('type', $this->detailService->switchType('退货入库'))->sum('fee');
- }
- return array($reports, $totalAmount, $totalFee, $newFee, $backFee);
- }
- public function getPaginate($owner_id, $counting_month, $paginateParams): LengthAwarePaginator
- {
- return $this->getSql($owner_id, $counting_month)
- ->paginate($paginateParams['paginate'] ?? 50);
- }
- public function getReports($owner_id, $counting_month)
- {
- return $this->getSql($owner_id, $counting_month)->orderByDesc('type')->get();
- }
- /**
- * @param $owner_id
- * @param $counting_month
- * @return Builder
- */
- public function getSql($owner_id, $counting_month): Builder
- {
- return OwnerStoreFeeReport::query()
- ->with('unit:id,name')
- ->where('owner_id', $owner_id)
- ->where('counting_month', $counting_month);
- }
- }
|