OrderController.php 21 KB

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