PurchaseController.php 16 KB

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