ReOrderForm.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\models\ReOrder;
  9. use app\models\ReOrderRefundMoney;
  10. use yii\base\Model;
  11. use app\models\User;
  12. use app\models\AccountLog;
  13. use app\utils\OrderNo;
  14. use app\utils\Refund;
  15. use app\utils\Notice\NoticeSend;
  16. use app\models\Option;
  17. use app\constants\OptionSetting;
  18. class ReOrderForm extends Model
  19. {
  20. public $export;
  21. public $user_id;
  22. public $order_id;
  23. public $store_id;
  24. public $name;
  25. public $phone;
  26. public $status;
  27. public $audit_status;
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function rules()
  32. {
  33. return [
  34. [['store_id'], 'integer'],
  35. [['name', 'phone', 'status', 'audit_status', 'user_id', 'order_id'], 'safe'],
  36. [['export'], 'safe'],
  37. ];
  38. }
  39. public function search() {
  40. $query = ReOrder::find()->alias('ro')->where([
  41. 'ro.is_pay' => 1,
  42. 'ro.is_delete' => 0,
  43. ])->leftJoin(['u' => User::tableName()], 'ro.user_id = u.id')->orderBy('ro.id DESC');
  44. if($this->store_id > 0){
  45. $query->andWhere(['ro.store_id' => $this->store_id]);
  46. }
  47. if($this->user_id > 0){
  48. $query->andWhere(['ro.user_id' => $this->user_id]);
  49. }
  50. if($this->name){
  51. $query->andWhere(['like', 'u.nickname', $this->name]);
  52. }
  53. if($this->phone){
  54. $query->andWhere(['like', 'u.binding', $this->phone]);
  55. }
  56. $query->select('ro.*, u.avatar_url, u.binding, u.nickname');
  57. $data = pagination_make($query);
  58. $data['data'] = $data['list'];
  59. unset($data['list']);
  60. foreach ($data['data'] as &$item) {
  61. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  62. }
  63. if($this->export){
  64. return $this->export($data['data']);
  65. }
  66. return [
  67. 'code' => 0,
  68. 'data' => $data,
  69. 'sql' => $query->createCommand()->getRawSql(),
  70. ];
  71. }
  72. private function export($list) {
  73. $rows = [[
  74. 'ID',
  75. '会员信息',
  76. '支付金额(元)',
  77. '赠送金额(元)',
  78. '订单号',
  79. '时间',
  80. ]];
  81. foreach($list as $item){
  82. $r = [
  83. $item['id'],
  84. $item['nickname'],
  85. $item['pay_price'],
  86. $item['send_price'],
  87. $item['order_no'],
  88. $item['created_at'],
  89. ];
  90. $rows[] = $r;
  91. }
  92. $writer = \Spatie\SimpleExcel\SimpleExcelWriter::streamDownload(time() . '.xlsx')->noHeaderRow()
  93. ->addRows($rows)->toBrowser();
  94. }
  95. private function getReOrderRefundNo()
  96. {
  97. $order_refund_no = null;
  98. while (true) {
  99. $order_refund_no = 'RM'.date('YmdHis') . mt_rand(100000, 999999);
  100. $exist_order_refund_no = ReOrderRefundMoney::find()->where(['order_refund_no' => $order_refund_no])->exists();
  101. if (!$exist_order_refund_no) {
  102. break;
  103. }
  104. }
  105. return $order_refund_no;
  106. }
  107. //充值订单退款列表
  108. public function refundMoneyList() {
  109. $query = ReOrderRefundMoney::find()->alias('rm')->where([
  110. 'rm.is_delete' => 0,
  111. ]);
  112. $query->leftJoin(['ro' => ReOrder::tableName()], 'ro.id = rm.order_id');
  113. $query->leftJoin(['u' => User::tableName()], 'rm.user_id = u.id')->orderBy('rm.id DESC');
  114. if($this->user_id > 0){
  115. $query->andWhere(['rm.user_id' => $this->user_id]);
  116. }
  117. if($this->order_id > 0){
  118. $query->andWhere(['rm.order_id' => $this->order_id]);
  119. }
  120. if($this->store_id > 0){
  121. $query->andWhere(['rm.store_id' => $this->store_id]);
  122. }
  123. if($this->name){
  124. $query->andWhere(['like', 'u.nickname', $this->name]);
  125. }
  126. if($this->phone){
  127. $query->andWhere(['like', 'u.binding', $this->phone]);
  128. }
  129. if($this->status > -1){
  130. $query->andWhere(['status' => $this->status]);
  131. }
  132. if($this->audit_status > -1){
  133. $query->andWhere(['audit_status' => $this->audit_status]);
  134. }
  135. $query->select('rm.*, ro.pay_price, ro.send_price, ro.send_integral, ro.pay_time, ro.order_no, u.avatar_url, u.binding, u.nickname');
  136. $data = pagination_make($query);
  137. $data['data'] = $data['list'];
  138. unset($data['list']);
  139. return [
  140. 'code' => 0,
  141. 'data' => $data,
  142. // 'sql' => $query->createCommand()->getRawSql(),
  143. ];
  144. }
  145. //充值订单申请退款
  146. public function refundMoneyApply($user_id = 0, $order_id = 0, $desc_user = '') {
  147. try{
  148. $key = 'refundMoneyApply_' . $order_id;
  149. $cache = cache()->get($key);
  150. if($cache){
  151. throw new \Exception('操作失败,操作太频繁,请稍后重试。');
  152. }
  153. cache()->set($key, 1, 30);
  154. $where = [
  155. 'user_id' => $user_id,
  156. 'order_id' => $order_id,
  157. 'is_delete' => 0,
  158. 'store_id' => $this->store_id,
  159. ];
  160. $has = ReOrderRefundMoney::findOne($where);
  161. if ($has) {
  162. throw new \Exception('操作失败,该订单已存在申请退款记录。');
  163. }
  164. $order = ReOrder::findOne($order_id);
  165. if (!$order) {
  166. throw new \Exception('操作失败,订单不存在。');
  167. }
  168. if($order->is_pay != 1){
  169. throw new \Exception('操作失败,订单还未支付。');
  170. }
  171. $time = time();
  172. $after_sale_time = Option::get(OptionSetting::STORE_AFTER_SALE_TIME, $this->store_id, 0)['value'];
  173. $sale_time = $time - ($after_sale_time * 86400);
  174. if($order->pay_time < $sale_time){
  175. throw new \Exception('操作失败,订单已过售后期。');
  176. }
  177. $refund = new ReOrderRefundMoney();
  178. $refund->store_id = $order->store_id;
  179. $refund->user_id = $order->user_id;
  180. $refund->order_id = $order->id;
  181. $refund->refund_price = $order->pay_price;
  182. $refund->sub_balance = $order->pay_price + $order->send_price;
  183. $refund->sub_integral = intval($order->send_integral);
  184. $refund->order_refund_no = $this->getReOrderRefundNo();
  185. $refund->desc_user = $desc_user;
  186. if (!$refund->save()) {
  187. throw new \Exception('操作失败,' . array_shift($refund->getFirstErrors()));
  188. }
  189. return [
  190. 'code' => 0,
  191. 'msg' => '操作成功,请等待审核'
  192. ];
  193. } catch (\Exception $ex) {
  194. return [
  195. 'code' => 1,
  196. 'msg' => $ex->getMessage(),
  197. ];
  198. }
  199. }
  200. //充值订单退款审核
  201. public function refundMoneyAudit($audit_status = 0, $refund_id = 0, $desc_admin = '', $refund_price = 0.01, $sub_balance = 0.01, $sub_integral = 0) {
  202. $t = \Yii::$app->db->beginTransaction();
  203. try{
  204. $refund = ReOrderRefundMoney::findOne($refund_id);
  205. if($refund->audit_status != 0){
  206. throw new \Exception('操作失败,记录状态错误。');
  207. }
  208. $refund->desc_admin = $desc_admin;
  209. if($audit_status == ReOrderRefundMoney::AUDIT_STATUS_REJECT){
  210. $refund->audit_status = ReOrderRefundMoney::AUDIT_STATUS_REJECT;
  211. if (!$refund->save()) {
  212. throw new \Exception('操作失败,审核错误。' . array_shift($refund->getFirstErrors()));
  213. }
  214. $t->commit();
  215. return [
  216. 'code' => 0,
  217. 'msg' => '拒绝成功',
  218. ];
  219. }
  220. $refund->audit_status = ReOrderRefundMoney::AUDIT_STATUS_OK;
  221. if($refund_price <= 0){
  222. throw new \Exception('操作失败,退款金额不能为0。');
  223. }
  224. if($sub_balance <= 0){
  225. throw new \Exception('操作失败,扣减余额不能为0。');
  226. }
  227. $where = [
  228. 'id' => $refund->order_id,
  229. 'is_delete' => 0,
  230. 'store_id' => $this->store_id,
  231. ];
  232. $order = ReOrder::findOne($where);
  233. if($refund_price > $order->pay_price){
  234. throw new \Exception('操作失败,退款金额超出支付金额。');
  235. }
  236. //扣余额
  237. $user = User::findOne(['id' => $order->user_id]);
  238. if($sub_balance > $user->money){
  239. throw new \Exception('操作失败,用户余额不足。');
  240. }
  241. if($sub_integral > $user->integral){
  242. throw new \Exception('操作失败,用户积分不足。');
  243. }
  244. if($sub_balance > 0){
  245. $type = AccountLog::TYPE_BALANCE;
  246. $log_type = AccountLog::LOG_TYPE_EXPEND;
  247. $order_type = AccountLog::TYPE_RECHARGE_REFUND_ORDER;
  248. $desc = '充值订单退款,充值订单号:' . $order->order_no;
  249. $saveLog = AccountLog::saveLog($user->id, floatval($sub_balance), $type, $log_type, $order_type, $order->id, $desc);
  250. if(!$saveLog){
  251. throw new \Exception('操作失败,余额修改失败。');
  252. }
  253. }
  254. //扣积分
  255. if($sub_integral > 0){
  256. $type = AccountLog::TYPE_INTEGRAL;
  257. $log_type = AccountLog::LOG_TYPE_EXPEND;
  258. $order_type = AccountLog::TYPE_RECHARGE_REFUND_ORDER;
  259. $desc = '充值订单退款,充值订单号:' . $order->order_no;
  260. $saveLog = AccountLog::saveLog($user->id, $sub_integral, $type, $log_type, $order_type, $order->id, $desc);
  261. if(!$saveLog){
  262. throw new \Exception('操作失败,积分修改失败。');
  263. }
  264. }
  265. $refund->refund_price = $refund_price;
  266. $refund->sub_balance = $sub_balance;
  267. $refund->sub_integral = $sub_integral;
  268. $refund->status = 1;
  269. if (!$refund->save()) {
  270. throw new \Exception('操作失败,审核错误。' . array_shift($refund->getFirstErrors()));
  271. }
  272. if (in_array($order->pay_type, [1, 4])) {
  273. $refund_res = Refund::refund($order, OrderNo::ORDER_MALL, $refund->order_refund_no, $refund->refund_price);
  274. if ($refund_res !== true) {
  275. \Yii::error([__METHOD__, $refund_res]);
  276. $refund->status = 0;
  277. $refund->status_desc = json_encode($refund_res, JSON_UNESCAPED_UNICODE);
  278. if (!$refund->save()) {
  279. throw new \Exception('操作失败,' . array_shift($refund->getFirstErrors()));
  280. }
  281. }
  282. }
  283. $t->commit();
  284. // $order->mobile = $user->binding;
  285. // NoticeSend::OrderRefund($order->user_id, $order->mobile, $order->order_no, $refund->refund_price, '充值订单退款');
  286. return [
  287. 'code' => 0,
  288. 'msg' => '处理成功。',
  289. ];
  290. } catch (\Exception $ex) {
  291. $t->rollback();
  292. return [
  293. 'code' => 1,
  294. 'msg' => $ex->getMessage(),
  295. ];
  296. }
  297. }
  298. }