Source of file PostageExtension.php
Size: 3,839 Bytes - Last Modified: 2021-12-23T10:24:37+00:00
/var/www/docs.ssmods.com/process/src/src/Extensions/PostageExtension.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 | <?php namespace SilverCommerce\Postage\Extensions; use SilverStripe\Forms\FieldList; use SilverStripe\ORM\DataExtension; use SilverStripe\SiteConfig\SiteConfig; use SilverCommerce\TaxAdmin\Model\TaxRate; use SilverStripe\Forms\ReadonlyField; use SilverCommerce\Postage\Helpers\PostageOption; /** * Add extra postage options to a DataObject (for example and Invoice or * Estimate) * * This function will attempt to autocalculate tax for an extended object, * and will add look for the following extension hooks to update the prices: * * - `updateTaxTotal` (to add the postage tax to the current tax value) * - `updateTotal` (to add the current postage price to the total value) * */ class PostageExtension extends DataExtension { private static $db = [ "PostageTitle" => "Varchar", "PostagePrice" => "Currency" ]; private static $has_one = [ "PostageTax" => TaxRate::class ]; private static $casting = [ "PostageDetails" => "Varchar", "PostageTaxPrice" => "Currency", "PostageTotal" => "Currency" ]; /** * Set the postage settings on this object based on the provided * PostageOption. * * @return self */ public function setPostage(PostageOption $postage) { $this->owner->PostageTitle = $postage->getName(); $this->owner->PostagePrice = $postage->getPrice(); $this->owner->PostageTax = $postage->getTax(); return $this->owner; } /** * Generate a PostageOption based on this object's details * * @return PostageOption */ public function getPostage() { return PostageOption::create( $this->owner->PostageTitle, $this->owner->PostagePrice, $this->owner->PostageTax ); } /** * Remove all postage settings from this object * * @return self */ public function clearPostage() { $this->owner->PostageTitle = ""; $this->owner->PostagePrice = 0; $this->owner->PostageTaxID = null; } /** * Generate a string outlining the details of selected postage * * @return string */ public function getPostageDetails() { return $this->owner->PostageTitle . " (" . $this->owner->obj("PostageTotal")->Nice() . ")"; } /** * Get the total value of postage (including tax) * * @return float */ public function getPostageTotal() { $price = $this->owner->PostagePrice; return $price + $this->owner->PostageTaxPrice; } /** * Get the total value of postage (including tax) * * @return float */ public function getPostageTaxPrice() { $price = $this->owner->PostagePrice; $tax = $this->owner->PostageTax(); $rate = ($tax->exists()) ? $tax->Rate : 0; return ($price / 100 * $rate); } /** * Add the current postage tax top the total tax * on this object */ public function updateTaxTotal(&$total) { $total = $total + $this->owner->PostageTaxPrice; } /** * Attempt to add the postage price to current totals * */ public function updateTotal(&$total) { $total = $total + $this->owner->PostagePrice; } public function updateCMSFields(FieldList $fields) { $config = SiteConfig::current_site_config(); // Move postage selection fields $title = $fields->dataFieldByName("PostageTitle"); $price = $fields->dataFieldByName("PostagePrice"); $tax = $fields->dataFieldByName("PostageTaxID"); $fields->addFieldsToTab( "Root.Delivery", [ $title, $price, $tax ] ); } } |