OrderController.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. <?php
  2. namespace ghs\controllers;
  3. use bizGhs\express\services\DadaExpressServices;
  4. use bizHd\purchase\classes\PurchaseClass;
  5. use bizHd\purchase\services\PurchaseService;
  6. use bizHd\wx\classes\WxOpenClass;
  7. use bizGhs\custom\classes\CustomClass;
  8. use bizGhs\order\classes\OrderClass;
  9. use bizGhs\order\services\OrderService;
  10. use bizHd\saas\services\RegionService;
  11. use bizGhs\product\classes\ProductClass;
  12. use common\components\dict;
  13. use common\components\dateUtil;
  14. use common\components\freight;
  15. use common\components\httpUtil;
  16. use Yii;
  17. use common\components\util;
  18. use biz\shop\classes\ShopAdminClass;
  19. class OrderController extends BaseController
  20. {
  21. public $guestAccess = ['create-order', 'order-relate', 'fast-pay'];
  22. //微信和支付宝付款码支付复查 shish 20211231
  23. public function actionCodePayCheck()
  24. {
  25. ini_set('date.timezone', 'Asia/Shanghai');
  26. header("Content-type: text/html; charset=utf-8");
  27. $get = Yii::$app->request->get();
  28. $id = isset($get['id']) ? $get['id'] : 0;
  29. $order = OrderClass::getById($id, true);
  30. if (empty($order)) {
  31. util::fail('没有找到订单');
  32. }
  33. if ($order->status != OrderClass::ORDER_STATUS_UN_PAY) {
  34. if ($order->status == OrderClass::ORDER_STATUS_COMPLETE) {
  35. util::fail('订单已付款');
  36. }
  37. util::fail('订单不是待付款状态');
  38. }
  39. OrderClass::valid($order, $this->shopId);
  40. $cgId = $order->purchaseId ?? 0;
  41. $cg = PurchaseClass::getById($cgId, true);
  42. $orderSn = $cg->orderSn ?? '';
  43. if (empty($orderSn)) {
  44. util::fail('没有找到采购单');
  45. }
  46. $payWay = $cg->payWay ?? dict::getDict('payWay', 'wxPay');
  47. if ($payWay == dict::getDict('payWay', 'wxPay')) {
  48. //微信付款
  49. $wx = Yii::getAlias("@vendor/wxPayApi_v3.0.10");
  50. require_once($wx . '/lib/WxPay.Api.php');
  51. require_once($wx . '/example/WxPay.MicroPay.php');
  52. //获取微信商户号等信息
  53. $sjExtend = WxOpenClass::getGhsWxInfo();
  54. $microPay = new \MicroPay();
  55. $payReturn = $microPay->query($orderSn, $sjExtend);
  56. if ($payReturn["return_code"] == "SUCCESS" && $payReturn["result_code"] == "SUCCESS") {
  57. if ($payReturn["trade_state"] == "SUCCESS") {
  58. //支付成功
  59. $transactionId = $payReturn['transaction_id'] ?? '';
  60. $openId = $payReturn['openid'] ?? '';
  61. PurchaseService::codePay($payWay, $orderSn, $transactionId, $openId);
  62. util::success(['returnStatus' => 'SUCCESS']);
  63. } else if ($payReturn["trade_state"] == "USERPAYING") {
  64. //用户支付中
  65. util::fail('正在付款,请稍候再试');
  66. }
  67. }
  68. if ($payReturn["err_code"] == "ORDERNOTEXIST") {
  69. //交易订单号不存在
  70. util::fail('订单没有发起过支付');
  71. } else {
  72. util::fail('系统错误,请稍候再试');
  73. }
  74. } elseif ($payWay == dict::getDict('payWay', 'alipay')) {
  75. //支付宝付款
  76. } else {
  77. util::fail('未知付款方式');
  78. }
  79. }
  80. //微信和支付宝付款码支付 shish 2021.4.11
  81. public function actionCodePay()
  82. {
  83. ini_set('date.timezone', 'Asia/Shanghai');
  84. header("Content-type: text/html; charset=utf-8");
  85. $get = Yii::$app->request->get();
  86. $id = isset($get['id']) ? $get['id'] : 0;
  87. $order = OrderClass::getById($id, true);
  88. if (isset($order->status) == false || $order->status != 1) {
  89. util::fail('不是待付款订单');
  90. }
  91. OrderClass::valid($order, $this->shopId);
  92. $cgId = $order->purchaseId ?? 0;
  93. $cg = PurchaseClass::getById($cgId, true);
  94. $orderSn = $cg->orderSn ?? '';
  95. if (empty($orderSn)) {
  96. util::fail('没有找到采购单');
  97. }
  98. //付款码
  99. $authCode = (string)$get['authCode'] ?? '';
  100. if (empty($authCode)) {
  101. util::fail('支付失败');
  102. }
  103. $wxPrefix = ['10', '11', '12', '13', '14', '15'];
  104. $isWx = false;
  105. foreach ($wxPrefix as $wxId) {
  106. if (strpos($authCode, $wxId) === 0) {
  107. $isWx = true;
  108. }
  109. }
  110. $deadline = $cg->deadline;
  111. $current = date("Y-m-d H:i:s");
  112. if ((strtotime($deadline) + 20) < strtotime($current)) {
  113. util::fail('订单已经失效');
  114. }
  115. $totalFee = $cg->actPrice ?? 0;
  116. $outTradeNo = $orderSn;
  117. $subject = '购买商品';
  118. $totalAmount = $totalFee;
  119. $body = "门店";
  120. if ($isWx) {
  121. try {
  122. $wx = Yii::getAlias("@vendor/wxPayApi_v3.0.10");
  123. require_once($wx . '/lib/WxPay.Api.php');
  124. require_once($wx . '/example/WxPay.MicroPay.php');
  125. $input = new \WxPayMicroPay();
  126. $input->SetAuth_code($authCode);
  127. $input->SetBody($subject);
  128. $input->SetTotal_fee($totalFee * 100);
  129. $input->SetOut_trade_no($orderSn);
  130. //获取微信商户号等信息
  131. $sjExtend = WxOpenClass::getGhsWxInfo();
  132. $microPay = new \MicroPay();
  133. $res = $microPay->pay($input, $sjExtend);
  134. if (isset($res["return_code"]) && $res["return_code"] == "SUCCESS") {
  135. $transactionId = $res['transaction_id'] ?? '';
  136. $openId = $res['openid'] ?? '';
  137. $payWay = dict::getDict('payWay', 'wxPay');
  138. PurchaseService::codePay($payWay, $orderSn, $transactionId, $openId);
  139. util::success(['returnStatus' => 'SUCCESS']);
  140. }
  141. } catch (\Exception $e) {
  142. Yii::info($e->getMessage());
  143. util::fail('支付失败,请查询订单支付结果');
  144. }
  145. } else {
  146. $aliF2F = Yii::getAlias("@vendor/alipayF2F");
  147. require_once($aliF2F . '/f2fpay/model/builder/AlipayTradePayContentBuilder.php');
  148. require_once($aliF2F . '/f2fpay/service/AlipayTradeService.php');
  149. $aliPId = '2017041906821571';
  150. $aliKey = 'MIIEpAIBAAKCAQEAvbeu6o90L+0AilI+mbhuJJH0lUv7hWQCEC+XUc+cWlC9ulKC3cSVScVLJHpUc09aj6v/iD1p9iVfgMxjeADNVKiQC2RJyRshAjzyTq+2bTcODp619iV2y4qW8UM65VcP7Ide3P0uT5xC2ReW0HpkdHZgm4F+P7mh1VhGt9qOP3hjvoraAHhtGACvsGvy8H69HhSAG+CEmFt7f2tKNWJ8xEUvkfhDvys93PfTk4xMWj048hJE2r38+2mWI/Il5dFQHWXXTVl2x9yi7uhEEW42a+7s9Q3HgqEvZiQ2NI1FTeEAPLFBApiEsNLY/smaszwqqfWGAyTMvtQ6T4K96GcTLQIDAQABAoIBAHwYTj33n9RJfnT73x7F2KXrIsUVcmyKQh88QgqtdmRNNA1QM3HESLJ8bu5pZhwW5/HaW8dOBKWRRKsHBnlUbPrXV4FcFDeLm0fPfd+iZ/2AaZ1+ix962f3BpYIiq7+f9zaMRazfnw9L8x31pByyMktLs12EkoQ0dHsMxxUzzKAOiiPnvCn8Pv+Jb2qej8GXgCjefutCVNMWGsdwJtMVXEbpdmMa48Wvw+0In5ZS/UG39YAreFzgDqI9jWHE1ZhkGmB1+8hKpeIlEeVGAeYVveUgssMORdA/YY8SWHk26KSIFXBrmdKjAcRJ8Q7ZHUmzIqAV+IMV+RD4J5mZHeJgdAECgYEA8J+Hp1M6eYbT4CPLXa//yRP6Xo+qSzrKHaRB1LRAjYhvKpx/TQetYpNCJ5bWpa+Jp0T5R21pzuYaJkPuGlNEHA13b5T3r1l2ltomSEdaShvD5EZ9WtErYV5zpSv7FjjIGkbwa0H3A8Ydnvw5z4A9TZjjHLjC2GtVGJReqHGglMECgYEAydddA4NRPxmXvIjjmLb1Ft1qwHuyv3vvWhi50foGZXNEnBcfP/OkfyBDDpOEu34GZBMXK9ykHBs1l93OEFGXh7AwW0Lp5lARahBEfBZgPjq5yPO8TOJZrb5hVcuvoDdGjxpRh93XDnQDhWS/1IvUcBLcIsTcnVvfrkX4NLxF/W0CgYEAxamG+gD4rBQBwMImsRN+/2MV7M//iEUG+0qPeXeI/7rv9wUP3etMlwl48qSKNxj37xxN2ksa/Acxu/VZhu6XqKO3VUX+IWFQdaNGh2F13iLozIDLQOtKw3WfcjOq0xpZ5pwXq0RI8iSw+IUhyD8EHNZW2qU8CiRBhyt6hsywqQECgYBa8+06FAachI/XqWfF/UvcDdJ5AkS9/L8SvmmdsSkItjSIkfLHAqdxkbwl6Vu6kUOX/PJIFZjuAWTZFl4xBFNgFYj01uZHnnT6cnIp6HteD2CAqTSFAMqgfFWoL6zoaYAmJBnxO4oZPTYI+ilnQcts5VLFaChx0GCvS2BZgy2W0QKBgQDjP2VtRq4aVSLUq2z1KIDI7GJBbOM5KSW1OEj4sfEdViozPh3Ar/FQWiij1oCZF+iJdKkonHoDbDd+Q0ykcpQ+5Gv07iiT8wJYFR/0j8g2kfql7bVQhGSBeBMS5sawKR3CvkfW73WX+CNnf0PklTY0kTyt6WRXDnKSeyxFVwSZ7g==';
  151. $aliPublicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsz3m9juAR1xew4DY6c34gvbwNg8pZ92f932tseKs+4+pF0e+jTDiUo/xHImNDi1KXt1B+s3LxY9L/sxEksbavwmhgbz/igN1cAEwS2YM+Gnf0csDrxAPJhoKL5FTwxPEQ/8VSgroU+6GlF3LAx4VHa1qfn5MqRPfLroJcyPDyGfNe9vna2FnO4E/PH+hVbvPUAwX3XrUvJIm4OgY0JE3HYFlu+O7F4Ln6+Sl+Zv/ZE2nZAvNiyAwW5iVKhKXLieExqwd0NdrP28baBqkkIY+tmV7butGjB52iK1UbU0+1uiaPL4xBxRy6d0LZmYlvdVHp0JRUuwxBLChGgOCo/76DQIDAQAB';
  152. $config = array(
  153. //签名方式,默认为RSA2(RSA2048)
  154. 'sign_type' => "RSA2",
  155. //支付宝公钥
  156. 'alipay_public_key' => $aliPublicKey,
  157. //商户私钥
  158. 'merchant_private_key' => $aliKey,
  159. //编码格式
  160. 'charset' => "UTF-8",
  161. //支付宝网关
  162. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  163. //应用ID
  164. 'app_id' => $aliPId,
  165. //异步通知地址,只有扫码支付预下单可用
  166. 'notify_url' => Yii::$app->params['hdHost'] . "/notice/ali-callback",
  167. //最大查询重试次数
  168. 'MaxQueryRetry' => "10",
  169. //查询间隔
  170. 'QueryDuration' => "3"
  171. );
  172. // 支付超时,线下扫码交易定义为5分钟
  173. $timeExpress = "5m";
  174. // 创建请求builder,设置请求参数
  175. $barPayRequestBuilder = new \AlipayTradePayContentBuilder();
  176. $barPayRequestBuilder->setOutTradeNo($outTradeNo);
  177. $barPayRequestBuilder->setTotalAmount($totalAmount);
  178. $barPayRequestBuilder->setAuthCode($authCode);
  179. $barPayRequestBuilder->setTimeExpress($timeExpress);
  180. $barPayRequestBuilder->setSubject($subject);
  181. $barPayRequestBuilder->setBody($body);
  182. // 调用barPay方法获取当面付应答
  183. $barPay = new \AlipayTradeService($config);
  184. $barPayResult = $barPay->barPay($barPayRequestBuilder);
  185. switch ($barPayResult->getTradeStatus()) {
  186. case "SUCCESS":
  187. // if (!empty($barPayResult->getResponse())) {
  188. // print_r($barPayResult->getResponse());
  189. // }
  190. util::success(['returnStatus' => 'SUCCESS']);
  191. break;
  192. case "FAILED":
  193. util::fail('支付失败,请查看订单详情');
  194. break;
  195. case "UNKNOWN":
  196. util::fail('支付异常');
  197. break;
  198. default:
  199. util::fail('支付异常');
  200. break;
  201. }
  202. }
  203. }
  204. //订单列表 ssh 2021.1.20
  205. public function actionList()
  206. {
  207. $get = Yii::$app->request->get();
  208. $status = $get['status'] ?? 0;
  209. if (!empty($status)) {
  210. $where['status'] = $status;
  211. }
  212. $where['sjId'] = $this->sjId;
  213. if (isset($get['shopId'])) {
  214. $shopId = $get['shopId'] ?? 0;
  215. if (!empty($shopId)) {
  216. $where['shopId'] = $this->shopId;
  217. }
  218. } else {
  219. $where['shopId'] = $this->shopId;
  220. }
  221. $searchTime = $get['searchTime'] ?? '';
  222. if (!empty($searchTime)) {
  223. $startTime = $get['startTime'] ?? '';
  224. $endTime = $get['endTime'] ?? '';
  225. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  226. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  227. }
  228. $name = $get['name'] ?? '';
  229. if (!empty($name)) {
  230. $searchType = $get['searchType'] ?? 0;
  231. if (empty($searchType)) {
  232. //订单编号
  233. if (strpos($name, 'XSD') === 0) {
  234. $where['orderSn'] = $name;
  235. } elseif (preg_match("/^[a-zA-Z\s]+$/", $name)) {
  236. $where['customNamePy'] = ['like', $name];
  237. } else {
  238. $where['customName'] = ['like', $name];
  239. }
  240. } elseif ($searchType == 1) {
  241. $where['sendNum'] = $name;
  242. } else {
  243. }
  244. }
  245. $respond = OrderClass::getOrderList($where);
  246. $respond['shop'] = $this->shop;
  247. $respond['shopExt'] = $this->shopExt;
  248. util::success($respond);
  249. }
  250. //商城下单操作 ssh 2021.1.14
  251. public function actionCreateOrder()
  252. {
  253. $post = Yii::$app->request->post();
  254. if (isset($post['newVersion']) == false) {
  255. util::fail('请升级小程序');
  256. }
  257. unset($post['newVersion']);
  258. $shopId = $this->shopId;
  259. $customId = $post['customId'] ?? 0;
  260. $post['customId'] = $customId;
  261. //开单必须选客户
  262. if (empty($customId)) {
  263. util::fail('请选择客户');
  264. }
  265. $custom = CustomClass::getCustom($customId);
  266. if (empty($custom)) {
  267. util::fail('请选客户哦');
  268. }
  269. CustomClass::valid($custom, $this->shopId);
  270. $post['ghsId'] = $custom['ghsId'] ?? 0;
  271. $post['customMobile'] = $custom['mobile'] ?? '';
  272. $customName = $custom['name'] ?? '';
  273. $post['customName'] = $customName;
  274. $post['customNamePy'] = $custom['py'] ?? '';
  275. $post['customAvatar'] = $custom['shortSmallAvatar'] ?? '';
  276. $post['shopAdminId'] = $this->shopAdminId;
  277. $post['province'] = $custom['province'] ?? '';
  278. $post['city'] = $custom['city'] ?? '';
  279. $post['dist'] = $custom['dist'] ?? '';
  280. $post['address'] = $custom['address'] ?? '';
  281. $post['floor'] = $custom['floor'] ?? '';
  282. $post['fullAddress'] = $custom['fullAddress'] ?? '';
  283. $post['showAddress'] = $custom['showAddress'] ?? '';
  284. $post['long'] = $custom['long'] ?? '';
  285. $post['lat'] = $custom['lat'] ?? '';
  286. $shopAdmin = $this->shopAdmin->attributes;
  287. $post['shopAdminName'] = $shopAdmin['name'] ?? '';
  288. $post['sjId'] = $this->sjId;
  289. $post['shopId'] = $shopId;
  290. $post['fromType'] = 1;
  291. $post['expressId'] = $post['expressId'] ?? 0;
  292. $post['sendCost'] = $post['sendCost'] ?? '0.00';
  293. //PC端开单
  294. if (isset($post['clearType'])) {
  295. $clearType = $post['clearType'];
  296. if ($clearType == OrderClass::CLEAR_DEBT) {
  297. $post['debt'] = OrderClass::DEBT_YES;
  298. $post['payWay'] = dict::getDict('payWay', 'debtPay');
  299. } else {
  300. $post['debt'] = OrderClass::DEBT_NO;
  301. }
  302. }
  303. $debt = $post['debt'] ?? OrderClass::DEBT_YES;
  304. //PC端开单
  305. if (isset($post['getType'])) {
  306. util::fail('PC端开单未开放');
  307. $getType = $post['getType'];
  308. //自取
  309. if ($getType == 1) {
  310. $post['sendType'] = OrderClass::SEND_TYPE_NO;
  311. } else {
  312. //选择配送时,在发货时让商家自己再确认一下用什么方式
  313. $post['sendType'] = OrderClass::SEND_TYPE_UNKNOWN;
  314. }
  315. }
  316. //员工不能开已收款订单
  317. if ($debt == OrderClass::DEBT_NO) {
  318. $shopAdmin = $this->shopAdmin;
  319. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  320. util::fail('只能开欠款单');
  321. }
  322. }
  323. $productJson = $post['product'] ?? '';
  324. if (empty($productJson)) {
  325. util::fail('请选择花材');
  326. }
  327. $productList = json_decode($productJson, true);
  328. if (empty($productList)) {
  329. util::fail('请选择花材');
  330. }
  331. $connection = Yii::$app->db;//事务处理
  332. $transaction = $connection->beginTransaction();
  333. try {
  334. //判断花材有效性
  335. ProductClass::valid($productList, $this->shopId);
  336. $post['product'] = $productList;
  337. //商家开单订单有效期更长(30)天
  338. $validTime = dict::getDict('kd_order_valid');
  339. $current = time() + $validTime;
  340. $post['deadline'] = date("Y-m-d H:i:s", $current);
  341. $hasPay = $post['hasPay'] ?? 1;
  342. $return = OrderService::createOrder($post, $custom, $hasPay);
  343. $transaction->commit();
  344. util::success($return);
  345. } catch (\Exception $e) {
  346. $transaction->rollBack();
  347. Yii::error("下单失败原因:" . $e->getMessage());
  348. util::fail('下单失败');
  349. }
  350. }
  351. //修改订单 ssh 20210810
  352. public function actionUpdateOrder()
  353. {
  354. $post = Yii::$app->request->post();
  355. if (isset($post['newVersion']) == false) {
  356. util::fail('请升级小程序');
  357. }
  358. unset($post['newVersion']);
  359. //PC端开单
  360. if (isset($post['clearType'])) {
  361. $clearType = $post['clearType'];
  362. if ($clearType == OrderClass::CLEAR_DEBT) {
  363. $post['debt'] = OrderClass::DEBT_YES;
  364. $post['payWay'] = dict::getDict('payWay', 'debtPay');
  365. } else {
  366. $post['debt'] = OrderClass::DEBT_NO;
  367. }
  368. }
  369. //员工不能开已收款订单
  370. $debt = $post['debt'] ?? OrderClass::DEBT_YES;
  371. if ($debt == OrderClass::DEBT_NO) {
  372. $shopAdmin = $this->shopAdmin;
  373. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  374. util::fail('不能开已收款订单');
  375. }
  376. }
  377. //PC端开单
  378. if (isset($post['getType'])) {
  379. util::fail('暂不支持PC端修改订单');
  380. $getType = $post['getType'];
  381. //自取
  382. if ($getType == 1) {
  383. $post['sendType'] = OrderClass::SEND_TYPE_NO;
  384. } else {
  385. //选择配送时,在发货时让商家自己再确认一下用什么方式
  386. $post['sendType'] = OrderClass::SEND_TYPE_UNKNOWN;
  387. }
  388. }
  389. //商家开非自取订单,默认订单是待收款的,debt字段为非欠款
  390. if (isset($post['sendType']) && $post['sendType'] != OrderClass::SEND_TYPE_NO) {
  391. $post['debt'] = OrderClass::DEBT_NO;
  392. }
  393. $id = $post['id'] ?? 0;
  394. $order = OrderClass::getLockById($id);
  395. if (empty($order)) {
  396. util::fail('没有找到订单');
  397. }
  398. OrderClass::valid($order, $this->shopId);
  399. unset($post['id']);
  400. $shopAdmin = $this->shopAdmin;
  401. $post['shopAdminId'] = $this->shopAdminId;
  402. $post['shopAdminName'] = $shopAdmin->name ?? '';
  403. $post['fromType'] = 1;
  404. $post['sendCost'] = $post['sendCost'] ?? '0.00';
  405. $productJson = $post['product'] ?? '';
  406. if (empty($productJson)) {
  407. util::fail('请选择花材');
  408. }
  409. $productList = json_decode($productJson, true);
  410. if (empty($productList)) {
  411. util::fail('请选择花材');
  412. }
  413. $connection = Yii::$app->db;//事务处理
  414. $transaction = $connection->beginTransaction();
  415. try {
  416. //判断花材有效性
  417. ProductClass::valid($productList, $this->shopId);
  418. $post['product'] = $productList;
  419. //商家开单订单有效期更长(30)天
  420. $validTime = dict::getDict('kd_order_valid');
  421. $current = time() + $validTime;
  422. $post['deadline'] = date("Y-m-d H:i:s", $current);
  423. $upData = [
  424. 'payWay' => $post['payWay'],
  425. 'sendType' => $post['sendType'],
  426. 'debt' => $post['debt'],
  427. 'needPrint' => $post['needPrint'],
  428. 'modifyPrice' => $post['modifyPrice'],
  429. 'shopAdminId' => $post['shopAdminId'],
  430. 'shopAdminName' => $post['shopAdminName'],
  431. 'fromType' => 1,
  432. 'sendCost' => $post['sendCost'],
  433. 'product' => $post['product'],
  434. 'deadline' => $post['deadline'],
  435. 'debt' => $post['debt'],
  436. ];
  437. $return = OrderService::updateOrder($order, $upData);
  438. $transaction->commit();
  439. util::success($return);
  440. } catch (\Exception $e) {
  441. $transaction->rollBack();
  442. Yii::error("下单失败原因:" . $e->getMessage());
  443. util::fail('下单失败');
  444. }
  445. }
  446. //延期支付 ssh 2021.1.19
  447. public function actionDebt()
  448. {
  449. $get = Yii::$app->request->get();
  450. $id = $get['id'] ?? 0;
  451. $info = OrderService::getOrderInfo($id);
  452. OrderClass::valid($info, $this->shopId);
  453. OrderClass::debt($info, $this->shop);
  454. util::complete();
  455. }
  456. //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
  457. public function actionWxPay()
  458. {
  459. ini_set('date.timezone', 'Asia/Shanghai');
  460. $post = Yii::$app->request->post();
  461. $couponId = $post['couponId'] ?? 0;
  462. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  463. $order = OrderClass::getByOrderSn($orderSn);
  464. if (empty($order)) {
  465. util::fail('订单号无效');
  466. }
  467. $id = $order['id'];
  468. //支付前验证订单有效性
  469. if (isset($order['adminId']) == false || $order['adminId'] != $this->adminId) {
  470. util::fail('没有权限操作');
  471. }
  472. $name = $order['orderName'] ?? '购买商品';
  473. $totalFee = $order['actPrice'];
  474. //强制使用小程序的miniOpenId
  475. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  476. if (empty($openId)) {
  477. util::fail('没有找到openId');
  478. }
  479. $capitalType = dict::getDict('capitalType', 'xhGhsOrder', 'id');
  480. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  481. $wxPayType = 0;
  482. if (httpUtil::isMiniProgram()) {
  483. $wxPayType = 1;
  484. }
  485. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  486. //订单30分钟后过期
  487. $now = time();
  488. $expireTime = $now + 1800;
  489. $wx = Yii::getAlias("@vendor/weixin");
  490. require_once($wx . '/lib/WxPay.Api.php');
  491. require_once($wx . '/example/WxPay.JsApiPay.php');
  492. $input = new \WxPayUnifiedOrder();
  493. $input->SetBody($name);
  494. $input->SetOut_trade_no($orderSn);
  495. $input->SetTotal_fee($totalFee * 100);
  496. $input->SetTime_start(date("YmdHis", $now));
  497. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  498. $input->SetTime_expire(date("YmdHis", $expireTime));
  499. $input->SetNotify_url(Yii::$app->params['ghsHost'] . '/notice/wx-callback/');
  500. $input->SetTrade_type("JSAPI");
  501. //花卉宝代为申请的微信支付
  502. //$sjExtend = $this->sjExtend->attributes;
  503. $sjExtend = WxOpenClass::getGhsWxInfo();
  504. if (isset($sjExtend['wxPayApply']) && $sjExtend['wxPayApply'] == 1) {
  505. $input->SetSub_openid($openId);
  506. } else {
  507. $input->SetOpenid($openId);
  508. }
  509. //强制使用小程序的miniAppId
  510. $sjExtend['wxAppId'] = $sjExtend['miniAppId'];
  511. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $sjExtend);
  512. $tools = new \JsApiPay();
  513. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $sjExtend);
  514. $newParams = json_decode($jsApiParameters, true);
  515. //微信不能修改价格
  516. $current = date("Y-m-d H:i:s");
  517. $updateData = ['deadline' => $current, 'modPrice' => 0];
  518. OrderClass::updateById($id, $updateData);
  519. util::success($newParams);
  520. }
  521. //获取订单详情 ssh 2021.1.21
  522. public function actionDetail()
  523. {
  524. $get = Yii::$app->request->get();
  525. $id = $get['id'] ?? 0;
  526. $info = OrderService::getOrderInfo($id, true, true, true, false);
  527. OrderClass::valid($info, $this->shopId);
  528. //是否有云打印判断
  529. $hasCloudPrint = 0;
  530. $shopExt = $this->shopExt;
  531. if (isset($shopExt->printSn) && !empty($shopExt->printSn) && isset($shopExt->printKey) && !empty($shopExt->printKey)) {
  532. $hasCloudPrint = 1;
  533. }
  534. $info['hasCloudPrint'] = $hasCloudPrint;
  535. //店长信息
  536. $customShopId = $info['customShopId'] ?? 0;
  537. $superInfo = ShopAdminClass::getSuper($customShopId);
  538. $info['customSuper'] = $superInfo;
  539. $purchaseId = $info['purchaseId'] ?? 0;
  540. $cg = PurchaseClass::getById($purchaseId);
  541. $info['cgInfo'] = $cg;
  542. util::success($info);
  543. }
  544. //自取免发货流程处理 ssh 2021.1.22
  545. public function actionWithoutSend()
  546. {
  547. $get = Yii::$app->request->get();
  548. $id = $get['id'] ?? 0;
  549. $info = OrderService::getById($id, true);
  550. OrderClass::valid($info, $this->shopId);
  551. $purchaseId = $info->purchaseId;
  552. $purchase = PurchaseClass::getById($purchaseId, true);
  553. OrderService::withoutSend($info, $purchase);
  554. util::complete();
  555. }
  556. //本店送 ssh 2021.1.24
  557. public function actionSelfSend()
  558. {
  559. $get = Yii::$app->request->get();
  560. $id = isset($get['id']) ? $get['id'] : 0;
  561. $info = OrderClass::getById($id, true);
  562. OrderClass::valid($info->attributes, $this->shopId);
  563. $connection = Yii::$app->db;
  564. $transaction = $connection->beginTransaction();
  565. try {
  566. OrderClass::selfSend($info);
  567. $transaction->commit();
  568. } catch (\Exception $exception) {
  569. $transaction->rollBack();
  570. Yii::info("本店送操作报错:" . $exception->getMessage());
  571. util::fail('操作失败');
  572. }
  573. util::complete();
  574. }
  575. //运费计算 ssh 2021.1.24
  576. public function actionFreight()
  577. {
  578. //2021.3.28 接入达达
  579. $get = Yii::$app->request->get();
  580. $post = Yii::$app->request->post();
  581. if (empty($get)) {
  582. $get = $post;
  583. }
  584. $orderId = $get['id'] ?? 0; //订单ID
  585. $customId = $get['customId'] ?? 0;
  586. if (empty($orderId)) {
  587. //自定义运费
  588. //freight::getCost();
  589. if (empty($customId)) {
  590. util::fail("请选择客户");
  591. }
  592. $customInfo = CustomClass::getById($customId);
  593. if (empty($customInfo)) {
  594. util::fail('没有找到客户');
  595. }
  596. $shop = $this->shop;
  597. //花材信息
  598. $productJson = $get['product'] ?? '';
  599. // $productJson = '[{"productId":31993,"bigNum":1,"smallNum":0}]';
  600. if (empty($productJson)) {
  601. util::fail('请选择花材');
  602. }
  603. $productList = json_decode($productJson, true);
  604. if (empty($productList)) {
  605. util::fail('请选择花材');
  606. }
  607. $productList = ProductClass::mergeItemInfo($productList);
  608. $weight = 0;
  609. $price = 0;
  610. $productData = ProductClass::getProductMapData($productList);
  611. $bigNum = 0;
  612. foreach ($productList as $key => $val) {
  613. $productId = $val['productId'];
  614. $w = ProductClass::getWeight($val['productId'], $val['bigNum'], $val['smallNum']);
  615. $bigNum += $val['bigNum'];
  616. $weight = bcadd($w, $weight, 2);
  617. $ratio = $productData[$productId]['ratio'] ?? 0;
  618. //大小数量合并多少扎
  619. $itemNum = ProductClass::mergeItemNum($val['bigNum'], $val['smallNum'], $ratio);
  620. //售卖价格
  621. $itemPrice = $productData[$productId]['price'] ?? 0;
  622. //合计价格
  623. $price = bcadd($price, bcmul($itemNum, $itemPrice, 2), 2);
  624. }
  625. //没有经纬度的客户运费直接返回空 shish 20210612
  626. if (empty($customInfo['lat']) || empty($customInfo['long'])) {
  627. util::success(['sedCost' => '']);
  628. }
  629. $cost = freight::getCost($shop->lat, $shop->long, $customInfo['lat'], $customInfo['long'], $weight);
  630. util::success(['sedCost' => $cost]);
  631. }
  632. $province = $get['province'] ?? '';
  633. $city = $get['city'] ?? '';
  634. $address = $get['address'] ?? '';
  635. $floor = $get['floor'] ?? '';
  636. $customName = $get['customName'] ?? '';
  637. $customMobile = $get['customMobile'] ?? '';
  638. $fullAddress = $province . $city . $address . $floor;
  639. $sendTime = $get['sendTime'] ?? '';
  640. $lat = $get['lat'] ?? '';
  641. $lng = $get['long'] ?? '';
  642. if (empty($lat) || empty($lng) || empty($fullAddress)) {
  643. util::fail('请填写客户地址');
  644. }
  645. ini_set('date.timezone', 'Asia/Shanghai');
  646. if (!empty($sendTime)) {
  647. //配送时间不为空
  648. // 预约发单时间(预约时间unix时间戳(10位),精确到分;整分钟为间隔,并且需要至少提前5分钟预约
  649. $sendTimeInt = strtotime($sendTime);
  650. $now = time();
  651. $diff = $sendTimeInt - $now;
  652. //因服务器写入时间,改为至少提前6分钟
  653. if ($diff <= 360) {
  654. util::fail('配送时间至少提前6分钟');
  655. }
  656. } else {
  657. //默认立即发送
  658. $sendTime = 0;
  659. }
  660. //更新订单表
  661. $info = OrderService::getOrderInfo($orderId);
  662. OrderClass::valid($info, $this->shopId);
  663. if ($sendTime) {
  664. $sendTime = date('Y-m-d H:i', strtotime($sendTime));
  665. } else {
  666. $sendTime = '0000-00-00 00:00:00';
  667. }
  668. $expressAddInfo = [
  669. 'customName' => $customName,
  670. 'customMobile' => $customMobile,
  671. 'province' => $province,
  672. 'city' => $city,
  673. 'address' => $address,
  674. 'floor' => $floor,
  675. 'lat' => $lat,
  676. 'long' => $lng,
  677. 'sendTime' => $sendTime,
  678. ];
  679. OrderClass::updateCustomExpressInfo($orderId, $expressAddInfo);
  680. //计算运费
  681. $info['province'] = $province;
  682. $info['city'] = $city;
  683. $info['address'] = $address;
  684. $info['floor'] = $floor;
  685. $info['fullAddress'] = $fullAddress;
  686. $info['lat'] = $lat;
  687. $info['long'] = $lng;
  688. $info['sendTime'] = $expressAddInfo['sendTime'];
  689. $res = DadaExpressServices::queryDeliverFee($info);
  690. util::success(['sedCost' => $res['fee']]);
  691. }
  692. //发快递和第三方配送 ssh 2021.1.24
  693. public function actionThirdSend()
  694. {
  695. $get = Yii::$app->request->get();
  696. $id = isset($get['id']) ? $get['id'] : 0;
  697. $info = OrderService::getById($id, true);
  698. OrderClass::valid($info->attributes, $this->shopId);
  699. if (empty($info->lat) || empty($info->long) || empty($info->fullAddress)) {
  700. util::fail('请填写客户地址');
  701. }
  702. $connection = Yii::$app->db;
  703. $transaction = $connection->beginTransaction();
  704. try {
  705. $respond = OrderClass::thirdSend($info, $get);
  706. $transaction->commit();
  707. util::success($respond);
  708. } catch (\Exception $exception) {
  709. $transaction->rollBack();
  710. Yii::info("发快递操作报错:" . $exception->getMessage());
  711. util::fail('操作失败' . $exception->getMessage());
  712. }
  713. }
  714. //确认送达 ssh 2021.1.24
  715. public function actionReach()
  716. {
  717. $get = Yii::$app->request->get();
  718. $id = isset($get['id']) ? $get['id'] : 0;
  719. $info = OrderClass::getById($id, true);
  720. OrderClass::valid($info->attributes, $this->shopId);
  721. $connection = Yii::$app->db;
  722. $transaction = $connection->beginTransaction();
  723. try {
  724. OrderService::reach($info);
  725. $transaction->commit();
  726. } catch (\Exception $exception) {
  727. $transaction->rollBack();
  728. Yii::info("送达操作报错:" . $exception->getMessage());
  729. util::fail('操作失败');
  730. }
  731. util::complete();
  732. }
  733. //下单要用到的相关信息 ssh 2019.12.6
  734. public function actionOrderRelate()
  735. {
  736. $regionTree = RegionService::tree();
  737. $shop = $this->shop->attributes;
  738. $freight = ['first' => 5, 'add' => 2];
  739. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  740. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'txMapKey' => $mapKey, 'merchant' => $shop];
  741. util::success($out);
  742. }
  743. //客户欠款的订单 ssh 2021.2.4
  744. public function actionDebtList()
  745. {
  746. $id = Yii::$app->request->get('id', 0);
  747. $info = CustomClass::getCustom($id);
  748. CustomClass::valid($info, $this->shopId);
  749. $where = ['customId' => $id, 'debt' => 1];
  750. $respond = OrderService::getDebtOrderList($where);
  751. $respond['customInfo'] = $info;
  752. util::success($respond);
  753. }
  754. //更新打印次数
  755. public function actionAddPrintNum()
  756. {
  757. $id = Yii::$app->request->get('id', 0);
  758. $order = OrderClass::getById($id, true);
  759. OrderClass::valid($order, $this->shopId);
  760. $order->printNum += 1;
  761. $order->save();
  762. util::complete();
  763. }
  764. //打印订单 shish 20210714
  765. public function actionCloudPrintOrder()
  766. {
  767. $id = Yii::$app->request->get('id', 0);
  768. $order = OrderClass::getById($id, true);
  769. OrderClass::valid($order, $this->shopId);
  770. $ext = $this->shopExt;
  771. if (isset($ext->printSn) == false || isset($ext->printKey) == false || empty($ext->printSn) || empty($ext->printKey)) {
  772. util::success(['hasNoPrint' => 1]);
  773. }
  774. OrderClass::onlinePrint($order);
  775. $order->printNum += 1;
  776. $order->save();
  777. util::complete();
  778. }
  779. //订单确认完成 shish 20210809
  780. public function actionConfirmFinish()
  781. {
  782. $payWay = Yii::$app->request->get('payWay', -1);
  783. $id = Yii::$app->request->get('id', 0);
  784. $clearType = Yii::$app->request->get('clearType', 0);
  785. $connection = Yii::$app->db;
  786. $transaction = $connection->beginTransaction();
  787. try {
  788. $order = OrderClass::getById($id, true);
  789. OrderClass::valid($order, $this->shopId);
  790. $purchaseId = $order->purchaseId ?? 0;
  791. $purchase = PurchaseClass::getById($purchaseId, true);
  792. if (empty($purchase)) {
  793. util::fail('没有采购信息');
  794. }
  795. if ($clearType == 1) {
  796. //欠款
  797. $payWay = dict::getDict('payWay', 'debtPay');
  798. } elseif ($clearType == 2) {
  799. //已收款
  800. if ($payWay == -1) {
  801. util::fail('请选择收款方式');
  802. }
  803. $shopAdmin = $this->shopAdmin;
  804. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  805. util::fail('超管才能发起已收款');
  806. }
  807. } else {
  808. util::fail('请选择结算方式');
  809. }
  810. //不需要打印
  811. $order->needPrint = dict::getDict('needPrint', 'noNeed');
  812. $order->save();
  813. //支付
  814. PurchaseService::payAfter($purchase, $payWay);
  815. OrderService::payAfter($order, $payWay);
  816. //自己送
  817. $info = OrderClass::getById($id, true);
  818. OrderClass::selfSend($info);
  819. //确认送达,并且不需要微信通知
  820. Yii::$app->params['noNeedWxNotice'] = 1;
  821. $info = OrderClass::getById($id, true);
  822. OrderService::reach($info);
  823. $transaction->commit();
  824. util::complete('操作成功');
  825. } catch (\Exception $exception) {
  826. $transaction->rollBack();
  827. Yii::info("确认完成订单报错:" . $exception->getMessage());
  828. util::fail('操作失败');
  829. }
  830. }
  831. //取消订单 shish 20210810
  832. public function actionCancel()
  833. {
  834. $shopAdmin = $this->shopAdmin;
  835. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  836. util::fail('超管才能取消订单');
  837. }
  838. $id = Yii::$app->request->get('id', 0);
  839. $order = OrderClass::getLockById($id);
  840. OrderClass::valid($order, $this->shopId);
  841. OrderService::expire($order, true);
  842. util::complete();
  843. }
  844. }