Skip to content

Commit

Permalink
Merge pull request #36 from tr3mulant/master
Browse files Browse the repository at this point in the history
Remove InventorySku functionality
  • Loading branch information
dvicklund authored Sep 27, 2022
2 parents 654e3ca + c7cbe56 commit 3fcaa40
Show file tree
Hide file tree
Showing 16 changed files with 160 additions and 652 deletions.
1 change: 0 additions & 1 deletion src/Commands/SchemaCheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class SchemaCheckCommand extends Command
'suppliers',
'inventory',
'inventories',
'inventory_skus',
'inventory_stocks',
'inventory_stock_movements',
'inventory_suppliers',
Expand Down
11 changes: 1 addition & 10 deletions src/Models/Inventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Inventory extends BaseModel
'category_id',
'metric_id',
'name',
'sku',
'description',
'is_parent',
'is_bundle',
Expand All @@ -51,16 +52,6 @@ public function metric()
return $this->hasOne(Metric::class, 'id', 'metric_id');
}

/**
* The hasOne sku relationship.
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function sku()
{
return $this->hasOne(InventorySku::class, 'inventory_id', 'id');
}

/**
* The hasMany stocks relationship.
*
Expand Down
30 changes: 0 additions & 30 deletions src/Models/InventorySku.php

This file was deleted.

16 changes: 0 additions & 16 deletions src/Traits/InventorySkuTrait.php

This file was deleted.

266 changes: 1 addition & 265 deletions src/Traits/InventoryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,6 @@ abstract public function category();
*/
abstract public function metric();

/**
* The hasOne SKU relationship.
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
abstract public function sku();

/**
* The hasMany stocks relationship.
*
Expand Down Expand Up @@ -101,23 +94,6 @@ public static function bootInventoryTrait()
static::creating(function (Model $record) {
$record->created_by = static::getCurrentUserId();
});

/*
* Generate the items SKU once it's created
*/
static::created(function (Model $record) {
$record->generateSku();
});

/*
* Generate an SKU if the item has been assigned a category,
* this will not overwrite any SKU the item had previously
*/
static::updated(function (Model $record) {
if ($record->category_id !== null) {
$record->generateSku();
}
});
}

/**
Expand Down Expand Up @@ -576,215 +552,11 @@ public function getStockFromLocation($location)
*/
public function getSku()
{
if ($this->hasSku()) {
return $this->sku->code;
}

return;
}

/**
* Laravel accessor for the current items SKU.
*
* @return null|string
*/
public function getSkuCodeAttribute()
{
return $this->getSku();
}

/**
* Generates an item SKU record.
*
* If an item already has an SKU, the SKU record will be returned.
*
* If an item does not have a category, it will return false.
*
* @return bool|mixed
*/
public function generateSku()
{
/*
* Make sure sku generation is enabled and the item has a category, if not we'll return false.
*/
if (!$this->skusEnabled() || !$this->hasCategory()) {
return false;
}

/*
* If the item already has an SKU, we'll return it
*/
if ($this->hasSku()) {
return $this->sku;
}

/*
* Get the set SKU code length from the configuration file
*/
$codeLength = Config::get('inventory'.InventoryServiceProvider::$packageConfigSeparator.'sku_code_length');

/*
* Get the set SKU prefix length from the configuration file
*/
$prefixLength = Config::get('inventory'.InventoryServiceProvider::$packageConfigSeparator.'sku_prefix_length');

/*
* Get the set SKU separator
*/
$skuSeparator = Config::get('inventory'.InventoryServiceProvider::$packageConfigSeparator.'sku_separator');

/*
* Make sure we trim empty spaces in the separator if it's a string, otherwise we'll
* set it to NULL
*/
$skuSeparator = (is_string($skuSeparator) ? trim($skuSeparator) : null);

/*
* Trim the category name to remove blank spaces, then
* grab the first 3 letters of the string, and uppercase them
*/
$prefix = strtoupper(substr(trim($this->category->name), 0, intval($prefixLength)));

/*
* We'll make sure the prefix length is greater than zero before we try and
* generate an SKU
*/
if (strlen($prefix) > 0) {
/*
* Create the numerical code by the items ID
* to accompany the prefix and pad left zeros
*/
$code = str_pad($this->getKey(), $codeLength, '0', STR_PAD_LEFT);

/*
* Process the generation
*/
return $this->processSkuGeneration($this->getKey(), $prefix.$skuSeparator.$code);
}

/*
* Always return false on generation failure
*/
return false;
}

/**
* Regenerates the current items SKU by
* deleting its current SKU and creating
* another. This will also generate an SKU
* if one does not exist.
*
* @return bool|mixed
*/
public function regenerateSku()
{
$sku = $this->sku()->first();

if ($sku) {
/*
* Capture current SKU
*/
$previousSku = $sku;

/*
* Delete current SKU
*/
$sku->delete();

/*
* Try to generate a new SKU
*/
$newSku = $this->generateSku();

/*
* New sku generation successful, return it
*/
if ($newSku) {
return $newSku;
}

/*
* Failed generating a new sku, we'll restore the old one
*/
return $this->processSkuGeneration($this->getKey(), $previousSku->code);
}

/*
* Always generate an SKU if one doesn't exist
*/
return $this->generateSku();
}

/**
* Creates an SKU with the specified code. If overwrite is true,
* the current items SKU will be deleted if it exists before creating
* then SKU. If overwrite is false but the item has an SKU, an exception
* is thrown.
*
* @param string $code
* @param bool $overwrite
*
* @throws SkuAlreadyExistsException
*
* @return mixed|bool
*/
public function createSku($code, $overwrite = false)
{
/*
* Get the current SKU record
*/
$sku = $this->sku()->first();

if ($sku) {
/*
* The dev doesn't want the SKU overridden,
* we'll thrown an exception
*/
if (!$overwrite) {
$message = Lang::get('inventory::exceptions.SkuAlreadyExistsException');

throw new SkuAlreadyExistsException($message);
}

/*
* Overwrite is true, lets update the current SKU
*/
return $this->updateSku($code, $sku);
}

/*
* No SKU exists, lets create one
*/
return $this->processSkuGeneration($this->getKey(), $code);
}

/**
* Updates the items current SKU or the SKU
* supplied with the specified code.
*
* @param string $code
* @param null $sku
*
* @return mixed|bool
*/
public function updateSku($code, $sku = null)
{
/*
* Get the current SKU record if one isn't supplied
*/
if (!$sku) {
$sku = $this->sku()->first();
}

/*
* If an SKU still doesn't exist after
* trying to find one, we'll create one
*/
if (!$sku) {
return $this->processSkuGeneration($this->getKey(), $code);
}

return $this->processSkuUpdate($sku, $code);
return;
}

/**
Expand Down Expand Up @@ -976,42 +748,6 @@ private function resolveSupplier($supplier) {
return $s;
}

/**
* Processes an SKU generation covered by database transactions.
*
* @param int|string $inventoryId
* @param string $code
*
* @return bool|mixed
*/
private function processSkuGeneration($inventoryId, $code)
{
$this->dbStartTransaction();

try {
$insert = [
'inventory_id' => $inventoryId,
'code' => $code,
];

$record = $this->sku()->create($insert);

if ($record) {
$this->dbCommitTransaction();

$this->fireEvent('inventory.sku.generated', [
'item' => $this,
]);

return $record;
}
} catch (\Exception $e) {
$this->dbRollbackTransaction();
}

return false;
}

/**
* Processes updating the specified SKU
* record with the specified code.
Expand Down
Loading

0 comments on commit 3fcaa40

Please sign in to comment.