# Docs functions


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




#**********************************************************
# docs_print
#**********************************************************
sub docs_contract {
  my ($attr) = @_;
	
	if ($FORM{UID}) {
		my $user_main_info = $users->info($FORM{UID}, { SHOW_PASSWORD => 1 });
		$Docs              = $users->pi({ UID => $FORM{UID} });

		require "../../Abills/modules/Docs/lng_$conf{default_language}.pl";
    my ($y, $m, $d)=split(/-/, (($users->{CONTRACT_DATE}) ? $users->{CONTRACT_DATE} : $DATE ) , 3);
    $Docs->{CONTRACT_DATE_LIT} = "$d ". $MONTHES_LIT[int($m)-1] ." $y $_YEAR";
    
    #$Docs->{ADDRESS_FULL}="$Docs->{ADDRESS_STREET}, $Docs->{ADDRESS_BUILD}/$Docs->{ADDRESS_FLAT}"; 
		
		%{ $attr } = %{ Docs }, %{ $user_main_info };
		
		docs_print('contract', { %$admin, %$Docs, %$attr });
	 }
}


#**********************************************************
# docs_invoice_add
# Order array format
# NAME|UNIT|COUNT|PRICE
#**********************************************************
sub docs_invoice_add  {
	my ($attr) = @_;

  if ($attr->{create}) {
   if (defined($FORM{OP_SID}) and $FORM{OP_SID} eq $COOKIES{OP_SID}) {
 	   $html->message('err', $_ERROR, "$_INVOICE $_EXIST");
 	   return 0;
    }
   elsif(! $attr->{CUSTOMER}) {
     $html->message('err', "$_ERROR", "$_ORG_NAME");
    }
   elsif($FORM{PREVIEW}) {
     docs_preview('invoice', { %FORM });
     return 0;
    }
   else {
     $Docs->{FROM} = $attr->{FROM} if ($attr->{FROM});
     $Docs->docs_invoice_add({ %$attr });

     if(! $Docs->{errno}) {
       $html->message('info', "$_ADDED", "$_NUM: [$Docs->{DOC_ID}] $_DATE: $FORM{DATE}");
       $Docs->docs_invoice_info($Docs->{DOC_ID});
      }
     elsif ($Docs->{errno} && $Docs->{errno} == 1) {
     	 $html->message('err', $_ERROR, "$ERR_NO_ORDERS");	
     	 return 0;
      }
     elsif ($Docs->{errno} ) {
       $html->message('err', $_ERROR, "[$Docs->{errno}] $err_strs{$Docs->{errno}} $Docs->{errstr}");	
       return 0;
      }
    }
  }

  $Docs->{TOTAL_SUM}=0.00;
  if ($Docs->{ORDERS}) {
		my $i = 1;
		my @ORDERS = @{ $Docs->{ORDERS} };
		$Docs->{ORDER}='';
		foreach my $line (@ORDERS) {
			my $sum = sprintf("%.2f", $line->[3] * $line->[4]);
		  $Docs->{ORDER}.="<tr><th align='right'>$i</th><td align='left'>$line->[1]</td><td align='center'>$units[$line->[2]]</td>
		  <td align='right'>$line->[3]</td><td align='right'>$line->[4]</td><td align='right'>$sum</td></tr>";

		  $i++;
		  $Docs->{TOTAL_SUM}+=$sum;
		 }
	 }

  $Docs->{TOTAL_SUM} = sprintf("%.2f", $Docs->{TOTAL_SUM});

  if ($conf{DOCS_PDF_PRINT}) {
  	 $html->message('info', $_ADDED, "$_INVOICE $_ADDED. ". $html->button($_PRINT, "qindex=$index&print=$Docs->{DOC_ID}&UID=$LIST_PARAMS{UID}&pdf=1", { ex_params => 'target=_new' }));
   	 return 0;
   }


  docs_print('invoice', $Docs);
}

#**********************************************************
# docs_invoice_list
#**********************************************************
sub docs_invoice {
  my ($attr) = @_;

  if ($FORM{create}) {
    docs_invoice_add({ %FORM }); 
    return 0;
   }
  elsif (! $FORM{print}) {
    $Docs->{SEL_ORDER} .= $html->form_select('ORDER', 
                                    { 
 	                                   SELECTED    => $FORM{ORDER},
 	                                   SEL_ARRAY   => ($conf{DOCS_ORDERS})? $conf{DOCS_ORDERS} : [$_DV],
                                     NO_ID       => 1
 	                                  });

    #$Docs->{COMPANY_VAT}=$user->{COMPANY_VAT}.' %';
    $Docs->{OP_SID}  = mk_unique_value(16);
    $users->pi({ $users->{UID} });

    $Docs->{CUSTOMER}=$users->{FIO} || '-';
    $Docs->{CAPTION}=$_INVOICE;

    $html->tpl_show(_include('docs_account_add', 'Docs'), { %$Docs, %$users });
   }


  docs_invoice_list();
}

#**********************************************************
# docs_invoice_list
#**********************************************************
sub docs_invoice_list {


#if ($LIST_PARAMS{UID}) {
#	docs_account();
#	return 0 if ($FORM{'print'});
# }
#els
if($FORM{del} && $FORM{is_js_confirmed}) {
  $Docs->docs_invoice_del($FORM{del});
  if(! $Docs->{errno}) {
    $html->message('info', "$_INFO", "$_DELETED N: [$FORM{del}]");
   }
  elsif ($Docs->{errno}) {
    $html->message('err', $_ERROR, "[$Docs->{errno}] $err_strs{$Docs->{errno}}");	
    return 0;
   }
 }
elsif($FORM{print}) {
  
  $Docs->docs_invoice_info($FORM{print}, { UID => $LIST_PARAMS{UID} });
  ($Docs->{SUM_MAIN}, $Docs->{SUM_SUB})=split(/\./, $Docs->{TOTAL_SUM});
   $Docs->{TOTAL_SUM}=0.00;
  
 
  if ($Docs->{ORDERS}) {
		my $i = 1;
		my @ORDERS = @{ $Docs->{ORDERS} };
		$Docs->{ORDER}='';
		foreach my $line (@ORDERS) {
			
			my $sum = sprintf("%.2f", $line->[3] * $line->[4]);
      $Docs->{ORDER}.="<tr><th align='right'>$i</th><td align='left'>$line->[1]</td><td align='center'>$units[$line->[2]]</td>
		      <td align='right'>$line->[3]</td><td align='right'>$line->[4]</td><td align='right'>$sum</td></tr>";
		  
   	  $Docs->{'ORDER_NUM_'.$i}   = $i;
      $Docs->{'ORDER_NAME_'.$i}  = $line->[1];
      $Docs->{'ORDER_COUNT_'.$i} = $line->[2];
      $Docs->{'ORDER_PRICE_'.$i} = $line->[4];
      $Docs->{'ORDER_SUM_'.$i}   = $sum;
       
      $Docs->{'ORDER_PRICE_WITHOUT_VAT_'.$i} = sprintf("%.2f", ($conf{DOCS_VAT_INCLUDE}) ? $line->[4] - $line->[4] / ((100 + $conf{DOCS_VAT_INCLUDE} ) / $conf{DOCS_VAT_INCLUDE}) : $line->[4]);
      $Docs->{'ORDER_SUM_WITHOUT_VAT_'.$i}   = sprintf("%.2f", ($conf{DOCS_VAT_INCLUDE}) ? $sum - $sum / ((100 + $conf{DOCS_VAT_INCLUDE} ) / $conf{DOCS_VAT_INCLUDE}) : $sum);

      
      		  
		  $Docs->{TOTAL_SUM}+=$sum;
		  $i++;
   	 }
	 }

  $Docs->{TOTAL_SUM} = sprintf("%.2f", $Docs->{TOTAL_SUM});
  
  if ($conf{DOCS_VAT_INCLUDE}) {
   	 $Docs->{ORDER_TOTAL_SUM_VAT} = sprintf("%.2f", $Docs->{TOTAL_SUM} / ((100 + $conf{DOCS_VAT_INCLUDE} ) / $conf{DOCS_VAT_INCLUDE}));
   	 $Docs->{TOTAL_SUM_WITHOUT_VAT}=sprintf("%.2f",$Docs->{TOTAL_SUM} - $Docs->{ORDER_TOTAL_SUM_VAT});
   	 $Docs->{VAT}=sprintf("%.2f", $conf{DOCS_VAT_INCLUDE});
   }
 
  docs_print('invoice', $Docs);
  return 0;
 }
elsif (! $LIST_PARAMS{UID}) {
  form_search({ SEARCH_FORM => $html->tpl_show(_include('docs_search', 'Docs'), 
  	                                  { %info, %FORM }, 
  	                                  { notprint => 1 })  
	            });
}


#  Date  Customer  Sum  User  Administrators  Time  

if (! defined($FORM{sort})) {
  $LIST_PARAMS{SORT}=1;
  $LIST_PARAMS{DESC}='DESC';
}

my $list = $Docs->docs_invoice_list( { %LIST_PARAMS } );

if ($conf{DOCS_PDF_PRINT}) {
	$pages_qs .= "&pdf=1";
}


my $table = $html->table( { width      => '100%',
                            caption    => "$_INVOICE",
                            border     => 1,
                            title      => ['#', $_DATE, $_CUSTOMER, $_SUM, $_USER, $_ADMIN, $_TIME, '-', '-'],
                            cols_align => ['right', 'right', 'left', 'right', 'left', 'left', 'right', 'center'],
                            qs         => $pages_qs,
                            pages      => $Docs->{TOTAL},
                            ID         => 'DOCS_INVOCE'
                          });




foreach my $line (@$list) {
  my $delete = ($permissions{1}{2}) ?  $html->button($_DEL, "index=$index&del=$line->[8]&UID=$line->[7]", { MESSAGE => "$_DEL ?" }) : ''; 

  $table->addrow("$line->[0]", 
   "$line->[1]",
   "$line->[2]", 
   "$line->[3]", 
   $html->button($line->[4], "index=11&UID=$line->[7]"),
   "$line->[5]", 
   "$line->[6]", 
   $html->button($_PRINT, "qindex=$index&print=$line->[8]&UID=$line->[7]$pages_qs", { ex_params => 'target=_new' }) ,
   $delete);
}
print $table->show();

$table = $html->table( { width      => '100%',
                         cols_align => ['right', 'right'],
                         rows       => [ [ "$_TOTAL:", $html->b($Docs->{TOTAL}) ] ]
                      } );


print $table->show();
}

#**********************************************************
# docs_accounts_list
#**********************************************************
sub docs_accounts_list {

if ($LIST_PARAMS{UID} || $FORM{UID}) {
	docs_account();
	return 0 if ($FORM{'print'});
 }
elsif(defined($FORM{del}) && $FORM{is_js_confirmed} ) {
  $Docs->account_del($FORM{del});
  if(! $Docs->{errno}) {
    $html->message('info', "$_INFO", "$_DELETED N: [$FORM{del}]");
   }
  elsif ($Docs->{errno}) {
    $html->message('err', $_ERROR, "[$Docs->{errno}] $err_strs{$Docs->{errno}}");	
    return 0;
   }
 }
else {
  form_search({ SEARCH_FORM => $html->tpl_show(_include('docs_search', 'Docs'), 
  	                                  { %info, %FORM }, 
  	                                  { notprint => 1 })  
	            });
}


#  Date  Customer  Sum  User  Administrators  Time  

if (! defined($FORM{sort})) {
  $LIST_PARAMS{SORT}=1;
  $LIST_PARAMS{DESC}='DESC';
}


if ($conf{DOCS_PDF_PRINT}) {
	$pages_qs .= "&pdf=1";
}

my $list = $Docs->accounts_list( { %LIST_PARAMS } );
my $table = $html->table( { width      => '100%',
                            border     => 1,
                            caption    => "$_ACCOUNTS",
                            title      => ['#', $_DATE, $_CUSTOMER, $_SUM, $_USER, $_ADMIN, $_TIME, '-', '-'],
                            cols_align => ['right', 'right', 'left', 'right', 'left', 'left', 'right', 'center:noprint', 'center:noprint'],
                            qs         => $pages_qs,
                            pages      => $Docs->{TOTAL},
                            ID         => 'DOCS_ACCOUNTS_LIST'
                          });


foreach my $line (@$list) {
  my $delete = ($permissions{1}{2}) ?  $html->button($_DEL, "index=$index&del=$line->[8]&UID=$line->[7]", { MESSAGE => "$_DEL ID $line->[8] ?" }) : ''; 

  $table->addrow("$line->[0]", 
   "$line->[1]",
   "$line->[2]", 
   "$line->[3]", 
   $html->button($line->[4], "index=11&UID=$line->[7]"),
   "$line->[5]", 
   "$line->[6]", 
   $html->button($_PRINT, "qindex=$index&print=$line->[8]&UID=$line->[7]$pages_qs", { ex_params => 'target=_new' }), 
   $delete);
}
print $table->show();

$table = $html->table( { width      => '100%',
                         cols_align => ['right', 'right'],
                         rows       => [ [ "$_TOTAL:", $html->b($Docs->{TOTAL}) ] ]
                      } );


print $table->show();

return 0;
}



#**********************************************************
# docs_tax_invoice_list
#**********************************************************
sub docs_tax_invoice_list {



if ($LIST_PARAMS{COMPANY_ID} || $FORM{COMPANY_ID}) {
	docs_tax_invoice();
	return 0 if ($FORM{'print'});
 }
elsif(defined($FORM{del}) && $FORM{is_js_confirmed} ) {
  $Docs->tax_invoice_del($FORM{del});
  if(! $Docs->{errno}) {
    $html->message('info', "$_INFO", "$_DELETED N: [$FORM{del}]");
   }
  elsif ($Docs->{errno}) {
    $html->message('err', $_ERROR, "[$Docs->{errno}] $err_strs{$Docs->{errno}}");	
    return 0;
   }
 }
elsif($FORM{print}) {
	docs_tax_invoice();
	
	return 0;
 }
else {
  form_search({ SEARCH_FORM => $html->tpl_show(_include('docs_search', 'Docs'), 
  	                                  { %info, %FORM }, 
  	                                  { notprint => 1 })  
	            });
 }


#  Date  Customer  Sum  User  Administrators  Time  

if (! defined($FORM{sort})) {
  $LIST_PARAMS{SORT}=1;
  $LIST_PARAMS{DESC}='DESC';
}


if ($conf{DOCS_PDF_PRINT}) {
	$pages_qs .= "&pdf=1";
}

my $list = $Docs->tax_invoice_list( { %LIST_PARAMS, COMPANY_ID => $FORM{COMPANY_ID} } );
my $table = $html->table( { width      => '100%',
                            border     => 1,
                            caption    => "$_TAX_INVOICE",
                            title      => ['#', $_DATE, $_CUSTOMER, $_SUM, $_ADMIN, $_TIME, '-', '-'],
                            cols_align => ['right', 'right', 'left', 'right', 'left', 'left', 'right', 'center:noprint', 'center:noprint'],
                            qs         => $pages_qs,
                            pages      => $Docs->{TOTAL},
                            ID         => 'DOCS_TAX_INVOICE'
                          });


foreach my $line (@$list) {
  my $delete = ($permissions{1}{2}) ?  $html->button($_DEL, "index=$index&del=$line->[8]&COMPANY_ID=$line->[7]", { MESSAGE => "$_DEL ID '$line->[8]' ?" }) : ''; 

  $table->addrow("$line->[0]", 
   "$line->[1]",
   $html->button("$line->[2]", "index=13&COMPANY_ID=$line->[7]"),, 
   "$line->[3]", 
   $html->button($line->[4], "index=11&UID=$line->[7]"),
   "$line->[5]", 
   $html->button($_PRINT, "qindex=$index&print=$line->[8]&COMPANY_ID=$line->[7]". (($pages_qs) ? $pages_qs : "&COMPANY_ID=$line->[7]"  ) , { ex_params => 'target=_new' }), 
   $delete);
}
print $table->show();

$table = $html->table( { width      => '100%',
                         cols_align => ['right', 'right'],
                         rows       => [ [ "$_TOTAL:", $html->b($Docs->{TOTAL}), "$_SUM:", $html->b($Docs->{SUM}),  ] ]
                      } );


print $table->show();

return 0;
}


#**********************************************************
# docs_tax_invoice
#**********************************************************
sub docs_tax_invoice {
	my ($attr) = @_;

  my $Customer = Customers->new($db, $admin, \%conf);
  my $Company = $Customer->company();
  $Company->info($FORM{COMPANY_ID});

  if (! $FORM{qindex}) {
    my $table = $html->table({ width       => '100%',
	                             rowcolor    => $_COLORS[3],
                               rows        => [[ $_NAME, $Company->{COMPANY_NAME} ]],
                            });
	  print $table->show();
  }

  if ($Company->{TOTAL} < 1){
  	$html->message('err', $_ERROR, "$_NOT_EXIST");
   }

  $LIST_PARAMS{COMPANY_ID}=$FORM{COMPANY_ID};

# $Docs->tax_invoice_defaults();		
  $Docs->{DATE}=$DATE;
  $Docs->{DONE_DATE}=$DATE;
  $Docs->{FROM_DATE}=$DATE;

 if ($FORM{create}) {
   # get fees info
   #my $list = $Fees->list({ MONTH      => $FORM{MONTH},
   #	                        BILL_ID    => $Company->{BILL_ID}
   #	                        });

   my $list = $Fees->reports({ MONTH      => $FORM{MONTH},
   	                           BILL_ID    => $Company->{BILL_ID},
   	                           TYPE       => 'METHOD'
   	                        });

   if ($Fees->{TOTAL} < 1) {
     $html->message('info', $_INFO, "$_FEES $_NOT_EXIST");
    }
  
   my @FEES_METHODS = ($_ONE_TIME, $_ABON, $_FINE, $_ACTIVATE);
   push @FEES_METHODS, @EX_FEES_METHODS if (@EX_FEES_METHODS);


   my $i = 1;
   foreach my $line (@$list) {
      $FORM{SUM}=$Fees->{SUM};    
      $FORM{'IDS'}     .= "$i, ";
      $FORM{'ORDER_'. $i}= $FEES_METHODS[$line->[0]];
      $FORM{'COUNTS_'.$i}= '1';
      $FORM{'UNIT_'.$i}  = '1';
      $FORM{'SUM_'.$i}   = $line->[3];
      $i++;
    }


   if (defined($FORM{OP_SID}) and $FORM{OP_SID} eq $COOKIES{OP_SID}) {
 	   $html->message('err', $_ERROR, "$_EXIST");
    }
   elsif (! $FORM{SUM_1} && $FORM{SUM} < 0.01) {
     $html->message('err', "$_ERROR", $_WRONG_SUM);
    }
   elsif($FORM{PREVIEW}) {
     docs_preview('tax_invoice', { %FORM });
     return 0;
    }
   else {
     $FORM{COMPANY_ID}=$LIST_PARAMS{COMPANY_ID} if (! $FORM{COMPANY_ID});
     
     $Docs->tax_invoice_add({ %FORM, DATE => "$FORM{MONTH}-01" });

     if(! $Docs->{errno}) {
    
       $html->message('info', "$_ADDED", "$_NUM: [$Docs->{DOC_ID}] $_DATE: $FORM{DATE}");
       $Docs->tax_invoice_info($Docs->{DOC_ID}, { COMPANY_ID => $LIST_PARAMS{COMPANY_ID} });
       ($Docs->{SUM_MAIN}, $Docs->{SUM_SUB})=split(/\./, $Docs->{TOTAL_SUM});
       $Docs->{NUMBER}=$Docs->{DOC_ID};

       my $list = $Docs->{ORDERS};
       my $i=0;

       foreach my $line (@$list) {
   	     $i++;
   	     $Docs->{ORDER} .= sprintf("<tr><td align='right'>%d</td><td>%s</td><td align='right'>%d</td><td align='right'>%d</td><td align='right'>%.2f</td><td align='right'>%.2f</td></tr>\n", 
   	       $i, $line->[1], $line->[2], $line->[3], $line->[4], ($line->[3]*$line->[4]));
  
        }

       if ($conf{DOCS_PDF_PRINT}) {
       	 $html->message('info', $_ADDED, "$_TAX_INVOICE $_ADDED. ". $html->button($_PRINT, "qindex=$index&print=$Docs->{DOC_ID}&UID=$LIST_PARAMS{UID}&COMPANY_ID=$FORM{COMPANY_ID}&pdf=1", { ex_params => 'target=_new' }));
       	 return 0;
        }

      
       #print $html->header() if ($FORM{qindex});
       if( $user->{UID} ) {
         $FORM{qindex}    = $index;
         $html->{NO_PRINT}= undef;

         docs_print('tax_invoice', { %$Docs, %$Company,  A_FIO => $admin->{A_FIO} });
         exit;
        }
       else {
         docs_print('tax_invoice', { %$Docs, %$Company, A_FIO => $admin->{A_FIO} });
        }

       $FORM{'print'}=1;
       return 0;
      }
     elsif ($Docs->{errno} == 7) {
     	 $html->message('err', $_ERROR, "$_EXIST");	
      }
     else {
     	 $html->message('err', $_ERROR, "[$Docs->{errno}] $err_strs{$Docs->{errno}}");	
      }
    }
  }
 elsif($FORM{print}){
   $Docs->tax_invoice_info($FORM{print}, { UID => $LIST_PARAMS{UID} });
   
   if ($conf{DOCS_VAT_INCLUDE}) {
   	 $Docs->{ORDER_TOTAL_SUM_VAT}  = sprintf("%.2f", $Docs->{TOTAL_SUM} / ((100 + $conf{DOCS_VAT_INCLUDE} ) / $conf{DOCS_VAT_INCLUDE}));
   	 $Docs->{TOTAL_SUM_WITHOUT_VAT}= sprintf("%.2f", $Docs->{TOTAL_SUM} - $Docs->{ORDER_TOTAL_SUM_VAT});
   	 $Docs->{VAT}=sprintf("%.2f", $conf{DOCS_VAT_INCLUDE});
    }

   $Docs->{NUMBER}=$Docs->{DOC_ID};

   if ($Docs->{TOTAL} > 0) {
   	 ($Docs->{SUM_MAIN}, $Docs->{SUM_SUB})=split(/\./, $Docs->{TOTAL_SUM});
      $Docs->{FROM_DATE_LIT}='';

     my $list = $Docs->{ORDERS};
     my $i=0;
     foreach my $line (@$list) {
   	   $i++;
   	   $Docs->{ORDER} .= sprintf("<tr><td align=right>%d</td><td>%s</td><td align=right>%d</td><td align=right>%d</td><td align=right>%.2f</td><td align=right>%.2f</td></tr>\n", 
   	     $i, $line->[1], $line->[2], $line->[3], $line->[4], ($line->[2]*$line->[4]));
   	   
   	   my $count = $line->[2] || 1;
   	   my $sum   = sprintf("%.2f", $count*$line->[4]);
   	   
   	   $Docs->{'ORDER_NUM_'.$i}   = $i;
       $Docs->{'ORDER_NAME_'.$i}  = $line->[1];
       $Docs->{'ORDER_COUNT_'.$i} = $count;
       $Docs->{'ORDER_PRICE_'.$i} = $line->[4];
       $Docs->{'ORDER_SUM_'.$i}   = $sum;
       
       $Docs->{'ORDER_PRICE_WITHOUT_VAT_'.$i} = sprintf("%.2f", ($conf{DOCS_VAT_INCLUDE}) ? $line->[4] - $line->[4] / ((100 + $conf{DOCS_VAT_INCLUDE} ) / $conf{DOCS_VAT_INCLUDE}) : $line->[3]);
       $Docs->{'ORDER_SUM_WITHOUT_VAT_'.$i}   = sprintf("%.2f", ($conf{DOCS_VAT_INCLUDE}) ? $sum - ($sum) / ((100 + $conf{DOCS_VAT_INCLUDE} ) / $conf{DOCS_VAT_INCLUDE}) : $sum );
      }

     
#print "Content-Type: text/html\n\n";
     $Docs->{TOTAL_SUM} = sprintf("%.2f", $Docs->{TOTAL_SUM});
     $Docs->{'TOTAL_SUM_WITHOUT_VAT'}   = sprintf("%.2f", ($conf{DOCS_VAT_INCLUDE}) ? $Docs->{TOTAL_SUM} - ($Docs->{TOTAL_SUM}) / ((100 + $conf{DOCS_VAT_INCLUDE} ) / $conf{DOCS_VAT_INCLUDE}) : $Docs->{TOTAL_SUM} );
     $Docs->{'TOTAL_SUM_VAT'} = sprintf("%.2f", $Docs->{TOTAL_SUM}-$Docs->{'TOTAL_SUM_WITHOUT_VAT'});
     
     my $i=0; 
     foreach my $field_id ( @{ $Company->{INFO_FIELDS_ARR} } ) {
       my($position, $type, $name)=split(/:/, $Company->{INFO_FIELDS_HASH}->{$field_id});
       $Company->{$field_id}=$Company->{INFO_FIELDS_VAL}->[$i];
       
       #print "";
       $i++;
     }
     
#     $Company->{_ipn_u}='123456790';
#     exit;
     docs_print('tax_invoice', { %$Docs, %$Company, A_FIO => $admin->{A_FIO} });
    }
   else {

     $html->message('err', $_ERROR, "[$Docs->{errno}] $err_strs{$Docs->{errno}}");	
    }
   return 0;
  }
 elsif(defined($FORM{change})) {
   $Docs->tax_invoice_change({ %FORM });
   if(! $Docs->{errno}) {
     $html->message('info', "$_INFO", "$_CHANGED N: [$FORM{DOC_ID}]");
    }
  }
 elsif(defined($FORM{chg})) {
   $Docs->tax_invoice_info($FORM{chg});
   if(! $Docs->{errno}) {
     $html->message('info', "$_INFO", "$_CHANGING N: [$FORM{chg}]");
    }
  }
 elsif(defined($FORM{del}) && $FORM{is_js_confirmed}) {
   $Docs->tax_invoice_del($FORM{del});
   if(! $Docs->{errno}) {
     $html->message('info', "$_INFO", "$_DELETED N: [$FORM{del}]");
    }
  }
	
	if(! $user->{UID}  ) {
  	$Docs->{FORM_ACCT_ID} = "<tr><td>$_TAX_INVOICE $_NUM:</td><td><input type='text' name='ACCT_ID' value='%ACCT_ID%'></td></tr>\n";
   }

  $Docs->{SEL_ORDER} .= $html->form_select('ORDER', 
                                    { 
 	                                   SELECTED    => $FORM{ORDER},
 	                                   SEL_ARRAY   => ($conf{DOCS_ORDERS})? $conf{DOCS_ORDERS} : [$_DV],
                                     NO_ID       => 1
 	                                  });

  #$Docs->{COMPANY_VAT}=$user->{COMPANY_VAT}.' %';
  $Docs->{OP_SID}  = mk_unique_value(16);
  $users->pi({  $users->{UID} });

  $Docs->{CUSTOMER}=  $users->{FIO} || '-';
  $Docs->{CAPTION} =  $_ACCOUNT;

  my ($Y, $M, $D)=split(/-/, $DATE, 3);  
  my @MONTH_ARR = ('');
  for(my $i=1; $i<13; $i++) {
	  my $m = sprintf("%02.d", $i);
	  push @MONTH_ARR, "$Y-$m";
   }
  $FORM{MONTH}="$Y-$M" if (! $FORM{MONTH});


  #my $company_users_list = $users->list({ COMPANY_ID => $FORM{COMPANY_ID} });  
  #my $users_list   =   $html->form_select("UID",
  #                              { SELECTED          => $FORM{UID},
  #                                SEL_MULTI_ARRAY   => $company_users_list,
  #                                MULTI_ARRAY_KEY   => 5+$users->{SEARCH_FIELDS_COUNT},
  #                                MULTI_ARRAY_VALUE => 0,
  #                                NO_ID             => 1
  #                                     });
  
  my $table = $html->table( { width       => '100%',
  	                          caption     => "$_TAX_INVOICE",
	                            rowcolor    => $_COLORS[0],
                              rows        => [[ 
                                                #"$_USER: ", $users_list,
                                                "$_PERIOD: ", $html->form_select('MONTH', 
                                                           { SELECTED   => $FORM{MONTH},
 	                                                           SEL_ARRAY  => \@MONTH_ARR,
 	                                                           NO_ID      => 1
                                                          } ),
                                                 "$_DATE: ", $html->form_input('DATE', "$Y-$M-01"),
                                          $html->form_input('show', $_CREATE, { TYPE => "SUBMIT" })
                                         ]],                                   
                      });

  print $html->form_main({ CONTENT => $table->show({ OUTPUT2RETURN => 1 }),
	                         HIDDEN  => { COMPANY_ID => "$FORM{COMPANY_ID}",
	                         	            index      => $index,
	                                      create     => "1"
	                                     }
	                       });

  #$html->tpl_show(_include('docs_tax_invoice_add', 'Docs'), { %$Docs, %$users });
}


#**********************************************************
# docs_account
#**********************************************************
sub docs_reports {
	my ($attr) = @_;
	
}



#**********************************************************
# docs_account
#**********************************************************
sub docs_account {
 my ($attr) = @_;

 $Docs->account_defaults();		
 $Docs->{DATE}=$DATE;

 if ($FORM{create}) {
   if (defined($FORM{OP_SID}) and $FORM{OP_SID} eq $COOKIES{OP_SID}) {
 	   $html->message('err', $_ERROR, "$_EXIST");
    }
   elsif (! $FORM{SUM_1} && $FORM{SUM} < 0.01) {
     $html->message('err', "$_ERROR", $_WRONG_SUM);
    }
   elsif(! $LIST_PARAMS{UID}) {
     $html->message('err', "$_ERROR", "$_SELECT_USER");
    }
   elsif(! $FORM{CUSTOMER}) {
     $html->message('err', "$_ERROR", "$_ORG_NAME");
    }
   elsif($FORM{PREVIEW}) {
     docs_preview('account', { %FORM });
     return 0;
    }
   else {
     $FORM{UID}=$LIST_PARAMS{UID} if (! $FORM{UID});
     $Docs->account_add({ %FORM });
     if(! $Docs->{errno}) {
    
       $html->message('info', "$_ADDED", "$_NUM: [$Docs->{ACCT_ID}] $_DATE: $FORM{DATE}");
       $Docs->account_info($Docs->{DOC_ID}, { UID => $LIST_PARAMS{UID} });
       ($Docs->{SUM_MAIN}, $Docs->{SUM_SUB})=split(/\./, $Docs->{TOTAL_SUM});

       my $list = $Docs->{ORDERS};
       my $i=0;


       foreach my $line (@$list) {
   	     $i++;
   	     $Docs->{ORDER} .= $html->tpl_show(_include('docs_account_order_row', 'Docs'), 
   	        { NUMBER => $i,
   	        	NAME   => $line->[1],
   	        	COUNT  => $line->[3],
   	        	UNIT   => $line->[2],
   	        	PRICE  => $line->[4],
   	        	SUM    => sprintf("%.2f", $line->[3]*$line->[4])
   	        	}, 
   	         { OUTPUT2RETURN => 1 });
   	     
        }

       if ($conf{DOCS_PDF_PRINT}) {
       	 $html->message('info', $_ADDED, "$_ACCOUNT $_ADDED. ". $html->button($_PRINT, "qindex=$index&print=$Docs->{DOC_ID}&UID=$LIST_PARAMS{UID}&pdf=1", { ex_params => 'target=_new' }));
       	 return 0;
        }

      
       #print $html->header() if ($FORM{qindex}AL_SUM});

       if ($conf{DOCS_PDF_PRINT}) {
       	 $html->message('info', $_ADDED, "$_ACCOUNT $_ADDED. ". $html->button($_PRINT, "qindex=$index&print=$Docs->{DOC_ID}&UID=$LIST_PARAMS{UID}&pdf=1", { ex_params => 'target=_new' }));
       	 return 0;
        }

      
       #print $html->header() if ($FORM{qindex});
       if( $user->{UID} ) {
         $FORM{qindex}    = $index;
         $html->{NO_PRINT}= undef;

         docs_print('account', $Docs);
         exit;
        }
       else {
         docs_print('account', $Docs);
        }

       $FORM{'print'}=1;
       return 0;
      }
    }
  }
 elsif(defined($FORM{print})){
   $Docs->account_info($FORM{print}, { UID => $LIST_PARAMS{UID} });
   
   if ($conf{DOCS_VAT_INCLUDE}) {
   	 $Docs->{ORDER_TOTAL_SUM_VAT} = sprintf("%.2f", $Docs->{TOTAL_SUM} / ((100 + $conf{DOCS_VAT_INCLUDE} ) / $conf{DOCS_VAT_INCLUDE}));
   	 $Docs->{TOTAL_SUM_WITHOUT_VAT}=sprintf("%.2f",$Docs->{TOTAL_SUM} - $Docs->{ORDER_TOTAL_SUM_VAT});
   	 $Docs->{VAT}=sprintf("%.2f", $conf{DOCS_VAT_INCLUDE});
    }

   if ($Docs->{TOTAL} > 0) {
   	 ($Docs->{SUM_MAIN}, $Docs->{SUM_SUB})=split(/\./, $Docs->{TOTAL_SUM});
      $Docs->{FROM_DATE_LIT}='';
     my $list = $Docs->{ORDERS};
     my $i=0;
     foreach my $line (@$list) {
   	   $i++;
 	     $Docs->{ORDER} .= $html->tpl_show(_include('docs_account_order_row', 'Docs'), 
   	        { NUMBER => $i,
   	        	NAME   => $line->[1],
   	        	COUNT  => $line->[3],
   	        	UNIT   => $line->[2],
   	        	PRICE  => $line->[4],
   	        	SUM    => sprintf("%.2f", $line->[3]*$line->[4])
   	        	}, 
   	         { OUTPUT2RETURN => 1 }) if (! $FORM{pdf});

   	   #$Docs->{ORDER} .= sprintf("<tr><td align=right>%d</td><td>%s</td><td align=right>%d</td><td align=right>%d</td><td align=right>%.2f</td><td align=right>%.2f</td></tr>\n", 
   	   #  $i, $line->[1], $line->[2], $line->[3], $line->[4], ($line->[2]*$line->[4]));
   	   
   	   my $count = $line->[2] || 1;
   	   my $sum   = sprintf("%.2f", $count*$line->[4]);
   	   
   	   $Docs->{'ORDER_NUM_'.$i}   = $i;
       $Docs->{'ORDER_NAME_'.$i}  = $line->[1];
       $Docs->{'ORDER_COUNT_'.$i} = $count;
       $Docs->{'ORDER_PRICE_'.$i} = $line->[4];
       $Docs->{'ORDER_SUM_'.$i}   = $sum;
       
       $Docs->{'ORDER_PRICE_WITHOUT_VAT_'.$i} = sprintf("%.2f", ($conf{DOCS_VAT_INCLUDE}) ? $line->[4] - $line->[4] / ((100 + $conf{DOCS_VAT_INCLUDE} ) / $conf{DOCS_VAT_INCLUDE}) : $line->[3]);
       $Docs->{'ORDER_SUM_WITHOUT_VAT_'.$i}   = sprintf("%.2f", ($conf{DOCS_VAT_INCLUDE}) ? $sum - ($sum) / ((100 + $conf{DOCS_VAT_INCLUDE} ) / $conf{DOCS_VAT_INCLUDE}) : $sum );
      }

     
     $Docs->{TOTAL_SUM} = sprintf("%.2f", $Docs->{TOTAL_SUM});
     
  
     docs_print('account', $Docs);
    }
   else {

     $html->message('err', $_ERROR, "[$Docs->{errno}] $err_strs{$Docs->{errno}}");	
    }
   return 0;
  }
 elsif(defined($FORM{change})) {
   $Docs->account_change({ %FORM });
   if(! $Docs->{errno}) {
     $html->message('info', "$_INFO", "$_CHANGED N: [$FORM{DOC_ID}]");
    }
  }
 elsif(defined($FORM{chg})) {
   $Docs->account_info($FORM{chg});
   if(! $Docs->{errno}) {
     $html->message('info', "$_INFO", "$_CHANGING N: [$FORM{chg}]");
    }
  }
 elsif(defined($FORM{del}) && $FORM{is_js_confirmed}) {
   $Docs->account_del($FORM{del});
   if(! $Docs->{errno}) {
     $html->message('info', "$_INFO", "$_DELETED N: [$FORM{del}]");
    }
  }
	
	if(! $user->{UID}  ) {
  	$Docs->{FORM_ACCT_ID} = "<tr><td>$_ACCOUNT $_NUM:</td><td><input type='text' name='ACCT_ID' value='%ACCT_ID%'></td></tr>\n";
    $Docs->{DATE_FIELD}="- <input type=text name=DATE value='%DATE%'>";
    
   }
  else {
  	$Docs->{DATE_FIELD}="$DATE";
  	$users = $user;
   }
  $Docs->{SEL_ORDER} .= $html->form_select('ORDER', 
                                    { 
 	                                   SELECTED    => $FORM{ORDER},
 	                                   SEL_ARRAY   => ($conf{DOCS_ORDERS})? $conf{DOCS_ORDERS} : [$_DV],
                                     NO_ID       => 1
 	                                  });

  #$Docs->{COMPANY_VAT}=$user->{COMPANY_VAT}.' %';
  $Docs->{OP_SID}  = mk_unique_value(16);
  $users->pi({  $users->{UID} });

  $Docs->{CUSTOMER}=$users->{FIO} || '-';
  $Docs->{CAPTION}=$_ACCOUNT;
  
  
  
  $html->tpl_show(_include('docs_account_add', 'Docs'), { %$Docs, %$users });
}


#**********************************************************
# docs_preview
#**********************************************************
sub docs_preview {
	my ($type, $ATTR)=@_;
	
	$html->tpl_show(_include("docs_$template.tpl", 'Docs'), $ATTR);
}


#**********************************************************
# docs_print
#**********************************************************
sub docs_print {
	my ($type, $ATTR)=@_;

  if (-f "../Abills/modules/Docs/lng_$conf{default_language}.pl") {
    require "../Abills/modules/Docs/lng_$conf{default_language}.pl";
   }
  else {
    require "../../Abills/modules/Docs/lng_$conf{default_language}.pl";
   }

  
	if (defined(@MONTHES_LIT) && $ATTR->{DATE}) {
    my ($y, $m, $d)=split(/-/, $ATTR->{DATE}, 3);
    $ATTR->{FROM_DATE_LIT} = "$d ". $MONTHES_LIT[int($m)-1] ." $y $_YEAR";
    
    $ATTR->{DAY}=$d;
    $ATTR->{MONTH_LIT}=$MONTHES_LIT[int($m)-1];
    $ATTR->{YEAR}=$y;
    $ATTR->{DATE_EURO_STANDART}="$d.$m.$y";
   }


  $ATTR->{SUM_LIT} = int2ml("$ATTR->{TOTAL_SUM}", { 
  	 ONES             => \@ones,
     TWOS             => \@twos,
     FIFTH            => \@fifth,
     ONE              => \@one,
     ONEST            => \@onest,
     TEN              => \@ten,
     TENS             => \@tens,
     HUNDRED          => \@hundred,
     MONEY_UNIT_NAMES => $conf{MONEY_UNIT_NAMES} || \@money_unit_names,
     LOCALE           => $conf{LOCALE}
  	  });


	my $template = $type;
  print $html->header() if ($FORM{qindex});


  if ($LIST_PARAMS{COMPANY_ID}) {

   }
  else {
    $users = $user if ($user->{UID});
   }
  

  if ($Docs->{UID}) {
    $users->pi({  UID => $Docs->{UID} }) ;
    $users->{ADDRESS_FULL}="$users->{ADDRESS_STREET}, $users->{ADDRESS_BUILD}/$users->{ADDRESS_FLAT}"; 
   }

	$html->tpl_show(_include("docs_$template", 'Docs', 
	    { pdf => $FORM{pdf} }), { %$users, %$ATTR });
}


#**********************************************************
# Make multiuser documents
#**********************************************************
sub docs_summary {

	
	my $list = $users->list({ DEPOSIT    => '<0',
		                        DISABLE    => 0,
		                        PAGE_ROWS  => 1000000   });
	my @MULTI_ARR = ();

	foreach my $line (@$list) {
    #print "-$line->[0], $line->[1], $line->[3], $line->[4]<br>";
    
    push @MULTI_ARR, { FIO     => $line->[1], 
    	                 DEPOSIT => $line->[2],
    	                 CREDIT  => $line->[3],
    	                 SUM     => $line->[2],
    	                 SUM_VAT => ($conf{DOCS_VAT_INCLUDE}) ? sprintf("%.2f", $line->[2] / ((100 + $conf{DOCS_VAT_INCLUDE} ) / $conf{DOCS_VAT_INCLUDE})) : 0.00
    	                };
	 }

  $html->tpl_show(_include("docs_multi_invoice", 'Docs', 
                                     { pdf         => $FORM{pdf} }), 
                                     { MULTI_PRINT => \@MULTI_ARR });
}

#**********************************************************
#
#**********************************************************
sub docs_acts {
	
	
	
}






1

