2019-08-27 17:25:52 +02:00
|
|
|
<?php
|
2020-09-07 15:04:06 +02:00
|
|
|
/*
|
|
|
|
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
|
2021-01-20 15:08:51 +01:00
|
|
|
* Copyright (C) DevCode s.r.l.
|
2020-09-07 15:04:06 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2019-08-27 17:25:52 +02:00
|
|
|
|
|
|
|
namespace Modules\Newsletter;
|
|
|
|
|
2020-09-22 20:28:37 +02:00
|
|
|
use Common\SimpleModelTrait;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2019-08-27 17:25:52 +02:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
use Models\User;
|
|
|
|
use Modules\Anagrafiche\Anagrafica;
|
2021-08-02 18:27:28 +02:00
|
|
|
use Modules\Anagrafiche\Referente;
|
|
|
|
use Modules\Anagrafiche\Sede;
|
2019-08-29 10:25:14 +02:00
|
|
|
use Modules\Emails\Account;
|
|
|
|
use Modules\Emails\Mail;
|
|
|
|
use Modules\Emails\Template;
|
2021-09-23 10:04:44 +02:00
|
|
|
use Respect\Validation\Validator as v;
|
2019-08-28 16:58:47 +02:00
|
|
|
use Traits\RecordTrait;
|
2019-08-27 17:25:52 +02:00
|
|
|
|
|
|
|
class Newsletter extends Model
|
|
|
|
{
|
2020-09-22 20:28:37 +02:00
|
|
|
use SimpleModelTrait;
|
2019-08-27 17:25:52 +02:00
|
|
|
use SoftDeletes;
|
2019-08-28 16:58:47 +02:00
|
|
|
use RecordTrait;
|
2019-08-27 17:25:52 +02:00
|
|
|
|
2019-08-29 10:25:14 +02:00
|
|
|
protected $table = 'em_newsletters';
|
2019-08-27 17:25:52 +02:00
|
|
|
|
2019-08-29 10:25:14 +02:00
|
|
|
public static function build(User $user, Template $template, $name)
|
2019-08-27 17:25:52 +02:00
|
|
|
{
|
2020-09-22 20:28:37 +02:00
|
|
|
$model = new static();
|
2019-08-27 17:25:52 +02:00
|
|
|
|
|
|
|
$model->user()->associate($user);
|
|
|
|
$model->template()->associate($template);
|
|
|
|
$model->name = $name;
|
|
|
|
|
|
|
|
$model->subject = $template->subject;
|
|
|
|
$model->content = $template->body;
|
|
|
|
|
|
|
|
$model->state = 'DEV';
|
|
|
|
|
|
|
|
$model->save();
|
|
|
|
|
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
|
2019-08-28 16:58:47 +02:00
|
|
|
/**
|
|
|
|
* Restituisce il nome del modulo a cui l'oggetto è collegato.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getModuleAttribute()
|
|
|
|
{
|
|
|
|
return 'Newsletter';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function fixStato()
|
|
|
|
{
|
|
|
|
$mails = $this->emails;
|
|
|
|
|
|
|
|
$completed = true;
|
|
|
|
foreach ($mails as $mail) {
|
|
|
|
if (empty($mail->sent_at)) {
|
|
|
|
$completed = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->state = $completed ? 'OK' : $this->state;
|
|
|
|
$this->completed_at = $completed ? date('Y-m-d H:i:s') : $this->completed_at;
|
|
|
|
$this->save();
|
|
|
|
}
|
|
|
|
|
2021-08-03 14:59:42 +02:00
|
|
|
public function getNumeroDestinatariSenzaEmail()
|
2021-08-02 18:27:28 +02:00
|
|
|
{
|
2021-08-03 14:59:42 +02:00
|
|
|
$anagrafiche = $this->getDestinatari(Anagrafica::class)
|
|
|
|
->join('an_anagrafiche', 'idanagrafica', '=', 'record_id')
|
|
|
|
->where('email', '=', '')
|
|
|
|
->count();
|
|
|
|
|
|
|
|
$sedi = $this->getDestinatari(Sede::class)
|
|
|
|
->join('an_sedi', 'an_sedi.id', '=', 'record_id')
|
|
|
|
->where('email', '=', '')
|
|
|
|
->count();
|
|
|
|
|
|
|
|
$referenti = $this->getDestinatari(Referente::class)
|
|
|
|
->join('an_referenti', 'an_referenti.id', '=', 'record_id')
|
|
|
|
->where('email', '=', '')
|
|
|
|
->count();
|
|
|
|
|
|
|
|
return $anagrafiche + $sedi + $referenti;
|
2021-08-02 18:27:28 +02:00
|
|
|
}
|
|
|
|
|
2021-08-03 14:59:42 +02:00
|
|
|
public function getNumeroDestinatariSenzaConsenso()
|
2019-08-27 17:25:52 +02:00
|
|
|
{
|
2021-08-03 14:59:42 +02:00
|
|
|
$anagrafiche = $this->getDestinatari(Anagrafica::class)
|
|
|
|
->join('an_anagrafiche', 'idanagrafica', '=', 'record_id')
|
|
|
|
->where('an_anagrafiche.enable_newsletter', '=', false)
|
|
|
|
->count();
|
|
|
|
|
|
|
|
$sedi = $this->getDestinatari(Sede::class)
|
|
|
|
->join('an_sedi', 'an_sedi.id', '=', 'record_id')
|
|
|
|
->join('an_anagrafiche', 'an_anagrafiche.idanagrafica', '=', 'an_sedi.idanagrafica')
|
|
|
|
->where('an_anagrafiche.enable_newsletter', '=', false)
|
|
|
|
->count();
|
|
|
|
|
|
|
|
$referenti = $this->getDestinatari(Referente::class)
|
|
|
|
->join('an_referenti', 'an_referenti.id', '=', 'record_id')
|
|
|
|
->join('an_anagrafiche', 'an_anagrafiche.idanagrafica', '=', 'an_referenti.idanagrafica')
|
|
|
|
->where('an_anagrafiche.enable_newsletter', '=', false)
|
|
|
|
->count();
|
|
|
|
|
|
|
|
return $anagrafiche + $sedi + $referenti;
|
2021-08-02 18:27:28 +02:00
|
|
|
}
|
|
|
|
|
2021-08-03 14:59:42 +02:00
|
|
|
public function getDestinatari($tipo)
|
2021-08-02 18:27:28 +02:00
|
|
|
{
|
2021-08-03 14:59:42 +02:00
|
|
|
return $this->destinatari()
|
|
|
|
->where('record_type', '=', $tipo);
|
2021-08-02 18:27:28 +02:00
|
|
|
}
|
|
|
|
|
2021-09-23 10:04:44 +02:00
|
|
|
/**
|
|
|
|
* Metodo per inviare l'email della newsletter a uno specifico destinatario.
|
|
|
|
*
|
|
|
|
* @return Mail|null
|
|
|
|
*/
|
|
|
|
public function inviaDestinatario(Destinatario $destinatario, $test = false)
|
|
|
|
{
|
|
|
|
$template = $this->template;
|
|
|
|
$uploads = $this->uploads()->pluck('id');
|
|
|
|
|
|
|
|
$origine = $destinatario->getOrigine();
|
|
|
|
|
|
|
|
$anagrafica = $origine instanceof Anagrafica ? $origine : $origine->anagrafica;
|
|
|
|
|
2021-09-23 17:55:58 +02:00
|
|
|
$abilita_newsletter = $origine->enable_newsletter;
|
2021-09-23 10:04:44 +02:00
|
|
|
$email = $destinatario->email;
|
|
|
|
if (empty($email) || empty($abilita_newsletter) || !v::email()->validate($email)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inizializzazione email
|
|
|
|
$mail = Mail::build(auth()->getUser(), $template, $anagrafica->id);
|
|
|
|
|
|
|
|
// Completamento informazioni
|
|
|
|
$mail->addReceiver($email);
|
|
|
|
$mail->subject = ($test ? '[Test] ' : '').$this->subject;
|
|
|
|
$mail->content = $this->content;
|
|
|
|
$mail->id_newsletter = $this->id;
|
|
|
|
|
|
|
|
// Registrazione allegati
|
|
|
|
foreach ($uploads as $upload) {
|
|
|
|
$mail->addUpload($upload);
|
|
|
|
}
|
|
|
|
|
|
|
|
$mail->save();
|
|
|
|
|
|
|
|
return $mail;
|
|
|
|
}
|
|
|
|
|
2021-08-03 14:59:42 +02:00
|
|
|
// Relazione Eloquent
|
|
|
|
|
|
|
|
public function destinatari()
|
2021-08-02 18:27:28 +02:00
|
|
|
{
|
2021-08-03 14:59:42 +02:00
|
|
|
return $this->hasMany(Destinatario::class, 'id_newsletter');
|
2019-08-27 17:25:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function emails()
|
|
|
|
{
|
2021-08-03 14:59:42 +02:00
|
|
|
return $this->belongsToMany(Mail::class, 'em_newsletter_receiver', 'id_newsletter', 'id_email')->withPivot(['record_id', 'record_type']);
|
2019-08-27 17:25:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function account()
|
|
|
|
{
|
2019-08-29 10:25:14 +02:00
|
|
|
return $this->belongsTo(Account::class, 'id_account');
|
2019-08-27 17:25:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function template()
|
|
|
|
{
|
2019-08-29 10:25:14 +02:00
|
|
|
return $this->belongsTo(Template::class, 'id_template');
|
2019-08-27 17:25:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function user()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(User::class, 'created_by');
|
|
|
|
}
|
|
|
|
}
|