PurchaseController.php 15 KB

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