Good.php 16 KB

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