OrderClearController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. $payRemark = $get['payRemark'] ?? '';
  106. $clear = OrderClearClass::getById($id, true);
  107. if (empty($clear)) {
  108. util::fail('没有找到结账单');
  109. }
  110. $ghsId = $clear->ghsId ?? 0;
  111. $ghs = GhsClass::getLockById($ghsId);
  112. if ($ghs->shopId != $this->shopId) {
  113. util::fail('没有获得授权');
  114. }
  115. if ($ghs->balance > 0) {
  116. util::mistake('客户有余额,不能通过订单销账,请走充值。有疑问请联系技术员', false, 'none', 3000);
  117. }
  118. $staff = $this->shopAdmin;
  119. if (!isset($staff->finance) || $staff->finance == 0) {
  120. $speShopList = [4608];
  121. //杭州斗南鲜花员工,没有财务权限也要能销单
  122. if (!in_array($this->shopId, $speShopList)) {
  123. util::fail('没有财务权限,不能销单哦');
  124. }
  125. }
  126. //避免网络延迟,造成重复操作
  127. $cacheKey = 'ghs_custom_confirm_clear_' . $id;
  128. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  129. if (!empty($has)) {
  130. util::fail('6秒之后再操作');
  131. }
  132. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 6, 'has']);
  133. //多个地方需要同步修改clear_order_power
  134. if (getenv('YII_ENV') == 'production') {
  135. //小向花卉
  136. if (in_array($this->shopId, [23580, 24713])) {
  137. if (!in_array($this->adminId, [24043, 23960])) {
  138. util::fail('不能销单哈!');
  139. }
  140. }
  141. //花样年华只有总控才能结账
  142. if (in_array($this->shopId, [1585, 1596])) {
  143. if ($this->adminId != 2876) {
  144. util::fail('不能销单。。。');
  145. }
  146. }
  147. //花大苪 洋桔梗,只有叶荷姐才能销账,多处请搜索关键词hdb_clear_control
  148. if (in_array($this->shopId, [16070])) {
  149. if (!in_array($this->adminId, [9303, 4])) {
  150. util::fail('不能销单。');
  151. }
  152. }
  153. //中山淘花里,销账权限控制,多处请搜索 thl_clear_control
  154. if (in_array($this->shopId, [17118])) {
  155. if (!in_array($this->adminId, [55494, 55445, 17908, 55459, 55707, 55709])) {
  156. util::fail('你不能销单那。。');
  157. }
  158. }
  159. //小齐鲜花总店和出车,多处请搜索关键词 xq_clear_control
  160. if (in_array($this->shopId, [41467, 42946])) {
  161. if (!in_array($this->adminId, [40144,42912,42023])) {
  162. util::fail('不能销单。');
  163. }
  164. }
  165. //恋善好多花控制销账权限
  166. if (in_array($this->shopId, [55238, 56609, 56611])) {
  167. if (!in_array($this->adminId, [43856, 54620])) {
  168. util::fail('暂无权限,请联系老板或财务');
  169. }
  170. }
  171. //天天鲜花 陈江店
  172. if (in_array($this->shopId, [763, 1405, 795])) {
  173. if (!in_array($this->adminId, [2094, 5959])) {
  174. util::fail('不能销单哦');
  175. }
  176. }
  177. //花儿好仙
  178. if (in_array($this->shopId, [22666])) {
  179. if (!in_array($this->adminId, [12869, 23174])) {
  180. util::fail('不能充值销单哈');
  181. }
  182. }
  183. //小丽鲜花
  184. if (in_array($this->shopId, [11094])) {
  185. if (!in_array($this->adminId, [12073, 12296])) {
  186. util::fail('您不能充值销单哦');
  187. }
  188. }
  189. //源花汇
  190. if (in_array($this->shopId, [10649])) {
  191. if (!in_array($this->adminId, [11641, 11648, 4])) {
  192. util::fail('请闵总或小周操作');
  193. }
  194. }
  195. //昱成
  196. if (in_array($this->shopId, [8596])) {
  197. if (!in_array($this->adminId, [9303, 9601])) {
  198. util::fail('只能叶荷和财务操作');
  199. }
  200. }
  201. //花大苪
  202. if (in_array($this->shopId, [8249])) {
  203. if (!in_array($this->adminId, [9303])) {
  204. util::fail('只能叶荷操作');
  205. }
  206. }
  207. } else {
  208. if (in_array($this->shopId, [36523])) {
  209. if ($this->adminId != 919) {
  210. util::fail('不能销单哈');
  211. }
  212. }
  213. }
  214. $connection = Yii::$app->db;
  215. $transaction = $connection->beginTransaction();
  216. try {
  217. $clear->payRemark = $payRemark;
  218. $clear->save();
  219. $amount = $clear->actPrice ?? 0;
  220. $customId = $clear->customId ?? 0;
  221. $custom = CustomClass::getLockById($customId);
  222. $shop = $this->shop;
  223. $staff = $this->shopAdmin;
  224. //充值
  225. $return = CustomClass::rechargeBalance($custom, $amount, $shop, $staff, $payWay);
  226. $custom = $return['custom'];
  227. $ghs = $return['ghs'];
  228. $customRecharge = $return['customRecharge'];
  229. //结账
  230. OrderClearClass::confirmClear($clear, $payWay);
  231. $clearInfo = OrderClearClass::getLockById($id);
  232. //结账消余额
  233. CustomClass::clearConsumeBalance($amount, $custom, $ghs, $shop, $staff, $clearInfo);
  234. $transaction->commit();
  235. //充值销账通知
  236. WxMessageClass::customRechargeClearInform($shop, $customRecharge);
  237. util::complete();
  238. } catch (\Exception $exception) {
  239. $transaction->rollBack();
  240. Yii::info("失败:" . $exception->getMessage());
  241. util::fail('操作失败');
  242. }
  243. }
  244. }