PurchaseController.php 15 KB

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