Good.php 14 KB

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