PtActivityOrderForm.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models\pt;
  8. use app\constants\OptionSetting;
  9. use app\models\AccountLog;
  10. use app\models\Cat;
  11. use app\models\DeliveryRules;
  12. use app\models\Goods;
  13. use app\models\GoodsCat;
  14. use app\models\IntegralAppreciationUser;
  15. use app\models\IntegralAppreciationUserIntegralLog;
  16. use app\models\Option;
  17. use app\models\Order;
  18. use app\models\PtActivity;
  19. use app\models\PtActivityGoods;
  20. use app\models\PtActivityGoodsCat;
  21. use app\models\PtActivityOrder;
  22. use app\models\PtActivityOrderDetail;
  23. use app\models\SaasUser;
  24. use app\models\SeckillActivity;
  25. use app\models\SeckillActivityGoods;
  26. use app\models\SeckillActivityOrderLog;
  27. use app\models\Store;
  28. use app\models\User;
  29. use app\modules\client\models\v1\pt\order\OrderForm;
  30. use app\utils\OrderNo;
  31. use app\utils\Refund;
  32. use app\utils\Wechat\WechatNewPay;
  33. use app\utils\Wechat\WechatPay;
  34. use yii\base\Model;
  35. class PtActivityOrderForm extends Model
  36. {
  37. public $order_id;
  38. //拼团id
  39. public $pt_number;
  40. //下单用户
  41. public $name;
  42. //拼团状态
  43. public $status;
  44. public $order_no;
  45. public $start_time;
  46. public $end_time;
  47. public $is_platform;
  48. public function rules()
  49. {
  50. return [
  51. [['order_id', 'pt_number', 'status', 'is_platform'], 'integer'],
  52. [['name', 'order_no', 'start_time', 'end_time'], 'string'],
  53. ];
  54. }
  55. public function search ()
  56. {
  57. try {
  58. $query = PtActivityOrder::find()->alias('o')->where(['o.is_pay' => 1, 'o.pt_number' => 0])
  59. ->leftJoin(['od' => PtActivityOrderDetail::tableName()], 'od.order_id = o.id')
  60. ->leftJoin(['a' => PtActivity::tableName()], 'od.activity_id = a.id')
  61. ->leftJoin(['s' => Store::tableName()], 'o.store_id = s.id');
  62. if ($this->pt_number) {
  63. $query->andWhere(['o.id' => $this->pt_number]);
  64. }
  65. if ($this->name) {
  66. $query->andWhere(['LIKE', 'od.goods_name', $this->name]);
  67. }
  68. if ($this->is_platform) {
  69. $query->andWhere(['a.is_platform' => 1]);
  70. } else {
  71. $query->andWhere(['o.store_id' => get_store_id(), 'a.is_platform' => 0]);
  72. }
  73. switch ((int)$this->status) {
  74. case 1:
  75. $query->andWhere(['o.is_pt_finish' => 0]);
  76. break;
  77. case 2:
  78. $query->andWhere(['o.is_pt_finish' => 1]);
  79. break;
  80. case 3:
  81. $query->andWhere(['o.is_pt_finish' => 2]);
  82. break;
  83. }
  84. $query->select('od.goods_name, od.pic, o.pay_time, o.id, o.pt_number, o.is_pt_finish, od.activity_id, o.store_id');
  85. $query->orderBy('o.pay_time desc');
  86. $pagination = pagination_make($query);
  87. foreach ($pagination['list'] as &$list) {
  88. $store = Store::findOne($list['store_id']);
  89. $list['store_name'] = $store->name;
  90. $list['logo'] = $store->logo ?: Option::get(OptionSetting::STORE_LOGO, $list['store_id'], 'store', '');
  91. $list['store_id'] = $store->id;
  92. $pt_activity = PtActivity::findOne($list['activity_id']);
  93. $list['party_size'] = $pt_activity->party_size;
  94. $list['join_num'] = PtActivityOrder::find()->where(['is_pay' => 1])
  95. ->andWhere(['OR', ['pt_number' => $list['id']], ['id' => $list['id']]])->select('id')->count();
  96. $list['error_order_num'] = PtActivityOrder::find()->where(['is_pay' => 1, 'is_winner' => 0, 'is_pay_loser' => 0])
  97. ->andWhere(['<>', 'is_pt_finish', 0])
  98. ->andWhere(['OR', ['pt_number' => $list['id']], ['id' => $list['id']]])->select('id')->count();
  99. $list['status'] = ((int)$list['is_pt_finish'] + 1);
  100. $list['pt_number'] = $list['id'];
  101. $list['pay_time'] = date("Y-m-d H:i:s", $list['pay_time']);
  102. $list['activity_info'] = $pt_activity;
  103. if($pt_activity->party_type == 1){
  104. $list['party_size'] = $pt_activity->party_goods_count;
  105. $pt_number = $list['pt_number'];
  106. $goodsCountQuery = PtActivityOrderDetail::find()->alias('pod')
  107. ->leftJoin(['po' => PtActivityOrder::tableName()], 'pod.order_id = po.id')
  108. ->where(['po.is_pay' => 1, 'pod.is_delete' => 0])
  109. ->andWhere(['OR' , ['po.id' => $pt_number], ['po.pt_number' => $pt_number]]);
  110. $goodsCount = $goodsCountQuery->sum('pod.num');
  111. $list['join_num'] = $goodsCount;
  112. }
  113. }
  114. return [
  115. 'code' => 0,
  116. 'msg' => 'success',
  117. 'data' => [
  118. 'data' => $pagination['list'],
  119. 'pageNo' => $pagination['pageNo'],
  120. 'totalCount' => $pagination['totalCount'],
  121. ]
  122. ];
  123. } catch (\Exception $e) {
  124. return [
  125. 'code' => 1,
  126. 'msg' => $e->getMessage()
  127. ];
  128. }
  129. }
  130. //拼团信息
  131. public function getPtInfo() {
  132. try {
  133. $pt_number = $this->pt_number;
  134. $query = PtActivityOrder::find()->alias('o')->where(['o.is_pay' => 1])
  135. ->andWhere(['OR', ['o.pt_number' => $pt_number], ['o.id' => $pt_number]])
  136. ->leftJoin(['u' => User::tableName()], 'u.id = o.user_id')
  137. ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile = u.binding');
  138. if ($this->order_no) {
  139. $query->andWhere(['LIKE', 'o.order_no', $this->order_no]);
  140. }
  141. if ($this->name) {
  142. $query->andWhere(['LIKE', 'su.name', $this->name]);
  143. }
  144. if ($this->start_time) {
  145. $query->andWhere(['>=', 'o.pay_time', $this->start_time]);
  146. }
  147. if ($this->end_time) {
  148. $query->andWhere(['<=', 'o.pay_time', $this->end_time]);
  149. }
  150. $query->select('o.id, o.order_no, su.name, su.avatar, o.is_pt_finish, o.pay_time, o.is_winner, o.is_pay_loser');
  151. $query->orderBy('o.pay_time desc');
  152. $pagination = pagination_make($query);
  153. foreach ($pagination['list'] as &$list) {
  154. $list['error_order'] = 0;
  155. if ((int)$list['is_pt_finish'] !== 0 && (int)$list['is_winner'] === 0 && (int)$list['is_pay_loser'] !== 1) {
  156. $list['error_order'] = 1;
  157. }
  158. $order_detail = PtActivityOrderDetail::findOne(['order_id' => $list['id']]);
  159. $list['goods_name'] = $order_detail->goods_name;
  160. $list['total_price'] = $order_detail->total_price;
  161. $list['num'] = $order_detail->num;
  162. $list['pic'] = $order_detail->pic;
  163. $list['attr'] = json_decode($order_detail->attr, true);
  164. $list['pay_time'] = date('Y-m-d H:i:s', $list['pay_time']);
  165. $list['status'] = 0;
  166. $list['is_pay_loser'] *= 1;
  167. if ((int)$list['is_pt_finish'] === 0) { //进行中
  168. $list['status'] = 1;
  169. }
  170. if ((int)$list['is_pt_finish'] === 1 && (int)$list['is_winner'] === 1) { //已成功
  171. $list['status'] = 2;
  172. }
  173. if (((int)$list['is_pt_finish'] === 1 && (int)$list['is_winner'] === 0) || (int)$list['is_pt_finish'] === 2) { //已失败
  174. $list['status'] = 3;
  175. }
  176. }
  177. return [
  178. 'code' => 0,
  179. 'msg' => 'success',
  180. 'data' => [
  181. 'data' => $pagination['list'],
  182. 'pageNo' => $pagination['pageNo'],
  183. 'totalCount' => $pagination['totalCount'],
  184. ]
  185. ];
  186. } catch (\Exception $e) {
  187. return [
  188. 'code' => 1,
  189. 'msg' => $e->getMessage()
  190. ];
  191. }
  192. }
  193. //拼团失败成员退款
  194. public function loserHandle($order_id, $head_is_free = 0) {
  195. $t = \Yii::$app->db->beginTransaction();
  196. try {
  197. // $order = PtActivityOrder::findOne($order_id);
  198. // TODO 这块注释掉 说是让所有拼团完成的订单过完售后期后才开始给团长退款 设置了团长免单后这里就不再进行退款了 过完售后才开始传递head_is_free = 1
  199. // $head_is_free = 0;
  200. // $order_detail = PtActivityOrderDetail::find()->where(['order_id' => $order_id])->select('goods_id, activity_id')->asArray()->one();
  201. // if (!empty($order_detail)) {
  202. // $pt_activity_goods = PtActivityGoods::findOne([
  203. // 'activity_id' => $order_detail['activity_id'],
  204. // 'goods_id' => $order_detail['goods_id'],
  205. // 'is_delete' => 0
  206. // ]);
  207. // if ($pt_activity_goods) {
  208. // $pt_activity = PtActivity::findOne(['id' => $pt_activity_goods->activity_id, 'is_delete' => 0]);
  209. // if ($pt_activity) {
  210. // $head_is_free = (int)$pt_activity->head_is_free;
  211. // }
  212. // }
  213. //
  214. // }
  215. $pt_order = PtActivityOrder::find()->where(['trade_status' => 0, 'is_pay' => 1, 'is_winner' => 0, 'is_pay_loser' => 0])
  216. ->andWhere(['OR', ['id' => $order_id], ['pt_number' => $order_id]])
  217. ->andWhere(['<>', 'is_pt_finish', 0])
  218. ->asArray()->all();
  219. if ($head_is_free) {
  220. $pt_order_id = [];
  221. if (!empty($pt_order)) {
  222. $pt_order_id = array_column($pt_order, 'id');
  223. }
  224. //如果团长已成功,则开始免单
  225. if (!in_array($order_id, $pt_order_id)) {
  226. $head_pt_order = PtActivityOrder::find()->where(['id' => $order_id])
  227. ->andWhere(['<>', 'is_pt_finish', 0])->asArray()->one();
  228. $pt_order = array_merge($pt_order, [$head_pt_order]);
  229. }
  230. }
  231. if (!empty($pt_order)) {
  232. foreach ($pt_order as $item) {
  233. $order = PtActivityOrder::findOne($item['id']);
  234. //余额支付
  235. if ((int)$order->pay_type === 3) {
  236. $user = User::findOne(['id' => $item['user_id']]);
  237. if ($user) {
  238. // $user->money = ((float)$user->money + (float)$item['pay_price']);
  239. // $user->save();
  240. $str = "拼团未成功退款,订单号为:{$item['order_no']}。";
  241. if ($order->is_winner) {
  242. $str = "拼团,团长免单退款,订单号为:{$item['order_no']}。";
  243. }
  244. $res = AccountLog::saveLog($user->id, floatval($item['pay_price']), AccountLog::TYPE_BALANCE, AccountLog::LOG_TYPE_INCOME, 3, $item['id'], $str);
  245. if (!$res) {
  246. $t->rollBack();
  247. return [
  248. 'code' => 1,
  249. 'msg' => '支付失败'
  250. ];
  251. }
  252. }
  253. }
  254. //微信支付
  255. if (in_array((int)$order->pay_type, [1, 4])) {
  256. $order_refund_no = 'RP' . substr($order->order_no, 2);
  257. $order->order_refund_no = $order_refund_no;
  258. $res = Refund::refund($order, OrderNo::ORDER_MALL, $order_refund_no, $order->pay_price);
  259. if ($res !== true) {
  260. return $res;
  261. }
  262. }
  263. /*if (!intval($order->is_winner)) {
  264. //增值积分订单取消
  265. $order_id = $order->id;
  266. //增值积分有订单取消
  267. $integralAppreciationUserIntegralLog = IntegralAppreciationUserIntegralLog::findOne([
  268. 'order_id' => $order_id,
  269. 'order_type' => IntegralAppreciationUserIntegralLog::ORDER_TYPE_PT,
  270. 'type' => IntegralAppreciationUserIntegralLog::TYPE_EXPEND,
  271. 'source_type' => IntegralAppreciationUserIntegralLog::SOURCE_TYPE_BUY_GOODS
  272. ]);
  273. if ($integralAppreciationUserIntegralLog) {
  274. $integralAppreciationUser = IntegralAppreciationUser::findOne($integralAppreciationUserIntegralLog->integral_user_id);
  275. $integralAppreciationUser->integral = bcadd($integralAppreciationUser->integral, $integralAppreciationUserIntegralLog->amount, 2);
  276. $integralAppreciationUser->save();
  277. IntegralAppreciationUserIntegralLog::saveIntegralLog(
  278. $integralAppreciationUser->id,
  279. $integralAppreciationUserIntegralLog->amount,
  280. IntegralAppreciationUserIntegralLog::TYPE_INCOME,
  281. IntegralAppreciationUserIntegralLog::SOURCE_TYPE_ORDER_CANCEL,
  282. "订单取消 订单号{$this->order_no}",
  283. $order_id,
  284. IntegralAppreciationUserIntegralLog::ORDER_TYPE_PT);
  285. }
  286. }*/
  287. $order->is_pay_loser = 1;
  288. $order->save();
  289. }
  290. }
  291. $t->commit();
  292. return [
  293. 'code' => 0,
  294. 'msg' => '支付成功'
  295. ];
  296. } catch (\Exception $e) {
  297. $t->rollBack();
  298. return [
  299. 'code' => 1,
  300. 'msg' => $e->getMessage()
  301. ];
  302. }
  303. }
  304. }