Blame view

application/admin/controller/kuerp/Replenishment.php 6.83 KB
2db47680   hexiaodong   chore: 初始提交 php 遗...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
  <?php

  

  namespace app\admin\controller\kuerp;

  

  use app\common\controller\Backend;

  use think\Db;

  use Exception;

  use think\Session;

  /**

   * 智能补货

   *

   * @icon fa fa-circle-o

   */

  class Replenishment extends Backend

  {

      protected $noNeedRight = ['setdata'];

      /**

       * GoodsSales模型对象

       * @var \app\admin\model\kuerp\GoodsSales

       */

      protected $model = null;

  

      public function _initialize()

      {

          parent::_initialize();

          $setData = (new Goods())->getSetData();

          $this->assign('set', $setData['setList']);

          $this->assignconfig('set',$setData['setList']);

          $this->assign('nowmenu', $setData['nowmenu']);

  

          $this->model = new \app\admin\model\kuerp\GoodsSalesOrder();

      }

  

      /**

       * 查看

       */

      public function index()

      {

          if ($this->request->isAjax()) {

              $data = input();

              $where = [];

              if(isset($data['filter'])){

                  $filter = (array)json_decode($data['filter'], true);

                  $filter = $filter ? $filter : [];

                  if(!empty($filter['id'])){

                      $where['id'] = ['<',$filter['id']];

                  }

                  if(!empty($filter['goods_code'])){

                      $where['goods_code'] = ["like", '%'.$filter['goods_code'].'%'];

                  }

              }

  

              $sales_list = Db::name('kuerp_goods_sales_order_info')->where((new Goods())->setWhere($where))->group('goods_code,goods_attr')->field('goods_code,goods_attr')->select();

              $lists = [];

              foreach ($sales_list as $sales_list_k=>$sales_list_v){

                  $row = [];

                  $goods = Db::name('kuerp_goods')->where((new Goods())->setWhere(['goods_code'=>$sales_list_v['goods_code']]))->find();

                  $row['goods_id'] = $goods['id'];

                  $row['goods_code'] = $goods['goods_code'];

                  $row['goods_name'] = $goods['goods_code'].'_'.$goods['goods_name'];

                  $row['buy_price'] = $goods['purchase'];

  

                  if($goods['min_stock']<=0){

                      $goods['min_stock'] = Db::name('kuerp_goods_stock_warning')->where((new Goods())->setWhere(['goods_code'=>$goods['goods_code']]))->sum('min_stock');

                  }

                  $row['min_stock'] = $goods['min_stock'];

  

                  $where = [];

                  $where['goods_code'] = $goods['goods_code'];

                  if($sales_list_v['goods_attr']>0){

                      $where['attribute_id'] = $sales_list_v['goods_attr'];

                  }

                  $stock = Db::name('kuerp_goods_stock')->where($where)->field('stock,id,cost_money')->sum('stock');

                  $row['stock'] = $stock;

  

                  $row['attr_id'] = '';

                  $row['attr_name'] = '';

                  if($goods['auxiliary']==1){

                      //商品设置属性

                      $row['attr_id'] = $sales_list_v['goods_attr'];

                      $attribute_id = Db::name('kuerp_goods_attr')->where((new Goods())->setWhere(['id'=>$sales_list_v['goods_attr']]))->value('attribute_id');

                      $attribute_ids = Db::name('kuerp_aux_combination')->where((new Goods())->setWhere(['id'=>$attribute_id]))->value('attr_ids');

                      if($attribute_ids){

                          $attribute_ids = explode('&',$attribute_ids);

                          $attribute = Db::name('kuerp_auxiliary')->where((new Goods())->setWhere(['id'=>['in',$attribute_ids]]))->column('name');

                          $attribute_name = implode('/',$attribute);

                          $row['attr_name'] = $attribute_name;

                      }

                  }

  

                  //商品单位

                  $unit = Db::name('kuerp_unit')->where((new Goods())->setWhere(['id'=>$goods['unit_id']]))->field('id,name')->find();

                  $row['goods_unit'] = $goods['unit_id'];

                  $row['unit_name'] = isset($unit['name'])?$unit['name']:'';

  

                  $list = Db::name('kuerp_goods_sales_order_info')->where((new Goods())->setWhere(['goods_code'=>$sales_list_v['goods_code'],'goods_attr'=>$sales_list_v['goods_attr']]))->select();

                  $row['number'] = 0;

                  foreach ($list as $list_k=>$list_v){

                      $goods = Db::name('kuerp_goods')->where((new Goods())->setWhere(['goods_code'=>$list_v['goods_code']]))->find();

                      $number = $list_v['number'];

                      $row['number'] = $number;

                      if($goods['more_unit']==1 && $goods['unit_id']!=$list_v['goods_unit']){

                          $advance_rate = Db::name('kuerp_unit_conversion')->where((new Goods())->setWhere(['main_unit'=>$goods['unit_id'],'vice_unit'=>$list_v['goods_unit']]))->value('advance_rate');

                          if($advance_rate){

                              $row['number'] = $number*$advance_rate;

                          }

                      }

                  }

  

                  $list = Db::name('kuerp_goods_purchase_order_info')->where((new Goods())->setWhere(['goods_code'=>$sales_list_v['goods_code'],'goods_attr'=>$sales_list_v['goods_attr']]))->select();

                  $row['buy_number'] = 0;

                  foreach ($list as $list_k=>$list_v){

                      $goods = Db::name('kuerp_goods')->where((new Goods())->setWhere(['goods_code'=>$list_v['goods_code']]))->find();

                      $number = $list_v['number'];

                      $row['buy_number'] = $number;

                      if($goods['more_unit']==1 && $goods['unit_id']!=$list_v['goods_unit']){

                          $advance_rate = Db::name('kuerp_unit_conversion')->where((new Goods())->setWhere(['main_unit'=>$goods['unit_id'],'vice_unit'=>$list_v['goods_unit']]))->value('advance_rate');

                          if($advance_rate){

                              $row['buy_number'] = $number*$advance_rate;

                          }

                      }

                  }

                  $row['now_buy_number'] = $row['number']-$row['stock']-$row['buy_number']+$row['min_stock'];

                  if($row['now_buy_number']<0){

                      $row['now_buy_number'] = 0;

                  }

                  $lists[] = $row;

              }

              $result = array("total" => sizeof($lists), "rows" => $lists);

              return json($result);

          }

          return $this->view->fetch();

      }

  

      public function setdata(){

          $params = $this->request->post("row/a");

          if ($params) {

              if($params[0]){

                  $params = json_decode($params[0],true);

              }else{

                  $params = [];

              }

              if(empty($params)){

                  $this->error('参数错误');

              }else{

                  Session::set('GoodsPurchaseOrder',serialize($params));

                  $this->success();

              }

          }

      }

  }