PurchaseController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. <?php
  2. namespace hd\controllers;
  3. use biz\admin\classes\AdminClass;
  4. use biz\ghs\classes\GhsClass;
  5. use biz\notice\classes\NoticeClass;
  6. use biz\shop\classes\ShopClass;
  7. use biz\shop\classes\ShopExpressClass;
  8. use bizGhs\custom\classes\CustomClass;
  9. use bizGhs\express\services\DadaExpressServices;
  10. use bizGhs\order\classes\OrderClass;
  11. use bizHd\purchase\classes\PurchaseClass;
  12. use bizHd\purchase\services\PurchaseService;
  13. use bizGhs\product\classes\ProductClass;
  14. use bizHd\wx\classes\WxOpenClass;
  15. use common\components\dateUtil;
  16. use common\components\dict;
  17. use common\components\freight;
  18. use common\components\httpUtil;
  19. use Yii;
  20. use common\components\util;
  21. class PurchaseController extends BaseController
  22. {
  23. public $guestAccess = ['detail'];
  24. //生成采购单 ssh 2021.1.17
  25. public function actionCreateOrder()
  26. {
  27. $post = Yii::$app->request->post();
  28. //多颜色花材数据结转换
  29. $colorItem = $post['xj'] ?? [];
  30. $newXj = [];
  31. if (!empty($colorItem)) {
  32. $colorData = json_decode($colorItem, true);
  33. if (!empty($colorData) && is_array($colorData)) {
  34. foreach ($colorData as $colorList) {
  35. if (!empty($colorList)) {
  36. foreach ($colorList as $color) {
  37. $ptItemId = $color['ptItemId'];
  38. $newXj[$ptItemId][] = $color;
  39. }
  40. }
  41. }
  42. }
  43. }
  44. $post['xj'] = $newXj;
  45. $ghsId = $post['ghsId'] ?? 0;
  46. //供应商
  47. $ghsInfo = GhsClass::getById($ghsId);
  48. if (empty($ghsInfo)) {
  49. util::fail('没有找到供应商');
  50. }
  51. $ghsShopId = $ghsInfo['shopId'] ?? 0;
  52. if (isset($ghsInfo['mainId']) && !empty($ghsInfo['mainId']) && $ghsInfo['mainId'] == $ghsInfo['ownMainId']) {
  53. util::fail('不能向自己的批发店采购');
  54. }
  55. $shop = $this->shop;
  56. $pfShopId = $shop->pfShopId ?? 0;
  57. if (!empty($pfShopId)) {
  58. util::fail('请在批发店进行采购');
  59. }
  60. $connection = Yii::$app->db;//事务处理
  61. $transaction = $connection->beginTransaction();
  62. try {
  63. $post['book'] = $post['book'] ?? 0;
  64. $post['sjId'] = $this->sjId;
  65. $post['shopId'] = $this->shopId;
  66. $post['shopAdminId'] = $this->shopAdminId;
  67. $post['ghsId'] = $ghsId;
  68. $post['ghsName'] = $ghsInfo['name'] ?? '';
  69. $post['ghsMobile'] = $ghsInfo['mobile'] ?? '';
  70. $post['ghsAvatar'] = $ghsInfo['avatar'] ?? '';
  71. $post['ghsFullAddress'] = $ghsInfo['fullAddress'] ?? '';
  72. $customId = $ghsInfo['customId'] ?? 0;
  73. $post['customId'] = $customId;
  74. $shopAdmin = $this->shopAdmin ?? null;
  75. $name = $shopAdmin->name ?? '';
  76. $post['shopAdminName'] = $name;
  77. $post['mainId'] = $this->mainId;
  78. $product = $post['product'] ?? '';
  79. $productList = json_decode($product, true);
  80. //判断product数据是不是同个供应商的
  81. $ghsShopInfo = ShopClass::getById($ghsShopId, true);
  82. $ghsMainId = $ghsShopInfo->mainId ?? 0;
  83. $isOpen = ShopClass::isOpen($ghsShopInfo);
  84. if ($isOpen == 0 && $post['book'] == 0) {
  85. util::fail('商家已关店休息了');
  86. }
  87. ProductClass::valid($productList, $ghsMainId);
  88. //供应商预订单最低公斤数要求和每公斤服务费
  89. $ghsInfo['kiloFee'] = $ghsShopInfo->kiloFee ?? 0;
  90. $ghsInfo['miniKilo'] = $ghsShopInfo->miniKilo ?? 0;
  91. $post['product'] = $productList;
  92. $post['sendCost'] = isset($post['sendCost']) && is_numeric($post['sendCost']) ? $post['sendCost'] : 0;
  93. $respond = PurchaseService::createPurchase($post, $ghsInfo);
  94. $transaction->commit();
  95. util::success($respond);
  96. } catch (Exception $e) {
  97. $transaction->rollBack();
  98. util::fail();
  99. }
  100. }
  101. //延期支付 ssh 2021.1.24
  102. public function actionDebtPay()
  103. {
  104. $get = Yii::$app->request->get();
  105. $orderSn = $get['orderSn'] ?? 0;
  106. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  107. PurchaseService::valid($info, $this->shopId);
  108. if ($info->status == 5) {
  109. util::fail('订单已取消');
  110. }
  111. if ($info->status != 1) {
  112. util::fail('订单不是待付款状态');
  113. }
  114. $ghsId = $info->ghsId;
  115. $ghs = GhsClass::getGhsInfo($ghsId);
  116. $customId = $ghs['customId'] ?? 0;
  117. $custom = CustomClass::getCustom($customId);
  118. if (empty($custom)) {
  119. util::fail('没有找到客户');
  120. }
  121. if (isset($custom['debt']) == false || $custom['debt'] == CustomClass::IS_DEBT_NO) {
  122. util::fail('您没有延期付款权限');
  123. }
  124. $connection = Yii::$app->db;//事务处理
  125. $transaction = $connection->beginTransaction();
  126. try {
  127. //延期支付
  128. PurchaseService::debt($info);
  129. $transaction->commit();
  130. //app新订单通知
  131. $saleId = $info->saleId ?? 0;
  132. $order = OrderClass::getById($saleId, true);
  133. NoticeClass::ghsNewOrderNotice($order);
  134. } catch (Exception $e) {
  135. $transaction->rollBack();
  136. util::fail('支付失败');
  137. }
  138. util::success($info->attributes);
  139. }
  140. //余额支付 ssh 2021.1.24
  141. public function actionBalancePay()
  142. {
  143. $shopAdmin = $this->shopAdmin;
  144. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  145. util::fail('超管才能操作');
  146. }
  147. $get = Yii::$app->request->get();
  148. $orderSn = $get['orderSn'] ?? 0;
  149. $password = $get['password'] ?? '';
  150. // 重新获取,不加密的用户数据
  151. $admin = AdminClass::getById($this->adminId, true);
  152. if (password_verify($password, $admin->payPassword) == false) {
  153. util::fail('密码错误');
  154. }
  155. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  156. if (empty($info)) {
  157. util::fail('没有找到采购单');
  158. }
  159. if ($info->status == 5) {
  160. util::fail('订单已取消');
  161. }
  162. if ($info->status != 1) {
  163. util::fail('订单不是待付款状态');
  164. }
  165. PurchaseService::valid($info, $this->shopId);
  166. $connection = Yii::$app->db;//事务处理
  167. $transaction = $connection->beginTransaction();
  168. try {
  169. //余额支付
  170. purchaseService::balancePay($info);
  171. $transaction->commit();
  172. } catch (Exception $e) {
  173. $transaction->rollBack();
  174. util::fail('支付失败');
  175. }
  176. util::success($info->attributes);
  177. }
  178. //采购记录 ssh 2021.1.24
  179. public function actionList()
  180. {
  181. $get = Yii::$app->request->get();
  182. $where = ['shopId' => $this->shopId];
  183. $status = $get['status'] ?? 0;
  184. if (!empty($status)) {
  185. $where['status'] = $status;
  186. }
  187. $respond = PurchaseService::getPurchaseList($where);
  188. $respond['shop'] = $this->shop->attributes;
  189. util::success($respond);
  190. }
  191. //采购详情 ssh 2021.01.25
  192. public function actionDetail()
  193. {
  194. $get = Yii::$app->request->get();
  195. $id = $get['id'] ?? 0;
  196. $info = PurchaseService::getInfo($id, true, true);
  197. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  198. //util::fail('没有权限访问');
  199. }
  200. $customId = $info['customId'] ?? 0;
  201. $custom = CustomClass::getById($customId);
  202. $hasDebtPay = $custom['debt'] ?? 0;
  203. $info['hasDebtPay'] = $hasDebtPay;
  204. $info['balance'] = $this->shop->balance ?? 0;
  205. util::success($info);
  206. }
  207. //可用的支付方式 ssh 2021.1.25
  208. public function actionPayWay()
  209. {
  210. $button = [
  211. ['type' => 'wxPay', 'show' => 1, 'disable' => 1],
  212. ['type' => 'debtPay', 'show' => 1, 'disable' => 0],
  213. ['type' => 'balancePay', 'show' => 1, 'disable' => 0],
  214. ];
  215. util::success(['button' => $button]);
  216. }
  217. //待结款明细 ssh 2021.1.26
  218. public function actionDebtList()
  219. {
  220. $get = Yii::$app->request->get();
  221. $id = $get['id'] ?? 0;
  222. $info = GhsClass::getGhsInfo($id);
  223. GhsClass::valid($info, $this->shopId);
  224. $where = ['ghsId' => $id, 'debt' => PurchaseClass::DEBT_YES];
  225. $searchTime = $get['searchTime'] ?? '';
  226. if (!empty($searchTime)) {
  227. $startTime = $get['startTime'] ?? '';
  228. $endTime = $get['endTime'] ?? '';
  229. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  230. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  231. }
  232. $respond = PurchaseService::getDebtList($where);
  233. util::success($respond);
  234. }
  235. //微信支付 ssh 2021.4.11
  236. public function actionWxPay()
  237. {
  238. ini_set('date.timezone', 'Asia/Shanghai');
  239. $post = Yii::$app->request->post();
  240. $couponId = $post['couponId'] ?? 0;
  241. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  242. $order = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  243. if (empty($order)) {
  244. util::fail('订单号无效');
  245. }
  246. if ($order->status == 5) {
  247. util::fail('订单已取消');
  248. }
  249. if ($order->status != 1) {
  250. util::fail('订单不是待付款状态');
  251. }
  252. $id = $order->id;
  253. $order->changePrice = 2;
  254. $order->save();
  255. $deadline = $order->deadline;
  256. $current = time();
  257. if (($current + 50) > strtotime($deadline)) {
  258. util::fail('订单已失效,请重新下单');
  259. }
  260. $name = $orderSn;
  261. $totalFee = $order->realPrice;
  262. //强制使用小程序的miniOpenId
  263. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  264. if (empty($openId)) {
  265. util::fail('没有miniOpenId...');
  266. }
  267. $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id');
  268. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  269. $wxPayType = 0;
  270. if (httpUtil::isMiniProgram()) {
  271. $wxPayType = 1;
  272. }
  273. $attach = "couponId=" . $couponId . "&capitalType=" . $purchaseCapital . '&wxPayType=' . $wxPayType;
  274. //订单30分钟后过期
  275. $now = time();
  276. $expireTime = $now + 1800;
  277. $wx = Yii::getAlias("@vendor/weixin");
  278. require_once($wx . '/lib/WxPay.Api.php');
  279. require_once($wx . '/example/WxPay.JsApiPay.php');
  280. $input = new \WxPayUnifiedOrder();
  281. $input->SetBody($name);
  282. $input->SetOut_trade_no($orderSn);
  283. $input->SetTotal_fee($totalFee * 100);
  284. $input->SetTime_start(date("YmdHis", $now));
  285. //将流水类型、代金劵等传给微信再回传
  286. $input->SetAttach($attach);
  287. $input->SetTime_expire(date("YmdHis", $expireTime));
  288. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  289. $input->SetTrade_type("JSAPI");
  290. //花卉宝代为申请的微信支付
  291. $merchantExtend = WxOpenClass::getWxInfo();
  292. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  293. $input->SetSub_openid($openId);
  294. } else {
  295. $input->SetOpenid($openId);
  296. }
  297. //强制使用小程序的miniAppId
  298. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  299. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  300. $tools = new \JsApiPay();
  301. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  302. $newParams = json_decode($jsApiParameters, true);
  303. $newParams['id'] = $id;
  304. util::success($newParams);
  305. }
  306. //运费计算 lqh 2021.4.12 暂时返回固定值
  307. public function actionFreight()
  308. {
  309. //lqh 2021.6.25 运费固定返回0
  310. util::success(['sedCost' => 0]);
  311. $post = Yii::$app->request->post();
  312. $ghsId = $post['ghsId'] ?? 0;
  313. //供应商
  314. $ghsInfo = GhsClass::getById($ghsId);
  315. if (empty($ghsInfo)) {
  316. util::fail('没有找到供应商');
  317. }
  318. //花材信息
  319. $productJson = $post['product'] ?? '';
  320. // $productJson = '[{"productId":31993,"bigNum":1,"smallNum":0}]';
  321. if (empty($productJson)) {
  322. util::fail('请选择花材');
  323. }
  324. $productList = json_decode($productJson, true);
  325. if (empty($productList)) {
  326. util::fail('请选择花材');
  327. }
  328. $productList = PurchaseClass::mergeItemInfo($productList);
  329. $weight = 0;
  330. $price = 0;
  331. $productData = ProductClass::getProductMapData($productList);
  332. $bigNum = 0;
  333. foreach ($productList as $key => $val) {
  334. $productId = $val['productId'];
  335. $w = ProductClass::getWeight($val['productId'], $val['bigNum'], $val['smallNum']);
  336. $bigNum += $val['bigNum'];
  337. $weight = bcadd($w, $weight, 2);
  338. $ratio = $productData[$productId]['ratio'] ?? 0;
  339. //大小数量合并多少扎
  340. $itemNum = ProductClass::mergeItemNum($val['bigNum'], $val['smallNum'], $ratio);
  341. //售卖价格
  342. $itemPrice = $productData[$productId]['price'] ?? 0;
  343. //合计价格
  344. $price = bcadd($price, bcmul($itemNum, $itemPrice, 2), 2);
  345. }
  346. $ghsShopId = $ghsInfo['shopId'] ?? 0;
  347. $ghsSjId = $ghsInfo['sjId'] ?? 0;
  348. $sendTime = $post['sendTime'] ?? '';
  349. if (!empty($sendTime)) {
  350. //配送时间不为空
  351. // 预约发单时间(预约时间unix时间戳(10位),精确到分;整分钟为间隔,并且需要至少提前5分钟预约
  352. $sendTimeInt = strtotime($sendTime);
  353. $now = time();
  354. $diff = $sendTimeInt - $now;
  355. //因服务器写入时间,改为至少提前6分钟
  356. if ($diff <= 360) {
  357. util::fail('配送时间至少提前6分钟');
  358. }
  359. $sendTime = date('Y-m-d H:i', strtotime($sendTime));
  360. } else {
  361. $sendTime = date('Y-m-d H:i', strtotime("+30 minute"));
  362. }
  363. //找供应商的客户信息,用里面的配送相关地址信息
  364. $customId = $ghsInfo['customId'] ?? 0;
  365. $customInfo = CustomClass::getById($customId);
  366. if (empty($customInfo)) {
  367. util::fail('没有找到客户');
  368. }
  369. //判断商家是否开启达达,若未开启,则使用自定义运费计算
  370. $shopNo = ShopExpressClass::getShopNo($ghsSjId, $ghsShopId);
  371. if (!$shopNo) {
  372. //使用自定义运费
  373. //util::fail('供应商暂未开通配送');
  374. $cost = freight::getCost($ghsInfo['lat'], $ghsInfo['long'], $customInfo['lat'], $customInfo['long'], $weight);
  375. util::success(['sedCost' => $cost]);
  376. }
  377. //组装订单信息
  378. $info = [];
  379. //发送放的商家信息,即供应商的相关信息
  380. $info['sjId'] = $ghsSjId;
  381. $info['shopId'] = $ghsShopId;
  382. $info['actPrice'] = $price;//价格
  383. $info['weight'] = $weight;
  384. $info['orderSn'] = \common\components\orderSn::getGhsOrderSn();
  385. $info['bigNum'] = $bigNum;
  386. //配送地址,当前零售端的门店地址
  387. $info['customName'] = $customInfo['name'] ?? '';
  388. $info['customMobile'] = $customInfo['mobile'] ?? '';
  389. $info['province'] = $customInfo['province'] ?? '';
  390. $info['city'] = $customInfo['city'] ?? '';;
  391. $info['address'] = $customInfo['address'] ?? '';;
  392. $info['floor'] = $customInfo['floor'] ?? '';;
  393. $info['fullAddress'] = $customInfo['fullAddress'] ?? '';;
  394. $info['lat'] = $customInfo['lat'] ?? '';;
  395. $info['long'] = $customInfo['long'] ?? '';;
  396. $info['sendTime'] = $sendTime;
  397. $res = DadaExpressServices::queryDeliverFee($info);
  398. util::success(['sedCost' => $res['fee']]);
  399. }
  400. }