使用变量计算发票上的税款

使用变量计算发票上的税款

我定制了一个类来获取税值以及发票中的总金额。这是我需要修改的部分:

{
\hline\hline\hline
{Valor venta} & & & {S/. \total{cost}} \\
{IGV (\taxval \%)} & & & {S/. \taxes{\money}{\taxval}} \\
{Importe total} & & & {S/. \addtaxes{\money}{\taxval}} \\
\end{longtable}
}
\def\money{600}

如您所见,“money”是硬编码的。我尝试用 \total{cost} 替换“\money”,但通常会出错。

知道如何改变这种状况吗?

这是我的文件:

\documentclass{invoice-spanish} % Use the custom invoice class (invoice.cls)
\def \tab {\hspace*{3ex}} % Define \tab to create some horizontal white space

\usepackage[ngerman,english,spanish]{babel} 
\decimalpoint
\usepackage[latin1,utf8]{inputenc}
\usepackage{microtype}
\usepackage[pdftex]{graphicx}    
\usepackage{hyperref} 
\usepackage{eurosym}
\usepackage{datetime}
\usepackage{pgf,pgfmath}
 \usepackage{pgfplots}

%Taxes
\pgfkeys{/pgf/fpu}
\pgfkeys{/pgf/number format/.cd,
fixed,
fixed zerofill,
precision=2,
set thousands separator={,},
set decimal separator={.}
}
\newcommand{\taxes}[2]{%
\pgfmathparse{#1*#2/100}%
\pgfmathprintnumber{\pgfmathresult}%
}
\newcommand{\addtaxes}[2]{%
\pgfmathparse{#1+#1*#2/100}%
\pgfmathprintnumber{\pgfmathresult}%
}
\def\taxval{18}

\begin{document}
\begin{invoiceTable}
\feetype{Mantenimiento website y soporte de sistemas} % Fee category description
\hourrow{Configuración de cuentas de correo}{10}{60}
\end{invoiceTable}

\end{document}

完整课程在这里:

\ProvidesClass{invoice}

\LoadClass[12pt]{article}

\usepackage[letterpaper,hmargin=0.79in,vmargin=0.79in]{geometry}
\usepackage[parfill]{parskip} % Do not indent paragraphs
\usepackage{fp} % Fixed-point arithmetic
\usepackage{calc} % Counters for totaling hours and cost
\usepackage{longtable}

\pagestyle{empty} % No page numbers
\linespread{1.5} % Line spacing

\setlength{\doublerulesep}{\arrayrulewidth} % Double rules look like one thick one

% Command for setting a default hourly rate
\newcommand{\feetype}[1]{
\textbf{#1}
\\
}

% Counters for totaling up hours and dollars
\newcounter{hours} \newcounter{subhours} \newcounter{cost} \newcounter{subcost}
\setcounter{hours}{0} \setcounter{subhours}{0}
\setcounter{cost}{0} \setcounter{subcost}{0}

% Formats inputed number with 2 digits after the decimal place
\newcommand*{\formatNumber}[1]{\FPround{\cost}{#1}{2}\cost} %

% Returns the total of counter
\newcommand*{\total}[1]{\FPdiv{\t}{\arabic{#1}}{1000}\formatNumber{\t}}

% Create an invoice table
\newenvironment{invoiceTable}{
% Create a new row from title, unit quantity, unit rate, and unit name
\newcommand*{\unitrow}[4]{%
     \addtocounter{cost}{1000 * \real{##2} * \real{##3}}%
     \addtocounter{subcost}{1000 * \real{##2} * \real{##3}}%
     ##1 & \formatNumber{##2} ##4 & S/. \formatNumber{##3} & S/. \FPmul{\cost}{##2}{##3}\formatNumber{\cost}%
     \\
}
% Create a new row from title and expense amount
\newcommand*{\feerow}[2]{%
     \addtocounter{cost}{1000 * \real{##2}}%
     \addtocounter{subcost}{1000 * \real{##2}}%
     ##1 & & S/. \formatNumber{##2} & S/. \FPmul{\cost}{##2}{1} \formatNumber{\cost}%
     \\
}

\newcommand{\subtotalNoStar}{
    {\bf Subtotal} & {\bf \total{subhours} hours} &  & {\bf S/.  \total{subcost}}
    \setcounter{subcost}{0}
    \setcounter{subhours}{0}
    \\*[1.5ex]
}
\newcommand{\subtotalStar}{
    {\bf Subtotal} & & & {\bf S/.  \total{subcost}}
    \setcounter{subcost}{0}
    \\*[1.5ex]
}
\newcommand{\subtotal}{
     \hline
     \@ifstar
     \subtotalStar%
     \subtotalNoStar%
}

% Create a new row from date and hours worked (use stored fee type and hourly rate)
\newcommand*{\hourrow}[3]{%
    \addtocounter{hours}{1000 * \real{##2}}%
    \addtocounter{subhours}{1000 * \real{##2}}%
    \unitrow{##1}{##2}{##3}{horas}%
}
\renewcommand{\tabcolsep}{0.8ex}
\setlength\LTleft{0pt}
\setlength\LTright{0pt}
\begin{longtable}{@{\extracolsep{\fill}\hspace{\tabcolsep}} l r r r }
\hline
{\bf Detalle de servicio} & \multicolumn{1}{c}{\bf Horas} & \multicolumn{1}{c}{\bf Costo} & \multicolumn{1}{c}{\bf Total} \\*
\hline\hline
\endhead
}{
\hline\hline\hline
{Valor venta} & & & {S/. \total{cost}} \\
{IGV (\taxval \%)} & & & {S/. \taxes{\money}{\taxval}} \\
{Importe total} & & & {S/. \addtaxes{\money}{\taxval}} \\
\end{longtable}
}
\def\money{600}

答案1

问题是,对于和命令来说,\total{cost}没有任何意义,这就是它们给出错误的原因。相反,如果您使用,那么一切都很好。也就是说,如果您向它们传递数字,而不是宏,它就会起作用——尽管格式并不像您希望的那样。\taxes\addtaxes\taxes{\arabic{cost}}

话虽如此,你的类文件需要一点重构。以下是我要更改的三件事:

  • \newcommand环境定义中存在大量s invoiceTable,在我看来这是一种不好的做法。

  • 您的类文件使用命令\taxval\taxes并且\addtaxes所有命令均在主 tex 文件中定义。

  • 主 tex 文件使用数学用于计算,而类文件使用FP。为了保持一致性,您应该只使用其中一种,而不是混合搭配。

fp我会用自己的命令替换所有命令pgfmath,但这可能只是因为我喜欢pgfmath(虽然fp似乎不再受支持,自 1999 年以来就没有改变过)

以下是解决上述一些评论的一小步。可能最重要的是它产生了您想要的输出:

在此处输入图片描述

以下是我重写主文件的方法:

\documentclass{invoice-spanish} % Use the custom invoice class (invoice.cls)
\def \tab {\hspace*{3ex}} % Define \tab to create some horizontal white space

\usepackage[ngerman,english,spanish]{babel}
\decimalpoint
\usepackage[latin1,utf8]{inputenc}
\usepackage{microtype}
%\usepackage[pdftex]{graphicx}
%\usepackage{hyperref}
%\usepackage{eurosym}
%\usepackage{datetime}
%\usepackage{pgfplots}

\begin{document}
\begin{invoiceTable}
\feetype{Mantenimiento website y soporte de sistemas} % Fee category description
\typeout{starting}
\hourrow{Configuración de cuentas de correo}{10}{60}
\end{invoiceTable}

\end{document}

我注释掉了一些包,因为 MWE 不需要它们。将它们重新添加进去可能需要小心一些,因为似乎存在冲突。

最后,这是修改后的类文件:

\ProvidesClass{invoice}% Should be invoice-spanish??

\LoadClass[12pt]{article}

\usepackage[letterpaper,hmargin=0.79in,vmargin=0.79in]{geometry}
\usepackage[parfill]{parskip} % Do not indent paragraphs
\usepackage{fp} % Fixed-point arithmetic
\usepackage{pgf,pgfmath}
\usepgflibrary{fpu}
\usepackage{calc} % Counters for totaling hours and cost
\usepackage{longtable}

\pagestyle{empty} % No page numbers
\linespread{1.5} % Line spacing

\setlength{\doublerulesep}{\arrayrulewidth} % Double rules look like one thick one

% Command for setting a default hourly rate -- Q Why not just use \textbf?
\newcommand{\feetype}[1]{\textbf{#1}\\}

%Taxes
\pgfkeys{/pgf/fpu=true}
\pgfkeys{/pgf/number format/.cd,
  fixed,
  fixed zerofill,
  precision=2,
  set thousands separator={,},
  set decimal separator={.}
}
\newcommand{\taxes}[2]{%
  \pgfmathparse{#1*#2/100000}%
  \pgfmathprintnumber{\pgfmathresult}%
}
\newcommand{\addtaxes}[2]{%
  \pgfmathparse{#1/1000+#1*#2/100000}%
  \pgfmathprintnumber{\pgfmathresult}%
}
\def\taxval{18}

% Counters for totaling up hours and dollars
\newcounter{hours}
\newcounter{subhours}
\newcounter{cost}
\newcounter{subcost}

% Formats inputed number with 2 digits after the decimal place
\newcommand*{\formatNumber}[1]{\FPround{\cost}{#1}{2}\cost} %

% Returns the total of counter
\newcommand*{\total}[1]{\pgfmathparse{\arabic{#1}/1000}\pgfmathprintnumber{\pgfmathresult}}

% Create a new row from title, unit quantity, unit rate, and unit name
\newcommand*{\unitrow}[4]{%
     \addtocounter{cost}{1000 * \real{#2} * \real{#3}}%
     \addtocounter{subcost}{1000 * \real{#2} * \real{#3}}%
     #1 & \formatNumber{#2} #4 & S/. \formatNumber{#3} & S/. \FPmul{\cost}{#2}{#3}\formatNumber{\cost}%
     \\
}
% Create a new row from title and expense amount
\newcommand*{\feerow}[2]{%
     \addtocounter{cost}{1000 * \real{#2}}%
     \addtocounter{subcost}{1000 * \real{#2}}%
     #1 & & S/. \formatNumber{#2} & S/. \FPmul{\cost}{#2}{1} \formatNumber{\cost}%
     \\
}

\newcommand{\subtotalNoStar}{
    {\bf Subtotal} & {\bf \total{subhours} hours} &  & {\bf S/.  \total{subcost}}
    \setcounter{subcost}{0}
    \setcounter{subhours}{0}
    \\*[1.5ex]
}
\newcommand{\subtotalStar}{
    {\bf Subtotal} & & & {\bf S/.  \total{subcost}}
    \setcounter{subcost}{0}
    \\*[1.5ex]
}
\newcommand{\subtotal}{
     \hline
     \@ifstar
     \subtotalStar%
     \subtotalNoStar%
}

% Create a new row from date and hours worked (use stored fee type and hourly rate)
\newcommand*{\hourrow}[3]{%
    \addtocounter{hours}{1000 * \real{#2}}%
    \addtocounter{subhours}{1000 * \real{#2}}%
    \unitrow{#1}{#2}{#3}{horas}%
}
\renewcommand{\tabcolsep}{0.8ex}
\setlength\LTleft{0pt}
\setlength\LTright{0pt}
% Create an invoice table
\newenvironment{invoiceTable}{%
  \setcounter{hours}{0} \setcounter{subhours}{0}
  \setcounter{cost}{0} \setcounter{subcost}{0}
  \longtable{@{\extracolsep{\fill}\hspace{\tabcolsep}} l r r r }
  \hline
  {\bf Detalle de servicio} & \multicolumn{1}{c}{\bf Horas} & \multicolumn{1}{c}{\bf Costo} & \multicolumn{1}{c}{\bf Total} \\*
  \hline\hline
  \endhead
  }{
  \hline\hline\hline
  {Valor venta} & & & {S/. \total{cost}} \\
  {IGV (\taxval \%)} & & & {S/. \taxes{\arabic{cost}}{\taxval}} \\
  {Importe total} & & & {S/. \addtaxes{\arabic{cost}}{\taxval}} \\
  \endlongtable
  }

我还没有修复所有问题,评论中有几个问题。目前文件可以满足您的要求(顺便说一句,您似乎1000在几个地方乘以了,这意味着您必须在其他地方除以它……这也应该简化)。

相关内容