<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use App\Controller\AppBundle\Email\Mail;
use DateTime;
use GuzzleHttp\Client;
use SendinBlue\Client\Api\SMTPApi;
use SendinBlue\Client\Configuration;
use SendinBlue\Client\Model\SendSmtpEmail;
use function App\Entity\env;
use function App\Entity\optional;
use function App\Entity\phpenv;
use const App\Entity\Offerte;
/**
* Offerte
*/
#[ORM\Table(name: 'offerte')]
#[ORM\Entity(repositoryClass: 'App\Repository\OfferteRepository')]
class Offerte
{
/**
* @var int
*/
#[ORM\Column(name: 'id', type: 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
public $id;
/**
* @var string
*/
#[ORM\Column(name: 'offerte_id', type: 'text')]
public $offerte_id;
/**
* @var string
*/
#[ORM\Column(name: 'path', type: 'text')]
public $path;
/**
* @var string
*/
#[ORM\Column(name: 'file', type: 'text')]
public $file;
/**
* @var string
*/
#[ORM\Column(name: 'fileFinal', type: 'text', nullable: true)]
public $fileFinal;
/**
* @var string
*/
#[ORM\Column(name: 'anva_key', type: 'text')]
public $anva_key;
/**
* @var string
*/
#[ORM\Column(name: 'token', type: 'text')]
public $token;
/**
* @var string
*/
#[ORM\Column(name: 'created_at', type: 'datetime')]
public $created_at;
/**
* @var string
*/
#[ORM\Column(name: 'confirmed_at', type: 'datetime', nullable: true)]
public $confirmed_at;
#[ORM\ManyToOne(targetEntity: 'App\Entity\OfferteCart', inversedBy: 'id', cascade: ['persist'])]
public $offerte_cart;
/**
* @var Consultant
*/
#[ORM\ManyToOne(targetEntity: 'App\Entity\Consultant', inversedBy: 'id', cascade: ['persist'])]
public $consultant;
/**
* @var boolean
*/
#[ORM\Column(name: 'active', type: 'boolean')]
public $active;
/**
* @var string
*/
#[ORM\Column(name: 'agent', type: 'string')]
public $agent;
/**
* @var string
*/
#[ORM\Column(name: 'name', type: 'string')]
public $name;
/**
* @var string
*/
#[ORM\Column(name: 'ip', type: 'string', nullable: true)]
public $ip;
/**
* @var boolean
*/
#[ORM\Column(name: 'indication', type: 'boolean')]
public $indication;
/**
* @var boolean
*/
#[ORM\Column(name: 'empty_offerte', type: 'boolean', nullable: true)]
public $empty = false;
public function __construct($path, Consultant $consultant, $agent, $name, $anva_key = null, $offerCart = null)
{
$this->path = $path;
$this->anva_key = $anva_key;
$this->consultant = $consultant;
$this->agent = $agent;
$this->active = true;
$this->name = $name;
$this->created_at = new DateTime;
if ($offerCart) {
$this->offerte_cart = $offerCart;
}
$this->indication = false;
$this->empty = false;
}
public function src()
{
return rtrim($this->path, '/') . '/' . $this->file;
}
public function srcFinal()
{
return rtrim($this->path, '/') . '/' . $this->fileFinal;
}
public function setFile()
{
$this->file = 'Offerte-' . $this->offerte_id . '.pdf';
}
public function setFileFinal()
{
$this->fileFinal = 'Offerte-' . $this->offerte_id . '-final.pdf';
}
public function getConsultant(): Consultant
{
return $this->consultant;
}
public function getUser()
{
$user = new User();
$user->setAnvaKey($this->anva_key);
$user->setAssu(is_numeric($this->anva_key));
return $user;
}
public function isValid()
{
if (!$this->active) {
return false;
}
$valid = $this->confirmed_at === null;
if ($valid) {
foreach ($this->offerte_cart->processes as $row) {
$process = $row->getProcess();
if ($process->hasExpired()) {
$valid = false;
break;
}
}
}
return $valid;
}
public function expireDate()
{
if ($this->empty || !isset($this->offerte_cart->getCart()->firstEntryDate)) {
return $this->offerte_cart->expireDate();
}
return $this->acceptExpireDate();
}
public function downloadPath()
{
return "/offerte/download/$this->token";
}
public function downloadPathFinal()
{
return "/offerte/download/$this->token/final";
}
public function slotvragenPath()
{
return "/offerte/$this->token";
}
public function toArray()
{
$res = [
'offerte_id' => $this->offerte_id,
'created_at' => $this->created_at->format('Y-m-d H:i:s'),
'download' => $this->downloadPath(),
'downloadFinal' => $this->downloadPathFinal(),
'slotvragen' => $this->slotvragenPath(),
];
if (isset($this->confirmed_at)) {
$res['confirmed_at'] = $this->confirmed_at->format('Y-m-d H:i:s');
}
$res['expire_date'] = $this->expireDate()->format('Y-m-d H:i:s');
return $res;
}
/**
* @param string $userInfo
*/
public function setUserInfo($userInfo): void
{
$this->userInfo = serialize($userInfo);
}
public function isActive() {
if (isset($this->confirmed_at)) {
return false;
}
if ($this->active == false) {
return false;
}
if ($this->isExpired()) {
return false;
}
if (!$this->isValid()) {
return false;
}
return true;
}
public function isExpired() {
try {
if ($this->empty || !isset($this->offerte_cart->getCart()->firstEntryDate)) {
return ($this->expireDate() < new DateTime()) || isset($this->confirmed_at);
}
return ($this->acceptExpireDate() < new DateTime()) || isset($this->confirmed_at);
} catch (\Throwable $e) {
return true;
}
}
public function acceptExpireDate() {
if ($this->empty || !isset($this->offerte_cart->getCart()->firstEntryDate)) {
return null;
}
$date = clone $this->created_at;
if ($this->offerte_cart->getCart()->firstEntryDate > $date) {
return $this->offerte_cart->getCart()->firstEntryDate;
}
return $date->modify('+2 days');
}
public function mailError() {
$config = Configuration::getDefaultConfiguration()
->setApiKey('api-key', $_ENV['SENDINBLUE_APIKEY']);
$smtpApiService = new SMTPApi(new Client(), $config);
$smtpEmail = new SendSmtpEmail([$this->anva_key . $this->id]);
$smtpEmail->setTo([['email' => $this->determineRecipient()]]);
$smtpEmail->setTags([$this->anva_key . '-' . $this->offerte_id . '-admin']);
$smtpEmail->setTemplateId(13);
$smtpEmail->setParams(Mail::toArray([
'name' => $this->name,
'consultant' => $this->consultant->getUsername(),
'key' => $this->anva_key . '-' . $this->offerte_id,
'link' => ''
]));
$smtpApiService->sendTransacEmail($smtpEmail);
}
public function determineRecipient()
{
try {
if (isset($this->consultant) && $this->consultant->hasEmail()) {
return $this->consultant->email;
}
switch ($this->agent) {
case 25:
return 'verzekeringen@amstelgeld.nl';
case 26:
return 'verzekeringen@internetverzekeren.nl';
case 32:
return 'info@kraemer.nl';
default:
return 'advies@optima.nl';
}
} catch (\Exception $e) {
return 'info@hollandiaassuradeuren.nl';
}
}
}