OrderClearController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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\custom\services\GhsRechargeSettleService;
  8. use bizGhs\order\classes\OrderClearClass;
  9. use bizGhs\shop\classes\ShopClass;
  10. use bizHd\purchase\classes\PurchaseClearClass;
  11. use common\components\dateUtil;
  12. use common\components\dict;
  13. use Yii;
  14. use common\components\util;
  15. class OrderClearController extends BaseController
  16. {
  17. //收款 ssh 2021.2.4
  18. public function actionClear()
  19. {
  20. $post = Yii::$app->request->post();
  21. $shopAdmin = $this->shopAdmin;
  22. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  23. util::fail('超管才能结帐');
  24. }
  25. $id = Yii::$app->request->post('id');
  26. $customId = Yii::$app->request->post('customId', 0);
  27. if (empty($id)) {
  28. util::fail('请选择交易记录');
  29. }
  30. //抹零之后的价格
  31. $modifyPrice = Yii::$app->request->post('modifyPrice', 0.00);
  32. if (is_numeric($modifyPrice) == false || $modifyPrice <= 0) {
  33. util::fail('金额填错了哦');
  34. }
  35. $custom = CustomClass::getLockById($customId);
  36. CustomClass::valid($custom, $this->shopId);
  37. $info = OrderClearClass::getByCondition(['customId' => $customId, 'status' => 1], true);
  38. if (!empty($info)) {
  39. util::fail('已有待结账单,请先取消或提醒客户付款');
  40. }
  41. $where = ['customId' => $customId, 'status' => PurchaseClearClass::STATUS_AWAIT_PAY];
  42. $searchTime = $post['searchTime'] ?? '';
  43. //结账记录太多数据库会报错,所以必须选月份才能结账!!!!!
  44. //订单详情里可以单个结账
  45. if (count((array)$id) > 135) {
  46. util::fail('提交订单数超过135单');
  47. }
  48. $startTime = $post['startTime'] ?? '';
  49. $endTime = $post['endTime'] ?? '';
  50. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  51. $startDate = $period['startTime'];
  52. $endDate = $period['endTime'];
  53. if (bcdiv(bcsub(strtotime($endDate), strtotime($startDate)), 86400) > 50) {
  54. util::fail('超过30天,请按月结账');
  55. }
  56. $list = OrderClearClass::getAllByCondition($where, null, '*', null, true);
  57. if (!empty($list)) {
  58. foreach ($list as $item) {
  59. //只要新建账单,老账单就取消掉
  60. $item->status = PurchaseClearClass::STATUS_EXPIRE;
  61. $item->save();
  62. OrderCgClearClass::markExpireByClearId($item->id);
  63. }
  64. }
  65. $payWay = Yii::$app->request->post('payWay', 0);
  66. $connection = Yii::$app->db;
  67. $transaction = $connection->beginTransaction();
  68. $name = $this->shopAdmin['name'] ?? '';
  69. $complete = $post['complete'] ?? 0;
  70. //检查客户余额是否有问题
  71. //CustomClass::checkBalance($custom);
  72. $data = [
  73. 'customId' => $customId,
  74. 'ghsShopAdminId' => $this->shopAdminId ?? 0,
  75. 'ghsShopId' => $this->shopId ?? 0,
  76. 'ghsShopAdminName' => $name,
  77. 'modifyPrice' => $modifyPrice,
  78. 'payWay' => $payWay,
  79. 'complete' => $complete,
  80. ];
  81. try {
  82. $clearInfo = OrderClearClass::clear($data, $id, $this->sjId, $this->shopId);
  83. $transaction->commit();
  84. if (!empty($clearInfo)) {
  85. //结账单通知客户
  86. $customShopId = $custom->shopId ?? 0;
  87. $customShop = ShopClass::getById($customShopId, true);
  88. if (!empty($customShop)) {
  89. $noticeRespond = \bizHd\notice\classes\NoticeClass::newClearNotice($customShop, $clearInfo);
  90. if (isset($noticeRespond['hasNotice']) && $noticeRespond['hasNotice'] == 1) {
  91. $clearInfo->hasNoticeCustom = 1;
  92. $clearInfo->save();
  93. }
  94. }
  95. }
  96. util::success($clearInfo);
  97. } catch (\Exception $exception) {
  98. $transaction->rollBack();
  99. Yii::info("批量收款失败:" . $exception->getMessage());
  100. util::fail('操作失败');
  101. }
  102. util::complete();
  103. }
  104. //确认结账 ssh 20220510
  105. public function actionConfirmClear()
  106. {
  107. $get = Yii::$app->request->get();
  108. $id = $get['id'] ?? 0;
  109. $payWay = $get['payWay'] ?? dict::getDict('payWay', 'wxPay');
  110. $payRemark = $get['payRemark'] ?? '';
  111. $clear = OrderClearClass::getById($id, true);
  112. if (empty($clear)) {
  113. util::fail('没有找到结账单');
  114. }
  115. $ghsId = $clear->ghsId ?? 0;
  116. $ghs = GhsClass::getLockById($ghsId);
  117. if ($ghs->shopId != $this->shopId) {
  118. util::fail('没有获得授权');
  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, 29865, 84462])) {
  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. $customId = $clear->customId ?? 0;
  240. $custom = CustomClass::getLockById($customId);
  241. $shop = $this->shop;
  242. $staff = $this->shopAdmin;
  243. $return = GhsRechargeSettleService::confirmClearBillWithIncoming($custom, $ghs, $shop, $staff, $clear, $payWay);
  244. $customRecharge = $return['customRecharge'] ?? null;
  245. $transaction->commit();
  246. //充值销账通知
  247. WxMessageClass::customRechargeClearInform($shop, $customRecharge);
  248. util::complete();
  249. } catch (\Exception $exception) {
  250. $transaction->rollBack();
  251. Yii::info("失败:" . $exception->getMessage());
  252. util::fail('操作失败');
  253. }
  254. }
  255. }