|
|
@@ -5,7 +5,9 @@ namespace App\Services;
|
|
|
use App\Interfaces\SettlementBillReportInterface;
|
|
|
use App\OwnerBillReport;
|
|
|
use App\OwnerBillReportArchive;
|
|
|
+use App\OwnerFeeDetail;
|
|
|
use App\OwnerStoreFeeReport;
|
|
|
+use App\OwnerStoreOutFeeDetail;
|
|
|
use App\Traits\ServiceAppAop;
|
|
|
use App\OwnerStoreOutFeeReport;
|
|
|
use Carbon\Carbon;
|
|
|
@@ -37,29 +39,27 @@ class OwnerStoreOutFeeReportService implements SettlementBillReportInterface
|
|
|
$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("
|
|
|
+ $details = OwnerStoreOutFeeDetail::query()
|
|
|
+ ->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();
|
|
|
+ step,
|
|
|
+ unit_id,
|
|
|
+ unit_price,
|
|
|
+ sum(amount) as amount ,
|
|
|
+ sum(fee) as fee ,
|
|
|
+ owner_id,
|
|
|
+ work_name
|
|
|
+ ")
|
|
|
+ ->whereBetween('created_at', [$start, $end])
|
|
|
+ ->groupBy(
|
|
|
+ 'counting_month',
|
|
|
+ 'step',
|
|
|
+ 'unit_id',
|
|
|
+ 'unit_price',
|
|
|
+ 'owner_id',
|
|
|
+ 'work_name'
|
|
|
+ )->get();
|
|
|
+
|
|
|
$reports = [];
|
|
|
foreach ($details as $detail) {
|
|
|
$counting_month = Carbon::parse($detail->counting_month)->startOfMonth()->toDateString();
|
|
|
@@ -69,16 +69,20 @@ class OwnerStoreOutFeeReportService implements SettlementBillReportInterface
|
|
|
->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,
|
|
|
+ 'amount' => $detail->amount,
|
|
|
+ 'fee' => $detail->fee,
|
|
|
+ 'work_name' => $detail->work_name,
|
|
|
];
|
|
|
}
|
|
|
+
|
|
|
+ //保证幂等性 插入前删除该月的统计数据
|
|
|
+ OwnerStoreOutFeeReport::query()->where('counting_month', $counting_month)->delete();
|
|
|
+
|
|
|
foreach (array_chunk($reports, 1000) as $reports_chunked) {
|
|
|
OwnerStoreOutFeeReport::query()->insertOrIgnore($reports_chunked);
|
|
|
}
|
|
|
@@ -104,6 +108,7 @@ class OwnerStoreOutFeeReportService implements SettlementBillReportInterface
|
|
|
function get(array $kvPairs)
|
|
|
{
|
|
|
$this->archiveService = app('OwnerBillReportArchiveService');
|
|
|
+ list($start, $end) = $this->getStartAndEnd($kvPairs['counting_month']);
|
|
|
if ($this->archiveService->isArchived($kvPairs['counting_month'], $kvPairs['owner_id'], $kvPairs['type']) == 1) {
|
|
|
//查询存档数据
|
|
|
$archived = $this->archiveService->get($kvPairs);
|
|
|
@@ -115,25 +120,18 @@ class OwnerStoreOutFeeReportService implements SettlementBillReportInterface
|
|
|
->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
|
|
|
+ sum(fee) as fee,
|
|
|
+ work_name
|
|
|
")
|
|
|
+ ->where('counting_month' , $kvPairs['counting_month'])
|
|
|
->where('owner_id', $kvPairs['owner_id'])
|
|
|
- ->where('counting_month', $kvPairs['counting_month'])
|
|
|
- ->groupBy('owner_price_operations.name')
|
|
|
+ ->groupBy('work_name')
|
|
|
->get();
|
|
|
-
|
|
|
- $fee_total = OwnerStoreOutFeeReport::query()
|
|
|
- ->where('owner_id', $kvPairs['owner_id'])
|
|
|
- ->where('counting_month', $kvPairs['counting_month'])
|
|
|
- ->sum('fee');
|
|
|
+ $fee_total = $work_name_fee_total->sum('fee');
|
|
|
}
|
|
|
return array($reports, $work_name_fee_total, $fee_total);
|
|
|
}
|