OrderController.php 18 KB

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