OrderClearController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\ghs\classes\GhsClass;
  4. use biz\wx\classes\WxMessageClass;
  5. use bizGhs\clear\classes\OrderCgClearClass;
  6. use bizGhs\custom\classes\CustomClass;
  7. use bizGhs\order\classes\OrderClearClass;
  8. use bizGhs\shop\classes\ShopClass;
  9. use bizHd\purchase\classes\PurchaseClearClass;
  10. use common\components\dateUtil;
  11. use common\components\dict;
  12. use Yii;
  13. use common\components\util;
  14. class OrderClearController extends BaseController
  15. {
  16. //收款 ssh 2021.2.4
  17. public function actionClear()
  18. {
  19. $post = Yii::$app->request->post();
  20. $shopAdmin = $this->shopAdmin;
  21. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  22. util::fail('超管才能结帐');
  23. }
  24. $id = Yii::$app->request->post('id');
  25. $customId = Yii::$app->request->post('customId', 0);
  26. if (empty($id)) {
  27. util::fail('请选择交易记录');
  28. }
  29. //抹零之后的价格
  30. $modifyPrice = Yii::$app->request->post('modifyPrice', 0.00);
  31. if (is_numeric($modifyPrice) == false || $modifyPrice <= 0) {
  32. util::fail('金额填错了哦');
  33. }
  34. $custom = CustomClass::getById($customId, true);
  35. CustomClass::valid($custom, $this->shopId);
  36. $info = OrderClearClass::getByCondition(['customId' => $customId, 'status' => 1], true);
  37. if (!empty($info)) {
  38. util::fail('已有待结账单,请先取消或提醒客户付款');
  39. }
  40. $where = ['customId' => $customId, 'status' => PurchaseClearClass::STATUS_AWAIT_PAY];
  41. $searchTime = $post['searchTime'] ?? '';
  42. //结账记录太多数据库会报错,所以必须选月份才能结账!!!!!
  43. //订单详情里可以单个结账
  44. if (count((array)$id) > 135) {
  45. util::fail('提交订单数超过135单');
  46. }
  47. $startTime = $post['startTime'] ?? '';
  48. $endTime = $post['endTime'] ?? '';
  49. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  50. $startDate = $period['startTime'];
  51. $endDate = $period['endTime'];
  52. if (bcdiv(bcsub(strtotime($endDate), strtotime($startDate)), 86400) > 50) {
  53. util::fail('超过30天,请按月结账');
  54. }
  55. $list = OrderClearClass::getAllByCondition($where, null, '*', null, true);
  56. if (!empty($list)) {
  57. foreach ($list as $item) {
  58. //只要新建账单,老账单就取消掉
  59. $item->status = PurchaseClearClass::STATUS_EXPIRE;
  60. $item->save();
  61. OrderCgClearClass::markExpireByClearId($item->id);
  62. }
  63. }
  64. $payWay = Yii::$app->request->post('payWay', 0);
  65. $connection = Yii::$app->db;
  66. $transaction = $connection->beginTransaction();
  67. $name = $this->shopAdmin['name'] ?? '';
  68. $complete = $post['complete'] ?? 0;
  69. $data = [
  70. 'customId' => $customId,
  71. 'ghsShopAdminId' => $this->shopAdminId ?? 0,
  72. 'ghsShopId' => $this->shopId ?? 0,
  73. 'ghsShopAdminName' => $name,
  74. 'modifyPrice' => $modifyPrice,
  75. 'payWay' => $payWay,
  76. 'complete' => $complete,
  77. ];
  78. try {
  79. $clearInfo = OrderClearClass::clear($data, $id, $this->sjId, $this->shopId);
  80. $transaction->commit();
  81. if (!empty($clearInfo)) {
  82. //结账单通知客户
  83. $customShopId = $custom->shopId ?? 0;
  84. $customShop = ShopClass::getById($customShopId, true);
  85. if (!empty($customShop)) {
  86. $noticeRespond = \bizHd\notice\classes\NoticeClass::newClearNotice($customShop, $clearInfo);
  87. if (isset($noticeRespond['hasNotice']) && $noticeRespond['hasNotice'] == 1) {
  88. $clearInfo->hasNoticeCustom = 1;
  89. $clearInfo->save();
  90. }
  91. }
  92. }
  93. util::success($clearInfo);
  94. } catch (\Exception $exception) {
  95. $transaction->rollBack();
  96. Yii::info("批量收款失败:" . $exception->getMessage());
  97. util::fail('操作失败');
  98. }
  99. util::complete();
  100. }
  101. //确认结账 ssh 20220510
  102. public function actionConfirmClear()
  103. {
  104. $get = Yii::$app->request->get();
  105. $id = $get['id'] ?? 0;
  106. $payWay = $get['payWay'] ?? dict::getDict('payWay', 'wxPay');
  107. $payRemark = $get['payRemark'] ?? '';
  108. $clear = OrderClearClass::getById($id, true);
  109. if (empty($clear)) {
  110. util::fail('没有找到结账单');
  111. }
  112. $ghsId = $clear->ghsId ?? 0;
  113. $ghs = GhsClass::getLockById($ghsId);
  114. if ($ghs->shopId != $this->shopId) {
  115. util::fail('没有获得授权');
  116. }
  117. if ($ghs->balance > 0) {
  118. util::mistake('客户有余额,不能通过订单销账,请走充值。有疑问请联系技术员', false, 'none', 3000);
  119. }
  120. $staff = $this->shopAdmin;
  121. if ($staff->finance == 0) {
  122. $speShopList = [4608, 17118, 72366, 78556];
  123. //杭州斗南鲜花、淘花里小榄店、淘花里中山店、淘花里珠海店,没有财务权限也要能销单
  124. if (!in_array($this->shopId, $speShopList)) {
  125. util::fail('没有财务权限,不能销单哦');
  126. }
  127. }
  128. //避免网络延迟,造成重复操作
  129. $cacheKey = 'ghs_custom_confirm_clear_' . $id;
  130. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  131. if (!empty($has)) {
  132. util::fail('6秒之后再操作');
  133. }
  134. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 6, 'has']);
  135. //多个地方需要同步修改clear_order_power
  136. if (getenv('YII_ENV') == 'production') {
  137. //小向花卉
  138. if (in_array($this->shopId, [23580, 24713])) {
  139. if (!in_array($this->adminId, [23960,77951,4])) {
  140. util::fail('不能销单哈!');
  141. }
  142. }
  143. //花样年华只有总控才能结账
  144. if (in_array($this->shopId, [1585, 1596])) {
  145. if ($this->adminId != 2876) {
  146. util::fail('不能销单。。。');
  147. }
  148. }
  149. //花大苪 洋桔梗,只有叶荷姐才能销账,多处请搜索关键词hdb_clear_control
  150. if (in_array($this->shopId, [16070])) {
  151. if (!in_array($this->adminId, [9303, 4])) {
  152. util::fail('不能销单。');
  153. }
  154. }
  155. //中山淘花里,销账权限控制,多处请搜索 thl_clear_control
  156. if (in_array($this->shopId, [17118])) {
  157. if (!in_array($this->adminId, [55494, 55445, 17908, 55459, 55707, 55709])) {
  158. util::fail('你不能销单那。。');
  159. }
  160. }
  161. //淘花里小榄店 thl_xl_clear
  162. if (in_array($this->shopId, [72366])) {
  163. if (!in_array($this->adminId, [69792, 55445, 55707, 69792])) {
  164. util::fail('你不能销单那。。。。');
  165. }
  166. }
  167. //淘花里珠海店
  168. if (in_array($this->shopId, [78556])) {
  169. if (!in_array($this->adminId, [69132, 75771, 43732, 55734, 47775, 55711, 40678, 75798])) {
  170. util::fail('你不能销单,编号00785');
  171. }
  172. }
  173. //小齐鲜花总店和出车,多处请搜索关键词 xq_clear_control
  174. if (in_array($this->shopId, [41467, 42946])) {
  175. if (!in_array($this->adminId, [40144, 42912, 42023])) {
  176. util::fail('不能销单。');
  177. }
  178. }
  179. //恋善好多花控制销账权限
  180. if (in_array($this->shopId, [55238, 56609, 56611])) {
  181. if (!in_array($this->adminId, [43856, 54620])) {
  182. util::fail('暂无权限,请联系老板或财务');
  183. }
  184. }
  185. //天天鲜花 陈江店
  186. if (in_array($this->shopId, [763, 1405, 795])) {
  187. if (!in_array($this->adminId, [2094, 5959])) {
  188. util::fail('不能销单哦');
  189. }
  190. }
  191. //花儿好仙
  192. if (in_array($this->shopId, [22666])) {
  193. if (!in_array($this->adminId, [12869, 23174])) {
  194. util::fail('不能充值销单哈');
  195. }
  196. }
  197. //小丽鲜花
  198. if (in_array($this->shopId, [11094])) {
  199. if (!in_array($this->adminId, [12073, 12296])) {
  200. util::fail('您不能充值销单哦');
  201. }
  202. }
  203. //源花汇
  204. if (in_array($this->shopId, [10649])) {
  205. if (!in_array($this->adminId, [11641, 11648, 4])) {
  206. util::fail('请闵总或小周操作');
  207. }
  208. }
  209. //昱成
  210. if (in_array($this->shopId, [8596])) {
  211. if (!in_array($this->adminId, [9303, 9601])) {
  212. util::fail('只能叶荷和财务操作');
  213. }
  214. }
  215. //花大苪
  216. if (in_array($this->shopId, [8249])) {
  217. if (!in_array($this->adminId, [9303])) {
  218. util::fail('只能叶荷操作');
  219. }
  220. }
  221. //泉城花卉批发
  222. if (in_array($this->shopId, [447])) {
  223. if (!in_array($this->adminId, [1398,2448,2457,2499,82260])) {
  224. util::fail('不能销账,请联系小娟');
  225. }
  226. }
  227. } else {
  228. if (in_array($this->shopId, [36523])) {
  229. if ($this->adminId != 919) {
  230. util::fail('不能销单哈');
  231. }
  232. }
  233. }
  234. $connection = Yii::$app->db;
  235. $transaction = $connection->beginTransaction();
  236. try {
  237. $clear->payRemark = $payRemark;
  238. $clear->save();
  239. $amount = $clear->actPrice ?? 0;
  240. $customId = $clear->customId ?? 0;
  241. $custom = CustomClass::getLockById($customId);
  242. $shop = $this->shop;
  243. $staff = $this->shopAdmin;
  244. //充值
  245. $return = CustomClass::rechargeBalance($custom, $amount, $shop, $staff, $payWay);
  246. $custom = $return['custom'];
  247. $ghs = $return['ghs'];
  248. $customRecharge = $return['customRecharge'];
  249. //结账(现金已在 rechargeBalance 记过,不再重复入账)
  250. OrderClearClass::confirmClear($clear, $payWay, ['skipCashMoney' => true]);
  251. $clearInfo = OrderClearClass::getLockById($id);
  252. //结账消余额
  253. CustomClass::clearConsumeBalance($amount, $custom, $ghs, $shop, $staff, $clearInfo);
  254. $transaction->commit();
  255. //充值销账通知
  256. WxMessageClass::customRechargeClearInform($shop, $customRecharge);
  257. util::complete();
  258. } catch (\Exception $exception) {
  259. $transaction->rollBack();
  260. Yii::info("失败:" . $exception->getMessage());
  261. util::fail('操作失败');
  262. }
  263. }
  264. }