OrderController.php 33 KB

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