OrderController.php 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. <?php
  2. namespace hd\controllers;
  3. use biz\shop\classes\ShopExtClass;
  4. use biz\sj\services\MerchantExtendService;
  5. use biz\sj\services\MerchantService;
  6. use bizHd\custom\classes\CustomClass;
  7. use bizHd\goods\classes\GoodsClass;
  8. use bizHd\merchant\services\ShopService;
  9. use bizHd\order\classes\OrderClass;
  10. use bizHd\order\classes\OrderSendClass;
  11. use bizHd\order\services\OrderService;
  12. use bizHd\product\classes\ProductClass;
  13. use bizHd\saas\services\RegionService;
  14. use bizHd\shop\classes\ShopClass;
  15. use bizHd\user\services\UserService;
  16. use bizHd\wx\classes\WxOpenClass;
  17. use common\components\dict;
  18. use common\components\dateUtil;
  19. use common\components\payUtil;
  20. use common\services\xhPayToolService;
  21. use Yii;
  22. use common\components\util;
  23. use common\components\stringUtil;
  24. use bizHd\promote\services\CouponService;
  25. use common\components\httpUtil;
  26. use biz\wx\classes\WxMessageClass;
  27. class OrderController extends BaseController
  28. {
  29. public $guestAccess = ['order-relate', 'fast-pay', 'create-order', 'list'];
  30. //客户欠款的订单 ssh 2021.2.4
  31. public function actionDebtList()
  32. {
  33. $get = Yii::$app->request->get();
  34. $id = $get['id'] ?? 0;
  35. $info = CustomClass::getById($id, true);
  36. CustomClass::valid($info, $this->shopId);
  37. $where = ['customId' => $id, 'debt' => 1];
  38. $searchTime = $get['searchTime'] ?? '';
  39. if (!empty($searchTime)) {
  40. $startTime = $get['startTime'] ?? '';
  41. $endTime = $get['endTime'] ?? '';
  42. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  43. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  44. }
  45. $list = OrderClass::getAllByCondition($where, 'addTime DESC', ['id', 'orderSn', 'actPrice', 'addTime', 'remainDebtPrice']);
  46. $result = ['customInfo' => $info, 'list' => $list];
  47. util::success($result);
  48. }
  49. //发货
  50. public function actionSend()
  51. {
  52. $get = Yii::$app->request->get();
  53. $id = isset($get['id']) ? $get['id'] : 0;
  54. $order = OrderClass::getById($id, true);
  55. if (empty($order)) {
  56. util::fail('没有找到订单');
  57. }
  58. OrderClass::valid($order, $this->mainId);
  59. if ($order->status != 2) {
  60. util::fail('订单不是待发货状态,请刷新');
  61. }
  62. $order->status = 4;
  63. $order->save();
  64. util::complete('操作成功');
  65. }
  66. //打印订单 ssh 20220601
  67. public function actionPrintOrder()
  68. {
  69. $get = Yii::$app->request->get();
  70. $id = isset($get['id']) ? $get['id'] : 0;
  71. $order = OrderClass::getById($id, true);
  72. if (empty($order)) {
  73. util::fail('没有找到订单');
  74. }
  75. OrderClass::valid($order, $this->mainId);
  76. OrderClass::onlinePrint($order, true);
  77. util::complete();
  78. }
  79. //微信和支付宝付款码支付复查 ssh 20220422
  80. public function actionCodePayCheck()
  81. {
  82. ini_set('date.timezone', 'Asia/Shanghai');
  83. header("Content-type: text/html; charset=utf-8");
  84. $get = Yii::$app->request->get();
  85. $id = isset($get['id']) ? $get['id'] : 0;
  86. $order = OrderClass::getById($id, true);
  87. OrderClass::valid($order, $this->mainId);
  88. $orderSn = $order->orderSn ?? '';
  89. $totalFee = $order->mainPay ?? 0;
  90. if ($order->status == OrderClass::ORDER_STATUS_COMPLETE) {
  91. util::success(['returnStatus' => 'SUCCESS']);
  92. }
  93. if ($order->status != OrderClass::ORDER_STATUS_UN_PAY) {
  94. util::complete('订单不是待付款状态');
  95. }
  96. $connection = Yii::$app->db;
  97. $transaction = $connection->beginTransaction();
  98. try {
  99. $payWay = $cg->payWay ?? dict::getDict('payWay', 'wxPay');
  100. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  101. if ($payWay == dict::getDict('payWay', 'wxPay')) {
  102. //微信付款
  103. $wx = Yii::getAlias("@vendor/wxPayApi_v3.0.10");
  104. require_once($wx . '/lib/WxPay.Api.php');
  105. require_once($wx . '/example/WxPay.MicroPay.php');
  106. //获取微信商户号等信息
  107. $sjExtend = WxOpenClass::getMallWxInfo();
  108. $microPay = new \MicroPay();
  109. $payReturn = $microPay->query($orderSn, $sjExtend);
  110. if ($payReturn["return_code"] == "SUCCESS" && $payReturn["result_code"] == "SUCCESS") {
  111. if ($payReturn["trade_state"] == "SUCCESS") {
  112. //支付成功
  113. $transactionId = $payReturn['transaction_id'] ?? '';
  114. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  115. $order->save();
  116. $attach = '';
  117. payUtil::thirdPay($payWay, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  118. $transaction->commit();
  119. //打印小票和语音播报
  120. $newOrder = OrderClass::getById($id, true);
  121. OrderClass::onlinePrint($newOrder);
  122. ShopExtClass::hdGatheringReport($newOrder);
  123. util::success(['returnStatus' => 'SUCCESS']);
  124. } else if ($payReturn["trade_state"] == "USERPAYING") {
  125. //用户支付中
  126. util::complete('正在付款,请稍候..');
  127. }
  128. }
  129. if ($payReturn["err_code"] == "ORDERNOTEXIST") {
  130. //交易订单号不存在
  131. util::complete('订单没有付款');
  132. } else {
  133. util::complete('系统错误,请稍候再试');
  134. }
  135. } elseif ($payWay == dict::getDict('payWay', 'alipay')) {
  136. //支付宝付款
  137. $aliF2F = Yii::getAlias("@vendor/alipayF2F");
  138. require_once($aliF2F . '/f2fpay/model/builder/AlipayTradePayContentBuilder.php');
  139. require_once($aliF2F . '/f2fpay/service/AlipayTradeService.php');
  140. // $aliPId = '2017041906821571';
  141. // $aliKey = 'MIIEpAIBAAKCAQEAvbeu6o90L+0AilI+mbhuJJH0lUv7hWQCEC+XUc+cWlC9ulKC3cSVScVLJHpUc09aj6v/iD1p9iVfgMxjeADNVKiQC2RJyRshAjzyTq+2bTcODp619iV2y4qW8UM65VcP7Ide3P0uT5xC2ReW0HpkdHZgm4F+P7mh1VhGt9qOP3hjvoraAHhtGACvsGvy8H69HhSAG+CEmFt7f2tKNWJ8xEUvkfhDvys93PfTk4xMWj048hJE2r38+2mWI/Il5dFQHWXXTVl2x9yi7uhEEW42a+7s9Q3HgqEvZiQ2NI1FTeEAPLFBApiEsNLY/smaszwqqfWGAyTMvtQ6T4K96GcTLQIDAQ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MLNHDwc03tpINRLrmq00sD0907oeuY8/2l6P7XOZSnCvE5Fw==';
  145. $aliPublicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgEb9zdQQDva63DhksZHPZCDrRe2xvZcTRy9mEiWs/pUhCG0QABkuePUyvqTAKINiCEv2QJmdF8qt3hmTSXuME5bT4vghckSGUTp4CNzsT90Vfjk4wci+bpAqNnpUASDuqZLCzebTuOhxYtGVBqYzhp/CxcC/Mag3ISjUG6jRqyUi15R1e2ruq74sohLGnb4m3nnwRVwnNVAaUcVfV1J0Nb0/9pusrj2t6FCyCkFeWbRDzKhplPQejaET2p5yplwSUpd1gqF+3srIn3erW5uAR6F0Z0NoCMzlo5TEXKWY2XMq97ubueE7Cgp8/c3dh5dkscAT2CCWwRq23aKozp+XMwIDAQAB';
  146. $config = array(
  147. //签名方式,默认为RSA2(RSA2048)
  148. 'sign_type' => "RSA2",
  149. //支付宝公钥
  150. 'alipay_public_key' => $aliPublicKey,
  151. //商户私钥
  152. 'merchant_private_key' => $aliKey,
  153. //编码格式
  154. 'charset' => "UTF-8",
  155. //支付宝网关
  156. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  157. //应用ID
  158. 'app_id' => $aliPId,
  159. //异步通知地址,只有扫码支付预下单可用
  160. 'notify_url' => Yii::$app->params['mallHost'] . "/notice/ali-callback",
  161. //最大查询重试次数
  162. 'MaxQueryRetry' => "10",
  163. //查询间隔
  164. 'QueryDuration' => "3"
  165. );
  166. //构造查询业务请求参数对象
  167. $queryContentBuilder = new \AlipayTradeQueryContentBuilder();
  168. $queryContentBuilder->setOutTradeNo($orderSn);
  169. //初始化类对象,调用queryTradeResult方法获取查询应答
  170. $queryResponse = new \AlipayTradeService($config);
  171. $queryResult = $queryResponse->queryTradeResult($queryContentBuilder);
  172. if ($queryResult->getTradeStatus() == 'SUCCESS') {
  173. $respond = $queryResult->getResponse();
  174. $transactionId = $respond->trade_no ?? '';
  175. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  176. $order->save();
  177. $attach = '';
  178. payUtil::thirdPay($payWay, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  179. $transaction->commit();
  180. //打印小票和语音播报
  181. $newOrder = OrderClass::getById($id, true);
  182. OrderClass::onlinePrint($newOrder);
  183. ShopExtClass::hdGatheringReport($newOrder);
  184. util::success(['returnStatus' => 'SUCCESS']);
  185. }
  186. } else {
  187. util::complete('还没有付款哦');
  188. }
  189. } catch (\Exception $e) {
  190. $transaction->rollBack();
  191. Yii::error("支付失败原因:" . $e->getMessage());
  192. util::complete('支付失败');
  193. }
  194. }
  195. //微信和支付宝付款码支付 ssh 2021.4.11
  196. public function actionCodePay()
  197. {
  198. ini_set('date.timezone', 'Asia/Shanghai');
  199. header("Content-type: text/html; charset=utf-8");
  200. $get = Yii::$app->request->get();
  201. $id = isset($get['id']) ? $get['id'] : 0;
  202. $order = OrderClass::getById($id, true);
  203. OrderClass::valid($order, $this->mainId);
  204. $authCode = (string)$get['authCode'] ?? '';
  205. if (empty($authCode)) {
  206. util::fail('没有获取到支付码');
  207. }
  208. $wxPrefix = ['10', '11', '12', '13', '14', '15'];
  209. $isWx = false;
  210. foreach ($wxPrefix as $wxId) {
  211. if (strpos($authCode, $wxId) === 0) {
  212. $isWx = true;
  213. }
  214. }
  215. $orderSn = $order->orderSn ?? '';
  216. $outTradeNo = $orderSn;
  217. $subject = '购买商品';
  218. $totalFee = $order->mainPay ?? 0;
  219. $totalAmount = $totalFee;
  220. $body = "门店";
  221. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  222. $connection = Yii::$app->db;
  223. $transaction = $connection->beginTransaction();
  224. try {
  225. if ($isWx) {
  226. $wxPayWay = dict::getDict('payWay', 'wxPay');
  227. $order->payWay = $wxPayWay;
  228. $order->save();
  229. $wx = Yii::getAlias("@vendor/wxPayApi_v3.0.10");
  230. require_once($wx . '/lib/WxPay.Api.php');
  231. require_once($wx . '/example/WxPay.MicroPay.php');
  232. $input = new \WxPayMicroPay();
  233. $input->SetAuth_code($authCode);
  234. $input->SetBody($subject);
  235. $input->SetTotal_fee($totalFee * 100);
  236. $input->SetOut_trade_no($orderSn);
  237. //获取微信商户号等信息
  238. $sjExtend = WxOpenClass::getMallWxInfo();
  239. $microPay = new \MicroPay();
  240. $res = $microPay->pay($input, $sjExtend);
  241. if (isset($res["return_code"]) && $res["return_code"] == "SUCCESS" && isset($res["result_code"]) && $res["result_code"] == 'SUCCESS') {
  242. $transactionId = $res['transaction_id'] ?? '';
  243. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  244. $order->save();
  245. $attach = '';
  246. payUtil::thirdPay($wxPayWay, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  247. $transaction->commit();
  248. $newOrder = OrderClass::getById($id, true);
  249. //打印小票
  250. OrderClass::onlinePrint($newOrder);
  251. //语音播报
  252. ShopExtClass::hdGatheringReport($newOrder);
  253. $shopId = $newOrder->shopId ?? 0;
  254. $shop = ShopClass::getById($shopId, true);
  255. WxMessageClass::gatheringIncomeInform($shop, $newOrder);
  256. util::success(['returnStatus' => 'SUCCESS']);
  257. } else {
  258. Yii::info(json_encode($res));
  259. $returnMsg = $res['return_msg'] ?? '';
  260. Yii::info($returnMsg);
  261. $errCode = $res['err_code'] ?? '';
  262. $msg = '';
  263. if ($errCode == 'NOTENOUGH') {
  264. $msg = '余额不足';
  265. }
  266. if ($errCode == 'USERPAYING') {
  267. $msg = '等待客户输入支付密码';
  268. }
  269. util::success(['returnStatus' => 'FAILURE'], $msg);
  270. }
  271. } else {
  272. $aliPayWay = dict::getDict('payWay', 'alipay');
  273. $order->payWay = $aliPayWay;
  274. $order->save();
  275. $aliF2F = Yii::getAlias("@vendor/alipayF2F");
  276. require_once($aliF2F . '/f2fpay/model/builder/AlipayTradePayContentBuilder.php');
  277. require_once($aliF2F . '/f2fpay/service/AlipayTradeService.php');
  278. // $aliPId = '2017041906821571';
  279. // $aliKey = 'MIIEpAIBAAKCAQEAvbeu6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Ha/PjCYietetO2gpDRmAdSqrWGH8CgYBkwp+r6pkJzW60kHSZIlEKAe7oJMwLNC2ZqQ4MwGwzfBaH+E34kCuR8ZqYzyAIVOa/Nwi5By1Zvi1KzBwJmUUTJ3o7WCOE96EzSrmOZlqXNCwO/36Vh6dshhhE/birIlXJSP0xdzpBQy2ksyli9eGy8cdJ2Y72Wo+TjSclRbKiPQKBgQD1C/CJRQbahz6B3Sc4NOiFUQioM30qDKbDibzUq95u5PtQOx69+/h68tA2nj7ILZOXoh6/KF6LM9WK0QctY0HR5Uy4Uz8YkY/Y0L/LExSkNdpYqbpBc8DdIsKYHoMLNHDwc03tpINRLrmq00sD0907oeuY8/2l6P7XOZSnCvE5Fw==';
  283. $aliPublicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgEb9zdQQDva63DhksZHPZCDrRe2xvZcTRy9mEiWs/pUhCG0QABkuePUyvqTAKINiCEv2QJmdF8qt3hmTSXuME5bT4vghckSGUTp4CNzsT90Vfjk4wci+bpAqNnpUASDuqZLCzebTuOhxYtGVBqYzhp/CxcC/Mag3ISjUG6jRqyUi15R1e2ruq74sohLGnb4m3nnwRVwnNVAaUcVfV1J0Nb0/9pusrj2t6FCyCkFeWbRDzKhplPQejaET2p5yplwSUpd1gqF+3srIn3erW5uAR6F0Z0NoCMzlo5TEXKWY2XMq97ubueE7Cgp8/c3dh5dkscAT2CCWwRq23aKozp+XMwIDAQAB';
  284. $config = array(
  285. //签名方式,默认为RSA2(RSA2048)
  286. 'sign_type' => "RSA2",
  287. //支付宝公钥
  288. 'alipay_public_key' => $aliPublicKey,
  289. //商户私钥
  290. 'merchant_private_key' => $aliKey,
  291. //编码格式
  292. 'charset' => "UTF-8",
  293. //支付宝网关
  294. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  295. //应用ID
  296. 'app_id' => $aliPId,
  297. //异步通知地址,只有扫码支付预下单可用
  298. 'notify_url' => Yii::$app->params['mallHost'] . "/notice/ali-callback",
  299. //最大查询重试次数
  300. 'MaxQueryRetry' => "10",
  301. //查询间隔
  302. 'QueryDuration' => "3"
  303. );
  304. // 支付超时,线下扫码交易定义为5分钟
  305. $timeExpress = "5m";
  306. // 创建请求builder,设置请求参数
  307. $barPayRequestBuilder = new \AlipayTradePayContentBuilder();
  308. $barPayRequestBuilder->setOutTradeNo($outTradeNo);
  309. $barPayRequestBuilder->setTotalAmount($totalAmount);
  310. $barPayRequestBuilder->setAuthCode($authCode);
  311. $barPayRequestBuilder->setTimeExpress($timeExpress);
  312. $barPayRequestBuilder->setSubject($subject);
  313. $barPayRequestBuilder->setBody($body);
  314. // 调用barPay方法获取当面付应答
  315. $barPay = new \AlipayTradeService($config);
  316. $barPayResult = $barPay->barPay($barPayRequestBuilder);
  317. if ($barPayResult->getTradeStatus() == 'SUCCESS') {
  318. $respond = $barPayResult->getResponse();
  319. $transactionId = $respond->trade_no ?? '';
  320. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  321. $order->save();
  322. $attach = '';
  323. payUtil::thirdPay($aliPayWay, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  324. $transaction->commit();
  325. $newOrder = OrderClass::getById($id, true);
  326. //打印小票
  327. OrderClass::onlinePrint($newOrder);
  328. //语音播报
  329. ShopExtClass::hdGatheringReport($newOrder);
  330. $shopId = $newOrder->shopId ?? 0;
  331. $shop = ShopClass::getById($shopId, true);
  332. WxMessageClass::gatheringIncomeInform($shop, $newOrder);
  333. util::success(['returnStatus' => 'SUCCESS']);
  334. }
  335. util::success(['returnStatus' => 'FAILURE']);
  336. }
  337. } catch (\Exception $e) {
  338. $transaction->rollBack();
  339. Yii::error("支付失败原因:" . $e->getMessage());
  340. util::fail('支付失败');
  341. }
  342. }
  343. //开单操作 ssh 20220316
  344. public function actionCreateOrder()
  345. {
  346. $post = Yii::$app->request->post();
  347. $post['sjId'] = $this->sjId;
  348. $post['shopId'] = $this->shopId;
  349. $post['mainId'] = $this->mainId ?? 0;
  350. $now = time();
  351. $orderValidTime = getenv('ORDER_VALID_TIME') == false ? 600 : getenv('ORDER_VALID_TIME');
  352. $expireTime = $now + $orderValidTime;
  353. $post['deadline'] = $expireTime;
  354. $post['staffId'] = $this->shopAdminId ?? 0;
  355. $post['staffName'] = $this->shopAdmin->name ?? '';
  356. //默认情况开单员工和收款员工是同个人
  357. $staffId = $this->shopAdminId ?? 0;
  358. $post['shopAdminId'] = $staffId;
  359. if (isset($post['getStaffId']) == false || empty($post['getStaffId'])) {
  360. $post['getStaffId'] = $staffId;
  361. $post['getStaffName'] = $this->shopAdmin->name ?? '';
  362. }
  363. $post['shopAdminName'] = $this->shopAdmin->name ?? '';
  364. $groupId = isset($post['groupId']) && !empty($post['groupId']) ? $post['groupId'] : 0;
  365. $post['fromType'] = $post['fromType'] ?? 1;
  366. if ($post['fromType'] == dict::getDict('fromType', 'friend')) {
  367. if (isset($post['bookName']) == false || empty($post['bookName'])) {
  368. util::fail('请填写订花人姓名,没有填【无】');
  369. }
  370. }
  371. $customId = isset($post['customId']) && $post['customId'] > 0 ? $post['customId'] : 0;
  372. if (empty($customId)) {
  373. $customId = $this->shop->defaultCustomId ?? 0;
  374. }
  375. $custom = CustomClass::getById($customId);
  376. if (empty($custom)) {
  377. util::fail('没有找到客户哦');
  378. }
  379. $post['customId'] = $customId;
  380. if (isset($post['receiveMobile']) && !empty($post['receiveMobile'])) {
  381. if (stringUtil::isMobile($post['receiveMobile']) == false) {
  382. util::fail('请填写正确的收花人手机号');
  383. }
  384. }
  385. if (isset($post['bookMobile']) && !empty($post['bookMobile'])) {
  386. if (stringUtil::isMobile($post['bookMobile']) == false) {
  387. util::fail('请填写正确的订花人手机号');
  388. }
  389. }
  390. $hasPay = $post['hasPay'] ?? dict::getDict('hasPay', 'unPay');
  391. if (isset($post['isCashier']) && $post['isCashier'] == 1) {
  392. //收银台开单默认到店自取
  393. $post['sendType'] = dict::getDict('sendType', 'shopGet');
  394. }
  395. $productJson = $post['product'] ?? '';
  396. $productList = [];
  397. if (!empty($productJson)) {
  398. $productList = json_decode($productJson, true);
  399. }
  400. if (empty($productList) && empty($groupId)) {
  401. //商家手机上开单并生成制作单
  402. if (isset($post['unitGoodsPrice']) && !empty($post['unitGoodsPrice'])) {
  403. $unitGoodsPrice = $post['unitGoodsPrice'];
  404. if (is_numeric($unitGoodsPrice) == false) {
  405. util::fail('请输入正确的单价');
  406. }
  407. $productList = GoodsClass::orderCreateGoods($post);
  408. }
  409. }
  410. if (empty($productList) && empty($groupId)) {
  411. if (isset($post['lsGoodsPrice']) == false || $post['lsGoodsPrice'] <= 0) {
  412. $lsGoodsPrice = $post['lsGoodsPrice'];
  413. if (is_numeric($lsGoodsPrice) == false) {
  414. util::fail('请输入正确的金额');
  415. }
  416. util::fail('请填写金额或选商品');
  417. }
  418. }
  419. $connection = Yii::$app->db;
  420. $transaction = $connection->beginTransaction();
  421. try {
  422. $post['product'] = $productList;
  423. //阿东开5支老是会出现5扎情况跟踪
  424. ProductClass::adStockCheck($this->shop, $productList);
  425. $return = OrderService::createFinishOrder($post, $custom, $hasPay);
  426. $transaction->commit();
  427. //打印小票和语音播报
  428. $id = $return->id ?? 0;
  429. $order = OrderClass::getById($id, true);
  430. if ($order->fromType == 3 || $order->fromType == 4) {
  431. //除了客服号下单和美团下单,其它情况允许打小票
  432. } else {
  433. //前台打小票
  434. OrderClass::onlinePrint($order);
  435. }
  436. //前台提示收款多少钱
  437. ShopExtClass::hdGatheringReport($order);
  438. //前台提醒出示付款码
  439. if (isset($order->status) && $order->status == 1) {
  440. if (isset($post['isCashier']) && $post['isCashier'] == 1) {
  441. ShopExtClass::pleasePayReport($order);
  442. }
  443. }
  444. $shopId = $order->shopId ?? 0;
  445. $shop = ShopClass::getById($shopId, true);
  446. WxMessageClass::gatheringIncomeInform($shop, $order);
  447. //新制作单提示
  448. $shopExt = ShopExtClass::getByCondition(['shopId' => $this->shopId], true);
  449. ShopExtClass::newWorkRemind($shopExt, $order);
  450. util::success($return);
  451. } catch (\Exception $e) {
  452. $transaction->rollBack();
  453. Yii::error("失败原因:" . $e->getMessage());
  454. util::fail('下单失败');
  455. }
  456. }
  457. //下单要用到的相关信息 ssh 2019.12.6
  458. public function actionOrderRelate()
  459. {
  460. $regionTree = RegionService::tree();
  461. $shop = $this->shop->attributes;
  462. $freight = MerchantExtendService::getFreight($this->sjExtend);
  463. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  464. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'txMapKey' => $mapKey, 'merchant' => $shop];
  465. util::success($out);
  466. }
  467. //余额支付 ssh 2019.12.6
  468. public function actionBalancePay()
  469. {
  470. $post = Yii::$app->request->post();
  471. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  472. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  473. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  474. $order = OrderService::getByOrderSn($orderSn);
  475. $userId = $this->adminId;
  476. $user = UserService::getUserInfo($userId);
  477. //验证支付密码是否正确
  478. UserService::validPayPassword($payPassword, $user);
  479. //支付前验证订单有效性
  480. OrderService::checkBeforePay($order);
  481. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  482. $totalFee = $order['prePrice'];
  483. $sourceType = 0;
  484. $discountData = OrderService::getDiscountPrice(['price' => $totalFee, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  485. $actPrice = $discountData['price'];
  486. $callbackParams = [];
  487. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  488. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  489. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'balance' => $balance]);
  490. }
  491. //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
  492. public function actionWxPay()
  493. {
  494. ini_set('date.timezone', 'Asia/Shanghai');
  495. $post = Yii::$app->request->post();
  496. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  497. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  498. $order = OrderService::getByOrderSn($orderSn);
  499. $orderId = $order['id'];
  500. //验证优惠券是否还有效
  501. if (!empty($couponId)) {
  502. CouponService::checkBeforeUse($couponId, $order['prePrice'], $order['userId']);
  503. }
  504. //支付前验证订单有效性
  505. OrderService::checkBeforePay($order);
  506. $name = isset($order['orderName']) && !empty($order['orderName']) ? $order['orderName'] : '购买商品';
  507. $totalFee = $order['realPrice'];
  508. //小程序使用miniOpenId
  509. if (httpUtil::isMiniProgram()) {
  510. $openId = $this->user['miniOpenId'];
  511. } else {
  512. $openId = $this->user['openId'];
  513. }
  514. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  515. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  516. $wxPayType = 0;
  517. if (httpUtil::isMiniProgram()) {
  518. $wxPayType = 1;
  519. }
  520. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  521. //订单30分钟后过期
  522. $now = time();
  523. $expireTime = $now + 1800;
  524. $wx = Yii::getAlias("@vendor/weixin");
  525. require_once($wx . '/lib/WxPay.Api.php');
  526. require_once($wx . '/example/WxPay.JsApiPay.php');
  527. $input = new \WxPayUnifiedOrder();
  528. $input->SetBody($name);
  529. $input->SetOut_trade_no($orderSn);
  530. $input->SetTotal_fee($totalFee * 100);
  531. $input->SetTime_start(date("YmdHis", $now));
  532. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  533. $input->SetTime_expire(date("YmdHis", $expireTime));
  534. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  535. $input->SetTrade_type("JSAPI");
  536. //花卉宝代为申请的微信支付
  537. $merchantExtend = $this->sjExtend->attributes;
  538. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  539. $input->SetSub_openid($openId);
  540. } else {
  541. $input->SetOpenid($openId);
  542. }
  543. //小程序使用miniAppId
  544. if (httpUtil::isMiniProgram()) {
  545. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  546. }
  547. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  548. $tools = new \JsApiPay();
  549. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  550. $newParams = json_decode($jsApiParameters, true);
  551. //已请求微信不能修改价格
  552. $updateData = [];
  553. $updateData['modPrice'] = 0;
  554. if (empty($order['deadline'])) {
  555. $updateData['deadline'] = $expireTime;
  556. }
  557. OrderService::updateById($orderId, $updateData);
  558. util::success($newParams);
  559. }
  560. //快捷付款订单
  561. public function actionFastPay()
  562. {
  563. ini_set('date.timezone', 'Asia/Shanghai');
  564. $post = Yii::$app->request->post();
  565. $payWay = isset($post['payWay']) ? $post['payWay'] : 0;
  566. $shopId = !empty($this->shopId) ? $this->shopId : ShopService::getDefaultShopId($this->sj);
  567. //验证门店是否有效
  568. $shopInfo = ShopService::getById($shopId);
  569. ShopService::valid($shopInfo, $this->sjId);
  570. $post['shopId'] = $shopId;
  571. //默认门店订单
  572. $store = isset($post['store']) ? $post['store'] : 1;
  573. $post['store'] = $store;
  574. //来源 0微信 1支付宝 2小程序 3朋友圈 4美团
  575. $sourceType = $this->isWx ? 0 : 1;
  576. if (httpUtil::isMiniProgram()) {
  577. $sourceType = 2;
  578. }
  579. $sourceType = isset($post['sourceType']) ? $post['sourceType'] : 0;
  580. //兼容旧系统sourceType=3表示朋友圈来源
  581. $fromType = $sourceType == 3 ? 2 : 0;
  582. $fromType = isset($post['fromType']) && !empty($post['fromType']) ? $post['fromType'] : $fromType;
  583. $post['userId'] = $this->adminId;
  584. $custom = $this->custom;
  585. $post['bookName'] = $custom['userName'] ?? '';
  586. $bookMobile = isset($post['bookMobile']) && !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  587. $bookMobile = empty($bookMobile) && isset($custom['mobile']) && !empty($custom['mobile']) ? $custom['mobile'] : $bookMobile;
  588. $post['bookMobile'] = $bookMobile;
  589. $prePrice = round($post['prePrice'], 2);
  590. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  591. $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  592. $actPrice = $discountData['price'];
  593. $post['discountType'] = $discountData['discountType'];
  594. $post['discountAmount'] = $discountData['discountAmount'];
  595. $post['actPrice'] = $actPrice;
  596. $post['realPrice'] = $actPrice;
  597. $post['payStyle'] = 0;
  598. $post['sjId'] = $this->sjId;
  599. $now = time();
  600. $expireTime = $now + 300;//订单5分钟后过期
  601. $post['createTime'] = date("Y-m-d H:i:s", $now);
  602. $post['deadline'] = $expireTime;
  603. if ($actPrice <= 0) {
  604. util::fail('请填写正确的金额');
  605. }
  606. //微信支付订单提交不能修改价格
  607. $post['modPrice'] = $this->isWx ? 0 : 1;
  608. $post['sourceType'] = $sourceType;
  609. $post['goodsNum'] = 1;
  610. $post['fromType'] = $fromType;
  611. $order = OrderService::addOrder($post);
  612. $orderId = $order['id'];
  613. $orderSn = $order['orderSn'];
  614. $sjId = $this->sjId;
  615. if ($payWay == 0) {
  616. $orderId = $order['id'];
  617. $name = '购买商品';
  618. $totalFee = $actPrice;
  619. $user = UserService::getById($this->adminId);
  620. $openId = isset($user['openId']) ? $user['openId'] : 0;
  621. if (httpUtil::isMiniProgram()) {
  622. //小程序使用miniOpenId
  623. $openId = $user['miniOpenId'];
  624. }
  625. if (empty($openId)) {
  626. util::fail('没有找到客户的openId');
  627. }
  628. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  629. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  630. $wxPayType = 0;
  631. if (httpUtil::isMiniProgram()) {
  632. $wxPayType = 1;
  633. }
  634. $attach = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&wxPayType=' . $wxPayType;
  635. $wx = Yii::getAlias("@vendor/weixin");
  636. require_once($wx . '/lib/WxPay.Api.php');
  637. require_once($wx . '/example/WxPay.JsApiPay.php');
  638. $input = new \WxPayUnifiedOrder();
  639. $input->SetBody($name);
  640. $input->SetOut_trade_no($orderSn);
  641. $input->SetTotal_fee($totalFee * 100);
  642. $input->SetTime_start(date("YmdHis", $now));
  643. $input->SetAttach($attach);
  644. //设置订单有效期5分钟
  645. $input->SetTime_expire(date("YmdHis", $expireTime));
  646. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  647. $input->SetTrade_type("JSAPI");
  648. //花卉宝代为申请的微信支付
  649. $merchantExtend = $this->sjExtend->attributes;
  650. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  651. $input->SetSub_openid($openId);
  652. } else {
  653. $input->SetOpenid($openId);
  654. }
  655. //小程序使用miniAppId
  656. if (httpUtil::isMiniProgram()) {
  657. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  658. }
  659. Yii::info('支付发起使用小程序信息' . json_encode($merchantExtend));
  660. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  661. $tools = new \JsApiPay();
  662. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  663. $newParams = json_decode($jsApiParameters, true);
  664. $newParams['orderId'] = $orderId;
  665. util::success($newParams);
  666. } elseif ($payWay == 1) {
  667. $extend = MerchantExtendService::getBySjId($sjId);
  668. if (isset($extend['alipayInit']) == false || $extend['alipayInit'] == 0) {
  669. util::fail('支付宝付款即将开通...');
  670. }
  671. $config = [
  672. //应用ID,您的APPID。
  673. 'app_id' => $extend['alipayPId'],
  674. //商户私钥,您的原始格式RSA私钥
  675. 'merchant_private_key' => $extend['alipayKey'],
  676. //异步通知地址
  677. 'notify_url' => Yii::$app->params['hdHost'] . "/notice/ali-callback",
  678. //同步跳转
  679. 'return_url' => "",
  680. //编码格式
  681. 'charset' => "UTF-8",
  682. //签名方式
  683. 'sign_type' => "RSA2",
  684. //支付宝网关
  685. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  686. //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
  687. 'alipay_public_key' => $extend['alipayPublicKey'],
  688. ];
  689. Yii::info(json_encode($config) . ' aplipay config');
  690. $alipayWap = Yii::getAlias("@vendor/alipayWap");
  691. require_once($alipayWap . '/wappay/service/AlipayTradeService.php');
  692. require_once($alipayWap . '/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php');
  693. $totalFee = $order['realPrice'];
  694. $out_trade_no = $orderSn;//商户订单号,商户网站订单系统中唯一订单号,必填
  695. $subject = '购买商品';//订单名称,必填
  696. $total_amount = $totalFee;//付款金额,必填
  697. $body = '';//商品描述,可空
  698. $timeout_express = "1m";//超时时间
  699. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  700. $passBackParams = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&sjId=' . $sjId;//将流水类型、优惠卷传过去
  701. $passBackParams = urlencode($passBackParams);
  702. $payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
  703. $payRequestBuilder->setBody($body);
  704. $payRequestBuilder->setSubject($subject);
  705. $payRequestBuilder->setOutTradeNo($out_trade_no);
  706. $payRequestBuilder->setTotalAmount($total_amount);
  707. $payRequestBuilder->setTimeExpress($timeout_express);
  708. //在异步通知时将该参数原样返回
  709. $payRequestBuilder->setPassbackParams($passBackParams);
  710. //同步通知
  711. $returnUrl = Yii::$app->params['mallDomain'] . "/#/pages/callback/success?account={$sjId}&shopId={$shopId}&orderSn={$orderSn}&totalPrice={$totalFee}&pageStatus=3&payDiscountPrice=0&payDiscountType=0";
  712. //异步通知
  713. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/ali-callback";
  714. $payResponse = new \AlipayTradeService($config);
  715. $result = $payResponse->wapPay($payRequestBuilder, $returnUrl, $notifyUrl);
  716. //直接将支付宝的html返回给前端
  717. echo $result;
  718. } elseif ($payWay == 2) {
  719. //余额支付,验证支付密码是否正确
  720. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  721. $user = UserService::getUserInfo($this->adminId);
  722. UserService::validPayPassword($payPassword, $user);
  723. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  724. $callbackParams = [];
  725. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  726. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  727. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'orderSn' => $orderSn, 'orderId' => $orderId, 'balance' => $balance]);
  728. } else {
  729. util::fail('无效的支付方式');
  730. }
  731. }
  732. //订单评价
  733. public function actionComment()
  734. {
  735. $post = Yii::$app->request->post();
  736. $id = $post['id'];
  737. $order = OrderService::getById($id);
  738. OrderService::valid($order, $this->mainId);
  739. if ($order['grade'] > 0) {
  740. util::fail('您已经评过了');
  741. }
  742. OrderService::comment($post);
  743. util::complete('提交成功');
  744. }
  745. //订单详情 ssh 2019.12.16
  746. public function actionDetail()
  747. {
  748. $id = Yii::$app->request->get('id', 0);
  749. $detail = OrderClass::getFullInfo($id);
  750. OrderService::valid($detail, $this->mainId);
  751. util::success($detail);
  752. }
  753. public function actionList()
  754. {
  755. $get = Yii::$app->request->get();
  756. $search = isset($get['search']) ? $get['search'] : '';
  757. $shopId = $this->shopId ?? 0;
  758. if (empty($shopId)) {
  759. //没有登录不显示数据
  760. util::success(['list' => [], 'moreData' => 0, 'shop' => [], 'totalNum' => 0, 'totalPage' => 0]);
  761. }
  762. $where = [];
  763. $where['shopId'] = $shopId;
  764. $status = $get['status'] ?? 0;
  765. if (!empty($status)) {
  766. $where['status'] = $get['status'];
  767. }
  768. $debt = $get['debt'] ?? 2;
  769. if ($debt != 2) {
  770. $where['debt'] = $debt;
  771. }
  772. if (isset($get['shopAdminId']) && !empty($get['shopAdminId'])) {
  773. $where['shopAdminId'] = $get['shopAdminId'];
  774. }
  775. if (isset($get['searchText']) && !empty($get['searchText'])) {
  776. if (is_numeric($get['searchText'])) {
  777. $where['sendNum'] = $get['searchText'];
  778. } else {
  779. $where['bookName'] = $get['searchText'];
  780. }
  781. }
  782. if (!empty($search)) {
  783. if (stringUtil::isMobile($search)) {
  784. $where['bookMobile'] = $search;
  785. } else {
  786. $where['orderSn'] = $search;
  787. }
  788. }
  789. $searchTime = $get['searchTime'] ?? '';
  790. if (!empty($searchTime)) {
  791. $startTime = $get['startTime'] ?? '';
  792. $endTime = $get['endTime'] ?? '';
  793. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  794. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  795. }
  796. $list = OrderService::getOrderList($where);
  797. $list['shop'] = $this->shop->attributes ?? [];
  798. util::success($list);
  799. }
  800. //订单归类 ssh 2019.12.17
  801. public function actionClassify()
  802. {
  803. $post = Yii::$app->request->post();
  804. $id = isset($post['id']) ? $post['id'] : 0;
  805. $order = OrderService::getById($id);
  806. OrderService::valid($order, $this->mainId);
  807. $categoryId = isset($post['categoryId']) ? $post['categoryId'] : $post['categoryId'];
  808. $usageId = isset($post['usageId']) ? $post['usageId'] : $post['usageId'];
  809. OrderService::classify($id, $categoryId, $usageId);
  810. util::complete();
  811. }
  812. //更新配送单 ssh 2019.12.17
  813. public function actionUpdateSheet()
  814. {
  815. $post = Yii::$app->request->post();
  816. $id = isset($post['id']) ? $post['id'] : 0;
  817. unset($post['id']);
  818. $order = OrderService::getById($id);
  819. OrderService::valid($order, $this->mainId);
  820. OrderService::updateSheet($order, $post);
  821. util::complete('提交成功');
  822. }
  823. //商家收款与发货 ssh 2019.12.18
  824. public function actionGathering()
  825. {
  826. $post = Yii::$app->request->post();
  827. $price = isset($post['price']) && is_numeric($post['price']) ? $post['price'] : 0;
  828. $payWay = isset($post['payWay']) && is_numeric($post['payWay']) ? $post['payWay'] : 0;
  829. $userId = isset($post['userId']) ? $post['userId'] : 0;
  830. $userInfo = UserService::getById($userId);
  831. if (empty($userInfo) || $userInfo['sjId'] != $this->sjId) {
  832. util::fail('您选择的客户无效');
  833. }
  834. if ($price <= 0) {
  835. util::fail('请输入金额');
  836. }
  837. $post['prePrice'] = $price;
  838. $post['actPrice'] = $price;
  839. $post['payWay'] = $payWay;
  840. $post['sourceType'] = 1;
  841. $post['userId'] = $userId;
  842. $post['goodsNum'] = 1;
  843. $post['orderName'] = '商品';
  844. $shopId = MerchantService::getDefaultShopId($this->sj);
  845. $post['shopId'] = $shopId;
  846. $order = OrderService::addOrder($post);
  847. $id = $order['id'];
  848. $orderSn = $order['orderSn'];
  849. //流水类型列表
  850. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  851. $callbackParams = [];
  852. switch ($payWay) {
  853. case 0:
  854. xhPayToolService::wxPay($callbackParams, $orderSn, $price, $capitalType, 0);
  855. break;
  856. case 1:
  857. xhPayToolService::alipay($callbackParams, $orderSn, $price, $capitalType, 0, []);
  858. break;
  859. case 2:
  860. xhPayToolService::balancePay($callbackParams, $orderSn, $price, $capitalType, 0);
  861. break;
  862. default:
  863. util::fail('无效支付方式');
  864. }
  865. util::success($order);
  866. }
  867. //免发货管理 ssh 2020.1.6
  868. public function actionWithoutSend()
  869. {
  870. $id = Yii::$app->request->get('id');
  871. $order = OrderService::getById($id);
  872. OrderService::valid($order, $this->mainId);
  873. //配送流程变更
  874. $currentFlow = OrderSendClass::withoutSend($order);
  875. util::success(['currentFlow' => $currentFlow]);
  876. }
  877. //修改价格 ssh 2020.1.6
  878. public function actionUpdatePrice()
  879. {
  880. $id = Yii::$app->request->get('id');
  881. $price = Yii::$app->request->get('price', 1);
  882. $order = OrderService::getById($id);
  883. OrderService::valid($order, $this->mainId);
  884. if (isset($order['modPrice']) && $order['modPrice'] == 0) {
  885. util::fail('已发起支付,不能修改价格');
  886. }
  887. OrderService::updateById($id, ['actPrice' => $price]);
  888. util::complete('修改成功');
  889. }
  890. //配送单 ssh 2020.2.29
  891. public function actionGetDeliverDetail()
  892. {
  893. $id = Yii::$app->request->get('id', 0);
  894. $order = OrderService::getById($id);
  895. OrderService::valid($order, $this->mainId);
  896. $reachDate = isset($order['reachDate']) && !empty($order['reachDate']) ? $order['reachDate'] : '';
  897. $reachPeriodId = $order['reachPeriod'];
  898. $reachPeriodArr = [0 => '上午', 1 => '下午', 2 => '晚上'];
  899. $reachPeriod = isset($reachPeriodArr[$reachPeriodId]) ? $reachPeriodArr[$reachPeriodId] : '';
  900. $bookName = isset($order['bookName']) ? $order['bookName'] : '';
  901. $bookMobile = isset($order['bookMobile']) ? $order['bookMobile'] : '';
  902. if (isset($order['anonymity']) && $order['anonymity'] == 1) {
  903. $bookName = '--';
  904. $bookMobile = '--';
  905. }
  906. $data = [
  907. 'reachDate' => $reachDate . ' ' . $reachPeriod,
  908. 'orderSn' => $order['orderSn'],
  909. 'receiveUserName' => $order['receiveUserName'],
  910. 'receiveMobile' => $order['receiveMobile'],
  911. 'fullAddress' => $order['fullAddress'] . "(" . $order['showAddress'] . ")",
  912. 'cardInfo' => $order['cardInfo'],
  913. 'bookMobile' => $bookMobile,
  914. 'bookName' => $bookName,
  915. 'remark' => $order['remark'],
  916. 'sendNum' => $order['sendNum'],
  917. 'telephone' => 18030142050,
  918. 'printTime' => '',
  919. 'img' => '',
  920. ];
  921. util::success($data);
  922. }
  923. //运费计算 lqh 2021.4.12 暂反回固定
  924. public function actionFreight()
  925. {
  926. util::success(['sedCost' => 10]);
  927. }
  928. }