OrderController.php 17 KB

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