#/usr/bin/perl
# Docs functions


require "Docs.pm";
Docs->import();
my $Docs = Docs->new($db, $admin, \%conf);

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

 if ($attr->{CREATE}) {
   if (defined($FORM{OP_SID}) and $FORM{OP_SID} eq $COOKIES{OP_SID}) {
 	   $html->message('err', $_ERROR, "$_INVOICE $_EXIST");
 	   return 0;
    }
   elsif(! $attr->{CUSTOMER}) {
     $html->message('err', "$_ERROR", "$_ORG_NAME");
    }
   elsif($FORM{PREVIEW}) {
     docs_preview('invoice', { %FORM });
     return 0;
    }
   else {
     
     $Docs->{FROM} = $attr->{FROM} if ($attr->{FROM});
     $Docs->docs_invoice_add({ %$attr });
     if(! $Docs->{errno}) {
       $html->message('info', "$_ADDED", "$_NUM: [$Docs->{DOC_ID}] $_DATE: $FORM{DATE}");
       $Docs->docs_invoice_info($Docs->{DOC_ID});
      }
     elsif ($Docs->{errno}) {
       $html->message('err', $_ERROR, "[$Docs->{errno}] $err_strs{$Docs->{errno}} $Docs->{errstr}");	
       return 0;
      }
    }
  }

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

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

  docs_print('invoice', $Docs);
	#$html->tpl_show(_include('docs_invoice', 'Docs'), $Docs);
}

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


if ($LIST_PARAMS{UID}) {
	docs_account();
	return 0 if ($FORM{'print'});
 }
elsif($FORM{del} ) {
  $Docs->docs_invoice_del($FORM{del});
  if(! $Docs->{errno}) {
    $html->message('info', "$_INFO", "$_DELETED N: [$FORM{del}]");
   }
  elsif ($Docs->{errno}) {
    $html->message('err', $_ERROR, "[$Docs->{errno}] $err_strs{$Docs->{errno}}");	
    return 0;
   }
 }
elsif($FORM{print}) {
  $Docs->docs_invoice_info($FORM{print});
  
  $Docs->{TOTAL_SUM}=0.00;
  if ($Docs->{ORDERS}) {
		my $i = 1;
		my @ORDERS = @{ $Docs->{ORDERS} };
		$Docs->{ORDER}='';
		foreach my $line (@ORDERS) {
			my $sum = sprintf("%.2f", $line->[3] * $line->[4]);
		  $Docs->{ORDER}.="<tr><th align='right'>$i</th><td align='left'>$line->[1]</td><td align='center'>$units[$line->[2]]</td>
		  <td align='right'>$line->[3]</td><td align='right'>$line->[4]</td><td align='right'>$sum</td></tr>";
		  $i++;
		  $Docs->{TOTAL_SUM}+=$sum;
		 }
	 }

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

  
  docs_print('invoice', $Docs);
  return 0;
 }
else {
  form_search({ SEARCH_FORM => $html->tpl_show(_include('docs_search', 'Docs'), 
  	                                  { %info, %FORM }, 
  	                                  { notprint => 1 })  
	            });
}


#  Date  Customer  Sum  User  Administrators  Time  

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

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


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

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

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


print $table->show();
}

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



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


#  Date  Customer  Sum  User  Administrators  Time  

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

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


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

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

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


print $table->show();

return 0;
}

#**********************************************************
# docs_account_add
#**********************************************************
sub docs_account {
 $Docs->account_defaults();		
 $Docs->{DATE}=$DATE;
	
	
 if ($FORM{create}) {
   if (defined($FORM{OP_SID}) and $FORM{OP_SID} eq $COOKIES{OP_SID}) {
 	   $html->message('err', $_ERROR, "$_EXIST");
    }
   elsif ($FORM{SUM} < 0.01) {
     $html->message('err', "$_ERROR", $_WRONG_SUM);
    }
   elsif(! $LIST_PARAMS{UID}) {
     $html->message('err', "$_ERROR", "$_SELECT_USER");
    }
   elsif(! $FORM{CUSTOMER}) {
     $html->message('err', "$_ERROR", "$_ORG_NAME");
    }
   elsif($FORM{PREVIEW}) {
     docs_preview('account', { %FORM });
     return 0;
    }
   else {
     $FORM{UID}=$LIST_PARAMS{UID} if (! $FORM{UID});
     $Docs->account_add({ %FORM });
     if(! $Docs->{errno}) {
       $html->message('info', "$_ADDED", "$_NUM: [$Docs->{ACCT_ID}] $_DATE: $FORM{DATE}");
       $Docs->account_info($Docs->{ACCT_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]));
        }
     
       docs_print('account', $Docs);
       $FORM{'print'}=1;
       return 0;
      }
    }
  }
 elsif(defined($FORM{print})){
   $Docs->account_info($FORM{print});
   $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->[3]*$line->[4]));
    }

   docs_print('account', $Docs);
   return 0;
  }
 elsif(defined($FORM{change})) {
   $Docs->account_change({ %FORM });
   if(! $Docs->{errno}) {
     $html->message('info', "$_INFO", "$_CHANGED N: [$FORM{DOC_ID}]");
    }
  }
 elsif(defined($FORM{chg})) {
   $Docs->account_info($FORM{chg});
   if(! $Docs->{errno}) {
     $html->message('info', "$_INFO", "$_CHANGING N: [$FORM{chg}]");
    }
  }
 elsif(defined($FORM{del})) {
   $Docs->account_del($FORM{del});
   if(! $Docs->{errno}) {
     $html->message('info', "$_INFO", "$_DELETED N: [$FORM{del}]");
    }
  }
	
	if(! $user->{UID}  ) {
  	$Docs->{FORM_ACCT_ID} = "<tr><td>N:</td><td><input type='text' name='ACCT_ID' value='%ACCT_ID%'></td></tr>\n";
   }

  $Docs->{SEL_ORDER} .= $html->form_select('ORDERS', 
                                    { 
 	                                   SELECTED    => $FORM{ORDERS},
 	                                   SEL_ARRAY   => ['Internet service'],
                                     NO_ID       => 1
 	                                  });

  #$Docs->{COMPANY_VAT}=$user->{COMPANY_VAT}.' %';
  $Docs->{OP_SID} = mk_unique_value(16);
  $html->tpl_show(_include('docs_account_add', 'Docs'), $Docs);
}


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


#**********************************************************
# docs_print
#**********************************************************
sub docs_print {
	my ($type, $ATTR)=@_;
	
	if (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->{SUM_LIT} = int2ml("$ATTR->{SUM}", { 
  	 ONES             => \@ones,
     TWOS             => \@twos,
     FIFTH            => \@fifth,
     ONE              => \@one,
     ONEST            => \@onest,
     TEN              => \@ten,
     TENS             => \@tens,
     HUNDRED          => \@hundred,
     MONEY_UNIT_NAMES => \@money_unit_names
  	  });
	
	my $template = $type;
	$html->tpl_show(_include("docs_$template", 'Docs'), $ATTR);
}



1

