Example #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;
 }
 /**
  * Filling the order data from request
  *
  * @internal
  *
  * @param Sale\Order $order		Entity order.
  * @param array $fields			An array of request data.
  *
  * @return Sale\Result
  */
 public static function fillOrderFromRequest(Sale\Order $order, array $fields)
 {
     global $USER;
     $result = new Sale\Result();
     if (isset($fields['CANCELED'])) {
         if ($order->getId() > 0 && $order->getField('CANCELED') != $fields['CANCELED']) {
             if (!\CSaleOrder::CanUserCancelOrder($order->getId(), $USER->GetUserGroupArray(), $USER->GetID())) {
                 $result->addError(new Sale\ResultError(Main\Localization\Loc::getMessage('SALE_COMPATIBLE_ORDER_CANCEL_NO_PERMISSION'), 'SALE_COMPATIBLE_ORDER_CANCEL_NO_PERMISSION'));
                 return $result;
             }
             /** @var Sale\Result $r */
             $r = $order->setField('CANCELED', $fields['CANCELED']);
             if (!$r->isSuccess()) {
                 $result->addErrors($r->getErrors());
             }
         }
     }
     if (isset($fields['MARKED'])) {
         if ($order->getId() > 0 && $order->getField('MARKED') != $fields['MARKED']) {
             /** @var Sale\Result $r */
             $r = $order->setField('MARKED', $fields['MARKED']);
             if (!$r->isSuccess()) {
                 $result->addErrors($r->getErrors());
             }
         }
     }
     if ($order->getId() > 0 && !empty($fields['ACCOUNT_NUMBER']) && !empty($fields['SITE_ID'])) {
         $filter = array('filter' => array('ACCOUNT_NUMBER' => $fields['ACCOUNT_NUMBER'], '!ID' => $order->getId()), 'select' => array('ID'));
         if (($res = OrderTable::getList($filter)) && $res->fetch()) {
             $result->addError(new Sale\ResultError(Main\Localization\Loc::getMessage('SALE_COMPATIBLE_ORDER_ACCOUNT_NUMBER_ALREADY_EXISTS'), 'SALE_COMPATIBLE_ORDER_ACCOUNT_NUMBER_ALREADY_EXISTS'));
         }
     }
     return $result;
 }
Example #3
0
 /**
  * @param Order $order
  * @param array $formData
  * @param int $userId
  *
  * @return \Bitrix\Sale\Result
  */
 protected static function fillSimpleFields(Order $order, array $formData, $userId = 0)
 {
     $result = new \Bitrix\Sale\Result();
     if (isset($formData["ORDER"]["RESPONSIBLE_ID"])) {
         /** @var \Bitrix\Sale\Result $r */
         $r = $order->setField("RESPONSIBLE_ID", $formData["ORDER"]["RESPONSIBLE_ID"]);
         if (!$r->isSuccess()) {
             $result->addErrors($r->getErrors());
         }
     }
     if (isset($formData["PERSON_TYPE_ID"]) && intval($formData["PERSON_TYPE_ID"]) > 0) {
         /** @var \Bitrix\Sale\Result $r */
         $r = $order->setPersonTypeId(intval($formData['PERSON_TYPE_ID']));
     } else {
         /** @var \Bitrix\Sale\Result $r */
         $r = $order->setPersonTypeId(Blocks\OrderBuyer::getDefaultPersonType($order->getSiteId()));
     }
     if (!$r->isSuccess()) {
         $result->addErrors($r->getErrors());
     }
     if (isset($formData["ORDER"]["COMMENTS"])) {
         /** @var \Bitrix\Sale\Result $r */
         $r = $order->setField("COMMENTS", $formData["ORDER"]["COMMENTS"]);
         if (!$r->isSuccess()) {
             $result->addErrors($r->getErrors());
         }
     }
     if (isset($formData["USER_DESCRIPTION"])) {
         /** @var \Bitrix\Sale\Result $r */
         $r = $order->setField("USER_DESCRIPTION", $formData["USER_DESCRIPTION"]);
         if (!$r->isSuccess()) {
             $result->addErrors($r->getErrors());
         }
     }
     if (isset($formData["RESPONSIBLE_ID"])) {
         /** @var \Bitrix\Sale\Result $r */
         $r = $order->setField("RESPONSIBLE_ID", $formData["RESPONSIBLE_ID"]);
         if (!$r->isSuccess()) {
             $result->addErrors($r->getErrors());
         }
     }
     if (isset($formData["STATUS_ID"]) && strlen($formData["STATUS_ID"]) > 0) {
         $statusesList = \Bitrix\Sale\OrderStatus::getAllowedUserStatuses($userId, \Bitrix\Sale\OrderStatus::getInitialStatus());
         if (array_key_exists($formData["STATUS_ID"], $statusesList)) {
             /** @var \Bitrix\Sale\Result $r */
             $r = $order->setField("STATUS_ID", $formData["STATUS_ID"]);
             if (!$r->isSuccess()) {
                 $result->addErrors($r->getErrors());
             }
         }
     }
     return $result;
 }
 /**
  * Filling the order data from request
  *
  * @internal
  *
  * @param Sale\Order $order		Entity order.
  * @param array $fields			An array of request data.
  *
  * @return Sale\Result
  */
 public static function fillOrderFromRequest(Sale\Order $order, array $fields)
 {
     global $USER;
     $result = new Sale\Result();
     if (isset($fields['CANCELED'])) {
         if ($order->getId() > 0 && $order->getField('CANCELED') != $fields['CANCELED']) {
             if (!\CSaleOrder::CanUserCancelOrder($order->getId(), $USER->GetUserGroupArray(), $USER->GetID())) {
                 $result->addError(new Sale\ResultError(Main\Localization\Loc::getMessage('SALE_COMPATIBLE_ORDER_CANCEL_NO_PERMISSION'), 'SALE_COMPATIBLE_ORDER_CANCEL_NO_PERMISSION'));
                 return $result;
             }
             /** @var Sale\Result $r */
             $r = $order->setField('CANCELED', $fields['CANCELED']);
             if (!$r->isSuccess()) {
                 $result->addErrors($r->getErrors());
             }
         }
     }
     if (isset($fields['MARKED'])) {
         if ($order->getId() > 0 && $order->getField('MARKED') != $fields['MARKED']) {
             if (!\CSaleOrder::CanUserMarkOrder($order->getId(), $USER->GetUserGroupArray(), $USER->GetID())) {
                 $result->addError(new Sale\ResultError(Main\Localization\Loc::getMessage('SALE_COMPATIBLE_ORDER_MARKED_NO_PERMISSION'), 'SALE_COMPATIBLE_ORDER_MARKED_NO_PERMISSION'));
                 return $result;
             }
             /** @var Sale\Result $r */
             $r = $order->setField('MARKED', $fields['MARKED']);
             if (!$r->isSuccess()) {
                 $result->addErrors($r->getErrors());
             }
         }
     }
     return $result;
 }
Example #5
0
 /**
  * @param Order $order
  * @param array $formData
  * @param int $userId
  * @return Order
  * @throws \Bitrix\Main\ArgumentOutOfRangeException
  */
 protected static function fillSimpleFields(Order $order, array $formData, $userId = 0)
 {
     if (isset($formData["ORDER"]["RESPONSIBLE_ID"])) {
         $order->setField("RESPONSIBLE_ID", $formData["ORDER"]["RESPONSIBLE_ID"]);
     }
     if (isset($formData["PERSON_TYPE_ID"]) && $formData["PERSON_TYPE_ID"] > 0) {
         $order->setPersonTypeId($formData['PERSON_TYPE_ID']);
     }
     if (isset($formData["ORDER"]["COMMENTS"])) {
         $order->setField("COMMENTS", $formData["ORDER"]["COMMENTS"]);
     }
     if (isset($formData["USER_DESCRIPTION"])) {
         $order->setField("USER_DESCRIPTION", $formData["USER_DESCRIPTION"]);
     }
     if (isset($formData["RESPONSIBLE_ID"])) {
         $order->setField("RESPONSIBLE_ID", $formData["RESPONSIBLE_ID"]);
     }
     if (isset($formData["STATUS_ID"]) && strlen($formData["STATUS_ID"]) > 0) {
         $statusesList = \Bitrix\Sale\OrderStatus::getAllowedUserStatuses($userId, \Bitrix\Sale\OrderStatus::getInitialStatus());
         if (array_key_exists($formData["STATUS_ID"], $statusesList)) {
             $order->setField("STATUS_ID", $formData["STATUS_ID"]);
         }
     }
     return $order;
 }