OwnerStoreFeeReportService.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace App\Services;
  3. use App\OwnerBillReport;
  4. use App\OwnerStoreFeeDetail;
  5. use App\Traits\ServiceAppAop;
  6. use App\OwnerStoreFeeReport;
  7. use App\Traits\SettlementBillTrait;
  8. use Carbon\Carbon;
  9. use Illuminate\Contracts\Pagination\LengthAwarePaginator;
  10. use Illuminate\Database\Eloquent\Builder;
  11. use Illuminate\Support\Facades\DB;
  12. class OwnerStoreFeeReportService
  13. {
  14. const TYPE = '入库费-合计';
  15. use ServiceAppAop;
  16. use SettlementBillTrait;
  17. protected $modelClass = OwnerStoreFeeReport::class;
  18. /** @var $archiveService OwnerBillReportArchiveService */
  19. private $archiveService;
  20. /** @var $detailService OwnerStoreFeeDetailService */
  21. private $detailService;
  22. /**
  23. * 生成报表数据
  24. * 如果参数$counting_month为空 统计上一个月的
  25. * 如果参数$counting_month为2021-01-01 则统计2021-01-01 -- 2021-01-31之间的数据
  26. * @param null $counting_month 统计月份,默认统计上个月的 2021-05-01
  27. */
  28. public function recordReport($counting_month = null)
  29. {
  30. $this->detailService = app('OwnerStoreFeeDetailService');
  31. if (is_null($counting_month)) {
  32. //默认统计上个月的数据
  33. $counting_month = now()->subMonth()->startOfMonth()->toDateString();
  34. }
  35. $this->reportDate = $counting_month;
  36. $start = $this->reportDate;
  37. $end = Carbon::parse($this->reportDate)->endOfMonth()->toDateString();
  38. $details =
  39. DB::table('owner_store_fee_details')
  40. ->leftJoin('owner_fee_details', 'owner_fee_detail_id', '=', 'owner_fee_details.id')
  41. ->selectRaw("DATE_FORMAT(owner_store_fee_details.created_at,'%Y-%m') as counting_month,
  42. unit_id,
  43. unit_price,
  44. sum(amount) as amounts ,
  45. owner_store_fee_details.owner_id,
  46. owner_fee_detail_id,
  47. sum(owner_fee_details.work_fee) as work_fee,
  48. owner_store_fee_details.owner_price_operation_id")
  49. ->whereBetween('owner_store_fee_details.created_at', [$start, $end])
  50. ->groupBy('counting_month', 'owner_store_fee_details.owner_id', 'owner_store_fee_details.owner_price_operation_id', 'unit_id', 'unit_price')
  51. ->get();
  52. $reports = [];
  53. foreach ($details as $detail) {
  54. $counting_month = Carbon::parse($detail->counting_month)->startOfMonth()->toDateString();
  55. $ownerBillReport = OwnerBillReport::query()
  56. ->selectRaw("id")
  57. ->where('owner_id', $detail->owner_id)
  58. ->where('counting_month', $counting_month)->first();
  59. $reports[] = [
  60. 'owner_bill_report_id' => $ownerBillReport->id ?? null,
  61. 'owner_price_operation_id' => $detail->owner_price_operation_id,
  62. 'owner_id' => $detail->owner_id,
  63. 'counting_month' => $counting_month,
  64. 'unit_id' => $detail->unit_id,
  65. 'unit_price' => $detail->unit_price,
  66. 'amount' => $detail->amounts,
  67. 'fee' => $detail->work_fee,
  68. ];
  69. }
  70. $reports_chunked = array_chunk($reports,1000 );
  71. foreach ($reports_chunked as $items) {
  72. OwnerStoreFeeReport::query()->insertOrIgnore($items);
  73. }
  74. }
  75. public function get(array $kvPairs): array
  76. {
  77. $this->archiveService = app('OwnerBillReportArchiveService');
  78. $this->detailService = app('OwnerStoreFeeDetailService');
  79. if ($this->archiveService->isArchived($kvPairs['counting_month'], $kvPairs['owner_id'], $kvPairs['type']) == 1) {
  80. //查询存档数据
  81. $archived = $this->archiveService->get($kvPairs);
  82. $reports = collect($archived->information['reports']);
  83. $totalAmount = $archived->information['totalAmount'];
  84. $totalFee = $archived->information['totalFee'];
  85. $owner_price_operation_fees = collect($archived->information['owner_price_operation_fees']);
  86. } else {
  87. $reports = $this->getSql($kvPairs['owner_id'], $kvPairs['counting_month'])->orderByDesc('owner_price_operation_id')->get();
  88. $totalAmount = $reports->sum('amount');
  89. $totalFee = number_format($reports->sum('fee'),2);
  90. $owner_price_operation_fees = OwnerStoreFeeReport::query()
  91. ->with('ownerPriceOperation:id,name')
  92. ->selectRaw("sum(fee) as fee,owner_price_operation_id")
  93. ->where('owner_id', $kvPairs['owner_id'])
  94. ->where('counting_month', $kvPairs['counting_month'])
  95. ->groupBy('owner_price_operation_id')->get();
  96. }
  97. return array($reports, $totalAmount, $totalFee,$owner_price_operation_fees);
  98. }
  99. /**
  100. * @param $owner_id
  101. * @param $counting_month
  102. * @return Builder
  103. */
  104. public function getSql($owner_id, $counting_month): Builder
  105. {
  106. return OwnerStoreFeeReport::query()
  107. ->with(['unit:id,name'])
  108. ->where('owner_id', $owner_id)
  109. ->where('counting_month', $counting_month);
  110. }
  111. }