Exemplo n.º 1
0
 /**
  * @param Order $order
  * @param $payments
  * @param bool $canSetPaid
  * @return Result
  * @throws Main\ArgumentOutOfRangeException
  * @throws Main\ObjectNotFoundException
  * @throws UserMessageException
  */
 public static function updateData(Order &$order, $payments, $canSetPaid = false)
 {
     global $USER;
     $result = new Result();
     $data['PAYMENT'] = array();
     if (!$order) {
         throw new UserMessageException('Order does not exist');
     }
     foreach ($payments as $payment) {
         $paymentId = intval($payment['PAYMENT_ID']);
         $isNew = $paymentId <= 0;
         $paymentCollection = $order->getPaymentCollection();
         /** @var \Bitrix\Sale\Payment $paymentItem */
         if ($isNew) {
             $paymentItem = $paymentCollection->createItem();
         } else {
             $paymentItem = $paymentCollection->getItemById($paymentId);
             if (!$paymentItem) {
                 throw new UserMessageException('Payment does not exist');
             }
         }
         self::$defaultFields = $paymentItem->getFieldValues();
         $isReturn = isset($payment['IS_RETURN']) && $payment['IS_RETURN'] == 'Y';
         /** @var \Bitrix\Sale\PaySystemService $paymentService */
         $paymentService = PaySystemService::load($payment['PAY_SYSTEM_ID']);
         if (!$paymentService) {
             $result->addError(new EntityError(Loc::getMessage('SALE_ORDER_PAYMENT_ERROR_PAYSYSTEM')));
         }
         $paymentFields = array('PAY_SYSTEM_ID' => $payment['PAY_SYSTEM_ID'], 'COMPANY_ID' => isset($payment['COMPANY_ID']) ? $payment['COMPANY_ID'] : 0, 'SUM' => $payment['SUM'], 'PAY_VOUCHER_NUM' => $payment['PAY_VOUCHER_NUM'], 'PAY_RETURN_NUM' => $payment['PAY_RETURN_NUM'], 'PAY_RETURN_COMMENT' => $payment['PAY_RETURN_COMMENT'], 'COMMENTS' => $payment['COMMENTS'], 'PAY_SYSTEM_NAME' => $paymentService ? $paymentService->getName() : '');
         if ($isNew) {
             $paymentFields['DATE_BILL'] = new DateTime();
         }
         if (!empty($payment['PAY_RETURN_DATE'])) {
             try {
                 $paymentFields['PAY_RETURN_DATE'] = new Date($payment['PAY_RETURN_DATE']);
             } catch (Main\ObjectException $exception) {
                 $result->addError(new EntityError(Loc::getMessage('SALE_ORDER_PAYMENT_ERROR_RETURN_DATE_FORMAT')));
             }
         }
         if (!empty($payment['PAY_VOUCHER_DATE'])) {
             try {
                 $paymentFields['PAY_VOUCHER_DATE'] = new Date($payment['PAY_VOUCHER_DATE']);
             } catch (Main\ObjectException $exception) {
                 $result->addError(new EntityError(Loc::getMessage('SALE_ORDER_PAYMENT_ERROR_VOUCHER_DATE_FORMAT')));
             }
         }
         if (isset($payment['RESPONSIBLE_ID'])) {
             $paymentFields['RESPONSIBLE_ID'] = !empty($payment['RESPONSIBLE_ID']) ? $payment['RESPONSIBLE_ID'] : $USER->GetID();
             if ($payment['RESPONSIBLE_ID'] != $paymentItem->getField('RESPONSIBLE_ID')) {
                 $paymentFields['DATE_RESPONSIBLE_ID'] = new DateTime();
                 if (!$isNew) {
                     $paymentFields['EMP_RESPONSIBLE_ID'] = $USER->GetID();
                 }
             }
         }
         if ($result->isSuccess()) {
             if ($paymentItem->getField('PAID') != $payment['PAID'] && $paymentItem->getField('IS_RETURN') == 'Y') {
                 $setResult = $paymentItem->setReturn('N');
                 if (!$setResult->isSuccess()) {
                     $result->addErrors($setResult->getErrors());
                 }
             }
             if ($isReturn && $payment['OPERATION_ID'] == 'RETURN') {
                 $setResult = $paymentItem->setReturn('Y');
                 if (!$setResult->isSuccess()) {
                     $result->addErrors($setResult->getErrors());
                 }
             }
             $setResult = $paymentItem->setFields($paymentFields);
             if (!$setResult->isSuccess()) {
                 $result->addErrors($setResult->getErrors());
             }
             if (!$canSetPaid) {
                 $setResult = $paymentItem->setPaid($payment['PAID']);
                 if (!$setResult->isSuccess()) {
                     $result->addErrors($setResult->getErrors());
                 }
             }
             if ($payment['ORDER_STATUS_ID']) {
                 $order->setField('STATUS_ID', $payment['ORDER_STATUS_ID']);
             }
         }
         $data['PAYMENT'][] = $paymentItem;
     }
     $result->setData($data);
     return $result;
 }
Exemplo n.º 2
0
 /**
  * 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;
 }
Exemplo n.º 3
0
 /**
  * @param $name
  * @param $value
  * @return Result
  * @throws Main\ArgumentOutOfRangeException
  * @throws Main\NotImplementedException
  * @throws \Exception
  */
 public function setField($name, $value)
 {
     if ($name == "PAY_SYSTEM_ID") {
         if (intval($value) > 0 && !PaySystemService::isExist($value)) {
             $result = new Result();
             $result->addError(new ResultError(Loc::getMessage('SALE_PAYMENT_WRONG_PAYMENT_SERVICE'), 'SALE_PAYMENT_WRONG_PAYMENT_SERVICE'));
             return $result;
         }
         /** @var PaymentCollection $paymentCollection */
         if (!($paymentCollection = $this->getCollection())) {
             throw new Main\ObjectNotFoundException('Entity "PaymentCollection" not found');
         }
         if (count($paymentCollection) == 1) {
             /** @var Order $order */
             if ($order = $paymentCollection->getOrder()) {
                 if (strval($order->getField('PAY_SYSTEM_ID')) == '') {
                     $order->setFieldNoDemand('PAY_SYSTEM_ID', intval($value));
                 }
             }
         }
     }
     return parent::setField($name, $value);
 }
Exemplo n.º 4
0
 /**
  * @return Payment|bool
  * @throws Main\ObjectNotFoundException
  */
 public function getInnerPayment()
 {
     /** @var Order $order */
     if (!($order = $this->getOrder())) {
         throw new Main\ObjectNotFoundException('Entity "Order" not found');
     }
     if ($paySystemId = Internals\PaySystemInner::getId()) {
         /** @var Payment $payment */
         foreach ($this->collection as $payment) {
             if ($payment->getPaymentSystemId() == $paySystemId) {
                 return $payment;
             }
         }
         /** @var PaySystemService $paySystemService */
         if ($paySystemService = PaySystemService::load($paySystemId)) {
             $payment = $this->createItem($paySystemService);
             return $payment;
         }
     }
     return false;
 }
Exemplo n.º 5
0
     $result['DATA'] = $message;
     break;
 case 16:
     $start = microtime(true);
     $res = null;
     if ($DB->type == "MYSQL") {
         $res = $DB->Query('SELECT id FROM b_sale_order_payment LIMIT 1', false);
     } elseif ($DB->type == "MSSQL") {
         $res = $DB->Query('SELECT TOP(1) id FROM b_sale_order_payment', false);
     } elseif ($DB->type == "ORACLE") {
         $res = $DB->Query('SELECT id FROM b_sale_order_payment WHERE ROWNUM=1', false);
     }
     if ($res && !$res->Fetch()) {
         $innerId = \Bitrix\Sale\Internals\PaySystemInner::getId();
         /** @var \Bitrix\Sale\PaySystemService $innerPaySystem */
         $innerPaySystem = \Bitrix\Sale\PaySystemService::load($innerId);
         $concatAccNum = $DB->type == "MSSQL" ? "o.ACCOUNT_NUMBER+'/1'" : "CONCAT(o.ACCOUNT_NUMBER, '/1')";
         if (!$DB->Query("\n\t\t\t\t\t\tINSERT INTO b_sale_order_payment\n\t\t\t\t\t\t\t(ORDER_ID, PAID, DATE_PAID, PS_STATUS, PS_STATUS_CODE, PS_STATUS_DESCRIPTION, PS_STATUS_MESSAGE, PS_SUM, PS_CURRENCY, PS_RESPONSE_DATE, SUM, CURRENCY, PAY_SYSTEM_ID, DATE_BILL, PAY_VOUCHER_NUM, PAY_VOUCHER_DATE, DATE_PAY_BEFORE, PAY_SYSTEM_NAME, RESPONSIBLE_ID, ACCOUNT_NUMBER)\n\t\t\t\t\tSELECT\n\t\t\t\t\t\to.ID, o.PAYED, o.DATE_PAYED, o.PS_STATUS, o.PS_STATUS_CODE, o.PS_STATUS_DESCRIPTION, o.PS_STATUS_MESSAGE, o.PS_SUM, o.PS_CURRENCY, o.PS_RESPONSE_DATE, o.SUM_PAID,\to.CURRENCY, " . $innerId . ", o.DATE_INSERT, o.PAY_VOUCHER_NUM, o.PAY_VOUCHER_DATE, o.DATE_PAY_BEFORE, '" . $innerPaySystem->getName() . "', o.CREATED_BY, " . $concatAccNum . "\n\t\t\t\t\tFROM b_sale_order o\n\t\t\t\t\tWHERE o.PAY_SYSTEM_ID IS NOT NULL AND o.SUM_PAID > 0", true)) {
             $error .= "<br>" . $DB->GetErrorMessage();
             \CEventLog::Add(array("SEVERITY" => "ERROR", "AUDIT_TYPE_ID" => "SALE_CONVERTER_ERROR", "MODULE_ID" => "sale", "ITEM_ID" => "-", "DESCRIPTION" => $error));
         }
         $concatAccNum = $DB->type == "MSSQL" ? "o.ACCOUNT_NUMBER+'/2'" : "CONCAT(o.ACCOUNT_NUMBER, '/2')";
         if (!$DB->Query("\n\t\t\t\t\t\tINSERT INTO b_sale_order_payment\n\t\t\t\t\t\t\t(ORDER_ID, PAID, DATE_PAID, PS_STATUS, PS_STATUS_CODE, PS_STATUS_DESCRIPTION, PS_STATUS_MESSAGE, PS_SUM, PS_CURRENCY, PS_RESPONSE_DATE, SUM, CURRENCY, PAY_SYSTEM_ID, DATE_BILL, PAY_VOUCHER_NUM, PAY_VOUCHER_DATE, DATE_PAY_BEFORE, PAY_SYSTEM_NAME, RESPONSIBLE_ID, ACCOUNT_NUMBER)\n\t\t\t\t\tSELECT\n\t\t\t\t\t\to.ID, o.PAYED, o.DATE_PAYED, o.PS_STATUS, o.PS_STATUS_CODE, o.PS_STATUS_DESCRIPTION, o.PS_STATUS_MESSAGE, o.PS_SUM, o.PS_CURRENCY, o.PS_RESPONSE_DATE, o.PRICE-o.SUM_PAID,\to.CURRENCY, o.PAY_SYSTEM_ID, o.DATE_INSERT, o.PAY_VOUCHER_NUM, o.PAY_VOUCHER_DATE, o.DATE_PAY_BEFORE, b_sale_pay_system.NAME, o.CREATED_BY, " . $concatAccNum . "\n\t\t\t\t\tFROM b_sale_order o\n\t\t\t\t\tINNER JOIN b_sale_pay_system ON b_sale_pay_system.ID=o.PAY_SYSTEM_ID\n\t\t\t\t\tWHERE o.PAY_SYSTEM_ID IS NOT NULL", true)) {
             $error .= "<br>" . $DB->GetErrorMessage();
             \CEventLog::Add(array("SEVERITY" => "ERROR", "AUDIT_TYPE_ID" => "SALE_CONVERTER_ERROR", "MODULE_ID" => "sale", "ITEM_ID" => "-", "DESCRIPTION" => $error));
         }
     }
     $end = microtime(true);
     file_put_contents($_SERVER["DOCUMENT_ROOT"] . '/bitrix/modules/sale_convert.txt', 'insert into b_sale_order_payment = ' . ($end - $start) . "\n", FILE_APPEND);
     if (empty($error)) {
         $message = Loc::getMessage('SALE_CONVERTER_AJAX_STEP_INSERT_SHIPMENT');