OrderController.php 68 KB

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