80 lines
2.6 KiB
PHP
80 lines
2.6 KiB
PHP
<?php
|
||
|
||
namespace App\Models\Pertanian;
|
||
|
||
use App\Models\Concerns\KembalikanStatusPetaniKeDraf;
|
||
use Illuminate\Database\Eloquent\Model;
|
||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||
|
||
use App\Models\Pertanian\Petani;
|
||
|
||
class SurveyPanen extends Model
|
||
{
|
||
use SoftDeletes, KembalikanStatusPetaniKeDraf;
|
||
|
||
protected $table = 'survey_panen';
|
||
|
||
protected $fillable = [
|
||
'uid',
|
||
'petani_id',
|
||
'dasar_penentuan_panen',
|
||
'frekuensi_panen_pertahun',
|
||
'bulan_panen_utama',
|
||
'hasil_komoditas_utama_permusim',
|
||
'hasil_komoditas_lainnya_permusim',
|
||
'disimpan_untuk_sendiri',
|
||
'hasil_panen_disimpan',
|
||
'tujuan_disimpan',
|
||
'praktik_sortasi',
|
||
'bentuk_sortasi',
|
||
'hasil_panen_reject',
|
||
'jumlah_panen_reject',
|
||
'tindakan_panen_reject',
|
||
'upah_tenaga_panen',
|
||
'biaya_pengangkutan_panen',
|
||
'biaya_wadah_panen',
|
||
'biaya_sewa_alat_panen',
|
||
'biaya_panen_lainnya',
|
||
'total_estimasi_biaya_panen',
|
||
'estimasi_kehilangan_akibat_opt',
|
||
'estimasi_kehilangan_akibat_hama_penyakit',
|
||
'created_by',
|
||
'updated_by',
|
||
'deleted_by',
|
||
];
|
||
|
||
protected static function booted(): void
|
||
{
|
||
static::saved(function (SurveyPanen $survey) {
|
||
|
||
$petani = Petani::find($survey->petani_id);
|
||
$skor = SkorPetani::where('petani_id', $petani->id)->orderBy('id')->first();
|
||
if (!$skor) {
|
||
$petaniUid = $petani->uid;
|
||
$part = explode('-', $petaniUid);
|
||
$skor = SkorPetani::create([
|
||
'petani_id' => $petani->id,
|
||
'uid' => 'SKR-' . $part[1] . '-' . strtoupper(Str::random(10)),
|
||
'created_by' => $petani->created_by,
|
||
'updated_by' => $petani->updated_by
|
||
]);
|
||
}
|
||
|
||
$skor->update([
|
||
'skor_d2_k3_1_penentuan_pematangan' => $survey->dasar_penentuan_panen,
|
||
'skor_d2_k3_5_sortasi_awal_pemisahan_kualitas' => $survey->praktik_sortasi,
|
||
// Hambatan mutu = "Tingkat hasil panen ditolak pembeli" (TingkatPanenReject, 1–5).
|
||
// Kolomnya bernama hasil_panen_reject, tetapi isinya tingkat reject.
|
||
'skor_d6_k5_1_hambatan_mutu' => $survey->hasil_panen_reject,
|
||
]);
|
||
});
|
||
}
|
||
|
||
public function petani(): BelongsTo
|
||
{
|
||
return $this->belongsTo(Petani::class, 'petani_id');
|
||
}
|
||
}
|