PurchaseController.php 13 KB

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