OrderController.php 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\shop\classes\ShopExtClass;
  4. use bizGhs\express\services\DadaExpressServices;
  5. use bizHd\purchase\classes\PurchaseClass;
  6. use bizHd\purchase\services\PurchaseService;
  7. use bizHd\wx\classes\WxOpenClass;
  8. use bizGhs\custom\classes\CustomClass;
  9. use bizGhs\order\classes\OrderClass;
  10. use bizGhs\order\services\OrderService;
  11. use bizHd\saas\services\RegionService;
  12. use bizGhs\product\classes\ProductClass;
  13. use common\components\baiduAip;
  14. use common\components\dict;
  15. use common\components\dateUtil;
  16. use common\components\freight;
  17. use common\components\httpUtil;
  18. use common\components\noticeUtil;
  19. use common\components\oss;
  20. use common\components\payUtil;
  21. use common\components\qrCodeUtil;
  22. use Yii;
  23. use common\components\util;
  24. use biz\shop\classes\ShopAdminClass;
  25. use biz\notice\classes\NoticeClass;
  26. use biz\wx\classes\WxMessageClass;
  27. use biz\shop\classes\ShopClass;
  28. use common\components\lakala\Lakala;
  29. class OrderController extends BaseController
  30. {
  31. public $guestAccess = ['create-order', 'order-relate', 'fast-pay', 'info'];
  32. public function actionAllFh()
  33. {
  34. $post = Yii::$app->request->post();
  35. $ids = $post['ids'] ?? '';
  36. if (empty($ids)) {
  37. util::fail('请选择要合并发货的订单');
  38. }
  39. $arr = explode(',', $ids);
  40. if (empty($arr)) {
  41. util::fail('请选择要合并发货的订单哦');
  42. }
  43. $fhWl = $post['fhWl'] ?? '';
  44. $fhWlNo = $post['fhWlNo'] ?? '';
  45. $fhType = 0;
  46. $params = [
  47. 'fhType' => $fhType,
  48. 'fhWl' => $fhWl,
  49. 'fhWlNo' => $fhWlNo,
  50. ];
  51. $connection = Yii::$app->db;
  52. $transaction = $connection->beginTransaction();
  53. try {
  54. $all = OrderClass::getAllByCondition(['id' => ['in', $arr]], null, '*', null, true);
  55. foreach ($all as $order) {
  56. if ($order->mainId != $this->mainId) {
  57. util::fail('不是你的订单哦');
  58. }
  59. OrderClass::orderSend($order, $params, true);
  60. }
  61. $transaction->commit();
  62. util::complete();
  63. } catch (\Exception $e) {
  64. $transaction->rollBack();
  65. Yii::error("操作失败原因:" . $e->getMessage());
  66. util::fail('操作失败');
  67. }
  68. }
  69. //发货 ssh 20230613
  70. public function actionFh()
  71. {
  72. $post = Yii::$app->request->post();
  73. $id = $post['id'] ?? '';
  74. $order = OrderClass::getById($id, true);
  75. OrderClass::valid($order, $this->shopId);
  76. $fhWl = $post['fhWl'] ?? '';
  77. $fhWlNo = $post['fhWlNo'] ?? '';
  78. $fhType = $post['fhType'] ?? '';
  79. $params = [
  80. 'fhType' => $fhType,
  81. 'fhWl' => $fhWl,
  82. 'fhWlNo' => $fhWlNo,
  83. ];
  84. $connection = Yii::$app->db;
  85. $transaction = $connection->beginTransaction();
  86. try {
  87. OrderClass::orderSend($order, $params);
  88. $transaction->commit();
  89. //小程序通知发货
  90. \bizHd\notice\classes\NoticeClass::orderFhMiniNotice($order);
  91. util::complete();
  92. } catch (\Exception $e) {
  93. $transaction->rollBack();
  94. Yii::error("操作失败原因:" . $e->getMessage());
  95. util::fail('操作失败');
  96. }
  97. }
  98. //合并打印 ssh 20230929
  99. public function actionMergePrint()
  100. {
  101. $post = Yii::$app->request->post();
  102. $ids = $post['ids'] ?? '';
  103. if (empty($ids)) {
  104. util::fail('请选择要合并打印的订单');
  105. }
  106. $arr = explode(',', $ids);
  107. if (empty($arr)) {
  108. util::fail('请选择要合并打印的订单哦');
  109. }
  110. if (getenv('YII_ENV') == 'production') {
  111. if ($this->mainId != 10536 && $this->mainId != 50) {
  112. //util::fail('功能开发中');
  113. }
  114. } else {
  115. if ($this->mainId != 11) {
  116. //util::fail('功能开发中');
  117. }
  118. }
  119. OrderClass::mergePrintOrder($arr, $this->mainId);
  120. util::complete('打印成功');
  121. }
  122. //修改分工 ssh 20221129
  123. public function actionChangeWorker()
  124. {
  125. $post = Yii::$app->request->post();
  126. $id = $post['id'] ?? 0;
  127. $shopAdminId = $post['shopAdminId'] ?? 0;
  128. $shopAdminName = $post['shopAdminName'] ?? '';
  129. $order = OrderClass::getById($id, true);
  130. OrderClass::valid($order, $this->shopId);
  131. $preShopAdminId = $order->shopAdminId ?? 0;
  132. if (!empty($preShopAdminId)) {
  133. $shopAdmin = $this->shopAdmin;
  134. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  135. util::fail('管理员才能操作');
  136. }
  137. }
  138. $order->shopAdminId = $shopAdminId;
  139. $order->shopAdminName = $shopAdminName;
  140. $order->save();
  141. util::complete();
  142. }
  143. //预订单到货 ssh 20220321
  144. public function actionArrival()
  145. {
  146. $post = Yii::$app->request->post();
  147. $id = $post['id'] ?? 0;
  148. $connection = Yii::$app->db;
  149. $transaction = $connection->beginTransaction();
  150. try {
  151. $order = OrderClass::getById($id, true);
  152. OrderClass::valid($order, $this->shopId);
  153. $post['shopAdminId'] = $this->shopAdminId;
  154. $post['shopAdminName'] = $this->shopAdmin ? $this->shopAdmin->name : '';
  155. OrderService::arrival($order, $post);
  156. $transaction->commit();
  157. util::complete();
  158. } catch (\Exception $e) {
  159. $transaction->rollBack();
  160. Yii::error("操作失败原因:" . $e->getMessage());
  161. util::fail('操作失败');
  162. }
  163. }
  164. //到货获取订单详情 ssh 20220323
  165. public function actionArrivalDetail()
  166. {
  167. $get = Yii::$app->request->get();
  168. $id = $get['id'] ?? 0;
  169. $info = OrderClass::getById($id);
  170. OrderClass::valid($info, $this->shopId);
  171. $showItemData = OrderClass::getOriginalShowItem(true, $info);
  172. $itemList = $showItemData['list'] ?? [];
  173. $info['product'] = $itemList;
  174. $kiloFee = $this->shop->kiloFee ?? 0;
  175. $miniKilo = $this->shop->miniKilo ?? 0;
  176. $info['kiloFee'] = $kiloFee;
  177. $info['miniKilo'] = $miniKilo;
  178. util::success($info);
  179. }
  180. //支付宝扫码支付 ssh 20210103
  181. public function actionScanPayCheck()
  182. {
  183. $get = Yii::$app->request->get();
  184. $id = isset($get['id']) ? $get['id'] : 0;
  185. $order = OrderClass::getById($id, true);
  186. if ($order->status != OrderClass::ORDER_STATUS_UN_PAY && $order->status != OrderClass::ORDER_STATUS_CANCEL) {
  187. util::success(['returnStatus' => 'SUCCESS']);
  188. }
  189. $deadline = $order->deadline ?? '';
  190. $deadTime = strtotime($deadline);
  191. $current = time();
  192. $diff = bcsub($deadTime, $current);
  193. if ($diff <= 60) {
  194. util::success(['returnStatus' => 'EXPIRE']);
  195. }
  196. util::success(['returnStatus' => 'FAILURE']);
  197. }
  198. //微信和支付宝扫码支付 ssh 20210103
  199. public function actionScanPay()
  200. {
  201. ini_set('date.timezone', 'Asia/Shanghai');
  202. header("Content-type: text/html; charset=utf-8");
  203. $get = Yii::$app->request->get();
  204. $id = isset($get['id']) ? $get['id'] : 0;
  205. $order = OrderClass::getById($id, true);
  206. if (isset($order->status) == false || $order->status != 1) {
  207. util::success(['returnStatus' => 'FAILURE'], '不是待付款订单');
  208. }
  209. OrderClass::valid($order, $this->shopId);
  210. $cgId = $order->purchaseId ?? 0;
  211. $cg = PurchaseClass::getById($cgId, true);
  212. $orderSn = $cg->orderSn ?? '';
  213. if (empty($orderSn)) {
  214. util::success(['returnStatus' => 'FAILURE'], '没有找到采购单');
  215. }
  216. $deadline = $cg->deadline;
  217. $current = date("Y-m-d H:i:s");
  218. if ((strtotime($deadline) + 20) < strtotime($current)) {
  219. util::success(['returnStatus' => 'FAILURE'], '订单已经失效');
  220. }
  221. $totalFee = $cg->actPrice ?? 0;
  222. $subject = '购买鲜花';
  223. $totalAmount = $totalFee;
  224. $body = "到店订单";
  225. //标记为扫码支付 和 自取订单
  226. $order->codePay = 2;
  227. $order->sendType = OrderClass::SEND_TYPE_NO;
  228. $order->save();
  229. $cg->getType = PurchaseClass::GET_TYPE_SELF_GET;
  230. $cg->codePay = 2;
  231. $cg->save();
  232. $shop = $this->shop;
  233. $currentCapitalType = dict::getDict('capitalType', 'xhPurchase', 'id');
  234. $wxPayWay = dict::getDict('payWay', 'wxPay');
  235. $aliPayWay = dict::getDict('payWay', 'alipay');
  236. $payWay = Yii::$app->request->get('payWay', $wxPayWay);
  237. if ($payWay == $wxPayWay) {
  238. if (isset($order->wxPayUrl) && !empty($order->wxPayUrl)) {
  239. $url = $order->wxPayUrl;
  240. } else {
  241. util::fail('暂不支持');
  242. $merchantExtend = WxOpenClass::getWxInfo();
  243. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  244. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  245. $params = [
  246. 'appid' => 'OP00002119',
  247. 'serial_no' => '018b08cfddbd',
  248. 'merchant_no' => $shop->lklSjNo,
  249. 'term_no' => $shop->lklScanTermNo,
  250. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  251. 'lklCertificatePath' => $lklCertificatePath,
  252. ];
  253. $laResource = new Lakala($params);
  254. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/pay-callback";
  255. $wxParams = [
  256. 'orderSn' => $orderSn,
  257. 'amount' => $totalFee,
  258. 'capitalType' => $currentCapitalType,
  259. 'notifyUrl' => $notifyUrl,
  260. 'subject' => $subject,
  261. 'couponId' => 0,
  262. 'wxAppId' => $merchantExtend['miniAppId'],
  263. ];
  264. $response = $laResource->driveNativeWxPay($wxParams);
  265. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  266. $msg = $response['msg'] ?? '';
  267. noticeUtil::push($msg, '15280215347');
  268. util::fail('二维码生成失败');
  269. }
  270. $url = isset($response['resp_data']['acc_resp_fields']['code']) ? $response['resp_data']['acc_resp_fields']['code'] : '';
  271. if (empty($url)) {
  272. util::fail('二维码生成失败..');
  273. }
  274. $order->wxPayUrl = $url;
  275. $order->save();
  276. }
  277. $suffixUrl = qrCodeUtil::generateShortTimeGatheringQrCode($url, $this->sjId, $this->shopId, $payWay);
  278. $payUrl = Yii::$app->params['ghsImgHost'] . $suffixUrl;
  279. util::success(['url' => $payUrl, 'returnStatus' => 'SUCCESS']);
  280. }
  281. if ($payWay == $aliPayWay) {
  282. if (isset($order->aliPayUrl) && !empty($order->aliPayUrl)) {
  283. $url = $order->aliPayUrl;
  284. } else {
  285. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  286. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  287. $params = [
  288. 'appid' => 'OP00002119',
  289. 'serial_no' => '018b08cfddbd',
  290. 'merchant_no' => $shop->lklSjNo,
  291. 'term_no' => $shop->lklScanTermNo,
  292. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  293. 'lklCertificatePath' => $lklCertificatePath,
  294. ];
  295. $laResource = new Lakala($params);
  296. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/pay-callback";
  297. $aliParams = [
  298. 'orderSn' => $orderSn,
  299. 'amount' => $totalFee,
  300. 'capitalType' => $currentCapitalType,
  301. 'notifyUrl' => $notifyUrl,
  302. 'subject' => $subject,
  303. ];
  304. $response = $laResource->driveAliPay($aliParams);
  305. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  306. util::fail('二维码生成失败');
  307. }
  308. $url = isset($response['resp_data']['acc_resp_fields']['code']) ? $response['resp_data']['acc_resp_fields']['code'] : '';
  309. if (empty($url)) {
  310. util::fail('二维码生成失败..');
  311. }
  312. $order->aliPayUrl = $url;
  313. $order->save();
  314. }
  315. $suffixUrl = qrCodeUtil::generateShortTimeGatheringQrCode($url, $this->sjId, $this->shopId, $payWay);
  316. $payUrl = Yii::$app->params['ghsImgHost'] . $suffixUrl;
  317. util::success(['url' => $payUrl, 'returnStatus' => 'SUCCESS']);
  318. }
  319. util::success(['returnStatus' => 'FAILURE'], '不支持的收款方式');
  320. }
  321. //微信和支付宝付款码支付复查 ssh 20211231
  322. public function actionCodePayCheck()
  323. {
  324. ini_set('date.timezone', 'Asia/Shanghai');
  325. header("Content-type: text/html; charset=utf-8");
  326. $get = Yii::$app->request->get();
  327. $id = isset($get['id']) ? $get['id'] : 0;
  328. $order = OrderClass::getById($id, true);
  329. if (empty($order)) {
  330. util::success(['returnStatus' => 'SUCCESS']);
  331. }
  332. if ($order->status == OrderClass::ORDER_STATUS_COMPLETE) {
  333. util::complete('订单已付款');
  334. }
  335. if ($order->status != OrderClass::ORDER_STATUS_UN_PAY) {
  336. util::complete('订单不是待付款状态');
  337. }
  338. OrderClass::valid($order, $this->shopId);
  339. $cgId = $order->purchaseId ?? 0;
  340. $cg = PurchaseClass::getById($cgId, true);
  341. $orderSn = $cg->orderSn ?? '';
  342. $totalFee = $cg->actPrice ?? 0;
  343. if (empty($orderSn)) {
  344. util::complete('没有找到采购单');
  345. }
  346. $connection = Yii::$app->db;
  347. $transaction = $connection->beginTransaction();
  348. try {
  349. $capitalType = dict::getDict('capitalType', 'xhPurchase', 'id');
  350. $shop = $this->shop;
  351. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  352. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  353. $params = [
  354. 'appid' => 'OP00002119',
  355. 'serial_no' => '018b08cfddbd',
  356. 'merchant_no' => $shop->lklSjNo,
  357. 'term_no' => $shop->lklScanTermNo,
  358. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  359. 'lklCertificatePath' => $lklCertificatePath,
  360. ];
  361. $laResource = new Lakala($params);
  362. $scanParams = ['orderSn' => $orderSn,];
  363. $response = $laResource->query($scanParams);
  364. if (isset($response['code']) && $response['code'] == 'BBS00000') {
  365. if (isset($response['resp_data']['trade_state']) && $response['resp_data']['trade_state'] == 'SUCCESS') {
  366. //支付成功
  367. $transactionId = $response['resp_data']['trade_no'] ?? '';
  368. $openId = '';
  369. $aliUserId = '';
  370. $cg->onlinePay = dict::getDict('onlinePay', 'yes');
  371. $cg->codePay = 1;
  372. $cg->payOpenId = $openId;
  373. $cg->aliUserId = $aliUserId;
  374. //自取订单
  375. $cg->getType = PurchaseClass::GET_TYPE_SELF_GET;
  376. $cg->save();
  377. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  378. $order->codePay = 1;
  379. //自取订单
  380. $order->sendType = OrderClass::SEND_TYPE_NO;
  381. $order->save();
  382. $payWayType = dict::getDict('payWay', 'wxPay');
  383. $account_type = $response['resp_data']['account_type'] ?? '';
  384. if ($account_type == 'WECHAT') {
  385. $payWayType = dict::getDict('payWay', 'wxPay');
  386. }
  387. if ($account_type == 'ALIPAY') {
  388. $payWayType = dict::getDict('payWay', 'alipay');
  389. }
  390. $order->payWay = $payWayType;
  391. $order->save();
  392. $cg->payWay = $payWayType;
  393. $cg->save();
  394. $attach = '';
  395. payUtil::thirdPay($payWayType, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  396. $transaction->commit();
  397. $saleId = $order->id ?? 0;
  398. $order = OrderClass::getById($saleId, true);
  399. if (!empty($order)) {
  400. //订单生成时唤起在线打印
  401. if (isset($order->needPrint) && $order->needPrint == dict::getDict('needPrint', 'need')) {
  402. if ($order->status != 1 && $order->status != 5) {
  403. OrderClass::onlinePrint($order);
  404. $order->printNum += 1;
  405. $order->save();
  406. }
  407. }
  408. ShopExtClass::ghsGatheringReport($order);
  409. }
  410. util::success(['returnStatus' => 'SUCCESS']);
  411. } else {
  412. util::complete('未知状态..');
  413. }
  414. } else {
  415. util::complete('未知状态....');
  416. }
  417. } catch (\Exception $e) {
  418. $transaction->rollBack();
  419. noticeUtil::push('查询订单状态失败:' . $e->getMessage(), '15280215347');
  420. util::fail('支付失败');
  421. }
  422. }
  423. //微信和支付宝付款码支付 ssh 2021.4.11
  424. public function actionCodePay()
  425. {
  426. ini_set('date.timezone', 'Asia/Shanghai');
  427. header("Content-type: text/html; charset=utf-8");
  428. $get = Yii::$app->request->get();
  429. $id = isset($get['id']) ? $get['id'] : 0;
  430. $order = OrderClass::getById($id, true);
  431. if (isset($order->status) == false || $order->status != 1) {
  432. util::fail('不是待付款订单');
  433. }
  434. OrderClass::valid($order, $this->shopId);
  435. $cgId = $order->purchaseId ?? 0;
  436. $cg = PurchaseClass::getById($cgId, true);
  437. $orderSn = $cg->orderSn ?? '';
  438. if (empty($orderSn)) {
  439. util::fail('没有找到采购单');
  440. }
  441. //付款码
  442. $authCode = (string)$get['authCode'] ?? '';
  443. if (empty($authCode)) {
  444. util::fail('支付失败');
  445. }
  446. $deadline = $cg->deadline;
  447. $current = date("Y-m-d H:i:s");
  448. if ((strtotime($deadline) + 20) < strtotime($current)) {
  449. util::fail('订单已经失效');
  450. }
  451. $totalFee = $cg->actPrice ?? 0;
  452. $connection = Yii::$app->db;
  453. $transaction = $connection->beginTransaction();
  454. try {
  455. $subject = '购买花材';
  456. $capitalType = dict::getDict('capitalType', 'xhPurchase', 'id');
  457. $shop = $this->shop;
  458. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  459. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  460. $params = [
  461. 'appid' => 'OP00002119',
  462. 'serial_no' => '018b08cfddbd',
  463. 'merchant_no' => $shop->lklSjNo,
  464. 'term_no' => $shop->lklScanTermNo,
  465. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  466. 'lklCertificatePath' => $lklCertificatePath,
  467. ];
  468. $laResource = new Lakala($params);
  469. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/pay-callback";
  470. $scanParams = [
  471. 'orderSn' => $orderSn,
  472. 'amount' => $totalFee,
  473. 'capitalType' => $capitalType,
  474. 'notifyUrl' => $notifyUrl,
  475. 'subject' => $subject,
  476. 'authCode' => $authCode,
  477. ];
  478. $response = $laResource->scanPay($scanParams);
  479. if (isset($response['code']) && $response['code'] == 'BBS00000') {
  480. $account_type = $response['resp_data']['account_type'] ?? '';
  481. $payWayType = dict::getDict('payWay', 'wxPay');
  482. if ($account_type == 'WECHAT') {
  483. $payWayType = dict::getDict('payWay', 'wxPay');
  484. }
  485. if ($account_type == 'ALIPAY') {
  486. $payWayType = dict::getDict('payWay', 'alipay');
  487. }
  488. $order->payWay = $payWayType;
  489. $order->save();
  490. $cg->payWay = $payWayType;
  491. $cg->save();
  492. $transactionId = $response['resp_data']['trade_no'] ?? '';
  493. $openId = '';
  494. $aliUserId = '';
  495. $cg->onlinePay = dict::getDict('onlinePay', 'yes');
  496. $cg->codePay = 1;
  497. $cg->payOpenId = $openId;
  498. $cg->aliUserId = $aliUserId;
  499. //自取订单
  500. $cg->getType = PurchaseClass::GET_TYPE_SELF_GET;
  501. $cg->save();
  502. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  503. $order->codePay = 1;
  504. //自取订单
  505. $order->sendType = OrderClass::SEND_TYPE_NO;
  506. $order->save();
  507. $attach = '';
  508. payUtil::thirdPay($payWayType, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  509. $transaction->commit();
  510. $saleId = $order->id ?? 0;
  511. $order = OrderClass::getById($saleId, true);
  512. if (!empty($order)) {
  513. //订单生成时唤起在线打印
  514. if (isset($order->needPrint) && $order->needPrint == dict::getDict('needPrint', 'need')) {
  515. if ($order->status != 1 && $order->status != 5) {
  516. OrderClass::onlinePrint($order);
  517. $order->printNum += 1;
  518. $order->save();
  519. }
  520. }
  521. ShopExtClass::ghsGatheringReport($order);
  522. }
  523. util::success(['returnStatus' => 'SUCCESS']);
  524. } else {
  525. $msg = $response['msg'] ?? '';
  526. util::success(['returnStatus' => 'FAILURE'], $msg);
  527. }
  528. } catch (\Exception $e) {
  529. $transaction->rollBack();
  530. Yii::error("支付失败原因:" . $e->getMessage());
  531. util::fail('支付失败');
  532. }
  533. }
  534. //订单列表 ssh 2021.1.20
  535. public function actionList()
  536. {
  537. $get = Yii::$app->request->get();
  538. $status = $get['status'] ?? 0;
  539. if (!empty($status)) {
  540. $where['status'] = $status;
  541. }
  542. $where['sjId'] = $this->sjId;
  543. if (isset($get['shopId'])) {
  544. $shopId = $get['shopId'] ?? 0;
  545. if (!empty($shopId)) {
  546. $where['shopId'] = $this->shopId;
  547. }
  548. } else {
  549. $where['shopId'] = $this->shopId;
  550. }
  551. $searchTime = $get['searchTime'] ?? '';
  552. if (!empty($searchTime)) {
  553. $startTime = $get['startTime'] ?? '';
  554. $endTime = $get['endTime'] ?? '';
  555. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  556. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  557. }
  558. $name = $get['name'] ?? '';
  559. if (!empty($name)) {
  560. $searchType = $get['searchType'] ?? 0;
  561. if (empty($searchType)) {
  562. //订单编号
  563. if (strpos($name, 'XSD') === 0) {
  564. $where['orderSn'] = $name;
  565. } elseif (preg_match("/^[a-zA-Z\s]+$/", $name)) {
  566. $where['customNamePy'] = ['like', $name];
  567. } else {
  568. $where['customName'] = ['like', $name];
  569. }
  570. } elseif ($searchType == 1) {
  571. $where['sendNum'] = $name;
  572. } else {
  573. }
  574. }
  575. if (isset($get['customId']) && !empty($get['customId'])) {
  576. $where['customId'] = $get['customId'];
  577. }
  578. if (isset($get['ids']) && !empty($get['ids'])) {
  579. $stringIds = $get['ids'];
  580. $newIds = explode(',', $stringIds);
  581. $where['id'] = ['in', $newIds];
  582. }
  583. $respond = OrderClass::getOrderList($where);
  584. $respond['shop'] = $this->shop;
  585. $respond['shopExt'] = $this->shopExt;
  586. util::success($respond);
  587. }
  588. // 新订单列表详情 -- 可能与 actionList 很相似,但为不影响 actionList,重新创建个方法 shizhongqi 2022.06.23
  589. public function actionNewOrderList()
  590. {
  591. $get = Yii::$app->request->get();
  592. $where['shopId'] = $this->shopId;
  593. /*$status = $get['status'] ?? 0;
  594. if (!empty($status)) {
  595. $where['status'] = $status;
  596. }*/
  597. // 按类别生成查询条件:1.今日实收 2.今日欠款 3.总欠款(所有历史欠款)
  598. if (isset($get['type'])) {
  599. $type = $get['type'];
  600. if ($type == 1) { // 今日实收
  601. $get['searchTime'] = 'today';
  602. $where['debt'] = 0;
  603. $where['debtPrice'] = '0.00';
  604. $where['status'] = ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]];
  605. } elseif ($type == 2) { // 今日欠款
  606. $get['searchTime'] = 'today';
  607. $where['debt'] = 1;
  608. } elseif ($type == 3) { // 总欠款
  609. $where['debt'] = 1;
  610. } else {
  611. util::fail('查询的订单类别不存在');
  612. }
  613. }
  614. $searchTime = $get['searchTime'] ?? '';
  615. if (!empty($searchTime)) {
  616. $startTime = '';
  617. $endTime = '';
  618. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  619. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  620. }
  621. $name = $get['name'] ?? '';
  622. if (!empty($name)) {
  623. $searchType = $get['searchType'] ?? 0;
  624. if (empty($searchType)) {
  625. //订单编号
  626. if (strpos($name, 'XSD') === 0) {
  627. $where['orderSn'] = $name;
  628. } elseif (preg_match("/^[a-zA-Z\s]+$/", $name)) {
  629. $where['customNamePy'] = ['like', $name];
  630. } else {
  631. $where['customName'] = ['like', $name];
  632. }
  633. } elseif ($searchType == 1) {
  634. $where['sendNum'] = $name;
  635. } else {
  636. util::fail('搜索的订单类别不存在');
  637. }
  638. }
  639. $respond = OrderClass::getOrderList($where);
  640. $respond['shop'] = $this->shop;
  641. $respond['shopExt'] = $this->shopExt;
  642. $respond['type'] = $type; //把查询类别返回
  643. util::success($respond);
  644. }
  645. //商城下单操作 ssh 2021.1.14
  646. public function actionCreateOrder()
  647. {
  648. $post = Yii::$app->request->post();
  649. $shopId = $this->shopId;
  650. $customId = $post['customId'] ?? 0;
  651. $post['customId'] = $customId;
  652. //开单必须选客户
  653. if (empty($customId)) {
  654. util::fail('请选择客户');
  655. }
  656. $custom = CustomClass::getCustom($customId);
  657. if (empty($custom)) {
  658. util::fail('请选客户哦');
  659. }
  660. CustomClass::valid($custom, $this->shopId);
  661. if (isset($custom['mainId']) && !empty($custom['mainId']) && $custom['mainId'] == $custom['ownMainId']) {
  662. util::fail('不能给自己的零售店开单');
  663. }
  664. $post['ghsId'] = $custom['ghsId'] ?? 0;
  665. $post['customMobile'] = $custom['mobile'] ?? '';
  666. $customName = $custom['name'] ?? '';
  667. $post['customName'] = $customName;
  668. $post['customNamePy'] = $custom['py'] ?? '';
  669. $post['customAvatar'] = $custom['shortSmallAvatar'] ?? '';
  670. $post['province'] = $custom['province'] ?? '';
  671. $post['city'] = $custom['city'] ?? '';
  672. $post['dist'] = $custom['dist'] ?? '';
  673. $post['address'] = $custom['address'] ?? '';
  674. $post['floor'] = $custom['floor'] ?? '';
  675. $post['fullAddress'] = $custom['fullAddress'] ?? '';
  676. $post['showAddress'] = $custom['showAddress'] ?? '';
  677. $post['long'] = $custom['long'] ?? '';
  678. $post['lat'] = $custom['lat'] ?? '';
  679. $shopAdmin = $this->shopAdmin;
  680. $shopAdminId = $this->shopAdminId;
  681. $shopAdminName = $shopAdmin->name ?? '';
  682. if (isset($post['getStaffId']) == true && !empty($post['getStaffId'])) {
  683. $getStaffName = $post['getStaffName'] ?? '';
  684. $shopAdminId = $post['getStaffId'];
  685. $shopAdminName = $getStaffName;
  686. }
  687. //开单人和收款人必定是同个人
  688. $post['shopAdminId'] = $shopAdminId;
  689. $post['shopAdminName'] = $shopAdminName;
  690. $post['getStaffId'] = $shopAdminId;
  691. $post['getStaffName'] = $shopAdminName;
  692. $post['sjId'] = $this->sjId;
  693. $post['shopId'] = $shopId;
  694. $post['mainId'] = $this->mainId;
  695. $post['fromType'] = 1;
  696. $post['expressId'] = $post['expressId'] ?? 0;
  697. $book = $post['book'] ?? 0;
  698. $post['book'] = $book;
  699. $sendType = $post['sendType'] ?? 0;
  700. $transType = $post['transType'] ?? 0;
  701. $shop = $this->shop;
  702. if (isset($shop->pfLevel) && $shop->pfLevel == 1) {
  703. //昆明发往全国
  704. $sendCost = isset($post['sendCost']) && $post['sendCost'] > 0 ? $post['sendCost'] : 0;
  705. $post['sendCost'] = $sendCost;
  706. $packCost = isset($post['packCost']) && $post['packCost'] > 0 ? $post['packCost'] : 0;
  707. $post['packCost'] = $packCost;
  708. $post['transType'] = $transType;
  709. $post['localOrder'] = 1;
  710. } else {
  711. //同步配送
  712. $sendCost = isset($post['sendCost']) && $post['sendCost'] > 0 ? $post['sendCost'] : 0;
  713. if ($sendType != dict::getDict('sendType', 'thirdSend') && $sendCost > 0) {
  714. util::fail('填了运费,请选择代叫跑腿');
  715. }
  716. $post['sendCost'] = $sendCost;
  717. $post['packCost'] = isset($post['packCost']) && $post['packCost'] > 0 ? $post['packCost'] : 0;
  718. }
  719. $hasPay = $post['hasPay'] ?? dict::getDict('hasPay', 'unPay');
  720. $post['debt'] = OrderClass::DEBT_NO;
  721. if ($hasPay == dict::getDict('hasPay', 'debt')) {
  722. $post['debt'] = OrderClass::DEBT_YES;
  723. }
  724. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  725. if ($hasPay == dict::getDict('hasPay', 'payed')) {
  726. //util::fail('没有权限');
  727. }
  728. }
  729. //多颜色数据整理
  730. $colorItem = $post['xj'] ?? [];
  731. $newXj = [];
  732. if (!empty($colorItem)) {
  733. $colorData = json_decode($colorItem, true);
  734. if (!empty($colorData) && is_array($colorData)) {
  735. foreach ($colorData as $colorInfo) {
  736. $ptItemId = $colorInfo['ptItemId'] ?? 0;
  737. $colorList = $colorInfo['list'] ?? [];
  738. if (!empty($colorList)) {
  739. foreach ($colorList as $colorItemKey => $colorItem) {
  740. $newXj[$ptItemId][] = $colorItem;
  741. }
  742. }
  743. }
  744. }
  745. }
  746. $post['xj'] = $newXj;
  747. $productJson = $post['product'] ?? '';
  748. if (empty($productJson)) {
  749. util::fail('请选择花材');
  750. }
  751. $productList = json_decode($productJson, true);
  752. if (empty($productList)) {
  753. util::fail('请选择花材');
  754. }
  755. $dealPrice = $post['dealPrice'] ?? 0;
  756. $ids = array_unique(array_filter(array_column($productList, 'productId')));
  757. $itemInfo = ProductClass::getByIds($ids, null, 'id');
  758. if (empty($itemInfo)) {
  759. util::fail('请选择花材哦');
  760. }
  761. //判断花材有效性
  762. foreach ($itemInfo as $itemData) {
  763. if (isset($itemData['mainId']) == false || $itemData['mainId'] != $this->mainId) {
  764. util::fail('只能选择同一家的商品哈');
  765. }
  766. }
  767. //开单提交订单时,别人修改价格,暂成提交价格不是真实卖价问题解决!!
  768. if ($dealPrice == 0) {
  769. $diff = [];
  770. $priceMap = CustomClass::$levelPriceKeyMap;
  771. $addPriceMap = CustomClass::$levelAddPriceKeyMap;
  772. $level = $custom['level'] ?? 1;
  773. foreach ($productList as $currentKey => $currentProduct) {
  774. $currentPrice = $currentProduct['price'] ?? 0;
  775. $currentId = $currentProduct['productId'] ?? 0;
  776. $systemInfo = $itemInfo[$currentId] ?? [];
  777. $name = $systemInfo['name'] ?? '';
  778. $systemPrice = ProductClass::getFinalPrice($systemInfo, $level, $priceMap, $addPriceMap);
  779. if (floatval($currentPrice) != floatval($systemPrice)) {
  780. $diff[] = [
  781. 'name' => $name,
  782. 'price' => $systemPrice,
  783. 'kdPrice' => $currentPrice,
  784. ];
  785. }
  786. }
  787. if (!empty($diff)) {
  788. util::success(['diff' => $diff, 'respondType' => 'priceError']);
  789. }
  790. }
  791. if ($book == 1) {
  792. foreach ($productList as $currentKey => $currentProduct) {
  793. //帮开预订单没有价格
  794. if ($book == 1) {
  795. $productList[$currentKey]['price'] = 0;
  796. }
  797. }
  798. $post['hasPay'] = 1;
  799. $post['payWay'] = 0;
  800. }
  801. //4秒内不允许重复开单
  802. $staffId = $this->shopAdminId ?? 0;
  803. $cacheKey = 'ghs_create_order_' . $staffId . '_' . $customId;
  804. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  805. if (!empty($has)) {
  806. util::fail('请稍等');
  807. }
  808. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 4, 'has']);
  809. $connection = Yii::$app->db;//事务处理
  810. $transaction = $connection->beginTransaction();
  811. try {
  812. $post['product'] = $productList;
  813. //商家开单到时间会自动标记为欠款,所以360天内不过期,以保证足够时间标记欠款,具体参数查看dict local_gys_kd_auto_set_debt_time
  814. $time = time();
  815. $hasTime = dict::getDict('order_pay_has_time');
  816. $totalTime = bcadd($time, $hasTime);
  817. $post['deadline'] = date("Y-m-d H:i:s", $totalTime);
  818. $return = OrderService::createFinishOrder($post, $custom, $hasPay);
  819. $transaction->commit();
  820. $saleId = $return->id ?? 0;
  821. $order = OrderClass::getById($saleId, true);
  822. if (!empty($order)) {
  823. $shopId = $order->shopId ?? 0;
  824. $shop = ShopClass::getById($shopId, true);
  825. if (isset($order->payStatus) && $order->payStatus == 1) {
  826. if (!empty($shop)) {
  827. //花店采购供货商端收到微信通知
  828. //WxMessageClass::ghsHasNewOrderInform($shop, $order);
  829. $noticeText = json_encode(['orderId' => $saleId]);
  830. $noticeKey = "hdCgNoticeGhs";
  831. Yii::$app->redis->executeCommand('LPUSH', [$noticeKey, $noticeText]);
  832. }
  833. }
  834. //订单生成时唤起在线打印
  835. if (isset($order->needPrint) && $order->needPrint == dict::getDict('needPrint', 'need')) {
  836. if ($order->status != 1 && $order->status != 5) {
  837. OrderClass::onlinePrint($order);
  838. $order->printNum += 1;
  839. $order->save();
  840. }
  841. }
  842. //前台提醒出示付款码
  843. if (isset($order->status) && $order->status == 1) {
  844. if (isset($post['isCashier']) && $post['isCashier'] == 1) {
  845. ShopExtClass::ghsPleasePayReport($order);
  846. }
  847. }
  848. }
  849. util::success($return);
  850. } catch (\Exception $e) {
  851. $transaction->rollBack();
  852. Yii::error("下单失败原因:" . $e->getMessage());
  853. noticeUtil::push("零售下单失败原因:" . $e->getMessage(), '15280215347');
  854. util::fail('下单失败');
  855. }
  856. }
  857. //修改订单 ssh 20210810
  858. public function actionUpdateOrder()
  859. {
  860. $post = Yii::$app->request->post();
  861. util::fail('订单不可修改');
  862. //PC端开单
  863. if (isset($post['clearType'])) {
  864. $clearType = $post['clearType'];
  865. if ($clearType == OrderClass::CLEAR_DEBT) {
  866. $post['debt'] = OrderClass::DEBT_YES;
  867. $post['payWay'] = dict::getDict('payWay', 'debtPay');
  868. } else {
  869. $post['debt'] = OrderClass::DEBT_NO;
  870. }
  871. }
  872. //员工不能开已收款订单
  873. $debt = $post['debt'] ?? OrderClass::DEBT_YES;
  874. if ($debt == OrderClass::DEBT_NO) {
  875. $shopAdmin = $this->shopAdmin;
  876. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  877. util::fail('不能开已收款订单');
  878. }
  879. }
  880. //PC端开单
  881. if (isset($post['getType'])) {
  882. util::fail('暂不支持PC端修改订单');
  883. $getType = $post['getType'];
  884. //自取
  885. if ($getType == 1) {
  886. $post['sendType'] = OrderClass::SEND_TYPE_NO;
  887. } else {
  888. //选择配送时,在发货时让商家自己再确认一下用什么方式
  889. $post['sendType'] = OrderClass::SEND_TYPE_UNKNOWN;
  890. }
  891. }
  892. //商家开非自取订单,默认订单是待收款的,debt字段为非欠款
  893. if (isset($post['sendType']) && $post['sendType'] != OrderClass::SEND_TYPE_NO) {
  894. $post['debt'] = OrderClass::DEBT_NO;
  895. }
  896. $id = $post['id'] ?? 0;
  897. $order = OrderClass::getLockById($id);
  898. if (empty($order)) {
  899. util::fail('没有找到订单');
  900. }
  901. OrderClass::valid($order, $this->shopId);
  902. unset($post['id']);
  903. $shopAdmin = $this->shopAdmin;
  904. $post['shopAdminId'] = $this->shopAdminId;
  905. $post['shopAdminName'] = $shopAdmin->name ?? '';
  906. $post['fromType'] = 1;
  907. $post['sendCost'] = $post['sendCost'] ?? '0.00';
  908. $productJson = $post['product'] ?? '';
  909. if (empty($productJson)) {
  910. util::fail('请选择花材');
  911. }
  912. $productList = json_decode($productJson, true);
  913. if (empty($productList)) {
  914. util::fail('请选择花材');
  915. }
  916. $connection = Yii::$app->db;//事务处理
  917. $transaction = $connection->beginTransaction();
  918. try {
  919. //判断花材有效性
  920. ProductClass::valid($productList, $this->mainId);
  921. $post['product'] = $productList;
  922. //商家开单订单有效期,到期记欠款
  923. $validTime = dict::getDict('order_pay_has_time');
  924. $current = time() + $validTime;
  925. $post['deadline'] = date("Y-m-d H:i:s", $current);
  926. $upData = [
  927. 'payWay' => $post['payWay'],
  928. 'sendType' => $post['sendType'],
  929. 'debt' => $post['debt'],
  930. 'needPrint' => $post['needPrint'],
  931. 'modifyPrice' => $post['modifyPrice'],
  932. 'shopAdminId' => $post['shopAdminId'],
  933. 'shopAdminName' => $post['shopAdminName'],
  934. 'fromType' => 1,
  935. 'sendCost' => $post['sendCost'],
  936. 'product' => $post['product'],
  937. 'deadline' => $post['deadline'],
  938. 'debt' => $post['debt'],
  939. ];
  940. $return = OrderService::updateOrder($order, $upData);
  941. $transaction->commit();
  942. util::success($return);
  943. } catch (\Exception $e) {
  944. $transaction->rollBack();
  945. Yii::error("下单失败原因:" . $e->getMessage());
  946. util::fail('下单失败');
  947. }
  948. }
  949. //取消订单 shizhongqi 20220619
  950. public function actionCancelOrder()
  951. {
  952. $post = Yii::$app->request->post();
  953. $id = $post['orderId'] ?? 0;
  954. if (empty($id)) {
  955. util::fail('请选择订单');
  956. }
  957. $order = OrderClass::getById($id, true);
  958. if (empty($order)) {
  959. util::fail('请选择订单...');
  960. }
  961. OrderClass::valid($order, $this->shopId);
  962. if (isset($order->status) == false || $order->status != 1) {
  963. util::fail('不是待付款状态,无法取消');
  964. }
  965. $purchaseId = $order->purchaseId ?? 0;
  966. $purchase = PurchaseClass::getById($purchaseId);
  967. if (empty($purchase)) {
  968. util::fail('没有找到采购单');
  969. }
  970. $deadLineTime = strtotime($purchase['deadline']);
  971. $diff = $deadLineTime - time();
  972. if ($diff <= 60) {
  973. util::fail('1分钟后订单将自动取消');
  974. }
  975. $connection = Yii::$app->db;//事务处理
  976. $transaction = $connection->beginTransaction();
  977. $orderSn = $purchase['orderSn'];
  978. try {
  979. $current = PurchaseClass::getById($purchaseId, true);
  980. PurchaseService::expire($current);
  981. $transaction->commit();
  982. //noticeUtil::push("采购单过期未付款,库存已回滚,单号:{$orderSn}");
  983. } catch (\Exception $e) {
  984. $transaction->rollBack();
  985. $msg = $e->getMessage();
  986. noticeUtil::push("采购单过期处理报错了!订单号:{$orderSn},错误信息:{$msg}", '15280215347');
  987. }
  988. util::complete();
  989. }
  990. //延期支付 ssh 2021.1.19
  991. public function actionDebt()
  992. {
  993. $id = Yii::$app->request->get('id');
  994. $order = OrderClass::getById($id, true);
  995. OrderClass::valid($order, $this->shopId);
  996. $payWay = dict::getDict('payWay', 'debtPay');
  997. $cgId = $order->purchaseId ?? 0;
  998. $cg = PurchaseClass::getById($cgId, true);
  999. if (empty($cg)) {
  1000. util::fail('没有找到采购单');
  1001. }
  1002. if ($order->book == 1) {
  1003. util::fail('预订单不能记欠款');
  1004. }
  1005. PurchaseService::payAfter($cg, $payWay);
  1006. OrderService::payAfter($order, $payWay);
  1007. $order = OrderClass::getById($id, true);
  1008. if (!empty($order)) {
  1009. $shopId = $order->shopId ?? 0;
  1010. $shop = ShopClass::getById($shopId, true);
  1011. if (!empty($shop)) {
  1012. //花店采购供货商端微信收到通知
  1013. //WxMessageClass::ghsHasNewOrderInform($shop, $order);
  1014. $noticeText = json_encode(['orderId' => $id]);
  1015. $noticeKey = "hdCgNoticeGhs";
  1016. Yii::$app->redis->executeCommand('LPUSH', [$noticeKey, $noticeText]);
  1017. }
  1018. //订单生成时唤起在线打印
  1019. if (isset($order->needPrint) && $order->needPrint == dict::getDict('needPrint', 'need')) {
  1020. if ($order->status != 1 && $order->status != 5) {
  1021. OrderClass::onlinePrint($order);
  1022. $order->printNum += 1;
  1023. $order->save();
  1024. }
  1025. }
  1026. if ($order->status != 1 && $order->status != 5) {
  1027. //花店采购供货商端APP收到通知
  1028. //NoticeClass::ghsNewOrderNotice($order);
  1029. }
  1030. }
  1031. util::complete();
  1032. }
  1033. //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
  1034. public function actionWxPay()
  1035. {
  1036. ini_set('date.timezone', 'Asia/Shanghai');
  1037. $post = Yii::$app->request->post();
  1038. $couponId = $post['couponId'] ?? 0;
  1039. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  1040. $order = OrderClass::getByOrderSn($orderSn);
  1041. if (empty($order)) {
  1042. util::fail('订单号无效');
  1043. }
  1044. $id = $order['id'];
  1045. //支付前验证订单有效性
  1046. if (isset($order['adminId']) == false || $order['adminId'] != $this->adminId) {
  1047. util::fail('无法操作');
  1048. }
  1049. $name = $order['orderName'] ?? '购买商品';
  1050. $totalFee = $order['actPrice'];
  1051. //强制使用小程序的miniOpenId
  1052. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  1053. if (empty($openId)) {
  1054. util::fail('没有找到openId');
  1055. }
  1056. $capitalType = dict::getDict('capitalType', 'xhGhsOrder', 'id');
  1057. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  1058. $wxPayType = 0;
  1059. if (httpUtil::isMiniProgram()) {
  1060. $wxPayType = 1;
  1061. }
  1062. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  1063. //订单30分钟后过期
  1064. $now = time();
  1065. $expireTime = $now + 1800;
  1066. $wx = Yii::getAlias("@vendor/weixin");
  1067. require_once($wx . '/lib/WxPay.Api.php');
  1068. require_once($wx . '/example/WxPay.JsApiPay.php');
  1069. $input = new \WxPayUnifiedOrder();
  1070. $input->SetBody($name);
  1071. $input->SetOut_trade_no($orderSn);
  1072. $input->SetTotal_fee($totalFee * 100);
  1073. $input->SetTime_start(date("YmdHis", $now));
  1074. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  1075. $input->SetTime_expire(date("YmdHis", $expireTime));
  1076. $input->SetNotify_url(Yii::$app->params['ghsHost'] . '/notice/wx-callback/');
  1077. $input->SetTrade_type("JSAPI");
  1078. //花卉宝代为申请的微信支付
  1079. //$sjExtend = $this->sjExtend->attributes;
  1080. $sjExtend = WxOpenClass::getGhsWxInfo();
  1081. if (isset($sjExtend['wxPayApply']) && $sjExtend['wxPayApply'] == 1) {
  1082. $input->SetSub_openid($openId);
  1083. } else {
  1084. $input->SetOpenid($openId);
  1085. }
  1086. //强制使用小程序的miniAppId
  1087. $sjExtend['wxAppId'] = $sjExtend['miniAppId'];
  1088. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $sjExtend);
  1089. $tools = new \JsApiPay();
  1090. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $sjExtend);
  1091. $newParams = json_decode($jsApiParameters, true);
  1092. //微信不能修改价格
  1093. $current = date("Y-m-d H:i:s");
  1094. $updateData = ['deadline' => $current, 'modPrice' => 0];
  1095. OrderClass::updateById($id, $updateData);
  1096. util::success($newParams);
  1097. }
  1098. //获取订单详情 ssh 2021.1.21
  1099. public function actionDetail()
  1100. {
  1101. $get = Yii::$app->request->get();
  1102. $id = $get['id'] ?? 0;
  1103. $info = OrderService::getOrderInfo($id, true, true, true, false);
  1104. OrderClass::valid($info, $this->shopId);
  1105. //是否有云打印判断
  1106. $hasCloudPrint = 0;
  1107. $shopExt = $this->shopExt;
  1108. if (isset($shopExt->printSn) && !empty($shopExt->printSn) && isset($shopExt->printKey) && !empty($shopExt->printKey)) {
  1109. $hasCloudPrint = 1;
  1110. }
  1111. $info['hasCloudPrint'] = $hasCloudPrint;
  1112. $purchaseId = $info['purchaseId'] ?? 0;
  1113. $cg = PurchaseClass::getById($purchaseId);
  1114. $info['cgInfo'] = $cg;
  1115. //店长信息
  1116. $mainId = $cg['mainId'] ?? 0;
  1117. $superInfo = ShopAdminClass::getManager($mainId);
  1118. $info['customSuper'] = $superInfo;
  1119. util::success($info);
  1120. }
  1121. //获取订单详情,不需要登录可以访问,后面要删除此方法 ssh 20220819
  1122. public function actionInfo()
  1123. {
  1124. $get = Yii::$app->request->get();
  1125. $id = $get['id'] ?? 0;
  1126. $info = OrderService::getOrderInfo($id, true, true, true, false);
  1127. if (empty($info)) {
  1128. util::fail('没有找到订单');
  1129. }
  1130. //是否有云打印判断
  1131. $hasCloudPrint = 0;
  1132. $shopExt = $this->shopExt;
  1133. if (isset($shopExt->printSn) && !empty($shopExt->printSn) && isset($shopExt->printKey) && !empty($shopExt->printKey)) {
  1134. $hasCloudPrint = 1;
  1135. }
  1136. $info['hasCloudPrint'] = $hasCloudPrint;
  1137. $purchaseId = $info['purchaseId'] ?? 0;
  1138. $cg = PurchaseClass::getById($purchaseId);
  1139. $info['cgInfo'] = $cg;
  1140. //店长信息
  1141. $mainId = $cg['mainId'] ?? 0;
  1142. $superInfo = ShopAdminClass::getManager($mainId);
  1143. $info['customSuper'] = $superInfo;
  1144. util::success($info);
  1145. }
  1146. //自取免发货流程处理 ssh 2021.1.22
  1147. public function actionWithoutSend()
  1148. {
  1149. util::complete();
  1150. }
  1151. //本店送 ssh 2021.1.24
  1152. public function actionSelfSend()
  1153. {
  1154. $get = Yii::$app->request->get();
  1155. $id = isset($get['id']) ? $get['id'] : 0;
  1156. $info = OrderClass::getById($id, true);
  1157. OrderClass::valid($info->attributes, $this->shopId);
  1158. $connection = Yii::$app->db;
  1159. $transaction = $connection->beginTransaction();
  1160. try {
  1161. OrderClass::selfSend($info);
  1162. $transaction->commit();
  1163. } catch (\Exception $exception) {
  1164. $transaction->rollBack();
  1165. Yii::info("本店送操作报错:" . $exception->getMessage());
  1166. util::fail('操作失败');
  1167. }
  1168. util::complete();
  1169. }
  1170. //运费计算 ssh 2021.1.24
  1171. public function actionFreight()
  1172. {
  1173. //2021.3.28 接入达达
  1174. $get = Yii::$app->request->get();
  1175. $post = Yii::$app->request->post();
  1176. if (empty($get)) {
  1177. $get = $post;
  1178. }
  1179. $orderId = $get['id'] ?? 0; //订单ID
  1180. $customId = $get['customId'] ?? 0;
  1181. if (empty($orderId)) {
  1182. //自定义运费
  1183. //freight::getCost();
  1184. if (empty($customId)) {
  1185. util::fail("请选择客户");
  1186. }
  1187. $customInfo = CustomClass::getById($customId);
  1188. if (empty($customInfo)) {
  1189. util::fail('没有找到客户');
  1190. }
  1191. $shop = $this->shop;
  1192. //花材信息
  1193. $productJson = $get['product'] ?? '';
  1194. // $productJson = '[{"productId":31993,"bigNum":1,"smallNum":0}]';
  1195. if (empty($productJson)) {
  1196. util::fail('请选择花材');
  1197. }
  1198. $productList = json_decode($productJson, true);
  1199. if (empty($productList)) {
  1200. util::fail('请选择花材');
  1201. }
  1202. $productList = ProductClass::mergeItemInfo($productList);
  1203. $weight = 0;
  1204. $price = 0;
  1205. $productData = ProductClass::getProductMapData($productList);
  1206. $bigNum = 0;
  1207. foreach ($productList as $key => $val) {
  1208. $productId = $val['productId'];
  1209. $w = ProductClass::getWeight($val['productId'], $val['bigNum'], $val['smallNum']);
  1210. $bigNum += $val['bigNum'];
  1211. $weight = bcadd($w, $weight, 2);
  1212. $ratio = $productData[$productId]['ratio'] ?? 0;
  1213. //大小数量合并多少扎
  1214. $itemNum = ProductClass::mergeItemNum($val['bigNum'], $val['smallNum'], $ratio);
  1215. //售卖价格
  1216. $itemPrice = $productData[$productId]['price'] ?? 0;
  1217. //合计价格
  1218. $price = bcadd($price, bcmul($itemNum, $itemPrice, 2), 2);
  1219. }
  1220. //没有经纬度的客户运费直接返回空 ssh 20210612
  1221. if (empty($customInfo['lat']) || empty($customInfo['long'])) {
  1222. util::success(['sedCost' => '']);
  1223. }
  1224. $cost = freight::getCost($shop->lat, $shop->long, $customInfo['lat'], $customInfo['long'], $weight);
  1225. util::success(['sedCost' => $cost]);
  1226. }
  1227. $province = $get['province'] ?? '';
  1228. $city = $get['city'] ?? '';
  1229. $address = $get['address'] ?? '';
  1230. $floor = $get['floor'] ?? '';
  1231. $customName = $get['customName'] ?? '';
  1232. $customMobile = $get['customMobile'] ?? '';
  1233. $fullAddress = $province . $city . $address . $floor;
  1234. $sendTime = $get['sendTime'] ?? '';
  1235. $lat = $get['lat'] ?? '';
  1236. $lng = $get['long'] ?? '';
  1237. if (empty($lat) || empty($lng) || empty($fullAddress)) {
  1238. util::fail('请填写客户地址');
  1239. }
  1240. ini_set('date.timezone', 'Asia/Shanghai');
  1241. if (!empty($sendTime)) {
  1242. //配送时间不为空
  1243. // 预约发单时间(预约时间unix时间戳(10位),精确到分;整分钟为间隔,并且需要至少提前5分钟预约
  1244. $sendTimeInt = strtotime($sendTime);
  1245. $now = time();
  1246. $diff = $sendTimeInt - $now;
  1247. //因服务器写入时间,改为至少提前6分钟
  1248. if ($diff <= 360) {
  1249. util::fail('配送时间至少提前6分钟');
  1250. }
  1251. } else {
  1252. //默认立即发送
  1253. $sendTime = 0;
  1254. }
  1255. //更新订单表
  1256. $info = OrderService::getOrderInfo($orderId);
  1257. OrderClass::valid($info, $this->shopId);
  1258. if ($sendTime) {
  1259. $sendTime = date('Y-m-d H:i', strtotime($sendTime));
  1260. } else {
  1261. $sendTime = '0000-00-00 00:00:00';
  1262. }
  1263. $expressAddInfo = [
  1264. 'customName' => $customName,
  1265. 'customMobile' => $customMobile,
  1266. 'province' => $province,
  1267. 'city' => $city,
  1268. 'address' => $address,
  1269. 'floor' => $floor,
  1270. 'lat' => $lat,
  1271. 'long' => $lng,
  1272. 'sendTime' => $sendTime,
  1273. ];
  1274. OrderClass::updateCustomExpressInfo($orderId, $expressAddInfo);
  1275. //计算运费
  1276. $info['province'] = $province;
  1277. $info['city'] = $city;
  1278. $info['address'] = $address;
  1279. $info['floor'] = $floor;
  1280. $info['fullAddress'] = $fullAddress;
  1281. $info['lat'] = $lat;
  1282. $info['long'] = $lng;
  1283. $info['sendTime'] = $expressAddInfo['sendTime'];
  1284. $res = DadaExpressServices::queryDeliverFee($info);
  1285. util::success(['sedCost' => $res['fee']]);
  1286. }
  1287. //发快递和第三方配送 ssh 2021.1.24
  1288. public function actionThirdSend()
  1289. {
  1290. $get = Yii::$app->request->get();
  1291. $id = isset($get['id']) ? $get['id'] : 0;
  1292. $info = OrderService::getById($id, true);
  1293. OrderClass::valid($info->attributes, $this->shopId);
  1294. if (empty($info->lat) || empty($info->long) || empty($info->fullAddress)) {
  1295. util::fail('请填写客户地址');
  1296. }
  1297. $connection = Yii::$app->db;
  1298. $transaction = $connection->beginTransaction();
  1299. try {
  1300. $respond = OrderClass::thirdSend($info, $get);
  1301. $transaction->commit();
  1302. util::success($respond);
  1303. } catch (\Exception $exception) {
  1304. $transaction->rollBack();
  1305. Yii::info("发快递操作报错:" . $exception->getMessage());
  1306. util::fail('操作失败' . $exception->getMessage());
  1307. }
  1308. }
  1309. //确认送达 ssh 2021.1.24
  1310. public function actionReach()
  1311. {
  1312. $get = Yii::$app->request->get();
  1313. $id = isset($get['id']) ? $get['id'] : 0;
  1314. $info = OrderClass::getById($id, true);
  1315. OrderClass::valid($info->attributes, $this->shopId);
  1316. $connection = Yii::$app->db;
  1317. $transaction = $connection->beginTransaction();
  1318. try {
  1319. OrderService::reach($info);
  1320. $transaction->commit();
  1321. } catch (\Exception $exception) {
  1322. $transaction->rollBack();
  1323. Yii::info("送达操作报错:" . $exception->getMessage());
  1324. util::fail('操作失败');
  1325. }
  1326. util::complete();
  1327. }
  1328. //下单要用到的相关信息 ssh 2019.12.6
  1329. public function actionOrderRelate()
  1330. {
  1331. $regionTree = RegionService::tree();
  1332. $shop = $this->shop->attributes;
  1333. $freight = ['first' => 5, 'add' => 2];
  1334. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  1335. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'txMapKey' => $mapKey, 'merchant' => $shop];
  1336. util::success($out);
  1337. }
  1338. //客户欠款的订单 ssh 2021.2.4
  1339. public function actionDebtList()
  1340. {
  1341. $get = Yii::$app->request->get();
  1342. $id = $get['id'] ?? 0;
  1343. $info = CustomClass::getCustom($id);
  1344. CustomClass::valid($info, $this->shopId);
  1345. $where = ['customId' => $id, 'debt' => 1];
  1346. $searchTime = $get['searchTime'] ?? '';
  1347. if (!empty($searchTime)) {
  1348. $startTime = $get['startTime'] ?? '';
  1349. $endTime = $get['endTime'] ?? '';
  1350. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  1351. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  1352. }
  1353. $list = OrderClass::getAllByCondition($where, 'addTime DESC', ['id', 'orderSn', 'actPrice', 'addTime', 'remainDebtPrice']);
  1354. $result = ['customInfo' => $info, 'list' => $list];
  1355. util::success($result);
  1356. }
  1357. //更新打印次数
  1358. public function actionAddPrintNum()
  1359. {
  1360. $id = Yii::$app->request->get('id', 0);
  1361. $order = OrderClass::getById($id, true);
  1362. OrderClass::valid($order, $this->shopId);
  1363. $order->printNum += 1;
  1364. $order->save();
  1365. util::complete();
  1366. }
  1367. //打印订单 ssh 20210714
  1368. public function actionCloudPrintOrder()
  1369. {
  1370. $get = Yii::$app->request->get();
  1371. $id = $get['id'] ?? 0;
  1372. $showPrice = $get['showPrice'] ?? 1;
  1373. $order = OrderClass::getById($id, true);
  1374. OrderClass::valid($order, $this->shopId);
  1375. if ($order->status == OrderClass::ORDER_STATUS_UN_PAY) {
  1376. util::fail('订单还没有付款');
  1377. }
  1378. if ($order->status == OrderClass::ORDER_STATUS_CANCEL) {
  1379. util::fail('订单已取消');
  1380. }
  1381. $ext = $this->shopExt;
  1382. if (isset($ext->printSn) == false || isset($ext->printKey) == false || empty($ext->printSn) || empty($ext->printKey)) {
  1383. util::success(['hasNoPrint' => 1]);
  1384. }
  1385. OrderClass::onlinePrint($order, $showPrice);
  1386. $order->printNum += 1;
  1387. $order->save();
  1388. util::complete();
  1389. }
  1390. //订单确认完成 ssh 20210809
  1391. public function actionConfirmFinish()
  1392. {
  1393. $payWay = Yii::$app->request->get('payWay', -1);
  1394. $id = Yii::$app->request->get('id', 0);
  1395. $clearType = Yii::$app->request->get('clearType', 0);
  1396. $connection = Yii::$app->db;
  1397. $transaction = $connection->beginTransaction();
  1398. try {
  1399. $order = OrderClass::getById($id, true);
  1400. OrderClass::valid($order, $this->shopId);
  1401. $purchaseId = $order->purchaseId ?? 0;
  1402. $purchase = PurchaseClass::getById($purchaseId, true);
  1403. if (empty($purchase)) {
  1404. util::fail('没有采购信息');
  1405. }
  1406. if ($clearType == 1) {
  1407. //欠款
  1408. $payWay = dict::getDict('payWay', 'debtPay');
  1409. } elseif ($clearType == 2) {
  1410. //已收款
  1411. if ($payWay == -1) {
  1412. util::fail('请选择收款方式');
  1413. }
  1414. $shopAdmin = $this->shopAdmin;
  1415. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  1416. util::fail('超管才能发起已收款');
  1417. }
  1418. } else {
  1419. util::fail('请选择结算方式');
  1420. }
  1421. //不需要打印
  1422. $order->needPrint = dict::getDict('needPrint', 'noNeed');
  1423. $order->save();
  1424. //支付
  1425. PurchaseService::payAfter($purchase, $payWay);
  1426. OrderService::payAfter($order, $payWay);
  1427. //自己送
  1428. $info = OrderClass::getById($id, true);
  1429. OrderClass::selfSend($info);
  1430. //确认送达,并且不需要微信通知
  1431. Yii::$app->params['noNeedWxNotice'] = 1;
  1432. $info = OrderClass::getById($id, true);
  1433. OrderService::reach($info);
  1434. $transaction->commit();
  1435. util::complete('操作成功');
  1436. } catch (\Exception $exception) {
  1437. $transaction->rollBack();
  1438. Yii::info("确认完成订单报错:" . $exception->getMessage());
  1439. util::fail('操作失败');
  1440. }
  1441. }
  1442. //取消订单 ssh 20220110
  1443. public function actionCancel()
  1444. {
  1445. $shopAdmin = $this->shopAdmin;
  1446. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  1447. util::fail('超管才能取消订单');
  1448. }
  1449. $id = Yii::$app->request->get('id');
  1450. $order = OrderClass::getById($id, true);
  1451. if ($order->orderType == 2) {
  1452. util::fail('客户下单,不能主动取消');
  1453. }
  1454. OrderClass::valid($order, $this->shopId);
  1455. OrderService::expire($order, true);
  1456. util::complete('已取消');
  1457. }
  1458. //获取收款语音播报地址 ssh 20211231
  1459. public function actionGetPayVoice()
  1460. {
  1461. $id = Yii::$app->request->get('id');
  1462. $order = OrderClass::getById($id, true);
  1463. $payWay = $order->payWay ?? 0;
  1464. $money = floatval($order->actPrice);
  1465. $payment = $payWay == dict::getDict('payWay', 'alipay') ? '支付宝' : '微信';
  1466. $msg = $payment . '收款' . $money . '元';
  1467. $audio = baiduAip::transfer($msg, 4, 0);
  1468. //上传到oss
  1469. $file = time() . rand(10000, 999999) . '.mp3';
  1470. $filePath = 'tmp/arrival/money/' . $file;
  1471. oss::uploadTmpAudio($filePath, $audio);
  1472. $url = Yii::$app->params['ghsImgHost'] . $filePath;
  1473. util::success(['url' => $url]);
  1474. }
  1475. //修改备注
  1476. public function actionModifyRemark()
  1477. {
  1478. $post = Yii::$app->request->post();
  1479. $id = $post['id'] ?? 0;
  1480. $remark = $post['remark'] ?? '';
  1481. $order = OrderClass::getById($id, true);
  1482. if (empty($order)) {
  1483. util::fail('没有找到订单');
  1484. }
  1485. if ($order->mainId != $this->mainId) {
  1486. util::fail('不是你的订单');
  1487. }
  1488. $cgId = $order->purchaseId ?? 0;
  1489. $cg = PurchaseClass::getById($cgId, true);
  1490. if (empty($cg)) {
  1491. util::fail('采购单没有找到');
  1492. }
  1493. $order->remark = $remark;
  1494. $order->save();
  1495. $cg->remark = $remark;
  1496. $cg->save();
  1497. util::complete('修改成功');
  1498. }
  1499. }