SettlementBillServiceTrait.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace App\Traits;
  3. use App\OwnerBillReport;
  4. use App\OwnerBillReportArchive;
  5. use App\Services\OwnerBillReportArchiveService;
  6. use App\Services\OwnerFeeTotalService;
  7. use Carbon\Carbon;
  8. trait SettlementBillServiceTrait
  9. {
  10. /**
  11. * @param $counting_month
  12. * @return array
  13. */
  14. public function getStartAndEnd($counting_month): array
  15. {
  16. $start = Carbon::parse($counting_month)->startOfMonth()->startOfDay()->toDateTimeString();
  17. $end = Carbon::parse($counting_month)->endOfMonth()->endOfDay()->toDateTimeString();
  18. return array($start, $end);
  19. }
  20. /**
  21. * 确认账单
  22. * @param $counting_month
  23. * @param $owner_id
  24. */
  25. public function confirmBill($counting_month, $owner_id)
  26. {
  27. $billReport = OwnerBillReport::query()
  28. ->select('id')
  29. ->where('owner_id', $owner_id)
  30. ->where('counting_month', $counting_month)
  31. ->firstOr(function () {
  32. return new OwnerBillReport();
  33. });
  34. OwnerBillReportArchive::query()->create([
  35. 'owner_bill_report_id' => $billReport->id ?? null,
  36. 'owner_id' => $owner_id,
  37. 'counting_month' => $counting_month,
  38. 'type' => $this::TYPE,
  39. 'archiver_id' => auth()->id(),
  40. 'archived_at' => now(),
  41. 'information' => [],
  42. ]);
  43. $this->confirmBillFeeTotal($counting_month, $owner_id);
  44. }
  45. /**
  46. * 确认总账单费用
  47. * @param $counting_month
  48. * @param $owner_id
  49. */
  50. public function confirmBillFeeTotal($counting_month, $owner_id): void
  51. {
  52. if ($this::TYPE !== '总费用') {
  53. /**@var $archiveService OwnerBillReportArchiveService */
  54. $archiveService = app('OwnerBillReportArchiveService');
  55. if (count($archiveService->getUnAchieved($counting_month, $owner_id)) == 0) {//全部子项已经确认完成
  56. //确认总费用
  57. /**@var $totalFeeService OwnerFeeTotalService */
  58. $totalFeeService = app('OwnerFeeTotalService');
  59. $totalFeeService->confirmBill($counting_month, $owner_id);
  60. }
  61. }
  62. }
  63. }