Example #1
0
 /**
  * @param null $oldPaid
  * @return Result
  * @throws Main\ObjectNotFoundException
  */
 protected function onAfterSyncPaid($oldPaid = null)
 {
     global $USER;
     $result = new Result();
     /** @var PaymentCollection $paymentCollection */
     if (!($paymentCollection = $this->getPaymentCollection())) {
         throw new Main\ObjectNotFoundException('Entity "PaymentCollection" not found');
     }
     /** @var ShipmentCollection $shipmentCollection */
     if (!($shipmentCollection = $this->getShipmentCollection())) {
         throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
     }
     $oldPaidBool = null;
     if ($oldPaid !== null) {
         $oldPaidBool = $oldPaid == "Y";
     }
     if ($oldPaid == "N" && $this->isPaid()) {
         $orderStatus = Config\Option::get('sale', 'status_on_paid', '');
         if (strval($orderStatus) != '') {
             if ($USER && $USER->isAuthorized()) {
                 $statusesList = OrderStatus::getAllowedUserStatuses($USER->getID(), $this->getField('STATUS_ID'));
                 $statusesList = array_keys($statusesList);
             } else {
                 $statusesList = OrderStatus::getAllStatuses();
             }
             if ($this->getField('STATUS_ID') != $orderStatus && in_array($orderStatus, $statusesList)) {
                 $this->setField('STATUS_ID', $orderStatus);
             }
         }
     }
     if ($oldPaid !== null && $this->isPaid() != $oldPaidBool) {
         Internals\EventsPool::addEvent($this, EventActions::EVENT_ON_ORDER_PAID, array('ENTITY' => $this));
         Internals\EventsPool::addEvent($this, EventActions::EVENT_ON_ORDER_PAID_SEND_MAIL, array('ENTITY' => $this));
     }
     if (Configuration::getProductReservationCondition() == Configuration::RESERVE_ON_PAY) {
         if ($paymentCollection->hasPaidPayment()) {
             $r = $shipmentCollection->tryReserve();
             if (!$r->isSuccess()) {
                 $result->addErrors($r->getErrors());
             }
         } else {
             $r = $shipmentCollection->tryUnreserve();
             if (!$r->isSuccess()) {
                 $result->addErrors($r->getErrors());
             }
         }
     } elseif (Configuration::getProductReservationCondition() == Configuration::RESERVE_ON_FULL_PAY) {
         if ($oldPaid == "N" && $this->isPaid()) {
             $r = $shipmentCollection->tryReserve();
             if (!$r->isSuccess()) {
                 $result->addErrors($r->getErrors());
             }
         } elseif ($oldPaid == "Y" && !$this->isPaid()) {
             $r = $shipmentCollection->tryUnreserve();
             if (!$r->isSuccess()) {
                 $result->addErrors($r->getErrors());
             }
         }
     }
     if (Configuration::needAllowDeliveryOnPay()) {
         if ($oldPaid == "N" && $this->isPaid()) {
             $r = $shipmentCollection->allowDelivery();
             if (!$r->isSuccess()) {
                 $result->addErrors($r->getErrors());
             }
         } elseif ($oldPaid == "Y" && !$this->isPaid()) {
             $r = $shipmentCollection->disallowDelivery();
             if (!$r->isSuccess()) {
                 $result->addErrors($r->getErrors());
             }
         }
     }
     return $result;
 }