2019-04-29 07:50:08 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Utils\Traits;
|
|
|
|
|
2019-04-29 14:14:11 +02:00
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\Credit;
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\Quote;
|
2019-04-30 08:02:39 +02:00
|
|
|
use App\Models\RecurringInvoice;
|
|
|
|
use Illuminate\Support\Carbon;
|
2019-04-29 14:14:11 +02:00
|
|
|
|
2019-04-29 07:50:08 +02:00
|
|
|
/**
|
|
|
|
* Class GeneratesNumberCounter
|
|
|
|
* @package App\Utils\Traits
|
|
|
|
*/
|
|
|
|
trait GeneratesNumberCounter
|
|
|
|
{
|
|
|
|
|
|
|
|
public function getNextNumber($entity)
|
|
|
|
{
|
|
|
|
|
2019-04-29 14:14:11 +02:00
|
|
|
$counter = $this->getCounter($entity);
|
2019-04-30 08:02:39 +02:00
|
|
|
$check = false;
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
|
|
|
if ($this->hasNumberPattern($entity)) {
|
|
|
|
$number = $this->applyNumberPattern($entity, $counter);
|
|
|
|
} else {
|
|
|
|
$number = $prefix . str_pad($counter, $this->invoice_number_padding, '0', STR_PAD_LEFT);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($entity->recurring_invoice_id) {
|
|
|
|
$number = $this->recurring_invoice_number_prefix . $number;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($entity->isentity(ENTITY_CLIENT)) {
|
|
|
|
$check = Client::scope(false, $this->id)->whereIdNumber($number)->withTrashed()->first();
|
|
|
|
} else {
|
|
|
|
$check = Invoice::scope(false, $this->id)->whereInvoiceNumber($number)->withTrashed()->first();
|
|
|
|
}
|
|
|
|
$counter++;
|
|
|
|
$counterOffset++;
|
|
|
|
|
|
|
|
// prevent getting stuck in a loop
|
|
|
|
if ($number == $lastNumber) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
$lastNumber = $number;
|
|
|
|
|
|
|
|
} while ($check);
|
|
|
|
|
2019-04-29 14:14:11 +02:00
|
|
|
|
2019-04-29 07:50:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function hasSharedCounter() : bool
|
|
|
|
{
|
|
|
|
|
2019-04-30 08:02:39 +02:00
|
|
|
return $this->getSettingsByKey('shared_invoice_quote_counter')->shared_invoice_quote_counter;
|
2019-04-29 07:50:08 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-04-30 08:02:39 +02:00
|
|
|
/**
|
|
|
|
* @param $entity
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasNumberPattern($entity) : bool
|
|
|
|
{
|
|
|
|
return $this->getNumberPattern($entity) ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $entity
|
|
|
|
*
|
|
|
|
* @return NULL|string
|
|
|
|
*/
|
|
|
|
public function getNumberPattern($entity)
|
|
|
|
{
|
|
|
|
/** Recurring invoice share the same number pattern as invoices */
|
|
|
|
if($entity == RecurringInvoice::class )
|
|
|
|
$entity = Invoice::class;
|
|
|
|
|
|
|
|
$field = $this->entityName($entity) . "_number_pattern";
|
|
|
|
|
|
|
|
return $this->getSettingsByKey( $field )->{$field};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-04-29 14:14:11 +02:00
|
|
|
private function incrementCounter($entity)
|
2019-04-29 07:50:08 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-04-30 08:02:39 +02:00
|
|
|
private function entityName($entity)
|
2019-04-29 07:50:08 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
return strtolower(class_basename($entity));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-04-29 14:14:11 +02:00
|
|
|
public function getCounter($entity) : int
|
|
|
|
{
|
2019-04-30 08:02:39 +02:00
|
|
|
/** Recurring invoice share the same counter as invoices also harvest the invoice_counter if quote and invoices are sharing a counter */
|
|
|
|
if($entity == RecurringInvoice::class || $this->hasSharedCounter())
|
|
|
|
$entity = Invoice::class;
|
|
|
|
|
|
|
|
$counter = $this->entityName($entity) . '_number_counter';
|
2019-04-29 14:14:11 +02:00
|
|
|
|
|
|
|
return $this->getSettingsByKey( $counter )->{$counter};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-04-30 08:02:39 +02:00
|
|
|
/**
|
|
|
|
* @param $entity
|
|
|
|
* @param mixed $counter
|
|
|
|
* todo localize PHP date
|
|
|
|
* @return bool|mixed
|
|
|
|
*/
|
|
|
|
public function applyNumberPattern($entity, $counter = 1)
|
|
|
|
{
|
|
|
|
$counter = $counter ?: $this->getCounter($entity);
|
|
|
|
$pattern = $this->getNumberPattern($entity);
|
|
|
|
|
|
|
|
if (! $pattern) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$search = ['{$year}'];
|
|
|
|
$replace = [date('Y')];
|
|
|
|
|
|
|
|
$search[] = '{$counter}';
|
|
|
|
$replace[] = str_pad($counter, $this->getSettingsByKey( 'counter_padding' )->counter_padding, '0', STR_PAD_LEFT);
|
|
|
|
|
|
|
|
if (strstr($pattern, '{$user_id}')) {
|
|
|
|
$user_id = $entity->user ? $entity->user->id : (auth()->check() ? auth()->user()->id : 0);
|
|
|
|
$search[] = '{$user_id}';
|
|
|
|
$replace[] = str_pad(($user_id + 1), 2, '0', STR_PAD_LEFT);
|
|
|
|
}
|
|
|
|
|
|
|
|
$matches = false;
|
|
|
|
preg_match('/{\$date:(.*?)}/', $pattern, $matches);
|
|
|
|
if (count($matches) > 1) {
|
|
|
|
$format = $matches[1];
|
|
|
|
$search[] = $matches[0];
|
|
|
|
|
|
|
|
$date = Carbon::now($this->company->timezone()->name)->format($format);
|
|
|
|
$replace[] = str_replace($format, $date, $matches[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$pattern = str_replace($search, $replace, $pattern);
|
|
|
|
$pattern = $this->getClientInvoiceNumber($pattern, $entity);
|
|
|
|
|
|
|
|
return $pattern;
|
|
|
|
}
|
|
|
|
|
2019-04-29 07:50:08 +02:00
|
|
|
}
|