2020-11-09 03:39:42 +01:00
< ? php
/**
* Invoice Ninja ( https :// invoiceninja . com ) .
*
* @ link https :// github . com / invoiceninja / invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @ copyright Copyright ( c ) 2023. Invoice Ninja LLC ( https :// invoiceninja . com )
2020-11-09 03:39:42 +01:00
*
2021-06-16 08:58:16 +02:00
* @ license https :// www . elastic . co / licensing / elastic - license
2020-11-09 03:39:42 +01:00
*/
namespace App\Mail\Engine ;
2023-10-26 04:57:44 +02:00
use App\DataMapper\EmailTemplateDefaults ;
use App\Jobs\Entity\CreateRawPdf ;
2023-05-28 10:03:18 +02:00
use App\Models\Account ;
2023-10-08 06:06:34 +02:00
use App\Models\Payment ;
2023-10-26 04:57:44 +02:00
use App\Services\Template\TemplateAction ;
use App\Utils\Helpers ;
use App\Utils\Ninja ;
use App\Utils\Number ;
2020-11-09 03:39:42 +01:00
use App\Utils\Traits\MakesDates ;
2021-06-29 11:46:40 +02:00
use Illuminate\Support\Facades\App ;
2023-02-02 23:56:58 +01:00
use Illuminate\Support\Facades\URL ;
2020-11-09 03:39:42 +01:00
2020-11-25 15:19:52 +01:00
class PaymentEmailEngine extends BaseEmailEngine
2020-11-09 03:39:42 +01:00
{
use MakesDates ;
2020-11-25 15:19:52 +01:00
public $client ;
2020-11-09 03:39:42 +01:00
2020-11-25 15:19:52 +01:00
public $payment ;
2020-11-09 03:39:42 +01:00
public $template_data ;
public $settings ;
public $company ;
public $contact ;
2020-12-25 12:13:26 +01:00
private $helpers ;
2021-04-20 13:32:25 +02:00
2021-08-10 00:04:44 +02:00
private $payment_template_body ;
private $payment_template_subject ;
2023-10-08 06:06:34 +02:00
public bool $is_refund = false ;
2020-11-09 03:39:42 +01:00
public function __construct ( $payment , $contact , $template_data = null )
{
$this -> payment = $payment ;
$this -> company = $payment -> company ;
$this -> client = $payment -> client ;
2021-10-12 09:41:36 +02:00
$this -> contact = $contact ? : $this -> client -> contacts () -> first ();
2021-10-08 07:03:26 +02:00
$this -> contact -> load ( 'client.company' );
2020-11-09 03:39:42 +01:00
$this -> settings = $this -> client -> getMergedSettings ();
$this -> template_data = $template_data ;
2020-12-25 12:13:26 +01:00
$this -> helpers = new Helpers ();
2020-11-09 03:39:42 +01:00
}
public function build ()
{
2021-06-29 11:46:40 +02:00
App :: forgetInstance ( 'translator' );
$t = app ( 'translator' );
App :: setLocale ( $this -> contact -> preferredLocale ());
$t -> replace ( Ninja :: transformTranslations ( $this -> client -> getMergedSettings ()));
2021-08-10 00:04:44 +02:00
$this -> resolvePaymentTemplate ();
2022-06-21 11:57:17 +02:00
if ( is_array ( $this -> template_data ) && array_key_exists ( 'body' , $this -> template_data ) && strlen ( $this -> template_data [ 'body' ]) > 0 ) {
2020-11-09 03:39:42 +01:00
$body_template = $this -> template_data [ 'body' ];
2021-08-10 00:04:44 +02:00
} elseif ( strlen ( $this -> client -> getSetting ( $this -> payment_template_body )) > 0 ) {
$body_template = $this -> client -> getSetting ( $this -> payment_template_body );
2020-11-25 15:19:52 +01:00
} else {
2021-08-10 00:04:44 +02:00
$body_template = EmailTemplateDefaults :: getDefaultTemplate ( $this -> payment_template_body , $this -> client -> locale ());
2020-11-09 03:39:42 +01:00
}
2022-06-21 11:57:17 +02:00
if ( is_array ( $this -> template_data ) && array_key_exists ( 'subject' , $this -> template_data ) && strlen ( $this -> template_data [ 'subject' ]) > 0 ) {
2020-11-09 03:39:42 +01:00
$subject_template = $this -> template_data [ 'subject' ];
2021-08-10 00:04:44 +02:00
} elseif ( strlen ( $this -> client -> getSetting ( $this -> payment_template_subject )) > 0 ) {
$subject_template = $this -> client -> getSetting ( $this -> payment_template_subject );
2020-11-25 15:19:52 +01:00
} else {
2021-08-10 00:04:44 +02:00
$subject_template = EmailTemplateDefaults :: getDefaultTemplate ( $this -> payment_template_subject , $this -> client -> locale ());
2020-11-09 03:39:42 +01:00
}
$this -> setTemplate ( $this -> client -> getSetting ( 'email_style' ))
-> setContact ( $this -> contact )
-> setVariables ( $this -> makeValues ())
-> setSubject ( $subject_template )
-> setBody ( $body_template )
-> setFooter ( '' )
-> setViewLink ( '' )
-> setViewText ( '' );
2021-05-17 00:09:20 +02:00
if ( $this -> client -> getSetting ( 'pdf_email_attachment' ) !== false && $this -> company -> account -> hasFeature ( Account :: FEATURE_PDF_ATTACHMENT )) {
2022-12-14 08:12:37 +01:00
2023-10-08 06:20:43 +02:00
$template_in_use = false ;
if ( $this -> is_refund && strlen ( $this -> payment -> client -> getSetting ( 'payment_refund_design_id' )) > 2 ) {
$pdf = ( new TemplateAction (
[ $this -> payment -> hashed_id ],
$this -> payment -> client -> getSetting ( 'payment_refund_design_id' ),
Payment :: class ,
$this -> payment -> user_id ,
$this -> payment -> company ,
$this -> payment -> company -> db ,
'nohash' ,
false
)) -> handle ();
$file_name = ctrans ( 'texts.payment_refund_receipt' , [ 'number' => $this -> payment -> number ]) . '.pdf' ;
$file_name = str_replace ( ' ' , '_' , $file_name );
$this -> setAttachments ([[ 'file' => base64_encode ( $pdf ), 'name' => $file_name ]]);
$template_in_use = true ;
} elseif ( ! $this -> is_refund && strlen ( $this -> payment -> client -> getSetting ( 'payment_receipt_design_id' )) > 2 ) {
$pdf = ( new TemplateAction (
[ $this -> payment -> hashed_id ],
$this -> payment -> client -> getSetting ( 'payment_receipt_design_id' ),
Payment :: class ,
$this -> payment -> user_id ,
$this -> payment -> company ,
$this -> payment -> company -> db ,
'nohash' ,
false
)) -> handle ();
$file_name = ctrans ( 'texts.payment_receipt' , [ 'number' => $this -> payment -> number ]) . '.pdf' ;
$file_name = str_replace ( ' ' , '_' , $file_name );
$this -> setAttachments ([[ 'file' => base64_encode ( $pdf ), 'name' => $file_name ]]);
$template_in_use = true ;
2023-10-08 06:06:34 +02:00
2023-10-08 06:20:43 +02:00
}
2023-10-26 04:57:44 +02:00
$this -> payment -> invoices -> each ( function ( $invoice ) use ( $template_in_use ) {
2023-10-08 06:20:43 +02:00
2023-10-26 04:57:44 +02:00
if ( ! $template_in_use ) {
2023-10-26 03:25:56 +02:00
$pdf = (( new CreateRawPdf ( $invoice -> invitations -> first ())) -> handle ());
2023-10-08 06:06:34 +02:00
$file_name = $invoice -> numberFormatter () . '.pdf' ;
2023-10-08 06:20:43 +02:00
$this -> setAttachments ([[ 'file' => base64_encode ( $pdf ), 'name' => $file_name ]]);
2023-10-08 06:06:34 +02:00
}
2022-11-28 04:16:46 +01:00
2022-12-15 06:56:53 +01:00
//attach invoice documents also to payments
2023-02-16 02:36:09 +01:00
if ( $this -> client -> getSetting ( 'document_email_attachment' ) !== false ) {
2023-10-26 04:57:44 +02:00
$invoice -> documents () -> where ( 'is_public' , true ) -> cursor () -> each ( function ( $document ) {
2023-02-16 02:36:09 +01:00
if ( $document -> size > $this -> max_attachment_size ) {
2023-02-02 23:56:58 +01:00
$this -> setAttachmentLinks ([ " <a class='doc_links' href=' " . URL :: signedRoute ( 'documents.public_download' , [ 'document_hash' => $document -> hash ]) . " '> " . $document -> name . " </a> " ]);
2023-02-16 02:36:09 +01:00
} else {
$this -> setAttachments ([[ 'path' => $document -> filePath (), 'name' => $document -> name , 'mime' => null , ]]);
}
2023-09-11 02:13:13 +02:00
});
2022-12-15 06:55:47 +01:00
}
2023-05-28 10:03:18 +02:00
2023-09-26 00:39:29 +02:00
// if($this->client->getSetting('enable_e_invoice'))
// {
2023-05-28 10:03:18 +02:00
2023-09-26 00:39:29 +02:00
// $e_invoice_filepath = $invoice->service()->getEInvoice($this->contact);
2023-05-28 10:03:18 +02:00
2023-09-26 00:39:29 +02:00
// if($e_invoice_filepath && strlen($e_invoice_filepath) > 1)
// $this->setAttachments([['file' => base64_encode($e_invoice_filepath), 'name' => $invoice->getFileName("xml")]]);
2023-05-28 10:03:18 +02:00
2023-09-26 00:39:29 +02:00
// }
2023-05-28 10:03:18 +02:00
2021-05-17 00:09:20 +02:00
});
}
2020-11-09 03:39:42 +01:00
return $this ;
}
2021-08-10 00:04:44 +02:00
/**
* Helper method to resolve which payment template
* to use . We need to check the invoice balances to
* determine if this is a partial payment , or full payment .
2022-06-21 11:57:17 +02:00
*
2021-08-10 00:04:44 +02:00
* @ return $this
*/
2022-06-21 11:57:17 +02:00
private function resolvePaymentTemplate ()
2021-08-10 00:04:44 +02:00
{
2022-06-21 11:57:17 +02:00
$partial = $this -> payment -> invoices -> contains ( function ( $invoice ) {
2021-08-10 00:04:44 +02:00
return $invoice -> balance > 0 ;
});
2022-06-21 11:57:17 +02:00
if ( $partial ) {
$this -> payment_template_body = 'email_template_payment_partial' ;
$this -> payment_template_subject = 'email_subject_payment_partial' ;
} else {
$this -> payment_template_body = 'email_template_payment' ;
$this -> payment_template_subject = 'email_subject_payment' ;
2021-08-10 00:04:44 +02:00
}
return $this ;
}
2021-03-31 03:55:33 +02:00
public function makePaymentVariables ()
2020-11-09 03:39:42 +01:00
{
$data = [];
$data [ '$from' ] = [ 'value' => '' , 'label' => ctrans ( 'texts.from' )];
$data [ '$to' ] = [ 'value' => '' , 'label' => ctrans ( 'texts.to' )];
$data [ '$number' ] = [ 'value' => $this -> payment -> number ? : ' ' , 'label' => ctrans ( 'texts.payment_number' )];
2020-11-09 03:57:34 +01:00
$data [ '$payment.number' ] = & $data [ '$number' ];
2020-11-09 03:39:42 +01:00
$data [ '$entity' ] = [ 'value' => '' , 'label' => ctrans ( 'texts.payment' )];
$data [ '$payment.amount' ] = [ 'value' => Number :: formatMoney ( $this -> payment -> amount , $this -> client ) ? : ' ' , 'label' => ctrans ( 'texts.amount' )];
2022-03-14 05:22:19 +01:00
$data [ '$payment.refunded' ] = [ 'value' => Number :: formatMoney ( $this -> payment -> refunded , $this -> client ) ? : ' ' , 'label' => ctrans ( 'texts.refund' )];
2023-08-28 12:24:48 +02:00
$data [ '$payment.unapplied' ] = [ 'value' => Number :: formatMoney (( $this -> payment -> amount - $this -> payment -> refunded - $this -> payment -> applied ), $this -> client ) ? : ' ' , 'label' => ctrans ( 'texts.refund' )];
2020-11-09 03:39:42 +01:00
$data [ '$amount' ] = & $data [ '$payment.amount' ];
2021-02-23 11:04:39 +01:00
$data [ '$payment.date' ] = [ 'value' => $this -> translateDate ( $this -> payment -> date , $this -> client -> date_format (), $this -> client -> locale ()), 'label' => ctrans ( 'texts.payment_date' )];
2020-11-09 03:39:42 +01:00
$data [ '$transaction_reference' ] = [ 'value' => $this -> payment -> transaction_reference , 'label' => ctrans ( 'texts.transaction_reference' )];
$data [ '$public_notes' ] = [ 'value' => $this -> payment -> public_notes , 'label' => ctrans ( 'texts.notes' )];
2020-12-25 12:29:13 +01:00
$data [ '$payment1' ] = [ 'value' => $this -> helpers -> formatCustomFieldValue ( $this -> company -> custom_fields , 'payment1' , $this -> payment -> custom_value1 , $this -> client ) ? : ' ' , 'label' => $this -> helpers -> makeCustomField ( $this -> company -> custom_fields , 'payment1' )];
$data [ '$payment2' ] = [ 'value' => $this -> helpers -> formatCustomFieldValue ( $this -> company -> custom_fields , 'payment2' , $this -> payment -> custom_value2 , $this -> client ) ? : ' ' , 'label' => $this -> helpers -> makeCustomField ( $this -> company -> custom_fields , 'payment2' )];
$data [ '$payment3' ] = [ 'value' => $this -> helpers -> formatCustomFieldValue ( $this -> company -> custom_fields , 'payment3' , $this -> payment -> custom_value3 , $this -> client ) ? : ' ' , 'label' => $this -> helpers -> makeCustomField ( $this -> company -> custom_fields , 'payment3' )];
$data [ '$payment4' ] = [ 'value' => $this -> helpers -> formatCustomFieldValue ( $this -> company -> custom_fields , 'payment4' , $this -> payment -> custom_value4 , $this -> client ) ? : ' ' , 'label' => $this -> helpers -> makeCustomField ( $this -> company -> custom_fields , 'payment4' )];
2023-01-16 23:43:11 +01:00
$data [ '$custom1' ] = & $data [ '$payment1' ];
$data [ '$custom2' ] = & $data [ '$payment2' ];
$data [ '$custom3' ] = & $data [ '$payment3' ];
$data [ '$custom4' ] = & $data [ '$payment4' ];
2020-11-09 03:39:42 +01:00
2020-12-25 12:29:13 +01:00
$data [ '$client1' ] = [ 'value' => $this -> helpers -> formatCustomFieldValue ( $this -> company -> custom_fields , 'client1' , $this -> client -> custom_value1 , $this -> client ) ? : ' ' , 'label' => $this -> helpers -> makeCustomField ( $this -> company -> custom_fields , 'client1' )];
$data [ '$client2' ] = [ 'value' => $this -> helpers -> formatCustomFieldValue ( $this -> company -> custom_fields , 'client2' , $this -> client -> custom_value2 , $this -> client ) ? : ' ' , 'label' => $this -> helpers -> makeCustomField ( $this -> company -> custom_fields , 'client2' )];
$data [ '$client3' ] = [ 'value' => $this -> helpers -> formatCustomFieldValue ( $this -> company -> custom_fields , 'client3' , $this -> client -> custom_value3 , $this -> client ) ? : ' ' , 'label' => $this -> helpers -> makeCustomField ( $this -> company -> custom_fields , 'client3' )];
$data [ '$client4' ] = [ 'value' => $this -> helpers -> formatCustomFieldValue ( $this -> company -> custom_fields , 'client4' , $this -> client -> custom_value4 , $this -> client ) ? : ' ' , 'label' => $this -> helpers -> makeCustomField ( $this -> company -> custom_fields , 'client4' )];
2020-11-09 03:39:42 +01:00
$data [ '$address1' ] = [ 'value' => $this -> client -> address1 ? : ' ' , 'label' => ctrans ( 'texts.address1' )];
$data [ '$address2' ] = [ 'value' => $this -> client -> address2 ? : ' ' , 'label' => ctrans ( 'texts.address2' )];
$data [ '$id_number' ] = [ 'value' => $this -> client -> id_number ? : ' ' , 'label' => ctrans ( 'texts.id_number' )];
2021-01-25 11:34:12 +01:00
$data [ '$client.number' ] = [ 'value' => $this -> client -> number ? : ' ' , 'label' => ctrans ( 'texts.number' )];
2020-11-09 03:39:42 +01:00
$data [ '$vat_number' ] = [ 'value' => $this -> client -> vat_number ? : ' ' , 'label' => ctrans ( 'texts.vat_number' )];
$data [ '$website' ] = [ 'value' => $this -> client -> present () -> website () ? : ' ' , 'label' => ctrans ( 'texts.website' )];
$data [ '$phone' ] = [ 'value' => $this -> client -> present () -> phone () ? : ' ' , 'label' => ctrans ( 'texts.phone' )];
$data [ '$country' ] = [ 'value' => isset ( $this -> client -> country -> name ) ? $this -> client -> country -> name : '' , 'label' => ctrans ( 'texts.country' )];
$data [ '$email' ] = [ 'value' => isset ( $this -> contact ) ? $this -> contact -> email : 'no contact email on record' , 'label' => ctrans ( 'texts.email' )];
$data [ '$client_name' ] = [ 'value' => $this -> client -> present () -> name () ? : ' ' , 'label' => ctrans ( 'texts.client_name' )];
$data [ '$client.name' ] = & $data [ '$client_name' ];
2021-08-05 23:21:40 +02:00
$data [ '$client' ] = & $data [ '$client_name' ];
2020-11-09 03:39:42 +01:00
$data [ '$client.address1' ] = & $data [ '$address1' ];
$data [ '$client.address2' ] = & $data [ '$address2' ];
$data [ '$client_address' ] = [ 'value' => $this -> client -> present () -> address () ? : ' ' , 'label' => ctrans ( 'texts.address' )];
$data [ '$client.address' ] = & $data [ '$client_address' ];
$data [ '$client.id_number' ] = & $data [ '$id_number' ];
$data [ '$client.vat_number' ] = & $data [ '$vat_number' ];
$data [ '$client.website' ] = & $data [ '$website' ];
$data [ '$client.phone' ] = & $data [ '$phone' ];
2023-08-31 16:20:38 +02:00
$data [ '$city' ] = [ 'value' => $this -> client -> city ? : ' ' , 'label' => ctrans ( 'texts.city' )];
$data [ '$client.city' ] = & $data [ '$city' ];
$data [ '$state' ] = [ 'value' => $this -> client -> state ? : ' ' , 'label' => ctrans ( 'texts.state' )];
$data [ '$client.state' ] = & $data [ '$state' ];
$data [ '$postal_code' ] = [ 'value' => $this -> client -> postal_code ? : ' ' , 'label' => ctrans ( 'texts.postal_code' )];
$data [ '$client.postal_code' ] = & $data [ '$postal_code' ];
2020-11-09 03:39:42 +01:00
$data [ '$city_state_postal' ] = [ 'value' => $this -> client -> present () -> cityStateZip ( $this -> client -> city , $this -> client -> state , $this -> client -> postal_code , false ) ? : ' ' , 'label' => ctrans ( 'texts.city_state_postal' )];
$data [ '$client.city_state_postal' ] = & $data [ '$city_state_postal' ];
$data [ '$postal_city_state' ] = [ 'value' => $this -> client -> present () -> cityStateZip ( $this -> client -> city , $this -> client -> state , $this -> client -> postal_code , true ) ? : ' ' , 'label' => ctrans ( 'texts.postal_city_state' )];
$data [ '$client.postal_city_state' ] = & $data [ '$postal_city_state' ];
2023-01-21 13:27:02 +01:00
$data [ '$postal_city' ] = [ 'value' => $this -> client -> present () -> cityStateZip ( $this -> client -> city , null , $this -> client -> postal_code , true ) ? : ' ' , 'label' => ctrans ( 'texts.postal_city' )];
$data [ '$client.postal_city' ] = & $data [ '$postal_city' ];
2020-11-09 03:39:42 +01:00
$data [ '$client.country' ] = & $data [ '$country' ];
$data [ '$client.email' ] = & $data [ '$email' ];
$data [ '$client.balance' ] = [ 'value' => Number :: formatMoney ( $this -> client -> balance , $this -> client ), 'label' => ctrans ( 'texts.account_balance' )];
$data [ '$outstanding' ] = [ 'value' => Number :: formatMoney ( $this -> client -> balance , $this -> client ), 'label' => ctrans ( 'texts.account_balance' )];
$data [ '$client_balance' ] = [ 'value' => Number :: formatMoney ( $this -> client -> balance , $this -> client ), 'label' => ctrans ( 'texts.account_balance' )];
$data [ '$paid_to_date' ] = [ 'value' => Number :: formatMoney ( $this -> client -> paid_to_date , $this -> client ), 'label' => ctrans ( 'texts.paid_to_date' )];
2021-03-31 23:28:51 +02:00
$data [ '$contact.full_name' ] = [ 'value' => isset ( $this -> contact ) ? $this -> contact -> present () -> name () : '' , 'label' => ctrans ( 'texts.name' )];
$data [ '$contact.email' ] = [ 'value' => isset ( $this -> contact ) ? $this -> contact -> email : '' , 'label' => ctrans ( 'texts.email' )];
2022-06-21 11:57:17 +02:00
$data [ '$contact.phone' ] = [ 'value' => isset ( $this -> contact ) ? $this -> contact -> phone : '' , 'label' => ctrans ( 'texts.phone' )];
2020-11-09 03:39:42 +01:00
$data [ '$contact.name' ] = [ 'value' => isset ( $this -> contact ) ? $this -> contact -> present () -> name () : 'no contact name on record' , 'label' => ctrans ( 'texts.contact_name' )];
$data [ '$contact.first_name' ] = [ 'value' => isset ( $this -> contact ) ? $this -> contact -> first_name : '' , 'label' => ctrans ( 'texts.first_name' )];
$data [ '$contact.last_name' ] = [ 'value' => isset ( $this -> contact ) ? $this -> contact -> last_name : '' , 'label' => ctrans ( 'texts.last_name' )];
2020-12-25 12:29:13 +01:00
$data [ '$contact.custom1' ] = [ 'value' => isset ( $this -> contact ) ? $this -> contact -> custom_value1 : ' ' , 'label' => $this -> helpers -> makeCustomField ( $this -> company -> custom_fields , 'contact1' )];
$data [ '$contact.custom2' ] = [ 'value' => isset ( $this -> contact ) ? $this -> contact -> custom_value2 : ' ' , 'label' => $this -> helpers -> makeCustomField ( $this -> company -> custom_fields , 'contact1' )];
$data [ '$contact.custom3' ] = [ 'value' => isset ( $this -> contact ) ? $this -> contact -> custom_value3 : ' ' , 'label' => $this -> helpers -> makeCustomField ( $this -> company -> custom_fields , 'contact1' )];
$data [ '$contact.custom4' ] = [ 'value' => isset ( $this -> contact ) ? $this -> contact -> custom_value4 : ' ' , 'label' => $this -> helpers -> makeCustomField ( $this -> company -> custom_fields , 'contact1' )];
2021-08-05 23:21:40 +02:00
$data [ '$firstName' ] = & $data [ '$contact.first_name' ];
2020-11-09 03:39:42 +01:00
$data [ '$company.city_state_postal' ] = [ 'value' => $this -> company -> present () -> cityStateZip ( $this -> settings -> city , $this -> settings -> state , $this -> settings -> postal_code , false ) ? : ' ' , 'label' => ctrans ( 'texts.city_state_postal' )];
$data [ '$company.postal_city_state' ] = [ 'value' => $this -> company -> present () -> cityStateZip ( $this -> settings -> city , $this -> settings -> state , $this -> settings -> postal_code , true ) ? : ' ' , 'label' => ctrans ( 'texts.postal_city_state' )];
2023-01-21 13:27:02 +01:00
$data [ '$company.postal_city' ] = [ 'value' => $this -> company -> present () -> cityStateZip ( $this -> settings -> city , null , $this -> settings -> postal_code , true ) ? : ' ' , 'label' => ctrans ( 'texts.postal_city' )];
2020-11-09 03:39:42 +01:00
$data [ '$company.name' ] = [ 'value' => $this -> company -> present () -> name () ? : ' ' , 'label' => ctrans ( 'texts.company_name' )];
$data [ '$company.address1' ] = [ 'value' => $this -> settings -> address1 ? : ' ' , 'label' => ctrans ( 'texts.address1' )];
$data [ '$company.address2' ] = [ 'value' => $this -> settings -> address2 ? : ' ' , 'label' => ctrans ( 'texts.address2' )];
$data [ '$company.city' ] = [ 'value' => $this -> settings -> city ? : ' ' , 'label' => ctrans ( 'texts.city' )];
$data [ '$company.state' ] = [ 'value' => $this -> settings -> state ? : ' ' , 'label' => ctrans ( 'texts.state' )];
$data [ '$company.postal_code' ] = [ 'value' => $this -> settings -> postal_code ? : ' ' , 'label' => ctrans ( 'texts.postal_code' )];
//$data['$company.country'] = ['value' => $this->getCountryName(), 'label' => ctrans('texts.country')];
$data [ '$company.phone' ] = [ 'value' => $this -> settings -> phone ? : ' ' , 'label' => ctrans ( 'texts.phone' )];
$data [ '$company.email' ] = [ 'value' => $this -> settings -> email ? : ' ' , 'label' => ctrans ( 'texts.email' )];
$data [ '$company.vat_number' ] = [ 'value' => $this -> settings -> vat_number ? : ' ' , 'label' => ctrans ( 'texts.vat_number' )];
$data [ '$company.id_number' ] = [ 'value' => $this -> settings -> id_number ? : ' ' , 'label' => ctrans ( 'texts.id_number' )];
$data [ '$company.website' ] = [ 'value' => $this -> settings -> website ? : ' ' , 'label' => ctrans ( 'texts.website' )];
$data [ '$company.address' ] = [ 'value' => $this -> company -> present () -> address ( $this -> settings ) ? : ' ' , 'label' => ctrans ( 'texts.address' )];
$logo = $this -> company -> present () -> logo ( $this -> settings );
$data [ '$company.logo' ] = [ 'value' => $logo ? : ' ' , 'label' => ctrans ( 'texts.logo' )];
$data [ '$company_logo' ] = & $data [ '$company.logo' ];
2020-12-25 12:29:13 +01:00
$data [ '$company1' ] = [ 'value' => $this -> helpers -> formatCustomFieldValue ( $this -> company -> custom_fields , 'company1' , $this -> settings -> custom_value1 , $this -> client ) ? : ' ' , 'label' => $this -> helpers -> makeCustomField ( $this -> company -> custom_fields , 'company1' )];
$data [ '$company2' ] = [ 'value' => $this -> helpers -> formatCustomFieldValue ( $this -> company -> custom_fields , 'company2' , $this -> settings -> custom_value2 , $this -> client ) ? : ' ' , 'label' => $this -> helpers -> makeCustomField ( $this -> company -> custom_fields , 'company2' )];
$data [ '$company3' ] = [ 'value' => $this -> helpers -> formatCustomFieldValue ( $this -> company -> custom_fields , 'company3' , $this -> settings -> custom_value3 , $this -> client ) ? : ' ' , 'label' => $this -> helpers -> makeCustomField ( $this -> company -> custom_fields , 'company3' )];
$data [ '$company4' ] = [ 'value' => $this -> helpers -> formatCustomFieldValue ( $this -> company -> custom_fields , 'company4' , $this -> settings -> custom_value4 , $this -> client ) ? : ' ' , 'label' => $this -> helpers -> makeCustomField ( $this -> company -> custom_fields , 'company4' )];
2020-11-09 03:39:42 +01:00
2023-02-15 23:20:24 +01:00
$data [ '$view_link' ] = [ 'value' => $this -> buildViewButton ( $this -> payment -> getLink (), ctrans ( 'texts.view_payment' )), 'label' => ctrans ( 'texts.view_payment' )];
2021-09-20 13:19:03 +02:00
$data [ '$view_button' ] = & $data [ '$view_link' ];
2022-08-31 06:30:23 +02:00
$data [ '$viewButton' ] = & $data [ '$view_link' ];
2022-03-14 22:24:17 +01:00
$data [ '$viewLink' ] = & $data [ '$view_link' ];
2021-08-05 23:21:40 +02:00
$data [ '$paymentLink' ] = & $data [ '$view_link' ];
2023-02-15 23:20:24 +01:00
$data [ '$portalButton' ] = [ 'value' => $this -> buildViewButton ( $this -> payment -> getPortalLink (), ctrans ( 'texts.login' )), 'label' => '' ];
2021-08-07 05:43:34 +02:00
$data [ '$portal_url' ] = & $data [ '$portalButton' ];
2021-08-05 23:21:40 +02:00
2020-11-09 03:39:42 +01:00
$data [ '$view_url' ] = [ 'value' => $this -> payment -> getLink (), 'label' => ctrans ( 'texts.view_payment' )];
2021-08-05 23:21:40 +02:00
$data [ '$signature' ] = [ 'value' => $this -> settings -> email_signature ? : ' ' , 'label' => '' ];
2022-03-14 22:24:17 +01:00
$data [ '$emailSignature' ] = & $data [ '$signature' ];
2020-11-09 03:39:42 +01:00
$data [ '$invoices' ] = [ 'value' => $this -> formatInvoices (), 'label' => ctrans ( 'texts.invoices' )];
2023-03-18 10:15:02 +01:00
$data [ '$invoice_references_subject' ] = [ 'value' => $this -> formatInvoiceReferencesSubject (), 'label' => ctrans ( 'texts.invoices' )];
2021-08-07 05:43:34 +02:00
$data [ '$invoice_references' ] = [ 'value' => $this -> formatInvoiceReferences (), 'label' => ctrans ( 'texts.invoices' )];
2021-10-14 07:25:09 +02:00
$data [ '$invoice' ] = [ 'value' => $this -> formatInvoice (), 'label' => ctrans ( 'texts.invoices' )];
2022-01-18 23:05:22 +01:00
$data [ '$invoice.po_number' ] = [ 'value' => $this -> formatPoNumber (), 'label' => ctrans ( 'texts.po_number' )];
$data [ '$poNumber' ] = & $data [ '$invoice.po_number' ];
2022-05-10 00:03:36 +02:00
$data [ '$payment.status' ] = [ 'value' => $this -> payment -> stringStatus ( $this -> payment -> status_id ), 'label' => ctrans ( 'texts.payment_status' )];
2022-08-29 10:15:50 +02:00
$data [ '$invoices.amount' ] = [ 'value' => $this -> formatInvoiceField ( 'amount' ), 'label' => ctrans ( 'texts.invoices' )];
$data [ '$invoices.balance' ] = [ 'value' => $this -> formatInvoiceField ( 'balance' ), 'label' => ctrans ( 'texts.invoices' )];
$data [ '$invoices.due_date' ] = [ 'value' => $this -> formatInvoiceField ( 'due_date' ), 'label' => ctrans ( 'texts.invoices' )];
$data [ '$invoices.po_number' ] = [ 'value' => $this -> formatInvoiceField ( 'po_number' ), 'label' => ctrans ( 'texts.invoices' )];
2023-03-23 06:51:45 +01:00
$data [ '$invoice_numbers' ] = [ 'value' => $this -> formatInvoiceNumbersRaw (), 'label' => ctrans ( 'texts.invoices' )];
2022-11-17 06:41:23 +01:00
2023-02-16 02:36:09 +01:00
if ( $this -> payment -> status_id == 4 ) {
2022-11-17 06:41:23 +01:00
$data [ '$status_logo' ] = [ 'value' => '<div class="stamp is-paid"> ' . ctrans ( 'texts.paid' ) . '</div>' , 'label' => '' ];
2023-02-16 02:36:09 +01:00
} else {
2022-11-17 06:41:23 +01:00
$data [ '$status_logo' ] = [ 'value' => '' , 'label' => '' ];
2023-02-16 02:36:09 +01:00
}
2022-11-17 06:41:23 +01:00
2022-02-12 06:05:05 +01:00
$arrKeysLength = array_map ( 'strlen' , array_keys ( $data ));
array_multisort ( $arrKeysLength , SORT_DESC , $data );
2022-06-21 11:57:17 +02:00
2020-11-09 03:39:42 +01:00
return $data ;
}
2022-08-29 10:15:50 +02:00
private function formatInvoiceField ( $field )
{
2022-10-30 11:37:22 +01:00
$invoicex = '' ;
2022-08-29 10:15:50 +02:00
foreach ( $this -> payment -> invoices as $invoice ) {
$invoice_field = $invoice -> { $field };
2023-02-16 02:36:09 +01:00
if ( in_array ( $field , [ 'amount' , 'balance' ])) {
2022-11-10 09:59:52 +01:00
$invoice_field = Number :: formatMoney ( $invoice_field , $this -> client );
2023-02-16 02:36:09 +01:00
}
2022-11-10 09:59:52 +01:00
2023-02-16 02:36:09 +01:00
if ( $field == 'due_date' ) {
2022-11-10 09:59:52 +01:00
$invoice_field = $this -> translateDate ( $invoice_field , $this -> client -> date_format (), $this -> client -> locale ());
2023-02-16 02:36:09 +01:00
}
2022-11-10 09:59:52 +01:00
2022-10-30 11:37:22 +01:00
$invoicex .= ctrans ( 'texts.invoice_number_short' ) . " { $invoice -> number } { $invoice_field } " ;
2022-08-29 10:15:50 +02:00
}
2022-10-30 11:37:22 +01:00
return $invoicex ;
2022-08-29 10:15:50 +02:00
}
2021-10-14 07:25:09 +02:00
private function formatInvoice ()
{
$invoice = '' ;
2022-06-21 11:57:17 +02:00
if ( $this -> payment -> invoices () -> exists ()) {
$invoice = ctrans ( 'texts.invoice_number_short' ) . implode ( ',' , $this -> payment -> invoices -> pluck ( 'number' ) -> toArray ());
}
2021-10-14 07:25:09 +02:00
return $invoice ;
}
2022-01-18 23:05:22 +01:00
private function formatPoNumber ()
{
$invoice = '' ;
2022-06-21 11:57:17 +02:00
if ( $this -> payment -> invoices () -> exists ()) {
$invoice = ctrans ( 'texts.po_number_short' ) . implode ( ',' , $this -> payment -> invoices -> pluck ( 'po_number' ) -> toArray ());
}
2022-01-18 23:05:22 +01:00
return $invoice ;
}
2020-11-09 03:39:42 +01:00
private function formatInvoices ()
{
2021-01-28 00:35:52 +01:00
$invoice_list = '<br><br>' ;
2020-11-09 03:39:42 +01:00
2020-11-25 15:19:52 +01:00
foreach ( $this -> payment -> invoices as $invoice ) {
2023-02-17 09:58:30 +01:00
$invoice_list .= ctrans ( 'texts.invoice_number_short' ) . " { $invoice -> number } " . Number :: formatMoney ( $invoice -> pivot -> amount , $this -> client ) . '<br>' ;
2020-11-09 03:39:42 +01:00
}
return $invoice_list ;
}
2023-03-18 10:15:02 +01:00
private function formatInvoiceReferencesSubject ()
{
2023-10-26 04:57:44 +02:00
$invoice_list = '' ;
2023-03-18 10:15:02 +01:00
foreach ( $this -> payment -> invoices as $invoice ) {
if ( strlen ( $invoice -> po_number ) > 1 ) {
$invoice_list .= ctrans ( 'texts.po_number' ) . " { $invoice -> po_number } <br> " ;
}
$invoice_list .= ctrans ( 'texts.invoice_number_short' ) . " { $invoice -> number } " . Number :: formatMoney ( $invoice -> pivot -> amount , $this -> client ) . ', ' ;
}
2023-10-26 04:57:44 +02:00
if ( strlen ( $invoice_list ) < 4 ) {
$invoice_list = Number :: formatMoney ( $this -> payment -> amount , $this -> client ) ? : ' ' ;
2023-06-22 03:54:11 +02:00
}
2023-03-18 10:15:02 +01:00
return $invoice_list ;
}
2023-10-26 04:57:44 +02:00
private function formatInvoiceNumbersRaw ()
{
2023-03-23 06:51:45 +01:00
return collect ( $this -> payment -> invoices -> pluck ( 'number' ) -> toArray ()) -> implode ( ', ' );
}
2023-03-18 10:15:02 +01:00
2021-08-07 05:43:34 +02:00
private function formatInvoiceReferences ()
{
$invoice_list = '<br><br>' ;
foreach ( $this -> payment -> invoices as $invoice ) {
2023-02-16 02:36:09 +01:00
if ( strlen ( $invoice -> po_number ) > 1 ) {
2022-08-29 10:15:50 +02:00
$invoice_list .= ctrans ( 'texts.po_number' ) . " { $invoice -> po_number } <br> " ;
2023-02-16 02:36:09 +01:00
}
2022-08-29 10:15:50 +02:00
2022-06-21 11:57:17 +02:00
$invoice_list .= ctrans ( 'texts.invoice_number_short' ) . " { $invoice -> number } <br> " ;
$invoice_list .= ctrans ( 'texts.invoice_amount' ) . ' ' . Number :: formatMoney ( $invoice -> pivot -> amount , $this -> client ) . '<br>' ;
$invoice_list .= ctrans ( 'texts.invoice_balance' ) . ' ' . Number :: formatMoney ( $invoice -> fresh () -> balance , $this -> client ) . '<br>' ;
$invoice_list .= '-----<br>' ;
2021-08-07 05:43:34 +02:00
}
return $invoice_list ;
}
2020-11-09 03:39:42 +01:00
public function makeValues () : array
{
$data = [];
$values = $this -> makePaymentVariables ();
foreach ( $values as $key => $value ) {
$data [ $key ] = $value [ 'value' ];
}
return $data ;
}
2023-02-15 23:20:24 +01:00
/**
* generateLabelsAndValues
2023-03-18 08:24:56 +01:00
*
2023-03-16 01:51:07 +01:00
* @ return array
2023-02-15 23:20:24 +01:00
*/
2023-03-16 01:51:07 +01:00
public function generateLabelsAndValues () : array
2022-10-27 11:17:31 +02:00
{
$data = [];
$values = $this -> makePaymentVariables ();
foreach ( $values as $key => $value ) {
$data [ 'values' ][ $key ] = $value [ 'value' ];
$data [ 'labels' ][ $key . '_label' ] = $value [ 'label' ];
}
return $data ;
}
2023-02-15 23:20:24 +01:00
/**
* buildViewButton
*
* @ param string $link
* @ param string $text
* @ return string
*/
private function buildViewButton ( string $link , string $text ) : string
{
2023-03-18 08:24:56 +01:00
if ( $this -> settings -> email_style == 'plain' ) {
return '<a href="' . $link . '" target="_blank">' . $text . '</a>' ;
}
2023-03-17 09:41:38 +01:00
2023-02-16 10:05:46 +01:00
return '
2023-02-22 00:22:42 +01:00
< div >
2023-02-16 11:09:50 +01:00
<!-- [ if ( gte mso 9 ) | ( IE )] >
< table align = " center " cellspacing = " 0 " cellpadding = " 0 " style = " width: 600px; " >
< tr >
< td align = " center " valign = " top " >
<! [ endif ] -->
< table align = " center " border = " 0 " cellpadding = " 0 " cellspacing = " 0 " role = " presentation " >
< tbody >< tr >
< td align = " center " class = " new_button " style = " border-radius: 2px; background-color: '. $this->settings ->primary_color.' " >
< a href = " '. $link . ' " target = " _blank " class = " new_button " style = " text-decoration: none; border: 1px solid '. $this->settings ->primary_color.'; display: inline-block; border-radius: 2px; padding-top: 15px; padding-bottom: 15px; padding-left: 25px; padding-right: 25px; font-size: 20px; color: #fff " >
< singleline label = " cta button " > '. $text .' </ singleline >
</ a >
</ td >
</ tr >
</ tbody >
</ table >
<!-- [ if ( gte mso 9 ) | ( IE )] >
</ td >
</ tr >
</ table >
<! [ endif ] -->
2023-02-22 00:22:42 +01:00
</ div >
2023-02-16 10:05:46 +01:00
' ;
2023-02-16 11:09:50 +01:00
2023-02-22 00:22:42 +01:00
2023-02-15 23:20:24 +01:00
return '
< table border = " 0 " cellspacing = " 0 " cellpadding = " 0 " align = " center " >
< tr style = " border: 0 !important; " >
< td class = " new_button " style = " padding: 12px 18px 12px 18px; border-radius:5px; " align = " center " >
< a href = " '. $link .' " target = " _blank " style = " border: 0 !important;font-size: 18px; font-family: Helvetica, Arial, sans-serif; color: #ffffff; text-decoration: none; display: inline-block; " > '. $text .' </ a >
</ td >
</ tr >
</ table >
' ;
}
2020-11-09 03:39:42 +01:00
}