OrderController.php 34 KB

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