
use Storage;
use Fees;

my $Storage = Storage->new($db, $admin, \%conf);
my $fees = Fees->new($db, $admin, \%conf);



#***********************************************************
#  	Storage articles
#***********************************************************
sub storage_articles {

	if ($FORM{message}) {				
		$html->message('info', $_INFO, "$FORM{message}");	
	}
	$Storage->{ACTION}='add';
	$Storage->{ACTION_LNG}=$_ADD;
	$Storage->{ADD_DATE} = '0000-00-00';
	$Storage->{ARTICLE_TYPES}=$html->form_select("ARTICLE_TYPE", 
							{	SELECTED          => $FORM{ARTICLE_TYPE},
								SEL_MULTI_ARRAY   => [['', ''], @{ $Storage->storage_types_list() } ], 
								MULTI_ARRAY_KEY   => 0,
								MULTI_ARRAY_VALUE => 1,
								NO_ID             => 1
							});		
	
	if ($FORM{add}) {
		
		if ($FORM{NAME} ne '' and $FORM{ARTICLE_TYPE} != '' and $FORM{MEASURE} ne '') {
		
			$Storage->storage_articles_add({ %FORM });
			if (! $Storage->{errno}) {
				$html->tpl_show(_include('storage_redirect', 'Storage'), { 
						SECTION => '',
						MESSAGE => "$_ADDED",
					
					});		
			}
		}
		else {
			
			$html->message('info', $_INFO, "$_FIELDS_FOR_NAME_ARTICLETYPE_MEASURE_ARE_REQUIRED");
			
			$html->tpl_show(_include('storage_articles', 'Storage'), {%$Storage, %FORM});
		
		}
	}
	elsif ($FORM{del}) {
		#$Storage->{debug}=1;
		$list = $Storage->storage_incoming_articles_list( { ARTICLE_ID =>$FORM{del}} );
		if (defined($list->[0]->[0]) ){

			$html->message('info', $_INFO, "$_CANT_DELETE_ERROR1 ");
		}
		else {
			$Storage->storage_articles_del( {ID =>$FORM{del}} );
			if (! $Storage->{errno}){
				$html->message('info', $_INFO, "$_DELETED");
			}
		}	
	}
	elsif ($FORM{change}) {
		if ($FORM{NAME} ne '' and $FORM{ARTICLE_TYPE} != '' and $FORM{MEASURE} ne '') {
			#$Storage->{debug}=1;
			$Storage->storage_articles_change({ %FORM });
			if (! $Storage->{errno}) {
				$html->tpl_show(_include('storage_redirect', 'Storage'), { 
						SECTION => '',
						MESSAGE => "$_CHANGED",
					
					});	
			}
		}
		else {
			$html->message('info', $_INFO, "$_FIELDS_FOR_NAME_ARTICLETYPE_MEASURE_ARE_REQUIRED");
			$html->tpl_show(_include('storage_articles', 'Storage'), {%$Storage, %FORM});
		}
	}
	elsif ($FORM{chg}) {
		#$Storage->{debug}=1;
		$Storage->{ACTION}='change';
		$Storage->{ACTION_LNG}=$_CHANGE;
		$Storage->storage_articles_info({ ID => $FORM{chg},});
		if (! $Storage->{errno}) {
			$html->message('info', $_INFO, "$_CHANGING");	
		}
	}	
	$Storage->{ARTICLE_TYPES}=$html->form_select("ARTICLE_TYPE", 
								{	SELECTED          => $Storage->{ARTICLE_TYPE},
									SEL_MULTI_ARRAY   => [['', ''], @{ $Storage->storage_types_list() } ], 
									MULTI_ARRAY_KEY   => 0,
									MULTI_ARRAY_VALUE => 1,
									NO_ID             => 1
								});	
	
	if (!$FORM{add} and !$FORM{change}) {
		
		
		$html->tpl_show(_include('storage_articles', 'Storage'), $Storage);
	}	
	
	my $table = $html->table(	{ 	width      => '100%',
									caption    => $_STORAGE,
									border     => 1,
									title      => [$_NAME, $_TYPE, $_MEASURE, $_DATE, $_COMMENTS, '-', '-'],
									cols_align => ['left', 'right', 'right', 'right', 'center', 'center'],
									pages      => $Storage->{TOTAL},
									ID         => 'STORAGE_ID'
								}
	);

	#$Storage->{debug}=1;	
	$list = $Storage->storage_articles_list( {});
	
	if ($Storage->{errno}) {
    $html->message('err', $_ERROR, "[$Storage->{errno}] $err_strs{$Storage->{errno}}");
	}
	
	foreach my $line ( @$list ) {
		$table->addrow(	$line->[1],  
						$line->[6], 
						$line->[3], 
						$line->[4],  
						$line->[5],
						$html->button($_INFO, "index=$index&chg=$line->[0]", { CLASS => 'change' }), 
						(defined($permissions{0}{5})) ? $html->button($_DEL, "index=$index&del=$line->[0]", { MESSAGE => "$_DEL $_ARTICLE $line->[1]?", CLASS => 'del' }) : ''
		);
	}	
	print $table->show();							
}

#***********************************************************
#  	Storage articles types
#***********************************************************
sub storage_articles_types {
	if ($FORM{message}) {				
		$html->message('info', $_INFO, "$FORM{message}");	
	}
	$Storage->{ACTION}='add';
	$Storage->{ACTION_LNG}=$_ADD;		
	
	if ($FORM{add}) {
		if ($FORM{NAME} ne '') {
			$Storage->storage_types_add({ %FORM });
			if (! $Storage->{errno}) {
				#$html->message('info', $_INFO, "$_ADDED");
				$html->tpl_show(_include('storage_redirect', 'Storage'), { 
					SECTION => '',
					MESSAGE => "$_ADDED",
				});		
			}
		}
		else {
			$html->message('info', $_INFO, "$_FIELDS_FOR_TYPE_ARE_REQUIRED");
			$html->tpl_show(_include('storage_articles_types', 'Storage'), {%$Storage, %FORM} );
		}	
	}
	elsif ($FORM{del}) {
		$list = $Storage->storage_articles_list( { ARTICLE_ID =>$FORM{del}} );
		
		if ($Storage->{errno}) {
    	$html->message('err', $_ERROR, "[$Storage->{errno}] $err_strs{$Storage->{errno}}");
		}
		
		if (defined($list->[0]->[0]) ){
	
			$html->message('info', $_INFO, "$_CANT_DELETE_ERROR2 ");
		}
		else {	
		
			#$Storage->{debug}=1;
			$Storage->storage_types_del( {ID =>$FORM{del}} );
	    	if (! $Storage->{errno}){
	    	 	$html->message('info', $_INFO, "$_DELETED");
	     	}
	    }	
	}
	elsif ($FORM{change}) {
		if ($FORM{NAME} ne '') {
			#$Storage->{debug}=1;
			$Storage->storage_types_change({ %FORM });
			if (! $Storage->{errno}) {
	        	$html->tpl_show(_include('storage_redirect', 'Storage'), { 
					SECTION => '',
					MESSAGE => "$_CHANGED",
				});			
	    	}
		}
		else {
			$Storage->{ACTION}='change';
			$Storage->{ACTION_LNG}=$_CHANGE;
			$html->message('info', $_INFO, "$_FIELDS_FOR_TYPE_ARE_REQUIRED");
			$html->tpl_show(_include('storage_articles_types', 'Storage'), {%$Storage, %FORM} );
		}
	}	
	elsif ($FORM{chg}) {
		$Storage->{ACTION}='change';
		$Storage->{ACTION_LNG}=$_CHANGE;		
		#$Storage->{debug}=1;
		$Storage->storage_articles_types_info({ ID => $FORM{chg},});
	} 
	if (!$FORM{add} and !$FORM{change}) {
		$html->tpl_show(_include('storage_articles_types', 'Storage'), $Storage);  		
	}
	my $table = $html->table(	{	width      => '100%',
															caption    => $_TYPE,
															border     => 1,
															title      => [$_NAME, $_COMMENTS, '-', '-'],
															cols_align => ['left', 'right', 'center', 'center'],
															pages      => $Storage->{TOTAL},
															ID         => 'STORAGE_TYPES'
								}
	);
	#$Storage->{debug}=1;	
	$list = $Storage->storage_types_list( {});
	
	if ($Storage->{errno}) {
    $html->message('err', $_ERROR, "[$Storage->{errno}] $err_strs{$Storage->{errno}}");
	}
	
	foreach my $line ( @$list ) {
		$table->addrow(	$line->[1],  
						$line->[2], 
						$html->button($_INFO, "index=$index&chg=$line->[0]", { CLASS => 'change' }), 
						(defined($permissions{0}{5})) ? $html->button($_DEL, "index=$index&del=$line->[0]", { MESSAGE => "$_DEL $_TYPE $line->[1]?", CLASS => 'del' }) : ''
    				  );
	}			
	print $table->show();  
	
}

#***********************************************************
# 	Suppliers
#***********************************************************
sub suppliers_main {
	
	if ($FORM{message}) {				
		$html->message('info', $_INFO, "$FORM{message}");	
	}		
	$Storage->{ACTION}='add';
	$Storage->{ACTION_LNG}=$_ADD;
	$Storage->{DATE}='0000-00-00';
	
	#$Storage->{WND} = $html->form_window("ARTICLE_TYPE_ID", { });
	
	

	if ($FORM{del}) {
		$list = $Storage->storage_incoming_articles_list( { SUPPLIER_ID =>$FORM{del}} );
		
		if ($Storage->{errno}) {
    	$html->message('err', $_ERROR, "[$Storage->{errno}] $err_strs{$Storage->{errno}}");
		}
	
	
		if (defined($list->[0]->[0]) ){

			$html->message('info', $_INFO, "$_CANT_DELETE_ERROR3");
		}	
		else {
			#$Storage->{debug}=1;
			$Storage->suppliers_del( {ID =>$FORM{del}} );
			if (! $Storage->{errno}){
				$html->message('info', $_INFO, "$_DELETED");
			}	
		}
	}	
	elsif ($FORM{change}) {
		if ($FORM{NAME} ne '') {
			#$Storage->{debug}=1;
			$Storage->suppliers_change({ %FORM });
			if (! $Storage->{errno}) {
				#$html->message('info', $_INFO, "$_CHANGED");
					$html->tpl_show(_include('storage_redirect', 'Storage'), { 
					SECTION => '',
					MESSAGE => "$_CHANGED",
				});		
			}
		}
		else {
			$Storage->{ACTION}='change';
			$Storage->{ACTION_LNG}=$_CHANGE;
			$html->message('info', $_INFO, "$_FIELDS_FOR_NAME_ARE_REQUIRED");
			$html->tpl_show(_include('storage_suppliers_form', 'Storage'), {%$Storage, %FORM});
		}	
	}	
	elsif ($FORM{add}) {
		if ($FORM{NAME} ne '') {
		
			$Storage->suppliers_add({ %FORM });
			if (! $Storage->{errno}) {
				#$html->message('info', $_INFO, "$_ADDED");
				$html->tpl_show(_include('storage_redirect', 'Storage'), { 
					SECTION => '',
					MESSAGE => "$_ADDED",
				});		
			}
		} 
		else {
			$html->message('info', $_INFO, "$_FIELDS_FOR_NAME_ARE_REQUIRED");
			$html->tpl_show(_include('storage_suppliers_form', 'Storage'), {%$Storage, %FORM});
		}
	}	
	elsif ($FORM{chg}) {		
		$Storage->{ACTION}='change';
		$Storage->{ACTION_LNG}=$_CHANGE;
		$Storage->suppliers_info({ ID => $FORM{chg} });
		if (! $Storage->{errno}) {
			$html->message('info', $_INFO, "$_CHANGING");	
		}

	}
	if (!$FORM{add} and !$FORM{change}) { 
		$html->tpl_show(_include('storage_suppliers_form', 'Storage'), $Storage);
	}
	my $table = $html->table(	{	width      => '100%',
									caption    => $_SUPPLIERS,
									border     => 1,
									title      => [$_NAME, $_PHONE, 'email', 'icq', $_SITE, $_DIRECTOR, $_BILL,'-', '-'],
									cols_align => ['left', 'right', 'right', 'right', 'center', 'center'],
									pages      => $Storage->{TOTAL},
									ID         => 'STORAGE_ID'
	});			
	#$Storage->{debug}=1;	
	$list = $Storage->suppliers_list({});
	if ($Storage->{errno}) {
    	$html->message('err', $_ERROR, "[$Storage->{errno}] $err_strs{$Storage->{errno}}");
	}

	foreach my $line ( @$list ) {
		$table->addrow(	$line->[1],  
						$line->[9], 
						$line->[13], 
						$line->[14],
						$line->[12],  
						$line->[16],
						$line->[8],
						#$line->[2], DATE
						$html->button($_INFO, "index=$index&chg=$line->[0]", { CLASS => 'change' }), 
						(defined($permissions{0}{5})) ? $html->button($_DEL, "index=$index&del=$line->[0]", { MESSAGE => "$_DEL $_SUPPLIER $line->[1]?", CLASS => 'del' }) : ''
		);
	}
	print $table->show();
}

#***********************************************************
# 	Storage incoming
#***********************************************************
sub storage_main {

#	if($FORM{''}) {
#		
#		$FORM{storage_status} = 1;
#	}

		$Storage->{ACTION}    = 'add';
		$Storage->{ACTION_LNG}= $_ADD;
		$Storage->{DATE}      = '0000-00-00 00:00:00';	

	if ($FORM{message}) {				
		$html->message('info', $_INFO, "$FORM{message}");	
	}

	
	if ($FORM{del}) {
		#$Storage->{debug}=1;
		$list = $Storage->storage_incoming_articles_list( { ID => $FORM{del} });	
		
		if ($Storage->{errno}) {
    	$html->message('err', $_ERROR, "[$Storage->{errno}] $err_strs{$Storage->{errno}}");
		}
		
		if ($list->[0]->[25] == '' and $list->[0]->[26] == ''  and $list->[0]->[27] == '' and $list->[0]->[28]  =='' ) {
			$Storage->storage_incoming_articles_del( {ID =>$FORM{del}} );
			if (! $Storage->{errno}){
				#$html->message('info', $_INFO, "$_DELETED");
				$html->tpl_show(_include('storage_redirect', 'Storage'), { 
					SECTION => '&storage_status=1',
					MESSAGE => "$_DELETED",
				});	
			}
			
		}
		else {
			$html->tpl_show(_include('storage_redirect', 'Storage'), { 
				SECTION => '&storage_status=1',
				MESSAGE => "$_CANT_DELETE",
			});	
		}
	}
	elsif ($FORM{change}) {
		
	if ($FORM{COUNT} != '' and $FORM{SUM} != '' and $FORM{ARTICLE_TYPE_ID} != '' and $FORM{ARTICLE_ID} != '' and $FORM{STORAGE_ID} != '') {
		#$Storage->{debug}=1;
		$Storage->storage_incoming_articles_change({ %FORM });
		if (! $Storage->{errno}) {
			#$html->message('info', $_INFO, "$_CHANGED");
			$html->tpl_show(_include('storage_redirect', 'Storage'), { 
					SECTION => '&storage_status=1',
					MESSAGE => "$_CHANGED",
				
				});		
		}	
	
		
	} else {
		$Storage->{ACTION}='change';
		$Storage->{ACTION_LNG}=$_CHANGE;
		$Storage->{DISABLED}='readonly';
			
			my  $ARTICLE_TYPES = $Storage->{ARTICLE_TYPES}=$html->form_select("ARTICLE_TYPE_ID", 
									{	SELECTED          => $FORM{ARTICLE_TYPE_ID},
										SEL_MULTI_ARRAY   => [['', ''], @{ $Storage->storage_types_list() } ], 
										MULTI_ARRAY_KEY   => 0,
										MULTI_ARRAY_VALUE => 1,
										NO_ID             => 1,
										EX_PARAMS		      => 'onchange="autoReload();"',
										MAIN_MENU         => get_function_index('storage_articles_types'),
									});
			
			
			
			my $ARTICLE_ID = $Storage->{ARTICLE_ID}=$html->form_select("ARTICLE_ID", 
									{	SELECTED          => $FORM{ARTICLE_ID},
										SEL_MULTI_ARRAY   => [['', ''], @{ $Storage->storage_articles_list({ ARTICLE_TYPE => $FORM{ARTICLE_TYPE_ID} }) } ], 
										MULTI_ARRAY_KEY   => 0,
										MULTI_ARRAY_VALUE => 1,
										NO_ID             => 1
									});		
		
			my $SUPPLIER_ID = $Storage->{SUPPLIER_ID}=$html->form_select("SUPPLIER_ID", 
											{	SELECTED          => $FORM{SUPPLIER_ID},
												SEL_MULTI_ARRAY   => [['', ''], @{ $Storage->suppliers_list() } ], 
												MULTI_ARRAY_KEY   => 0,
												MULTI_ARRAY_VALUE => 1,
												NO_ID             => 1
											});
			
			my $STORAGE_STORAGES = $Storage->{STORAGE_STORAGES} = $html->form_select('STORAGE_ID',
											{	SELECTED		=> $FORM{STORAGE_ID},
												SEL_ARRAY		=> ['',split(/, /, $conf{STORAGE_STORAGES})   ],
												ARRAY_NUM_ID	=> 1,
												OUTPUT2RETURN	=> 1					
											});		
						
				
				$html->message('info', $_INFO, "$_FIELDS_FOR_ARTICLE_TYPE_SUM_COUNT_ARE_REQUIRED");
				$html->tpl_show(_include('storage_main_form', 'Storage'),	{	%$Storage, 
																				%FORM, 
																				ARTICLE_ID		=> $ARTICLE_ID, 
																				ARTICLE_TYPES 	=> $ARTICLE_TYPES, 
																				SUPPLIER_ID 	=> $SUPPLIER_ID,
																				STORAGE_ID		=> $STORAGE_STORAGES,

																			});
			} 
		
	}
	elsif($FORM{add_article}==1 and $FORM{chg}) {
		#$Storage->{debug}=1;
		$Storage->{ACTION}='change';
		$Storage->{ACTION_LNG}=$_CHANGE;
		$Storage->{DISABLED}='readonly';
		$Storage->storage_incoming_articles_info({ ID => $FORM{chg},});

		if (! $Storage->{errno}) {
			$html->message('info', $_INFO, "$_CHANGING");	
		}
	}
	elsif($FORM{divide}) {
		#$Storage->{debug}=1;	
		$list = $Storage->storage_incoming_articles_list( { ID => $FORM{divide} });
		
		if ($Storage->{errno}) {
    	$html->message('err', $_ERROR, "[$Storage->{errno}] $err_strs{$Storage->{errno}}");
		}
		
		foreach my $line ( @$list ) {
			if ($line->[2] > 1 and $line->[3] != 0) {
				$Storage->storage_incoming_articles_divide ({
					ARTICLE_ID			=>	$line->[1],
					COUNT				=>	$line->[2],
					SUM					=>	$line->[3]/$line->[2],
					SN					=>	$line->[4],
					MAIN_ARTICLE_ID		=>	$line->[0],
					STORAGE_INCOMING_ID	=>	$line->[5],
					SUM_TOTAL			=>	$line->[3],
				});
				if (! $Storage->{errno}) {
					#$html->message('info', $_INFO, "$_DIVIDED");
					$html->tpl_show(_include('storage_redirect', 'Storage'), { 
										SECTION => '&storage_status=1',
										MESSAGE => "$_DIVIDED",
									
									});		
				}

			} else {
				$html->message('info', $_INFO, "$_CANT_DIVIDE");
			}			
		}
	}
	elsif($FORM{accountability}) {
		$Storage->{ACTION}='add_accountability';
		$Storage->{ACTION_LNG}=$_ADD;
		$Storage->{ID}=$FORM{accountability}; 
		$Storage->{AID}=$html->form_select("AID", 
							{	SELECTED          => 1,
								SEL_MULTI_ARRAY   => [['', ''], @{ $Storage->storage_admins_list({ }) } ], 
								MULTI_ARRAY_KEY   => 0,
								MULTI_ARRAY_VALUE => 1,
								NO_ID             => 1
							});	
	
		$html->tpl_show(_include('storage_accountability', 'Storage'), $Storage );
	
	}
	elsif($FORM{discard}) {
			$Storage->{ID}=$FORM{discard};
			$Storage->{ACTION}='add_discard';
			$Storage->{ACTION_LNG}=$_DISCARD;
			$html->tpl_show(_include('storage_discard', 'Storage'), $Storage );
		
	}
	elsif($FORM{add_discard}) {
		#$Storage->{debug}=1;		
		if ($FORM{COMMENTS} ne '') {
			
			$list = $Storage->storage_incoming_articles_list( { ID => $FORM{ID} });
			
			if ($Storage->{errno}) {
	    	$html->message('err', $_ERROR, "[$Storage->{errno}] $err_strs{$Storage->{errno}}");
			}
			
			$leftover = $list->[0]->[21];
			if (($leftover - $FORM{COUNT}) > -1 and $FORM{COUNT} ne '' and $FORM{COUNT} != 0 ) {	
					$Storage->storage_discard ({
						ARTICLE_ID				=>	$list->[0]->[1],
						COUNT_INCOMING			=>	$list->[0]->[2],
						SUM_INCOMING			=>	$list->[0]->[3],
						SN						=>	$list->[0]->[4],
						MAIN_ARTICLE_ID			=>	$list->[0]->[0],
						STORAGE_INCOMING_ID		=>	$list->[0]->[5],
						SUM_TOTAL				=>	$list->[0]->[23], #$list->[0]->[23],
						%FORM,
								
					});

					if (! $Storage->{errno}) {
						$html->tpl_show(_include('storage_redirect', 'Storage'), { 
										SECTION => '&storage_status=1',
										MESSAGE => "$_DISCARDED",
									
									});		
					}
					
			} else {
				$html->message('info', $_INFO, "$_CANT_DISCARD_MAX_VALUE $leftover ");
				$Storage->{ID}=$FORM{discard};
				$Storage->{ACTION}='add_discard';
				$Storage->{ACTION_LNG}=$_DISCARD;
				$html->tpl_show(_include('storage_discard', 'Storage'), {%$Storage, %FORM} );
			}
						
		} else {
			#$Storage->{ACTION}='add_discard';
			#$Storage->{ACTION_LNG}=$_DISCARD;
			$html->message('info', $_INFO, "$_FIELDS_FOR_COMMENTS_ARE_REQUIRED");
			$html->tpl_show(_include('storage_discard', 'Storage'), {	%FORM, 
																		ACTION => 'add_discard',
																		ACTION_LNG => $_DISCARD,
																		ID => $FORM{ID},
																	
																	});
		}
	}
	
	if ($FORM{add_accountability}) {
		#$Storage->{debug}=1;
		$list = $Storage->storage_incoming_articles_list ({ ID => $FORM{ID}});
		
		if ($Storage->{errno}) {
    	$html->message('err', $_ERROR, "[$Storage->{errno}] $err_strs{$Storage->{errno}}");
		}
		
		$leftover = $list->[0]->[21];
		# $Storage->{list}->[0]->[21];
		if (($leftover - $FORM{COUNT}) > -1 and $FORM{COUNT} ne '' and $FORM{COUNT} != 0) {
		
			#$Storage->{debug}=1;
			$Storage->storage_accountability_add({ %FORM });
				if (! $Storage->{errno}) {
					#$html->message('info', $_INFO, "$_ADDED");
					$html->tpl_show(_include('storage_redirect', 'Storage'), { 
															SECTION => '&storage_status=1',
															MESSAGE => "$_ADDED_TO_ACOUNTABILITY",
														
														});	
				}
		
		} else {
		$Storage->{ACTION}='add_accountability';
		$Storage->{ACTION_LNG}=$_ADD;
		$Storage->{ID}=$FORM{accountability};
		$Storage->{AID}=$html->form_select("AID", 
							{	SELECTED          => $FORM{AID},
								SEL_MULTI_ARRAY   => [['', ''], @{ $Storage->storage_admins_list({ }) } ], 
								MULTI_ARRAY_KEY   => 0,
								MULTI_ARRAY_VALUE => 1,
								NO_ID             => 1
							});	 
			$html->message('info', $_INFO, "$_CANT_ACCOUNTABILITY_ADD_MAX_VALUE $leftover ");
			$html->tpl_show(_include('storage_accountability', 'Storage'), {%$Storage, %FORM, AID => $Storage->{AID} }, );
		}
	
	}
	elsif ($FORM{add_reserve}) {
		#$Storage->{debug}=1;
		$list = $Storage->storage_incoming_articles_list ({ ID => $FORM{ID}});
		if ($Storage->{errno}) {
    	$html->message('err', $_ERROR, "[$Storage->{errno}] $err_strs{$Storage->{errno}}");
		}
		
		$leftover = $list->[0]->[21];
		if (($leftover - $FORM{COUNT}) > -1 and $FORM{COUNT} ne '' and $FORM{COUNT} != 0) {
		
			#$Storage->{debug}=1;
			$Storage->storage_reserve_add({ %FORM });
				if (! $Storage->{errno}){
					$html->tpl_show(_include('storage_redirect', 'Storage'), { 
												SECTION => '&storage_status=1',
												MESSAGE => "$_RESERVED",
											
											});	
				}
		} else {
		$Storage->{ACTION}='add_reserve';
		$Storage->{ACTION_LNG}=$_RESERVE;
		$Storage->{ID}=$FORM{reserve}; 
		$Storage->{AID}=$html->form_select("AID", 
							{	SELECTED          => $FORM{AID},
								SEL_MULTI_ARRAY   => [['', ""], @{ $Storage->storage_admins_list({ }) } ], 
								MULTI_ARRAY_KEY   => 0,
								MULTI_ARRAY_VALUE => 1,
								NO_ID             => 1
							});
		$html->message('info', $_INFO, "$_CANT_RESERVE_MAX_VALUE $leftover ");
		$html->tpl_show(_include('storage_reserve', 'Storage'), {%$Storage, 
																	%FORM, 
																	AID => $Storage->{AID}, });
			
		}

	}



	$Storage->{SUPPLIER_ID}=$html->form_select("SUPPLIER_ID", 
									{	SELECTED          => $Storage->{SUPPLIER_ID},
										SEL_MULTI_ARRAY   => [['', ''], @{ $Storage->suppliers_list() } ], 
										MULTI_ARRAY_KEY   => 0,
										MULTI_ARRAY_VALUE => 1,
										NO_ID             => 1
									});
	

	if ($FORM{ARTICLE_TYPE_ID}) {	
		$Storage->{ARTICLE_ID}=$html->form_select("ARTICLE_ID", 
										{	SELECTED          => $Storage->{ARTICLE_ID},
											SEL_MULTI_ARRAY   => [['', ''], @{ $Storage->storage_articles_list({ ARTICLE_TYPE => $FORM{ARTICLE_TYPE_ID}, }) } ], 
											MULTI_ARRAY_KEY   => 0,
											MULTI_ARRAY_VALUE => 1,
											NO_ID             => 1,
								      MAIN_MENU         => get_function_index('storage_articles'),
										});
		
		$Storage->{ARTICLE_TYPES}=$html->form_select("ARTICLE_TYPE_ID", 
										{	SELECTED          => $FORM{ARTICLE_TYPE_ID},
											SEL_MULTI_ARRAY   => [['', ''], @{ $Storage->storage_types_list() } ], 
											MULTI_ARRAY_KEY   => 0,
											MULTI_ARRAY_VALUE => 1,
											NO_ID             => 1,
											EX_PARAMS			=> 'onchange="autoReload();"'
										});
	
	} else {
		$Storage->{ARTICLE_ID}=$html->form_select("ARTICLE_ID", 
							{	SELECTED          => $Storage->{ARTICLE_ID},
								SEL_MULTI_ARRAY   => [['', ''], @{ $Storage->storage_articles_list({ ARTICLE_TYPE =>$Storage->{ARTICLE_TYPE_ID} }) } ], 
								MULTI_ARRAY_KEY   => 0,
								MULTI_ARRAY_VALUE => 1,
								NO_ID             => 1
							});		


		$Storage->{ARTICLE_TYPES}=$html->form_select("ARTICLE_TYPE_ID", 
										{	SELECTED          => $Storage->{ARTICLE_TYPE_ID},
											SEL_MULTI_ARRAY   => [['', ''], @{ $Storage->storage_types_list() } ], 
											MULTI_ARRAY_KEY   => 0,
											MULTI_ARRAY_VALUE => 1,
											NO_ID             => 1,
											EX_PARAMS			=> 'onchange="autoReload();"'
										});													
	}

	if ($conf{STORAGE_STORAGES}) {		  		
		$Storage->{STORAGE_STORAGES} = $html->form_select('STORAGE_ID',
									{	SELECTED		=> $Storage->{STORAGE_ID},
										SEL_ARRAY		=> ['',split(/, /, $conf{STORAGE_STORAGES})   ],
										ARRAY_NUM_ID	=> 1,
										OUTPUT2RETURN	=> 1					
									});		 		
	}	
	my @storage_storages = ('', split(/,/, $conf{STORAGE_STORAGES})); 		
	
	if($FORM{sn} == 1) {
		$Storage->{INPUT_TYPE} ='text';
	} else {
		$Storage->{INPUT_TYPE} ='hidden';
	}
	

	
	
	print $html->br() .  
	
		$html->button(($FORM{storage_status}==1) ? $html->b($_STORAGE)  : $_STORAGE, "index=$index&storage_status=1", { BUTTON => 1 }) . ' ' .
		$html->button(($FORM{show_accountability}==1) ? $html->b($_ACCOUNTABILITY)  : $_ACCOUNTABILITY, "index=$index&show_accountability=1", { BUTTON => 1 }) . ' ' . 
		$html->button(($FORM{show_reserve}==1) ? $html->b($_RESERVE)  : $_RESERVE, "index=$index&show_reserve=1", { BUTTON => 1 }) . ' ' .
		$html->button(($FORM{show_installation}==1) ? $html->b($_INSTALLED)  : $_INSTALLED, "index=$index&show_installation=1", { BUTTON => 1 }) . ' ' .
		$html->button(($FORM{storage_status}==5) ? $html->b($_DISCARDED)  : $_DISCARDED, "index=$index&storage_status=5", { BUTTON => 1 }) . ' ' .
		$html->br(). $html->br(); 
	

	if($FORM{add_article} == 1 and !$FORM{add} and !$FORM{change})  {
		$html->tpl_show(_include('storage_main_form', 'Storage'), $Storage );
	}

	if ($FORM{add_article} == 1 and $FORM{add}) {
		#$Storage->{debug}=1;
		$Storage->{ACTION}='add';
		$Storage->{ACTION_LNG}=$_ADD;
			if ($FORM{COUNT} != '' and $FORM{SUM} != '' and $FORM{ARTICLE_TYPE_ID} != '' and $FORM{ARTICLE_ID} != '' and $FORM{STORAGE_ID} != '') {
			
			 	$Storage->storage_incoming_articles_add({ %FORM });
				if (! $Storage->{errno}) {
					#$html->message('info', $_INFO, "$_ADDED");
					$html->tpl_show(_include('storage_redirect', 'Storage'), { 
										SECTION => '&storage_status=1',
										MESSAGE => "$_ADDED",		
									});	 	
					
				}		
			} else {
			
			my  $ARTICLE_TYPES = $Storage->{ARTICLE_TYPES}=$html->form_select("ARTICLE_TYPE_ID", 
									{	SELECTED          => $FORM{ARTICLE_TYPE_ID},
										SEL_MULTI_ARRAY   => [['', ''], @{ $Storage->storage_types_list() } ], 
										MULTI_ARRAY_KEY   => 0,
										MULTI_ARRAY_VALUE => 1,
										NO_ID             => 1,
										EX_PARAMS		  => 'onchange="autoReload();"',
										MAIN_MENU         => get_function_index('storage_articles_types'),
									});
			
			
			
			my $ARTICLE_ID = $Storage->{ARTICLE_ID}=$html->form_select("ARTICLE_ID", 
									{	SELECTED          => $FORM{ARTICLE_ID},
										SEL_MULTI_ARRAY   => [['', ''], @{ $Storage->storage_articles_list({ ARTICLE_TYPE => $FORM{ARTICLE_TYPE_ID} }) } ], 
										MULTI_ARRAY_KEY   => 0,
										MULTI_ARRAY_VALUE => 1,
										NO_ID             => 1
									});		
		
			my $SUPPLIER_ID = $Storage->{SUPPLIER_ID}=$html->form_select("SUPPLIER_ID", 
											{	SELECTED          => $FORM{SUPPLIER_ID},
												SEL_MULTI_ARRAY   => [['', ''], @{ $Storage->suppliers_list() } ], 
												MULTI_ARRAY_KEY   => 0,
												MULTI_ARRAY_VALUE => 1,
												NO_ID             => 1
											});
			
			my $STORAGE_STORAGES = $Storage->{STORAGE_STORAGES} = $html->form_select('STORAGE_ID',
											{	SELECTED		=> $FORM{STORAGE_ID},
												SEL_ARRAY		=> ['',split(/, /, $conf{STORAGE_STORAGES})   ],
												ARRAY_NUM_ID	=> 1,
												OUTPUT2RETURN	=> 1					
											});		
						
				
				$html->message('info', $_INFO, "$_FIELDS_FOR_ARTICLE_TYPE_SUM_COUNT_ARE_REQUIRED");
				$html->tpl_show(_include('storage_main_form', 'Storage'),	{	%$Storage, 
																				%FORM, 
																				ARTICLE_ID		=> $ARTICLE_ID, 
																				ARTICLE_TYPES 	=> $ARTICLE_TYPES, 
																				SUPPLIER_ID 	=> $SUPPLIER_ID,
																				STORAGE_ID		=> $STORAGE_STORAGES,
																			});
			} 
	}
	
	
	if ($FORM{storage_status} == 1) {
		
	    $Storage->{ARTICLE_TYPES}=$html->form_select("ARTICLE_TYPE_ID", 
								{	SELECTED          => $FORM{ARTICLE_TYPE_ID},
									SEL_MULTI_ARRAY   => [['', "$_ALL"], @{ $Storage->storage_types_list() } ], 
									MULTI_ARRAY_KEY   => 0,
									MULTI_ARRAY_VALUE => 1,
									NO_ID             => 1,
									EX_PARAMS		      => 'onchange="autoReload();"',
									MAIN_MENU         => get_function_index('storage_articles_types'),
								});
			
			
			
		 $Storage->{ARTICLE_ID}=$html->form_select("ARTICLE_ID", 
									{	SELECTED          => $FORM{ARTICLE_ID},
										SEL_MULTI_ARRAY   => [['', "$_ALL"], @{ $Storage->storage_articles_list({ ARTICLE_TYPE => $FORM{ARTICLE_TYPE_ID} }) } ], 
										MULTI_ARRAY_KEY   => 0,
										MULTI_ARRAY_VALUE => 1,
										NO_ID             => 1
									});		
		
	 	 $Storage->{SUPPLIER_ID}=$html->form_select("SUPPLIER_ID", 
											{	SELECTED          => $FORM{SUPPLIER_ID},
												SEL_MULTI_ARRAY   => [['', "$_ALL"], @{ $Storage->suppliers_list() } ], 
												MULTI_ARRAY_KEY   => 0,
												MULTI_ARRAY_VALUE => 1,
												NO_ID             => 1
											});
			
		$Storage->{STORAGE_STORAGES} = $html->form_select('STORAGE_ID',
											{	SELECTED		=> $FORM{STORAGE_ID},
												SEL_ARRAY		=> [ "$_ALL", split(/, /, $conf{STORAGE_STORAGES})   ],
												ARRAY_NUM_ID	=> 1,
												OUTPUT2RETURN	=> 1					
											});	
		$html->tpl_show(_include('storage_main_filter', 'Storage'), $Storage, );	
		
		my $table = $html->table(	{	width		=> '100%',
										caption		=> $_STORAGE,
										border		=> 1,
										title		=> [$_TYPE, $_NAME, $_COUNT, $_SUM,$_ADDED, $_SUPPLIERS,$_STORAGE , '-'],
										cols_align	=> ['left', 'right', 'right', 'right', 'center', 'center'],
										pages		=> $Storage->{TOTAL},
										ID			=> 'STORAGE_ID',
										EXPORT		=> "$_EXPORT XML:&xml=1",
										header		=> $html->button("$_ADD_ITEM", "index=$index&add_article=1", { BUTTON => 1 }),
									}	
		);
		#$Storage->{debug}=1;	
		$list = $Storage->storage_incoming_articles_list( { ARTICLE_ID => $FORM{ARTICLE_ID}, 
															ARTICLE_TYPES => $FORM{ARTICLE_TYPE_ID},
															STORAGE_ID => $FORM{STORAGE_ID},
															SUPPLIER_ID => $FORM{SUPPLIER_ID},
														});	
		
		if ($Storage->{errno}) {
    		$html->message('err', $_ERROR, "[$Storage->{errno}] $err_strs{$Storage->{errno}}");
		}

		foreach my $line ( @$list ) {
			if ($line->[2] != 0 ) {				
							
							if ($line->[2] < 2) {
								$info = '';
								$info_divide = $html->button($_INFO, "index=$index&add_article=1&chg=$line->[0]&sn=1", { BUTTON => 0, ex_params => 'style = text-decoration:none'  });
							} else {
								$info = $html->button($_DIVIDE, "index=$index&divide=$line->[0]", { BUTTON => 0, ex_params => 'style = text-decoration:none'  });
								$info_divide = $html->button($_INFO, "index=$index&add_article=1&chg=$line->[0]", { BUTTON => 0, ex_params => 'style = text-decoration:none'  });	
							}
								$del = (defined($permissions{0}{5})) ? $html->button($_DEL, "index=$index&del=$line->[0]", { MESSAGE => "$_DEL $line->[14] $line->[21] $line->[15]?", BUTTON => 0, ex_params => 'style = text-decoration:none'  }) : '';
	
			$table->addrow(	$line->[17],  
							$line->[14],
							"$_INSTALLED: "				. $line->[28] * 1 . ' '. $line->[15] . $html->br() .
							"$_ACCOUNTABILITY: " 	. $line->[25] * 1 . ' '. $line->[15] . $html->br() .
							"$_RESERVE: " 				. $line->[26] * 1 . ' '. $line->[15] . $html->br() .
							"$_DISCARDED: "  			. $line->[27] * 1 . ' '. $line->[15] . $html->br() .  
							$html->b("$_LEFTOVER: ". $line->[21] * 1 . ' '. $line->[15]) . $html->br() . '-' . $html->br() .
							"$_TOTAL: " 			. ($line->[2] + $line->[28] + $line->[27]). ' '. $line->[15],  
							"$_INSTALLED: " 	. $line->[28] * $line->[22]  . $html->br() .
							"$_ACCOUNTABILITY: " 	. $line->[25] * $line->[22]. $html->br() .
							"$_RESERVE: " 			. $line->[26] * $line->[22]. $html->br() .
							"$_DISCARDED: " 		. $line->[27] * $line->[22]. $html->br() .  
							$html->b("$_LEFTOVER: ". $line->[21] * $line->[22]) . $html->br() . '-' . $html->br() .							
							"$_TOTAL: " 			. ($line->[2] + $line->[28] + $line->[27]) * $line->[22],
							$line->[7],
							$line->[18],   # (sm.supplier_id)
							$storage_storages[$line->[12]],  #C (sm.depot_id)
							#$line->[19],
							#$line->[9],
							$info .$html->br(). 
									$info_divide .$html->br(). 
									$del .$html->br(). 
									$html->button("$_TO_ACCOUNTABILITY", "index=$index&accountability=$line->[0]", { BUTTON => 0, ex_params => 'style = text-decoration:none' }).$html->br(). 
									$html->button("$_TO_RESERVE", "index=$index&reserve=$line->[0]", { BUTTON => 0, ex_params => 'style = text-decoration:none'  }).$html->br().
									$html->button("$_TO_DISCARD", "index=$index&discard=$line->[0]", { BUTTON => 0, ex_params => 'style = text-decoration:none'  }),
											 
							
					);
		
			}
		}
		print $table->show();
	}
	elsif($FORM{install_accountability}) {
			$Storage->{NAS}=$html->form_select("NAS", 
								{	SELECTED          => 0,
									SEL_MULTI_ARRAY   => [['0', '-'], @{ $Storage->storage_installation_nas_list({ }) } ], 
									MULTI_ARRAY_KEY   => 0,
									MULTI_ARRAY_VALUE => 1,
									NO_ID             => 1
								});
	
		$Storage->{ADDRESS_TPL} = $html->tpl_show(templates('form_address_sel'), $Storage, { OUTPUT2RETURN => 1 }); 
		$Storage->{ID} = $FORM{install_accountability};
		$html->tpl_show(_include('storage_installation_add', 'Storage'), $Storage);	
	}
	elsif($FORM{install}) {
		#$Storage->{debug}=1;	
			my $NAS =	$Storage->{NAS}=$html->form_select("NAS", 
				{	SELECTED          => $FORM{NAS},
					SEL_MULTI_ARRAY   => [['0', '-'], @{ $Storage->storage_installation_nas_list({ }) } ], 
					MULTI_ARRAY_KEY   => 0,
					MULTI_ARRAY_VALUE => 1,
					NO_ID             => 1
				});
		
			$list2 = $Storage->storage_accountability_list( { ID => $FORM{ID} });
			
			if ($Storage->{errno}) {
    			$html->message('err', $_ERROR, "[$Storage->{errno}] $err_strs{$Storage->{errno}}");
			}
			
			$list = $Storage->storage_incoming_articles_list( { ID => $list2->[0]->[2] });
			
			if ($Storage->{errno}) {
    			$html->message('err', $_ERROR, "[$Storage->{errno}] $err_strs{$Storage->{errno}}");
			}
			
			$leftover = $list2->[0]->[3];
			if ($leftover == '') {
				$leftover = 0;
			} 
		
		if ($FORM{ADDRESS_DISTRICT} ne '' and $FORM{ADDRESS_STREET} ne '' and $FORM{ADDRESS_BUILD} != ''){
		
		
			if (($leftover - $FORM{COUNT}) > -1 and $FORM{COUNT} ne '' and $FORM{COUNT} != 0) {
				$Storage->storage_installation_add({ %FORM,
													COUNT_INCOMING	=> $list->[0]->[2],
													SUM_TOTAL		=> $list->[0]->[23],
													MAIN_ARTICLE_ID		=>	$list->[0]->[0],
													STORAGE_INCOMING_ARTICLES_ID => $list2->[0]->[2],
													
												});
				$Storage->storage_accountability_del({ID =>$FORM{ID}});
					if (! $Storage->{errno}) {
						#$html->message('info', $_INFO, "$_INSTALLED");
						$html->tpl_show(_include('storage_redirect', 'Storage'), { 
												SECTION => '&show_accountability=1',
												MESSAGE => "$_INSTALLED",
											
											});		
					}	
			}
			else {
					$Storage->{ID}=$FORM{ID}; 
					$html->message('info', $_INFO, "$_CANT_INSTALL_MAX_VALUE $leftover ");
					$html->tpl_show(_include('storage_installation_add', 'Storage'), { %$Storage, 
																						%FORM,
																						NAS	=> $NAS, 						
																					 });			
			}
		}
		else {

			
			$html->message('info', $_INFO, "$_FIELDS_FOR_STREET_DISTRICT_HOUSE_ARE_REQUIRED");
			$html->tpl_show(_include('storage_installation_add', 'Storage'), { %$Storage, 
																				%FORM,
																				NAS	=> $NAS,						
																			});	
				
		}



	}
	elsif ($FORM{show_accountability}) {
		
			$Storage->{AID}=$html->form_select("AID", 
								{	SELECTED          => 0,
									SEL_MULTI_ARRAY   => [['0', 'All'], @{ $Storage->storage_admins_list({ }) } ], 
									MULTI_ARRAY_KEY   => 0,
									MULTI_ARRAY_VALUE => 1,
									NO_ID             => 1
								});
					
			print $html->br() . 
				  $html->br() .
				  $html->form_main({	CONTENT => "$_ADMIN: ".  $Storage->{AID},
										HIDDEN  => { index  => "$index" },
										SUBMIT  => { show_accountability  => "$_SHOW" },
										NAME    => 'admin_select'
								});
##			
			my $table = $html->table(	{	width	=> '100%',
											caption    => $_ACCOUNTABILITY,
											border     => 1,
											title      => [$_ADMIN, $_TYPE, $_NAME, $_COUNT, $_SUM, $_DATE,$_COMMENTS, '-'],
											cols_align => ['center', 'right', 'right', 'right', 'center', 'center'],
											pages      => $Storage->{TOTAL},
											ID         => 'STORAGE_ID'
										}	
			); 
			#Storage->{debug}=1;	
			$list = $Storage->storage_accountability_list({ AID => $FORM{AID} });
			
			if ($Storage->{errno}) {
    		$html->message('err', $_ERROR, "[$Storage->{errno}] $err_strs{$Storage->{errno}}");
			}
			
			foreach my $line ( @$list ) {
				$del = (defined($permissions{0}{5})) ? $html->button("$_RETURN_STORAGE", "index=$index&del_accountability=$line->[0]", { MESSAGE => "$_RETURN_STORAGE $line->[7] $line->[3] $line->[10]?", BUTTON => 0, ex_params => 'style = text-decoration:none'}) : '';
				$table->addrow(	$line->[6],  
								$line->[8],
								$line->[7],
								$line->[3] . ' ' . $line->[10] ,
								$line->[9],  
								$line->[4],
								$line->[5],
								$html->button($_INSTALL, "index=$index&install_accountability=$line->[0]", { BUTTON => 0, ex_params => 'style = text-decoration:none' }) . $html->br() .  $del);
			}
			print $table->show();
		
	} 
	elsif ($FORM{del_accountability}) {
		#$Storage->{debug}=1;
		$Storage->storage_accountability_del( {ID =>$FORM{del_accountability}} );
		if (! $Storage->{errno}){
			$html->tpl_show(_include('storage_redirect', 'Storage'), { 
						SECTION => '&show_accountability=1',
						MESSAGE => "$_RETURNED_STORAGE",
			});
		}	
	}
	elsif ($FORM{show_reserve}) {
		
					$Storage->{AID}=$html->form_select("AID", 
								{	SELECTED          => 0,
									SEL_MULTI_ARRAY   => [['0', 'All'], @{ $Storage->storage_admins_list({ }) } ], 
									MULTI_ARRAY_KEY   => 0,
									MULTI_ARRAY_VALUE => 1,
									NO_ID             => 1
								});
					
			print $html->br() . 
				  $html->br() .
				  $html->form_main({	CONTENT => "$_RESERVE: ".  $Storage->{AID},
										HIDDEN  => { index  => "$index" },
										SUBMIT  => { show_reserve  => "$_SHOW" },
										NAME    => 'admin_select'
								});
##			
			my $table = $html->table(	{	width	=> '100%',
											caption    => $_RESERVE,
											border     => 1,
											title      => [$_ADMIN, $_TYPE, $_NAME, $_COUNT, $_SUM, $_DATE,$_COMMENTS, '-'],
											cols_align => ['center', 'right', 'right', 'right', 'center', 'center'],
											pages      => $Storage->{TOTAL},
											ID         => 'STORAGE_ID'
										}	
			); 
			#Storage->{debug}=1;	
			$list = $Storage->storage_reserve_list({ AID => $FORM{AID} });
			
			if ($Storage->{errno}) {
    		$html->message('err', $_ERROR, "[$Storage->{errno}] $err_strs{$Storage->{errno}}");
			}
			
			foreach my $line ( @$list ) {
				$del = (defined($permissions{0}{5})) ? $html->button($_DELETE_FROM_RESERVE, "index=$index&del_reserve=$line->[0]", { MESSAGE => "$_DELETE_FROM_RESERVE $line->[7] $line->[3] $line->[10]?", BUTTON => 0, ex_params => 'style = text-decoration:none'}) : '';
				$table->addrow(	$line->[6],  
								$line->[8],
								$line->[7],
								$line->[3] . ' ' . $line->[10] ,
								$line->[9],  
								$line->[4],
								$line->[5],
								$del
								
			 					
							);
			}
			print $table->show();
		
	}
	elsif ($FORM{reserve}) {
		$Storage->{ACTION}='add_reserve';
		$Storage->{ACTION_LNG}=$_RESERVE;
		$Storage->{ID}=$FORM{reserve}; 
		$Storage->{AID}=$html->form_select("AID", 
							{	SELECTED          => 1,
								SEL_MULTI_ARRAY   => [['', ""], @{ $Storage->storage_admins_list({ }) } ], 
								MULTI_ARRAY_KEY   => 0,
								MULTI_ARRAY_VALUE => 1,
								NO_ID             => 1
							});
		$html->tpl_show(_include('storage_reserve', 'Storage'), $Storage );
	}
	elsif ($FORM{del_reserve}) {
		#$Storage->{debug}=1;
		$Storage->storage_reserve_del( {ID =>$FORM{del_reserve}} );
		if (! $Storage->{errno}){
			$html->tpl_show(_include('storage_redirect', 'Storage'), { 
				SECTION => '&show_reserve=1',
				MESSAGE => "$_DELETED_FROM_RESERVE",
			});
		}	
	}
	
	#elsif ($FORM{storage_status} == 4) 
	elsif ($FORM{show_installation}) {
		$Storage->{AID}=$html->form_select("AID", 
							{	SELECTED          => $FORM{AID},
								SEL_MULTI_ARRAY   => [['0', "$_ALL"], @{ $Storage->storage_admins_list({ }) } ], 
								MULTI_ARRAY_KEY   => 0,
								MULTI_ARRAY_VALUE => 1,
								NO_ID             => 1
							});
		$Storage->{DISTRICTS}=$html->form_select("DISTRICTS", 
							{	SELECTED          => $FORM{DISTRICTS},
								SEL_MULTI_ARRAY   => [['0', "$_ALL"], @{ $Storage->storage_districts_list({ }) } ], 
								MULTI_ARRAY_KEY   => 0,
								MULTI_ARRAY_VALUE => 1,
								NO_ID             => 1
							});
		$Storage->{STREETS}=$html->form_select("STREETS", 
							{	SELECTED          => $FORM{STREETS},
								SEL_MULTI_ARRAY   => [['0', "$_ALL"], @{ $Storage->storage_streets_list({ }) } ], 
								MULTI_ARRAY_KEY   => 0,
								MULTI_ARRAY_VALUE => 1,
								NO_ID             => 1
							});
				


		$html->tpl_show(_include('storage_installation_filter', 'Storage'), $Storage);	

				my $table = $html->table(	{	width	=> '100%',
												caption    => $_INSTALLED,
												border     => 1,
												title      => [$_TYPE,$_NAME, $_COUNT, $_INSTALLED, $_ADMIN, $_NAS,  $_COMMENTS, '-'],
												cols_align => ['left', 'right', 'right', 'right', 'center', 'center'],
												pages      => $Storage->{TOTAL},
												ID         => 'INSTALLED_ID'
											}	
		);
		
		
		#$Storage->{debug}=1;
		$list = $Storage->storage_installation_list({	AID 		=> $FORM{AID}, 
													 	DISTRICTS 	=> $FORM{DISTRICTS}, 
													 	STREETS 	=> $FORM{STREETS},
													});
		
		if ($Storage->{errno}) {
  		$html->message('err', $_ERROR, "[$Storage->{errno}] $err_strs{$Storage->{errno}}");
		}

		foreach my $line ( @$list ) {
			$table->addrow(	$line->[10],  
							$line->[9],
							$line->[6] .' ' . $line->[12],
							$line->[15].' ' . $line->[14],
							$line->[8],  
							$line->[13],
							$line->[7],
							(defined($permissions{0}{5})) ? $html->button($_RETURN_STORAGE, "index=$index&return_storage=$line->[0]", { MESSAGE => "$_RETURN_STORAGE $line->[9] $line->[6] $line->[12]?", BUTTON => 0, ex_params => 'style = text-decoration:none' }) : ''
							
			);
		}
		
		
		print $table->show();
		
	}
	elsif ($FORM{return_storage}) {
		
	#$Storage->{debug}=1;
	$list = $Storage->storage_installation_list({ ID => $FORM{return_storage}});		
	
	if ($Storage->{errno}) {
		$html->message('err', $_ERROR, "[$Storage->{errno}] $err_strs{$Storage->{errno}}");
	}	
	
	$list2 = $Storage->storage_incoming_articles_list( { ID => $list->[0]->[1] });	
	
	if ($Storage->{errno}) {
    		$html->message('err', $_ERROR, "[$Storage->{errno}] $err_strs{$Storage->{errno}}");
	}
	
		$Storage->storage_installation_return ({
											COUNT_INCOMING	=> $list2->[0]->[2],
											SUM_TOTAL		=> $list2->[0]->[23],
											MAIN_ARTICLE_ID	=> $list2->[0]->[0],
											COUNT 			=> $list->[0]->[6],
											ID_INSTALLATION	=> $FORM{return_storage},
											SUM 			=> $list->[0]->[19]	
										 });
										 
		if (! $Storage->{errno}){
			
				$html->tpl_show(_include('storage_redirect', 'Storage'), { 
					SECTION => '&show_installation=1',
					MESSAGE => "$_RETURNED $list->[0]->[9] $list->[0]->[6] $list2->[0]->[15] $_AT_STORAGE  - $storage_storages[$list2->[0]->[24]]",
				});
		}	
	}
	# Discard **********************************************
	elsif ($FORM{storage_status} == 5) {
		my $table = $html->table(	{	width	=> '100%',
										caption    => $_DISCARDED,
										border     => 1,
										title      => [$_ADMIN, $_TYPE, $_NAME, $_COUNT, $_SUM, $_DATE,$_COMMENTS],
										cols_align => ['left', 'right', 'right', 'right', 'center', 'center'],
										pages      => $Storage->{TOTAL},
										ID         => 'DISCARD_ID'
									}	
		);

		#$Storage->{debug}=1;
		$list = $Storage->storage_discard_list({ });
		
		if ($Storage->{errno}) {
    		$html->message('err', $_ERROR, "[$Storage->{errno}] $err_strs{$Storage->{errno}}");
		}
		
		foreach my $line ( @$list ) {
			$table->addrow(	$line->[6],  
							$line->[8],
							$line->[7],
							$line->[2] .' ' . $line->[10],
							$line->[11],  
							$line->[4],
							$line->[5]);
		}

		print $table->show();
	}
	else {
		$FORM{storage_status} = 1;
	}
}

#***********************************************************
#  	Storage log
#***********************************************************
sub storage_log {

	my $table = $html->table(	{	width	=> '100%',
									caption    => $_LOG,
									border     => 1,
									title      => [$_DATE, $_NAME, $_COUNT, $_STORAGE, $_ACTION, $_ADMIN, IP],
									cols_align => ['left', 'right', 'right', 'right', 'center', 'center'],
									pages      => $Storage->{TOTAL},
									ID         => 'STORAGE_ID'
								}	
	);
	my @storage_id = split(/,/, $conf{STORAGE_STORAGES}); 
	my @action = (	"$_ARRIVAL_OF_THE_GOODS", 
					"$_INSTALLATION", "$_DISCARDED", 
					"$_NARRATED_TO_ACCOUNTABILITY", 
					"$_RETURN_TO_THE_MANUFACTURER", 
					"$_RESERVE", 
					"$_RETURNED_STORAGE", 
					"$_RECORD_CREATED", 
					"$_RECORD_CHANGED");
	
	#$Storage->{debug}=1;	
	$list = $Storage->storage_log_list({});
	if ($Storage->{errno}) {
  		$html->message('err', $_ERROR, "[$Storage->{errno}] $err_strs{$Storage->{errno}}");
	}
	
	foreach my $line ( @$list ) {
		$table->addrow(	$line->[1],  
						$line->[10], 
						$line->[11] . ' ' . $line->[12] , 
						$storage_id[$line->[4]],
						#$line->[5], comments
						$action[$line->[6]],
						$line->[8], 
						$line->[7]);
	}
	print $table->show();
}

#***********************************************************
#  	Storage orders 
#***********************************************************

sub storage_orders {
	
	if ($FORM{message}) {				
		$html->message('info', $_INFO, "$FORM{message}");	
	}
	
	$Storage->{ACTION}='add';
	$Storage->{ACTION_LNG}=$_ADD;
	
	if($FORM{add_order}==1 and $FORM{chg}) {
		#$Storage->{debug}=1;
		$Storage->{ACTION}='change';
		$Storage->{ACTION_LNG}=$_CHANGE;
		$Storage->{CHG}='<input type=hidden name=chg value=1>';
		$Storage->storage_orders_info({ ID => $FORM{chg},});
		if (! $Storage->{errno}) {
			$html->message('info', $_INFO, "$_CHANGING");	
		}
	}
	if ($FORM{change}) {
			
		if ($FORM{ARTICLE_TYPE_ID} != '' and $FORM{ARTICLE_ID} != '') {
			#$Storage->{debug}=1;
			$Storage->storage_orders_change({ %FORM });
			if (! $Storage->{errno}) {
				#$html->message('info', $_INFO, "$_CHANGED");
				$html->tpl_show(_include('storage_redirect', 'Storage'), { 
						SECTION => '',
						MESSAGE => "$_CHANGED",
					
					});		
			}	
		
			
		} else {
			$Storage->{ACTION}='change';
			$Storage->{ACTION_LNG}=$_CHANGE;
				
				my  $ARTICLE_TYPES = $Storage->{ARTICLE_TYPES}=$html->form_select("ARTICLE_TYPE_ID", 
										{	SELECTED          => $FORM{ARTICLE_TYPE_ID},
											SEL_MULTI_ARRAY   => [['', ''], @{ $Storage->storage_types_list() } ], 
											MULTI_ARRAY_KEY   => 0,
											MULTI_ARRAY_VALUE => 1,
											NO_ID             => 1,
											EX_PARAMS		      => 'onchange="autoReload();"',
											MAIN_MENU         => get_function_index('storage_articles_types'),
										});
				
				
				
				my $ARTICLE_ID = $Storage->{ARTICLE_ID}=$html->form_select("ARTICLE_ID", 
										{	SELECTED          => $FORM{ARTICLE_ID},
											SEL_MULTI_ARRAY   => [['', ''], @{ $Storage->storage_articles_list({ ARTICLE_TYPE => $FORM{ARTICLE_TYPE_ID} }) } ], 
											MULTI_ARRAY_KEY   => 0,
											MULTI_ARRAY_VALUE => 1,
											NO_ID             => 1
										});		
			
						
					$html->message('info', $_INFO, "$_FIELDS_FOR_NAME_TYPE_ARE_REQUIRED");
					$html->tpl_show(_include('storage_orders', 'Storage'),	{	%$Storage, 
																					%FORM, 
																					ARTICLE_ID		=> $ARTICLE_ID, 
																					ARTICLE_TYPES 	=> $ARTICLE_TYPES, 
																				});
		} 
			
	}
	
	
	if ($FORM{ARTICLE_TYPE_ID}) {	
		$Storage->{ARTICLE_ID}=$html->form_select("ARTICLE_ID", 
										{	SELECTED          => $Storage->{ARTICLE_ID},
											SEL_MULTI_ARRAY   => [['', ''], @{ $Storage->storage_articles_list({ ARTICLE_TYPE => $FORM{ARTICLE_TYPE_ID}, }) } ], 
											MULTI_ARRAY_KEY   => 0,
											MULTI_ARRAY_VALUE => 1,
											NO_ID             => 1
										});
		
		$Storage->{ARTICLE_TYPES}=$html->form_select("ARTICLE_TYPE_ID", 
										{	SELECTED          => $FORM{ARTICLE_TYPE_ID},
											SEL_MULTI_ARRAY   => [['', ''], @{ $Storage->storage_types_list() } ], 
											MULTI_ARRAY_KEY   => 0,
											MULTI_ARRAY_VALUE => 1,
											NO_ID             => 1,
											EX_PARAMS			=> 'onchange="autoReload();"'
										});
	
	} else {
		$Storage->{ARTICLE_ID}=$html->form_select("ARTICLE_ID", 
							{	SELECTED          => $Storage->{ARTICLE_ID},
								SEL_MULTI_ARRAY   => [['', ''], @{ $Storage->storage_articles_list({ ARTICLE_TYPE =>$Storage->{ARTICLE_TYPE_ID} }) } ], 
								MULTI_ARRAY_KEY   => 0,
								MULTI_ARRAY_VALUE => 1,
								NO_ID             => 1
							});		


		$Storage->{ARTICLE_TYPES}=$html->form_select("ARTICLE_TYPE_ID", 
										{	SELECTED          => $Storage->{ARTICLE_TYPE_ID},
											SEL_MULTI_ARRAY   => [['', ''], @{ $Storage->storage_types_list() } ], 
											MULTI_ARRAY_KEY   => 0,
											MULTI_ARRAY_VALUE => 1,
											NO_ID             => 1,
											EX_PARAMS			=> 'onchange="autoReload();"'
										});													
	}


	if($FORM{add_order} == 1 and !$FORM{add} and !$FORM{change})  {
		$html->tpl_show(_include('storage_orders', 'Storage'), $Storage );
	}

	if ($FORM{add_order} == 1 and $FORM{add}) {
			#$Storage->{debug}=1;
			$Storage->{ACTION}='add';
			$Storage->{ACTION_LNG}=$_ADD;
				if ($FORM{ARTICLE_TYPE_ID} != '' and $FORM{ARTICLE_ID} != '') {
				
				 	$Storage->storage_orders_add({ %FORM });
					if (! $Storage->{errno}) {
						#$html->message('info', $_INFO, "$_ADDED");
						$html->tpl_show(_include('storage_redirect', 'Storage'), { 
											SECTION => '',
											MESSAGE => "$_ADDED",		
										});	 	
						
					}		
				} else {
				my  $ARTICLE_TYPES = $Storage->{ARTICLE_TYPES}=$html->form_select("ARTICLE_TYPE_ID", 
										{	SELECTED          => $FORM{ARTICLE_TYPE_ID},
											SEL_MULTI_ARRAY   => [['', ''], @{ $Storage->storage_types_list() } ], 
											MULTI_ARRAY_KEY   => 0,
											MULTI_ARRAY_VALUE => 1,
											NO_ID             => 1,
											EX_PARAMS		  => 'onchange="autoReload();"'
										});
				
				my $ARTICLE_ID = $Storage->{ARTICLE_ID}=$html->form_select("ARTICLE_ID", 
										{	SELECTED          => $FORM{ARTICLE_ID},
											SEL_MULTI_ARRAY   => [['', ''], @{ $Storage->storage_articles_list({ ARTICLE_TYPE => $FORM{ARTICLE_TYPE_ID} }) } ], 
											MULTI_ARRAY_KEY   => 0,
											MULTI_ARRAY_VALUE => 1,
											NO_ID             => 1
										});								
					
					$html->message('info', $_INFO, "$_FIELDS_FOR_NAME_TYPE_ARE_REQUIRED");
					$html->tpl_show(_include('storage_orders', 'Storage'),	{	%$Storage, 
																					%FORM,
																				ARTICLE_ID		=> $ARTICLE_ID, 
																				ARTICLE_TYPES 	=> $ARTICLE_TYPES, 
																				});
				} 
	}
	elsif($FORM{del}) {
			$Storage->storage_orders_del( {ID =>$FORM{del}} );
			if (! $Storage->{errno}){
				$html->message('info', $_INFO, "$_DELETED");
			}	
	}

	my $table = $html->table(	{	width		=> '100%',
									caption		=> ORDERS,
									border		=> 1,
									title		=> [$_TYPE, $_NAME, $_COUNT, $_COMMENTS, '-', '-'],
									cols_align	=> ['right', 'center', 'center','center','center'],
									pages		=> $Storage->{TOTAL},
									ID			=> 'STORAGE_ID',
									header	=> $html->button($_ADD_ORDER, "index=$index&add_order=1", { BUTTON => 1 }),
								}	
	);
	#$Storage->{debug}=1;
	$list = $Storage->storage_orders_list({});
	
	if ($Storage->{errno}) {
  	$html->message('err', $_ERROR, "[$Storage->{errno}] $err_strs{$Storage->{errno}}");
	}
	
	foreach my $line ( @$list ) {
		$table->addrow(	$line->[5],  
						$line->[3], 
						$line->[1]. ' ' . $line->[4], 
 						$line->[2],
 						$html->button($_INFO, "index=$index&add_order=1&chg=$line->[0]", { BUTTON => 1 }),
 						(defined($permissions{0}{5})) ? $html->button($_ORDERED, "index=$index&del=$line->[0]", { MESSAGE => "$_DEL $_ORDER_FOR $line->[3]?", BUTTON => 1 }) : '',
					);
	}
	

	print $table->show();



}

#***********************************************************
# 	Hardware
#***********************************************************
sub storage_hardware {




	#$Storage->{debug} = 1;	
	if ($FORM{message}) {				
		$html->message('info', $_INFO, "$FORM{message}");	
	}		
	$Storage->{ACTION}='add';
	$Storage->{ACTION_LNG}=$_ADD;
	$Storage->{DATE}='0000-00-00';
	#"$_INSTALLED", "$_SOLD", "$_RENT"
	my @status;
	
	
	$status[0] = "$_INSTALLED";
	$status[1] = "$_SOLD";
	$status[2] = "$_RENT";
	
	my @storage_storages = ('', split(/,/, $conf{STORAGE_STORAGES}));
	

	

# ADD HARDWARE TO USER -------------------------------------
	
	if($FORM{add} and defined($FORM{UID}) and $FORM{UID} ne '') {
		

		if(defined($FORM{MAC}) and $FORM{MAC} ne '') {		
			if (in_array('Dhcphosts', \@MODULES)) {	
				load_module('Dhcphosts', $html);
				my $result = dhcphosts_user({ REGISTRATION => 1 });
				#$FORM{AUTO_IP}=1;
			}
		}
		
		
		
			
		if($FORM{ARTICLE_ID}) {
			#$Storage->{debug} = 1;	
			$list = $Storage->storage_incoming_articles_list({ ID => $FORM{ARTICLE_ID} });
				
			if ($Storage->{errno}) {
				$html->message('err', $_ERROR, "[$Storage->{errno}] $err_strs{$Storage->{errno}}");
			}
			
			$leftover = $list->[0]->[21];
			
			my $sell_price = $list->[0]->[29] * $FORM{COUNT};
			my $rent_price = $list->[0]->[30] * $FORM{COUNT};

			
			#$fees->{debug}=1;
						
			if($FORM{STATUS} == 1) {
				$fees->take($users, $sell_price, { DESCRIBE => "$_PAY_FOR_SELL $list->[0]->[14]" } );
			}
			elsif($FORM{STATUS} == 2) {
				$fees->take($users, $rent_price, { DESCRIBE => "$_PAY_FOR_RENT $list->[0]->[14]" } );
			}			
			
			if ($leftover == '') {
				$leftover = 0;
			} 
			
			if (($leftover - $FORM{COUNT}) > -1 and $FORM{COUNT} ne '' and $FORM{COUNT} != 0) {
								
				$Storage->storage_installation_user_add({	
					%FORM,
					COUNT_INCOMING	=> $list->[0]->[2],
					SUM_TOTAL		=> $list->[0]->[23],
					MAIN_ARTICLE_ID		=>	$list->[0]->[0],
					STORAGE_INCOMING_ARTICLES_ID => $FORM{ARTICLE_ID},
														
				});
				
				
				
				
							
				if (! $Storage->{errno}) {
	
					$html->tpl_show(_include('storage_redirect', 'Storage'), { 
											SECTION => '&UID=' . $FORM{UID},
											MESSAGE => "$_ADDED",
					});		
				}	
				
			}
			else {
					$Storage->{ID}=$FORM{ID}; 
					$html->message('info', $_INFO, "$_CANT_INSTALL_MAX_VALUE $leftover ");
#					$html->tpl_show(_include('storage_hardware_installation', 'Storage'), { 
#						%$Storage, 
#						%FORM,
#						ARTICLE_ID		=> $Storage->{ARTICLE_ID}, 
#						ARTICLE_TYPES 	=> $Storage->{ARTICLE_TYPES},
#						STATUS 			=> $Storage->{STATUS},	
#						#NAS	=> $NAS, 						
#					});	
			}

		} else {
			$Storage->{ID}=$FORM{ID}; 
			$html->message('info', $_INFO, "$_CANT_DELETE_ERROR4");
			$html->tpl_show(_include('storage_hardware_installation', 'Storage'), { 
				%$Storage, 
				%FORM,
				ARTICLE_ID		=> $Storage->{ARTICLE_ID}, 
				ARTICLE_TYPES 	=> $Storage->{ARTICLE_TYPES},
				STATUS 			=> $Storage->{STATUS},	
				#NAS	=> $NAS, 						
			});				
		}
		
	}


# RETURN HARDWARE TO STORAGE -------------------------------

	elsif ($FORM{return_storage} and defined($FORM{UID}) and $FORM{UID} ne '' ) {
		
	#$Storage->{debug}=1;
	$list = $Storage->storage_installation_list({ ID => $FORM{return_storage}});		
	
	
	print $FORM{COMMENTS};
	if ($Storage->{errno}) {
		$html->message('err', $_ERROR, "[$Storage->{errno}] $err_strs{$Storage->{errno}}");
	}	
	
	$list2 = $Storage->storage_incoming_articles_list( { ID => $list->[0]->[1] });	
	
	if ($Storage->{errno}) {
    		$html->message('err', $_ERROR, "[$Storage->{errno}] $err_strs{$Storage->{errno}}");
	}
	
		$Storage->storage_installation_return ({
											COUNT_INCOMING	=> $list2->[0]->[2],
											SUM_TOTAL		    => $list2->[0]->[23],
											MAIN_ARTICLE_ID	=> $list2->[0]->[0],
											COUNT 			    => $list->[0]->[6],
											ID_INSTALLATION	=> $FORM{return_storage},
											SUM 			      => $list->[0]->[19],	
											RETURN_STATUS	  => 1,
											COMMENTS		    => $FORM{COMMENTS},
										 });
										 
		if (! $Storage->{errno}){
			
				$html->tpl_show(_include('storage_redirect', 'Storage'), { 
					SECTION => "&UID=$FORM{UID}",
					MESSAGE => "$_RETURNED $list->[0]->[9] $list->[0]->[6] $list2->[0]->[15] $_AT_STORAGE  - $storage_storages[$list2->[0]->[24]]",
				});
		}	

	}

# CHANGE USER HARDWARE -------------------------------------

	if ($FORM{change}) {

			#$Storage->{debug}=1;
			$Storage->storage_installation_change({ %FORM });
			if (! $Storage->{errno}) {
				$html->tpl_show(_include('storage_redirect', 'Storage'), { 
					SECTION => '&UID=' . $FORM{UID},
					MESSAGE => "$_CHANGED",
				});			
			}
	}

	
	#$Storage->{debug}=1;
	if ($FORM{chg} and defined($FORM{UID}) and $FORM{UID} ne '') {		
		$Storage->{ACTION}='change';
		$Storage->{ACTION_LNG}=$_CHANGE;
		$Storage->{DISABLE}="disabled='disabled'";
		
		
		#$Storage->suppliers_info({ ID => $FORM{chg} });
		$Storage->storage_installation_info({ ID => $FORM{chg} });
		$Storage->{ARTICLE_ID1} = $Storage->{ARTICLE_ID};
		$Storage->{COUNT1} = $Storage->{COUNT};
		
		if (! $Storage->{errno}) {
			$html->message('info', $_INFO, "$_CHANGING");	
		}
		

	}
	
	#defined($Storage->{DISABLE})  ? $Storage->{DISABLE} : '';

	$Storage->{ARTICLE_TYPES}=$html->form_select("ARTICLE_TYPE_ID", 
									{	SELECTED          => defined($FORM{ARTICLE_TYPE_ID}) ? $FORM{ARTICLE_TYPE_ID} : $Storage->{ARTICLE_TYPE_ID},
										SEL_MULTI_ARRAY   => [ @{ $Storage->storage_types_list() } ], 
										MULTI_ARRAY_KEY   => 0,
										MULTI_ARRAY_VALUE => 1,
										NO_ID             => 1,
										EX_PARAMS			    => 'onchange=autoReload() ' . $Storage->{DISABLE},
										MAIN_MENU         => get_function_index('storage_articles_types'),
	});
	

	
	$val = $Storage->storage_types_list();
	$FORM{ARTICLE_TYPE_ID} = defined($FORM{ARTICLE_TYPE_ID}) ? $FORM{ARTICLE_TYPE_ID} : (defined($Storage->{ARTICLE_TYPE_ID}) ? $Storage->{ARTICLE_TYPE_ID} : $val->[0]->[0]);
	
	#$Storage->{debug}=1;
	
	$Storage->{ARTICLE_ID}=$html->form_select("ARTICLE_ID", {
										SELECTED          => defined($FORM{ARTICLE_ID}) ? $FORM{ARTICLE_ID} : $Storage->{ARTICLE_ID},
										SEL_MULTI_ARRAY   => [ @{  $Storage->storage_incoming_articles_list({ ARTICLE_TYPES => $FORM{ARTICLE_TYPE_ID}, HIDE_ZERO_VALUE => 1 }) } ], 
										MULTI_ARRAY_KEY   => 0,
										MULTI_ARRAY_VALUE => '14, 21',
										NO_ID             => 1,
										EX_PARAMS => $Storage->{DISABLE}
	});
		
	
	$FORM{STATUS} = defined($FORM{STATUS}) ? $FORM{STATUS} : (defined($Storage->{STATUS_ID}) ? $Storage->{STATUS_ID} : 0);
	
	$Storage->{STATUS} = $html->form_select("STATUS", {
									SELECTED		=> $FORM{STATUS},
									SEL_ARRAY		=> [ @status ], 
									ARRAY_NUM_ID	=> 1,
									OUTPUT2RETURN	=> 1,	
	});

 if (in_array('Dhcphosts', \@MODULES)) {
	
	
	load_module('Dhcphosts', $html);
	#use Dhcphosts;
	#my $Dhcphosts = Dhcphosts->new($db, $admin, \%conf);
  	
  	

	
	if(defined($Storage->{NETWORK})) {
		$FORM{NETWORK} = $Storage->{NETWORK};	
	} 

  	my $network_index = get_function_index('dhcphosts_networks');
  	if($network_index) {
  		$Storage->{NETWORK_BUTTON} = $html->button($_INFO, "&index=$network_index&chg=$Storage->{NETWORK}", { CLASS => 'show rightAlignText' });
   	}	
	


	
	$Storage->{NETWORKS_SEL} = dhcphosts_network_sel();


	

}	
	if (!defined($FORM{return_comment})) { 
		
		$html->tpl_show(_include('storage_hardware_installation', 'Storage'), {	
			%FORM,
			%$Storage,
			ARTICLE_ID		=> $Storage->{ARTICLE_ID}, 
			ARTICLE_TYPES 	=> $Storage->{ARTICLE_TYPES},
			STATUS 			=> $Storage->{STATUS},		
		});
	} else {
		
		
		$Storage->{ACTION}='return';
		$Storage->{ACTION_LNG}= "$_RETURN_STORAGE";
		
		$html->tpl_show(_include('storage_return_comment_add', 'Storage'), { %$Storage } );
		
	}
	
	
	$FORM{SHOW_STATUS} = defined($FORM{SHOW_STATUS}) ? $FORM{SHOW_STATUS}: 5 ;
	
	if (defined($FORM{UID}) and $FORM{UID} ne '') {
	
		my $table = $html->table({	
			width      => '100%',
			border     => 1,
			title      => ['-', '-', $_LOGIN, $_TYPE, $_NAME, 'MAC', IP, $_SERIAL, $_STATUS, $_DATE, $_COMMENTS],
			cols_align => ['left', 'right', 'right', 'right', 'center', 'center'],
			pages      => $Storage->{TOTAL},
			ID         => 'STORAGE_ID',
			EXPORT     => $_EXPORT .' XML:&xml=1',
			header		=> 
				((defined($FORM{SHOW_STATUS}) && $FORM{SHOW_STATUS} == 5) ? $html->b($_ALL) : $html->button($_ALL, "index=$index&UID=$FORM{UID}&SHOW_STATUS=5", { BUTTON => 1 })) . ' ' .
				((defined($FORM{SHOW_STATUS}) && $FORM{SHOW_STATUS} == 2) ? $html->b($status[2]) : $html->button($status[2], "index=$index&UID=$FORM{UID}&SHOW_STATUS=2", { BUTTON => 1 })) . ' ' .
				(($FORM{SHOW_STATUS} == 1 ) ? $html->b($status[1]) : $html->button($status[1], "index=$index&UID=$FORM{UID}&SHOW_STATUS=1", { BUTTON => 1 })) . ' ' .
				(($FORM{SHOW_STATUS} == 4 ) ? $html->b($_RETURNED_STORAGE) : $html->button($_RETURNED_STORAGE, "index=$index&UID=$FORM{UID}&SHOW_STATUS=4", { BUTTON => 1 })) . ' ' .
				((!defined($FORM{SHOW_STATUS}) or $FORM{SHOW_STATUS} == 0 ) ? $html->b($status[0]) : $html->button($status[0], "index=$index&UID=$FORM{UID}&SHOW_STATUS=0", { BUTTON => 1 }))	

		});			
		#$Storage->{debug}=1;	
		
		
		#($FORM{SHOW_STATUS} == 2) ? "UID => $FORM{UID}, STATUS => $FORM{SHOW_STATUS}" : "UID => $FORM{UID}, STATUS => 1"
		
		$list = $Storage->storage_installation_list({ 
			UID => $FORM{UID},
			STATUS => defined($FORM{SHOW_STATUS}) ? $FORM{SHOW_STATUS} : 2,	
		});
		
		if ($Storage->{errno}) {
			$html->message('err', $_ERROR, "[$Storage->{errno}] $err_strs{$Storage->{errno}}");
		}
		
		
		
		foreach my $line ( @$list ) {
			my $br = $html->br();
			$line->[26] =~ s/\r\n/$br/g;
			my $dhcp_index = get_function_index('dhcphosts_user');			
			$table->addrow(	
				($line->[25] == 4) ? '' : $html->button($_INFO, "index=$index&chg=$line->[0]&UID=$FORM{UID}", { CLASS => 'change' }),  
				($line->[25] == 4) ? '' : (defined($permissions{0}{5})) ? $html->button($_RETURN_STORAGE, "index=$index&return_comment=$line->[0]&UID=" . $FORM{UID}, { MESSAGE => "$_RETURN_STORAGE $line->[9]?", CLASS => 'del' }) : '',
				$line->[20],  
				$line->[10], 
				$line->[9], 
				$html->button($line->[21], "index=$dhcp_index&UID=$FORM{UID}"),
				$line->[27],  
				$line->[26],
				($line->[25] != 4) ? $status[$line->[25]] : "$_RETURNED_STORAGE",
				$line->[23],
				$line->[7],
				#$line->[2], DATE
	
			);
		}
		
		
		print $table->show();
		
		
		#$Storage->{debug}=1;

		
# USER HARDWARE LOG	----------------------------------------
	
		if($FORM{VIEW_HARDWARE_LOG}) {
		
			
			my @action = (	"$_ARRIVAL_OF_THE_GOODS", 
							"$_INSTALLATION", 
							"$_DISCARDED", 
							"$_NARRATED_TO_ACCOUNTABILITY", 
							"$_RETURN_TO_THE_MANUFACTURER", 
							"$_RESERVE", 
							"$_RETURNED_STORAGE", 
							"$_RECORD_CREATED", 
							"$_RECORD_CHANGED",);
			

				print $html->button("$_HIDE_HARDWARE_LOG", "index=$index&UID=$FORM{UID}&SHOW_STATUS=$FORM{SHOW_STATUS}", { BUTTON => 1 });
			
			
			my $table = $html->table({	
				width      => '100%',
				border     => 1,
				title      => [$_LOGIN, $_NAME, 'MAC', IP, $_SERIAL, $_ACTION, $_STATUS, $_DATE, $_COMMENTS, $_GROUND],
				pages      => $Storage->{TOTAL},
				ID         => 'STORAGE_ID',
			});				
			
			
				#print 'ololo';	
			$list = $Storage->storage_installation_log({ 
				UID => $FORM{UID},
				STATUS => defined($FORM{SHOW_STATUS}) ? $FORM{SHOW_STATUS} : 2,	
			});
			
			if ($Storage->{errno}) {
				$html->message('err', $_ERROR, "[$Storage->{errno}] $err_strs{$Storage->{errno}}");
			}
			
			
			
			foreach my $line ( @$list ) {
				my $br = $html->br();
			  $line->[26] =~ s/[\r\n]+/$br/g;
				$dhcp_index = get_function_index('dhcphosts_user');			
				$table->addrow(	
					$line->[14],  
					$line->[9], 
					$html->button("$line->[15]", "index=$dhcp_index&UID=$FORM{UID}", { BUTTON => 1 }),		
					$line->[20],  
					$line->[19],
					$action[$line->[22]] . $html->br() . $_AUTHOR . $line->[8],
					($line->[18] != 4) ? $status[$line->[18]] : "$_RETURNED_STORAGE",
					$line->[21],
					$line->[23],
					$line->[16]
				);
			}
			
			
			print $table->show();						
		}
		else {
      print $html->button("$_HIDE_HARDWARE_LOG", "index=$index&UID=$FORM{UID}&SHOW_STATUS=$FORM{SHOW_STATUS}&VIEW_HARDWARE_LOG=1", { BUTTON => 1 });		
		}
	}
}

#***********************************************************
# 	storage_monthly_fees
#***********************************************************

sub storage_monthly_fees {
	my ($attr) = @_;
	 		
	$ADMIN_REPORT{DATE}=$DATE if (! $ADMIN_REPORT{DATE});
	#$ADMIN_REPORT{DATE} = '2011-12-01';
	my ($y, $m, $d)=split(/-/, $ADMIN_REPORT{DATE}, 3);
	
	if ($d == 01) {

	#$Storage->{debug}=1;	
	my $list = $Storage->storage_rent_fees({});
		
		foreach my $line (@$list) {
			
			$users->{BILL_ID} = $line->[4];
			$users->{UID} = $line->[1];
					
			$line->[2] = $line->[2] * $line->[3];
				
			#$fees->{debug}=1;
			$fees->take($users, $line->[2], { DESCRIBE => "$_PAY_FOR_RENT $line->[5]" } );	
				
			if ($fees->{errno}) {
				$html->message('err', $_ERROR, "[$fees->{errno}] $err_strs{$fees->{errno}}");
			}				
				
				
				
		}
	}
	
	#print 'ok';
}
