OrderController.php 26 KB

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