OrderListForm.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\models\v1;
  8. use app\constants\OptionSetting;
  9. use app\models\AgentGoodsInstallLog;
  10. use app\models\common\ExpressDetail;
  11. use app\models\DeliveryRules;
  12. use app\models\FoodFlag;
  13. use app\models\Goods;
  14. use app\models\GoodsBook;
  15. use app\models\Mch;
  16. use app\models\OrderGoodsCancel;
  17. use app\models\Store;
  18. use app\modules\admin\models\ActivityCutPriceForm;
  19. use app\models\Md;
  20. use app\models\Option;
  21. use app\models\Order;
  22. use app\models\OrderDetail;
  23. use app\models\OrderRefund;
  24. use app\models\User;
  25. use app\models\UserCoupon;
  26. use app\models\AccountLog;
  27. use app\modules\client\models\v1\order\OrderForm;
  28. use app\utils\Delivery\Delivery;
  29. use yii\base\BaseObject;
  30. use yii\data\Pagination;
  31. use app\utils\Notice\NoticeAction;
  32. use yii\helpers\Json;
  33. use app\models\DeliveryInfo;
  34. use app\modules\client\models\v1\OrderDetailForm;
  35. class OrderListForm extends OrderForm
  36. {
  37. public $store_id;
  38. public $user_id;
  39. public $status;
  40. public $delivery_status;
  41. public $delivery_type;
  42. public $page;
  43. public $limit = 20;
  44. public $order_id;
  45. public $order_no;
  46. public $key;
  47. public $nowlatitude;
  48. public $nowlongitude;
  49. public function rules()
  50. {
  51. return [
  52. [['page', 'limit', 'status', 'delivery_type'], 'integer'],
  53. [['page'], 'default', 'value' => 1],
  54. [['limit'], 'default', 'value' => 20],
  55. [['order_no'], 'default', 'value' => 0],
  56. [['order_id', 'key'],'trim'],
  57. [['nowlongitude', 'nowlatitude'], 'safe'],
  58. ];
  59. }
  60. public function search($arr = [])
  61. {
  62. if (!$this->validate()) {
  63. return [
  64. 'code' => 1,
  65. 'msg' => $this->getErrorSummary(false)[0],
  66. ];
  67. }
  68. $query = Order::find()->where([
  69. 'is_delete' => 0,
  70. 'store_id' => $this->store_id,
  71. 'user_delete' => 0
  72. ]);
  73. if ($arr['is_giving_gifts'] == 1) {
  74. $query->andWhere(['order_type' => Order::ORDER_TYPE_GIVING_GIFTS]);
  75. if(isset($arr['self_received']) && $arr['self_received']){
  76. $query->andWhere(['giving_gifts_received_user_id' => $this->user_id]);
  77. }else{
  78. $query->andWhere(['user_id' => $this->user_id]);
  79. }
  80. }else{
  81. $query->andWhere(['!=', 'order_type', Order::ORDER_TYPE_GIVING_GIFTS]);
  82. $query->andWhere(['user_id' => $this->user_id]);
  83. }
  84. $query->andWhere([
  85. '<>', 'order_type', Order::ORDER_TYPE_WORKER
  86. ]);
  87. if (isset($this->status) && $this->status > -1) {
  88. $query->andWhere([
  89. '<>', 'trade_status', Order::ORDER_FLOW_CANCEL
  90. ]);
  91. }
  92. // if (get_md_id() > 0) {
  93. // $query->andWhere(['OR', ['md_id' => get_md_id()], ['order_type' => [1, 2]]]);
  94. // }
  95. if ($this->status == 0) {//待付款
  96. $query->andWhere([
  97. 'is_pay' => Order::IS_PAY_FALSE,
  98. ])->andWhere(['!=', 'pay_type' , 2])->andWhere(['!=', 'trade_status', 1]);
  99. if($this->order_id){
  100. // 是不是查询指定的带支付订单
  101. $query->andWhere(['id'=>$this->order_id]);
  102. }
  103. }
  104. if ($this->status == 1) {//待发货
  105. $query->andWhere([
  106. 'or',
  107. ['trade_status' => Order::ORDER_FLOW_NO_SEND],
  108. ['pay_type' => 2, 'trade_status' => Order::ORDER_FLOW_DEFAULT]
  109. ])->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]]);
  110. }
  111. if ($this->status == 2) {//待收货
  112. $query->andWhere([
  113. 'trade_status' => Order::ORDER_FLOW_SEND,
  114. ]);
  115. }
  116. if ($this->status == 3) {//已完成
  117. $query->andWhere([
  118. 'trade_status' => Order::ORDER_FLOW_CONFIRM,
  119. ]);
  120. }
  121. if ($this->status == 4) {//售后订单
  122. return $this->getRefundList();
  123. }
  124. $query->andWhere(['is_recycle' => 0]);
  125. $count = $query->count();
  126. $pagination = new Pagination(['totalCount' => $count, 'page' => $this->page - 1, 'pageSize' => $this->limit]);
  127. /* @var Order[] $list */
  128. $list = $query->limit($pagination->limit)->offset($pagination->offset)->orderBy('created_at DESC')->all();
  129. $new_list = [];
  130. $pay_type_list = OrderForm::getPayType([],['huodao']);
  131. foreach ($list as $order) {
  132. // 预约订单超过15分钟未支付自动取消
  133. if (time() - $order->created_at > 15 * 60 && $order->trade_status != Order::ORDER_FLOW_CANCEL && $order->is_pay == 0 && !in_array($order->pay_type, [2, Order::PAY_TYPE_OFFLINE]) && ActivityCutPriceForm::orderCanCancel($order)) {
  134. $t = \Yii::$app->db->beginTransaction();
  135. $order->trade_status = Order::ORDER_FLOW_CANCEL;
  136. $order->updated_at = time();
  137. $orderInfo = $order->save();
  138. if($order->user_coupon_id > 0){
  139. UserCoupon::updateAll(['is_use' => 0], ['id' => $order->user_coupon_id]);
  140. }
  141. //如果是积分商品退回积分
  142. // if($order->integral_price > 0){
  143. // $log = AccountLog::saveLog($order->user_id, $order->integral_price, AccountLog::TYPE_INTEGRAL, AccountLog::LOG_TYPE_INCOME, AccountLog::TYPE_PLATFORM_REFUND_ORDER, $order->id, "商城订单退积分,订单号:{$order->order_no}");
  144. // }else{
  145. // $log = true;
  146. // }
  147. $log = true;
  148. // 退还积分
  149. $integral = Json::decode($order->integral);
  150. // 减去当前用户账户积分
  151. $log2 = true;
  152. // if ($integral['forehead_integral'] > 0) {
  153. // $log2 = AccountLog::saveLog($order->user_id, $integral['forehead_integral'], AccountLog::TYPE_INTEGRAL, AccountLog::LOG_TYPE_INCOME, AccountLog::TYPE_PLATFORM_REFUND_ORDER, $order->id, "商城订单退积分,订单号:{$order->order_no}");
  154. // } elseif ($order->integral_price > 0) {
  155. // $log = AccountLog::saveLog($order->user_id, $order->integral_price, AccountLog::TYPE_INTEGRAL, AccountLog::LOG_TYPE_INCOME, AccountLog::TYPE_PLATFORM_REFUND_ORDER, $order->id, "商城订单退积分,订单号:{$order->order_no}");
  156. // }
  157. if (!$orderInfo || !$log || !$log2) { //
  158. $t->rollBack();
  159. } else {
  160. $t->commit();
  161. }
  162. }
  163. $order_detail_list = OrderDetail::findAll(['order_id' => $order->id, 'is_delete' => 0]);
  164. $goods_list = [];
  165. $is_can_confirm = true;
  166. // 获取联动2+1商品条件
  167. $holderLevelConditionDay = null;
  168. $holderLevelOption = \app\utils\Share\BonusPool::getShareHolderLevelOption($this->store_id);
  169. foreach ($order_detail_list as $order_detail) {
  170. $goods = Json::encode($order_detail->goods_info);
  171. if (!$goods) {
  172. continue;
  173. }
  174. foreach ($holderLevelOption as $holderLevelOptionItem) {
  175. if (\in_array($order_detail->goods_id, $holderLevelOptionItem['goods_ids'])) {
  176. $holderLevelConditionDay = $holderLevelOptionItem['day'];
  177. break;
  178. }
  179. }
  180. $goods_model = Goods::findOne($order_detail->goods_id);
  181. $delivery_rules = DeliveryRules::find()->where(['id' => $goods_model->delivery_rules_id, 'is_delete' => 0, 'status' => 1, 'store_id' => get_store_id()])->select('type, times, days, name')->asArray()->one();
  182. if ($delivery_rules) {
  183. $days = "下单" . $delivery_rules['days'] . "天后";
  184. $delivery_rules['times'] =
  185. (int)$delivery_rules['type'] === 1 ? $days : date("m月d日 H:i:s", $delivery_rules['times']);
  186. $delivery_rules['type'] .= '';
  187. }
  188. //判断是否可点击确认收货
  189. $is_can_confirm_sub = ((bool)AgentGoodsInstallLog::findOne(['order_detail_id' => $order_detail->id, 'status' => [0, 1], 'is_need_install' => 1, 'order_type' => AgentGoodsInstallLog::ORDER_TYPE_NORMAL]));
  190. if ($is_can_confirm_sub) {
  191. $is_can_confirm = false;
  192. }
  193. $goods_pic = isset($order_detail->pic) ? $order_detail->pic : $goods['cover_pic'];
  194. $goods_list[] = (object)[
  195. 'goods_id' => isset($goods['id']) ? $goods['id'] : $order_detail->goods_id,
  196. 'goods_pic' => $goods_pic,
  197. 'goods_name' => $order_detail->goods_name,
  198. 'num' => $order_detail->num,
  199. 'price' => $order_detail->total_price,
  200. 'attr_list' => json_decode($order_detail->attr),
  201. 'food_ext_goods' => (array)json_decode($order_detail['food_ext_goods'], true),
  202. 'integral_price' => $order_detail->integral_price,
  203. 'delivery_rules' => $delivery_rules ?: null
  204. ];
  205. }
  206. $qrcode = null;
  207. $mch = [
  208. 'id' => 0,
  209. 'name' => '平台自营',
  210. 'logo' => '',
  211. ];
  212. if ($order->md_id) {
  213. $md = Md::findOne($order->md_id);
  214. }
  215. if ($order->mch_id) {
  216. $mch = Mch::findOne($order->mch_id);
  217. }
  218. if($order->order_type == 7){
  219. $mch['name'] = '积分兑换';
  220. }
  221. $orderRefund = OrderRefund::find()->where(['store_id' => $order->store_id, 'order_id' => $order->id])->exists();
  222. $deliveryInfo = null;
  223. if ($order->is_delivery == 1) {
  224. $deliveryInfo = DeliveryInfo::find()->where(['order_no' => $order->order_no])->one();
  225. }
  226. $is_order_refund = $order_refund_enable = 0;
  227. if ($orderRefund) {
  228. $is_order_refund = 1;
  229. } else {
  230. $is_order_refund = 0;
  231. }
  232. if ($order->is_pay == 1 && ($order->trade_status == Order::ORDER_FLOW_SEND || $order->trade_status == Order::ORDER_FLOW_CONFIRM)) {
  233. $order_refund_enable = 1;
  234. if (intval($order->is_sale)) {
  235. $order_refund_enable = 0;
  236. }
  237. } else {
  238. $order_refund_enable = 0;
  239. }
  240. if ($order->pay_type == 2 && intval($order->is_pay) === 0) {
  241. $is_can_confirm = false;
  242. }
  243. if ($order->trade_status == Order::ORDER_FLOW_CONFIRM && intval($order->is_sale) === 0) {
  244. // if ($holderLevelConditionDay === null) {
  245. // $after_sale_time = Option::get(OptionSetting::STORE_AFTER_SALE_TIME, $this->store_id);
  246. // } else {
  247. // $after_sale_time = $holderLevelConditionDay;
  248. // }
  249. // if ((time() - $order->confirm_time) > $after_sale_time['value'] * 86400) {//超过可售后时间
  250. $order_refund_enable = 1;
  251. // }
  252. }
  253. // 查询物流信息
  254. $model = new ExpressDetail();
  255. $model->express = $order->express;
  256. $model->express_no = $order->express_no;
  257. $model->receive_mobile = $order->mobile;
  258. $model->store_id = $this->store_id;
  259. $res = $model->search();
  260. $express_detail = [];
  261. if ($res['code'] != 0) {
  262. $express_detail['list'] = [];
  263. $express_detail['status'] = 0;
  264. $express_detail['status_text'] = '未知';
  265. } else {
  266. $express_detail = $res['data'];
  267. }
  268. $food_pay_type = [];
  269. $store = Store::findOne($order->store_id);
  270. if (!empty($store)) {
  271. if ($order->food_flag_id > 0) {
  272. if ($order->md_id > 0) {
  273. $md = Md::findOne($order->md_id);
  274. $food_payment = $md->food_payment;
  275. } else if($order['mch_id'] > 0){
  276. $mchModel = Mch::findOne($order['mch_id']);
  277. $food_payment = $mchModel->food_payment;
  278. } else {
  279. $store = Store::findOne($order->store_id);
  280. $food_payment = $store->food_payment;
  281. }
  282. $food_pay_list = [];
  283. if (!empty($food_payment)) {
  284. $food_pay_type = Json::decode($food_payment);
  285. foreach ($food_pay_type as &$value) {
  286. if ($value['key'] == 'friend') {
  287. $value['payment'] = 7;
  288. if(!$value['name']){
  289. $value['name'] = "朋友代付";
  290. }
  291. }
  292. if ($value['key'] == 'alipay') {
  293. $value['payment'] = 4;
  294. if(!$value['name']){
  295. $value['name'] = "支付宝支付";
  296. }
  297. }
  298. if ($value['key'] == 'wechat') {
  299. $value['payment'] = 1;
  300. if(!$value['name']){
  301. $value['name'] = "微信支付";
  302. }
  303. }
  304. if ($value['key'] == 'huodao') {
  305. $value['payment'] = 2;
  306. if(!$value['name']){
  307. $value['name'] = "货到付款";
  308. }
  309. }
  310. }
  311. $food_pay_list = $food_pay_type;
  312. }
  313. foreach ($food_pay_list as $k => $val) {
  314. if ($val['payment'] == 4 && is_wechat_platform() || ($val['payment'] == 4 && intval($val['value']) === 0)) {
  315. unset($food_pay_list[$k]);
  316. }
  317. if ($val['payment'] == 1 && is_alipay_platform() || ($val['payment'] == 1 && intval($val['value']) === 0)) {
  318. unset($food_pay_list[$k]);
  319. }
  320. if ($val['payment'] == 7 && intval($val['value']) === 0) {
  321. unset($food_pay_list[$k]);
  322. }
  323. if ($val['payment'] == 2 && intval($val['value']) === 0) {
  324. unset($food_pay_list[$k]);
  325. }
  326. if ($val['payment'] == 5 && intval($val['value']) === 0) {
  327. unset($food_pay_list[$k]);
  328. }
  329. }
  330. $food_pay_list = array_values($food_pay_list);
  331. // 点餐支付方式
  332. $food_pay_type = $food_pay_list;
  333. }
  334. }
  335. //获取退款的总商品金额 / 获取最新一条的状态
  336. $cancel_info = [];
  337. $order_cancel = OrderGoodsCancel::find()->where(['order_id' => $order->id])
  338. ->orderBy('id desc')->select('status')->asArray()->one();
  339. if ($order_cancel) {
  340. $cancel_info['refund_price'] = OrderGoodsCancel::find()->where(['order_id' => $order->id])->sum('refund_price');
  341. $cancel_info['status_text'] = OrderGoodsCancel::$status_desc[$order_cancel['status']];
  342. }
  343. $hideBtn = OrderDetailForm::hideBtn($order);
  344. $new_list[] = (object)[
  345. 'hideBtn' => $hideBtn,
  346. 'givingGiftsOrderData' => \app\modules\admin\models\givingGifts\GivingGiftsForm::givingGiftsOrderData($order),
  347. 'is_sale' => intval($order->is_sale),
  348. 'is_order_refund' => $is_order_refund,
  349. 'order_refund_enable' => $order_refund_enable,
  350. 'order_id' => $order->id,
  351. 'order_no' => $order->order_no,
  352. 'add_time' => $order->created_at,
  353. 'goods_list' => $goods_list,
  354. 'total_price' => $order->total_price,
  355. 'pay_price' => sprintf("%1.2f", $order->pay_price),
  356. 'is_pay' => $order->is_pay,
  357. 'is_comment' => $order->is_comment,
  358. 'is_offline' => $order->is_offline,
  359. 'qrcode' => $qrcode,
  360. 'offline_qrcode' => $order->offline_qrcode,
  361. 'express' => $order->express,
  362. 'mch' => $mch,
  363. 'type' => $order->type,
  364. 'pay_type' => $order->pay_type,
  365. 'refund' => $orderRefund,
  366. 'apply_delete' => $order->apply_delete,
  367. 'trade_status' => $order->trade_status,
  368. 'delivery_fee' => $deliveryInfo ? $deliveryInfo->fee : 0,
  369. 'is_delivery' => $order->is_delivery,
  370. 'order_type' => $order->order_type,
  371. 'md_name' => $mch->name ?? ($md->name ?? "平台自营"),
  372. 'express_detail' => $express_detail,
  373. 'express_no' => $order->express_no,
  374. 'is_can_confirm' => $is_can_confirm,
  375. 'pay_type_list' => $food_pay_type ?: $pay_type_list,
  376. 'cancel_info' => $cancel_info ?: null
  377. ];
  378. }
  379. return [
  380. 'code' => 0,
  381. 'msg' => 'success',
  382. 'data' => [
  383. 'q' => $query->createCommand()->getRawSql(),
  384. 'row_count' => $count,
  385. 'page_count' => $pagination->pageCount,
  386. 'list' => $new_list,
  387. 'pay_type_list' => $pay_type_list,
  388. 'revoke_tpl'=>'',
  389. 'template_id' => NoticeAction::getSendTamplateId(['order_cancel'], is_h5() ? 'wxaapi' : 'miapp'),
  390. 'cancel_reason' => Delivery::$validReason
  391. ],
  392. ];
  393. }
  394. private function getRefundList()
  395. {
  396. $query = OrderRefund::find()->alias('or')
  397. ->leftJoin(['od' => OrderDetail::tableName()], 'od.id=or.order_detail_id')
  398. ->leftJoin(['o' => Order::tableName()], 'o.id=or.order_id')
  399. ->where([
  400. 'or.store_id' => $this->store_id,
  401. 'or.user_id' => $this->user_id,
  402. 'or.is_delete' => 0,
  403. 'o.is_delete' => 0,
  404. 'od.is_delete' => 0,
  405. ]);
  406. if($this->order_id){
  407. $query->andWhere(['o.id' => $this->order_id]);
  408. }
  409. $count = $query->count();
  410. $pagination = new Pagination(['totalCount' => $count, 'page' => $this->page - 1, 'pageSize' => $this->limit]);
  411. $list = $query->select('o.id AS order_id,o.order_no,or.id AS order_refund_id,od.goods_id,or.created_at,od.num,od.total_price,od.attr,or.refund_price,or.type,or.status,or.is_agree,or.is_user_send,od.goods_info, od.goods_name, od.pic')
  412. ->limit($pagination->limit)->offset($pagination->offset)->orderBy('or.id DESC')->asArray()->all();
  413. $new_list = [];
  414. foreach ($list as $item) {
  415. $goods = Json::decode($item['goods_info']) ?:Goods::find()->where(['id' => $item['goods_id']])->asArray()->one();
  416. if (!$goods) {
  417. continue;
  418. }
  419. $new_list[] = (object)[
  420. 'order_id' => intval($item['order_id']),
  421. 'order_no' => $item['order_no'],
  422. 'goods_list' => [(object)[
  423. 'goods_id' => intval($goods->id),
  424. 'goods_pic' => $item['pic'] ?: $goods['cover_pic'],
  425. 'goods_name' => $item['goods_name'],
  426. 'num' => intval($item['num']),
  427. 'price' => doubleval(sprintf('%.2f', $item['total_price'])),
  428. 'attr_list' => json_decode($item['attr']),
  429. ]],
  430. 'addtime' => date('Y-m-d H:i', $item['created_at']),
  431. 'refund_price' => doubleval(sprintf('%.2f', $item['refund_price'])),
  432. 'refund_type' => $item['type'],
  433. 'refund_status' => $item['status'],
  434. 'order_refund_id' => $item['order_refund_id'],
  435. 'is_agree' => $item['is_agree'],
  436. 'is_user_send' => $item['is_user_send'],
  437. ];
  438. }
  439. return [
  440. 'code' => 0,
  441. 'msg' => 'success',
  442. 'data' => [
  443. 'row_count' => $count,
  444. 'page_count' => $pagination->pageCount,
  445. 'list' => $new_list,
  446. ],
  447. ];
  448. }
  449. public function getNewRefundList()
  450. {
  451. $after_sale_time = Option::get(OptionSetting::STORE_AFTER_SALE_TIME, $this->store_id);
  452. $new_list = [];
  453. if (!$this->status) {
  454. $query = Order::find()->alias('o')
  455. ->where([
  456. 'o.store_id' => $this->store_id,
  457. 'o.user_id' => $this->user_id,
  458. 'o.is_delete' => 0,
  459. ]);
  460. // if (get_md_id()) {
  461. // $query->andWhere(['o.md_id' => get_md_id()]);
  462. // }
  463. $query->andWhere(['o.trade_status' => Order::ORDER_FLOW_CONFIRM]);
  464. $query->andWhere(['>', 'o.confirm_time', time() - $after_sale_time['value'] * 86400]);
  465. $list = $query->select('o.pay_price, o.confirm_time, o.id AS order_id,o.order_no,o.created_at')->orderBy('o.id DESC')->asArray()->all();
  466. foreach ($list as $item) {
  467. $detail_goods = OrderDetail::find()->where(['order_id' => $item['order_id'], 'is_refund' => 0])->asArray()->all();
  468. if (empty($detail_goods)) {
  469. continue;
  470. }
  471. $detail_goods = OrderDetail::find()->alias('od')
  472. ->leftJoin(['g' => Goods::tableName()], 'od.goods_id=g.id')->where(['od.order_id' => $item['order_id']])->select('od.*, g.product_type')->asArray()->all();
  473. $order_data = [
  474. 'order_id' => intval($item['order_id']),
  475. 'order_no' => $item['order_no'],
  476. 'pay_price' => $item['pay_price'],
  477. 'goods_list' => [],
  478. 'addtime' => date('Y-m-d H:i', $item['created_at']),
  479. 'refund_price' => isset($item['refund_price']) ? doubleval(sprintf('%.2f', $item['refund_price'])) : 0,
  480. 'refund_type' => isset($item['type']) ? $item['type'] : 0,
  481. 'refund_status' => isset($item['status']) ? $item['status'] : 0,
  482. 'order_refund_id' => isset($item['order_refund_id']) ? $item['order_refund_id'] : 0,
  483. 'is_agree' => isset($item['is_agree']) ? $item['is_agree'] : 0,
  484. 'is_user_send' => isset($item['is_user_send']) ? $item['is_user_send'] : 0,
  485. ];
  486. $goods_list = [];
  487. foreach ($detail_goods as $detail) {
  488. $goods_list[] = [
  489. 'goods_id' => intval($detail['goods_id']),
  490. 'goods_pic' => $detail['pic'],
  491. 'goods_name' => $detail['goods_name'],
  492. 'num' => intval($detail['num']),
  493. 'price' => doubleval(sprintf('%.2f', $detail['total_price'])),
  494. 'attr_list' => json_decode($detail['attr']),
  495. 'product_type' => $detail['product_type'],
  496. ];
  497. }
  498. $order_data['goods_list'] = $goods_list;
  499. $new_list[] = $order_data;
  500. }
  501. }
  502. // 处理中 或者 已完成
  503. if ($this->status == 1 || $this->status == 2) {
  504. $query = OrderRefund::find()->alias('or')
  505. ->leftJoin(['o' => Order::tableName()], 'o.id=or.order_id')
  506. ->where([
  507. 'or.store_id' => $this->store_id,
  508. 'or.user_id' => $this->user_id,
  509. 'or.is_delete' => 0,
  510. 'o.is_delete' => 0,
  511. 'or.user_delete' => 0
  512. ]);
  513. // if (get_md_id()) {
  514. // $query->andWhere(['or.md_id' => get_md_id()]);
  515. // }
  516. if ($this->status == 1) {
  517. $query->andWhere(['or.status' => OrderRefund::STATUS_IN, 'or.is_user_cancel' => 0]);
  518. }
  519. if ($this->status == 2) {
  520. $query->andWhere(['or', ['or.is_user_cancel' => 1], ['in', 'or.status', OrderRefund::$is_handle_status]]);
  521. }
  522. $list = $query->select('or.is_user_cancel, or.order_detail_id,o.pay_price, o.confirm_time, o.id AS order_id,o.order_no,or.id AS order_refund_id,or.created_at,or.refund_price,or.type,or.status,or.is_agree,or.is_user_send')->orderBy('or.id DESC')->asArray()->all();
  523. if (!empty($list)) {
  524. foreach ($list as $item) {
  525. $order_data = [
  526. 'is_user_cancel' => intval($item['is_user_cancel']),
  527. 'order_id' => intval($item['order_id']),
  528. 'order_no' => $item['order_no'],
  529. 'pay_price' => $item['pay_price'],
  530. 'goods_list' => [],
  531. 'addtime' => date('Y-m-d H:i', $item['created_at']),
  532. 'refund_price' => isset($item['refund_price']) ? doubleval(sprintf('%.2f', $item['refund_price'])) : 0,
  533. 'refund_type' => isset($item['type']) ? $item['type'] : 0,
  534. 'refund_status' => isset($item['status']) ? $item['status'] : 0,
  535. 'order_refund_id' => isset($item['order_refund_id']) ? $item['order_refund_id'] : 0,
  536. 'is_agree' => isset($item['is_agree']) ? $item['is_agree'] : 0,
  537. 'is_user_send' => isset($item['is_user_send']) ? $item['is_user_send'] : 0,
  538. ];
  539. $detail_id = Json::decode($item['order_detail_id']);
  540. $detail_goods = OrderDetail::find()->where(['order_id' => $item['order_id'], 'is_refund' => 1])->andWhere(['in', 'id', $detail_id])->asArray()->all();
  541. $goods_list = [];
  542. $price = 0;
  543. foreach ($detail_goods as $detail) {
  544. $goods_info = json_decode($detail['goods_info'], true);
  545. $order_data['is_front_centralize'] = intval($goods_info['is_front_centralize']);
  546. $goods_list[] = [
  547. 'goods_id' => intval($detail['goods_id']),
  548. 'goods_pic' => $detail['pic'],
  549. 'goods_name' => $detail['goods_name'],
  550. 'num' => intval($detail['num']),
  551. 'price' => doubleval(sprintf('%.2f', $detail['total_price'])),
  552. 'attr_list' => json_decode($detail['attr']),
  553. 'is_refund' => $detail['is_refund']
  554. ];
  555. $price += $detail['total_price'];
  556. }
  557. $order_data['pay_price'] = $price;
  558. $order_data['goods_list'] = $goods_list;
  559. $new_list[] = $order_data;
  560. }
  561. }
  562. }
  563. $data = array_slice($new_list, ($this->page - 1) * $this->limit, $this->limit);
  564. return [
  565. 'code' => 0,
  566. 'msg' => 'success',
  567. 'data' => [
  568. 'row_count' => count($new_list),
  569. 'page_count' => ceil(count($new_list) / $this->limit),
  570. 'list' => $data,
  571. ],
  572. ];
  573. }
  574. public static function getCountData($store_id, $user_id)
  575. {
  576. $form = new OrderListForm();
  577. $form->limit = 1;
  578. $form->store_id = $store_id;
  579. $form->user_id = $user_id;
  580. $data = [];
  581. $form->status = -1;
  582. $res = $form->searchOrderCount();
  583. $data['all'] = $res;
  584. $form->status = 0;
  585. $res = $form->searchOrderCount();
  586. $data['status_0'] = $res;
  587. $form->status = 1;
  588. $res = $form->searchOrderCount();
  589. $data['status_1'] = $res;
  590. $form->status = 2;
  591. $res = $form->searchOrderCount();
  592. $data['status_2'] = $res;
  593. $form->status = 3;
  594. $res = $form->searchOrderCount();
  595. $data['status_3'] = $res;
  596. return $data;
  597. }
  598. /**
  599. * 获取订单数方法
  600. * @return array|int|string
  601. */
  602. public function searchOrderCount()
  603. {
  604. $query = Order::find()->where([
  605. 'is_delete' => 0,
  606. 'user_delete' => 0,
  607. 'store_id' => $this->store_id,
  608. 'user_id' => $this->user_id,
  609. ]);
  610. $query->andWhere(['!=', 'order_type', Order::ORDER_TYPE_GIVING_GIFTS]);
  611. $query->andWhere(['!=', 'order_type', 6]);
  612. // if (get_md_id()) {
  613. // $query->andWhere(['md_id' => get_md_id()]);
  614. // }
  615. if (isset($this->status) && $this->status > -1) {
  616. $query->andWhere([
  617. '<>', 'trade_status', Order::ORDER_FLOW_CANCEL
  618. ]);
  619. }
  620. if ($this->status == 0) {//待付款
  621. $query->andWhere([
  622. 'is_pay' => Order::IS_PAY_FALSE,
  623. ])->andWhere(['!=', 'pay_type' , 2]);
  624. }
  625. if ($this->status == 1) {//待发货
  626. $query->andWhere([
  627. 'trade_status' => Order::ORDER_FLOW_NO_SEND,
  628. ])->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]]);
  629. }
  630. if ($this->status == 2) {//待收货
  631. $query->andWhere([
  632. 'trade_status' => Order::ORDER_FLOW_SEND,
  633. ]);
  634. }
  635. if ($this->status == 3) {//已完成
  636. $query->andWhere([
  637. 'trade_status' => Order::ORDER_FLOW_CONFIRM,
  638. ]);
  639. }
  640. $query->andWhere(['is_recycle' => 0]);
  641. return $query->count();
  642. }
  643. //获取积分兑换商品列表
  644. public function getIntegralList(){
  645. $query = Order::find()->alias('o')->leftJoin(["d"=>OrderDetail::tablename()],'o.id = d.order_id');
  646. $query->where([
  647. 'o.is_delete' => 0,
  648. 'o.user_delete' => 0,
  649. 'o.store_id' => $this->store_id,
  650. 'o.user_id' => $this->user_id,
  651. 'o.order_type' => 7,
  652. 'o.is_pay' => 1
  653. ]);
  654. $query->select("o.id,o.pay_price,o.order_no,o.integral_price,o.trade_status,d.goods_name,d.goods_id,d.num,d.total_price,d.integral_price,d.pic,d.goods_info");
  655. $pagination = pagination_make($query);
  656. $list = $pagination['list'];
  657. foreach($list as &$val){
  658. $val['goods_info'] = json_decode($val['goods_info'],true);
  659. }
  660. return [
  661. 'code' => 0,
  662. 'msg' => 'success',
  663. 'data' => [
  664. 'data' => $list,
  665. 'pageNo' => $pagination['pageNo'],
  666. 'totalCount' => $pagination['totalCount'],
  667. ],
  668. ];
  669. }
  670. }