×

Invoice Management

Like transactions and party, an invoice number also identifies an invoice. However, there is a difference. Like party, the invoice numbers needs to be
unique within a given client but not within a fiscal year. Since there are different kinds of invoices (purchase or sales), the sales and purchase invoices
have their own number. Hence, it is possible that you will find different invoices with the same number but with a different person role (which actually
indicates the invoice type). You can use the method free invoice number to set any number, which could not be unique. Each invoice is linked to a
transaction, but there is a special case where it is not. In the first fiscal year, we store invoices without transactions if the invoice date is lower than
the start date of the fiscal year. In all other cases, the transaction must be created for the invoice. All invoice payment postings are available in
PaymentPostings property.
 
Example Code
 
Create an Invoice with a VAT Posting (VAT inclusive)
The associated VAT posting is defined as inclusive VAT
 
Create Invoice with VAT inclusive
    
        public void API_CreateInvoice_VAT_Inclusive()
        {
            /* Initial parameter definition for Invoice */
            int PartyNumber = 1019;  // Bucherer AG
            decimal paymentAmount = 10000m;

            Assert.IsNotNull(manager);

            errorcode = manager.LoadTransactions();

            /* create party object and assign party to manager object */

            IParty party = null;
            errorcode = manager.FindParty(PartyNumber, out party);

            /* create Invoice */
            IInvoice invoice = new Invoice();
            invoice.FreeInvoiceNum = "SN_1000";
            DateTime date = DateTime.Today;
            invoice.InvoiceDate = new DateTime(date.Year, date.Month, date.Day);
            invoice.PartyFID = party.ID;
            invoice.PersonRoleFID = (int)PersonRole.Debtor;
            invoice.Text = "Test Invoice";
            invoice.TotalAmount = paymentAmount;

            invoice.PayMethodFID = ((IPayMethod)party.Debtor.PayMethods[0]).ID;
            invoice.Transaction.DocDate = new DateTime(date.Year, date.Month, date.Day);

            manager.LoadAccounts();

            /* Create first posting*/
            IPosting posting1 = new Posting();

            IData paymethods = party.Debtor.PayMethods;

            /* Invoice paymentod and paymethod of first posting must always be the same */
            posting1.AccountFID = ((IPayMethod)paymethods[0]).AccountFID;
            posting1.Text = "first posting";
            posting1.FreeCode = "Free 1";
            posting1.Amount = paymentAmount;
            posting1.IsDebit = true;
            posting1.PostingTypeFID = (int)PostingType.Invoice;
            posting1.IsInclusive = true;

            invoice.Transaction.Postings.Add(posting1);

            /* create posting with VAT */
            IPosting posting2 = new Posting();

            posting2.AccountFID = manager.FindAccountID("3200"); // party.Debtor.AccountFID; //
            posting2.Text = "Posting #2 (VAT posting)";
            posting2.IsDebit = false;
            posting2.PostingTypeFID = (int)PostingType.Invoice;
            posting2.IsInclusive = true;

            IVAT vat = new VAT();
            vat = manager.FindVAT("UStn");

            /* fill amount of posting with VAT
             * insert VAT and amounts in key currency and  foreign currency
             *  - for key currency posting amounts are the same
             *  - for foreign currency amount must be precalculated based on Exchange Rate
             */

            manager.FillVATPostingAmount(posting2, vat, paymentAmount, paymentAmount);
            invoice.Transaction.Postings.Add(posting2);

            errorcode = manager.SaveInvoice(invoice);
            if (errorcode != 0)
            {
                Helper.createNewFiscalYear(manager);
                manager.LoadFiscalYears(manager.Client);
                errorcode = manager.SaveInvoice(invoice);

                state = Helper.ViewErrorMessage(errorcode, manager);
                Assert.AreEqual(true, state.passed, state.message);
            }

            manager.Close();
        }
    
  
Create Invoice with VAT inclusive
    
UNDER CONSTRUCTION
    
  
 
Create an Invoice with a VAT Posting (VAT exclusive)
The associated VAT posting is defined as exclusive VAT
 
Create Invoice with VAT exclusive
    
        public void API_CreateInvoice_VAT_Exclusive()
        {
            int PartyNumber = 1019;  // Bucherer AG
            decimal totalAmount = 10800.0M;

            errorcode = manager.LoadTransactions();
            manager.LoadInvoices();

            state = Helper.ViewErrorMessage(errorcode, manager);
            Assert.AreEqual(true, state.passed, "LoadTransaction: " + state.message);

            IParty party = null;
            errorcode = manager.FindParty(PartyNumber, out party);

            /* create Invoice */
            IInvoice invoice = new Invoice();
            invoice.FreeInvoiceNum = "free1";
            DateTime date = DateTime.Today;
            invoice.InvoiceDate = new DateTime(date.Year, date.Month, date.Day);
            invoice.PartyFID = party.ID;
            invoice.PersonRoleFID = (int)PersonRole.Debtor;
            invoice.Text = "Test Invoice";
            invoice.TotalAmount = totalAmount;

            invoice.PayMethodFID = ((IPayMethod)party.Debtor.PayMethods[0]).ID;
            invoice.Transaction.DocDate = new DateTime(date.Year, date.Month, date.Day);

            manager.LoadAccounts();

            /* Create first posting*/
            IPosting posting1 = new Posting();

            IData paymethods = party.Debtor.PayMethods;

            posting1.AccountFID = ((IPayMethod)paymethods[0]).AccountFID;
            posting1.Text = "first posting";
            posting1.FreeCode = "Free 1";
            posting1.Amount = totalAmount;
            posting1.IsDebit = true;
            posting1.PostingTypeFID = (int)PostingType.Invoice;
            posting1.IsInclusive = false;

            invoice.Transaction.Postings.Add(posting1);

            /* create posting with VAT */
            IPosting posting2 = new Posting();

            posting2.AccountFID = manager.FindAccountID("3200"); // party.Debtor.AccountFID; //
            posting2.Text = "Posting #2 (VAT posting)";
            posting2.IsDebit = false;
            posting2.PostingTypeFID = (int)PostingType.Invoice;
            posting2.IsInclusive = false;

            IVAT vat = new VAT();
            vat = manager.FindVAT("UStn");

            /* fill amount of posting with VAT
             * insert VAT and amounts in key currency and  foreign currency
             *  - for key currency posting amounts are the same
             *  - for foreign currency amount must be precalculated based on Exchange Rate
             */

            manager.FillVATPostingAmount(posting2, vat, totalAmount, totalAmount);
            invoice.Transaction.Postings.Add(posting2);

            errorcode = manager.SaveInvoice(invoice);
            if (errorcode != 0)
            {
                Helper.createNewFiscalYear(manager);
                manager.LoadFiscalYears(manager.Client);
                errorcode = manager.SaveInvoice(invoice);

                state = Helper.ViewErrorMessage(errorcode, manager);
                Assert.AreEqual(true, state.passed, state.message);
            }
            manager.Close();
        }

    
  
Create Invoice with VAT exclusive
    
UNDER CONSTRUCTION
    
  
 
Load open Invoices of a Party
Load open Invoices of a party (creditor or debtor)
 
Load open invoice of a party
    
        public void API_LoadInvoices_OpenInvoicesOfParty() {
            
            /* Initial parameter definition for Invoice */
            int PartyNumber = 1019;  // Bucherer AG

            /* create party object and assign party to manager object */
            IParty oParty = null;
            errorcode = manager.FindParty(PartyNumber, out oParty);
            state = Helper.ViewErrorMessage(errorcode, manager);
            Assert.AreEqual(true, state.passed, state.message);

            errorcode = manager.LoadInvoices();
            state = Helper.ViewErrorMessage(errorcode, manager);
            Assert.AreEqual(true, state.passed, state.message);

            IData invoices = manager.Invoices;
            List debtorInvoices = new List();
            IInvoice oInvoice = new Invoice();

            decimal creditLine = 0.0m;

            foreach (IInvoice invoice in invoices) {

                if ((invoice.PartyFID == oParty.ID) && (oParty.IsHaveDebtor) && (invoice.Balance != 0)) {
                    creditLine = oParty.Debtor.CreditLine;

                    errorcode = manager.FindInvoice(invoice.InvoiceNum, true, out oInvoice);
                    debtorInvoices.Add(oInvoice);
                }
            }

            state.passed = false;
            state.message = "Invoice Load failed";
            if (debtorInvoices.Count > 0)
            {
                state.passed = true;
                state.message = "Invoice Load successful";
            }
            Assert.AreEqual(true, state.passed, state.message);

            /* Initial parameter definition for Invoice */
            PartyNumber = 1044;  // 20 min

            /* create party object and assign party to manager object */
            oParty = null;
            errorcode = manager.FindParty(PartyNumber, out oParty);
            state = Helper.ViewErrorMessage(errorcode, manager);
            Assert.AreEqual(true, state.passed, state.message);

            errorcode = manager.LoadInvoices();
            state = Helper.ViewErrorMessage(errorcode, manager);
            Assert.AreEqual(true, state.passed, state.message);

            invoices = manager.Invoices;
            List creditorInvoices = new List();
            oInvoice = new Invoice();

            creditLine = 0.0m;

            foreach (IInvoice invoice in invoices)
            {

                if ((invoice.PartyFID == oParty.ID) && (oParty.IsHaveCreditor) && (invoice.Balance != 0))
                {
                    creditLine = oParty.Creditor.CreditLine;

                    errorcode = manager.FindInvoice(invoice.InvoiceNum, false, out oInvoice);
                    creditorInvoices.Add(oInvoice);
                    decimal amount = invoice.TotalAmount;
                }
            }

            state.passed  = false;
            state.message = "Invoice Load failed";
            if (creditorInvoices.Count > 0) {
                state.passed = true;
                state.message = "Invoice Load successful";
            }
            Assert.AreEqual(true, state.passed, state.message);
        }
    
  
Load open invoice of a party
    
UNDER CONSTRUCTION