OrderController.php 19 KB

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