Good.php 15 KB

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