Пример #1
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));
        }