89 lines
2.7 KiB
PHP
89 lines
2.7 KiB
PHP
<?php
|
||
|
||
namespace App\Services\Admin;
|
||
|
||
use App\Models\Oldmen;
|
||
use App\Models\ConstFlow;
|
||
use DB;
|
||
use Throwable;
|
||
use Carbon\Carbon;
|
||
use Slowlyo\OwlAdmin\OwlAdmin;
|
||
|
||
/**
|
||
* @method Oldmen getModel()
|
||
* @method Oldmen|\Illuminate\Database\Query\Builder query()
|
||
*/
|
||
class LiveContinueService extends OldmenService
|
||
{
|
||
public function listQuery()
|
||
{
|
||
$filter = $this->getModelFilter();
|
||
|
||
$query = $this->query();
|
||
if($this->withRelationships){
|
||
$query->with($this->withRelationships);
|
||
}
|
||
|
||
if ($filter) {
|
||
$query->filter(request()->input(), $filter)->whereIn('need_pay', [Oldmen::PAY_NOTICE, Oldmen::PAY_ARREAR]);
|
||
}
|
||
|
||
return $query;
|
||
}
|
||
|
||
public function do($oldManId, $startAt, $endAt, $feeArr = null, $changeLv = 0, $lv = 0){
|
||
|
||
$oldMan = $this->getDetail($oldManId);
|
||
if($oldMan){
|
||
//判断状态-是否是已经入住状态;未入住状态无法续住;
|
||
if($oldMan->live_in != Oldmen::STATUS_LIVE){
|
||
return $this->setError('客人状态异常,请刷新后重试!');
|
||
}
|
||
|
||
//判断开始时间和用户截至时间是否相隔1秒;
|
||
if(Carbon::parse($oldMan->avliable_at)->diffInSeconds($startAt) != 1){
|
||
return $this->setError('续住开始时间异常,请刷新重试!');
|
||
}
|
||
|
||
$flow = new ConstFlow();
|
||
$flow->oldman_id = $oldMan->id;
|
||
$flow->const_type = ConstFlow::TYPE_CONTINUE;
|
||
$flow->start_at = $startAt;
|
||
$flow->end_at = $endAt;
|
||
$flow->money = $this->totalFee($feeArr);
|
||
$flow->extends = $feeArr;
|
||
$flow->adminuser_id = OwlAdmin::user()->id;
|
||
|
||
//记录变更护理等级情况;
|
||
if($changeLv && $lv != $oldMan->nurse_lv){
|
||
$newLv = $lv;
|
||
$flow->change_lv = $changeLv;
|
||
$flow->new_lv = $lv;
|
||
$flow->old_lv = $oldMan->nurse_lv;
|
||
}else{
|
||
$newLv = $oldMan->nurse_lv;
|
||
}
|
||
|
||
try{
|
||
DB::beginTransaction();
|
||
//缴费生成流水;
|
||
$flow->save();
|
||
//更新客人信息状态;
|
||
$oldMan->update([
|
||
'need_pay' => Oldmen::PAY_NORMAL,
|
||
'avliable_at' => $endAt,
|
||
'nurse_lv' => $newLv,
|
||
]);
|
||
DB::commit();
|
||
}catch(Throwable $th){
|
||
DB::rollBack();
|
||
report($th);
|
||
return $this->setError('系统错误,请刷新后重试');
|
||
}
|
||
return true;
|
||
}else{
|
||
return $this->setError('未找到客人信息!');
|
||
}
|
||
}
|
||
}
|