TagGood.php 14 KB

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