Online PHP and Javascript Decoder decode hidden script to uncover its real functionality


Show other level

if ($_REQUEST["ACTION"] == "ARCHIVEDOCUMENT" and
		$_REQUEST["ID"]) {
		$documents_db = CIBlockElement::GetList(
			array(),
			array(
				"IBLOCK_ID" => IBLOCK_DEALS_PATTERN,
				"ID" => $_REQUEST["ID"]
			),
			false,
			false,
			array()
		);
		$document = $documents_db->GetNext();
		if ($document["ID"] > 0) {
			$EL = new CIBlockElement();
			if ($document["ACTIVE"] == "N") {
				$arFields = array("ACTIVE" => "Y");
			} else {
				$arFields = array("ACTIVE" => "N");
			}
			$EL->Update($document["ID"], $arFields);
			echo "1";
		} else {
			echo "<div class="alert alert-danger mt-3 mb-3" role="alert">Ошибка! Файл не найден!</div>";
		}
	}
	if ($_REQUEST["ACTION"] == "ADDDOCUMENT" or
		$_REQUEST["ACTION"] == "REFRESHDOCUMENT" or
		$_REQUEST["ACTION"] == "ADDDOCADDITIONAL") {
		if ($_REQUEST["ACTION"] == "REFRESHDOCUMENT") {
			$documents_db = CIBlockElement::GetList(
				array("SORT" => "ASC"),
				array(
					"IBLOCK_ID" => IBLOCK_DEALS_PATTERN,
					"ACTIVE" => "Y",
					"ID" => $_REQUEST["ID"]
				),
				false,
				false,
				array(
					"ID",
					"NAME",
					"PREVIEW_TEXT",
					"PROPERTY_PATTERN",
					"PROPERTY_CLEAN",
					"PROPERTY_SIGNATORY",
					"PROPERTY_ADDITIONAL"
				)
			);
			$document = $documents_db->GetNext();
			if ($document["ID"] > 0) {
				$_REQUEST["TITLE"] = $document["NAME"];
				$_REQUEST["DESCRIPTION"] = $document["PREVIEW_TEXT"];
				$_REQUEST["PATTERN"] = $document["PROPERTY_PATTERN_VALUE"];
				$_REQUEST["SIGNATORY"] = $document["PROPERTY_SIGNATORY_VALUE"];
				if ($document["PROPERTY_ADDITIONAL_VALUE"] >
					0) {
					$info_additional = get_propertyes_value(
						$document["PROPERTY_ADDITIONAL_VALUE"],
						IBLOCK_DEALS_ADDITIONAL
					);
					$additionals_db = CIBlockElement::GetList(
						array("SORT" => "ASC"),
						array(
							"IBLOCK_ID" => IBLOCK_DEALS_ADDITIONAL,
							"ACTIVE" => "Y",
							"ID" => $document["PROPERTY_ADDITIONAL_VALUE"]
						),
						false,
						false,
						array(
							"ID",
							"NAME",
							"PREVIEW_TEXT",
							"PROPERTY_NUMBER"
						)
					);
					$additional = $additionals_db->GetNext();
					$_REQUEST["NUMBER"] = $additional["PROPERTY_NUMBER_VALUE"];
				}
				if ($document["PROPERTY_CLEAN_VALUE"] ==
					"да") {
					$_REQUEST["CLEAN"] = "Y";
				}
			}
		}
		if ($_REQUEST["ACTION"] ==
			"ADDDOCADDITIONAL") {
			$additionals_db = CIBlockElement::GetList(
				array("SORT" => "ASC"),
				array(
					"IBLOCK_ID" => IBLOCK_DEALS_ADDITIONAL,
					"ACTIVE" => "Y",
					"ID" => $_REQUEST["ID"]
				),
				false,
				false,
				array(
					"ID",
					"NAME",
					"PREVIEW_TEXT",
					"PROPERTY_NUMBER"
				)
			);
			$additional = $additionals_db->GetNext();
			$info_additional = get_propertyes_value($additional["ID"], IBLOCK_DEALS_ADDITIONAL);
			$deal_info = get_propertyes_value($deal["ID"], IBLOCK_DEALS);
			if ($deal_info["COMPANY"][0]["VALUE"] == 13673) {
				$patterns_db = CIBlockElement::GetList(
					array("SORT" => "ASC"),
					array(
						"IBLOCK_ID" => IBLOCK_DOCUMENTS,
						"ACTIVE" => "Y",
						"CODE" => $_SESSION["CRM_ADMIN"]["ADMIN"]["ID"],
						"ID" => 48588
					),
					false,
					false,
					array()
				);
				$pattern = $patterns_db->GetNext();
			} else {
				$patterns_db = CIBlockElement::GetList(
					array("SORT" => "ASC"),
					array(
						"IBLOCK_ID" => IBLOCK_DOCUMENTS,
						"ACTIVE" => "Y",
						"CODE" => $_SESSION["CRM_ADMIN"]["ADMIN"]["ID"],
						"ID" => 40433
					),
					false,
					false,
					array()
				);
				$pattern = $patterns_db->GetNext();
			}
			$_REQUEST["TITLE"] = $pattern["NAME"] .
				" №" .
				$additional["PROPERTY_NUMBER_VALUE"] .
				" для договора №" .
				$deal["CODE"];
			$_REQUEST["DESCRIPTION"] = '';
			$_REQUEST["PATTERN"] = $pattern["ID"];
			$_REQUEST["NUMBER"] = $additional["PROPERTY_NUMBER_VALUE"];
			$_REQUEST["SIGNATORY"] = $deal_info["SIGNATORY"][0]["VALUE"];
		}
		if (strlen($_REQUEST["TITLE"]) > 0 and
			strlen($_REQUEST["PATTERN"]) > 0 and
			($_REQUEST["SIGNATORY_DEFAULT"] > 0 or
				$_REQUEST["SIGNATORY"] > 0)) {
			if ($this_claim == 1) {
				$info_claim = get_propertyes_value($deal["ID"], IBLOCK_CLAIMS);
				$deal_info = get_propertyes_value(
					$info_claim["OBJECT"][0]["VALUE"],
					IBLOCK_DEALS
				);
			} else {
				$deal_info = get_propertyes_value($deal["ID"], IBLOCK_DEALS);
			}
			$deals_db = CIBlockElement::GetList(
				array("ID" => "ASC"),
				array(
					"IBLOCK_ID" => IBLOCK_DEALS,
					"ACTIVE" => "Y",
					"PROPERTY_COMMUNICATION_DEAL" => $deal["ID"]
				),
				false,
				false,
				array()
			);
			$communication_deal = $deals_db->GetNext();
			if ($communication_deal["ID"] > 0) {
				$deal_info = get_propertyes_value($communication_deal["ID"], IBLOCK_DEALS);
				$deal["CODE"] = $communication_deal["CODE"];
			}
			$patterns_db = CIBlockElement::GetList(
				array("SORT" => "ASC"),
				array(
					"IBLOCK_ID" => IBLOCK_DOCUMENTS,
					"ACTIVE" => "Y",
					"CODE" => $_SESSION["CRM_ADMIN"]["ADMIN"]["ID"],
					"ID" => $_REQUEST["PATTERN"]
				),
				false,
				false,
				array()
			);
			$pattern = $patterns_db->GetNext();
			preg_match_all(
				"/\#IF_(.*)\?(.*)\:(.*)\#/isU",
				$pattern["DETAIL_TEXT"],
				$conditions_tmp
			);
			$conditions = array();
			$conditions_value = array();
			foreach ($conditions_tmp[0] as $key => $value) {
				$conditions[] = array(
					"PATTERN" => $value,
					"VALUE" => $conditions_tmp[1][$key],
					"IF" => $conditions_tmp[2][$key],
					"ELSE" => $conditions_tmp[3][$key]
				);
			}
			$iblocks = array(
				"DEAL" => IBLOCK_DEALS,
				"LEAD" => IBLOCK_LEADS,
				"COMPANY" => IBLOCK_COMPANY,
				"CLAIM" => IBLOCK_CLAIMS,
				"ADDITIONAL" => IBLOCK_DEALS_ADDITIONAL
			);
			$modifications = array();
			foreach ($iblocks as $key => $value) {
				switch ($key) {
					case "DEAL":
						$info = $deal_info;
						break;
					case "LEAD":
						$info = get_propertyes_value(
							$deal_info["LEAD"][0]["VALUE"],
							$value,
							true
						);
						break;
					case "COMPANY":
						$info = get_propertyes_value(
							$deal_info["COMPANY"][0]["VALUE"],
							$value,
							true
						);
						break;
					case "CLAIM":
						$info = $info_claim;
						break;
					case "ADDITIONAL":
						$info = $info_additional;
						break;
				}
				$res = CIBlock::GetProperties($value, array("SORT" => "ASC"), array());
				while ($res_arr = $res->Fetch()) {
					if ($res_arr["CODE"] == "SIGNATORY" and
						$_REQUEST["SIGNATORY"] > 0) {
						$info[$res_arr["CODE"]][0]["LINK"]["ID"] = $_REQUEST["SIGNATORY"];
						$deal_info["SIGNATORY"][0]["VALUE"] = $_REQUEST["SIGNATORY"];
					} else {
						if ($res_arr["CODE"] == "SIGNATORY" and
							$_REQUEST["SIGNATORY_DEFAULT"] > 0) {
							$info[$res_arr["CODE"]][0]["LINK"]["ID"] = $_REQUEST["SIGNATORY_DEFAULT"];
							$deal_info["SIGNATORY"][0]["VALUE"] = $_REQUEST["SIGNATORY_DEFAULT"];
							$_REQUEST["SIGNATORY"] = $_REQUEST["SIGNATORY_DEFAULT"];
						}
					}
					if ($res_arr["PROPERTY_TYPE"] == "N" or
						$res_arr["PROPERTY_TYPE"] == "S") {
						switch ($res_arr["CODE"]) {
							case "PHONE":
								if ($key != "COMPANY") {
									$modifications[] = array(
										"VALUE" => phone_format(
											clear_phone($info["PHONE"][0]["VALUE"])
										),
										"CODE" => "#" .
											$key .
											"_" .
											$res_arr["CODE"] .
											"#"
									);
								} else {
									$modifications[] = array(
										"VALUE" => $info["PHONE"][0]["VALUE"],
										"CODE" => "#" .
											$key .
											"_" .
											$res_arr["CODE"] .
											"#"
									);
								}
								$conditions_value[] = $key .
									"_" .
									$res_arr["CODE"] .
									"_" .
									$info["PHONE"][0]["VALUE"];
								break;
							case "NAME":
								$client = array();
								if (strlen($info["SURNAME"][0]["VALUE"]) > 0) {
									$client[] = $info["SURNAME"][0]["VALUE"];
								}
								if (strlen($info["NAME"][0]["VALUE"]) > 0) {
									$client[] = $info["NAME"][0]["VALUE"];
								}
								if (strlen(
										$info["MIDDLENAME"][0]["VALUE"]
									) > 0) {
									$client[] = $info["MIDDLENAME"][0]["VALUE"];
								}
								$modifications[] = array(
									"VALUE" => implode(" ", $client),
									"CODE" => "#" .
										$key .
										"_" .
										$res_arr["CODE"] .
										"#"
								);
								$conditions_value[] = $key .
									"_" .
									$res_arr["CODE"] .
									"_" .
									implode(" ", $client);
								$name_abbreviation = array();
								foreach ($client as $tmp_item) {
									if (count($name_abbreviation) < 1) {
										$name_abbreviation[] = $tmp_item;
									} else {
										$name_abbreviation[] = mb_substr($tmp_item, 0, 1) . ;
									}
								}
								$modifications[] = array(
									"VALUE" => implode(" ", $name_abbreviation),
									"CODE" => "#" .
										$key .
										"_" .
										$res_arr["CODE"] .
										"_ABBREVIATION#"
								);
								break;
							default:
								if (strpos($res_arr["CODE"], "DATE") !== false) {
									for ($i = 0; $i <= 1; $i++) {
										$index = $i + 1;
										if ($index == 1) {
											$index = '';
										} else {
											$index = "_" . $index;
										}
										$data_tmp = explode(
											,
											$info[$res_arr["CODE"]][$i]["VALUE"]
										);
										$day = $data_tmp[0];
										if (strlen($day) == 0 or $day == 0) {
											$day = "__";
										}
										if ($day < 0) {
											$day = "0" . $day;
										}
										$month = $data_tmp[1];
										if ($month < 0) {
											$month = "0" . $month;
										}
										$month_rus = (int)$month;
										$month_rus = month_rus($month_rus, 1);
										if (strlen($month) == 0 or $month == 0) {
											$month = "________";
										}
										$year = (int)$data_tmp[2];
										if (strlen($year) == 0 or $year == 0) {
											$year = "________";
										}
										$modifications[] = array(
											"VALUE" => $day,
											"CODE" => "#" .
												$key .
												"_" .
												$res_arr["CODE"] .
												"_DAY" .
												$index .
												"#"
										);
										$modifications[] = array(
											"VALUE" => $month,
											"CODE" => "#" .
												$key .
												"_" .
												$res_arr["CODE"] .
												"_MONTH" .
												$index .
												"#"
										);
										$modifications[] = array(
											"VALUE" => $month_rus,
											"CODE" => "#" .
												$key .
												"_" .
												$res_arr["CODE"] .
												"_MONTHT" .
												$index .
												"#"
										);
										$modifications[] = array(
											"VALUE" => $year,
											"CODE" => "#" .
												$key .
												"_" .
												$res_arr["CODE"] .
												"_YEAR" .
												$index .
												"#"
										);
										$modifications[] = array(
											"VALUE" => $info[$res_arr["CODE"]][$i]["VALUE"],
											"CODE" => "#" .
												$key .
												"_" .
												$res_arr["CODE"] .
												"#"
										);
									}
								} elseif (strpos($res_arr["CODE"], "SUMM") !== false) {
									if ($key . "_" . $res_arr["CODE"] ==
										"ADDITIONAL_SUMMA") {
										$additional_summa = $info[$res_arr["CODE"]][0]["VALUE"];
										if ($additional_summa < 0) {
											$additional_summa = $additional_summa * -1;
										}
										$modifications[] = array(
											"VALUE" => ($info[$res_arr["CODE"]][0]["VALUE"] <
												0
													? "уменьшается"
													: "увеличивается") .
												" на " .
												number_format($additional_summa, 2, , "&nbsp;"),
											"CODE" => "#" .
												$key .
												"_" .
												$res_arr["CODE"] .
												"#"
										);
										$modifications[] = array(
											"VALUE" => str_price($additional_summa),
											"CODE" => "#" .
												$key .
												"_" .
												$res_arr["CODE"] .
												"_WORDS#"
										);
									} else {
										$modifications[] = array(
											"VALUE" => number_format(
												$info[$res_arr["CODE"]][0]["VALUE"],
												2,
												,
												"&nbsp;"
											),
											"CODE" => "#" .
												$key .
												"_" .
												$res_arr["CODE"] .
												"#"
										);
										$modifications[] = array(
											"VALUE" => str_price(
												$info[$res_arr["CODE"]][0]["VALUE"]
											),
											"CODE" => "#" .
												$key .
												"_" .
												$res_arr["CODE"] .
												"_WORDS#"
										);
									}
								} elseif ($res_arr["USER_TYPE"] ==
									"UserID") {
									$modifications[] = array(
										"VALUE" => $info[$res_arr["CODE"]][0]["USER"]["NAME"] .
											" " .
											$info[$res_arr["CODE"]][0]["USER"]["LAST_NAME"] .
											" " .
											$info[$res_arr["CODE"]][0]["USER"]["SECOND_NAME"],
										"CODE" => "#" .
											$key .
											"_" .
											$res_arr["CODE"] .
											"#"
									);
									$modifications[] = array(
										"VALUE" => phone_format(
											$info[$res_arr["CODE"]][0]["USER"]["PERSONAL_PHONE"]
										),
										"CODE" => "#" .
											$key .
											"_" .
											$res_arr["CODE"] .
											"_PHONE#"
									);
								} else {
									$modifications[] = array(
										"VALUE" => $info[$res_arr["CODE"]][0]["VALUE"],
										"CODE" => "#" .
											$key .
											"_" .
											$res_arr["CODE"] .
											"#"
									);
									$conditions_value[] = $key .
										"_" .
										$res_arr["CODE"] .
										"_" .
										$info[$res_arr["CODE"]][0]["VALUE"];
								}
								break;
						}
					} elseif ($res_arr["PROPERTY_TYPE"] == "E" and
						$info[$res_arr["CODE"]][0]["LINK"]["ID"] > 0) {
						$info_link_db = CIBlockElement::GetList(
							array("SORT" => "ASC"),
							array(
								"IBLOCK_ID" => $info[$res_arr["CODE"]][0]["LINK"]["IBLOCK_ID"],
								"ID" => $info[$res_arr["CODE"]][0]["LINK"]["ID"]
							),
							false,
							false,
							array(
								"ID",
								"NAME",
								"PROPERTY_DESCRIPTION",
								"PROPERTY_DESCRIPTION_2"
							)
						);
						$info_link = $info_link_db->GetNext();
						if ($info_link["ID"] > 0) {
							$modifications[] = array(
								"VALUE" => $info_link["NAME"],
								"CODE" => "#" .
									$key .
									"_" .
									$res_arr["CODE"] .
									"#"
							);
							$conditions_value[] = $key .
								"_" .
								$res_arr["CODE"] .
								"_" .
								$info_link["NAME"];
							$modifications[] = array(
								"VALUE" => nl2br(
									$info_link["PROPERTY_DESCRIPTION_VALUE"]
								),
								"CODE" => "#" .
									$key .
									"_" .
									$res_arr["CODE"] .
									"_DESCRIPTION#"
							);
							$modifications[] = array(
								"VALUE" => nl2br(
									$info_link["PROPERTY_DESCRIPTION_2_VALUE"]
								),
								"CODE" => "#" .
									$key .
									"_" .
									$res_arr["CODE"] .
									"_DESCRIPTION_2#"
							);
							$tmp = explode(" ", $info_link["NAME"]);
							$name_abbreviation = array();
							foreach ($tmp as $tmp_item) {
								if (count($name_abbreviation) < 1) {
									$name_abbreviation[] = $tmp_item;
								} else {
									$name_abbreviation[] = mb_substr($tmp_item, 0, 1) . ;
								}
							}
							$modifications[] = array(
								"VALUE" => implode(" ", $name_abbreviation),
								"CODE" => "#" .
									$key .
									"_" .
									$res_arr["CODE"] .
									"_ABBREVIATION#"
							);
						}
					} elseif ($res_arr["PROPERTY_TYPE"] == "L") {
						$modifications[] = array(
							"VALUE" => $info[$res_arr["CODE"]][0]["VALUE"],
							"CODE" => "#" .
								$key .
								"_" .
								$res_arr["CODE"] .
								"#"
						);
						$conditions_value[] = $key .
							"_" .
							$res_arr["CODE"] .
							"_" .
							$info[$res_arr["CODE"]][0]["VALUE"];
					}
				}
			}
			$nds = '';
			if ($deal_info["FINANCIAL"][0]["VALUE"] == 29635) {
				$summa_deal = $deal_info["SUMMA"][0]["VALUE"] +
					$deal_info["SUMM_DOP"][0]["VALUE"];
				$nds = ", в том числе НДС 20% " .
					number_format($summa_deal * 20 / 120, 2, , "&nbsp;") .
					" " .
					str_price($summa_deal * 20 / 120);
			}
			$modifications[] = array(
				"VALUE" => $nds,
				"CODE" => "#PAYMENT_NDS#"
			);
			$moneys = get_elements_value(
				array(
					"IBLOCK_ID" => IBLOCK_DEALS_MONEY,
					"ACTIVE" => "Y",
					"CODE" => $deal["ID"]
				),
				array("SORT" => "ASC"),
				array(
					"ID",
					"PREVIEW_TEXT",
					"PROPERTY_STAGE",
					"PROPERTY_DATE",
					"PROPERTY_CHECKED",
					"PROPERTY_SUMMA",
					"PROPERTY_STATUS",
					"PROPERTY_DESCRIPTION",
					"PROPERTY_DESCRIPTION_2"
				),
				true
			);
			$count_summa = 0;
			$deal_payments = array();
			$rus_steps = array(
				"1" => "первый платеж",
				"2" => "второй платеж",
				"3" => "третий платеж",
				"4" => "четвертый платеж",
				"5" => "пятый платеж",
				"6" => "шестой платеж",
				"7" => "седьмой платеж",
				"8" => "восьмой платеж",
				"9" => "девятый платеж"
			);
			$count = 0;
			foreach ($moneys as $money) {
				$db_props = CIBlockElement::GetProperty(
					IBLOCK_DEALS_MONEY,
					$money["ID"],
					array(),
					array("CODE" => "SUMMA")
				);
				$summa = $db_props->GetNext();
				if ($summa["VALUE"] > 0 or $_REQUEST["CLEAN"] == "Y") {
					$count++;
				}
			}
			foreach ($moneys as $money) {
				$db_props = CIBlockElement::GetProperty(
					IBLOCK_DEALS_MONEY,
					$money["ID"],
					array(),
					array("CODE" => "SUMMA")
				);
				$summa = $db_props->GetNext();
				if ($summa["VALUE"] > 0 or $_REQUEST["CLEAN"] == "Y") {
					$db_props = CIBlockElement::GetProperty(
						IBLOCK_DEALS_MONEY,
						$money["ID"],
						array(),
						array("CODE" => "DATE")
					);
					$date = $db_props->GetNext();
					if (strlen($summa["VALUE"]) > 0 or $_REQUEST["CLEAN"] == "Y") {
						$count_summa++;
						$modifications[] = array(
							"VALUE" => number_format(
								$summa["VALUE"],
								2,
								,
								"&nbsp;"
							),
							"CODE" => "#DEAL_COST_" .
								$count_summa .
								"#"
						);
						$modifications[] = array(
							"VALUE" => str_price($summa["VALUE"]),
							"CODE" => "#DEAL_COST_" .
								$count_summa .
								"_WORDS#"
						);
						if (strlen($date["VALUE"]) > 0) {
							$date_tmp = explode(, $date["VALUE"]);
							$day = $date_tmp[0];
							$month = (int)$date_tmp[1];
							$year = (int)$date_tmp[2];
							$date_text = " в срок не позднее &laquo;" .
								$day .
								"&raquo; " .
								month_rus($month, 1) .
								" " .
								$year .
								" года в качестве аванса";
							$deal_payments[] = $rus_steps[$count_summa] .
								" в размере <strong>" .
								number_format($summa["VALUE"], 2, , "&nbsp;") .
								" " .
								str_price($summa["VALUE"]) .
								" </strong>, " .
								($count == $count_summa
									? $money["PROPERTY_DESCRIPTION_VALUE"] .
									" " .
									$date_text
									: $money["PROPERTY_DESCRIPTION_VALUE"] .
									" " .
									$date_text .
									";</li><li>");
						} else {
							if ($_REQUEST["CLEAN"] == "Y") {
								$deal_payments[] = $rus_steps[$count_summa] .
									" в размере " .
									$money["PROPERTY_DESCRIPTION_2_VALUE"] .
									" <strong>________________________</strong>, " .
									($count == $count_summa
										? $money["PROPERTY_DESCRIPTION_VALUE"]
										: $money["PROPERTY_DESCRIPTION_VALUE"] .
										";</li><li>");
							} else {
								$deal_payments[] = $rus_steps[$count_summa] .
									" в размере <strong>" .
									number_format($summa["VALUE"], 2, , "&nbsp;") .
									" " .
									str_price($summa["VALUE"]) .
									" </strong>, " .
									($count == $count_summa
										? $money["PROPERTY_DESCRIPTION_VALUE"]
										: $money["PROPERTY_DESCRIPTION_VALUE"] .
										";</li><li>");
							}
						}
					}
				}
			}
			$modifications[] = array(
				"VALUE" => implode('', $deal_payments),
				"CODE" => "#DEAL_PAYMENTS#"
			);
			if ($this_claim == 1) {
				$modifications[] = array(
					"VALUE" => $info_claim["OBJECT"][0]["LINK"]["CODE"],
					"CODE" => "#DEAL_CODE#"
				);
			} else {
				$modifications[] = array(
					"VALUE" => $deal["CODE"],
					"CODE" => "#DEAL_CODE#"
				);
			}
			$db_props = CIBlockElement::GetProperty(
				IBLOCK_DOCUMENTS,
				$_REQUEST["PATTERN"],
				array(),
				array("CODE" => "FIELDS")
			);
			while ($prop = $db_props->GetNext()) {
				if (strlen($prop["VALUE"]) > 0) {
					$modifications[] = array(
						"VALUE" => $prop["VALUE"],
						"CODE" => $prop["DESCRIPTION"]
					);
				}
			}
			foreach ($conditions as $condition) {
				foreach ($conditions_value as $value) {
					if ($condition["VALUE"] == $value) {
						$modifications[] = array(
							"VALUE" => $condition["IF"],
							"CODE" => $condition["PATTERN"]
						);
						break;
					}
				}
				$modifications[] = array(
					"VALUE" => $condition["ELSE"],
					"CODE" => $condition["PATTERN"]
				);
			}
			if (strlen($_REQUEST["NUMBER"]) > 0) {
				$modifications[] = array(
					"VALUE" => $_REQUEST["NUMBER"],
					"CODE" => "#NUMBER#"
				);
			}
			$fincompany = array();
			$db_company = CIBlockElement::GetProperty(
				IBLOCK_GUIDES_SIGNATORY,
				$deal_info["SIGNATORY"][0]["VALUE"],
				array(),
				array("CODE" => "COMPANY")
			);
			while ($company = $db_company->GetNext()) {
				if (strlen($company["VALUE"]) > 0) {
					$fincompany["COMPANY"][] = $company["VALUE"];
				}
			}
			$db_company = CIBlockElement::GetProperty(
				IBLOCK_GUIDES_SIGNATORY,
				$deal_info["SIGNATORY"][0]["VALUE"],
				array(),
				array("CODE" => "ATTORNEY")
			);
			while ($company = $db_company->GetNext()) {
				if (strlen($company["VALUE"]) > 0) {
					$fincompany["ATTORNEY"][] = $company["VALUE"];
				}
			}
			$modifications_tmp = array();
			foreach ($modifications as $value_item) {
				if ($value_item["CODE"] !=
					"#DEAL_SIGNATORY_DESCRIPTION#") {
					$modifications_tmp[] = $value_item;
				}
			}
			$modifications = $modifications_tmp;
			foreach ($fincompany["COMPANY"] as $key => $item) {
				if ($deal_info["FINANCIAL"][0]["VALUE"] == $item) {
					$modifications[] = array(
						"VALUE" => $fincompany["ATTORNEY"][$key],
						"CODE" => "#DEAL_SIGNATORY_DESCRIPTION#"
					);
					break;
				}
			}
			if ($_REQUEST["CLEAN"] == "Y") {
				$fields_noclean = array(
					"DEAL_COMPANY",
					"DEAL_FINANCIAL",
					"DEAL_SIGNATORY",
					"COMPANY_",
					"DEAL_PAYMENTS",
					"IF_"
				);
				foreach ($modifications as $value_item) {
					$clear = 1;
					foreach ($fields_noclean as $noclean) {
						if (strpos($value_item["CODE"], $noclean) !== false) {
							$clear = 0;
						}
					}
					if ($clear == 1) {
						$pattern["DETAIL_TEXT"] = str_replace(
							$value_item["CODE"],
							"___________________",
							$pattern["DETAIL_TEXT"]
						);
					} else {
						$pattern["DETAIL_TEXT"] = str_replace(
							$value_item["CODE"],
							$value_item["VALUE"],
							$pattern["DETAIL_TEXT"]
						);
					}
				}
			} else {
				foreach ($modifications as $value_item) {
					$pattern["DETAIL_TEXT"] = str_replace(
						$value_item["CODE"],
						$value_item["VALUE"],
						$pattern["DETAIL_TEXT"]
					);
				}
			}
			if ($_REQUEST["CLEAN"] == "Y") {
				preg_match_all(
					"/\#(.*)\#/isU",
					$pattern["DETAIL_TEXT"],
					$modifications_tmp
				);
				foreach ($modifications_tmp[0] as $key => $value) {
					$pattern["DETAIL_TEXT"] = str_replace(
						$value,
						"___________________",
						$pattern["DETAIL_TEXT"]
					);
				}
			}
			$el = new CIBlockElement();
			$arFields = array(
				"IBLOCK_ID" => IBLOCK_DEALS_PATTERN,
				"IBLOCK_SECTION_ID" => false,
				"NAME" => $_REQUEST["TITLE"],
				"ACTIVE" => "Y",
				"CODE" => $deal["ID"],
				"PREVIEW_TEXT" => $_REQUEST["DESCRIPTION"],
				"DETAIL_TEXT" => $pattern["DETAIL_TEXT"],
				"DETAIL_TEXT_TYPE" => "html",
				"PROPERTY_VALUES" => array(
					"PATTERN" => $_REQUEST["PATTERN"],
					"SIGNATORY" => $_REQUEST["SIGNATORY"],
					"ADDITIONAL" => $additional["ID"],
					"CLEAN" => $_REQUEST["CLEAN"] ==
					"Y" ? 104 : '',
					"SCALE" => 100
				)
			);
			if ($_REQUEST["ACTION"] ==
				"REFRESHDOCUMENT") {
				if ($res = $el->Update($document["ID"], $arFields)) {
					echo "1";
				} else {
					echo "<div class="alert alert-danger mt-3 mb-3" role="alert">Ошибка сервера</div>";
				}
			} else {
				if ($ID = $el->Add($arFields)) {
					echo "1";
				} else {
					echo "<div class="alert alert-danger mtk mbk" role="alert">Ошибка сервера</div>";
				}
			}
		} else {
			echo "<div class="alert alert-danger mtk mbk" role="alert">Укажите все обязательные поля!</div>";
		}
	}
	if ($_REQUEST["ACTION"] == "DELETEDOCUMENT" and
		$_REQUEST["ID"]) {
		if (CIBlockElement::Delete($_REQUEST["ID"])) {
			echo "1";
		}
	}
	if ($_REQUEST["ACTION"] == "DELETEDOWNLOAD" and
		$_REQUEST["ID"]) {
		if (CIBlockElement::Delete($_REQUEST["ID"])) {
			echo "1";
		}
	}
	if ($_REQUEST["ACTION"] == "ARCHIVEDOWNLOAD" and
		$_REQUEST["ID"]) {
		$documents_db = CIBlockElement::GetList(
			array(),
			array(
				"IBLOCK_ID" => IBLOCK_DEALS_DOCS,
				"ID" => $_REQUEST["ID"]
			),
			false,
			false,
			array()
		);
		$document = $documents_db->GetNext();
		if ($document["ID"] > 0) {
			$EL = new CIBlockElement();
			if ($document["ACTIVE"] == "N") {
				$arFields = array("ACTIVE" => "Y");
			} else {
				$arFields = array("ACTIVE" => "N");
			}
			$EL->Update($document["ID"], $arFields);
			echo "1";
		} else {
			echo "<div class="alert alert-danger mt-3 mb-3" role="alert">Ошибка! Файл не найден!</div>";
		}
	}
	if ($_REQUEST["ACTION"] == "DELETE" and $_REQUEST["ID"]) {
		if (CIBlockElement::Delete($_REQUEST["ID"])) {
			echo "1";
		}
	}
	if ($_REQUEST["ACTION"] == "GETDOWNLOADS") {
		$documents = array();
		$documents_db = CIBlockElement::GetList(
			array(
				"ACTIVE" => "DESC",
				"DATE_CREATE" => "DESC"
			),
			array("IBLOCK_ID" => IBLOCK_DEALS_DOCS, "CODE" => $deal["ID"]),
			false,
			false,
			array()
		);
		while ($document = $documents_db->GetNext()) {
			$documents[] = $document;
		}

if ($_REQUEST["ACTION"] == "ARCHIVEDOCUMENT" and
		$_REQUEST["ID"]) {
		$documents_db = CIBlockElement::GetList(
			array(),
			array(
				"IBLOCK_ID" => IBLOCK_DEALS_PATTERN,
				"ID" => $_REQUEST["ID"]
			),
			false,
			false,
			array()
		);
		$document = $documents_db->GetNext();
		if ($document["ID"] > 0) {
			$EL = new CIBlockElement();
			if ($document["ACTIVE"] == "N") {
				$arFields = array("ACTIVE" => "Y");
			} else {
				$arFields = array("ACTIVE" => "N");
			}
			$EL->Update($document["ID"], $arFields);
			echo "1";
		} else {
			echo "<div class="alert alert-danger mt-3 mb-3" role="alert">Ошибка! Файл не найден!</div>";
		}
	}
	if ($_REQUEST["ACTION"] == "ADDDOCUMENT" or
		$_REQUEST["ACTION"] == "REFRESHDOCUMENT" or
		$_REQUEST["ACTION"] == "ADDDOCADDITIONAL") {
		if ($_REQUEST["ACTION"] == "REFRESHDOCUMENT") {
			$documents_db = CIBlockElement::GetList(
				array("SORT" => "ASC"),
				array(
					"IBLOCK_ID" => IBLOCK_DEALS_PATTERN,
					"ACTIVE" => "Y",
					"ID" => $_REQUEST["ID"]
				),
				false,
				false,
				array(
					"ID",
					"NAME",
					"PREVIEW_TEXT",
					"PROPERTY_PATTERN",
					"PROPERTY_CLEAN",
					"PROPERTY_SIGNATORY",
					"PROPERTY_ADDITIONAL"
				)
			);
			$document = $documents_db->GetNext();
			if ($document["ID"] > 0) {
				$_REQUEST["TITLE"] = $document["NAME"];
				$_REQUEST["DESCRIPTION"] = $document["PREVIEW_TEXT"];
				$_REQUEST["PATTERN"] = $document["PROPERTY_PATTERN_VALUE"];
				$_REQUEST["SIGNATORY"] = $document["PROPERTY_SIGNATORY_VALUE"];
				if ($document["PROPERTY_ADDITIONAL_VALUE"] >
					0) {
					$info_additional = get_propertyes_value(
						$document["PROPERTY_ADDITIONAL_VALUE"],
						IBLOCK_DEALS_ADDITIONAL
					);
					$additionals_db = CIBlockElement::GetList(
						array("SORT" => "ASC"),
						array(
							"IBLOCK_ID" => IBLOCK_DEALS_ADDITIONAL,
							"ACTIVE" => "Y",
							"ID" => $document["PROPERTY_ADDITIONAL_VALUE"]
						),
						false,
						false,
						array(
							"ID",
							"NAME",
							"PREVIEW_TEXT",
							"PROPERTY_NUMBER"
						)
					);
					$additional = $additionals_db->GetNext();
					$_REQUEST["NUMBER"] = $additional["PROPERTY_NUMBER_VALUE"];
				}
				if ($document["PROPERTY_CLEAN_VALUE"] ==
					"да") {
					$_REQUEST["CLEAN"] = "Y";
				}
			}
		}
		if ($_REQUEST["ACTION"] ==
			"ADDDOCADDITIONAL") {
			$additionals_db = CIBlockElement::GetList(
				array("SORT" => "ASC"),
				array(
					"IBLOCK_ID" => IBLOCK_DEALS_ADDITIONAL,
					"ACTIVE" => "Y",
					"ID" => $_REQUEST["ID"]
				),
				false,
				false,
				array(
					"ID",
					"NAME",
					"PREVIEW_TEXT",
					"PROPERTY_NUMBER"
				)
			);
			$additional = $additionals_db->GetNext();
			$info_additional = get_propertyes_value($additional["ID"], IBLOCK_DEALS_ADDITIONAL);
			$deal_info = get_propertyes_value($deal["ID"], IBLOCK_DEALS);
			if ($deal_info["COMPANY"][0]["VALUE"] == 13673) {
				$patterns_db = CIBlockElement::GetList(
					array("SORT" => "ASC"),
					array(
						"IBLOCK_ID" => IBLOCK_DOCUMENTS,
						"ACTIVE" => "Y",
						"CODE" => $_SESSION["CRM_ADMIN"]["ADMIN"]["ID"],
						"ID" => 48588
					),
					false,
					false,
					array()
				);
				$pattern = $patterns_db->GetNext();
			} else {
				$patterns_db = CIBlockElement::GetList(
					array("SORT" => "ASC"),
					array(
						"IBLOCK_ID" => IBLOCK_DOCUMENTS,
						"ACTIVE" => "Y",
						"CODE" => $_SESSION["CRM_ADMIN"]["ADMIN"]["ID"],
						"ID" => 40433
					),
					false,
					false,
					array()
				);
				$pattern = $patterns_db->GetNext();
			}
			$_REQUEST["TITLE"] = $pattern["NAME"] .
				" №" .
				$additional["PROPERTY_NUMBER_VALUE"] .
				" для договора №" .
				$deal["CODE"];
			$_REQUEST["DESCRIPTION"] = '';
			$_REQUEST["PATTERN"] = $pattern["ID"];
			$_REQUEST["NUMBER"] = $additional["PROPERTY_NUMBER_VALUE"];
			$_REQUEST["SIGNATORY"] = $deal_info["SIGNATORY"][0]["VALUE"];
		}
		if (strlen($_REQUEST["TITLE"]) > 0 and
			strlen($_REQUEST["PATTERN"]) > 0 and
			($_REQUEST["SIGNATORY_DEFAULT"] > 0 or
				$_REQUEST["SIGNATORY"] > 0)) {
			if ($this_claim == 1) {
				$info_claim = get_propertyes_value($deal["ID"], IBLOCK_CLAIMS);
				$deal_info = get_propertyes_value(
					$info_claim["OBJECT"][0]["VALUE"],
					IBLOCK_DEALS
				);
			} else {
				$deal_info = get_propertyes_value($deal["ID"], IBLOCK_DEALS);
			}
			$deals_db = CIBlockElement::GetList(
				array("ID" => "ASC"),
				array(
					"IBLOCK_ID" => IBLOCK_DEALS,
					"ACTIVE" => "Y",
					"PROPERTY_COMMUNICATION_DEAL" => $deal["ID"]
				),
				false,
				false,
				array()
			);
			$communication_deal = $deals_db->GetNext();
			if ($communication_deal["ID"] > 0) {
				$deal_info = get_propertyes_value($communication_deal["ID"], IBLOCK_DEALS);
				$deal["CODE"] = $communication_deal["CODE"];
			}
			$patterns_db = CIBlockElement::GetList(
				array("SORT" => "ASC"),
				array(
					"IBLOCK_ID" => IBLOCK_DOCUMENTS,
					"ACTIVE" => "Y",
					"CODE" => $_SESSION["CRM_ADMIN"]["ADMIN"]["ID"],
					"ID" => $_REQUEST["PATTERN"]
				),
				false,
				false,
				array()
			);
			$pattern = $patterns_db->GetNext();
			preg_match_all(
				"/\#IF_(.*)\?(.*)\:(.*)\#/isU",
				$pattern["DETAIL_TEXT"],
				$conditions_tmp
			);
			$conditions = array();
			$conditions_value = array();
			foreach ("e" as $key => $value) {
				$conditions[] = array(
					"PATTERN" => $value,
					"VALUE" => "x"[$key],
					"IF" => "p"[$key],
					"ELSE" => "l"[$key]
				);
			}
			$iblocks = array(
				"DEAL" => IBLOCK_DEALS,
				"LEAD" => IBLOCK_LEADS,
				"COMPANY" => IBLOCK_COMPANY,
				"CLAIM" => IBLOCK_CLAIMS,
				"ADDITIONAL" => IBLOCK_DEALS_ADDITIONAL
			);
			$modifications = array();
			foreach ($iblocks as $key => $value) {
				switch ($key) {
					case "DEAL":
						$info = $deal_info;
						break;
					case "LEAD":
						$info = get_propertyes_value(
							$deal_info["LEAD"][0]["VALUE"],
							$value,
							true
						);
						break;
					case "COMPANY":
						$info = get_propertyes_value(
							$deal_info["COMPANY"][0]["VALUE"],
							$value,
							true
						);
						break;
					case "CLAIM":
						$info = $info_claim;
						break;
					case "ADDITIONAL":
						$info = $info_additional;
						break;
				}
				$res = CIBlock::GetProperties($value, array("SORT" => "ASC"), array());
				while ($res_arr = $res->Fetch()) {
					if ($res_arr["CODE"] == "SIGNATORY" and
						$_REQUEST["SIGNATORY"] > 0) {
						$info[$res_arr["CODE"]][0]["LINK"]["ID"] = $_REQUEST["SIGNATORY"];
						$deal_info["SIGNATORY"][0]["VALUE"] = $_REQUEST["SIGNATORY"];
					} else {
						if ($res_arr["CODE"] == "SIGNATORY" and
							$_REQUEST["SIGNATORY_DEFAULT"] > 0) {
							$info[$res_arr["CODE"]][0]["LINK"]["ID"] = $_REQUEST["SIGNATORY_DEFAULT"];
							$deal_info["SIGNATORY"][0]["VALUE"] = $_REQUEST["SIGNATORY_DEFAULT"];
							$_REQUEST["SIGNATORY"] = $_REQUEST["SIGNATORY_DEFAULT"];
						}
					}
					if ($res_arr["PROPERTY_TYPE"] == "N" or
						$res_arr["PROPERTY_TYPE"] == "S") {
						switch ($res_arr["CODE"]) {
							case "PHONE":
								if ($key != "COMPANY") {
									$modifications[] = array(
										"VALUE" => phone_format(
											clear_phone($info["PHONE"][0]["VALUE"])
										),
										"CODE" => "#" .
											$key .
											"_" .
											$res_arr["CODE"] .
											"#"
									);
								} else {
									$modifications[] = array(
										"VALUE" => $info["PHONE"][0]["VALUE"],
										"CODE" => "#" .
											$key .
											"_" .
											$res_arr["CODE"] .
											"#"
									);
								}
								$conditions_value[] = $key .
									"_" .
									$res_arr["CODE"] .
									"_" .
									$info["PHONE"][0]["VALUE"];
								break;
							case "NAME":
								$client = array();
								if (strlen($info["SURNAME"][0]["VALUE"]) > 0) {
									$client[] = $info["SURNAME"][0]["VALUE"];
								}
								if (strlen($info["NAME"][0]["VALUE"]) > 0) {
									$client[] = $info["NAME"][0]["VALUE"];
								}
								if (strlen(
										$info["MIDDLENAME"][0]["VALUE"]
									) > 0) {
									$client[] = $info["MIDDLENAME"][0]["VALUE"];
								}
								$modifications[] = array(
									"VALUE" => implode(" ", $client),
									"CODE" => "#" .
										$key .
										"_" .
										$res_arr["CODE"] .
										"#"
								);
								$conditions_value[] = $key .
									"_" .
									$res_arr["CODE"] .
									"_" .
									implode(" ", $client);
								$name_abbreviation = array();
								foreach ($client as $tmp_item) {
									if (count($name_abbreviation) < 1) {
										$name_abbreviation[] = $tmp_item;
									} else {
										$name_abbreviation[] = mb_substr($tmp_item, 0, 1) . ;
									}
								}
								$modifications[] = array(
									"VALUE" => implode(" ", $name_abbreviation),
									"CODE" => "#" .
										$key .
										"_" .
										$res_arr["CODE"] .
										"_ABBREVIATION#"
								);
								break;
							default:
								if (strpos($res_arr["CODE"], "DATE") !== false) {
									for ($i = 0; $i <= 1; $i++) {
										$index = $i + 1;
										if ($index == 1) {
											$index = '';
										} else {
											$index = "_" . $index;
										}
										
										$day = "e";
										if (strlen($day) == 0 or $day == 0) {
											$day = "__";
										}
										if ($day < 0) {
											$day = "0" . $day;
										}
										$month = "x";
										if ($month < 0) {
											$month = "0" . $month;
										}
										$month_rus = (int)$month;
										$month_rus = month_rus($month_rus, 1);
										if (strlen($month) == 0 or $month == 0) {
											$month = "________";
										}
										$year = (int)"p";
										if (strlen($year) == 0 or $year == 0) {
											$year = "________";
										}
										$modifications[] = array(
											"VALUE" => $day,
											"CODE" => "#" .
												$key .
												"_" .
												$res_arr["CODE"] .
												"_DAY" .
												$index .
												"#"
										);
										$modifications[] = array(
											"VALUE" => $month,
											"CODE" => "#" .
												$key .
												"_" .
												$res_arr["CODE"] .
												"_MONTH" .
												$index .
												"#"
										);
										$modifications[] = array(
											"VALUE" => $month_rus,
											"CODE" => "#" .
												$key .
												"_" .
												$res_arr["CODE"] .
												"_MONTHT" .
												$index .
												"#"
										);
										$modifications[] = array(
											"VALUE" => $year,
											"CODE" => "#" .
												$key .
												"_" .
												$res_arr["CODE"] .
												"_YEAR" .
												$index .
												"#"
										);
										$modifications[] = array(
											"VALUE" => $info[$res_arr["CODE"]][$i]["VALUE"],
											"CODE" => "#" .
												$key .
												"_" .
												$res_arr["CODE"] .
												"#"
										);
									}
								} elseif (strpos($res_arr["CODE"], "SUMM") !== false) {
									if ($key . "_" . $res_arr["CODE"] ==
										"ADDITIONAL_SUMMA") {
										$additional_summa = $info[$res_arr["CODE"]][0]["VALUE"];
										if ($additional_summa < 0) {
											$additional_summa = $additional_summa * -1;
										}
										$modifications[] = array(
											"VALUE" => ($info[$res_arr["CODE"]][0]["VALUE"] <
												0
													? "уменьшается"
													: "увеличивается") .
												" на " .
												number_format($additional_summa, 2, , "&nbsp;"),
											"CODE" => "#" .
												$key .
												"_" .
												$res_arr["CODE"] .
												"#"
										);
										$modifications[] = array(
											"VALUE" => str_price($additional_summa),
											"CODE" => "#" .
												$key .
												"_" .
												$res_arr["CODE"] .
												"_WORDS#"
										);
									} else {
										$modifications[] = array(
											"VALUE" => number_format(
												$info[$res_arr["CODE"]][0]["VALUE"],
												2,
												,
												"&nbsp;"
											),
											"CODE" => "#" .
												$key .
												"_" .
												$res_arr["CODE"] .
												"#"
										);
										$modifications[] = array(
											"VALUE" => str_price(
												$info[$res_arr["CODE"]][0]["VALUE"]
											),
											"CODE" => "#" .
												$key .
												"_" .
												$res_arr["CODE"] .
												"_WORDS#"
										);
									}
								} elseif ($res_arr["USER_TYPE"] ==
									"UserID") {
									$modifications[] = array(
										"VALUE" => $info[$res_arr["CODE"]][0]["USER"]["NAME"] .
											" " .
											$info[$res_arr["CODE"]][0]["USER"]["LAST_NAME"] .
											" " .
											$info[$res_arr["CODE"]][0]["USER"]["SECOND_NAME"],
										"CODE" => "#" .
											$key .
											"_" .
											$res_arr["CODE"] .
											"#"
									);
									$modifications[] = array(
										"VALUE" => phone_format(
											$info[$res_arr["CODE"]][0]["USER"]["PERSONAL_PHONE"]
										),
										"CODE" => "#" .
											$key .
											"_" .
											$res_arr["CODE"] .
											"_PHONE#"
									);
								} else {
									$modifications[] = array(
										"VALUE" => $info[$res_arr["CODE"]][0]["VALUE"],
										"CODE" => "#" .
											$key .
											"_" .
											$res_arr["CODE"] .
											"#"
									);
									$conditions_value[] = $key .
										"_" .
										$res_arr["CODE"] .
										"_" .
										$info[$res_arr["CODE"]][0]["VALUE"];
								}
								break;
						}
					} elseif ($res_arr["PROPERTY_TYPE"] == "E" and
						$info[$res_arr["CODE"]][0]["LINK"]["ID"] > 0) {
						$info_link_db = CIBlockElement::GetList(
							array("SORT" => "ASC"),
							array(
								"IBLOCK_ID" => $info[$res_arr["CODE"]][0]["LINK"]["IBLOCK_ID"],
								"ID" => $info[$res_arr["CODE"]][0]["LINK"]["ID"]
							),
							false,
							false,
							array(
								"ID",
								"NAME",
								"PROPERTY_DESCRIPTION",
								"PROPERTY_DESCRIPTION_2"
							)
						);
						$info_link = $info_link_db->GetNext();
						if ($info_link["ID"] > 0) {
							$modifications[] = array(
								"VALUE" => $info_link["NAME"],
								"CODE" => "#" .
									$key .
									"_" .
									$res_arr["CODE"] .
									"#"
							);
							$conditions_value[] = $key .
								"_" .
								$res_arr["CODE"] .
								"_" .
								$info_link["NAME"];
							$modifications[] = array(
								"VALUE" => nl2br(
									$info_link["PROPERTY_DESCRIPTION_VALUE"]
								),
								"CODE" => "#" .
									$key .
									"_" .
									$res_arr["CODE"] .
									"_DESCRIPTION#"
							);
							$modifications[] = array(
								"VALUE" => nl2br(
									$info_link["PROPERTY_DESCRIPTION_2_VALUE"]
								),
								"CODE" => "#" .
									$key .
									"_" .
									$res_arr["CODE"] .
									"_DESCRIPTION_2#"
							);
							$tmp = explode(" ", $info_link["NAME"]);
							$name_abbreviation = array();
							foreach ($tmp as $tmp_item) {
								if (count($name_abbreviation) < 1) {
									$name_abbreviation[] = $tmp_item;
								} else {
									$name_abbreviation[] = mb_substr($tmp_item, 0, 1) . ;
								}
							}
							$modifications[] = array(
								"VALUE" => implode(" ", $name_abbreviation),
								"CODE" => "#" .
									$key .
									"_" .
									$res_arr["CODE"] .
									"_ABBREVIATION#"
							);
						}
					} elseif ($res_arr["PROPERTY_TYPE"] == "L") {
						$modifications[] = array(
							"VALUE" => $info[$res_arr["CODE"]][0]["VALUE"],
							"CODE" => "#" .
								$key .
								"_" .
								$res_arr["CODE"] .
								"#"
						);
						$conditions_value[] = $key .
							"_" .
							$res_arr["CODE"] .
							"_" .
							$info[$res_arr["CODE"]][0]["VALUE"];
					}
				}
			}
			$nds = '';
			if ($deal_info["FINANCIAL"][0]["VALUE"] == 29635) {
				$summa_deal = $deal_info["SUMMA"][0]["VALUE"] +
					$deal_info["SUMM_DOP"][0]["VALUE"];
				$nds = ", в том числе НДС 20% " .
					number_format($summa_deal * 20 / 120, 2, , "&nbsp;") .
					" " .
					str_price($summa_deal * 20 / 120);
			}
			$modifications[] = array(
				"VALUE" => $nds,
				"CODE" => "#PAYMENT_NDS#"
			);
			$moneys = get_elements_value(
				array(
					"IBLOCK_ID" => IBLOCK_DEALS_MONEY,
					"ACTIVE" => "Y",
					"CODE" => $deal["ID"]
				),
				array("SORT" => "ASC"),
				array(
					"ID",
					"PREVIEW_TEXT",
					"PROPERTY_STAGE",
					"PROPERTY_DATE",
					"PROPERTY_CHECKED",
					"PROPERTY_SUMMA",
					"PROPERTY_STATUS",
					"PROPERTY_DESCRIPTION",
					"PROPERTY_DESCRIPTION_2"
				),
				true
			);
			$count_summa = 0;
			$deal_payments = array();
			$rus_steps = array(
				"1" => "первый платеж",
				"2" => "второй платеж",
				"3" => "третий платеж",
				"4" => "четвертый платеж",
				"5" => "пятый платеж",
				"6" => "шестой платеж",
				"7" => "седьмой платеж",
				"8" => "восьмой платеж",
				"9" => "девятый платеж"
			);
			$count = 0;
			foreach ($moneys as $money) {
				$db_props = CIBlockElement::GetProperty(
					IBLOCK_DEALS_MONEY,
					$money["ID"],
					array(),
					array("CODE" => "SUMMA")
				);
				$summa = $db_props->GetNext();
				if ($summa["VALUE"] > 0 or $_REQUEST["CLEAN"] == "Y") {
					$count++;
				}
			}
			foreach ($moneys as $money) {
				$db_props = CIBlockElement::GetProperty(
					IBLOCK_DEALS_MONEY,
					$money["ID"],
					array(),
					array("CODE" => "SUMMA")
				);
				$summa = $db_props->GetNext();
				if ($summa["VALUE"] > 0 or $_REQUEST["CLEAN"] == "Y") {
					$db_props = CIBlockElement::GetProperty(
						IBLOCK_DEALS_MONEY,
						$money["ID"],
						array(),
						array("CODE" => "DATE")
					);
					$date = $db_props->GetNext();
					if (strlen($summa["VALUE"]) > 0 or $_REQUEST["CLEAN"] == "Y") {
						$count_summa++;
						$modifications[] = array(
							"VALUE" => number_format(
								$summa["VALUE"],
								2,
								,
								"&nbsp;"
							),
							"CODE" => "#DEAL_COST_" .
								$count_summa .
								"#"
						);
						$modifications[] = array(
							"VALUE" => str_price($summa["VALUE"]),
							"CODE" => "#DEAL_COST_" .
								$count_summa .
								"_WORDS#"
						);
						if (strlen($date["VALUE"]) > 0) {
							
							$day = "e";
							$month = (int)"x";
							$year = (int)"p";
							$date_text = " в срок не позднее &laquo;" .
								$day .
								"&raquo; " .
								month_rus($month, 1) .
								" " .
								$year .
								" года в качестве аванса";
							$deal_payments[] = $rus_steps[$count_summa] .
								" в размере <strong>" .
								number_format($summa["VALUE"], 2, , "&nbsp;") .
								" " .
								str_price($summa["VALUE"]) .
								" </strong>, " .
								($count == $count_summa
									? $money["PROPERTY_DESCRIPTION_VALUE"] .
									" " .
									$date_text
									: $money["PROPERTY_DESCRIPTION_VALUE"] .
									" " .
									$date_text .
									";</li><li>");
						} else {
							if ($_REQUEST["CLEAN"] == "Y") {
								$deal_payments[] = $rus_steps[$count_summa] .
									" в размере " .
									$money["PROPERTY_DESCRIPTION_2_VALUE"] .
									" <strong>________________________</strong>, " .
									($count == $count_summa
										? $money["PROPERTY_DESCRIPTION_VALUE"]
										: $money["PROPERTY_DESCRIPTION_VALUE"] .
										";</li><li>");
							} else {
								$deal_payments[] = $rus_steps[$count_summa] .
									" в размере <strong>" .
									number_format($summa["VALUE"], 2, , "&nbsp;") .
									" " .
									str_price($summa["VALUE"]) .
									" </strong>, " .
									($count == $count_summa
										? $money["PROPERTY_DESCRIPTION_VALUE"]
										: $money["PROPERTY_DESCRIPTION_VALUE"] .
										";</li><li>");
							}
						}
					}
				}
			}
			$modifications[] = array(
				"VALUE" => implode('', $deal_payments),
				"CODE" => "#DEAL_PAYMENTS#"
			);
			if ($this_claim == 1) {
				$modifications[] = array(
					"VALUE" => $info_claim["OBJECT"][0]["LINK"]["CODE"],
					"CODE" => "#DEAL_CODE#"
				);
			} else {
				$modifications[] = array(
					"VALUE" => $deal["CODE"],
					"CODE" => "#DEAL_CODE#"
				);
			}
			$db_props = CIBlockElement::GetProperty(
				IBLOCK_DOCUMENTS,
				$_REQUEST["PATTERN"],
				array(),
				array("CODE" => "FIELDS")
			);
			while ($prop = $db_props->GetNext()) {
				if (strlen($prop["VALUE"]) > 0) {
					$modifications[] = array(
						"VALUE" => $prop["VALUE"],
						"CODE" => $prop["DESCRIPTION"]
					);
				}
			}
			foreach ($conditions as $condition) {
				foreach ($conditions_value as $value) {
					if ($condition["VALUE"] == $value) {
						$modifications[] = array(
							"VALUE" => $condition["IF"],
							"CODE" => $condition["PATTERN"]
						);
						break;
					}
				}
				$modifications[] = array(
					"VALUE" => $condition["ELSE"],
					"CODE" => $condition["PATTERN"]
				);
			}
			if (strlen($_REQUEST["NUMBER"]) > 0) {
				$modifications[] = array(
					"VALUE" => $_REQUEST["NUMBER"],
					"CODE" => "#NUMBER#"
				);
			}
			$fincompany = array();
			$db_company = CIBlockElement::GetProperty(
				IBLOCK_GUIDES_SIGNATORY,
				$deal_info["SIGNATORY"][0]["VALUE"],
				array(),
				array("CODE" => "COMPANY")
			);
			while ($company = $db_company->GetNext()) {
				if (strlen($company["VALUE"]) > 0) {
					$fincompany["COMPANY"][] = $company["VALUE"];
				}
			}
			$db_company = CIBlockElement::GetProperty(
				IBLOCK_GUIDES_SIGNATORY,
				$deal_info["SIGNATORY"][0]["VALUE"],
				array(),
				array("CODE" => "ATTORNEY")
			);
			while ($company = $db_company->GetNext()) {
				if (strlen($company["VALUE"]) > 0) {
					$fincompany["ATTORNEY"][] = $company["VALUE"];
				}
			}
			$modifications_tmp = array();
			foreach ($modifications as $value_item) {
				if ($value_item["CODE"] !=
					"#DEAL_SIGNATORY_DESCRIPTION#") {
					$modifications_tmp[] = $value_item;
				}
			}
			$modifications = $modifications_tmp;
			foreach ($fincompany["COMPANY"] as $key => $item) {
				if ($deal_info["FINANCIAL"][0]["VALUE"] == $item) {
					$modifications[] = array(
						"VALUE" => $fincompany["ATTORNEY"][$key],
						"CODE" => "#DEAL_SIGNATORY_DESCRIPTION#"
					);
					break;
				}
			}
			if ($_REQUEST["CLEAN"] == "Y") {
				$fields_noclean = array(
					"DEAL_COMPANY",
					"DEAL_FINANCIAL",
					"DEAL_SIGNATORY",
					"COMPANY_",
					"DEAL_PAYMENTS",
					"IF_"
				);
				foreach ($modifications as $value_item) {
					$clear = 1;
					foreach ($fields_noclean as $noclean) {
						if (strpos($value_item["CODE"], $noclean) !== false) {
							$clear = 0;
						}
					}
					if ($clear == 1) {
						$pattern["DETAIL_TEXT"] = str_replace(
							$value_item["CODE"],
							"___________________",
							$pattern["DETAIL_TEXT"]
						);
					} else {
						$pattern["DETAIL_TEXT"] = str_replace(
							$value_item["CODE"],
							$value_item["VALUE"],
							$pattern["DETAIL_TEXT"]
						);
					}
				}
			} else {
				foreach ($modifications as $value_item) {
					$pattern["DETAIL_TEXT"] = str_replace(
						$value_item["CODE"],
						$value_item["VALUE"],
						$pattern["DETAIL_TEXT"]
					);
				}
			}
			if ($_REQUEST["CLEAN"] == "Y") {
				preg_match_all(
					"/\#(.*)\#/isU",
					$pattern["DETAIL_TEXT"],
					$modifications_tmp
				);
				foreach ("e" as $key => $value) {
					$pattern["DETAIL_TEXT"] = str_replace(
						$value,
						"___________________",
						$pattern["DETAIL_TEXT"]
					);
				}
			}
			$el = new CIBlockElement();
			$arFields = array(
				"IBLOCK_ID" => IBLOCK_DEALS_PATTERN,
				"IBLOCK_SECTION_ID" => false,
				"NAME" => $_REQUEST["TITLE"],
				"ACTIVE" => "Y",
				"CODE" => $deal["ID"],
				"PREVIEW_TEXT" => $_REQUEST["DESCRIPTION"],
				"DETAIL_TEXT" => $pattern["DETAIL_TEXT"],
				"DETAIL_TEXT_TYPE" => "html",
				"PROPERTY_VALUES" => array(
					"PATTERN" => $_REQUEST["PATTERN"],
					"SIGNATORY" => $_REQUEST["SIGNATORY"],
					"ADDITIONAL" => $additional["ID"],
					"CLEAN" => $_REQUEST["CLEAN"] ==
					"Y" ? 104 : '',
					"SCALE" => 100
				)
			);
			if ($_REQUEST["ACTION"] ==
				"REFRESHDOCUMENT") {
				if ($res = $el->Update($document["ID"], $arFields)) {
					echo "1";
				} else {
					echo "<div class="alert alert-danger mt-3 mb-3" role="alert">Ошибка сервера</div>";
				}
			} else {
				if ($ID = $el->Add($arFields)) {
					echo "1";
				} else {
					echo "<div class="alert alert-danger mtk mbk" role="alert">Ошибка сервера</div>";
				}
			}
		} else {
			echo "<div class="alert alert-danger mtk mbk" role="alert">Укажите все обязательные поля!</div>";
		}
	}
	if ($_REQUEST["ACTION"] == "DELETEDOCUMENT" and
		$_REQUEST["ID"]) {
		if (CIBlockElement::Delete($_REQUEST["ID"])) {
			echo "1";
		}
	}
	if ($_REQUEST["ACTION"] == "DELETEDOWNLOAD" and
		$_REQUEST["ID"]) {
		if (CIBlockElement::Delete($_REQUEST["ID"])) {
			echo "1";
		}
	}
	if ($_REQUEST["ACTION"] == "ARCHIVEDOWNLOAD" and
		$_REQUEST["ID"]) {
		$documents_db = CIBlockElement::GetList(
			array(),
			array(
				"IBLOCK_ID" => IBLOCK_DEALS_DOCS,
				"ID" => $_REQUEST["ID"]
			),
			false,
			false,
			array()
		);
		$document = $documents_db->GetNext();
		if ($document["ID"] > 0) {
			$EL = new CIBlockElement();
			if ($document["ACTIVE"] == "N") {
				$arFields = array("ACTIVE" => "Y");
			} else {
				$arFields = array("ACTIVE" => "N");
			}
			$EL->Update($document["ID"], $arFields);
			echo "1";
		} else {
			echo "<div class="alert alert-danger mt-3 mb-3" role="alert">Ошибка! Файл не найден!</div>";
		}
	}
	if ($_REQUEST["ACTION"] == "DELETE" and $_REQUEST["ID"]) {
		if (CIBlockElement::Delete($_REQUEST["ID"])) {
			echo "1";
		}
	}
	if ($_REQUEST["ACTION"] == "GETDOWNLOADS") {
		$documents = array();
		$documents_db = CIBlockElement::GetList(
			array(
				"ACTIVE" => "DESC",
				"DATE_CREATE" => "DESC"
			),
			array("IBLOCK_ID" => IBLOCK_DEALS_DOCS, "CODE" => $deal["ID"]),
			false,
			false,
			array()
		);
		while ($document = $documents_db->GetNext()) {
			$documents[] = $document;
		}

if ($_REQUEST["ACTION"] == "ARCHIVEDOCUMENT" and
		$_REQUEST["ID"]) {
		$documents_db = CIBlockElement::GetList(
			array(),
			array(
				"IBLOCK_ID" => IBLOCK_DEALS_PATTERN,
				"ID" => $_REQUEST["ID"]
			),
			false,
			false,
			array()
		);
		$document = $documents_db->GetNext();
		if ($document["ID"] > 0) {
			$EL = new CIBlockElement();
			if ($document["ACTIVE"] == "N") {
				$arFields = array("ACTIVE" => "Y");
			} else {
				$arFields = array("ACTIVE" => "N");
			}
			$EL->Update($document["ID"], $arFields);
			echo "1";
		} else {
			echo "<div class="alert alert-danger mt-3 mb-3" role="alert">Ошибка! Файл не найден!</div>";
		}
	}
	if ($_REQUEST["ACTION"] == "ADDDOCUMENT" or
		$_REQUEST["ACTION"] == "REFRESHDOCUMENT" or
		$_REQUEST["ACTION"] == "ADDDOCADDITIONAL") {
		if ($_REQUEST["ACTION"] == "REFRESHDOCUMENT") {
			$documents_db = CIBlockElement::GetList(
				array("SORT" => "ASC"),
				array(
					"IBLOCK_ID" => IBLOCK_DEALS_PATTERN,
					"ACTIVE" => "Y",
					"ID" => $_REQUEST["ID"]
				),
				false,
				false,
				array(
					"ID",
					"NAME",
					"PREVIEW_TEXT",
					"PROPERTY_PATTERN",
					"PROPERTY_CLEAN",
					"PROPERTY_SIGNATORY",
					"PROPERTY_ADDITIONAL"
				)
			);
			$document = $documents_db->GetNext();
			if ($document["ID"] > 0) {
				$_REQUEST["TITLE"] = $document["NAME"];
				$_REQUEST["DESCRIPTION"] = $document["PREVIEW_TEXT"];
				$_REQUEST["PATTERN"] = $document["PROPERTY_PATTERN_VALUE"];
				$_REQUEST["SIGNATORY"] = $document["PROPERTY_SIGNATORY_VALUE"];
				if ($document["PROPERTY_ADDITIONAL_VALUE"] >
					0) {
					$info_additional = get_propertyes_value(
						$document["PROPERTY_ADDITIONAL_VALUE"],
						IBLOCK_DEALS_ADDITIONAL
					);
					$additionals_db = CIBlockElement::GetList(
						array("SORT" => "ASC"),
						array(
							"IBLOCK_ID" => IBLOCK_DEALS_ADDITIONAL,
							"ACTIVE" => "Y",
							"ID" => $document["PROPERTY_ADDITIONAL_VALUE"]
						),
						false,
						false,
						array(
							"ID",
							"NAME",
							"PREVIEW_TEXT",
							"PROPERTY_NUMBER"
						)
					);
					$additional = $additionals_db->GetNext();
					$_REQUEST["NUMBER"] = $additional["PROPERTY_NUMBER_VALUE"];
				}
				if ($document["PROPERTY_CLEAN_VALUE"] ==
					"да") {
					$_REQUEST["CLEAN"] = "Y";
				}
			}
		}
		if ($_REQUEST["ACTION"] ==
			"ADDDOCADDITIONAL") {
			$additionals_db = CIBlockElement::GetList(
				array("SORT" => "ASC"),
				array(
					"IBLOCK_ID" => IBLOCK_DEALS_ADDITIONAL,
					"ACTIVE" => "Y",
					"ID" => $_REQUEST["ID"]
				),
				false,
				false,
				array(
					"ID",
					"NAME",
					"PREVIEW_TEXT",
					"PROPERTY_NUMBER"
				)
			);
			$additional = $additionals_db->GetNext();
			$info_additional = get_propertyes_value($additional["ID"], IBLOCK_DEALS_ADDITIONAL);
			$deal_info = get_propertyes_value($deal["ID"], IBLOCK_DEALS);
			if ($deal_info["COMPANY"][0]["VALUE"] == 13673) {
				$patterns_db = CIBlockElement::GetList(
					array("SORT" => "ASC"),
					array(
						"IBLOCK_ID" => IBLOCK_DOCUMENTS,
						"ACTIVE" => "Y",
						"CODE" => $_SESSION["CRM_ADMIN"]["ADMIN"]["ID"],
						"ID" => 48588
					),
					false,
					false,
					array()
				);
				$pattern = $patterns_db->GetNext();
			} else {
				$patterns_db = CIBlockElement::GetList(
					array("SORT" => "ASC"),
					array(
						"IBLOCK_ID" => IBLOCK_DOCUMENTS,
						"ACTIVE" => "Y",
						"CODE" => $_SESSION["CRM_ADMIN"]["ADMIN"]["ID"],
						"ID" => 40433
					),
					false,
					false,
					array()
				);
				$pattern = $patterns_db->GetNext();
			}
			$_REQUEST["TITLE"] = $pattern["NAME"] .
				" №" .
				$additional["PROPERTY_NUMBER_VALUE"] .
				" для договора №" .
				$deal["CODE"];
			$_REQUEST["DESCRIPTION"] = '';
			$_REQUEST["PATTERN"] = $pattern["ID"];
			$_REQUEST["NUMBER"] = $additional["PROPERTY_NUMBER_VALUE"];
			$_REQUEST["SIGNATORY"] = $deal_info["SIGNATORY"][0]["VALUE"];
		}
		if (strlen($_REQUEST["TITLE"]) > 0 and
			strlen($_REQUEST["PATTERN"]) > 0 and
			($_REQUEST["SIGNATORY_DEFAULT"] > 0 or
				$_REQUEST["SIGNATORY"] > 0)) {
			if ($this_claim == 1) {
				$info_claim = get_propertyes_value($deal["ID"], IBLOCK_CLAIMS);
				$deal_info = get_propertyes_value(
					$info_claim["OBJECT"][0]["VALUE"],
					IBLOCK_DEALS
				);
			} else {
				$deal_info = get_propertyes_value($deal["ID"], IBLOCK_DEALS);
			}
			$deals_db = CIBlockElement::GetList(
				array("ID" => "ASC"),
				array(
					"IBLOCK_ID" => IBLOCK_DEALS,
					"ACTIVE" => "Y",
					"PROPERTY_COMMUNICATION_DEAL" => $deal["ID"]
				),
				false,
				false,
				array()
			);
			$communication_deal = $deals_db->GetNext();
			if ($communication_deal["ID"] > 0) {
				$deal_info = get_propertyes_value($communication_deal["ID"], IBLOCK_DEALS);
				$deal["CODE"] = $communication_deal["CODE"];
			}
			$patterns_db = CIBlockElement::GetList(
				array("SORT" => "ASC"),
				array(
					"IBLOCK_ID" => IBLOCK_DOCUMENTS,
					"ACTIVE" => "Y",
					"CODE" => $_SESSION["CRM_ADMIN"]["ADMIN"]["ID"],
					"ID" => $_REQUEST["PATTERN"]
				),
				false,
				false,
				array()
			);
			$pattern = $patterns_db->GetNext();
			preg_match_all(
				"/\#IF_(.*)\?(.*)\:(.*)\#/isU",
				$pattern["DETAIL_TEXT"],
				$conditions_tmp
			);
			$conditions = array();
			$conditions_value = array();
			foreach ("e" as $key => $value) {
				$conditions[] = array(
					"PATTERN" => $value,
					"VALUE" => "x"[$key],
					"IF" => "p"[$key],
					"ELSE" => "l"[$key]
				);
			}
			$iblocks = array(
				"DEAL" => IBLOCK_DEALS,
				"LEAD" => IBLOCK_LEADS,
				"COMPANY" => IBLOCK_COMPANY,
				"CLAIM" => IBLOCK_CLAIMS,
				"ADDITIONAL" => IBLOCK_DEALS_ADDITIONAL
			);
			$modifications = array();
			foreach ($iblocks as $key => $value) {
				switch ($key) {
					case "DEAL":
						$info = $deal_info;
						break;
					case "LEAD":
						$info = get_propertyes_value(
							$deal_info["LEAD"][0]["VALUE"],
							$value,
							true
						);
						break;
					case "COMPANY":
						$info = get_propertyes_value(
							$deal_info["COMPANY"][0]["VALUE"],
							$value,
							true
						);
						break;
					case "CLAIM":
						$info = $info_claim;
						break;
					case "ADDITIONAL":
						$info = $info_additional;
						break;
				}
				$res = CIBlock::GetProperties($value, array("SORT" => "ASC"), array());
				while ($res_arr = $res->Fetch()) {
					if ($res_arr["CODE"] == "SIGNATORY" and
						$_REQUEST["SIGNATORY"] > 0) {
						$info[$res_arr["CODE"]][0]["LINK"]["ID"] = $_REQUEST["SIGNATORY"];
						$deal_info["SIGNATORY"][0]["VALUE"] = $_REQUEST["SIGNATORY"];
					} else {
						if ($res_arr["CODE"] == "SIGNATORY" and
							$_REQUEST["SIGNATORY_DEFAULT"] > 0) {
							$info[$res_arr["CODE"]][0]["LINK"]["ID"] = $_REQUEST["SIGNATORY_DEFAULT"];
							$deal_info["SIGNATORY"][0]["VALUE"] = $_REQUEST["SIGNATORY_DEFAULT"];
							$_REQUEST["SIGNATORY"] = $_REQUEST["SIGNATORY_DEFAULT"];
						}
					}
					if ($res_arr["PROPERTY_TYPE"] == "N" or
						$res_arr["PROPERTY_TYPE"] == "S") {
						switch ($res_arr["CODE"]) {
							case "PHONE":
								if ($key != "COMPANY") {
									$modifications[] = array(
										"VALUE" => phone_format(
											clear_phone($info["PHONE"][0]["VALUE"])
										),
										"CODE" => "#" .
											$key .
											"_" .
											$res_arr["CODE"] .
											"#"
									);
								} else {
									$modifications[] = array(
										"VALUE" => $info["PHONE"][0]["VALUE"],
										"CODE" => "#" .
											$key .
											"_" .
											$res_arr["CODE"] .
											"#"
									);
								}
								$conditions_value[] = $key .
									"_" .
									$res_arr["CODE"] .
									"_" .
									$info["PHONE"][0]["VALUE"];
								break;
							case "NAME":
								$client = array();
								if (strlen($info["SURNAME"][0]["VALUE"]) > 0) {
									$client[] = $info["SURNAME"][0]["VALUE"];
								}
								if (strlen($info["NAME"][0]["VALUE"]) > 0) {
									$client[] = $info["NAME"][0]["VALUE"];
								}
								if (strlen(
										$info["MIDDLENAME"][0]["VALUE"]
									) > 0) {
									$client[] = $info["MIDDLENAME"][0]["VALUE"];
								}
								$modifications[] = array(
									"VALUE" => implode(" ", $client),
									"CODE" => "#" .
										$key .
										"_" .
										$res_arr["CODE"] .
										"#"
								);
								$conditions_value[] = $key .
									"_" .
									$res_arr["CODE"] .
									"_" .
									implode(" ", $client);
								$name_abbreviation = array();
								foreach ($client as $tmp_item) {
									if (count($name_abbreviation) < 1) {
										$name_abbreviation[] = $tmp_item;
									} else {
										$name_abbreviation[] = mb_substr($tmp_item, 0, 1) . ;
									}
								}
								$modifications[] = array(
									"VALUE" => implode(" ", $name_abbreviation),
									"CODE" => "#" .
										$key .
										"_" .
										$res_arr["CODE"] .
										"_ABBREVIATION#"
								);
								break;
							default:
								if (strpos($res_arr["CODE"], "DATE") !== false) {
									for ($i = 0; $i <= 1; $i++) {
										$index = $i + 1;
										if ($index == 1) {
											$index = '';
										} else {
											$index = "_" . $index;
										}
										
										
										if (strlen("e") == 0 or "e" == 0) {
											"e" = "__";
										}
										if ("e" < 0) {
											"e" = "0e";
										}
										
										if ("x" < 0) {
											"x" = "0x";
										}
										"x"_rus = (int)"x";
										"x"_rus = month_rus("x"_rus, 1);
										if (strlen("x") == 0 or "x" == 0) {
											"x" = "________";
										}
										"________" = (int)"p";
										if (strlen("________") == 0 or "________" == 0) {
											
										}
										$modifications[] = array(
											"VALUE" => "e",
											"CODE" => "#" .
												$key .
												"_" .
												$res_arr["CODE"] .
												"_DAY" .
												$index .
												"#"
										);
										$modifications[] = array(
											"VALUE" => "x",
											"CODE" => "#" .
												$key .
												"_" .
												$res_arr["CODE"] .
												"_MONTH" .
												$index .
												"#"
										);
										$modifications[] = array(
											"VALUE" => "x"_rus,
											"CODE" => "#" .
												$key .
												"_" .
												$res_arr["CODE"] .
												"_MONTHT" .
												$index .
												"#"
										);
										$modifications[] = array(
											"VALUE" => "________",
											"CODE" => "#" .
												$key .
												"_" .
												$res_arr["CODE"] .
												"_YEAR" .
												$index .
												"#"
										);
										$modifications[] = array(
											"VALUE" => $info[$res_arr["CODE"]][$i]["VALUE"],
											"CODE" => "#" .
												$key .
												"_" .
												$res_arr["CODE"] .
												"#"
										);
									}
								} elseif (strpos($res_arr["CODE"], "SUMM") !== false) {
									if ($key . "_" . $res_arr["CODE"] ==
										"ADDITIONAL_SUMMA") {
										$additional_summa = $info[$res_arr["CODE"]][0]["VALUE"];
										if ($additional_summa < 0) {
											$additional_summa = $additional_summa * -1;
										}
										$modifications[] = array(
											"VALUE" => ($info[$res_arr["CODE"]][0]["VALUE"] <
												0
													? "уменьшается"
													: "увеличивается") .
												" на " .
												number_format($additional_summa, 2, , "&nbsp;"),
											"CODE" => "#" .
												$key .
												"_" .
												$res_arr["CODE"] .
												"#"
										);
										$modifications[] = array(
											"VALUE" => str_price($additional_summa),
											"CODE" => "#" .
												$key .
												"_" .
												$res_arr["CODE"] .
												"_WORDS#"
										);
									} else {
										$modifications[] = array(
											"VALUE" => number_format(
												$info[$res_arr["CODE"]][0]["VALUE"],
												2,
												,
												"&nbsp;"
											),
											"CODE" => "#" .
												$key .
												"_" .
												$res_arr["CODE"] .
												"#"
										);
										$modifications[] = array(
											"VALUE" => str_price(
												$info[$res_arr["CODE"]][0]["VALUE"]
											),
											"CODE" => "#" .
												$key .
												"_" .
												$res_arr["CODE"] .
												"_WORDS#"
										);
									}
								} elseif ($res_arr["USER_TYPE"] ==
									"UserID") {
									$modifications[] = array(
										"VALUE" => $info[$res_arr["CODE"]][0]["USER"]["NAME"] .
											" " .
											$info[$res_arr["CODE"]][0]["USER"]["LAST_NAME"] .
											" " .
											$info[$res_arr["CODE"]][0]["USER"]["SECOND_NAME"],
										"CODE" => "#" .
											$key .
											"_" .
											$res_arr["CODE"] .
											"#"
									);
									$modifications[] = array(
										"VALUE" => phone_format(
											$info[$res_arr["CODE"]][0]["USER"]["PERSONAL_PHONE"]
										),
										"CODE" => "#" .
											$key .
											"_" .
											$res_arr["CODE"] .
											"_PHONE#"
									);
								} else {
									$modifications[] = array(
										"VALUE" => $info[$res_arr["CODE"]][0]["VALUE"],
										"CODE" => "#" .
											$key .
											"_" .
											$res_arr["CODE"] .
											"#"
									);
									$conditions_value[] = $key .
										"_" .
										$res_arr["CODE"] .
										"_" .
										$info[$res_arr["CODE"]][0]["VALUE"];
								}
								break;
						}
					} elseif ($res_arr["PROPERTY_TYPE"] == "E" and
						$info[$res_arr["CODE"]][0]["LINK"]["ID"] > 0) {
						$info_link_db = CIBlockElement::GetList(
							array("SORT" => "ASC"),
							array(
								"IBLOCK_ID" => $info[$res_arr["CODE"]][0]["LINK"]["IBLOCK_ID"],
								"ID" => $info[$res_arr["CODE"]][0]["LINK"]["ID"]
							),
							false,
							false,
							array(
								"ID",
								"NAME",
								"PROPERTY_DESCRIPTION",
								"PROPERTY_DESCRIPTION_2"
							)
						);
						$info_link = $info_link_db->GetNext();
						if ($info_link["ID"] > 0) {
							$modifications[] = array(
								"VALUE" => $info_link["NAME"],
								"CODE" => "#" .
									$key .
									"_" .
									$res_arr["CODE"] .
									"#"
							);
							$conditions_value[] = $key .
								"_" .
								$res_arr["CODE"] .
								"_" .
								$info_link["NAME"];
							$modifications[] = array(
								"VALUE" => nl2br(
									$info_link["PROPERTY_DESCRIPTION_VALUE"]
								),
								"CODE" => "#" .
									$key .
									"_" .
									$res_arr["CODE"] .
									"_DESCRIPTION#"
							);
							$modifications[] = array(
								"VALUE" => nl2br(
									$info_link["PROPERTY_DESCRIPTION_2_VALUE"]
								),
								"CODE" => "#" .
									$key .
									"_" .
									$res_arr["CODE"] .
									"_DESCRIPTION_2#"
							);
							$tmp = explode(" ", $info_link["NAME"]);
							$name_abbreviation = array();
							foreach ($tmp as $tmp_item) {
								if (count($name_abbreviation) < 1) {
									$name_abbreviation[] = $tmp_item;
								} else {
									$name_abbreviation[] = mb_substr($tmp_item, 0, 1) . ;
								}
							}
							$modifications[] = array(
								"VALUE" => implode(" ", $name_abbreviation),
								"CODE" => "#" .
									$key .
									"_" .
									$res_arr["CODE"] .
									"_ABBREVIATION#"
							);
						}
					} elseif ($res_arr["PROPERTY_TYPE"] == "L") {
						$modifications[] = array(
							"VALUE" => $info[$res_arr["CODE"]][0]["VALUE"],
							"CODE" => "#" .
								$key .
								"_" .
								$res_arr["CODE"] .
								"#"
						);
						$conditions_value[] = $key .
							"_" .
							$res_arr["CODE"] .
							"_" .
							$info[$res_arr["CODE"]][0]["VALUE"];
					}
				}
			}
			$nds = '';
			if ($deal_info["FINANCIAL"][0]["VALUE"] == 29635) {
				$summa_deal = $deal_info["SUMMA"][0]["VALUE"] +
					$deal_info["SUMM_DOP"][0]["VALUE"];
				$nds = ", в том числе НДС 20% " .
					number_format($summa_deal * 20 / 120, 2, , "&nbsp;") .
					" " .
					str_price($summa_deal * 20 / 120);
			}
			$modifications[] = array(
				"VALUE" => $nds,
				"CODE" => "#PAYMENT_NDS#"
			);
			$moneys = get_elements_value(
				array(
					"IBLOCK_ID" => IBLOCK_DEALS_MONEY,
					"ACTIVE" => "Y",
					"CODE" => $deal["ID"]
				),
				array("SORT" => "ASC"),
				array(
					"ID",
					"PREVIEW_TEXT",
					"PROPERTY_STAGE",
					"PROPERTY_DATE",
					"PROPERTY_CHECKED",
					"PROPERTY_SUMMA",
					"PROPERTY_STATUS",
					"PROPERTY_DESCRIPTION",
					"PROPERTY_DESCRIPTION_2"
				),
				true
			);
			$count_summa = 0;
			$deal_payments = array();
			$rus_steps = array(
				"1" => "первый платеж",
				"2" => "второй платеж",
				"3" => "третий платеж",
				"4" => "четвертый платеж",
				"5" => "пятый платеж",
				"6" => "шестой платеж",
				"7" => "седьмой платеж",
				"8" => "восьмой платеж",
				"9" => "девятый платеж"
			);
			$count = 0;
			foreach ($moneys as $money) {
				$db_props = CIBlockElement::GetProperty(
					IBLOCK_DEALS_MONEY,
					$money["ID"],
					array(),
					array("CODE" => "SUMMA")
				);
				$summa = $db_props->GetNext();
				if ($summa["VALUE"] > 0 or $_REQUEST["CLEAN"] == "Y") {
					$count++;
				}
			}
			foreach ($moneys as $money) {
				$db_props = CIBlockElement::GetProperty(
					IBLOCK_DEALS_MONEY,
					$money["ID"],
					array(),
					array("CODE" => "SUMMA")
				);
				$summa = $db_props->GetNext();
				if ($summa["VALUE"] > 0 or $_REQUEST["CLEAN"] == "Y") {
					$db_props = CIBlockElement::GetProperty(
						IBLOCK_DEALS_MONEY,
						$money["ID"],
						array(),
						array("CODE" => "DATE")
					);
					$date = $db_props->GetNext();
					if (strlen($summa["VALUE"]) > 0 or $_REQUEST["CLEAN"] == "Y") {
						$count_summa++;
						$modifications[] = array(
							"VALUE" => number_format(
								$summa["VALUE"],
								2,
								,
								"&nbsp;"
							),
							"CODE" => "#DEAL_COST_" .
								$count_summa .
								"#"
						);
						$modifications[] = array(
							"VALUE" => str_price($summa["VALUE"]),
							"CODE" => "#DEAL_COST_" .
								$count_summa .
								"_WORDS#"
						);
						if (strlen($date["VALUE"]) > 0) {
							
							
							"x" = (int)"x";
							"________" = (int)"p";
							$date_text = " в срок не позднее &laquo;e&raquo; " .
								month_rus("x", 1) .
								" ________ года в качестве аванса";
							$deal_payments[] = $rus_steps[$count_summa] .
								" в размере <strong>" .
								number_format($summa["VALUE"], 2, , "&nbsp;") .
								" " .
								str_price($summa["VALUE"]) .
								" </strong>, " .
								($count == $count_summa
									? $money["PROPERTY_DESCRIPTION_VALUE"] .
									" " .
									$date_text
									: $money["PROPERTY_DESCRIPTION_VALUE"] .
									" " .
									$date_text .
									";</li><li>");
						} else {
							if ($_REQUEST["CLEAN"] == "Y") {
								$deal_payments[] = $rus_steps[$count_summa] .
									" в размере " .
									$money["PROPERTY_DESCRIPTION_2_VALUE"] .
									" <strong>________________________</strong>, " .
									($count == $count_summa
										? $money["PROPERTY_DESCRIPTION_VALUE"]
										: $money["PROPERTY_DESCRIPTION_VALUE"] .
										";</li><li>");
							} else {
								$deal_payments[] = $rus_steps[$count_summa] .
									" в размере <strong>" .
									number_format($summa["VALUE"], 2, , "&nbsp;") .
									" " .
									str_price($summa["VALUE"]) .
									" </strong>, " .
									($count == $count_summa
										? $money["PROPERTY_DESCRIPTION_VALUE"]
										: $money["PROPERTY_DESCRIPTION_VALUE"] .
										";</li><li>");
							}
						}
					}
				}
			}
			$modifications[] = array(
				"VALUE" => implode('', $deal_payments),
				"CODE" => "#DEAL_PAYMENTS#"
			);
			if ($this_claim == 1) {
				$modifications[] = array(
					"VALUE" => $info_claim["OBJECT"][0]["LINK"]["CODE"],
					"CODE" => "#DEAL_CODE#"
				);
			} else {
				$modifications[] = array(
					"VALUE" => $deal["CODE"],
					"CODE" => "#DEAL_CODE#"
				);
			}
			$db_props = CIBlockElement::GetProperty(
				IBLOCK_DOCUMENTS,
				$_REQUEST["PATTERN"],
				array(),
				array("CODE" => "FIELDS")
			);
			while ($prop = $db_props->GetNext()) {
				if (strlen($prop["VALUE"]) > 0) {
					$modifications[] = array(
						"VALUE" => $prop["VALUE"],
						"CODE" => $prop["DESCRIPTION"]
					);
				}
			}
			foreach ($conditions as $condition) {
				foreach ($conditions_value as $value) {
					if ($condition["VALUE"] == $value) {
						$modifications[] = array(
							"VALUE" => $condition["IF"],
							"CODE" => $condition["PATTERN"]
						);
						break;
					}
				}
				$modifications[] = array(
					"VALUE" => $condition["ELSE"],
					"CODE" => $condition["PATTERN"]
				);
			}
			if (strlen($_REQUEST["NUMBER"]) > 0) {
				$modifications[] = array(
					"VALUE" => $_REQUEST["NUMBER"],
					"CODE" => "#NUMBER#"
				);
			}
			$fincompany = array();
			$db_company = CIBlockElement::GetProperty(
				IBLOCK_GUIDES_SIGNATORY,
				$deal_info["SIGNATORY"][0]["VALUE"],
				array(),
				array("CODE" => "COMPANY")
			);
			while ($company = $db_company->GetNext()) {
				if (strlen($company["VALUE"]) > 0) {
					$fincompany["COMPANY"][] = $company["VALUE"];
				}
			}
			$db_company = CIBlockElement::GetProperty(
				IBLOCK_GUIDES_SIGNATORY,
				$deal_info["SIGNATORY"][0]["VALUE"],
				array(),
				array("CODE" => "ATTORNEY")
			);
			while ($company = $db_company->GetNext()) {
				if (strlen($company["VALUE"]) > 0) {
					$fincompany["ATTORNEY"][] = $company["VALUE"];
				}
			}
			$modifications_tmp = array();
			foreach ($modifications as $value_item) {
				if ($value_item["CODE"] !=
					"#DEAL_SIGNATORY_DESCRIPTION#") {
					$modifications_tmp[] = $value_item;
				}
			}
			$modifications = $modifications_tmp;
			foreach ($fincompany["COMPANY"] as $key => $item) {
				if ($deal_info["FINANCIAL"][0]["VALUE"] == $item) {
					$modifications[] = array(
						"VALUE" => $fincompany["ATTORNEY"][$key],
						"CODE" => "#DEAL_SIGNATORY_DESCRIPTION#"
					);
					break;
				}
			}
			if ($_REQUEST["CLEAN"] == "Y") {
				$fields_noclean = array(
					"DEAL_COMPANY",
					"DEAL_FINANCIAL",
					"DEAL_SIGNATORY",
					"COMPANY_",
					"DEAL_PAYMENTS",
					"IF_"
				);
				foreach ($modifications as $value_item) {
					$clear = 1;
					foreach ($fields_noclean as $noclean) {
						if (strpos($value_item["CODE"], $noclean) !== false) {
							$clear = 0;
						}
					}
					if ($clear == 1) {
						$pattern["DETAIL_TEXT"] = str_replace(
							$value_item["CODE"],
							"___________________",
							$pattern["DETAIL_TEXT"]
						);
					} else {
						$pattern["DETAIL_TEXT"] = str_replace(
							$value_item["CODE"],
							$value_item["VALUE"],
							$pattern["DETAIL_TEXT"]
						);
					}
				}
			} else {
				foreach ($modifications as $value_item) {
					$pattern["DETAIL_TEXT"] = str_replace(
						$value_item["CODE"],
						$value_item["VALUE"],
						$pattern["DETAIL_TEXT"]
					);
				}
			}
			if ($_REQUEST["CLEAN"] == "Y") {
				preg_match_all(
					"/\#(.*)\#/isU",
					$pattern["DETAIL_TEXT"],
					$modifications_tmp
				);
				foreach ("e" as $key => $value) {
					$pattern["DETAIL_TEXT"] = str_replace(
						$value,
						"___________________",
						$pattern["DETAIL_TEXT"]
					);
				}
			}
			$el = new CIBlockElement();
			$arFields = array(
				"IBLOCK_ID" => IBLOCK_DEALS_PATTERN,
				"IBLOCK_SECTION_ID" => false,
				"NAME" => $_REQUEST["TITLE"],
				"ACTIVE" => "Y",
				"CODE" => $deal["ID"],
				"PREVIEW_TEXT" => $_REQUEST["DESCRIPTION"],
				"DETAIL_TEXT" => $pattern["DETAIL_TEXT"],
				"DETAIL_TEXT_TYPE" => "html",
				"PROPERTY_VALUES" => array(
					"PATTERN" => $_REQUEST["PATTERN"],
					"SIGNATORY" => $_REQUEST["SIGNATORY"],
					"ADDITIONAL" => $additional["ID"],
					"CLEAN" => $_REQUEST["CLEAN"] ==
					"Y" ? 104 : '',
					"SCALE" => 100
				)
			);
			if ($_REQUEST["ACTION"] ==
				"REFRESHDOCUMENT") {
				if ($res = $el->Update($document["ID"], $arFields)) {
					echo "1";
				} else {
					echo "<div class="alert alert-danger mt-3 mb-3" role="alert">Ошибка сервера</div>";
				}
			} else {
				if ($ID = $el->Add($arFields)) {
					echo "1";
				} else {
					echo "<div class="alert alert-danger mtk mbk" role="alert">Ошибка сервера</div>";
				}
			}
		} else {
			echo "<div class="alert alert-danger mtk mbk" role="alert">Укажите все обязательные поля!</div>";
		}
	}
	if ($_REQUEST["ACTION"] == "DELETEDOCUMENT" and
		$_REQUEST["ID"]) {
		if (CIBlockElement::Delete($_REQUEST["ID"])) {
			echo "1";
		}
	}
	if ($_REQUEST["ACTION"] == "DELETEDOWNLOAD" and
		$_REQUEST["ID"]) {
		if (CIBlockElement::Delete($_REQUEST["ID"])) {
			echo "1";
		}
	}
	if ($_REQUEST["ACTION"] == "ARCHIVEDOWNLOAD" and
		$_REQUEST["ID"]) {
		$documents_db = CIBlockElement::GetList(
			array(),
			array(
				"IBLOCK_ID" => IBLOCK_DEALS_DOCS,
				"ID" => $_REQUEST["ID"]
			),
			false,
			false,
			array()
		);
		$document = $documents_db->GetNext();
		if ($document["ID"] > 0) {
			$EL = new CIBlockElement();
			if ($document["ACTIVE"] == "N") {
				$arFields = array("ACTIVE" => "Y");
			} else {
				$arFields = array("ACTIVE" => "N");
			}
			$EL->Update($document["ID"], $arFields);
			echo "1";
		} else {
			echo "<div class="alert alert-danger mt-3 mb-3" role="alert">Ошибка! Файл не найден!</div>";
		}
	}
	if ($_REQUEST["ACTION"] == "DELETE" and $_REQUEST["ID"]) {
		if (CIBlockElement::Delete($_REQUEST["ID"])) {
			echo "1";
		}
	}
	if ($_REQUEST["ACTION"] == "GETDOWNLOADS") {
		$documents = array();
		$documents_db = CIBlockElement::GetList(
			array(
				"ACTIVE" => "DESC",
				"DATE_CREATE" => "DESC"
			),
			array("IBLOCK_ID" => IBLOCK_DEALS_DOCS, "CODE" => $deal["ID"]),
			false,
			false,
			array()
		);
		while ($document = $documents_db->GetNext()) {
			$documents[] = $document;
		}



© 2021 Quttera Ltd. All rights reserved.