OrderListForm.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.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\SaaSLeaguePriceLog;
  17. use app\models\Store;
  18. use app\models\UserStringCodeOrder;
  19. use app\modules\admin\models\ActivityCutPriceForm;
  20. use app\models\Md;
  21. use app\models\Option;
  22. use app\models\Order;
  23. use app\models\OrderDetail;
  24. use app\models\OrderRefund;
  25. use app\models\User;
  26. use app\models\UserCoupon;
  27. use app\models\AccountLog;
  28. use app\modules\client\models\v1\order\OrderForm;
  29. use app\utils\Delivery\Delivery;
  30. use yii\base\BaseObject;
  31. use yii\data\Pagination;
  32. use app\utils\Notice\NoticeAction;
  33. use yii\helpers\Json;
  34. use app\models\DeliveryInfo;
  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()
  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. 'saas_id' =>get_saas_user_id(),
  71. 'user_delete' => 0
  72. ]);
  73. if($this->store_id !=226){
  74. $query->andWhere([
  75. 'or',
  76. ['store_id' => $this->store_id],
  77. ['origin_store_id' => $this->store_id]
  78. ]);
  79. }else{
  80. $query->andWhere([
  81. 'store_id' => $this->store_id,
  82. ]);
  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. if ($order->take_price > 0){
  148. $league_order = SaaSLeaguePriceLog::findOne(['order_id' => $order->id,'type'=>36]);
  149. $league_order_d = SaaSLeaguePriceLog::findOne(['order_id' => $order->id,'type'=>4]);
  150. if (!$league_order && $league_order_d){
  151. $title = '订单取消返还:'.$order->order_no;
  152. SaaSLeaguePriceLog::addLog($order->saas_id,$order->store_id,$order->take_price, UserStringCodeOrder::TYPE_LEAGUE_PRICE[UserStringCodeOrder::TYPE_ORDER_FLOW_CANCEL],$order->id,0,0,$title);
  153. }
  154. }
  155. $log = true;
  156. // 退还积分
  157. $integral = Json::decode($order->integral);
  158. // 减去当前用户账户积分
  159. $log2 = true;
  160. // if ($integral['forehead_integral'] > 0) {
  161. // $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}");
  162. // } elseif ($order->integral_price > 0) {
  163. // $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}");
  164. // }
  165. if (!$orderInfo || !$log || !$log2) { //
  166. $t->rollBack();
  167. } else {
  168. $t->commit();
  169. }
  170. }
  171. $order_detail_list = OrderDetail::findAll(['order_id' => $order->id, 'is_delete' => 0]);
  172. $goods_list = [];
  173. $is_can_confirm = true;
  174. // 获取联动2+1商品条件
  175. $holderLevelConditionDay = null;
  176. $holderLevelOption = \app\utils\Share\BonusPool::getShareHolderLevelOption($this->store_id);
  177. foreach ($order_detail_list as $order_detail) {
  178. $goods = Json::encode($order_detail->goods_info);
  179. if (!$goods) {
  180. continue;
  181. }
  182. foreach ($holderLevelOption as $holderLevelOptionItem) {
  183. if (\in_array($order_detail->goods_id, $holderLevelOptionItem['goods_ids'])) {
  184. $holderLevelConditionDay = $holderLevelOptionItem['day'];
  185. break;
  186. }
  187. }
  188. $goods_model = Goods::findOne($order_detail->goods_id);
  189. $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();
  190. if ($delivery_rules) {
  191. $days = "下单" . $delivery_rules['days'] . "天后";
  192. $delivery_rules['times'] =
  193. (int)$delivery_rules['type'] === 1 ? $days : date("m月d日 H:i:s", $delivery_rules['times']);
  194. $delivery_rules['type'] .= '';
  195. }
  196. //判断是否可点击确认收货
  197. $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]));
  198. if ($is_can_confirm_sub) {
  199. $is_can_confirm = false;
  200. }
  201. $goods_pic = isset($order_detail->pic) ? $order_detail->pic : $goods['cover_pic'];
  202. $goods_list[] = (object)[
  203. 'goods_id' => isset($goods['id']) ? $goods['id'] : $order_detail->goods_id,
  204. 'goods_pic' => $goods_pic,
  205. 'goods_name' => $order_detail->goods_name,
  206. 'num' => $order_detail->num,
  207. 'price' => $order_detail->total_price,
  208. 'attr_list' => json_decode($order_detail->attr),
  209. 'food_ext_goods' => (array)json_decode($order_detail['food_ext_goods'], true),
  210. 'integral_price' => $order_detail->integral_price,
  211. 'delivery_rules' => $delivery_rules ?: null,
  212. 'shop_coin_attr'=>$order_detail->shop_coin_attr,
  213. ];
  214. }
  215. $qrcode = null;
  216. $mch = [
  217. 'id' => 0,
  218. 'name' => '平台自营',
  219. 'logo' => '',
  220. ];
  221. if ($order->md_id) {
  222. $md = Md::findOne($order->md_id);
  223. }
  224. if ($order->mch_id) {
  225. $mch = Mch::findOne($order->mch_id);
  226. }
  227. if($order->order_type == 7){
  228. $mch['name'] = '积分兑换';
  229. }
  230. //新增店铺名称逻辑
  231. $store_info = \Yii::$app->db->createCommand("SELECT * FROM cyy_store WHERE id = :id")
  232. ->bindValue(':id', $order->store_id)
  233. ->queryOne();
  234. if(($order->origin_store_id >0) && ($order->origin_store_id != $order->store_id)){
  235. $mch['name'] =$store_info['name'];
  236. }
  237. $orderRefund = OrderRefund::find()->where(['store_id' => $order->store_id, 'order_id' => $order->id])->exists();
  238. $deliveryInfo = null;
  239. if ($order->is_delivery == 1) {
  240. $deliveryInfo = DeliveryInfo::find()->where(['order_no' => $order->order_no])->one();
  241. }
  242. $is_order_refund = $order_refund_enable = 0;
  243. if ($orderRefund) {
  244. $is_order_refund = 1;
  245. } else {
  246. $is_order_refund = 0;
  247. }
  248. if ($order->is_pay == 1 && ($order->trade_status == Order::ORDER_FLOW_SEND || $order->trade_status == Order::ORDER_FLOW_CONFIRM)) {
  249. $order_refund_enable = 1;
  250. if (intval($order->is_sale)) {
  251. $order_refund_enable = 0;
  252. }
  253. } else {
  254. $order_refund_enable = 0;
  255. }
  256. if ($order->pay_type == 2 && intval($order->is_pay) === 0) {
  257. $is_can_confirm = false;
  258. }
  259. if ($order->trade_status == Order::ORDER_FLOW_CONFIRM && intval($order->is_sale) === 0) {
  260. // if ($holderLevelConditionDay === null) {
  261. // $after_sale_time = Option::get(OptionSetting::STORE_AFTER_SALE_TIME, $this->store_id);
  262. // } else {
  263. // $after_sale_time = $holderLevelConditionDay;
  264. // }
  265. // if ((time() - $order->confirm_time) > $after_sale_time['value'] * 86400) {//超过可售后时间
  266. $order_refund_enable = 1;
  267. // }
  268. }
  269. // 查询物流信息
  270. $model = new ExpressDetail();
  271. $model->express = $order->express;
  272. $model->express_no = $order->express_no;
  273. $model->receive_mobile = $order->mobile;
  274. $model->store_id = $this->store_id;
  275. $res = $model->search();
  276. $express_detail = [];
  277. if ($res['code'] != 0) {
  278. $express_detail['list'] = [];
  279. $express_detail['status'] = 0;
  280. $express_detail['status_text'] = '未知';
  281. } else {
  282. $express_detail = $res['data'];
  283. }
  284. $food_pay_type = [];
  285. $store = Store::findOne($order->store_id);
  286. if (!empty($store)) {
  287. if ($order->food_flag_id > 0) {
  288. if ($order->md_id > 0) {
  289. $md = Md::findOne($order->md_id);
  290. $food_payment = $md->food_payment;
  291. } else {
  292. $store = Store::findOne($order->store_id);
  293. $food_payment = $store->food_payment;
  294. }
  295. $food_pay_list = [];
  296. if (!empty($food_payment)) {
  297. $food_pay_type = Json::decode($food_payment);
  298. foreach ($food_pay_type as &$value) {
  299. if ($value['key'] == 'friend') {
  300. $value['payment'] = 7;
  301. if(!$value['name']){
  302. $value['name'] = "朋友代付";
  303. }
  304. }
  305. if ($value['key'] == 'alipay') {
  306. $value['payment'] = 4;
  307. if(!$value['name']){
  308. $value['name'] = "支付宝支付";
  309. }
  310. }
  311. if ($value['key'] == 'wechat') {
  312. $value['payment'] = 1;
  313. if(!$value['name']){
  314. $value['name'] = "微信支付";
  315. }
  316. }
  317. if ($value['key'] == 'huodao') {
  318. $value['payment'] = 2;
  319. if(!$value['name']){
  320. $value['name'] = "货到付款";
  321. }
  322. }
  323. }
  324. $food_pay_list = $food_pay_type;
  325. }
  326. foreach ($food_pay_list as $k => $val) {
  327. if ($val['payment'] == 4 && is_wechat_platform() || ($val['payment'] == 4 && intval($val['value']) === 0)) {
  328. unset($food_pay_list[$k]);
  329. }
  330. if ($val['payment'] == 1 && is_alipay_platform() || ($val['payment'] == 1 && intval($val['value']) === 0)) {
  331. unset($food_pay_list[$k]);
  332. }
  333. if ($val['payment'] == 7 && intval($val['value']) === 0) {
  334. unset($food_pay_list[$k]);
  335. }
  336. if ($val['payment'] == 2 && intval($val['value']) === 0) {
  337. unset($food_pay_list[$k]);
  338. }
  339. if ($val['payment'] == 5 && intval($val['value']) === 0) {
  340. unset($food_pay_list[$k]);
  341. }
  342. }
  343. $food_pay_list = array_values($food_pay_list);
  344. // 点餐支付方式
  345. $food_pay_type = $food_pay_list;
  346. }
  347. }
  348. $is_get_coin =1;
  349. if(($order->trade_status == 3) && $order->is_get_coin == 0){
  350. //查询商品是不是
  351. foreach ($goods_list as $k1 => $v1) {
  352. $cloud_goods_id = \Yii::$app->db->createCommand("SELECT cloud_goods_id FROM cyy_cloud_goods_bind WHERE goods_id = :goods_id")
  353. ->bindValue(':goods_id', $v1->goods_id) // 改为箭头运算符
  354. ->queryScalar();
  355. $count_crry = \Yii::$app->db->createCommand("SELECT cloud_goods_id FROM cyy_goods_wallet WHERE cloud_goods_id = :cloud_goods_id")
  356. ->bindValue(':cloud_goods_id', $cloud_goods_id)
  357. ->queryScalar();
  358. if ($count_crry) {
  359. if(($order->created_at)>1741968000){
  360. $is_get_coin = 0;
  361. }
  362. }
  363. $shop_coin_attr = json_decode($v1->shop_coin_attr);
  364. if($shop_coin_attr){
  365. $is_get_coin = 0;
  366. }
  367. }
  368. }
  369. $new_list[] = (object)[
  370. 'store_id_first'=>$order->store_id,
  371. 'store_id'=>$order->origin_store_id,
  372. 'is_get_coin'=>$is_get_coin,
  373. 'is_sale' => intval($order->is_sale),
  374. 'is_order_refund' => $is_order_refund,
  375. 'order_refund_enable' => $order_refund_enable,
  376. 'order_id' => $order->id,
  377. 'order_no' => $order->order_no,
  378. 'add_time' => $order->created_at,
  379. 'goods_list' => $goods_list,
  380. 'total_price' => $order->total_price,
  381. 'pay_price' => sprintf("%1.2f", $order->pay_price),
  382. 'is_pay' => $order->is_pay,
  383. 'is_comment' => $order->is_comment,
  384. 'is_offline' => $order->is_offline,
  385. 'qrcode' => $qrcode,
  386. 'offline_qrcode' => $order->offline_qrcode,
  387. 'express' => $order->express,
  388. 'mch' => $mch,
  389. 'type' => $order->type,
  390. 'pay_type' => $order->pay_type,
  391. 'refund' => $orderRefund,
  392. 'apply_delete' => $order->apply_delete,
  393. 'trade_status' => $order->trade_status,
  394. 'delivery_fee' => $deliveryInfo ? $deliveryInfo->fee : 0,
  395. 'is_delivery' => $order->is_delivery,
  396. 'order_type' => $order->order_type,
  397. 'md_name' => $mch->name ?? ($md->name ?? "平台自营"),
  398. 'express_detail' => $express_detail,
  399. 'express_no' => $order->express_no,
  400. 'is_can_confirm' => $is_can_confirm,
  401. 'pay_type_list' => $food_pay_type ?: $pay_type_list
  402. ];
  403. }
  404. return [
  405. 'code' => 0,
  406. 'msg' => 'success',
  407. 'data' => [
  408. 'row_count' => $count,
  409. 'page_count' => $pagination->pageCount,
  410. 'list' => $new_list,
  411. 'pay_type_list' => $pay_type_list,
  412. 'revoke_tpl'=>'',
  413. 'template_id' => NoticeAction::getSendTamplateId(['order_cancel'], is_h5() ? 'wxaapi' : 'miapp'),
  414. 'cancel_reason' => Delivery::$validReason
  415. ],
  416. ];
  417. }
  418. private function getRefundList()
  419. {
  420. $query = OrderRefund::find()->alias('or')
  421. ->leftJoin(['od' => OrderDetail::tableName()], 'od.id=or.order_detail_id')
  422. ->leftJoin(['o' => Order::tableName()], 'o.id=or.order_id')
  423. ->where([
  424. 'or.store_id' => $this->store_id,
  425. 'or.user_id' => $this->user_id,
  426. 'or.is_delete' => 0,
  427. 'o.is_delete' => 0,
  428. 'od.is_delete' => 0,
  429. ]);
  430. if($this->order_id){
  431. $query->andWhere(['o.id' => $this->order_id]);
  432. }
  433. $count = $query->count();
  434. $pagination = new Pagination(['totalCount' => $count, 'page' => $this->page - 1, 'pageSize' => $this->limit]);
  435. $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')
  436. ->limit($pagination->limit)->offset($pagination->offset)->orderBy('or.id DESC')->asArray()->all();
  437. $new_list = [];
  438. foreach ($list as $item) {
  439. $goods = Json::decode($item['goods_info']) ?:Goods::find()->where(['id' => $item['goods_id']])->asArray()->one();
  440. if (!$goods) {
  441. continue;
  442. }
  443. $new_list[] = (object)[
  444. 'order_id' => intval($item['order_id']),
  445. 'order_no' => $item['order_no'],
  446. 'goods_list' => [(object)[
  447. 'goods_id' => intval($goods->id),
  448. 'goods_pic' => $item['pic'] ?: $goods['cover_pic'],
  449. 'goods_name' => $item['goods_name'],
  450. 'num' => intval($item['num']),
  451. 'price' => doubleval(sprintf('%.2f', $item['total_price'])),
  452. 'attr_list' => json_decode($item['attr']),
  453. ]],
  454. 'addtime' => date('Y-m-d H:i', $item['created_at']),
  455. 'refund_price' => doubleval(sprintf('%.2f', $item['refund_price'])),
  456. 'refund_type' => $item['type'],
  457. 'refund_status' => $item['status'],
  458. 'order_refund_id' => $item['order_refund_id'],
  459. 'is_agree' => $item['is_agree'],
  460. 'is_user_send' => $item['is_user_send'],
  461. ];
  462. }
  463. return [
  464. 'code' => 0,
  465. 'msg' => 'success',
  466. 'data' => [
  467. 'row_count' => $count,
  468. 'page_count' => $pagination->pageCount,
  469. 'list' => $new_list,
  470. ],
  471. ];
  472. }
  473. public function getNewRefundList()
  474. {
  475. $after_sale_time = Option::get(OptionSetting::STORE_AFTER_SALE_TIME, $this->store_id);
  476. $new_list = [];
  477. if (!$this->status) {
  478. $query = Order::find()->alias('o')
  479. ->where([
  480. 'o.store_id' => $this->store_id,
  481. 'o.user_id' => $this->user_id,
  482. 'o.is_delete' => 0,
  483. ]);
  484. // if (get_md_id()) {
  485. // $query->andWhere(['o.md_id' => get_md_id()]);
  486. // }
  487. $query->andWhere(['o.trade_status' => Order::ORDER_FLOW_CONFIRM]);
  488. $query->andWhere(['>', 'o.confirm_time', time() - $after_sale_time['value'] * 86400]);
  489. $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();
  490. foreach ($list as $item) {
  491. $detail_goods = OrderDetail::find()->where(['order_id' => $item['order_id'], 'is_refund' => 0])->asArray()->all();
  492. if (empty($detail_goods)) {
  493. continue;
  494. }
  495. $detail_goods = OrderDetail::find()->where(['order_id' => $item['order_id']])->asArray()->all();
  496. $order_data = [
  497. 'order_id' => intval($item['order_id']),
  498. 'order_no' => $item['order_no'],
  499. 'pay_price' => $item['pay_price'],
  500. 'goods_list' => [],
  501. 'addtime' => date('Y-m-d H:i', $item['created_at']),
  502. 'refund_price' => isset($item['refund_price']) ? doubleval(sprintf('%.2f', $item['refund_price'])) : 0,
  503. 'refund_type' => isset($item['type']) ? $item['type'] : 0,
  504. 'refund_status' => isset($item['status']) ? $item['status'] : 0,
  505. 'order_refund_id' => isset($item['order_refund_id']) ? $item['order_refund_id'] : 0,
  506. 'is_agree' => isset($item['is_agree']) ? $item['is_agree'] : 0,
  507. 'is_user_send' => isset($item['is_user_send']) ? $item['is_user_send'] : 0,
  508. ];
  509. $goods_list = [];
  510. foreach ($detail_goods as $detail) {
  511. $goods_list[] = [
  512. 'goods_id' => intval($detail['goods_id']),
  513. 'goods_pic' => $detail['pic'],
  514. 'goods_name' => $detail['goods_name'],
  515. 'num' => intval($detail['num']),
  516. 'price' => doubleval(sprintf('%.2f', $detail['total_price'])),
  517. 'attr_list' => json_decode($detail['attr']),
  518. ];
  519. }
  520. $order_data['goods_list'] = $goods_list;
  521. $new_list[] = $order_data;
  522. }
  523. }
  524. // 处理中 或者 已完成
  525. if ($this->status == 1 || $this->status == 2) {
  526. $query = OrderRefund::find()->alias('or')
  527. ->leftJoin(['o' => Order::tableName()], 'o.id=or.order_id')
  528. ->where([
  529. 'or.store_id' => $this->store_id,
  530. 'or.user_id' => $this->user_id,
  531. 'or.is_delete' => 0,
  532. 'o.is_delete' => 0,
  533. 'or.user_delete' => 0
  534. ]);
  535. // if (get_md_id()) {
  536. // $query->andWhere(['or.md_id' => get_md_id()]);
  537. // }
  538. if ($this->status == 1) {
  539. $query->andWhere(['or.status' => OrderRefund::STATUS_IN, 'or.is_user_cancel' => 0]);
  540. }
  541. if ($this->status == 2) {
  542. $query->andWhere(['or', ['or.is_user_cancel' => 1], ['in', 'or.status', OrderRefund::$is_handle_status]]);
  543. }
  544. $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();
  545. if (!empty($list)) {
  546. foreach ($list as $item) {
  547. $order_data = [
  548. 'is_user_cancel' => intval($item['is_user_cancel']),
  549. 'order_id' => intval($item['order_id']),
  550. 'order_no' => $item['order_no'],
  551. 'pay_price' => $item['pay_price'],
  552. 'goods_list' => [],
  553. 'addtime' => date('Y-m-d H:i', $item['created_at']),
  554. 'refund_price' => isset($item['refund_price']) ? doubleval(sprintf('%.2f', $item['refund_price'])) : 0,
  555. 'refund_type' => isset($item['type']) ? $item['type'] : 0,
  556. 'refund_status' => isset($item['status']) ? $item['status'] : 0,
  557. 'order_refund_id' => isset($item['order_refund_id']) ? $item['order_refund_id'] : 0,
  558. 'is_agree' => isset($item['is_agree']) ? $item['is_agree'] : 0,
  559. 'is_user_send' => isset($item['is_user_send']) ? $item['is_user_send'] : 0,
  560. ];
  561. $detail_id = Json::decode($item['order_detail_id']);
  562. $detail_goods = OrderDetail::find()->where(['order_id' => $item['order_id'], 'is_refund' => 1])->andWhere(['in', 'id', $detail_id])->asArray()->all();
  563. $goods_list = [];
  564. $price = 0;
  565. foreach ($detail_goods as $detail) {
  566. $goods_list[] = [
  567. 'goods_id' => intval($detail['goods_id']),
  568. 'goods_pic' => $detail['pic'],
  569. 'goods_name' => $detail['goods_name'],
  570. 'num' => intval($detail['num']),
  571. 'price' => doubleval(sprintf('%.2f', $detail['total_price'])),
  572. 'attr_list' => json_decode($detail['attr']),
  573. 'is_refund' => $detail['is_refund']
  574. ];
  575. $price += $detail['total_price'];
  576. }
  577. $order_data['pay_price'] = $price;
  578. $order_data['goods_list'] = $goods_list;
  579. $new_list[] = $order_data;
  580. }
  581. }
  582. }
  583. $data = array_slice($new_list, ($this->page - 1) * $this->limit, $this->limit);
  584. return [
  585. 'code' => 0,
  586. 'msg' => 'success',
  587. 'data' => [
  588. 'row_count' => count($new_list),
  589. 'page_count' => ceil(count($new_list) / $this->limit),
  590. 'list' => $data,
  591. ],
  592. ];
  593. }
  594. public static function getCountData($store_id, $user_id)
  595. {
  596. $form = new OrderListForm();
  597. $form->limit = 1;
  598. $form->store_id = $store_id;
  599. $form->user_id = $user_id;
  600. $data = [];
  601. $form->status = -1;
  602. $res = $form->searchOrderCount();
  603. $data['all'] = $res;
  604. $form->status = 0;
  605. $res = $form->searchOrderCount();
  606. $data['status_0'] = $res;
  607. $form->status = 1;
  608. $res = $form->searchOrderCount();
  609. $data['status_1'] = $res;
  610. $form->status = 2;
  611. $res = $form->searchOrderCount();
  612. $data['status_2'] = $res;
  613. $form->status = 3;
  614. $res = $form->searchOrderCount();
  615. $data['status_3'] = $res;
  616. return $data;
  617. }
  618. /**
  619. * 获取订单数方法
  620. * @return array|int|string
  621. */
  622. public function searchOrderCount()
  623. {
  624. $query = Order::find()->where([
  625. 'is_delete' => 0,
  626. 'user_delete' => 0,
  627. 'store_id' => $this->store_id,
  628. 'user_id' => $this->user_id,
  629. ]);
  630. $query->andWhere(['!=', 'order_type', 6]);
  631. // if (get_md_id()) {
  632. // $query->andWhere(['md_id' => get_md_id()]);
  633. // }
  634. if (isset($this->status) && $this->status > -1) {
  635. $query->andWhere([
  636. '<>', 'trade_status', Order::ORDER_FLOW_CANCEL
  637. ]);
  638. }
  639. if ($this->status == 0) {//待付款
  640. $query->andWhere([
  641. 'is_pay' => Order::IS_PAY_FALSE,
  642. ])->andWhere(['!=', 'pay_type' , 2]);
  643. }
  644. if ($this->status == 1) {//待发货
  645. $query->andWhere([
  646. 'trade_status' => Order::ORDER_FLOW_NO_SEND,
  647. ])->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]]);
  648. }
  649. if ($this->status == 2) {//待收货
  650. $query->andWhere([
  651. 'trade_status' => Order::ORDER_FLOW_SEND,
  652. ]);
  653. }
  654. if ($this->status == 3) {//已完成
  655. $query->andWhere([
  656. 'trade_status' => Order::ORDER_FLOW_CONFIRM,
  657. 'is_comment'=> 0,
  658. ]);
  659. }
  660. $query->andWhere(['is_recycle' => 0]);
  661. return $query->count();
  662. }
  663. //获取积分兑换商品列表
  664. public function getIntegralList(){
  665. $query = Order::find()->alias('o')->leftJoin(["d"=>OrderDetail::tablename()],'o.id = d.order_id');
  666. $query->where([
  667. 'o.is_delete' => 0,
  668. 'o.user_delete' => 0,
  669. 'o.store_id' => $this->store_id,
  670. 'o.user_id' => $this->user_id,
  671. 'o.order_type' => 7,
  672. 'o.is_pay' => 1
  673. ]);
  674. $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");
  675. $pagination = pagination_make($query);
  676. $list = $pagination['list'];
  677. foreach($list as &$val){
  678. $val['goods_info'] = json_decode($val['goods_info'],true);
  679. }
  680. return [
  681. 'code' => 0,
  682. 'msg' => 'success',
  683. 'data' => [
  684. 'data' => $list,
  685. 'pageNo' => $pagination['pageNo'],
  686. 'totalCount' => $pagination['totalCount'],
  687. ],
  688. ];
  689. }
  690. public function getHashList(){
  691. $store_id = $this->store_id;
  692. $query = Order::find()->where([
  693. 'is_delete' => 0,
  694. 'user_delete' => 0,
  695. 'saas_id' => get_saas_user_id(),
  696. 'is_pay' => 1
  697. ]);
  698. $query->select(['id','order_no','tx_has',
  699. 'FROM_UNIXTIME(confirm_time) as confirm_time'])->andWhere('tx_has IS NOT NULL')->orderBy('confirm_time DESC');
  700. $pagination = pagination_make($query);
  701. $list = $pagination['list'];
  702. return [
  703. 'code' => 0,
  704. 'msg' => 'success',
  705. 'data' => $list
  706. ];
  707. }
  708. }