TagGood.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\OrderTag;use app\admin\model\Pay;use app\admin\model\QrdInfo;use app\admin\model\TagLog;use think\App;
  4. use think\facade\Db;
  5. class TagGood extends Base{
  6. #public $novalidate=['*'];
  7. public function __construct(App $app) {parent::__construct($app);}
  8. //新建标签
  9. public function create(){
  10. $post = $this->post;
  11. $type = isset($post['type'])&& $post['type']!="" ? intval($post['type']) :"";
  12. if($type==''){
  13. return error_show(1004,"参数 type 不能未空");
  14. }
  15. if($post['relaComNo']!=""){
  16. $companyNo = isset($post['relaComNo'])&& $post['relaComNo']!="" ? trim($post['relaComNo']) :"";
  17. }else{
  18. $companyNo = isset($post['companyNo'])&& $post['companyNo']!="" ? trim($post['companyNo']) :"";
  19. }
  20. if($companyNo==''){
  21. return error_show(1004,"参数 companyNo 不能未空");
  22. }
  23. $tagName =isset($post['tag_name'])&&$post['tag_name']!='' ? trim($post['tag_name']):"";
  24. if($tagName==''){
  25. return error_show(1004,"参数 tag_name 不能未空");
  26. }
  27. $isT =Db::name("order_tag")->where(["type"=>$type,"companyNo"=>$companyNo,"tag_name"=>$tagName,"is_del"=>0])
  28. ->findOrEmpty();
  29. if(!empty($isT)){
  30. return error_show(1004,"标签名称已存在");
  31. }
  32. $tagData=[
  33. "type"=>$type,
  34. "tag_name"=>$tagName,
  35. "status"=>1,
  36. "companyNo"=>$companyNo,
  37. "apply_id"=>$this->uid,
  38. "apply_name"=>$this->uname,
  39. "addtime"=>date("Y-m-d H:i:s"),
  40. "updatetime"=>date("Y-m-d H:i:s")
  41. ];
  42. $ins=Db::name("order_tag")->insert($tagData);
  43. return $ins?app_show(0,"标签新建成功"): error_show(1004,"标签新建失败");
  44. }
  45. //标签列表翻页
  46. public function list(){
  47. $page = isset($this->post['page'])&&$this->post['page']!=''?intval($this->post['page']):1;
  48. $size = isset($this->post['size'])&&$this->post['size']!=''?intval($this->post['size']):15;
  49. $type = isset($this->post['type'])&&$this->post['type']!=''?intval($this->post['type']):'';
  50. $status = isset($this->post['status'])&&$this->post['status']!==''?intval($this->post['status']):'';
  51. if($this->post['relaComNo']!=""){
  52. $companyNo = isset($this->post['relaComNo'])&& $this->post['relaComNo']!="" ? trim($this->post['relaComNo']) :"";
  53. }else{
  54. $companyNo = isset($this->post['companyNo'])&& $this->post['companyNo']!="" ? trim($this->post['companyNo']) :"";
  55. }
  56. $condition =[["is_del","=",0]];
  57. $roleid = $this->roleid;
  58. $check = checkRole($roleid,'101');
  59. if($check){
  60. $condition[]=["apply_id","=",$this->uid];
  61. }
  62. if($type!=''){
  63. $condition[]=["type","=",$type];
  64. }
  65. if($status!==''){
  66. $condition[]=["status","=",$status];
  67. }
  68. $tagName =isset($this->post['tag_name'])&&$this->post['tag_name']!='' ? trim($this->post['tag_name']):"";
  69. if($tagName!=''){
  70. $condition[]=["tag_name","like","%$tagName%"];
  71. }
  72. if($companyNo!=''){
  73. $condition[]=["companyNo","=",$companyNo];
  74. }
  75. $count =Db::name("order_tag")->where($condition)->count();
  76. $total=ceil($count/$size);
  77. $page = $page>=$total ? intval($total):$page;
  78. $list =Db::name("order_tag")->where($condition)->page($page,$size)->order("addtime desc")->select()->toArray();
  79. $comNo=array_column($list,"companyNo");
  80. $ComArr=Db::name("supplier_info")->where(["code"=>$comNo])->column("name","code");
  81. foreach ($list as &$item){
  82. $item['companyName']=$ComArr[$item['companyNo']]??"";
  83. }
  84. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  85. }
  86. //根据条件筛选合适的标签数据
  87. public function query(){
  88. $type = isset($this->post['type'])&&$this->post['type']!=''?intval($this->post['type']):'';
  89. $condition =[["is_del","=",0]];
  90. if($type!=''){
  91. $condition[]=["type","=",$type];
  92. }
  93. if($this->post['relaComNo']!=""){
  94. $companyNo = isset($this->post['relaComNo'])&& $this->post['relaComNo']!="" ? trim($this->post['relaComNo']) :"";
  95. }else{
  96. $companyNo = isset($this->post['companyNo'])&& $this->post['companyNo']!="" ? trim($this->post['companyNo']) :"";
  97. }
  98. if($companyNo!=''){
  99. $condition[]=["companyNo","=",$companyNo];
  100. }
  101. $tagName =isset($this->post['tag_name'])&&$this->post['tag_name']!='' ? trim($this->post['tag_name']):"";
  102. if($tagName!=''){
  103. $condition[]=["tag_name","like","%$tagName%"];
  104. }
  105. $status = isset($this->post['status'])&&$this->post['status']!==''?intval($this->post['status']):'';
  106. if($status!==''){
  107. $condition[]=["status","=",$status];
  108. }
  109. $list =Db::name("order_tag")->where($condition)->order("addtime desc")->select()->toArray();
  110. return app_show(0,"获取成功",$list);
  111. }
  112. //启禁用状态
  113. public function status(){
  114. $id =isset($this->post['id'])&&$this->post['id']!=''?intval($this->post['id']):'';
  115. if($id==''){
  116. return error_show(1004,"参数 id 不能为空");
  117. }
  118. $taginfo =Db::name("order_tag")->find(["id"=>$id]);
  119. if($taginfo==false){
  120. return error_show(1004,"标签数据不存在");
  121. }
  122. $status=isset($this->post['status'])&&$this->post['status']!==''?intval($this->post['status']):'';
  123. if($status===''){
  124. return error_show(1004,"参数 status 不能为空");
  125. }
  126. $update =["status"=>$status,"updatetime"=>date("Y-m-d H:i:s")];
  127. $up =Db::name("order_tag")->where($taginfo)->update($update);
  128. return $up?app_show(0,"标签更新成功"): error_show(1004,"标签更新失败");
  129. }
  130. //编辑标签名称类型数据
  131. public function save(){
  132. $id =isset($this->post['id'])&&$this->post['id']!=''?intval($this->post['id']):'';
  133. if($id==''){
  134. return error_show(1004,"参数 id 不能为空");
  135. }
  136. $taginfo =Db::name("order_tag")->where(["id"=>$id,"is_del"=>0])->find();
  137. if($taginfo==false){
  138. return error_show(1004,"标签数据不存在");
  139. }
  140. $tagName=isset($this->post['tag_name'])&&$this->post['tag_name']!=''?trim($this->post['tag_name']):'';
  141. if($tagName===''){
  142. return error_show(1004,"参数 tag_name 不能为空");
  143. }
  144. $type=isset($this->post['type'])&&$this->post['type']!=''?intval($this->post['type']):'';
  145. if($type==''){
  146. return error_show(1004,"参数 type 不能为空");
  147. }
  148. $tag =Db::name("order_tag")->where([['tag_name',"=",$tagName],['companyNo',"=",$taginfo['companyNo']],
  149. ['type',"=",$type], ["id","<>",$id],["is_del","=",0]])->find();
  150. if($tag!=false){
  151. return error_show(1004,"标签名称已存在");
  152. }
  153. $update =["type"=>$type,"tag_name"=>$tagName,"updatetime"=>date("Y-m-d H:i:s")];
  154. $up =Db::name("order_tag")->where($taginfo)->update($update);
  155. return $up?app_show(0,"标签更新成功"): error_show(1004,"标签更新失败");
  156. }
  157. //删除标签
  158. public function delete(){
  159. $id =isset($this->post['id'])&&$this->post['id']!=''?intval($this->post['id']):'';
  160. if($id==''){
  161. return error_show(1004,"参数 id 不能为空");
  162. }
  163. $taginfo =Db::name("order_tag")->find(["id"=>$id]);
  164. if($taginfo==false){
  165. return error_show(1004,"标签数据不存在");
  166. }
  167. $update=["is_del"=>1,"updatetime"=>date("Y-m-d H:i:s")];
  168. $up =Db::name("order_tag")->where($taginfo)->update($update);
  169. return $up?app_show(0,"标签删除成功"): error_show(1004,"标签删除失败");
  170. }
  171. //单号添加标签
  172. public function AddTag(){
  173. $tagid =isset($this->post["tag_id"]) && $this->post["tag_id"]!=""?intval($this->post["tag_id"]) :"";
  174. if($tagid==""){
  175. return error_show(1004,"参数 tag_id 不能为空");
  176. }
  177. $taginfo =Db::name("order_tag")->where(["id"=>$tagid,"is_del"=>0])->find();
  178. if($taginfo==false){
  179. return error_show(1005,"标签数据不存在");
  180. }
  181. if($taginfo['status']==0){
  182. return error_show(1005,"标签已禁用");
  183. }
  184. $code =isset($this->post['code']) &&$this->post["code"]!="" ? trim($this->post["code"]):"";
  185. if($code==''){
  186. return error_show(1004,"参数 code 不能为空");
  187. }
  188. $taglog =Db::name("tag_log")->where(["tag_id"=>$tagid,"code"=>$code,"status"=>1])->findOrEmpty();
  189. $total_fee =isset($this->post['total_fee']) &&$this->post["total_fee"]!=="" ? floatval($this->post["total_fee"]):"";
  190. if($total_fee==''){
  191. return error_show(1004,"参数 total_fee 不能为空");
  192. }
  193. $tagimg=isset($this->post["tag_img"]) && $this->post["tag_img"]!=""?$this->post["tag_img"]:"";
  194. $tag_remark=isset($this->post["tag_remark"]) && $this->post["tag_remark"]!=""?$this->post["tag_remark"]:"";
  195. Db::startTrans();
  196. try{
  197. if(!empty($taglog)){
  198. $up = Db::name("tag_log")->where($taglog)->update(["status"=>0,"updatetime"=>date("Y-m-d H:i:s")]);
  199. if($up==false){
  200. Db::rollback();
  201. return error_show(1004,"标签日志新建失败");
  202. }
  203. }
  204. if($taginfo['type']==1 || $taginfo['type']==2){
  205. $result= $this->payTag($taginfo['type'],$code,$total_fee);
  206. }
  207. if($taginfo['type']==3|| $taginfo['type']==4){
  208. $result= $this->qrdTag($taginfo['type'],$code,$total_fee);
  209. }
  210. $tagdata=[
  211. "code"=>$code,
  212. "tag_id"=>$tagid,
  213. "creater"=>$this->uname,
  214. "createrid"=>$this->uid,
  215. "tag_fee"=>$total_fee,
  216. "tag_img"=>$tagimg,
  217. "tag_remark"=>$tag_remark,
  218. "status"=>1,
  219. "addtime"=>date("Y-m-d H:i:s"),
  220. "updatetime"=>date("Y-m-d H:i:s")
  221. ];
  222. $in =Db::name("tag_log")->insert($tagdata);
  223. if($in==false){
  224. Db::rollback();
  225. return error_show(1004,"标签日志新建失败");
  226. }
  227. $data =$result->getData();
  228. if($data['code']==0){
  229. Db::commit();
  230. return app_show(0,"标签添加成功");
  231. }
  232. Db::rollback();
  233. return error_show($data['code'],$data['message']);
  234. }catch (\Exception $e){
  235. Db::rollback();
  236. return error_show(1004,$e->getMessage());
  237. }
  238. }
  239. public function checkTag(){
  240. $param= $this->request->post(["logid"=>''],"post","trim");
  241. $loginfo = TagLog::where("id",$param['logid'])->findOrEmpty();
  242. if($loginfo->isEmpty())$this->error('未找到标签关联数据');
  243. $tag = OrderTag::where("id",$loginfo->tag_id)->findOrEmpty();
  244. if($tag->isEmpty())$this->error('未找到标签信息数据');
  245. TagLog::startTrans();
  246. $message="";
  247. try{
  248. if($tag['type']==1 || $tag['type']==2){
  249. $message="对账单{$loginfo->code}{$tag->tag_name}";
  250. $this->payReTag($tag['type'],$loginfo->code,$loginfo->tag_fee);
  251. }
  252. if($tag['type']==3|| $tag['type']==4){
  253. $message="销售单{$loginfo->code}{$tag->tag_name}";
  254. $this->qrdReTag($tag['type'],$loginfo->code,$loginfo->tag_fee);
  255. }
  256. $loginfo->status=0;
  257. $loginfo->save();
  258. }catch (\Exception $exception){
  259. TagLog::rollback();
  260. $this->error($exception->getMessage());
  261. }
  262. TagLog::commit();
  263. $this->success("{$message}数据标签解除");
  264. }
  265. /**
  266. * @param $tagId 标签类型 1 付款2回票
  267. * @param $code 对账单编号
  268. * @param $total_fee 标签金额
  269. * @return \think\response\Json|void
  270. * @throws \think\db\exception\DbException
  271. */
  272. private function payTag($tagId,$code,$total_fee){
  273. $pay=Db::name("pay")->where(["payNo"=>$code,"is_del"=>0])->findOrEmpty();
  274. if(empty($pay)){
  275. return error_show(1004,"未找到对账单数据");
  276. }
  277. $update=["updatetime"=>date("Y-m-d H:i:s")];
  278. if($tagId==1){
  279. if($pay['wpay_fee']+$pay['pay_tag_fee']<$total_fee){
  280. return error_show(1004,"对账单未付金额不足");
  281. }
  282. $update['wpay_fee']=$pay['wpay_fee']+$pay['pay_tag_fee']-$total_fee;
  283. $status = $update['wpay_fee']==0 &&$pay['pay_fee']==0 ? 3:($pay['apay_fee']==0?1:2);
  284. $update['pay_tag_fee'] = $total_fee;
  285. $update['pay_status'] = $status;
  286. $update['pay_tag'] = 1;
  287. }
  288. if($tagId==2){
  289. if($pay['winv_fee']+$pay['inv_tag_fee']<$total_fee){
  290. return error_show(1004,"对账单未付金额不足");
  291. }
  292. $update['winv_fee']=$pay['winv_fee']+$pay['inv_tag_fee']-$total_fee;
  293. $status = $update['winv_fee']==0 &&$pay['inv_fee']==0 ? 3:($pay['ainv_fee']==0?1:2);
  294. $update['inv_tag_fee'] = $total_fee;
  295. $update['inv_status'] = $status;
  296. $update['inv_tag'] = 1;
  297. }
  298. $resulr= Db::name("pay")->where($pay)->update($update);
  299. return $resulr? app_show(0,"标签添加成功"):error_show(1004,"标签添加失败");
  300. }
  301. /**解除标签
  302. * @param $tagId 标签类型 1 付款2回票
  303. * @param $code 对账单编号
  304. * @param $total_fee 标签金额
  305. * @return \think\response\Json|void
  306. * @throws \think\db\exception\DbException
  307. */
  308. private function payReTag($tagId,$code,$total_fee){
  309. $pay=Pay::where(['payNo'=>$code,'is_del'=>0])->findOrEmpty();
  310. if($pay->isEmpty())throw new \Exception("未找到对账单数据");
  311. if($tagId==1){
  312. if($pay['pay_tag_fee']<$total_fee)throw new \Exception('对账单付款标签金额不足');
  313. $pay->pay_tag_fee=$pay->pay_tag_fee-$total_fee;
  314. $pay->wpay_fee=$pay->wpay_fee+$total_fee;
  315. $pay->pay_status = $pay->wpay_fee==0 && $pay->pay_fee==0?3:($pay->apay_fee==0?1:2);
  316. $pay->pay_tag=0;
  317. }
  318. if($tagId==2){
  319. if($pay['inv_tag_fee']<$total_fee)throw new \Exception('对账单回票标签金额不足');
  320. $pay->inv_tag_fee=$pay->inv_tag_fee-$total_fee;
  321. $pay->winv_fee=$pay->winv_fee+$total_fee;
  322. $pay->inv_status = $pay->winv_fee==0 && $pay->inv_fee==0?3:($pay->ainv_fee==0?1:2);
  323. $pay->inv_tag=0;
  324. }
  325. $resulr= $pay->save();
  326. if($resulr==false)throw new \Exception("对账单更新失败");
  327. }
  328. /**
  329. * @param $tagId 标签类型 3 回款4开票
  330. * @param $code 销售单编号
  331. * @param $total_fee 标签金额
  332. * @return \think\response\Json|void
  333. * @throws \think\db\exception\DbException
  334. */
  335. private function qrdTag($tagId,$code,$total_fee){
  336. $qrd=Db::name("qrd_info")->where(["sequenceNo"=>$code,"is_del"=>0])->findOrEmpty();
  337. if(empty($qrd)){
  338. return error_show(1004,"未找到销售单数据");
  339. }
  340. $update=["updatetime"=>date("Y-m-d H:i:s")];
  341. if($tagId==3){
  342. if($qrd['wpay_fee']+$qrd['pay_tag_fee']<$total_fee){
  343. return error_show(1004,"对账单未付金额不足");
  344. }
  345. $update['wpay_fee']=$qrd['wpay_fee']+$qrd['pay_tag_fee']-$total_fee;
  346. $status = $update['wpay_fee']==0 &&$qrd['pay_fee']==0 ? 3:($qrd['apay_fee']==0?1:2);
  347. $update['pay_tag_fee'] = $total_fee;
  348. $update['pay_status'] = $status;
  349. $update['pay_tag'] = 1;
  350. }
  351. if($tagId==4){
  352. if($qrd['winv_fee']+$qrd['inv_tag_fee']<$total_fee){
  353. return error_show(1004,"对账单未开票金额不足");
  354. }
  355. $update['winv_fee']=$qrd['winv_fee']+$qrd['inv_tag_fee']-$total_fee;
  356. $status = $update['winv_fee']==0 &&$qrd['inv_fee']==0 ? 3:($qrd['ainv_fee']==0?1:2);
  357. $update['inv_tag_fee'] = $total_fee;
  358. $update['inv_status'] = $status;
  359. $update['inv_tag'] = 1;
  360. }
  361. $resulr= Db::name("qrd_info")->where($qrd)->update($update);
  362. return $resulr? app_show(0,"标签添加成功"):error_show(1004,"标签添加失败");
  363. }
  364. /** 解除标签销售单
  365. * @param $tagId 标签类型 3 回款4开票
  366. * @param $code 销售单编号
  367. * @param $total_fee 标签金额
  368. * @return \think\response\Json|void
  369. * @throws \think\db\exception\DbException
  370. */
  371. private function qrdReTag($tagId,$code,$total_fee){
  372. $qrd=QrdInfo::where(['sequenceNo'=>$code,'is_del'=>0])->findOrEmpty();
  373. if($qrd->isEmpty())throw new \Exception('未找到销售单数据');
  374. if($tagId==3){
  375. if($qrd->pay_tag_fee<$total_fee)throw new \Exception('销售单回款标签金额不足');
  376. $qrd->pay_tag_fee=$qrd->pay_tag_fee-$total_fee;
  377. $qrd->wpay_fee=$qrd->wpay_fee+$total_fee;
  378. $qrd->pay_status = $qrd->wpay_fee==0 && $qrd->pay_fee==0?3:($qrd->apay_fee==0?1:2);
  379. $qrd->pay_tag=0;
  380. }
  381. if($tagId==4){
  382. if($qrd->pay_tag_fee<$total_fee)throw new \Exception('销售单开票标签金额不足');
  383. $qrd->inv_tag_fee=$qrd->inv_tag_fee-$total_fee;
  384. $qrd->winv_fee=$qrd->winv_fee+$total_fee;
  385. $qrd->inv_status = $qrd->winv_fee==0 && $qrd->inv_fee==0?3:($qrd->ainv_fee==0?1:2);
  386. $qrd->inv_tag=0;
  387. }
  388. $resulr= $qrd->save();
  389. if($resulr==false)throw new \Exception('销售单更新失败');
  390. }
  391. }