OrderController.php 53 KB

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