Blame view

application/admin/controller/kuerp/GoodsStock.php 19 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
  <?php

  

  namespace app\admin\controller\kuerp;

  

  use app\common\controller\Backend;

  use think\Db;

  

  /**

   * 商品库存管理

   *

   * @icon fa fa-circle-o

   */

  class GoodsStock extends Backend

  {

  

      /**

       * GoodsStock模型对象

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

       */

      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\GoodsStock;

          $this->view->assign("statusList", $this->model->getStatusList());

          $this->view->assign("stockWarningList", $this->model->getStockWarningList());

  

          $storesList = $this->model->getStoresList();

          $storesLists = [];

          foreach ($storesList as $storesList_k=>$storesList_v){

              $storesLists[] = ['name'=>$storesList_v['name'],'value'=>$storesList_v['id']];

          }

          $this->view->assign("storesList", $storesList);

          $this->assignconfig("storesList", $storesLists);

  

          $goodsTypeLists = (new \app\admin\model\kuerp\GoodsType())->select();

          $goodsTypeList = [];

          foreach ($goodsTypeLists as $storesList_k=>$storesList_v){

              $goodsTypeList[] = ['name'=>$storesList_v['supplier_type_name'],'value'=>$storesList_v['id']];

          }

          $this->assignconfig("goodsTypeList", $goodsTypeList);

  

          $goodsList = $this->model->getGoodsList();

          $goodsLists = [];

          foreach ($goodsList as $goodsList_k=>$goodsList_v){

              $goodsLists[] = ['name'=>$goodsList_v['goods_name'],'value'=>$goodsList_v['id']];

          }

          $this->view->assign("goodsList", $goodsList);

          $this->assignconfig("goodsList", $goodsLists);

  

          $rowlist = [

              ['field'=>'stock','title'=>'基本数量','totalRow'=>true],

              ['field'=>'more_unit_num','title'=>'副单位数'],

              ['field'=>'unit_price','title'=>'单位成本','totalRow'=>true],

              ['field'=>'cost_money','title'=>'成本','totalRow'=>true]

          ];

          $rowspan = [

              ['field'=>'goods_code','title'=>'商品编号','rowspan'=>2,'totalRow'=>'合计'],

              ['field'=>'goods_name','title'=>'商品名称','rowspan'=>2,'width'=>'120'],

              ['field'=>'type_name','title'=>'商品类别','rowspan'=>2],

              ['field'=>'goods_specification_id','title'=>'规格型号','rowspan'=>2],

              ['field'=>'note','title'=>'商品备注','rowspan'=>2],

              ['field'=>'unit_name','title'=>'单位','rowspan'=>2],

              ['field'=>'unitconversion','title'=>'副单位','rowspan'=>2],

              ['field'=>'goods_barcode','title'=>'商品条码','rowspan'=>2],

              ['field'=>'stores_id','title'=>'所有仓库','colspan'=>4]

          ];

          foreach ($storesList as $data_k=>$data_v){

              $rowspan[] = ['field'=>'stores_id'.$data_v['id'],'title'=>$data_v['name'],'colspan'=>4];

              $rowlist[] = ['field'=>'stock'.$data_v['id'],'title'=>'基本数量','totalRow'=>true];

              $rowlist[] = ['field'=>'more_unit_num'.$data_v['id'],'title'=>'副单位数'];

              $rowlist[] = ['field'=>'unit_price'.$data_v['id'],'title'=>'单位成本','totalRow'=>true];

              $rowlist[] = ['field'=>'cost_money'.$data_v['id'],'title'=>'成本','totalRow'=>true];

          }

          $this->assignconfig('rowspan',json_encode($rowspan));

          $this->assignconfig('rowlist',json_encode($rowlist));

      }

  

      /**

       * 查看

       */

      public function index()

      {

          //当前是否为关联查询

          $this->relationSearch = true;

          //设置过滤方法

          $this->request->filter(['strip_tags', 'trim']);

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

              //如果发送的来源是Selectpage,则转发到Selectpage

              if ($this->request->request('keyField')) {

                  return $this->selectpage();

              }

              list($where, $sort, $order, $offset, $limit) = $this->buildparams();

  

              $where = [];

              $searchdata = json_decode(input('filter'),true);

              if($searchdata){

                  foreach ($searchdata as $k => &$v) {

                      if($v!==''){

                          if($k=='id'){

                              $where['goods_stock.'.$k] = ['<',$v];

                          }else if(strpos($v,' - ') !== false && strpos($k,'time') !== false){

                              $str = explode(' - ',$v);

                              $where['goods_stock.'.$k] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];

                          }else if($k=='goods_id' || $k=='stores_id'){

                              $where['goods_stock.'.$k] = ["in", $v];

                          }else if($k=='goods_type_id'){

                              $where['goods.'.$k] = ["in", $v];

                          }else{

                              $where['goods_stock.'.$k] = ["LIKE", "%{$v}%"];

                          }

                      }

                  }

              }

  

              $list = Db::name('kuerp_goods_stock')->alias('goods_stock')

                  ->join('kuerp_goods goods','goods.goods_code=goods_stock.goods_code')

                  ->where($where)

                  ->group('goods.goods_code,goods_stock.attribute_id')

                  ->field('ANY_VALUE(goods_stock.goods_code) as goods_code,sum(goods_stock.stock) as stock,

                      sum(goods_stock.cost_money) as cost_money,ANY_VALUE(goods_stock.attribute_id) as attribute_id')

                  ->select();

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

                  $list[$list_k]['goods'] = Db::name('kuerp_goods')->where(['goods_code'=>$list_v['goods_code']])

                      ->field('goods_type_id,goods_code,goods_name,goods_specification_id,note,unit_id,more_unit,goods_barcode')

                      ->find();

              }

  

              $number_decimal = Db::name('kuerp_sets')->where(['name'=>'number_decimal'])->value('value');

              $price_decimal = Db::name('kuerp_sets')->where(['name'=>'price_decimal'])->value('value');

              $checkdate = Db::name('kuerp_checkout')->order(['checkdate'=>'DESC'])->value('checktime');

              $enable_time = Db::name('kuerp_sets')->where(['name'=>'enable_time'])->value('value');

              $valuation_set= Db::name('kuerp_sets')->where(['name'=>'valuation'])->value('value');//计算方法

              $stores_set= Db::name('kuerp_sets')->where(['name'=>'stores'])->value('is_checked');//是否分仓

              if($valuation_set!=2){

                  $valuation_set = 1;

              }

              if($stores_set!=1){

                  $stores_set = 0;

              }

  

              $price_taxes= Db::name('kuerp_sets')->where(['name'=>'price_taxes'])->value('is_checked');

              $taxes_set= Db::name('kuerp_sets')->where(['name'=>'taxes'])->field('value,is_checked')->find();

              $stores_ids = Db::name('kuerp_stores')->where(['status'=>'1'])->column('id');

  

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

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

                  $purchase_money = Db::name('kuerp_goods_purchase_info')->where(['pi.goods_code'=>$list_v['goods_code'],'p.examine_status'=>'1'])->alias('pi')

                      ->join('kuerp_goods_purchase p','p.purchase_code=pi.purchase_code')

                      ->field('sum(pi.all_money) as all_money,sum(pi.basic_nmber) as basic_nmber')->find();

  

                  $goods_period = Db::name('kuerp_goods_period')->where(['goods_id'=>$goods['id']])

                      ->field('sum(price) as all_money,sum(stock) as basic_nmber')->find();

                  if($goods_period){

                      $purchase_money['all_money'] += $goods_period['all_money'];$purchase_money['basic_nmber'] += $goods_period['basic_nmber'];

                  }

  

                  $warehousing_money = Db::name('kuerp_warehousing_info')->where(['pi.goods_id'=>$goods['id'],'p.examine_status'=>'1'])->alias('pi')

                      ->join('kuerp_warehousing p','p.purchase_code=pi.purchase_code')

                      ->field('sum(pi.price) as all_money,sum(pi.basic_nmber) as basic_nmber')->find();

                  if($warehousing_money){

                      $purchase_money['all_money'] += $warehousing_money['all_money'];$purchase_money['basic_nmber'] += $warehousing_money['basic_nmber'];

                  }

                  $assemble_money = Db::name('kuerp_assemble_info')->where(['pi.goods_code'=>$goods['id'],'p.examine_status'=>'1'])->alias('pi')

                      ->join('kuerp_assemble p','p.purchase_code=pi.purchase_code')

                      ->field('sum(pi.cost_money) as all_money,sum(pi.basic_number) as basic_nmber')->find();

                  if($assemble_money){

                      $purchase_money['all_money'] += $assemble_money['all_money'];$purchase_money['basic_nmber'] += $assemble_money['basic_nmber'];

                  }

                  $result = (new Goods())->getGoodsCostPrice(

                      $list_v['goods_code'],

                      $number_decimal,

                      $price_decimal,

                      $checkdate,

                      $enable_time,

                      $valuation_set,

                      $stores_set,

                      $price_taxes,

                      $taxes_set,

                      $stores_ids

                  );

                  if(isset($result[0]['cost_all']) && isset($result[0]['basic_nmber'])){

                      $cost_money = $purchase_money['all_money']-$result[0]['cost_all'];

                      $cost_number = $purchase_money['basic_nmber']-$result[0]['basic_nmber'];

                  }else{

                      $cost_money = $purchase_money['all_money'];

                      $cost_number = $purchase_money['basic_nmber'];

                  }

                  if($cost_number!=0){

                      $unit_price = round($cost_money/$cost_number,$price_decimal);

                  }else{

                      $unit_price = 0;

                  }

                  $list[$list_k]['unit_price'] = round($unit_price,$price_decimal);

                  $list[$list_k]['cost_money'] = round($cost_money,$price_decimal);

                  $list[$list_k]['type_name'] = Db::name('kuerp_goods_type')->where((new Goods())->setWhere(['id'=>$list_v['goods']['goods_type_id']]))->value('supplier_type_name');

                  //单位信息

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

                  $list[$list_k]['unit_name'] = $unit['name'];

                  $max_unit = 1;

                  $unit_arr = [];

                  $more_unit_num = '';

                  if($unit){

                      $unit_arr[] = ['name'=>$unit['name'],'advance_rate'=>1];

                      if($list_v['goods']['more_unit']==1){

                          $more_unit = Db::name('kuerp_unit_conversion')->alias('uc')

                              ->join('kuerp_unit u','u.id=uc.vice_unit')

                              ->where((new Goods())->setWhere(['uc.main_unit'=>$unit['id']],'uc'))

                              ->order('uc.advance_rate','ASC')

                              ->field('uc.vice_unit,u.name,u.id,uc.advance_rate')

                              ->select();

                          foreach ($more_unit as $unit_k=>$unit_v){

                              $unit_arr[] = ['name'=>$unit_v['name'],'advance_rate'=>$unit_v['advance_rate']];

                              if($unit_v['advance_rate']>$max_unit){

                                  $max_unit = $unit_v['advance_rate'];

                              }

                          }

                          $unitconversion = '';

                          $unit_arr = (new Goods())->arraySequence($unit_arr,'advance_rate','SORT_DESC');

                          foreach ($unit_arr as $unit_arr_k=>$unit_arr_v){

                              if(!empty($unitconversion)){

                                  $unitconversion .= '=';

                              }else{

                                  $more_unit_num .= round(($list_v['stock']/$unit_arr_v['advance_rate']),$number_decimal).$unit_arr_v['name'];

                              }

                              $unitconversion .= round(($max_unit/$unit_arr_v['advance_rate']),$number_decimal).$unit_arr_v['name'];

                          }

                          $list[$list_k]['unitconversion'] = $unitconversion;

                          $list[$list_k]['more_unit_num'] = $more_unit_num;

                      }

                  }

              }

  

              $storesList = $this->model->getStoresList();

              $countArr = [];

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

                  $list[$list_k]['cost_money'] = round($list_v['stock']*$list_v['unit_price'],$price_decimal);

              }

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

                  if(isset($countArr['stock'])){

                      $countArr['stock'] += $list_v['stock'];

                  }else{

                      $countArr['stock'] = $list_v['stock'];

                  }

                  if(isset($countArr['cost_money'])){

                      $countArr['cost_money'] += $list_v['cost_money'];

                  }else{

                      $countArr['cost_money'] = $list_v['cost_money'];

                  }

                  $countArr['stock'] = round($countArr['stock'],$number_decimal);

                  $countArr['cost_money'] = round($countArr['cost_money'],$price_decimal);

                  $list[$list_k]['goods_code'] = $list_v['goods']['goods_code'];

                  $list[$list_k]['goods_name'] = $list_v['goods']['goods_name'];

                  $list[$list_k]['goods_specification_id'] = $list_v['goods']['goods_specification_id'];

                  $list[$list_k]['note'] = $list_v['goods']['note'];

                  $list[$list_k]['goods_barcode'] = $list_v['goods']['goods_barcode'];

  

                  //按属性分组显示

                  $attribute_id = Db::name('kuerp_goods_attr')->where(['id'=>$list_v['attribute_id']])->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);

                      $list[$list_k]['goods_name'] = $list_v['goods']['goods_name'].'_'.$attribute_name;

                  }

  

                  foreach ($storesList as $storesList_k=>$storesList_v){

                      $list2 = Db::name('kuerp_goods_stock')->alias('goods_stock')

                          ->join('kuerp_goods goods','goods.goods_code=goods_stock.goods_code')

                          ->where($where)

                          ->where(['goods_stock.stores_id'=>$storesList_v['id'],'goods_stock.goods_code'=>$list_v['goods_code']])

                          ->where(['goods_stock.attribute_id'=>$list_v['attribute_id']])

                          ->group('goods.goods_code')

                          ->field('sum(goods_stock.stock) as stock,sum(goods_stock.cost_money) as cost_money')

                          ->find();

                      if($list2){

                          $list2['unit_price'] = $list_v['unit_price'];

                          $list2['cost_money'] = round( ($list_v['unit_price']*$list2['stock']), $price_decimal);

  

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

                          if($unit){

                              if($list_v['goods']['more_unit']==1){

                                  $more_unit = Db::name('kuerp_unit_conversion')->alias('uc')

                                      ->join('kuerp_unit u','u.id=uc.vice_unit')

                                      ->where((new Goods())->setWhere(['uc.main_unit'=>$unit['id']],'uc'))

                                      ->order('uc.advance_rate','DESC')

                                      ->field('uc.vice_unit,u.name,u.id,uc.advance_rate')

                                      ->find();

                                  $more_unit_num = '';

                                  if($more_unit){

                                      $more_unit_num .= round(($list2['stock']/$more_unit['advance_rate']),$number_decimal).$more_unit['name'];

                                  }

                                  $list2['more_unit_num'] = $more_unit_num;

                              }

                          }

                          if(!isset($list2['more_unit_num'])){

                              $list2['more_unit_num'] = '';

                          }

                          $list[$list_k]['stock'.$storesList_v['id']] = $list2['stock'];

                          $list[$list_k]['cost_money'.$storesList_v['id']] = round($list2['cost_money'],$price_decimal);

                          $list[$list_k]['unit_price'.$storesList_v['id']] = round($list2['unit_price'],$price_decimal);

                          $list[$list_k]['more_unit_num'.$storesList_v['id']] = $list2['more_unit_num'];

  

                          if(isset($countArr['stock'.$storesList_v['id']])){

                              $countArr['stock'.$storesList_v['id']] += $list2['stock'];

                          }else{

                              $countArr['stock'.$storesList_v['id']] = $list2['stock'];

                          }

                          if(isset($countArr['cost_money'.$storesList_v['id']])){

                              $countArr['cost_money'.$storesList_v['id']] += $list2['cost_money'];

                          }else{

                              $countArr['cost_money'.$storesList_v['id']] = $list2['cost_money'];

                          }

                      }

                  }

              }

              $sumArr = [];

              if(isset($countArr['stock']) && $countArr['stock']>0){

                  $sumArr[] = ['unit_price_count'=>round(($countArr['cost_money']/$countArr['stock']),$price_decimal),'id'=>''];

              }else{

                  $sumArr[] = ['unit_price_count'=>0,'id'=>''];

              }

              foreach ($storesList as $storesList_k=>$storesList_v){

                  if(isset($countArr['stock'.$storesList_v['id']]) && $countArr['stock'.$storesList_v['id']]>0){

                      $sumArr[] = ['unit_price_count'=>round(($countArr['cost_money'.$storesList_v['id']]/$countArr['stock'.$storesList_v['id']]),$price_decimal),'id'=>$storesList_v['id']];

                  }else{

                      $sumArr[] = ['unit_price_count'=>0,'id'=>$storesList_v['id']];

                  }

              }

              $result = array("total" => sizeof($list), "rows" => $list, "countArr" => $sumArr);

  

              return json($result);

          }

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

      }

  

  }