# Docs functions

use Docs;
use Fees;
use Customers;

$Docs        = Docs->new($db, $admin, \%conf);
my $Fees     = Fees->new($db, $admin, \%conf);
my $Payments = Payments->new($db, $admin, \%conf);
my $Customer = Customers->new($db, $admin, \%conf);
my $Company  = $Customer->company();
my $debug    = $FORM{debug};

#Default 20% VAT
$conf{DOCS_VAT_INCLUDE} = 20 if (!defined($conf{DOCS_VAT_INCLUDE}));

#***********************************************************
# Delete user from module
# docs_user_del()
#***********************************************************
sub docs_user_del {
  my ($uid, $attr) = @_;

  $Docs->{UID} = $uid;
  $Docs->del({ UID => $uid });

  return 0;
}

#**********************************************************
# 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} });

    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";
    if ($y=~/(\d{2})$/) {
      $Docs->{CONTRACT_YY}=$1;
    }

    %{$attr} = %{$user_main_info};
    my $contract_tpl = 'contract';

    if ($Docs->{CONTRACT_SUFIX}) {
      if ($conf{DOCS_CONTRACT_TYPES}) {
        #PREFIX:SUFIX:NAME;
        $conf{DOCS_CONTRACT_TYPES} =~ s/\n//g;
        my (@contract_types_list) = split(/;/, $conf{DOCS_CONTRACT_TYPES});
        foreach my $line (@contract_types_list) {
          my ($prefix, $sufix, $name, $tpl_name) = split(/:/, $line);

          if ($sufix eq $Docs->{CONTRACT_SUFIX}) {
            $contract_tpl = $tpl_name;

            $Docs->{CONTRACT_ID} .= $Docs->{CONTRACT_SUFIX};
            last;
          }
        }
      }
    }
    docs_print($contract_tpl, { %$admin, %$Docs, %$attr });
  }
  else {
    my ($y, $m, $d) = split(/-/, (($attr->{CONTRACT_DATE}) ? $attr->{CONTRACT_DATE} : $DATE), 3);
    $Docs->{CONTRACT_DATE_LIT} = "$d " . $MONTHES_LIT[ int($m) - 1 ] . " $y $_YEAR";
    docs_print('contract_company', { %$admin, %$Docs, %$attr });
  }
}

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

  if ($attr->{create}) {
    if (defined($FORM{OP_SID}) and $FORM{OP_SID} eq $COOKIES{OP_SID}) {
      $html->message('err', $_ERROR, "$_RECEIPT $_EXIST");
      return 0;
    }
    elsif ($FORM{PREVIEW}) {
      docs_preview('receipt', {%FORM});
      return 0;
    }
    else {
      $Docs->{FROM} = $attr->{FROM} if ($attr->{FROM});
      $Docs->docs_receipt_add({ %$attr, DEPOSIT => $users->{DEPOSIT} });

      my $date = $FORM{DATE} || $DATE;

      if (!$Docs->{errno}) {
        if ($conf{DOCS_PDF_PRINT}) {
          $html->message(
            'info',
            "$_RECEIPT $_ADDED ",
            "$_RECEIPT $_NUM: [$Docs->{DOC_ID}]\n $_DATE: $date\n "
            . $html->button("$_SEND $_EMAIL", "qindex=" . get_function_index('docs_receipt_list') . "&sendmail=$Docs->{DOC_ID}&UID=$LIST_PARAMS{UID}",    { ex_params => 'target=_new', CLASS => 'sendmail rightAlignText' }) . ' '
            . $html->button($_PRINT,          "qindex=" . get_function_index('docs_receipt_list') . "&print=$Docs->{DOC_ID}&UID=$LIST_PARAMS{UID}&pdf=1", { ex_params => 'target=_new', CLASS => 'print rightAlignText' })
          );

          delete($FORM{create});
          delete $FORM{print};
          #docs_receipt_list() if ($index != 2 && !$attr->{SKIP_LIST});

          $FORM{print} = $Docs->{DOC_ID};
          $FORM{pdf}   = 1;

          return docs_receipt(
            {
              GET_EMAIL_INFO => $FORM{SEND_EMAIL},
              SEND_EMAIL     => $FORM{SEND_EMAIL},
              %$attr
            }
          );

        }
        else {
          $html->message(
            'info',
            "$_RECEIPT $_ADDED",
            "$_RECEIPT $_NUM: [$Docs->{DOC_ID}]\n $_DATE: $date\n"
            . $html->button("$_SEND_MAIL", "qindex=" . get_function_index('docs_receipt_list') . "&sendmail=$Docs->{DOC_ID}&UID=$LIST_PARAMS{UID}", { ex_params => 'target=_new', CLASS => 'sendmail rightAlignText' }) . ' '
            . $html->button($_PRINT,       "qindex=" . get_function_index('docs_receipt_list') . "&print=$Docs->{DOC_ID}&UID=$LIST_PARAMS{UID}",    { ex_params => 'target=_new', CLASS => 'print rightAlignText' })
          );
          #$Docs->docs_receipt_info($Docs->{DOC_ID});
        }

      }
      elsif ($Docs->{errno}) {
        if ($Docs->{errno} == 1) {
          $html->message('err', "$_RECEIPT:$_ERROR", "$ERR_NO_ORDERS");
        }
        else {
          $html->message('err', "$_RECEIPT:$_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>";

      $Docs->{ 'ORDER_NUM_' . $i }               = $i;
      $Docs->{ 'ORDER_NAME_' . $i }              = $line->[1];
      $Docs->{ 'ORDER_COUNT_' . $i }             = $line->[3];
      $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);
      $i++;
      $Docs->{TOTAL_SUM} += $sum;
    }
  }

  if ($Docs->{PAYMENT_ID}) {
    my $list = $Docs->invoices_list({ PAYMENT_ID => $Docs->{PAYMENT_ID} });
    if ($Docs->{TOTAL} > 0) {
      $Docs->invoice_info($list->[0]->[9]);
    }
    $Docs->{INVOICE_ID} = $Docs->{INVOICE_ID};

    push @PAYMENT_METHODS, @EX_PAYMENT_METHODS if (@EX_PAYMENT_METHODS);

    for (my $i = 0 ; $i <= $#PAYMENT_METHODS ; $i++) {
      $PAYMENTS_METHODS{"$i"} = "$PAYMENT_METHODS[$i]";
    }

    my %PAYSYS_PAYMENT_METHODS = %{ cfg2hash($conf{PAYSYS_PAYMENTS_METHODS}) };

    while (my ($k, $v) = each %PAYSYS_PAYMENT_METHODS) {
      $PAYMENTS_METHODS{$k} = $v;
    }

    $list = $Payments->list({ ID => $Docs->{PAYMENT_ID} });
    if ($Payments->{TOTAL} > 0) {
      $Docs->{PAYMENT_METHOD_ID} = $list->[0]->[6];

      if ($conf{DOCS_PAYMENT_METHODS}) {
        my %methods_hash = %{ cfg2hash($conf{DOCS_PAYMENT_METHODS}) };

        if ($methods_hash{ $Docs->{PAYMENT_METHOD_ID} }) {
          $Docs->{PAYMENT_METHOD} = $methods_hash{ $Docs->{PAYMENT_METHOD_ID} };
        }
        else {
          $Docs->{PAYMENT_METHOD} = $methods_hash{0};
        }
      }
      else {
        $Docs->{PAYMENT_METHOD} = $PAYMENT_METHODS[ $Docs->{PAYMENT_METHOD_ID} ];
      }
    }
  }

  $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 $receipt_file = 'receipt';
  #  if ($Docs->{PAYMENT_METHOD_ID}) {
  #    $receipt_file =  'docs_receipt_'.$Docs->{PAYMENT_METHOD_ID};
  #    my $receipt_content = _include($receipt_file, 'Docs', { $FORM{pdf} => 1 });
  #    if (  $receipt_content =~ /No such tem/ || $receipt_content eq ''  ) {
  #      $receipt_file = 'receipt';
  #     }
  #    else {
  #      $receipt_file = 'receipt_'.$Docs->{PAYMENT_METHOD_ID};
  #     }
  #   }

  docs_print("receipt", $Docs) if (!$FORM{QUICK});
}

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

  if ($FORM{create}) {
    docs_receipt_add({%FORM});
    return 0;
  }
  elsif ($FORM{skip}) {
    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
      }
    );

    $users->pi({ UID => $users->{UID} });
    $Docs->{OP_SID}   = mk_unique_value(16);
    $Docs->{CUSTOMER} = $users->{FIO} || '-';
    $Docs->{CAPTION}  = $_RECEIPT;
    $Docs->{DATE}     = $html->date_fld2('DATE', { MONTHES => \@MONTHES, FORM_NAME => 'receipt_add', WEEK_DAYS => \@WEEKDAYS });

    # Get docs info
    $Docs->user_info($FORM{UID});
    if ($Docs->{TOTAL}) {
      if (!$FORM{NEXT_PERIOD}) {
        $FORM{NEXT_PERIOD} = $Docs->{RECEIPT_PERIOD} if ($Docs->{RECEIPT_PERIOD} > 1);
      }
    }

    if (!$FORM{INCLUDE_CUR_BILLING_PERIOD}) {
      my ($Y, $M, $D) = split(/-/, $DATE, 3);
      $FORM{FROM_DATE} = "$Y-01-01";
    }

    my $list = $Docs->docs_receipt_new(
      {
        FROM_DATE => $FORM{FROM_DATE} || $html->{FROM_DATE},
        TO_DATE => $FORM{TO_DATE} || $html->{TO_DATE} || $DATE,
        PAGE_ROWS => 500,
        UID       => $users->{UID}
      }
    );

    my $table = $html->table(
      {
        width       => '100%',
        caption     => "$_ORDERS",
        border      => 1,
        title_plain => [ '#', "$_LOGIN", "$_DATE", "$_DESCRIBE", $_SUM ],
        cols_align => [ 'right', 'left', 'right', 'left' ],
        pages      => $Docs->{TOTAL},
        ID         => 'DOCS_INVOCE_ORDERS',
        class      => 'form'
      }
    );

    my $num            = 0;
    my $total_sum      = ($users->{DEPOSIT} < 0) ? abs($users->{DEPOSIT}) : 0 - $users->{DEPOSIT};
    my $amount_for_pay = 0;
    foreach my $line (@$list) {
      next if ($line->[5]);
      $num++;
      my $date = $line->[2];
      $date =~ s/ \d+:\d+:\d+//g;
      $table->addrow(
        $html->form_input("ORDER_" . $line->[0], "$line->[3]", { TYPE => 'hidden', OUTPUT2RETURN => 1 })
        . $html->form_input("SUM_" . $line->[0],     "$line->[4]", { TYPE => 'hidden', OUTPUT2RETURN => 1 })
        . $html->form_input("FEES_ID_" . $line->[0], "$line->[0]", { TYPE => 'hidden', OUTPUT2RETURN => 1 })
        . $html->form_input("IDS", "$line->[0]", { TYPE => 'checkbox', STATE => 1, OUTPUT2RETURN => 1 }) . "$num",
        "$line->[1]", "$line->[2]", "$line->[3] $date", "$line->[4]"
      );

      #$total_sum+=$line->[4];
    }

    my $date = $DATE;
    if ($users->{ACTIVATE} ne '0000-00-00') {
      $date = $users->{ACTIVATE};
      $FORM{FROM_DATE} = $users->{ACTIVATE};
    }

    my ($Y, $M, $D) = split(/-/, $date);
    my $start_period_unixtime;
    if ($users->{ACTIVATE} ne '0000-00-00') {
      $start_period_unixtime = (mktime(0, 0, 0, $D, ($M - 1), ($Y - 1900), 0, 0, 0));
      $Docs->{CURENT_BILLING_PERIOD_START} = $users->{ACTIVATE};
      $Docs->{CURENT_BILLING_PERIOD_STOP} = strftime("%Y-%m-%d", localtime((mktime(0, 0, 0, $D, ($M - 1), ($Y - 1900), 0, 0, 0) + 30 * 86400)));
    }
    else {
      $D                                   = '01';
      $Docs->{CURENT_BILLING_PERIOD_START} = "$Y-$M-$D";
      $TO_D                                = ($M != 2 ? (($M % 2) ^ ($M > 7)) + 30 : (!($Y % 400) || !($Y % 4) && ($Y % 25) ? 29 : 28));
      $Docs->{CURENT_BILLING_PERIOD_STOP}  = "$Y-$M-$TO_D";
    }

    #Next period payments
    if ($FORM{NEXT_PERIOD}) {
      # Get invoces
      my %current_receipt = ();
      my $list = $Docs->docs_receipt_list({ UID => $FORM{UID}, PAYMENT_ID => 0, ORDERS_LIST => 1 });

      foreach my $line (@$list) {
        $current_receipt{ $line->[1] } = $line->[0];
      }

      my $cross_modules_return = cross_modules_call('_docs');
      my $next_period          = $FORM{NEXT_PERIOD};
      if ($users->{ACTIVATE} ne '0000-00-00') {
        ($Y, $M, $D) = split(/-/, strftime("%Y-%m-%d", localtime((mktime(0, 0, 0, $D, ($M - 1), ($Y - 1900), 0, 0, 0) + ((($start_period_unixtime > time) ? 0 : 1) + 30 * (($start_period_unixtime > time) ? 0 : 1)) * 86400))));
        $FORM{FROM_DATE} = "$Y-$M-$D";

        ($Y, $M, $D) = split(/-/, strftime("%Y-%m-%d", localtime((mktime(0, 0, 0, $D, ($M - 1), ($Y - 1900), 0, 0, 0) + ((($start_period_unixtime > time) ? 1 : (1 * $next_period - 1)) + 30 * (($start_period_unixtime > time) ? 1 : $next_period)) * 86400))));
        $FORM{TO_DATE} = "$Y-$M-$D";
      }
      else {
        $M += 1;
        if ($M < 12) {
          $M = sprintf("%02d", $M);
        }
        else {
          $M = sprintf("%02d", $M - 12);
          $Y++;
        }
        $FORM{FROM_DATE} = "$Y-$M-$D";

        $M += $next_period - 1;
        if ($M < 12) {
          $M = sprintf("%02d", $M);
        }
        else {
          $M = sprintf("%02d", $M - 13);
          $Y++;
        }

        if ($users->{ACTIVATE} eq '0000-00-00') {
          $TO_D = ($M != 2 ? (($M % 2) ^ ($M > 7)) + 30 : (!($Y % 400) || !($Y % 4) && ($Y % 25) ? 29 : 28));
        }
        else {
          $TO_D = $D;
        }

        $FORM{TO_DATE} = "$Y-$M-$TO_D";
      }

      my $period_from = $FORM{FROM_DATE};
      my $period_to   = $FORM{FROM_DATE};

      foreach my $module (sort keys %$cross_modules_return) {
        if (ref $cross_modules_return->{$module} eq 'ARRAY') {
          next if ($#{ $cross_modules_return->{$module} } == -1);
          $table->{extra} = "colspan='5' class='small'";
          $table->addrow("$module");
          $table->{extra} = undef;

          foreach my $line (@{ $cross_modules_return->{$module} }) {
            my ($name, $describe, $sum) = split(/\|/, $line);
            next if ($sum < 0);

            #my ($Y, $M, $D) = split(/-/, $FORM{FROM_DATE}, 3);
            #$period_from = strftime "%Y-%m-%d", localtime( (mktime(0, 0, 0, $D, ($M-1), ($Y-1900), 0, 0, 0) + 1 * 86400) );
            $period_from = $FORM{FROM_DATE};

            for (my $i = ($FORM{NEXT_PERIOD} == -1) ? -2 : 0 ; $i < int($FORM{NEXT_PERIOD}) ; $i++) {
              $result_sum = sprintf("%.2f", $sum);
              if ($users->{REDUCTION} && $module ne 'Abon') {
                $result_sum = sprintf("%.2f", $sum * (100 - $users->{REDUCTION}) / 100);
              }

              my ($Y, $M, $D) = split(/-/, $period_from, 3);
              if ($users->{ACTIVATE} ne '0000-00-00') {
                ($Y, $M, $D) = split(/-/, strftime("%Y-%m-%d", localtime((mktime(0, 0, 0, $D, ($M - 1), ($Y - 1900), 0, 0, 0)))));  #+ (31 * $i) * 86400) ));
                $period_from = "$Y-$M-$D";

                ($Y, $M, $D) = split(/-/, strftime("%Y-%m-%d", localtime((mktime(0, 0, 0, $D, ($M - 1), ($Y - 1900), 0, 0, 0) + (30) * 86400))));
                $period_to = "$Y-$M-$D";
              }
              else {
                $M += 1 if ($i > 0);
                if ($M < 12) {
                  $M = sprintf("%02d", $M);
                }
                else {
                  $M = sprintf("%02d", $M - 12);
                  $Y++;
                }
                $period_from = "$Y-$M-01";

                #$M+=1;
                if ($M < 12) {
                  $M = sprintf("%02d", $M);
                }
                else {
                  $M = sprintf("%02d", $M - 13);
                  $Y++;
                }

                if ($users->{ACTIVATE} eq '0000-00-00') {
                  $TO_D = ($M != 2 ? (($M % 2) ^ ($M > 7)) + 30 : (!($Y % 400) || !($Y % 4) && ($Y % 25) ? 29 : 28));
                }
                else {
                  $TO_D = $D;
                }

                $period_to = "$Y-$M-$TO_D";
              }

              my $order = "$name $describe ($period_from-$period_to)";

              $num++ if (!$current_receipt{$order});
              $table->addrow(
                (
                  (!$current_receipt{$order})
                  ? $html->form_input('ORDER_' . $num, "$order",    { TYPE => 'hidden', OUTPUT2RETURN => 1 })
                  . $html->form_input('SUM_' . $num,   $result_sum, { TYPE => 'hidden', OUTPUT2RETURN => 1 })
                  . $html->form_input('IDS', "$num", { TYPE => ($user->{UID}) ? 'hidden' : 'checkbox', STATE => 'checked', OUTPUT2RETURN => 1 })
                  . $num
                  : ''
                ),
                $users->{LOGIN},
                $period_from,
                $order . (($current_receipt{$order}) ? ' ' . $html->color_mark($_EXIST, 'red') : ''),
                $result_sum
              );

              $total_sum += $sum if (!$current_receipt{$order});
              $period_from = strftime("%Y-%m-%d", localtime((mktime(0, 0, 0, $D, ($M - 1), ($Y - 1900), 0, 0, 0) + 1 * 86400)));
            }
          }
        }
      }
    }

    if ($users->{DEPOSIT} > 0) {
      $amount_for_pay = ($total_sum < $users->{DEPOSIT}) ? 0 : $total_sum - $users->{DEPOSIT};
    }
    else {
      $amount_for_pay = $total_sum;
    }

    $table->{extra} = " colspan=4 class=total ";
    $table->addrow("$_COUNT: $num $_TOTAL $_SUM: ", sprintf("%.2f", $total_sum));
    $table->addrow($html->b("$_DEPOSIT:"),        $html->b(sprintf("%.2f", $users->{DEPOSIT})));
    $table->addrow($html->b("$_AMOUNT_FOR_PAY:"), $html->b(sprintf("%.2f", $amount_for_pay)));
    $Docs->{ORDERS} = $table->show({ OUTPUT2RETURN => 1 });
    $Docs->{FROM_DATE} = $html->date_fld2('FROM_DATE', { MONTHES => \@MONTHES, FORM_NAME => 'receipt_add', WEEK_DAYS => \@WEEKDAYS });
    $Docs->{TO_DATE}   = $html->date_fld2('TO_DATE',   { MONTHES => \@MONTHES, FORM_NAME => 'receipt_add', WEEK_DAYS => \@WEEKDAYS });
    $FORM{NEXT_PERIOD} = 0 if ($FORM{NEXT_PERIOD} < 0);
    if ($attr->{REGISTRATION}) {
      $Docs->{BACK} = $html->form_input('back', "$_BACK", { TYPE => 'submit' });
      $Docs->{NEXT} = $html->form_input('skip', "$_NEXT", { TYPE => 'submit' });
    }

    #$html->tpl_show(_include('docs_receipt_add', 'Docs'), { %FORM, %$attr, %$Docs, %$users }) if (! $FORM{pdf});
  }

  docs_receipt_list($attr);
  return 0;
}

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

  push @PAYMENT_METHODS, @EX_PAYMENT_METHODS if (@EX_PAYMENT_METHODS);

  for (my $i = 0 ; $i <= $#PAYMENT_METHODS ; $i++) {
    $PAYMENTS_METHODS{$i} = $PAYMENT_METHODS[$i];
  }

  my %PAYSYS_PAYMENT_METHODS = %{ cfg2hash($conf{PAYSYS_PAYMENTS_METHODS}) };
  while (my ($k, $v) = each %PAYSYS_PAYMENT_METHODS) {
    $PAYMENTS_METHODS{$k} = $v;
  }

  if ($FORM{del} && $FORM{is_js_confirmed}) {
    $Docs->docs_receipt_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_receipt_info($FORM{print}, { UID => $LIST_PARAMS{UID} || $attr->{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} = '';
      $Docs->{AMOUNT_FOR_PAY} = ($Docs->{DEPOSIY} < 0) ? abs($Docs->{DEPOSIT}) : 0 - $Docs->{DEPOSIT};

      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->[3] || 1;
        $Docs->{ 'ORDER_PRICE_' . $i }             = $line->[4];
        $Docs->{ 'LOGIN_' . $i }                   = $line->[6];
        $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;
        $Docs->{AMOUNT_FOR_PAY} += $Docs->{ 'ORDER_COUNT_' . $i } * $Docs->{ 'ORDER_PRICE_' . $i } if ($line->[5] == 0);

        #alternative currancy sum
        if ($Docs->{EXCHANGE_RATE} > 0) {
          $Docs->{ 'ORDER_ALT_SUM_' . $i }               = sprintf("%.2f", $Docs->{ 'ORDER_SUM_' . $i } * $Docs->{EXCHANGE_RATE});
          $Docs->{ 'ORDER_ALT_PRICE_' . $i }             = sprintf("%.2f", $Docs->{ 'ORDER_PRICE_' . $i } * $Docs->{EXCHANGE_RATE});
          $Docs->{ 'ORDER_ALT_VAT_' . $i }               = sprintf("%.2f", $Docs->{ 'ORDER_VAT_' . $i } * $Docs->{EXCHANGE_RATE});
          $Docs->{ 'ORDER_ALT_PRICE_WITHOUT_VAT_' . $i } = sprintf("%.2f", $Docs->{ 'ORDER_PRICE_WITHOUT_VAT_' . $i } * $Docs->{EXCHANGE_RATE});
          $Docs->{ 'ORDER_ALT_SUM_WITHOUT_VAT_' . $i }   = sprintf("%.2f", $Docs->{ 'ORDER_SUM_WITHOUT_VAT_' . $i } * $Docs->{EXCHANGE_RATE});
        }
        $i++;
      }
    }

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

    if ($Docs->{EXCHANGE_RATE} > 0) {
      $Docs->{TOTAL_ALT_SUM}      = sprintf("%.2f", $Docs->{TOTAL_SUM} * $Docs->{EXCHANGE_RATE});
      $Docs->{AMOUNT_FOR_PAY_ALT} = sprintf("%.2f", $Docs->{AMOUNT_FOR_PAY} * $Docs->{EXCHANGE_RATE});
      $Docs->{CHARGED_ALT_SUM}    = sprintf("%.2f", $Docs->{CHARGED_SUM} * $Docs->{EXCHANGE_RATE});
    }

    if ($Docs->{PAYMENT_ID}) {
      my $list = $Docs->invoices_list({ PAYMENT_ID => $Docs->{PAYMENT_ID} });
      if ($Docs->{TOTAL} > 0) {
        $Docs->invoice_info($list->[0]->[10]);
      }
      $Docs->{INVOICE_ID} = $Docs->{INVOICE_ID};

      push @PAYMENT_METHODS, @EX_PAYMENT_METHODS if (@EX_PAYMENT_METHODS);

      for (my $i = 0 ; $i <= $#PAYMENT_METHODS ; $i++) {
        $PAYMENTS_METHODS{"$i"} = "$PAYMENT_METHODS[$i]";
      }

      my %PAYSYS_PAYMENT_METHODS = %{ cfg2hash($conf{PAYSYS_PAYMENTS_METHODS}) };

      while (my ($k, $v) = each %PAYSYS_PAYMENT_METHODS) {
        $PAYMENTS_METHODS{$k} = $v;
      }

      $list = $Payments->list({ ID => $Docs->{PAYMENT_ID} });
      if ($Payments->{TOTAL} > 0) {
        $Docs->{PAYMENT_METHOD_ID} = $list->[0]->[6];

        if ($conf{DOCS_PAYMENT_METHODS}) {
          my %methods_hash = %{ cfg2hash($conf{DOCS_PAYMENT_METHODS}) };

          if ($methods_hash{ $Docs->{PAYMENT_METHOD_ID} }) {
            $Docs->{PAYMENT_METHOD} = $methods_hash{ $Docs->{PAYMENT_METHOD_ID} };
          }
          else {
            $Docs->{PAYMENT_METHOD} = $methods_hash{0};
          }
        }
        else {
          $Docs->{PAYMENT_METHOD} = $PAYMENT_METHODS[ $Docs->{PAYMENT_METHOD_ID} ];
        }

      }
    }

    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});
    }

    $attr->{SEND_EMAIL} = 0 if (!defined($attr->{SEND_EMAIL}));

    if ($attr->{GET_EMAIL_INFO}) {
      delete $FORM{pdf};
      $attr->{EMAIL_MSG_TEXT}        = $html->tpl_show(_include('docs_receipt_email', 'Docs'), { %$Docs, %$users }, { OUTPUT2RETURN => 1 });
      $attr->{EMAIL_ATTACH_FILENAME} = 'receipt_' . $Docs->{RECEIPT_NUM} . '.pdf' if (!$attr->{EMAIL_ATTACH_FILENAME});
      $attr->{EMAIL_MSG_SUBJECT}     = "ABillS - $_RECEIPT: $Docs->{RECEIPT_NUM}" if (!$attr->{EMAIL_MSG_SUBJECT});
      $attr->{OUTPUT2RETURN}         = undef;
      $FORM{pdf}                     = $conf{DOCS_PDF_PRINT};
      $attr->{SEND_EMAIL}            = 1;
    }

    docs_print('receipt', { %$Docs, %$attr });
    return 1;
  }
  elsif ($FORM{sendmail}) {
    $FORM{print} = $FORM{sendmail};
    if (
      docs_receipt_list(
        {
          SEND_EMAIL     => 1,
          GET_EMAIL_INFO => 1
        }
      )
    )
    {
      $html->message('info', "$_INFO", "E-Mail $_SENDED ");
    }
    else {
      $html->message('info', "$_ERROR", "E-Mail $_SENDED Error: $FORM{ERR_MESSAGE} ");
    }
    return 0;
  }
  elsif (!$LIST_PARAMS{UID}) {
    $info{PAYMENT_METHOD_SEL} = $html->form_select(
      'PAYMENT_METHOD',
      {
        SELECTED => (defined($FORM{PAYMENT_METHOD}) && $FORM{PAYMENT_METHOD} ne '') ? $FORM{METHOD} : '',
        SEL_HASH => { '' => $_ALL, %PAYMENTS_METHODS },
        NO_ID    => 1,
        SORT_KEY => 1
      }
    );

    form_search({ SEARCH_FORM => ($FORM{pdf}) ? '' : $html->tpl_show(_include('docs_receipt_search', 'Docs'), { %info, %FORM }, { notprint => 1 }) });
  }

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

    #$LIST_PARAMS{DESC}='DESC';
  }

  if ($FORM{print_list}) {
  	print "Content-Type: text/html\n\n";
    my $receipt_list = $Docs->docs_receipt_list({ %FORM, 
    	                                         %LIST_PARAMS, 
    	                                         FULL_INFO => 1,
    	                                         COLS_NAME => 1
    	                                       });
    my @MULTI_ARR = ();
    my $doc_num   = 0;

    foreach my $line (@$receipt_list) {
      my %D = ();
      $D{RECEIPT_ID}         = $line->[0];
      $D{DATE}               = $line->[1];
      my ($y, $m, $d)        = split(/-/, $line->[1], 3);
      $D{FROM_DATE_LIT}      = "$d " . $MONTHES_LIT[ int($m) - 1 ] . " $y $_YEAR";
      $D{DATE_EURO_STANDART} = "$d.$m.$y";
      $D{FIO}                = $line->[2];
      $D{LOGIN}              = $line->[15];
      $D{INVOICE_ID}         = $line->[8];
      $D{FIO}                = $line->[2];
      $D{ADDRESS_FULL}       = "$line->[10], $line->[11], $line->[12]";
      $D{TOTAL_SUM}          = sprintf("%.2f", $line->[3]);
      $D{CONTRACT_ID}        = $line->[14] || '';
      $D{CONTRACT_DATE}      = $line->[15] || '0000-00-00';
      $D{A_FIO}              = $line->[5];
      $D{PHONE}              = $line->[13];

      my $method = $line->[7];
      if ($conf{DOCS_PAYMENT_METHODS}) {
        my %methods_hash = %{ cfg2hash($conf{DOCS_PAYMENT_METHODS}) };

        if ($methods_hash{$method}) {
          $D{PAYMENT_METHOD} = $methods_hash{$method};
        }
        else {
          $D{PAYMENT_METHOD} = $methods_hash{0};
        }
      }
      else {
        $D{PAYMENT_METHOD} = $PAYMENT_METHODS[$method];
      }
      $D{TOTAL_SUM} = $line->[3];

      if ($conf{DOCS_VAT_INCLUDE}) {
        $D{ORDER_TOTAL_SUM_VAT}   = sprintf("%.2f", $D{TOTAL_SUM} / ((100 + $conf{DOCS_VAT_INCLUDE}) / $conf{DOCS_VAT_INCLUDE}));
        $D{TOTAL_SUM_WITHOUT_VAT} = sprintf("%.2f", $D{TOTAL_SUM} - $D{ORDER_TOTAL_SUM_VAT});
        $D{TOTAL_SUM_VAT}         = sprintf("%.2f", $D{TOTAL_SUM} - $D{TOTAL_SUM_WITHOUT_VAT});
      }
      $D{ORDER_PRICE_WITHOUT_VAT_1} = $D{TOTAL_SUM_WITHOUT_VAT} || 0, 00;
      $D{ORDER_SUM_WITHOUT_VAT_1}   = $D{TOTAL_SUM_WITHOUT_VAT} || 0, 00;
      $D{ORDER_TOTAL_SUM_VAT}       = $D{TOTAL_SUM_VAT}         || 0, 00;
      push @MULTI_ARR, { %D, DOC_NUMBER => sprintf("%.6d", $doc_num), };
      $doc_num++;
      print "UID: LOGIN: $line->[0] FIO: $line->[1] SUM: $line->[2]\n" if ($debug > 2);
    }

    print $html->header() if ($FORM{qindex});
    my $receipt_file = ($D{PAYMENT_METHOD}) ? 'docs_receipt_' . $D{PAYMENT_METHOD} : 'docs_receipt';
    my $receipt_content = _include($receipt_file, 'Docs', { pdf => $FORM{pdf} });

    if ($receipt_content =~ /No such t/) {
      $receipt_content = _include('docs_receipt', 'Docs', { pdf => $FORM{pdf} });
    }

    my $single_tpl = $html->tpl_show(
      $receipt_content,
      undef,
      {
        MULTI_DOCS => \@MULTI_ARR,
        #SAVE_AS      => $save_filename,
        #DOCS_IN_FILE => $docs_in_file,
        debug => $debug
      }
    );
    return 0;
  }

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

  my $table = $html->table(
    {
      width   => '100%',
      caption => "$_RECEIPT",
      border  => 1,
      title   => [ '#', $_DATE, $_CUSTOMER, $_SUM, $_USER, $_ADMIN, $_TIME, $_PAYMENT_METHOD, '-', '-', '-' ],
      cols_align => [ 'right', 'right', 'left', 'right', 'left', 'left', 'right', 'center', 'center', 'center' ],
      qs         => $pages_qs,
      pages      => $Docs->{TOTAL},
      ID         => 'DOCS_INVOCE',
      EXPORT     => ' XML:&xml=1',
      MENU       => ($FORM{UID})
      ? "$_SEARCH:index=" . get_function_index('docs_receipt_list') . "&search=1:search"
      : ''

      #MENU    => (($FORM{UID}) ? "$_ADD:index=". get_function_index('docs_receipt', { ARGS => UID }) ."&UID=$FORM{UID}:add" : '').
      #";$_SEARCH:index=".get_function_index('docs_receipt_list')."&search=1:search"
    }
  );

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

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

    $table->addrow(
      "$line->[0]",
      "$line->[1]",
      "$line->[2]",
      "$line->[3]",
      $html->button($line->[4], "index=11&UID=$line->[8]"),
      "$line->[5]",
      "$line->[6]",
      (defined($line->[7]) && $line->[7] ne '') ? $html->button($PAYMENTS_METHODS{"$line->[7]"}, "index=2&ID=$line->[10]&UID=$line->[8]") : '',
      $html->button($_SEND_MAIL, "qindex=" . get_function_index('docs_receipt_list') . "&sendmail=$line->[9]&UID=$line->[8]",       { ex_params => 'target=_new', CLASS => 'sendmail' }),
      $html->button($_PRINT,     "qindex=" . get_function_index('docs_receipt_list') . "&print=$line->[9]&UID=$line->[8]$pages_qs", { ex_params => 'target=_new', CLASS => 'print' }),
      $delete
    );
  }
  print $table->show();

  $table = $html->table(
    {
      width      => '100%',
      cols_align => [ 'right', 'right' ],
      rows       => [ [ $html->button("$_PRINT $_LIST", "qindex=$index&print_list=1$pages_qs" . (($conf{DOCS_PDF_PRINT}) ? '&pdf=1' : ''), { BUTTON => 1, ex_params => 'target=new' }), "$_TOTAL:", $html->b($Docs->{TOTAL}) ] ]
    }
  );

  print $table->show();
}

#**********************************************************
# docs_invoice_company
#**********************************************************
sub docs_invoice_company {
  my ($attr) = @_;
  $FORM{ALL_SERVICES} = 1;
  docs_invoice();
}

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

  my @payments_status = ("$_UNPAID", "$_PAID", "$_PARTLY_PAID");
  push @PAYMENT_METHODS, @EX_PAYMENT_METHODS if (@EX_PAYMENT_METHODS);

  if ($attr->{COMPANY}) {
    $LIST_PARAMS{COMPANY_ID} = $FORM{COMPANY_ID};
    $index = $FORM{index};
  }
  elsif ($FORM{COMPANY_ID} && !$attr->{COMPANY} && !$FORM{qindex}) {
    $FORM{subf} = $FORM{index};
    $index2 = $FORM{index};
    form_companies();
    return 0;
  }

  for (my $i = 0 ; $i <= $#PAYMENT_METHODS ; $i++) {
    $PAYMENTS_METHODS{$i} = $PAYMENT_METHODS[$i];
  }

  if ($conf{PAYSYS_PAYMENTS_METHODS}) {
    my %PAYSYS_PAYMENT_METHODS = %{ cfg2hash($conf{PAYSYS_PAYMENTS_METHODS}) };
    while (my ($k, $v) = each %PAYSYS_PAYMENT_METHODS) {
      $PAYMENTS_METHODS{$k} = $v;
    }
  }

  if ($LIST_PARAMS{UID} || $FORM{UID}) {
    docs_invoice();
    return 0 if ($FORM{'print'} || $FORM{SHOW_ORDERS});
  }
  elsif (defined($FORM{del}) && $FORM{is_js_confirmed}) {
    $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;
    }
  }
  else {
    $info{PAID_STATUS_SEL} = $html->form_select(
      'PAID_STATUS',
      {
        SELECTED     => $FORM{PAID_STATUS},
        ARRAY_NUM_ID => 1,
        SEL_ARRAY    => [ $_ALL, $_PAID, $_UNPAID ],
        NO_ID        => 1
      }
    );

    $info{PAYMENT_METHOD_SEL} = $html->form_select(
      'PAYMENT_METHOD',
      {
        SELECTED => (defined($FORM{PAYMENT_METHOD}) && $FORM{PAYMENT_METHOD} ne '') ? $FORM{METHOD} : '',
        SEL_HASH => { '' => $_ALL, %PAYMENTS_METHODS },
        NO_ID    => 1,
        SORT_KEY => 1
      }
    );

    $info{CUSTOMER_TYPE_SEL} = $html->form_select(
      'CUSTOMER_TYPE',
      {
        SELECTED => $FORM{CUSTOMER_TYPE} || '',
        SEL_HASH => {
          ''   => $_ALL,
          '=0' => $_USERS,
          ">0" => $_COMPANIES
        },
        NO_ID    => 1,
        SORT_KEY => 1
      }
    );

    form_search({ SEARCH_FORM => ($FORM{pdf}) ? '' : $html->tpl_show(_include('docs_invoice_search', 'Docs'), { %info, %FORM }, { notprint => 1 }) });
  }

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

  if (!$FORM{sort}) {
    $LIST_PARAMS{SORT} = '2 desc, 1';
    $LIST_PARAMS{DESC} = 'DESC';
  }
  
  
  if ($FORM{print_list}) {
  	print "Content-Type: text/html\n\n" if ($debug > 2);
    my $invoices_list = $Docs->invoices_list({ %FORM, 
    	                                %LIST_PARAMS, 
    	                                FULL_INFO   => 1,
    	                                ORDERS_LIST => 1,
    	                                COLS_NAME   => 1,
    	                                COLS_UPPER  => 1
    	                             });
    my @MULTI_ARR = ();
    my $doc_num   = 0;

    foreach my $d (@$invoices_list) {
      $d->{AMOUNT_FOR_PAY}     = ($d->{DEPOSIT} < 0) ? abs($d->{DEPOSIT}) : 0 - $d->{DEPOSIT};

      $d->{NUMBER}             = $d->{INVOICE_NUM} || '-';
      my ($year, $month, $day) = split(/-/, $d->{DATE}, 3);
      $d->{FROM_DATE_LIT}      = "$day " . $MONTHES_LIT[ int($month) - 1 ] . " $year $_YEAR";
      $d->{DATE_EURO_STANDART} = "$day.$month.$year";
      $d->{FIO}                = $d->{CUSTOMER} if ($d->{CUSTOMER});
      $d->{ADDRESS_FULL}       = "$d->{ADDRESS_STREET}, $d->{ADDRESS_BUILD}, $d->{ADDRESS_FLAT}";
      $d->{TOTAL_SUM}          = sprintf("%.2f", $d->{TOTAL_SUM});
      $d->{A_FIO}              = $d->{ADMIN_FIO};
      $d->{DEPOSIT}            = $d->{DOCS_DEPOSIT};
      $d->{DOC_ID}             = $d->{ID};
      $d->{AMOUNT_FOR_PAY}     = ($d->{DEPOSIT} < 0) ? abs($d->{DEPOSIT}) : 0 - $d->{DEPOSIT};
      $FORM{COMPANY_ID}        = $d->{COMPANY_ID};

      if ($conf{DOCS_VAT_INCLUDE}) {
        $d->{ORDER_TOTAL_SUM_VAT}   = sprintf("%.2f", $d->{TOTAL_SUM} / ((100 + $conf{DOCS_VAT_INCLUDE}) / $conf{DOCS_VAT_INCLUDE}));
        $d->{TOTAL_SUM_WITHOUT_VAT} = sprintf("%.2f", $d->{TOTAL_SUM} - $d->{ORDER_TOTAL_SUM_VAT});
        $d->{TOTAL_SUM_VAT}         = sprintf("%.2f", $conf{DOCS_VAT_INCLUDE});
      }

      ($d->{SUM_MAIN}, $d->{SUM_SUB}) = split(/\./, $d->{TOTAL_SUM});
      $d->{FROM_DATE_LIT} = '';

      my $i    = 0;
      foreach my $order (@{ $Docs->{ORDERS}->{$d->{DOC_ID}} }) {
        $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, $order->{orders}, $order->{unit}, $order->{counts}, $order->{price}, ($order->{counts} * $order->{price})) if (!$conf{DOCS_PDF_PRINT});

        my $count = $order->{counts} || 1;
        my $sum   = sprintf("%.2f", $count * $order->{price});

        $d->{ 'LOGIN_' . $i }       = $d->{LOGIN};
        $d->{ 'ORDER_NUM_' . $i }   = $i;
        $d->{ 'ORDER_NAME_' . $i }  = $order->{orders};
        $d->{ 'ORDER_COUNT_' . $i } = $count;
        $d->{ 'ORDER_PRICE_' . $i } = $order->{price};
        $d->{ 'ORDER_SUM_' . $i }   = $sum;

        $d->{ 'ORDER_PRICE_WITHOUT_VAT_' . $i } = sprintf("%.2f", ($conf{DOCS_VAT_INCLUDE}) ? $order->{price} - $order->{price} / ((100 + $conf{DOCS_VAT_INCLUDE}) / $conf{DOCS_VAT_INCLUDE}) : $line->{price});
        $d->{ 'ORDER_SUM_WITHOUT_VAT_' . $i } = sprintf("%.2f", ($conf{DOCS_VAT_INCLUDE}) ? $sum - ($sum) / ((100 + $conf{DOCS_VAT_INCLUDE}) / $conf{DOCS_VAT_INCLUDE}) : $sum);
        
        if ($line->{fees_id} == 0) {
          $d->{AMOUNT_FOR_PAY} += $d->{ 'ORDER_COUNT_' . $i } * $d->{ 'ORDER_PRICE_' . $i } 
        }
        
        if ($d->{EXCHANGE_RATE} > 0) {
          $d->{ 'ORDER_ALT_SUM_' . $i }               = sprintf("%.2f", $d->{ 'ORDER_SUM_' . $i } * $d->{EXCHANGE_RATE});
          $d->{ 'ORDER_ALT_PRICE_' . $i }             = sprintf("%.2f", $d->{ 'ORDER_PRICE_' . $i } * $d->{EXCHANGE_RATE});
          $d->{ 'ORDER_ALT_VAT_' . $i }               = sprintf("%.2f", $d->{ 'ORDER_VAT_' . $i } * $d->{EXCHANGE_RATE});
          $d->{ 'ORDER_ALT_PRICE_WITHOUT_VAT_' . $i } = sprintf("%.2f", $d->{ 'ORDER_PRICE_WITHOUT_VAT_' . $i } * $d->{EXCHANGE_RATE});
          $d->{ 'ORDER_ALT_SUM_WITHOUT_VAT_' . $i }   = sprintf("%.2f", $d->{ 'ORDER_SUM_WITHOUT_VAT_' . $i } * $d->{EXCHANGE_RATE});
        }
      }

      $d->{TOTAL_SUM}      = sprintf("%.2f", $d->{TOTAL_SUM});
      $d->{AMOUNT_FOR_PAY} = sprintf("%.2f", $d->{AMOUNT_FOR_PAY});
      $d->{DEPOSIT}        = sprintf("%.2f", $d->{DEPOSIT});
      if ($d->{EXCHANGE_RATE} > 0) {
        $d->{TOTAL_ALT_SUM}      = sprintf("%.2f", $d->{TOTAL_SUM} * $d->{EXCHANGE_RATE});
        $d->{AMOUNT_FOR_PAY_ALT} = sprintf("%.2f", $d->{AMOUNT_FOR_PAY} * $d->{EXCHANGE_RATE});
        $d->{DEPOSIT_ALT}        = sprintf("%.2f", $d->{DEPOSIT} * $d->{EXCHANGE_RATE});
        $d->{CHARGED_ALT_SUM}    = sprintf("%.2f", $d->{CHARGED_SUM} * $d->{EXCHANGE_RATE});
      }

      $d->{'TOTAL_SUM_WITHOUT_VAT'} = sprintf("%.2f", ($conf{DOCS_VAT_INCLUDE}) ? $d->{TOTAL_SUM} - ($d->{TOTAL_SUM}) / ((100 + $conf{DOCS_VAT_INCLUDE}) / $conf{DOCS_VAT_INCLUDE}) : $d->{TOTAL_SUM});
      $d->{'TOTAL_SUM_VAT'} = sprintf("%.2f", $d->{TOTAL_SUM} - $d->{'TOTAL_SUM_WITHOUT_VAT'});

      $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];
        $i++;
      }

      push @MULTI_ARR, { %$d, DOC_NUMBER => sprintf("%.6d", $doc_num), };
      $doc_num++;
      
      print "UID: LOGIN: $d->{LOGIN} FIO: $d->{FIO} SUM: $d->{TOTAL_SUM}\n" if ($debug > 2);
    }

    print $html->header() if ($FORM{qindex});
    #my $tpl = ($FORM{COMPANY_ID}) ? 'docs_invoice_company' : 'docs_invoice';
    my $tpl = 'docs_invoice';
    my $single_tpl = $html->tpl_show(
      _include($tpl, 'Docs', { pdf => $FORM{pdf} }),
      undef,
      {
        MULTI_DOCS => \@MULTI_ARR,
        debug      => $debug
      }
    );
    return 0;
  }

  #$conf{DOCS_INVOICES_LIST}='invoice_num,date,customer,total_sum,payment_id';
  if (! $conf{DOCS_INVOICES_LIST}) {
  	if ($user->{UID}) { 
  		$conf{DOCS_INVOICES_LIST}='invoice_num,date,customer,total_sum,payment_id';
  	}
    else {
      $conf{DOCS_INVOICES_LIST}='invoice_num,date,customer,total_sum,payment_id,login,admin_name,created,method,ext_id,group_name';
    }
  }
  my @CAPTION    = ();

  my %EXT_TITLES = (
  invoice_num    => '#',
  date           => $_DATE,
  customer       => $_CUSTOMER,    
  total_sum      => "$_SUM",    
  payment_id     => "$_STATUS",
  login          => "$_USER", 
  admin_name     => "$_ADMIN", 
  created        => "$_CREATED",
  method         => "$_PAYMENT_METHOD", 
  ext_id         => "EXT ID",
  group_name     => "$_GROUP $_NAME",
  currency       => "$_ATL $_CURRENCY",
  );

  my @field_names = split(/,/, $conf{DOCS_INVOICES_LIST});
  foreach my $c ( @field_names ) {
	  push @CAPTION, $EXT_TITLES{$c};
  }


  if ($conf{DOCS_CURRENCY}) {
    push @CAPTION, "$_ATL $_CURRENCY";
  }

  if ($user->{UID}) {
    @CAPTION = ('#', $_DATE, $_CUSTOMER, $_SUM, $_STATUS);
  }
  else {
    if ($FORM{BILL_ID}) {
      push @CAPTION, 'BILL_ID';
    }
    push @CAPTION, '-', '-', '-', '-', '-';
  }


  my $invoice_list  = $Docs->invoices_list({%LIST_PARAMS, 
  	   COLS_NAME   => 1, 
  	   FULL_INFO   => $FORM{BILL_ID},
  	   ORDERS_LIST => $FORM{xml}
  	   });

  my $table = $html->table(
    {
      width      => '100%',
      border     => 1,
      caption    => "$_INVOICES",
      title      => \@CAPTION,
      cols_align => [ 'right', 'right', 'left', 'right', 'left', 'left', 'right', 'center:noprint', 'center:noprint', 
      'center:noprint', 'center:noprint'  ],
      qs         => $pages_qs,
      pages      => $Docs->{TOTAL},
      EXPORT     => ' XML:&xml=1',
      ID         => 'DOCS_INVOICES_LIST',
    }
  );

  my @rows = ();
  my %orders_hash = %{ $self->{ORDERS} };
  foreach my $invoice (@$invoice_list) {
    my $delete = ($permissions{1}{2}) ? $html->button($_DEL, "index=$index&del=$invoice->{id}&UID=$invoice->{uid}", { MESSAGE => "$_DEL ID $line->{id} ?", CLASS => 'del' }) : '';
    @rows = ();
    for(my $i=0; $i<=$#field_names; $i++) {
      my $fname = $field_names[$i];
      my $val   = '';

      if ($fname eq 'date') {
      	$val = $html->button("$invoice->{date}", "qindex=$index&print=$invoice->{id}&UID=$invoice->{uid}$pages_qs" . (($conf{DOCS_PDF_PRINT}) ? '&pdf=1' : ''), { ex_params => 'target=_new' }),
      }
      elsif($fname eq 'payment_id') {
      	$val = ($invoice->{payment_id}) ? (($user->{UID}) ? $payments_status[1] : $html->button($payments_status[1], "index=2&UID=$invoice->{uid}&ID=$invoice->{payment_id}")) : $html->color_mark($payments_status[0], $_COLORS[6]);
      }
      elsif($fname eq 'login') {
        $val = $html->button($invoice->{login}, "index=11&UID=$invoice->{uid}") . (($line->{company_id}) ? $html->br() . $html->button(substr($line->{company_name}, 0, 30), "index=13&COMPANY_ID=$invoice->{company_id}", { CLASS => 'small' }) : '');
      }
      elsif($fname eq 'method') {
        $val = ($PAYMENTS_METHODS{ $invoice->{method} }) ? $PAYMENTS_METHODS{ $invoice->{method} } : $invoice->{method};
      }
      else {
      	$val = $invoice->{$fname};
      }
      push 	@rows, $val;
    }

    if (! $user->{UID}) {
      if ($conf{DOCS_CURRENCY}) {
        push @rows, sprintf("%.2f", $invoice->{alt_sum});
      }

      my $payments_info = ($invoice->{currency} > 0 && !$conf{DOCS_PAYMENT_SYSTEM_CURRENCY}) ? "&SUM=$invoice->{alt_sum}&ISO=$invoice->{currency}" : "&SUM=$invoice->{total_sum}";

      if ($FORM{BILL_ID}) {
        push @rows, $invoice->{bill_id};
      }

      push @rows, $html->button($_PAYMENTS, "index=2&INVOICE_ID=$invoice->{id}&UID=$invoice->{uid}$payments_info", { CLASS => 'payments' }),
      $html->button($_INFO, "index=$index&SHOW_ORDERS=$invoice->{id}&UID=$invoice->{uid}", { 
      	  CLASS           => 'show',
          NEW_WINDOW      => "$SELF_URL?qindex=$index&SHOW_ORDERS=$invoice->{id}&UID=$invoice->{uid}&header=1", 
          NEW_WINDOW_SIZE => "640:600"
       }),
      $html->button($_SEND_MAIL, "qindex=" . get_function_index('docs_invoices_list') . "&sendmail=$invoice->{id}&UID=$invoice->{id}", { ex_params => 'target=_new', CLASS => 'sendmail' }),
      $html->button($_PRINT, "qindex=$index&print=$invoice->{id}&UID=$invoice->{uid}" . (($conf{DOCS_PDF_PRINT}) ? '&pdf=1' : ''), { ex_params => 'target=_new', CLASS => 'print' }),
      $delete;

      if ($FORM{xml}) {
    	  my $orders = '';
    	  #o.invoice_id, o.orders, o.unit, o.counts, o.price, o.fees_id
        my $i=1;
        foreach my $order ( @{ $Docs->{ORDERS}{$invoice->{id}} } ) {
      	  $orders .= "<orders>
      	            <num>$i</num> 
      	            <order>$order->{orders}</order> 
      	            <unit>$order->{unit}</unit> 
      	            <counts>$order->{counts}</counts>
      	            <price>$order->{price}</price>
      	           </orders>";
      	 $i++;
        }
      }
    
    
    	push @rows, $orders;
    }
    
    $table->addrow(@rows);
  }

  print $table->show();

  $table = $html->table(
    {
      width      => '100%',
      cols_align => [ 'right', 'right' ],
      rows       => [ [ (($user->{UID}) ? '' : $html->button("$_PRINT $_LIST", "qindex=$index&print_list=1$pages_qs" . (($conf{DOCS_PDF_PRINT}) ? '&pdf=1' : ''), { BUTTON => 1, ex_params => 'target=new' })), "$_TOTAL:", $html->b($Docs->{TOTAL}) ] ]
    }
  );

  print $table->show();

  return 0;
}

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

  if (!$attr->{COMPANY}) {
    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 {
    $index = $FORM{index};
  }

  form_search({ SEARCH_FORM => ($conf{DOCS_PDF_PRINT}) ? '' : $html->tpl_show(_include('docs_search', 'Docs'), { %info, %FORM }, { notprint => 1 }) });

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

    #$LIST_PARAMS{DESC}='DESC';
  }

  if ($FORM{print_list}) {
    my $list = $Docs->tax_invoice_list(
      {
        %LIST_PARAMS,
        COMPANY_ID => $FORM{COMPANY_ID},
        FULL_INFO  => 1
      }
    );
    my @MULTI_ARR = ();
    my $doc_num   = 0;

    foreach my $line (@$list) {
      my %D = ();
      $D{NUMBER}       = $line->[0];
      $D{DATE}         = $line->[1];
      $D{COMPANY_NAME} = $line->[2];
      $D{ADDRESS}      = "$line->[11], $line->[12], $line->[13]";
      $D{TOTAL_SUM}    = sprintf("%.2f", $line->[3]);

      if ($conf{DOCS_VAT_INCLUDE}) {
        $D{ORDER_TOTAL_SUM_VAT}   = sprintf("%.2f", $D{TOTAL_SUM} / ((100 + $conf{DOCS_VAT_INCLUDE}) / $conf{DOCS_VAT_INCLUDE}));
        $D{TOTAL_SUM_WITHOUT_VAT} = sprintf("%.2f", $D{TOTAL_SUM} - $Docs->{ORDER_TOTAL_SUM_VAT});
        $D{TOTAL_SUM_VAT}         = sprintf("%.2f", $conf{DOCS_VAT_INCLUDE});
      }

      if ($Docs->{TOTAL} > 0) {
        ($Docs->{SUM_MAIN}, $Docs->{SUM_SUB}) = split(/\./, $D{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])) if (!$conf{DOCS_PDF_PRINT});

          my $count = $line->[2] || 1;
          my $sum = sprintf("%.2f", $count * $line->[4]);

          $D{ 'ORDER_NUM_' . $i }               = $i;
          $D{ 'ORDER_NAME_' . $i }              = $line->[1];
          $D{ 'ORDER_COUNT_' . $i }             = $count;
          $D{ 'ORDER_PRICE_' . $i }             = $line->[4];
          $D{ 'ORDER_SUM_' . $i }               = $sum;
          $D{ '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]);
          $D{ 'ORDER_SUM_WITHOUT_VAT_' . $i }   = sprintf("%.2f", ($conf{DOCS_VAT_INCLUDE}) ? $sum - ($sum) / ((100 + $conf{DOCS_VAT_INCLUDE}) / $conf{DOCS_VAT_INCLUDE}) : $sum);
        }

        $D{'TOTAL_SUM_WITHOUT_VAT'} = sprintf("%.2f", ($conf{DOCS_VAT_INCLUDE}) ? $D{TOTAL_SUM} - ($Docs->{TOTAL_SUM}) / ((100 + $conf{DOCS_VAT_INCLUDE}) / $conf{DOCS_VAT_INCLUDE}) : $D{TOTAL_SUM});
        $Docs->{'TOTAL_SUM_VAT'} = sprintf("%.2f", $D{TOTAL_SUM} - $Docs->{'TOTAL_SUM_WITHOUT_VAT'});

        $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];
          $i++;
        }
      }

      #-----------------------

      push @MULTI_ARR, { %D, DOC_NUMBER => sprintf("%.6d", $doc_num), };
      $doc_num++;
      print "UID: LOGIN: $line->[0] FIO: $line->[1] SUM: $line->[2]\n" if ($debug > 2);
    }

    print $html->header() if ($FORM{qindex});
    my $single_tpl = $html->tpl_show(
      _include('docs_tax_invoice', 'Docs', { pdf => $FORM{pdf} }),
      undef,
      {
        MULTI_DOCS => \@MULTI_ARR,

        #SAVE_AS      => $save_filename,
        #DOCS_IN_FILE => $docs_in_file,
        debug => $debug
      }
    );
    return 0;
  }

  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]' ?", CLASS => 'del' }) : '';

    $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]&UID=$line->[8]$pages_qs" . (($conf{DOCS_PDF_PRINT}) ? '&pdf=1' : ''), { ex_params => 'target=_new', CLASS => 'print' }), $delete
    );
  }
  print $table->show();

  $table = $html->table(
    {
      width      => '100%',
      cols_align => [ 'right', 'right' ],
      rows       => [ [ $html->button("$_PRINT $_LIST", "qindex=$index&print_list=1$pages_qs" . (($conf{DOCS_PDF_PRINT}) ? '&pdf=1' : ''), { BUTTON => 1, ex_params => 'target=new' }), "$_TOTAL:", $html->b($Docs->{TOTAL}), "$_SUM:", $html->b($Docs->{SUM}), ] ]
    }
  );

  print $table->show();
  return 0;
}

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

  if (!$attr->{COMPANY} && !$FORM{qindex}) {
    $FORM{subf} = $FORM{index};

    #$index=13;
    $index2 = $FORM{index};
    form_companies();
    return 0;
  }
  elsif ($FORM{qindex}) {
    $Company->info($FORM{COMPANY_ID});
  }
  else {
    $Company = $attr->{COMPANY};
  }
  $Docs->{DATE}      = $DATE;
  $Docs->{DONE_DATE} = $DATE;
  $Docs->{FROM_DATE} = $DATE;

  if ($FORM{create}) {
    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++;
    }

    my ($y, $m) = split(/-/, $FORM{PERIOD});

    my $days_in_month = ($m != 2 ? (($m % 2) ^ ($m > 7)) + 30 : (!($y % 400) || !($y % 4) && ($y % 25) ? 29 : 28));

    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}]\n $_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. \n" . $html->button($_PRINT, "qindex=" . (($FORM{subf}) ? $FORM{subf} : $index) . "&print=$Docs->{DOC_ID}&UID=$LIST_PARAMS{UID}&COMPANY_ID=$FORM{COMPANY_ID}&pdf=1", { ex_params => 'target=_new', CLASS => 'print' }));
          return 0;
        }

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

          docs_print('tax_invoice', { %$Docs, %$Company });
          exit;
        }
        else {
          docs_print('tax_invoice', { %$Docs, %$Company });
        }

        $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->{TOTAL_SUM_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);
      }

      $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'});

      $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];
        $i++;
      }

      #     $Company->{_ipn_u}='123456790';
      #     exit;
      docs_print('tax_invoice', { %$Docs, %$Company });
    }
    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_INVOICE_ID} = "<tr><td>$_TAX_INVOICE $_NUM:</td><td><input type='text' name='INVOICE_ID' value='%INVOICE_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({ UID => $users->{UID} });

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

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

  my $table = $html->table(
    {
      width    => '100%',
      caption  => "$_TAX_INVOICE",
      rowcolor => $_COLORS[0],
      rows     => [
        [
          "$_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,
        subf       => $FORM{subf},
        create     => "1"
      }
    }
  );

  docs_tax_invoice_list($attr);
}

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

}

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

  if ($FORM{create}) {
    if ($FORM{SUM} < 0.01) {
      $html->message('err', "$_ERROR", "$_WRONG_SUM");
      return 0;
    }

    my @uids_arr = split(/, /, $FORM{UIDS});

    my $count     = 0;
    my $total_sum = 0;
    foreach my $uid (@uids_arr) {
      $count++;
      $FORM{UID} = $uid;
      $Docs->invoice_add({%FORM});
      delete($FORM{create});
      if (!$Docs->{errno}) {
        if ($conf{DOCS_PDF_PRINT}) {
          if ($FORM{SEND_EMAIL}) {
            $FORM{pdf}   = 1;
            $FORM{print} = $Docs->{DOC_ID};

            docs_invoice(
              {
                GET_EMAIL_INFO => 1,
                SEND_EMAIL     => 1,
                EMAIL          => $FORM{EMAIL},
                %$attr
              }
            );
          }
        }

        #else {
        #  $html->message('info', "$_INVOICE $_CREATED", "$_INVOICE $_NUM: [$Docs->{INVOICE_ID}] $_DATE: $FORM{DATE} ");
        # }
      }
    }

    $total_sum = $count * $FORM{SUM};
    $html->message('info', "$_INVOICE $_CREATED", "$_INVOICE $_CREATED\n $_COUNT: $count \n $_SUM: $total_sum");
    return 0;
  }

  $FORM{GROUP_SEL} = sel_groups();
  $html->tpl_show(_include('docs_invoice_multi_sel', 'Docs'), {%FORM});
  my $list = $users->list({ %LIST_PARAMS, %FORM });

  my $table = $html->table(
    {
      width      => '100%',
      border     => 1,
      caption    => "$_USERS",
      title      => [ $_LOGIN, $_FIO, $_DEPOSIT, $_CREDIT, "$_STATUS" ],
      cols_align => [ 'left', 'left', 'right', 'right', 'left', 'left', 'right' ],
      qs         => $pages_qs,
      pages      => $users->{TOTAL},
      ID         => 'DOCS_INVOICE_USERS'
    }
  );

  foreach my $line (@$list) {
    $table->addrow(
      $html->form_input('UIDS', $line->[ 5 + $users->{SEARCH_FIELDS_COUNT} ], { TYPE => 'checkbox' }) . ' ' . $html->button($line->[0], "index=15&UID=" . $line->[ 5 + $users->{SEARCH_FIELDS_COUNT} ]),
      $line->[1], $line->[2], $line->[3], $html->color_mark($status[ $line->[4] ], $state_colors[ $line->[4] ]),
    );
  }

  $Docs->{USERS_TABLE} = $table->show();
  $Docs->{DATE}        = $html->date_fld2(
    'PASPORT_DATE',
    {
      FORM_NAME => 'multi_create',
      WEEK_DAYS => \@WEEKDAYS,
      MONTHES   => \@MONTHES,
      DATE      => $DATE
    }
  );
  $Docs->{SUM} = $FORM{SUM} || 0.00;

  $html->tpl_show(_include('docs_invoice_multi_add', 'Docs'), $Docs);
}

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

  use POSIX;

  $users = $user if ($user->{UID});
  $Docs->invoice_defaults();
  $Docs->{DATE} = $DATE;
  $FORM{UID} = $LIST_PARAMS{UID} if (!$FORM{UID});

  if ($FORM{create}) {
    $FORM{SUM} =~ s/\,/\./g;
    if ($FORM{OP_SID} and $FORM{OP_SID} eq $COOKIES{OP_SID}) {
      $html->message('err', "$_DOCS : $_ERROR", "$_EXIST");
    }
    elsif (!$FORM{IDS} && $FORM{SUM} < 0.01) {
      $html->message('err', "$_DOCS :$_ERROR", "$_WRONG_SUM");
    }
    elsif ($FORM{PREVIEW}) {
      docs_preview('invoice', {%FORM});
      return 0;
    }
    else {
      if ($FORM{VAT} && $FORM{VAT} == 1) {
        $FORM{VAT} = $conf{DOCS_VAT_INCLUDE};
      }

      $FORM{DOCS_CURRENCY}=$FORM{CURRENCY};

      if (($conf{SYSTEM_CURRENCY} && $conf{DOCS_CURRENCY})
        && $conf{SYSTEM_CURRENCY} ne $conf{DOCS_CURRENCY})
      {
        use Finance;
        my $Finance = Finance->new($db, $admin, \%conf);
        $Finance->exchange_info(0, { ISO => $FORM{DOCS_CURRENCY} || $conf{DOCS_CURRENCY} });
        $FORM{EXCHANGE_RATE} = $Finance->{ER_RATE};
        $FORM{DOCS_CURRENCY} = $Finance->{ISO};
      }

      $Docs->invoice_add({ %FORM, DEPOSIT => ($FORM{INCLUDE_DEPOSIT}) ?  $users->{DEPOSIT}  : undef });
      if (!$Docs->{errno}) {
        #Add date of last invoice
        if ($attr->{REGISTRATION}) {
          my ($Y, $M, $D) = split(/-/, $DATE, 3);
          $Docs->user_change(
            {
              UID          => $FORM{UID},
              INVOICE_DATE => ($users->{ACTIVATE} ne '0000-00-00') ? $DATE : "$Y-$M-01",
              CHANGE_DATE  => 1
            }
          );
        }

        $FORM{INVOICE_ID} = $Docs->{DOC_ID};
        $Docs->invoice_info($Docs->{DOC_ID}, { UID => $LIST_PARAMS{UID} });
        $Docs->{CUSTOMER} = $self->{COMPANY_NAME} || $Docs->{FIO} || '-';
        ($Docs->{SUM_MAIN}, $Docs->{SUM_SUB}) = split(/\./, $Docs->{TOTAL_SUM});

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

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

        if ($conf{DOCS_PDF_PRINT}) {
          if (!$attr->{QUITE}) {
            $html->message(
              'info',
              "$_INVOICE $_CREATED",
              "$_INVOICE $_NUM: [$Docs->{INVOICE_NUM}]\n $_DATE: $Docs->{DATE}\n $_TOTAL $_SUM: $Docs->{TOTAL_SUM} \n"
              . $html->button("$_SEND $_EMAIL", "qindex=" . get_function_index('docs_invoices_list') . "&INVOICE_ID=$Docs->{DOC_ID}&sendmail=$Docs->{DOC_ID}&UID=$LIST_PARAMS{UID}",    { ex_params => 'target=_new', CLASS => 'sendmail rightAlignText' })
              . $html->button($_PRINT,          "qindex=" . get_function_index('docs_invoices_list') . "&INVOICE_ID=$Docs->{DOC_ID}&print=$Docs->{DOC_ID}&UID=$LIST_PARAMS{UID}&pdf=1", { ex_params => 'target=_new', CLASS => 'print rightAlignText' })
            );

            $FORM{print} = $FORM{INVOICE_ID};
            $FORM{pdf}   = 1;
            delete($FORM{create});

            my $return = docs_invoice(
              {
                GET_EMAIL_INFO => 1,
                SEND_EMAIL     => (defined($FORM{SEND_EMAIL})) ? $FORM{SEND_EMAIL} : $attr->{SEND_EMAIL},
                EMAIL          => $FORM{EMAIL},
                %$attr
              }
            );
            
            return ($attr->{REGISTRATION}) ? 0 : $return;
          }
        }
        else {
          $html->message(
            'info',
            "$_INVOICE $_CREATED",
            "$_INVOICE $_NUM: [$Docs->{INVOICE_NUM}]\n $_DATE: $FORM{DATE} \n"
            . $html->button("$_SEND $_EMAIL", "qindex=$index&INVOICE_ID=$Docs->{DOC_ID}&sendmail=$Docs->{DOC_ID}&UID=$LIST_PARAMS{UID}", { ex_params => 'target=_new', CLASS => 'sendmail rightAlignText' })
            . $html->button($_PRINT,          "qindex=$index&INVOICE_ID=$Docs->{DOC_ID}&print=$Docs->{DOC_ID}&UID=$LIST_PARAMS{UID}",    { ex_params => 'target=_new', CLASS => 'print rightAlignText' })
          );
        }

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

          if ($Docs->{COMPANY_ID}) {
            $Company->info($Docs->{COMPANY_ID});
            docs_print('invoice_company', { %$Docs, %$Company, SUFIX => ($Docs->{VAT} > 0) ? 'vat' : '' });
          }
          else {
            docs_print('invoice', { %$Docs, %$attr });
          }

          exit;
        }
        else {
          if ($Docs->{COMPANY_ID}) {
            $Company->info($Docs->{COMPANY_ID});
            docs_print('invoice_company', { %$Docs, %$Company, %$attr, SUFIX => ($Docs->{VAT} > 0) ? 'vat' : '' });
          }
          else {
            docs_print('invoice', { %$Docs, %$attr }) if (!$FORM{QUICK});
          }
        }

        $FORM{'print'} = 1;
        return 0;
      }
      else {
        if (!$FORM{QUICK}) {
          if ($Docs->{errno} && $Docs->{errno} == 2) {
            $html->message('err', "$_DOCS : $_ERROR", "$_DOC $_NOT_EXIST");
          }
          else {
            $html->message('err', "$_DOCS : $_ERROR", "[$Docs->{errno}] $err_strs{$Docs->{errno}}");
          }
        }
      }
    }

  }
  elsif ($FORM{print}) {

    $Docs->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->{CUSTOMER} = $self->{COMPANY_NAME} || $Docs->{FIO} || '-' if (!$Docs->{CUSTOMER});
    $Docs->{DEPOSIT}  = sprintf("%.2f", $Docs->{DEPOSIT});

    if ($FORM{CHECK_PEYMENT_ID} && $Docs->{PAYMENT_ID}) {
      return 0;
    }

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

      (undef, $Docs->{TIME}) = split(/ /, $Docs->{CREATED}, 2);
      $Docs->{AMOUNT_FOR_PAY} = ($Docs->{DEPOSIT} < 0) ? abs($Docs->{DEPOSIT}) : 0 - $Docs->{DEPOSIT};


      my $list = $Docs->{ORDERS};
      my $i    = 0;
      foreach my $line (@$list) {
        $i++;
        $Docs->{ORDER} .= $html->tpl_show(
          _include('docs_invoice_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});

        my $count = $line->[2] || 1;
        my $sum   = sprintf("%.2f", $count * $line->[4]);

        $Docs->{ 'LOGIN_' . $i }                   = $line->[6];
        $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_VAT_' . $i }               = ($conf{DOCS_VAT_INCLUDE}) ? sprintf("%.2f", $line->[4] / ((100 + $conf{DOCS_VAT_INCLUDE}) / $conf{DOCS_VAT_INCLUDE})) : 0;
        $Docs->{ 'ORDER_PRICE_WITHOUT_VAT_' . $i } = sprintf("%.2f", ($Docs->{ 'ORDER_VAT_' . $i }) ? $line->[4] - $Docs->{ 'ORDER_VAT_' . $i } : $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);

        # not charged service
        if ($line->[5] == 0) {
          $Docs->{AMOUNT_FOR_PAY} += $Docs->{ 'ORDER_COUNT_' . $i } * $Docs->{ 'ORDER_PRICE_' . $i } 
        }

        #alternative currancy sum
        if ($Docs->{EXCHANGE_RATE} > 0) {
          $Docs->{ 'ORDER_ALT_SUM_' . $i }               = sprintf("%.2f", $Docs->{ 'ORDER_SUM_' . $i } * $Docs->{EXCHANGE_RATE});
          $Docs->{ 'ORDER_ALT_PRICE_' . $i }             = sprintf("%.2f", $Docs->{ 'ORDER_PRICE_' . $i } * $Docs->{EXCHANGE_RATE});
          $Docs->{ 'ORDER_ALT_VAT_' . $i }               = sprintf("%.2f", $Docs->{ 'ORDER_VAT_' . $i } * $Docs->{EXCHANGE_RATE});
          $Docs->{ 'ORDER_ALT_PRICE_WITHOUT_VAT_' . $i } = sprintf("%.2f", $Docs->{ 'ORDER_PRICE_WITHOUT_VAT_' . $i } * $Docs->{EXCHANGE_RATE});
          $Docs->{ 'ORDER_ALT_SUM_WITHOUT_VAT_' . $i }   = sprintf("%.2f", $Docs->{ 'ORDER_SUM_WITHOUT_VAT_' . $i } * $Docs->{EXCHANGE_RATE});
        }
      }

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

      if ($Docs->{EXCHANGE_RATE} > 0) {
        $Docs->{TOTAL_ALT_SUM}      = sprintf("%.2f", $Docs->{TOTAL_SUM} * $Docs->{EXCHANGE_RATE});
        $Docs->{AMOUNT_FOR_PAY_ALT} = sprintf("%.2f", $Docs->{AMOUNT_FOR_PAY} * $Docs->{EXCHANGE_RATE});
        $Docs->{DEPOSIT_ALT}        = sprintf("%.2f", $Docs->{DEPOSIT} * $Docs->{EXCHANGE_RATE});
        $Docs->{CHARGED_ALT_SUM}    = sprintf("%.2f", $Docs->{CHARGED_SUM} * $Docs->{EXCHANGE_RATE});
      }

      

      $attr->{SEND_EMAIL} = 0 if (!defined($attr->{SEND_EMAIL}));
      if ($attr->{GET_EMAIL_INFO} && $attr->{SEND_EMAIL}) {
      	print $FORM{pdf}=undef;
        $attr->{EMAIL_MSG_TEXT}        = $html->tpl_show(_include('docs_invoice_email', 'Docs'), { %$users, %FORM, %$attr, %$Docs }, { OUTPUT2RETURN => 1 });
        $attr->{EMAIL_ATTACH_FILENAME} = 'invoice_' . $Docs->{INVOICE_NUM} . '.pdf' if (!$attr->{EMAIL_ATTACH_FILENAME});
        $attr->{EMAIL_MSG_SUBJECT}     = "ABillS - $_INVOICE: $Docs->{INVOICE_NUM}" if (!$attr->{EMAIL_MSG_SUBJECT});
      }

      if ($conf{DOCS_EXTRA_DEPOSIT}) {
        $Docs->{DEPOSIT} =  sprintf("%.2f", $Docs->{DEPOSIT} + $Docs->{CHARGED_SUM});
        if ($Docs->{EXCHANGE_RATE}) {
          $Docs->{DEPOSIT_ALT}    = sprintf("%.2f", $Docs->{DEPOSIT} * $Docs->{EXCHANGE_RATE});
        }
      }

      $FORM{pdf} = $conf{DOCS_PDF_PRINT};
      if ($Docs->{COMPANY_ID}) {
        $Company->info($Docs->{COMPANY_ID});
        delete $Company->{DEPOSIT};
        return docs_print('invoice_company', { %$Docs, %$Company, %$attr, SUFIX => ($Docs->{VAT} > 0) ? 'vat' : '' });
      }
      else {
        return docs_print('invoice', { %$Docs, %$attr });
      }
    }
    else {
      $html->message('err', "$_DOCS : $_ERROR", "[$Docs->{errno}] $err_strs{$Docs->{errno}}") if (!$FORM{QUICK});
    }
    return 0;
  }
  elsif ($FORM{sendmail}) {
    $FORM{print} = $FORM{sendmail};
    if (
      docs_invoice(
        {
          SEND_EMAIL     => 1,
          GET_EMAIL_INFO => 1
        }
      )
    )
    {
      $html->message('info', "$_INFO", "E-Mail $_SENDED ");
    }
    else {
      $html->message('info', "$_ERROR", "E-Mail $_SENDED Error: $FORM{ERR_MESSAGE} ");
    }

    return 0;
  }
  elsif (defined($FORM{change})) {
    $Docs->invoice_change({ %FORM, UID => $users->{UID} });
    if (!$Docs->{errno}) {
      $html->message('info', "$_INFO", "$_CHANGED N: [$FORM{DOC_ID}]");
    }
  }
  elsif (defined($FORM{chg})) {
    $Docs->invoice_info($FORM{chg});
    if (!$Docs->{errno}) {
      $html->message('info', "$_INFO", "$_CHANGING N: [$FORM{chg}]");
    }
  }
  elsif (defined($FORM{del}) && $FORM{is_js_confirmed}) {
    $Docs->invoice_del($FORM{del});
    if (!$Docs->{errno}) {
      $html->message('info', "$_INFO", "$_DELETED ID: [$FORM{del}]");
    }
  }
  elsif ($FORM{SHOW_ORDERS}) {
    $Docs->invoice_info($FORM{SHOW_ORDERS}, { UID => $LIST_PARAMS{UID} });

    my $table = $html->table(
      {
        width => ($user->{UID}) ? '600' : '100%',
        caption     => "$_INVOICE: $Docs->{INVOICE_NUM} $_DATE: $Docs->{DATE}",
        border      => 1,
        title_plain => [ '#', "$_NAME", "$_COUNT", "$_PRICE", $_SUM ],
        ID          => 'DOCS_INVOCE_ORDERS',
      }
    );

    if ($Docs->{TOTAL} > 0) {
      my $list = $Docs->{ORDERS};
      foreach my $line (@$list) {
        $i++;
        $table->addrow($i, $line->[1], 
          $line->[2], 
          $line->[4], 
          sprintf("%.2f", $line->[2] * $line->[4])
        );
      }
    }

    print $table->show();
    return 0;
  }

  if (!$user->{UID}) {
    $Docs->{FORM_INVOICE_ID} = "<tr><td>$_INVOICE $_NUM:</td><td><input type='text' name='INVOICE_NUM' value='%INVOICE_ID%' size=12></td></tr>\n";
    $Docs->{DATE_FIELD} = $html->date_fld2('DATE', { MONTHES => \@MONTHES, FORM_NAME => 'invoice_add', WEEK_DAYS => \@WEEKDAYS });
  }
  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
    }
  );

  $users->pi({ UID => $users->{UID} || $FORM{UID} });
  $Docs->{OP_SID}   = mk_unique_value(16);
  $Docs->{CUSTOMER} = $self->{COMPANY_NAME} || $users->{FIO} || '-';
  $Docs->{CAPTION}  = $_INVOICE;
  if (!$FORM{pdf}) {
    my ($Y, $M, $D) = split(/-/, $DATE);

    if ($attr->{REGISTRATION} || $FORM{ALL_SERVICES}) {
      $Docs->{DATE} = $html->date_fld2('DATE', { MONTHES => \@MONTHES, FORM_NAME => 'receipt_add', WEEK_DAYS => \@WEEKDAYS });

      # Get docs info
      $Docs->user_info($FORM{UID});
      if ($Docs->{TOTAL}) {      	
        if (!defined($FORM{NEXT_PERIOD})) {
        	$FORM{NEXT_PERIOD}=0;
          #$FORM{NEXT_PERIOD} = $Docs->{INVOICING_PERIOD} if ($Docs->{INVOICING_PERIOD});
        }
      }

      if (!$FORM{INCLUDE_CUR_BILLING_PERIOD}) {
        my ($Y, $M, $D) = split(/-/, $DATE, 3);
        $FORM{FROM_DATE} = "$Y-01-01";
      }

      my $num = 0;
      if ($user->{DEPOSIT} > 0) {
        return 0;
      }

      my $table = $html->table(
        {
          width   => ($user->{UID}) ? '600'                  : '100%',
          caption => ($user->{UID}) ? $_ACTIVATE_NEXT_PERIOD : "$_INVOICE $_PERIOD: $Y-$M",
          border  => 1,
          title_plain => [ '#',     "$_LOGIN", "$_DATE", "$_DESCRIBE", $_SUM ],
          cols_align  => [ 'right', 'left',    'right',  'left' ],
          pages       => $Docs->{TOTAL},
          ID          => 'DOCS_INVOCE_ORDERS',
          class       => 'form'
        }
      );

      my $total_sum         = 0;
      my $total_not_invoice = 0;
      my $amount_for_pay    = 0;

      # Get invoces
      my %current_invoice = ();
      my $invoice_list    = $Docs->invoices_list(
        {
          UID         => $FORM{UID},
          #PAYMENT_ID  => 0,
          ORDERS_LIST => 1,
          COLS_NAME   => 1,
          PAGE_ROWS   => 1000
        }
      );


      foreach my $doc_id (keys %{ $Docs->{ORDERS} }) {
        foreach my $invoice ( @{ $Docs->{ORDERS}->{$doc_id} }) {
          $current_invoice{ $invoice->{orders} } = $invoice->{invoice_id};
        }
      }

      if (!$user->{UID}) {
        my $list = $Docs->invoice_new(
          {
            FROM_DATE => $FORM{FROM_DATE} || $html->{FROM_DATE},
            TO_DATE   => $FORM{TO_DATE} || $html->{TO_DATE} || $DATE,
            PAGE_ROWS => 500,
            UID       => $users->{UID}
          }
        );

        foreach my $line (@$list) {
          next if ($line->[5]);
          $num++;
          my $date = $line->[2];
          $date =~ s/ \d+:\d+:\d+//g;
#=comments
# Not invoiced fees
          if (! $current_invoice{$line->[3]}) {
            $table->addrow(
              $html->form_input("ORDER_" . $line->[0], "$line->[3]", { TYPE => 'hidden', OUTPUT2RETURN => 1 })
              . $html->form_input("SUM_" . $line->[0],     "$line->[4]", { TYPE => 'hidden', OUTPUT2RETURN => 1 })
              . $html->form_input("FEES_ID_" . $line->[0], "$line->[0]", { TYPE => 'hidden', OUTPUT2RETURN => 1 })
              . ((! $current_invoice{$line->[3]}) ? $html->form_input("IDS", "$line->[0]", { TYPE => 'checkbox', STATE => 1, OUTPUT2RETURN => 1 }) . "$num" : ''),
              "$line->[1]", 
              "$line->[2]", 
              "$line->[3] $date". (($current_invoice{$line->[3]}) ? ' ' . $html->color_mark($_EXIST, 'red') : ''), 
              "$line->[4]"
            );
          }
#=cut     
          $total_not_invoice += $line->[4];
        }
      }
      else {
        $users = $user;
        $FORM{NEXT_PERIOD} = 1;
      }

      #($users->{DEPOSIT}<0) ? abs($users->{DEPOSIT}) : 0;
      my $date = $DATE;
      if ($users->{ACTIVATE} ne '0000-00-00') {
        $date = $users->{ACTIVATE};
        $FORM{FROM_DATE} = $users->{ACTIVATE};
      }

      my ($Y, $M, $D) = split(/-/, $date);
      my $start_period_unixtime;
      if ($users->{ACTIVATE} ne '0000-00-00') {
        $start_period_unixtime = (mktime(0, 0, 0, $D, ($M - 1), ($Y - 1900), 0, 0, 0));
        $Docs->{CURENT_BILLING_PERIOD_START} = $users->{ACTIVATE};
        $Docs->{CURENT_BILLING_PERIOD_STOP} = strftime("%Y-%m-%d", localtime((mktime(0, 0, 0, $D, ($M - 1), ($Y - 1900), 0, 0, 0) + 30 * 86400)));
      }
      else {
        $D                                   = '01';
        $Docs->{CURENT_BILLING_PERIOD_START} = "$Y-$M-$D";
        $TO_D                                = ($M != 2 ? (($M % 2) ^ ($M > 7)) + 30 : (!($Y % 400) || !($Y % 4) && ($Y % 25) ? 29 : 28));
        $Docs->{CURENT_BILLING_PERIOD_STOP}  = "$Y-$M-$TO_D";
      }

      #Next period payments
      if ($FORM{NEXT_PERIOD}) {
        my $cross_modules_return = cross_modules_call('_docs');
        my $next_period          = $FORM{NEXT_PERIOD};
        if ($users->{ACTIVATE} ne '0000-00-00') {
          ($Y, $M, $D) = split(/-/, strftime("%Y-%m-%d", localtime((mktime(0, 0, 0, $D, ($M - 1), ($Y - 1900), 0, 0, 0) + ((($start_period_unixtime > time) ? 0 : 1) + 30 * (($start_period_unixtime > time) ? 0 : 1)) * 86400))));
          $FORM{FROM_DATE} = "$Y-$M-$D";

          ($Y, $M, $D) = split(/-/, strftime("%Y-%m-%d", localtime((mktime(0, 0, 0, $D, ($M - 1), ($Y - 1900), 0, 0, 0) + ((($start_period_unixtime > time) ? 1 : (1 * $next_period - 1)) + 30 * (($start_period_unixtime > time) ? 1 : $next_period)) * 86400))));
          $FORM{TO_DATE} = "$Y-$M-$D";
        }
        else {
          $M += 1;
          if ($M < 12) {
            $M = sprintf("%02d", $M);
          }
          else {
            $M = sprintf("%02d", $M - 12);
            $Y++;
          }
          $FORM{FROM_DATE} = "$Y-$M-$D";

          $M += $next_period - 1;
          if ($M < 12) {
            $M = sprintf("%02d", $M);
          }
          else {
            $M = sprintf("%02d", $M - 13);
            $Y++;
          }

          if ($users->{ACTIVATE} eq '0000-00-00') {
            $TO_D = ($M != 2 ? (($M % 2) ^ ($M > 7)) + 30 : (!($Y % 400) || !($Y % 4) && ($Y % 25) ? 29 : 28));
          }
          else {
            $TO_D = $D;
          }

          $FORM{TO_DATE} = "$Y-$M-$TO_D";
        }

        my $period_from = $FORM{FROM_DATE};
        my $period_to   = $FORM{FROM_DATE};

        foreach my $module (sort keys %$cross_modules_return) {
          if (ref $cross_modules_return->{$module} eq 'ARRAY') {
            next if ($#{ $cross_modules_return->{$module} } == -1);
            $table->{extra} = "colspan='5' class='small'";
            $table->addrow("$module");
            $table->{extra} = undef;

            foreach my $line (@{ $cross_modules_return->{$module} }) {
              my ($name, $describe, $sum) = split(/\|/, $line);
              next if ($sum < 0);

              $period_from = $FORM{FROM_DATE};

              for (my $i = ($FORM{NEXT_PERIOD} == -1) ? -2 : 0 ; $i < int($FORM{NEXT_PERIOD}) ; $i++) {
                $result_sum = sprintf("%.2f", $sum);

                if ($users->{REDUCTION} && $module ne 'Abon') {
                  $result_sum = sprintf("%.2f", $sum * (100 - $users->{REDUCTION}) / 100);
                }

                my ($Y, $M, $D) = split(/-/, $period_from, 3);
                if ($users->{ACTIVATE} ne '0000-00-00') {
                  ($Y, $M, $D) = split(/-/, strftime("%Y-%m-%d", localtime((mktime(0, 0, 0, $D, ($M - 1), ($Y - 1900), 0, 0, 0)))));    #+ (31 * $i) * 86400) ));
                  $period_from = "$Y-$M-$D";

                  ($Y, $M, $D) = split(/-/, strftime("%Y-%m-%d", localtime((mktime(0, 0, 0, $D, ($M - 1), ($Y - 1900), 0, 0, 0) + (30) * 86400))));
                  $period_to = "$Y-$M-$D";
                }
                else {
                  $M += 1 if ($i > 0);
                  if ($M < 12) {
                    $M = sprintf("%02d", $M);
                  }
                  else {
                    $M = sprintf("%02d", $M - 12);
                    $Y++;
                  }
                  $period_from = "$Y-$M-01";

                  #$M+=1;
                  if ($M < 12) {
                    $M = sprintf("%02d", $M);
                  }
                  else {
                    $M = sprintf("%02d", $M - 13);
                    $Y++;
                  }

                  if ($users->{ACTIVATE} eq '0000-00-00') {
                    $TO_D = ($M != 2 ? (($M % 2) ^ ($M > 7)) + 30 : (!($Y % 400) || !($Y % 4) && ($Y % 25) ? 29 : 28));
                  }
                  else {
                    $TO_D = $D;
                  }

                  $period_to = "$Y-$M-$TO_D";
                }

                my $order = "$name $describe($period_from-$period_to)";

                $num++ if (!$current_invoice{$order});
                $table->addrow(
                  (
                    (!$current_invoice{$order})  ? $html->form_input('ORDER_' . $num, "$order",    { TYPE => 'hidden', OUTPUT2RETURN => 1 })
                    . $html->form_input('SUM_' . $num,   $result_sum, { TYPE => 'hidden', OUTPUT2RETURN => 1 })
                    . $html->form_input('IDS', "$num", { TYPE => ($user->{UID}) ? 'hidden' : 'checkbox', STATE => 'checked', OUTPUT2RETURN => 1 })
                    . $num
                    : ''
                  ),
                  $users->{LOGIN},
                  $period_from,
                  $order . (($current_invoice{$order}) ? ' ' . $html->color_mark($_EXIST, 'red') : ''),
                  $result_sum
                );

                $total_sum += $result_sum if (!$current_invoice{$order});

                $period_from = strftime("%Y-%m-%d", localtime((mktime(0, 0, 0, $D, ($M - 1), ($Y - 1900), 0, 0, 0) + 1 * 86400)));
              }
            }
          }
        }
      }

      if ($users->{DEPOSIT} != 0 ) {
        $amount_for_pay = ($total_sum < $users->{DEPOSIT}) ? 0 : $total_sum - $users->{DEPOSIT};
      }
      else {
      	$amount_for_pay = $total_sum;
      }
      $total_sum += $total_not_invoice;

      $table->{extra} = " colspan=4 class=total ";
      $table->addrow("$_COUNT: $num $_TOTAL $_SUM: ", sprintf("%.2f", $total_sum));
      $table->addrow($html->b("$_DEPOSIT:"),        $html->b(sprintf("%.2f", $users->{DEPOSIT})));
      $table->addrow($html->b("$_AMOUNT_FOR_PAY:"), $html->b(sprintf("%.2f", $amount_for_pay)));

      $Docs->{FROM_DATE} = $html->date_fld2('FROM_DATE', { MONTHES => \@MONTHES, FORM_NAME => 'invoice_add', WEEK_DAYS => \@WEEKDAYS });
      $Docs->{TO_DATE}   = $html->date_fld2('TO_DATE',   { MONTHES => \@MONTHES, FORM_NAME => 'invoice_add', WEEK_DAYS => \@WEEKDAYS });
      $FORM{NEXT_PERIOD} = 0 if ($FORM{NEXT_PERIOD} < 0);
      if ($attr->{REGISTRATION}) {
        return 0 if (!$attr->{ACTION});
        $Docs->{BACK} = $html->form_input('back', "$_BACK", { TYPE => 'submit' });
        $Docs->{NEXT} = $html->form_input($attr->{ACTION}, "$attr->{LNG_ACTION}", { TYPE => 'submit' });
      }

      if ($user->{UID}) {
        return 0 if (!$num);

        $action = $html->form_input('make', "$_CREATE $_INVOICE", { TYPE => 'submit', OUTPUT2RETURN => 1 });
        $table->{rowcolor} = 'even';
        $table->{extra}    = ' colspan=5 align=center';
        $table->addrow($action);

        my $content = $html->form_main(
          {
            CONTENT => $table->show({ OUTPUT2RETURN => 1 }),
            HIDDEN  => {
              index    => "$index",
              UID      => $FORM{UID},
              DATE     => $DATE,
              create   => 1,
              CUSTOMER => $Docs->{CUSTOMER},
              step     => $FORM{step},
              #ALL_SERVICES   => 1
            },
            NAME => 'DOCS_SERVICES_INVOICE',
          }
        );

        #print $content;
      }
      else {
        $Docs->{ORDERS} = $table->show({ OUTPUT2RETURN => 1 });
        #$FORM{NEXT_PERIOD}=$Docs->{INVOICING_PERIOD} if (! $FORM{NEXT_PERIOD});
        $html->tpl_show(_include('docs_receipt_add', 'Docs'), { %FORM, %$attr, %$Docs, %$users }) if (!$FORM{pdf});
      }
    }
    #
    else {
      $Docs->{ORDERS} = $html->tpl_show(
        _include('docs_invoice_orders', 'Docs'),
        {
          %$Docs,
          %$users,
          MONTH => $MONTHES[ int($M - 1) ],
          DATE  => $DATE,
          TIME  => $TIME,
          %FORM
        },
        { OUTPUT2RETURN => 1 }
      );
      if ($user->{UID}) {
        $html->tpl_show(_include('docs_invoice_client_add', 'Docs'), { %$Docs, %$users, %FORM });
      }
      else {
      	$html->tpl_show(_include('docs_invoice_add', 'Docs'), { %$Docs, %$users, %FORM });
      }
    }

  }
  return 0;
}

#**********************************************************
# 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) = @_;

  $conf{DOCS_LANGUAGE} = $conf{default_language} if (!$conf{DOCS_LANGUAGE});

  if ($conf{DOCS_LANGUAGE} && $html->{language} ne $conf{DOCS_LANGUAGE}) {
    if (-f "../Abills/modules/Docs/lng_$conf{DOCS_LANGUAGE}.pl") {
      require "../language/$conf{DOCS_LANGUAGE}.pl";
      require "../Abills/modules/Docs/lng_$conf{DOCS_LANGUAGE}.pl";
    }
    elsif (-f $Bin . "/../Abills/modules/Docs/lng_$conf{DOCS_LANGUAGE}.pl") {
      require $Bin . "/../language/$conf{DOCS_LANGUAGE}.pl";
      require $Bin . "/../Abills/modules/Docs/lng_$conf{DOCS_LANGUAGE}.pl";
    }
    else {
      require "../../language/$conf{DOCS_LANGUAGE}.pl";
      require "../../Abills/modules/Docs/lng_$conf{DOCS_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->{A_FIO} = $admin->{A_FIO};

  $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}
    }
  );

  if ($attr->{TOTAL_ALT_SUM}) {
    $attr->{SUM_ALT_LIT} = int2ml(
    "$attr->{TOTAL_ALT_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}
    }
  );
  	
  }

  if ($Docs->{ORDER_TOTAL_SUM_VAT}) {
    $attr->{ORDER_TOTAL_SUM_VAT_LIT} = int2ml(
      "$attr->{ORDER_TOTAL_SUM_VAT}",
      {
        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;

  #Make individual templates for paments methods
  if ($attr->{PAYMENT_METHOD_ID} && $FORM{pdf}) {
    my $new_template = $template . '_' . $attr->{PAYMENT_METHOD_ID};
    my $template_content = _include('docs_' . $new_template, 'Docs', { $FORM{pdf} => 1 });
    if ($template_content !~ /No such tem/ && $template_content ne '') {
      $template = $new_template;
    }
  }

  print $html->header() if ($FORM{qindex});
  if ($LIST_PARAMS{COMPANY_ID}) {

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

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

    if ($users->{GID}) {
      $users->group_info($users->{GID});
      if ($users->{SEPARATE_DOCS}) {
        $attr->{SUFIX} = "$users->{GID}";
      }
    }

    $Docs->user_info($Docs->{UID});
    if ($Docs->{TOTAL} > 0) {
      $users->{EMAIL} = $Docs->{EMAIL} if ($Docs->{EMAIL});
      $attr->{SEND_EMAIL} = $Docs->{SEND_DOCS} if (!defined($attr->{SEND_EMAIL}) && $Docs->{SEND_DOCS});
    }
  }

  my $html_old;
  if ($FORM{pdf} && !$html->{pdf_output}) {
    $html_old = $html;
    PDF::API2->import();
    require Abills::PDF;
    my $pdf = Abills::PDF->new(
      {
        IMG_PATH => $IMG_PATH,
        NO_PRINT => defined($attr->{'NO_PRINT'}) ? $attr->{'NO_PRINT'} : 1,
        CONF     => $CONF,
        CHARSET  => $conf{default_charset}
      }
    );
    $html = $pdf;
  }

  my $content = $html->tpl_show(
    _include(
      "docs_$template",
      'Docs',
      {
        pdf           => $FORM{pdf},
        OUTPUT2RETURN => $attr->{OUTPUT2RETURN},
        SUFIX         => $attr->{SUFIX}
      }
    ),
    { %$users, %$attr }
  );

  $html = $html_old if ($html_old);
  delete $FORM{pdf};

  if ($conf{DOCS_LANGUAGE} && $html->{language} ne $conf{DOCS_LANGUAGE}) {
    if (-f "../Abills/modules/Docs/lng_$html->{language}.pl") {
      require "../language/$html->{language}.pl";
      require "../Abills/modules/Docs/lng_$html->{language}.pl";
    }
    elsif (-f $Bin . "/../Abills/modules/Docs/lng_$html->{language}.pl") {
      require $Bin . "/../language/$html->{language}.pl";
      require $Bin . "/../Abills/modules/Docs/lng_$html->{language}.pl";
    }
    else {
      delete $INC{"../../language/$html->{language}.pl"};
      require "../../language/$html->{language}.pl";
      require "../../Abills/modules/Docs/lng_$html->{language}.pl";
    }
  }

  if ($attr->{SEND_EMAIL} && !$attr->{OUTPUT2RETURN}) {    # && ! $FORM{SKIP_SEND_EMAIL}) {
                                                           # Email not specified
    if (!$attr->{EMAIL} && !$users->{EMAIL}) {
      $FORM{ERR_MESSAGE} = "Email $_NOT_EXIST";
      return 0;
    }

    sendmail(
      "$conf{ADMIN_MAIL}",
      $attr->{EMAIL} || $users->{EMAIL},
      $attr->{EMAIL_MSG_SUBJECT} || "ABillS",
      $attr->{EMAIL_MSG_TEXT},
      "$conf{MAIL_CHARSET}",
      '',
      {
        ATTACHMENTS => [
          {
            CONTENT      => $content,
            CONTENT_TYPE => 'Content-type: application/pdf',
            FILENAME     => ($attr->{EMAIL_ATTACH_FILENAME}) ? $attr->{EMAIL_ATTACH_FILENAME} : $template . '.pdf',
          }
        ],
        TEST => $FORM{DEBUG} || undef
      }
    );

  }
  else {
    return $content;
  }
}

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

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

  foreach my $line (@$list) {
    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 });
}

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

  if (!$attr->{COMPANY}) {
    if ($LIST_PARAMS{COMPANY_ID} || $FORM{COMPANY_ID}) {
      docs_acts();
      return 0 if ($FORM{'print'});
    }
    elsif (defined($FORM{del}) && $FORM{is_js_confirmed}) {
      $Docs->act_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_acts();

      return 0;
    }
  }
  else {
    $index = $FORM{index};
  }

  form_search(
    {
      SEARCH_FORM => ($FORM{pdf}) ? '' : $html->tpl_show(_include('docs_search', 'Docs'), { %info, %FORM }, { notprint => 1 }),
      HIDDEN_FIELDS => { COMPANY_ID => $FORM{COMPANY_ID} || undef }
    }
  );

  #  Date  Customer  Sum  User  Administrators  Time
  if (!defined($FORM{sort})) {
    $LIST_PARAMS{SORT} = 1;
    $LIST_PARAMS{DESC} = 'DESC';
  }

  my $list = $Docs->acts_list({ %LIST_PARAMS, COMPANY_ID => $FORM{COMPANY_ID} });

  my $table = $html->table(
    {
      width      => '100%',
      border     => 1,
      caption    => "$_ACTS",
      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'
    }
  );

  $pages_qs = '';

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

    $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=$FORM{index}&print=$line->[8]&COMPANY_ID=$line->[7]" . (($conf{DOCS_PDF_PRINT}) ? '&pdf=1' : '') . (($pages_qs) ? $pages_qs : ""), { ex_params => 'target=_new', CLASS => 'print' }), $delete
    );
  }
  print $table->show();

  $table = $html->table(
    {
      width      => '100%',
      cols_align => [ 'right', 'right' ],
      rows       => [ [ $html->button("$_PRINT $_LIST", "qindex=$index&print_list=1$pages_qs" . (($conf{DOCS_PDF_PRINT}) ? '&pdf=1' : ''), { BUTTON => 1, ex_params => 'target=new' }), "$_TOTAL:", $html->b($Docs->{TOTAL}), "$_SUM:", $html->b($Docs->{SUM}), ] ]
    }
  );

  print $table->show();

  return 0;
}

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

  if (!$attr->{COMPANY} && !$FORM{qindex}) {
    $FORM{subf} = $FORM{index};
    $index      = 13;
    $index2     = $FORM{index};
    form_companies();
    return 0;
  }
  elsif ($FORM{qindex}) {
    $Company->info($FORM{COMPANY_ID});
  }
  else {
    $Company = $attr->{COMPANY};
  }

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

  #$LIST_PARAMS{COMPANY_ID}=$FORM{COMPANY_ID};
  $Docs->{DATE}      = $FORM{DATE} || $DATE;
  $Docs->{DONE_DATE} = $DATE;
  $Docs->{FROM_DATE} = $DATE;

  my ($y, $m) = split(/-/, $FORM{MONTH});
  my $days_in_month = ($m != 2 ? (($m % 2) ^ ($m > 7)) + 30 : (!($y % 400) || !($y % 4) && ($y % 25) ? 29 : 28));

  if ($FORM{create}) {
    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);

    $FORM{SUM} = $Fees->{SUM};

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

    #Get internet using
    my $report_list = $Sessions->reports(
      {
        INTERVAL   => $interval,
        TP_ID      => $TP_ID,
        COMPANY_ID => $FORM{COMPANY_ID}
      }
    );

    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->act_add({ %FORM, DATE => "$FORM{MONTH}-01" });

      if ($Docs->{errno} == 7) {
        $html->message('err', $_ERROR, "$_EXIST");
      }
      elsif ($Docs->{errno}) {
        $html->message('err', $_ERROR, "[$Docs->{errno}] $err_strs{$Docs->{errno}}");
      }
    }
  }
  elsif ($FORM{print}) {
    $Docs->act_info($FORM{print}, { UID => $LIST_PARAMS{UID} });

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

    if ($Docs->{TOTAL} > 0) {
      ($Docs->{SUM_MAIN}, $Docs->{SUM_SUB}) = split(/\./, $Docs->{TOTAL_SUM});
      $Docs->{FROM_DATE_LIT} = '';
      $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];
        $i++;
      }

      docs_print('act', { %$Docs, %$Company, SUFIX => ($Company->{VAT} > 0) ? 'vat' : '' });
    }
    else {

      $html->message('err', $_ERROR, "[$Docs->{errno}] $err_strs{$Docs->{errno}}");
    }
    return 0;
  }
  elsif ($FORM{change}) {
    $Docs->act_change({%FORM});

    if (!$Docs->{errno}) {
      $html->message('info', "$_INFO", "$_CHANGED N: [$FORM{DOC_ID}]");
    }
  }
  elsif ($FORM{chg}) {
    $Docs->act_info($FORM{chg});

    if (!$Docs->{errno}) {
      $html->message('info', "$_INFO", "$_CHANGING N: [$FORM{chg}]");
    }
  }
  elsif ($FORM{del} && $FORM{is_js_confirmed}) {
    $Docs->act_del($FORM{del});
    if (!$Docs->{errno}) {
      $html->message('info', "$_INFO", "$_DELETED N: [$FORM{del}]");
    }
  }

  if (!$user->{UID}) {
    $Docs->{FORM_INVOICE_ID} = "<tr><td>$_TAX_INVOICE $_NUM:</td><td><input type='text' name='INVOICE_ID' value='%INVOICE_ID%'></td></tr>\n";
  }

  #$Docs->{COMPANY_VAT}=$user->{COMPANY_VAT}.' %';

  $users->pi({ UID => $users->{UID} });
  $Docs->{CUSTOMER} = $users->{FIO} || '-';
  $Docs->{CAPTION}  = $_INVOICE;
  $Docs->{OP_SID}   = mk_unique_value(16);

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

  $FORM{MONTH} = "$Y-$M" if (!$FORM{MONTH});
  my $table = $html->table(
    {
      width    => '100%',
      caption  => "$_ACTS",
      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-$D"),
          $html->form_input('show', $_CREATE, { TYPE => "SUBMIT" })
        ]
      ],
    }
  );

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

  docs_acts_list($attr);
}

#**********************************************************
#
#**********************************************************
sub docs_tax_exports {

  if ($FORM{d}) {
    my ($y, $m, $d) = split(/-/, ($FORM{MONTH}) ? $FORM{MONTH} . '-01' : $DATE);
    my $file_prefix = "26590025388695J120150510000000011";
    my $filename    = $file_prefix . "$m$y" . '2659.xml';

    my ($fill_y, $fill_m, $fill_d) = split(/-/, ($FORM{DATE}) ? $FORM{DATE} : $DATE);
    $Docs->{FILL_DATE} = "$fill_d$fill_m$fill_y";

    $Docs->{YEAR}  = $y;
    $Docs->{MONTH} = $m;

    my $doc = '';

    $LIST_PARAMS{PAGE_ROWS} = 1000000;
    $LIST_PARAMS{MONTH} = $FORM{MONTH} || "$y-$m";

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

    my $num = 1;
    foreach my $line (@$list) {
      push @{ $invoice_hash{T1RXXXXG1} },  $num;
      push @{ $invoice_hash{T1RXXXXG2D} }, $line->[1];
      push @{ $invoice_hash{T1RXXXXG3S} }, $line->[2];
      push @{ $invoice_hash{T1RXXXXG4S} }, 'ПН02';
      push @{ $invoice_hash{T1RXXXXG5S} }, $line->[3];

      push @{ $invoice_hash{T1RXXXXG6} }, '';                            # | $line->[3];
      push @{ $invoice_hash{T1RXXXXG7} }, sprintf("%.2f", $line->[5]);
      push @{ $invoice_hash{T1RXXXXG8} }, sprintf("%.2f", $line->[6]);
      push @{ $invoice_hash{T1RXXXXG9} }, sprintf("%.2f", $line->[7]);

      push @{ $invoice_hash{T1RXXXXG10} }, '';
      push @{ $invoice_hash{T1RXXXXG11} }, '';
      push @{ $invoice_hash{T1RXXXXG12} }, '';
      $invoice_hash{R011G7} += $line->[5];
      $invoice_hash{R011G8} += $line->[6];
      $invoice_hash{R011G9} += $line->[7];
      $num++;
    }

    my @arr = ('T1RXXXXG1', 'T1RXXXXG2D', 'T1RXXXXG3S', 'T1RXXXXG4S', 'T1RXXXXG5S', 'T1RXXXXG7', 'T1RXXXXG8', 'T1RXXXXG9', 'T1RXXXXG13', 'T1RXXXXG14', 'T1RXXXXG15', 'T1RXXXXG16',);

    foreach my $key (@arr) {
      my $i = 1;
      foreach my $value (@{ $invoice_hash{$key} }) {
        $doc .= ($value) ? "<$key ROWNUM=\"$i\">$value</$key>\n" : "<$key xsi:nil=\"true\" ROWNUM=\"$i\" />\n";
        $i++;
      }
    }

    $doc .= "
<R011G7>" . sprintf("%.2f", $invoice_hash{R011G7}) . "</R011G7>
<R011G8>" . sprintf("%.2f", $invoice_hash{R011G8}) . "</R011G8>
<R011G9>" . sprintf("%.2f", $invoice_hash{R011G9}) . "</R011G9>
<R011G12 xsi:nil=\"true\" />
<R012G7 xsi:nil=\"true\" />
<R012G8 xsi:nil=\"true\" />
<R012G9 xsi:nil=\"true\" />
<R012G10 xsi:nil=\"true\" />
<R012G11 xsi:nil=\"true\" />
<R012G12 xsi:nil=\"true\" />
<R021G8 xsi:nil=\"true\" />
<R021G9 xsi:nil=\"true\" />
<R021G10 xsi:nil=\"true\" />
<R021G11 xsi:nil=\"true\" />
<R021G12 xsi:nil=\"true\" />
<R021G13 xsi:nil=\"true\" />
<R021G14 xsi:nil=\"true\" />
<R021G15 xsi:nil=\"true\" />
<R021G16 xsi:nil=\"true\" />
<R022G8 xsi:nil=\"true\" />
<R022G9 xsi:nil=\"true\" />
<R022G10 xsi:nil=\"true\" />
<R022G11 xsi:nil=\"true\" />
<R022G12 xsi:nil=\"true\" />
<R022G13 xsi:nil=\"true\" />
<R022G14 xsi:nil=\"true\" />
<R022G15 xsi:nil=\"true\" />
<R022G16 xsi:nil=\"true\" />
<HFILL>$fill_d$m$y</HFILL>
";

    $doc = $html->tpl_show(_include('docs_tax_export', 'Docs'), { %$Docs, BODY => $doc }, { OUTPUT2RETURN => 1, SKIP_QUOTE => 1 });
    $doc = convert($doc, { utf82win => 1 });

    my $filesize = length($doc);
    print "Content-Type: text/plain; filename=\"$filename\"\n" . "Content-Disposition: attachment; filename=\"$filename\" size=$filesize;" . "\n\n";

    print $doc;

    return 0;
  }

  my @MONTH_ARR = ('');
  my ($Y, $M, $D) = split(/-/, $DATE, 3);
  for (my $year = 2009 ; $year <= $Y ; $year++) {
    for (my $i = 1 ; $i < 13 ; $i++) {
      my $m = sprintf("%.2d", $i);
      push @MONTH_ARR, "$year-$m";

    }
  }

  $FORM{MONTH} = "$Y-$M" if (!$FORM{MONTH});

  my $table = $html->table(
    {
      width    => '500',
      caption  => "$_EXPORT - $_TAX_INVOICE",
      rowcolor => $_COLORS[0],
      rows     => [
        [
          "$_PERIOD: ",
          $html->form_select(
            'MONTH',
            {
              SELECTED  => $FORM{MONTH},
              SEL_ARRAY => \@MONTH_ARR,
              NO_ID     => 1
            }
          ),
          "$_CREATED: ",
          $html->date_fld2('DATE', { MONTHES => \@MONTHES, FORM_NAME => 'TAX_EXPORT', WEEK_DAYS => \@WEEKDAYS, DATE => "$Y-$M-01" }),
          $html->form_input('show', $_CREATE, { TYPE => "SUBMIT" })
        ]
      ],
    }
  );

  print $html->form_main(
    {
      CONTENT => $table->show({ OUTPUT2RETURN => 1 }),
      HIDDEN  => {
        qindex => $index,
        subf   => $FORM{subf},
        create => "1",
        d      => 1
      },
      NAME => 'TAX_EXPORT'
    }
  );
}

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

  #Add invoice number
  if ($FORM{INVOICE_ID}) {
    $Docs->invoice_change(
      {
        ID         => $FORM{INVOICE_ID},
        UID        => $FORM{UID},
        PAYMENT_ID => $attr->{PAYMENT_ID}
      }
    );

    if ($Docs->{errno}) {
      if ($Docs->{errno} == 2) {
        $html->message('err', "$_INVOICE $_ERROR", "$_INVOICE $_NOT_EXIST ID: $FORM{INVOICE_ID}");
      }
      else {
        $html->message('err', "$_INVOICE $_ERROR", "[$Docs->{errno}] $err_strs{$Docs->{errno}}");
      }
    }
    elsif (!$conf{PAYMENTS_NOT_CHECK_INVOICE_SUM} && $Docs->{TOTAL_SUM} != $FORM{SUM}) {
      $html->message('err', $_ERROR, "$_INVOICE $_SUM: $Docs->{TOTAL_SUM} \n$_PAYMENTS $_SUM: $FORM{SUM}");
    }
  }

  if ($FORM{CREATE_RECEIPT}) {
    docs_receipt_add(
      {
        DATE       => $DATE,
        CUSTOMER   => '-',
        PHONE      => '',
        UID        => $FORM{UID},
        ORDER      => "$FORM{DESCRIBE}" || '-',
        SUM        => $FORM{SUM},
        create     => 1,
        PAYMENT_ID => $attr->{PAYMENT_ID},
        SEND_EMAIL => $FORM{SEND_EMAIL}
      }
    );
  }

}

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

  $Docs->{INVOICE_PERIOD_SEL} = $html->form_select(
    'INVOICING_PERIOD',
    {
      SELECTED => $Docs->{INVOICING_PERIOD},
      SEL_HASH => {
      	'' => $_ALL,
        1  => "1 $_MONTH",
        3  => "3 $_MONTH",
        6  => "6 $_MONTH",
        12 => "12 $_MONTH",
      },
      NO_ID        => 1,
      SORT_KEY_NUM => 1
    }
  );

  $Docs->{PERIODIC_CREATE_DOCS}='checked' if ($FORM{PERIODIC_CREATE_DOCS});
  $Docs->{SEND_DOCS}='checked' if ($FORM{SEND_DOCS});
  $Docs->{PERSONAL_DELIVERY}='checked' if ($FORM{PERSONAL_DELIVERY});

  $Docs->{INVOICE_DATE}         = $html->date_fld2(
    'INVOICE_DATE',
    {
      MONTHES   => \@MONTHES,
      FORM_NAME => 'form_search',
      WEEK_DAYS => \@WEEKDAYS,
      NO_DEFAULT_DATE => 1,
    }
  );

  $Docs->{PRE_INVOICE_DATE}         = $html->date_fld2(
    'PRE_INVOICE_DATE',
    {
      MONTHES   => \@MONTHES,
      FORM_NAME => 'form_search',
      WEEK_DAYS => \@WEEKDAYS,
      NO_DEFAULT_DATE => 1,
    }
  );


  form_search({ SEARCH_FORM  => $html->tpl_show(_include('docs_users_search', 'Docs'), 
  	  { %FORM, %$Docs }, 
  	  { OUTPUT2RETURN => 1 }),
  	  ADDRESS_FORM => 1 
  	});


  my $list  = $Docs->user_list({%LIST_PARAMS, COLS_NAME => 1 });
  my $table = $html->table(
    {
      width      => '100%',
      caption    => "$_USERS",
      border     => 1,
      title      => [ "$_LOGIN", "$_FIO", "$_DEPOSIT", "$_CREDIT", "$_STATUS", "$_LAST_INVOICE_DATE", "$_NEXT_INVOICE_DATE", "$_PERIOD ($_MONTH)" ],
      cols_align => [ 'left', 'left', 'right', 'left', 'right', 'left' ],
      pages      => $Docs->{TOTAL},
      ID         => 'DOCS_USERS',
    }
  );

  foreach my $line (@$list) {
    $table->addrow($html->button($line->{login}, "index=15&UID=$line->{uid}"), 
      "$line->{fio}", 
      "$line->{deposit}", 
      "$line->{credit}", 
      "$line->{login_status}", 
      "$line->{invoice_date}", 
      "$line->{pre_invoice_date}", 
      "$line->{invoicing_period}"
      );
  }

  print $table->show();

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

  print $table->show();

}

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

  if ($FORM{add}) {
    if (!$permissions{0}{1}) {
      $html->message('err', $_ERROR, "$ERR_ACCESS_DENY");
      return 0;
    }

    $Docs->user_add({%FORM});
    if (!$Docs->{errno}) {
      $html->message('info', $_INFO, "$_ADDED");
      if ($attr->{REGISTRATION}) {
        return 0;
      }
    }
  }
  elsif ($FORM{change}) {
    if (!$permissions{0}{4}) {
      $html->message('err', $_ERROR, "$ERR_ACCESS_DENY");
      return 0;
    }

    $Docs->user_change({%FORM});

    if (!$Docs->{errno}) {
      $html->message('info', $_INFO, "$_CHANGED");
      if ($attr->{REGISTRATION}) {
        return 0;
      }
    }
  }
  elsif ($FORM{del}) {
    $Docs->user_del();
    if (!$Docs->{errno}) {
      $html->message('info', $_INFO, "$_DELETED");
    }
  }

  if ($Docs->{errno}) {
    $html->message('err', "Docs:$_ERROR", "[$Docs->{errno}] $err_strs{$Docs->{errno}}");
    if ($attr->{REGISTRATION}) {
      return 1;
    }
  }

  $Docs->user_info($FORM{UID});

  if ($Docs->{TOTAL} < 1) {
    if ($attr->{ACTION}) {
      $Docs->{ACTION}     = 'add';
      $Docs->{LNG_ACTION} = $attr->{LNG_ACTION};
    }
    else {
      $html->message('info', $_INFO, $_NOT_ACTIVE);
      $Docs->{ACTION}     = 'add';
      $Docs->{LNG_ACTION} = $_ACTIVATE;
    }
  }
  else {
    if ($attr->{ACTION}) {
      $Docs->{ACTION}     = 'change';
      $Docs->{LNG_ACTION} = $attr->{LNG_ACTION};
    }
    else {
      $Docs->{ACTION}     = 'change';
      $Docs->{LNG_ACTION} = $_CHANGE;
    }
  }

  $Docs->{INVOICE_PERIOD_SEL} = $html->form_select(
    'INVOICING_PERIOD',
    {
      SELECTED => $Docs->{INVOICING_PERIOD},
      SEL_HASH => {
        1  => "1 $_MONTH",
        3  => "3 $_MONTH",
        6  => "6 $_MONTH",
        12 => "12 $_MONTH",
      },
      NO_ID        => 1,
      SORT_KEY_NUM => 1
    }
  );

  $Docs->{PERIODIC_CREATE_DOCS} = ($Docs->{PERIODIC_CREATE_DOCS}) ? 'checked' : '';
  $Docs->{SEND_DOCS}            = ($Docs->{SEND_DOCS})            ? 'checked' : '';
  $Docs->{PERSONAL_DELIVERY}    = ($Docs->{PERSONAL_DELIVERY})    ? 'checked' : '';
  $Docs->{INVOICE_DATE}         = $html->date_fld2(
    'INVOICE_DATE',
    {
      MONTHES   => \@MONTHES,
      FORM_NAME => 'receipt_add',
      WEEK_DAYS => \@WEEKDAYS,
      DATE      => $Docs->{INVOICE_DATE} || $users->{ACTIVATE}
    }
  );

  $html->tpl_show(_include('docs_user', 'Docs'), { %$attr, %$Docs }, {});
  return 0;
}

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

  if ($FORM{INVOICE_ID} && $FORM{INVOICE_ID} eq 'create') {
    $FORM{INVOICE_ID} = undef;
    $LIST_PARAMS{UID} = $FORM{UID};
    $FORM{create}     = 1;
    $FORM{CUSTOMER}   = '-';
    $FORM{ORDER}      = $FORM{DESCRIBE};
    docs_invoice();
  }
  elsif ($FORM{INVOICE_ID}) {
    $Docs->invoice_info($FORM{INVOICE_ID});
    if ($Docs->{TOTAL} == 0) {
      $FORM{INVOICE_SUM} = 0;
    }
    else {
      $FORM{INVOICE_SUM} = $Docs->{TOTAL_SUM};
    }
  }

  return 1;
}

1
