ldc-jeusselin/app/Http/Controllers/Bo/CotationsController.php
2021-03-09 18:02:07 +01:00

63 lines
1.8 KiB
PHP

<?php
namespace App\Http\Controllers\Bo;
use App\Http\Controllers\Controller;
use App\Models\Cotations;
use App\Models\CotationsData;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use App\Rules\Cotationsxml;
class CotationsController extends Controller
{
public function index(Request $request)
{
$cotations = Cotations::all();
return view('bo.cotations.index',compact('cotations'));
}
public function create()
{
return view('bo.cotations.create');
}
public function store(Request $request)
{
$request->validate([
'xmlfile' => ['required','mimes:application/xml,xml','max:2048',new Cotationsxml],
]);
$is_published = isset($request->is_published) ? true : false;
$xmlfile = $request->xmlfile;
$path = $xmlfile->store('/cotations/' . date('Y') . '/' . date('m') . '/' . date("d"));
//save data to show in pro view
$xml = simplexml_load_string(Storage::get($path));
$json = json_encode($xml);
$data = json_decode($json,TRUE);
$cotations = $data['enregistrement'];
//first truncate
CotationsData::truncate();
foreach($cotations as $cotation) {
$enreg = new CotationsData();
$enreg->produit = $cotation['Produit'];
$enreg->qualite = $cotation['Qualité'];
$enreg->recolte = $cotation['Récolte'];
$enreg->echeance = $cotation['Échéance'];
$enreg->prix = isset($cotation['Prix']) ? $cotation['Prix'] : null;
$enreg->save();
}
$c = new Cotations();
$c->file_path = $path;
$c->is_published = $is_published;
$c->save();
return redirect()->route('bo.cotations.index');
}
}