OrderController.php 70 KB

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