OrderController.php 21 KB

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