Jelajahi Sumber

工单处理 添加工单商品详情及图片详情

ajun 4 tahun lalu
induk
melakukan
e2eb4d04a3

+ 2 - 2
app/Http/Controllers/TestController.php

@@ -427,7 +427,7 @@ sql;
         ]);
     }
 
-    public function teset12313(){
-        dd(route("discharge.storeApi"));
+    public function testStr(){
+        $order = factory(Order::class)->create();
     }
 }

+ 85 - 0
app/Http/Controllers/WorkOrderCommoditiesController.php

@@ -0,0 +1,85 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use App\WorkOrderCommodities;
+use Illuminate\Http\Request;
+
+class WorkOrderCommoditiesController extends Controller
+{
+    /**
+     * Display a listing of the resource.
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function index()
+    {
+        //
+    }
+
+    /**
+     * Show the form for creating a new resource.
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function create()
+    {
+        //
+    }
+
+    /**
+     * Store a newly created resource in storage.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @return \Illuminate\Http\Response
+     */
+    public function store(Request $request)
+    {
+        //
+    }
+
+    /**
+     * Display the specified resource.
+     *
+     * @param  \App\WorkOrderCommodities  $workOrderCommodities
+     * @return \Illuminate\Http\Response
+     */
+    public function show(WorkOrderCommodities $workOrderCommodities)
+    {
+        //
+    }
+
+    /**
+     * Show the form for editing the specified resource.
+     *
+     * @param  \App\WorkOrderCommodities  $workOrderCommodities
+     * @return \Illuminate\Http\Response
+     */
+    public function edit(WorkOrderCommodities $workOrderCommodities)
+    {
+        //
+    }
+
+    /**
+     * Update the specified resource in storage.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @param  \App\WorkOrderCommodities  $workOrderCommodities
+     * @return \Illuminate\Http\Response
+     */
+    public function update(Request $request, WorkOrderCommodities $workOrderCommodities)
+    {
+        //
+    }
+
+    /**
+     * Remove the specified resource from storage.
+     *
+     * @param  \App\WorkOrderCommodities  $workOrderCommodities
+     * @return \Illuminate\Http\Response
+     */
+    public function destroy(WorkOrderCommodities $workOrderCommodities)
+    {
+        //
+    }
+}

+ 85 - 0
app/Http/Controllers/WorkOrderImageController.php

@@ -0,0 +1,85 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use App\WorkOrderImage;
+use Illuminate\Http\Request;
+
+class WorkOrderImageController extends Controller
+{
+    /**
+     * Display a listing of the resource.
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function index()
+    {
+        //
+    }
+
+    /**
+     * Show the form for creating a new resource.
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function create()
+    {
+        //
+    }
+
+    /**
+     * Store a newly created resource in storage.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @return \Illuminate\Http\Response
+     */
+    public function store(Request $request)
+    {
+        //
+    }
+
+    /**
+     * Display the specified resource.
+     *
+     * @param  \App\WorkOrderImage  $workOrderImage
+     * @return \Illuminate\Http\Response
+     */
+    public function show(WorkOrderImage $workOrderImage)
+    {
+        //
+    }
+
+    /**
+     * Show the form for editing the specified resource.
+     *
+     * @param  \App\WorkOrderImage  $workOrderImage
+     * @return \Illuminate\Http\Response
+     */
+    public function edit(WorkOrderImage $workOrderImage)
+    {
+        //
+    }
+
+    /**
+     * Update the specified resource in storage.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @param  \App\WorkOrderImage  $workOrderImage
+     * @return \Illuminate\Http\Response
+     */
+    public function update(Request $request, WorkOrderImage $workOrderImage)
+    {
+        //
+    }
+
+    /**
+     * Remove the specified resource from storage.
+     *
+     * @param  \App\WorkOrderImage  $workOrderImage
+     * @return \Illuminate\Http\Response
+     */
+    public function destroy(WorkOrderImage $workOrderImage)
+    {
+        //
+    }
+}

+ 4 - 0
app/Providers/AppServiceProvider.php

@@ -185,6 +185,8 @@ use App\Services\OrderPackageRemarkService;
 use App\Services\LaborCompanyService;
 use App\Services\NotificationService;
 use App\Services\WorkOrderDetailService;
+use App\Services\WorkOrderCommoditiesService;
+use App\Services\WorkOrderImageService;
 
 class AppServiceProvider extends ServiceProvider
 {
@@ -378,7 +380,9 @@ class AppServiceProvider extends ServiceProvider
         app()->singleton('WaybillFinancialService', WaybillFinancialService::class);
         app()->singleton('WaybillService', WaybillService::class);
         app()->singleton('WeighExceptedService', WeighExceptedService::class);
+        app()->singleton('WorkOrderCommoditiesService',WorkOrderCommoditiesService::class);
         app()->singleton('WorkOrderDetailService',WorkOrderDetailService::class);
+        app()->singleton('WorkOrderImageService',WorkOrderImageService::class);
         app()->singleton('WorkOrderService',WorkOrderService::class);
         app()->singleton('WorkOrderTypeService',WorkOrderTypeService::class);
     }

+ 13 - 0
app/Services/WorkOrderCommoditiesService.php

@@ -0,0 +1,13 @@
+<?php 
+
+namespace App\Services;
+
+use App\Traits\ServiceAppAop;
+use App\WorkOrderCommodities;
+
+class WorkOrderCommoditiesService
+{
+    use ServiceAppAop;
+    protected $modelClass=WorkOrderCommodities::class;
+
+}

+ 13 - 0
app/Services/WorkOrderImageService.php

@@ -0,0 +1,13 @@
+<?php 
+
+namespace App\Services;
+
+use App\Traits\ServiceAppAop;
+use App\WorkOrderImage;
+
+class WorkOrderImageService
+{
+    use ServiceAppAop;
+    protected $modelClass=WorkOrderImage::class;
+
+}

+ 19 - 1
app/Services/WorkOrderService.php

@@ -103,7 +103,11 @@ class WorkOrderService
         $orderIssueType = OrderIssueType::query()->firstOrCreate(['name' => '破损']);
         $order = Order::query()->where('code', $param['order_no'])->first();
         $remake = $param['remark'] ?? '';
-        return $this->createAndNotification($order, $orderIssueType, $remake);
+        $workOrder = $this->createAndNotification($order, $orderIssueType, $remake);
+        $workOrder->addPackageImages($param['packageImages']); // 外包装图片
+        $workOrder->addCommodityImage($param['commodityImage']); // 内物破损图片
+        $workOrder->addDealImages($param['dealImages']); // 交易图片
+        return $workOrder;
     }
 
     /***
@@ -139,6 +143,20 @@ class WorkOrderService
         return $this->createAndNotification($order, $orderIssueType, $remake);
     }
 
+    /**
+     * 快递丢件
+    */
+    public function createLoseWorkOrder($param):WorkOrder
+    {
+        $orderIssueType = OrderIssueType::query()->firstOrCreate(['name' => '快递丢件']);
+        $order = Order::query()->where('code', $param['order_no'])->first();
+        $remake = $param['remark'] ?? '';
+        $workOrder = $this->createAndNotification($order, $orderIssueType, $remake);
+        $workOrder->addPackageImages($param['packageImages']);
+        $workOrder->addDealImages($param['dealImages']);
+        return $workOrder;
+    }
+
     /**
      * 工单审核
      * @param $wordOrder

+ 92 - 15
app/WorkOrder.php

@@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Model;
 
 use App\Traits\ModelLogChanging;
 use Illuminate\Database\Eloquent\Relations\BelongsTo;
+use Illuminate\Database\Eloquent\Relations\HasMany;
 use Illuminate\Database\Eloquent\SoftDeletes;
 use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\Gate;
@@ -111,27 +112,58 @@ class WorkOrder extends Model
     // 关联货主
     public function owner(): BelongsTo
     {
-        return $this->belongsTo(Owner::class,'owner_id');
+        return $this->belongsTo(Owner::class, 'owner_id');
     }
 
     // 工单类型
     public function type(): BelongsTo
     {
-        return $this->BelongsTo(WorkOrderType::class,'work_order_type_id');
+        return $this->BelongsTo(WorkOrderType::class, 'work_order_type_id');
     }
 
     // 生成问题件类型
-    public function issueType():BelongsTo
+    public function issueType(): BelongsTo
     {
-        return $this->belongsTo(OrderIssueType::class,'order_issue_type_id');
+        return $this->belongsTo(OrderIssueType::class, 'order_issue_type_id');
     }
 
     /** 对应问题件 */
     public function orderIssue(): BelongsTo
     {
-        return $this->belongsTo(OrderIssue::class,'order_id','order_id');
+        return $this->belongsTo(OrderIssue::class, 'order_id', 'order_id');
     }
 
+    // 图片
+    public function image(): HasMany
+    {
+        return $this->hasMany(WorkOrderImage::class);
+    }
+
+    // 外包装图片
+    public function packageImages(): HasMany
+    {
+        return $this->hasMany(WorkOrderImage::class)->where('type', 0);
+    }
+
+    // 内物破损图片
+    public function commodityImage(): HasMany
+    {
+        return $this->hasMany(WorkOrderImage::class)->where('type', 1);
+    }
+
+    // 交易图片
+    public function dealImages(): HasMany
+    {
+        return $this->hasMany(WorkOrderImage::class)->where('type', 2);
+    }
+
+    // 退款图片
+    public function refundImages(): HasMany
+    {
+        return $this->hasMany(WorkOrderImage::class)->where('type', 3);
+    }
+
+
     public function scopeFilter($query, $filters)
     {
         return $filters->apply($query);
@@ -141,16 +173,16 @@ class WorkOrder extends Model
     /** 默认 with 参数 */
     public function scopeDefaultWith($query)
     {
-        $query->with(['type','owner','issueType','creator','reviewer','order'=>function($query){
+        $query->with(['type', 'owner', 'issueType', 'creator', 'reviewer', 'order' => function ($query) {
             /** @var $query Builder */
-            $query->with('packages','logistic','owner');
-        },'orderIssue'=>function($query){
+            $query->with('packages', 'logistic', 'owner');
+        }, 'orderIssue' => function ($query) {
             /** @var $query Builder */
-            $query->with(['issueType','logs'=>function($query){
-                if (Gate::denies('订单管理-问题件-客户不可见')){
+            $query->with(['issueType', 'logs' => function ($query) {
+                if (Gate::denies('订单管理-问题件-客户不可见')) {
                     $query->with('user')->orderByDesc('created_at');
-                } else{
-                    $query->with('user')->where('tag','=',0)->orderByDesc('created_at');
+                } else {
+                    $query->with('user')->where('tag', '=', 0)->orderByDesc('created_at');
                 }
             }]);
         }]);
@@ -159,11 +191,56 @@ class WorkOrder extends Model
     public function notification()
     {
         $user = Auth::user();
-        $this->loadMissing('owner','order');
+        $this->loadMissing('owner', 'order');
         $remark = $this->remark;
         $ownerName = $this->owner->name ?? '';
         $clientCode = $this->order->client_code ?? '';
-        $msg = $user["name"]."建立了新工单<br/>".$ownerName.":".$clientCode."<br/>".$remark;
-        NotificationService::SingleRegister($msg,$clientCode,"订单管理-问题件");
+        $msg = $user["name"] . "建立了新工单<br/>" . $ownerName . ":" . $clientCode . "<br/>" . $remark;
+        NotificationService::SingleRegister($msg, $clientCode, "订单管理-问题件");
+    }
+
+
+    public function addPackageImages($images)
+    {
+        foreach ($images as $image) {
+            $count = $this->packageImages()->count();
+            /** @var WorkOrderImage $workOrderImage */
+            $workOrderImage = WorkOrderImage::query()->create(['type' => 0, 'amount' => ++$count]);
+            $workOrderImage->saveFile($image);
+            $this->packageImages()->save($workOrderImage);
+        }
+    }
+
+    public function addCommodityImage($images)
+    {
+        foreach ($images as $image) {
+            $count = $this->commodityImage()->count();
+            /** @var WorkOrderImage $workOrderImage */
+            $workOrderImage = WorkOrderImage::query()->create(['type' => 1, 'amount' => ++$count]);
+            $workOrderImage->saveFile($image);
+            $this->commodityImage()->save($workOrderImage);
+        }
+    }
+
+    public function addDealImages($images)
+    {
+        foreach ($images as $image) {
+            $count = $this->dealImages()->count();
+            /** @var WorkOrderImage $workOrderImage */
+            $workOrderImage = WorkOrderImage::query()->create(['type' => 2, 'amount' => ++$count]);
+            $workOrderImage->saveFile($image);
+            $this->dealImages()->save($workOrderImage);
+        }
+    }
+
+    public function addRefundImage($images)
+    {
+        foreach ($images as $image) {
+            $count = $this->refundImages()->count();
+            /** @var WorkOrderImage $workOrderImage */
+            $workOrderImage = WorkOrderImage::query()->create(['type' => 3, 'amount' => ++$count]);
+            $workOrderImage->saveFile($image);
+            $this->refundImages()->save($workOrderImage);
+        }
     }
 }

+ 33 - 0
app/WorkOrderCommodities.php

@@ -0,0 +1,33 @@
+<?php
+
+namespace App;
+
+use App\Traits\ModelTimeFormat;
+use Illuminate\Database\Eloquent\Model;
+
+use App\Traits\ModelLogChanging;
+use Illuminate\Database\Eloquent\Relations\BelongsTo;
+
+class WorkOrderCommodities extends Model
+{
+    use ModelLogChanging;
+    use ModelTimeFormat;
+
+    //
+    protected $fillable = [
+        'work_order_id',
+        'logistic_number',
+        'commodity_id',
+        'amount',
+    ];
+
+    public function workOrder(): BelongsTo
+    {
+        return $this->belongsTo(WorkOrder::class);
+    }
+
+    public function commodity(): BelongsTo
+    {
+        return $this->belongsTo(Commodity::class);
+    }
+}

+ 139 - 0
app/WorkOrderImage.php

@@ -0,0 +1,139 @@
+<?php
+
+namespace App;
+
+use Illuminate\Database\Eloquent\Model;
+
+use App\Traits\ModelLogChanging;
+use Illuminate\Database\Eloquent\Relations\BelongsTo;
+use Illuminate\Database\Eloquent\Relations\HasOne;
+use Illuminate\Support\Str;
+use Intervention\Image\Facades\Image;
+use Ramsey\Uuid\Uuid;
+
+class WorkOrderImage extends Model
+{
+    use ModelLogChanging;
+
+    protected $fillable = [
+        'work_order_id',
+        'type',
+        'number',
+    ];
+
+    static public $enums = [
+        'type' => [
+            '外包装图片' => 0,
+            '内物破碎图片' => 1,
+            '交易截图' => 2,
+            '退款成功截图' => 2,
+        ],
+    ];
+
+    function __construct(array $attributes = [])
+    {
+        foreach (self::$enums as &$enum) {
+            $enum = $enum + array_flip($enum);
+        }
+        parent::__construct($attributes);
+    }
+
+    public function getStatusAttribute($value)
+    {
+        if (!$value) return '';
+        return self::$enums['type'][$value];
+    }
+
+    public function setTypeAttribute($value)
+    {
+        if (!$value) return ;
+        if (is_numeric($value)) {
+            $this->attributes['type'] = $value;
+        } else {
+            $this->attributes['type'] = self::$enums['type'][$value];
+        }
+    }
+
+    public function workOrder(): BelongsTo
+    {
+        return $this->belongsTo(WorkOrder::class);
+    }
+
+    public function uploadFile(): HasOne
+    {
+        return $this->hasOne(UploadFile::class,'table_id','id')->where('table_name','work_order_image');
+    }
+
+    public function saveFile($image): bool
+    {
+        if (!$this->checkFile($image)) return false;
+        $tmpFile = $image->getRealPath();
+        $fileSuffix = $image->getClientOriginalExtension();
+        $dirPath = $this->getStorageDirPath();
+        $fileName = date('ymd') . '-' . Uuid::uuid1();
+        $pathName = $dirPath . $fileName . $fileSuffix;
+        if ($image->height() > $image->width()) {
+            $image->heighten(250)->save($pathName);
+        } else {
+            $image->widen(250)->save($pathName);
+        }
+        $result = move_uploaded_file($tmpFile, $pathName);
+
+        $img = Image::make($result);
+        $img->heighten(250)->save($pathName);
+        $this->uploadFile()->create(
+            ['table_name' => $this->getTable(), 'table_id' => $this['id'], 'url' => '/files/workOrder/'.$fileName, 'type' => $fileSuffix]
+        );
+//        UploadFile::query()->create(['table_name' => $this->getTable(), 'table_id' => $this['id'], 'url' => '/files/workOrder/'.$fileName, 'type' => $fileSuffix]);
+        return true;
+    }
+
+    public function checkFile($image): bool
+    {
+        $tmpFile = $image->getRealPath();
+        $fileSuffix = $image->getClientOriginalExtension();
+        if (!is_uploaded_file($tmpFile)) return false;
+        if ($image->getSize() > 5 * 1024 * 1024) return false;
+        if (!in_array($fileSuffix,[ 'gif','image','jpeg','jpg','png','svg'])) return false;
+        return true;
+    }
+
+    public function getStorageDirPath(): string
+    {
+        $path = ['app','public','files','workOrder'];
+        $path = join(DIRECTORY_SEPARATOR,$path);
+        $dirPath = storage_path($path);
+
+        if (!file_exists($dirPath)) {
+            mkdir($dirPath);
+        }
+        return $dirPath;
+    }
+
+    public function updateFile($image)
+    {
+        $this->deleteStorageFile();
+        $this->uploadFile()->delete();
+        $this->saveFile($image);
+    }
+
+    public function deleteStorageFile()
+    {
+        $path = $this->getPathName();
+        if (!$path) return;
+        if (file_exists($path)){
+            unlink($path);
+        }
+    }
+
+    public function getPathName(): ?string
+    {
+        if (!$this->uploadFile) return null;
+        $names = [];
+        preg_match("/([\w]+)$/", $this->uploadFile->url,$names);
+        $path = ['app','public','files','workOrder' , $names[0],".{$this->uploadFile->type}"];
+        $path = join(DIRECTORY_SEPARATOR,$path);
+        return storage_path($path);
+    }
+
+}

+ 12 - 0
database/factories/WorkOrderCommoditiesFactory.php

@@ -0,0 +1,12 @@
+<?php
+
+/** @var \Illuminate\Database\Eloquent\Factory $factory */
+
+use App\WorkOrderCommodities;
+use Faker\Generator as Faker;
+
+$factory->define(WorkOrderCommodities::class, function (Faker $faker) {
+    return [
+        //
+    ];
+});

+ 12 - 0
database/factories/WorkOrderImageFactory.php

@@ -0,0 +1,12 @@
+<?php
+
+/** @var \Illuminate\Database\Eloquent\Factory $factory */
+
+use App\WorkOrderImage;
+use Faker\Generator as Faker;
+
+$factory->define(WorkOrderImage::class, function (Faker $faker) {
+    return [
+        //
+    ];
+});

+ 35 - 0
database/migrations/2021_09_22_160325_create_work_order_commodities_table.php

@@ -0,0 +1,35 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class CreateWorkOrderCommoditiesTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('work_order_commodities', function (Blueprint $table) {
+            $table->id();
+            $table->integer('work_order_id')->index()->comment('工单');
+            $table->string('logistic_number')->comment('sku');
+            $table->integer('commodity_id')->comment('sku');
+            $table->integer('amount')->comment('数量');
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('work_order_commodities');
+    }
+}

+ 34 - 0
database/migrations/2021_09_22_162827_create_work_order_images_table.php

@@ -0,0 +1,34 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class CreateWorkOrderImagesTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('work_order_images', function (Blueprint $table) {
+            $table->id();
+            $table->integer('work_order_id')->index()->comment('工单');
+            $table->tinyInteger('type')->comment('类别');
+            $table->tinyInteger('number')->comment('序号');
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('work_order_images');
+    }
+}

+ 16 - 0
database/seeds/WorkOrderCommoditiesSeeder.php

@@ -0,0 +1,16 @@
+<?php
+
+use Illuminate\Database\Seeder;
+
+class WorkOrderCommoditiesSeeder extends Seeder
+{
+    /**
+     * Run the database seeds.
+     *
+     * @return void
+     */
+    public function run()
+    {
+        //
+    }
+}

+ 16 - 0
database/seeds/WorkOrderImageSeeder.php

@@ -0,0 +1,16 @@
+<?php
+
+use Illuminate\Database\Seeder;
+
+class WorkOrderImageSeeder extends Seeder
+{
+    /**
+     * Run the database seeds.
+     *
+     * @return void
+     */
+    public function run()
+    {
+        //
+    }
+}