PurchaseController.php 15 KB

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