Source of file CountryPrice_CopyPrices.php
Size: 6,347 Bytes - Last Modified: 2021-12-23T10:40:38+00:00
/var/www/docs.ssmods.com/process/src/code/model/buyables/CountryPrice_CopyPrices.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 | <?php /** * a class to copy prices from country A to country * This can be added to Products, but also to Product Groups * and other pages ... * * * */ class CountryPrice_CopyPrices extends DataExtension { private static $db = array( "AllowCopying" => "Boolean" ); public function updateCMSFields(FieldList $fields) { if ($this->owner->ID) { $page = is_a($this->owner, 'SiteTree'); // We use singleton to skip the different is_a php versions issues $tab = 'Root.Countries.Pricing'; $fromCountries = CountryPrice_EcommerceCountry::get_real_countries_list(); //->where('CountryPrice.ObjectClass = \''.$this->owner->ClassName.'\' AND CountryPrice.ObjectID = '.$this->owner->ID.'') if ($fromCountries && $fromCountries->count()) { $fromCountriesArray = $fromCountries->map('Code', 'Name')->toArray(); } else { $fromCountriesArray = array(); } $allCountries = EcommerceCountry::get(); $toCountries = array(); foreach ($allCountries as $country) { $country = CountryPrice_EcommerceCountry::get_real_country($country); $toCountries[$country->Code] = $country->Name . ($country->DoNotAllowSales ? ' (Sales not allowed)' : ''); } $countryCurrencies = CountryPrice_EcommerceCurrency::get_currency_per_country(); $link = CountryPrice_CopyPrices_Controller::get_link($this->owner); $fields->addFieldToTab( $tab, $allowCopyingField = new CheckboxField("AllowCopying", "Allow copying") ); $allowCopyingField->setRightTitle("Turn this on only when you like to copy a bunch of prices. Otherwise just leave it turned off to avoid accidental copies and speed up the CMS loading times."); if (count($fromCountriesArray) && count($toCountries) && $this->owner->AllowCopying) { $fields->addFieldsToTab($tab, array( new HeaderField('Copy Prices'), new DropdownField('From', 'From', $fromCountriesArray), new CheckboxSetField('To', 'To', $toCountries), new HiddenField('CountryCurrencies', '', Convert::array2json($countryCurrencies)), new EcommerceCMSButtonField( 'UpdatePriceLink', $link, 'Copy Prices' ) )); } } } /** * update all child buyables and the current buyable prices * based on $fromCountry and applied to ALL $toCountries * @param string $fromCountryCode the country code to copy from * @param array $toCountriesArray the country code to copy to */ public function updatePrices($fromCountryCode, array $toCountriesArray) { $fromCountryObject = EcommerceCountry::get()->filter(array("Code" => $fromCountryCode)); if ($fromCountryObject) { $fromCountryObject = CountryPrice_EcommerceCountry::get_real_country($fromCountryObject); } else { user_error('From Country is not valid'); } $currencyObject = $fromCountryObject->EcommerceCurrency(); if ($currencyObject && $currencyObject->Code) { $values = $this->getUpdatePriceValues($fromCountryCodeA, $currencyObject->Code, array()); foreach ($toCountriesArray as $toCountryCode) { $toCountryCode = CountryPrice_EcommerceCountry::get_real_country($toCountryCode)->Code; foreach ($values as $value) { $sqlValues[] = "(NOW(),NOW(),{$value[0]},'$toCountryCode','".$currencyObject->Code."','{$value[1]}',{$value[2]})"; } } if (isset($sqlValues)) { $sqlValues = implode(',', $sqlValues); $sql = " INSERT INTO \"CountryPrice\" (\"Created\",\"LastEdited\",\"Price\",\"Country\",\"Currency\",\"ObjectClass\",\"ObjectID\") VALUES $sqlValues ON DUPLICATE KEY UPDATE \"LastEdited\" = VALUES(\"LastEdited\"), \"Price\" = VALUES(\"Price\")"; DB::query($sql); } } } /** * returns an array of * - Price * - ClassName * - ID * searches through children, until all all childpages have been added * * @param string $country [description] * @param array $values [description] * @return array [description] */ public function getUpdatePriceValues($fromCountryCode, $currencyCode, array $values) { $fromCountryCode = CountryPrice_EcommerceCountry::get_real_country($fromCountryCode)->Code; if ($this->owner->hasExtension('CountryPrice_BuyableExtension')) { $countryPrice = $this->owner->CountryPriceForCountryAndCurrency($fromCountry, $currency); if ($countryPrice) { $price = $countryPrice->First()->Price; } else { $countryPrices = $this->owner->CountryPriceForCountryAndCurrency($fromCountryCode, $currencyCode, $values); if ($countryPrices && $countryPrices->count()) { $price = $countryPrices->First()->Price; } } if (isset($price)) { $values[] = array( $price, $this->owner->ClassName, $this->owner->ID ); } } if ($this->owner->hasExtension('ProductWithVariationDecorator')) { $variations = $this->owner->Variations(); foreach ($variations as $variation) { $values = array_merge($values, $variation->getUpdatePriceValues($fromCountryCode, $currencyCode, $values)); } } if (is_a($this->owner, 'SiteTree')) { $pages = $this->owner->AllChildren(); if ($pages) { foreach ($pages as $page) { $values = array_merge($values, $page->getUpdatePriceValues($fromCountryCode, $currencyCode, $values)); } } } return $values; } } |