PurchaseController.php 14 KB

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