|
|
|
@ -3,10 +3,8 @@
|
|
|
|
|
namespace App\Models; |
|
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
|
|
|
use App\Models\RequestMaterial; |
|
|
|
|
use App\Models\Activity; |
|
|
|
|
use App\Models\ReportActivityMaterial; |
|
|
|
|
|
|
|
|
|
use App\Models\{Activity,RequestMaterial,ReportActivityMaterial}; |
|
|
|
|
use Illuminate\Support\Facades\DB; |
|
|
|
|
class AssignMaterial extends Model |
|
|
|
|
{ |
|
|
|
|
protected $table = 'assign_material_to_activity'; |
|
|
|
@ -23,24 +21,25 @@ class AssignMaterial extends Model
|
|
|
|
|
protected $casts = [ |
|
|
|
|
'id' => 'integer', |
|
|
|
|
'budget' => 'string', |
|
|
|
|
'qty_planning' => 'float' |
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
public static function boot() { |
|
|
|
|
parent::boot(); |
|
|
|
|
|
|
|
|
|
DB::transaction(function () { |
|
|
|
|
static::created(function($data) { |
|
|
|
|
$activity = Activity::find($data->activity_id); |
|
|
|
|
$material = RequestMaterial::where("id", $data->material_id)->first(); |
|
|
|
|
$activity->rencana_biaya += floatval($material->price) * floatval($data->qty_planning); |
|
|
|
|
$activity = Activity::find((int)$data->activity_id); |
|
|
|
|
$material = RequestMaterial::find((int)$data->material_id); |
|
|
|
|
$activity->rencana_biaya += (doubleval($material->price) * doubleval($data->qty_planning)); |
|
|
|
|
$activity->save(); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
static::deleted(function($data) { |
|
|
|
|
$reportActivities = ReportActivityMaterial::where('assign_material_id', $data->id)->delete(); |
|
|
|
|
$activity = Activity::where('id', $data->activity_id)->first(); |
|
|
|
|
$activity->rencana_biaya -= floatval($data->budget) * floatval($data->qty_planning); |
|
|
|
|
ReportActivityMaterial::where('assign_material_id', $data->id)->delete(); |
|
|
|
|
$activity = Activity::find($data->activity_id); |
|
|
|
|
$activity->rencana_biaya -= (doubleval($data->budget) * doubleval($data->qty_planning)); |
|
|
|
|
$activity->save(); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
}, 5); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|