OrderController.php 18 KB

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