View source
<?php
namespace Drupal\commerce_stock_field\Plugin\Field\FieldType;
use Drupal\commerce_stock\ContextCreatorTrait;
use Drupal\commerce_stock\StockTransactionsInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
class StockLevel extends FieldItemBase {
use ContextCreatorTrait;
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'value' => [
'type' => 'numeric',
'size' => 'normal',
'precision' => 19,
'scale' => 4,
'not null' => FALSE,
],
],
];
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('float')
->setLabel(t('Available stock'));
$properties['available_stock'] = DataDefinition::create('float')
->setLabel(t('Available stock'))
->setComputed(TRUE)
->setInternal(FALSE)
->setReadOnly(TRUE)
->setClass('Drupal\\commerce_stock_field\\StockLevelProcessor')
->setSetting('stock level', 'summary');
return $properties;
}
public function isEmpty() {
$value = $this
->get('value')
->getValue();
return $value === NULL;
}
public function setValue($values, $notify = TRUE) {
if (!is_array($values)) {
$value = filter_var($values, FILTER_VALIDATE_FLOAT);
if ($value !== FALSE) {
$values = [
'adjustment' => $value,
];
}
else {
throw new \InvalidArgumentException('Values passed to the commerce stock level field must be floats');
}
}
if (isset($values['value'])) {
$values['value'] = $values['value'];
}
elseif (isset($values['adjustment'])) {
$values['value'] = $values['adjustment'];
}
else {
$values['value'] = 0.0;
}
parent::setValue($values, $notify);
}
public function postSave($update) {
$entity = $this
->getEntity();
$values = $entity->{$this
->getFieldDefinition()
->getName()}
->getValue();
$values = reset($values);
$this
->createTransaction($entity, $values);
}
private function createTransaction(EntityInterface $entity, array $values) {
static $processed = [];
if (isset($processed[$entity
->getEntityTypeId() . $entity
->id()])) {
return;
}
$processed[$entity
->getEntityTypeId() . $entity
->id()] = TRUE;
$stockServiceManager = \Drupal::service('commerce_stock.service_manager');
$transaction_qty = empty($values['adjustment']) ? 0 : $values['adjustment'];
$transaction_qty = filter_var((double) $transaction_qty, FILTER_VALIDATE_FLOAT);
if ($transaction_qty) {
$transaction_type = $transaction_qty > 0 ? StockTransactionsInterface::STOCK_IN : StockTransactionsInterface::STOCK_OUT;
$location = $stockServiceManager
->getTransactionLocation($this
->getContext($entity), $entity, $transaction_qty);
if (empty($location)) {
throw new \RuntimeException('The StockServiceManager didn\'t return a location. Make sure your store is set up correctly?');
}
$zone = empty($values['zone']) ? '' : $values['zone'];
$unit_cost = NULL;
if (isset($values['unit_cost']['amount'])) {
$unit_cost = filter_var((double) $values['unit_cost']['amount'], FILTER_VALIDATE_FLOAT);
$unit_cost ?: NULL;
}
$currency_code = empty($values['unit_cost']['currency_code']) ? NULL : $values['unit_cost']['currency_code'];
$transaction_note = empty($values['stock_transaction_note']) ? '' : $values['stock_transaction_note'];
$metadata = [
'data' => [
'message' => $transaction_note,
],
];
if (!empty($values['user_id'])) {
$metadata['related_uid'] = $values['user_id'];
}
else {
$metadata['related_uid'] = \Drupal::currentUser()
->id();
}
$stockServiceManager
->createTransaction($entity, $location
->getId(), $zone, $transaction_qty, (double) $unit_cost, $currency_code, $transaction_type, $metadata);
}
}
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$scale = 4;
$random_decimal = mt_rand() / mt_getrandmax() * 999 * 2 - 999;
$values['value'] = floor($random_decimal * pow(10, $scale)) / pow(10, $scale);
return $values;
}
}