99 lines
3.2 KiB
PHP
99 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Cotations;
|
|
use App\Models\CotationsData;
|
|
use App\Models\Objectives;
|
|
use App\Mail\AlertGoalAchieved;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Carbon;
|
|
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.checkgoals');
|
|
}
|
|
|
|
public function checkObjectives()
|
|
{
|
|
$counter = 0;
|
|
//get all cotations
|
|
$cotations = CotationsData::all();
|
|
foreach ($cotations as $cotation) {
|
|
//get relevant objectives
|
|
$objectives = Objectives::where([
|
|
['status_id', 1],
|
|
['produit', $cotation->produit],
|
|
['qualite', $cotation->qualite],
|
|
['campagne', $cotation->recolte],
|
|
['echeance', $cotation->echeance],
|
|
])->orderBy('updated_at', 'DESC')->get();
|
|
|
|
foreach ($objectives as $objectif) {
|
|
//check la hausse pour avertir le vendeur (agri) de l'atteinte de son objectif
|
|
if($cotation->prix > $objectif->prix) {
|
|
//objectif atteint
|
|
$objectif->status_id = 2;
|
|
$objectif->goal_at = Carbon::now();
|
|
$objectif->save();
|
|
//envoi de message a l'agri
|
|
$email = $objectif->user->email;
|
|
//envoi du mail d'ouverture
|
|
Mail::to($email)->send(new AlertGoalAchieved($objectif));
|
|
$counter += 1;
|
|
}
|
|
}
|
|
}
|
|
return view('bo.cotations.checkgoals', compact('counter'));
|
|
}
|
|
}
|