PHP Trait Stevebauman\Inventory\Traits\InventoryTransactionTrait

Inheritance: use trait CommonMethodsTrait
Show file Open project: stevebauman/inventory

Protected Properties

Property Type Description
$beforeQuantity string Stores the quantity before an update.
$beforeState string Stores the state before an update.

Public Methods

Method Description
backOrder ( integer | float | string $quantity ) Back-orders the specified amount of quantity on the stock, if stock is sufficient enough for the quantity specified, this will throw an exception. This prevents back-orders being created when unnecessary.
bootInventoryTransactionTrait ( ) : void Overrides the models boot function to generate a new transaction history record when it is created and updated.
cancel ( string $reason = '', integer | float | string $cost ) Cancels any transaction and returns or removes stock depending on the last state.
checkout ( integer | float | string $quantity, string $reason = '', integer | float | string $cost ) Checks out the specified amount of quantity from the stock, waiting to be sold.
fillBackOrder ( string $reason = '', integer | float | string $cost ) Fills a back order by trying to remove the transaction quantity from the stock. This will return false if there was not enough stock to fill the back order, or an exception occurred.
getByState ( string $state ) : Illuminate\Database\Eloquent\Collection Returns all transactions by the specified state.
getHistory ( ) : mixed Returns the current transaction history.
getLastHistoryRecord ( ) : boolean | mixed Returns the last transaction history record.
getStockRecord ( ) : mixed Returns the current stock record attached to the current transaction.
hasStock ( ) : boolean Returns true/false depending if the current transaction is attached to a stock.
histories ( ) : Illuminate\Database\Eloquent\Relations\HasMany The hasMany histories relationship.
hold ( integer | float | string $quantity, string $reason = '', integer | float | string $cost ) Holds the specified amount of quantity until it is either used or released.
isBackOrder ( ) : boolean Returns true or false depending if the current state of the transaction is a back order.
isCancelled ( ) : boolean Returns true or false depending if the current state of the transaction is cancelled.
isCheckout ( ) : boolean Returns true or false depending if the current state of the transaction is a checkout.
isOnHold ( ) : boolean Returns true or false depending if the current state of the transaction is on-hold.
isOrder ( ) : boolean Returns true or false depending if the current state of the transaction is an order.
isOrderReceived ( ) : boolean Returns true or false depending if the current state of the transaction is a received order.
isReleased ( ) : boolean Returns true or false depending if the current state of the transaction is released inventory.
isRemoved ( ) : boolean Returns true or false depending if the current state of the transaction is removed inventory.
isReservation ( ) : boolean Returns true or false depending if the current state of the transaction is reserved.
isReturn ( ) : boolean Returns true or false depending if the current state of the transaction is a return.
isSold ( ) : boolean Returns true or false depending if the current state of the transaction is sold.
ordered ( integer | float | string $quantity ) Creates a transaction that specifies the amount of quantity that has been ordered.
postCreate ( ) Generates a transaction history record after a transaction has been created.
postUpdate ( ) Generates a transaction history record when a transaction has been updated.
received ( integer | float | string $quantity, string $reason = '', integer | float | string $cost ) Marks a transaction as received. If the previous state was ordered then the amount ordered is inserted into the stock. If a quantity is specified then the status of the transaction is set to received-partial, and then returned to ordered with the amount of quantity left to receive.
receivedAll ( string $reason = '', integer | float | string $cost ) Marks an order transaction as received, placing all the quantity from the transaction into the stock.
receivedPartial ( integer | float | string $quantity, string $reason = '', integer | float | string $cost ) Marks an order transaction as received-partial, placing the specified quantity into the stock and returning the transaction to the previous ordered state with the remaining stock to receive.
release ( integer | float | string $quantity, string $reason = '', integer | float | string $cost ) Releases held inventory and inserts it back into the stock. If a quantity is specified and it is lower than the held quantity, than the transaction state will change to released-partial and then returned to the state on-hold with the remainder of the held stock.
releaseAll ( string $reason = '', integer | float | string $cost ) Releases an on-hold inventory transaction, placing all the quantity in the transaction back into the stock.
releasePartial ( integer | float | string $quantity, string $reason = '', integer | float | string $cost ) Releases a partial amount of the specified quantity from the transaction and returns it to the previous state.
remove ( integer | float | string $quantity, string $reason = '', integer | float | string $cost ) Removes the specified quantity from the stock for the current transaction.
removeAll ( ) Permanently removes all of the transaction quantity from the stock. Since the stock was already removed with the on-hold method, the removed state is an 'end of the line' state, and cannot be recovered or reversed.
removePartial ( integer | float | string $quantity, string $reason = '', integer | float | string $cost ) Removes a partial amount of quantity from the inventory. If the transactions previous state was on-hold, no inventory will be removed since the stock was already taken. If the previous state is null or opened, then it will remove the specified quantity from the stock.
reserved ( integer | float | string $quantity, boolean $backOrder = false, string $reason = '', integer | float | string $cost ) Reserves the specified amount of quantity for a reservation for commerce.
returned ( integer | float | string $quantity, string $reason = '', integer | float | string $cost ) Returns the specified amount of quantity back into the stock. A previous state is required to successfully insert the quantity back into the stock, for example, if the stock was sold, or was in checkout, the returned method could be called and the quantity that was sold or was in checkout would be inserted back into the stock. If a quantity is specified and it is less than the amount that was sold/checked-out, then the specified amount is inserted back into the stock and the transaction is reverted to its previous state with the leftover amount.
returnedAll ( string $reason = '', integer | float | string $cost ) Marks a transaction as returned and places the stock that was taken back into the inventory.
returnedPartial ( integer | float | string $quantity, string $reason = '', integer | float | string $cost ) Marks a transaction as partially returned and returns the specified quantity back into the stock. If the transaction quantity is greater or equal to the specified quantity then a full return is processed.
setQuantityAttribute ( integer | float | string $quantity ) Validates the quantity attribute when it has been set.
setStateAttribute ( string $state ) Verifies if the state being set is valid.
sold ( integer | float | string $quantity, string $reason = '', integer | float | string $cost ) Marks and removes the specified amount of quantity sold from the stock.
soldAmount ( integer | float | string $quantity, string $reason = '', integer | float | string $cost ) Marks a new or open transaction as sold and removes the amount of the specified quantity from from the inventory stock.
stock ( ) : BelongsTo The belongsTo stock relationship.

Protected Methods

Method Description
checkoutFromReserved ( ) Changes the state of the current transaction to checkout. This will not take any stock from the inventory since a reservation already does this.
generateTransactionHistory ( string $stateBefore, string $stateAfter, integer | float | string $quantityBefore, integer | float | string $quantityAfter ) : boolean | Model Processes generating a transaction history entry.
getAvailableStates ( ) : array Returns an array of available states.
getTransactionReason ( string $key ) : string Returns a default transaction reason from the specified key in the reasons lang file.
processSave ( string $event = '' ) Processes saving the transaction by covering it with a database transaction.
processStockPutAndSave ( integer | float | string $quantity, string $event = '', string $reason = '', integer | float | string $cost ) Processes putting the specified quantity into the current transaction stock and saving the current transaction.
processStockTakeAndSave ( integer | float | string $quantity, string $event = '', string $reason = '', integer | float | string $cost ) Processes removing the specified quantity from transaction stock and saving the current transaction.
reservedFromCheckout ( ) Changes the state of the current transaction to reserved. This will not take any stock from the inventory since a checkout already does this.
returnToPreviousState ( string $previousState ) Returns a transaction to its previous specified state when a returned partial is called. This is to allow a transaction to continue functioning normally since only a partial amount of the transaction was returned, therefore it is still open.
validatePreviousState ( array $allowedStates = [], string $toState ) : boolean Returns true if the current state equals at least one of the allowed states in the array. Throws an exception otherwise.
validateStateIsAvailable ( string $state ) : boolean Returns true if the specified state is valid, throws an exception otherwise.

Method Details

backOrder() public method

Back-orders the specified amount of quantity on the stock, if stock is sufficient enough for the quantity specified, this will throw an exception. This prevents back-orders being created when unnecessary.
public backOrder ( integer | float | string $quantity )
$quantity integer | float | string

bootInventoryTransactionTrait() public static method

Overrides the models boot function to generate a new transaction history record when it is created and updated.
public static bootInventoryTransactionTrait ( ) : void
return void

cancel() public method

Transactions with states of opened, checkout, reserved, back ordered, ordered-pending, and inventory on hold CAN be cancelled Transactions with states such as sold, returned, order-received, and inventory released CAN NOT be cancelled.
public cancel ( string $reason = '', integer | float | string $cost )
$reason string
$cost integer | float | string

checkout() public method

Checks out the specified amount of quantity from the stock, waiting to be sold.
public checkout ( integer | float | string $quantity, string $reason = '', integer | float | string $cost )
$quantity integer | float | string
$reason string
$cost integer | float | string

checkoutFromReserved() protected method

Changes the state of the current transaction to checkout. This will not take any stock from the inventory since a reservation already does this.
protected checkoutFromReserved ( )

fillBackOrder() public method

Fills a back order by trying to remove the transaction quantity from the stock. This will return false if there was not enough stock to fill the back order, or an exception occurred.
public fillBackOrder ( string $reason = '', integer | float | string $cost )
$reason string
$cost integer | float | string

generateTransactionHistory() protected method

Processes generating a transaction history entry.
protected generateTransactionHistory ( string $stateBefore, string $stateAfter, integer | float | string $quantityBefore, integer | float | string $quantityAfter ) : boolean | Model
$stateBefore string
$stateAfter string
$quantityBefore integer | float | string
$quantityAfter integer | float | string
return boolean | Illuminate\Database\Eloquent\Model

getAvailableStates() protected method

Returns an array of available states.
protected getAvailableStates ( ) : array
return array

getByState() public static method

Returns all transactions by the specified state.
public static getByState ( string $state ) : Illuminate\Database\Eloquent\Collection
$state string
return Illuminate\Database\Eloquent\Collection

getHistory() public method

Returns the current transaction history.
public getHistory ( ) : mixed
return mixed

getLastHistoryRecord() public method

Returns the last transaction history record.
public getLastHistoryRecord ( ) : boolean | mixed
return boolean | mixed

getStockRecord() public method

Returns the current stock record attached to the current transaction.
public getStockRecord ( ) : mixed
return mixed

getTransactionReason() protected method

Returns a default transaction reason from the specified key in the reasons lang file.
protected getTransactionReason ( string $key ) : string
$key string
return string

hasStock() public method

Returns true/false depending if the current transaction is attached to a stock.
public hasStock ( ) : boolean
return boolean

histories() abstract public method

The hasMany histories relationship.
abstract public histories ( ) : Illuminate\Database\Eloquent\Relations\HasMany
return Illuminate\Database\Eloquent\Relations\HasMany

hold() public method

Holds the specified amount of quantity until it is either used or released.
public hold ( integer | float | string $quantity, string $reason = '', integer | float | string $cost )
$quantity integer | float | string
$reason string
$cost integer | float | string

isBackOrder() public method

Returns true or false depending if the current state of the transaction is a back order.
public isBackOrder ( ) : boolean
return boolean

isCancelled() public method

Returns true or false depending if the current state of the transaction is cancelled.
public isCancelled ( ) : boolean
return boolean

isCheckout() public method

Returns true or false depending if the current state of the transaction is a checkout.
public isCheckout ( ) : boolean
return boolean

isOnHold() public method

Returns true or false depending if the current state of the transaction is on-hold.
public isOnHold ( ) : boolean
return boolean

isOrder() public method

Returns true or false depending if the current state of the transaction is an order.
public isOrder ( ) : boolean
return boolean

isOrderReceived() public method

Returns true or false depending if the current state of the transaction is a received order.
public isOrderReceived ( ) : boolean
return boolean

isReleased() public method

Returns true or false depending if the current state of the transaction is released inventory.
public isReleased ( ) : boolean
return boolean

isRemoved() public method

Returns true or false depending if the current state of the transaction is removed inventory.
public isRemoved ( ) : boolean
return boolean

isReservation() public method

Returns true or false depending if the current state of the transaction is reserved.
public isReservation ( ) : boolean
return boolean

isReturn() public method

Returns true or false depending if the current state of the transaction is a return.
public isReturn ( ) : boolean
return boolean

isSold() public method

Returns true or false depending if the current state of the transaction is sold.
public isSold ( ) : boolean
return boolean

ordered() public method

The received or cancel method must be used after this is performed.
public ordered ( integer | float | string $quantity )
$quantity integer | float | string

postCreate() public method

Generates a transaction history record after a transaction has been created.
public postCreate ( )

postUpdate() public method

Generates a transaction history record when a transaction has been updated.
public postUpdate ( )

processSave() protected method

Processes saving the transaction by covering it with a database transaction.
protected processSave ( string $event = '' )
$event string

processStockPutAndSave() protected method

Processes putting the specified quantity into the current transaction stock and saving the current transaction.
protected processStockPutAndSave ( integer | float | string $quantity, string $event = '', string $reason = '', integer | float | string $cost )
$quantity integer | float | string
$event string
$reason string
$cost integer | float | string

processStockTakeAndSave() protected method

Processes removing the specified quantity from transaction stock and saving the current transaction.
protected processStockTakeAndSave ( integer | float | string $quantity, string $event = '', string $reason = '', integer | float | string $cost )
$quantity integer | float | string
$event string
$reason string
$cost integer | float | string

received() public method

Marks a transaction as received. If the previous state was ordered then the amount ordered is inserted into the stock. If a quantity is specified then the status of the transaction is set to received-partial, and then returned to ordered with the amount of quantity left to receive.
public received ( integer | float | string $quantity, string $reason = '', integer | float | string $cost )
$quantity integer | float | string
$reason string
$cost integer | float | string

receivedAll() public method

Marks an order transaction as received, placing all the quantity from the transaction into the stock.
public receivedAll ( string $reason = '', integer | float | string $cost )
$reason string
$cost integer | float | string

receivedPartial() public method

If the quantity specified is greater or equal to the amount ordered, this will mark the transaction as received all and place the quantity of the transaction into the stock.
public receivedPartial ( integer | float | string $quantity, string $reason = '', integer | float | string $cost )
$quantity integer | float | string
$reason string
$cost integer | float | string

release() public method

Releases held inventory and inserts it back into the stock. If a quantity is specified and it is lower than the held quantity, than the transaction state will change to released-partial and then returned to the state on-hold with the remainder of the held stock.
public release ( integer | float | string $quantity, string $reason = '', integer | float | string $cost )
$quantity integer | float | string
$reason string
$cost integer | float | string

releaseAll() public method

Releases an on-hold inventory transaction, placing all the quantity in the transaction back into the stock.
public releaseAll ( string $reason = '', integer | float | string $cost )
$reason string
$cost integer | float | string

releasePartial() public method

Releases a partial amount of the specified quantity from the transaction and returns it to the previous state.
public releasePartial ( integer | float | string $quantity, string $reason = '', integer | float | string $cost )
$quantity integer | float | string
$reason string
$cost integer | float | string

remove() public method

If the transaction state is current on-hold, and a quantity is given then a partial-remove will be triggered and the remaining quantity will be on-hold. If no quantity is given, then this will set the transaction state to removed and the stock will be permanently removed from the current stock. If the transaction state was open or null, and a quantity is given, then the specified quantity is permanently removed from the stock.
public remove ( integer | float | string $quantity, string $reason = '', integer | float | string $cost )
$quantity integer | float | string
$reason string
$cost integer | float | string

removeAll() public method

Permanently removes all of the transaction quantity from the stock. Since the stock was already removed with the on-hold method, the removed state is an 'end of the line' state, and cannot be recovered or reversed.
public removeAll ( )

removePartial() public method

Removes a partial amount of quantity from the inventory. If the transactions previous state was on-hold, no inventory will be removed since the stock was already taken. If the previous state is null or opened, then it will remove the specified quantity from the stock.
public removePartial ( integer | float | string $quantity, string $reason = '', integer | float | string $cost )
$quantity integer | float | string
$reason string
$cost integer | float | string

reserved() public method

If backOrder is true then the state will be set to back-order if the specified quantity is unavailable to be reserved. Otherwise it will throw an exception. If reserved is called from being checked out we'll make sure we don't take any inventory.
public reserved ( integer | float | string $quantity, boolean $backOrder = false, string $reason = '', integer | float | string $cost )
$quantity integer | float | string
$backOrder boolean
$reason string
$cost integer | float | string

reservedFromCheckout() protected method

Changes the state of the current transaction to reserved. This will not take any stock from the inventory since a checkout already does this.
protected reservedFromCheckout ( )

returnToPreviousState() protected method

Returns a transaction to its previous specified state when a returned partial is called. This is to allow a transaction to continue functioning normally since only a partial amount of the transaction was returned, therefore it is still open.
protected returnToPreviousState ( string $previousState )
$previousState string

returned() public method

Returns the specified amount of quantity back into the stock. A previous state is required to successfully insert the quantity back into the stock, for example, if the stock was sold, or was in checkout, the returned method could be called and the quantity that was sold or was in checkout would be inserted back into the stock. If a quantity is specified and it is less than the amount that was sold/checked-out, then the specified amount is inserted back into the stock and the transaction is reverted to its previous state with the leftover amount.
public returned ( integer | float | string $quantity, string $reason = '', integer | float | string $cost )
$quantity integer | float | string
$reason string
$cost integer | float | string

returnedAll() public method

Marks a transaction as returned and places the stock that was taken back into the inventory.
public returnedAll ( string $reason = '', integer | float | string $cost )
$reason string
$cost integer | float | string

returnedPartial() public method

Marks a transaction as partially returned and returns the specified quantity back into the stock. If the transaction quantity is greater or equal to the specified quantity then a full return is processed.
public returnedPartial ( integer | float | string $quantity, string $reason = '', integer | float | string $cost )
$quantity integer | float | string
$reason string
$cost integer | float | string

setQuantityAttribute() public method

Validates the quantity attribute when it has been set.
public setQuantityAttribute ( integer | float | string $quantity )
$quantity integer | float | string

setStateAttribute() public method

Verifies if the state being set is valid.
public setStateAttribute ( string $state )
$state string

sold() public method

If no quantity is specified and the previous state was not in checkout, reserved, back ordered, returned or returned partial, this will throw an exception.
public sold ( integer | float | string $quantity, string $reason = '', integer | float | string $cost )
$quantity integer | float | string
$reason string
$cost integer | float | string

soldAmount() public method

Marks a new or open transaction as sold and removes the amount of the specified quantity from from the inventory stock.
public soldAmount ( integer | float | string $quantity, string $reason = '', integer | float | string $cost )
$quantity integer | float | string
$reason string
$cost integer | float | string

stock() abstract public method

The belongsTo stock relationship.
abstract public stock ( ) : BelongsTo
return Illuminate\Database\Eloquent\Relations\BelongsTo

validatePreviousState() protected method

Returns true if the current state equals at least one of the allowed states in the array. Throws an exception otherwise.
protected validatePreviousState ( array $allowedStates = [], string $toState ) : boolean
$allowedStates array
$toState string
return boolean

validateStateIsAvailable() protected method

Returns true if the specified state is valid, throws an exception otherwise.
protected validateStateIsAvailable ( string $state ) : boolean
$state string
return boolean

Property Details

$beforeQuantity protected property

Stores the quantity before an update.
protected string $beforeQuantity
return string

$beforeState protected property

Stores the state before an update.
protected string $beforeState
return string