PurchaseController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. $name = $orderSn;
  185. $totalFee = $order->realPrice;
  186. //强制使用小程序的miniOpenId
  187. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  188. if (empty($openId)) {
  189. util::fail('没有openId');
  190. }
  191. $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id');
  192. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  193. $wxPayType = 0;
  194. if (httpUtil::isMiniProgram()) {
  195. $wxPayType = 1;
  196. }
  197. $attach = "couponId=" . $couponId . "&capitalType=" . $purchaseCapital . '&wxPayType=' . $wxPayType;
  198. //订单30分钟后过期
  199. $now = time();
  200. $expireTime = $now + 1800;
  201. $wx = Yii::getAlias("@vendor/weixin");
  202. require_once($wx . '/lib/WxPay.Api.php');
  203. require_once($wx . '/example/WxPay.JsApiPay.php');
  204. $input = new \WxPayUnifiedOrder();
  205. $input->SetBody($name);
  206. $input->SetOut_trade_no($orderSn);
  207. $input->SetTotal_fee($totalFee * 100);
  208. $input->SetTime_start(date("YmdHis", $now));
  209. //将流水类型、代金劵等传给微信再回传
  210. $input->SetAttach($attach);
  211. $input->SetTime_expire(date("YmdHis", $expireTime));
  212. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  213. $input->SetTrade_type("JSAPI");
  214. //花卉宝代为申请的微信支付
  215. $merchantExtend = WxOpenClass::getWxInfo();
  216. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  217. $input->SetSub_openid($openId);
  218. } else {
  219. $input->SetOpenid($openId);
  220. }
  221. //强制使用小程序的miniAppId
  222. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  223. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  224. $tools = new \JsApiPay();
  225. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  226. $newParams = json_decode($jsApiParameters, true);
  227. $current = date("Y-m-d H:i:s");
  228. $updateData = ['deadline' => $current, 'changePrice' => 2];
  229. PurchaseClass::updateById($id, $updateData);
  230. $newParams['id'] = $id;
  231. util::success($newParams);
  232. }
  233. //运费计算 linqh 2021.4.12 暂时返回固定值
  234. public function actionFreight()
  235. {
  236. $post = Yii::$app->request->post();
  237. //$ghsId = $post['ghsId'] ?? 0;
  238. $ghsId = 31;
  239. //供货商
  240. $ghsInfo = GhsClass::getById($ghsId);
  241. if (empty($ghsInfo)) {
  242. util::fail('没有找到供货商');
  243. }
  244. //花材信息
  245. $productJson = $post['product'] ?? '';
  246. // $productJson = '[{"productId":31993,"bigNum":1,"smallNum":0}]';
  247. if (empty($productJson)) {
  248. util::fail('请选择花材');
  249. }
  250. $productList = json_decode($productJson, true);
  251. if (empty($productList)) {
  252. util::fail('请选择花材');
  253. }
  254. $productList = PurchaseClass::mergeItemInfo($productList);
  255. $weight = 0;
  256. $price = 0;
  257. $productData = ProductClass::getProductMapData($productList);
  258. $bigNum = 0;
  259. foreach ($productList as $key => $val) {
  260. $productId = $val['productId'];
  261. $w = ProductClass::getWeight($val['productId'], $val['bigNum'], $val['smallNum']);
  262. $bigNum += $val['bigNum'];
  263. $weight = bcadd($w, $weight, 2);
  264. $ratio = $productData[$productId]['ratio'] ?? 0;
  265. //大小数量合并多少扎
  266. $itemNum = ProductClass::mergeItemNum($val['bigNum'], $val['smallNum'], $ratio);
  267. //售卖价格
  268. $itemPrice = $productData[$productId]['price'] ?? 0;
  269. //合计价格
  270. $price = bcadd($price, bcmul($itemNum, $itemPrice, 2), 2);
  271. }
  272. $ghsShopId = $ghsInfo['shopId'] ?? 0;
  273. $ghsSjId = $ghsInfo['sjId'] ?? 0;
  274. $sendTime = $post['sendTime'] ?? '';
  275. if (!empty($sendTime)) {
  276. //配送时间不为空
  277. // 预约发单时间(预约时间unix时间戳(10位),精确到分;整分钟为间隔,并且需要至少提前5分钟预约
  278. $sendTimeInt = strtotime($sendTime);
  279. $now = time();
  280. $diff = $sendTimeInt - $now;
  281. //因服务器写入时间,改为至少提前6分钟
  282. if ($diff <= 360) {
  283. util::fail('配送时间至少提前6分钟');
  284. }
  285. $sendTime = date('Y-m-d H:i', strtotime($sendTime));
  286. } else {
  287. $sendTime = date('Y-m-d H:i', strtotime("+30 minute"));
  288. }
  289. //找供货商的客户信息,用里面的配送相关地址信息
  290. $customId = $ghsInfo['customId'] ?? 0;
  291. $customInfo = CustomClass::getById($customId);
  292. if (empty($customInfo)) {
  293. util::fail('没有找到客户');
  294. }
  295. //判断商家是否开启达达,若未开启,则使用自定义运费计算
  296. $shopNo = ShopExpressClass::getShopNo($ghsSjId, $ghsShopId);
  297. if (!$shopNo) {
  298. //使用自定义运费
  299. $cost = freight::getCost($ghsInfo['lat'], $ghsInfo['long'], $customInfo['lat'], $customInfo['long'], $weight);
  300. util::success(['sedCost' => $cost]);
  301. }
  302. //组装订单信息
  303. $info = [];
  304. //发送放的商家信息,即供货商的相关信息
  305. $info['sjId'] = $ghsSjId;
  306. $info['shopId'] = $ghsShopId;
  307. $info['actPrice'] = $price;//价格
  308. $info['weight'] = $weight;
  309. $info['orderSn'] = \common\components\orderSn::getGhsOrderSn();
  310. $info['bigNum'] = $bigNum;
  311. //配送地址,当前零售端的门店地址
  312. $info['customName'] = $customInfo['name'] ?? '';
  313. $info['customMobile'] = $customInfo['mobile'] ?? '';
  314. $info['province'] = $customInfo['province'] ?? '';
  315. $info['city'] = $customInfo['city'] ?? '';;
  316. $info['address'] = $customInfo['address'] ?? '';;
  317. $info['floor'] = $customInfo['floor'] ?? '';;
  318. $info['fullAddress'] = $customInfo['fullAddress'] ?? '';;
  319. $info['lat'] = $customInfo['lat'] ?? '';;
  320. $info['long'] = $customInfo['long'] ?? '';;
  321. $info['sendTime'] = $sendTime;
  322. $res = DadaExpressServices::queryDeliverFee($info);
  323. util::success(['sedCost' => $res['fee']]);
  324. }
  325. }