<?php
include $_SERVER['DOCUMENT_ROOT'] . "/standalone.php";
if (isset($_GET['id'])) {
$id = $_GET['id'];
function getDiscountPrice($price, $percent) {
$discount = $price * $percent / 100;
return $price - $discount;
}
// Получаем объекты
$get = umiObjectsCollection::getInstance();
$object = $get->getObject($id);
$percent = $object->getValue('sale_percent');
$dateStart = $object->getValue('sale_start');
$dateEnd = $object->getValue('sale_end');
if (isset($percent) && isset($dateStart) && isset($dateEnd)) {
$price = $object->getValue('price');
$priceOld = $object->getValue('old_price');
$dateNow = new DateTime();
$dateNow = $dateNow->format('Y-m-d');
$needDiscount = ($dateNow >= $dateStart && $dateNow <= $dateEnd) ? true : false;
if ($needDiscount) { // Ставим скидку
$newPrice = getDiscountPrice($price, $percent);
if ($priceOld == 0) {
$object->setValue('price', $newPrice);
$object->setValue('old_price', $price);
$object->commit();
} else if ($priceOld == $price) {
$object->setValue('price', $newPrice);
$object->commit();
}
} else { // убираем скидку
if ($priceOld != 0) { // если есть старая цена
$object->setValue('price', $priceOld);
$object->setValue('old_price', 0);
$object->commit();
}
}
}
}