mirror of
				https://github.com/invoiceninja/invoiceninja.git
				synced 2025-11-04 02:27:29 -05:00 
			
		
		
		
	Updates for vendor notifications
This commit is contained in:
		
							parent
							
								
									c1fef3db4f
								
							
						
					
					
						commit
						2d6bf6cace
					
				@ -92,6 +92,11 @@ class EmailTemplateDefaults
 | 
			
		||||
            case 'email_subject_custom3':
 | 
			
		||||
                return self::emailInvoiceSubject();
 | 
			
		||||
                
 | 
			
		||||
            case 'email_vendor_notification_subject':
 | 
			
		||||
                return self::emailVendorNotificationSubject();
 | 
			
		||||
 | 
			
		||||
            case 'email_vendor_notification_body':
 | 
			
		||||
                return self::emailVendorNotificationBody();
 | 
			
		||||
 | 
			
		||||
            default:
 | 
			
		||||
                return self::emailInvoiceTemplate();
 | 
			
		||||
@ -99,6 +104,16 @@ class EmailTemplateDefaults
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static function emailVendorNotificationSubject()
 | 
			
		||||
    {
 | 
			
		||||
        return self::transformText('vendor_notification_subject');
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static function emailVendorNotificationBody()
 | 
			
		||||
    {
 | 
			
		||||
        return self::transformText('vendor_notification_body');
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static function emailInvoiceSubject()
 | 
			
		||||
    {
 | 
			
		||||
        return ctrans('texts.invoice_subject', ['number'=>'$number', 'account'=>'$company.name']);
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										99
									
								
								app/Jobs/Expense/VendorExpenseNotify.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										99
									
								
								app/Jobs/Expense/VendorExpenseNotify.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,99 @@
 | 
			
		||||
<?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\Expense;
 | 
			
		||||
 | 
			
		||||
use App\Utils\Ninja;
 | 
			
		||||
use App\Utils\Number;
 | 
			
		||||
use App\Models\Expense;
 | 
			
		||||
use App\Models\Activity;
 | 
			
		||||
use App\Libraries\MultiDB;
 | 
			
		||||
use App\Models\VendorContact;
 | 
			
		||||
use App\Services\Email\Email;
 | 
			
		||||
use Illuminate\Bus\Queueable;
 | 
			
		||||
use App\Services\Email\EmailObject;
 | 
			
		||||
use Illuminate\Queue\SerializesModels;
 | 
			
		||||
use App\Repositories\ActivityRepository;
 | 
			
		||||
use App\Utils\Traits\MakesDates;
 | 
			
		||||
use Illuminate\Queue\InteractsWithQueue;
 | 
			
		||||
use Illuminate\Contracts\Queue\ShouldQueue;
 | 
			
		||||
use Illuminate\Foundation\Bus\Dispatchable;
 | 
			
		||||
 | 
			
		||||
class VendorExpenseNotify implements ShouldQueue
 | 
			
		||||
{
 | 
			
		||||
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, MakesDates;
 | 
			
		||||
 | 
			
		||||
    public $tries = 1;
 | 
			
		||||
 | 
			
		||||
    public function __construct(private Expense $expense, private string $db)
 | 
			
		||||
    {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function handle()
 | 
			
		||||
    {
 | 
			
		||||
        MultiDB::setDB($this->db);
 | 
			
		||||
 | 
			
		||||
        $this->expense->vendor->contacts->filter(function(VendorContact $contact){
 | 
			
		||||
            return $contact->send_email && $contact->email;
 | 
			
		||||
        })->each(function(VendorContact $contact){
 | 
			
		||||
            $this->notify($contact);
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function notify(VendorContact $contact)
 | 
			
		||||
    {
 | 
			
		||||
        
 | 
			
		||||
        $mo = new EmailObject;
 | 
			
		||||
        $mo->contact = $contact;
 | 
			
		||||
        $mo->vendor_contact_id = $contact->id;
 | 
			
		||||
        $mo->user_id = $this->expense->user_id;
 | 
			
		||||
        $mo->company_key = $this->expense->company->company_key;
 | 
			
		||||
        $mo->subject = ctrans('texts.vendor_notification_subject', [
 | 
			
		||||
            'amount' => Number::formatMoney($this->expense->amount, $contact->vendor),
 | 
			
		||||
            'vendor' => $contact->vendor->present()->name(),
 | 
			
		||||
        ]);
 | 
			
		||||
        $mo->body = ctrans('texts.vendor_notification_body', [
 | 
			
		||||
            'vendor' => $this->expense->vendor->present()->name(),
 | 
			
		||||
            'amount' => Number::formatMoney($this->expense->amount, $contact->vendor),
 | 
			
		||||
            'contact' => $contact->present()->name(),
 | 
			
		||||
            'payment_date' => $this->translateDate($this->expense->payment_date, $this->expense->company->date_format(), $this->expense->vendor->locale()),
 | 
			
		||||
            'transaction_reference' => $this->expense->transaction_reference ?? '',
 | 
			
		||||
            'number' => $this->expense->number,
 | 
			
		||||
        ]);
 | 
			
		||||
        $mo->template = '';
 | 
			
		||||
        $mo->email_template_body = 'vendor_notification_subject';
 | 
			
		||||
        $mo->email_template_subject = 'vendor_notification_body';
 | 
			
		||||
        $mo->vendor_id = $contact->vendor_id ?? null;
 | 
			
		||||
        $mo->variables = [
 | 
			
		||||
            'amount' => Number::formatMoney($this->expense->amount, $contact->vendor),
 | 
			
		||||
            'contact' => $contact->present()->name(),
 | 
			
		||||
            'vendor' => $contact->vendor->present()->name(),
 | 
			
		||||
            'payment_date' => $this->translateDate($this->expense->payment_date, $this->expense->company->date_format(), $this->expense->vendor->locale()),
 | 
			
		||||
            'transaction_reference' => $this->expense->transaction_reference ?? '',
 | 
			
		||||
            'number' => $this->expense->number,
 | 
			
		||||
        ];
 | 
			
		||||
 | 
			
		||||
        Email::dispatch($mo, $this->expense->company);
 | 
			
		||||
 | 
			
		||||
        $fields = new \stdClass();
 | 
			
		||||
        $fields->expense_id = $this->expense->id;
 | 
			
		||||
        $fields->vendor_id = $contact->vendor_id;
 | 
			
		||||
        $fields->vendor_contact_id = $contact->id;
 | 
			
		||||
        $fields->user_id = $this->expense->user_id;
 | 
			
		||||
        $fields->company_id = $contact->company_id;
 | 
			
		||||
        $fields->activity_type_id = Activity::VENDOR_NOTIFICATION_EMAIL;
 | 
			
		||||
        $fields->account_id = $this->expense->company->account_id;
 | 
			
		||||
 | 
			
		||||
        $activity_repo = new ActivityRepository();
 | 
			
		||||
        $activity_repo->save($fields, $this->expense, Ninja::eventVars());
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -257,6 +257,8 @@ class Activity extends StaticModel
 | 
			
		||||
 | 
			
		||||
    const PAYMENT_EMAILED = 138;
 | 
			
		||||
 | 
			
		||||
    const VENDOR_NOTIFICATION_EMAIL = 139;
 | 
			
		||||
 | 
			
		||||
    protected $casts = [
 | 
			
		||||
        'is_system' => 'boolean',
 | 
			
		||||
        'updated_at' => 'timestamp',
 | 
			
		||||
 | 
			
		||||
@ -13,6 +13,7 @@ namespace App\Models;
 | 
			
		||||
 | 
			
		||||
use App\DataMapper\CompanySettings;
 | 
			
		||||
use App\Models\Presenters\VendorPresenter;
 | 
			
		||||
use App\Services\Vendor\VendorService;
 | 
			
		||||
use App\Utils\Traits\AppSetup;
 | 
			
		||||
use App\Utils\Traits\GeneratesCounter;
 | 
			
		||||
use Illuminate\Database\Eloquent\SoftDeletes;
 | 
			
		||||
@ -280,4 +281,9 @@ class Vendor extends BaseModel
 | 
			
		||||
    {
 | 
			
		||||
        return $this->company->company_key.'/'.$this->vendor_hash.'/backups';
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function service()
 | 
			
		||||
    {
 | 
			
		||||
        return new VendorService($this);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -18,9 +18,10 @@ use App\Factory\ExpenseFactory;
 | 
			
		||||
use App\Models\ExpenseCategory;
 | 
			
		||||
use App\Utils\Traits\GeneratesCounter;
 | 
			
		||||
use Illuminate\Database\QueryException;
 | 
			
		||||
use App\Jobs\Expense\VendorExpenseNotify;
 | 
			
		||||
use Illuminate\Database\Eloquent\Collection;
 | 
			
		||||
use Carbon\Exceptions\InvalidFormatException;
 | 
			
		||||
use App\Libraries\Currency\Conversion\CurrencyApi;
 | 
			
		||||
use Illuminate\Database\Eloquent\Collection;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * ExpenseRepository.
 | 
			
		||||
@ -31,6 +32,7 @@ class ExpenseRepository extends BaseRepository
 | 
			
		||||
 | 
			
		||||
    private $completed = true;
 | 
			
		||||
 | 
			
		||||
    private $notify_vendor = false;
 | 
			
		||||
    /**
 | 
			
		||||
     * Saves the expense and its contacts.
 | 
			
		||||
     *
 | 
			
		||||
@ -41,10 +43,15 @@ class ExpenseRepository extends BaseRepository
 | 
			
		||||
     */
 | 
			
		||||
    public function save(array $data, Expense $expense): Expense
 | 
			
		||||
    {
 | 
			
		||||
        /** @var \App\Models\User $user */
 | 
			
		||||
        $user = auth()->user();
 | 
			
		||||
 | 
			
		||||
        if(isset($data['payment_date']) && $data['payment_date'] == $expense->payment_date) {
 | 
			
		||||
            //do nothing
 | 
			
		||||
        } elseif(isset($data['payment_date']) && strlen($data['payment_date']) > 1 && $expense->company->notify_vendor_when_paid) {
 | 
			
		||||
            nlog("NOT SAME");
 | 
			
		||||
        }
 | 
			
		||||
        elseif(isset($data['payment_date']) && strlen($data['payment_date']) > 1 && $user->company()->notify_vendor_when_paid && ($data['vendor_id'] || $expense->vendor_id)) {
 | 
			
		||||
            nlog("ping");
 | 
			
		||||
            $this->notify_vendor = true;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $expense->fill($data);
 | 
			
		||||
@ -63,6 +70,9 @@ class ExpenseRepository extends BaseRepository
 | 
			
		||||
            $this->saveDocuments($data['documents'], $expense);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if($this->notify_vendor)
 | 
			
		||||
            VendorExpenseNotify::dispatch($expense, $expense->company->db);
 | 
			
		||||
 | 
			
		||||
        return $expense;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -166,7 +166,6 @@ class EmailDefaults
 | 
			
		||||
 | 
			
		||||
        if (strlen($this->email->email_object->body) > 3) {
 | 
			
		||||
            // A Custom Message has been set in the email screen.
 | 
			
		||||
            // return $this;
 | 
			
		||||
        } elseif (strlen($this->email->email_object->settings?->{$this->email->email_object->email_template_body}) > 3) {
 | 
			
		||||
            // A body has been saved in the settings.
 | 
			
		||||
            $this->email->email_object->body = $this->email->email_object->settings?->{$this->email->email_object->email_template_body};
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										21
									
								
								app/Services/Vendor/VendorService.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								app/Services/Vendor/VendorService.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,21 @@
 | 
			
		||||
<?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\Services\Vendor;
 | 
			
		||||
 | 
			
		||||
use App\Models\Vendor;
 | 
			
		||||
 | 
			
		||||
class VendorService
 | 
			
		||||
{
 | 
			
		||||
    public function __construct(public Vendor $vendor)
 | 
			
		||||
    {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -5213,6 +5213,10 @@ $LANG = array(
 | 
			
		||||
    'payment_email_all_contacts' => 'Payment Email To All Contacts',
 | 
			
		||||
    'payment_email_all_contacts_help' => 'Sends the payment email to all contacts when enabled',
 | 
			
		||||
    'add_line' => 'Add Line',
 | 
			
		||||
    'activity_139' => 'Expense :expense notification sent to :contact',
 | 
			
		||||
    'vendor_notification_subject' => 'Confirmation of payment :amount sent to :vendor',
 | 
			
		||||
    'vendor_notification_body' => 'Payment processed for :amount dated :payment_date. <br>[Transaction Reference: :transaction_reference]',
 | 
			
		||||
 | 
			
		||||
); 
 | 
			
		||||
 | 
			
		||||
return $LANG;
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user