| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php
- namespace App\Services;
- use App\Interfaces\SettlementBillDetailInterface;
- use App\OwnerBillReport;
- use App\OwnerBillReportArchive;
- use App\OwnerMaterial;
- use App\Procurement;
- use App\Traits\ServiceAppAop;
- use Illuminate\Database\Eloquent\Builder;
- class OwnerProcurementSettlementBillService implements SettlementBillDetailInterface
- {
- const TYPE = '包材费';
- use ServiceAppAop;
- use \App\Traits\SettlementBillServiceTrait;
- protected $modelClass = Procurement::class;
- /**php
- * @var $archiveService OwnerBillReportArchiveService
- */
- private $archiveService;
- /**
- * OwnerProcurementSettlementBillService constructor.
- */
- public function __construct()
- {
- $this->archiveService = app('OwnerBillReportArchiveService');
- }
- public function getTotalFee($owner_id, $counting_month)
- {
- return $this->getSql($owner_id, $counting_month)->get()->sum('fee');
- }
- /**
- *
- * @param array $kvPairs
- * @return array
- */
- public function get(array $kvPairs): array
- {
- if ($this->archiveService->isArchived($kvPairs['counting_month'], $kvPairs['owner_id'], $kvPairs['type']) == 1) {
- $archived = $this->archiveService->get($kvPairs);
- $details = collect($archived['information']['details']);
- $total_fee = $archived['information']['total_fee'];
- } else {
- $details = $this->getSql($kvPairs['owner_id'], $kvPairs['counting_month'])->get();
- $total_fee = $details->sum('fee');
- }
- return array($details, $total_fee);
- }
- public function getSql($owner_id, $counting_month): Builder
- {
- $ownerMaterialQuery = OwnerMaterial::query()->select('id')->where('owner_id', $owner_id);
- list($start, $end) = $this->getStartAndEnd($counting_month);
- return Procurement::query()
- ->selectRaw("id,owner_material_id,quantity,unit_price,ROUND(unit_price*quantity,2) as fee,created_at")
- ->with('ownerMaterial:id,size,special,specification')
- ->whereIn('owner_material_id', $ownerMaterialQuery)
- ->whereBetween('created_at', [$start, $end]);
- }
- public function switchType($type)
- {
- // TODO: Implement switchType() method.
- }
- public function buildExport($details): array
- {
- $result = [];
- foreach ($details as $detail) {
- $result[] = [
- $detail->created_at,
- $detail->ownerMaterial->special,
- $detail->ownerMaterial->specification,
- $detail->ownerMaterial->size,
- $detail->quantity,
- $detail->unit_price,
- $detail->fee,
- ];
- }
- return $result;
- }
- public function add(array $model)
- {
- // TODO: Implement add() method.
- }
- public function confirmBill($counting_month, $owner_id)
- {
- $billReport = OwnerBillReport::query()
- ->select('storage_fee', 'id')
- ->where('owner_id', $owner_id)
- ->where('counting_month', $counting_month)
- ->firstOr(function () {
- return new OwnerBillReport();
- });
- list($details, $total_fee) = $this->get([
- 'owner_id' => $owner_id,
- 'counting_month' => $counting_month,
- 'type' => $this::TYPE,
- ]);
- OwnerBillReportArchive::query()->create([
- 'owner_bill_report_id' => $billReport->id ?? null,
- 'owner_id' => $owner_id,
- 'counting_month' => $counting_month,
- 'type' => $this::TYPE,
- 'archiver_id' => auth()->id(),
- 'archived_at' => now(),
- 'information' => [
- 'details' => $details,
- 'total_fee' => $total_fee,
- ],
- ]);
- $this->confirmBillFeeTotal($counting_month, $owner_id);
- }
- }
|