OrderController.php 51 KB

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