InvCat.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. 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']!==''?trim($this->post['addTax']):'';
  36. if($is_discount==1){
  37. // 优惠政策下 税率标识只能选择 0非零说率 1免税 2不征税 增值税管理为空"出口零税/免税/不征税"
  38. if($tax==0 && $invTag==3){
  39. return error_show(1004,"税率标识不能选择零税率");
  40. }
  41. if($addTax==''){
  42. return error_show(1004,"参数 addTax 不能为空");
  43. }
  44. }else{
  45. // 非优惠政策下 零税率 税率标识只能选择 3 普通零税率 增值税管理为空
  46. // 非优惠政策下 非零税率 税率标识只能选择 0 非零税率 增值税管理为空
  47. if($tax==0 && $invTag!=3){
  48. return error_show(1004,"税率标识只能选择零税率");
  49. }
  50. $addTax='';
  51. }
  52. $data=[
  53. "inv_cat_name"=>$catinfo['short_name'],
  54. "inv_cat_code"=>$catinfo['merge_code'],
  55. "inv_tax"=>$tax,
  56. "inv_good_name"=>$inv_good_name,
  57. "inv_tag"=>$invTag,
  58. "is_discount"=>$is_discount,
  59. "addTax"=>$addTax,
  60. "status"=>1,
  61. "updatetime"=>date("Y-m-d H:i:s")
  62. ];
  63. $up =Db::name("good")->where($goodinfo)->update($data);
  64. return $up? app_show(0,"添加成功"):error_show(1004,"添加失败");
  65. }
  66. //商品列表
  67. public function GoodList(){
  68. $page =isset($this->post['page'])&& $this->post['page']!="" ? intval($this->post['page']) :1;
  69. $size =isset($this->post['size'])&& $this->post['size']!="" ? intval($this->post['size']) :15;
  70. $condition =[];
  71. $status =isset($this->post['status'])&&$this->post['status']!==''?intval($this->post['status']):"";
  72. if($status!==''){
  73. $condition[]=["status","=",$status];
  74. }
  75. $isZx =isset($this->post['isZx'])&&$this->post['isZx']!==''?intval($this->post['isZx']):"";
  76. if($isZx!==''){
  77. $condition[]=["isZx","=",$isZx];
  78. }
  79. $spuCode=isset($this->post['spuCode'])&&$this->post['spuCode']!=''?trim($this->post['spuCode']):"";
  80. if($spuCode!="") $condition[]=["spuCode","like","%$spuCode%"];
  81. $good_name=isset($this->post['good_name'])&&$this->post['good_name']!=''?trim($this->post['good_name']):"";
  82. if($good_name!="") $condition[]=["good_name","like","%$good_name%"];
  83. $companyNo=isset($this->post['companyNo'])&&$this->post['companyNo']!=''?trim($this->post['companyNo']):"";
  84. if($companyNo!="") $condition[]=["companyNo","like","%$companyNo%"];
  85. $supplierNo=isset($this->post['supplierNo'])&&$this->post['supplierNo']!=''?trim($this->post['supplierNo']):"";
  86. if($supplierNo!="") $condition[]=["supplierNo","like","%$supplierNo%"];
  87. $creater=isset($this->post['creater'])&&$this->post['creater']!=''?trim($this->post['creater']):"";
  88. if($creater!="") $condition[]=["creater","like","%$creater%"];
  89. $count=Db::name("good")->where($condition)->count();
  90. $total=ceil($count/$size);
  91. $page = $page>=$total? intval($total):$page;
  92. $list =Db::name("good")->where($condition)->order("addtime desc")->page($page,$size)->select()->toArray();
  93. foreach ($list as &$value){
  94. $company =Db::name("supplier_info")->where(["code"=>$value['companyNo']])->find();
  95. $value['companyName']=$company['name']??"";
  96. $supplier =Db::name("supplier_info")->where(["code"=>$value["supplierNo"]])->find();
  97. $value["supplierName"]=$supplier["name"]??"";
  98. }
  99. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  100. }
  101. //商品详情
  102. public function goodinfo(){
  103. $spuCode=isset($this->post['spuCode'])&&$this->post['spuCode']!=''?trim($this->post['spuCode']):"";
  104. if($spuCode=="") return error_show(1004,"参数 spuCode 不能为空");
  105. $goodinfo =Db::name("good")->where(["spuCode"=>$spuCode])->findOrEmpty();
  106. if(empty($goodinfo))return error_show(1004,"商品数据未找到");
  107. $company =Db::name("company_info")->where(["companyNo"=>$goodinfo['companyNo']])->find();
  108. $goodinfo['companyName']=$company['company_name']??"";
  109. $supplier =Db::name("supplier_info")->where(["code"=>$goodinfo["supplierNo"]])->find();
  110. $goodinfo["supplierName"]=$supplier["name"]??"";
  111. return app_show(0,"获取成功",$goodinfo);
  112. }
  113. //发票类目列表
  114. public function catlist(){
  115. $page =isset($this->post['page'])&& $this->post['page']!="" ? intval($this->post['page']) :1;
  116. $size =isset($this->post['size'])&& $this->post['size']!="" ? intval($this->post['size']) :15;
  117. $condition =[];
  118. $cat_name = isset($this->post['cat_name'])&&$this->post['cat_name']!=''?trim($this->post['cat_name']):"";
  119. if($cat_name!=''){
  120. $condition[]=["cat_name|short_name","like","%$cat_name%"];
  121. }
  122. $cat_code = isset($this->post['cat_code'])&&$this->post['cat_code']!=''?trim($this->post['cat_code']):"";
  123. if($cat_code!=''){
  124. $condition[]=["cat_code|merge_code","like","%$cat_code%"];
  125. }
  126. $status =isset($this->post['status'])&&$this->post['status']!==''?intval($this->post['status']):"";
  127. if($status!==''){
  128. $condition[]=["status","=",$status];
  129. }
  130. $count =Db::name("inv_cat")->where($condition)->count();
  131. $total=ceil($count/$size);
  132. $page = $page>=$total? intval($total):$page;
  133. $list =Db::name("inv_cat")->where($condition)->order("addtime desc")->page($page,$size)->select()->toArray();
  134. foreach ($list as &$value){
  135. $value['tax'] = $value['tax']==''?[]:explode("、",$value['tax']);
  136. }
  137. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  138. }
  139. //类目查询
  140. public function query(){
  141. $condition =[["tax","<>",""]];
  142. $cat_name = isset($this->post['cat_name'])&&$this->post['cat_name']!=''?trim($this->post['cat_name']):"";
  143. if($cat_name!=''){
  144. $condition[]=["short_name","like","%$cat_name%"];
  145. }
  146. $cat_code = isset($this->post['cat_code'])&&$this->post['cat_code']!=''?trim($this->post['cat_code']):"";
  147. if($cat_code!=''){
  148. $condition[]=["cat_code|merge_code","like","%$cat_code%"];
  149. }
  150. $list =Db::name("inv_cat")->where($condition)->order("addtime desc")->limit(30)->select()->toArray();
  151. foreach ($list as &$value){
  152. $value['tax'] = $value['tax']==''?[]:explode("、",$value['tax']);
  153. }
  154. return app_show(0,"获取成功",$list);
  155. }
  156. //批量设置类目信息
  157. public function addGoodBatch()
  158. {
  159. $list = $this->request->post('list/a', [], 'trim');
  160. $val = Validate::rule([
  161. 'spuCode|商品编号' => 'require|max:255',
  162. 'tax|税率' => 'require|max:4',
  163. 'cat_code|类目编号简写' => 'require|max:255',
  164. 'inv_good_name|开票商品名称' => 'require|max:255',
  165. 'inv_tag|税率标识' => 'require|between:0,3',
  166. 'is_discount|是否有优惠政策' => 'require|in:0,1',
  167. 'addTax|增值税管理内容' => 'require|max:255',
  168. ]);
  169. Db::startTrans();
  170. try {
  171. $all_spuCode = Db::name('good')
  172. ->whereIn('spuCode', array_column($list, 'spuCode'))
  173. ->column('id', 'spuCode');
  174. $all_cat_code = Db::name('inv_cat')
  175. ->whereIn('cat_code', array_column($list, 'cat_code'))
  176. ->column('tax,merge_code,short_name', 'cat_code');
  177. $date = date('Y-m-d H:i:s');
  178. foreach ($list as $value) {
  179. if (!$val->check($value)) throw new Exception($val->getError());
  180. if (!isset($all_spuCode[$value['spuCode']])) throw new Exception('商品数据未找到');
  181. if (!isset($all_cat_code[$value['cat_code']])) throw new Exception('未找到对应的开票类目');
  182. if (!in_array($value['tax'], $all_cat_code[$value['cat_code']]['tax'] != '' ? explode('、', $all_cat_code[$value['cat_code']]['tax']) : [])) throw new Exception('未找到对应的开票类目税率');
  183. $value['tax'] = str_replace('%', '', $value['tax']);
  184. $value['tax'] = round(bcdiv($value['tax'], 100, 3), 2);
  185. if ($value['is_discount'] == 1) {
  186. // 优惠政策下 税率标识只能选择 0非零说率 1免税 2不征税 增值税管理为空"出口零税/免税/不征税"
  187. if ($value['tax'] == 0 && $value['inv_tag'] == 3) throw new Exception('税率标识不能选择零税率');
  188. if ($value['addTax'] == '') throw new Exception('参数 addTax 不能为空');
  189. } else {
  190. // 非优惠政策下 零税率 税率标识只能选择 3 普通零税率 增值税管理为空
  191. // 非优惠政策下 非零税率 税率标识只能选择 0 非零税率 增值税管理为空
  192. if ($value['tax'] == 0 && $value['inv_tag'] != 3) throw new Exception('税率标识只能选择零税率');
  193. $value['addTax'] = '';
  194. }
  195. $data = [
  196. 'inv_cat_name' => $all_cat_code[$value['cat_code']]['short_name'],
  197. 'inv_cat_code' => $all_cat_code[$value['cat_code']]['merge_code'],
  198. 'inv_tax' => $value['tax'],
  199. 'inv_good_name' => $value['inv_good_name'],
  200. 'inv_tag' => $value['inv_tag'],
  201. 'is_discount' => $value['is_discount'],
  202. 'addTax' => $value['addTax'],
  203. 'status' => 1,
  204. 'updatetime' => $date
  205. ];
  206. Db::name("good")->where('id', $all_spuCode[$value['spuCode']])->update($data);
  207. }
  208. Db::commit();
  209. return app_show(0, '添加成功');
  210. } catch (Exception $exception) {
  211. Db::rollback();
  212. return error_show(1004, '添加失败,' . $exception->getMessage());
  213. }
  214. }
  215. }