Order.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace mall\basic;
  10. use mall\library\payment\alipay\Alipay;
  11. use mall\utils\CString;
  12. use think\facade\Db;
  13. use think\facade\Request;
  14. use think\facade\Session;
  15. use mall\basic\Spread;
  16. use mall\basic\Subscribe;
  17. class Order {
  18. public static function create($data = []) {
  19. Db::startTrans();
  20. try{
  21. Db::name("order")->insert([
  22. "activity_id"=>isset($data["activity_id"]) ? $data["activity_id"] : 0,
  23. "shipping_type"=>isset($data["shipping_type"]) ? $data["shipping_type"] : 1,
  24. "store_id"=>isset($data["store_id"]) ? $data["store_id"] : 0,
  25. "type"=>$data['type'],
  26. "user_id"=>Users::get("id"),
  27. "order_no"=>self::orderNo(),
  28. "pay_type"=>$data['payment']["id"],
  29. "distribution_id"=>0,
  30. "accept_name"=>isset($data["address"]["accept_name"]) ? $data["address"]["accept_name"] : "",
  31. "zip"=>isset($data["address"]["zip"]) ? $data["address"]["zip"] : "",
  32. "mobile"=>isset($data["address"]["mobile"]) ? $data["address"]["mobile"] : "",
  33. "phone"=>isset($data["address"]["phone"]) ? $data["address"]["phone"] : "",
  34. "province"=>isset($data["address"]["province"]) ? $data["address"]["province"] : "",
  35. "city"=>isset($data["address"]["city"]) ? $data["address"]["city"] : "",
  36. "area"=>isset($data["address"]["area"]) ? $data["address"]["area"] : "",
  37. "address"=>isset($data["address"]["address"]) ? $data["address"]["address"] : "",
  38. "message"=>$data["remarks"],
  39. "promotions"=>isset($data["promotions"]) ? $data["promotions"] : 0,
  40. "discount"=>isset($data["discount"]) ? $data["discount"] : 0,
  41. "real_freight"=>$data["real_freight"],
  42. "payable_freight"=>$data["payable_freight"],
  43. "real_amount"=>$data["real_amount"],
  44. "real_point"=>isset($data["real_point"]) ? $data["real_point"] : 0,
  45. "order_amount"=>$data["order_amount"],
  46. "payable_amount"=>$data["payable_amount"],
  47. "shipping_code"=>isset($data["shipping_type"]) && $data["shipping_type"] == 2 ? Store::getUniqueCode() : "",
  48. "exp"=>$data["exp"],
  49. "point"=>$data["point"],
  50. "source"=>$data["source"],
  51. "create_time"=>time()
  52. ]);
  53. $order_id = Db::name("order")->getLastInsID();
  54. foreach($data["item"] as $val){
  55. $val["order_id"] = $order_id;
  56. $val["thumb_image"] = str_replace(Request::domain(),"",$val["thumb_image"]);
  57. $val["goods_array"] = json_encode([
  58. "title"=>$val["title"],
  59. "spec"=>!empty($val["goods_array"]) ? implode(", ",array_map(function ($res){
  60. return $res["name"] . ':' . $res['value'];
  61. },$val["goods_array"])) : ""
  62. ],JSON_UNESCAPED_UNICODE);
  63. Db::name("order_goods")->strict(false)->insert($val);
  64. }
  65. Db::commit();
  66. }catch(\Exception $ex){
  67. Db::rollback();
  68. throw new \Exception("由于网络繁忙,请稍后在试",0);
  69. }
  70. return $order_id;
  71. }
  72. /**
  73. * 支付成功后修改订单状态
  74. */
  75. public static function payment($order_no,$admin_id=0,$note="",$trade_no=""){
  76. if(($order = Db::name("order")->where(["order_no"=>$order_no])->find()) == false){
  77. throw new \Exception("您要查找的订单不存在!",0);
  78. }
  79. if($order["pay_status"] == 1){
  80. throw new \Exception("您查找的订单已支付!",0);
  81. }
  82. if(Db::name("order")->where(["order_no"=>$order_no])->update([
  83. "status" => ($order['status'] == 5) ? 5 : 2,
  84. "pay_time" => time(),
  85. "pay_status" => 1,
  86. "note" => $note,
  87. "trade_no"=>$trade_no,
  88. "admin_id"=>$admin_id
  89. ]) == false){
  90. throw new \Exception("操作订单失败,请重试!",0);
  91. }
  92. //插入收款单
  93. Db::name('order_collection')->insert([
  94. 'order_id' => $order['id'],
  95. 'user_id' => $order['user_id'],
  96. 'amount' => $order['order_amount'],
  97. 'create_time' => time(),
  98. 'payment_id' => $order['pay_type'],
  99. 'pay_status' => 1,
  100. 'is_delete' => 0,
  101. 'note' => $note,
  102. 'admin_id' => $admin_id ? $admin_id : 0
  103. ]);
  104. $orderGoodsList = Db::name("order_goods")->where(['order_id' => $order['id']])->select()->toArray();
  105. //减少库存量
  106. if ($order['pay_type'] != 0) {
  107. foreach ($orderGoodsList as $val) {
  108. self::updateStock([
  109. "goods_id" => $val["goods_id"],
  110. "product_id" => $val["product_id"],
  111. "goods_nums" => $val["goods_nums"]
  112. ], "-");
  113. Db::name("goods")->where('id',$val["goods_id"])->update([
  114. "sale"=>Db::raw("sale+1")
  115. ]);
  116. }
  117. }
  118. // 核销订单,购买后发货
  119. if($order["shipping_type"] == 2){
  120. foreach ($orderGoodsList as $val) {
  121. Db::name("order_goods")->where(["id"=>$val["id"]])->update([
  122. "is_send" => 1,
  123. "distribution_id" => 0
  124. ]);
  125. }
  126. //更新发货状态
  127. Db::name('order')->where(['id'=>$order['id']])->update([
  128. 'distribution_status' => 1,
  129. 'send_time' =>time()
  130. ]);
  131. Db::name("order_log")->insert([
  132. 'order_id' => $order['id'],
  133. 'username' => "system",
  134. 'action' => '发货',
  135. 'result' => '成功',
  136. 'note' => '订单【' . $order["order_no"] . '】由 system 发货',
  137. 'create_time' => time()
  138. ]);
  139. }
  140. $user = Db::name("wechat_users")->where("user_id",$order["user_id"])->find();
  141. if(!empty($user["mp_openid"])){
  142. Subscribe::orderPaySuccess($user["mp_openid"],$order["id"]);
  143. }
  144. if(!empty($user["openid"])){
  145. WechatTemplate::pay($user["openid"],$order["id"]);
  146. }
  147. return true;
  148. }
  149. /**
  150. * 订单完成
  151. */
  152. public static function complete($order_no,$admin_id=0){
  153. if(($order = Db::name("order")->where(["order_no"=>$order_no])->find()) == false){
  154. throw new \Exception("您要查找的订单不存在,请刷新重试!",0);
  155. }
  156. if(($users = Db::name("users")->where(array("id"=>$order["user_id"]))->find()) != false){
  157. if($order['exp'] > 0){
  158. $log = '成功购买了订单号:' . $order['order_no'] . '中的商品,奖励经验' . $order['exp'];
  159. Db::name("users")->where(["id"=>$order["user_id"]])->inc("exp",$order['exp'])->update();
  160. Db::name("users_log")->insert([
  161. "order_no"=>$order_no,
  162. "user_id"=>$order["user_id"],
  163. "admin_id"=> $admin_id ? $admin_id : 0,
  164. "action"=>2,
  165. "operation"=>0,
  166. "exp"=>$order['exp'],
  167. "description"=>$log,
  168. "create_time"=>time()
  169. ]);
  170. }
  171. if($order['point'] > 0){
  172. $log = '成功购买了订单号:' . $order['order_no'] . '中的商品,奖励积分' . $order['point'];
  173. Db::name("users")->where(["id"=>$order["user_id"]])->inc("point",$order['point'])->update();
  174. Db::name("users_log")->insert([
  175. "order_no"=>$order_no,
  176. "user_id"=>$order["user_id"],
  177. "admin_id"=> $admin_id ? $admin_id : 0,
  178. "action"=>1,
  179. "operation"=>0,
  180. "point"=>$order['point'],
  181. "description"=>$log,
  182. "create_time"=>time()
  183. ]);
  184. }
  185. Spread::backBrokerage($order);
  186. }
  187. //获取此订单中的商品种类
  188. $orderList = Db::name("order_goods")->where(['order_id'=>$order["id"]])->group('goods_id')->select()->toArray();
  189. $orderData = [
  190. "point"=>0,
  191. "describes"=>0,
  192. "service"=>0,
  193. "logistics"=>0,
  194. ];
  195. if($order["status"] == 5){
  196. $orderData["point"] = 5;
  197. $orderData["describes"] = 5;
  198. $orderData["service"] = 5;
  199. $orderData["logistics"] = 5;
  200. $orderData["comment_time"] = time();
  201. }
  202. //对每类商品进行评论开启
  203. foreach ($orderList as $val) {
  204. if (Db::name("goods")->where(['id'=>$val['goods_id']])->find()) {
  205. Db::name("users_comment")->insert(array_merge($orderData,[
  206. 'goods_id' => $val['goods_id'],
  207. 'order_no' => $order['order_no'],
  208. 'user_id' => $order['user_id'],
  209. 'create_time' => time()
  210. ]));
  211. }
  212. }
  213. self::updateOrderGroupStatus($order,2);
  214. $user = Db::name("wechat_users")->where("user_id",$order["user_id"])->find();
  215. if(!empty($user["mp_openid"])){
  216. Subscribe::orderComplete($user["mp_openid"],$order["id"]);
  217. }
  218. return true;
  219. }
  220. public static function updateOrderGroupStatus($order,$status,$refundStatus=0){
  221. if($order["type"] != 5){
  222. return false;
  223. }
  224. $condition = ["order_id"=>$order["id"],"user_id"=>$order["user_id"],"status"=>1,"is_refund"=>0];
  225. if($order["type"] == 5 && Db::name("order_group")->where($condition)->count()){
  226. return Db::name("order_group")->where($condition)->update([
  227. "is_refund"=>$refundStatus,"status"=>$status,"complete_time"=>time()
  228. ]);
  229. }
  230. return false;
  231. }
  232. public static function sendDistributionGoods($order_id, $order_goods_id, $admin_id){
  233. if ($order_id <= 0) {
  234. throw new \Exception("参数错误!", 0);
  235. }
  236. if (empty($order_goods_id)) {
  237. throw new \Exception("请选择要发货的商品", 0);
  238. }
  239. $distribution_code = Request::post('distribution_code',"","trim,strip_tags");
  240. $freight_id = Request::post('freight_id',0,"intval");
  241. if (empty($distribution_code)) {
  242. throw new \Exception("请填写配送单号", 0);
  243. }
  244. if (empty($freight_id)) {
  245. throw new \Exception("请选择物流公司", 0);
  246. }
  247. $refund = Db::name('order_refundment')->where([
  248. "order_id"=>$order_id,"pay_status"=>0,"is_delete"=>0
  249. ])->find();
  250. if(!empty($refund)){
  251. throw new \Exception("此订单有未处理的退款申请",0);
  252. }
  253. $order = Db::name("order")->where(["id"=>$order_id])->find();
  254. if(empty($order)){
  255. throw new \Exception("该订单不存在!",0);
  256. }
  257. $data = [
  258. 'order_id' => $order_id,
  259. 'user_id' => $order["user_id"],
  260. 'name' => Request::post('accept_name','','trim,strip_tags'),
  261. 'zip' => Request::post('zip','','intval'),
  262. 'phone' => Request::post('phone','','intval'),
  263. 'province' => Request::post('province','','intval'),
  264. 'city' => Request::post('city','','intval'),
  265. 'area' => Request::post('area','','intval'),
  266. 'address' => Request::post('address','','trim,strip_tags'),
  267. 'mobile' => Request::post('mobile','','trim,strip_tags'),
  268. 'freight' => $order["real_freight"],
  269. 'distribution_code' => $distribution_code,
  270. 'distribution_id' => $order["distribution_id"],
  271. 'note' => Request::post('remarks','','trim,strip_tags'),
  272. 'create_time' => time(),
  273. 'freight_id' => $freight_id
  274. ];
  275. $data['admin_id'] = $admin_id;
  276. $delivery_id = Db::name('order_delivery')->insert($data,true);
  277. $admin = Db::name("system_users")->where(["id"=>$admin_id])->find();
  278. $orderGoodsList = Db::name("order_goods")->where("id","in",$order_goods_id)->select()->toArray();
  279. if ($order['pay_type'] == 0) {
  280. //减少库存量
  281. foreach ($orderGoodsList as $val) {
  282. self::updateStock([
  283. "goods_id" => $val["goods_id"],
  284. "product_id" => $val["product_id"],
  285. "goods_nums"=>$val["goods_nums"]
  286. ], "-");
  287. }
  288. }
  289. foreach ($orderGoodsList as $val) {
  290. if(!empty($val["fictitious_array"])){
  291. $fictitious_array = json_decode($val["fictitious_array"],true);
  292. if(!empty($fictitious_array) && $fictitious_array["goods_type"] == 1){
  293. $cardTemp = Db::name("goods_card_template")->where([
  294. ["card_id","=",$fictitious_array["value"]],
  295. ["order_id","<>",$order["id"]],
  296. ["status","=",1]
  297. ])->find();
  298. if(empty($cardTemp)){
  299. throw new \Exception("卡密为空,请添加新的卡密",0);
  300. }
  301. $fictitious_array["card"] = "帐号" . $cardTemp["name"] . " 密码:" . $cardTemp["content"];
  302. Db::name("order_goods")->where("id",$val["id"])->update([
  303. "fictitious_array"=>json_encode($fictitious_array,JSON_UNESCAPED_UNICODE)
  304. ]);
  305. Db::name("goods_card_template")->where("id",$cardTemp["id"])->update([
  306. "order_id"=>$order["id"],
  307. "user_id"=>$order["user_id"],
  308. "status"=>2
  309. ]);
  310. }
  311. }
  312. }
  313. //更新发货状态
  314. $orderGoods = Db::name("order_goods")->field('count(*) as num')->where([
  315. "is_send"=>0,"order_id"=>$order_id
  316. ])->find();
  317. $sendStatus = 2; //部分发货
  318. if (count($order_goods_id) >= $orderGoods['num']) {
  319. $sendStatus = 1; //全部发货
  320. }
  321. foreach ($order_goods_id as $val) {
  322. Db::name("order_goods")->where(["id"=>$val])->update([
  323. "is_send" => 1,
  324. "distribution_id" => $delivery_id
  325. ]);
  326. }
  327. //更新发货状态
  328. Db::name('order')->where(['id'=>$order_id])->update([
  329. 'distribution_status' => $sendStatus,
  330. 'send_time' =>time()
  331. ]);
  332. Db::name("order_log")->insert([
  333. 'order_id' => $order_id,
  334. 'username' => $admin["username"],
  335. 'action' => '发货',
  336. 'result' => '成功',
  337. 'note' => '订单【' . $order["order_no"] . '】由【管理员】' . $admin["username"] . '发货',
  338. 'create_time' => time()
  339. ]);
  340. $user = Db::name("wechat_users")->where("user_id",$order["user_id"])->find();
  341. if(!empty($user["mp_openid"])){
  342. Subscribe::deliveryNotice($user["mp_openid"],$delivery_id);
  343. }
  344. if(!empty($user["openid"])){
  345. WechatTemplate::delivery($user["openid"],$delivery_id);
  346. }
  347. try {
  348. Sms::send(
  349. ["mobile"=>$order["mobile"],"order_no"=>$order["order_no"]],
  350. "deliver_goods"
  351. );
  352. }catch (\Exception $e){}
  353. return true;
  354. }
  355. public static function refund($refunds_id,$admin_id=0){
  356. $refunds = Db::name("order_refundment")->where(["id"=>$refunds_id])->find();
  357. $orderGoodsList = Db::name("order_goods")->where("id","in",$refunds['order_goods_id'])->where("is_send","<>","2")->select()->toArray();
  358. if (!$orderGoodsList) {
  359. throw new \Exception("订单中没有符合退货条件的商品!",0);
  360. }
  361. //退款的商品关联信息
  362. $autoMount = 0;
  363. $orderRow = [
  364. 'exp' => 0,
  365. 'point' => 0,
  366. 'order_no' => $refunds['order_no']
  367. ];
  368. foreach ($orderGoodsList as $val) {
  369. $autoMount += $val['goods_nums'] * $val['real_price'];
  370. //库存增加
  371. self::updateStock(["goods_id" => $val["goods_id"], "product_id" => $val["product_id"], "goods_nums"=>$val["goods_nums"]], '+');
  372. //更新退款状态
  373. Db::name("order_goods")->where('id',$val['id'])->update(['is_send' => 2]);
  374. //退款积分,经验
  375. $goodsRow = Db::name("goods")->where('id',$val['goods_id'])->find();
  376. $orderRow['exp'] += $goodsRow['exp'] * $val['goods_nums'];
  377. $orderRow['point'] += $goodsRow['point'] * $val['goods_nums'];
  378. }
  379. //如果管理员自定义了退款金额。否则就使用默认的付款商品金额
  380. $amount = $refunds['amount'] > 0 ? $refunds['amount'] : $autoMount;
  381. //更新order表状态,查询是否订单中还有未退款的商品,判断是订单退款状态:全部退款或部分退款
  382. $isSendData = Db::name("order_goods")->where('order_id',$refunds['order_id'])->where('is_send','<>','2')->find();
  383. $orderStatus = 6; //全部退款
  384. if ($isSendData) {
  385. $orderStatus = 7; //部分退款
  386. }
  387. Db::name("order")->where(["id"=>$refunds['order_id']])->update(['status' => $orderStatus]);
  388. $order = Db::name("order")->where('id',$refunds['order_id'])->find();
  389. if ($orderStatus == 6) {
  390. //在订单商品没有发货情况下,返还运费,报价,税金
  391. $isDeliveryData = Db::name("order_goods")->where('order_id',$refunds['order_id'])->where('distribution_id','>','0')->find();
  392. if (!$isDeliveryData) {
  393. $amount += $order['real_freight'] + $order['insured'] + $order['taxes'];
  394. }
  395. }
  396. $out_refund_no = self::orderNo();
  397. //更新退款表
  398. Db::name("order_refundment")->where(["id"=>$refunds_id])->update([
  399. 'out_refund_no'=>$out_refund_no,
  400. 'amount' => $amount,
  401. 'pay_status' => 2,
  402. 'dispose_time' =>time()
  403. ]);
  404. $admin = Db::name("system_users")->where(["id"=>$refunds["admin_id"]])->find();
  405. if($refunds["type"] == 0){
  406. Db::name("users")->where(["id"=>$order["user_id"]])->inc("amount",$amount)->update();
  407. Db::name("users_log")->insert([
  408. "order_no"=>$order["order_no"],
  409. "user_id"=>$order["user_id"],
  410. "admin_id"=>Session::get("system_user_id"),
  411. "action"=>3,
  412. "operation"=>1,
  413. "amount"=>$amount,
  414. "description"=>'退款订单号:' . $refunds['order_no'] . '中的商品,退款金额 -¥' . $amount,
  415. "create_time"=>time()
  416. ]);
  417. }else if($refunds["type"] == 1){
  418. $payment = Db::name("payment")->where("id",$order["pay_type"])->find();
  419. switch($payment["code"]){
  420. case "wechat":
  421. case "wechat-h5":
  422. case "wechat-app":
  423. case "wechat-qrcode":
  424. $refund = new \mall\library\wechat\chat\payment\Refund();
  425. $refund->create([
  426. "transaction_id"=>$order['trade_no'],
  427. "out_trade_no"=>$order["order_no"],
  428. "out_refund_no"=>$out_refund_no,
  429. "total_fee"=>$order["order_amount"] * 100,
  430. "refund_fee"=>$amount * 100
  431. ]);
  432. break;
  433. case "wechat-mini":
  434. $refund = new \mall\library\wechat\mini\payment\Refund();
  435. $refund->create([
  436. "transaction_id"=>$order['trade_no'],
  437. "out_trade_no"=>$order["order_no"],
  438. "out_refund_no"=>$out_refund_no,
  439. "total_fee"=>$order["order_amount"] * 100,
  440. "refund_fee"=>$amount * 100
  441. ]);
  442. break;
  443. case "alipay-app":
  444. case "alipay-web":
  445. case "alipay-wap":
  446. $alipay = Alipay::instance()->refund($payment["code"],$order["order_no"],$amount);
  447. if($alipay->msg != "Success" && $alipay->code != "10000"){
  448. throw new \Exception("支付宝退款失败 [CODE:{$alipay->code}]" . $alipay->msg . " [sub_code: {$alipay->subCode}]" . $alipay->subMsg,0);
  449. }
  450. break;
  451. }
  452. }
  453. if($orderRow['exp'] > 0){
  454. //更新用户的信息
  455. $users = Db::name("users")->where(["id"=>$refunds["user_id"]])->find();
  456. $exp = $users['exp'] - $orderRow['exp'];
  457. if($exp > 0) {
  458. Db::name("users")->where(["id" => $refunds["user_id"]])->update([
  459. 'exp' => $exp <= 0 ? 0 : $exp
  460. ]);
  461. }
  462. $log = '退款订单号:' . $refunds['order_no'] . '中的商品,减掉经验 -' . $orderRow['exp'];
  463. Db::name("users_log")->insert([
  464. "order_no"=>$order["order_no"],
  465. "user_id"=>$refunds["user_id"],
  466. "admin_id"=>$admin_id ? $admin_id : "-1",
  467. "action"=>2,
  468. "operation"=>1,
  469. "point"=>$orderRow['exp'],
  470. "description"=>$log,
  471. "create_time"=>time()
  472. ]);
  473. }
  474. if($orderRow['point'] > 0){
  475. $log = '退款订单号:' . $refunds['order_no'] . '中的商品,减掉积分 -' . $orderRow['point'];
  476. Db::name("users")->where(["id"=>$order["user_id"]])->dec("point",$orderRow['point'])->update();
  477. Db::name("users_log")->insert([
  478. "order_no"=>$order["order_no"],
  479. "user_id"=>$refunds["user_id"],
  480. "admin_id"=>$admin_id ? $admin_id : "-1",
  481. "action"=>1,
  482. "operation"=>1,
  483. "point"=>$orderRow['point'],
  484. "description"=>$log,
  485. "create_time"=>time()
  486. ]);
  487. }
  488. $user = Db::name("wechat_users")->where("user_id",$order["user_id"])->find();
  489. if(!empty($user["mp_openid"])){
  490. Subscribe::refundNotice($user["mp_openid"],$refunds['order_no']);
  491. }
  492. Db::name("order_log")->insert([
  493. 'order_id' => $refunds["order_id"],
  494. 'username' => $admin["username"],
  495. 'action' => '退款',
  496. 'result' => '成功',
  497. 'note' => '订单【' . $refunds["order_no"] . '】退款,退款金额:' . $amount,
  498. 'create_time' => time()
  499. ]);
  500. self::updateOrderGroupStatus($order,3,1);
  501. return true;
  502. }
  503. /**
  504. * 获取已退款金额
  505. * @param array $data
  506. * @return float|int
  507. */
  508. public static function getRefundAmount($data){
  509. $list = Db::name("order_refundment")->where([
  510. "order_id"=>$data["id"],"pay_status"=>2
  511. ])->select()->toArray();
  512. $refundFee = 0.00;
  513. foreach ($list as $val) {
  514. $refundFee += $val['amount'];
  515. }
  516. return number_format($refundFee,2);
  517. }
  518. /**
  519. * 是否允许退款申请
  520. * @param $order
  521. * @param $message
  522. * @return bool
  523. */
  524. public static function refundmentApply($order, $message) {
  525. if(!in_array($order["status"],[2,7])){
  526. throw new \Exception("您的订单不允许做退款处理",0);
  527. }
  528. $orderGoods = Db::name("order_goods")->where([
  529. "order_id"=>$order["id"]
  530. ])->select()->toArray();
  531. $arr = [];
  532. foreach ($orderGoods as $val) {
  533. if ($val['is_send'] == 2) {
  534. throw new \Exception("该订单已经有商品做了退款处理", 0);
  535. }
  536. if (Db::name("order_refundment")
  537. ->where("is_delete",0)->where("pay_status",0)
  538. ->where('FIND_IN_SET(' . $val["id"] . ',order_goods_id)')->count()) {
  539. throw new \Exception("您已经对此商品提交了退款申请,请耐心等待", 0);
  540. }
  541. $arr[] = $val["id"];
  542. }
  543. Db::startTrans();
  544. try{
  545. Db::name("order_refundment")->insert([
  546. "order_no"=>$order["order_no"],
  547. "order_id"=>$order["id"],
  548. "user_id"=>$order["user_id"],
  549. "pay_status"=>0,
  550. "content"=>$message,
  551. "amount"=>$order['order_amount'],
  552. "order_goods_id"=>implode(',',$arr),
  553. "create_time"=>time(),
  554. ]);
  555. self::updateOrderGroupStatus($order,3,1);
  556. Db::commit();
  557. }catch (\Exception $e){
  558. Db::rollback();
  559. throw new \Exception("提交退款申请失败,请稍后在试。",0);
  560. //throw new \Exception($e->getMessage(),0);
  561. }
  562. return true;
  563. }
  564. public static function getOrderType($type=""){
  565. $arr = ["point"=>1,"regiment"=>2,"second"=>3,"special"=>4,"group"=>5,"buy"=>0,"cart"=>0];
  566. return isset($arr[$type]) ? $arr[$type] : 0;
  567. }
  568. public static function getOrderTypeText($type,$length=-1) {
  569. switch ($type) {
  570. case "1":
  571. $string = '积分订单';
  572. break;
  573. case "2":
  574. $string = '团购订单';
  575. break;
  576. case "3":
  577. $string = "秒杀订单";
  578. break;
  579. case "5":
  580. $string = "拼团订单";
  581. break;
  582. case '4':
  583. default:
  584. $string = '普通订单';
  585. }
  586. return $length == -1 ? $string : CString::msubstr($string,$length,false);
  587. }
  588. public static function getRefundmentText($code) {
  589. $result = ['0' => '申请退款', '1' => '退款失败', '2' => '退款成功'];
  590. return isset($result[$code]) ? $result[$code] : '';
  591. }
  592. public static function getSendStatus($code) {
  593. $data = [0 => '等待发货', 1 => '已发货', 2=>"部份发货", 3 => '已退货'];
  594. return isset($data[$code]) ? $data[$code] : '未配货';
  595. }
  596. public static function getPaymentStatusText($status){
  597. return $status == 0 ? "未支付" : "已支付";
  598. }
  599. public static function getDeliveryStatus($status){
  600. return $status == 0 ? "未收货" : "已收货";
  601. }
  602. public static function getEvaluateStatus($status){
  603. switch ($status){
  604. case 0:
  605. return '未评价';
  606. case 1:
  607. return '已评价';
  608. case 2:
  609. return '部份评价';
  610. default:
  611. return '';
  612. }
  613. }
  614. public static function getOrderActive($order){
  615. if($order["pay_status"] == 0){
  616. return 0;
  617. }
  618. if($order["status"] == 2 && $order["distribution_status"] == 0){
  619. return 1;
  620. }else if($order["status"] == 2 && $order["distribution_status"]){
  621. return 2;
  622. }
  623. if($order["status"] == 5 && in_array($order["evaluate_status"],[0,2])){
  624. return 3;
  625. }
  626. if($order["status"] == 5 && $order["evaluate_status"] == 1){
  627. return 4;
  628. }
  629. return -1;
  630. }
  631. public static function getStatusText($code) {
  632. $result = [
  633. 0=>'未知',1=>'等待付款',2=>'等待发货',3=>'待收货',4 => '部份发货',
  634. 5=>'待评价',6=>'已完成',7=>'已取消',8=>'退款成功',9=>'未发货',
  635. 10=>'部分退款',11=>'申请退款',12=>'拒绝退款'
  636. ];
  637. return isset($result[$code]) ? $result[$code] : '';
  638. }
  639. public static function getStatus($order){
  640. if(empty($order)){
  641. return 0;
  642. }
  643. if($order["status"] == 1 && $order["pay_status"] == 0){ // 待付款
  644. return 1;
  645. }else if($order["status"] == 2){
  646. // 检查是否有退款
  647. $refund = Db::name('order_refundment')->where([
  648. "order_id"=>$order['id'],"is_delete"=>0
  649. ])->find();
  650. if(!empty($refund)){
  651. if($refund["pay_status"] == 0){
  652. return 11;
  653. }
  654. if($refund["pay_status"] == 1){
  655. if($order["distribution_status"] == 1){
  656. return 3;
  657. }else if($order["distribution_status"] == 2){
  658. return 4;
  659. }
  660. return 12;
  661. }
  662. }
  663. if($order["distribution_status"] == 0){ // 待发货
  664. return 2;
  665. }else if($order["distribution_status"] == 1){ // 待收货
  666. return 3;
  667. }else if($order["distribution_status"] == 2){ // 部份发货
  668. return 4;
  669. }
  670. }
  671. if($order["status"] == 5){
  672. if(in_array($order["evaluate_status"],[0,2])){ // 待评价
  673. return 5;
  674. }else if($order["evaluate_status"] == 1){ // 已完成
  675. return 6;
  676. }
  677. }else if ($order['status'] == 3 || $order['status'] == 4) { //3,取消或者作废订单
  678. return 7;
  679. }else if ($order['status'] == 6) { // 5,退款
  680. return 8;
  681. }else if ($order['status'] == 7) { // 6,部分退款
  682. if ($order['distribution_status'] == 1) { // 发货
  683. return 10;
  684. } else { // 未发货
  685. return 9;
  686. }
  687. }
  688. return 0;
  689. }
  690. public static function orderNo($number = '', $date = 'YmdHis') {
  691. $arr = explode(" ", microtime());
  692. $usec = substr(str_replace('0.', '', $arr[0]), 0, 2) . rand(100, 999);
  693. return $number . date($date) . $usec;
  694. }
  695. /**
  696. * 更新库存
  697. * @return boolean
  698. */
  699. public static function updateStock($data, $type = "-") {
  700. if ($data["product_id"] > 0) {
  701. $product = Db::name("goods_item")->where([
  702. "goods_id"=>$data["goods_id"],"id"=>$data["product_id"]
  703. ])->find();
  704. }
  705. $product_store = 0;
  706. $goods = Db::name("goods")->where(["id"=>$data["goods_id"]])->find();
  707. switch ($type) {
  708. case "-":
  709. if (!empty($product)) {
  710. $product_store = $product["store_nums"] - $data["goods_nums"];
  711. }
  712. $goods_store = $goods["store_nums"] - $data["goods_nums"];
  713. break;
  714. case "+":
  715. if (!empty($product)) {
  716. $product_store = $product["store_nums"] + $data["goods_nums"];
  717. }
  718. $goods_store = $goods["store_nums"] + $data["goods_nums"];
  719. break;
  720. }
  721. if ($data["product_id"] > 0) {
  722. Db::name("goods_item")->where([
  723. "goods_id"=>$data["goods_id"],"id"=>$data["product_id"]
  724. ])->update(["store_nums" => $product_store]);
  725. }
  726. Db::name("goods")->where([ "id"=>$data["goods_id"]])->update(["store_nums" => $goods_store]);
  727. return true;
  728. }
  729. }