@@ -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