OrderController.php 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  1. <?php
  2. namespace hd\controllers;
  3. use biz\shop\classes\ShopExtClass;
  4. use biz\sj\services\MerchantExtendService;
  5. use biz\sj\services\MerchantService;
  6. use bizHd\custom\classes\CustomClass;
  7. use bizHd\goods\classes\GoodsClass;
  8. use bizHd\merchant\services\ShopService;
  9. use bizHd\order\classes\OrderClass;
  10. use bizHd\order\classes\OrderSendClass;
  11. use bizHd\order\services\OrderService;
  12. use bizHd\product\classes\ProductClass;
  13. use bizHd\saas\services\RegionService;
  14. use bizHd\shop\classes\ShopClass;
  15. use bizHd\user\services\UserService;
  16. use bizHd\work\classes\WorkClass;
  17. use bizHd\wx\classes\WxOpenClass;
  18. use common\components\dict;
  19. use common\components\dateUtil;
  20. use common\components\payUtil;
  21. use common\services\xhPayToolService;
  22. use Yii;
  23. use common\components\util;
  24. use common\components\stringUtil;
  25. use bizHd\promote\services\CouponService;
  26. use common\components\httpUtil;
  27. use biz\wx\classes\WxMessageClass;
  28. class OrderController extends BaseController
  29. {
  30. public $guestAccess = ['order-relate', 'fast-pay', 'create-order', 'list'];
  31. //修改订单 ssh 20220926
  32. public function actionUpdate()
  33. {
  34. $post = Yii::$app->request->post();
  35. $id = $post['id'] ?? 0;
  36. $order = OrderClass::getById($id, true);
  37. OrderClass::valid($order, $this->mainId);
  38. unset($post['id']);
  39. if ($order->status == 3) {
  40. util::fail('订单已配送');
  41. }
  42. if ($order->status == 4) {
  43. util::fail('订单已完成');
  44. }
  45. if ($order->status == 5) {
  46. util::fail('订单已取消');
  47. }
  48. if ($order->fromType == 4) {
  49. util::fail('请在美团APP上修改');
  50. }
  51. $address = $post['address'] ?? '';
  52. $floor = $post['floor'] ?? '';
  53. $post['fullAddress'] = $address . $floor;
  54. OrderClass::updateById($id, $post);
  55. $workList = WorkClass::getAllByCondition(['orderId' => $id], null, '*', null, true);
  56. $remark = $post['remark'] ?? '';
  57. $reachDate = $post['reachDate'] ?? '';
  58. $reachPeriod = $post['reachPeriod'] ?? '';
  59. $cardInfo = $post['cardInfo'] ?? '';
  60. $anonymity = $post['anonymity'] ?? 0;
  61. $bookName = $post['bookName'] ?? '';
  62. if (!empty($workList)) {
  63. foreach ($workList as $work) {
  64. $work->remark = $remark;
  65. $work->reachDate = $reachDate;
  66. $work->reachPeriod = $reachPeriod;
  67. $work->cardInfo = $cardInfo;
  68. $work->anonymity = $anonymity;
  69. $work->bookName = $bookName;
  70. $work->save();
  71. }
  72. }
  73. $shopExt = ShopExtClass::getByCondition(['shopId' => $this->shopId], true);
  74. ShopExtClass::orderUpdateRemind($shopExt, $order);
  75. util::complete();
  76. }
  77. //客户欠款的订单 ssh 2021.2.4
  78. public function actionDebtList()
  79. {
  80. $get = Yii::$app->request->get();
  81. $id = $get['id'] ?? 0;
  82. $info = CustomClass::getById($id, true);
  83. CustomClass::valid($info, $this->shopId);
  84. $where = ['customId' => $id, 'debt' => 1];
  85. $searchTime = $get['searchTime'] ?? '';
  86. if (!empty($searchTime)) {
  87. $startTime = $get['startTime'] ?? '';
  88. $endTime = $get['endTime'] ?? '';
  89. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  90. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  91. }
  92. $list = OrderClass::getAllByCondition($where, 'addTime DESC', ['id', 'orderSn', 'actPrice', 'addTime', 'remainDebtPrice']);
  93. $result = ['customInfo' => $info, 'list' => $list];
  94. util::success($result);
  95. }
  96. //发货
  97. public function actionSend()
  98. {
  99. $get = Yii::$app->request->get();
  100. $id = isset($get['id']) ? $get['id'] : 0;
  101. $order = OrderClass::getById($id, true);
  102. if (empty($order)) {
  103. util::fail('没有找到订单');
  104. }
  105. OrderClass::valid($order, $this->mainId);
  106. if ($order->status != 2) {
  107. util::fail('订单不是待发货状态');
  108. }
  109. $workList = WorkClass::getAllByCondition(['orderId' => $id], null, '*', null, true);
  110. if (!empty($workList)) {
  111. $unComplete = false;
  112. foreach ($workList as $work) {
  113. if ($work->status != 1) {
  114. $unComplete = true;
  115. }
  116. }
  117. if ($unComplete == true) {
  118. util::fail('还有制作单没有完成');
  119. }
  120. }
  121. $order->status = 4;
  122. $order->save();
  123. util::complete('操作成功');
  124. }
  125. //打印订单 ssh 20220601
  126. public function actionPrintOrder()
  127. {
  128. $get = Yii::$app->request->get();
  129. $id = isset($get['id']) ? $get['id'] : 0;
  130. $order = OrderClass::getById($id, true);
  131. if (empty($order)) {
  132. util::fail('没有找到订单');
  133. }
  134. OrderClass::valid($order, $this->mainId);
  135. OrderClass::onlinePrint($order, true);
  136. util::complete();
  137. }
  138. //微信和支付宝付款码支付复查 ssh 20220422
  139. public function actionCodePayCheck()
  140. {
  141. ini_set('date.timezone', 'Asia/Shanghai');
  142. header("Content-type: text/html; charset=utf-8");
  143. $get = Yii::$app->request->get();
  144. $id = isset($get['id']) ? $get['id'] : 0;
  145. $recheck = $get['recheck'] ?? 0;
  146. $order = OrderClass::getById($id, true);
  147. OrderClass::valid($order, $this->mainId);
  148. $orderSn = $order->orderSn ?? '';
  149. $totalFee = $order->mainPay ?? 0;
  150. if ($order->status == OrderClass::ORDER_STATUS_COMPLETE) {
  151. util::success(['returnStatus' => 'SUCCESS']);
  152. }
  153. if ($order->status != OrderClass::ORDER_STATUS_UN_PAY) {
  154. util::complete('订单不是待付款状态');
  155. }
  156. $connection = Yii::$app->db;
  157. $transaction = $connection->beginTransaction();
  158. try {
  159. if ($recheck == 1) {
  160. $payWay = $get['payWay'] ?? 0;
  161. } else {
  162. $payWay = $cg->payWay ?? dict::getDict('payWay', 'wxPay');
  163. }
  164. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  165. if ($payWay == dict::getDict('payWay', 'wxPay')) {
  166. //微信付款
  167. $wx = Yii::getAlias("@vendor/wxPayApi_v3.0.10");
  168. require_once($wx . '/lib/WxPay.Api.php');
  169. require_once($wx . '/example/WxPay.MicroPay.php');
  170. //获取微信商户号等信息
  171. $sjExtend = WxOpenClass::getMallWxInfo();
  172. $microPay = new \MicroPay();
  173. $payReturn = $microPay->query($orderSn, $sjExtend);
  174. if ($payReturn["return_code"] == "SUCCESS" && $payReturn["result_code"] == "SUCCESS") {
  175. if ($payReturn["trade_state"] == "SUCCESS") {
  176. //支付成功
  177. $transactionId = $payReturn['transaction_id'] ?? '';
  178. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  179. $order->save();
  180. $attach = '';
  181. payUtil::thirdPay($payWay, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  182. $transaction->commit();
  183. //打印小票和语音播报
  184. $newOrder = OrderClass::getById($id, true);
  185. OrderClass::onlinePrint($newOrder);
  186. ShopExtClass::hdGatheringReport($newOrder);
  187. $shopId = $newOrder->shopId ?? 0;
  188. $shop = ShopClass::getById($shopId, true);
  189. WxMessageClass::gatheringIncomeInform($shop, $newOrder);
  190. util::success(['returnStatus' => 'SUCCESS']);
  191. } else if ($payReturn["trade_state"] == "USERPAYING") {
  192. //用户支付中
  193. util::complete('正在付款,请稍候..');
  194. }
  195. }
  196. if ($payReturn["err_code"] == "ORDERNOTEXIST") {
  197. //交易订单号不存在
  198. util::complete('订单没有付款');
  199. } else {
  200. util::complete('系统错误,请稍候再试');
  201. }
  202. } elseif ($payWay == dict::getDict('payWay', 'alipay')) {
  203. //支付宝付款
  204. $aliF2F = Yii::getAlias("@vendor/alipayF2F");
  205. require_once($aliF2F . '/f2fpay/model/builder/AlipayTradePayContentBuilder.php');
  206. require_once($aliF2F . '/f2fpay/service/AlipayTradeService.php');
  207. // $aliPId = '2017041906821571';
  208. // $aliKey = 'MIIEpAIBAAKCAQEAvbeu6o90L+0AilI+mbhuJJH0lUv7hWQCEC+XUc+cWlC9ulKC3cSVScVLJHpUc09aj6v/iD1p9iVfgMxjeADNVKiQC2RJyRshAjzyTq+2bTcODp619iV2y4qW8UM65VcP7Ide3P0uT5xC2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zSrmOZlqXNCwO/36Vh6dshhhE/birIlXJSP0xdzpBQy2ksyli9eGy8cdJ2Y72Wo+TjSclRbKiPQKBgQD1C/CJRQbahz6B3Sc4NOiFUQioM30qDKbDibzUq95u5PtQOx69+/h68tA2nj7ILZOXoh6/KF6LM9WK0QctY0HR5Uy4Uz8YkY/Y0L/LExSkNdpYqbpBc8DdIsKYHoMLNHDwc03tpINRLrmq00sD0907oeuY8/2l6P7XOZSnCvE5Fw==';
  212. $aliPublicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgEb9zdQQDva63DhksZHPZCDrRe2xvZcTRy9mEiWs/pUhCG0QABkuePUyvqTAKINiCEv2QJmdF8qt3hmTSXuME5bT4vghckSGUTp4CNzsT90Vfjk4wci+bpAqNnpUASDuqZLCzebTuOhxYtGVBqYzhp/CxcC/Mag3ISjUG6jRqyUi15R1e2ruq74sohLGnb4m3nnwRVwnNVAaUcVfV1J0Nb0/9pusrj2t6FCyCkFeWbRDzKhplPQejaET2p5yplwSUpd1gqF+3srIn3erW5uAR6F0Z0NoCMzlo5TEXKWY2XMq97ubueE7Cgp8/c3dh5dkscAT2CCWwRq23aKozp+XMwIDAQAB';
  213. $config = array(
  214. //签名方式,默认为RSA2(RSA2048)
  215. 'sign_type' => "RSA2",
  216. //支付宝公钥
  217. 'alipay_public_key' => $aliPublicKey,
  218. //商户私钥
  219. 'merchant_private_key' => $aliKey,
  220. //编码格式
  221. 'charset' => "UTF-8",
  222. //支付宝网关
  223. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  224. //应用ID
  225. 'app_id' => $aliPId,
  226. //异步通知地址,只有扫码支付预下单可用
  227. 'notify_url' => Yii::$app->params['mallHost'] . "/notice/ali-callback",
  228. //最大查询重试次数
  229. 'MaxQueryRetry' => "10",
  230. //查询间隔
  231. 'QueryDuration' => "3"
  232. );
  233. //构造查询业务请求参数对象
  234. $queryContentBuilder = new \AlipayTradeQueryContentBuilder();
  235. $queryContentBuilder->setOutTradeNo($orderSn);
  236. //初始化类对象,调用queryTradeResult方法获取查询应答
  237. $queryResponse = new \AlipayTradeService($config);
  238. $queryResult = $queryResponse->queryTradeResult($queryContentBuilder);
  239. if ($queryResult->getTradeStatus() == 'SUCCESS') {
  240. $respond = $queryResult->getResponse();
  241. $transactionId = $respond->trade_no ?? '';
  242. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  243. $order->save();
  244. $attach = '';
  245. payUtil::thirdPay($payWay, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  246. $transaction->commit();
  247. //打印小票和语音播报
  248. $newOrder = OrderClass::getById($id, true);
  249. OrderClass::onlinePrint($newOrder);
  250. ShopExtClass::hdGatheringReport($newOrder);
  251. $shopId = $newOrder->shopId ?? 0;
  252. $shop = ShopClass::getById($shopId, true);
  253. WxMessageClass::gatheringIncomeInform($shop, $newOrder);
  254. util::success(['returnStatus' => 'SUCCESS']);
  255. }else{
  256. util::complete('没有付款');
  257. }
  258. } else {
  259. util::complete('还没有付款哦');
  260. }
  261. } catch (\Exception $e) {
  262. $transaction->rollBack();
  263. Yii::error("支付失败原因:" . $e->getMessage());
  264. util::complete('支付失败');
  265. }
  266. }
  267. //微信和支付宝付款码支付 ssh 2021.4.11
  268. public function actionCodePay()
  269. {
  270. ini_set('date.timezone', 'Asia/Shanghai');
  271. header("Content-type: text/html; charset=utf-8");
  272. $get = Yii::$app->request->get();
  273. $id = isset($get['id']) ? $get['id'] : 0;
  274. $order = OrderClass::getById($id, true);
  275. OrderClass::valid($order, $this->mainId);
  276. $authCode = (string)$get['authCode'] ?? '';
  277. if (empty($authCode)) {
  278. util::fail('没有获取到支付码');
  279. }
  280. $wxPrefix = ['10', '11', '12', '13', '14', '15'];
  281. $isWx = false;
  282. foreach ($wxPrefix as $wxId) {
  283. if (strpos($authCode, $wxId) === 0) {
  284. $isWx = true;
  285. }
  286. }
  287. $orderSn = $order->orderSn ?? '';
  288. $outTradeNo = $orderSn;
  289. $subject = '购买商品';
  290. $totalFee = $order->mainPay ?? 0;
  291. $totalAmount = $totalFee;
  292. $body = "门店";
  293. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  294. $connection = Yii::$app->db;
  295. $transaction = $connection->beginTransaction();
  296. try {
  297. if ($isWx) {
  298. $wxPayWay = dict::getDict('payWay', 'wxPay');
  299. $order->payWay = $wxPayWay;
  300. $order->save();
  301. $wx = Yii::getAlias("@vendor/wxPayApi_v3.0.10");
  302. require_once($wx . '/lib/WxPay.Api.php');
  303. require_once($wx . '/example/WxPay.MicroPay.php');
  304. $input = new \WxPayMicroPay();
  305. $input->SetAuth_code($authCode);
  306. $input->SetBody($subject);
  307. $input->SetTotal_fee($totalFee * 100);
  308. $input->SetOut_trade_no($orderSn);
  309. //获取微信商户号等信息
  310. $sjExtend = WxOpenClass::getMallWxInfo();
  311. $microPay = new \MicroPay();
  312. $res = $microPay->pay($input, $sjExtend);
  313. if (isset($res["return_code"]) && $res["return_code"] == "SUCCESS" && isset($res["result_code"]) && $res["result_code"] == 'SUCCESS') {
  314. $transactionId = $res['transaction_id'] ?? '';
  315. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  316. $order->save();
  317. $attach = '';
  318. payUtil::thirdPay($wxPayWay, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  319. $transaction->commit();
  320. $newOrder = OrderClass::getById($id, true);
  321. //打印小票
  322. OrderClass::onlinePrint($newOrder);
  323. //语音播报
  324. ShopExtClass::hdGatheringReport($newOrder);
  325. $shopId = $newOrder->shopId ?? 0;
  326. $shop = ShopClass::getById($shopId, true);
  327. WxMessageClass::gatheringIncomeInform($shop, $newOrder);
  328. util::success(['returnStatus' => 'SUCCESS']);
  329. } else {
  330. Yii::info(json_encode($res));
  331. $returnMsg = $res['return_msg'] ?? '';
  332. Yii::info($returnMsg);
  333. $errCode = $res['err_code'] ?? '';
  334. $msg = '';
  335. if ($errCode == 'NOTENOUGH') {
  336. $msg = '余额不足';
  337. }
  338. if ($errCode == 'USERPAYING') {
  339. $msg = '等待客户输入支付密码';
  340. }
  341. util::success(['returnStatus' => 'FAILURE'], $msg);
  342. }
  343. } else {
  344. $aliPayWay = dict::getDict('payWay', 'alipay');
  345. $order->payWay = $aliPayWay;
  346. $order->save();
  347. $aliF2F = Yii::getAlias("@vendor/alipayF2F");
  348. require_once($aliF2F . '/f2fpay/model/builder/AlipayTradePayContentBuilder.php');
  349. require_once($aliF2F . '/f2fpay/service/AlipayTradeService.php');
  350. // $aliPId = '2017041906821571';
  351. // $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+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2CCWwRq23aKozp+XMwIDAQAB';
  356. $config = array(
  357. //签名方式,默认为RSA2(RSA2048)
  358. 'sign_type' => "RSA2",
  359. //支付宝公钥
  360. 'alipay_public_key' => $aliPublicKey,
  361. //商户私钥
  362. 'merchant_private_key' => $aliKey,
  363. //编码格式
  364. 'charset' => "UTF-8",
  365. //支付宝网关
  366. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  367. //应用ID
  368. 'app_id' => $aliPId,
  369. //异步通知地址,只有扫码支付预下单可用
  370. 'notify_url' => Yii::$app->params['mallHost'] . "/notice/ali-callback",
  371. //最大查询重试次数
  372. 'MaxQueryRetry' => "10",
  373. //查询间隔
  374. 'QueryDuration' => "3"
  375. );
  376. // 支付超时,线下扫码交易定义为5分钟
  377. $timeExpress = "5m";
  378. // 创建请求builder,设置请求参数
  379. $barPayRequestBuilder = new \AlipayTradePayContentBuilder();
  380. $barPayRequestBuilder->setOutTradeNo($outTradeNo);
  381. $barPayRequestBuilder->setTotalAmount($totalAmount);
  382. $barPayRequestBuilder->setAuthCode($authCode);
  383. $barPayRequestBuilder->setTimeExpress($timeExpress);
  384. $barPayRequestBuilder->setSubject($subject);
  385. $barPayRequestBuilder->setBody($body);
  386. // 调用barPay方法获取当面付应答
  387. $barPay = new \AlipayTradeService($config);
  388. $barPayResult = $barPay->barPay($barPayRequestBuilder);
  389. if ($barPayResult->getTradeStatus() == 'SUCCESS') {
  390. $respond = $barPayResult->getResponse();
  391. $transactionId = $respond->trade_no ?? '';
  392. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  393. $order->save();
  394. $attach = '';
  395. payUtil::thirdPay($aliPayWay, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  396. $transaction->commit();
  397. $newOrder = OrderClass::getById($id, true);
  398. //打印小票
  399. OrderClass::onlinePrint($newOrder);
  400. //语音播报
  401. ShopExtClass::hdGatheringReport($newOrder);
  402. $shopId = $newOrder->shopId ?? 0;
  403. $shop = ShopClass::getById($shopId, true);
  404. WxMessageClass::gatheringIncomeInform($shop, $newOrder);
  405. util::success(['returnStatus' => 'SUCCESS']);
  406. }
  407. util::success(['returnStatus' => 'FAILURE']);
  408. }
  409. } catch (\Exception $e) {
  410. $transaction->rollBack();
  411. Yii::error("支付失败原因:" . $e->getMessage());
  412. util::fail('支付失败');
  413. }
  414. }
  415. //开单操作 ssh 20220316
  416. public function actionCreateOrder()
  417. {
  418. $post = Yii::$app->request->post();
  419. $post['sjId'] = $this->sjId;
  420. $post['shopId'] = $this->shopId;
  421. $post['mainId'] = $this->mainId ?? 0;
  422. $now = time();
  423. $orderValidTime = getenv('ORDER_VALID_TIME') == false ? 600 : getenv('ORDER_VALID_TIME');
  424. $expireTime = $now + $orderValidTime;
  425. $post['deadline'] = $expireTime;
  426. $post['staffId'] = $this->shopAdminId ?? 0;
  427. $post['staffName'] = $this->shopAdmin->name ?? '';
  428. //默认情况开单员工和收款员工是同个人
  429. $staffId = $this->shopAdminId ?? 0;
  430. $staffName = $this->shopAdmin->name ?? '';
  431. if (isset($post['shopAdminId']) == false || empty($post['shopAdminId'])) {
  432. $post['shopAdminId'] = $staffId;
  433. $post['shopAdminName'] = $staffName;
  434. }
  435. if (isset($post['getStaffId']) == false || empty($post['getStaffId'])) {
  436. $post['getStaffId'] = $staffId;
  437. $post['getStaffName'] = $this->shopAdmin->name ?? '';
  438. }
  439. $groupId = isset($post['groupId']) && !empty($post['groupId']) ? $post['groupId'] : 0;
  440. $post['fromType'] = $post['fromType'] ?? 1;
  441. if ($post['fromType'] == dict::getDict('fromType', 'friend')) {
  442. if (isset($post['bookName']) == false || empty($post['bookName'])) {
  443. util::fail('请填写订花人姓名');
  444. }
  445. }
  446. $customId = isset($post['customId']) && $post['customId'] > 0 ? $post['customId'] : 0;
  447. if (empty($customId)) {
  448. $customId = $this->shop->defaultCustomId ?? 0;
  449. }
  450. $custom = CustomClass::getById($customId);
  451. if (empty($custom)) {
  452. util::fail('没有找到客户哦');
  453. }
  454. $post['customId'] = $customId;
  455. $customName = $custom['name'] ?? '';
  456. $post['customName'] = $customName;
  457. $post['customNamePy'] = stringUtil::py($customName);
  458. if (isset($post['receiveMobile']) && !empty($post['receiveMobile'])) {
  459. if (stringUtil::isMobile($post['receiveMobile']) == false) {
  460. util::fail('请填写正确的收花人手机号');
  461. }
  462. }
  463. if (isset($post['bookMobile']) && !empty($post['bookMobile'])) {
  464. if (stringUtil::isMobile($post['bookMobile']) == false) {
  465. util::fail('请填写正确的订花人手机号');
  466. }
  467. }
  468. $hasPay = $post['hasPay'] ?? dict::getDict('hasPay', 'unPay');
  469. if (isset($post['isCashier']) && $post['isCashier'] == 1) {
  470. //收银台开单默认到店自取
  471. $post['sendType'] = dict::getDict('sendType', 'shopGet');
  472. }
  473. $productJson = $post['product'] ?? '';
  474. $productList = [];
  475. if (!empty($productJson)) {
  476. $productList = json_decode($productJson, true);
  477. }
  478. if (empty($productList) && empty($groupId)) {
  479. //商家手机上开单并生成制作单
  480. if (isset($post['unitGoodsPrice']) && !empty($post['unitGoodsPrice'])) {
  481. $unitGoodsPrice = $post['unitGoodsPrice'];
  482. if (is_numeric($unitGoodsPrice) == false) {
  483. util::fail('请输入正确的单价');
  484. }
  485. $productList = GoodsClass::orderCreateGoods($post);
  486. }
  487. }
  488. if (empty($productList) && empty($groupId)) {
  489. if (isset($post['lsGoodsPrice']) == false || $post['lsGoodsPrice'] <= 0) {
  490. $lsGoodsPrice = $post['lsGoodsPrice'];
  491. if (is_numeric($lsGoodsPrice) == false) {
  492. util::fail('请输入正确的金额');
  493. }
  494. util::fail('请填写金额或选商品');
  495. }
  496. }
  497. $connection = Yii::$app->db;
  498. $transaction = $connection->beginTransaction();
  499. try {
  500. $post['product'] = $productList;
  501. //阿东开5支老是会出现5扎情况跟踪
  502. ProductClass::adStockCheck($this->shop, $productList);
  503. $return = OrderService::createFinishOrder($post, $custom, $hasPay);
  504. $transaction->commit();
  505. //打印小票和语音播报
  506. $id = $return->id ?? 0;
  507. $order = OrderClass::getById($id, true);
  508. if ($order->fromType == 3 || $order->fromType == 4) {
  509. //除了客服号下单和美团下单,其它情况允许打小票
  510. } else {
  511. //前台打小票
  512. OrderClass::onlinePrint($order);
  513. }
  514. //前台提示收款多少钱
  515. ShopExtClass::hdGatheringReport($order);
  516. //前台提醒出示付款码
  517. if (isset($order->status) && $order->status == 1) {
  518. if (isset($post['isCashier']) && $post['isCashier'] == 1) {
  519. ShopExtClass::pleasePayReport($order);
  520. }
  521. }
  522. $shopId = $order->shopId ?? 0;
  523. $shop = ShopClass::getById($shopId, true);
  524. WxMessageClass::gatheringIncomeInform($shop, $order);
  525. //新制作单提示
  526. $shopExt = ShopExtClass::getByCondition(['shopId' => $this->shopId], true);
  527. ShopExtClass::newWorkRemind($shopExt, $order);
  528. util::success($return);
  529. } catch (\Exception $e) {
  530. $transaction->rollBack();
  531. Yii::error("失败原因:" . $e->getMessage());
  532. util::fail('下单失败');
  533. }
  534. }
  535. //下单要用到的相关信息 ssh 2019.12.6
  536. public function actionOrderRelate()
  537. {
  538. $regionTree = RegionService::tree();
  539. $shop = $this->shop->attributes;
  540. $freight = MerchantExtendService::getFreight($this->sjExtend);
  541. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  542. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'txMapKey' => $mapKey, 'merchant' => $shop];
  543. util::success($out);
  544. }
  545. //余额支付 ssh 2019.12.6
  546. public function actionBalancePay()
  547. {
  548. $post = Yii::$app->request->post();
  549. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  550. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  551. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  552. $order = OrderService::getByOrderSn($orderSn);
  553. $userId = $this->adminId;
  554. $user = UserService::getUserInfo($userId);
  555. //验证支付密码是否正确
  556. UserService::validPayPassword($payPassword, $user);
  557. //支付前验证订单有效性
  558. OrderService::checkBeforePay($order);
  559. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  560. $totalFee = $order['prePrice'];
  561. $sourceType = 0;
  562. $discountData = OrderService::getDiscountPrice(['price' => $totalFee, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  563. $actPrice = $discountData['price'];
  564. $callbackParams = [];
  565. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  566. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  567. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'balance' => $balance]);
  568. }
  569. //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
  570. public function actionWxPay()
  571. {
  572. ini_set('date.timezone', 'Asia/Shanghai');
  573. $post = Yii::$app->request->post();
  574. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  575. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  576. $order = OrderService::getByOrderSn($orderSn);
  577. $orderId = $order['id'];
  578. //验证优惠券是否还有效
  579. if (!empty($couponId)) {
  580. CouponService::checkBeforeUse($couponId, $order['prePrice'], $order['userId']);
  581. }
  582. //支付前验证订单有效性
  583. OrderService::checkBeforePay($order);
  584. $name = isset($order['orderName']) && !empty($order['orderName']) ? $order['orderName'] : '购买商品';
  585. $totalFee = $order['realPrice'];
  586. //小程序使用miniOpenId
  587. if (httpUtil::isMiniProgram()) {
  588. $openId = $this->user['miniOpenId'];
  589. } else {
  590. $openId = $this->user['openId'];
  591. }
  592. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  593. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  594. $wxPayType = 0;
  595. if (httpUtil::isMiniProgram()) {
  596. $wxPayType = 1;
  597. }
  598. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  599. //订单30分钟后过期
  600. $now = time();
  601. $expireTime = $now + 1800;
  602. $wx = Yii::getAlias("@vendor/weixin");
  603. require_once($wx . '/lib/WxPay.Api.php');
  604. require_once($wx . '/example/WxPay.JsApiPay.php');
  605. $input = new \WxPayUnifiedOrder();
  606. $input->SetBody($name);
  607. $input->SetOut_trade_no($orderSn);
  608. $input->SetTotal_fee($totalFee * 100);
  609. $input->SetTime_start(date("YmdHis", $now));
  610. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  611. $input->SetTime_expire(date("YmdHis", $expireTime));
  612. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  613. $input->SetTrade_type("JSAPI");
  614. //花卉宝代为申请的微信支付
  615. $merchantExtend = $this->sjExtend->attributes;
  616. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  617. $input->SetSub_openid($openId);
  618. } else {
  619. $input->SetOpenid($openId);
  620. }
  621. //小程序使用miniAppId
  622. if (httpUtil::isMiniProgram()) {
  623. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  624. }
  625. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  626. $tools = new \JsApiPay();
  627. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  628. $newParams = json_decode($jsApiParameters, true);
  629. //已请求微信不能修改价格
  630. $updateData = [];
  631. $updateData['modPrice'] = 0;
  632. if (empty($order['deadline'])) {
  633. $updateData['deadline'] = $expireTime;
  634. }
  635. OrderService::updateById($orderId, $updateData);
  636. util::success($newParams);
  637. }
  638. //快捷付款订单
  639. public function actionFastPay()
  640. {
  641. ini_set('date.timezone', 'Asia/Shanghai');
  642. $post = Yii::$app->request->post();
  643. $payWay = isset($post['payWay']) ? $post['payWay'] : 0;
  644. $shopId = !empty($this->shopId) ? $this->shopId : ShopService::getDefaultShopId($this->sj);
  645. //验证门店是否有效
  646. $shopInfo = ShopService::getById($shopId);
  647. ShopService::valid($shopInfo, $this->sjId);
  648. $post['shopId'] = $shopId;
  649. //默认门店订单
  650. $store = isset($post['store']) ? $post['store'] : 1;
  651. $post['store'] = $store;
  652. //来源 0微信 1支付宝 2小程序 3朋友圈 4美团
  653. $sourceType = $this->isWx ? 0 : 1;
  654. if (httpUtil::isMiniProgram()) {
  655. $sourceType = 2;
  656. }
  657. $sourceType = isset($post['sourceType']) ? $post['sourceType'] : 0;
  658. //兼容旧系统sourceType=3表示朋友圈来源
  659. $fromType = $sourceType == 3 ? 2 : 0;
  660. $fromType = isset($post['fromType']) && !empty($post['fromType']) ? $post['fromType'] : $fromType;
  661. $post['userId'] = $this->adminId;
  662. $custom = $this->custom;
  663. $post['bookName'] = $custom['userName'] ?? '';
  664. $bookMobile = isset($post['bookMobile']) && !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  665. $bookMobile = empty($bookMobile) && isset($custom['mobile']) && !empty($custom['mobile']) ? $custom['mobile'] : $bookMobile;
  666. $post['bookMobile'] = $bookMobile;
  667. $prePrice = round($post['prePrice'], 2);
  668. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  669. $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  670. $actPrice = $discountData['price'];
  671. $post['discountType'] = $discountData['discountType'];
  672. $post['discountAmount'] = $discountData['discountAmount'];
  673. $post['actPrice'] = $actPrice;
  674. $post['realPrice'] = $actPrice;
  675. $post['payStyle'] = 0;
  676. $post['sjId'] = $this->sjId;
  677. $now = time();
  678. $expireTime = $now + 300;//订单5分钟后过期
  679. $post['createTime'] = date("Y-m-d H:i:s", $now);
  680. $post['deadline'] = $expireTime;
  681. if ($actPrice <= 0) {
  682. util::fail('请填写正确的金额');
  683. }
  684. //微信支付订单提交不能修改价格
  685. $post['modPrice'] = $this->isWx ? 0 : 1;
  686. $post['sourceType'] = $sourceType;
  687. $post['goodsNum'] = 1;
  688. $post['fromType'] = $fromType;
  689. $order = OrderService::addOrder($post);
  690. $orderId = $order['id'];
  691. $orderSn = $order['orderSn'];
  692. $sjId = $this->sjId;
  693. if ($payWay == 0) {
  694. $orderId = $order['id'];
  695. $name = '购买商品';
  696. $totalFee = $actPrice;
  697. $user = UserService::getById($this->adminId);
  698. $openId = isset($user['openId']) ? $user['openId'] : 0;
  699. if (httpUtil::isMiniProgram()) {
  700. //小程序使用miniOpenId
  701. $openId = $user['miniOpenId'];
  702. }
  703. if (empty($openId)) {
  704. util::fail('没有找到客户的openId');
  705. }
  706. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  707. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  708. $wxPayType = 0;
  709. if (httpUtil::isMiniProgram()) {
  710. $wxPayType = 1;
  711. }
  712. $attach = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&wxPayType=' . $wxPayType;
  713. $wx = Yii::getAlias("@vendor/weixin");
  714. require_once($wx . '/lib/WxPay.Api.php');
  715. require_once($wx . '/example/WxPay.JsApiPay.php');
  716. $input = new \WxPayUnifiedOrder();
  717. $input->SetBody($name);
  718. $input->SetOut_trade_no($orderSn);
  719. $input->SetTotal_fee($totalFee * 100);
  720. $input->SetTime_start(date("YmdHis", $now));
  721. $input->SetAttach($attach);
  722. //设置订单有效期5分钟
  723. $input->SetTime_expire(date("YmdHis", $expireTime));
  724. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  725. $input->SetTrade_type("JSAPI");
  726. //花卉宝代为申请的微信支付
  727. $merchantExtend = $this->sjExtend->attributes;
  728. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  729. $input->SetSub_openid($openId);
  730. } else {
  731. $input->SetOpenid($openId);
  732. }
  733. //小程序使用miniAppId
  734. if (httpUtil::isMiniProgram()) {
  735. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  736. }
  737. Yii::info('支付发起使用小程序信息' . json_encode($merchantExtend));
  738. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  739. $tools = new \JsApiPay();
  740. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  741. $newParams = json_decode($jsApiParameters, true);
  742. $newParams['orderId'] = $orderId;
  743. util::success($newParams);
  744. } elseif ($payWay == 1) {
  745. $extend = MerchantExtendService::getBySjId($sjId);
  746. if (isset($extend['alipayInit']) == false || $extend['alipayInit'] == 0) {
  747. util::fail('支付宝付款即将开通...');
  748. }
  749. $config = [
  750. //应用ID,您的APPID。
  751. 'app_id' => $extend['alipayPId'],
  752. //商户私钥,您的原始格式RSA私钥
  753. 'merchant_private_key' => $extend['alipayKey'],
  754. //异步通知地址
  755. 'notify_url' => Yii::$app->params['hdHost'] . "/notice/ali-callback",
  756. //同步跳转
  757. 'return_url' => "",
  758. //编码格式
  759. 'charset' => "UTF-8",
  760. //签名方式
  761. 'sign_type' => "RSA2",
  762. //支付宝网关
  763. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  764. //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
  765. 'alipay_public_key' => $extend['alipayPublicKey'],
  766. ];
  767. Yii::info(json_encode($config) . ' aplipay config');
  768. $alipayWap = Yii::getAlias("@vendor/alipayWap");
  769. require_once($alipayWap . '/wappay/service/AlipayTradeService.php');
  770. require_once($alipayWap . '/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php');
  771. $totalFee = $order['realPrice'];
  772. $out_trade_no = $orderSn;//商户订单号,商户网站订单系统中唯一订单号,必填
  773. $subject = '购买商品';//订单名称,必填
  774. $total_amount = $totalFee;//付款金额,必填
  775. $body = '';//商品描述,可空
  776. $timeout_express = "1m";//超时时间
  777. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  778. $passBackParams = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&sjId=' . $sjId;//将流水类型、优惠卷传过去
  779. $passBackParams = urlencode($passBackParams);
  780. $payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
  781. $payRequestBuilder->setBody($body);
  782. $payRequestBuilder->setSubject($subject);
  783. $payRequestBuilder->setOutTradeNo($out_trade_no);
  784. $payRequestBuilder->setTotalAmount($total_amount);
  785. $payRequestBuilder->setTimeExpress($timeout_express);
  786. //在异步通知时将该参数原样返回
  787. $payRequestBuilder->setPassbackParams($passBackParams);
  788. //同步通知
  789. $returnUrl = Yii::$app->params['mallDomain'] . "/#/pages/callback/success?account={$sjId}&shopId={$shopId}&orderSn={$orderSn}&totalPrice={$totalFee}&pageStatus=3&payDiscountPrice=0&payDiscountType=0";
  790. //异步通知
  791. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/ali-callback";
  792. $payResponse = new \AlipayTradeService($config);
  793. $result = $payResponse->wapPay($payRequestBuilder, $returnUrl, $notifyUrl);
  794. //直接将支付宝的html返回给前端
  795. echo $result;
  796. } elseif ($payWay == 2) {
  797. //余额支付,验证支付密码是否正确
  798. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  799. $user = UserService::getUserInfo($this->adminId);
  800. UserService::validPayPassword($payPassword, $user);
  801. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  802. $callbackParams = [];
  803. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  804. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  805. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'orderSn' => $orderSn, 'orderId' => $orderId, 'balance' => $balance]);
  806. } else {
  807. util::fail('无效的支付方式');
  808. }
  809. }
  810. //订单评价
  811. public function actionComment()
  812. {
  813. $post = Yii::$app->request->post();
  814. $id = $post['id'];
  815. $order = OrderService::getById($id);
  816. OrderService::valid($order, $this->mainId);
  817. if ($order['grade'] > 0) {
  818. util::fail('您已经评过了');
  819. }
  820. OrderService::comment($post);
  821. util::complete('提交成功');
  822. }
  823. //订单详情 ssh 2019.12.16
  824. public function actionDetail()
  825. {
  826. $id = Yii::$app->request->get('id', 0);
  827. $detail = OrderClass::getFullInfo($id);
  828. OrderService::valid($detail, $this->mainId);
  829. util::success($detail);
  830. }
  831. public function actionList()
  832. {
  833. $get = Yii::$app->request->get();
  834. $search = isset($get['search']) ? $get['search'] : '';
  835. $shopId = $this->shopId ?? 0;
  836. if (empty($shopId)) {
  837. //没有登录不显示数据
  838. util::success(['list' => [], 'moreData' => 0, 'shop' => [], 'totalNum' => 0, 'totalPage' => 0]);
  839. }
  840. $where = [];
  841. $where['shopId'] = $shopId;
  842. $status = $get['status'] ?? 0;
  843. if (!empty($status)) {
  844. $where['status'] = $get['status'];
  845. }
  846. $debt = $get['debt'] ?? 2;
  847. if ($debt != 2) {
  848. $where['debt'] = $debt;
  849. }
  850. if (isset($get['shopAdminId']) && !empty($get['shopAdminId'])) {
  851. $where['shopAdminId'] = $get['shopAdminId'];
  852. }
  853. if (isset($get['searchText']) && !empty($get['searchText'])) {
  854. if (is_numeric($get['searchText'])) {
  855. $where['actPrice'] = $get['searchText'];
  856. } else {
  857. $where['bookName'] = $get['searchText'];
  858. }
  859. }
  860. if (!empty($search)) {
  861. if (stringUtil::isMobile($search)) {
  862. $where['bookMobile'] = $search;
  863. } else {
  864. $where['orderSn'] = $search;
  865. }
  866. }
  867. $searchTime = $get['searchTime'] ?? '';
  868. if (!empty($searchTime)) {
  869. $startTime = $get['startTime'] ?? '';
  870. $endTime = $get['endTime'] ?? '';
  871. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  872. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  873. }
  874. $list = OrderService::getOrderList($where);
  875. $list['shop'] = $this->shop->attributes ?? [];
  876. util::success($list);
  877. }
  878. //订单归类 ssh 2019.12.17
  879. public function actionClassify()
  880. {
  881. $post = Yii::$app->request->post();
  882. $id = isset($post['id']) ? $post['id'] : 0;
  883. $order = OrderService::getById($id);
  884. OrderService::valid($order, $this->mainId);
  885. $categoryId = isset($post['categoryId']) ? $post['categoryId'] : $post['categoryId'];
  886. $usageId = isset($post['usageId']) ? $post['usageId'] : $post['usageId'];
  887. OrderService::classify($id, $categoryId, $usageId);
  888. util::complete();
  889. }
  890. //更新配送单 ssh 2019.12.17
  891. public function actionUpdateSheet()
  892. {
  893. $post = Yii::$app->request->post();
  894. $id = isset($post['id']) ? $post['id'] : 0;
  895. unset($post['id']);
  896. $order = OrderService::getById($id);
  897. OrderService::valid($order, $this->mainId);
  898. OrderService::updateSheet($order, $post);
  899. util::complete('提交成功');
  900. }
  901. //商家收款与发货 ssh 2019.12.18
  902. public function actionGathering()
  903. {
  904. $post = Yii::$app->request->post();
  905. $price = isset($post['price']) && is_numeric($post['price']) ? $post['price'] : 0;
  906. $payWay = isset($post['payWay']) && is_numeric($post['payWay']) ? $post['payWay'] : 0;
  907. $userId = isset($post['userId']) ? $post['userId'] : 0;
  908. $userInfo = UserService::getById($userId);
  909. if (empty($userInfo) || $userInfo['sjId'] != $this->sjId) {
  910. util::fail('您选择的客户无效');
  911. }
  912. if ($price <= 0) {
  913. util::fail('请输入金额');
  914. }
  915. $post['prePrice'] = $price;
  916. $post['actPrice'] = $price;
  917. $post['payWay'] = $payWay;
  918. $post['sourceType'] = 1;
  919. $post['userId'] = $userId;
  920. $post['goodsNum'] = 1;
  921. $post['orderName'] = '商品';
  922. $shopId = MerchantService::getDefaultShopId($this->sj);
  923. $post['shopId'] = $shopId;
  924. $order = OrderService::addOrder($post);
  925. $id = $order['id'];
  926. $orderSn = $order['orderSn'];
  927. //流水类型列表
  928. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  929. $callbackParams = [];
  930. switch ($payWay) {
  931. case 0:
  932. xhPayToolService::wxPay($callbackParams, $orderSn, $price, $capitalType, 0);
  933. break;
  934. case 1:
  935. xhPayToolService::alipay($callbackParams, $orderSn, $price, $capitalType, 0, []);
  936. break;
  937. case 2:
  938. xhPayToolService::balancePay($callbackParams, $orderSn, $price, $capitalType, 0);
  939. break;
  940. default:
  941. util::fail('无效支付方式');
  942. }
  943. util::success($order);
  944. }
  945. //免发货管理 ssh 2020.1.6
  946. public function actionWithoutSend()
  947. {
  948. $id = Yii::$app->request->get('id');
  949. $order = OrderService::getById($id);
  950. OrderService::valid($order, $this->mainId);
  951. //配送流程变更
  952. $currentFlow = OrderSendClass::withoutSend($order);
  953. util::success(['currentFlow' => $currentFlow]);
  954. }
  955. //修改价格 ssh 2020.1.6
  956. public function actionUpdatePrice()
  957. {
  958. $id = Yii::$app->request->get('id');
  959. $price = Yii::$app->request->get('price', 1);
  960. $order = OrderService::getById($id);
  961. OrderService::valid($order, $this->mainId);
  962. if (isset($order['modPrice']) && $order['modPrice'] == 0) {
  963. util::fail('已发起支付,不能修改价格');
  964. }
  965. OrderService::updateById($id, ['actPrice' => $price]);
  966. util::complete('修改成功');
  967. }
  968. //配送单 ssh 2020.2.29
  969. public function actionGetDeliverDetail()
  970. {
  971. $id = Yii::$app->request->get('id', 0);
  972. $order = OrderService::getById($id);
  973. OrderService::valid($order, $this->mainId);
  974. $reachDate = isset($order['reachDate']) && !empty($order['reachDate']) ? $order['reachDate'] : '';
  975. $reachPeriodId = $order['reachPeriod'];
  976. $reachPeriodArr = [0 => '上午', 1 => '下午', 2 => '晚上'];
  977. $reachPeriod = isset($reachPeriodArr[$reachPeriodId]) ? $reachPeriodArr[$reachPeriodId] : '';
  978. $bookName = isset($order['bookName']) ? $order['bookName'] : '';
  979. $bookMobile = isset($order['bookMobile']) ? $order['bookMobile'] : '';
  980. if (isset($order['anonymity']) && $order['anonymity'] == 1) {
  981. $bookName = '--';
  982. $bookMobile = '--';
  983. }
  984. $data = [
  985. 'reachDate' => $reachDate . ' ' . $reachPeriod,
  986. 'orderSn' => $order['orderSn'],
  987. 'receiveUserName' => $order['receiveUserName'],
  988. 'receiveMobile' => $order['receiveMobile'],
  989. 'fullAddress' => $order['fullAddress'] . "(" . $order['showAddress'] . ")",
  990. 'cardInfo' => $order['cardInfo'],
  991. 'bookMobile' => $bookMobile,
  992. 'bookName' => $bookName,
  993. 'remark' => $order['remark'],
  994. 'sendNum' => $order['sendNum'],
  995. 'telephone' => 18030142050,
  996. 'printTime' => '',
  997. 'img' => '',
  998. ];
  999. util::success($data);
  1000. }
  1001. //运费计算 lqh 2021.4.12 暂反回固定
  1002. public function actionFreight()
  1003. {
  1004. util::success(['sedCost' => 10]);
  1005. }
  1006. }