mirror of
				https://github.com/invoiceninja/invoiceninja.git
				synced 2025-11-04 04:07:32 -05:00 
			
		
		
		
	Working on discounts
This commit is contained in:
		
							parent
							
								
									5aed73a43f
								
							
						
					
					
						commit
						bbb0e07eca
					
				@ -7,6 +7,7 @@ use DateTime;
 | 
				
			|||||||
use Event;
 | 
					use Event;
 | 
				
			||||||
use Cache;
 | 
					use Cache;
 | 
				
			||||||
use App;
 | 
					use App;
 | 
				
			||||||
 | 
					use Carbon;
 | 
				
			||||||
use App\Events\UserSettingsChanged;
 | 
					use App\Events\UserSettingsChanged;
 | 
				
			||||||
use Illuminate\Support\Facades\Storage;
 | 
					use Illuminate\Support\Facades\Storage;
 | 
				
			||||||
use Illuminate\Database\Eloquent\SoftDeletes;
 | 
					use Illuminate\Database\Eloquent\SoftDeletes;
 | 
				
			||||||
@ -1395,7 +1396,7 @@ class Account extends Eloquent
 | 
				
			|||||||
            $date = date_create();
 | 
					            $date = date_create();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return $date->format('Y-m-d');
 | 
					        return Carbon::instance($date);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
 | 
				
			|||||||
@ -54,13 +54,15 @@ class Company extends Eloquent
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // handle promos and discounts
 | 
					    // handle promos and discounts
 | 
				
			||||||
    public function hasActiveDiscount()
 | 
					    public function hasActiveDiscount(Carbon $date = null)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        if ( ! $this->discount) {
 | 
					        if ( ! $this->discount) {
 | 
				
			||||||
            return false;
 | 
					            return false;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return $this->discount_expires && $this->discount_expires->gt(Carbon::today());
 | 
					        $date = $date ?: Carbon::today();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return $this->discount_expires && $this->discount_expires->gt($date);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function discountedPrice($price)
 | 
					    public function discountedPrice($price)
 | 
				
			||||||
 | 
				
			|||||||
@ -278,7 +278,9 @@ class AccountRepository
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        $account = $this->getNinjaAccount();
 | 
					        $account = $this->getNinjaAccount();
 | 
				
			||||||
        $lastInvoice = Invoice::withTrashed()->whereAccountId($account->id)->orderBy('public_id', 'DESC')->first();
 | 
					        $lastInvoice = Invoice::withTrashed()->whereAccountId($account->id)->orderBy('public_id', 'DESC')->first();
 | 
				
			||||||
 | 
					        $renewalDate = $clientAccount->getRenewalDate();
 | 
				
			||||||
        $publicId = $lastInvoice ? ($lastInvoice->public_id + 1) : 1;
 | 
					        $publicId = $lastInvoice ? ($lastInvoice->public_id + 1) : 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $invoice = new Invoice();
 | 
					        $invoice = new Invoice();
 | 
				
			||||||
        $invoice->is_public = true;
 | 
					        $invoice->is_public = true;
 | 
				
			||||||
        $invoice->account_id = $account->id;
 | 
					        $invoice->account_id = $account->id;
 | 
				
			||||||
@ -286,13 +288,13 @@ class AccountRepository
 | 
				
			|||||||
        $invoice->public_id = $publicId;
 | 
					        $invoice->public_id = $publicId;
 | 
				
			||||||
        $invoice->client_id = $client->id;
 | 
					        $invoice->client_id = $client->id;
 | 
				
			||||||
        $invoice->invoice_number = $account->getNextInvoiceNumber($invoice);
 | 
					        $invoice->invoice_number = $account->getNextInvoiceNumber($invoice);
 | 
				
			||||||
        $invoice->invoice_date = $clientAccount->getRenewalDate();
 | 
					        $invoice->invoice_date = $renewalDate->format('Y-m-d');
 | 
				
			||||||
        $invoice->amount = $invoice->balance = $plan_cost - $credit;
 | 
					        $invoice->amount = $invoice->balance = $plan_cost - $credit;
 | 
				
			||||||
        $invoice->invoice_type_id = INVOICE_TYPE_STANDARD;
 | 
					        $invoice->invoice_type_id = INVOICE_TYPE_STANDARD;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // check for promo/discount
 | 
					        // check for promo/discount
 | 
				
			||||||
        $clientCompany = $clientAccount->company;
 | 
					        $clientCompany = $clientAccount->company;
 | 
				
			||||||
        if ($clientCompany->hasActivePromo() || $clientCompany->hasActiveDiscount()) {
 | 
					        if ($clientCompany->hasActivePromo() || $clientCompany->hasActiveDiscount($renewalDate)) {
 | 
				
			||||||
            $discount = $invoice->amount * $clientCompany->discount;
 | 
					            $discount = $invoice->amount * $clientCompany->discount;
 | 
				
			||||||
            $invoice->discount = $clientCompany->discount * 100;
 | 
					            $invoice->discount = $clientCompany->discount * 100;
 | 
				
			||||||
            $invoice->amount -= $discount;
 | 
					            $invoice->amount -= $discount;
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user