OrderController.php 53 KB

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