/** * Save widget value in entity * * @param array $field Field data * * @return void */ public function saveValueShippingId($field) { $shippingId = $field['widget']->getValue(); if ($shippingId !== \XLite\View\FormField\Select\ShipMethod::KEY_DELETED && $shippingId !== \XLite\View\FormField\Select\ShipMethod::KEY_UNAVAILABLE) { $shippingId = (int) $shippingId; $shippingMethod = $shippingId ? \XLite\Core\Database::getRepo('XLite\\Model\\Shipping\\Method')->find($shippingId) : null; if ((int) $this->getEntity()->getShippingId() !== $shippingId) { $newName = $shippingMethod ? $shippingMethod->getName() : 'None'; $oldName = $this->getEntity()->getShippingMethodName(); \XLite\Controller\Admin\Order::setOrderChanges($this->getParam(static::PARAM_FIELD_NAME), sprintf('%s (id: %d)', $newName, $shippingId), sprintf('%s (id: %d)', $oldName, $this->getEntity()->getShippingId())); } $this->getEntity()->setShippingId($shippingId); $this->getEntity()->setShippingMethodName($shippingMethod ? $shippingMethod->getName() : null); } }
/** * Save widget value in entity * * @param array $field Field data * * @return void */ public function saveValuePaymentMethod($field) { $paymentId = $field['widget']->getValue(); $serviceCodes = array(\XLite\View\FormField\Select\PaymentMethod::KEY_DELETED, \XLite\View\FormField\Select\PaymentMethod::KEY_NONE); if (!in_array($paymentId, $serviceCodes)) { $entity = $this->getEntity(); $oldMethodId = $entity->getPaymentMethod() ? $entity->getPaymentMethod()->getMethodId() : null; $oldValue = sprintf('%s (id: %d)', $entity->getPaymentMethod() ? $entity->getPaymentMethod()->getName() : static::t('None'), $entity->getPaymentMethod() ? $oldMethodId : 'n/a'); if (!$entity->isPersistent()) { $entity->setOrder($this->getOrder()); $this->getOrder()->addPaymentTransactions($entity); } $paymentMethod = $this->preprocessValueBeforeSave($paymentId); $entity->setPaymentMethod($paymentMethod); if ($entity->getPaymentMethod() && $entity->getPaymentMethod()->getMethodId() != $oldMethodId) { \XLite\Controller\Admin\Order::setOrderChanges($this->getParam(static::PARAM_FIELD_NAME), sprintf('%s (id: %d)', $entity->getPaymentMethod() ? $entity->getPaymentMethod()->getName() : 'n/a', $entity->getPaymentMethod() ? $entity->getPaymentMethod()->getMethodId() : 'n/a'), $oldValue); } } }
/** * Remove entity * * @param \XLite\Model\AEntity $entity Entity * * @return boolean */ protected function removeEntity(\XLite\Model\AEntity $entity) { $product = $entity->getProduct(); if ($this->isNeedUpdateStock() && !$entity->isDeleted()) { // Return items to stock $entity->changeAmount($entity->getAmount()); } $attributes = $this->getAttributeValuesAsString($entity); \XLite\Controller\Admin\Order::setOrderChanges('Removed items:' . $entity->getItemId(), sprintf('[%s] %s (%s x %d%s)', $product->getSku(), $product->getName(), static::formatPrice($entity->getPrice(), $entity->getOrder()->getCurrency(), true), $entity->getAmount(), $attributes ? ', ' . $attributes : '')); return parent::removeEntity($entity); }
/** * Save field value to entity * * @param array $field Field * @param mixed $value Value * * @return void */ protected function saveFieldEntityValue(array $field, $value) { $data = \XLite\Core\Request::getInstance()->getPostData(); $currency = $this->getEntity()->getOrder()->getCurrency(); $isPersistent = $this->getEntity()->isPersistent(); if ($this->getEntity()->getModifier() && !empty($data['auto']['surcharges'][$this->getEntity()->getCode()]['value'])) { if (\XLite\Logic\Order\Modifier\Shipping::MODIFIER_CODE === $this->getEntity()->getCode()) { // Reset selected rate to avoid cache $this->getEntity()->getModifier()->resetSelectedRate(); } // Calculate surcharge and get new surcharge object or array of surcharge objects $surcharges = $this->getEntity()->getModifier()->canApply() ? $this->getEntity()->getModifier()->calculate() : array(); if (!is_array($surcharges)) { $surcharges = $surcharges ? array($surcharges) : array(); } $value = 0; foreach ($surcharges as $surcharge) { if (is_object($surcharge)) { if ($surcharge->getCode() === $this->getEntity()->getCode()) { $value += $surcharge->getValue(); } // Remove added surcharges if current entity exists in DB to avoid duplicates if ($isPersistent) { \XLite\Core\Database::getEM()->remove($surcharge); $surcharge->getOrder()->removeSurcharge($surcharge); } } } } elseif (0 != $value && !$isPersistent) { $this->addOrderSurcharge($this->getEntity(), $value); } if (0 < $value && $this->getEntity()->getType() === \XLite\Model\Base\Surcharge::TYPE_DISCOUNT) { $value = $value * -1; } $oldValue = $currency->roundValue($this->getEntity()->getValue()); $newValue = $currency->roundValue($value); if ($oldValue !== $newValue) { \XLite\Controller\Admin\Order::setOrderChanges($this->getParam(static::PARAM_FIELD_NAME), static::formatPrice(abs($value), $currency, true), static::formatPrice(abs($this->getEntity()->getValue()), $currency, true)); } $this->getEntity()->setValue($value); }
/** * Save field value to entity * * @param array $field Field * @param mixed $value Value * * @return void */ protected function saveFieldEntityValue(array $field, $value) { $isPersistent = $this->getEntity()->isPersistent(); if ($this->getEntity()->getModifier()) { $surcharges = $this->getEntity()->getModifier()->calculate(); if (!is_array($surcharges)) { $surcharges = array($surcharges); } $value = 0; foreach ($surcharges as $surcharge) { if (is_object($surcharge)) { $value += $surcharge->getValue(); if ($isPersistent) { \XLite\Core\Database::getEM()->remove($surcharge); $surcharge->getOrder()->removeSurcharge($surcharge); } } } } if (0 < $value && $this->getEntity()->getType() === \XLite\Model\Base\Surcharge::TYPE_DISCOUNT) { $value = $value * -1; } $oldValue = $this->getEntity()->getOrder()->getCurrency()->roundValue($this->getEntity()->getValue()); $newValue = $this->getEntity()->getOrder()->getCurrency()->roundValue($value); if ($oldValue !== $newValue) { \XLite\Controller\Admin\Order::setOrderChanges($this->getParam(static::PARAM_FIELD_NAME), static::formatPrice(abs($value), $this->getEntity()->getOrder()->getCurrency(), true), static::formatPrice(abs($this->getEntity()->getValue()), $this->getEntity()->getOrder()->getCurrency(), true)); } $this->getEntity()->setValue($value); }
/** * Save field value to entity * * @param array $field Field * @param mixed $value Value * * @return void */ protected function saveFieldEntityValue(array $field, $value) { if ($this->getEntityValue() != $value) { \XLite\Controller\Admin\Order::setOrderChanges($this->getParam(static::PARAM_FIELD_NAME), $value, $this->getEntityValue()); } parent::saveFieldEntityValue($field, $value); }
/** * Save field value to entity * * @param array $field Field * @param mixed $value Value * * @return void */ protected function saveFieldEntityValue(array $field, $value) { $address = $this->getAddressModel(); if ($address) { // Get property old value $getterMethod = 'get' . \XLite\Core\Converter::convertToCamelCase($field['field'][static::FIELD_NAME]); if (method_exists($address, $getterMethod)) { // Set address property via specific method $oldValue = $address->{$getterMethod}(); } else { // Set address property via common setterProperty() method $oldValue = $address->getterProperty($field['field'][static::FIELD_NAME], $value); } // Set address property $setterMethod = $this->getAddressFieldMethodName($field); if (method_exists($address, $setterMethod)) { // Set address property via specific method $address->{$setterMethod}($value); } else { // Set address property via common setterProperty() method $address->setterProperty($field['field'][static::FIELD_NAME], $value); } if ($value != $oldValue) { // Prepare data to register as an order changes $ignoreChange = false; $fieldName = $field['field'][static::FIELD_NAME]; switch ($fieldName) { case 'country_code': $fieldName = 'Country'; $oldCountry = \XLite\Core\Database::getRepo('XLite\\Model\\Country')->findOneBy(array('code' => $oldValue)); $oldValue = $oldCountry ? $oldCountry->getCountry() : $oldValue; $newCountry = \XLite\Core\Database::getRepo('XLite\\Model\\Country')->findOneBy(array('code' => $value)); $value = $newCountry ? $newCountry->getCountry() : $value; break; case 'state_id': if ($address->getCountry() && $address->getCountry()->hasStates()) { $fieldName = 'State'; $oldState = \XLite\Core\Database::getRepo('XLite\\Model\\State')->find($oldValue); $oldValue = $oldState ? $oldState->getState() : $oldValue; $newState = \XLite\Core\Database::getRepo('XLite\\Model\\State')->find($value); $value = $newState ? $newState->getState() : $value; } else { $ignoreChange = true; } break; case 'custom_state': if ($address->getCountry() && $address->getCountry()->hasStates()) { $ignoreChange = true; } else { $fieldName = 'State'; } break; } if (!$ignoreChange) { \XLite\Controller\Admin\Order::setOrderChanges($this->getParam(static::PARAM_FIELD_NAME) . ':' . $fieldName, $value, $oldValue); } } } }