OrderController.php 16 KB

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