old-hotel-new/app/Services/Admin/LiveExitService.php

81 lines
2.9 KiB
PHP

<?php
namespace App\Services\Admin;
use App\Models\Oldmen;
use App\Models\ConstFlow;
use DB;
use Slowlyo\OwlAdmin\OwlAdmin;
use Throwable;
/**
* @method Oldmen getModel()
* @method Oldmen|\Illuminate\Database\Query\Builder query()
*/
class LiveExitService extends OldmenService
{
public function do($oldManId, $feeArr = null){
$oldMan = $this->getDetail($oldManId);
if($oldMan){
//判断状态-是否是已经入住状态;未入住状态无法结算;
if($oldMan->live_in != Oldmen::STATUS_LIVE){
return $this->setError('客人状态异常,请刷新后重试!');
}
$flowMoney = 0;
//取消计算历史费用
// $flowList = ConstFlow::where('oldman_id', $oldMan->id)
// ->whereIn('const_type', [ConstFlow::TYPE_IN, ConstFlow::TYPE_CONTINUE])
// ->where('state', 0)
// ->whereBetween('start_at', [$oldMan->live_in_at, $oldMan->avliable_at])//开始时间在用户这段时间内的清单
// ->get();
// foreach($flowList as $flow){
// $flowMoney = bcsub($flowMoney, $flow->money, 2);
// }
$flow = new ConstFlow();
$flow->oldman_id = $oldMan->id;
$flow->const_type = ConstFlow::TYPE_EXIT;
$flow->start_at = $oldMan->live_in_at;//开始入住时间
$flow->end_at = $oldMan->avliable_at;//办理结算时间
$flow->money = bcadd($flowMoney, $this->totalFee($feeArr), 2);
$flow->extends = $feeArr;
$flow->state = 1;
$flow->adminuser_id = OwlAdmin::user()->id;
try{
DB::beginTransaction();
//更新明细清算状态
ConstFlow::where('oldman_id', $oldMan->id)
->whereIn('const_type', [ConstFlow::TYPE_IN, ConstFlow::TYPE_CONTINUE])
->where('state', 0)
->whereBetween('start_at', [$oldMan->live_in_at, $oldMan->avliable_at])//开始时间在用户这段时间内的清单
->update([
'state' => 1
]);
//缴费生成流水;
$flow->save();
//更新客人信息状态;
$oldMan->update([
'live_in' => Oldmen::STATUS_NORMAL,
'need_pay' => Oldmen::PAY_NORMAL,
'live_in_at' => null,
'avliable_at' => null,
'bonds' => [],//保证金归零
]);
DB::commit();
}catch(Throwable $th){
DB::rollBack();
report($th);
return $this->setError('系统错误,请刷新后重试');
}
return true;
}else{
return $this->setError('未找到客人信息!');
}
}
}