OrderController.php 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  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. //默认情况开单员工和收款员工是同个人
  491. $staffId = $this->shopAdminId ?? 0;
  492. $staffName = $this->shopAdmin->name ?? '';
  493. if (empty($post['shopAdminId'])) {
  494. $post['shopAdminId'] = $staffId;
  495. $post['shopAdminName'] = $staffName;
  496. }
  497. if (empty($post['getStaffId'])) {
  498. $post['getStaffId'] = $staffId;
  499. $post['getStaffName'] = $this->shopAdmin->name ?? '';
  500. }
  501. $groupId = !empty($post['groupId']) ? $post['groupId'] : 0;
  502. $post['fromType'] = $post['fromType'] ?? 1;
  503. if ($post['fromType'] == dict::getDict('fromType', 'friend')) {
  504. if (empty($post['bookName'])) {
  505. util::fail('请填写订花人姓名');
  506. }
  507. }
  508. $customId = $post['customId'] ?? 0;
  509. $custom = CustomClass::getById($customId, true);
  510. if (empty($custom)) {
  511. util::fail('没有找到客户哦');
  512. }
  513. $hdId = $custom->hdId ?? 0;
  514. $hd = HdClass::getById($hdId, true);
  515. if (empty($hd)) {
  516. util::fail('客户对应的花店信息缺失,编号869');
  517. }
  518. $hdName = $hd->name ?? '';
  519. $post['hdId'] = $hdId;
  520. $post['hdName'] = $hdName;
  521. $customName = $custom->name ?? '';
  522. $post['customName'] = $customName;
  523. $post['customNamePy'] = stringUtil::py($customName);
  524. if (!empty($post['receiveMobile'])) {
  525. if (!stringUtil::isMobile($post['receiveMobile'])) {
  526. util::fail('请填写正确的收花人手机号');
  527. }
  528. }
  529. if (!empty($post['bookMobile'])) {
  530. if (!stringUtil::isMobile($post['bookMobile'])) {
  531. util::fail('请填写正确的订花人手机号');
  532. }
  533. }
  534. $hasPay = $post['hasPay'] ?? dict::getDict('hasPay', 'unPay');
  535. if (isset($post['isCashier']) && $post['isCashier'] == 1) {
  536. //收银台开单默认到店自取
  537. $post['sendType'] = dict::getDict('sendType', 'shopGet');
  538. }
  539. $forward = $post['forward'] ?? 0;
  540. if ($forward == 1) {
  541. if ($hasPay != 1) {
  542. util::fail('售后付款单只能走线下转账');
  543. }
  544. } else {
  545. //如果不是售后付款单,并且选的不是余额支付,余额又够付当前订单,则自动切到余额支付
  546. $balance = $custom->balance ?? 0;
  547. $modifyPrice = $post['modifyPrice'] ?? 0;
  548. if ($hasPay != 4 && $balance > $modifyPrice) {
  549. $post['hasPay'] = 4;
  550. $hasPay = 4;
  551. }
  552. }
  553. $productJson = $post['product'] ?? '';
  554. $productList = [];
  555. if (!empty($productJson)) {
  556. $productList = json_decode($productJson, true);
  557. }
  558. if (empty($productList) && empty($groupId)) {
  559. //商家手机上开单并生成制作单
  560. if (!empty($post['unitGoodsPrice'])) {
  561. $unitGoodsPrice = $post['unitGoodsPrice'];
  562. if (!is_numeric($unitGoodsPrice)) {
  563. util::fail('请输入正确的单价');
  564. }
  565. $productList = GoodsClass::orderCreateGoods($post);
  566. }
  567. }
  568. if (empty($productList) && empty($groupId)) {
  569. if ($post['lsGoodsPrice'] <= 0) {
  570. $lsGoodsPrice = $post['lsGoodsPrice'];
  571. if (!is_numeric($lsGoodsPrice)) {
  572. util::fail('请输入正确的金额');
  573. }
  574. util::fail('请填写金额或选商品');
  575. }
  576. }
  577. $connection = Yii::$app->db;
  578. $transaction = $connection->beginTransaction();
  579. try {
  580. $post['product'] = $productList;
  581. //解决重复提交
  582. util::checkRepeatCommit($this->adminId, 2);
  583. $reduceStock = $post['reduceStock'] ?? 0;
  584. if ($reduceStock == 1) {
  585. //如果扣除库,则走另外一条路
  586. $return = OrderClass::reduceMyStock($post, $this->shop, $custom);
  587. $transaction->commit();
  588. util::success($return);
  589. }
  590. //解决开单提交订单时,别人修改价格,造成提交价格不是真实卖价问题!!
  591. $version = $post['version'] ?? 0;
  592. if ($version == 2) {
  593. $dealPrice = $post['dealPrice'] ?? 0;
  594. if ($dealPrice == 0) {
  595. $productIds = [];
  596. $hsIds = [];
  597. foreach ($productList as $pv) {
  598. $property = $pv['property'] ?? 0;
  599. $puId = $pv['productId'] ?? 0;
  600. if ($property == 0) {
  601. $hsIds[] = $puId;
  602. } else {
  603. $productIds[] = $puId;
  604. }
  605. }
  606. $itemInfo = [];
  607. if (!empty($productIds)) {
  608. $itemInfo = ProductClass::getByIds($productIds, null, 'id');
  609. }
  610. $hsInfo = [];
  611. if (!empty($hsIds)) {
  612. $hsInfo = GoodsClass::getByIds($hsIds, null, 'id');
  613. }
  614. $diff = [];
  615. $priceMap = \bizGhs\custom\classes\CustomClass::$levelPriceKeyMap;
  616. $addPriceMap = \bizGhs\custom\classes\CustomClass::$levelAddPriceKeyMap;
  617. $level = 0;
  618. foreach ($productList as $currentProduct) {
  619. $property = $currentProduct['property'] ?? 0;
  620. $currentPrice = $currentProduct['unitPrice'] ?? 0;
  621. $currentId = $currentProduct['productId'] ?? 0;
  622. if ($property == 1) {
  623. $systemInfo = $itemInfo[$currentId] ?? [];
  624. if (!empty($systemInfo)) {
  625. $name = $systemInfo['name'] ?? '';
  626. $systemPrice = \bizGhs\product\classes\ProductClass::getFinalPrice($systemInfo, $level, $priceMap, $addPriceMap);
  627. if (floatval($currentPrice) != floatval($systemPrice)) {
  628. $diff[] = [
  629. 'name' => $name,
  630. 'price' => $systemPrice,
  631. 'kdPrice' => $currentPrice,
  632. ];
  633. }
  634. }
  635. } else {
  636. $systemInfo = $hsInfo[$currentId] ?? [];
  637. if (!empty($systemInfo)) {
  638. $params = [];
  639. $name = $systemInfo['name'] ?? '';
  640. $hsData = GoodsClass::getFinalPrice($systemInfo, $shop, $custom, $params);
  641. $systemPrice = $hsData['price'] ?? 0;
  642. if ($systemInfo['priceType'] == 1) {
  643. if (floatval($currentPrice) != floatval($systemPrice)) {
  644. $diff[] = [
  645. 'name' => $name,
  646. 'price' => $systemPrice,
  647. 'kdPrice' => $currentPrice,
  648. ];
  649. }
  650. }
  651. }
  652. }
  653. }
  654. if (!empty($diff)) {
  655. util::success(['diff' => $diff, 'respondType' => 'priceError']);
  656. }
  657. }
  658. }
  659. //多处调用这个方法,注意同步修改,搜索关键词 createHdNewOrder
  660. $return = OrderService::createHdOrder($post, $custom, $hasPay);
  661. $shop = $this->shop;
  662. $staff = $this->shopAdmin;
  663. //售后开付款单,库存要变化,关系要建立,多处需要同步修改 sh_pay_change ssh
  664. OrderClass::shPayChange($return, $shop, $staff);
  665. $transaction->commit();
  666. //打印小票和语音播报
  667. $id = $return->id ?? 0;
  668. $order = OrderClass::getById($id, true);
  669. if ($order->fromType == 3 || $order->fromType == 4) {
  670. //除了客服号下单和美团下单,其它情况允许打小票
  671. } else {
  672. //前台打小票
  673. OrderClass::onlinePrint($order);
  674. }
  675. //前台提示收款多少钱
  676. ShopExtClass::hdGatheringReport($order);
  677. //前台提醒出示付款码
  678. if (isset($order->status) && $order->status == 1) {
  679. if (isset($post['isCashier']) && $post['isCashier'] == 1) {
  680. ShopExtClass::pleasePayReport($order);
  681. }
  682. }
  683. $shopId = $order->shopId ?? 0;
  684. $shop = ShopClass::getById($shopId, true);
  685. WxMessageClass::gatheringIncomeInform($shop, $order);
  686. //新制作单提示
  687. $shopExt = ShopExtClass::getByCondition(['shopId' => $this->shopId], true);
  688. ShopExtClass::newWorkRemind($shopExt, $order);
  689. util::success($return);
  690. } catch (\Exception $e) {
  691. $transaction->rollBack();
  692. Yii::error("失败原因:" . $e->getMessage());
  693. util::fail('下单失败');
  694. }
  695. }
  696. //下单要用到的相关信息 ssh 2019.12.6
  697. public function actionOrderRelate()
  698. {
  699. $regionTree = RegionService::tree();
  700. $shop = $this->shop->attributes;
  701. $freight = MerchantExtendService::getFreight($this->sjExtend);
  702. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  703. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'thirdMapKey' => $mapKey, 'merchant' => $shop];
  704. util::success($out);
  705. }
  706. //余额支付 ssh 2019.12.6
  707. public function actionBalancePay()
  708. {
  709. $post = Yii::$app->request->post();
  710. $orderSn = $post['orderSn'] ?? 0;
  711. $payPassword = $post['payPassword'] ?? 0;
  712. $couponId = $post['couponId'] ?? 0;
  713. $order = OrderService::getByOrderSn($orderSn);
  714. $userId = $this->adminId;
  715. $user = UserService::getUserInfo($userId);
  716. //验证支付密码是否正确
  717. UserService::validPayPassword($payPassword, $user);
  718. //支付前验证订单有效性
  719. OrderService::checkBeforePay($order);
  720. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  721. $totalFee = $order['prePrice'];
  722. $sourceType = 0;
  723. $discountData = OrderService::getDiscountPrice(['price' => $totalFee, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  724. $actPrice = $discountData['price'];
  725. $callbackParams = [];
  726. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  727. $balance = $respond['balance'] ?? 0;
  728. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'balance' => $balance]);
  729. }
  730. //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
  731. public function actionWxPay()
  732. {
  733. ini_set('date.timezone', 'Asia/Shanghai');
  734. $post = Yii::$app->request->post();
  735. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  736. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  737. $order = OrderService::getByOrderSn($orderSn);
  738. $orderId = $order['id'];
  739. //验证优惠券是否还有效
  740. if (!empty($couponId)) {
  741. CouponService::checkBeforeUse($couponId, $order['prePrice'], $order['userId']);
  742. }
  743. //支付前验证订单有效性
  744. OrderService::checkBeforePay($order);
  745. $name = isset($order['orderName']) && !empty($order['orderName']) ? $order['orderName'] : '购买商品';
  746. $totalFee = $order['realPrice'];
  747. //小程序使用miniOpenId
  748. if (httpUtil::isMiniProgram()) {
  749. $openId = $this->user['miniOpenId'];
  750. } else {
  751. $openId = $this->user['openId'];
  752. }
  753. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  754. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  755. $wxPayType = 0;
  756. if (httpUtil::isMiniProgram()) {
  757. $wxPayType = 1;
  758. }
  759. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  760. //订单30分钟后过期
  761. $now = time();
  762. $expireTime = $now + 1800;
  763. $wx = Yii::getAlias("@vendor/weixin");
  764. require_once($wx . '/lib/WxPay.Api.php');
  765. require_once($wx . '/example/WxPay.JsApiPay.php');
  766. $input = new \WxPayUnifiedOrder();
  767. $input->SetBody($name);
  768. $input->SetOut_trade_no($orderSn);
  769. $input->SetTotal_fee($totalFee * 100);
  770. $input->SetTime_start(date("YmdHis", $now));
  771. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  772. $input->SetTime_expire(date("YmdHis", $expireTime));
  773. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  774. $input->SetTrade_type("JSAPI");
  775. //花卉宝代为申请的微信支付
  776. $merchantExtend = $this->sjExtend->attributes;
  777. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  778. $input->SetSub_openid($openId);
  779. } else {
  780. $input->SetOpenid($openId);
  781. }
  782. //小程序使用miniAppId
  783. if (httpUtil::isMiniProgram()) {
  784. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  785. }
  786. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  787. $tools = new \JsApiPay();
  788. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  789. $newParams = json_decode($jsApiParameters, true);
  790. //已请求微信不能修改价格
  791. $updateData = [];
  792. $updateData['modPrice'] = 0;
  793. if (empty($order['deadline'])) {
  794. $updateData['deadline'] = $expireTime;
  795. }
  796. OrderService::updateById($orderId, $updateData);
  797. util::success($newParams);
  798. }
  799. //快捷付款订单
  800. public function actionFastPay()
  801. {
  802. ini_set('date.timezone', 'Asia/Shanghai');
  803. $post = Yii::$app->request->post();
  804. $payWay = isset($post['payWay']) ? $post['payWay'] : 0;
  805. $shopId = !empty($this->shopId) ? $this->shopId : ShopService::getDefaultShopId($this->sj);
  806. //验证门店是否有效
  807. $shopInfo = ShopService::getById($shopId);
  808. ShopService::valid($shopInfo, $this->sjId);
  809. $post['shopId'] = $shopId;
  810. //默认门店订单
  811. $store = isset($post['store']) ? $post['store'] : 1;
  812. $post['store'] = $store;
  813. //来源 0微信 1支付宝 2小程序 3朋友圈 4美团
  814. $sourceType = $this->isWx ? 0 : 1;
  815. if (httpUtil::isMiniProgram()) {
  816. $sourceType = 2;
  817. }
  818. $sourceType = isset($post['sourceType']) ? $post['sourceType'] : 0;
  819. //兼容旧系统sourceType=3表示朋友圈来源
  820. $fromType = $sourceType == 3 ? 2 : 0;
  821. $fromType = isset($post['fromType']) && !empty($post['fromType']) ? $post['fromType'] : $fromType;
  822. $post['userId'] = $this->adminId;
  823. $custom = $this->custom;
  824. $post['bookName'] = $custom['userName'] ?? '';
  825. $bookMobile = isset($post['bookMobile']) && !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  826. $bookMobile = empty($bookMobile) && isset($custom['mobile']) && !empty($custom['mobile']) ? $custom['mobile'] : $bookMobile;
  827. $post['bookMobile'] = $bookMobile;
  828. $prePrice = round($post['prePrice'], 2);
  829. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  830. $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  831. $actPrice = $discountData['price'];
  832. $post['discountType'] = $discountData['discountType'];
  833. $post['discountAmount'] = $discountData['discountAmount'];
  834. $post['actPrice'] = $actPrice;
  835. $post['realPrice'] = $actPrice;
  836. $post['payStyle'] = 0;
  837. $post['sjId'] = $this->sjId;
  838. $now = time();
  839. $expireTime = $now + 300;//订单5分钟后过期
  840. $post['createTime'] = date("Y-m-d H:i:s", $now);
  841. $post['deadline'] = $expireTime;
  842. if ($actPrice <= 0) {
  843. util::fail('请填写正确的金额');
  844. }
  845. //微信支付订单提交不能修改价格
  846. $post['modPrice'] = $this->isWx ? 0 : 1;
  847. $post['sourceType'] = $sourceType;
  848. $post['goodsNum'] = 1;
  849. $post['fromType'] = $fromType;
  850. $order = OrderService::addOrder($post);
  851. $orderId = $order['id'];
  852. $orderSn = $order['orderSn'];
  853. $sjId = $this->sjId;
  854. if ($payWay == 0) {
  855. $orderId = $order['id'];
  856. $name = '购买商品';
  857. $totalFee = $actPrice;
  858. $user = UserService::getById($this->adminId);
  859. $openId = isset($user['openId']) ? $user['openId'] : 0;
  860. if (httpUtil::isMiniProgram()) {
  861. //小程序使用miniOpenId
  862. $openId = $user['miniOpenId'];
  863. }
  864. if (empty($openId)) {
  865. util::fail('没有找到客户的openId');
  866. }
  867. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  868. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  869. $wxPayType = 0;
  870. if (httpUtil::isMiniProgram()) {
  871. $wxPayType = 1;
  872. }
  873. $attach = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&wxPayType=' . $wxPayType;
  874. $wx = Yii::getAlias("@vendor/weixin");
  875. require_once($wx . '/lib/WxPay.Api.php');
  876. require_once($wx . '/example/WxPay.JsApiPay.php');
  877. $input = new \WxPayUnifiedOrder();
  878. $input->SetBody($name);
  879. $input->SetOut_trade_no($orderSn);
  880. $input->SetTotal_fee($totalFee * 100);
  881. $input->SetTime_start(date("YmdHis", $now));
  882. $input->SetAttach($attach);
  883. //设置订单有效期5分钟
  884. $input->SetTime_expire(date("YmdHis", $expireTime));
  885. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  886. $input->SetTrade_type("JSAPI");
  887. //花卉宝代为申请的微信支付
  888. $merchantExtend = $this->sjExtend->attributes;
  889. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  890. $input->SetSub_openid($openId);
  891. } else {
  892. $input->SetOpenid($openId);
  893. }
  894. //小程序使用miniAppId
  895. if (httpUtil::isMiniProgram()) {
  896. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  897. }
  898. Yii::info('支付发起使用小程序信息' . json_encode($merchantExtend));
  899. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  900. $tools = new \JsApiPay();
  901. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  902. $newParams = json_decode($jsApiParameters, true);
  903. $newParams['orderId'] = $orderId;
  904. util::success($newParams);
  905. } elseif ($payWay == 1) {
  906. $extend = MerchantExtendService::getBySjId($sjId);
  907. if (isset($extend['alipayInit']) == false || $extend['alipayInit'] == 0) {
  908. util::fail('支付宝付款即将开通...');
  909. }
  910. $config = [
  911. //应用ID,您的APPID。
  912. 'app_id' => $extend['alipayPId'],
  913. //商户私钥,您的原始格式RSA私钥
  914. 'merchant_private_key' => $extend['alipayKey'],
  915. //异步通知地址
  916. 'notify_url' => Yii::$app->params['hdHost'] . "/notice/ali-callback",
  917. //同步跳转
  918. 'return_url' => "",
  919. //编码格式
  920. 'charset' => "UTF-8",
  921. //签名方式
  922. 'sign_type' => "RSA2",
  923. //支付宝网关
  924. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  925. //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
  926. 'alipay_public_key' => $extend['alipayPublicKey'],
  927. ];
  928. Yii::info(json_encode($config) . ' aplipay config');
  929. $alipayWap = Yii::getAlias("@vendor/alipayWap");
  930. require_once($alipayWap . '/wappay/service/AlipayTradeService.php');
  931. require_once($alipayWap . '/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php');
  932. $totalFee = $order['realPrice'];
  933. $out_trade_no = $orderSn;//商户订单号,商户网站订单系统中唯一订单号,必填
  934. $subject = '购买商品';//订单名称,必填
  935. $total_amount = $totalFee;//付款金额,必填
  936. $body = '';//商品描述,可空
  937. $timeout_express = "1m";//超时时间
  938. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  939. $passBackParams = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&sjId=' . $sjId;//将流水类型、优惠卷传过去
  940. $passBackParams = urlencode($passBackParams);
  941. $payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
  942. $payRequestBuilder->setBody($body);
  943. $payRequestBuilder->setSubject($subject);
  944. $payRequestBuilder->setOutTradeNo($out_trade_no);
  945. $payRequestBuilder->setTotalAmount($total_amount);
  946. $payRequestBuilder->setTimeExpress($timeout_express);
  947. //在异步通知时将该参数原样返回
  948. $payRequestBuilder->setPassbackParams($passBackParams);
  949. //同步通知
  950. $returnUrl = Yii::$app->params['mallDomain'] . "/#/pages/callback/success?account={$sjId}&shopId={$shopId}&orderSn={$orderSn}&totalPrice={$totalFee}&pageStatus=3&payDiscountPrice=0&payDiscountType=0";
  951. //异步通知
  952. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/ali-callback";
  953. $payResponse = new \AlipayTradeService($config);
  954. $result = $payResponse->wapPay($payRequestBuilder, $returnUrl, $notifyUrl);
  955. //直接将支付宝的html返回给前端
  956. echo $result;
  957. } elseif ($payWay == 2) {
  958. //余额支付,验证支付密码是否正确
  959. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  960. $user = UserService::getUserInfo($this->adminId);
  961. UserService::validPayPassword($payPassword, $user);
  962. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  963. $callbackParams = [];
  964. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  965. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  966. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'orderSn' => $orderSn, 'orderId' => $orderId, 'balance' => $balance]);
  967. } else {
  968. util::fail('无效的支付方式');
  969. }
  970. }
  971. //订单评价
  972. public function actionComment()
  973. {
  974. $post = Yii::$app->request->post();
  975. $id = $post['id'];
  976. $order = OrderService::getById($id);
  977. OrderService::valid($order, $this->mainId);
  978. if ($order['grade'] > 0) {
  979. util::fail('您已经评过了');
  980. }
  981. OrderService::comment($post);
  982. util::complete('提交成功');
  983. }
  984. //订单详情 ssh 2019.12.16
  985. public function actionDetail()
  986. {
  987. $id = Yii::$app->request->get('id', 0);
  988. $detail = OrderClass::getFullInfo($id);
  989. OrderService::valid($detail, $this->mainId);
  990. $forwardMap = [];
  991. if ($detail['forward'] == 1) {
  992. //当前单如果已经是售后付款单,则取出其对应的原单
  993. $map = OrderForwardClass::getByCondition(['shOrderId' => $id], true);
  994. if (!empty($map)) {
  995. $originOrderId = $map->orderId ?? 0;
  996. $forwardMap['orderId'] = $originOrderId;
  997. }
  998. }
  999. $detail['forwardMap'] = $forwardMap;
  1000. $shOrderMap = [];
  1001. if ($detail['hasForward'] == 1) {
  1002. $shOrderMap = OrderForwardClass::getAllByCondition(['orderId' => $id], null, '*');
  1003. }
  1004. $detail['shOrderMap'] = $shOrderMap;
  1005. $main = $this->main;
  1006. //是否走售后付款单模式判断 ssh
  1007. $ret = OrderClass::ifShPay($detail, $main, 0);
  1008. $shAddPay = $ret['shAddPay'];
  1009. $shAddPayReason = $ret['shAddPayReason'];
  1010. $detail['shAddPay'] = $shAddPay;
  1011. $detail['shAddPayReason'] = $shAddPayReason;
  1012. util::success($detail);
  1013. }
  1014. public function actionList()
  1015. {
  1016. $get = Yii::$app->request->get();
  1017. $searchText = $get['searchText'] ?? '';
  1018. $searchStyle = $get['searchStyle'] ?? 0;
  1019. $shopId = $this->shopId ?? 0;
  1020. if (empty($shopId)) {
  1021. //没有登录不显示数据
  1022. util::success(['list' => [], 'moreData' => 0, 'shop' => [], 'totalNum' => 0, 'totalPage' => 0]);
  1023. }
  1024. $where = [];
  1025. $where['shopId'] = $shopId;
  1026. if (!empty($get['customId'])) {
  1027. $where['customId'] = $get['customId'];
  1028. }
  1029. if (!empty($get['ids'])) {
  1030. $stringIds = $get['ids'];
  1031. $newIds = explode(',', $stringIds);
  1032. $where['id'] = ['in', $newIds];
  1033. }
  1034. $status = $get['status'] ?? 0;
  1035. if (!empty($status)) {
  1036. $where['status'] = $get['status'];
  1037. }
  1038. $debt = $get['debt'] ?? -1;
  1039. if ($debt > -1) {
  1040. $where['debt'] = $debt;
  1041. }
  1042. if (!empty($get['shopAdminId'])) {
  1043. $where['shopAdminId'] = $get['shopAdminId'];
  1044. }
  1045. if ($searchStyle == 1 && !empty($searchText)) {
  1046. $where['sendNum'] = $searchText;
  1047. }
  1048. if (isset($get['repeat']) && $get['repeat'] > -1) {
  1049. $where['repeat'] = $get['repeat'];
  1050. }
  1051. if (!empty($get['searchTime'])) {
  1052. $startTime = $get['startTime'] ?? '';
  1053. $endTime = $get['endTime'] ?? '';
  1054. $period = dateUtil::formatTime($get['searchTime'], $startTime, $endTime);
  1055. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  1056. }
  1057. $list = OrderService::getOrderList($where);
  1058. $list['shop'] = $this->shop->attributes ?? [];
  1059. util::success($list);
  1060. }
  1061. //订单归类 ssh 2019.12.17
  1062. public function actionClassify()
  1063. {
  1064. $post = Yii::$app->request->post();
  1065. $id = $post['id'] ?? 0;
  1066. $order = OrderService::getById($id);
  1067. OrderService::valid($order, $this->mainId);
  1068. $categoryId = $post['categoryId'];
  1069. $usageId = $post['usageId'];
  1070. OrderService::classify($id, $categoryId, $usageId);
  1071. util::complete();
  1072. }
  1073. //更新配送单 ssh 2019.12.17
  1074. public function actionUpdateSheet()
  1075. {
  1076. $post = Yii::$app->request->post();
  1077. $id = $post['id'] ?? 0;
  1078. unset($post['id']);
  1079. $order = OrderService::getById($id);
  1080. OrderService::valid($order, $this->mainId);
  1081. OrderService::updateSheet($order, $post);
  1082. util::complete('提交成功');
  1083. }
  1084. //商家收款与发货 ssh 2019.12.18
  1085. public function actionGathering()
  1086. {
  1087. $post = Yii::$app->request->post();
  1088. $price = isset($post['price']) && is_numeric($post['price']) ? $post['price'] : 0;
  1089. $payWay = isset($post['payWay']) && is_numeric($post['payWay']) ? $post['payWay'] : 0;
  1090. $userId = $post['userId'] ?? 0;
  1091. $userInfo = UserService::getById($userId);
  1092. if (empty($userInfo) || $userInfo['sjId'] != $this->sjId) {
  1093. util::fail('您选择的客户无效');
  1094. }
  1095. if ($price <= 0) {
  1096. util::fail('请输入金额');
  1097. }
  1098. $post['prePrice'] = $price;
  1099. $post['actPrice'] = $price;
  1100. $post['payWay'] = $payWay;
  1101. $post['sourceType'] = 1;
  1102. $post['userId'] = $userId;
  1103. $post['goodsNum'] = 1;
  1104. $post['orderName'] = '商品';
  1105. $shopId = MerchantService::getDefaultShopId($this->sj);
  1106. $post['shopId'] = $shopId;
  1107. $order = OrderService::addOrder($post);
  1108. $id = $order['id'];
  1109. $orderSn = $order['orderSn'];
  1110. //流水类型列表
  1111. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  1112. $callbackParams = [];
  1113. switch ($payWay) {
  1114. case 0:
  1115. xhPayToolService::wxPay($callbackParams, $orderSn, $price, $capitalType, 0);
  1116. break;
  1117. case 1:
  1118. xhPayToolService::alipay($callbackParams, $orderSn, $price, $capitalType, 0, []);
  1119. break;
  1120. case 2:
  1121. xhPayToolService::balancePay($callbackParams, $orderSn, $price, $capitalType, 0);
  1122. break;
  1123. default:
  1124. util::fail('无效支付方式');
  1125. }
  1126. util::success($order);
  1127. }
  1128. //免发货管理 ssh 2020.1.6
  1129. public function actionWithoutSend()
  1130. {
  1131. $id = Yii::$app->request->get('id');
  1132. $order = OrderService::getById($id);
  1133. OrderService::valid($order, $this->mainId);
  1134. //配送流程变更
  1135. $currentFlow = OrderSendClass::withoutSend($order);
  1136. util::success(['currentFlow' => $currentFlow]);
  1137. }
  1138. //修改价格 ssh 2020.1.6
  1139. public function actionUpdatePrice()
  1140. {
  1141. $id = Yii::$app->request->get('id');
  1142. $price = Yii::$app->request->get('price', 1);
  1143. $order = OrderService::getById($id);
  1144. OrderService::valid($order, $this->mainId);
  1145. if (isset($order['modPrice']) && $order['modPrice'] == 0) {
  1146. util::fail('已发起支付,不能修改价格');
  1147. }
  1148. OrderService::updateById($id, ['actPrice' => $price]);
  1149. util::complete('修改成功');
  1150. }
  1151. //配送单 ssh 2020.2.29
  1152. public function actionGetDeliverDetail()
  1153. {
  1154. $id = Yii::$app->request->get('id', 0);
  1155. $order = OrderService::getById($id);
  1156. OrderService::valid($order, $this->mainId);
  1157. $reachDate = isset($order['reachDate']) && !empty($order['reachDate']) ? $order['reachDate'] : '';
  1158. $reachPeriodId = $order['reachPeriod'];
  1159. $reachPeriodArr = [0 => '上午', 1 => '下午', 2 => '晚上'];
  1160. $reachPeriod = isset($reachPeriodArr[$reachPeriodId]) ? $reachPeriodArr[$reachPeriodId] : '';
  1161. $bookName = isset($order['bookName']) ? $order['bookName'] : '';
  1162. $bookMobile = isset($order['bookMobile']) ? $order['bookMobile'] : '';
  1163. if (isset($order['anonymity']) && $order['anonymity'] == 1) {
  1164. $bookName = '--';
  1165. $bookMobile = '--';
  1166. }
  1167. $data = [
  1168. 'reachDate' => $reachDate . ' ' . $reachPeriod,
  1169. 'orderSn' => $order['orderSn'],
  1170. 'receiveUserName' => $order['receiveUserName'],
  1171. 'receiveMobile' => $order['receiveMobile'],
  1172. 'fullAddress' => $order['fullAddress'] . "(" . $order['showAddress'] . ")",
  1173. 'cardInfo' => $order['cardInfo'],
  1174. 'bookMobile' => $bookMobile,
  1175. 'bookName' => $bookName,
  1176. 'remark' => $order['remark'],
  1177. 'sendNum' => $order['sendNum'],
  1178. 'telephone' => 18030142050,
  1179. 'printTime' => '',
  1180. 'img' => '',
  1181. ];
  1182. util::success($data);
  1183. }
  1184. //运费计算 lqh 2021.4.12 暂反回固定
  1185. public function actionFreight()
  1186. {
  1187. util::success(['sedCost' => 10]);
  1188. }
  1189. }