OrderClearController.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\ghs\classes\GhsClass;
  4. use biz\wx\classes\WxMessageClass;
  5. use bizGhs\custom\classes\CustomClass;
  6. use bizGhs\order\classes\OrderClearClass;
  7. use bizGhs\shop\classes\ShopClass;
  8. use bizHd\purchase\classes\PurchaseClearClass;
  9. use common\components\dateUtil;
  10. use common\components\dict;
  11. use Yii;
  12. use common\components\util;
  13. class OrderClearController extends BaseController
  14. {
  15. //收款 ssh 2021.2.4
  16. public function actionClear()
  17. {
  18. $post = Yii::$app->request->post();
  19. $shopAdmin = $this->shopAdmin;
  20. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  21. util::fail('超管才能结帐');
  22. }
  23. $id = Yii::$app->request->post('id');
  24. $customId = Yii::$app->request->post('customId', 0);
  25. if (empty($id)) {
  26. util::fail('请选择交易记录');
  27. }
  28. //抹零之后的价格
  29. $modifyPrice = Yii::$app->request->post('modifyPrice', 0.00);
  30. if (is_numeric($modifyPrice) == false || $modifyPrice <= 0) {
  31. util::fail('金额填错了哦');
  32. }
  33. $custom = CustomClass::getById($customId, true);
  34. CustomClass::valid($custom, $this->shopId);
  35. $info = OrderClearClass::getByCondition(['customId' => $customId, 'status' => 1], true);
  36. if (!empty($info)) {
  37. util::fail('已有待结账单,请先取消或提醒客户付款');
  38. }
  39. $where = ['customId' => $customId, 'status' => PurchaseClearClass::STATUS_AWAIT_PAY];
  40. $searchTime = $post['searchTime'] ?? '';
  41. //结账记录太多数据库会报错,所以必须选月份才能结账!!!!!
  42. //订单详情里可以单个结账
  43. if (count((array)$id) > 135) {
  44. util::fail('提交订单数超过135单');
  45. }
  46. $startTime = $post['startTime'] ?? '';
  47. $endTime = $post['endTime'] ?? '';
  48. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  49. $startDate = $period['startTime'];
  50. $endDate = $period['endTime'];
  51. if (bcdiv(bcsub(strtotime($endDate), strtotime($startDate)), 86400) > 50) {
  52. util::fail('超过30天,请按月结账');
  53. }
  54. $list = OrderClearClass::getAllByCondition($where, null, '*', null, true);
  55. if (!empty($list)) {
  56. foreach ($list as $item) {
  57. //只要新建账单,老账单就取消掉
  58. $item->status = PurchaseClearClass::STATUS_EXPIRE;
  59. $item->save();
  60. }
  61. }
  62. $payWay = Yii::$app->request->post('payWay', 0);
  63. $connection = Yii::$app->db;
  64. $transaction = $connection->beginTransaction();
  65. $name = $this->shopAdmin['name'] ?? '';
  66. $complete = $post['complete'] ?? 0;
  67. $data = [
  68. 'customId' => $customId,
  69. 'ghsShopAdminId' => $this->shopAdminId ?? 0,
  70. 'ghsShopId' => $this->shopId ?? 0,
  71. 'ghsShopAdminName' => $name,
  72. 'modifyPrice' => $modifyPrice,
  73. 'payWay' => $payWay,
  74. 'complete' => $complete,
  75. ];
  76. try {
  77. $clearInfo = OrderClearClass::clear($data, $id, $this->sjId, $this->shopId);
  78. $transaction->commit();
  79. if (!empty($clearInfo)) {
  80. //结账单通知客户
  81. $customShopId = $custom->shopId ?? 0;
  82. $customShop = ShopClass::getById($customShopId, true);
  83. if (!empty($customShop)) {
  84. $noticeRespond = \bizHd\notice\classes\NoticeClass::newClearNotice($customShop, $clearInfo);
  85. if (isset($noticeRespond['hasNotice']) && $noticeRespond['hasNotice'] == 1) {
  86. $clearInfo->hasNoticeCustom = 1;
  87. $clearInfo->save();
  88. }
  89. }
  90. }
  91. util::success($clearInfo);
  92. } catch (\Exception $exception) {
  93. $transaction->rollBack();
  94. Yii::info("批量收款失败:" . $exception->getMessage());
  95. util::fail('操作失败');
  96. }
  97. util::complete();
  98. }
  99. //确认结账 ssh 20220510
  100. public function actionConfirmClear()
  101. {
  102. $get = Yii::$app->request->get();
  103. $id = $get['id'] ?? 0;
  104. $payWay = $get['payWay'] ?? dict::getDict('payWay', 'wxPay');
  105. $clear = OrderClearClass::getById($id, true);
  106. if (empty($clear)) {
  107. util::fail('没有找到结账单');
  108. }
  109. $ghsId = $clear->ghsId ?? 0;
  110. $ghs = GhsClass::getLockById($ghsId);
  111. if ($ghs->shopId != $this->shopId) {
  112. util::fail('没有获得授权');
  113. }
  114. $staff = $this->shopAdmin;
  115. if (isset($staff->finance) == false || $staff->finance == 0) {
  116. util::fail('不能销单...');
  117. }
  118. //避免网络延迟,造成重复操作
  119. $cacheKey = 'ghs_custom_confirm_clear_' . $id;
  120. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  121. if (!empty($has)) {
  122. util::fail('6秒之后再操作');
  123. }
  124. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 6, 'has']);
  125. //多个地方需要同步修改clear_order_power
  126. if (getenv('YII_ENV') == 'production') {
  127. //小向花卉
  128. if (in_array($this->shopId, [23580, 24713])) {
  129. if ($this->adminId != 24043) {
  130. util::fail('不能销单!');
  131. }
  132. }
  133. //花样年华只有总控才能结账
  134. if (in_array($this->shopId, [1585, 1596])) {
  135. if ($this->adminId != 2876) {
  136. //util::fail('不能销单!');
  137. }
  138. }
  139. //花大苪 洋桔梗,只有叶荷姐才能销账
  140. if (in_array($this->shopId, [16070])) {
  141. if (!in_array($this->adminId, [9303, 4])) {
  142. util::fail('不能销单。');
  143. }
  144. }
  145. //天天鲜花 陈江店
  146. if (in_array($this->shopId, [763, 1405, 795])) {
  147. if (!in_array($this->adminId, [2094, 5959])) {
  148. util::fail('不能销单哦');
  149. }
  150. }
  151. //花儿好仙
  152. if (in_array($this->shopId, [22666])) {
  153. if (!in_array($this->adminId, [12869, 23174])) {
  154. util::fail('不能充值销单哈');
  155. }
  156. }
  157. //小丽鲜花
  158. if (in_array($this->shopId, [11094])) {
  159. if (!in_array($this->adminId, [12073, 12296])) {
  160. util::fail('您不能充值销单哦');
  161. }
  162. }
  163. } else {
  164. if (in_array($this->shopId, [36523])) {
  165. if ($this->adminId != 919) {
  166. util::fail('不能销单哈');
  167. }
  168. }
  169. }
  170. $connection = Yii::$app->db;
  171. $transaction = $connection->beginTransaction();
  172. try {
  173. $amount = $clear->actPrice ?? 0;
  174. $customId = $clear->customId ?? 0;
  175. $custom = CustomClass::getLockById($customId);
  176. $shop = $this->shop;
  177. $staff = $this->shopAdmin;
  178. //充值
  179. $return = CustomClass::rechargeBalance($custom, $amount, $shop, $staff, $payWay);
  180. $custom = $return['custom'];
  181. $ghs = $return['ghs'];
  182. $customRecharge = $return['customRecharge'];
  183. //结账
  184. OrderClearClass::confirmClear($clear, $payWay);
  185. $clearInfo = OrderClearClass::getLockById($id);
  186. //结账消余额
  187. CustomClass::clearConsumeBalance($amount, $custom, $ghs, $shop, $staff, $clearInfo);
  188. $transaction->commit();
  189. //充值销账通知
  190. WxMessageClass::customRechargeClearInform($shop, $customRecharge);
  191. util::complete();
  192. } catch (\Exception $exception) {
  193. $transaction->rollBack();
  194. Yii::info("失败:" . $exception->getMessage());
  195. util::fail('操作失败');
  196. }
  197. }
  198. }