OrderController.php 32 KB

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