Skip to content

Commit

Permalink
Solucionado bug cuando desde un listado se mandan por email varias fa…
Browse files Browse the repository at this point in the history
…cturas, albaranes, etc... a la vez: no se estaban marcando todos como enviados.
  • Loading branch information
NeoRazorX committed Jul 31, 2023
1 parent 749b336 commit 3b16e08
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 57 deletions.
44 changes: 28 additions & 16 deletions Core/Controller/SendMail.php
@@ -1,7 +1,7 @@
<?php
/**
* This file is part of FacturaScripts
* Copyright (C) 2018-2022 Carlos Garcia Gomez <carlos@facturascripts.com>
* Copyright (C) 2018-2023 Carlos Garcia Gomez <carlos@facturascripts.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
Expand All @@ -22,7 +22,7 @@
use FacturaScripts\Core\Base\Controller;
use FacturaScripts\Core\Base\ControllerPermissions;
use FacturaScripts\Core\Base\DataBase\DataBaseWhere;
use FacturaScripts\Core\Model\Base\ModelCore;
use FacturaScripts\Core\Tools;
use FacturaScripts\Dinamic\Lib\Email\NewMail;
use FacturaScripts\Dinamic\Model\Cliente;
use FacturaScripts\Dinamic\Model\CodeModel;
Expand Down Expand Up @@ -101,6 +101,9 @@ public function url(): string
if ($this->request->get('modelClassName') && $this->request->get('modelCode')) {
$sendParams['modelClassName'] = $this->request->get('modelClassName');
$sendParams['modelCode'] = $this->request->get('modelCode');
if ($this->request->get('modelCodes')) {
$sendParams['modelCodes'] = urldecode($this->request->get('modelCodes'));
}
}

return parent::url() . '?' . http_build_query($sendParams);
Expand Down Expand Up @@ -296,16 +299,16 @@ protected function send(): bool
$this->newMail->addAttachment($file->getPathname(), $file->getClientOriginalName());
}

if ($this->newMail->send()) {
$fileName = $this->request->get('fileName', '');
if (file_exists(FS_FOLDER . '/MyFiles/' . $fileName)) {
unlink(FS_FOLDER . '/MyFiles/' . $fileName);
}
if (false === $this->newMail->send()) {
return false;
}

return true;
$fileName = $this->request->get('fileName', '');
if (file_exists(FS_FOLDER . '/MyFiles/' . $fileName)) {
unlink(FS_FOLDER . '/MyFiles/' . $fileName);
}

return false;
return true;
}

/**
Expand Down Expand Up @@ -364,24 +367,33 @@ protected function updateFemail()
return;
}

// marcamos la fecha del envío del email
$model = new $className();
$modelCode = $this->request->get('modelCode');
if ($model->loadFromCode($modelCode) && property_exists($className, 'femail')) {
$model->femail = date(ModelCore::DATE_STYLE);
$model->femail = Tools::date();
if (false === $model->save()) {
$this->toolBox()->i18nLog()->error('record-save-error');
return;
}

// si el sujeto no tiene email, le asignamos el del destinatario
$subject = $model->getSubject();
if (!empty($subject->email)) {
return;
if (empty($subject->email)) {
foreach ($this->newMail->getToAddresses() as $email) {
$subject->email = $email;
$subject->save();
break;
}
}
}

foreach ($this->newMail->getToAddresses() as $email) {
$subject->email = $email;
$subject->save();
return;
// si hay más documentos, marcamos también la fecha de envío
$modelCodes = $this->request->get('modelCodes', '');
foreach (explode(',', $modelCodes) as $modelCode) {
if ($model->loadFromCode($modelCode) && property_exists($className, 'femail')) {
$model->femail = Tools::date();
$model->save();
}
}
}
Expand Down
42 changes: 26 additions & 16 deletions Core/Lib/Export/MAILExport.php
@@ -1,7 +1,7 @@
<?php
/**
* This file is part of FacturaScripts
* Copyright (C) 2017-2020 Carlos Garcia Gomez <carlos@facturascripts.com>
* Copyright (C) 2017-2023 Carlos Garcia Gomez <carlos@facturascripts.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
Expand All @@ -16,6 +16,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace FacturaScripts\Core\Lib\Export;

use FacturaScripts\Core\Model\Base\BusinessDocument;
Expand All @@ -30,55 +31,64 @@
*/
class MAILExport extends ParentClass
{

/**
*
* @var array
*/
/** @var array */
protected $sendParams = [];

/**
*
* @param BusinessDocument $model
*
* @return bool
*/
public function addBusinessDocPage($model): bool
{
$this->sendParams['modelClassName'] = $model->modelClassName();
$this->sendParams['modelCode'] = $model->primaryColumnValue();

if (false === array_key_exists('modelCode', $this->sendParams)) {
$this->sendParams['modelCode'] = $model->primaryColumnValue();
} elseif (false === array_key_exists('modelCodes', $this->sendParams)) {
$this->sendParams['modelCodes'] = $model->primaryColumnValue();
} else {
$this->sendParams['modelCodes'] .= ',' . $model->primaryColumnValue();
}

return parent::addBusinessDocPage($model);
}

/**
*
* @param ModelClass $model
* @param array $columns
* @param string $title
* @param array $columns
* @param string $title
*
* @return bool
*/
public function addModelPage($model, $columns, $title = ''): bool
{
$this->sendParams['modelClassName'] = $model->modelClassName();
$this->sendParams['modelCode'] = $model->primaryColumnValue();

if (false === array_key_exists('modelCode', $this->sendParams)) {
$this->sendParams['modelCode'] = $model->primaryColumnValue();
} elseif (false === array_key_exists('modelCodes', $this->sendParams)) {
$this->sendParams['modelCodes'] = $model->primaryColumnValue();
} else {
$this->sendParams['modelCodes'] .= ',' . $model->primaryColumnValue();
}

return parent::addModelPage($model, $columns, $title);
}

/**
*
* @param Response $response
*/
public function show(Response &$response)
{
$fileName = $this->getFileName() . '_mail_' . time() . '.pdf';
$filePath = \FS_FOLDER . '/MyFiles/' . $fileName;
if (false === \file_put_contents($filePath, $this->getDoc())) {
$filePath = FS_FOLDER . '/MyFiles/' . $fileName;
if (false === file_put_contents($filePath, $this->getDoc())) {
$this->toolBox()->i18nLog()->error('folder-not-writable');
return;
}

$this->sendParams['fileName'] = $fileName;
$response->headers->set('Refresh', '0; SendMail?' . \http_build_query($this->sendParams));
$response->headers->set('Refresh', '0; SendMail?' . http_build_query($this->sendParams));
}
}
46 changes: 21 additions & 25 deletions Core/View/SendMail.html.twig
@@ -1,7 +1,7 @@
{#
/**
* This file is part of FacturaScripts
* Copyright (C) 2017-2022 Carlos Garcia Gomez <carlos@facturascripts.com>
* Copyright (C) 2017-2023 Carlos Garcia Gomez <carlos@facturascripts.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
Expand Down Expand Up @@ -30,17 +30,17 @@
<div class="row">
<div class="col-12">
<form action="{{ fsc.url() }}" method="post" enctype="multipart/form-data">
{{ formToken() }}
<input type="hidden" name="action" value="send"/>
<input type="hidden" name="multireqtoken" value="{{ fsc.multiRequestProtection.newToken() }}"/>
<div class="card shadow">
<div class="card-body">
<h1 class="h3 mb-3">
<i class="fas fa-envelope fa-fw" aria-hidden="true"></i> {{ i18n.trans('send-mail') }}
<i class="fas fa-envelope fa-fw" aria-hidden="true"></i> {{ trans('send-mail') }}
</h1>
<div class="form-group">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text">{{ i18n.trans('from') }}</span>
<span class="input-group-text">{{ trans('from') }}</span>
</div>
<select class="custom-select" name="email-from">
{% for emailFrom in fsc.newMail.getAvailableMailboxes() %}
Expand All @@ -52,60 +52,57 @@
<div class="form-group">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text">{{ i18n.trans('to') }}</span>
<span class="input-group-text">{{ trans('to') }}</span>
</div>
{% set emails = fsc.newMail.getToAddresses() is empty ? '' : fsc.newMail.getToAddresses() | join(',') ~ ', ' %}
<input type="text" id="email" name="email" value="{{ emails }}" class="form-control"
required="" placeholder="{{ i18n.trans('email-to') }}"/>
placeholder="{{ trans('email-to') }}" required/>
<div class="input-group-append">
<button type="button" class="btn btn-outline-secondary"
title="{{ i18n.trans('email-cc') }}" onclick="$('#fgCC').show();
$(this).hide();">
{{ i18n.trans('cc') }}
title="{{ trans('email-cc') }}" onclick="$('#fgCC').show();
$(this).hide();">{{ trans('cc') }}
</button>
<button type="button" class="btn btn-outline-secondary"
title="{{ i18n.trans('email-bcc') }}" onclick="$('#fgBCC').show();
$(this).hide();">
{{ i18n.trans('bcc') }}
title="{{ trans('email-bcc') }}" onclick="$('#fgBCC').show();
$(this).hide();">{{ trans('bcc') }}
</button>
</div>
</div>
</div>
<div id="fgCC" class="form-group" style="display: none;">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text">{{ i18n.trans('cc') }}</span>
<span class="input-group-text">{{ trans('cc') }}</span>
</div>
<input type="text" id="email-cc" name="email-cc" class="form-control"
placeholder="{{ i18n.trans('email-cc') }}"/>
placeholder="{{ trans('email-cc') }}"/>
</div>
</div>
<div id="fgBCC" class="form-group" style="display: none;">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text">{{ i18n.trans('bcc') }}</span>
<span class="input-group-text">{{ trans('bcc') }}</span>
</div>
<input type="text" id="email-bcc" name="email-bcc" class="form-control"
placeholder="{{ i18n.trans('email-bcc') }}"/>
placeholder="{{ trans('email-bcc') }}"/>
</div>
</div>
<div class="form-group">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text">{{ i18n.trans('subject') }}</span>
<span class="input-group-text">{{ trans('subject') }}</span>
</div>
<input type="text" name="subject" value="{{ fsc.newMail.title }}"
class="form-control" required="" placeholder="{{ i18n.trans('subject') }}"/>
class="form-control" placeholder="{{ trans('subject') }}" required/>
</div>
</div>
<div class="form-group">
<textarea name="body" rows="5" class="form-control">{{ fsc.newMail.text }}</textarea>
</div>
{% if fsc.newMail.signature %}
<div class="form-group">
{{ i18n.trans('email-signature') }}
<textarea rows="3" class="form-control"
readonly="true">{{ fsc.newMail.signature }}</textarea>
{{ trans('email-signature') }}
<textarea rows="3" class="form-control" readonly>{{ fsc.newMail.signature }}</textarea>
</div>
{% endif %}
<div class="form-group">
Expand All @@ -124,15 +121,14 @@
<input type="checkbox" name="replyto" value="1" {{ check }}
class="form-check-input" id="replytoCheck"/>
<label class="form-check-label" for="replytoCheck">
{{ i18n.trans('email-replies-to', {'%email%': fsc.user.email}) }}
{{ trans('email-replies-to', {'%email%': fsc.user.email}) }}
</label>
</div>
{% endif %}
</div>
<div class="col-sm-6 text-right">
<button type="submit" class="btn btn-primary">
<i class="fas fa-envelope fa-fw"
aria-hidden="true"></i> {{ i18n.trans('send') }}
<i class="fas fa-envelope fa-fw" aria-hidden="true"></i> {{ trans('send') }}
</button>
</div>
</div>
Expand Down Expand Up @@ -206,4 +202,4 @@
});
});
</script>
{% endblock %}
{% endblock %}

0 comments on commit 3b16e08

Please sign in to comment.