InvCat.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\BaseController;
  4. use think\App;
  5. use think\Exception;
  6. use think\facade\Db;
  7. use think\facade\Validate;
  8. class InvCat extends BaseController{
  9. private $Tax=[1=>"免税",2=>"不征税",3=>"零税率"];
  10. public function __construct(App $app) {parent::__construct($app);}
  11. //商品关联开票类目
  12. public function AddGood(){
  13. $spuCode=isset($this->post['spuCode'])&&$this->post['spuCode']!=''?trim($this->post['spuCode']):"";
  14. if($spuCode==''){
  15. return error_show(1004,"参数 spuCode 不能为空");
  16. }
  17. $goodinfo =Db::name("good")->where(["spuCode"=>$spuCode])->findOrEmpty();
  18. if(empty($goodinfo)) return error_show(1004,"商品数据未找到");
  19. $inv_good_name = isset($this->post['inv_good_name'])&&$this->post['inv_good_name']!=''?trim($this->post['inv_good_name']):"";
  20. if($inv_good_name==''){
  21. return error_show(1004,"参数 inv_good_name 不能为空");
  22. }
  23. $tax = isset($this->post['tax'])&&$this->post['tax']!=''?trim($this->post['tax']):"";
  24. if($tax=="") return error_show(1004,"参数 tax 不能为空");
  25. $cat_code = isset($this->post['cat_code'])&&$this->post['cat_code']!=''?trim($this->post['cat_code']):"";
  26. if($cat_code==''){
  27. return error_show(1004,"参数 cat_code 不能为空");
  28. }
  29. $catinfo=Db::name("inv_cat")->where(["cat_code"=>$cat_code])->findOrEmpty();
  30. if(empty($catinfo)) return error_show(1004,"未找到对应的开票类目");
  31. $taxArr=$catinfo['tax']!=''? explode("、",$catinfo['tax']):[];
  32. if(!in_array($tax,$taxArr))return error_show(1004,"未找到对应的开票类目税率");
  33. $tax=str_replace("%","",$tax)/100;
  34. $invTag=isset($this->post['inv_tag'])&& $this->post['inv_tag']!==''?intval($this->post['inv_tag']):0;
  35. $is_discount=isset($this->post['is_discount'])&& $this->post['is_discount']!==''?intval($this->post['is_discount']):0;
  36. $addTax=isset($this->post['addTax'])&& $this->post['addTax']!==''?trim($this->post['addTax']):'';
  37. if($is_discount==1){
  38. // 1.如果YHZCBS为1, 则ZZSTSGL必须填; 如果YHZCBS为0,ZZSTSGL不填。
  39. // 2.如果YHZCBS为1, 且税率为0, 则LSLBS只能根据实际情况选择"1或2"中的一种, 不能选择3, 且ZZSTSGL内容也只能写与1/2对应的"免税/不征税";
  40. // 3.如果税率为0,但并不属于优惠政策(即普通的零税率),则YHZCBS填0,LSLBS填3,ZZSTSGL为空;
  41. // 4.如果税率不为0, 但属于优惠政策,则YHZCBS填1,LSLBS填空或不填,ZZSTSGL根据实际情况填写;
  42. // 5.如果税率不为0, 且不属于优惠政策, 则YHZCBS填0,LSLBS填空或不填,ZZSTSGL不填或空.
  43. // 优惠政策下 税率标识只能选择 0非零说率 1免税 2不征税 增值税管理为空"出口零税/免税/不征税"
  44. if($tax==0){
  45. if($invTag==3){
  46. return error_show(1004,"税率标识不能选择零税率");
  47. }
  48. if($addTax==''){
  49. return error_show(1004,"参数 addTax 不能为空");
  50. }
  51. if($addTax!=$this->Tax[$invTag]){
  52. return error_show(1004,"税率标识与增值税管理内容不符");
  53. }
  54. }
  55. }else{
  56. // 非优惠政策下 零税率 税率标识只能选择 3 普通零税率 增值税管理为空
  57. // 非优惠政策下 非零税率 税率标识只能选择 0 非零税率 增值税管理为空
  58. if($tax==0 && $invTag!=3){
  59. return error_show(1004,"税率标识只能选择零税率");
  60. }
  61. if($tax!=0 && $invTag!=0){
  62. return error_show(1004,"税率标识只能选择非零税率");
  63. }
  64. $addTax='';
  65. }
  66. $data=[
  67. "inv_cat_name"=>$catinfo['short_name'],
  68. "inv_cat_code"=>$catinfo['merge_code'],
  69. "inv_tax"=>$tax,
  70. "inv_good_name"=>$inv_good_name,
  71. "inv_tag"=>$invTag,
  72. "is_discount"=>$is_discount,
  73. "addTax"=>$addTax,
  74. "status"=>1,
  75. "updatetime"=>date("Y-m-d H:i:s")
  76. ];
  77. $up =Db::name("good")->where($goodinfo)->update($data);
  78. return $up? app_show(0,"添加成功"):error_show(1004,"添加失败");
  79. }
  80. //商品列表
  81. public function GoodList(){
  82. $page =isset($this->post['page'])&& $this->post['page']!="" ? intval($this->post['page']) :1;
  83. $size =isset($this->post['size'])&& $this->post['size']!="" ? intval($this->post['size']) :15;
  84. $condition =[];
  85. $status =isset($this->post['status'])&&$this->post['status']!==''?intval($this->post['status']):"";
  86. if($status!==''){
  87. $condition[]=["status","=",$status];
  88. }
  89. $isZx =isset($this->post['isZx'])&&$this->post['isZx']!==''?intval($this->post['isZx']):"";
  90. if($isZx!==''){
  91. $condition[]=["isZx","=",$isZx];
  92. }
  93. $spuCode=isset($this->post['spuCode'])&&$this->post['spuCode']!=''?trim($this->post['spuCode']):"";
  94. if($spuCode!="") $condition[]=["spuCode","like","%$spuCode%"];
  95. $good_name=isset($this->post['good_name'])&&$this->post['good_name']!=''?trim($this->post['good_name']):"";
  96. if($good_name!="") $condition[]=["good_name","like","%$good_name%"];
  97. $companyNo=isset($this->post['companyNo'])&&$this->post['companyNo']!=''?trim($this->post['companyNo']):"";
  98. if($companyNo!="") $condition[]=["companyNo","like","%$companyNo%"];
  99. $supplierNo=isset($this->post['supplierNo'])&&$this->post['supplierNo']!=''?trim($this->post['supplierNo']):"";
  100. if($supplierNo!="") $condition[]=["supplierNo","like","%$supplierNo%"];
  101. $creater=isset($this->post['creater'])&&$this->post['creater']!=''?trim($this->post['creater']):"";
  102. if($creater!="") $condition[]=["creater","like","%$creater%"];
  103. $count=Db::name("good")->where($condition)->count();
  104. $total=ceil($count/$size);
  105. $page = $page>=$total? intval($total):$page;
  106. $list =Db::name("good")->where($condition)->order("id desc")->page($page,$size)->select()->toArray();
  107. foreach ($list as &$value){
  108. $company =Db::name("supplier_info")->where(["code"=>$value['companyNo']])->find();
  109. $value['companyName']=$company['name']??"";
  110. $supplier =Db::name("supplier_info")->where(["code"=>$value["supplierNo"]])->find();
  111. $value["supplierName"]=$supplier["name"]??"";
  112. }
  113. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  114. }
  115. //商品详情
  116. public function goodinfo(){
  117. $spuCode=isset($this->post['spuCode'])&&$this->post['spuCode']!=''?trim($this->post['spuCode']):"";
  118. if($spuCode=="") return error_show(1004,"参数 spuCode 不能为空");
  119. $goodinfo =Db::name("good")->where(["spuCode"=>$spuCode])->findOrEmpty();
  120. if(empty($goodinfo))return error_show(1004,"商品数据未找到");
  121. $company =Db::name("company_info")->where(["companyNo"=>$goodinfo['companyNo']])->find();
  122. $goodinfo['companyName']=$company['company_name']??"";
  123. $supplier =Db::name("supplier_info")->where(["code"=>$goodinfo["supplierNo"]])->find();
  124. $goodinfo["supplierName"]=$supplier["name"]??"";
  125. return app_show(0,"获取成功",$goodinfo);
  126. }
  127. //发票类目列表
  128. public function catlist(){
  129. $page =isset($this->post['page'])&& $this->post['page']!="" ? intval($this->post['page']) :1;
  130. $size =isset($this->post['size'])&& $this->post['size']!="" ? intval($this->post['size']) :15;
  131. $condition =[];
  132. $cat_name = isset($this->post['cat_name'])&&$this->post['cat_name']!=''?trim($this->post['cat_name']):"";
  133. if($cat_name!=''){
  134. $condition[]=["cat_name|short_name","like","%$cat_name%"];
  135. }
  136. $cat_code = isset($this->post['cat_code'])&&$this->post['cat_code']!=''?trim($this->post['cat_code']):"";
  137. if($cat_code!=''){
  138. $condition[]=["cat_code|merge_code","like","%$cat_code%"];
  139. }
  140. $status =isset($this->post['status'])&&$this->post['status']!==''?intval($this->post['status']):"";
  141. if($status!==''){
  142. $condition[]=["status","=",$status];
  143. }
  144. $count =Db::name("inv_cat")->where($condition)->count();
  145. $total=ceil($count/$size);
  146. $page = $page>=$total? intval($total):$page;
  147. $list =Db::name("inv_cat")->where($condition)->order("id desc")->page($page,$size)->select()->toArray();
  148. foreach ($list as &$value){
  149. $value['tax'] = $value['tax']==''?[]:explode("、",$value['tax']);
  150. }
  151. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  152. }
  153. //类目查询
  154. public function query(){
  155. $condition =[["tax","<>",""]];
  156. $cat_name = isset($this->post['cat_name'])&&$this->post['cat_name']!=''?trim($this->post['cat_name']):"";
  157. if($cat_name!=''){
  158. $condition[]=["cat_name","like","%$cat_name%"];
  159. }
  160. $cat_code = isset($this->post['cat_code'])&&$this->post['cat_code']!=''?trim($this->post['cat_code']):"";
  161. if($cat_code!=''){
  162. $condition[]=["cat_code|merge_code","like","%$cat_code%"];
  163. }
  164. $list =Db::name("inv_cat")->where($condition)
  165. ->field("cat_name,cat_code,status,tax,addtax,sumitem,`desc`,short_name,merge_code,LENGTH(cat_name) as weight")
  166. ->order("weight asc,addtime desc")
  167. ->limit(30)->select()->toArray();
  168. foreach ($list as &$value){
  169. $value['tax'] = $value['tax']==''?[]:explode("、",$value['tax']);
  170. }
  171. return app_show(0,"获取成功",$list);
  172. }
  173. //批量设置类目信息
  174. public function addGoodBatch()
  175. {
  176. $list = $this->request->post('list/a', [], 'trim');
  177. $val = Validate::rule([
  178. 'spuCode|商品编号' => 'require|max:255',
  179. 'tax|税率' => 'require|max:4',
  180. 'cat_code|类目编号简写' => 'require|max:255',
  181. 'inv_good_name|开票商品名称' => 'require|max:255',
  182. 'inv_tag|税率标识' => 'require|between:0,3',
  183. 'is_discount|是否有优惠政策' => 'require|in:0,1',
  184. 'addTax|增值税管理内容' => 'max:255',
  185. ]);
  186. Db::startTrans();
  187. try {
  188. $all_spuCode = Db::name('good')
  189. ->whereIn('spuCode', array_column($list, 'spuCode'))
  190. ->column('id', 'spuCode');
  191. $all_cat_code = Db::name('inv_cat')
  192. ->whereIn('cat_code', array_column($list, 'cat_code'))
  193. ->column('tax,merge_code,short_name', 'cat_code');
  194. $date = date('Y-m-d H:i:s');
  195. foreach ($list as $value) {
  196. if (!$val->check($value)) throw new Exception($val->getError());
  197. if (!isset($all_spuCode[$value['spuCode']])) throw new Exception($value['spuCode'].'商品数据未找到');
  198. if (!isset($all_cat_code[$value['cat_code']])) throw new Exception($value['cat_code'].'未找到对应的开票类目');
  199. if (!in_array($value['tax'], $all_cat_code[$value['cat_code']]['tax'] != '' ? explode('、', $all_cat_code[$value['cat_code']]['tax']) : [])) throw new Exception($value['tax'].'未找到对应的开票类目税率');
  200. $value['tax'] = str_replace('%', '', $value['tax']);
  201. $value['tax'] = round(bcdiv($value['tax'], 100, 3), 2);
  202. //
  203. // if ($value['is_discount'] == 1) {
  204. // // 优惠政策下 税率标识只能选择 0非零说率 1免税 2不征税 增值税管理为空"出口零税/免税/不征税"
  205. // if($value['tax'] == 0){
  206. // if ( $value['inv_tag'] == 3) throw new Exception($value['spuCode'].'税率标识不能选择零税率');
  207. // if ($value['addTax'] == '') throw new Exception($value['spuCode'].'参数 addTax 不能为空');
  208. // if($value['addTax']!=$this->Tax[$value['inv_tag']]){
  209. // throw new Exception($value['spuCode'].'税率标识与增值税管理内容不符');
  210. // }
  211. // }
  212. //
  213. //
  214. // } else {
  215. // // 非优惠政策下 零税率 税率标识只能选择 3 普通零税率 增值税管理为空
  216. // // 非优惠政策下 非零税率 税率标识只能选择 0 非零税率 增值税管理为空
  217. // if ($value['tax'] == 0 && $value['inv_tag'] != 3) throw new Exception($value['spuCode'].'税率标识只能选择零税率');
  218. // $value['addTax'] = '';
  219. // }
  220. if($value['is_discount'] ==1){
  221. // 1.如果YHZCBS为1, 则ZZSTSGL必须填; 如果YHZCBS为0,ZZSTSGL不填。
  222. // 2.如果YHZCBS为1, 且税率为0, 则LSLBS只能根据实际情况选择"1或2"中的一种, 不能选择3, 且ZZSTSGL内容也只能写与1/2对应的"免税/不征税";
  223. // 3.如果税率为0,但并不属于优惠政策(即普通的零税率),则YHZCBS填0,LSLBS填3,ZZSTSGL为空;
  224. // 4.如果税率不为0, 但属于优惠政策,则YHZCBS填1,LSLBS填空或不填,ZZSTSGL根据实际情况填写;
  225. // 5.如果税率不为0, 且不属于优惠政策, 则YHZCBS填0,LSLBS填空或不填,ZZSTSGL不填或空.
  226. // 优惠政策下 税率标识只能选择 0非零说率 1免税 2不征税 增值税管理为空"出口零税/免税/不征税"
  227. if($value['tax'] ==0){
  228. if($value['inv_tag']==3){
  229. throw new Exception($value['spuCode'].'税率标识不能选择零税率');
  230. }
  231. if($value['addTax']==''){
  232. throw new Exception($value['spuCode'].'参数 addTax 不能为空');
  233. }
  234. if($value['addTax']!=$this->Tax[$value['inv_tag']]){
  235. throw new Exception($value['spuCode'].'参数 addTax 不能为空');
  236. }
  237. }
  238. }else{
  239. // 非优惠政策下 零税率 税率标识只能选择 3 普通零税率 增值税管理为空
  240. // 非优惠政策下 非零税率 税率标识只能选择 0 非零税率 增值税管理为空
  241. if($value['tax'] == 0 && $value['inv_tag'] != 3){
  242. throw new Exception($value['spuCode'].'税率标识只能选择零税率');
  243. }
  244. if($value['tax']!=0 && $value['inv_tag']!=0){
  245. throw new Exception($value['spuCode']."税率标识只能选择非零税率");
  246. }
  247. $value['addTax']='';
  248. }
  249. $data = [
  250. 'inv_cat_name' => $all_cat_code[$value['cat_code']]['short_name'],
  251. 'inv_cat_code' => $all_cat_code[$value['cat_code']]['merge_code'],
  252. 'inv_tax' => $value['tax'],
  253. 'inv_good_name' => $value['inv_good_name'],
  254. 'inv_tag' => $value['inv_tag'],
  255. 'is_discount' => $value['is_discount'],
  256. 'addTax' => $value['addTax'],
  257. 'status' => 1,
  258. 'updatetime' => $date
  259. ];
  260. Db::name("good")->where('id', $all_spuCode[$value['spuCode']])->update($data);
  261. }
  262. Db::commit();
  263. return app_show(0, '添加成功');
  264. } catch (Exception $exception) {
  265. Db::rollback();
  266. return error_show(1004, '添加失败,' . $exception->getMessage());
  267. }
  268. }
  269. }