Good.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\controller;
  4. use app\admin\model\GoodChange;use app\admin\model\GoodCombind;use think\App;use think\facade\Validate;
  5. class Good extends Base
  6. {
  7. protected $model=null;
  8. public function __construct(App $app) {
  9. parent::__construct($app);
  10. $this->model=new \app\admin\model\Good();
  11. }
  12. /** 商品列表
  13. * @throws \think\db\exception\DbException
  14. * @throws \think\exception\DbException
  15. */
  16. public function list(){
  17. $param=$this->request->param(["spuCode"=>"","status"=>"","good_name"=>"",'companyNo'=>'','relaComNo'=>'',
  18. 'supplierNo'=>'','cgd_supplierNo'=>'','creater'=>'','isZx'=>'','isChild'=>0,'isCombind'=>'','isChange'=>'',"cat_diff"=>"",
  19. "tax_diff"=>"","is_diff"=>"","start"=>"",'end'=>'','page'=>'1','size'=>'15'],"post","trim");
  20. $where=[];
  21. $param["spuCode"]==""?: $where[]=['spuCode','like',"%{$param['spuCode']}%"];
  22. $param["good_name"]==""?: $where[]=['good_name','like',"%{$param['good_name']}%"];
  23. $param["companyNo"]==""?: $where[]=['companyNo','like',"%{$param['companyNo']}%"];
  24. $param["relaComNo"]==""?: $where[]=['companyNo|supplierNo','like',"%{$param['relaComNo']}%"];
  25. $param["supplierNo"]==""?: $where[]=['supplierNo|cgd_supplierNo','like',"%{$param['supplierNo']}%"];
  26. $param['cgd_supplierNo']==''?: $where[]=['cgd_supplierNo','like',"%{$param['cgd_supplierNo']}%"];
  27. $param["creater"]==""?: $where[]=['creater','like',"%{$param['creater']}%"];
  28. $param["status"]===""?: $where[]=['status','=',$param['status']];
  29. $param["isZx"]===""?: $where[]=['isZx','=',$param['isZx']];
  30. $param["isChild"]==""?: $where[]=['isChild','=',$param['isChild']];
  31. $param["isCombind"]==""?: $where[]=['isCombind','=',$param['isCombind']];
  32. $param["isChange"]===""?: $where[]=['isChange','=',$param['isChange']];
  33. $param["is_diff"]==""?: $where[]=['cat_diff|tax_diff','=',2];
  34. $param["cat_diff"]==""?: $where[]=['cat_diff','=',$param['cat_diff']];
  35. $param["tax_diff"]==""?: $where[]=['tax_diff','=',$param['tax_diff']];
  36. $param["start"]==""?: $where[]=['addtime','>=',date("Y-m-d 00:00:00",strtotime($param['start']))];
  37. $param["end"]==""?: $where[]=['addtime','<=',date("Y-m-d 23:59:59",strtotime($param['end']))];
  38. $list= $this->model
  39. ->where($where)
  40. ->order("id desc")
  41. ->paginate(["page"=>$param['page'],"list_rows"=>$param["size"]]);
  42. $this->success("获取成功",["list"=>$list->items(),"count"=>$list->total()]);
  43. }
  44. //批量更新数据状态审核
  45. public function exam(){
  46. $param=$this->request->param(["list"=>[],"status"=>""],"post","trim");
  47. $valid=Validate::rule(["list|审核数据集"=>"require|array","status|状态"=>"require|number|in:1,2"]);
  48. if($valid->check($param)==false)$this->error($valid->getError());
  49. $validList=Validate::rule([
  50. 'spuCode|商品编号' => 'require|max:255',
  51. 'tax|税率' => 'require|max:4',
  52. 'cat_code|类目编号简写' => 'require|max:255',
  53. 'inv_good_name|开票商品名称' => 'require|max:255',
  54. 'inv_tag|税率标识' => 'requireIf:status,2|between:0,3',
  55. 'is_discount|是否有优惠政策' => 'requireIf:status,2|in:0,1',
  56. 'addTax|增值税管理内容' => 'max:255']);
  57. $goodUpdate=[];
  58. $catArr = \app\admin\model\InvCat::where(["merge_code"=>array_unique(array_column($param['list'],"cat_code"))])
  59. ->column('tax,merge_code,short_name', 'merge_code');
  60. foreach ($param['list'] as $item){
  61. $temp=[];
  62. $item['status'] = $param['status'];
  63. if($validList->check($item)==false) $this->error("商品{$item['spuCode']}".$validList->getError());
  64. $good=$this->model->where(["spuCode"=>$item['spuCode']])->findOrEmpty();
  65. if($good->isEmpty())$this->error("商品信息{$item['spuCode']}不存在");
  66. if(!isset($catArr[$item['cat_code']]))$this->error("商品{$item['spuCode']}所选类目不存在");
  67. $tax = $catArr[$item['cat_code']]['tax']==""?[]:explode('、', $catArr[$item['cat_code']]['tax']);
  68. if(!in_array($item['tax'],$tax) && $param['status']==2 && $good['companyNo'] =='GS2203161855277894')$this->error("商品{$item['spuCode']}所选税率与类目包含税率不一致");
  69. //采购端修改 只能在待财务设置税务类目状态 切该商品未参与采购付款回票对账
  70. //销售端修改 只要没参与销售回款开票结算
  71. $temp['id']=$good->id;
  72. $temp['status']=$param['status'];
  73. if($param['status']<$good->status) $this->error("商品{$item['spuCode']}当前状态".\app\admin\model\Good::$status[$good->status]."不可修改");
  74. $tax= $item['tax'];
  75. $item['tax'] = bcdiv(str_replace('%', '', $item['tax']), "100", 2);
  76. if($param['status']==1){
  77. $temp["cgd_inv_cat_code"]=$catArr[$item['cat_code']]['merge_code'];
  78. $temp["cgd_inv_cat_name"]=$catArr[$item['cat_code']]['short_name'];
  79. $temp["cgd_inv_good_name"]= $item['inv_good_name'];
  80. $temp["cgd_inv_tax"]= $item['tax'];
  81. }
  82. if($param['status']==2){
  83. $check = CheckTax($item['is_discount'],$item['tax'],$item['inv_tag'],$item['addTax'],$message);
  84. if($check==false)$this->error("商品{$item['spuCode']}{$message}");
  85. $temp['inv_cat_code']=$catArr[$item['cat_code']]['merge_code'];
  86. $temp['inv_cat_name']=$catArr[$item['cat_code']]['short_name'];
  87. $temp['inv_good_name']= $item['inv_good_name'];
  88. $temp['inv_tax']= $item['tax'];
  89. $temp['is_discount']= $item['is_discount'];
  90. $temp['addTax']= $item['addTax'];
  91. $temp['inv_tag']= $item['inv_tag'];
  92. $temp['tax_diff']= $tax==$good->cgd_inv_tax?1:2;
  93. $temp['cat_diff']=intval($catArr[$item['cat_code']]['merge_code'])==intval($good->cgd_inv_cat_code)?1:2;
  94. }
  95. $check = $this->checkChange($good,$temp);
  96. if(!empty($check)){
  97. GoodChange::create($check);
  98. $temp['isChange']=1;
  99. }
  100. $examinfo=$good->exam_info;
  101. $examinfo[]=['apply_name'=>$this->uname,'exam_status'=>$item['status'],"updatetime"=>date("Y-m-d H:i:s")];
  102. $temp['exam_info'] = json_encode($examinfo,JSON_UNESCAPED_UNICODE);
  103. $goodUpdate[]=$temp;
  104. }
  105. $up=$this->model->saveAll($goodUpdate);
  106. $this->success("更新成功");
  107. }
  108. //商品信息
  109. public function info(){
  110. $param=$this->request->param(['spuCode'=>''],'post','trim');
  111. $valid=Validate::rule(['spuCode|商品成本编号'=>'require|max:255']);
  112. if($valid->check($param)==false)$this->error($valid->getError());
  113. $info = $this->model->where(["spuCode"=>$param['spuCode']])->findOrEmpty();
  114. if($info->isEmpty())$this->error("商品信息不存在");
  115. $info->combind=[];
  116. if($info->isCombind==1){
  117. $good = GoodCombind::with(['child'])->where(['spuCode'=>$info->spuCode,'is_del'=>0])->select();
  118. $info->combind = $good->isEmpty()?[]:$good->toArray();
  119. }
  120. $this->success("获取成功",$info);
  121. }
  122. //导出列表
  123. public function exportDiff(){
  124. $param=$this->request->param(['spuCode'=>'','status'=>'','good_name'=>'','companyNo'=>'','supplierNo'=>'','relaComNo'=>'',
  125. 'cgd_supplierNo'=>'','creater'=>'','isZx'=>'','cat_diff'=>'','tax_diff'=>'','isChild'=>0,"start"=>"",'end'=>'','is_diff'=>''],
  126. 'post','trim');
  127. ini_set('memory_limit' , '-1');
  128. $where=[];
  129. $param['spuCode']==''?: $where[]=['spuCode','like',"%{$param['spuCode']}%"];
  130. $param['good_name']==''?: $where[]=['good_name','like',"%{$param['good_name']}%"];
  131. $param['companyNo']==''?: $where[]=['companyNo','like',"%{$param['companyNo']}%"];
  132. $param['supplierNo']==''?: $where[]=['supplierNo|cgd_supplierNo','like',"%{$param['supplierNo']}%"];
  133. $param['relaComNo']==''?: $where[]=['companyNo|supplierNo','like',"%{$param['relaComNo']}%"];
  134. $param['cgd_supplierNo']==''?: $where[]=['cgd_supplierNo','like',"%{$param['cgd_supplierNo']}%"];
  135. $param['creater']==''?: $where[]=['creater','like',"%{$param['creater']}%"];
  136. $param['status']===''?: $where[]=['status','=',$param['status']];
  137. $param['isZx']==''?: $where[]=['isZx','=',$param['isZx']];
  138. $param['isChild']==''?: $where[]=['isChild','=',$param['isChild']];
  139. $param['is_diff']==''?: $where[]=['cat_diff|tax_diff','=',2];
  140. $param['cat_diff']==''?: $where[]=['cat_diff','=',$param['cat_diff']];
  141. $param['tax_diff']==''?: $where[]=['tax_diff','=',$param['tax_diff']];
  142. $param['start']==''?: $where[]=['addtime','>=',date('Y-m-d 00:00:00',strtotime($param['start']))];
  143. $param['end']==''?: $where[]=['addtime','<=',date('Y-m-d 23:59:59',strtotime($param['end']))];
  144. $field=["spuCode","cat_name","good_name","isZx","inv_cat_name","inv_good_name","inv_tax","cgd_inv_cat_name","
  145. cgd_inv_good_name","cgd_inv_tax","cat_diff","tax_diff","addtime"];
  146. $list= $this->model->export($where,$field);
  147. $header=["商品编码","商品分类","商品名称",'商品类型','采购商品类目','采购商品名称','采购商品税率','财务商品类目','财务商品名称','财务商品税率',
  148. '类目是否一致','税率是否一致','创建时间'];
  149. excelExport("商品税率异常记录", $header, $list);
  150. }
  151. public function export(){
  152. ini_set('memory_limit' , '-1');
  153. $param=$this->request->param(['spuCode'=>'','status'=>'','good_name'=>'','companyNo'=>'','supplierNo'=>'',
  154. 'cgd_supplierNo'=>'','relaComNo'=>'','creater'=>'','isZx'=>'','isChild'=>0,'isChange'=>'','cat_diff'=>'','tax_diff'=>'',
  155. 'is_diff'=>'','start'=>'','end'=>''],'post','trim');
  156. $where=[];
  157. $param['spuCode']==''?: $where[]=['spuCode','like',"%{$param['spuCode']}%"];
  158. $param['good_name']==''?: $where[]=['good_name','like',"%{$param['good_name']}%"];
  159. $param['companyNo']==''?: $where[]=['companyNo','like',"%{$param['companyNo']}%"];
  160. $param['supplierNo']==''?: $where[]=['supplierNo|cgd_supplierNo','like',"%{$param['supplierNo']}%"];
  161. $param['relaComNo']==''?: $where[]=['companyNo|supplierNo','like',"%{$param['relaComNo']}%"];
  162. $param['cgd_supplierNo']==''?: $where[]=['cgd_supplierNo','like',"%{$param['cgd_supplierNo']}%"];
  163. $param['creater']==''?: $where[]=['creater','like',"%{$param['creater']}%"];
  164. $param['status']===''?: $where[]=['status','=',$param['status']];
  165. $param['isZx']==''?: $where[]=['isZx','=',$param['isZx']];
  166. $param['isChild']===''?: $where[]=['isChild','=',$param['isChild']];
  167. $param['isChange']===''?: $where[]=['isChange','=',$param['isChange']];
  168. $param['is_diff']==''?: $where[]=['cat_diff|tax_diff','=',2];
  169. $param['cat_diff']==''?: $where[]=['cat_diff','=',$param['cat_diff']];
  170. $param['tax_diff']==''?: $where[]=['tax_diff','=',$param['tax_diff']];
  171. $param['start']==''?: $where[]=['addtime','>=',date('Y-m-d 00:00:00',strtotime($param['start']))];
  172. $param['end']==''?: $where[]=['addtime','<=',date('Y-m-d 23:59:59',strtotime($param['end']))];
  173. $header=['商品编码','卖出方公司编号','卖出方公司','买入方公司编号','买入方公司',"源头供应商编号","源头供应商名称",'商品名称','采购类目编码','采购类目名称','采购开票商品名称',
  174. "采购类目税率",'财务类目编码','财务类目名称','财务开票商品名称',"财务类目税率",'状态','增值税管理内容','创建人','创建时间'];
  175. $field=['spuCode','supplierNo','supplierName','companyNo','companyName','cgd_supplierNo','cgd_supplierName','good_name','cgd_inv_cat_code','cgd_inv_cat_name','
  176. cgd_inv_good_name','cgd_inv_tax','inv_cat_code','inv_cat_name','inv_good_name','inv_tax','status','addTax','creater','addtime'];
  177. $list= $this->model->export($where,$field);
  178. excelExport('结算类目设置导出表',$header,$list);
  179. }
  180. /**
  181. * @param $good
  182. * @param $check
  183. * @return array
  184. */
  185. private function checkChange($good,$check){
  186. $temp=[];
  187. if($check['status']==1){
  188. $before=[];
  189. $after=[];
  190. if($good->cgd_inv_cat_code!=""&&intval($check['cgd_inv_cat_code'])!=intval($good->cgd_inv_cat_code)){
  191. $before['inv_cat_code'] = $good->cgd_inv_cat_code;
  192. $after['inv_cat_code'] = $check['cgd_inv_cat_code'];
  193. }
  194. if($good->cgd_inv_cat_name!=''&&$check['cgd_inv_cat_name']!=$good->cgd_inv_cat_name){
  195. $before['inv_cat_name'] = $good->cgd_inv_cat_name;
  196. $after['inv_cat_name'] = $check['cgd_inv_cat_name'];
  197. }
  198. if($good->cgd_inv_good_name!=''&&$check['cgd_inv_good_name']!=$good->cgd_inv_good_name){
  199. $before['inv_good_name'] = $good->cgd_inv_good_name;
  200. $after['inv_good_name'] = $check['cgd_inv_good_name'];
  201. }
  202. if($good->cgd_inv_tax!=''){
  203. $cgd_inv_tax = bcdiv(str_replace('%', '', $good->cgd_inv_tax), '100', 2);
  204. if($check['cgd_inv_tax']!=$cgd_inv_tax){
  205. $before['inv_tax'] = $cgd_inv_tax;
  206. $after['inv_tax'] = $check['cgd_inv_tax'];
  207. }
  208. }
  209. if(!empty($before))$temp['before'] = json_encode($before,JSON_UNESCAPED_UNICODE);
  210. if(!empty($after))$temp['after'] = json_encode($after,JSON_UNESCAPED_UNICODE);
  211. }
  212. if($check['status']==2){
  213. $before=[];
  214. $after=[];
  215. if($good->inv_cat_code!=''&&$check['inv_cat_code']!=$good->inv_cat_code){
  216. $before['inv_cat_code'] = $good->inv_cat_code;
  217. $after['inv_cat_code'] = $check['inv_cat_code'];
  218. }
  219. if($good->inv_cat_name!=''&&$check['inv_cat_name']!=$good->inv_cat_name){
  220. $before['inv_cat_name'] = $good->inv_cat_name;
  221. $after['inv_cat_name'] = $check['inv_cat_name'];
  222. }
  223. if($good->inv_good_name!=''&&$check['inv_good_name']!=$good->inv_good_name){
  224. $before['inv_good_name'] = $good->inv_good_name;
  225. $after['inv_good_name'] = $check['inv_good_name'];
  226. }
  227. if($good->inv_tax!=''){
  228. $inv_tax = bcdiv(str_replace('%', '', $good->inv_tax), '100', 2);
  229. if($check['inv_tax']!=$inv_tax){
  230. $before['inv_tax'] = $inv_tax;
  231. $after['inv_tax'] = $check['inv_tax'];
  232. }
  233. }
  234. if($good->status==2&&$check['inv_tag']!=$good->inv_tag){
  235. $before['inv_tag'] = $good->inv_tag;
  236. $after['inv_tag'] = $check['inv_tag'];
  237. }
  238. if($good->status==2&&$check['is_discount']!=$good->is_discount){
  239. $before['is_discount'] = $good->is_discount;
  240. $after['is_discount'] = $check['is_discount'];
  241. }
  242. if($good->status==2&&$check['addTax']!=$good->addTax){
  243. $before['addTax'] = $good->addTax;
  244. $after['addTax'] = $check['addTax'];
  245. }
  246. if(!empty($before))$temp['before'] = json_encode($before,JSON_UNESCAPED_UNICODE);
  247. if(!empty($after))$temp['after'] = json_encode($after,JSON_UNESCAPED_UNICODE);
  248. }
  249. if(!empty($temp)){
  250. $temp['spuCode'] = $good->spuCode;
  251. $temp['good_name'] = $good->good_name;
  252. $temp['apply_id'] = $this->uid;
  253. $temp['apply_name'] = $this->uname;
  254. $temp['exam_status'] =$check['status'];
  255. }
  256. return $temp;
  257. }
  258. }