OrderController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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\wx\classes\WxOpenClass;
  8. use bizGhs\custom\classes\CustomClass;
  9. use bizGhs\order\classes\OrderClass;
  10. use bizGhs\order\services\OrderService;
  11. use bizHd\saas\services\RegionService;
  12. use bizGhs\product\classes\ProductClass;
  13. use common\components\dada\dada;
  14. use common\components\dict;
  15. use common\components\dateUtil;
  16. use common\components\freight;
  17. use common\components\httpUtil;
  18. use Yii;
  19. use common\components\util;
  20. use bizGhs\order\classes\RefundOrderClass;
  21. class OrderController extends BaseController
  22. {
  23. public $guestAccess = ['create-order', 'order-relate', 'fast-pay'];
  24. //订单列表 ssh 2021.1.20
  25. public function actionList()
  26. {
  27. $get = Yii::$app->request->get();
  28. $status = $get['status'] ?? 0;
  29. if (!empty($status)) {
  30. $where['status'] = $status;
  31. }
  32. $where['sjId'] = $this->sjId;
  33. if (isset($get['shopId'])) {
  34. $shopId = $get['shopId'] ?? 0;
  35. if (!empty($shopId)) {
  36. $where['shopId'] = $this->shopId;
  37. }
  38. } else {
  39. $where['shopId'] = $this->shopId;
  40. }
  41. $searchTime = $get['searchTime'] ?? '';
  42. if (!empty($searchTime)) {
  43. $startTime = $get['startTime'] ?? '';
  44. $endTime = $get['endTime'] ?? '';
  45. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  46. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  47. }
  48. $name = $get['name'] ?? '';
  49. if (!empty($name)) {
  50. //订单编号
  51. if (strpos($name, 'XSD') === 0) {
  52. $where['orderSn'] = $name;
  53. } elseif (preg_match("/^[a-zA-Z\s]+$/", $name)) {
  54. $where['customNamePy'] = ['like', $name];
  55. } else {
  56. $where['customName'] = ['like', $name];
  57. }
  58. }
  59. $respond = OrderClass::getOrderList($where);
  60. $respond['shop'] = $this->shop;
  61. $respond['shopExt'] = $this->shopExt;
  62. util::success($respond);
  63. }
  64. //商城下单操作 ssh 2021.1.14
  65. public function actionCreateOrder()
  66. {
  67. $post = Yii::$app->request->post();
  68. $shopId = $this->shopId;
  69. $customId = $post['customId'] ?? 0;
  70. $post['customId'] = $customId;
  71. //开单必须选客户
  72. if (empty($customId)) {
  73. util::fail('请选择客户');
  74. }
  75. $custom = CustomClass::getCustom($customId);
  76. if (empty($custom)) {
  77. util::fail('请选客户哦');
  78. }
  79. CustomClass::valid($custom, $this->shopId);
  80. $post['ghsId'] = $custom['ghsId'] ?? 0;
  81. $post['customMobile'] = $custom['mobile'] ?? '';
  82. $customName = $custom['name'] ?? '';
  83. $post['customName'] = $customName;
  84. $post['customNamePy'] = $custom['py'] ?? '';
  85. $post['customAvatar'] = $custom['shortSmallAvatar'] ?? '';
  86. $post['shopAdminId'] = $this->shopAdminId;
  87. $post['province'] = $custom['province'] ?? '';
  88. $post['city'] = $custom['city'] ?? '';
  89. $post['dist'] = $custom['dist'] ?? '';
  90. $post['address'] = $custom['address'] ?? '';
  91. $post['floor'] = $custom['floor'] ?? '';
  92. $post['fullAddress'] = $custom['fullAddress'] ?? '';
  93. $post['showAddress'] = $custom['showAddress'] ?? '';
  94. $shopAdmin = $this->shopAdmin->attributes;
  95. $post['shopAdminName'] = $shopAdmin['name'] ?? '';
  96. $post['sjId'] = $this->sjId;
  97. $post['shopId'] = $shopId;
  98. $post['fromType'] = 1;
  99. $post['expressId'] = $post['expressId'] ?? 0;
  100. $post['sendCost'] = $post['sendCost'] ?? '0.00';
  101. //$post['payWay'] = dict::getDict('payWay', 'wxPay');
  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. $return = OrderService::createOrder($post, $custom);
  147. $transaction->commit();
  148. util::success($return);
  149. } catch (\Exception $e) {
  150. $transaction->rollBack();
  151. Yii::error("下单失败原因:" . $e->getMessage());
  152. util::fail('下单失败');
  153. }
  154. }
  155. //延期支付 ssh 2021.1.19
  156. public function actionDebt()
  157. {
  158. $get = Yii::$app->request->get();
  159. $id = $get['id'] ?? 0;
  160. $info = OrderService::getOrderInfo($id);
  161. OrderClass::valid($info, $this->shopId);
  162. OrderClass::debt($info, $this->shop);
  163. util::complete();
  164. }
  165. //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
  166. public function actionWxPay()
  167. {
  168. ini_set('date.timezone', 'Asia/Shanghai');
  169. $post = Yii::$app->request->post();
  170. $couponId = $post['couponId'] ?? 0;
  171. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  172. $order = OrderClass::getByOrderSn($orderSn);
  173. if (empty($order)) {
  174. util::fail('订单号无效');
  175. }
  176. $id = $order['id'];
  177. //支付前验证订单有效性
  178. if (isset($order['adminId']) == false || $order['adminId'] != $this->adminId) {
  179. util::fail('没有权限操作');
  180. }
  181. $name = $order['orderName'] ?? '购买商品';
  182. $totalFee = $order['actPrice'];
  183. //强制使用小程序的miniOpenId
  184. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  185. if (empty($openId)) {
  186. util::fail('没有找到openId');
  187. }
  188. $capitalType = dict::getDict('capitalType', 'xhGhsOrder', 'id');
  189. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  190. $wxPayType = 0;
  191. if (httpUtil::isMiniProgram()) {
  192. $wxPayType = 1;
  193. }
  194. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  195. //订单30分钟后过期
  196. $now = time();
  197. $expireTime = $now + 1800;
  198. $wx = Yii::getAlias("@vendor/weixin");
  199. require_once($wx . '/lib/WxPay.Api.php');
  200. require_once($wx . '/example/WxPay.JsApiPay.php');
  201. $input = new \WxPayUnifiedOrder();
  202. $input->SetBody($name);
  203. $input->SetOut_trade_no($orderSn);
  204. $input->SetTotal_fee($totalFee * 100);
  205. $input->SetTime_start(date("YmdHis", $now));
  206. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  207. $input->SetTime_expire(date("YmdHis", $expireTime));
  208. $input->SetNotify_url(Yii::$app->params['ghsHost'] . '/notice/wx-callback/');
  209. $input->SetTrade_type("JSAPI");
  210. //花卉宝代为申请的微信支付
  211. //$merchantExtend = $this->sjExtend->attributes;
  212. $merchantExtend = WxOpenClass::getGhsWxInfo();
  213. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  214. $input->SetSub_openid($openId);
  215. } else {
  216. $input->SetOpenid($openId);
  217. }
  218. //强制使用小程序的miniAppId
  219. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  220. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  221. $tools = new \JsApiPay();
  222. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  223. $newParams = json_decode($jsApiParameters, true);
  224. //微信不能修改价格
  225. $current = date("Y-m-d H:i:s");
  226. $updateData = ['deadline' => $current, 'modPrice' => 0];
  227. OrderClass::updateById($id, $updateData);
  228. util::success($newParams);
  229. }
  230. //获取订单详情 ssh 2021.1.21
  231. public function actionDetail()
  232. {
  233. $get = Yii::$app->request->get();
  234. $id = $get['id'] ?? 0;
  235. $info = OrderService::getOrderInfo($id, true, true, true, false);
  236. OrderClass::valid($info, $this->shopId);
  237. //是否有云打印判断
  238. $hasCloudPrint = 0;
  239. $shopExt = $this->shopExt;
  240. if (isset($shopExt->printSn) && !empty($shopExt->printSn) && isset($shopExt->printKey) && !empty($shopExt->printKey)) {
  241. $hasCloudPrint = 1;
  242. }
  243. $info['hasCloudPrint'] = $hasCloudPrint;
  244. util::success($info);
  245. }
  246. //自取免发货流程处理 ssh 2021.1.22
  247. public function actionWithoutSend()
  248. {
  249. $get = Yii::$app->request->get();
  250. $id = $get['id'] ?? 0;
  251. $info = OrderService::getById($id, true);
  252. OrderClass::valid($info, $this->shopId);
  253. $purchaseId = $info->purchaseId;
  254. $purchase = PurchaseClass::getById($purchaseId, true);
  255. OrderService::withoutSend($info, $purchase);
  256. util::complete();
  257. }
  258. //本店送 ssh 2021.1.24
  259. public function actionSelfSend()
  260. {
  261. $get = Yii::$app->request->get();
  262. $id = isset($get['id']) ? $get['id'] : 0;
  263. $info = OrderClass::getById($id, true);
  264. OrderClass::valid($info->attributes, $this->shopId);
  265. $connection = Yii::$app->db;
  266. $transaction = $connection->beginTransaction();
  267. try {
  268. OrderClass::selfSend($info);
  269. $transaction->commit();
  270. } catch (\Exception $exception) {
  271. $transaction->rollBack();
  272. Yii::info("本店送操作报错:" . $exception->getMessage());
  273. util::fail('操作失败');
  274. }
  275. util::complete();
  276. }
  277. //运费计算 ssh 2021.1.24
  278. public function actionFreight()
  279. {
  280. //2021.3.28 接入达达
  281. $get = Yii::$app->request->get();
  282. $post = Yii::$app->request->post();
  283. if (empty($get)) {
  284. $get = $post;
  285. }
  286. $orderId = $get['id'] ?? 0; //订单ID
  287. $customId = $get['customId'] ?? 0;
  288. if (empty($orderId)) {
  289. //自定义运费
  290. //freight::getCost();
  291. if (empty($customId)) {
  292. util::fail("请选择客户");
  293. }
  294. $customInfo = CustomClass::getById($customId);
  295. if (empty($customInfo)) {
  296. util::fail('没有找到客户');
  297. }
  298. $shop = $this->shop;
  299. //花材信息
  300. $productJson = $get['product'] ?? '';
  301. // $productJson = '[{"productId":31993,"bigNum":1,"smallNum":0}]';
  302. if (empty($productJson)) {
  303. util::fail('请选择花材');
  304. }
  305. $productList = json_decode($productJson, true);
  306. if (empty($productList)) {
  307. util::fail('请选择花材');
  308. }
  309. $productList = ProductClass::mergeItemInfo($productList);
  310. $weight = 0;
  311. $price = 0;
  312. $productData = ProductClass::getProductMapData($productList);
  313. $bigNum = 0;
  314. foreach ($productList as $key => $val) {
  315. $productId = $val['productId'];
  316. $w = ProductClass::getWeight($val['productId'], $val['bigNum'], $val['smallNum']);
  317. $bigNum += $val['bigNum'];
  318. $weight = bcadd($w, $weight, 2);
  319. $ratio = $productData[$productId]['ratio'] ?? 0;
  320. //大小数量合并多少扎
  321. $itemNum = ProductClass::mergeItemNum($val['bigNum'], $val['smallNum'], $ratio);
  322. //售卖价格
  323. $itemPrice = $productData[$productId]['price'] ?? 0;
  324. //合计价格
  325. $price = bcadd($price, bcmul($itemNum, $itemPrice, 2), 2);
  326. }
  327. //没有经纬度的客户运费直接返回空 shish 20210612
  328. if (empty($customInfo['lat']) || empty($customInfo['long'])) {
  329. util::success(['sedCost' => '']);
  330. }
  331. $cost = freight::getCost($shop->lat, $shop->long, $customInfo['lat'], $customInfo['long'], $weight);
  332. util::success(['sedCost' => $cost]);
  333. }
  334. $province = $get['province'] ?? '';
  335. $city = $get['city'] ?? '';
  336. $address = $get['address'] ?? '';
  337. $floor = $get['floor'] ?? '';
  338. $customName = $get['customName'] ?? '';
  339. $customMobile = $get['customMobile'] ?? '';
  340. $fullAddress = $province . $city . $address . $floor;
  341. $sendTime = $get['sendTime'] ?? '';
  342. $lat = $get['lat'] ?? '';
  343. $lng = $get['long'] ?? '';
  344. if (empty($lat) || empty($lng) || empty($fullAddress)) {
  345. util::fail('请填写客户地址');
  346. }
  347. ini_set('date.timezone', 'Asia/Shanghai');
  348. if (!empty($sendTime)) {
  349. //配送时间不为空
  350. // 预约发单时间(预约时间unix时间戳(10位),精确到分;整分钟为间隔,并且需要至少提前5分钟预约
  351. $sendTimeInt = strtotime($sendTime);
  352. $now = time();
  353. $diff = $sendTimeInt - $now;
  354. //因服务器写入时间,改为至少提前6分钟
  355. if ($diff <= 360) {
  356. util::fail('配送时间至少提前6分钟');
  357. }
  358. } else {
  359. //默认立即发送
  360. $sendTime = 0;
  361. }
  362. //更新订单表
  363. $info = OrderService::getOrderInfo($orderId);
  364. OrderClass::valid($info, $this->shopId);
  365. if ($sendTime) {
  366. $sendTime = date('Y-m-d H:i', strtotime($sendTime));
  367. } else {
  368. $sendTime = '0000-00-00 00:00:00';
  369. }
  370. $expressAddInfo = [
  371. 'customName' => $customName,
  372. 'customMobile' => $customMobile,
  373. 'province' => $province,
  374. 'city' => $city,
  375. 'address' => $address,
  376. 'floor' => $floor,
  377. 'lat' => $lat,
  378. 'long' => $lng,
  379. 'sendTime' => $sendTime,
  380. ];
  381. OrderClass::updateCustomExpressInfo($orderId, $expressAddInfo);
  382. //计算运费
  383. $info['province'] = $province;
  384. $info['city'] = $city;
  385. $info['address'] = $address;
  386. $info['floor'] = $floor;
  387. $info['fullAddress'] = $fullAddress;
  388. $info['lat'] = $lat;
  389. $info['long'] = $lng;
  390. $info['sendTime'] = $expressAddInfo['sendTime'];
  391. $res = DadaExpressServices::queryDeliverFee($info);
  392. util::success(['sedCost' => $res['fee']]);
  393. }
  394. //发快递和第三方配送 ssh 2021.1.24
  395. public function actionThirdSend()
  396. {
  397. $get = Yii::$app->request->get();
  398. $id = isset($get['id']) ? $get['id'] : 0;
  399. $info = OrderService::getById($id, true);
  400. OrderClass::valid($info->attributes, $this->shopId);
  401. if (empty($info->lat) || empty($info->long) || empty($info->fullAddress)) {
  402. util::fail('请填写客户地址');
  403. }
  404. $connection = Yii::$app->db;
  405. $transaction = $connection->beginTransaction();
  406. try {
  407. $respond = OrderClass::thirdSend($info, $get);
  408. $transaction->commit();
  409. util::success($respond);
  410. } catch (\Exception $exception) {
  411. $transaction->rollBack();
  412. Yii::info("发快递操作报错:" . $exception->getMessage());
  413. util::fail('操作失败' . $exception->getMessage());
  414. }
  415. }
  416. //确认送达 ssh 2021.1.24
  417. public function actionReach()
  418. {
  419. $get = Yii::$app->request->get();
  420. $id = isset($get['id']) ? $get['id'] : 0;
  421. $info = OrderClass::getById($id, true);
  422. OrderClass::valid($info->attributes, $this->shopId);
  423. $connection = Yii::$app->db;
  424. $transaction = $connection->beginTransaction();
  425. try {
  426. OrderService::reach($info);
  427. $transaction->commit();
  428. } catch (\Exception $exception) {
  429. $transaction->rollBack();
  430. Yii::info("送达操作报错:" . $exception->getMessage());
  431. util::fail('操作失败');
  432. }
  433. util::complete();
  434. }
  435. //下单要用到的相关信息 ssh 2019.12.6
  436. public function actionOrderRelate()
  437. {
  438. $regionTree = RegionService::tree();
  439. $shop = $this->shop->attributes;
  440. $freight = ['first' => 5, 'add' => 2];
  441. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  442. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'txMapKey' => $mapKey, 'merchant' => $shop];
  443. util::success($out);
  444. }
  445. //客户欠款的订单 ssh 2021.2.4
  446. public function actionDebtList()
  447. {
  448. $id = Yii::$app->request->get('id', 0);
  449. $info = CustomClass::getCustom($id);
  450. CustomClass::valid($info, $this->shopId);
  451. $where = ['customId' => $id, 'debt' => 1];
  452. $respond = OrderService::getDebtOrderList($where);
  453. $respond['customInfo'] = $info;
  454. util::success($respond);
  455. }
  456. //更新打印次数
  457. public function actionAddPrintNum()
  458. {
  459. $id = Yii::$app->request->get('id', 0);
  460. $order = OrderClass::getById($id, true);
  461. OrderClass::valid($order, $this->shopId);
  462. $order->printNum += 1;
  463. $order->save();
  464. util::complete();
  465. }
  466. //打印订单 shish 20210714
  467. public function actionCloudPrintOrder()
  468. {
  469. $id = Yii::$app->request->get('id', 0);
  470. $order = OrderClass::getById($id, true);
  471. OrderClass::valid($order, $this->shopId);
  472. //打印订单
  473. OrderClass::onlinePrint($order);
  474. $order->printNum += 1;
  475. $order->save();
  476. util::complete();
  477. }
  478. }