|
@@ -7,6 +7,7 @@ use App\Model\DepartIndex;
|
|
|
use App\Model\InOutRecord;
|
|
|
use App\Model\InvoiceOrder;
|
|
|
use App\Model\InvoiceOrderInfo;
|
|
|
+use App\Model\LastJc;
|
|
|
use App\Model\Product;
|
|
|
use App\Model\ProductCategory;
|
|
|
use App\Model\PurchaseOrder;
|
|
@@ -666,17 +667,59 @@ class StatisticsService extends Service
|
|
|
|
|
|
//上月结存(汇总这个月之前的出入)(库存流水)
|
|
|
public function getLastMonthBalance($product = [], $user = [], $search = []){
|
|
|
- $return = [];
|
|
|
+ //用户提交的上月结存
|
|
|
+ $lastData = $this->getLastMonthJc($product,$user,$search);
|
|
|
+
|
|
|
$startStamp = strtotime(date("Y-m-01 00:00:00"));
|
|
|
$model = InOutRecord::TopClear($user,$search);
|
|
|
$list = $model->where('del_time',0)
|
|
|
->where('crt_time','<',$startStamp)
|
|
|
->whereIn('product_id',$product)
|
|
|
- ->select('product_id','number','price')
|
|
|
+ ->select('product_id','number','price','top_depart_id')
|
|
|
->get()->toArray();
|
|
|
+ $map = [];
|
|
|
+ foreach ($list as $val){
|
|
|
+ $map[$val['product_id'] . $val['top_depart_id']] = $val;
|
|
|
+ }
|
|
|
+
|
|
|
+ //先结存表
|
|
|
$in = $out = [];
|
|
|
+ foreach ($lastData as $value){
|
|
|
+ $key = $value['product_id'] . '|' . $value['top_depart_id'];
|
|
|
+ if($value['number'] >= 0){
|
|
|
+ if(isset($in[$key])){
|
|
|
+ $number = bcadd($in[$key]['number'], $value['number'],0);
|
|
|
+ $total = bcadd($in[$key]['total'], $value['total'],2);
|
|
|
+ $in[$key]['number'] = $number;
|
|
|
+ $in[$key]['total'] = $total;
|
|
|
+ }else{
|
|
|
+ $in[$key] = [
|
|
|
+ 'number' => $value['number'],
|
|
|
+ 'total' => $value['total'],
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ $number = abs($value['number']);
|
|
|
+ $total = abs($value['total']);
|
|
|
+ if(isset($out[$key])){
|
|
|
+ $number = bcadd($out[$key]['number'], $number,0);
|
|
|
+ $total = bcadd($out[$key]['total'], $total,2);
|
|
|
+ $out[$key]['number'] = $number;
|
|
|
+ $out[$key]['total'] = $total;
|
|
|
+ }else{
|
|
|
+ $out[$key] = [
|
|
|
+ 'number' => $number,
|
|
|
+ 'total' => $total,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //后库存流水
|
|
|
foreach ($list as $value){
|
|
|
+ $key = $value['product_id'] . '|' . $value['top_depart_id'];
|
|
|
+
|
|
|
if($value['number'] >= 0){
|
|
|
+ if(isset($in[$key])) continue;
|
|
|
$tmp_total = bcmul($value['number'], $value['price'], 2);
|
|
|
if(isset($in[$value['product_id']])){
|
|
|
$number = bcadd($in[$value['product_id']]['number'], $value['number'],0);
|
|
@@ -690,6 +733,7 @@ class StatisticsService extends Service
|
|
|
];
|
|
|
}
|
|
|
}else{
|
|
|
+ if(isset($out[$key])) continue;
|
|
|
$number = abs($value['number']);
|
|
|
$tmp_total = bcmul($number, $value['price'], 2);
|
|
|
if(isset($out[$value['product_id']])){
|
|
@@ -705,8 +749,72 @@ class StatisticsService extends Service
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ //两个数组组合过滤
|
|
|
+ $new_in = $new_out = [];
|
|
|
+ foreach ($in as $key => $value){
|
|
|
+ $tmp = explode('|', $key);
|
|
|
+ $product_id = $tmp[0];
|
|
|
+ if(isset($new_in[$product_id])){
|
|
|
+ $number = bcadd($new_in[$product_id]['number'], $value['number'],0);
|
|
|
+ $total = bcadd($new_in[$product_id]['total'], $value['total'],2);
|
|
|
+ $new_in[$product_id] = [
|
|
|
+ 'number' => $number,
|
|
|
+ 'total' => $total,
|
|
|
+ ];
|
|
|
+ }else{
|
|
|
+ $new_in[$product_id] = [
|
|
|
+ 'number' => $value['number'],
|
|
|
+ 'total' => $value['total'],
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ foreach ($out as $key => $value){
|
|
|
+ $tmp = explode('|', $key);
|
|
|
+ $product_id = $tmp[0];
|
|
|
+ if(isset($new_out[$product_id])){
|
|
|
+ $number = bcadd($new_out[$product_id]['number'], $value['number'],0);
|
|
|
+ $total = bcadd($new_out[$product_id]['total'], $value['total'],2);
|
|
|
+ $new_out[$product_id] = [
|
|
|
+ 'number' => $number,
|
|
|
+ 'total' => $total,
|
|
|
+ ];
|
|
|
+ }else{
|
|
|
+ $new_out[$product_id] = [
|
|
|
+ 'number' => $value['number'],
|
|
|
+ 'total' => $value['total'],
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- return [$in, $out];
|
|
|
+ return [$new_in, $new_out];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getLastMonthJc($product = [], $user = [], $search = []){
|
|
|
+ // 如果存在上月结存数据则使用这个
|
|
|
+ $top_depart_id = 0;
|
|
|
+ if(empty($search['top_depart_id'])) $top_depart_id = $search['top_depart_id'];
|
|
|
+
|
|
|
+ $startStamp = strtotime(date("Y-m-01 00:00:00"));
|
|
|
+ $result = LastJc::where('del_time',0)
|
|
|
+ ->whereIn('product_id',$product)
|
|
|
+ ->where('time','<',$startStamp)
|
|
|
+ ->when(! empty($top_depart_id), function ($query) use ($top_depart_id) {
|
|
|
+ return $query->where('top_depart_id',$top_depart_id);
|
|
|
+ })
|
|
|
+ ->select('product_id','top_depart_id','total','number','price')
|
|
|
+ ->orderBy('time','desc')
|
|
|
+ ->get()->toArray();
|
|
|
+ $return = [];
|
|
|
+ $tmp = [];
|
|
|
+ foreach ($result as $value){
|
|
|
+ $key = $value['product_id'] . $value['top_depart_id'];
|
|
|
+ if(in_array($key, $tmp)) continue;
|
|
|
+
|
|
|
+ $return[] = $value;
|
|
|
+ $tmp[] = $key;
|
|
|
+ }
|
|
|
+
|
|
|
+ return $return;
|
|
|
}
|
|
|
|
|
|
//本月入库(单据)暂时没用
|