OrderController.php 17 KB

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