pos-gis/app/Imports/DeviceImport.php

105 lines
3.5 KiB
PHP
Raw Permalink Normal View History

2024-10-07 06:13:42 +00:00
<?php
namespace App\Imports;
use App\Http\Controllers\Controller;
use App\Models\deviceModel;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Validators\Failure;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\SkipsOnError;
use Maatwebsite\Excel\Concerns\WithValidation;
use Maatwebsite\Excel\Concerns\SkipsErrors;
class DeviceImport implements ToCollection, WithHeadingRow, SkipsOnError
{
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
use Importable, SkipsErrors;
public $validate;
public function collection(Collection $rows)
{
$checkData = '';
foreach ($rows as $key => $row)
{
if ($row['sn'] == '' || $row['sn'] == null) {
$checkData .= '<li>Kolom <b>sn</b> Pada Baris '.($key+1).' Tidak Boleh Kosong </li>';
}
if ($row['imei'] == '' || $row['imei'] == null) {
$checkData .= '<li>Kolom <b>imei</b> Pada Baris '.($key+1).' Tidak Boleh Kosong </li>';
}
if ($row['brand'] == '' || $row['brand'] == null) {
$checkData .= '<li>Kolom <b>brand</b> Pada Baris '.($key+1).' Tidak Boleh Kosong </li>';
}
if ($row['model'] == '' || $row['model'] == null) {
$checkData .= '<li>Kolom <b>model</b> Pada Baris '.($key+1).' Tidak Boleh Kosong </li>';
}
if ($row['tid'] == '' || $row['tid'] == null) {
$checkData .= '<li>Kolom <b>tid</b> Pada Baris '.($key+1).' Tidak Boleh Kosong </li>';
}
if (!is_int($row['tid'])) {
$checkData .= '<li>Kolom <b>tid</b> Pada Baris '.($key+1).' Harus Angka </li>';
}
if (!is_int($row['brand'])) {
$checkData .= '<li>Kolom <b>brand</b> Pada Baris '.($key+1).' Harus Angka Berupa ID Brand </li>';
}
// if ($row['mid'] == '' || $row['mid'] == null) {
// $checkData .= '<li>Kolom <b>mid</b> Pada Baris '.($key+1).' Tidak Boleh Kosong </li>';
// }
$checkSN = \DB::table('device')->where('sn',$row['sn'])->first();
if ($checkSN) {
$checkData .= "<li>Serial Number (sn) :<b>".$row['sn']."</b> Pada Baris ".($key+1)." Sudah Terdaftar </li>";
}
$checkBrand = \DB::table('reff_brand')->where('id_brand',$row['brand'])->count();
if ($checkBrand == 0) {
$checkData .= "<li>ID Brand :<b>".$row['brand']."</b> Pada Baris ".($key+1)." Tidak Terdaftar </li>";
}
}
$cGlobal = new Controller();
$this->validate = $checkData;
if ($checkData == '') {
foreach ($rows as $row)
{
// $get = \DB::table('device')->max('device_id');
deviceModel::create([
// 'device_id' => $get+1,
'sn' => $cGlobal->cleanString($row['sn']),
'imei' => $cGlobal->cleanString($row['imei']),
'brand' => $cGlobal->cleanString($row['brand']),
'model' => $cGlobal->cleanString($row['model']),
'tid' => $cGlobal->cleanString($row['tid']),
// 'mid' => $row['mid'],
]);
}
}
}
}