StoreOrderDetail.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. namespace addons\Store\common\models;
  3. use addons\Store\common\enums\StoreEnum;
  4. use common\enums\OrderStatusEnum;
  5. use common\enums\StatusEnum;
  6. use common\models\base\BaseActiveRecord;
  7. use common\models\order\Order;
  8. use common\models\user\User;
  9. use Yii;
  10. use Exception;
  11. /**
  12. * This is the model class for table "qimall_addons_store_order_detail".
  13. *
  14. * @property int $id 订单项ID
  15. * @property int $order_id 订单ID
  16. * @property int $main_order_detail_id 主商城订单详情ID
  17. * @property int $user_id 用户id
  18. * @property int $mall_id 商城ID
  19. * @property int $mch_id 多商户id
  20. * @property int $store_id 店铺ID
  21. * @property int $goods_id 商品ID
  22. * @property string $goods_name 商品名称
  23. * @property int $goods_attr_id 规格ID
  24. * @property string $goods_attr_name 规格名称
  25. * @property string $sign_id 规格ID标识
  26. * @property string|null $attr_groups_format 格式化规格组
  27. * @property float $price 商品价格
  28. * @property float $cost_price 商品成本价
  29. * @property float $original_goods_price 商品原本总价
  30. * @property float $goods_price 商品优惠后总价
  31. * @property int $num 购买数量
  32. * @property float $adjust_money 调整金额
  33. * @property string $pic_url 商品图片
  34. * @property int $marketing_id 促销ID
  35. * @property string $marketing_type 促销类型
  36. * @property int $order_type 订单类型
  37. * @property int $give_score 赠送积分数量
  38. * @property int $order_status 订单状态【0待付款 1待发货 2 已发货 3已收货 4已完成 -1申请售后 -2售后中 -3售后完成 -4 已关闭 -5撤销售后】
  39. * @property int $shipping_status 物流状态【1已发货 0待发货】
  40. * @property int $is_feedback 订单售后状态【0未售后 1售后中 2售后完成】
  41. * @property string $remark 备注
  42. * @property int $is_evaluate 是否评价 【0未评价 1已评价 2已追评】
  43. * @property float $refund_balance_money 订单退款余额
  44. * @property int $is_virtual 是否包含 虚拟商品 0 不包含 1 包含
  45. * @property int $status 状态[-1:删除;0:禁用;1启用]
  46. * @property string|null $extra_info 额外信息
  47. * @property int $created_at
  48. * @property int $updated_at
  49. */
  50. class StoreOrderDetail extends BaseActiveRecord
  51. {
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public static function tableName()
  56. {
  57. return '{{%addons_store_order_detail}}';
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function rules()
  63. {
  64. return [
  65. [['order_id', 'main_order_detail_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'], 'integer'],
  66. [['attr_groups_format', 'extra_info'], 'string'],
  67. [['price', 'cost_price', 'original_goods_price', 'goods_price', 'adjust_money', 'refund_balance_money'], 'number'],
  68. [['goods_name', 'remark'], 'string', 'max' => 200],
  69. [['goods_attr_name', 'sign_id', 'pic_url'], 'string', 'max' => 255],
  70. [['marketing_type'], 'string', 'max' => 50],
  71. ['num', 'safe'],
  72. ];
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function attributeLabels()
  78. {
  79. return [
  80. 'id' => '订单项ID',
  81. 'order_id' => '订单ID',
  82. 'main_order_detail_id' => '主商城订单详情ID',
  83. 'user_id' => '用户id',
  84. 'mall_id' => '商城ID',
  85. 'mch_id' => '多商户id',
  86. 'store_id' => '店铺ID',
  87. 'goods_id' => '商品ID',
  88. 'goods_name' => '商品名称',
  89. 'goods_attr_id' => '规格ID',
  90. 'goods_attr_name' => '规格名称',
  91. 'sign_id' => '规格ID标识',
  92. 'attr_groups_format' => '格式化规格组',
  93. 'price' => '商品价格',
  94. 'cost_price' => '商品成本价',
  95. 'original_goods_price' => '商品原本总价',
  96. 'goods_price' => '商品优惠后总价',
  97. 'num' => '购买数量',
  98. 'adjust_money' => '调整金额',
  99. 'pic_url' => '商品图片',
  100. 'marketing_id' => '促销ID',
  101. 'marketing_type' => '促销类型',
  102. 'order_type' => '订单类型',
  103. 'give_score' => '赠送积分数量',
  104. 'order_status' => '订单状态【0待付款 1待发货 2 已发货 3已收货 4已完成 -1申请售后 -2售后中 -3售后完成 -4 已关闭 -5撤销售后】',
  105. 'shipping_status' => '物流状态【1已发货 0待发货】',
  106. 'is_feedback' => '订单售后状态【0未售后 1售后中 2售后完成】',
  107. 'remark' => '备注',
  108. 'is_evaluate' => '是否评价 【0未评价 1已评价 2已追评】',
  109. 'refund_balance_money' => '订单退款余额',
  110. 'is_virtual' => '是否包含 虚拟商品 0 不包含 1 包含',
  111. 'status' => '状态[-1:删除;0:禁用;1启用]',
  112. 'extra_info' => '额外信息',
  113. 'created_at' => 'Created At',
  114. 'updated_at' => 'Updated At',
  115. ];
  116. }
  117. public function getUser(){
  118. return $this->hasOne(User::class, ['id' => 'user_id']);
  119. }
  120. public function getOrder(){
  121. return $this->hasOne(StoreOrder::class, ['id' => 'order_id']);
  122. }
  123. public function getStore()
  124. {
  125. return $this->hasOne(Store::class, ['id' => 'store_id'])->select('id,shop_mobile,store_name,logo_img');
  126. }
  127. /**
  128. * 保存数据
  129. * @Author bing
  130. * @DateTime 2021-08-24 15:34:48
  131. * @copyright: Copyright (c) 2020 广东七件事集团
  132. * @param array $params
  133. * @return void
  134. */
  135. public static function setData(array $params)
  136. {
  137. try {
  138. if (!empty($params['id'])) {
  139. $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'], 'status' => [StatusEnum::ENABLED]]);
  140. if (empty($model)) throw new Exception('订单详情不存在或已删除');
  141. } else {
  142. $model = new self();
  143. $model->loadDefaultValues();
  144. }
  145. if (!$model->load($params, '') || !$model->save()) {
  146. throw new Exception($model->getErrorMessage());
  147. }
  148. return $model;
  149. } catch (Exception $e) {
  150. self::$static_error = $e->getMessage();
  151. return false;
  152. }
  153. }
  154. /**
  155. * @name:
  156. * @msg: 获取订单详情列表
  157. * @param {array} $cond
  158. * @param {array} $with
  159. * @param {bool} $is_array
  160. * @param {string} $select
  161. * @param {*} $index
  162. * @return {*}
  163. */
  164. public static function getOrderDetails(array $cond = [], array $with = [], bool $is_array = true, string $select = '*', $index = '')
  165. {
  166. $default_cond = [['<>', 'status', StatusEnum::DELETE]];
  167. $cond = array_merge($default_cond, $cond);
  168. $query = self::find()->select($select);
  169. self::paramPack(['where' => $cond, 'with' => $with], $query);
  170. if (!empty($index)) $query->indexBy($index);
  171. $labels = $query->asArray($is_array)->all();
  172. return $labels;
  173. }
  174. /**
  175. * @name:
  176. * @msg: 获取订单售后数量
  177. * @param {*} $order_id
  178. * @return {*}
  179. */
  180. public static function getAfterSaleCountByOrderId($order_id)
  181. {
  182. return self::find()
  183. ->select(['count(id) as count'])
  184. ->where(['status' => StatusEnum::ENABLED])
  185. ->andWhere(['order_id' => $order_id])
  186. ->andWhere([ 'is_feedback'=> OrderStatusEnum::FEEDBACKING])
  187. ->scalar();
  188. }
  189. /**
  190. * 获取订单详情api接口内容
  191. * @Author: lun
  192. * @DateTime: 2022/5/11 0011 14:31
  193. * @Copyright: copyright (c) 2021 广东七件事集团
  194. * @param $order
  195. */
  196. public static function getApiOrderDetail($order)
  197. {
  198. // 检测是否是o2o订单
  199. if ($order['order_source'] != StoreEnum::ADDONS_NAME) return [];
  200. // 获取订单内容
  201. $order_select = "id,take_code";
  202. $store_order = StoreOrder::find()->select($order_select)->where(['order_no' => $order['order_no']])->asArray()->one();
  203. // 获取店铺名称
  204. $store_select = "id,store_name,shop_mobile,longitude,latitude,province_id,city_id,district_id,address";
  205. $store = Store::find()->select($store_select)->where(['id' => $order['store_id']])->asArray()->one();
  206. $data = [
  207. 'store_name' => $store['store_name'],
  208. 'address' => $store['address'],
  209. 'longitude' => $store['longitude'],
  210. 'latitude' => $store['latitude'],
  211. 'shop_mobile' => $store['shop_mobile'],
  212. 'take_mobile' => $order['mobile'],
  213. 'take_code' => $store_order['take_code'],
  214. 'take_url' => 'oid='.$order['id'].'&take_code='.$store_order['take_code'],
  215. ];
  216. return ['Store' => $data];
  217. }
  218. //供公共调用
  219. public static function getCapitalLogList($params){
  220. if (!isset($params['list']) || !is_array($params['list'])) return [];
  221. $source_table_id_arr_by_store_order_detail = [];
  222. foreach ($params['list'] as $item) {
  223. if (strpos($item['source_table'],'store_order_detail')!==false) {
  224. $source_table_id_arr_by_store_order_detail[] = $item['source_table_id'];
  225. }
  226. }
  227. if(!empty($source_table_id_arr_by_store_order_detail)){
  228. $order_detail_arr = [];
  229. $cond = [];
  230. $cond['where'] = [['id' => $source_table_id_arr_by_store_order_detail]];
  231. $cond['with'] = ['user' => function ($query) {
  232. $query->select('id,nickname,avatar_url');
  233. }, 'order' => function ($query) {
  234. $query->select('id,order_no');
  235. }];
  236. $order_detail_list = StoreOrderDetail::lists($cond);
  237. if($order_detail_list){
  238. foreach ($order_detail_list as $value) {
  239. $order_detail_arr[$value['id']] = [
  240. 'goods_name' => $value['goods_name'],
  241. 'goods_price' => $value['goods_price'],
  242. 'created_at' => date('Y-m-d H:i:s', $value['created_at']),
  243. 'order_no' => $value['order']['order_no'],
  244. 'user_id' => $value['user']['id'],
  245. 'nickname' => $value['user']['nickname'],
  246. 'avatar_url' => $value['user']['avatar_url'],
  247. ];
  248. }
  249. }
  250. foreach ($params['list'] as &$item) {
  251. if (strpos($item['source_table'],'{{%addons_course_orders}}')!==false) {
  252. $item['user_id'] = '';
  253. $item['nickname'] = '';
  254. $item['avatar_url'] = '';
  255. $item['order_no'] = '';
  256. $item['created_at'] = '';
  257. $item['goods_price'] = '';
  258. $item['goods_name'] = '';
  259. if (!empty($order_detail_arr[$item['source_table_id']])) {
  260. $item['user_id'] = $order_detail_arr[$item['source_table_id']]['user_id'];
  261. $item['nickname'] = $order_detail_arr[$item['source_table_id']]['nickname'];
  262. $item['avatar_url'] = $order_detail_arr[$item['source_table_id']]['avatar_url'];
  263. $item['order_no'] = $order_detail_arr[$item['source_table_id']]['order_no'];
  264. $item['created_at'] = $order_detail_arr[$item['source_table_id']]['created_at'];
  265. $item['goods_price'] = $order_detail_arr[$item['source_table_id']]['goods_price'];
  266. $item['goods_name'] = $order_detail_arr[$item['source_table_id']]['goods_name'];
  267. }
  268. }
  269. }
  270. }
  271. return $params['list'];
  272. }
  273. public static function orderRefundSubmit($params)
  274. {
  275. if (empty($params['mall_id'])) return [];
  276. $mall_id = $params['mall_id'];
  277. if (empty($params['order_detail'])) return [];
  278. $order_detail = $params['order_detail'];
  279. // 订单ID
  280. $order_id = is_array($order_detail) ? $order_detail['order_id'] : $order_detail['order_id'];
  281. $main_order = Order::findOne(['id'=>$order_id]);
  282. if(empty($main_order)) return [];
  283. $store_order = StoreOrder::findOne(['order_no'=>$main_order['order_no']]);
  284. if(empty($store_order)) return [];
  285. $order_credit = StoreOrderCredit::find()
  286. ->where(['mall_id'=>$mall_id,'order_id'=>$store_order['id'],'type'=>1,'is_repayment'=>0])
  287. ->andWhere(['>','pay_time',0])
  288. ->one();
  289. if(empty($order_credit)) return [];
  290. $store_order_detail = StoreOrderDetail::findOne(['main_order_detail_id'=>$order_detail['id']]);
  291. if(empty($store_order_detail)) return [];
  292. $goods_id = $store_order_detail['goods_id'];
  293. $extra = json_decode($order_credit['extra'],true);
  294. if(!empty($extra[$goods_id]['credit_money'])){
  295. $order_detail['goods_price'] = bcsub($order_detail['goods_price'],$extra[$goods_id]['credit_money']);
  296. }
  297. return $order_detail;
  298. }
  299. }