classes/XLite/Model/Shipping/Method.php line 20

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright (c) 2011-present Qualiteam software Ltd. All rights reserved.
  4.  * See https://www.x-cart.com/license-agreement.html for license details.
  5.  */
  6. namespace XLite\Model\Shipping;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Includes\Utils\ConfigParser;
  9. use XLite\View\FormField\Input\PriceOrPercent;
  10. /**
  11.  * Shipping method model
  12.  *
  13.  * @ORM\Entity
  14.  * @ORM\Table  (name="shipping_methods",
  15.  *      indexes={
  16.  *          @ORM\Index (name="processor", columns={"processor"}),
  17.  *          @ORM\Index (name="carrier", columns={"carrier"}),
  18.  *          @ORM\Index (name="enabled", columns={"enabled"}),
  19.  *          @ORM\Index (name="position", columns={"position"})
  20.  *      }
  21.  * )
  22.  */
  23. class Method extends \XLite\Model\Base\I18n implements \XLite\Model\Base\IModuleRelatedEntity
  24. {
  25.     /**
  26.      * A unique ID of the method
  27.      *
  28.      * @var integer
  29.      *
  30.      * @ORM\Id
  31.      * @ORM\GeneratedValue (strategy="AUTO")
  32.      * @ORM\Column         (type="integer")
  33.      */
  34.     protected $method_id;
  35.     /**
  36.      * Processor class name
  37.      *
  38.      * @var string
  39.      *
  40.      * @ORM\Column (type="string", length=255)
  41.      */
  42.     protected $processor '';
  43.     /**
  44.      * Carrier of the method (for instance, "UPS" or "USPS")
  45.      *
  46.      * @var string
  47.      *
  48.      * @ORM\Column (type="string", length=255)
  49.      */
  50.     protected $carrier '';
  51.     /**
  52.      * Unique code of shipping method (within processor space)
  53.      *
  54.      * @var string
  55.      *
  56.      * @ORM\Column (type="string", length=255)
  57.      */
  58.     protected $code '';
  59.     /**
  60.      * Whether the method is enabled or disabled
  61.      *
  62.      * @var string
  63.      *
  64.      * @ORM\Column (type="boolean")
  65.      */
  66.     protected $enabled false;
  67.     /**
  68.      * A position of the method among other registered methods
  69.      *
  70.      * @var integer
  71.      *
  72.      * @ORM\Column (type="integer")
  73.      */
  74.     protected $position 0;
  75.     /**
  76.      * Shipping rates (relation)
  77.      *
  78.      * @var \Doctrine\Common\Collections\ArrayCollection
  79.      *
  80.      * @ORM\OneToMany (targetEntity="XLite\Model\Shipping\Markup", mappedBy="shipping_method", cascade={"all"})
  81.      */
  82.     protected $shipping_markups;
  83.     /**
  84.      * Tax class (relation)
  85.      *
  86.      * @var \XLite\Model\TaxClass
  87.      *
  88.      * @ORM\ManyToOne  (targetEntity="XLite\Model\TaxClass")
  89.      * @ORM\JoinColumn (name="tax_class_id", referencedColumnName="id", onDelete="SET NULL")
  90.      */
  91.     protected $taxClass;
  92.     /**
  93.      * Added status
  94.      *
  95.      * @var boolean
  96.      *
  97.      * @ORM\Column (type="boolean")
  98.      */
  99.     protected $added false;
  100.     /**
  101.      * Specific module family name
  102.      *
  103.      * @var string
  104.      *
  105.      * @ORM\Column (type="string", length=255)
  106.      */
  107.     protected $moduleName '';
  108.     /**
  109.      * Flag:
  110.      *   1 - method has been got from marketplace,
  111.      *   0 - method has been added after distr or module installation
  112.      *
  113.      * @var boolean
  114.      *
  115.      * @ORM\Column (type="boolean")
  116.      */
  117.     protected $fromMarketplace false;
  118.     /**
  119.      * Table type
  120.      *
  121.      * @var string
  122.      *
  123.      * @ORM\Column (type="string", options={ "fixed": true }, length=3, nullable=true)
  124.      */
  125.     protected $tableType;
  126.     /**
  127.      * Handling fee (surcharge) for online methods
  128.      *
  129.      * @var float
  130.      *
  131.      * @ORM\Column (type="decimal", precision=14, scale=4)
  132.      */
  133.     protected $handlingFee 0;
  134.     /**
  135.      * Handling fee type(absolute or percent)
  136.      *
  137.      * @var string
  138.      *
  139.      * @ORM\Column (type="string", length=1)
  140.      */
  141.     protected $handlingFeeType \XLite\View\FormField\Select\AbsoluteOrPercent::TYPE_ABSOLUTE;
  142.     /**
  143.      * @var \Doctrine\Common\Collections\Collection
  144.      *
  145.      * @ORM\OneToMany (targetEntity="XLite\Model\Shipping\MethodTranslation", mappedBy="owner", cascade={"all"})
  146.      */
  147.     protected $translations;
  148.     /**
  149.      * @ORM\Column(type="string", nullable=true)
  150.      */
  151.     protected ?string $module;
  152.     /**
  153.      * Constructor
  154.      *
  155.      * @param array $data Entity properties OPTIONAL
  156.      */
  157.     public function __construct(array $data = [])
  158.     {
  159.         $this->shipping_markups = new \Doctrine\Common\Collections\ArrayCollection();
  160.         parent::__construct($data);
  161.     }
  162.     /**
  163.      * Get processor class object
  164.      *
  165.      * @return null|\XLite\Model\Shipping\Processor\AProcessor
  166.      */
  167.     public function getProcessorObject()
  168.     {
  169.         return \XLite\Model\Shipping::getProcessorObjectByProcessorId($this->getProcessor());
  170.     }
  171.     /**
  172.      * Returns processor module
  173.      *
  174.      * @return string|null
  175.      */
  176.     public function getProcessorModule()
  177.     {
  178.         return ($processor $this->getProcessorObject())
  179.             ? $processor->getModule()
  180.             : $this->getModuleName();
  181.     }
  182.     /**
  183.      * Get shipping method admin icon URL
  184.      *
  185.      * @return string
  186.      */
  187.     public function getAdminIconURL()
  188.     {
  189.         [$author$name] = explode('-'$this->getModuleName());
  190.         $url $this->getProcessorObject()
  191.             ? $this->getProcessorObject()->getAdminIconURL($this)
  192.             : false;
  193.         if ($url === true || $url === null) {
  194.             $url $author && $name
  195.                 \XLite\Core\Layout::getInstance()
  196.                     ->getResourceWebPath('modules/' $author '/' $name '/method_icon.jpg')
  197.                 : null;
  198.         }
  199.         if (!$url) {
  200.             $addonImagesUrl ConfigParser::getOptions(['marketplace''addon_images_url']);
  201.             $url            "{$addonImagesUrl}{$author}/{$name}/list_icon.jpg";
  202.         }
  203.         return $url;
  204.     }
  205.     /**
  206.      * Return true if rates exists for this shipping method
  207.      *
  208.      * @return boolean
  209.      */
  210.     public function hasRates()
  211.     {
  212.         return (bool) $this->getRatesCount();
  213.     }
  214.     /**
  215.      * Get count of rates specified for this shipping method
  216.      *
  217.      * @return integer
  218.      */
  219.     public function getRatesCount()
  220.     {
  221.         return count($this->getShippingMarkups());
  222.     }
  223.     /**
  224.      * Check if method is enabled
  225.      *
  226.      * @return boolean
  227.      */
  228.     public function isEnabled()
  229.     {
  230.         return $this->enabled
  231.             && ($this->getProcessorObject() === null || $this->getProcessorObject()->isConfigured());
  232.     }
  233.     /**
  234.      * Returns present status
  235.      *
  236.      * @return boolean
  237.      */
  238.     public function isAdded()
  239.     {
  240.         return (bool) $this->added;
  241.     }
  242.     /**
  243.      * Set present status
  244.      *
  245.      * @param boolean $value Value
  246.      */
  247.     public function setAdded($value)
  248.     {
  249.         $changed     $this->added !== $value;
  250.         $this->added = (bool) $value;
  251.         if (!$this->added) {
  252.             $this->setEnabled(false);
  253.         } elseif ($changed) {
  254.             $this->setPosition($this->getRepository()->getMaxPosition('offline') + 10);
  255.         }
  256.     }
  257.     /**
  258.      * Check if shipping method is from marketplace
  259.      *
  260.      * @return bool
  261.      */
  262.     public function isFromMarketplace()
  263.     {
  264.         return (bool) $this->getFromMarketplace();
  265.     }
  266.     /**
  267.      * Returns module author and name (with underscore as separator)
  268.      *
  269.      * @return string
  270.      */
  271.     public function getModuleName()
  272.     {
  273.         $result $this->moduleName;
  274.         if (!$this->isFromMarketplace()) {
  275.             $processor $this->getProcessorObject();
  276.             if ($processor) {
  277.                 $result $processor->getModule();
  278.             }
  279.         }
  280.         return str_replace('_''-'$result);
  281.     }
  282.     /**
  283.      * Return parent method for online carrier service
  284.      *
  285.      * @return \XLite\Model\Shipping\Method
  286.      */
  287.     public function getParentMethod()
  288.     {
  289.         return $this->getProcessor() !== 'offline' && $this->getCarrier() !== ''
  290.             $this->getRepository()->findOnlineCarrier($this->getProcessor())
  291.             : null;
  292.     }
  293.     /**
  294.      * Retuns children methods for online carrier
  295.      *
  296.      * @return array
  297.      */
  298.     public function getChildrenMethods()
  299.     {
  300.         return $this->getProcessor() !== 'offline' && $this->getCarrier() === ''
  301.             $this->getRepository()->findMethodsByProcessor($this->getProcessor(), false)
  302.             : [];
  303.     }
  304.     /**
  305.      * Returns handling fee
  306.      *
  307.      * @return float
  308.      */
  309.     public function getHandlingFee()
  310.     {
  311.         return [
  312.             PriceOrPercent::PRICE_VALUE => $this->getHandlingFeeValue(),
  313.             PriceOrPercent::TYPE_VALUE  => $this->getHandlingFeeType(),
  314.         ];
  315.     }
  316.     /**
  317.      * Returns handling fee
  318.      *
  319.      * @return float
  320.      */
  321.     public function getHandlingFeeValue()
  322.     {
  323.         $parentMethod $this->getParentMethod();
  324.         return $parentMethod $parentMethod->getHandlingFeeValue() : $this->handlingFee;
  325.     }
  326.     /**
  327.      * Returns handling fee
  328.      *
  329.      * @param float
  330.      *
  331.      * @return float
  332.      */
  333.     public function setHandlingFeeValue($value)
  334.     {
  335.         $this->handlingFee $value;
  336.         return $this;
  337.     }
  338.     /**
  339.      * Get method_id
  340.      *
  341.      * @return integer
  342.      */
  343.     public function getMethodId()
  344.     {
  345.         return $this->method_id;
  346.     }
  347.     /**
  348.      * Set processor
  349.      *
  350.      * @param string $processor
  351.      *
  352.      * @return Method
  353.      */
  354.     public function setProcessor($processor)
  355.     {
  356.         $this->processor $processor;
  357.         return $this;
  358.     }
  359.     /**
  360.      * Get processor
  361.      *
  362.      * @return string
  363.      */
  364.     public function getProcessor()
  365.     {
  366.         return $this->processor;
  367.     }
  368.     /**
  369.      * Set carrier
  370.      *
  371.      * @param string $carrier
  372.      *
  373.      * @return Method
  374.      */
  375.     public function setCarrier($carrier)
  376.     {
  377.         $this->carrier $carrier;
  378.         return $this;
  379.     }
  380.     /**
  381.      * Get carrier
  382.      *
  383.      * @return string
  384.      */
  385.     public function getCarrier()
  386.     {
  387.         return $this->carrier;
  388.     }
  389.     /**
  390.      * Set code
  391.      *
  392.      * @param string $code
  393.      *
  394.      * @return Method
  395.      */
  396.     public function setCode($code)
  397.     {
  398.         $this->code $code;
  399.         return $this;
  400.     }
  401.     /**
  402.      * Get code
  403.      *
  404.      * @return string
  405.      */
  406.     public function getCode()
  407.     {
  408.         return $this->code;
  409.     }
  410.     /**
  411.      * Set enabled
  412.      *
  413.      * @param boolean $enabled
  414.      *
  415.      * @return Method
  416.      */
  417.     public function setEnabled($enabled)
  418.     {
  419.         $this->enabled = (bool) $enabled;
  420.         return $this;
  421.     }
  422.     /**
  423.      * Get enabled
  424.      *
  425.      * @return boolean
  426.      */
  427.     public function getEnabled()
  428.     {
  429.         return $this->enabled;
  430.     }
  431.     /**
  432.      * Set position
  433.      *
  434.      * @param integer $position
  435.      *
  436.      * @return Method
  437.      */
  438.     public function setPosition($position)
  439.     {
  440.         $this->position $position;
  441.         return $this;
  442.     }
  443.     /**
  444.      * Get position
  445.      *
  446.      * @return integer
  447.      */
  448.     public function getPosition()
  449.     {
  450.         return $this->position;
  451.     }
  452.     /**
  453.      * Get added
  454.      *
  455.      * @return boolean
  456.      */
  457.     public function getAdded()
  458.     {
  459.         return $this->added;
  460.     }
  461.     /**
  462.      * Set moduleName
  463.      *
  464.      * @param string $moduleName
  465.      *
  466.      * @return Method
  467.      */
  468.     public function setModuleName($moduleName)
  469.     {
  470.         $this->moduleName $moduleName;
  471.         return $this;
  472.     }
  473.     /**
  474.      * Set fromMarketplace
  475.      *
  476.      * @param boolean $fromMarketplace
  477.      *
  478.      * @return Method
  479.      */
  480.     public function setFromMarketplace($fromMarketplace)
  481.     {
  482.         $this->fromMarketplace $fromMarketplace;
  483.         return $this;
  484.     }
  485.     /**
  486.      * Get fromMarketplace
  487.      *
  488.      * @return boolean
  489.      */
  490.     public function getFromMarketplace()
  491.     {
  492.         return $this->fromMarketplace;
  493.     }
  494.     /**
  495.      * Set tableType
  496.      *
  497.      * @param string $tableType
  498.      *
  499.      * @return Method
  500.      */
  501.     public function setTableType($tableType)
  502.     {
  503.         $this->tableType $tableType;
  504.         return $this;
  505.     }
  506.     /**
  507.      * Get tableType
  508.      *
  509.      * @return string
  510.      */
  511.     public function getTableType()
  512.     {
  513.         return $this->tableType;
  514.     }
  515.     /**
  516.      * Set handlingFee
  517.      *
  518.      * @param array $handlingFee
  519.      *
  520.      * @return Method
  521.      */
  522.     public function setHandlingFee($handlingFee)
  523.     {
  524.         $this->setHandlingFeeValue(
  525.             $handlingFee[PriceOrPercent::PRICE_VALUE] ?? 0
  526.         );
  527.         $this->setHandlingFeeType(
  528.             $handlingFee[PriceOrPercent::TYPE_VALUE] ?? \XLite\View\FormField\Select\AbsoluteOrPercent::TYPE_ABSOLUTE
  529.         );
  530.         return $this;
  531.     }
  532.     /**
  533.      * Return handling fee type, possible values:
  534.      * \XLite\View\FormField\Select\AbsoluteOrPercent::TYPE_ABSOLUTE
  535.      * \XLite\View\FormField\Select\AbsoluteOrPercent::TYPE_PERCENT
  536.      *
  537.      * @return string
  538.      */
  539.     public function getHandlingFeeType()
  540.     {
  541.         $parentMethod $this->getParentMethod();
  542.         return $parentMethod $parentMethod->getHandlingFeeType() : $this->handlingFeeType;
  543.     }
  544.     /**
  545.      * Set shipping handling fee type (% or absolute)
  546.      *
  547.      * @param string $handlingFeeType
  548.      *
  549.      * @return $this
  550.      */
  551.     public function setHandlingFeeType($handlingFeeType)
  552.     {
  553.         $this->handlingFeeType $handlingFeeType;
  554.         return $this;
  555.     }
  556.     /**
  557.      * Add shipping_markups
  558.      *
  559.      * @param \XLite\Model\Shipping\Markup $shippingMarkups
  560.      *
  561.      * @return Method
  562.      */
  563.     public function addShippingMarkups(\XLite\Model\Shipping\Markup $shippingMarkups)
  564.     {
  565.         $this->shipping_markups[] = $shippingMarkups;
  566.         return $this;
  567.     }
  568.     /**
  569.      * Get shipping_markups
  570.      *
  571.      * @return \Doctrine\Common\Collections\Collection
  572.      */
  573.     public function getShippingMarkups()
  574.     {
  575.         return $this->shipping_markups;
  576.     }
  577.     /**
  578.      * Set taxClass
  579.      *
  580.      * @param \XLite\Model\TaxClass $taxClass
  581.      *
  582.      * @return Method
  583.      */
  584.     public function setTaxClass(\XLite\Model\TaxClass $taxClass null)
  585.     {
  586.         $this->taxClass $taxClass;
  587.         return $this;
  588.     }
  589.     /**
  590.      * Get taxClass
  591.      *
  592.      * @return \XLite\Model\TaxClass
  593.      */
  594.     public function getTaxClass()
  595.     {
  596.         return $this->taxClass;
  597.     }
  598.     /**
  599.      * Get translation
  600.      *
  601.      * @param string  $code             Language code OPTIONAL
  602.      * @param boolean $allowEmptyResult Flag OPTIONAL
  603.      *
  604.      * @return \XLite\Model\Base\Translation
  605.      */
  606.     public function getTranslation($code null$allowEmptyResult false)
  607.     {
  608.         $translation parent::getTranslation($code$allowEmptyResult);
  609.         if (
  610.             $translation
  611.             && $translation->getName() === ''
  612.             && $translation->getCode() !== 'en'
  613.         ) {
  614.             $defaultTranslation $this->getHardTranslation('en');
  615.             if ($defaultTranslation) {
  616.                 $translation->setName(
  617.                     $defaultTranslation->getName()
  618.                 );
  619.             }
  620.         }
  621.         return $translation;
  622.     }
  623.     public function getModule(): ?string
  624.     {
  625.         return $this->module;
  626.     }
  627.     public function setModule(?string $module): void
  628.     {
  629.         $this->module $module;
  630.     }
  631.     // {{{ Translation Getters / setters
  632.     /**
  633.      * @return string
  634.      */
  635.     public function getDeliveryTime()
  636.     {
  637.         return $this->getTranslationField(__FUNCTION__);
  638.     }
  639.     /**
  640.      * @param string $deliveryTime
  641.      *
  642.      * @return \XLite\Model\Base\Translation
  643.      */
  644.     public function setDeliveryTime($deliveryTime)
  645.     {
  646.         return $this->setTranslationField(__FUNCTION__$deliveryTime);
  647.     }
  648.     /**
  649.      * @return string
  650.      */
  651.     public function getAdminDescription()
  652.     {
  653.         return $this->getTranslationField(__FUNCTION__);
  654.     }
  655.     /**
  656.      * @param string $adminDescription
  657.      *
  658.      * @return \XLite\Model\Base\Translation
  659.      */
  660.     public function setAdminDescription($adminDescription)
  661.     {
  662.         return $this->setTranslationField(__FUNCTION__$adminDescription);
  663.     }
  664.     // }}}
  665.     public function canBeEstimated(): bool
  666.     {
  667.         return $this->isEnabled() && $this->hasRates();
  668.     }
  669. }