OwnerStoreOutFeeReportService.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace App\Services;
  3. use App\Interfaces\SettlementBillReportInterface;
  4. use App\OwnerBillReport;
  5. use App\OwnerStoreFeeReport;
  6. use App\Traits\ServiceAppAop;
  7. use App\OwnerStoreOutFeeReport;
  8. use Carbon\Carbon;
  9. use Illuminate\Database\Eloquent\Builder;
  10. use Illuminate\Support\Facades\DB;
  11. class OwnerStoreOutFeeReportService implements SettlementBillReportInterface
  12. {
  13. const TYPE = '出库费-合计';
  14. use ServiceAppAop;
  15. protected $modelClass = OwnerStoreOutFeeReport::class;
  16. /** @var $detailService OwnerBillReportArchiveService */
  17. private $archiveService;
  18. /** @var $detailService OwnerStoreOutFeeDetailService */
  19. private $detailService;
  20. public function recordReport($counting_month = null)
  21. {
  22. $this->detailService = app('OwnerStoreOutFeeDetailService');
  23. if (is_null($counting_month)) {
  24. //默认统计上个月的数据
  25. $counting_month = now()->subMonth()->startOfMonth()->toDateString();
  26. }
  27. $this->reportDate = $counting_month;
  28. $start = $this->reportDate;
  29. $end = Carbon::parse($this->reportDate)->endOfMonth()->toDateString();
  30. $details =
  31. DB::table('owner_store_out_fee_details')
  32. ->leftJoin('owner_fee_details', 'owner_fee_detail_id', '=', 'owner_fee_details.id')
  33. ->selectRaw("DATE_FORMAT(owner_store_out_fee_details.created_at,'%Y-%m') as counting_month,
  34. owner_store_out_fee_details.work_name,unit_id,unit_price,sum(amount) as amounts ,
  35. owner_store_out_fee_details.owner_id,owner_fee_detail_id,
  36. sum(owner_fee_details.work_fee) as work_fee,step")
  37. ->whereBetween('owner_store_out_fee_details.created_at', [$start, $end])
  38. ->groupBy('counting_month', 'owner_store_out_fee_details.owner_id', 'owner_store_out_fee_details.work_name', 'unit_id', 'unit_price', 'step')
  39. ->get();
  40. $reports = [];
  41. foreach ($details as $detail) {
  42. $counting_month = Carbon::parse($detail->counting_month)->startOfMonth()->toDateString();
  43. $ownerBillReport = OwnerBillReport::query()
  44. ->selectRaw("id")
  45. ->where('owner_id', $detail->owner_id)
  46. ->where('counting_month', $counting_month)->first();
  47. $reports[] = [
  48. 'owner_bill_report_id' => $ownerBillReport->id ?? null,
  49. 'owner_price_operation_id' => null,//$detail->ownerFeeDetail->ownerPriceOperation->id??null,
  50. 'owner_id' => $detail->owner_id,
  51. 'counting_month' => $counting_month,
  52. 'step' => $detail->step,
  53. 'unit_id' => $detail->unit_id,
  54. 'unit_price' => $detail->unit_price,
  55. 'amount' => $detail->amounts,
  56. 'work_name' => $detail->work_name,
  57. 'fee' => $detail->work_fee,
  58. ];
  59. }
  60. OwnerStoreOutFeeReport::query()->insertOrIgnore($reports);
  61. }
  62. public function getSql($owner_id, $counting_month): Builder
  63. {
  64. // TODO: Implement getSql() method.
  65. }
  66. public function buildExport($details): array
  67. {
  68. // TODO: Implement buildExport() method.
  69. }
  70. public function switchType($type)
  71. {
  72. // TODO: Implement switchType() method.
  73. }
  74. function get(array $kvPairs)
  75. {
  76. $this->archiveService = app('OwnerBillReportArchiveService');
  77. if ($this->archiveService->isArchived($kvPairs['counting_month'], $kvPairs['owner_id'], $kvPairs['type']) == 1) {
  78. //查询存档数据
  79. $archived = $this->archiveService->get($kvPairs);
  80. $reports = collect($archived->information['reports']);
  81. $work_name_fee_total = collect($archived->information['work_name_fee_total']);
  82. $fee_total = $archived->information['fee_total'];
  83. } else {
  84. $reports = OwnerStoreOutFeeReport::query()
  85. ->with('unit')
  86. ->where('owner_id', $kvPairs['owner_id'])
  87. ->where('counting_month', $kvPairs['counting_month'])
  88. ->orderBy('work_name')
  89. ->get();
  90. $work_name_fee_total = DB::table('owner_store_out_fee_reports')
  91. ->selectRaw("work_name,sum(fee) as fee")
  92. ->where('owner_id', $kvPairs['owner_id'])
  93. ->where('counting_month', $kvPairs['counting_month'])
  94. ->groupBy('work_name')
  95. ->get();
  96. $fee_total = OwnerStoreOutFeeReport::query()
  97. ->where('owner_id', $kvPairs['owner_id'])
  98. ->where('counting_month', $kvPairs['counting_month'])
  99. ->sum('fee');
  100. }
  101. return array($reports, $work_name_fee_total, $fee_total);
  102. }
  103. }