Example #1
0
 /**
  * @return PaymentCollection
  */
 public function loadPaymentCollection()
 {
     return PaymentCollection::load($this);
 }
 /**
  * @internal
  *
  * @param Sale\PaymentCollection $paymentCollection
  * @param array $fields
  * @return Sale\Result
  * @throws Main\ObjectNotFoundException
  */
 public static function fillPaymentCollectionFromRequest(Sale\PaymentCollection $paymentCollection, array $fields)
 {
     $result = new Sale\Result();
     $sum = floatval($fields['PRICE']);
     if (isset($fields['SUM_PAID']) && floatval($fields['SUM_PAID']) >= floatval($fields['PRICE'])) {
         $sum = floatval($fields['SUM_PAID']);
     }
     $isPayFromUserBudget = null;
     $backToUserBudget = null;
     if (array_key_exists('ONLY_FULL_PAY_FROM_ACCOUNT', $fields)) {
         $isPayFromUserBudget = $fields['ONLY_FULL_PAY_FROM_ACCOUNT'];
     }
     if ($isPayFromUserBudget === null && array_key_exists('PAY_CURRENT_ACCOUNT', $fields) && $fields['PAY_CURRENT_ACCOUNT'] !== null) {
         $isPayFromUserBudget = $fields['PAY_CURRENT_ACCOUNT'] != "Y";
     }
     if (array_key_exists('PAY_FROM_ACCOUNT_BACK', $fields)) {
         $backToUserBudget = $fields['PAY_FROM_ACCOUNT_BACK'] == "Y";
     }
     $paySystemId = null;
     $paySystemName = null;
     $paidFull = false;
     /** @var Sale\Order $order */
     if (!($order = $paymentCollection->getOrder())) {
         throw new Main\ObjectNotFoundException('Entity "Order" not found');
     }
     $userId = $order->getUserId();
     $currency = $order->getCurrency();
     $paymentInner = null;
     $paymentOuter = null;
     $countPayments = count($paymentCollection);
     if ($countPayments == 0 && $order->getId() == 0 || $countPayments == 2 && $paymentCollection->isExistsInnerPayment() || $countPayments == 1 && !$paymentCollection->isExistsInnerPayment()) {
         $needSum = $order->getPrice() - $order->getSumPaid();
         if ($countPayments == 0) {
             if (!isset($fields["PAY_SYSTEM_ID"])) {
                 $fields["PAY_SYSTEM_ID"] = static::getDefaultPaySystemId($order->getPersonTypeId());
             }
             /** @var Sale\PaySystemService $paySystem */
             $paySystem = \Bitrix\Sale\PaySystemService::load($fields["PAY_SYSTEM_ID"]);
             /** @var Sale\Payment $paymentOuter */
             $paymentOuter = $paymentCollection->createItem($paySystem);
             $paymentOuter->setField('SUM', $needSum);
             $paymentOuter->setField('PAY_SYSTEM_NAME', $paySystem->getAttribute('NAME'));
             $order->setFieldNoDemand('PAY_SYSTEM_ID', $fields["PAY_SYSTEM_ID"]);
             $countPayments = 1;
         }
         if (isset($fields['PAYED'])) {
             $paidFlag = null;
             if ($countPayments > 0) {
                 /** @var Sale\Payment $payment */
                 foreach ($paymentCollection as $payment) {
                     if ($paidFlag === null && $payment->isPaid() && $needSum == 0) {
                         $paidFlag = 'Y';
                     }
                     if ($payment->isInner()) {
                         continue;
                     }
                     $paymentOuter = $payment;
                 }
             }
             if ($paidFlag === null) {
                 $paidFlag = 'N';
             }
             if ($paidFlag != $fields['PAYED']) {
                 if ($fields['PAYED'] == "Y") {
                     $pay = true;
                     if ($isPayFromUserBudget !== null) {
                         if (static::canPayWithUserBudget($needSum, $userId, $currency, $isPayFromUserBudget)) {
                             $userBudget = Sale\Internals\UserBudgetPool::getUserBudget($userId, $currency);
                             if ($userBudget >= $needSum) {
                                 $pay = false;
                             }
                         }
                     }
                     /** @var Sale\Result $r */
                     $r = static::payFromBudget($order, $pay, $isPayFromUserBudget);
                     if ($r->isSuccess()) {
                         $needSum = $order->getPrice() - $order->getSumPaid();
                         if (!$pay) {
                             /** @var Sale\Result $r */
                             $r = $paymentOuter->setField('SUM', $needSum);
                         }
                         if (!$r->isSuccess()) {
                             $result->addErrors($r->getErrors());
                         }
                     } else {
                         $result->addErrors($r->getErrors());
                     }
                 } else {
                     //
                     /** @var Sale\Payment $payment */
                     foreach ($paymentCollection as $payment) {
                         if ($payment->isPaid()) {
                             if ($backToUserBudget && $payment->isInner()) {
                                 $payment->setReturn('Y');
                             } else {
                                 $payment->setPaid('N');
                             }
                         }
                         if ($payment->isInner()) {
                             $payment->delete();
                         } else {
                             $payment->setField('SUM', $order->getPrice());
                         }
                     }
                 }
                 unset($fields['PAYED']);
             } elseif ($order->getId() == 0) {
                 if ($isPayFromUserBudget !== null) {
                     if (static::canPayWithUserBudget($needSum, $userId, $currency, $isPayFromUserBudget)) {
                         $userBudget = Sale\Internals\UserBudgetPool::getUserBudget($userId, $currency);
                         $setSum = $userBudget;
                         if ($userBudget >= $sum) {
                             $paidFull = true;
                         }
                         /** @var Sale\Result $r */
                         $r = static::payFromBudget($order, false);
                         if ($r->isSuccess()) {
                             $sum -= $setSum;
                         } else {
                             $result->addErrors($r->getErrors());
                         }
                     }
                 }
             }
             if ($order->getId() > 0) {
                 $payment = null;
                 /** @var Sale\Payment $paymentItem */
                 foreach ($paymentCollection as $paymentItem) {
                     if ($paymentItem->isInner()) {
                         $paymentInner = $paymentItem;
                         if ($payment === null && $paymentItem->isPaid()) {
                             $payment = $paymentItem;
                         }
                     } else {
                         $paymentOuter = $paymentItem;
                         if ($payment === null && $paymentItem->isPaid()) {
                             $payment = $paymentItem;
                         }
                     }
                 }
                 if ($payment === null) {
                     if ($paymentOuter !== null) {
                         $payment = $paymentOuter;
                     } else {
                         $payment = $paymentInner;
                     }
                 }
                 if ($payment === null) {
                     return $result;
                 }
                 $paymentFields = static::convertDateFields($fields, static::getPaymentDateFields());
                 if (!empty($paymentFields['PAY_SYSTEM_ID']) && $paymentFields['PAY_SYSTEM_ID'] != $payment->getPaymentSystemId()) {
                     if ($payment->isInner()) {
                         unset($paymentFields['PAY_SYSTEM_ID']);
                     } else {
                         $paySystemId = (int) $paymentFields['PAY_SYSTEM_ID'];
                         /** @var Sale\PaySystemService $paysystem */
                         if ($paysystem = Sale\PaySystemService::load($paySystemId)) {
                             $paymentFields['PAY_SYSTEM_NAME'] = $paysystem->getAttribute('NAME');
                         }
                     }
                 }
                 $paymentFields = static::replaceFields($paymentFields, static::getPaymentReplaceFields());
                 $paymentFields = static::clearFields($paymentFields, static::getPaymentAvailableFields());
                 /** @var Sale\Result $r */
                 $r = $payment->setFields($paymentFields);
                 if ($r->isSuccess()) {
                     $orderFields = array();
                     foreach (static::getPaymentFieldsToConvert() as $checkField) {
                         $checkOrderField = $order->getField($checkField);
                         $isDate = false;
                         if (array_key_exists($checkField, static::getPaymentDateFields())) {
                             $isDate = true;
                             $checkOrderField = static::convertDateFieldToOldFormat($order->getField($checkField));
                         }
                         if (!empty($fields[$checkField]) && $checkOrderField != trim($fields[$checkField])) {
                             $setValue = $payment->getField($checkField);
                             if ($isDate) {
                                 $setValue = static::convertDateField($checkOrderField, $payment->getField($checkField), static::getPaymentDateFields());
                             }
                             $orderFields[$checkField] = $setValue;
                         }
                     }
                     if (!empty($orderFields)) {
                         /** @var Sale\Result $r */
                         $r = $order->setFieldsNoDemand($orderFields);
                         if (!$r->isSuccess()) {
                             $result->addErrors($r->getErrors());
                         }
                     }
                 } else {
                     $result->addErrors($r->getErrors());
                 }
                 if ($result->isSuccess() && intval($paySystemId) > 0) {
                     $order = $paymentCollection->getOrder();
                     $order->setFieldNoDemand('PAY_SYSTEM_ID', $paySystemId);
                 }
             } else {
                 /*					if ($paidFull !== true)
                 					{
                 						$paySystemId = $fields['PAY_SYSTEM_ID'];
                 					}
                 
                 					if ($paidFull === false)
                 					{
                 						if (intval($paySystemId) == 0)
                 						{
                 							$paySystemId = static::getDefaultPaySystemId($order->getPersonTypeId());
                 						}
                 
                 						if ($paysystem = Sale\PaySystemService::load($paySystemId))
                 						{
                 							$payment = Sale\Payment::create($paymentCollection, $paysystem);
                 							$payment->setField('SUM', $sum);
                 							$paymentCollection->addItem($payment);
                 						}
                 					}
                 
                 					$order = $paymentCollection->getOrder();
                 					$order->setFieldNoDemand('PAY_SYSTEM_ID', $paySystemId);*/
             }
         }
         $paymentOuter = null;
         $calcSum = 0;
         /** @var Sale\Payment $payment */
         foreach ($paymentCollection as $payment) {
             $calcSum += $payment->getSum();
             if ($payment->isInner()) {
                 continue;
             }
             $paymentOuter = $payment;
         }
         if ($paymentOuter && !$paymentOuter->isPaid()) {
             if ($order->getPrice() != $calcSum) {
                 $paymentOuter->setField('SUM', $paymentOuter->getSum() + ($order->getPrice() - $calcSum));
             }
         }
         if (!$paymentOuter) {
             return $result;
         }
         $fieldsFromOrder = array('PS_STATUS', 'PS_STATUS_CODE', 'PS_STATUS_DESCRIPTION', 'PS_STATUS_MESSAGE', 'PS_SUM', 'PS_CURRENCY', 'PS_RESPONSE_DATE', 'PAY_VOUCHER_NUM', 'PAY_VOUCHER_DATE', 'DATE_PAY_BEFORE', 'DATE_BILL', 'PAY_SYSTEM_NAME', 'PAY_SYSTEM_ID', 'DATE_PAYED', 'EMP_PAYED_ID');
         foreach ($fieldsFromOrder as $fieldName) {
             if (isset($fields[$fieldName])) {
                 switch ($fieldName) {
                     case 'DATE_BILL':
                     case 'DATE_PAY_BEFORE':
                     case 'PS_RESPONSE_DATE':
                         $value = new Main\Type\DateTime($fields[$fieldName]);
                         break;
                     case 'PAY_VOUCHER_DATE':
                         $value = new Main\Type\Date($fields[$fieldName]);
                         break;
                     case 'DATE_PAYED':
                         $fieldName = 'DATE_PAID';
                         $value = new Main\Type\DateTime($fields['DATE_PAYED']);
                         break;
                     case 'EMP_PAYED_ID':
                         $fieldName = 'EMP_PAID_ID';
                         $value = $fields['EMP_PAYED_ID'];
                         break;
                     default:
                         $value = $fields[$fieldName];
                         break;
                 }
                 $paymentOuter->setFieldNoDemand($fieldName, $value);
                 if ($fieldName === 'PAY_SYSTEM_ID') {
                     $order->setFieldNoDemand('PAY_SYSTEM_ID', $value);
                     if ($paysystem = Sale\PaySystemService::load($value)) {
                         $paymentOuter->setFieldNoDemand('PAY_SYSTEM_NAME', $paysystem->getAttribute('NAME'));
                     }
                 }
             }
         }
     }
     return $result;
 }