You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.3 KiB

<?php
namespace App\Models;
use App\Events\OrderAssigned;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
class Approval extends SoftDeletesModel
{
protected $table = "approval";
const STATUS_HANDLING = 0;
const STATUS_PASSED = 1;
const STATUS_REJECTED = -1;
const STATUS_EXPIRED = -2;
const TEXT_HANDLING = "待处理";
const TEXT_PASSED = "已通过";
const TEXT_REJECTED = "已驳回";
const TEXT_EXPIRED = "已过期";
const TYPE_ASSIGN = "assign";
const TYPE_MODIFY = "modify";
const TYPE_CHECKOUT = "checkout";
const TYPE_CAUSED_BY_CHECKOUT = "caused_by_checkout";
public function primaryOrder() {
return $this->hasOne(Orders::class,"id","order_id");
}
public function approvalItems()
{
return $this->hasMany(ApprovalItems::class);
}
public function generateExpireTime()
{
//todo:change expire rule to be better
return date("Y-m-d") . " 23:59:59";
}
public function pass()
{
DB::beginTransaction();
try {
foreach ($this->approvalItems as $approvalItem) {
$updates = json_decode($approvalItem->updates, true);
$belongs_type = "\\" . $approvalItem->belongs_type;
$model = new $belongs_type();
if ($approvalItem->belongs_id) {
$model->find($approvalItem->belongs_id)->update($updates);
} else {
$model->create($updates);
}
}
if ($this->id) {
$this->update([
"status" => self::STATUS_PASSED
]);
}
if ($this->type == Approval::TYPE_CHECKOUT) {
//处理剩余款项的退款
//todo:实现退款流程
}
if ($this->type == Approval::TYPE_ASSIGN) {
$order = (new Orders())->find($this->order_id);
event(new OrderAssigned($order));
}
DB::commit();
return [
"status" => true
];
} catch (\Exception $exception) {
DB::rollBack();
return [
"status" => false,
"errorcode" => $exception->getCode(),
"errormsg" => $exception->getMessage()
];
}
}
}