OrderDetail.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. namespace common\models\order;
  3. use common\enums\OrderStatusEnum;
  4. use common\enums\RefundStatusEnum;
  5. use common\enums\StatusEnum;
  6. use common\models\goods\Goods;
  7. use common\models\user\User;
  8. use Exception;
  9. use Yii;
  10. /**
  11. * This is the model class for table "{{%order_detail}}".
  12. * @property int $id 订单项ID
  13. * @property int|null $mall_id 商城ID
  14. * @property int|null $order_id 订单ID
  15. * @property int|null $user_id 用户id
  16. * @property int|null $mch_id 多商户ID
  17. * @property int|null $store_id 门店ID
  18. * @property int|null $goods_id 商品ID
  19. * @property string|null $goods_name 商品名称
  20. * @property int|null $goods_attr_id 规格ID
  21. * @property int|null $goods_attr_name 规格ID
  22. * @property string $sign_id 规格ID标识
  23. * @property string|null $attr_groups_format 格式化规格组
  24. * @property float|null $price 商品价格
  25. * @property float|null $cost_price 商品成本价
  26. * @property int|null $num 购买数量
  27. * @property float|null $adjust_money 调整金额
  28. * @property float|null $goods_price 商品优惠后总价
  29. * @property float|null $original_goods_price 商品原本总价
  30. * @property string|null $pic_url 商品图片
  31. * @property int|null $marketing_id 促销ID
  32. * @property string|null $marketing_type 促销类型
  33. * @property int|null $order_type 订单类型
  34. * @property int|null $give_score 赠送积分数量
  35. * @property int|null $order_status 订单状态【0待付款 1待发货 2 已发货 3已收货 4已完成 -1申请售后 -2售后中 -3售后完成 -4 已关闭 -5撤销售后】
  36. * @property int|null $shipping_status 物流状态【1已发货 0待发货】
  37. * @property int|null $is_feedback 退款状态
  38. * @property string|null $remark 备注
  39. * @property int|null $is_evaluate 是否评价 【0未评价 1已评价 2已追评】
  40. * @property float|null $refund_balance_money 订单退款余额
  41. * @property int|null $is_virtual 是否包含 虚拟商品 0 不包含 1 包含
  42. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  43. * @property string|null extra_info 额外信息
  44. * @property int|null $created_at
  45. * @property int|null $updated_at
  46. * @property string|null $order_source 订单来源:JuheShop=>聚合供应链
  47. * @property string|null $shipped_num 已发货数量
  48. * @property array|null $extra_data 额外信息
  49. * @property string $unit 额外信息
  50. * @property int $goods_subtype 商品子类型 (0:普通商品 其他:根据商品的goods_subtype而定)
  51. *
  52. */
  53. class OrderDetail extends \common\models\base\BaseActiveRecord
  54. {
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public static function tableName()
  59. {
  60. return '{{%order_detail}}';
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function rules()
  66. {
  67. return [
  68. [['order_id', 'user_id','mall_id' ,'mch_id', 'store_id','goods_id', 'goods_attr_id', 'marketing_id', 'order_type', 'give_score', 'order_status', 'shipping_status', 'is_feedback', 'is_evaluate', 'is_virtual', 'status', 'created_at', 'updated_at', 'shipped_num', 'goods_subtype'], 'integer'],
  69. [['attr_groups_format','goods_attr_name'], 'string'],
  70. [['price', 'cost_price', 'adjust_money', 'goods_price', 'original_goods_price', 'refund_balance_money'], 'number'],
  71. [['feedback_evidence', 'num', 'extra_data', 'unit'], 'safe'],
  72. [['goods_name', 'remark'], 'string', 'max' => 200],
  73. [['sign_id',], 'string', 'max' => 255],
  74. [['pic_url'], 'string', 'max' => 255],
  75. [['marketing_type','order_source'], 'string', 'max' => 50],
  76. [['extra_info'], 'string'],
  77. ];
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function attributeLabels()
  83. {
  84. return [
  85. 'id' => '订单项ID',
  86. 'mall_id' => '商城ID',
  87. 'order_id' => '订单ID',
  88. 'user_id' => '用户id',
  89. 'mch_id' => '多商户ID',
  90. 'store_id' => '门店id',
  91. 'goods_id' => '商品ID',
  92. 'goods_name' => '商品名称',
  93. 'goods_attr_id' => '规格ID',
  94. 'goods_attr_name' => '规格名称',
  95. 'sign_id' => '规格ID标识',
  96. 'attr_groups_format' => '格式化规格组',
  97. 'price' => '商品价格',
  98. 'cost_price' => '商品成本价',
  99. 'num' => '购买数量',
  100. 'adjust_money' => '调整金额',
  101. 'goods_price' => '商品优惠后总价',
  102. 'original_goods_price' => '商品原本总价',
  103. 'pic_url' => '商品图片',
  104. 'marketing_id' => '促销ID',
  105. 'marketing_type' => '促销类型',
  106. 'order_type' => '订单类型',
  107. 'give_score' => '赠送积分数量',
  108. 'order_status' => '订单状态【0待付款 1待发货 2 已发货 3已收货 4已完成 -1申请售后 -2售后中 -3售后完成 -4 已关闭 -5撤销售后】',
  109. 'shipping_status' => '物流状态【1已发货 0待发货】',
  110. 'is_feedback' => '退款状态',
  111. 'remark' => '备注',
  112. 'is_evaluate' => '是否评价 【0未评价 1已评价 2已追评】',
  113. 'refund_balance_money' => '订单退款余额',
  114. 'is_virtual' => '是否包含 虚拟商品 0 不包含 1 包含',
  115. 'status' => '状态[-1:删除;0:禁用;1启用]',
  116. 'extra_info' => '额外信息',
  117. 'created_at' => 'Created At',
  118. 'updated_at' => 'Updated At',
  119. 'order_source' => '订单来源',
  120. 'shipped_num' => '已发货数量',
  121. 'goods_subtype' => '商品子类型 (0:普通商品 其他:根据商品的goods_subtype而定)',
  122. ];
  123. }
  124. public function getUser(){
  125. return $this->hasOne(User::class, ['id' => 'user_id']);
  126. }
  127. public function getOrder(){
  128. return $this->hasOne(Order::class, ['id' => 'order_id']);
  129. }
  130. public function getGoods(){
  131. return $this->hasOne(Goods::class, ['id' => 'goods_id']);
  132. }
  133. public function getRefundFinish()
  134. {
  135. return $this
  136. ->hasOne(OrderRefund::class, ['order_detail_id' => 'id'])
  137. ->select('id, order_detail_id')
  138. ->where(['step_status' => [RefundStatusEnum::STEP_4, RefundStatusEnum::STEP_6]]);
  139. }
  140. /**
  141. * 保存数据
  142. * @Author bing
  143. * @DateTime 2021-08-24 15:34:48
  144. * @copyright: Copyright (c) 2020 广东七件事集团
  145. * @param array $params
  146. * @return void
  147. */
  148. public static function setData(array $params){
  149. try{
  150. if(!empty($params['id'])){
  151. $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'],'status'=>[StatusEnum::ENABLED]]);
  152. if(empty($model)) throw new Exception('订单详情不存在或已删除');
  153. }else{
  154. $model = new self();
  155. $model->loadDefaultValues();
  156. }
  157. if(!$model->load($params,'') || !$model->save()){
  158. throw new Exception($model->getErrorMessage());
  159. }
  160. return $model;
  161. }catch(Exception $e){
  162. self::$static_error = $e->getMessage();
  163. return false;
  164. }
  165. }
  166. /**
  167. * 订单商品调价
  168. * @Author bing
  169. * @DateTime 2021-09-13 18:19:27
  170. * @copyright: Copyright (c) 2020 广东七件事集团
  171. * @return void
  172. */
  173. public static function priceAdjustment($params){
  174. //查询所有的商品订单
  175. $detail_ids = array_column($params,'id');
  176. $details = self::getOrderDetails([['in','id',$detail_ids]],[],false,'*','id');
  177. $all_goods_price = 0;
  178. $all_adjust_money = 0;
  179. foreach($params as $key => $detail){
  180. if(!isset($details[$detail['id']])) throw new Exception('订单商品不存在或已删除');
  181. $item = $details[$detail['id']];
  182. $item->adjust_money = $detail['adjust_money'];
  183. $all_adjust_money += $detail['adjust_money'];
  184. //$item->goods_price = $item->goods_price + $item->adjust_money;
  185. $item->save();
  186. $all_goods_price += $item->goods_price;
  187. }
  188. $all_goods_price += $all_adjust_money;
  189. return $all_goods_price;
  190. }
  191. /**
  192. * 获取订单商品列表
  193. * @Author bing
  194. * @DateTime 2021-08-26 13:00:11
  195. * @copyright: Copyright (c) 2020 广东七件事集团
  196. * @param array $cond
  197. * @param array $with
  198. * @param integer $is_array
  199. * @return array|null
  200. */
  201. public static function getOrderDetails(array $cond=[],array $with=[],bool $is_array=true,string $select='*',$index=''){
  202. $default_cond = [['<>','status',StatusEnum::DELETE]];
  203. $cond = array_merge($default_cond,$cond);
  204. $query = self::find()->select($select);
  205. self::paramPack(['where'=>$cond,'with'=>$with],$query);
  206. if(!empty($index)) $query->indexBy($index);
  207. $labels = $query->asArray($is_array)->all();
  208. return $labels;
  209. }
  210. /**
  211. * 获取订单售后数量
  212. * @Author bing
  213. * @DateTime 2021-09-17 18:12:25
  214. * @param [type] $order_id
  215. * @return false|int|string
  216. *@copyright: Copyright (c) 2020 广东七件事集团
  217. */
  218. public static function getAfterSaleCountByOrderId($order_id)
  219. {
  220. return self::find()
  221. ->select(['count(id) as count'])
  222. ->where(['status' => StatusEnum::ENABLED])
  223. ->andWhere(['order_id' => $order_id])
  224. ->andWhere([ 'is_feedback'=> OrderStatusEnum::FEEDBACKING])
  225. ->scalar();
  226. }
  227. /**
  228. * 根据订单详情ID获取订单号
  229. * @Author: lun
  230. * @DateTime: 2023/5/27 0027 18:14
  231. * @Copyright: copyright (c) 2021 广东七件事集团
  232. * @param $orderDetailIds
  233. * @return array
  234. */
  235. public static function getOrderNoByDetailIds($orderDetailIds) {
  236. $data = [];
  237. $details = self::find()->select('id,order_id')->where(['id' => $orderDetailIds])->indexBy('id')->asArray()->all();
  238. foreach ($details as $detail) {
  239. $order_ids[] = $detail['order_id'];
  240. }
  241. // 获取订单号
  242. $orders = Order::find()->select('id,order_no')->where(['id' => array_unique($order_ids)])->indexBy(['id'])->asArray()->all();
  243. foreach ($details as $detail) {
  244. $data[$detail['id']] = $orders[$detail['order_id']]['order_no'];
  245. }
  246. return $data;
  247. }
  248. }