sortBy('updated_at', \SORT_REGULAR, 'DESC'); 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 $this->sendMessageObjectif($objectif); $counter += 1; } } }//end foreach return view('bo.cotations.checkgoals', compact('counter')); } protected function sendMessageObjectif($objectif) { $objectif->load('customer'); // par défaut on va envoyer un mail, car on peut toujours // envoi du mail d'alerte Mail::to($objectif->customer->email)->send(new AlertGoalAchieved($objectif)); // pour le moment on est pas sur de pouvoir envoyer un sms $sendSms = false; // il va nous falloir le num de téléphone $toPhone = $objectif->customer->apiData->phone; // on ne garde que les numériques $toPhone = preg_replace('/[^0-9+]/', '', $toPhone); // on internationalise $toPhone = preg_replace('/^0/', '+33', $toPhone); // vérification de format de téléphone français et mobile if (preg_match('/^\+33[67][0-9]{8}$/', $toPhone)) { $sendSms = true; } // check si ce numéro est valide pour l'envoi de SMS if ($sendSms) { $message = "Objectif " . $objectif->produit . " " . $objectif->echeance . " atteint. Contacter votre TC avant 17h45."; /** @var \App\Lib\LDC\MailjetSms */ $mj = app('MailjetSms'); $mjResponse = $mj->send($message, $toPhone); /** @var \GuzzleHttp\Psr7\Response $mjResponse */ $responseStatus = $mjResponse->getStatusCode(); // "200" $responsePhrase = $mjResponse->getReasonPhrase(); // "OK" if ($responseStatus == 200) { $mjSmsResponse = $mjResponse->getBody()->getContents(); } /* $format de la $mjSmsResponse {"ID":"6302d3eb-b135-45ff-9aca-71513cf3d5a2", "From":"Jeusselin", "To":"+33682398703", "Status":{ "Code":1, "Name":"sent_pending", "Description":"Message is being sent"}, "Cost":{ "Value":0.04, "Currency":"EUR"}, "CreationTS":1617289400, "Text":"Objectif Blé Avril 21 atteint. Contacter votre TC avant 17h45.", "SmsCount":1} */ }//end if } }