OwnerProcurementSettlementBillService.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace App\Services;
  3. use App\Interfaces\SettlementBillDetailInterface;
  4. use App\OwnerBillReport;
  5. use App\OwnerBillReportArchive;
  6. use App\OwnerMaterial;
  7. use App\Procurement;
  8. use App\Traits\ServiceAppAop;
  9. use Illuminate\Database\Eloquent\Builder;
  10. class OwnerProcurementSettlementBillService implements SettlementBillDetailInterface
  11. {
  12. const TYPE = '包材费';
  13. use ServiceAppAop;
  14. use \App\Traits\SettlementBillServiceTrait;
  15. protected $modelClass = Procurement::class;
  16. /**php
  17. * @var $archiveService OwnerBillReportArchiveService
  18. */
  19. private $archiveService;
  20. /**
  21. * OwnerProcurementSettlementBillService constructor.
  22. */
  23. public function __construct()
  24. {
  25. $this->archiveService = app('OwnerBillReportArchiveService');
  26. }
  27. public function getTotalFee($owner_id, $counting_month)
  28. {
  29. return $this->getSql($owner_id, $counting_month)->get()->sum('fee');
  30. }
  31. /**
  32. *
  33. * @param array $kvPairs
  34. * @return array
  35. */
  36. public function get(array $kvPairs): array
  37. {
  38. if ($this->archiveService->isArchived($kvPairs['counting_month'], $kvPairs['owner_id'], $kvPairs['type']) == 1) {
  39. $archived = $this->archiveService->get($kvPairs);
  40. $details = collect($archived['information']['details']);
  41. $total_fee = $archived['information']['total_fee'];
  42. } else {
  43. $details = $this->getSql($kvPairs['owner_id'], $kvPairs['counting_month'])->get();
  44. $total_fee = $details->sum('fee');
  45. }
  46. return array($details, $total_fee);
  47. }
  48. public function getSql($owner_id, $counting_month): Builder
  49. {
  50. $ownerMaterialQuery = OwnerMaterial::query()->select('id')->where('owner_id', $owner_id);
  51. list($start, $end) = $this->getStartAndEnd($counting_month);
  52. return Procurement::query()
  53. ->selectRaw("id,owner_material_id,quantity,unit_price,ROUND(unit_price*quantity,2) as fee,created_at")
  54. ->with('ownerMaterial:id,size,special,specification')
  55. ->whereIn('owner_material_id', $ownerMaterialQuery)
  56. ->whereBetween('created_at', [$start, $end]);
  57. }
  58. public function switchType($type)
  59. {
  60. // TODO: Implement switchType() method.
  61. }
  62. public function buildExport($details): array
  63. {
  64. $result = [];
  65. foreach ($details as $detail) {
  66. $result[] = [
  67. $detail->created_at,
  68. $detail->ownerMaterial->special,
  69. $detail->ownerMaterial->specification,
  70. $detail->ownerMaterial->size,
  71. $detail->quantity,
  72. $detail->unit_price,
  73. $detail->fee,
  74. ];
  75. }
  76. return $result;
  77. }
  78. public function add(array $model)
  79. {
  80. // TODO: Implement add() method.
  81. }
  82. public function confirmBill($counting_month, $owner_id)
  83. {
  84. $billReport = OwnerBillReport::query()
  85. ->select('storage_fee', 'id')
  86. ->where('owner_id', $owner_id)
  87. ->where('counting_month', $counting_month)
  88. ->firstOr(function () {
  89. return new OwnerBillReport();
  90. });
  91. list($details, $total_fee) = $this->get([
  92. 'owner_id' => $owner_id,
  93. 'counting_month' => $counting_month,
  94. 'type' => $this::TYPE,
  95. ]);
  96. OwnerBillReportArchive::query()->create([
  97. 'owner_bill_report_id' => $billReport->id ?? null,
  98. 'owner_id' => $owner_id,
  99. 'counting_month' => $counting_month,
  100. 'type' => $this::TYPE,
  101. 'archiver_id' => auth()->id(),
  102. 'archived_at' => now(),
  103. 'information' => [
  104. 'details' => $details,
  105. 'total_fee' => $total_fee,
  106. ],
  107. ]);
  108. $this->confirmBillFeeTotal($counting_month, $owner_id);
  109. }
  110. }