<?php if (file_exists($baseD) AND file_exists($baseP) AND file_exists($fileD)) { ?>

	<?php if( $aliases_arr[1] == 'deals' or $aliases_arr[1] == 'deals-archive' ) {?>
	
		<?php if( isset($_GET['edit']) OR isset($_GET['new']) ) {?>
			<div class="panel panel-default" style="margin-top:15px;">
				<div class="panel-heading">
					<h3 class="panel-title"><i class="fa fa-fw fa-clipboard"></i> Шаблоны сделок</h3>
				</div>
				<div class="panel-body">
					<?php if(count_templs($module) > 0){ ?>
					<div class="page_block2">
						<ul class="templs_list">
							<?php 
							$dom = new DOMDocument;
							$dom->load($baseTMPL);
							$xpath = new DOMXPath($dom);
							$templs = $xpath->query('/templs/templ[mod="'.$module.'"]');
							foreach ($templs as $templ) {
								$ID		= $templ->getAttribute('id');
								$Name 	= $templ->getElementsByTagName("name")->item(0)->nodeValue;
								$Mod 	= $templ->getElementsByTagName("mod")->item(0)->nodeValue;
								echo '<li id="templ_'.$ID.'">';
								if ($GroupCheck == 'admin' or $GroupCheck == 'manager') { echo '<i class="fa fa-times-circle clr_high" title="Удалить шаблон" onclick="del_templ('.$ID.');"></i>';}
								echo '<span onclick="get_templ('.$ID.');">'.$Name.'</span></li>';
							}
							?>
						</ul>
					</div>
					<?php } else { ?>
					<center>- нет шаблонов -</center>
					<?php } ?>
				</div>
			</div>
			<p style="line-height:15px;margin:5px 0 0;color:#999;text-align:justify;"><small>Для сохранения нового шаблона обязательно заполните: Метку сделки, Описание, включите "Сохранить как шаблон"</small></p>
		<?php } else { ?>
			<?php if( $aliases_arr[1] != 'deals-archive' ) {?>
			<div class="btn-group btn-group-justified" style="margin-bottom: 20px;">
				<a href="/<?php echo $folder; ?>/deals/?new" class="btn btn-md btn-info" style="color:#fff;">Добавить</a>
				<a href="/<?php echo $folder; ?>/deals-archive" class="btn btn-md btn-default">Архив</a>
			</div>
			<?php } ?>
			
			<a href="/<?php echo $folder; ?>/payments" class="btn btn-md btn-block btn-default" style="color:#333;margin-bottom:20px;">Список всех платежей</a>

			<div class="filters">
				<form method="POST" id="filter-list">
				
					
							<div class="input-group" style="margin-bottom:20px;">
								<span class="input-group-addon">
									<input type="checkbox" name="checkYear" id="checkYear" />
									<label for="checkYear"><label>
								</span>
								<?php 
								$YearStart	= date("Y");
								$YearEnd	= date("Y",strtotime("-15 years"));
								$yearArray = range($YearStart, $YearEnd);
								?>
								<select class="form-control" name="selectYear" disabled>
									<?php if ( $aliases_arr[1] == 'all-deals') {?><option value="">-- год не указан --</option><?php } ?>
									<?
									$keys = array_keys($yearArray);
									for($i=0;$i<count($keys);$i++) {
										$value = date("Y",strtotime("- $i years"));
										echo '<option value="-'.$i.' years">за '.$value.' год</option>';
									}
									?>
								</select>
							</div>
					<div class="panel panel-default">
						<div class="panel-body">
							<?php if ( $aliases_arr[1] == 'deals-archive') {?>
							<div class="input-group">
								<span class="input-group-addon">
									<input type="checkbox" name="checkDealType" id="checkDealType" />
									<label for="checkDealType"><label>
								</span>
								<select class="form-control" name="selectDealType" disabled>
									<option value="deal-a">Все активные сделки</option>
									<option value="deal-c">Все завершенные сделки</option>
									<option value="offer">Все предложения</option>
								</select>
							</div>
							<hr/>
							<?php } ?>
							<?php
							if(isset($_POST["selectClient"]) AND isset($_POST["selectClient"])){
								$StatPrice 	= count_deals_price('deal-a', 'price', $_POST["selectClient"], $sel_year, 'all')+count_deals_price('deal-c', 'price', $_POST["selectClient"], $sel_year, 'all');
								$StatCosts 	= count_deals_price('deal-a', 'costs', $_POST["selectClient"], $sel_year, 'all')+count_deals_price('deal-c', 'costs', $_POST["selectClient"], $sel_year, 'all');
								$StatPaid 	= count_deals_price('deal-a', 'paid', $_POST["selectClient"], $sel_year, 'all')+count_deals_price('deal-c', 'paid', $_POST["selectClient"], $sel_year, 'all');
							} else {
								$StatPrice 	= count_deals_price('deal-a', 'price', '', $sel_year, 'all')+count_deals_price('deal-c', 'price', '', $sel_year, 'all');
								$StatCosts 	= count_deals_price('deal-a', 'costs', '', $sel_year, 'all')+count_deals_price('deal-c', 'costs', '', $sel_year, 'all');
								$StatPaid 	= count_deals_price('deal-a', 'paid', '', $sel_year, 'all')+count_deals_price('deal-c', 'paid', '', $sel_year, 'all');
							}
								$StatProfit = $StatPaid - $StatCosts;
							?>

							<ul class="list">
								<li><i class="fa fa-fw fa-arrow-circle-o-up"></i> <b>Доходы:<span class="filter-list-num" title="<?php echo conv_price( $StatPrice , 'long' , 'руб.' );?>"><?php if(isset( $countDealYear )){ ?><span class="filter-list-num"><?php if(isset($StatPrice)){echo conv_price( $StatPrice , 'short' , '<i class="fa fa-sm fa-rub"></i>');} else {echo '0 <i class="fa fa-sm fa-rub"></i>';}?></span><?php } ?> </span></b></li>
								<li class="clr_red"><i class="fa fa-fw fa-arrow-circle-o-down"></i> <b>Расходы:<span class="filter-list-num" title="<?php echo conv_price( $StatCosts , 'long' , 'руб.' );?>"><?php if($StatCosts > 0){echo conv_price( $StatCosts , 'short' , '<i class="fa fa-sm fa-rub"></i>');} else {echo '0 <i class="fa fa-sm fa-rub"></i>';} ?></span></b></li>
								<li class="clr_green"><i class="fa fa-fw fa-arrow-circle-up"></i> <b>Прибыль:<span class="filter-list-num" title="<?php echo conv_price( $StatProfit , 'long' , 'руб.' );?>"><?php echo conv_price( $StatProfit , 'short' , '<i class="fa fa-sm fa-rub"></i>' );?></span></b></li>
								<li><hr/></li>
								<li><b>Рентабельность:<span class="filter-list-num"><?php if($StatPrice != '0' and $StatPrice != ''){echo round(($StatPrice - $StatCosts) / $StatPrice * 100 , 1);}else{echo '0';} ?> %</span></b></li>
								<?php if ( $aliases_arr[1] == 'deals') {?>
								<li><b>Прибыль в мес.:<span class="filter-list-num" title="<?php echo conv_price( $StatProfit/$count_month , 'long' , 'руб.' );?>"><?php echo conv_price( $StatProfit/$count_month , 'short' , '<i class="fa fa-sm fa-rub"></i>' );?></span></b></li>
								<?php } ?>
							</ul>
							<hr/>
							<ul class="list">
								<?php if (file_exists($baseD) and file_exists($fileD)) { ?>
								<li onclick="javascript:document.location.href='/<?php echo $folder; ?>/search/?type=deal-c'"><i class="fa fa-fw fa-briefcase"></i> Завершенные сделки<b><span class="filter-list-num"><?php if(isset($_POST["selectClient"]) AND isset($_POST["inputPriceS"]) AND isset($_POST["inputPriceE"])){echo multi_count_deal('deal-c', $_POST["selectClient"], $sel_year, $_POST["inputPriceS"], $_POST["inputPriceE"]);} else {echo multi_count_deal('deal-c', '', $sel_year, '', '');}?></span></b></li>
								<li onclick="javascript:document.location.href='/<?php echo $folder; ?>/search/?type=deal-a'"><i class="fa fa-fw fa-handshake-o"></i> Активные сделки<b><span class="filter-list-num"><?php if(isset($_POST["selectClient"]) AND isset($_POST["inputPriceS"]) AND isset($_POST["inputPriceE"])){echo multi_count_deal('deal-a', $_POST["selectClient"], $sel_year, $_POST["inputPriceS"], $_POST["inputPriceE"]);} else {echo multi_count_deal('deal-a', '', $sel_year, '', '');}?></span></b></li>
								<li onclick="javascript:document.location.href='/<?php echo $folder; ?>/search/?type=offer'"><i class="fa fa-fw fa-file-text-o"></i> Предложения<b><span class="filter-list-num"><?php if(isset($_POST["selectClient"]) AND isset($_POST["inputPriceS"]) AND isset($_POST["inputPriceE"])){echo multi_count_deal('offer', $_POST["selectClient"], $sel_year, $_POST["inputPriceS"], $_POST["inputPriceE"]);} else {echo multi_count_deal('offer', '', $sel_year, '', '');} ?></span></b></li>
								<?php } ?>
							</ul>
							<hr/>
							<ul class="list">
								<li><b class="clr_vlow">Долги клиентов:<span class="filter-list-num" title="<?php echo conv_price(count_deals_debt_summ('', '', '', ''), 'long' , 'руб.' ); ?>"><?php echo conv_price(count_deals_debt_summ('', '', '', ''), 'short' , '<i class="fa fa-sm fa-rub"></i>' ); ?></span></b></li>
								
							</ul>
						</div>
					</div>
					
					<?php if(file_exists($baseC) and file_exists($fileC)){ ?>
					<div class="panel panel-default">
						<div class="panel-heading">
							<h3 class="panel-title"><i class="fa fa-fw fa-address-book"></i> Фильтр по клиенту</h3>
						</div>
						<div class="panel-body">
							<div class="input-group">
								<span class="input-group-addon">
									<input type="checkbox" name="checkClient" id="checkClient">
									<label for="checkClient"><label>
								</span>
								<select id="select" class="form-control" name="selectClient" id="selectClient" disabled>
									<option value="">-- клиент не указан --</option>
									<?
									$dom_clients 	= new DOMDocument;
									$dom_clients	->load($baseC);
									$xpath_clients 	= new DOMXPath($dom_clients);
									$clients 		= $xpath_clients->query('/clients/client');
									
									if ( $aliases_arr[1] == 'deals' ) {
										foreach ($clients as $client) {
											$ID 	= $client->getAttribute('id');
											if(isset($_POST["inputPriceS"]) AND isset($_POST["inputPriceE"])){
												$count 	= multi_count_deal('deal-a', $ID, $sel_year, $_POST["inputPriceS"], $_POST["inputPriceE"]) + multi_count_deal('deal-c', $ID, $sel_year, $_POST["inputPriceS"], $_POST["inputPriceE"]);
											} else {
												$count 	= multi_count_deal('deal-a', $ID, $sel_year, '', '') + multi_count_deal('deal-c', $ID, $sel_year, '', '');
											}
											if($count > 0){
												echo '<option value="' . $ID . '">' . get_client_name($ID) . ' (' . $count . ')</option>';
											}
										}
									} elseif ( $aliases_arr[1] == 'deals-archive' ) {
										foreach ($clients as $client) {
											$ID 	= $client->getAttribute('id');
											if(isset($_POST["inputPriceS"]) AND isset($_POST["inputPriceE"])){
												$count 	= multi_count_deal('', $ID, '', $_POST["inputPriceS"], $_POST["inputPriceE"]);
											} else {
												$count 	= multi_count_deal('', $ID, '', '', '');
											}
											if($count > 0){
												echo '<option value="' . $ID . '">' . get_client_name($ID) . ' (' . $count . ')</option>';
											}
										}
									} else {
										foreach ($clients as $client) {
											$ID 	= $client->getAttribute('id');
											echo '<option value="' . $ID . '">' . get_client_name($ID) . '</option>';
										}
									}
									
									?>
								</select>
							</div>
						</div>
					</div>
					<?php } ?>

					<div class="panel panel-default">
						<div class="panel-heading">
							<h3 class="panel-title"><i class="fa fa-fw fa-rub"></i> Фильтр по стоимости</h3>
						</div>
						<div class="panel-body">
							<div class="input-group">
								<span class="input-group-addon">
									<input type="checkbox" name="checkPrice" id="checkPrice">
									<label for="checkPrice"></label>
								</span>
								<input style="width:50%;" id="inputPriceS" class="form-control" name="inputPriceS" placeholder="от, руб." disabled="" />
								<input style="width:50%;" id="inputPriceE" class="form-control" name="inputPriceE" placeholder="до, руб." disabled="" />
							</div>
						</div>
					</div>
					
					<div class="btn-group btn-group-justified filter-buttons">
						<input type="submit" class="btn-filter btn btn-md btn-success" value="Фильтровать" name="DealFilter" />
						<input type="submit" class="btn-freset btn btn-md btn-default" value="Сбросить" />
					</div>
					
				</form>
			</div>
		<?php } ?>

	<?php } ?>

	<?php if($aliases_arr[1] == 'payments'){?>
		<div class="filters">
			<form method="POST" id="filter-list">
				<?php 
					if(isset($_POST['SaveFirstPayment'])){
						$dom 		= new DOMDocument;
						$dom		->load($baseS);
						$xpath 		= new DOMXPath($dom);
						$Payment 	= $xpath->query('/settings/other')->item(0)->getElementsByTagName("firstpayment")->item(0)->nodeValue = $_POST['inputFirstPayment'];
						$dom		->save($baseS);
						$notices[] 	= 'Успешно изменено';
					}
					
					if(isset($_POST['SaveFirstPayment'])){
						$firstPayment = $_POST['inputFirstPayment'];
					} else {
						$firstPayment = $nCRMfirstpayment;
					}
					$moneyBalance = $firstPayment + count_payments_val('profit', '', 'all') + count_payments_val('cost', '', 'all');
				?>
				<?php  /* ?>
				<div class="panel panel-default">
					<div class="panel-heading">
						<h3 class="panel-title"><i class="fa fa-fw fa-money"></i> Начальный баланс счета</h3>
					</div>
					<div class="panel-body">
						<div class="input-group">
							<span class="input-group-addon" style="padding:6px 12px!important;"><i class="fa fa-fw fa-rub"></i></span>
							<input type="text" class="form-control" name="inputFirstPayment" placeholder="..." value="<?php echo $firstPayment; ?>" maxlength="10" <?php if($GroupCheck != 'admin'){?>readonly<?php } ?>>
							<span class="input-group-btn">
								<button <?php if($GroupCheck == 'admin'){?>class="btn btn-success" type="submit" name="SaveFirstPayment"<?php }else{ ?>class="btn btn-default" disabled<?php } ?>><i class="fa fa-save fa-fw"></i></button>
							</span>
						</div>
					</div>
				</div>
				<?php */ ?>

				<?php /* ?>
				<div class="panel panel-default">
					<div class="panel-body">
						<ul class="list">
							<li>
								<b>Баланс счета:
									<span class="filter-list-num" title="<?php echo conv_price( $moneyBalance , 'long' , 'руб.'); ?>">
										<?php echo conv_price( $moneyBalance , 'short' , '<i class="fa fa-sm fa-rub"></i>'); ?>
									</span>
								</b>
							</li>
						</ul>
					</div>
				</div>	
				<?php */ ?>
				<div class="input-group" style="margin-bottom:20px;">
					<span class="input-group-addon">
						<input type="checkbox" name="checkYear" id="checkYear" />
						<label for="checkYear"><label>
					</span>
					<?php 
					$YearStart	= date("Y");
					$YearEnd	= date("Y",strtotime("-15 years"));
					$yearArray = range($YearStart, $YearEnd);
					?>
					<select class="form-control" name="selectYear" disabled>
						<?php if ( $aliases_arr[1] == 'all-deals') {?><option value="">-- год не указан --</option><?php } ?>
						<?
						$keys = array_keys($yearArray);
						for($i=0;$i<count($keys);$i++) {
							$value = date("Y",strtotime("- $i years"));
							echo '<option value="-'.$i.' years">за '.$value.' год</option>';
						}
						?>
					</select>
				</div>
				<div class="panel panel-default">
					<div class="panel-body">
						<ul class="list">
							<li class="clr_green">
								<i class="fa fa-fw fa-arrow-circle-up"></i> 
								<b>Доходы:
									<span class="filter-list-num" title="<?php echo conv_price( count_payments_val('profit', $sel_year, 'all') , 'long' , 'руб.' );?>">
										<?php echo conv_price( count_payments_val('profit', $sel_year, 'all') , 'short' , '<i class="fa fa-sm fa-rub"></i>'); ?>
									</span>
								</b>
							</li>
							<li class="clr_red">
								<i class="fa fa-fw fa-arrow-circle-down"></i> 
								<b>Расходы:
									<span class="filter-list-num" title="<?php echo conv_price( count_payments_val('cost', $sel_year, 'all') , 'long' , 'руб.' );?>">
										<?php echo conv_price( count_payments_val('cost', $sel_year, 'all') , 'short' , '<i class="fa fa-sm fa-rub"></i>'); ?>
									</span>
								</b>
							</li>
							<li><hr/></li>
							<li>
								<b>Доход в месяц:
									<span class="filter-list-num" title="<?php echo conv_price( count_payments_val('profit', $sel_year, 'all')/$count_month , 'long' , 'руб.' );?>">
										<?php echo conv_price( count_payments_val('profit', $sel_year, 'all')/$count_month , 'short' , '<i class="fa fa-sm fa-rub"></i>'); ?>
									</span>
								</b>
							</li>
							<li><hr/></li>
							<li>
								<b class="clr_vlow">Долги клиентов:
									<span class="filter-list-num" title="<?php echo conv_price( count_deals_debt_summ('', '', '', ''), 'long' , 'руб.' );?>">
										<?php echo conv_price(count_deals_debt_summ('', '', '', ''), 'short' , '<i class="fa fa-sm fa-rub"></i>' ); ?>
									</span>
								</b>
							</li>
						</ul>
					</div>
				</div>
				
				<div class="panel panel-default">
					<div class="panel-heading">
						<h3 class="panel-title"><i class="fa fa-fw fa-arrow-circle-up"></i> Фильтр по типу</h3>
					</div>
					<div class="panel-body">
						<ul class="filter-list">
							<li>
								<input type="radio" name="radioType" id="radioTypeAll" value="allType"/>
								<label for="radioTypeAll">Все типы</label>
								<b><span class="filter-list-num"><?php echo multi_count_payment('', $selectStatus, '', $sel_year);?></span></b>
							</li>
							<li>
								<input type="radio" name="radioType" id="radioTypeProfit" value="profit"/>
								<label for="radioTypeProfit">Доход</label>
								<b><span class="filter-list-num"><?php echo multi_count_payment('profit', $selectStatus, '', $sel_year);?></span></b>
							</li>
							<li>
								<input type="radio" name="radioType" id="radioTypeCost" value="cost"/>
								<label for="radioTypeCost">Расход</label>
								<b><span class="filter-list-num"><?php echo multi_count_payment('cost', $selectStatus, '', $sel_year);?></span></b>
							</li>
						</ul>
					</div>
				</div>
				
				<div class="panel panel-default">
					<div class="panel-heading">
						<h3 class="panel-title"><i class="fa fa-fw fa-check"></i> Фильтр по статусу</h3>
					</div>
					<div class="panel-body">
						<ul class="filter-list">
							<li>
								<input type="radio" name="radioStatus" id="radioStatusAll" value="allStatus"/>
								<label for="radioStatusAll">Все статусы</label>
								<b><span class="filter-list-num"><?php echo multi_count_payment($selectType, '', '', $sel_year);?></span></b>
							</li>
							<li>
								<input type="radio" name="radioStatus" id="radioStatusDone" value="+"/>
								<label for="radioStatusDone">Подтвержден</label>
								<b><span class="filter-list-num"><?php echo multi_count_payment($selectType, '+', '', $sel_year);?></span></b>
							</li>
							<li>
								<input type="radio" name="radioStatus" id="radioStatusNone" value="-"/>
								<label for="radioStatusNone">Не подтвержден</label>
								<b><span class="filter-list-num"><?php echo multi_count_payment($selectType, '-', '', $sel_year);?></span></b>
							</li>
						</ul>
					</div>
				</div>
				
				<div class="panel panel-default">
					<div class="panel-heading">
						<h3 class="panel-title"><i class="fa fa-fw fa-comment"></i> Фильтр по комментарию</h3>
					</div>
					<div class="panel-body">
						<div class="input-group">
							<span class="input-group-addon">
								<input type="checkbox" name="checkCom" id="checkCom">
								<label for="checkCom"><label>
							</span>
							<select id="select" class="form-control" name="selectCom" id="selectCom" disabled>
								<option value="all">-- все комментарии --</option>
								<?php
								$dom = new DOMDocument;
								$dom->load($baseP);
								$xpath = new DOMXPath($dom);
								$alltagspayment = $xpath->query('/payments/payment');
								$sourses_arr = array();
								$i = 0;
								foreach ($alltagspayment as $payment) {
									$listSourceName = $payment->getElementsByTagName("com")->item(0)->nodeValue;
									$countSource = count($xmlpayment->xpath('/payments/payment[com="'.$listSourceName.'" and datey="'.$sel_year.'"]'));
									$sourses_arr[$listSourceName] =  $countSource;
									$i++;
								}
								$sources = $sourses_arr;
								foreach ($sources as $key => $value){
									if($value > 0){
										if($key == ''){
											echo '<option value="' . $key . '">Нет комментария ('.$value.')</option>';
										} else {
											echo '<option value="' . $key . '">'.$key.' ('.$value.')</option>';
										}
									}
								}
								?>
							</select>
						</div>
					</div>
				</div>
				
				<div class="btn-group btn-group-justified filter-buttons">
					<input type="submit" class="btn-filter btn btn-md btn-success" value="Фильтровать" name="DealFilter" />
					<input type="submit" class="btn-freset btn btn-md btn-default" value="Сбросить" />
				</div>
				
			</form>
		</div>
	<?php } ?>

<?php } ?>