PurchaseController.php 15 KB

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