Skip to content

[5.4] Added a Fulfill Outstanding Committed Items function to Order #3909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: 5.4
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/elements/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
use craft\commerce\base\StoreTrait;
use craft\commerce\behaviors\CurrencyAttributeBehavior;
use craft\commerce\behaviors\CustomerBehavior;
use craft\commerce\collections\InventoryMovementCollection;
use craft\commerce\db\Table;
use craft\commerce\elements\traits\OrderElementTrait;
use craft\commerce\elements\traits\OrderNoticesTrait;
use craft\commerce\elements\traits\OrderValidatorsTrait;
use craft\commerce\enums\InventoryTransactionType;
use craft\commerce\errors\CurrencyException;
use craft\commerce\errors\OrderStatusException;
use craft\commerce\events\AddLineItemEvent;
Expand All @@ -33,6 +35,7 @@
use craft\commerce\events\OrderNoticeEvent;
use craft\commerce\helpers\Currency;
use craft\commerce\helpers\Order as OrderHelper;
use craft\commerce\models\inventory\InventoryFulfillMovement;
use craft\commerce\models\LineItem;
use craft\commerce\models\OrderAdjustment;
use craft\commerce\models\OrderHistory;
Expand Down Expand Up @@ -1848,6 +1851,45 @@ public function markAsComplete(): bool
return true;
}

/**
* Fulfills any items that are not filed for this order.
*
* @return bool
* @throws DeprecationException
* @throws InvalidConfigException
* @throws \yii\db\Exception
* @since 5.4.0
*/
public function fulfillOutstandingCommittedQuantity(): bool
{
$levels = Plugin::getInstance()->getInventory()->getInventoryFulfillmentLevels($this);
$movements = [];
foreach ($levels as $level) {
$outstanding = $level->outstandingCommittedQuantity;
if ($outstanding > 0) {
$inventoryLocation = $level->getInventoryLocation();
$movement = new InventoryFulfillMovement();
$movement->fromInventoryLocation = $inventoryLocation;
$movement->inventoryItemId = $level->inventoryItemId;
$movement->toInventoryLocation = $inventoryLocation;
$movement->fromInventoryTransactionType = InventoryTransactionType::COMMITTED;
$movement->toInventoryTransactionType = InventoryTransactionType::FULFILLED;
$movement->lineItemId = $level->lineItemId;
$movement->quantity = $outstanding;
$movement->userId = Craft::$app->getUser()->getId();
$movements[] = $movement;
}
}

$movements = InventoryMovementCollection::make($movements);

if (!Plugin::getInstance()->getInventory()->executeInventoryMovements($movements)) {
return false;
}

return true;
}

/**
* Called after the order successfully completes
*/
Expand Down
Loading