Source of file GSTTaxModifierOptions.php
Size: 8,438 Bytes - Last Modified: 2021-12-23T10:41:22+00:00
/var/www/docs.ssmods.com/process/src/src/Model/GSTTaxModifierOptions.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 | <?php namespace Sunnysideup\EcommerceTax\Model; use SilverStripe\Control\Controller; use SilverStripe\Core\Config\Config; use SilverStripe\Forms\DropdownField; use SilverStripe\Forms\ReadonlyField; use SilverStripe\ORM\DatabaseAdmin; use SilverStripe\ORM\DataObject; use SilverStripe\ORM\FieldType\DBField; use SilverStripe\Security\Permission; use SilverStripe\Security\Security; use Sunnysideup\Ecommerce\Config\EcommerceConfig; use Sunnysideup\Ecommerce\Model\Address\EcommerceCountry; use Sunnysideup\Ecommerce\Model\Extensions\EcommerceRole; /** *@author nicolaas [at] sunnysideup.co.nz */ class GSTTaxModifierOptions extends DataObject { /** * standard SS variable. * * @var string */ private static $table_name = 'GSTTaxModifierOptions'; private static $db = [ 'CountryCode' => 'Varchar(3)', 'Code' => 'Varchar(12)', 'Name' => 'Varchar(175)', 'LegalNotice' => 'Varchar(255)', 'InclusiveOrExclusive' => "Enum('Inclusive,Exclusive', 'Inclusive')", 'Rate' => 'Double', 'DoesNotApplyToAllProducts' => 'Boolean', 'AppliesToAllCountries' => 'Boolean', ]; /** * standard SS variable. * * @var array */ private static $defaults = [ 'InclusiveOrExclusive' => 'Inclusive', ]; /** * standard SS variable. * * @var array */ private static $casting = [ 'CountryName' => 'Varchar', 'PercentageNice' => 'Varchar', ]; /** * standard SS variable. * * @var array */ private static $indexes = [ 'Code' => true, ]; /** * standard SS variable. * * @var array */ private static $searchable_fields = [ 'CountryCode' => 'PartialMatchFilter', 'Code' => 'PartialMatchFilter', 'Name' => 'PartialMatchFilter', ]; /** * standard SS variable. * * @var array */ private static $field_labels = [ 'CountryName' => 'Country Name', 'CountryCode' => 'Country Code', 'Code' => 'Code for tax', 'Name' => 'Name for tax', 'InclusiveOrExclusive' => 'Inclusive/Exclusive', 'LegalNotice' => 'Here you can put your GST number or VAT registration number', 'Rate' => 'Rate (e.g. 0.125 = 12.5%)', 'PercentageNice' => 'Percentage', 'DoesNotApplyToAllProducts' => 'Added to individual products only', ]; /** * standard SS variable. * * @var array */ private static $summary_fields = [ 'CountryName' => 'Country', 'Code' => 'Code', 'Name' => 'Title', 'InclusiveOrExclusive' => 'Type', 'PercentageNice' => 'Percentage', ]; /** * standard SS variable. * * @var string */ private static $singular_name = 'Tax Option'; /** * standard SS variable. * * @var string */ private static $plural_name = 'Tax Options'; public function i18n_singular_name() { return _t('GSTTaxModifierOptions.TAXOPTION', 'Tax Option'); } public function i18n_plural_name() { return _t('GSTTaxModifierOptions.TAXOPTIONS', 'Tax Options'); } /** * standard SS method. * * @param null|mixed $member * @param mixed $context * * @return bool */ public function canCreate($member = null, $context = []) { $extended = $this->extendedCan(__FUNCTION__, $member); if (null !== $extended) { return $extended; } if (Permission::checkMember($member, Config::inst()->get(EcommerceRole::class, 'admin_permission_code'))) { return true; } return parent::canCreate($member); } /** * standard SS method. * * @param null|mixed $member * @param mixed $context * * @return bool */ public function canView($member = null, $context = []) { $extended = $this->extendedCan(__FUNCTION__, $member); if (null !== $extended) { return $extended; } if (Permission::checkMember($member, Config::inst()->get(EcommerceRole::class, 'admin_permission_code'))) { return true; } return parent::canCreate($member); } /** * standard SS method. * * @param null|mixed $member * @param mixed $context * * @return bool */ public function canEdit($member = null, $context = []) { $extended = $this->extendedCan(__FUNCTION__, $member); if (null !== $extended) { return $extended; } if (Permission::checkMember($member, Config::inst()->get(EcommerceRole::class, 'admin_permission_code'))) { return true; } return parent::canEdit($member); } /** * standard SS method. * * @param null|mixed $member * * @return bool */ public function canDelete($member = null) { $extended = $this->extendedCan(__FUNCTION__, $member); if (null !== $extended) { return $extended; } if (Permission::checkMember($member, Config::inst()->get(EcommerceRole::class, 'admin_permission_code'))) { return true; } return parent::canDelete($member); } /** * standard SS method. * * @return \SilverStripe\Forms\FieldList */ public function getCMSFields() { $fields = parent::getCMSFields(); $fieldLabels = $this->Config()->get('field_labels'); $fields->replaceField('CountryCode', new DropdownField('CountryCode', $fieldLabels['CountryCode'], EcommerceCountry::get_country_dropdown())); $InclusiveOrExclusive = 'Inclusive'; if (EcommerceConfig::inst()->ShopPricesAreTaxExclusive) { $InclusiveOrExclusive = 'Exclusive'; } $fields->replaceField( 'InclusiveOrExclusive', ReadonlyField::create( 'InclusiveOrExclusive', 'This tax is: ' . $InclusiveOrExclusive . ', you can change this setting in the e-commerce configuration.' ) ); return $fields; } public function Title() { return $this->getTitle(); } public function getTitle() { if ($this->AppliesToAllCountries) { $country = _t('GSTTExModifierOption.WORLDWIDE', 'world-wide'); } else { $country = $this->CountryCode; } return $this->Name . " ({$country}, " . number_format($this->Rate * 100, 2) . '%)'; } /** * standard SS method. */ public function populateDefaults() { //can only run after first dev/build if (Security::database_is_ready()) { $controller = Controller::curr(); if ($controller instanceof DatabaseAdmin) { //cant do this now. } else { if (EcommerceConfig::inst()->ShopPricesAreTaxExclusive) { $this->InclusiveOrExclusive = 'Exclusive'; } else { $this->InclusiveOrExclusive = 'Inclusive'; } } } return parent::populateDefaults(); } /** * standard SS method. */ public function onBeforeWrite() { parent::onBeforeWrite(); if (EcommerceConfig::inst()->ShopPricesAreTaxExclusive) { $this->InclusiveOrExclusive = 'Exclusive'; } else { $this->InclusiveOrExclusive = 'Inclusive'; } } // /** // * standard SS method // */ // public function requireDefaultRecords() // { // parent::requireDefaultRecords(); // DB::query(" // UPDATE \"GSTTaxModifierOptions\" // SET \"InclusiveOrExclusive\" = 'Inclusive' // WHERE \"InclusiveOrExclusive\" <> 'Exclusive'" // ); // } public function CountryName() { return $this->getCountryName(); } public function getCountryName() { return EcommerceCountry::find_title($this->CountryCode); } public function PercentageNice() { return $this->getPercentageNice(); } public function getPercentageNice() { return DBField::create_field('Text', ($this->Rate * 100) . '%'); } } |