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. $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. $post['sendTime'] = date("Y-m-d H:i:s");
  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. util::success($info);
  357. }
  358. //自取免发货流程处理 ssh 2021.1.22
  359. public function actionWithoutSend()
  360. {
  361. $get = Yii::$app->request->get();
  362. $id = $get['id'] ?? 0;
  363. $info = OrderService::getById($id, true);
  364. OrderClass::valid($info, $this->shopId);
  365. $purchaseId = $info->purchaseId;
  366. $purchase = PurchaseClass::getById($purchaseId, true);
  367. OrderService::withoutSend($info, $purchase);
  368. util::complete();
  369. }
  370. //本店送 ssh 2021.1.24
  371. public function actionSelfSend()
  372. {
  373. $get = Yii::$app->request->get();
  374. $id = isset($get['id']) ? $get['id'] : 0;
  375. $info = OrderClass::getById($id, true);
  376. OrderClass::valid($info->attributes, $this->shopId);
  377. $connection = Yii::$app->db;
  378. $transaction = $connection->beginTransaction();
  379. try {
  380. OrderClass::selfSend($info);
  381. $transaction->commit();
  382. } catch (\Exception $exception) {
  383. $transaction->rollBack();
  384. Yii::info("本店送操作报错:" . $exception->getMessage());
  385. util::fail('操作失败');
  386. }
  387. util::complete();
  388. }
  389. //运费计算 ssh 2021.1.24
  390. public function actionFreight()
  391. {
  392. //2021.3.28 接入达达
  393. $get = Yii::$app->request->get();
  394. $post = Yii::$app->request->post();
  395. if (empty($get)) {
  396. $get = $post;
  397. }
  398. $orderId = $get['id'] ?? 0; //订单ID
  399. $customId = $get['customId'] ?? 0;
  400. if (empty($orderId)) {
  401. //自定义运费
  402. //freight::getCost();
  403. if (empty($customId)) {
  404. util::fail("请选择客户");
  405. }
  406. $customInfo = CustomClass::getById($customId);
  407. if (empty($customInfo)) {
  408. util::fail('没有找到客户');
  409. }
  410. $shop = $this->shop;
  411. //花材信息
  412. $productJson = $get['product'] ?? '';
  413. // $productJson = '[{"productId":31993,"bigNum":1,"smallNum":0}]';
  414. if (empty($productJson)) {
  415. util::fail('请选择花材');
  416. }
  417. $productList = json_decode($productJson, true);
  418. if (empty($productList)) {
  419. util::fail('请选择花材');
  420. }
  421. $productList = ProductClass::mergeItemInfo($productList);
  422. $weight = 0;
  423. $price = 0;
  424. $productData = ProductClass::getProductMapData($productList);
  425. $bigNum = 0;
  426. foreach ($productList as $key => $val) {
  427. $productId = $val['productId'];
  428. $w = ProductClass::getWeight($val['productId'], $val['bigNum'], $val['smallNum']);
  429. $bigNum += $val['bigNum'];
  430. $weight = bcadd($w, $weight, 2);
  431. $ratio = $productData[$productId]['ratio'] ?? 0;
  432. //大小数量合并多少扎
  433. $itemNum = ProductClass::mergeItemNum($val['bigNum'], $val['smallNum'], $ratio);
  434. //售卖价格
  435. $itemPrice = $productData[$productId]['price'] ?? 0;
  436. //合计价格
  437. $price = bcadd($price, bcmul($itemNum, $itemPrice, 2), 2);
  438. }
  439. //没有经纬度的客户运费直接返回空 shish 20210612
  440. if (empty($customInfo['lat']) || empty($customInfo['long'])) {
  441. util::success(['sedCost' => '']);
  442. }
  443. $cost = freight::getCost($shop->lat, $shop->long, $customInfo['lat'], $customInfo['long'], $weight);
  444. util::success(['sedCost' => $cost]);
  445. }
  446. $province = $get['province'] ?? '';
  447. $city = $get['city'] ?? '';
  448. $address = $get['address'] ?? '';
  449. $floor = $get['floor'] ?? '';
  450. $customName = $get['customName'] ?? '';
  451. $customMobile = $get['customMobile'] ?? '';
  452. $fullAddress = $province . $city . $address . $floor;
  453. $sendTime = $get['sendTime'] ?? '';
  454. $lat = $get['lat'] ?? '';
  455. $lng = $get['long'] ?? '';
  456. if (empty($lat) || empty($lng) || empty($fullAddress)) {
  457. util::fail('请填写客户地址');
  458. }
  459. ini_set('date.timezone', 'Asia/Shanghai');
  460. if (!empty($sendTime)) {
  461. //配送时间不为空
  462. // 预约发单时间(预约时间unix时间戳(10位),精确到分;整分钟为间隔,并且需要至少提前5分钟预约
  463. $sendTimeInt = strtotime($sendTime);
  464. $now = time();
  465. $diff = $sendTimeInt - $now;
  466. //因服务器写入时间,改为至少提前6分钟
  467. if ($diff <= 360) {
  468. util::fail('配送时间至少提前6分钟');
  469. }
  470. } else {
  471. //默认立即发送
  472. $sendTime = 0;
  473. }
  474. //更新订单表
  475. $info = OrderService::getOrderInfo($orderId);
  476. OrderClass::valid($info, $this->shopId);
  477. if ($sendTime) {
  478. $sendTime = date('Y-m-d H:i', strtotime($sendTime));
  479. } else {
  480. $sendTime = '0000-00-00 00:00:00';
  481. }
  482. $expressAddInfo = [
  483. 'customName' => $customName,
  484. 'customMobile' => $customMobile,
  485. 'province' => $province,
  486. 'city' => $city,
  487. 'address' => $address,
  488. 'floor' => $floor,
  489. 'lat' => $lat,
  490. 'long' => $lng,
  491. 'sendTime' => $sendTime,
  492. ];
  493. OrderClass::updateCustomExpressInfo($orderId, $expressAddInfo);
  494. //计算运费
  495. $info['province'] = $province;
  496. $info['city'] = $city;
  497. $info['address'] = $address;
  498. $info['floor'] = $floor;
  499. $info['fullAddress'] = $fullAddress;
  500. $info['lat'] = $lat;
  501. $info['long'] = $lng;
  502. $info['sendTime'] = $expressAddInfo['sendTime'];
  503. $res = DadaExpressServices::queryDeliverFee($info);
  504. util::success(['sedCost' => $res['fee']]);
  505. }
  506. //发快递和第三方配送 ssh 2021.1.24
  507. public function actionThirdSend()
  508. {
  509. $get = Yii::$app->request->get();
  510. $id = isset($get['id']) ? $get['id'] : 0;
  511. $info = OrderService::getById($id, true);
  512. OrderClass::valid($info->attributes, $this->shopId);
  513. if (empty($info->lat) || empty($info->long) || empty($info->fullAddress)) {
  514. util::fail('请填写客户地址');
  515. }
  516. $connection = Yii::$app->db;
  517. $transaction = $connection->beginTransaction();
  518. try {
  519. $respond = OrderClass::thirdSend($info, $get);
  520. $transaction->commit();
  521. util::success($respond);
  522. } catch (\Exception $exception) {
  523. $transaction->rollBack();
  524. Yii::info("发快递操作报错:" . $exception->getMessage());
  525. util::fail('操作失败' . $exception->getMessage());
  526. }
  527. }
  528. //确认送达 ssh 2021.1.24
  529. public function actionReach()
  530. {
  531. $get = Yii::$app->request->get();
  532. $id = isset($get['id']) ? $get['id'] : 0;
  533. $info = OrderClass::getById($id, true);
  534. OrderClass::valid($info->attributes, $this->shopId);
  535. $connection = Yii::$app->db;
  536. $transaction = $connection->beginTransaction();
  537. try {
  538. OrderService::reach($info);
  539. $transaction->commit();
  540. } catch (\Exception $exception) {
  541. $transaction->rollBack();
  542. Yii::info("送达操作报错:" . $exception->getMessage());
  543. util::fail('操作失败');
  544. }
  545. util::complete();
  546. }
  547. //下单要用到的相关信息 ssh 2019.12.6
  548. public function actionOrderRelate()
  549. {
  550. $regionTree = RegionService::tree();
  551. $shop = $this->shop->attributes;
  552. $freight = ['first' => 5, 'add' => 2];
  553. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  554. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'txMapKey' => $mapKey, 'merchant' => $shop];
  555. util::success($out);
  556. }
  557. //客户欠款的订单 ssh 2021.2.4
  558. public function actionDebtList()
  559. {
  560. $id = Yii::$app->request->get('id', 0);
  561. $info = CustomClass::getCustom($id);
  562. CustomClass::valid($info, $this->shopId);
  563. $where = ['customId' => $id, 'debt' => 1];
  564. $respond = OrderService::getDebtOrderList($where);
  565. $respond['customInfo'] = $info;
  566. util::success($respond);
  567. }
  568. //更新打印次数
  569. public function actionAddPrintNum()
  570. {
  571. $id = Yii::$app->request->get('id', 0);
  572. $order = OrderClass::getById($id, true);
  573. OrderClass::valid($order, $this->shopId);
  574. $order->printNum += 1;
  575. $order->save();
  576. util::complete();
  577. }
  578. //打印订单 shish 20210714
  579. public function actionCloudPrintOrder()
  580. {
  581. $id = Yii::$app->request->get('id', 0);
  582. $order = OrderClass::getById($id, true);
  583. OrderClass::valid($order, $this->shopId);
  584. //打印订单
  585. OrderClass::onlinePrint($order);
  586. $order->printNum += 1;
  587. $order->save();
  588. util::complete();
  589. }
  590. //订单确认完成 shish 20210809
  591. public function actionConfirmFinish()
  592. {
  593. $payWay = Yii::$app->request->get('payWay', -1);
  594. $id = Yii::$app->request->get('id', 0);
  595. $clearType = Yii::$app->request->get('clearType', 0);
  596. $connection = Yii::$app->db;
  597. $transaction = $connection->beginTransaction();
  598. try {
  599. $order = OrderClass::getById($id, true);
  600. OrderClass::valid($order, $this->shopId);
  601. $purchaseId = $order->purchaseId ?? 0;
  602. $purchase = PurchaseClass::getById($purchaseId, true);
  603. if (empty($purchase)) {
  604. util::fail('没有采购信息');
  605. }
  606. if ($clearType == 1) {
  607. //欠款
  608. $payWay = dict::getDict('payWay', 'debtPay');
  609. } elseif ($clearType == 2) {
  610. //已收款
  611. if ($payWay == -1) {
  612. util::fail('请选择收款方式');
  613. }
  614. $shopAdmin = $this->shopAdmin;
  615. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  616. util::fail('超管才能发起已收款');
  617. }
  618. } else {
  619. util::fail('请选择结算方式');
  620. }
  621. //不需要打印
  622. $order->needPrint = dict::getDict('needPrint', 'noNeed');
  623. $order->save();
  624. //支付
  625. PurchaseService::payAfter($purchase, $payWay);
  626. OrderService::payAfter($order, $payWay);
  627. //自己送
  628. $info = OrderClass::getById($id, true);
  629. OrderClass::selfSend($info);
  630. //确认送达,并且不需要微信通知
  631. Yii::$app->params['noNeedWxNotice'] = 1;
  632. $info = OrderClass::getById($id, true);
  633. OrderService::reach($info);
  634. $transaction->commit();
  635. util::complete('操作成功');
  636. } catch (\Exception $exception) {
  637. $transaction->rollBack();
  638. Yii::info("确认完成订单报错:" . $exception->getMessage());
  639. util::fail('操作失败');
  640. }
  641. }
  642. //取消订单 shish 20210810
  643. public function actionCancel()
  644. {
  645. $shopAdmin = $this->shopAdmin;
  646. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  647. util::fail('超管才能取消订单');
  648. }
  649. $id = Yii::$app->request->get('id', 0);
  650. $order = OrderClass::getLockById($id);
  651. OrderClass::valid($order, $this->shopId);
  652. OrderService::expire($order, true);
  653. util::complete();
  654. }
  655. }