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