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.
 
 
 

167 lines
5.1 KiB

<?php
namespace backend\models\ars;
use Yii;
use yii\behaviors\TimestampBehavior;
/**
* This is the model class for table "ats_order".
*
* @property int $id
* @property int $user_id 用户id
* @property string $order_sn 订单号
* @property string $invoice_id 发票单号
* @property int $status 状态
* @property int $type 类型
* @property int $goods_count 商品数量
* @property int $goods_amount 商品金额
* @property int $shipping_amount 物流金额
* @property int $shipping_type 物流类型
* @property string $consignee 收件人
* @property string $phone 手机号码
* @property string $province 省份
* @property string $city 城市
* @property string $area 区域
* @property int $taking_site 自提点
* @property int $pay_type 支付方式
* @property int $pay_at 支付时间
* @property string $payment_sn 付款单号
* @property int $payment_amount 支付金额
* @property int $receivables 应收款
* @property string $remarks 备注
* @property int $discount_amount 折扣金额
* @property string $discount_description 折扣说明
* @property int $updated_at 更新时间
* @property int $created_at 创建时间
* @property string $address 详细地址
*/
class Order extends \yii\db\ActiveRecord
{
const TYPE_SHOPPING = 1;//普通购物订单
const SHIPPING_TYPE_VIRTUAL_GOODS = 0;//虚拟货物
const SHIPPING_TYPE_PICKED_UP = 1;//自提
const SHIPPING_TYPE_EXPRESS = 2;//快递物流
const STATUS_UNCONFIRMED = 0;
const STATUS_NONPAYMENT = 1;
const STATUS_CANCEL = 2;
const STATUS_PAYMENT_TO_BE_CONFIRMED = 3;
const STATUS_TO_BE_SHIPPING = 6;
const STATUS_APPLY_REFUND = 4;
const STATUS_REFUND = 5;
const STATUS_SHIPMENT_ALL = 7;
const STATUS_SHIPMENT_PORTION = 8;
const STATUS_FINISH = 9;
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'ats_order';
}
public function fields()
{
$fields = parent::fields();
unset($fields['user_id']);
unset($fields['payment_sn']);
return $fields;
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['user_id'], 'required'],
[['user_id', 'status', 'type', 'goods_count', 'goods_amount', 'shipping_amount', 'shipping_type', 'taking_site', 'pay_type', 'pay_at', 'payment_amount', 'receivables', 'discount_amount'], 'integer'],
[['discount_description', 'address'], 'string'],
[['order_sn', 'invoice_id'], 'string', 'max' => 64],
[['consignee', 'phone'], 'string', 'max' => 20],
[['province', 'city', 'area'], 'string', 'max' => 10],
[['payment_sn'], 'string', 'max' => 120],
[['remarks'], 'string', 'max' => 255],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'id',
'user_id' => '用户id',
'order_sn' => '订单号',
'invoice_id' => '发票单号',
'status' => '状态',
'type' => '类型',
'goods_count' => '商品数量',
'goods_amount' => '商品金额',
'shipping_amount' => '物流金额',
'shipping_type' => '物流类型',
'consignee' => '收件人',
'phone' => '手机号码',
'province' => '省份',
'city' => '城市',
'area' => '区域',
'taking_site' => '自提点',
'pay_type' => '支付方式',
'pay_at' => '支付时间',
'payment_sn' => '付款单号',
'payment_amount' => '支付金额',
'receivables' => '应收款',
'remarks' => '备注',
'discount_amount' => '折扣金额',
'discount_description' => '折扣说明',
'updated_at' => '更新时间',
'created_at' => '创建时间',
'address' => '详细地址',
];
}
public function beforeSave($insert)
{
if ($this->status === self::STATUS_UNCONFIRMED && $this->type == self::TYPE_SHOPPING) {
$this->shipping_amount = $this->countShippingAmount();
$this->receivables = $this->goods_amount + $this->shipping_amount;
$this->payment_amount = $this->receivables - $this->discount_amount;
}
return parent::beforeSave($insert);
}
private function countShippingAmount()
{
//TODO 根据运费模板计算运费
$amount = 0;
if ($this->shipping_type !== Order::SHIPPING_TYPE_EXPRESS) {
return 0;
}
return $amount;
}
/**
* @author linyao
* @email 602604991@qq.com
* @created Nov 8, 2019
*
* 行为存储创建时间和更新时间
*/
public function behaviors()
{
return [
[
'class' => TimestampBehavior::className(),
'createdAtAttribute' => 'created_at',
'updatedAtAttribute' => 'updated_at',
'value' => function () {
return time();
},
],
];
}
}