TagGood.php 14 KB

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