CompanyCgd.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\controller;
  4. use app\admin\model\CompanyCgdLog;use app\admin\model\PurcheaseOrder;use app\admin\model\WarehouseInfo;use app\model\Headquarters;use think\App;use think\facade\Validate;use think\Request;
  5. class CompanyCgd extends Base
  6. {
  7. public $model=null;
  8. public function __construct(App $app) {
  9. parent::__construct($app);
  10. $this->model=new \app\admin\model\CompanyCgd();
  11. }
  12. /**
  13. * 显示资源列表
  14. *
  15. * @return \think\Response
  16. */
  17. public function list()
  18. {
  19. $param = $this->request->only(['companyNo'=>'','is_cgd'=>0,"page"=>1,"size"=>10],'post');
  20. $where=[];
  21. $param["companyNo"]==""?:$where[]=["companyNo","=",$param["companyNo"]];
  22. $param["is_cgd"]==""?:$where[]=["is_cgd","=",$param["is_cgd"]];
  23. $list = $this->model->where($where)->order('id','desc')->paginate(['page'=>$param['page'],'list_rows'=>$param['size']]);
  24. return app_show(0,'获取成功',['list'=>$list->items(),'count'=>$list->total()]);
  25. }
  26. /**
  27. * 显示创建资源表单页.
  28. *
  29. * @return \think\Response
  30. */
  31. public function create()
  32. {
  33. $param = $this->request->only(["companyNo"=>'',"is_cgd"=>0],"post");
  34. $valid=Validate::rule(["companyNo|业务公司"=>"require|max:255|unique:\app\admin\model\CompanyCgd"]);
  35. if($valid->check($param)==false)return error_show(1004,$valid->getError());
  36. $data=[
  37. "companyNo"=>$param["companyNo"],
  38. "companyName"=>Headquarters::where("code",$param['companyNo'])->value("name",''),
  39. "is_cgd"=>$param["is_cgd"]??0,
  40. "apply_id"=>$this->uid,
  41. "apply_name"=>$this->uname
  42. ];
  43. \app\admin\model\CompanyCgd::create($data);
  44. return app_show(0,"创建成功");
  45. }
  46. /**
  47. * 保存更新的资源
  48. *
  49. * @param \think\Request $request
  50. * @param int $id
  51. * @return \think\Response
  52. */
  53. public function update()
  54. {
  55. $param = $this->request->only(['id'=>'','is_cgd'=>0],'post');
  56. $valid=Validate::rule(['id|数据主键'=>'require|number',"is_cgd|是否启用"=>"require|number|in:0,1"]);
  57. if($valid->check($param)==false)return error_show(1004,$valid->getError());
  58. $info = $this->model->findOrEmpty($param["id"]);
  59. if($info->isEmpty())return error_show(1004,"未找到数据");
  60. $info->is_cgd = $param['is_cgd'];
  61. $info->apply_id = $this->uid;
  62. $info->apply_name = $this->uname;
  63. $info->save();
  64. return app_show(0,'修改成功');
  65. }
  66. public function loglist(){
  67. $param = $this->request->only(['companyNo'=>'','code'=>'','good_code'=>'',"start"=>'',"end"=>"",'page'=>1,
  68. 'size'=>10],'post');
  69. $where=[["a.source","=",2]];
  70. $param['companyNo']==''?:$where[]=['b.companyNo','=',$param['companyNo']];
  71. $param['code']==''?:$where[]=['a.code','=',$param['code']];
  72. $param['good_code']==''?:$where[]=['b.spuCode','=',$param['good_code']];
  73. $param['start']==""||$param['end']==""?:$where[]=['a.createtime','between',[startTime($param['start']),endTime
  74. ($param['end'])]];
  75. $model =new \app\admin\model\DataChange();
  76. $list =$model->alias("a")
  77. ->leftJoin("purchease_order b","a.code=b.cgdNo")
  78. ->where($where)
  79. ->field("a.*,b.companyNo,b.companyName")
  80. ->json(["before","after"])
  81. ->order('id','desc')
  82. ->paginate(['page'=>$param['page'],'list_rows'=>$param['size']]);
  83. return app_show(0,'获取成功',['list'=>$list->items(),'count'=>$list->total()]);
  84. }
  85. public function LogCreate(){
  86. $param = $this->request->only(['supplierNo'=>'','cgdNo'=>'','price'=>''],'post');
  87. $valid=Validate::rule([
  88. 'cgdNo|采购单编号'=>'require|max:255',
  89. 'supplierNo|供应商编号'=>'require|max:255',
  90. 'price|采购价格'=>'require|float',
  91. ]);
  92. if($valid->check($param)==false)return error_show(1004,$valid->getError());
  93. $info = PurcheaseOrder::where("cgdNo",$param['cgdNo'])->findOrEmpty();
  94. if($info->status>0)return error_show(1004,"采购单当前状态不可修改");
  95. if($info->order_source!=2)return error_show(1004,"非咨询采购单不可修改");
  96. $ist = $this->model->where([["companyNo","=",$info->companyNo],["is_cgd","=",1]])->findOrEmpty();
  97. if($ist->isEmpty())return error_show(1004,'采购单业务公司无修改权限');
  98. $before = [
  99. 'supplierNo'=>$info->supplierNo,
  100. 'supplier_name'=>$info->supplier_name,
  101. 'good_price'=>$info->good_price,
  102. 'total_fee'=>$info->origin_total,
  103. "cgder_id"=>$info->cgder_id,
  104. "cgder"=>$info->cgder,
  105. 'good_createrid'=>$info->good_createrid,
  106. 'good_creater'=>$info->good_creater,
  107. ];
  108. $after = [
  109. 'supplierNo'=>$param['supplierNo'],
  110. 'supplier_name'=>Headquarters::where('code',$param['supplierNo'])->value('name',''),
  111. 'good_price'=>$param['price'],
  112. 'total_fee'=>bcmul($info->good_num,$param['price'],2),
  113. 'cgder_id'=>$this->uid,
  114. 'cgder'=>$this->uname,
  115. 'good_createrid'=>$this->uid,
  116. 'good_creater'=>$this->uname,
  117. ];
  118. if($before['good_price']<=$after['good_price'])return error_show(1004,'修改成本必须小于当前成本');
  119. $dataChange=new \app\admin\model\DataChange();
  120. $this->model->startTrans();
  121. try{
  122. $before = json_encode($before,JSON_UNESCAPED_UNICODE);
  123. $after = json_encode($after,JSON_UNESCAPED_UNICODE);
  124. $data=[
  125. 'code'=>$param['cgdNo'],
  126. 'type'=>1,
  127. 'source'=>2,
  128. 'before'=>$before,
  129. 'after'=>$after,
  130. 'apply_id'=>$this->uid,
  131. 'apply_name'=>$this->uname,
  132. 'creater_id'=>$this->uid,
  133. 'creater'=>$this->uname,
  134. 'status'=>2,
  135. ];
  136. $save=$dataChange->save($data);
  137. if($save){
  138. $dataChange->cgdUpdate($data);
  139. }
  140. }catch (\Exception $exception){
  141. $this->model->rollback();
  142. return error_show(1004,$exception->getMessage());
  143. }
  144. $this->model->commit();
  145. return app_show(0,"修改成功");
  146. }
  147. }