PurchaseController.php 13 KB

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