OrderController.php 32 KB

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