/** * Paid from internal account * * @param Sale\Order $order Entity of the order. * @param bool $pay Flag making donations to internal account. * @param null $paidFormUserBudget * @return Sale\Result * @throws Main\ObjectNotFoundException * @throws \Bitrix\Main\ArgumentOutOfRangeException * @throws \Bitrix\Main\InvalidOperationException */ public static function payFromBudget(Sale\Order $order, $pay, $paidFormUserBudget = null) { $result = new Sale\Result(); /** @var Sale\Payment|null $paymentOuter */ $paymentInner = null; /** @var Sale\Payment|null $paymentOuter */ $paymentOuter = null; /** @var Sale\PaymentCollection $paymentCollection */ if (!($paymentCollection = $order->getPaymentCollection())) { throw new Main\ObjectNotFoundException('Entity "PaymentCollection" not found'); } if (count($paymentCollection) > 2) { return $result; } $needSum = $order->getPrice() - $order->getSumPaid(); if ($needSum > 0) { /** @var Sale\Payment $payment */ foreach ($paymentCollection as $payment) { if (!$payment->isInner()) { $paymentOuter = $payment; break; } } if (!$pay || $pay && $paidFormUserBudget === false) { /** @var Sale\Payment $paymentInner */ if (!($paymentInner = $paymentCollection->getInnerPayment())) { throw new Main\ObjectNotFoundException('Entity inner "Payment" not found'); } $userBudget = Sale\Internals\UserBudgetPool::getUserBudget($order->getUserId(), $order->getCurrency()); $setSum = $userBudget; if ($userBudget >= $needSum) { $setSum = $needSum; } if ($paymentInner->getId() == 0) { $paymentInnerFields = array('SUM' => $setSum, 'CURRENCY' => $order->getCurrency(), 'DATE_BILL' => new Main\Type\DateTime()); $r = $paymentInner->setFields($paymentInnerFields); if (!$r->isSuccess()) { $result->addErrors($r->getErrors()); } } else { if ($paymentInner->getSum() < $needSum) { $paymentInner->setField('SUM', $needSum - $paymentInner->getSum()); } } if ($pay && $paidFormUserBudget === false) { $paymentOuter->setField('SUM', $needSum - $setSum); } $payment = $paymentInner; } else { $payment = $paymentOuter; } if ($pay) { if ($payment === null) { $paySystemId = static::getDefaultPaySystemId($order->getPersonTypeId()); /** @var Sale\PaySystemService $paysystem */ if ($paysystem = Sale\PaySystemService::load($paySystemId)) { $payment = Sale\Payment::create($paymentCollection, $paysystem); $payment->setField('SUM', $needSum); $payment->setField('DATE_BILL', new Main\Type\DateTime()); $paymentCollection->addItem($payment); } } $operationPayment = $payment; if ($paidFormUserBudget === false) { $operationPayment = $paymentOuter; } $r = Sale\Internals\PaySystemInner::createOperation($order, $operationPayment, Sale\Internals\PaySystemInner::OPERATION_CREDIT); if (!$r->isSuccess()) { $result->addErrors($r->getErrors()); } } if ($payment->isReturn() && $payment->isInner()) { /** @var Sale\Result $r */ $r = $payment->setReturn('N'); } else { /** @var Sale\Result $r */ $r = $payment->setPaid('Y'); if ($r->isSuccess()) { if ($pay) { $operationPayment = $payment; if ($paidFormUserBudget === false) { $operationPayment = $paymentOuter; /** @var Sale\Result $resultPayment */ $resultPayment = $paymentOuter->setPaid('Y'); if (!$resultPayment->isSuccess()) { $result->addErrors($resultPayment->getErrors()); } } /** @var Sale\Result $r */ $r = Sale\Internals\PaySystemInner::createOperation($order, $operationPayment, Sale\Internals\PaySystemInner::OPERATION_DEBIT); } } else { $result->addErrors($r->getErrors()); } } if (!$r->isSuccess()) { $result->addErrors($r->getErrors()); } } $result->setData(array('PAID_FROM_BUDGET' => $paidFormUserBudget)); return $result; }
/** * @param $value * @return Result * @throws Main\ArgumentOutOfRangeException * @throws Main\ObjectNotFoundException * @throws \Exception */ public function setPaid($value) { $result = new Result(); if ($value == "Y") { if ($this->isPaid()) { return new Result(); } if ($this->isReturn() && $this->isInner()) { $r = $this->setField('IS_RETURN', 'N'); if (!$r->isSuccess()) { $result->addErrors($r->getErrors()); return $result; } } else { if ($this->isInner()) { /** @var PaymentCollection $paymentCollection */ if (!($paymentCollection = $this->getCollection())) { throw new Main\ObjectNotFoundException('Entity "PaymentCollection" not found'); } /** @var Order $order */ if (!($order = $paymentCollection->getOrder())) { throw new Main\ObjectNotFoundException('Entity "Order" not found'); } /** @var Result $r */ $r = Internals\PaySystemInner::createOperation($order, $this, Internals\PaySystemInner::OPERATION_DEBIT); if (!$r->isSuccess()) { $result->addErrors($r->getErrors()); return $result; } } } } elseif ($value == "N") { if (!$this->isPaid()) { return new Result(); } if ($this->isInner()) { /** @var PaymentCollection $paymentCollection */ if (!($paymentCollection = $this->getCollection())) { throw new Main\ObjectNotFoundException('Entity "PaymentCollection" not found'); } /** @var Order $order */ if (!($order = $paymentCollection->getOrder())) { throw new Main\ObjectNotFoundException('Entity "Order" not found'); } /** @var Result $r */ $r = Internals\PaySystemInner::createOperation($order, $this, Internals\PaySystemInner::OPERATION_CREDIT); if (!$r->isSuccess()) { $result->addErrors($r->getErrors()); return $result; } } } else { throw new Main\ArgumentOutOfRangeException('value'); } /** @var Result $r */ $r = $this->setField('PAID', $value); if (!$r->isSuccess()) { $result->addErrors($r->getErrors()); } return $result; }