OrderController.php 19 KB

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