silvercommerce /
discounts
| 1 | <?php |
||
| 2 | |||
| 3 | namespace SilverCommerce\Discounts; |
||
| 4 | |||
| 5 | use DateTime; |
||
| 6 | use SilverStripe\ORM\DB; |
||
| 7 | use SilverStripe\SiteConfig\SiteConfig; |
||
| 8 | use SilverStripe\Subsites\Model\Subsite; |
||
|
0 ignored issues
–
show
|
|||
| 9 | use SilverStripe\Core\Config\Configurable; |
||
| 10 | use SilverStripe\Core\Injector\Injectable; |
||
| 11 | use SilverStripe\ORM\FieldType\DBDatetime; |
||
| 12 | use SilverCommerce\Discounts\Model\Discount; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Simple factroy to handle getting discounts (either by code or valid) |
||
| 16 | */ |
||
| 17 | class DiscountFactory |
||
| 18 | { |
||
| 19 | |||
| 20 | use Injectable; |
||
| 21 | |||
| 22 | use Configurable; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Find a discount by the prodvided code. |
||
| 26 | * |
||
| 27 | * @param string $code A discount code to find |
||
| 28 | * @param boolean $only_valid Only find valid codes |
||
| 29 | */ |
||
| 30 | public function getByIdent($ident, $only_valid = true) |
||
| 31 | { |
||
| 32 | $siteconfig = SiteConfig::current_site_config(); |
||
| 33 | |||
| 34 | $discount = Discount::get()->filter( |
||
| 35 | [ |
||
| 36 | "Code" => $ident, |
||
| 37 | 'SiteID' => $siteconfig->ID |
||
| 38 | ] |
||
| 39 | )->first(); |
||
| 40 | |||
| 41 | // Check if this discount is valid |
||
| 42 | if ($discount && $only_valid) { |
||
| 43 | // Set the current date to now using DBDateTime |
||
| 44 | // for unit testing support |
||
| 45 | $now = new DateTime( |
||
| 46 | DBDatetime::now()->format(DBDatetime::ISO_DATETIME) |
||
| 47 | ); |
||
| 48 | |||
| 49 | $starts = new DateTime($discount->Starts); |
||
| 50 | $expires = new DateTime($discount->Expires); |
||
| 51 | |||
| 52 | // If in the future, invalid |
||
| 53 | if ($now > $expires) { |
||
| 54 | $discount = null; |
||
| 55 | } |
||
| 56 | |||
| 57 | // If in the past, invalid |
||
| 58 | if ($now < $starts) { |
||
| 59 | $discount = null; |
||
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | return $discount; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Get a list of discounts that are valid (not expired and have passed their |
||
| 68 | * start date). |
||
| 69 | * |
||
| 70 | * @return SSList |
||
|
0 ignored issues
–
show
The type
SilverCommerce\Discounts\SSList was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 71 | */ |
||
| 72 | public static function getValid() |
||
| 73 | { |
||
| 74 | $config = SiteConfig::current_site_config(); |
||
| 75 | $list = $config->Discounts(); |
||
| 76 | $db = DB::get_conn(); |
||
| 77 | // Set the current date to now using DBDateTime |
||
| 78 | // for unit testing support |
||
| 79 | $start = new DateTime( |
||
| 80 | DBDatetime::now()->format(DBDatetime::ISO_DATETIME) |
||
| 81 | ); |
||
| 82 | $format = "%Y-%m-%d"; |
||
| 83 | |||
| 84 | $start_field = $db->formattedDatetimeClause( |
||
| 85 | '"Discount"."Starts"', |
||
| 86 | $format |
||
| 87 | ); |
||
| 88 | $end_field = $db->formattedDatetimeClause( |
||
| 89 | '"Discount"."Expires"', |
||
| 90 | $format |
||
| 91 | ); |
||
| 92 | |||
| 93 | $now = $start->format("Y-m-d"); |
||
| 94 | $list = $list->where( |
||
| 95 | [ |
||
| 96 | $start_field . ' <= ?' => $now, |
||
| 97 | $end_field . ' >= ?' => $now |
||
| 98 | ] |
||
| 99 | ); |
||
| 100 | |||
| 101 | return $list; |
||
| 102 | } |
||
| 103 | |||
| 104 | public function generateAppliedDiscount($code, $estimate) |
||
| 105 | { |
||
| 106 | $discount = $this->getByIdent($code); |
||
| 107 | |||
| 108 | if (!$discount) { |
||
| 109 | } |
||
| 110 | |||
| 111 | $discount->applyDiscount($estimate, $code); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Get a list of valid discounts as an array |
||
| 116 | * |
||
| 117 | * @return array |
||
| 118 | */ |
||
| 119 | public static function getValidArray() |
||
| 120 | { |
||
| 121 | return self::getValid()->toArray(); |
||
| 122 | } |
||
| 123 | } |
||
| 124 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths