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.

229 lines
7.3 KiB

  1. <?php
  2. namespace backend\modules\shop\models\ars;
  3. use common\models\User;
  4. use Yii;
  5. use yii\behaviors\TimestampBehavior;
  6. use yii\helpers\ArrayHelper;
  7. /**
  8. * This is the model class for table "ats_order".
  9. *
  10. * @property int $id
  11. * @property int $user_id 用户id
  12. * @property string $order_sn 订单号
  13. * @property string $invoice_id 发票单号
  14. * @property int $status 状态
  15. * @property int $type 类型
  16. * @property int $goods_count 商品数量
  17. * @property int $goods_amount 商品金额
  18. * @property int $shipping_amount 物流金额
  19. * @property int $shipping_type 物流类型
  20. * @property string $consignee 收件人
  21. * @property string $phone 手机号码
  22. * @property string $province 省份
  23. * @property string $city 城市
  24. * @property string $area 区域
  25. * @property int $taking_site 自提点
  26. * @property int $pay_type 支付方式
  27. * @property int $pay_at 支付时间
  28. * @property string $payment_sn 付款单号
  29. * @property int $payment_amount 支付金额
  30. * @property int $receivables 应收款
  31. * @property string $remarks 备注
  32. * @property int $discount_amount 折扣金额
  33. * @property string $discount_description 折扣说明
  34. * @property int $updated_at 更新时间
  35. * @property int $created_at 创建时间
  36. * @property string $address 详细地址
  37. */
  38. class Order extends \yii\db\ActiveRecord
  39. {
  40. const TYPE_SHOPPING = 1;//普通购物订单
  41. const SHIPPING_TYPE_VIRTUAL_GOODS = 0;//虚拟货物
  42. const SHIPPING_TYPE_PICKED_UP = 1;//自提
  43. const SHIPPING_TYPE_EXPRESS = 2;//快递物流
  44. const STATUS_UNCONFIRMED = 0;
  45. const STATUS_NONPAYMENT = 1;
  46. const STATUS_CANCEL = 2;
  47. const STATUS_PAYMENT_TO_BE_CONFIRMED = 3;
  48. const STATUS_TO_BE_SHIPPING = 6;
  49. const STATUS_APPLY_REFUND = 4;
  50. const STATUS_REFUND = 5;
  51. const STATUS_SHIPMENT_ALL = 7;
  52. const STATUS_SHIPMENT_PORTION = 8;
  53. const STATUS_FINISH = 9;
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public static function tableName()
  58. {
  59. return 'ats_order';
  60. }
  61. public function fields()
  62. {
  63. $fields = parent::fields();
  64. unset($fields['user_id']);
  65. unset($fields['payment_sn']);
  66. return $fields;
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function rules()
  72. {
  73. return [
  74. [['user_id', 'status', 'type', 'goods_count', 'goods_amount', 'shipping_amount', 'shipping_type', 'taking_site', 'pay_type', 'pay_at', 'payment_amount', 'receivables', 'discount_amount'], 'integer'],
  75. [['discount_description', 'address'], 'string'],
  76. [['order_sn', 'invoice_id'], 'string', 'max' => 64],
  77. [['consignee', 'phone'], 'string', 'max' => 20],
  78. [['province', 'city', 'area'], 'string', 'max' => 10],
  79. [['payment_sn'], 'string', 'max' => 120],
  80. [['remarks'], 'string', 'max' => 255],
  81. ];
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function attributeLabels()
  87. {
  88. return [
  89. 'id' => 'id',
  90. 'user_id' => '用户id',
  91. 'order_sn' => '订单号',
  92. 'invoice_id' => '发票单号',
  93. 'status' => '状态',
  94. 'type' => '类型',
  95. 'goods_count' => '商品数量',
  96. 'goods_amount' => '商品金额',
  97. 'shipping_amount' => '物流金额',
  98. 'shipping_type' => '物流类型',
  99. 'consignee' => '收件人',
  100. 'phone' => '手机号码',
  101. 'province' => '省份',
  102. 'city' => '城市',
  103. 'area' => '区域',
  104. 'taking_site' => '自提点',
  105. 'pay_type' => '支付方式',
  106. 'pay_at' => '支付时间',
  107. 'payment_sn' => '付款单号',
  108. 'payment_amount' => '支付金额',
  109. 'receivables' => '应收款',
  110. 'remarks' => '备注',
  111. 'discount_amount' => '折扣金额',
  112. 'discount_description' => '折扣说明',
  113. 'updated_at' => '更新时间',
  114. 'created_at' => '创建时间',
  115. 'address' => '详细地址',
  116. ];
  117. }
  118. public function beforeSave($insert)
  119. {
  120. $address = Yii::createObject([
  121. 'class' => 'api\logic\AddressLogic',
  122. ]);
  123. if ($insert) {
  124. $default = $address->getDefault();
  125. $this->province = $default['province'] ?? '';
  126. $this->city = $default['city'] ?? '';
  127. $this->area = $default['area'] ?? '';
  128. }
  129. if (!$insert && $this->status == self::STATUS_UNCONFIRMED && $this->type == self::TYPE_SHOPPING) {
  130. $express = Yii::createObject([
  131. 'class' => 'api\logic\ExpressLogic',
  132. ]);
  133. $this->shipping_amount = $express->countShippingAmount($this);
  134. $this->receivables = $this->goods_amount + $this->shipping_amount;
  135. $this->payment_amount = $this->receivables - $this->discount_amount;
  136. }
  137. return parent::beforeSave($insert);
  138. }
  139. /**
  140. * @author linyao
  141. * @email 602604991@qq.com
  142. * @created Nov 8, 2019
  143. *
  144. * 行为存储创建时间和更新时间
  145. */
  146. public function behaviors()
  147. {
  148. return [
  149. [
  150. 'class' => TimestampBehavior::className(),
  151. 'createdAtAttribute' => 'created_at',
  152. 'updatedAtAttribute' => 'updated_at',
  153. 'value' => function () {
  154. return time();
  155. },
  156. ],
  157. ];
  158. }
  159. public function getUser()
  160. {
  161. return $this->hasOne(User::className(), ['id' => 'user_id']);
  162. }
  163. public function getProvinceId()
  164. {
  165. return $this->hasOne(Province::className(), ['province_id' => 'province']);
  166. }
  167. public function getCityId()
  168. {
  169. return $this->hasOne(City::className(), ['city_id' => 'city']);
  170. }
  171. public function getAreaId()
  172. {
  173. return $this->hasOne(Area::className(), ['area_id' => 'area']);
  174. }
  175. /**
  176. * @param $column
  177. * @param null $value
  178. * @return bool
  179. * 获取各状态数组
  180. */
  181. public static function dropDown($column, $value = null)
  182. {
  183. $dropDownList = [
  184. 'shipping_type' => [
  185. self::SHIPPING_TYPE_VIRTUAL_GOODS => '虚拟货物',
  186. self::SHIPPING_TYPE_PICKED_UP => '自提',
  187. self::SHIPPING_TYPE_EXPRESS => '快递物流',
  188. ],
  189. 'type' => [
  190. self::TYPE_SHOPPING => '普通购物订单',
  191. ],
  192. 'status' => [
  193. self::STATUS_UNCONFIRMED => '待确认',
  194. self::STATUS_NONPAYMENT => '待支付',
  195. self::STATUS_CANCEL => '已取消',
  196. self::STATUS_PAYMENT_TO_BE_CONFIRMED => '支付待确认',
  197. self::STATUS_APPLY_REFUND => '申请退款',
  198. self::STATUS_REFUND => '已退款',
  199. self::STATUS_TO_BE_SHIPPING => '待发货',
  200. self::STATUS_SHIPMENT_ALL => '全部发货',
  201. self::STATUS_SHIPMENT_PORTION => '部分发货',
  202. self::STATUS_FINISH => '已完成',
  203. ]
  204. ];
  205. //根据具体值显示对应的值
  206. if ($value !== null)
  207. return array_key_exists($column, $dropDownList) ? $dropDownList[$column][$value] : false;
  208. //返回关联数组,用户下拉的filter实现
  209. else
  210. return array_key_exists($column, $dropDownList) ? $dropDownList[$column] : false;
  211. }
  212. }