Completed
Push — master ( b966de...ad1aa4 )
by zacksleo
05:17 queued 04:09
created

Order::afterDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 2
1
<?php
2
3
namespace zacksleo\yii2\shop\models;
4
5
use Yii;
6
use yii\behaviors\TimestampBehavior;
7
8
/**
9
 * This is the model class for table "{{%order}}".
10
 *
11
 * @property integer $id
12
 * @property integer $user_id
13
 * @property integer $payment_method
14
 * @property string $total_amount
15
 * @property integer $status
16
 * @property string $sn
17
 * @property integer $created_at
18
 * @property integer $updated_at
19
 * @property string $address
20
 * @property string $remark
21
 * @property string $recipient
22
 * @property string $phone
23
 * @property string $express
24
 * @property string $tracking_no
25
 */
26
class Order extends \yii\db\ActiveRecord
27
{
28
    const PAYMENT_METHOD_ALIPAY = 1;
29
    const PAYMENT_METHOD_WECHATPAY = 2;
30
    const PAYMENT_METHOD_QRCODE = 3;
31
32
    const STATUS_RETURN_FAILED = -5; //退货失败
33
    const STATUS_RETURNED = -4;// 已退货
34
    const STATUS_RETURNING = -3; //退货中
35
    const STATUS_DELETED = -2; //已删除
36
    const STATUS_CANCELED = -1; //已取消
37
    const STATUS_UNPAID = 0; //未支付
38
    const STATUS_PAID = 1; //已支付
39
    const STATUS_UNSHIPPED = 2; //未发货
40
    const STATUS_SHIPPED = 3; //已发货
41
    const STATUS_DELIVERED = 4; //已签收
42
    const STATUS_CONSUMED = 5; //已消费
43
44
    /**
45
     * @inheritdoc
46
     */
47 1
    public static function tableName()
48
    {
49 1
        return '{{%order}}';
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55 1
    public function rules()
56
    {
57
        return [
58 1
            [['user_id', 'address', 'recipient', 'phone'], 'required'],
59
            [['user_id', 'payment_method', 'status', 'created_at', 'updated_at'], 'integer'],
60
            [['total_amount'], 'number'],
61
            [['sn', 'address', 'remark', 'express', 'tracking_no'], 'string', 'max' => 255],
62
        ];
63
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68 1
    public function attributeLabels()
69
    {
70
        return [
71 1
            'id' => 'ID',
72
            'user_id' => '用户',
73
            'payment_method' => '支付方式',
74
            'total_amount' => '总金额',
75
            'status' => '状态',
76
            'sn' => '订单编号',
77
            'created_at' => '创建时间',
78
            'updated_at' => '更新时间',
79
            'address' => '配送地址',
80
            'remark' => '备注',
81
            'recipient' => '收件人',
82
            'phone' => '电话',
83
            'express' => '送货公司',
84
            'tracking_no' => '物流编号',
85
        ];
86
    }
87
88
    /**
89
     * @inheritdoc
90
     */
91 2
    public function behaviors()
92
    {
93
        return [
94
            'timestamp' => [
95 2
                'class' => TimestampBehavior::className(),
96
            ],
97
        ];
98
    }
99
100
    public function beforeSave($insert)
101
    {
102
        if ($insert) {
103
            $this->sn = $this->createSN();
104
        }
105
        return parent::beforeSave($insert);
106
    }
107
108
    /**
109
     * 产生序列号
110
     * @param string $prefix 序列号前缀
111
     * @return string
112
     * @link http://goo.gl/TZYwZo 参考说明
113
     */
114
    public function createSN($prefix = '')
115
    {
116
        return $prefix . date('Y') . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
117
    }
118
119 1
    public static function getStatusList()
120
    {
121
        return [
122 1
            self::STATUS_RETURN_FAILED => '退货失败',
123 1
            self::STATUS_RETURNED => '已退货',
124 1
            self::STATUS_RETURNING => '退货中',
125 1
            self::STATUS_DELETED => '已删除',
126 1
            self::STATUS_CANCELED => '已取消',
127 1
            self::STATUS_UNPAID => '未支付',
128 1
            self::STATUS_PAID => '已支付',
129 1
            self::STATUS_UNSHIPPED => '未发货',
130 1
            self::STATUS_SHIPPED => '已发货',
131 1
            self::STATUS_DELIVERED => '已签收',
132 1
            self::STATUS_CONSUMED => '已消费',
133
        ];
134
    }
135
136 1
    public static function getPaymentMethodList()
137
    {
138
        return [
139 1
            self::PAYMENT_METHOD_ALIPAY => '支付宝',
140 1
            self::PAYMENT_METHOD_WECHATPAY => '微信',
141 1
            self::PAYMENT_METHOD_QRCODE => '扫码转账',
142
        ];
143
    }
144
145
    /**
146
     * @return \yii\db\ActiveQuery
147
     */
148
    public function getOrderPaymentRecord()
149
    {
150
        return $this->hasOne(OrderPaymentRecord::className(), ['order_sn' => 'sn']);
151
    }
152
153
    public function afterDelete()
154
    {
155
        parent::afterDelete();
156
        OrderField::deleteAll(['order_id' => $this->id]);
157
        OrderPaymentRecord::deleteAll(['order_sn' => $this->sn]);
158
    }
159
}
160