public InvoiceType AddLine(Action<InvoiceLineType> line)
        {
            if (this.Items == null)
                this.Items = new InvoiceLineType[] { };

            if (this.TaxesOutputs == null)
                this.TaxesOutputs = new TaxOutputType[] { };

            if (this.InvoiceTotals == null)
                this.InvoiceTotals = new InvoiceTotalsType();

            var invoiceLine = new InvoiceLineType();
            line.Invoke(invoiceLine);

            this.Items = this.Items.Concat(new InvoiceLineType[] { invoiceLine }).ToArray();

            foreach (var tax in invoiceLine.TaxesOutputs)
            {
                var taxOutput = this.TaxesOutputs.Where(to => to.TaxTypeCode == tax.TaxTypeCode).SingleOrDefault();
                if (taxOutput == null)
                {
                    taxOutput = new TaxOutputType() 
                        { 
                            TaxTypeCode = tax.TaxTypeCode, 
                            TaxRate = tax.TaxRate, 
                            TaxableBase = new AmountType() { TotalAmount = new DoubleTwoDecimalType() },
                            TaxAmount = new AmountType() { TotalAmount = new DoubleTwoDecimalType() } 
                        };
                    this.TaxesOutputs = this.TaxesOutputs.Concat(new TaxOutputType[] { taxOutput }).ToArray();
                }

                taxOutput.TaxableBase.TotalAmount.Value += tax.TaxableBase.TotalAmount.Value;
                taxOutput.TaxAmount.TotalAmount.Value += tax.TaxAmount.TotalAmount.Value;
            }

            this.InvoiceTotals.TotalTaxOutputs.Value = this.TaxesOutputs.Sum(to => to.TaxAmount.TotalAmount.Value);
            this.InvoiceTotals.InvoiceTotal.Value = this.Items.Sum(i => i.GrossAmount.Value) + this.InvoiceTotals.TotalTaxOutputs.Value;
            this.InvoiceTotals.TotalGeneralDiscounts.Value = this.Items.Where(i => i.DiscountsAndRebates != null).SelectMany(i => i.DiscountsAndRebates).Sum(i => i.DiscountAmount.Value);
            this.InvoiceTotals.TotalGeneralSurcharges.Value = this.Items.Where(i => i.Charges != null).SelectMany(i => i.Charges).Sum(d => d.ChargeAmount.Value);
            this.InvoiceTotals.TotalGrossAmount.Value = this.Items.Sum(i => i.GrossAmount.Value);
            this.InvoiceTotals.TotalGrossAmountBeforeTaxes.Value = this.InvoiceTotals.TotalGrossAmount.Value;
            this.InvoiceTotals.TotalOutstandingAmount.Value = this.InvoiceTotals.InvoiceTotal.Value;
            this.InvoiceTotals.TotalExecutableAmount.Value = this.InvoiceTotals.InvoiceTotal.Value;

            return this;
        }
Exemple #2
0
        public InvoiceType AddLine(Action <InvoiceLineType> line)
        {
            if (this.Items == null)
            {
                this.Items = new InvoiceLineType[] { }
            }
            ;

            if (this.TaxesOutputs == null)
            {
                this.TaxesOutputs = new TaxOutputType[] { }
            }
            ;

            if (this.InvoiceTotals == null)
            {
                this.InvoiceTotals = new InvoiceTotalsType();
            }

            var invoiceLine = new InvoiceLineType();

            line.Invoke(invoiceLine);

            this.Items = this.Items.Concat(new InvoiceLineType[] { invoiceLine }).ToArray();

            foreach (var tax in invoiceLine.TaxesOutputs)
            {
                var taxOutput = this.TaxesOutputs.Where(to => to.TaxTypeCode == tax.TaxTypeCode).SingleOrDefault();
                if (taxOutput == null)
                {
                    taxOutput = new TaxOutputType()
                    {
                        TaxTypeCode = tax.TaxTypeCode,
                        TaxRate     = tax.TaxRate,
                        TaxableBase = new AmountType()
                        {
                            TotalAmount = new DoubleTwoDecimalType()
                        },
                        TaxAmount = new AmountType()
                        {
                            TotalAmount = new DoubleTwoDecimalType()
                        }
                    };
                    this.TaxesOutputs = this.TaxesOutputs.Concat(new TaxOutputType[] { taxOutput }).ToArray();
                }

                taxOutput.TaxableBase.TotalAmount.Value += tax.TaxableBase.TotalAmount.Value;
                taxOutput.TaxAmount.TotalAmount.Value   += tax.TaxAmount.TotalAmount.Value;
            }

            this.InvoiceTotals.TotalTaxOutputs.Value             = this.TaxesOutputs.Sum(to => to.TaxAmount.TotalAmount.Value);
            this.InvoiceTotals.InvoiceTotal.Value                = this.Items.Sum(i => i.GrossAmount.Value) + this.InvoiceTotals.TotalTaxOutputs.Value;
            this.InvoiceTotals.TotalGeneralDiscounts.Value       = this.Items.Where(i => i.DiscountsAndRebates != null).SelectMany(i => i.DiscountsAndRebates).Sum(i => i.DiscountAmount.Value);
            this.InvoiceTotals.TotalGeneralSurcharges.Value      = this.Items.Where(i => i.Charges != null).SelectMany(i => i.Charges).Sum(d => d.ChargeAmount.Value);
            this.InvoiceTotals.TotalGrossAmount.Value            = this.Items.Sum(i => i.GrossAmount.Value);
            this.InvoiceTotals.TotalGrossAmountBeforeTaxes.Value = this.InvoiceTotals.TotalGrossAmount.Value;
            this.InvoiceTotals.TotalOutstandingAmount.Value      = this.InvoiceTotals.InvoiceTotal.Value;
            this.InvoiceTotals.TotalExecutableAmount.Value       = this.InvoiceTotals.InvoiceTotal.Value;

            return(this);
        }
    }
}