| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- namespace App\Services;
- use App\Interfaces\SettlementBillReportInterface;
- use App\OwnerBillReport;
- use App\OwnerBillReportArchive;
- use App\OwnerStoreFeeReport;
- use App\Traits\ServiceAppAop;
- use App\OwnerStoreOutFeeReport;
- use Carbon\Carbon;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Support\Facades\DB;
- class OwnerStoreOutFeeReportService implements SettlementBillReportInterface
- {
- const TYPE = '出库费-合计';
- use ServiceAppAop;
- use \App\Traits\SettlementBillServiceTrait;
- protected $modelClass = OwnerStoreOutFeeReport::class;
- /** @var $detailService OwnerBillReportArchiveService */
- private $archiveService;
- /** @var $detailService OwnerStoreOutFeeDetailService */
- private $detailService;
- public function recordReport($counting_month = null)
- {
- $this->detailService = app('OwnerStoreOutFeeDetailService');
- 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_out_fee_details')
- ->leftJoin('owner_fee_details', 'owner_store_out_fee_details.owner_fee_detail_id', '=', 'owner_fee_details.id')
- ->leftJoin('owner_price_operations', 'owner_store_out_fee_details.owner_price_operation_id', '=', 'owner_price_operations.id')
- ->selectRaw("
- DATE_FORMAT(owner_store_out_fee_details.created_at,'%Y-%m') as counting_month,
- owner_store_out_fee_details.unit_id,
- owner_store_out_fee_details.unit_price,
- sum(owner_store_out_fee_details.amount) as amounts ,
- owner_store_out_fee_details.owner_id,
- owner_store_out_fee_details.owner_fee_detail_id,
- owner_store_out_fee_details.owner_price_operation_id,
- owner_store_out_fee_details.step,
- sum(owner_fee_details.work_fee) as work_fee,
- owner_price_operations.name
- ")
- ->whereBetween('owner_store_out_fee_details.created_at', [$start, $end])
- ->groupBy('counting_month',
- 'owner_store_out_fee_details.owner_id',
- 'owner_store_out_fee_details.step',
- 'owner_price_operations.id'
- )
- ->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' => $detail->owner_price_operation_id,
- 'owner_id' => $detail->owner_id,
- 'counting_month' => $counting_month,
- 'step' => $detail->step,
- 'unit_id' => $detail->unit_id,
- 'unit_price' => $detail->unit_price,
- 'amount' => $detail->amounts,
- 'fee' => $detail->work_fee,
- ];
- }
- foreach (array_chunk($reports, 1000) as $reports_chunked) {
- OwnerStoreOutFeeReport::query()->insertOrIgnore($reports_chunked);
- }
- }
- public function getSql($owner_id, $counting_month): Builder
- {
- // TODO: Implement getSql() method.
- }
- public function buildExport($details): array
- {
- // TODO: Implement buildExport() method.
- }
- public function switchType($type)
- {
- // TODO: Implement switchType() method.
- }
- function get(array $kvPairs)
- {
- $this->archiveService = app('OwnerBillReportArchiveService');
- if ($this->archiveService->isArchived($kvPairs['counting_month'], $kvPairs['owner_id'], $kvPairs['type']) == 1) {
- //查询存档数据
- $archived = $this->archiveService->get($kvPairs);
- $reports = collect($archived->information['reports']);
- $work_name_fee_total = collect($archived->information['work_name_fee_total']);
- $fee_total = $archived->information['fee_total'];
- } else {
- $reports = OwnerStoreOutFeeReport::query()
- ->with(['unit:id,name'])
- ->where('owner_id', $kvPairs['owner_id'])
- ->where('counting_month', $kvPairs['counting_month'])
- ->orderBy('owner_price_operation_id')
- ->get();
- $work_name_fee_total = OwnerStoreOutFeeReport::query()
- ->leftJoin('owner_price_operations', 'owner_store_out_fee_reports.owner_price_operation_id', '=', 'owner_price_operations.id')
- ->selectRaw("
- sum(owner_store_out_fee_reports.fee) as fee,
- owner_price_operations.name,
- owner_price_operations.id
- ")
- ->where('owner_id', $kvPairs['owner_id'])
- ->where('counting_month', $kvPairs['counting_month'])
- ->groupBy('owner_price_operations.name')
- ->get();
- $fee_total = OwnerStoreOutFeeReport::query()
- ->where('owner_id', $kvPairs['owner_id'])
- ->where('counting_month', $kvPairs['counting_month'])
- ->sum('fee');
- }
- return array($reports, $work_name_fee_total, $fee_total);
- }
- public function confirmBill($counting_month, $owner_id)
- {
- $billReport = OwnerBillReport::query()
- ->select('storage_fee', 'id')
- ->where('owner_id', $owner_id)
- ->where('counting_month', $counting_month)
- ->firstOr(function () {
- return new OwnerBillReport();
- });
- list($reports, $work_name_fee_total, $fee_total) = $this->get([
- 'owner_id' => $owner_id,
- 'counting_month' => $counting_month,
- 'type' => $this::TYPE,
- ]);
- OwnerBillReportArchive::query()->create([
- 'owner_bill_report_id' => $billReport->id ?? null,
- 'owner_id' => $owner_id,
- 'counting_month' => $counting_month,
- 'type' => $this::TYPE,
- 'archiver_id' => auth()->id(),
- 'archived_at' => now(),
- 'information' => [
- 'reports' => $reports,
- 'work_name_fee_total' => $work_name_fee_total,
- 'fee_total' => $fee_total,
- ],
- ]);
- $this->confirmBillFeeTotal($counting_month, $owner_id);
- }
- }
|