admin/app/frontend/models/order/PreOrderServiceFee.php

144 lines
2.9 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2020/8/21
* Time: 9:50
*/
namespace app\frontend\models\order;
use app\common\models\order\OrderServiceFee;
use app\frontend\modules\order\models\PreOrder;
use app\frontend\modules\order\serviceFee\BaseOrderServiceFee;
class PreOrderServiceFee extends OrderServiceFee
{
protected $appends = ['checked', 'show'];
/**
* @var PreOrder
*/
public $order;
/**
* @var BaseOrderServiceFee
*/
protected $serviceFee;
public function init(BaseOrderServiceFee $serviceFee, PreOrder $order)
{
$this->serviceFee = $serviceFee;
$this->setOrder($order);
}
public function setOrder(PreOrder $order)
{
$this->order = $order;
$this->order->orderServiceFees->push($this);
}
public function getServiceFee()
{
return $this->serviceFee;
}
public function getUidAttribute()
{
return $this->order->uid;
}
public function getCodeAttribute()
{
return $this->getServiceFee()->getCode();
}
public function getNameAttribute()
{
return $this->getServiceFee()->getName();
}
/**
* @return mixed
* @throws \app\common\exceptions\AppException
*/
public function getAmountAttribute()
{
return $this->getServiceFee()->getAmount();
}
/**
* 调用当前类不存在方法时 查询 BaseOrderServiceFee 类存在代替处理
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
if (method_exists($this->getServiceFee(), $method)) {
return $this->getServiceFee()->$method($parameters);
}
return parent::__call($method, $parameters); // TODO: Change the autogenerated stub
}
/**
* @return mixed
* @throws \app\common\exceptions\AppException
*/
// public function getAmount()
// {
//
// return $this->getServiceFee()->getAmount();
// }
/**
* @return bool
*/
public function getCheckedAttribute()
{
return $this->getServiceFee()->isChecked();
}
/**
* @return bool
*/
public function getShowAttribute()
{
return $this->getServiceFee()->isShow();
}
/**
* @return array
*/
public function toArray()
{
$this->code = (string)$this->code;
$this->name = (string)$this->name;
$this->amount = sprintf('%.2f', $this->amount);
return parent::toArray();
}
/**
* @return bool
*/
public function beforeSaving()
{
if (!$this->getServiceFee()->isChecked()) {
return false;
}
$this->uid = (string)$this->uid;
$this->code = (string)$this->code;
$this->name = (string)$this->name;
$this->amount = sprintf('%.2f', $this->amount);
return parent::beforeSaving();
}
}