PurchaseController.php 16 KB

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