TagGood.php 13 KB

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