env-config

This commit is contained in:
paulwer 2023-12-14 08:32:15 +01:00
parent 1125f57a7f
commit b065542020
4 changed files with 58 additions and 28 deletions

View File

@ -24,9 +24,9 @@ class IncomingMailHandler
{ {
private $server; private $server;
public $connection; public $connection;
public function __construct(string $server, string $user, string $password) public function __construct(string $server, string $port, string $user, string $password)
{ {
$this->server = new Server($server); $this->server = new Server($server, $port == '' ? null : $port);
$this->connection = $this->server->authenticate($user, $password); $this->connection = $this->server->authenticate($user, $password);
} }

View File

@ -46,32 +46,26 @@ use Turbo124\Beacon\Facades\LightLogs;
/*Multi Mailer implemented*/ /*Multi Mailer implemented*/
class ExpenseImportJob implements ShouldQueue class InboundExpensesJob implements ShouldQueue
{ {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, MakesHash; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, MakesHash;
public $tries = 4; //number of retries public $tries = 4; //number of retries
public $deleteWhenMissingModels = true; public $deleteWhenMissingModels = true;
private array $imap_companies;
/** @var null|\App\Models\Company $company **/ private array $imap_credentials;
public Company $company;
private $expense_repo; private $expense_repo;
public function __construct() public function __construct()
{ {
$this->credentials = [];
$this->getImapCredentials();
$this->expense_repo = new ExpenseRepository(); $this->expense_repo = new ExpenseRepository();
} }
public function backoff()
{
// return [5, 10, 30, 240];
return [rand(5, 10), rand(30, 40), rand(60, 79), rand(160, 400)];
}
public function handle() public function handle()
{ {
@ -81,8 +75,8 @@ class ExpenseImportJob implements ShouldQueue
nlog("importing expenses from imap-servers"); nlog("importing expenses from imap-servers");
$a = Account::with('companies')->cursor()->each(function ($account) { Account::with('companies')->cursor()->each(function ($account) {
$account->companies()->where('expense_import', true)->whereNotNull('expense_mailbox_imap_host')->whereNotNull('expense_mailbox_imap_user')->whereNotNull('expense_mailbox_imap_password')->cursor()->each(function ($company) { $account->companies()->whereIn('id', $this->imap_companies)->cursor()->each(function ($company) {
$this->handleCompanyImap($company); $this->handleCompanyImap($company);
}); });
}); });
@ -90,9 +84,33 @@ class ExpenseImportJob implements ShouldQueue
} }
private function getImapCredentials()
{
$servers = explode(",", config('ninja.imap_inbound_expense.servers'));
$ports = explode(",", config('ninja.imap_inbound_expense.servers'));
$users = explode(",", config('ninja.imap_inbound_expense.servers'));
$passwords = explode(",", config('ninja.imap_inbound_expense.servers'));
$companies = explode(",", config('ninja.imap_inbound_expense.servers'));
if (sizeOf($servers) != sizeOf($ports) || sizeOf($servers) != sizeOf($users) || sizeOf($servers) != sizeOf($passwords) || sizeOf($servers) != sizeOf($companies))
throw new \Exception('invalid configuration imap_inbound_expenses (wrong element-count)');
foreach ($companies as $index => $companyId) {
$this->imap_credentials[$companyId] = [
"server" => $servers[$index],
"port" => $servers[$index],
"user" => $servers[$index],
"password" => $servers[$index],
];
$this->imap_companies[] = $companyId;
}
}
private function handleCompanyImap(Company $company) private function handleCompanyImap(Company $company)
{ {
$incommingMails = new IncomingMailHandler($company->expense_mailbox_imap_host, $company->company->expense_mailbox_imap_user, $company->company->expense_mailbox_imap_password); $credentials = $this->imap_credentials[$company->id];
$incommingMails = new IncomingMailHandler($credentials->server, $credentials->port, $credentials->user, $credentials->password);
$emails = $incommingMails->getUnprocessedEmails(); $emails = $incommingMails->getUnprocessedEmails();
@ -100,18 +118,19 @@ class ExpenseImportJob implements ShouldQueue
$sender = $mail->getSender(); $sender = $mail->getSender();
$vendor = Vendor::where('expense_sender_email', $sender)->orWhere($sender, 'LIKE', "CONCAT('%',expense_sender_email)")->first(); $vendor = Vendor::where('expense_sender_email', $sender)->orWhere($sender, 'LIKE', "CONCAT('%',expense_sender_domain)")->first();
if ($vendor !== null) if ($vendor !== null)
$vendor = Vendor::where("email", $sender)->first(); $vendor = Vendor::where("email", $sender)->first();
// TODO: check email for existing vendor?! $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" => $mail->getAttachments(), // 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
@ -128,4 +147,11 @@ class ExpenseImportJob implements ShouldQueue
} }
} }
public function backoff()
{
// return [5, 10, 30, 240];
return [rand(5, 10), rand(30, 40), rand(60, 79), rand(160, 400)];
}
} }

View File

@ -7,7 +7,7 @@ return [
'license_url' => 'https://app.invoiceninja.com', 'license_url' => 'https://app.invoiceninja.com',
'react_url' => env('REACT_URL', 'https://app.invoicing.co'), 'react_url' => env('REACT_URL', 'https://app.invoicing.co'),
'production' => env('NINJA_PROD', false), 'production' => env('NINJA_PROD', false),
'license' => env('NINJA_LICENSE', ''), 'license' => env('NINJA_LICENSE', ''),
'version_url' => 'https://pdf.invoicing.co/api/version', 'version_url' => 'https://pdf.invoicing.co/api/version',
'app_name' => env('APP_NAME', 'Invoice Ninja'), 'app_name' => env('APP_NAME', 'Invoice Ninja'),
'app_env' => env('APP_ENV', 'selfhosted'), 'app_env' => env('APP_ENV', 'selfhosted'),
@ -86,7 +86,7 @@ return [
'password' => 'password', 'password' => 'password',
'stripe' => env('STRIPE_KEYS', ''), 'stripe' => env('STRIPE_KEYS', ''),
'paypal' => env('PAYPAL_KEYS', ''), 'paypal' => env('PAYPAL_KEYS', ''),
'ppcp' => env('PPCP_KEYS', ''), 'ppcp' => env('PPCP_KEYS', ''),
'paypal_rest' => env('PAYPAL_REST_KEYS', ''), 'paypal_rest' => env('PAYPAL_REST_KEYS', ''),
'authorize' => env('AUTHORIZE_KEYS', ''), 'authorize' => env('AUTHORIZE_KEYS', ''),
'checkout' => env('CHECKOUT_KEYS', ''), 'checkout' => env('CHECKOUT_KEYS', ''),
@ -196,7 +196,7 @@ return [
'ninja_default_company_id' => env('NINJA_COMPANY_ID', null), 'ninja_default_company_id' => env('NINJA_COMPANY_ID', null),
'ninja_default_company_gateway_id' => env('NINJA_COMPANY_GATEWAY_ID', null), 'ninja_default_company_gateway_id' => env('NINJA_COMPANY_GATEWAY_ID', null),
'ninja_hosted_secret' => env('NINJA_HOSTED_SECRET', ''), 'ninja_hosted_secret' => env('NINJA_HOSTED_SECRET', ''),
'ninja_hosted_header' =>env('NINJA_HEADER', ''), 'ninja_hosted_header' => env('NINJA_HEADER', ''),
'ninja_connect_secret' => env('NINJA_CONNECT_SECRET', ''), 'ninja_connect_secret' => env('NINJA_CONNECT_SECRET', ''),
'internal_queue_enabled' => env('INTERNAL_QUEUE_ENABLED', true), 'internal_queue_enabled' => env('INTERNAL_QUEUE_ENABLED', true),
'ninja_apple_api_key' => env('APPLE_API_KEY', false), 'ninja_apple_api_key' => env('APPLE_API_KEY', false),
@ -227,5 +227,12 @@ return [
'paypal' => [ 'paypal' => [
'secret' => env('PAYPAL_SECRET', null), 'secret' => env('PAYPAL_SECRET', null),
'client_id' => env('PAYPAL_CLIENT_ID', null), 'client_id' => env('PAYPAL_CLIENT_ID', null),
],
'imap_inbound_expense' => [
'servers' => env('IMAP_INBOUND_EXPENSE_SERVERS', ''),
'ports' => env('IMAP_INBOUND_EXPENSE_PORTS', ''),
'users' => env('IMAP_INBOUND_EXPENSE_USERS', ''),
'passwords' => env('IMAP_INBOUND_EXPENSE_PASSWORDS', ''),
'companies' => env('IMAP_INBOUND_EXPENSE_COMPANIES', ''),
] ]
]; ];

View File

@ -11,14 +11,11 @@ return new class extends Migration {
public function up(): void public function up(): void
{ {
Schema::table('company', function (Blueprint $table) { Schema::table('company', function (Blueprint $table) {
$table->string("expense_mailbox_imap_host")->nullable(); $table->string("expense_import")->default(true);
$table->string("expense_mailbox_imap_port")->nullable();
$table->string("expense_mailbox_imap_user")->nullable();
$table->string("expense_mailbox_imap_password")->nullable();
}); });
Schema::table('vendor', function (Blueprint $table) { Schema::table('vendor', function (Blueprint $table) {
$table->string("expense_sender_email")->nullable(); $table->string("expense_sender_email")->nullable();
$table->string("expense_sender_url")->nullable(); $table->string("expense_sender_domain")->nullable();
}); });
} }