cqpCow 1 jaar geleden
bovenliggende
commit
2e27b3addf

+ 86 - 0
app/Http/Controllers/Api/BookingListController.php

@@ -0,0 +1,86 @@
+<?php
+
+namespace App\Http\Controllers\Api;
+
+use App\Service\BookingListService;
+use Illuminate\Http\Request;
+
+class BookingListController extends BaseController
+{
+    //金额记账
+    public function customerAdd(Request $request)
+    {
+        $service = new BookingListService();
+        $userData = $request->userData->toArray();
+        list($status,$data) = $service->customerAdd($request->all(),$userData);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function customerEdit(Request $request)
+    {
+        $service = new BookingListService();
+        $userData = $request->userData->toArray();
+        list($status,$data) = $service->customerEdit($request->all(),$userData);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function customerDel(Request $request)
+    {
+        $service = new BookingListService();
+        $userData = $request->userData->toArray();
+        list($status,$data) = $service->customerDel($request->all());
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function customerList(Request $request)
+    {
+        $service = new BookingListService();
+        $userData = $request->userData->toArray();
+        list($status,$data) = $service->customerList($request->all(),$userData);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function customerDetail(Request $request)
+    {
+        $service = new BookingListService();
+        list($status,$data) = $service->customerDetail($request->all());
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function customerConfirm(Request $request)
+    {
+        $service = new BookingListService();
+        list($status,$data) = $service->customerConfirm($request->all());
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+}

+ 38 - 0
app/Model/BookingList.php

@@ -0,0 +1,38 @@
+<?php
+
+namespace App\Model;
+
+use Illuminate\Database\Eloquent\Model;
+
+class BookingList extends Model
+{
+    protected $table = "booking_list"; //指定表
+    const CREATED_AT = 'crt_time';
+    const UPDATED_AT = 'upd_time';
+    protected $dateFormat = 'U';
+    const STATE_ZERO = 0;//未确认
+    const STATE_ONE = 1;//已确认
+    public static $name = [
+        self::STATE_ZERO => '未确认',
+        self::STATE_ONE => '已确认',
+    ];
+
+    const type_one = 1; // 收款
+    const type_two = 2; // 坏账
+    const type_three = 3; // 退款
+    const type_four = 4; // 补收
+    public static $model_type = [
+        self::type_one => '收款',
+        self::type_two => '坏账',
+        self::type_three => '退款',
+        self::type_four => '补收',
+    ];
+
+    const data_type_one = 1; // 合同
+    const data_type_two = 2; // 采购
+    public static $data_type = [
+        self::data_type_one => '合同',
+        self::data_type_two => '采购',
+    ];
+
+}

+ 17 - 0
app/Model/BookingListInfo.php

@@ -0,0 +1,17 @@
+<?php
+
+namespace App\Model;
+
+use Illuminate\Database\Eloquent\Model;
+
+class BookingListInfo extends Model
+{
+    protected $table = "booking_list_info"; //指定表
+    const CREATED_AT = 'crt_time';
+    const UPDATED_AT = 'upd_time';
+    protected $dateFormat = 'U';
+    const type_one = 1; // 文件
+    public static $type = [
+        self::type_one,
+    ];
+}

+ 239 - 0
app/Service/BookingListService.php

@@ -0,0 +1,239 @@
+<?php
+
+namespace App\Service;
+
+use App\Model\BookingList;
+use App\Model\BookingListInfo;
+use App\Model\Employee;
+use App\Model\SalesOrder;
+use Illuminate\Support\Facades\DB;
+
+class BookingListService extends Service
+{
+    public function customerEdit($data,$user){
+        list($status,$msg) = $this->customerRule($data,$user, false);
+        if(!$status) return [$status,$msg];
+
+        try {
+            DB::beginTransaction();
+
+            $model = BookingList::where('id',$data['id'])->first();
+            $model->type = $data['type'];
+            $model->mark = $data['mark'] ?? '';
+            $model->amount = $data['amount'] ?? 0;
+            $model->save();
+
+            $time = time();
+            BookingListInfo::where('del_time',0)
+                ->where('booking_list_id',$data['id'])
+                ->update('del_time',$time);
+            if(! empty($data['file'])){
+                $insert = [];
+                foreach ($data['file'] as $value){
+                    $insert[] = [
+                        'booking_list_id' => $model->id,
+                        'file' => $value['url'],
+                        'type' => BookingListInfo::type_one,
+                        'name' => $value['name'],
+                        'crt_time' => $time,
+                    ];
+                }
+                BookingListInfo::insert($insert);
+            }
+
+            DB::commit();
+        }catch (\Exception $exception){
+            DB::rollBack();
+            return [false,$exception->getMessage()];
+        }
+
+        return [true,''];
+    }
+
+    public function customerAdd($data,$user){
+        list($status,$msg) = $this->customerRule($data,$user);
+        if(!$status) return [$status,$msg];
+
+        try {
+            DB::beginTransaction();
+
+            $model = new BookingList();
+            $model->data_id = $data['data_id'];
+            $model->data_type = $data['data_type'] ?? 0;
+            $model->amount = $data['amount'] ?? 0;
+            $model->mark = $data['mark'] ?? '';
+            $model->crt_id = $user['id'];
+            $model->save();
+
+            if(! empty($data['file'])){
+                $insert = [];
+                foreach ($data['file'] as $value){
+                    $insert[] = [
+                        'booking_list_id' => $model->id,
+                        'file' => $value['url'],
+                        'type' => BookingListInfo::type_one,
+                        'name' => $value['name'],
+                        'crt_time' => time(),
+                    ];
+                }
+                BookingListInfo::insert($insert);
+            }
+            DB::commit();
+        }catch (\Exception $exception){
+            DB::rollBack();
+            return [false,$exception->getMessage()];
+        }
+
+        return [true,''];
+    }
+
+    public function customerDel($data){
+        if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
+        $booking = BookingList::where('del_time',0)->where('id',$data['id'])->first();
+        if(empty($booking)) return [false,'记录不存在或已被删除'];
+        $booking = $booking->toArray();
+        if($booking['state'] != BookingList::STATE_ZERO) return [false,'请确认记录状态,删除失败'];
+
+        try {
+            DB::beginTransaction();
+
+            $time = time();
+            BookingList::where('id',$data['id'])->update([
+                'del_time'=> $time
+            ]);
+            BookingListInfo::where('del_time',0)
+                ->where('booking_list_id',$data['id'])
+                ->update('del_time',$time);
+
+            DB::commit();
+        }catch (\Exception $exception){
+            DB::rollBack();
+            return [false,$exception->getMessage()];
+        }
+
+        return [true,''];
+    }
+
+    public function customerDetail($data){
+        if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
+
+        $customer = BookingList::where('del_time',0)
+            ->where('id',$data['id'])
+            ->first();
+        if(empty($customer)) return [false,'记录不存在或已被删除'];
+        $customer = $customer->toArray();
+        $customer['state_title'] = BookingList::$name[$customer['state']] ?? "";
+        $customer['type'] = BookingList::$model_type[$customer['type']] ?? "";
+
+        $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
+        $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
+
+        $file = BookingListInfo::where('del_time',0)
+            ->where('booking_list_id',$data['id'])
+            ->get()->toArray();
+        $customer['file'] = [];
+        foreach ($file as $value){
+            if($value['type'] == BookingListInfo::type_one){
+                $tmp = [
+                    'url' => $value['file'],
+                    'name' => $value['name'],
+                ];
+                $customer['file'][] = $tmp;
+            }
+        }
+
+        return [true, $customer];
+    }
+
+    public function customerList($data,$user){
+        $model = BookingList::where('del_time',0)
+            ->select('id','data_id','data_type','type','amount','crt_id','crt_time','state','mark')
+            ->orderby('id', 'asc');
+
+        if(! empty($data['data_id'])) $model->where('data_id', $data['data_id']);
+        if(! empty($data['data_type'])) $model->where('data_type',$data['data_type']);
+
+        $list = $this->limit($model,'',$data);
+        $list = $this->fillData($list);
+
+        return [true, $list];
+    }
+
+    public function customerRule(&$data, $user, $is_add = true){
+        if(empty($data['data_id'])) return [false,'数据ID不能为空'];
+        if(empty($data['data_type'])) return [false,'数据类型不能为空'];
+        if(empty($data['type'])) return [false,'金额类型不能为空'];
+        if(empty($data['amount'])) return [false,'金额不能为空'];
+        $res = $this->checkNumber($data['amount']);
+        if(! $res) return [false, '金额请输入不超过两位小数并且大于0的数值'];
+
+        if($data['data_type'] == BookingList::data_type_one){
+            $bool = SalesOrder::where('id',$data['data_id'])->where('state',SalesOrder::State2_zero)->exists();
+            if($bool) return [false,'合同还未指派,添加金额记录失败'];
+        }
+
+        if($is_add){
+
+        }else{
+            if(empty($data['id'])) return [false,'ID不能为空'];
+            $booking = BookingList::where('del_time',0)->where('id',$data['id'])->first();
+            if(empty($booking)) return [false,'记录不存在或已被删除'];
+            $booking = $booking->toArray();
+            if($booking['state'] != BookingList::STATE_ZERO) return [false,'请确认记录状态,编辑失败'];
+
+        }
+
+        return [true, ''];
+    }
+
+    public function fillData($data){
+        if(empty($data['data'])) return $data;
+
+        $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
+            ->pluck('emp_name','id')
+            ->toArray();
+
+        foreach ($data['data'] as $key => $value){
+            $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
+            $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
+            $data['data'][$key]['state_title'] = BookingList::$name[$value['state']] ?? '';
+        }
+
+        return $data;
+    }
+
+    public function customerConfirm($data){
+        if(empty($data['id'])) return [false,'记录ID不能为空'];
+        $customer = BookingList::where('del_time',0)
+            ->where('id',$data['id'])
+            ->first();
+        if(empty($customer)) return [false,'记录不存在或已被删除'];
+        $customer = $customer->toArray();
+        if($customer['state'] != BookingList::STATE_ZERO) return [false,'请确认记录状态,确认金额失败'];
+
+        BookingList::where('id',$data['id'])->update(['state' => BookingList::STATE_ONE]);
+
+        return [true,''];
+    }
+
+    public function getAllAmount($data_id = [], $data_type = 0){
+        if(empty($data_id) || empty($data_type)) return [];
+
+        $booking = BookingList::where('del_time',0)
+            ->where('state',BookingList::STATE_ONE)
+            ->whereIn('data_id',$data_id)
+            ->where('data_type',$data_type)
+            ->select('data_id','amount');
+
+        $return = [];
+        foreach ($booking as $value){
+            if(isset($return[$value['data_id']][$value['data_type']])){
+                $return[$value['data_id']][$value['data_type']] += $value['amount'];
+            }else{
+                $return[$value['data_id']][$value['data_type']] = $value['amount'];
+            }
+        }
+
+        return $return;
+    }
+}

+ 19 - 0
app/Service/PurchaseOrderService.php

@@ -3,6 +3,7 @@
 namespace App\Service;
 
 use App\Model\BasicType;
+use App\Model\BookingList;
 use App\Model\Depart;
 use App\Model\Employee;
 use App\Model\PurchaseOrder;
@@ -350,6 +351,8 @@ class PurchaseOrderService extends Service
                 $product[$value['purchase_order_id']][] = $value;
             }
         }
+        //获取金额列表
+        $money = (new BookingListService())->getAllAmount(array_column($data['data'],'id'), BookingList::data_type_two);
         foreach ($data['data'] as $key => $value){
             $data['data'][$key]['crt_time'] = $value['crt_time'] ? date("Y-m-d H:i:s",$value['crt_time']) : '';
             $data['data'][$key]['depart_name'] = $depart_map[$value['depart_id']] ?? '';
@@ -361,6 +364,22 @@ class PurchaseOrderService extends Service
             $data['data'][$key]['storehouse_title'] = $storehouse[$value['storehouse_id']] ?? '';
             $data['data'][$key]['supplier_title'] = $supplier[$value['supplier']] ?? '';
             $data['data'][$key]['product'] = $product[$value['id']] ?? [];
+
+            //合同对应的金额数据
+            $tmp = $money[$value['id']] ?? [];
+            $fee1 = $tmp[BookingList::type_one] ?? 0; //收款金额
+            $fee2 = $tmp[BookingList::type_two] ?? 0; //坏账金额
+            $fee3 = $tmp[BookingList::type_three] ?? 0; //退款金额
+            $fee4 = $tmp[BookingList::type_four] ?? 0; //补收金额
+
+            //已收金额
+            $data['data'][$key]['fee1'] = $fee1 + $fee4;
+            //未收金额
+            $data['data'][$key]['fee2'] = $value['purchase_total'] - ($fee1 + $fee4);
+            //坏账金额
+            $data['data'][$key]['fee3'] = $fee2;
+            //最终金额
+            $data['data'][$key]['fee4'] = $value['purchase_total'] + $fee4 - $fee3;
         }
 
         return $data;

+ 25 - 0
app/Service/SalesOrderService.php

@@ -3,6 +3,7 @@
 namespace App\Service;
 
 use App\Model\BasicType;
+use App\Model\BookingList;
 use App\Model\Construction;
 use App\Model\Customer;
 use App\Model\Employee;
@@ -581,6 +582,11 @@ class SalesOrderService extends Service
 //            }
         }else{
             if(empty($data['id'])) return [false,'ID不能为空'];
+            $order = SalesOrder::where('del_time',0)->where('id',$data['id'])->first();
+            if(empty($order)) return [false,'合同不存在或已被删除'];
+            $order = $order->toArray();
+            if($order != SalesOrder::State2_zero) return [false, '请确认合同状态,编辑失败'];
+
 //            if($data['model_type'] == SalesOrder::Model_type_two){
 //                $boolean = SalesOrder::where('del_time',0)
 //                    ->where('id','<>',$data['id'])
@@ -639,6 +645,9 @@ class SalesOrderService extends Service
                 $fee[$value['sales_order_id']][] = $value;
             }
         }
+
+        //获取金额列表
+        $money = (new BookingListService())->getAllAmount(array_column($data['data'],'id'), BookingList::data_type_one);
         foreach ($data['data'] as $key => $value){
             $data['data'][$key]['sales_order_type_title'] = SalesOrder::$order_type[$value['sales_order_type']] ?? '';
             $data['data'][$key]['order_type_title'] = $basic_map[$value['order_type']] ?? '';
@@ -663,6 +672,22 @@ class SalesOrderService extends Service
             }else{
                 $data['data'][$key]['state_name'] = SalesOrder::$state2[$value['state']] ?? '';
             }
+
+            //合同对应的金额数据
+            $tmp = $money[$value['id']] ?? [];
+            $fee1 = $tmp[BookingList::type_one] ?? 0; //收款金额
+            $fee2 = $tmp[BookingList::type_two] ?? 0; //坏账金额
+            $fee3 = $tmp[BookingList::type_three] ?? 0; //退款金额
+            $fee4 = $tmp[BookingList::type_four] ?? 0; //补收金额
+
+            //已收金额
+            $data['data'][$key]['fee1'] = $fee1 + $fee4;
+            //未收金额
+            $data['data'][$key]['fee2'] = $value['contract_fee'] - ($fee1 + $fee4);
+            //坏账金额
+            $data['data'][$key]['fee3'] = $fee2;
+            //最终金额
+            $data['data'][$key]['fee4'] = $value['contract_fee'] + $fee4 - $fee3;
         }
 
         return $data;

+ 7 - 0
routes/api.php

@@ -185,6 +185,13 @@ Route::group(['middleware'=> ['checkLogin']],function ($route){
     $route->any('productActivityDel', 'Api\ProductActivityController@productDel');
     $route->any('productActivityDetail', 'Api\ProductActivityController@productDetail');
 
+    //金额记账
+    $route->any('BookingList', 'Api\BookingListController@customerList');
+    $route->any('BookingAdd', 'Api\BookingListController@customerAdd');
+    $route->any('BookingDel', 'Api\BookingListController@customerDel');
+    $route->any('BookingDetail', 'Api\BookingListController@customerDetail');
+    $route->any('BookingConfirm', 'Api\BookingListController@customerConfirm');
+
     //现存量
     $route->any('productInventoryList', 'Api\ProductInventoryController@productInventoryList');
     //收发存汇总