OrderController.php 26 KB

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