OrderController.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\stat\classes\StatOrderCountClass;
  4. use bizGhs\express\services\DadaExpressServices;
  5. use bizGhs\order\classes\StockWastageOrderClass;
  6. use bizHd\purchase\classes\PurchaseClass;
  7. use bizHd\purchase\services\PurchaseService;
  8. use bizHd\wx\classes\WxOpenClass;
  9. use bizGhs\custom\classes\CustomClass;
  10. use bizGhs\order\classes\OrderClass;
  11. use bizGhs\order\services\OrderService;
  12. use bizHd\saas\services\RegionService;
  13. use bizGhs\product\classes\ProductClass;
  14. use common\components\dada\dada;
  15. use common\components\dict;
  16. use common\components\dateUtil;
  17. use common\components\freight;
  18. use common\components\httpUtil;
  19. use Yii;
  20. use common\components\util;
  21. use bizGhs\order\classes\RefundOrderClass;
  22. use biz\shop\classes\ShopAdminClass;
  23. class OrderController extends BaseController
  24. {
  25. public $guestAccess = ['create-order', 'order-relate', 'fast-pay'];
  26. //订单列表 ssh 2021.1.20
  27. public function actionList()
  28. {
  29. $get = Yii::$app->request->get();
  30. $status = $get['status'] ?? 0;
  31. if (!empty($status)) {
  32. $where['status'] = $status;
  33. }
  34. $where['sjId'] = $this->sjId;
  35. if (isset($get['shopId'])) {
  36. $shopId = $get['shopId'] ?? 0;
  37. if (!empty($shopId)) {
  38. $where['shopId'] = $this->shopId;
  39. }
  40. } else {
  41. $where['shopId'] = $this->shopId;
  42. }
  43. $searchTime = $get['searchTime'] ?? '';
  44. if (!empty($searchTime)) {
  45. $startTime = $get['startTime'] ?? '';
  46. $endTime = $get['endTime'] ?? '';
  47. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  48. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  49. }
  50. $name = $get['name'] ?? '';
  51. if (!empty($name)) {
  52. $searchType = $get['searchType'] ?? 0;
  53. if (empty($searchType)) {
  54. //订单编号
  55. if (strpos($name, 'XSD') === 0) {
  56. $where['orderSn'] = $name;
  57. } elseif (preg_match("/^[a-zA-Z\s]+$/", $name)) {
  58. $where['customNamePy'] = ['like', $name];
  59. } else {
  60. $where['customName'] = ['like', $name];
  61. }
  62. } elseif ($searchType == 1) {
  63. $where['sendNum'] = $name;
  64. } else {
  65. }
  66. }
  67. $respond = OrderClass::getOrderList($where);
  68. $respond['shop'] = $this->shop;
  69. $respond['shopExt'] = $this->shopExt;
  70. util::success($respond);
  71. }
  72. //商城下单操作 ssh 2021.1.14
  73. public function actionCreateOrder()
  74. {
  75. $post = Yii::$app->request->post();
  76. if (isset($post['newVersion']) == false) {
  77. util::fail('请升级小程序');
  78. }
  79. unset($post['newVersion']);
  80. $shopId = $this->shopId;
  81. $customId = $post['customId'] ?? 0;
  82. $post['customId'] = $customId;
  83. //开单必须选客户
  84. if (empty($customId)) {
  85. util::fail('请选择客户');
  86. }
  87. $custom = CustomClass::getCustom($customId);
  88. if (empty($custom)) {
  89. util::fail('请选客户哦');
  90. }
  91. CustomClass::valid($custom, $this->shopId);
  92. $post['ghsId'] = $custom['ghsId'] ?? 0;
  93. $post['customMobile'] = $custom['mobile'] ?? '';
  94. $customName = $custom['name'] ?? '';
  95. $post['customName'] = $customName;
  96. $post['customNamePy'] = $custom['py'] ?? '';
  97. $post['customAvatar'] = $custom['shortSmallAvatar'] ?? '';
  98. $post['shopAdminId'] = $this->shopAdminId;
  99. $post['province'] = $custom['province'] ?? '';
  100. $post['city'] = $custom['city'] ?? '';
  101. $post['dist'] = $custom['dist'] ?? '';
  102. $post['address'] = $custom['address'] ?? '';
  103. $post['floor'] = $custom['floor'] ?? '';
  104. $post['fullAddress'] = $custom['fullAddress'] ?? '';
  105. $post['showAddress'] = $custom['showAddress'] ?? '';
  106. $post['long'] = $custom['long'] ?? '';
  107. $post['lat'] = $custom['lat'] ?? '';
  108. $shopAdmin = $this->shopAdmin->attributes;
  109. $post['shopAdminName'] = $shopAdmin['name'] ?? '';
  110. $post['sjId'] = $this->sjId;
  111. $post['shopId'] = $shopId;
  112. $post['fromType'] = 1;
  113. $post['expressId'] = $post['expressId'] ?? 0;
  114. $post['sendCost'] = $post['sendCost'] ?? '0.00';
  115. //PC端开单
  116. if (isset($post['clearType'])) {
  117. $clearType = $post['clearType'];
  118. if ($clearType == OrderClass::CLEAR_DEBT) {
  119. $post['debt'] = OrderClass::DEBT_YES;
  120. $post['payWay'] = dict::getDict('payWay', 'debtPay');
  121. } else {
  122. $post['debt'] = OrderClass::DEBT_NO;
  123. }
  124. }
  125. $debt = $post['debt'] ?? OrderClass::DEBT_YES;
  126. //PC端开单
  127. if (isset($post['getType'])) {
  128. util::fail('PC端开单未开放');
  129. $getType = $post['getType'];
  130. //自取
  131. if ($getType == 1) {
  132. $post['sendType'] = OrderClass::SEND_TYPE_NO;
  133. } else {
  134. //选择配送时,在发货时让商家自己再确认一下用什么方式
  135. $post['sendType'] = OrderClass::SEND_TYPE_UNKNOWN;
  136. }
  137. }
  138. //员工不能开已收款订单
  139. if ($debt == OrderClass::DEBT_NO) {
  140. $shopAdmin = $this->shopAdmin;
  141. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  142. util::fail('只能开欠款单');
  143. }
  144. }
  145. $productJson = $post['product'] ?? '';
  146. if (empty($productJson)) {
  147. util::fail('请选择花材');
  148. }
  149. $productList = json_decode($productJson, true);
  150. if (empty($productList)) {
  151. util::fail('请选择花材');
  152. }
  153. $connection = Yii::$app->db;//事务处理
  154. $transaction = $connection->beginTransaction();
  155. try {
  156. //判断花材有效性
  157. ProductClass::valid($productList, $this->shopId);
  158. $post['product'] = $productList;
  159. //商家开单订单有效期更长(30)天
  160. $validTime = dict::getDict('kd_order_valid');
  161. $current = time() + $validTime;
  162. $post['deadline'] = date("Y-m-d H:i:s", $current);
  163. $return = OrderService::createOrder($post, $custom);
  164. $transaction->commit();
  165. util::success($return);
  166. } catch (\Exception $e) {
  167. $transaction->rollBack();
  168. Yii::error("下单失败原因:" . $e->getMessage());
  169. util::fail('下单失败');
  170. }
  171. }
  172. //修改订单 ssh 20210810
  173. public function actionUpdateOrder()
  174. {
  175. $post = Yii::$app->request->post();
  176. if (isset($post['newVersion']) == false) {
  177. util::fail('请升级小程序');
  178. }
  179. unset($post['newVersion']);
  180. //PC端开单
  181. if (isset($post['clearType'])) {
  182. $clearType = $post['clearType'];
  183. if ($clearType == OrderClass::CLEAR_DEBT) {
  184. $post['debt'] = OrderClass::DEBT_YES;
  185. $post['payWay'] = dict::getDict('payWay', 'debtPay');
  186. } else {
  187. $post['debt'] = OrderClass::DEBT_NO;
  188. }
  189. }
  190. //员工不能开已收款订单
  191. $debt = $post['debt'] ?? OrderClass::DEBT_YES;
  192. if ($debt == OrderClass::DEBT_NO) {
  193. $shopAdmin = $this->shopAdmin;
  194. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  195. util::fail('不能开已收款订单');
  196. }
  197. }
  198. //PC端开单
  199. if (isset($post['getType'])) {
  200. util::fail('暂不支持PC端修改订单');
  201. $getType = $post['getType'];
  202. //自取
  203. if ($getType == 1) {
  204. $post['sendType'] = OrderClass::SEND_TYPE_NO;
  205. } else {
  206. //选择配送时,在发货时让商家自己再确认一下用什么方式
  207. $post['sendType'] = OrderClass::SEND_TYPE_UNKNOWN;
  208. }
  209. }
  210. //商家开非自取订单,默认订单是待收款的,debt字段为非欠款
  211. if (isset($post['sendType']) && $post['sendType'] != OrderClass::SEND_TYPE_NO) {
  212. $post['debt'] = OrderClass::DEBT_NO;
  213. }
  214. $id = $post['id'] ?? 0;
  215. $order = OrderClass::getLockById($id);
  216. if (empty($order)) {
  217. util::fail('没有找到订单');
  218. }
  219. OrderClass::valid($order, $this->shopId);
  220. unset($post['id']);
  221. $shopAdmin = $this->shopAdmin;
  222. $post['shopAdminId'] = $this->shopAdminId;
  223. $post['shopAdminName'] = $shopAdmin->name ?? '';
  224. $post['fromType'] = 1;
  225. $post['sendCost'] = $post['sendCost'] ?? '0.00';
  226. $productJson = $post['product'] ?? '';
  227. if (empty($productJson)) {
  228. util::fail('请选择花材');
  229. }
  230. $productList = json_decode($productJson, true);
  231. if (empty($productList)) {
  232. util::fail('请选择花材');
  233. }
  234. $connection = Yii::$app->db;//事务处理
  235. $transaction = $connection->beginTransaction();
  236. try {
  237. //判断花材有效性
  238. ProductClass::valid($productList, $this->shopId);
  239. $post['product'] = $productList;
  240. //商家开单订单有效期更长(30)天
  241. $validTime = dict::getDict('kd_order_valid');
  242. $current = time() + $validTime;
  243. $post['deadline'] = date("Y-m-d H:i:s", $current);
  244. $upData = [
  245. 'payWay' => $post['payWay'],
  246. 'sendType' => $post['sendType'],
  247. 'debt' => $post['debt'],
  248. 'needPrint' => $post['needPrint'],
  249. 'modifyPrice' => $post['modifyPrice'],
  250. 'shopAdminId' => $post['shopAdminId'],
  251. 'shopAdminName' => $post['shopAdminName'],
  252. 'fromType' => 1,
  253. 'sendCost' => $post['sendCost'],
  254. 'product' => $post['product'],
  255. 'deadline' => $post['deadline'],
  256. 'debt' => $post['debt'],
  257. ];
  258. $return = OrderService::updateOrder($order, $upData);
  259. $transaction->commit();
  260. util::success($return);
  261. } catch (\Exception $e) {
  262. $transaction->rollBack();
  263. Yii::error("下单失败原因:" . $e->getMessage());
  264. util::fail('下单失败');
  265. }
  266. }
  267. //延期支付 ssh 2021.1.19
  268. public function actionDebt()
  269. {
  270. $get = Yii::$app->request->get();
  271. $id = $get['id'] ?? 0;
  272. $info = OrderService::getOrderInfo($id);
  273. OrderClass::valid($info, $this->shopId);
  274. OrderClass::debt($info, $this->shop);
  275. util::complete();
  276. }
  277. //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
  278. public function actionWxPay()
  279. {
  280. ini_set('date.timezone', 'Asia/Shanghai');
  281. $post = Yii::$app->request->post();
  282. $couponId = $post['couponId'] ?? 0;
  283. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  284. $order = OrderClass::getByOrderSn($orderSn);
  285. if (empty($order)) {
  286. util::fail('订单号无效');
  287. }
  288. $id = $order['id'];
  289. //支付前验证订单有效性
  290. if (isset($order['adminId']) == false || $order['adminId'] != $this->adminId) {
  291. util::fail('没有权限操作');
  292. }
  293. $name = $order['orderName'] ?? '购买商品';
  294. $totalFee = $order['actPrice'];
  295. //强制使用小程序的miniOpenId
  296. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  297. if (empty($openId)) {
  298. util::fail('没有找到openId');
  299. }
  300. $capitalType = dict::getDict('capitalType', 'xhGhsOrder', 'id');
  301. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  302. $wxPayType = 0;
  303. if (httpUtil::isMiniProgram()) {
  304. $wxPayType = 1;
  305. }
  306. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  307. //订单30分钟后过期
  308. $now = time();
  309. $expireTime = $now + 1800;
  310. $wx = Yii::getAlias("@vendor/weixin");
  311. require_once($wx . '/lib/WxPay.Api.php');
  312. require_once($wx . '/example/WxPay.JsApiPay.php');
  313. $input = new \WxPayUnifiedOrder();
  314. $input->SetBody($name);
  315. $input->SetOut_trade_no($orderSn);
  316. $input->SetTotal_fee($totalFee * 100);
  317. $input->SetTime_start(date("YmdHis", $now));
  318. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  319. $input->SetTime_expire(date("YmdHis", $expireTime));
  320. $input->SetNotify_url(Yii::$app->params['ghsHost'] . '/notice/wx-callback/');
  321. $input->SetTrade_type("JSAPI");
  322. //花卉宝代为申请的微信支付
  323. //$merchantExtend = $this->sjExtend->attributes;
  324. $merchantExtend = WxOpenClass::getGhsWxInfo();
  325. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  326. $input->SetSub_openid($openId);
  327. } else {
  328. $input->SetOpenid($openId);
  329. }
  330. //强制使用小程序的miniAppId
  331. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  332. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  333. $tools = new \JsApiPay();
  334. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  335. $newParams = json_decode($jsApiParameters, true);
  336. //微信不能修改价格
  337. $current = date("Y-m-d H:i:s");
  338. $updateData = ['deadline' => $current, 'modPrice' => 0];
  339. OrderClass::updateById($id, $updateData);
  340. util::success($newParams);
  341. }
  342. //获取订单详情 ssh 2021.1.21
  343. public function actionDetail()
  344. {
  345. $get = Yii::$app->request->get();
  346. $id = $get['id'] ?? 0;
  347. $info = OrderService::getOrderInfo($id, true, true, true, false);
  348. OrderClass::valid($info, $this->shopId);
  349. //是否有云打印判断
  350. $hasCloudPrint = 0;
  351. $shopExt = $this->shopExt;
  352. if (isset($shopExt->printSn) && !empty($shopExt->printSn) && isset($shopExt->printKey) && !empty($shopExt->printKey)) {
  353. $hasCloudPrint = 1;
  354. }
  355. $info['hasCloudPrint'] = $hasCloudPrint;
  356. //店长信息
  357. $customShopId = $info['customShopId'] ?? 0;
  358. $superInfo = ShopAdminClass::getSuper($customShopId);
  359. $info['customSuper'] = $superInfo;
  360. $purchaseId = $info['purchaseId'] ?? 0;
  361. $cg = PurchaseClass::getById($purchaseId);
  362. $info['cgInfo'] = $cg;
  363. util::success($info);
  364. }
  365. //自取免发货流程处理 ssh 2021.1.22
  366. public function actionWithoutSend()
  367. {
  368. $get = Yii::$app->request->get();
  369. $id = $get['id'] ?? 0;
  370. $info = OrderService::getById($id, true);
  371. OrderClass::valid($info, $this->shopId);
  372. $purchaseId = $info->purchaseId;
  373. $purchase = PurchaseClass::getById($purchaseId, true);
  374. OrderService::withoutSend($info, $purchase);
  375. util::complete();
  376. }
  377. //本店送 ssh 2021.1.24
  378. public function actionSelfSend()
  379. {
  380. $get = Yii::$app->request->get();
  381. $id = isset($get['id']) ? $get['id'] : 0;
  382. $info = OrderClass::getById($id, true);
  383. OrderClass::valid($info->attributes, $this->shopId);
  384. $connection = Yii::$app->db;
  385. $transaction = $connection->beginTransaction();
  386. try {
  387. OrderClass::selfSend($info);
  388. $transaction->commit();
  389. } catch (\Exception $exception) {
  390. $transaction->rollBack();
  391. Yii::info("本店送操作报错:" . $exception->getMessage());
  392. util::fail('操作失败');
  393. }
  394. util::complete();
  395. }
  396. //运费计算 ssh 2021.1.24
  397. public function actionFreight()
  398. {
  399. //2021.3.28 接入达达
  400. $get = Yii::$app->request->get();
  401. $post = Yii::$app->request->post();
  402. if (empty($get)) {
  403. $get = $post;
  404. }
  405. $orderId = $get['id'] ?? 0; //订单ID
  406. $customId = $get['customId'] ?? 0;
  407. if (empty($orderId)) {
  408. //自定义运费
  409. //freight::getCost();
  410. if (empty($customId)) {
  411. util::fail("请选择客户");
  412. }
  413. $customInfo = CustomClass::getById($customId);
  414. if (empty($customInfo)) {
  415. util::fail('没有找到客户');
  416. }
  417. $shop = $this->shop;
  418. //花材信息
  419. $productJson = $get['product'] ?? '';
  420. // $productJson = '[{"productId":31993,"bigNum":1,"smallNum":0}]';
  421. if (empty($productJson)) {
  422. util::fail('请选择花材');
  423. }
  424. $productList = json_decode($productJson, true);
  425. if (empty($productList)) {
  426. util::fail('请选择花材');
  427. }
  428. $productList = ProductClass::mergeItemInfo($productList);
  429. $weight = 0;
  430. $price = 0;
  431. $productData = ProductClass::getProductMapData($productList);
  432. $bigNum = 0;
  433. foreach ($productList as $key => $val) {
  434. $productId = $val['productId'];
  435. $w = ProductClass::getWeight($val['productId'], $val['bigNum'], $val['smallNum']);
  436. $bigNum += $val['bigNum'];
  437. $weight = bcadd($w, $weight, 2);
  438. $ratio = $productData[$productId]['ratio'] ?? 0;
  439. //大小数量合并多少扎
  440. $itemNum = ProductClass::mergeItemNum($val['bigNum'], $val['smallNum'], $ratio);
  441. //售卖价格
  442. $itemPrice = $productData[$productId]['price'] ?? 0;
  443. //合计价格
  444. $price = bcadd($price, bcmul($itemNum, $itemPrice, 2), 2);
  445. }
  446. //没有经纬度的客户运费直接返回空 shish 20210612
  447. if (empty($customInfo['lat']) || empty($customInfo['long'])) {
  448. util::success(['sedCost' => '']);
  449. }
  450. $cost = freight::getCost($shop->lat, $shop->long, $customInfo['lat'], $customInfo['long'], $weight);
  451. util::success(['sedCost' => $cost]);
  452. }
  453. $province = $get['province'] ?? '';
  454. $city = $get['city'] ?? '';
  455. $address = $get['address'] ?? '';
  456. $floor = $get['floor'] ?? '';
  457. $customName = $get['customName'] ?? '';
  458. $customMobile = $get['customMobile'] ?? '';
  459. $fullAddress = $province . $city . $address . $floor;
  460. $sendTime = $get['sendTime'] ?? '';
  461. $lat = $get['lat'] ?? '';
  462. $lng = $get['long'] ?? '';
  463. if (empty($lat) || empty($lng) || empty($fullAddress)) {
  464. util::fail('请填写客户地址');
  465. }
  466. ini_set('date.timezone', 'Asia/Shanghai');
  467. if (!empty($sendTime)) {
  468. //配送时间不为空
  469. // 预约发单时间(预约时间unix时间戳(10位),精确到分;整分钟为间隔,并且需要至少提前5分钟预约
  470. $sendTimeInt = strtotime($sendTime);
  471. $now = time();
  472. $diff = $sendTimeInt - $now;
  473. //因服务器写入时间,改为至少提前6分钟
  474. if ($diff <= 360) {
  475. util::fail('配送时间至少提前6分钟');
  476. }
  477. } else {
  478. //默认立即发送
  479. $sendTime = 0;
  480. }
  481. //更新订单表
  482. $info = OrderService::getOrderInfo($orderId);
  483. OrderClass::valid($info, $this->shopId);
  484. if ($sendTime) {
  485. $sendTime = date('Y-m-d H:i', strtotime($sendTime));
  486. } else {
  487. $sendTime = '0000-00-00 00:00:00';
  488. }
  489. $expressAddInfo = [
  490. 'customName' => $customName,
  491. 'customMobile' => $customMobile,
  492. 'province' => $province,
  493. 'city' => $city,
  494. 'address' => $address,
  495. 'floor' => $floor,
  496. 'lat' => $lat,
  497. 'long' => $lng,
  498. 'sendTime' => $sendTime,
  499. ];
  500. OrderClass::updateCustomExpressInfo($orderId, $expressAddInfo);
  501. //计算运费
  502. $info['province'] = $province;
  503. $info['city'] = $city;
  504. $info['address'] = $address;
  505. $info['floor'] = $floor;
  506. $info['fullAddress'] = $fullAddress;
  507. $info['lat'] = $lat;
  508. $info['long'] = $lng;
  509. $info['sendTime'] = $expressAddInfo['sendTime'];
  510. $res = DadaExpressServices::queryDeliverFee($info);
  511. util::success(['sedCost' => $res['fee']]);
  512. }
  513. //发快递和第三方配送 ssh 2021.1.24
  514. public function actionThirdSend()
  515. {
  516. $get = Yii::$app->request->get();
  517. $id = isset($get['id']) ? $get['id'] : 0;
  518. $info = OrderService::getById($id, true);
  519. OrderClass::valid($info->attributes, $this->shopId);
  520. if (empty($info->lat) || empty($info->long) || empty($info->fullAddress)) {
  521. util::fail('请填写客户地址');
  522. }
  523. $connection = Yii::$app->db;
  524. $transaction = $connection->beginTransaction();
  525. try {
  526. $respond = OrderClass::thirdSend($info, $get);
  527. $transaction->commit();
  528. util::success($respond);
  529. } catch (\Exception $exception) {
  530. $transaction->rollBack();
  531. Yii::info("发快递操作报错:" . $exception->getMessage());
  532. util::fail('操作失败' . $exception->getMessage());
  533. }
  534. }
  535. //确认送达 ssh 2021.1.24
  536. public function actionReach()
  537. {
  538. $get = Yii::$app->request->get();
  539. $id = isset($get['id']) ? $get['id'] : 0;
  540. $info = OrderClass::getById($id, true);
  541. OrderClass::valid($info->attributes, $this->shopId);
  542. $connection = Yii::$app->db;
  543. $transaction = $connection->beginTransaction();
  544. try {
  545. OrderService::reach($info);
  546. $transaction->commit();
  547. } catch (\Exception $exception) {
  548. $transaction->rollBack();
  549. Yii::info("送达操作报错:" . $exception->getMessage());
  550. util::fail('操作失败');
  551. }
  552. util::complete();
  553. }
  554. //下单要用到的相关信息 ssh 2019.12.6
  555. public function actionOrderRelate()
  556. {
  557. $regionTree = RegionService::tree();
  558. $shop = $this->shop->attributes;
  559. $freight = ['first' => 5, 'add' => 2];
  560. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  561. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'txMapKey' => $mapKey, 'merchant' => $shop];
  562. util::success($out);
  563. }
  564. //客户欠款的订单 ssh 2021.2.4
  565. public function actionDebtList()
  566. {
  567. $id = Yii::$app->request->get('id', 0);
  568. $info = CustomClass::getCustom($id);
  569. CustomClass::valid($info, $this->shopId);
  570. $where = ['customId' => $id, 'debt' => 1];
  571. $respond = OrderService::getDebtOrderList($where);
  572. $respond['customInfo'] = $info;
  573. util::success($respond);
  574. }
  575. //更新打印次数
  576. public function actionAddPrintNum()
  577. {
  578. $id = Yii::$app->request->get('id', 0);
  579. $order = OrderClass::getById($id, true);
  580. OrderClass::valid($order, $this->shopId);
  581. $order->printNum += 1;
  582. $order->save();
  583. util::complete();
  584. }
  585. //打印订单 shish 20210714
  586. public function actionCloudPrintOrder()
  587. {
  588. $id = Yii::$app->request->get('id', 0);
  589. $order = OrderClass::getById($id, true);
  590. OrderClass::valid($order, $this->shopId);
  591. //打印订单
  592. OrderClass::onlinePrint($order);
  593. $order->printNum += 1;
  594. $order->save();
  595. util::complete();
  596. }
  597. //订单确认完成 shish 20210809
  598. public function actionConfirmFinish()
  599. {
  600. $payWay = Yii::$app->request->get('payWay', -1);
  601. $id = Yii::$app->request->get('id', 0);
  602. $clearType = Yii::$app->request->get('clearType', 0);
  603. $connection = Yii::$app->db;
  604. $transaction = $connection->beginTransaction();
  605. try {
  606. $order = OrderClass::getById($id, true);
  607. OrderClass::valid($order, $this->shopId);
  608. $purchaseId = $order->purchaseId ?? 0;
  609. $purchase = PurchaseClass::getById($purchaseId, true);
  610. if (empty($purchase)) {
  611. util::fail('没有采购信息');
  612. }
  613. if ($clearType == 1) {
  614. //欠款
  615. $payWay = dict::getDict('payWay', 'debtPay');
  616. } elseif ($clearType == 2) {
  617. //已收款
  618. if ($payWay == -1) {
  619. util::fail('请选择收款方式');
  620. }
  621. $shopAdmin = $this->shopAdmin;
  622. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  623. util::fail('超管才能发起已收款');
  624. }
  625. } else {
  626. util::fail('请选择结算方式');
  627. }
  628. //不需要打印
  629. $order->needPrint = dict::getDict('needPrint', 'noNeed');
  630. $order->save();
  631. //支付
  632. PurchaseService::payAfter($purchase, $payWay);
  633. OrderService::payAfter($order, $payWay);
  634. //自己送
  635. $info = OrderClass::getById($id, true);
  636. OrderClass::selfSend($info);
  637. //确认送达,并且不需要微信通知
  638. Yii::$app->params['noNeedWxNotice'] = 1;
  639. $info = OrderClass::getById($id, true);
  640. OrderService::reach($info);
  641. $transaction->commit();
  642. util::complete('操作成功');
  643. } catch (\Exception $exception) {
  644. $transaction->rollBack();
  645. Yii::info("确认完成订单报错:" . $exception->getMessage());
  646. util::fail('操作失败');
  647. }
  648. }
  649. //取消订单 shish 20210810
  650. public function actionCancel()
  651. {
  652. $shopAdmin = $this->shopAdmin;
  653. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  654. util::fail('超管才能取消订单');
  655. }
  656. $id = Yii::$app->request->get('id', 0);
  657. $order = OrderClass::getLockById($id);
  658. OrderClass::valid($order, $this->shopId);
  659. OrderService::expire($order, true);
  660. util::complete();
  661. }
  662. }