OwnerStoreFeeReportService.php 5.3 KB

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