OwnerStoreFeeReportService.php 5.3 KB

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