PurchaseController.php 15 KB

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