Ver Fonte

二次加工-参与人变动时触发事件回调重置统计表开始时间

Zhouzhendong há 5 anos atrás
pai
commit
c4c8ac883b

+ 24 - 0
app/Events/ResetProcessStatisticStartDateEvent.php

@@ -0,0 +1,24 @@
+<?php
+
+namespace App\Events;
+
+use App\Process;
+use Illuminate\Broadcasting\InteractsWithSockets;
+use Illuminate\Foundation\Events\Dispatchable;
+use Illuminate\Queue\SerializesModels;
+
+class ResetProcessStatisticStartDateEvent
+{
+    use Dispatchable, InteractsWithSockets, SerializesModels;
+
+    public $process;
+    /**
+     * Create a new event instance.
+     * @param Process $process
+     * @return void
+     */
+    public function __construct(Process $process)
+    {
+        $this->process = $process;
+    }
+}

+ 26 - 4
app/Http/Controllers/ProcessController.php

@@ -4,6 +4,7 @@ namespace App\Http\Controllers;
 
 use App\Commodity;
 use App\CommodityBarcode;
+use App\Events\ResetProcessStatisticStartDateEvent;
 use App\LaborReport;
 use App\Owner;
 use App\Process;
@@ -20,6 +21,7 @@ use App\UserDutyCheck;
 use App\UserLabor;
 use Carbon\Carbon;
 use Exception;
+use Illuminate\Database\Eloquent\Builder;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\DB;
@@ -319,6 +321,8 @@ class ProcessController extends Controller
         }
         return $process_id;
     }
+
+    //新增参与人
     public function storeProcessDailyParticipant(Request $request){
         if(!Gate::allows('二次加工管理-登记工时')){ return redirect(url('/'));  }
         $errors=$this->validatorProcessDailyParticipant($request)->errors();
@@ -355,6 +359,11 @@ class ProcessController extends Controller
         $processDailyParticipant->load(['processDaily'=>function($query){
             $query->with('process');
         }]);
+
+        // 重置统计开始时间
+        event(new ResetProcessStatisticStartDateEvent($processDailyParticipant->processDaily->process));
+
+
         if ($processDailyParticipant->processDaily && $processDailyParticipant->processDaily->process &&
             $processDailyParticipant->processDaily->process->status=='交接完成'){
             $this->statistic($processDailyParticipant->processDaily->process);
@@ -908,9 +917,13 @@ class ProcessController extends Controller
                 unset($processDaily);
             }
         }
-        ProcessDaily::destroy($processDailiesId);
-        ProcessDailyParticipant::whereIn('process_daily_id',$processDailiesId)->delete();
+
         $process=Process::find($process_id);
+        if (count($processDailiesId) > 0){
+            ProcessDaily::destroy($processDailiesId);
+            ProcessDailyParticipant::whereIn('process_daily_id',$processDailiesId)->delete();
+            event(new ResetProcessStatisticStartDateEvent($process));
+        }
         if (!$is_process_daily && $process){
             $ProcessDailyNow=ProcessDaily::create([
                 'process_id'=>$process_id,
@@ -920,7 +933,7 @@ class ProcessController extends Controller
             ]);
             $processDailies->push($ProcessDailyNow);
         }
-        $today=Carbon::now()->format('Y-m-d');
+        $today=Carbon::parse($start_date)->format('Y-m-d');
         $this->createDeficiencyData($processDailies,$today);
         $processDailies=ProcessDaily::with('processDailyParticipants')->where('process_id',$process_id)
             ->orderBy('date','DESC')->get();
@@ -963,7 +976,16 @@ class ProcessController extends Controller
 
     //删除临时工工作记录
     public function destroyDailyParticipant($id){
-        if (ProcessDailyParticipant::destroy($id)) return ['success'=>true];
+        $pro = ProcessDailyParticipant::query()->with(["processDaily"=>function($query){
+            /** @var Builder $query */
+            $query->with("process");
+        }])->find($id);
+        if ($pro->delete()) {
+
+            event(new ResetProcessStatisticStartDateEvent($pro->processDaily->process));
+
+            return ['success'=>true];
+        }
         return ['success'=>false , 'data'=>'记录不存在!'];
     }
     //导入商品数据

+ 6 - 11
app/Http/Controllers/TestController.php

@@ -9,6 +9,7 @@ use App\City;
 use App\Commodity;
 use App\CommodityBarcode;
 use App\Events\CancelOrder;
+use App\Events\ResetProcessStatisticStartDateEvent;
 use App\InventoryAccountMission;
 use App\InventoryCompare;
 use App\InventoryDailyLog;
@@ -108,17 +109,11 @@ class TestController extends Controller
     }
 
     public function test2(){
-        $db = DB::connection('oracle');
-        $db->beginTransaction();
-        try{
-            $bas = $db->select("SELECT * FROM BAS_CODES where codeid = 'IVC_STS' and code = '00'");
-            foreach ($bas as $a) {
-                $row = $db->update(DB::raw("UPDATE BAS_CODES set codename_c = '创建' WHERE codeid = ? and code = ?"), [$a->codeid, $a->code]);
-                if ($row > 0) $db->commit();
-            }
-        }catch (\Exception $e){
-            $db->rollBack();
-        }
+        $a = Unit::query()->first();
+        dd($a);
+        /** @var Process $process */
+        $process = Process::query()->first();
+        event(new ResetProcessStatisticStartDateEvent($process));
     }
 
     function packageFromLog(Request $request)

+ 60 - 0
app/Listeners/ResetProcessStatisticStartDateListener.php

@@ -0,0 +1,60 @@
+<?php
+
+namespace App\Listeners;
+
+use App\Events\ResetProcessStatisticStartDateEvent;
+use App\Process;
+use App\ProcessStatistic;
+use Carbon\Carbon;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Database\Eloquent\Builder;
+use Illuminate\Queue\InteractsWithQueue;
+
+class ResetProcessStatisticStartDateListener
+{
+    /**
+     * Create the event listener.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        //
+    }
+
+    /**
+     * Handle the event.
+     *
+     * @param  ResetProcessStatisticStartDateEvent  $event
+     * @return void
+     */
+    public function handle(ResetProcessStatisticStartDateEvent $event)
+    {
+        $process = $event->process;
+        $process->load(["processDailies"=>function($query){
+            /** @var Builder $query */
+            $query->orderBy("date")->withCount(["processDailyParticipants"]);
+        },"processStatistic"=>function($query){
+            /** @var Builder $query */
+            $query->select("process_id","started_at");
+        }]);
+
+        $dailies = $process->processDailies ?? false;
+        if ($dailies){
+            foreach ($dailies as $daily){
+                if ($daily->process_daily_participants_count > 0){
+                    $this->comparisonReplace($process->processStatistic,$daily->date." 09:00:00");
+                    return;
+                }
+            }
+        }
+        $this->comparisonReplace($process->processStatistic,null);
+    }
+
+    private function comparisonReplace($statistic, $date)
+    {
+        if ($statistic->started_at != $date){
+            ProcessStatistic::query()->where("process_id",$statistic->process_id)->update(["started_at"=>$date]);
+        }
+    }
+}

+ 4 - 0
app/Process.php

@@ -50,6 +50,10 @@ class Process extends Model
         return $this->hasMany('App\Sign','signable_id','id')
             ->where('signable_type','processes')->where('field','unit_price');
     }
+    public function processStatistic()
+    {
+        return $this->hasOne(ProcessStatistic::class,"process_id","id");
+    }
 
     public function automaticSupplementSign(){
         return $this->hasOne('App\Sign','signable_id','id')

+ 3 - 0
app/Providers/EventServiceProvider.php

@@ -27,6 +27,9 @@ class EventServiceProvider extends ServiceProvider
         'App\Events\CustomerStored' =>[
             'App\Listeners\FulfillPackageCustomer'
         ],
+        'App\Events\ResetProcessStatisticStartDateEvent' =>[
+            'App\Listeners\ResetProcessStatisticStartDateListener'
+        ],
     ];
 
     /**