static void Main(string[] args)
        {
            using (BlauModel contextDB = new BlauModel())
            {
                contextDB.Database.CreateIfNotExists();
            }

            BlauModel context = new BlauModel();
            DBAddress a = context.Address.Where(x => x.Address1 == "509 Schaefer Street" && x.Postcode == "2641").FirstOrDefault();
            if (a == default(DBAddress))
            {
                a = new DBAddress();
                a.Address1 = "509 Schaefer Street";
                a.State = "NSW";
                a.Postcode = "2641";
                a.City = "Lavington";
                a.Country = context.Country.Where(x => x.countryCode == "AU").FirstOrDefault();
                context.Address.Add(a);
            }

            DBCustomer c = context.Customer.Where(x => x.CustomerNumber == "CN073396").FirstOrDefault();
            if (c == default(DBCustomer))
            {
                c = new DBCustomer();
                c.FirstName = "Michael";
                c.LastName = "Dann";
                c.Address = a;
                c.CustomerNumber = "CN073396";
                c.PhoneNumber = "0412032869";
                c.Title = "Mr";
                context.Customer.Add(c);
            }

            DateTime invoiceDate = new DateTime(2014, 3, 13, 13, 55, 41);
            string invoiceNumber = "SI1450869";
            DBCompany company = context.Company.Where(x => x.CompanyName == "Bright Life Australia").FirstOrDefault();
            DBInvoice invoice = context.Invoice.Where(x => x.InvoiceNumber == invoiceNumber && x.TimeCreated == invoiceDate).FirstOrDefault();
            if(invoice == default(DBInvoice))
            {
                invoice = new DBInvoice();
                invoice.TimeCreated = invoiceDate;
                invoice.InvoiceNumber = invoiceNumber;
                invoice.InvoiceStatus = 1;
                invoice.IPv4 = "127.0.0.1";
                invoice.Charge = 0;
                invoice.Company = company;
                invoice.Customer = c;
                context.Invoice.Add(invoice);
            }

            context.SaveChanges();
        }
        public Invoice(DBInvoice invoice)
        {
            invoiceCustomer = API.CustomerAPI.GetCustomerDetailsByCustomerNumber(invoice.Customer.CustomerNumber);
            companyName = invoice.Company.CompanyName;
            companyABN = invoice.Company.CompanyABN;
            CompanyAddress = string.Format("{0} {1} {2} {3}",
                invoice.Company.CompanyAddress.Address1,
                invoice.Company.CompanyAddress.City,
                invoice.Company.CompanyAddress.State,
                invoice.Company.CompanyAddress.Postcode);
            provider = API.CarrierAPI.GetCarrierById((Enums.CarrierDatabaseKey)invoice.Carrier.carrierId);

            invoiceNo = invoice.InvoiceNumber;
            invoiceDate = invoice.TimeCreated;

            status = (Enums.InvoiceStatus)invoice.InvoiceStatus;
            packed = false;
        }
 internal bool InvoiceIsPacked(DBInvoice invoice)
 {
     return invoice.Parcels != null && invoice.Parcels.Count > 0 && (
         from p in invoice.Parcels
         where p.ParcelStatus == (int)Enums.ParcelStatus.PACKED
         select p).Count() > 0;
 }
        public void Save()
        {
            BlauModel _dbcontext = BlauModel.GetContext();
            DBInvoice invoice = null;

            if (Id == 0)
            {
                invoice = new DBInvoice();
                invoice.TimeCreated = DateTime.Now;
            } else
            {
                invoice = _dbcontext.Invoice.Where(x => x.InvoiceId == Id).FirstOrDefault();
            }

            API.CustomerAPI.SaveCustomerDetails(InvoiceCustomer);

            if (invoice == default(DBInvoice))
                throw new InvoiceException(
                    string.Format("Invoice for id {0} not found in data context", Id)
                );

            invoice.Customer = _dbcontext.Customer.Where(x => x.CustomerNumber == InvoiceCustomer.CustomerNumber).FirstOrDefault();
            invoice.Company = _dbcontext.Company.Where(x => x.CompanyName == CompanyName).FirstOrDefault();
            invoice.InvoiceNumber = InvoiceNo;
            invoice.InvoiceStatus = (int)Status;
            invoice.Charge = 0;

            _dbcontext.SaveChanges();

            if(Id == 0)
            {
                Id = _dbcontext.Invoice.Where(x => x.InvoiceNumber == invoice.InvoiceNumber).First().InvoiceId;
            }
        }