InvCat.php 14 KB

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