OrderController.php 26 KB

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