| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- <?php
- namespace ThurData\Servers\KerioEmail\App\Services\ConfigurableOptions\Abstracts;
- use ThurData\Servers\KerioEmail\App\Models\Whmcs\ProductConfigGroups;
- use ThurData\Servers\KerioEmail\App\Models\Whmcs\ProductConfigLinks;
- use ThurData\Servers\KerioEmail\App\Services\ConfigurableOptions\Models\Option;
- use ThurData\Servers\KerioEmail\App\Services\ConfigurableOptions\Models\SubOption;
- use ThurData\Servers\KerioEmail\Core\Models\Whmcs\Currency;
- use ThurData\Servers\KerioEmail\Core\Models\Whmcs\Pricing;
- use ThurData\Servers\KerioEmail\Core\Models\Whmcs\Product;
- use ThurData\Servers\KerioEmail\Core\Models\Whmcs\ProductConfigOption;
- use ThurData\Servers\KerioEmail\Core\Models\Whmcs\ProductConfigOptionSub;
- /**
- * Description of TypeConstans
- *
- * @autor ThurData <info@thrudata.ch>
- */
- abstract class AbstractConfigurableOptions
- {
- /*
- * @var integer $productID;
- */
- protected $productID;
- /*
- * @var integer $optionsGroupID;
- */
- protected $optionsGroupID;
- /*
- * @var array $options;
- */
- protected $options = [];
- /*
- * Set Product ID
- *
- * @params integer $productID
- * @return void
- */
- public function __construct($porductID)
- {
- $this->productID = $porductID;
- }
- /*
- * Create and assign configurable options group
- *
- * @return object $this;
- */
- /*
- * Get Product
- *
- * @return object \ThurData\Servers\KerioEmail\Core\Models\Whmcs\Product
- *
- */
- protected function getProduct()
- {
- return Product::where('id', $this->productID)->first();
- }
- protected function addGroup()
- {
- if (!$this->checkExistAssignedOptionsGroup())
- {
- $this->createOptionsGroup();
- $this->assignGroupToProduct();
- return true;
- }
- }
- /*
- * Check if configurable options exists
- *
- * $params integer $productId
- * @throw \Exception
- */
- public function checkExistAssignedOptionsGroup()
- {
- $check = \ThurData\Servers\KerioEmail\App\Models\Whmcs\ProductConfigLinks::where('pid', $this->productID)->first();
- if (!is_null($check))
- {
- $this->optionsGroupID = $check->gid;
- return true;
- }
- }
- /*
- * Prepare Options Array
- *
- *
- * @return array $fields
- */
- protected function showOptions()
- {
- $fields = [];
- foreach ($this->options as $option)
- {
- $fields[$option->getKey()] = $option->getName();
- }
- return $fields;
- }
- /*
- * Add options group
- *
- *
- * @throw \Exception
- */
- protected function buildOptions()
- {
- if (is_array($this->options))
- {
- foreach ($this->options as $option)
- {
- $optionID = $this->createOptions($option);
- if (!empty($option->getSubOptions()) && !empty($optionID))
- {
- $this->buildSubOptions($optionID, $option->getSubOptions());
- }
- }
- }
- }
- /*
- * Add suboptions to options group
- *
- * @params integer $optionsID
- * @params object \ThurData\Servers\KerioEmail\Core\Models\Whmcs\ProductConfigOptionSub $sobOptions
- * @throw \Exception
- */
- protected function buildSubOptions($optionID, $subOptions)
- {
- if (is_array($subOptions))
- {
- foreach ($subOptions as $option)
- {
- $this->createSubOptions($optionID, $option);
- }
- }
- }
- /*
- * Create configurable options exist
- *
- * @params string $key
- * @return mixed
- * @throw \Exception
- */
- public function checkExistOption($key)
- {
- return ProductConfigOption::where([
- ['gid', $this->optionsGroupID],
- ['optionname', 'LIKE', $key . '|%']
- ])->first();
- }
- /*
- * Create configurable options group
- *
- * $params object \ThurData\Servers\KerioEmail\Core\Models\Whmcs\Product $product
- * @throw \Exception
- */
- private function createOptionsGroup()
- {
- $product = $this->getProduct();
- $optionsGroup = new ProductConfigGroups();
- $optionsGroup->name = "Configurable options for " . $product->name . " product";
- $optionsGroup->description = "Auto generated by module " . $product->servertype;
- $optionsGroup->save();
- $this->optionsGroupID = $optionsGroup->id;
- }
- /*
- * Assign Configurable Options Group to Product
- *
- * $params object \ThurData\Servers\KerioEmail\Core\Models\Whmcs\ProductConfigGroups $optionsGroup
- * @throw \Exception
- */
- private function assignGroupToProduct()
- {
- $assignProduct = new ProductConfigLinks();
- $assignProduct->gid = $this->optionsGroupID;
- $assignProduct->pid = $this->productID;
- $assignProduct->save();
- }
- public function createOptions(Option $option)
- {
- if ($this->checkExistOption($option->getKey()))
- {
- return;
- }
- $model = new ProductConfigOption($option->setGid($this->optionsGroupID)->toArray());
- $model->save();
- return $model->id;
- }
- /*
- * Create sub-options to configurable options
- * Function automatically create pricing for configurable options
- *
- * $params integer $optionID
- * @params object \ThurData\Servers\KerioEmail\Core\Models\Whmcs\ProductConfigOptionSub $sobOption
- * @throw \Exception
- */
- public function createSubOptions($optionID, SubOption $subOption)
- {
- $model = new ProductConfigOptionSub($subOption->setConfigid($optionID)->toArray());
- $model->save();
- $this->createPricingForOptions($model->id);
- }
- /*
- * Get available currencies in WHMCS
- */
- private function getCurrencies()
- {
- $currencies = Currency::get();
- $allCurrencies = [];
- foreach ($currencies as $currency)
- {
- $allCurrencies[] = $currency->id;
- }
- return $allCurrencies;
- }
- /*
- * Get all currencies and create pricing for each one
- *
- * @params integer $subOptionID
- */
- private function createPricingForOptions($subOptionID)
- {
- foreach ($this->getCurrencies() as $currency)
- {
- $this->addPricing($subOptionID, $currency);
- }
- }
- /*
- * Add pricing for sub-options
- *
- * @params integer $subOptionID, $currencyID string $type
- * @throw \Exception
- */
- private function addPricing($subOptionID, $currencyID, $type = 'configoptions')
- {
- $pricing = new Pricing();
- $pricing->type = $type;
- $pricing->currency = $currencyID;
- $pricing->relid = $subOptionID;
- $pricing->msetupfee = 0;
- $pricing->qsetupfee = 0;
- $pricing->ssetupfee = 0;
- $pricing->asetupfee = 0;
- $pricing->bsetupfee = 0;
- $pricing->tsetupfee = 0;
- $pricing->monthly = 0;
- $pricing->quarterly = 0;
- $pricing->semiannually = 0;
- $pricing->annually = 0;
- $pricing->biennially = 0;
- $pricing->triennially = 0;
- $pricing->save();
- }
- }
|