OrderController.php 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  1. <?php
  2. namespace hd\controllers;
  3. use Yii;
  4. use biz\shop\classes\ShopExtClass;
  5. use biz\sj\services\MerchantExtendService;
  6. use biz\sj\services\MerchantService;
  7. use bizHd\custom\classes\CustomClass;
  8. use bizHd\custom\classes\HdClass;
  9. use bizHd\goods\classes\GoodsClass;
  10. use bizHd\merchant\services\ShopService;
  11. use bizHd\order\classes\OrderClass;
  12. use bizHd\order\classes\OrderForwardClass;
  13. use bizHd\order\classes\OrderItemClass;
  14. use bizHd\order\classes\OrderSendClass;
  15. use bizHd\order\services\OrderService;
  16. use bizHd\product\classes\ProductClass;
  17. use bizHd\saas\services\RegionService;
  18. use bizHd\shop\classes\ShopClass;
  19. use bizHd\user\services\UserService;
  20. use bizHd\work\classes\WorkClass;
  21. use common\components\dict;
  22. use common\components\dateUtil;
  23. use common\components\noticeUtil;
  24. use common\components\orderSn;
  25. use common\components\payUtil;
  26. use common\services\xhPayToolService;
  27. use common\components\util;
  28. use common\components\stringUtil;
  29. use bizHd\promote\services\CouponService;
  30. use common\components\httpUtil;
  31. use biz\wx\classes\WxMessageClass;
  32. use common\components\lakala\Lakala;
  33. class OrderController extends BaseController
  34. {
  35. public $guestAccess = ['order-relate', 'fast-pay', 'create-order', 'list'];
  36. public function actionDebt()
  37. {
  38. $get = Yii::$app->request->get();
  39. $id = $get['id'] ?? 0;
  40. $info = OrderClass::getById($id, true);
  41. if (empty($info)) {
  42. util::fail('没有找到订单');
  43. }
  44. if ($info->mainId != $this->mainId) {
  45. util::fail('不是你的订单');
  46. }
  47. //4秒内不允许重复提交
  48. util::checkRepeatCommit($id, 4);
  49. if ($info->status != 1) {
  50. util::fail('订单已付款或取消');
  51. }
  52. $payWay = dict::getDict('payWay', 'debtPay');
  53. $connection = Yii::$app->db;
  54. $transaction = $connection->beginTransaction();
  55. try {
  56. $actPrice = $info->actPrice ?? 0;
  57. $info->debtPrice = $actPrice;
  58. $info->remainDebtPrice = $actPrice;
  59. $info->debt = 1;
  60. $info->save();
  61. OrderClass::payAfter($info, $payWay);
  62. $transaction->commit();
  63. util::complete('赊账成功');
  64. } catch (\Exception $exception) {
  65. $transaction->rollBack();
  66. Yii::info("赊账失败原因:" . $exception->getMessage());
  67. util::fail('操作失败');
  68. }
  69. }
  70. //取消订单 ssh 20221231
  71. public function actionCancel()
  72. {
  73. //避免重复提交
  74. $adminId = $this->adminId;
  75. util::checkRepeatCommit($adminId, 8);
  76. $get = Yii::$app->request->get();
  77. $id = $get['id'] ?? 0;
  78. $order = OrderClass::getById($id, true);
  79. OrderClass::valid($order, $this->mainId);
  80. $deadTime = $order->deadline ?? 0;
  81. $current = time();
  82. $diff = bcsub($deadTime, $current);
  83. if ($diff < 60) {
  84. util::fail('60秒后将自动取消');
  85. }
  86. $connection = Yii::$app->db;
  87. $transaction = $connection->beginTransaction();
  88. try {
  89. OrderClass::setExpire($order, true);
  90. $transaction->commit();
  91. util::complete();
  92. } catch (\Exception $exception) {
  93. $transaction->rollBack();
  94. Yii::info("取消原因:" . $exception->getMessage());
  95. util::fail('取消失败');
  96. }
  97. }
  98. //修改订单 ssh 20220926
  99. public function actionUpdate()
  100. {
  101. $post = Yii::$app->request->post();
  102. $id = $post['id'] ?? 0;
  103. $order = OrderClass::getById($id, true);
  104. OrderClass::valid($order, $this->mainId);
  105. unset($post['id']);
  106. if ($order->status == 3) {
  107. util::fail('订单已配送');
  108. }
  109. if ($order->status == 4) {
  110. util::fail('订单已完成');
  111. }
  112. if ($order->status == 5) {
  113. util::fail('订单已取消');
  114. }
  115. if ($order->fromType == 4) {
  116. util::fail('请在美团APP上修改');
  117. }
  118. $address = $post['address'] ?? '';
  119. $floor = $post['floor'] ?? '';
  120. $post['fullAddress'] = $address . $floor;
  121. OrderClass::updateById($id, $post);
  122. $workList = WorkClass::getAllByCondition(['orderId' => $id], null, '*', null, true);
  123. $remark = $post['remark'] ?? '';
  124. $reachDate = $post['reachDate'] ?? '';
  125. $reachPeriod = $post['reachPeriod'] ?? '';
  126. $cardInfo = $post['cardInfo'] ?? '';
  127. $anonymity = $post['anonymity'] ?? 0;
  128. $bookName = $post['bookName'] ?? '';
  129. if (!empty($workList)) {
  130. foreach ($workList as $work) {
  131. $work->remark = $remark;
  132. $work->reachDate = $reachDate;
  133. $work->reachPeriod = $reachPeriod;
  134. $work->cardInfo = $cardInfo;
  135. $work->anonymity = $anonymity;
  136. $work->bookName = $bookName;
  137. $work->save();
  138. }
  139. }
  140. $shopExt = ShopExtClass::getByCondition(['shopId' => $this->shopId], true);
  141. ShopExtClass::orderUpdateRemind($shopExt, $order);
  142. util::complete();
  143. }
  144. //客户欠款的订单 ssh 2021.2.4
  145. public function actionDebtList()
  146. {
  147. $get = Yii::$app->request->get();
  148. $id = $get['id'] ?? 0;
  149. $info = CustomClass::getById($id, true);
  150. CustomClass::valid($info, $this->shopId);
  151. $where = ['customId' => $id, 'debt' => 1];
  152. $searchTime = $get['searchTime'] ?? '';
  153. if (!empty($searchTime)) {
  154. $startTime = $get['startTime'] ?? '';
  155. $endTime = $get['endTime'] ?? '';
  156. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  157. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  158. }
  159. $list = OrderClass::getAllByCondition($where, 'addTime DESC', ['id', 'orderSn', 'actPrice', 'addTime', 'remainDebtPrice', 'debtPrice']);
  160. if (!empty($list)) {
  161. foreach ($list as $key => $value) {
  162. $debtPrice = $value['debtPrice'] ?? 0;
  163. $remainDebtPrice = $value['remainDebtPrice'] ?? 0;
  164. $hasPayDebt = 0;
  165. if ($debtPrice > $remainDebtPrice) {
  166. $hasPayDebt = bcsub($debtPrice, $remainDebtPrice, 2);
  167. }
  168. $list[$key]['hasPayDebt'] = floatval($hasPayDebt);
  169. }
  170. }
  171. $result = ['customInfo' => $info, 'list' => $list];
  172. util::success($result);
  173. }
  174. //确认送达 ssh 20250902
  175. public function actionReach()
  176. {
  177. $get = Yii::$app->request->get();
  178. $id = $get['id'] ?? 0;
  179. $order = OrderClass::getById($id, true);
  180. if (empty($order)) {
  181. util::fail('没有找到订单');
  182. }
  183. OrderClass::valid($order, $this->mainId);
  184. if ($order->status == 5) {
  185. util::fail('订单已取消');
  186. }
  187. if ($order->status == 1) {
  188. util::fail('订单待付款');
  189. }
  190. if ($order->status == 2) {
  191. util::fail('订单未发货');
  192. }
  193. if ($order->status == 4) {
  194. util::fail('订单已经送到了');
  195. }
  196. if ($order->sendStatus == 1 || $order->sendStatus == 2) {
  197. util::fail('由跑腿送货,无需确认');
  198. }
  199. $order->status = 4;
  200. $order->save();
  201. util::complete('操作成功');
  202. }
  203. //确认取货
  204. public function actionFetch()
  205. {
  206. $get = Yii::$app->request->get();
  207. $id = $get['id'] ?? 0;
  208. $order = OrderClass::getById($id, true);
  209. if (empty($order)) {
  210. util::fail('没有找到订单');
  211. }
  212. OrderClass::valid($order, $this->mainId);
  213. if ($order->status == 5) {
  214. util::fail('订单已取消');
  215. }
  216. if ($order->status == 1) {
  217. util::fail('订单待付款');
  218. }
  219. if ($order->status == 3 || $order->status == 4) {
  220. util::fail('订单已取过了');
  221. }
  222. $order->status = 4;
  223. $order->save();
  224. util::complete('操作成功');
  225. }
  226. //确认发货
  227. public function actionSend()
  228. {
  229. $get = Yii::$app->request->get();
  230. $id = isset($get['id']) ? $get['id'] : 0;
  231. $order = OrderClass::getById($id, true);
  232. if (empty($order)) {
  233. util::fail('没有找到订单');
  234. }
  235. OrderClass::valid($order, $this->mainId);
  236. if ($order->status != 2) {
  237. util::fail('订单不是待发货状态');
  238. }
  239. // $workList = WorkClass::getAllByCondition(['orderId' => $id], null, '*', null, true);
  240. // if (!empty($workList)) {
  241. // $unComplete = false;
  242. // foreach ($workList as $work) {
  243. // if ($work->status != 1) {
  244. // $unComplete = true;
  245. // }
  246. // }
  247. // if ($unComplete == true) {
  248. // util::fail('还有制作单没有完成');
  249. // }
  250. // }
  251. $order->status = 3;
  252. $order->save();
  253. util::complete('已发货');
  254. }
  255. //打印订单 ssh 20220601
  256. public function actionPrintOrder()
  257. {
  258. $get = Yii::$app->request->get();
  259. $id = isset($get['id']) ? $get['id'] : 0;
  260. $order = OrderClass::getById($id, true);
  261. if (empty($order)) {
  262. util::fail('没有找到订单');
  263. }
  264. if ($order->forward == 1) {
  265. util::fail('售后付款单不能打印');
  266. }
  267. OrderClass::valid($order, $this->mainId);
  268. OrderClass::onlinePrint($order, true);
  269. util::complete();
  270. }
  271. //微信和支付宝付款码支付复查 ssh 20220422
  272. public function actionCodePayCheck()
  273. {
  274. ini_set('date.timezone', 'Asia/Shanghai');
  275. header("Content-type: text/html; charset=utf-8");
  276. $get = Yii::$app->request->get();
  277. $id = isset($get['id']) ? $get['id'] : 0;
  278. $order = OrderClass::getById($id, true);
  279. OrderClass::valid($order, $this->mainId);
  280. $orderSn = $order->orderSn ?? '';
  281. $totalFee = $order->mainPay ?? 0;
  282. if ($order->status == OrderClass::ORDER_STATUS_COMPLETE) {
  283. util::success(['returnStatus' => 'SUCCESS']);
  284. }
  285. if ($order->status == OrderClass::ORDER_STATUS_CANCEL) {
  286. util::complete('订单已取消');
  287. }
  288. if ($order->status != OrderClass::ORDER_STATUS_UN_PAY) {
  289. util::complete('订单不是待付款状态');
  290. }
  291. $connection = Yii::$app->db;
  292. $transaction = $connection->beginTransaction();
  293. try {
  294. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  295. $shop = $this->shop;
  296. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  297. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  298. $params = [
  299. 'appid' => 'OP00002119',
  300. 'serial_no' => '018b08cfddbd',
  301. 'merchant_no' => $shop->lklSjNo,
  302. 'term_no' => $shop->lklScanTermNo,
  303. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  304. 'lklCertificatePath' => $lklCertificatePath,
  305. ];
  306. $laResource = new Lakala($params);
  307. $scanParams = ['orderSn' => $orderSn,];
  308. $response = $laResource->query($scanParams);
  309. if (isset($response['code']) && $response['code'] == 'BBS00000') {
  310. if (isset($response['resp_data']['trade_state']) && $response['resp_data']['trade_state'] == 'SUCCESS') {
  311. $payWayType = dict::getDict('payWay', 'wxPay');
  312. $account_type = $response['resp_data']['account_type'] ?? '';
  313. if ($account_type == 'WECHAT') {
  314. $payWayType = dict::getDict('payWay', 'wxPay');
  315. }
  316. if ($account_type == 'ALIPAY') {
  317. $payWayType = dict::getDict('payWay', 'alipay');
  318. }
  319. $transactionId = $response['resp_data']['trade_no'] ?? '';
  320. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  321. $order->save();
  322. $attach = '';
  323. payUtil::thirdPay($payWayType, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  324. $transaction->commit();
  325. //打印小票和语音播报
  326. $newOrder = OrderClass::getById($id, true);
  327. OrderClass::onlinePrint($newOrder);
  328. ShopExtClass::hdGatheringReport($newOrder);
  329. $shopId = $newOrder->shopId ?? 0;
  330. $shop = ShopClass::getById($shopId, true);
  331. WxMessageClass::gatheringIncomeInform($shop, $newOrder);
  332. util::success(['returnStatus' => 'SUCCESS']);
  333. } else {
  334. util::complete('未知状态..');
  335. }
  336. }
  337. util::complete('未知状态..');
  338. } catch (\Exception $e) {
  339. $transaction->rollBack();
  340. util::complete('支付失败');
  341. }
  342. }
  343. //微信和支付宝付款码支付 ssh 2021.4.11
  344. public function actionCodePay()
  345. {
  346. ini_set('date.timezone', 'Asia/Shanghai');
  347. header("Content-type: text/html; charset=utf-8");
  348. $get = Yii::$app->request->get();
  349. $id = isset($get['id']) ? $get['id'] : 0;
  350. $version = $get['version'] ?? 0;
  351. $order = OrderClass::getById($id, true);
  352. OrderClass::valid($order, $this->mainId);
  353. if ($order->status == OrderClass::ORDER_STATUS_CANCEL) {
  354. $msg = '订单已取消';
  355. util::success(['returnStatus' => 'FAILURE'], $msg);
  356. }
  357. if ($order->status != OrderClass::ORDER_STATUS_UN_PAY) {
  358. $msg = '订单不是待付款状态';
  359. util::success(['returnStatus' => 'FAILURE'], $msg);
  360. }
  361. $shop = $this->shop;
  362. $sj = $shop->merchantName;
  363. $shopName = $shop->shopName;
  364. $name = $shopName == '首店' ? $sj : $sj . '-' . $shopName;
  365. $authCode = (string)$get['authCode'] ?? '';
  366. if ($version > 0) {
  367. if (empty($authCode)) {
  368. $msg = '没有扫到付款码';
  369. $remind = $msg . "(零售端付款码:{$authCode})" . '(' . $name . ')';
  370. noticeUtil::push($remind . ' 编号005', '15280215347');
  371. util::success(['returnStatus' => 'FAILURE'], $msg);
  372. }
  373. //微信文档说明 https://pay.weixin.qq.com/doc/v2/merchant/4011936234
  374. $wxPattern = '/^(10|11|12|13|14|15)\d{16}$/';
  375. //支付宝文档说明 https://opendocs.alipay.com/open/194/106039/
  376. $aliPattern = '/^(25|26|27|28|29|30)\d{14,22}$/';
  377. if (!preg_match($wxPattern, $authCode) && !preg_match($aliPattern, $authCode)) {
  378. $msg = '付款码错误,请使用微信或支付宝付款码';
  379. $remind = $msg . "(零售端付款码:{$authCode})" . '(' . $name . ')';
  380. //noticeUtil::push($remind . ' 编号009', '15280215347');
  381. util::success(['returnStatus' => 'FAILURE'], $msg);
  382. }
  383. } else {
  384. if (empty($authCode)) {
  385. util::fail('没有获取到支付码');
  386. }
  387. }
  388. $orderSn = $order->orderSn ?? '';
  389. $lsCodePayErrorKey = 'ls_code_pay_error_' . $id;
  390. $hasFailPay = Yii::$app->redis->executeCommand('GET', [$lsCodePayErrorKey]);
  391. //前面有付失败过,要重新生成单号
  392. if (!empty($hasFailPay)) {
  393. $oldOrderSn = $order->orderSn;
  394. $orderSn = orderSn::getOrderSn();
  395. $order->orderSn = $orderSn;
  396. $order->save();
  397. OrderItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $orderSn]);
  398. }
  399. $subject = '购买花材 ' . $orderSn;
  400. $totalFee = $order->mainPay ?? 0;
  401. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  402. $connection = Yii::$app->db;
  403. $transaction = $connection->beginTransaction();
  404. try {
  405. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  406. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  407. $params = [
  408. 'appid' => 'OP00002119',
  409. 'serial_no' => '018b08cfddbd',
  410. 'merchant_no' => $shop->lklSjNo,
  411. 'term_no' => $shop->lklScanTermNo,
  412. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  413. 'lklCertificatePath' => $lklCertificatePath,
  414. ];
  415. $laResource = new Lakala($params);
  416. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/pay-callback";
  417. $scanParams = [
  418. 'orderSn' => $orderSn,
  419. 'amount' => $totalFee,
  420. 'capitalType' => $capitalType,
  421. 'notifyUrl' => $notifyUrl,
  422. 'subject' => $subject,
  423. 'authCode' => $authCode,
  424. ];
  425. $response = $laResource->scanPay($scanParams);
  426. if (isset($response['code']) && $response['code'] == 'BBS00000') {
  427. $account_type = $response['resp_data']['account_type'] ?? '';
  428. $payWayType = dict::getDict('payWay', 'wxPay');
  429. if ($account_type == 'WECHAT') {
  430. $payWayType = dict::getDict('payWay', 'wxPay');
  431. }
  432. if ($account_type == 'ALIPAY') {
  433. $payWayType = dict::getDict('payWay', 'alipay');
  434. }
  435. $order->payWay = $payWayType;
  436. $transactionId = $response['resp_data']['trade_no'] ?? '';
  437. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  438. $order->save();
  439. $attach = '';
  440. payUtil::thirdPay($payWayType, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  441. $transaction->commit();
  442. //解决重复通知
  443. $cacheKey = 'hd_shop_order_pay_' . $orderSn;
  444. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  445. if (empty($has)) {
  446. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 300, 'has']);
  447. $newOrder = OrderClass::getById($id, true);
  448. //打印小票
  449. OrderClass::onlinePrint($newOrder);
  450. //语音播报
  451. ShopExtClass::hdGatheringReport($newOrder);
  452. $shopId = $newOrder->shopId ?? 0;
  453. $shop = ShopClass::getById($shopId, true);
  454. WxMessageClass::gatheringIncomeInform($shop, $newOrder);
  455. }
  456. util::success(['returnStatus' => 'SUCCESS']);
  457. } else {
  458. //标记有没付成功的次数,原因有可能是余额不足等
  459. Yii::$app->redis->executeCommand('SETEX', [$lsCodePayErrorKey, 1200, 'hasFail']);
  460. $msg = $response['msg'] ?? '';
  461. $retCode = $response['code'] ?? '';
  462. $fullMsg = $msg . "(零售端付款码:{$authCode})" . '(' . $name . ')';
  463. if ($retCode == 'BBS10000' || $retCode == 'BBS11105') {
  464. $shopId = $order->shopId;
  465. ShopExtClass::remindInputPassword($shopId);
  466. }
  467. //noticeUtil::push($fullMsg . ' ' . $retCode . ' 编号008', '15280215347');
  468. util::success(['returnStatus' => 'FAILURE', 'returnCode' => $retCode], $msg);
  469. }
  470. } catch (\Exception $e) {
  471. $transaction->rollBack();
  472. util::fail('支付失败');
  473. }
  474. }
  475. //开单操作 ssh 20220316
  476. public function actionCreateOrder()
  477. {
  478. $post = Yii::$app->request->post();
  479. $post['sjId'] = $this->sjId;
  480. $post['shopId'] = $this->shopId;
  481. $post['mainId'] = $this->mainId ?? 0;
  482. $shop = $this->shop;
  483. $now = time();
  484. $orderValidTime = getenv('ORDER_VALID_TIME') == false ? 600 : getenv('ORDER_VALID_TIME');
  485. $expireTime = $now + $orderValidTime;
  486. $post['deadline'] = $expireTime;
  487. $post['staffId'] = $this->shopAdminId ?? 0;
  488. $post['staffName'] = $this->shopAdmin->name ?? '';
  489. $post['flowerNum'] = isset($post['flowerNum']) && $post['flowerNum'] > 0 ? $post['flowerNum'] : 0;
  490. $version = $post['version'] ?? 0;
  491. if ($version < 3) {
  492. util::fail('请升级版本,或用小程序');
  493. }
  494. //默认情况开单员工和收款员工是同个人
  495. $staffId = $this->shopAdminId ?? 0;
  496. $staffName = $this->shopAdmin->name ?? '';
  497. if (empty($post['shopAdminId'])) {
  498. $post['shopAdminId'] = $staffId;
  499. $post['shopAdminName'] = $staffName;
  500. }
  501. if (empty($post['getStaffId'])) {
  502. $post['getStaffId'] = $staffId;
  503. $post['getStaffName'] = $this->shopAdmin->name ?? '';
  504. }
  505. $groupId = !empty($post['groupId']) ? $post['groupId'] : 0;
  506. $post['fromType'] = $post['fromType'] ?? 1;
  507. if ($post['fromType'] == dict::getDict('fromType', 'friend')) {
  508. if (empty($post['bookName'])) {
  509. util::fail('请填写订花人姓名');
  510. }
  511. }
  512. $customId = $post['customId'] ?? 0;
  513. $custom = CustomClass::getById($customId, true);
  514. if (empty($custom)) {
  515. util::fail('没有找到客户哦');
  516. }
  517. $hdId = $custom->hdId ?? 0;
  518. $hd = HdClass::getById($hdId, true);
  519. if (empty($hd)) {
  520. util::fail('客户对应的花店信息缺失,编号869');
  521. }
  522. $hdName = $hd->name ?? '';
  523. $post['hdId'] = $hdId;
  524. $post['hdName'] = $hdName;
  525. $customName = $custom->name ?? '';
  526. $post['customName'] = $customName;
  527. $post['customNamePy'] = stringUtil::py($customName);
  528. if (!empty($post['receiveMobile'])) {
  529. if (!stringUtil::isMobile($post['receiveMobile'])) {
  530. util::fail('请填写正确的收花人手机号');
  531. }
  532. }
  533. if (!empty($post['bookMobile'])) {
  534. if (!stringUtil::isMobile($post['bookMobile'])) {
  535. util::fail('请填写正确的订花人手机号');
  536. }
  537. }
  538. $hasPay = $post['hasPay'] ?? dict::getDict('hasPay', 'unPay');
  539. if (isset($post['isCashier']) && $post['isCashier'] == 1) {
  540. //收银台开单默认到店自取
  541. $post['sendType'] = dict::getDict('sendType', 'shopGet');
  542. }
  543. $forward = $post['forward'] ?? 0;
  544. if ($forward == 1) {
  545. if ($hasPay != 1) {
  546. util::fail('售后付款单只能走线下转账');
  547. }
  548. } else {
  549. //如果不是售后付款单,并且选的不是余额支付,余额又够付当前订单,则自动切到余额支付
  550. $balance = $custom->balance ?? 0;
  551. $modifyPrice = $post['modifyPrice'] ?? 0;
  552. if ($hasPay != 4 && $balance > $modifyPrice) {
  553. $post['hasPay'] = 4;
  554. $hasPay = 4;
  555. }
  556. }
  557. $productJson = $post['product'] ?? '';
  558. $productList = [];
  559. if (!empty($productJson)) {
  560. $productList = json_decode($productJson, true);
  561. }
  562. if (empty($productList) && empty($groupId)) {
  563. //商家手机上开单并生成制作单
  564. if (!empty($post['unitGoodsPrice'])) {
  565. $unitGoodsPrice = $post['unitGoodsPrice'];
  566. if (!is_numeric($unitGoodsPrice)) {
  567. util::fail('请输入正确的单价');
  568. }
  569. $productList = GoodsClass::orderCreateGoods($post);
  570. }
  571. }
  572. if (empty($productList) && empty($groupId)) {
  573. if ($post['lsGoodsPrice'] <= 0) {
  574. $lsGoodsPrice = $post['lsGoodsPrice'];
  575. if (!is_numeric($lsGoodsPrice)) {
  576. util::fail('请输入正确的金额');
  577. }
  578. util::fail('请填写金额或选商品');
  579. }
  580. }
  581. $connection = Yii::$app->db;
  582. $transaction = $connection->beginTransaction();
  583. try {
  584. $post['product'] = $productList;
  585. //解决重复提交
  586. util::checkRepeatCommit($this->adminId, 2);
  587. $reduceStock = $post['reduceStock'] ?? 0;
  588. if ($reduceStock == 1) {
  589. //如果扣除库,则走另外一条路
  590. $return = OrderClass::reduceMyStock($post, $this->shop, $custom);
  591. $transaction->commit();
  592. util::success($return);
  593. }
  594. //解决开单提交订单时,别人修改价格,造成提交价格不是真实卖价问题!!
  595. $version = $post['version'] ?? 0;
  596. if ($version == 2) {
  597. $dealPrice = $post['dealPrice'] ?? 0;
  598. if ($dealPrice == 0) {
  599. $productIds = [];
  600. $hsIds = [];
  601. foreach ($productList as $pv) {
  602. $property = $pv['property'] ?? 0;
  603. $puId = $pv['productId'] ?? 0;
  604. if ($property == 0) {
  605. $hsIds[] = $puId;
  606. } else {
  607. $productIds[] = $puId;
  608. }
  609. }
  610. $itemInfo = [];
  611. if (!empty($productIds)) {
  612. $itemInfo = ProductClass::getByIds($productIds, null, 'id');
  613. }
  614. $hsInfo = [];
  615. if (!empty($hsIds)) {
  616. $hsInfo = GoodsClass::getByIds($hsIds, null, 'id');
  617. }
  618. $diff = [];
  619. $priceMap = \bizGhs\custom\classes\CustomClass::$levelPriceKeyMap;
  620. $addPriceMap = \bizGhs\custom\classes\CustomClass::$levelAddPriceKeyMap;
  621. $level = 0;
  622. foreach ($productList as $currentProduct) {
  623. $property = $currentProduct['property'] ?? 0;
  624. $currentPrice = $currentProduct['unitPrice'] ?? 0;
  625. $currentId = $currentProduct['productId'] ?? 0;
  626. if ($property == 1) {
  627. $systemInfo = $itemInfo[$currentId] ?? [];
  628. if (!empty($systemInfo)) {
  629. $name = $systemInfo['name'] ?? '';
  630. $systemPrice = \bizGhs\product\classes\ProductClass::getFinalPrice($systemInfo, $level, $priceMap, $addPriceMap);
  631. if (floatval($currentPrice) != floatval($systemPrice)) {
  632. $diff[] = [
  633. 'name' => $name,
  634. 'price' => $systemPrice,
  635. 'kdPrice' => $currentPrice,
  636. ];
  637. }
  638. }
  639. } else {
  640. $systemInfo = $hsInfo[$currentId] ?? [];
  641. if (!empty($systemInfo)) {
  642. $params = [];
  643. $name = $systemInfo['name'] ?? '';
  644. $hsData = GoodsClass::getFinalPrice($systemInfo, $shop, $custom, $params);
  645. $systemPrice = $hsData['price'] ?? 0;
  646. if ($systemInfo['priceType'] == 1) {
  647. if (floatval($currentPrice) != floatval($systemPrice)) {
  648. $diff[] = [
  649. 'name' => $name,
  650. 'price' => $systemPrice,
  651. 'kdPrice' => $currentPrice,
  652. ];
  653. }
  654. }
  655. }
  656. }
  657. }
  658. if (!empty($diff)) {
  659. util::success(['diff' => $diff, 'respondType' => 'priceError']);
  660. }
  661. }
  662. }
  663. //多处调用这个方法,注意同步修改,搜索关键词 createHdNewOrder
  664. $return = OrderService::createHdOrder($post, $custom, $hasPay);
  665. $shop = $this->shop;
  666. $staff = $this->shopAdmin;
  667. //售后开付款单,库存要变化,关系要建立,多处需要同步修改 sh_pay_change ssh
  668. OrderClass::shPayChange($return, $shop, $staff);
  669. $transaction->commit();
  670. //打印小票和语音播报
  671. $id = $return->id ?? 0;
  672. $order = OrderClass::getById($id, true);
  673. if ($order->fromType == 3 || $order->fromType == 4) {
  674. //除了客服号下单和美团下单,其它情况允许打小票
  675. } else {
  676. //前台打小票
  677. OrderClass::onlinePrint($order);
  678. }
  679. //前台提示收款多少钱
  680. ShopExtClass::hdGatheringReport($order);
  681. //前台提醒出示付款码
  682. if (isset($order->status) && $order->status == 1) {
  683. if (isset($post['isCashier']) && $post['isCashier'] == 1) {
  684. ShopExtClass::pleasePayReport($order);
  685. }
  686. }
  687. $shopId = $order->shopId ?? 0;
  688. $shop = ShopClass::getById($shopId, true);
  689. WxMessageClass::gatheringIncomeInform($shop, $order);
  690. //如果需要生成制作单
  691. $needWork = $post['needWork'] ?? 0;
  692. if ($needWork == 1) {
  693. $main = $this->main;
  694. $shopExt = ShopExtClass::getByCondition(['shopId' => $this->shopId], true);
  695. WorkClass::toDoWork($order, $main, $shopExt);
  696. }
  697. util::success($return);
  698. } catch (\Exception $e) {
  699. $transaction->rollBack();
  700. Yii::error("失败原因:" . $e->getMessage());
  701. util::fail('下单失败');
  702. }
  703. }
  704. //下单要用到的相关信息 ssh 2019.12.6
  705. public function actionOrderRelate()
  706. {
  707. $regionTree = RegionService::tree();
  708. $shop = $this->shop->attributes;
  709. $freight = MerchantExtendService::getFreight($this->sjExtend);
  710. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  711. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'thirdMapKey' => $mapKey, 'merchant' => $shop];
  712. util::success($out);
  713. }
  714. //余额支付 ssh 2019.12.6
  715. public function actionBalancePay()
  716. {
  717. $post = Yii::$app->request->post();
  718. $orderSn = $post['orderSn'] ?? 0;
  719. $payPassword = $post['payPassword'] ?? 0;
  720. $couponId = $post['couponId'] ?? 0;
  721. $order = OrderService::getByOrderSn($orderSn);
  722. $userId = $this->adminId;
  723. $user = UserService::getUserInfo($userId);
  724. //验证支付密码是否正确
  725. UserService::validPayPassword($payPassword, $user);
  726. //支付前验证订单有效性
  727. OrderService::checkBeforePay($order);
  728. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  729. $totalFee = $order['prePrice'];
  730. $sourceType = 0;
  731. $discountData = OrderService::getDiscountPrice(['price' => $totalFee, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  732. $actPrice = $discountData['price'];
  733. $callbackParams = [];
  734. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  735. $balance = $respond['balance'] ?? 0;
  736. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'balance' => $balance]);
  737. }
  738. //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
  739. public function actionWxPay()
  740. {
  741. ini_set('date.timezone', 'Asia/Shanghai');
  742. $post = Yii::$app->request->post();
  743. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  744. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  745. $order = OrderService::getByOrderSn($orderSn);
  746. $orderId = $order['id'];
  747. //验证优惠券是否还有效
  748. if (!empty($couponId)) {
  749. CouponService::checkBeforeUse($couponId, $order['prePrice'], $order['userId']);
  750. }
  751. //支付前验证订单有效性
  752. OrderService::checkBeforePay($order);
  753. $name = isset($order['orderName']) && !empty($order['orderName']) ? $order['orderName'] : '购买商品';
  754. $totalFee = $order['realPrice'];
  755. //小程序使用miniOpenId
  756. if (httpUtil::isMiniProgram()) {
  757. $openId = $this->user['miniOpenId'];
  758. } else {
  759. $openId = $this->user['openId'];
  760. }
  761. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  762. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  763. $wxPayType = 0;
  764. if (httpUtil::isMiniProgram()) {
  765. $wxPayType = 1;
  766. }
  767. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  768. //订单30分钟后过期
  769. $now = time();
  770. $expireTime = $now + 1800;
  771. $wx = Yii::getAlias("@vendor/weixin");
  772. require_once($wx . '/lib/WxPay.Api.php');
  773. require_once($wx . '/example/WxPay.JsApiPay.php');
  774. $input = new \WxPayUnifiedOrder();
  775. $input->SetBody($name);
  776. $input->SetOut_trade_no($orderSn);
  777. $input->SetTotal_fee($totalFee * 100);
  778. $input->SetTime_start(date("YmdHis", $now));
  779. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  780. $input->SetTime_expire(date("YmdHis", $expireTime));
  781. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  782. $input->SetTrade_type("JSAPI");
  783. //花卉宝代为申请的微信支付
  784. $merchantExtend = $this->sjExtend->attributes;
  785. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  786. $input->SetSub_openid($openId);
  787. } else {
  788. $input->SetOpenid($openId);
  789. }
  790. //小程序使用miniAppId
  791. if (httpUtil::isMiniProgram()) {
  792. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  793. }
  794. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  795. $tools = new \JsApiPay();
  796. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  797. $newParams = json_decode($jsApiParameters, true);
  798. //已请求微信不能修改价格
  799. $updateData = [];
  800. $updateData['modPrice'] = 0;
  801. if (empty($order['deadline'])) {
  802. $updateData['deadline'] = $expireTime;
  803. }
  804. OrderService::updateById($orderId, $updateData);
  805. util::success($newParams);
  806. }
  807. //快捷付款订单
  808. public function actionFastPay()
  809. {
  810. ini_set('date.timezone', 'Asia/Shanghai');
  811. $post = Yii::$app->request->post();
  812. $payWay = isset($post['payWay']) ? $post['payWay'] : 0;
  813. $shopId = !empty($this->shopId) ? $this->shopId : ShopService::getDefaultShopId($this->sj);
  814. //验证门店是否有效
  815. $shopInfo = ShopService::getById($shopId);
  816. ShopService::valid($shopInfo, $this->sjId);
  817. $post['shopId'] = $shopId;
  818. //默认门店订单
  819. $store = isset($post['store']) ? $post['store'] : 1;
  820. $post['store'] = $store;
  821. //来源 0微信 1支付宝 2小程序 3朋友圈 4美团
  822. $sourceType = $this->isWx ? 0 : 1;
  823. if (httpUtil::isMiniProgram()) {
  824. $sourceType = 2;
  825. }
  826. $sourceType = isset($post['sourceType']) ? $post['sourceType'] : 0;
  827. //兼容旧系统sourceType=3表示朋友圈来源
  828. $fromType = $sourceType == 3 ? 2 : 0;
  829. $fromType = isset($post['fromType']) && !empty($post['fromType']) ? $post['fromType'] : $fromType;
  830. $post['userId'] = $this->adminId;
  831. $custom = $this->custom;
  832. $post['bookName'] = $custom['userName'] ?? '';
  833. $bookMobile = isset($post['bookMobile']) && !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  834. $bookMobile = empty($bookMobile) && isset($custom['mobile']) && !empty($custom['mobile']) ? $custom['mobile'] : $bookMobile;
  835. $post['bookMobile'] = $bookMobile;
  836. $prePrice = round($post['prePrice'], 2);
  837. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  838. $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  839. $actPrice = $discountData['price'];
  840. $post['discountType'] = $discountData['discountType'];
  841. $post['discountAmount'] = $discountData['discountAmount'];
  842. $post['actPrice'] = $actPrice;
  843. $post['realPrice'] = $actPrice;
  844. $post['payStyle'] = 0;
  845. $post['sjId'] = $this->sjId;
  846. $now = time();
  847. $expireTime = $now + 300;//订单5分钟后过期
  848. $post['createTime'] = date("Y-m-d H:i:s", $now);
  849. $post['deadline'] = $expireTime;
  850. if ($actPrice <= 0) {
  851. util::fail('请填写正确的金额');
  852. }
  853. //微信支付订单提交不能修改价格
  854. $post['modPrice'] = $this->isWx ? 0 : 1;
  855. $post['sourceType'] = $sourceType;
  856. $post['goodsNum'] = 1;
  857. $post['fromType'] = $fromType;
  858. $order = OrderService::addOrder($post);
  859. $orderId = $order['id'];
  860. $orderSn = $order['orderSn'];
  861. $sjId = $this->sjId;
  862. if ($payWay == 0) {
  863. $orderId = $order['id'];
  864. $name = '购买商品';
  865. $totalFee = $actPrice;
  866. $user = UserService::getById($this->adminId);
  867. $openId = isset($user['openId']) ? $user['openId'] : 0;
  868. if (httpUtil::isMiniProgram()) {
  869. //小程序使用miniOpenId
  870. $openId = $user['miniOpenId'];
  871. }
  872. if (empty($openId)) {
  873. util::fail('没有找到客户的openId');
  874. }
  875. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  876. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  877. $wxPayType = 0;
  878. if (httpUtil::isMiniProgram()) {
  879. $wxPayType = 1;
  880. }
  881. $attach = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&wxPayType=' . $wxPayType;
  882. $wx = Yii::getAlias("@vendor/weixin");
  883. require_once($wx . '/lib/WxPay.Api.php');
  884. require_once($wx . '/example/WxPay.JsApiPay.php');
  885. $input = new \WxPayUnifiedOrder();
  886. $input->SetBody($name);
  887. $input->SetOut_trade_no($orderSn);
  888. $input->SetTotal_fee($totalFee * 100);
  889. $input->SetTime_start(date("YmdHis", $now));
  890. $input->SetAttach($attach);
  891. //设置订单有效期5分钟
  892. $input->SetTime_expire(date("YmdHis", $expireTime));
  893. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  894. $input->SetTrade_type("JSAPI");
  895. //花卉宝代为申请的微信支付
  896. $merchantExtend = $this->sjExtend->attributes;
  897. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  898. $input->SetSub_openid($openId);
  899. } else {
  900. $input->SetOpenid($openId);
  901. }
  902. //小程序使用miniAppId
  903. if (httpUtil::isMiniProgram()) {
  904. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  905. }
  906. Yii::info('支付发起使用小程序信息' . json_encode($merchantExtend));
  907. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  908. $tools = new \JsApiPay();
  909. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  910. $newParams = json_decode($jsApiParameters, true);
  911. $newParams['orderId'] = $orderId;
  912. util::success($newParams);
  913. } elseif ($payWay == 1) {
  914. $extend = MerchantExtendService::getBySjId($sjId);
  915. if (isset($extend['alipayInit']) == false || $extend['alipayInit'] == 0) {
  916. util::fail('支付宝付款即将开通...');
  917. }
  918. $config = [
  919. //应用ID,您的APPID。
  920. 'app_id' => $extend['alipayPId'],
  921. //商户私钥,您的原始格式RSA私钥
  922. 'merchant_private_key' => $extend['alipayKey'],
  923. //异步通知地址
  924. 'notify_url' => Yii::$app->params['hdHost'] . "/notice/ali-callback",
  925. //同步跳转
  926. 'return_url' => "",
  927. //编码格式
  928. 'charset' => "UTF-8",
  929. //签名方式
  930. 'sign_type' => "RSA2",
  931. //支付宝网关
  932. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  933. //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
  934. 'alipay_public_key' => $extend['alipayPublicKey'],
  935. ];
  936. Yii::info(json_encode($config) . ' aplipay config');
  937. $alipayWap = Yii::getAlias("@vendor/alipayWap");
  938. require_once($alipayWap . '/wappay/service/AlipayTradeService.php');
  939. require_once($alipayWap . '/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php');
  940. $totalFee = $order['realPrice'];
  941. $out_trade_no = $orderSn;//商户订单号,商户网站订单系统中唯一订单号,必填
  942. $subject = '购买商品';//订单名称,必填
  943. $total_amount = $totalFee;//付款金额,必填
  944. $body = '';//商品描述,可空
  945. $timeout_express = "1m";//超时时间
  946. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  947. $passBackParams = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&sjId=' . $sjId;//将流水类型、优惠卷传过去
  948. $passBackParams = urlencode($passBackParams);
  949. $payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
  950. $payRequestBuilder->setBody($body);
  951. $payRequestBuilder->setSubject($subject);
  952. $payRequestBuilder->setOutTradeNo($out_trade_no);
  953. $payRequestBuilder->setTotalAmount($total_amount);
  954. $payRequestBuilder->setTimeExpress($timeout_express);
  955. //在异步通知时将该参数原样返回
  956. $payRequestBuilder->setPassbackParams($passBackParams);
  957. //同步通知
  958. $returnUrl = Yii::$app->params['mallDomain'] . "/#/pages/callback/success?account={$sjId}&shopId={$shopId}&orderSn={$orderSn}&totalPrice={$totalFee}&pageStatus=3&payDiscountPrice=0&payDiscountType=0";
  959. //异步通知
  960. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/ali-callback";
  961. $payResponse = new \AlipayTradeService($config);
  962. $result = $payResponse->wapPay($payRequestBuilder, $returnUrl, $notifyUrl);
  963. //直接将支付宝的html返回给前端
  964. echo $result;
  965. } elseif ($payWay == 2) {
  966. //余额支付,验证支付密码是否正确
  967. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  968. $user = UserService::getUserInfo($this->adminId);
  969. UserService::validPayPassword($payPassword, $user);
  970. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  971. $callbackParams = [];
  972. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  973. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  974. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'orderSn' => $orderSn, 'orderId' => $orderId, 'balance' => $balance]);
  975. } else {
  976. util::fail('无效的支付方式');
  977. }
  978. }
  979. //订单评价
  980. public function actionComment()
  981. {
  982. $post = Yii::$app->request->post();
  983. $id = $post['id'];
  984. $order = OrderService::getById($id);
  985. OrderService::valid($order, $this->mainId);
  986. if ($order['grade'] > 0) {
  987. util::fail('您已经评过了');
  988. }
  989. OrderService::comment($post);
  990. util::complete('提交成功');
  991. }
  992. //订单详情 ssh 2019.12.16
  993. public function actionDetail()
  994. {
  995. $id = Yii::$app->request->get('id', 0);
  996. $detail = OrderClass::getFullInfo($id);
  997. OrderService::valid($detail, $this->mainId);
  998. $forwardMap = [];
  999. if ($detail['forward'] == 1) {
  1000. //当前单如果已经是售后付款单,则取出其对应的原单
  1001. $map = OrderForwardClass::getByCondition(['shOrderId' => $id], true);
  1002. if (!empty($map)) {
  1003. $originOrderId = $map->orderId ?? 0;
  1004. $forwardMap['orderId'] = $originOrderId;
  1005. }
  1006. }
  1007. $detail['forwardMap'] = $forwardMap;
  1008. $shOrderMap = [];
  1009. if ($detail['hasForward'] == 1) {
  1010. $shOrderMap = OrderForwardClass::getAllByCondition(['orderId' => $id], null, '*');
  1011. }
  1012. $detail['shOrderMap'] = $shOrderMap;
  1013. $main = $this->main;
  1014. //是否走售后付款单模式判断 ssh
  1015. $ret = OrderClass::ifShPay($detail, $main, 0);
  1016. $shAddPay = $ret['shAddPay'];
  1017. $shAddPayReason = $ret['shAddPayReason'];
  1018. $detail['shAddPay'] = $shAddPay;
  1019. $detail['shAddPayReason'] = $shAddPayReason;
  1020. util::success($detail);
  1021. }
  1022. public function actionList()
  1023. {
  1024. $get = Yii::$app->request->get();
  1025. $searchText = $get['searchText'] ?? '';
  1026. $searchStyle = $get['searchStyle'] ?? 0;
  1027. $shopId = $this->shopId ?? 0;
  1028. if (empty($shopId)) {
  1029. //没有登录不显示数据
  1030. util::success(['list' => [], 'moreData' => 0, 'shop' => [], 'totalNum' => 0, 'totalPage' => 0]);
  1031. }
  1032. $where = [];
  1033. $where['shopId'] = $shopId;
  1034. if (!empty($get['customId'])) {
  1035. $where['customId'] = $get['customId'];
  1036. }
  1037. if (!empty($get['ids'])) {
  1038. $stringIds = $get['ids'];
  1039. $newIds = explode(',', $stringIds);
  1040. $where['id'] = ['in', $newIds];
  1041. }
  1042. $status = $get['status'] ?? 0;
  1043. if (!empty($status)) {
  1044. $where['status'] = $get['status'];
  1045. }
  1046. $debt = $get['debt'] ?? -1;
  1047. if ($debt > -1) {
  1048. $where['debt'] = $debt;
  1049. }
  1050. if (!empty($get['shopAdminId'])) {
  1051. $where['shopAdminId'] = $get['shopAdminId'];
  1052. }
  1053. if ($searchStyle == 1 && !empty($searchText)) {
  1054. $where['sendNum'] = $searchText;
  1055. }
  1056. if (isset($get['repeat']) && $get['repeat'] > -1) {
  1057. $where['repeat'] = $get['repeat'];
  1058. }
  1059. if (!empty($get['searchTime'])) {
  1060. $startTime = $get['startTime'] ?? '';
  1061. $endTime = $get['endTime'] ?? '';
  1062. $period = dateUtil::formatTime($get['searchTime'], $startTime, $endTime);
  1063. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  1064. }
  1065. $list = OrderService::getOrderList($where);
  1066. $list['shop'] = $this->shop->attributes ?? [];
  1067. util::success($list);
  1068. }
  1069. //订单归类 ssh 2019.12.17
  1070. public function actionClassify()
  1071. {
  1072. $post = Yii::$app->request->post();
  1073. $id = $post['id'] ?? 0;
  1074. $order = OrderService::getById($id);
  1075. OrderService::valid($order, $this->mainId);
  1076. $categoryId = $post['categoryId'];
  1077. $usageId = $post['usageId'];
  1078. OrderService::classify($id, $categoryId, $usageId);
  1079. util::complete();
  1080. }
  1081. //更新配送单 ssh 2019.12.17
  1082. public function actionUpdateSheet()
  1083. {
  1084. $post = Yii::$app->request->post();
  1085. $id = $post['id'] ?? 0;
  1086. unset($post['id']);
  1087. $order = OrderService::getById($id);
  1088. OrderService::valid($order, $this->mainId);
  1089. OrderService::updateSheet($order, $post);
  1090. util::complete('提交成功');
  1091. }
  1092. //商家收款与发货 ssh 2019.12.18
  1093. public function actionGathering()
  1094. {
  1095. $post = Yii::$app->request->post();
  1096. $price = isset($post['price']) && is_numeric($post['price']) ? $post['price'] : 0;
  1097. $payWay = isset($post['payWay']) && is_numeric($post['payWay']) ? $post['payWay'] : 0;
  1098. $userId = $post['userId'] ?? 0;
  1099. $userInfo = UserService::getById($userId);
  1100. if (empty($userInfo) || $userInfo['sjId'] != $this->sjId) {
  1101. util::fail('您选择的客户无效');
  1102. }
  1103. if ($price <= 0) {
  1104. util::fail('请输入金额');
  1105. }
  1106. $post['prePrice'] = $price;
  1107. $post['actPrice'] = $price;
  1108. $post['payWay'] = $payWay;
  1109. $post['sourceType'] = 1;
  1110. $post['userId'] = $userId;
  1111. $post['goodsNum'] = 1;
  1112. $post['orderName'] = '商品';
  1113. $shopId = MerchantService::getDefaultShopId($this->sj);
  1114. $post['shopId'] = $shopId;
  1115. $order = OrderService::addOrder($post);
  1116. $id = $order['id'];
  1117. $orderSn = $order['orderSn'];
  1118. //流水类型列表
  1119. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  1120. $callbackParams = [];
  1121. switch ($payWay) {
  1122. case 0:
  1123. xhPayToolService::wxPay($callbackParams, $orderSn, $price, $capitalType, 0);
  1124. break;
  1125. case 1:
  1126. xhPayToolService::alipay($callbackParams, $orderSn, $price, $capitalType, 0, []);
  1127. break;
  1128. case 2:
  1129. xhPayToolService::balancePay($callbackParams, $orderSn, $price, $capitalType, 0);
  1130. break;
  1131. default:
  1132. util::fail('无效支付方式');
  1133. }
  1134. util::success($order);
  1135. }
  1136. //免发货管理 ssh 2020.1.6
  1137. public function actionWithoutSend()
  1138. {
  1139. $id = Yii::$app->request->get('id');
  1140. $order = OrderService::getById($id);
  1141. OrderService::valid($order, $this->mainId);
  1142. //配送流程变更
  1143. $currentFlow = OrderSendClass::withoutSend($order);
  1144. util::success(['currentFlow' => $currentFlow]);
  1145. }
  1146. //修改价格 ssh 2020.1.6
  1147. public function actionUpdatePrice()
  1148. {
  1149. $id = Yii::$app->request->get('id');
  1150. $price = Yii::$app->request->get('price', 1);
  1151. $order = OrderService::getById($id);
  1152. OrderService::valid($order, $this->mainId);
  1153. if (isset($order['modPrice']) && $order['modPrice'] == 0) {
  1154. util::fail('已发起支付,不能修改价格');
  1155. }
  1156. OrderService::updateById($id, ['actPrice' => $price]);
  1157. util::complete('修改成功');
  1158. }
  1159. //配送单 ssh 2020.2.29
  1160. public function actionGetDeliverDetail()
  1161. {
  1162. $id = Yii::$app->request->get('id', 0);
  1163. $order = OrderService::getById($id);
  1164. OrderService::valid($order, $this->mainId);
  1165. $reachDate = isset($order['reachDate']) && !empty($order['reachDate']) ? $order['reachDate'] : '';
  1166. $reachPeriodId = $order['reachPeriod'];
  1167. $reachPeriodArr = [0 => '上午', 1 => '下午', 2 => '晚上'];
  1168. $reachPeriod = isset($reachPeriodArr[$reachPeriodId]) ? $reachPeriodArr[$reachPeriodId] : '';
  1169. $bookName = isset($order['bookName']) ? $order['bookName'] : '';
  1170. $bookMobile = isset($order['bookMobile']) ? $order['bookMobile'] : '';
  1171. if (isset($order['anonymity']) && $order['anonymity'] == 1) {
  1172. $bookName = '--';
  1173. $bookMobile = '--';
  1174. }
  1175. $data = [
  1176. 'reachDate' => $reachDate . ' ' . $reachPeriod,
  1177. 'orderSn' => $order['orderSn'],
  1178. 'receiveUserName' => $order['receiveUserName'],
  1179. 'receiveMobile' => $order['receiveMobile'],
  1180. 'fullAddress' => $order['fullAddress'] . "(" . $order['showAddress'] . ")",
  1181. 'cardInfo' => $order['cardInfo'],
  1182. 'bookMobile' => $bookMobile,
  1183. 'bookName' => $bookName,
  1184. 'remark' => $order['remark'],
  1185. 'sendNum' => $order['sendNum'],
  1186. 'telephone' => 18030142050,
  1187. 'printTime' => '',
  1188. 'img' => '',
  1189. ];
  1190. util::success($data);
  1191. }
  1192. //运费计算 lqh 2021.4.12 暂反回固定
  1193. public function actionFreight()
  1194. {
  1195. util::success(['sedCost' => 10]);
  1196. }
  1197. }