2023-12-10 16:06:33 +01:00
< ? php
/**
* Invoice Ninja ( https :// invoiceninja . com ) .
*
* @ link https :// github . com / invoiceninja / invoiceninja source repository
*
* @ copyright Copyright ( c ) 2023. Invoice Ninja LLC ( https :// invoiceninja . com )
*
* @ license https :// www . elastic . co / licensing / elastic - license
*/
namespace App\Jobs\Mail ;
use App\Events\Expense\ExpenseWasCreated ;
use App\Factory\ExpenseFactory ;
2023-12-14 16:40:43 +01:00
use App\Helpers\Mail\Mailbox\Imap\ImapMailbox ;
2023-12-10 16:06:33 +01:00
use App\Libraries\MultiDB ;
use App\Models\Company ;
use App\Models\Vendor ;
use App\Repositories\ExpenseRepository ;
use App\Utils\Ninja ;
2023-12-16 18:13:08 +01:00
use App\Utils\TempFile ;
2023-12-10 16:06:33 +01:00
use App\Utils\Traits\MakesHash ;
2023-12-16 18:13:08 +01:00
use App\Utils\Traits\SavesDocuments ;
2023-12-10 16:06:33 +01:00
use Illuminate\Bus\Queueable ;
use Illuminate\Contracts\Queue\ShouldQueue ;
use Illuminate\Foundation\Bus\Dispatchable ;
use Illuminate\Queue\InteractsWithQueue ;
use Illuminate\Queue\SerializesModels ;
/*Multi Mailer implemented*/
2023-12-14 10:33:49 +01:00
class ExpenseMailboxJob implements ShouldQueue
2023-12-10 16:06:33 +01:00
{
2023-12-16 18:13:08 +01:00
use Dispatchable , InteractsWithQueue , Queueable , SerializesModels , MakesHash , SavesDocuments ;
2023-12-10 16:06:33 +01:00
public $tries = 4 ; //number of retries
public $deleteWhenMissingModels = true ;
2023-12-14 08:32:15 +01:00
private array $imap_companies ;
private array $imap_credentials ;
2023-12-10 16:06:33 +01:00
private $expense_repo ;
public function __construct ()
{
2023-12-14 08:32:15 +01:00
$this -> credentials = [];
2023-12-10 16:06:33 +01:00
2023-12-14 08:32:15 +01:00
$this -> getImapCredentials ();
2023-12-10 16:06:33 +01:00
2023-12-14 10:33:49 +01:00
$this -> expense_repo = new ExpenseRepository (); // @turbo124 @todo is this the right aproach? should it be handled just with the model?
2023-12-10 16:06:33 +01:00
}
public function handle ()
{
//multiDB environment, need to
2023-12-16 18:13:08 +01:00
if ( sizeOf ( $this -> imap_credentials ) == 0 )
return ;
foreach ( $this -> imap_companies as $companyId ) {
$company = MultiDB :: findAndSetDbByCompanyId ( $companyId );
if ( ! $company ) {
nlog ( " processing of an imap_mailbox skipped because of unknown companyId: " . $companyId );
return ;
}
2023-12-10 16:06:33 +01:00
2023-12-16 18:13:08 +01:00
try {
nlog ( " start importing expenses from imap-server of company: " . $companyId );
$this -> handleImapCompany ( $company );
2023-12-10 16:06:33 +01:00
2023-12-16 18:13:08 +01:00
} catch ( \Exception $e ) {
nlog ( " processing of an imap_mailbox failed upnormally: " . $companyId . " message: " . $e -> getMessage ()); // @turbo124 @todo should this be handled in an other way?
2023-12-14 10:33:49 +01:00
}
2023-12-10 16:06:33 +01:00
}
}
2023-12-14 08:32:15 +01:00
private function getImapCredentials ()
{
2023-12-14 13:16:14 +01:00
$servers = array_map ( 'trim' , explode ( " , " , config ( 'ninja.inbound_expense.imap.servers' )));
2023-12-16 18:13:08 +01:00
$ports = array_map ( 'trim' , explode ( " , " , config ( 'ninja.inbound_expense.imap.ports' )));
$users = array_map ( 'trim' , explode ( " , " , config ( 'ninja.inbound_expense.imap.users' )));
$passwords = array_map ( 'trim' , explode ( " , " , config ( 'ninja.inbound_expense.imap.passwords' )));
$companies = array_map ( 'trim' , explode ( " , " , config ( 'ninja.inbound_expense.imap.companies' )));
2023-12-14 08:32:15 +01:00
if ( sizeOf ( $servers ) != sizeOf ( $ports ) || sizeOf ( $servers ) != sizeOf ( $users ) || sizeOf ( $servers ) != sizeOf ( $passwords ) || sizeOf ( $servers ) != sizeOf ( $companies ))
2023-12-14 13:16:14 +01:00
throw new \Exception ( 'invalid configuration inbound_expense.imap (wrong element-count)' );
2023-12-14 08:32:15 +01:00
foreach ( $companies as $index => $companyId ) {
2023-12-16 18:13:08 +01:00
if ( $servers [ $index ] == '' ) // if property is empty, ignore => this happens exspecialy when no config is provided and it enabled us to set a single default company for env (usefull on self-hosted)
continue ;
2023-12-14 08:32:15 +01:00
$this -> imap_credentials [ $companyId ] = [
" server " => $servers [ $index ],
2023-12-14 10:33:49 +01:00
" port " => $ports [ $index ] != '' ? $ports [ $index ] : null ,
" user " => $users [ $index ],
" password " => $passwords [ $index ],
2023-12-14 08:32:15 +01:00
];
$this -> imap_companies [] = $companyId ;
}
}
2023-12-14 10:33:49 +01:00
private function handleImapCompany ( Company $company )
2023-12-10 16:06:33 +01:00
{
2023-12-14 10:33:49 +01:00
nlog ( " importing expenses for company: " . $company -> id );
2023-12-14 08:32:15 +01:00
2023-12-14 10:33:49 +01:00
$credentials = $this -> imap_credentials [ $company -> id ];
$imapMailbox = new ImapMailbox ( $credentials -> server , $credentials -> port , $credentials -> user , $credentials -> password );
2023-12-10 16:06:33 +01:00
2023-12-14 10:33:49 +01:00
$emails = $imapMailbox -> getUnprocessedEmails ();
2023-12-10 16:06:33 +01:00
foreach ( $emails as $mail ) {
2023-12-14 10:33:49 +01:00
try {
2023-12-10 16:06:33 +01:00
2023-12-14 10:33:49 +01:00
$sender = $mail -> getSender ();
2023-12-10 16:06:33 +01:00
2023-12-14 10:33:49 +01:00
$vendor = Vendor :: where ( 'expense_sender_email' , $sender ) -> first ();
if ( $vendor == null )
$vendor = Vendor :: where ( $sender , 'LIKE' , " CONCAT('%',expense_sender_domain) " ) -> first ();
if ( $vendor == null )
$vendor = Vendor :: where ( " email " , $sender ) -> first ();
2023-12-10 16:06:33 +01:00
2023-12-14 10:33:49 +01:00
$documents = []; // TODO: $mail->getAttachments() + save email as document (.html)
2023-12-14 08:32:15 +01:00
2023-12-14 10:33:49 +01:00
$data = [
" vendor_id " => $vendor !== null ? $vendor -> id : null ,
" date " => $mail -> getDate (),
" public_notes " => $mail -> getSubject (),
" private_notes " => $mail -> getCompleteBodyText (),
" documents " => $documents , // FIXME: https://github.com/ddeboer/imap?tab=readme-ov-file#message-attachments
];
2023-12-16 18:13:08 +01:00
$expense = ExpenseFactory :: create ( $company -> company -> id , $company -> company -> owner () -> id );
2023-12-10 16:06:33 +01:00
2023-12-16 18:13:08 +01:00
$expense -> vendor_id = $vendor !== null ? $vendor -> id : null ;
$expense -> public_notes = $mail -> getSubject ();
$expense -> private_notes = $mail -> getBodyText ();
$expense -> date = $mail -> getDate ();
2023-12-10 16:06:33 +01:00
2023-12-16 18:13:08 +01:00
// add html_message as document to the expense
$documents [] = TempFile :: UploadedFileFromRaw ( $mail -> getBodyHtml (), " E-Mail.html " , " text/html " );
$this -> saveDocuments ( $documents , $expense );
$expense -> saveQuietly ();
event ( new ExpenseWasCreated ( $expense , $expense -> company , Ninja :: eventVars ( null )));
2023-12-14 10:33:49 +01:00
event ( 'eloquent.created: App\Models\Expense' , $expense );
2023-12-10 16:06:33 +01:00
2023-12-14 10:33:49 +01:00
$mail -> markAsSeen ();
$imapMailbox -> moveProcessed ( $mail );
2023-12-10 16:06:33 +01:00
2023-12-14 10:33:49 +01:00
} catch ( \Exception $e ) {
$imapMailbox -> moveFailed ( $mail );
2023-12-10 16:06:33 +01:00
2023-12-14 10:33:49 +01:00
nlog ( " processing of an email failed upnormally: " . $company -> id . " message: " . $e -> getMessage ()); // @turbo124 @todo should this be handled in an other way?
}
2023-12-10 16:06:33 +01:00
}
}
2023-12-14 08:32:15 +01:00
public function backoff ()
{
// return [5, 10, 30, 240];
return [ rand ( 5 , 10 ), rand ( 30 , 40 ), rand ( 60 , 79 ), rand ( 160 , 400 )];
}
2023-12-10 16:06:33 +01:00
}