Source of file DiscountFactory.php
Size: 3,140 Bytes - Last Modified: 2021-12-23T10:24:29+00:00
/var/www/docs.ssmods.com/process/src/src/DiscountFactory.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 | <?php namespace SilverCommerce\Discounts; use DateTime; use SilverStripe\ORM\DB; use SilverStripe\SiteConfig\SiteConfig; use SilverStripe\Subsites\Model\Subsite; use SilverStripe\Core\Config\Configurable; use SilverStripe\Core\Injector\Injectable; use SilverStripe\ORM\FieldType\DBDatetime; use SilverCommerce\Discounts\Model\Discount; /** * Simple factroy to handle getting discounts (either by code or valid) */ class DiscountFactory { use Injectable; use Configurable; /** * Find a discount by the prodvided code. * * @param string $code A discount code to find * @param boolean $only_valid Only find valid codes */ public function getByIdent($ident, $only_valid = true) { $siteconfig = SiteConfig::current_site_config(); $discount = Discount::get()->filter( [ "Code" => $ident, 'SiteID' => $siteconfig->ID ] )->first(); // Check if this discount is valid if ($discount && $only_valid) { // Set the current date to now using DBDateTime // for unit testing support $now = new DateTime( DBDatetime::now()->format(DBDatetime::ISO_DATETIME) ); $starts = new DateTime($discount->Starts); $expires = new DateTime($discount->Expires); // If in the future, invalid if ($now > $expires) { $discount = null; } // If in the past, invalid if ($now < $starts) { $discount = null; } } return $discount; } /** * Get a list of discounts that are valid (not expired and have passed their * start date). * * @return SSList */ public static function getValid() { $config = SiteConfig::current_site_config(); $list = $config->Discounts(); $db = DB::get_conn(); // Set the current date to now using DBDateTime // for unit testing support $start = new DateTime( DBDatetime::now()->format(DBDatetime::ISO_DATETIME) ); $format = "%Y-%m-%d"; $start_field = $db->formattedDatetimeClause( '"Discount"."Starts"', $format ); $end_field = $db->formattedDatetimeClause( '"Discount"."Expires"', $format ); $now = $start->format("Y-m-d"); $list = $list->where( [ $start_field . ' <= ?' => $now, $end_field . ' >= ?' => $now ] ); return $list; } public function generateAppliedDiscount($code, $estimate) { $discount = $this->getByIdent($code); if (!$discount) { } $discount->applyDiscount($estimate, $code); } /** * Get a list of valid discounts as an array * * @return array */ public static function getValidArray() { return self::getValid()->toArray(); } } |