Пример #1
0
        public bool CreateInvoice(InvoiceData model)
        {
            bool isSaved = false;

            try
            {
                System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                X509Certificate2 cert = new X509Certificate2(privatePublicFile, privatePublicKey);
                var private_app_api   = new XeroCoreApi("https://api.xero.com/api.xro/2.0/invoices", new PrivateAuthenticator(cert),
                                                        new Consumer(consumerKey, consumerSecret), null,
                                                        new DefaultMapper(), new DefaultMapper());

                var inv = private_app_api.Invoices;

                Contact newContact = new Contact();
                newContact.Name = model.custName;

                Invoice newInvoice = new Invoice();
                newInvoice.Contact         = new Contact();
                newInvoice.Contact         = newContact;
                newInvoice.Date            = System.DateTime.Now;
                newInvoice.Reference       = "TestDemoNZ";
                newInvoice.DueDate         = System.DateTime.Now.AddMonths(1);
                newInvoice.Type            = Xero.Api.Core.Model.Types.InvoiceType.AccountsReceivable;
                newInvoice.LineAmountTypes = Xero.Api.Core.Model.Types.LineAmountType.Exclusive;
                List <LineItem> lines = new List <LineItem>();
                LineItem        li    = new LineItem();
                foreach (var item in model.gridItems)
                {
                    li.Quantity    = Convert.ToDecimal(item.qty);
                    li.Description = item.des;
                    li.AccountCode = "200";  //Need to analyse from Xero invoice for which this column is being used. Time being hard coded the value.
                    li.UnitAmount  = Convert.ToDecimal(item.rate);
                    lines.Add(li);
                }
                newInvoice.LineItems = lines;

                if (model.invoicePaid == "True")
                {
                    var result = inv.Create(newInvoice);
                    IEnumerable <Invoice> invF = inv.Find().Where(invt => invt.Contact.Name == model.custName).ToList();
                    if (invF.Count() > 0)
                    {
                        foreach (Invoice item in invF)
                        {
                            item.AmountPaid = item.AmountDue;
                            item.Status     = Xero.Api.Core.Model.Status.InvoiceStatus.Authorised;
                            inv.Update(item);


                            var     pItem = private_app_api.Payments;
                            Payment pm    = new Payment();

                            Account ac = new Account();
                            ac.Code = "091";
                            Invoice iv = new Invoice();
                            iv.Id      = item.Id;
                            pm.Account = ac;
                            pm.Invoice = iv;
                            pm.Amount  = item.AmountDue;
                            pm.Date    = System.DateTime.Now;
                            pm.Status  = Xero.Api.Core.Model.Status.PaymentStatus.Authorised;
                            pItem.Create(pm);

                            isSaved = true;
                        }
                    }
                }
                else
                {
                    // call the API to create the Invoice
                    var result = inv.Create(newInvoice);
                    isSaved = true;
                }

                // To Create Contact if required
                //var cItem = private_app_api.Contacts;
                //IEnumerable<Contact> invC = cItem.Find().Where(invtc => invtc.Name == "Sangi NZ").ToList();
                //if (invC.Count() == 0)
                //{
                //    var contacts = CreateContacts("Sangi", "NZ").ToList();
                //    cItem.Create(contacts);
                //}
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(isSaved);
        }