TagGood.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. Db::startTrans();
  174. try{
  175. if(!empty($taglog)){
  176. $up = Db::name("tag_log")->where($taglog)->update(["status"=>0,"updatetime"=>date("Y-m-d H:i:s")]);
  177. if($up==false){
  178. Db::rollback();
  179. return error_show(1004,"标签日志新建失败");
  180. }
  181. }
  182. if($taginfo['type']==1 || $taginfo['type']==2){
  183. $result= $this->payTag($taginfo['type'],$code,$total_fee);
  184. }
  185. if($taginfo['type']==3|| $taginfo['type']==4){
  186. $result= $this->qrdTag($taginfo['type'],$code,$total_fee);
  187. }
  188. $tagdata=[
  189. "code"=>$code,
  190. "tag_id"=>$tagid,
  191. "creater"=>$this->uname,
  192. "createrid"=>$this->uid,
  193. "tag_fee"=>$total_fee,
  194. "status"=>1,
  195. "addtime"=>date("Y-m-d H:i:s"),
  196. "updatetime"=>date("Y-m-d H:i:s")
  197. ];
  198. $in =Db::name("tag_log")->insert($tagdata);
  199. if($in==false){
  200. Db::rollback();
  201. return error_show(1004,"标签日志新建失败");
  202. }
  203. $data =$result->getData();
  204. if($data['code']==0){
  205. Db::commit();
  206. return app_show(0,"标签添加成功");
  207. }
  208. Db::rollback();
  209. return error_show($data['code'],$data['message']);
  210. }catch (\Exception $e){
  211. Db::rollback();
  212. return error_show(1004,$e->getMessage());
  213. }
  214. }
  215. /**
  216. * @param $tagId 标签类型 1 付款2回票
  217. * @param $code 对账单编号
  218. * @param $total_fee 标签金额
  219. * @return \think\response\Json|void
  220. * @throws \think\db\exception\DbException
  221. */
  222. private function payTag($tagId,$code,$total_fee){
  223. $pay=Db::name("pay")->where(["payNo"=>$code,"is_del"=>0])->findOrEmpty();
  224. if(empty($pay)){
  225. return error_show(1004,"未找到对账单数据");
  226. }
  227. $update=["updatetime"=>date("Y-m-d H:i:s")];
  228. if($tagId==1){
  229. if($pay['wpay_fee']+$pay['pay_tag_fee']<$total_fee){
  230. return error_show(1004,"对账单未付金额不足");
  231. }
  232. $update['wpay_fee']=$pay['wpay_fee']+$pay['pay_tag_fee']-$total_fee;
  233. $status = $update['wpay_fee']==0 &&$pay['pay_fee']==0 ? 3:($pay['apay_fee']==0?1:2);
  234. $update['pay_tag_fee'] = $total_fee;
  235. $update['pay_status'] = $status;
  236. $update['pay_tag'] = 1;
  237. }
  238. if($tagId==2){
  239. if($pay['winv_fee']+$pay['inv_tag_fee']<$total_fee){
  240. return error_show(1004,"对账单未付金额不足");
  241. }
  242. $update['winv_fee']=$pay['winv_fee']+$pay['inv_tag_fee']-$total_fee;
  243. $status = $update['winv_fee']==0 &&$pay['inv_fee']==0 ? 3:($pay['ainv_fee']==0?1:2);
  244. $update['inv_tag_fee'] = $total_fee;
  245. $update['inv_status'] = $status;
  246. $update['inv_tag'] = 1;
  247. }
  248. $resulr= Db::name("pay")->where($pay)->update($update);
  249. return $resulr? app_show(0,"标签添加成功"):error_show(1004,"标签添加失败");
  250. }
  251. /**
  252. * @param $tagId 标签类型 3 回款4开票
  253. * @param $code 销售单编号
  254. * @param $total_fee 标签金额
  255. * @return \think\response\Json|void
  256. * @throws \think\db\exception\DbException
  257. */
  258. private function qrdTag($tagId,$code,$total_fee){
  259. $qrd=Db::name("qrd_info")->where(["sequenceNo"=>$code,"is_del"=>0])->findOrEmpty();
  260. if(empty($qrd)){
  261. return error_show(1004,"未找到销售单数据");
  262. }
  263. $update=["updatetime"=>date("Y-m-d H:i:s")];
  264. if($tagId==3){
  265. if($qrd['wpay_fee']+$qrd['pay_tag_fee']<$total_fee){
  266. return error_show(1004,"对账单未付金额不足");
  267. }
  268. $update['wpay_fee']=$qrd['wpay_fee']+$qrd['pay_tag_fee']-$total_fee;
  269. $status = $update['wpay_fee']==0 &&$qrd['pay_fee']==0 ? 3:($qrd['apay_fee']==0?1:2);
  270. $update['pay_tag_fee'] = $total_fee;
  271. $update['pay_status'] = $status;
  272. $update['pay_tag'] = 1;
  273. }
  274. if($tagId==4){
  275. if($qrd['winv_fee']+$qrd['inv_tag_fee']<$total_fee){
  276. return error_show(1004,"对账单未付金额不足");
  277. }
  278. $update['winv_fee']=$qrd['winv_fee']+$qrd['inv_tag_fee']-$total_fee;
  279. $status = $update['winv_fee']==0 &&$qrd['inv_fee']==0 ? 3:($qrd['ainv_fee']==0?1:2);
  280. $update['inv_tag_fee'] = $total_fee;
  281. $update['inv_status'] = $status;
  282. $update['inv_tag'] = 1;
  283. }
  284. $resulr= Db::name("qrd_info")->where($qrd)->update($update);
  285. return $resulr? app_show(0,"标签添加成功"):error_show(1004,"标签添加失败");
  286. }
  287. }