credentials = []; $this->getImapCredentials(); $this->expense_repo = new ExpenseRepository(); } public function handle() { //multiDB environment, need to foreach (MultiDB::$dbs as $db) { MultiDB::setDB($db); nlog("importing expenses from imap-servers"); Account::with('companies')->cursor()->each(function ($account) { $account->companies()->whereIn('id', $this->imap_companies)->cursor()->each(function ($company) { $this->handleCompanyImap($company); }); }); } } 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) { $credentials = $this->imap_credentials[$company->id]; $incommingMails = new IncomingMailHandler($credentials->server, $credentials->port, $credentials->user, $credentials->password); $emails = $incommingMails->getUnprocessedEmails(); foreach ($emails as $mail) { $sender = $mail->getSender(); $vendor = Vendor::where('expense_sender_email', $sender)->orWhere($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) $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 ]; $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('eloquent.created: App\Models\Expense', $expense); $mail->markAsSeen(); $incommingMails->moveProcessed($mail); } } public function backoff() { // return [5, 10, 30, 240]; return [rand(5, 10), rand(30, 40), rand(60, 79), rand(160, 400)]; } }