PurchaseController.php 13 KB

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