AbstractConfigurableOptions.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\App\Services\ConfigurableOptions\Abstracts;
  3. use ThurData\Servers\KerioEmail\App\Models\Whmcs\ProductConfigGroups;
  4. use ThurData\Servers\KerioEmail\App\Models\Whmcs\ProductConfigLinks;
  5. use ThurData\Servers\KerioEmail\App\Services\ConfigurableOptions\Models\Option;
  6. use ThurData\Servers\KerioEmail\App\Services\ConfigurableOptions\Models\SubOption;
  7. use ThurData\Servers\KerioEmail\Core\Models\Whmcs\Currency;
  8. use ThurData\Servers\KerioEmail\Core\Models\Whmcs\Pricing;
  9. use ThurData\Servers\KerioEmail\Core\Models\Whmcs\Product;
  10. use ThurData\Servers\KerioEmail\Core\Models\Whmcs\ProductConfigOption;
  11. use ThurData\Servers\KerioEmail\Core\Models\Whmcs\ProductConfigOptionSub;
  12. /**
  13. * Description of TypeConstans
  14. *
  15. * @autor ThurData <info@thrudata.ch>
  16. */
  17. abstract class AbstractConfigurableOptions
  18. {
  19. /*
  20. * @var integer $productID;
  21. */
  22. protected $productID;
  23. /*
  24. * @var integer $optionsGroupID;
  25. */
  26. protected $optionsGroupID;
  27. /*
  28. * @var array $options;
  29. */
  30. protected $options = [];
  31. /*
  32. * Set Product ID
  33. *
  34. * @params integer $productID
  35. * @return void
  36. */
  37. public function __construct($porductID)
  38. {
  39. $this->productID = $porductID;
  40. }
  41. /*
  42. * Create and assign configurable options group
  43. *
  44. * @return object $this;
  45. */
  46. /*
  47. * Get Product
  48. *
  49. * @return object \ThurData\Servers\KerioEmail\Core\Models\Whmcs\Product
  50. *
  51. */
  52. protected function getProduct()
  53. {
  54. return Product::where('id', $this->productID)->first();
  55. }
  56. protected function addGroup()
  57. {
  58. if (!$this->checkExistAssignedOptionsGroup())
  59. {
  60. $this->createOptionsGroup();
  61. $this->assignGroupToProduct();
  62. return true;
  63. }
  64. }
  65. /*
  66. * Check if configurable options exists
  67. *
  68. * $params integer $productId
  69. * @throw \Exception
  70. */
  71. public function checkExistAssignedOptionsGroup()
  72. {
  73. $check = \ThurData\Servers\KerioEmail\App\Models\Whmcs\ProductConfigLinks::where('pid', $this->productID)->first();
  74. if (!is_null($check))
  75. {
  76. $this->optionsGroupID = $check->gid;
  77. return true;
  78. }
  79. }
  80. /*
  81. * Prepare Options Array
  82. *
  83. *
  84. * @return array $fields
  85. */
  86. protected function showOptions()
  87. {
  88. $fields = [];
  89. foreach ($this->options as $option)
  90. {
  91. $fields[$option->getKey()] = $option->getName();
  92. }
  93. return $fields;
  94. }
  95. /*
  96. * Add options group
  97. *
  98. *
  99. * @throw \Exception
  100. */
  101. protected function buildOptions()
  102. {
  103. if (is_array($this->options))
  104. {
  105. foreach ($this->options as $option)
  106. {
  107. $optionID = $this->createOptions($option);
  108. if (!empty($option->getSubOptions()) && !empty($optionID))
  109. {
  110. $this->buildSubOptions($optionID, $option->getSubOptions());
  111. }
  112. }
  113. }
  114. }
  115. /*
  116. * Add suboptions to options group
  117. *
  118. * @params integer $optionsID
  119. * @params object \ThurData\Servers\KerioEmail\Core\Models\Whmcs\ProductConfigOptionSub $sobOptions
  120. * @throw \Exception
  121. */
  122. protected function buildSubOptions($optionID, $subOptions)
  123. {
  124. if (is_array($subOptions))
  125. {
  126. foreach ($subOptions as $option)
  127. {
  128. $this->createSubOptions($optionID, $option);
  129. }
  130. }
  131. }
  132. /*
  133. * Create configurable options exist
  134. *
  135. * @params string $key
  136. * @return mixed
  137. * @throw \Exception
  138. */
  139. public function checkExistOption($key)
  140. {
  141. return ProductConfigOption::where([
  142. ['gid', $this->optionsGroupID],
  143. ['optionname', 'LIKE', $key . '|%']
  144. ])->first();
  145. }
  146. /*
  147. * Create configurable options group
  148. *
  149. * $params object \ThurData\Servers\KerioEmail\Core\Models\Whmcs\Product $product
  150. * @throw \Exception
  151. */
  152. private function createOptionsGroup()
  153. {
  154. $product = $this->getProduct();
  155. $optionsGroup = new ProductConfigGroups();
  156. $optionsGroup->name = "Configurable options for " . $product->name . " product";
  157. $optionsGroup->description = "Auto generated by module " . $product->servertype;
  158. $optionsGroup->save();
  159. $this->optionsGroupID = $optionsGroup->id;
  160. }
  161. /*
  162. * Assign Configurable Options Group to Product
  163. *
  164. * $params object \ThurData\Servers\KerioEmail\Core\Models\Whmcs\ProductConfigGroups $optionsGroup
  165. * @throw \Exception
  166. */
  167. private function assignGroupToProduct()
  168. {
  169. $assignProduct = new ProductConfigLinks();
  170. $assignProduct->gid = $this->optionsGroupID;
  171. $assignProduct->pid = $this->productID;
  172. $assignProduct->save();
  173. }
  174. public function createOptions(Option $option)
  175. {
  176. if ($this->checkExistOption($option->getKey()))
  177. {
  178. return;
  179. }
  180. $model = new ProductConfigOption($option->setGid($this->optionsGroupID)->toArray());
  181. $model->save();
  182. return $model->id;
  183. }
  184. /*
  185. * Create sub-options to configurable options
  186. * Function automatically create pricing for configurable options
  187. *
  188. * $params integer $optionID
  189. * @params object \ThurData\Servers\KerioEmail\Core\Models\Whmcs\ProductConfigOptionSub $sobOption
  190. * @throw \Exception
  191. */
  192. public function createSubOptions($optionID, SubOption $subOption)
  193. {
  194. $model = new ProductConfigOptionSub($subOption->setConfigid($optionID)->toArray());
  195. $model->save();
  196. $this->createPricingForOptions($model->id);
  197. }
  198. /*
  199. * Get available currencies in WHMCS
  200. */
  201. private function getCurrencies()
  202. {
  203. $currencies = Currency::get();
  204. $allCurrencies = [];
  205. foreach ($currencies as $currency)
  206. {
  207. $allCurrencies[] = $currency->id;
  208. }
  209. return $allCurrencies;
  210. }
  211. /*
  212. * Get all currencies and create pricing for each one
  213. *
  214. * @params integer $subOptionID
  215. */
  216. private function createPricingForOptions($subOptionID)
  217. {
  218. foreach ($this->getCurrencies() as $currency)
  219. {
  220. $this->addPricing($subOptionID, $currency);
  221. }
  222. }
  223. /*
  224. * Add pricing for sub-options
  225. *
  226. * @params integer $subOptionID, $currencyID string $type
  227. * @throw \Exception
  228. */
  229. private function addPricing($subOptionID, $currencyID, $type = 'configoptions')
  230. {
  231. $pricing = new Pricing();
  232. $pricing->type = $type;
  233. $pricing->currency = $currencyID;
  234. $pricing->relid = $subOptionID;
  235. $pricing->msetupfee = 0;
  236. $pricing->qsetupfee = 0;
  237. $pricing->ssetupfee = 0;
  238. $pricing->asetupfee = 0;
  239. $pricing->bsetupfee = 0;
  240. $pricing->tsetupfee = 0;
  241. $pricing->monthly = 0;
  242. $pricing->quarterly = 0;
  243. $pricing->semiannually = 0;
  244. $pricing->annually = 0;
  245. $pricing->biennially = 0;
  246. $pricing->triennially = 0;
  247. $pricing->save();
  248. }
  249. }