OrderController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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['merchantId'] = $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['merchantId'] = $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 = $this->admin['miniOpenId'];
  168. $capitalType = dict::getDict('capitalType', 'xhGhsOrder', 'id');
  169. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  170. $wxPayType = 0;
  171. if (httpUtil::isMiniProgram()) {
  172. $wxPayType = 1;
  173. }
  174. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  175. //订单30分钟后过期
  176. $now = time();
  177. $expireTime = $now + 1800;
  178. $wx = Yii::getAlias("@vendor/weixin");
  179. require_once($wx . '/lib/WxPay.Api.php');
  180. require_once($wx . '/example/WxPay.JsApiPay.php');
  181. $input = new \WxPayUnifiedOrder();
  182. $input->SetBody($name);
  183. $input->SetOut_trade_no($orderSn);
  184. $input->SetTotal_fee($totalFee * 100);
  185. $input->SetTime_start(date("YmdHis", $now));
  186. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  187. $input->SetTime_expire(date("YmdHis", $expireTime));
  188. $input->SetNotify_url(Yii::$app->params['ghsHost'] . '/notice/wx-callback/');
  189. $input->SetTrade_type("JSAPI");
  190. //花卉宝代为申请的微信支付
  191. //$merchantExtend = $this->sjExtend->attributes;
  192. $merchantExtend = WxOpenClass::getGhsWxInfo();
  193. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  194. $input->SetSub_openid($openId);
  195. } else {
  196. $input->SetOpenid($openId);
  197. }
  198. //强制使用小程序的miniAppId
  199. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  200. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  201. $tools = new \JsApiPay();
  202. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  203. $newParams = json_decode($jsApiParameters, true);
  204. //微信不能修改价格
  205. $current = date("Y-m-d H:i:s");
  206. $updateData = ['deadline' => $current, 'modPrice' => 0];
  207. OrderClass::updateById($id, $updateData);
  208. util::success($newParams);
  209. }
  210. //获取订单详情 ssh 2021.1.21
  211. public function actionDetail()
  212. {
  213. $get = Yii::$app->request->get();
  214. $id = $get['id'] ?? 0;
  215. $info = OrderService::getOrderInfo($id, true, true, true, false);
  216. OrderClass::valid($info, $this->shopId);
  217. util::success($info);
  218. }
  219. //自取免发货流程处理 ssh 2021.1.22
  220. public function actionWithoutSend()
  221. {
  222. $get = Yii::$app->request->get();
  223. $id = $get['id'] ?? 0;
  224. $info = OrderService::getById($id, true);
  225. OrderClass::valid($info, $this->shopId);
  226. $purchaseId = $info->purchaseId;
  227. $purchase = PurchaseClass::getById($purchaseId, true);
  228. OrderService::withoutSend($info, $purchase);
  229. util::complete();
  230. }
  231. //本店送 ssh 2021.1.24
  232. public function actionSelfSend()
  233. {
  234. $get = Yii::$app->request->get();
  235. $id = isset($get['id']) ? $get['id'] : 0;
  236. $info = OrderClass::getById($id, true);
  237. OrderClass::valid($info->attributes, $this->shopId);
  238. $connection = Yii::$app->db;
  239. $transaction = $connection->beginTransaction();
  240. try {
  241. OrderClass::selfSend($info);
  242. $transaction->commit();
  243. } catch (\Exception $exception) {
  244. $transaction->rollBack();
  245. Yii::info("本店送操作报错:" . $exception->getMessage());
  246. util::fail('操作失败');
  247. }
  248. util::complete();
  249. }
  250. //运费计算 ssh 2021.1.24
  251. public function actionFreight()
  252. {
  253. //2021.3.28 接入达达
  254. $get = Yii::$app->request->get();
  255. $post = Yii::$app->request->post();
  256. if (empty($get)) {
  257. $get = $post;
  258. }
  259. $orderId = $get['id'] ?? 0; //订单ID
  260. $customId = $get['customId'] ?? 0;
  261. if (empty($orderId)) {
  262. //自定义运费
  263. //freight::getCost();
  264. if (empty($customId)) {
  265. util::fail("请选择客户");
  266. }
  267. $customInfo = CustomClass::getById($customId);
  268. if (empty($customInfo)) {
  269. util::fail('没有找到客户');
  270. }
  271. $shop = $this->shop;
  272. //花材信息
  273. $productJson = $post['product'] ?? '';
  274. // $productJson = '[{"productId":31993,"bigNum":1,"smallNum":0}]';
  275. if (empty($productJson)) {
  276. util::fail('请选择花材');
  277. }
  278. $productList = json_decode($productJson, true);
  279. if (empty($productList)) {
  280. util::fail('请选择花材');
  281. }
  282. $productList = ProductClass::mergeItemInfo($productList);
  283. $weight = 0;
  284. $price = 0;
  285. $productData = ProductClass::getProductMapData($productList);
  286. $bigNum = 0;
  287. foreach ($productList as $key => $val) {
  288. $productId = $val['productId'];
  289. $w = ProductClass::getWeight($val['productId'], $val['bigNum'], $val['smallNum']);
  290. $bigNum += $val['bigNum'];
  291. $weight = bcadd($w, $weight, 2);
  292. $ratio = $productData[$productId]['ratio'] ?? 0;
  293. //大小数量合并多少扎
  294. $itemNum = ProductClass::mergeItemNum($val['bigNum'], $val['smallNum'], $ratio);
  295. //售卖价格
  296. $itemPrice = $productData[$productId]['price'] ?? 0;
  297. //合计价格
  298. $price = bcadd($price,bcmul($itemNum, $itemPrice, 2),2);
  299. }
  300. $cost = freight::getCost($shop->lat,$shop->long,$customInfo['lat'],$customInfo['long'],$weight);
  301. util::success(['sedCost' => $cost]);
  302. }
  303. $province = $get['province'] ?? '';
  304. $city = $get['city'] ?? '';
  305. $address = $get['address'] ?? '';
  306. $floor = $get['floor'] ?? '';
  307. $customName = $get['customName'] ?? '';
  308. $customMobile = $get['customMobile'] ?? '';
  309. $fullAddress = $province . $city . $address . $floor;
  310. $sendTime = $get['sendTime'] ?? '';
  311. $lat = $get['lat'] ?? '';
  312. $lng = $get['long'] ?? '';
  313. if (empty($lat) || empty($lng) || empty($fullAddress)) {
  314. util::fail('请选择配送地址');
  315. }
  316. if (!empty($sendTime)) {
  317. //配送时间不为空
  318. // 预约发单时间(预约时间unix时间戳(10位),精确到分;整分钟为间隔,并且需要至少提前5分钟预约
  319. $sendTimeInt = strtotime($sendTime);
  320. $now = time();
  321. $diff = $sendTimeInt - $now;
  322. //因服务器写入时间,改为至少提前6分钟
  323. if ($diff <= 360) {
  324. util::fail('配送时间至少提前6分钟');
  325. }
  326. }
  327. //更新订单表
  328. $info = OrderService::getOrderInfo($orderId);
  329. OrderClass::valid($info, $this->shopId);
  330. $expressAddInfo = [
  331. 'customName' => $customName,
  332. 'customMobile' => $customMobile,
  333. 'province' => $province,
  334. 'city' => $city,
  335. 'address' => $address,
  336. 'floor' => $floor,
  337. 'lat' => $lat,
  338. 'long' => $lng,
  339. 'sendTime' => date('Y-m-d H:i', strtotime($sendTime)),
  340. ];
  341. OrderClass::updateCustomExpressInfo($orderId, $expressAddInfo);
  342. //计算运费
  343. $info['province'] = $province;
  344. $info['city'] = $city;
  345. $info['address'] = $address;
  346. $info['floor'] = $floor;
  347. $info['fullAddress'] = $fullAddress;
  348. $info['lat'] = $lat;
  349. $info['long'] = $lng;
  350. $info['sendTime'] = $expressAddInfo['sendTime'];
  351. $res = DadaExpressServices::queryDeliverFee($info);
  352. util::success(['sedCost' => $res['fee']]);
  353. }
  354. //发快递和第三方配送 ssh 2021.1.24
  355. public function actionThirdSend()
  356. {
  357. $get = Yii::$app->request->get();
  358. $id = isset($get['id']) ? $get['id'] : 0;
  359. $info = OrderService::getById($id, true);
  360. OrderClass::valid($info->attributes, $this->shopId);
  361. $connection = Yii::$app->db;
  362. $transaction = $connection->beginTransaction();
  363. try {
  364. $respond = OrderClass::thirdSend($info, $get);
  365. $transaction->commit();
  366. util::success($respond);
  367. } catch (\Exception $exception) {
  368. $transaction->rollBack();
  369. Yii::info("发快递操作报错:" . $exception->getMessage());
  370. util::fail('操作失败' . $exception->getMessage());
  371. }
  372. }
  373. //确认送达 ssh 2021.1.24
  374. public function actionReach()
  375. {
  376. $get = Yii::$app->request->get();
  377. $id = isset($get['id']) ? $get['id'] : 0;
  378. $info = OrderClass::getById($id, true);
  379. OrderClass::valid($info->attributes, $this->shopId);
  380. $connection = Yii::$app->db;
  381. $transaction = $connection->beginTransaction();
  382. try {
  383. OrderService::reach($info);
  384. $transaction->commit();
  385. } catch (\Exception $exception) {
  386. $transaction->rollBack();
  387. Yii::info("送达操作报错:" . $exception->getMessage());
  388. util::fail('操作失败');
  389. }
  390. util::complete();
  391. }
  392. //下单要用到的相关信息 ssh 2019.12.6
  393. public function actionOrderRelate()
  394. {
  395. $regionTree = RegionService::tree();
  396. $shop = $this->shop->attributes;
  397. $freight = ['first' => 5, 'add' => 2];
  398. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  399. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'txMapKey' => $mapKey, 'merchant' => $shop];
  400. util::success($out);
  401. }
  402. //客户欠款的订单 ssh 2021.2.4
  403. public function actionDebtList()
  404. {
  405. $id = Yii::$app->request->get('id', 0);
  406. $info = CustomClass::getCustom($id);
  407. CustomClass::valid($info, $this->shopId);
  408. $where = ['customId' => $id, 'debt' => 1];
  409. $respond = OrderService::getDebtOrderList($where);
  410. $respond['customInfo'] = $info;
  411. util::success($respond);
  412. }
  413. }