mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
improv
This commit is contained in:
parent
b065542020
commit
d8064a9a35
@ -18,6 +18,7 @@ use App\Jobs\Cron\SubscriptionCron;
|
|||||||
use App\Jobs\Cron\UpdateCalculatedFields;
|
use App\Jobs\Cron\UpdateCalculatedFields;
|
||||||
use App\Jobs\Invoice\InvoiceCheckLateWebhook;
|
use App\Jobs\Invoice\InvoiceCheckLateWebhook;
|
||||||
use App\Jobs\Mail\ExpenseImportJob;
|
use App\Jobs\Mail\ExpenseImportJob;
|
||||||
|
use App\Jobs\Mail\ExpenseMailboxJob;
|
||||||
use App\Jobs\Ninja\AdjustEmailQuota;
|
use App\Jobs\Ninja\AdjustEmailQuota;
|
||||||
use App\Jobs\Ninja\BankTransactionSync;
|
use App\Jobs\Ninja\BankTransactionSync;
|
||||||
use App\Jobs\Ninja\CheckACHStatus;
|
use App\Jobs\Ninja\CheckACHStatus;
|
||||||
@ -98,6 +99,9 @@ class Kernel extends ConsoleKernel
|
|||||||
/* Fires webhooks for overdue Invoice */
|
/* Fires webhooks for overdue Invoice */
|
||||||
$schedule->job(new InvoiceCheckLateWebhook)->dailyAt('07:00')->withoutOverlapping()->name('invoice-overdue-job')->onOneServer();
|
$schedule->job(new InvoiceCheckLateWebhook)->dailyAt('07:00')->withoutOverlapping()->name('invoice-overdue-job')->onOneServer();
|
||||||
|
|
||||||
|
/* Check ExpenseMainboxes */
|
||||||
|
$schedule->job(new ExpenseMailboxJob)->everyThirtyMinutes()->withoutOverlapping()->name('expense-mailboxes-job')->onOneServer();
|
||||||
|
|
||||||
if (Ninja::isSelfHost()) {
|
if (Ninja::isSelfHost()) {
|
||||||
$schedule->call(function () {
|
$schedule->call(function () {
|
||||||
Account::whereNotNull('id')->update(['is_scheduler_running' => true]);
|
Account::whereNotNull('id')->update(['is_scheduler_running' => true]);
|
||||||
|
@ -20,13 +20,13 @@ use Ddeboer\Imap\Search\Flag\Unflagged;
|
|||||||
/**
|
/**
|
||||||
* GmailTransport.
|
* GmailTransport.
|
||||||
*/
|
*/
|
||||||
class IncomingMailHandler
|
class ImapMailbox
|
||||||
{
|
{
|
||||||
private $server;
|
private $server;
|
||||||
public $connection;
|
public $connection;
|
||||||
public function __construct(string $server, string $port, string $user, string $password)
|
public function __construct(string $server, string $port, string $user, string $password)
|
||||||
{
|
{
|
||||||
$this->server = new Server($server, $port == '' ? null : $port);
|
$this->server = new Server($server, $port != '' ? $port : null);
|
||||||
|
|
||||||
$this->connection = $this->server->authenticate($user, $password);
|
$this->connection = $this->server->authenticate($user, $password);
|
||||||
}
|
}
|
||||||
@ -54,4 +54,9 @@ class IncomingMailHandler
|
|||||||
{
|
{
|
||||||
return $mail->move($this->connection->getMailbox('PROCESSED'));
|
return $mail->move($this->connection->getMailbox('PROCESSED'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function moveFailed(MessageInterface $mail)
|
||||||
|
{
|
||||||
|
return $mail->move($this->connection->getMailbox('FAILED'));
|
||||||
|
}
|
||||||
}
|
}
|
@ -17,7 +17,7 @@ use App\Events\Expense\ExpenseWasCreated;
|
|||||||
use App\Events\Invoice\InvoiceWasEmailedAndFailed;
|
use App\Events\Invoice\InvoiceWasEmailedAndFailed;
|
||||||
use App\Events\Payment\PaymentWasEmailedAndFailed;
|
use App\Events\Payment\PaymentWasEmailedAndFailed;
|
||||||
use App\Factory\ExpenseFactory;
|
use App\Factory\ExpenseFactory;
|
||||||
use App\Helpers\Mail\IncomingMailHandler;
|
use App\Helpers\Mail\ImapMailbox;
|
||||||
use App\Jobs\Util\SystemLogger;
|
use App\Jobs\Util\SystemLogger;
|
||||||
use App\Libraries\Google\Google;
|
use App\Libraries\Google\Google;
|
||||||
use App\Libraries\MultiDB;
|
use App\Libraries\MultiDB;
|
||||||
@ -46,7 +46,7 @@ use Turbo124\Beacon\Facades\LightLogs;
|
|||||||
|
|
||||||
/*Multi Mailer implemented*/
|
/*Multi Mailer implemented*/
|
||||||
|
|
||||||
class InboundExpensesJob implements ShouldQueue
|
class ExpenseMailboxJob implements ShouldQueue
|
||||||
{
|
{
|
||||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, MakesHash;
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, MakesHash;
|
||||||
|
|
||||||
@ -63,7 +63,7 @@ class InboundExpensesJob implements ShouldQueue
|
|||||||
|
|
||||||
$this->getImapCredentials();
|
$this->getImapCredentials();
|
||||||
|
|
||||||
$this->expense_repo = new ExpenseRepository();
|
$this->expense_repo = new ExpenseRepository(); // @turbo124 @todo is this the right aproach? should it be handled just with the model?
|
||||||
}
|
}
|
||||||
|
|
||||||
public function handle()
|
public function handle()
|
||||||
@ -73,20 +73,20 @@ class InboundExpensesJob implements ShouldQueue
|
|||||||
foreach (MultiDB::$dbs as $db) {
|
foreach (MultiDB::$dbs as $db) {
|
||||||
MultiDB::setDB($db);
|
MultiDB::setDB($db);
|
||||||
|
|
||||||
nlog("importing expenses from imap-servers");
|
if (sizeOf($this->imap_credentials) != 0) {
|
||||||
|
nlog("importing expenses from imap-servers");
|
||||||
|
|
||||||
Account::with('companies')->cursor()->each(function ($account) {
|
Company::whereIn('id', $this->imap_companies)->cursor()->each(function ($company) {
|
||||||
$account->companies()->whereIn('id', $this->imap_companies)->cursor()->each(function ($company) {
|
$this->handleImapCompany($company);
|
||||||
$this->handleCompanyImap($company);
|
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getImapCredentials()
|
private function getImapCredentials()
|
||||||
{
|
{
|
||||||
$servers = explode(",", config('ninja.imap_inbound_expense.servers'));
|
$servers = array_map('trim', explode(",", config('ninja.imap_inbound_expense.servers')));
|
||||||
$ports = explode(",", config('ninja.imap_inbound_expense.servers'));
|
$ports = explode(",", config('ninja.imap_inbound_expense.servers'));
|
||||||
$users = explode(",", config('ninja.imap_inbound_expense.servers'));
|
$users = explode(",", config('ninja.imap_inbound_expense.servers'));
|
||||||
$passwords = explode(",", config('ninja.imap_inbound_expense.servers'));
|
$passwords = explode(",", config('ninja.imap_inbound_expense.servers'));
|
||||||
@ -98,51 +98,59 @@ class InboundExpensesJob implements ShouldQueue
|
|||||||
foreach ($companies as $index => $companyId) {
|
foreach ($companies as $index => $companyId) {
|
||||||
$this->imap_credentials[$companyId] = [
|
$this->imap_credentials[$companyId] = [
|
||||||
"server" => $servers[$index],
|
"server" => $servers[$index],
|
||||||
"port" => $servers[$index],
|
"port" => $ports[$index] != '' ? $ports[$index] : null,
|
||||||
"user" => $servers[$index],
|
"user" => $users[$index],
|
||||||
"password" => $servers[$index],
|
"password" => $passwords[$index],
|
||||||
];
|
];
|
||||||
$this->imap_companies[] = $companyId;
|
$this->imap_companies[] = $companyId;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function handleCompanyImap(Company $company)
|
private function handleImapCompany(Company $company)
|
||||||
{
|
{
|
||||||
|
nlog("importing expenses for company: " . $company->id);
|
||||||
|
|
||||||
$credentials = $this->imap_credentials[$company->id];
|
$credentials = $this->imap_credentials[$company->id];
|
||||||
|
$imapMailbox = new ImapMailbox($credentials->server, $credentials->port, $credentials->user, $credentials->password);
|
||||||
|
|
||||||
$incommingMails = new IncomingMailHandler($credentials->server, $credentials->port, $credentials->user, $credentials->password);
|
$emails = $imapMailbox->getUnprocessedEmails();
|
||||||
|
|
||||||
$emails = $incommingMails->getUnprocessedEmails();
|
|
||||||
|
|
||||||
foreach ($emails as $mail) {
|
foreach ($emails as $mail) {
|
||||||
|
|
||||||
$sender = $mail->getSender();
|
try {
|
||||||
|
|
||||||
$vendor = Vendor::where('expense_sender_email', $sender)->orWhere($sender, 'LIKE', "CONCAT('%',expense_sender_domain)")->first();
|
$sender = $mail->getSender();
|
||||||
|
|
||||||
if ($vendor !== null)
|
$vendor = Vendor::where('expense_sender_email', $sender)->first();
|
||||||
$vendor = Vendor::where("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();
|
||||||
|
|
||||||
$documents = []; // TODO: $mail->getAttachments() + save email as document (.html)
|
$documents = []; // TODO: $mail->getAttachments() + save email as document (.html)
|
||||||
|
|
||||||
$data = [
|
$data = [
|
||||||
"vendor_id" => $vendor !== null ? $vendor->id : null,
|
"vendor_id" => $vendor !== null ? $vendor->id : null,
|
||||||
"date" => $mail->getDate(),
|
"date" => $mail->getDate(),
|
||||||
"public_notes" => $mail->getSubject(),
|
"public_notes" => $mail->getSubject(),
|
||||||
"private_notes" => $mail->getCompleteBodyText(),
|
"private_notes" => $mail->getCompleteBodyText(),
|
||||||
"documents" => $documents, // FIXME: https://github.com/ddeboer/imap?tab=readme-ov-file#message-attachments
|
"documents" => $documents, // FIXME: https://github.com/ddeboer/imap?tab=readme-ov-file#message-attachments
|
||||||
];
|
];
|
||||||
|
|
||||||
$expense = $this->expense_repo->save($data, ExpenseFactory::create($company->company->id, $company->company->owner()->id)); // TODO: dont assign a new number at beginning
|
$expense = $this->expense_repo->save($data, ExpenseFactory::create($company->company->id, $company->company->owner()->id)); // TODO: dont assign a new number at beginning
|
||||||
|
|
||||||
// TODO: check for recurring expense?! => maybe replace existing ?!
|
event(new ExpenseWasCreated($expense, $expense->company, Ninja::eventVars(null)));
|
||||||
|
|
||||||
event(new ExpenseWasCreated($expense, $expense->company, Ninja::eventVars(null)));
|
event('eloquent.created: App\Models\Expense', $expense);
|
||||||
|
|
||||||
event('eloquent.created: App\Models\Expense', $expense);
|
$mail->markAsSeen();
|
||||||
|
$imapMailbox->moveProcessed($mail);
|
||||||
|
|
||||||
$mail->markAsSeen();
|
} catch (\Exception $e) {
|
||||||
$incommingMails->moveProcessed($mail);
|
$imapMailbox->moveFailed($mail);
|
||||||
|
|
||||||
|
nlog("processing of an email failed upnormally: " . $company->id . " message: " . $e->getMessage()); // @turbo124 @todo should this be handled in an other way?
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user