app/Plugin/PayPalCheckout42/Entity/SubscribingCustomer.php line 145

Open in your IDE?
  1. <?php
  2. namespace Plugin\PayPalCheckout42\Entity;
  3. use DateTime;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Eccube\Entity\Customer;
  6. use Eccube\Entity\Master\Pref;
  7. use Eccube\Entity\ProductClass;
  8. use Eccube\Repository\Master\PrefRepository;
  9. /**
  10.  * Class SubscribingCustomer
  11.  * @package Plugin\PayPalCheckout42\Entity
  12.  *
  13.  * @ORM\Table(name="plg_paypal_subscribing_customer")
  14.  * @ORM\Entity(repositoryClass="Plugin\PayPalCheckout42\Repository\SubscribingCustomerRepository")
  15.  */
  16. class SubscribingCustomer
  17. {
  18.     /**
  19.      * @var integer
  20.      *
  21.      * @ORM\Column(name="id", type="integer", options={"unsigned":true})
  22.      * @ORM\Id
  23.      * @ORM\GeneratedValue(strategy="IDENTITY")
  24.      */
  25.     private $id;
  26.     /**
  27.      * @var integer
  28.      *
  29.      * @ORM\Column(name="customer_id", type="integer", options={"unsigned":true, "default":null})
  30.      */
  31.     private $customer_id;
  32.     /**
  33.      * @var integer
  34.      *
  35.      * @ORM\Column(name="product_class_id", type="integer", options={"unsigned":true, "default":null})
  36.      */
  37.     private $product_class_id;
  38.     /**
  39.      * @var Customer
  40.      * @ORM\ManyToOne(targetEntity="Eccube\Entity\Customer", inversedBy="SubscribingCustomer")
  41.      * @ORM\JoinColumns({
  42.      *   @ORM\JoinColumn(name="customer_id", referencedColumnName="id")
  43.      * })
  44.      */
  45.     private $Customer;
  46.     /**
  47.      * @ORM\ManyToOne(targetEntity="Eccube\Entity\ProductClass", inversedBy="SubscribingCustomer")
  48.      * @ORM\JoinColumns({
  49.      *   @ORM\JoinColumn(name="product_class_id", referencedColumnName="id")
  50.      * })
  51.      */
  52.     private $ProductClass;
  53.     /**
  54.      * @var Transaction
  55.      * @ORM\ManyToOne(targetEntity="Transaction", inversedBy="SubscribingCustomer")
  56.      * @ORM\JoinColumns({
  57.      *   @ORM\JoinColumn(name="reference_transaction_id", referencedColumnName="id")
  58.      * })
  59.      */
  60.     private $ReferenceTransaction;
  61.     /**
  62.      * @ORM\Column(name="primary_shipping_address", type="json", length=2048, nullable=true)
  63.      */
  64.     private $primary_shipping_address;
  65.     /**
  66.      * @ORM\Column(name="error_message", type="string", length=255, nullable=true)
  67.      */
  68.     private $error_message;
  69.     /**
  70.      * @var DateTime
  71.      * @ORM\Column(name="next_payment_date", type="datetimetz", nullable=true)
  72.      */
  73.     private $next_payment_date;
  74.     /**
  75.      * @ORM\Column(name="contracted_at", type="datetimetz", nullable=true)
  76.      */
  77.     private $contracted_at;
  78.     /**
  79.      * @ORM\Column(name="withdrawal_at", type="datetimetz", nullable=true)
  80.      */
  81.     private $withdrawal_at;
  82.     /**
  83.      * SubscribingCustomer constructor.
  84.      * @param Customer $customer
  85.      * @param ProductClass $productClass
  86.      * @param Transaction $transaction
  87.      * @throws \Exception
  88.      */
  89.     private function __construct(Customer $customerProductClass $productClassTransaction $transaction)
  90.     {
  91.         $this->Customer $customer;
  92.         $this->ProductClass $productClass;
  93.         $this->ReferenceTransaction $transaction;
  94.         $this->next_payment_date = (new DateTime())->modify("+ 1 months");
  95.         $this->contracted_at = new DateTime();
  96.     }
  97.     /**
  98.      * @param Customer $customer
  99.      * @param ProductClass $productClass
  100.      * @param Transaction $transaction
  101.      * @return SubscribingCustomer
  102.      * @throws \Exception
  103.      */
  104.     public static function create(Customer $customerProductClass $productClassTransaction $transaction): SubscribingCustomer
  105.     {
  106.         if ($productClass->getUseSubscription()) {
  107.             $instance = new static ($customer$productClass$transaction);
  108.             return $instance;
  109.         }
  110.     }
  111.     /**
  112.      * @param int $referenceDay
  113.      * @param int $cutOffDay
  114.      * @return DateTime
  115.      */
  116.     public function calculateNextPaymentDate(int $referenceDayint $cutOffDay): DateTime
  117.     {
  118.         /** @var DateTime $nextPaymentDate */
  119.         $nextPaymentDate = clone $this->next_payment_date;
  120.         /** @var int $base */
  121.         $base = (int) $nextPaymentDate->format('d');
  122.         if ($base $cutOffDay) {
  123.             $nextPaymentDate->modify('first day of next months');
  124.         } else {
  125.             $nextPaymentDate->modify('first day of + 2 months');
  126.         }
  127.         $referenceDay--;
  128.         $nextPaymentDate->modify("+ ${referenceDay} days");
  129.         return $nextPaymentDate;
  130.     }
  131.     /**
  132.      * @return int
  133.      */
  134.     public function getId()
  135.     {
  136.         return $this->id;
  137.     }
  138.     /**
  139.      * @return int
  140.      */
  141.     public function getCustomerId()
  142.     {
  143.         return $this->customer_id;
  144.     }
  145.     /**
  146.      * @return int
  147.      */
  148.     public function getProductClassId()
  149.     {
  150.         return $this->product_class_id;
  151.     }
  152.     /**
  153.      * @param Transaction $transaction
  154.      */
  155.     public function setReferenceTransaction(Transaction $transaction): void
  156.     {
  157.         $this->ReferenceTransaction $transaction;
  158.     }
  159.     /**
  160.      * @return Transaction
  161.      */
  162.     public function getReferenceTransaction(): Transaction
  163.     {
  164.         return $this->ReferenceTransaction;
  165.     }
  166.     /**
  167.      * @param DateTime $date
  168.      */
  169.     public function setNextPaymentDate($date)
  170.     {
  171.         $this->next_payment_date $date;
  172.     }
  173.     /**
  174.      * @return DateTime
  175.      */
  176.     public function getNextPaymentDate(): DateTime
  177.     {
  178.         return $this->next_payment_date;
  179.     }
  180.     /**
  181.      * @return Customer
  182.      */
  183.     public function getCustomer(): Customer
  184.     {
  185.         return $this->Customer;
  186.     }
  187.     /**
  188.      * @return ProductClass
  189.      */
  190.     public function getProductClass(): ProductClass
  191.     {
  192.         return $this->ProductClass;
  193.     }
  194.     /**
  195.      * @param DateTime $date
  196.      */
  197.     public function setWithdrawalAt($date)
  198.     {
  199.         $this->withdrawal_at $date;
  200.     }
  201.     /**
  202.      * @return DateTime
  203.      */
  204.     public function getWithdrawalAt(): DateTime
  205.     {
  206.         return $this->withdrawal_at;
  207.     }
  208.     /**
  209.      * @return ConfigSubscription
  210.      */
  211.     public function createConfigSubscription(): ConfigSubscription
  212.     {
  213.         /** @var ConfigSubscription $configSubscription */
  214.         $configSubscription = new ConfigSubscription();
  215.         /** @var bool $deleted */
  216.         $deleted is_null($this->withdrawal_at) ? false true;
  217.         $configSubscription->setIsDeleted($deleted);
  218.         $configSubscription->setNextPaymentDate($this->next_payment_date);
  219.         return $configSubscription;
  220.     }
  221.     /**
  222.      * @param $errorMessage
  223.      */
  224.     public function setErrorMessage($errorMessage)
  225.     {
  226.         $this->error_message $errorMessage;
  227.     }
  228.     /**
  229.      * @param $name
  230.      * @param $arguments
  231.      * @return mixed
  232.      */
  233.     public function __call($name$arguments)
  234.     {
  235.         if (property_exists($this$name)) {
  236.             return $this->$name;
  237.         }
  238.     }
  239.     /**
  240.      * @return PrimaryShippingAddress
  241.      */
  242.     public function getPrimaryShippingAddress(): PrimaryShippingAddress
  243.     {
  244.         /** @var array|null $values */
  245.         $values $this->primary_shipping_address;
  246.         /** @var Customer $Customer */
  247.         $Customer $this->ReferenceTransaction->getOrder()->getCustomer();
  248.         /** @var PrimaryShippingAddress $PrimaryShippingAddress */
  249.         $PrimaryShippingAddress = new PrimaryShippingAddress();
  250.         $PrimaryShippingAddress
  251.             ->setFirstName($values['first_name'] ?? $Customer->getName02())
  252.             ->setLastName($values['last_name'] ?? $Customer->getName01())
  253.             ->setFirstNameKana($values['first_name_kana'] ?? $Customer->getKana02())
  254.             ->setLastNameKana($values['last_name_kana'] ?? $Customer->getKana01())
  255.             ->setPref($values['pref'] ?? $Customer->getPref()->getName())
  256.             ->setPostalCode($values['postal_code'] ?? $Customer->getPostalCode())
  257.             ->setAddress1($values['address1'] ?? $Customer->getAddr01())
  258.             ->setAddress2($values['address2'] ?? $Customer->getAddr02())
  259.             ->setPhoneNumber($values['phone_number'] ?? $Customer->getPhoneNumber())
  260.             ->setCompanyName($values['company_name'] ?? $Customer->getCompanyName());
  261.         return $PrimaryShippingAddress;
  262.     }
  263.     /**
  264.      * @param PrefRepository $prefRepository
  265.      * @return Customer|null
  266.      */
  267.     public function getPrimaryCustomer(PrefRepository $prefRepository): ?Customer
  268.     {
  269.         /** @var array $values */
  270.         $values $this->primary_shipping_address;
  271.         if (count($values) !== 0) {
  272.             /** @var Pref $Pref */
  273.             $Pref $prefRepository->findOneBy(['name' => $values['pref']]);
  274.             return $this->Customer
  275.                 ->setName01($values['last_name'])
  276.                 ->setName02($values['first_name'])
  277.                 ->setKana01($values['last_name_kana'])
  278.                 ->setKana02($values['first_name_kana'])
  279.                 ->setCompanyName($values['company_name'])
  280.                 ->setPhonenumber($values['phone_number'])
  281.                 ->setPostalcode($values['postal_code'])
  282.                 ->setPref($Pref)
  283.                 ->setAddr01($values['address1'])
  284.                 ->setAddr02($values['address2']);
  285.         }
  286.         return null;
  287.     }
  288.     /**
  289.      * @param PrimaryShippingAddress $PrimaryShippingAddress
  290.      */
  291.     public function setPrimaryShippingAddress(PrimaryShippingAddress $PrimaryShippingAddress): void
  292.     {
  293.         $values = [
  294.             'first_name' => $PrimaryShippingAddress->getFirstName(),
  295.             'last_name' => $PrimaryShippingAddress->getLastName(),
  296.             'first_name_kana' => $PrimaryShippingAddress->getFirstNameKana(),
  297.             'last_name_kana' => $PrimaryShippingAddress->getLastNameKana(),
  298.             'pref' => $PrimaryShippingAddress->getPref(),
  299.             'postal_code' => $PrimaryShippingAddress->getPostalCode(),
  300.             'address1' => $PrimaryShippingAddress->getAddress1(),
  301.             'address2' => $PrimaryShippingAddress->getAddress2(),
  302.             'phone_number' => $PrimaryShippingAddress->getPhoneNumber(),
  303.             'company_name' => $PrimaryShippingAddress->getCompanyName(),
  304.         ];
  305.         $this->primary_shipping_address $values;
  306.     }
  307. }