silvercommerce /
discounts
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace SilverCommerce\Discounts\Model; |
||||
| 4 | |||||
| 5 | use SilverStripe\Core\Convert; |
||||
| 6 | use SilverStripe\ORM\DataObject; |
||||
| 7 | use SilverStripe\Security\Group; |
||||
| 8 | use SilverStripe\Security\Security; |
||||
| 9 | use SilverStripe\Control\Controller; |
||||
| 10 | use SilverStripe\Forms\ReadonlyField; |
||||
| 11 | use SilverStripe\Security\Permission; |
||||
| 12 | use SilverStripe\SiteConfig\SiteConfig; |
||||
| 13 | use SilverStripe\Security\PermissionProvider; |
||||
| 14 | use SilverCommerce\OrdersAdmin\Model\Estimate; |
||||
| 15 | use SilverCommerce\TaxAdmin\Helpers\MathsHelper; |
||||
| 16 | use SilverCommerce\Discounts\Model\AppliedDiscount; |
||||
| 17 | use SilverCommerce\CatalogueAdmin\Model\CatalogueCategory; |
||||
| 18 | |||||
| 19 | class Discount extends DataObject implements PermissionProvider |
||||
| 20 | { |
||||
| 21 | private static $table_name = 'Discount'; |
||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
| 22 | |||||
| 23 | private static $db = [ |
||||
|
0 ignored issues
–
show
|
|||||
| 24 | "Title" => "Varchar", |
||||
| 25 | "Code" => "Varchar(99)", |
||||
| 26 | "MinOrder" => "Decimal", |
||||
| 27 | "Starts" => "Date", |
||||
| 28 | "Expires" => "Date" |
||||
| 29 | ]; |
||||
| 30 | |||||
| 31 | private static $has_one = [ |
||||
|
0 ignored issues
–
show
|
|||||
| 32 | "Site" => SiteConfig::class |
||||
| 33 | ]; |
||||
| 34 | |||||
| 35 | private static $many_many = [ |
||||
|
0 ignored issues
–
show
|
|||||
| 36 | "Groups" => Group::class, |
||||
| 37 | "Categories" => CatalogueCategory::class |
||||
| 38 | ]; |
||||
| 39 | |||||
| 40 | private static $casting = [ |
||||
|
0 ignored issues
–
show
|
|||||
| 41 | 'I18nType' |
||||
| 42 | ]; |
||||
| 43 | |||||
| 44 | private static $summary_fields = [ |
||||
|
0 ignored issues
–
show
|
|||||
| 45 | 'I18nType', |
||||
| 46 | "Title", |
||||
| 47 | "Code", |
||||
| 48 | "Starts", |
||||
| 49 | "Expires" |
||||
| 50 | ]; |
||||
| 51 | |||||
| 52 | private static $field_labels = [ |
||||
|
0 ignored issues
–
show
|
|||||
| 53 | 'I18nType' => 'Type' |
||||
| 54 | ]; |
||||
| 55 | |||||
| 56 | public function getCMSFields() |
||||
| 57 | { |
||||
| 58 | $self = $this; |
||||
| 59 | |||||
| 60 | $this->beforeUpdateCMSFields( |
||||
| 61 | function ($fields) use ($self) { |
||||
|
0 ignored issues
–
show
|
|||||
| 62 | // Add i18n type field to field list |
||||
| 63 | $fields->addFieldToTab( |
||||
| 64 | 'Root.Main', |
||||
| 65 | ReadonlyField::create('I18nType', $this->fieldLabel('I18nType')), |
||||
| 66 | 'Title' |
||||
| 67 | ); |
||||
| 68 | |||||
| 69 | $min = $fields->dataFieldByName("MinOrder"); |
||||
| 70 | |||||
| 71 | if ($min) { |
||||
| 72 | $min->setDescription( |
||||
| 73 | _t(self::class.".MinOrderPreTax", "This is the SubTotal of an order EXCLUDING vat and tax") |
||||
| 74 | ); |
||||
| 75 | } |
||||
| 76 | } |
||||
| 77 | ); |
||||
| 78 | |||||
| 79 | return parent::getCMSFields(); |
||||
| 80 | } |
||||
| 81 | |||||
| 82 | /** |
||||
| 83 | * Generate a translated type of this discount (based on it's classname) |
||||
| 84 | * |
||||
| 85 | * @return string |
||||
| 86 | */ |
||||
| 87 | public function getI18nType() |
||||
| 88 | { |
||||
| 89 | return $this->i18n_singular_name(); |
||||
| 90 | } |
||||
| 91 | |||||
| 92 | /** |
||||
| 93 | * calculate the price reduction for this discount |
||||
| 94 | * |
||||
| 95 | * @param Currency $value - the total/sub-total of the items this discount applies to. |
||||
|
0 ignored issues
–
show
The type
SilverCommerce\Discounts\Model\Currency 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...
|
|||||
| 96 | * @return int |
||||
| 97 | */ |
||||
| 98 | public function calculateAmount(Estimate $estimate) |
||||
| 99 | { |
||||
| 100 | return 0; |
||||
| 101 | } |
||||
| 102 | |||||
| 103 | /** |
||||
| 104 | * calculate the value of a discount using an AppliedDiscount item. |
||||
| 105 | * |
||||
| 106 | * @param AppliedDiscount $item |
||||
| 107 | * @return float |
||||
| 108 | */ |
||||
| 109 | public function appliedAmount(AppliedDiscount $item) |
||||
|
0 ignored issues
–
show
The parameter
$item is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 110 | { |
||||
| 111 | return 0; |
||||
| 112 | } |
||||
| 113 | |||||
| 114 | public function applyDiscount($estimate, $code = null) |
||||
|
0 ignored issues
–
show
The parameter
$code is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 115 | { |
||||
| 116 | $applied = AppliedDiscount::create(); |
||||
| 117 | $applied->Code = $this->Code; |
||||
| 118 | $applied->Title = $this->Title; |
||||
| 119 | $applied->Value = $this->calculateAmount($estimate); |
||||
| 120 | $applied->EstimateID = $estimate->ID; |
||||
| 121 | |||||
| 122 | $applied->write(); |
||||
| 123 | |||||
| 124 | $estimate->Discounts()->add($applied); |
||||
| 125 | } |
||||
| 126 | |||||
| 127 | /** |
||||
| 128 | * Generate a random string that we can use for the code by default |
||||
| 129 | * |
||||
| 130 | * @return string |
||||
| 131 | */ |
||||
| 132 | protected static function generateRandomString($length = 10) |
||||
| 133 | { |
||||
| 134 | $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
||||
| 135 | $string = ''; |
||||
| 136 | |||||
| 137 | for ($i = 0; $i < $length; $i++) { |
||||
| 138 | $string .= $characters[rand(0, strlen($characters) - 1)]; |
||||
| 139 | } |
||||
| 140 | |||||
| 141 | return $string; |
||||
| 142 | } |
||||
| 143 | |||||
| 144 | /** |
||||
| 145 | * Set more complex default data |
||||
| 146 | */ |
||||
| 147 | public function populateDefaults() |
||||
| 148 | { |
||||
| 149 | $this->setField('Code', self::generateRandomString()); |
||||
| 150 | } |
||||
| 151 | |||||
| 152 | public function onBeforeWrite() |
||||
| 153 | { |
||||
| 154 | parent::onBeforeWrite(); |
||||
| 155 | |||||
| 156 | // Ensure that the code is URL safe |
||||
| 157 | $this->Code = Convert::raw2url($this->Code); |
||||
|
0 ignored issues
–
show
|
|||||
| 158 | } |
||||
| 159 | |||||
| 160 | public function canView($member = null, $context = []) |
||||
|
0 ignored issues
–
show
The parameter
$context is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 161 | { |
||||
| 162 | $extended = $this->extendedCan('canView', $member); |
||||
| 163 | if ($extended !== null) { |
||||
| 164 | return $extended; |
||||
| 165 | } |
||||
| 166 | |||||
| 167 | return true; |
||||
| 168 | } |
||||
| 169 | |||||
| 170 | public function canCreate($member = null, $context = []) |
||||
| 171 | { |
||||
| 172 | $extended = $this->extendedCan('canCreate', $member); |
||||
| 173 | if ($extended !== null) { |
||||
| 174 | return $extended; |
||||
| 175 | } |
||||
| 176 | |||||
| 177 | $permissions = ["ADMIN", "DISCOUNTS_CREATE"]; |
||||
| 178 | |||||
| 179 | if (!$member) { |
||||
| 180 | $member = Security::getCurrentUser(); |
||||
| 181 | } |
||||
| 182 | |||||
| 183 | if ($member && Permission::checkMember($member->ID, $permissions)) { |
||||
| 184 | return true; |
||||
| 185 | } |
||||
| 186 | |||||
| 187 | return false; |
||||
| 188 | } |
||||
| 189 | |||||
| 190 | public function canEdit($member = null, $context = []) |
||||
|
0 ignored issues
–
show
The parameter
$context is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 191 | { |
||||
| 192 | $extended = $this->extendedCan('canEdit', $member); |
||||
| 193 | if ($extended !== null) { |
||||
| 194 | return $extended; |
||||
| 195 | } |
||||
| 196 | |||||
| 197 | $permissions = ["ADMIN", "DISCOUNTS_EDIT"]; |
||||
| 198 | |||||
| 199 | if (!$member) { |
||||
| 200 | $member = Security::getCurrentUser(); |
||||
| 201 | } |
||||
| 202 | |||||
| 203 | if ($member && Permission::checkMember($member->ID, $permissions)) { |
||||
| 204 | return true; |
||||
| 205 | } |
||||
| 206 | |||||
| 207 | return false; |
||||
| 208 | } |
||||
| 209 | |||||
| 210 | public function canDelete($member = null, $context = []) |
||||
|
0 ignored issues
–
show
The parameter
$context is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 211 | { |
||||
| 212 | $extended = $this->extendedCan('canDelete', $member); |
||||
| 213 | if ($extended !== null) { |
||||
| 214 | return $extended; |
||||
| 215 | } |
||||
| 216 | |||||
| 217 | $permissions = ["ADMIN", "DISCOUNTS_DELETE"]; |
||||
| 218 | |||||
| 219 | if (!$member) { |
||||
| 220 | $member = Security::getCurrentUser(); |
||||
| 221 | } |
||||
| 222 | |||||
| 223 | if ($member && Permission::checkMember($member->ID, $permissions)) { |
||||
| 224 | return true; |
||||
| 225 | } |
||||
| 226 | |||||
| 227 | return false; |
||||
| 228 | } |
||||
| 229 | |||||
| 230 | |||||
| 231 | |||||
| 232 | public function providePermissions() |
||||
| 233 | { |
||||
| 234 | return [ |
||||
| 235 | "DISCOUNTS_CREATE" => [ |
||||
| 236 | 'name' => 'Create Discounts', |
||||
| 237 | 'help' => 'Allow user to create discounts', |
||||
| 238 | 'category' => 'Discounts', |
||||
| 239 | 'sort' => 88 |
||||
| 240 | ], |
||||
| 241 | "DISCOUNTS_EDIT" => [ |
||||
| 242 | 'name' => 'Edit Discounts', |
||||
| 243 | 'help' => 'Allow user to edit discounts', |
||||
| 244 | 'category' => 'Discounts', |
||||
| 245 | 'sort' => 87 |
||||
| 246 | ], |
||||
| 247 | "DISCOUNTS_DELETE" => [ |
||||
| 248 | 'name' => 'Delete Discounts', |
||||
| 249 | 'help' => 'Allow user to delete discounts', |
||||
| 250 | 'category' => 'Discounts', |
||||
| 251 | 'sort' => 86 |
||||
| 252 | ] |
||||
| 253 | ]; |
||||
| 254 | } |
||||
| 255 | } |
||||
| 256 |