/// <summary>
        /// Creates a Xero line item for every Aquairum line item with a matching Invoice number,  and bundles them up into a Xero invoice
        /// </summary>
        /// <param name="theInvoiceLineItemToConvert"></param>
        /// <returns></returns>
        public static XeroApi.Model.Invoice ConvertMultipleOutboundInvoiceLineItemsToXeroInvoice(OutboundInvoiceLineItemList theInvoiceLineItemsToConvert, OutboundInvoiceLineItem theLineItemToMatchTo, Datalayer.Xero.Interresolve.InterResolveXeroService aService)
        {
            try
            {
                XeroApi.Model.Invoice theInvoiceToReturn = new XeroApi.Model.Invoice();
                theInvoiceToReturn.LineItems = new XeroApi.Model.LineItems();
                theInvoiceToReturn.Contact = new XeroApi.Model.Contact(); //NEED TO SPECIFY A CONTACT

                //assign the xero contact
                aService.LoginToXero();
                theInvoiceToReturn.Contact = aService.GetXeroContactFromAquariumInvoiceLineItemList(theLineItemToMatchTo);

                //assign top level invoice fields for the passed invoice to the Xero counterpart
                theInvoiceToReturn.InvoiceNumber = theLineItemToMatchTo.InvoiceNumber.ToString();
                theInvoiceToReturn.Status = "DRAFT";
                theInvoiceToReturn.Type = "ACCREC"; //Sales invoice
                theInvoiceToReturn.Reference = theLineItemToMatchTo.SageNarrative + " " + theLineItemToMatchTo.YourRef;
                theInvoiceToReturn.LineAmountTypes = XeroApi.Model.LineAmountType.Exclusive;
                theInvoiceToReturn.SubTotal = theLineItemToMatchTo.OriginalPurchaseInvoiceTotal;
                theInvoiceToReturn.DueDate = DateTime.Now.AddDays(14);
                theInvoiceToReturn.Date = theLineItemToMatchTo.InvoiceDate;
               // theInvoiceToReturn.TotalTax //this must be calculated AFTER by summing all the line item VATs
                theInvoiceToReturn.Total = theLineItemToMatchTo.OriginalPurchaseInvoiceTotal;

                //iterate through and if number matches specified invoice number, add it to the list
                decimal RunningVATTotalForInvoice = new decimal(0.00);

                for (int i = 0; i < theInvoiceLineItemsToConvert.OutboundInvoiceLineItems.Count(); i++)
                {
                    if (theInvoiceLineItemsToConvert.OutboundInvoiceLineItems.ElementAt(i).InvoiceNumber == theLineItemToMatchTo.InvoiceNumber)
                    {
                         //if the invoice numbers match, create a new line item and add this line item to the list
                        XeroApi.Model.LineItem aLineItem = new XeroApi.Model.LineItem();
                        aLineItem.Quantity = 1; //always one line item
                        aLineItem.Description = theInvoiceLineItemsToConvert.OutboundInvoiceLineItems.ElementAt(i).InvoiceType; //
                        aLineItem.UnitAmount = theInvoiceLineItemsToConvert.OutboundInvoiceLineItems.ElementAt(i).OriginalPurchaseInvoiceAmount;
                        aLineItem.TaxAmount = theInvoiceLineItemsToConvert.OutboundInvoiceLineItems.ElementAt(i).OriginalPurchaseInvoiceVAT;
                        aLineItem.LineAmount = (aLineItem.Quantity * aLineItem.UnitAmount);
                        aLineItem.AccountCode = InterResolveXeroService.GetXeroAccountCodeForAquariumLineItem(theInvoiceLineItemsToConvert.OutboundInvoiceLineItems.ElementAt(i));

                        //add the VAT to the running total
                        RunningVATTotalForInvoice =+ theInvoiceLineItemsToConvert.OutboundInvoiceLineItems.ElementAt(i).OriginalPurchaseInvoiceVAT;

                        //add it to the list
                        theInvoiceToReturn.LineItems.Add(aLineItem);
                     }

                 }

                //assign the overall invoice VAT
                theInvoiceToReturn.TotalTax = RunningVATTotalForInvoice;

                return theInvoiceToReturn;
                }

            catch (Exception ex)
            {
                throw ex;
            }
        }
예제 #2
0
        public ActionResult Create([Bind(Include = "Contact,Date,Type,DueDate,Status")] Invoice invoice)
        {
            if (invoice.Date < DateTime.Now.AddDays(-1))
            {
                ModelState.AddModelError("Date", "Date must be today or a future time");
            }
            if (invoice.DueDate < invoice.Date)
            {
                ModelState.AddModelError("DueDate", "DueDate must be greater than the Start Date");
            }


            //in real app we also need to check the existence of the contact

            if (ModelState.IsValid)
            {
                var XeroSession = new XeroApi.OAuth.XeroApiPrivateSession("MyApiTestSoftware", "5INHEEY7VVKTPZPBMTF9BRCDN33KGM", new X509Certificate2("C:\\OpenSSL-Win32\\bin\\public_privatekey.pfx"));
                var repository  = new XeroApi.Repository(XeroSession);

                var contacts = repository.Contacts.Where(c => c.Name == "Marine Systems");

                //new comment

                var uInvoice = new XeroApi.Model.Invoice();
                var uContact = new XeroApi.Model.Contact();

                DateTime today   = DateTime.Now;
                DateTime duedate = today.AddDays(30);

                uContact.Name = "Marine Systems";

                uInvoice.Contact = uContact;
                //uInvoice.Date = DateTime.Now;
                uInvoice.Date = invoice.Date;
                uInvoice.Type = "ACCREC";
                //uInvoice.DueDate = duedate;
                uInvoice.DueDate         = invoice.DueDate;
                uInvoice.Status          = "AUTHORISED";
                uInvoice.FullyPaidOnDate = today;
                uInvoice.AmountPaid      = 230;



                uInvoice.LineItems = new XeroApi.Model.LineItems();
                var uLineItem1 = new XeroApi.Model.LineItem();
                uLineItem1.Quantity    = 1;
                uLineItem1.Description = "Product 1";
                uLineItem1.AccountCode = "200";
                uLineItem1.UnitAmount  = 50;
                uInvoice.LineItems.Add(uLineItem1);

                var uLineItem2 = new XeroApi.Model.LineItem();
                uLineItem2.Quantity    = 3;
                uLineItem2.Description = "Product 2";
                uLineItem2.AccountCode = "200";
                uLineItem2.UnitAmount  = 50;
                uInvoice.LineItems.Add(uLineItem2);

                var sResults = repository.Create((XeroApi.Model.Invoice)uInvoice);

                var theAccount = repository.Accounts.FirstOrDefault();

                //the following section is to create a payment for the invoice
                var invoiceNumber = sResults.InvoiceNumber;
                var invoiceID     = sResults.InvoiceID;
                var NewInvoice    = repository.Invoices.Where(i => i.InvoiceID == invoiceID).FirstOrDefault();

                //var account = repository.Accounts.Where(a => a.Name == "MyAccount").FirstOrDefault();
                var account = new XeroApi.Model.Account();

                //account.ReportingCode = "ABC";

                var Payment = new XeroApi.Model.Payment();
                //Payment.Account = (XeroApi.Model.Account)account;
                Payment.Account = theAccount;
                Payment.Invoice = (XeroApi.Model.Invoice)NewInvoice;
                Payment.Invoice.InvoiceNumber = invoiceNumber.ToString();
                Payment.Invoice.InvoiceID     = invoiceID;
                Payment.Status = "AUTHORISED";
                Payment.Date   = DateTime.Now;
                Payment.Amount = 200;

                var paymentResults = repository.Create((XeroApi.Model.Payment)Payment);

                db.Invoice.Add(invoice);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(invoice));
        }