OrderController.php 52 KB

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