Skip to content

Commit 3fcaa40

Browse files
authored
Merge pull request #36 from tr3mulant/master
Remove InventorySku functionality
2 parents 654e3ca + c7cbe56 commit 3fcaa40

16 files changed

Lines changed: 160 additions & 652 deletions

src/Commands/SchemaCheckCommand.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ class SchemaCheckCommand extends Command
5151
'suppliers',
5252
'inventory',
5353
'inventories',
54-
'inventory_skus',
5554
'inventory_stocks',
5655
'inventory_stock_movements',
5756
'inventory_suppliers',

src/Models/Inventory.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class Inventory extends BaseModel
2626
'category_id',
2727
'metric_id',
2828
'name',
29+
'sku',
2930
'description',
3031
'is_parent',
3132
'is_bundle',
@@ -51,16 +52,6 @@ public function metric()
5152
return $this->hasOne(Metric::class, 'id', 'metric_id');
5253
}
5354

54-
/**
55-
* The hasOne sku relationship.
56-
*
57-
* @return \Illuminate\Database\Eloquent\Relations\HasOne
58-
*/
59-
public function sku()
60-
{
61-
return $this->hasOne(InventorySku::class, 'inventory_id', 'id');
62-
}
63-
6455
/**
6556
* The hasMany stocks relationship.
6657
*

src/Models/InventorySku.php

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/Traits/InventorySkuTrait.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/Traits/InventoryTrait.php

Lines changed: 1 addition & 265 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,6 @@ abstract public function category();
5353
*/
5454
abstract public function metric();
5555

56-
/**
57-
* The hasOne SKU relationship.
58-
*
59-
* @return \Illuminate\Database\Eloquent\Relations\HasOne
60-
*/
61-
abstract public function sku();
62-
6356
/**
6457
* The hasMany stocks relationship.
6558
*
@@ -101,23 +94,6 @@ public static function bootInventoryTrait()
10194
static::creating(function (Model $record) {
10295
$record->created_by = static::getCurrentUserId();
10396
});
104-
105-
/*
106-
* Generate the items SKU once it's created
107-
*/
108-
static::created(function (Model $record) {
109-
$record->generateSku();
110-
});
111-
112-
/*
113-
* Generate an SKU if the item has been assigned a category,
114-
* this will not overwrite any SKU the item had previously
115-
*/
116-
static::updated(function (Model $record) {
117-
if ($record->category_id !== null) {
118-
$record->generateSku();
119-
}
120-
});
12197
}
12298

12399
/**
@@ -576,215 +552,11 @@ public function getStockFromLocation($location)
576552
*/
577553
public function getSku()
578554
{
579-
if ($this->hasSku()) {
580-
return $this->sku->code;
581-
}
582-
583-
return;
584-
}
585-
586-
/**
587-
* Laravel accessor for the current items SKU.
588-
*
589-
* @return null|string
590-
*/
591-
public function getSkuCodeAttribute()
592-
{
593-
return $this->getSku();
594-
}
595-
596-
/**
597-
* Generates an item SKU record.
598-
*
599-
* If an item already has an SKU, the SKU record will be returned.
600-
*
601-
* If an item does not have a category, it will return false.
602-
*
603-
* @return bool|mixed
604-
*/
605-
public function generateSku()
606-
{
607-
/*
608-
* Make sure sku generation is enabled and the item has a category, if not we'll return false.
609-
*/
610-
if (!$this->skusEnabled() || !$this->hasCategory()) {
611-
return false;
612-
}
613-
614-
/*
615-
* If the item already has an SKU, we'll return it
616-
*/
617555
if ($this->hasSku()) {
618556
return $this->sku;
619557
}
620558

621-
/*
622-
* Get the set SKU code length from the configuration file
623-
*/
624-
$codeLength = Config::get('inventory'.InventoryServiceProvider::$packageConfigSeparator.'sku_code_length');
625-
626-
/*
627-
* Get the set SKU prefix length from the configuration file
628-
*/
629-
$prefixLength = Config::get('inventory'.InventoryServiceProvider::$packageConfigSeparator.'sku_prefix_length');
630-
631-
/*
632-
* Get the set SKU separator
633-
*/
634-
$skuSeparator = Config::get('inventory'.InventoryServiceProvider::$packageConfigSeparator.'sku_separator');
635-
636-
/*
637-
* Make sure we trim empty spaces in the separator if it's a string, otherwise we'll
638-
* set it to NULL
639-
*/
640-
$skuSeparator = (is_string($skuSeparator) ? trim($skuSeparator) : null);
641-
642-
/*
643-
* Trim the category name to remove blank spaces, then
644-
* grab the first 3 letters of the string, and uppercase them
645-
*/
646-
$prefix = strtoupper(substr(trim($this->category->name), 0, intval($prefixLength)));
647-
648-
/*
649-
* We'll make sure the prefix length is greater than zero before we try and
650-
* generate an SKU
651-
*/
652-
if (strlen($prefix) > 0) {
653-
/*
654-
* Create the numerical code by the items ID
655-
* to accompany the prefix and pad left zeros
656-
*/
657-
$code = str_pad($this->getKey(), $codeLength, '0', STR_PAD_LEFT);
658-
659-
/*
660-
* Process the generation
661-
*/
662-
return $this->processSkuGeneration($this->getKey(), $prefix.$skuSeparator.$code);
663-
}
664-
665-
/*
666-
* Always return false on generation failure
667-
*/
668-
return false;
669-
}
670-
671-
/**
672-
* Regenerates the current items SKU by
673-
* deleting its current SKU and creating
674-
* another. This will also generate an SKU
675-
* if one does not exist.
676-
*
677-
* @return bool|mixed
678-
*/
679-
public function regenerateSku()
680-
{
681-
$sku = $this->sku()->first();
682-
683-
if ($sku) {
684-
/*
685-
* Capture current SKU
686-
*/
687-
$previousSku = $sku;
688-
689-
/*
690-
* Delete current SKU
691-
*/
692-
$sku->delete();
693-
694-
/*
695-
* Try to generate a new SKU
696-
*/
697-
$newSku = $this->generateSku();
698-
699-
/*
700-
* New sku generation successful, return it
701-
*/
702-
if ($newSku) {
703-
return $newSku;
704-
}
705-
706-
/*
707-
* Failed generating a new sku, we'll restore the old one
708-
*/
709-
return $this->processSkuGeneration($this->getKey(), $previousSku->code);
710-
}
711-
712-
/*
713-
* Always generate an SKU if one doesn't exist
714-
*/
715-
return $this->generateSku();
716-
}
717-
718-
/**
719-
* Creates an SKU with the specified code. If overwrite is true,
720-
* the current items SKU will be deleted if it exists before creating
721-
* then SKU. If overwrite is false but the item has an SKU, an exception
722-
* is thrown.
723-
*
724-
* @param string $code
725-
* @param bool $overwrite
726-
*
727-
* @throws SkuAlreadyExistsException
728-
*
729-
* @return mixed|bool
730-
*/
731-
public function createSku($code, $overwrite = false)
732-
{
733-
/*
734-
* Get the current SKU record
735-
*/
736-
$sku = $this->sku()->first();
737-
738-
if ($sku) {
739-
/*
740-
* The dev doesn't want the SKU overridden,
741-
* we'll thrown an exception
742-
*/
743-
if (!$overwrite) {
744-
$message = Lang::get('inventory::exceptions.SkuAlreadyExistsException');
745-
746-
throw new SkuAlreadyExistsException($message);
747-
}
748-
749-
/*
750-
* Overwrite is true, lets update the current SKU
751-
*/
752-
return $this->updateSku($code, $sku);
753-
}
754-
755-
/*
756-
* No SKU exists, lets create one
757-
*/
758-
return $this->processSkuGeneration($this->getKey(), $code);
759-
}
760-
761-
/**
762-
* Updates the items current SKU or the SKU
763-
* supplied with the specified code.
764-
*
765-
* @param string $code
766-
* @param null $sku
767-
*
768-
* @return mixed|bool
769-
*/
770-
public function updateSku($code, $sku = null)
771-
{
772-
/*
773-
* Get the current SKU record if one isn't supplied
774-
*/
775-
if (!$sku) {
776-
$sku = $this->sku()->first();
777-
}
778-
779-
/*
780-
* If an SKU still doesn't exist after
781-
* trying to find one, we'll create one
782-
*/
783-
if (!$sku) {
784-
return $this->processSkuGeneration($this->getKey(), $code);
785-
}
786-
787-
return $this->processSkuUpdate($sku, $code);
559+
return;
788560
}
789561

790562
/**
@@ -976,42 +748,6 @@ private function resolveSupplier($supplier) {
976748
return $s;
977749
}
978750

979-
/**
980-
* Processes an SKU generation covered by database transactions.
981-
*
982-
* @param int|string $inventoryId
983-
* @param string $code
984-
*
985-
* @return bool|mixed
986-
*/
987-
private function processSkuGeneration($inventoryId, $code)
988-
{
989-
$this->dbStartTransaction();
990-
991-
try {
992-
$insert = [
993-
'inventory_id' => $inventoryId,
994-
'code' => $code,
995-
];
996-
997-
$record = $this->sku()->create($insert);
998-
999-
if ($record) {
1000-
$this->dbCommitTransaction();
1001-
1002-
$this->fireEvent('inventory.sku.generated', [
1003-
'item' => $this,
1004-
]);
1005-
1006-
return $record;
1007-
}
1008-
} catch (\Exception $e) {
1009-
$this->dbRollbackTransaction();
1010-
}
1011-
1012-
return false;
1013-
}
1014-
1015751
/**
1016752
* Processes updating the specified SKU
1017753
* record with the specified code.

0 commit comments

Comments
 (0)