private static void UpdateInvoice(DSModel db, KeyBinder key, InvoiceModel model)
        {
            var poco = db.Invoices.Where(i => i.InvoiceID == model.InvoiceID).FirstOrDefault();
            if (poco == null)
                throw new ArgumentException("No invoice with the specified ID!");

            poco.CompanyID = model.CompanyID;
            poco.LocationID = model.LocationID;
            poco.InvoiceTypeID = model.InvoiceTypeID;
            poco.InvoiceNumber = model.InvoiceNumber;
            poco.InvoiceIssueDate = model.InvoiceIssueDate;
            poco.InvoicePeriodFrom = model.InvoicePeriodFrom;
            poco.InvoicePeriodTo = model.InvoicePeriodTo;
            poco.InvoiceNote = model.InvoiceNote;
            poco.LateCharge = model.LateCharge;
            poco.LateChargeDays = model.LateChargeDays;
            poco.IsConfirmed = model.IsConfirmed;
            poco.UserID = model.UserID;
            poco.LastUpdateTime = model.LastUpdateTime;

            foreach (var d in poco.InvoicesDetails.ToList())
            {
                db.Delete(d);
                poco.InvoicesDetails.Remove(d);
            }

            foreach (var d in model.Details)
            {
                var det = new InvoicesDetail();
                det.InvoiceDetailDate = d.InvoiceDetailDate;
                det.InvoiceDetailName = d.InvoiceDetailName;
                det.InvoiceDetailTotalTime = d.InvoiceDetailTotalTime;
                det.InvoiceDetailOverTime = d.InvoiceDetailOverTime;
                det.InvoiceDetailRegularRate = d.InvoiceDetailRegularRate;
                det.InvoiceDetailOverRate = d.InvoiceDetailOverRate;
                det.InvoiceDetailRegularPay = d.InvoiceDetailRegularPay;
                det.InvoiceDetailOvertimePay = d.InvoiceDetailOvertimePay;
                det.InvoiceDetailGroupName = d.InvoiceDetailGroupName;
                det.InvoiceDetailGroupPosition = d.InvoiceDetailGroupPosition;
                det.Invoice = poco;
                det.InvoiceID = poco.InvoiceID;
                key.AddKey(det, d, d.GetName(p => p.InvoiceDetailID));
                poco.InvoicesDetails.Add(det);
                db.Add(det);
            }
        }
        private static void InsertInvoice(DSModel db, KeyBinder key, InvoiceModel model)
        {
            var poco = new Invoice();
            poco.CompanyID = model.CompanyID;
            poco.LocationID = model.LocationID;
            poco.InvoiceTypeID = model.InvoiceTypeID;
            poco.InvoiceNumber = InvoiceRepository.NextNumber(db, model);
            poco.InvoiceIssueDate = model.InvoiceIssueDate;
            poco.InvoicePeriodFrom = model.InvoicePeriodFrom;
            poco.InvoicePeriodTo = model.InvoicePeriodTo;
            poco.InvoiceNote = model.InvoiceNote;
            poco.LateCharge = model.LateCharge;
            poco.LateChargeDays = model.LateChargeDays;
            poco.IsConfirmed = model.IsConfirmed;
            poco.UserID = model.UserID;
            poco.LastUpdateTime = model.LastUpdateTime;
            key.AddKey(poco, model, model.GetName(p => p.InvoiceID));
            db.Add(poco);

            foreach (var d in model.Details)
            {
                var det = new InvoicesDetail();
                det.InvoiceDetailDate = d.InvoiceDetailDate;
                det.InvoiceDetailName = d.InvoiceDetailName;
                det.InvoiceDetailTotalTime = d.InvoiceDetailTotalTime;
                det.InvoiceDetailOverTime = d.InvoiceDetailOverTime;
                det.InvoiceDetailRegularRate = d.InvoiceDetailRegularRate;
                det.InvoiceDetailOverRate = d.InvoiceDetailOverRate;
                det.InvoiceDetailRegularPay = d.InvoiceDetailRegularPay;
                det.InvoiceDetailOvertimePay = d.InvoiceDetailOvertimePay;
                det.InvoiceDetailGroupName = d.InvoiceDetailGroupName;
                det.InvoiceDetailGroupPosition = d.InvoiceDetailGroupPosition;
                det.Invoice = poco;
                key.AddKey(det, d, d.GetName(p => p.InvoiceDetailID));
                key.AddKey(poco, d, d.GetName(p => p.InvoiceID));
                poco.InvoicesDetails.Add(det);
                db.Add(det);
            }
        }