public void RecalculateInvoice(InvoiceModel model)
 {
     using (var db = DB.GetContext())
     {
         InvoiceRepository.CalculateInvoice(db, model);
     }
 }
        private InvoiceManager(InvoiceModel invoice)
        {
            using (var db = DB.GetContext())
            {
                this.ActiveModel = invoice;

                var comp = db.CompanyInfos.FirstOrDefault();
            }
        }
        private void BindModel(InvoiceModel mod)
        {
            BindingSource bsMod = new BindingSource();
            bsMod.DataSource = mod;

            InvoiceNumber.DataBindings.Clear();
            InvoiceNumber.DataBindings.Add("EditValue", bsMod, mod.GetName(p => p.InvoiceNumber), true, DataSourceUpdateMode.OnPropertyChanged);

            InvoiceTypeID.DataBindings.Clear();
            InvoiceTypeID.DataBindings.Add("EditValue", bsMod, mod.GetName(p => p.InvoiceTypeID), true, DataSourceUpdateMode.OnPropertyChanged);

            InvoiceIssueDate.DataBindings.Clear();
            InvoiceIssueDate.DataBindings.Add("EditValue", bsMod, mod.GetName(p => p.InvoiceIssueDate), true, DataSourceUpdateMode.OnPropertyChanged);

            IsConfirmed.DataBindings.Clear();
            IsConfirmed.DataBindings.Add("Checked", bsMod, mod.GetName(p => p.IsConfirmed), true, DataSourceUpdateMode.OnPropertyChanged);

            InvoicePeriodFrom.DataBindings.Clear();
            InvoicePeriodFrom.DataBindings.Add("EditValue", bsMod, mod.GetName(p => p.InvoicePeriodFrom), true, DataSourceUpdateMode.OnPropertyChanged);

            InvoicePeriodTo.DataBindings.Clear();
            InvoicePeriodTo.DataBindings.Add("EditValue", bsMod, mod.GetName(p => p.InvoicePeriodTo), true, DataSourceUpdateMode.OnPropertyChanged);

            InvoiceNote.DataBindings.Clear();
            InvoiceNote.DataBindings.Add("EditValue", bsMod, mod.GetName(p => p.InvoiceNote), true, DataSourceUpdateMode.OnPropertyChanged);

            LateCharge.DataBindings.Clear();
            LateCharge.DataBindings.Add("EditValue", bsMod, mod.GetName(p => p.LateCharge), true, DataSourceUpdateMode.OnPropertyChanged);

            LateChargeDays.DataBindings.Clear();
            LateChargeDays.DataBindings.Add("EditValue", bsMod, mod.GetName(p => p.LateChargeDays), true, DataSourceUpdateMode.OnPropertyChanged);

            TotalAmount.DataBindings.Clear();
            TotalAmount.DataBindings.Add("EditValue", bsMod, mod.GetName(p => p.TotalAmount), true, DataSourceUpdateMode.OnPropertyChanged);

            ReceiverCompany.DataBindings.Clear();
            ReceiverCompany.DataBindings.Add("EditValue", bsMod, mod.GetName(p => p.ReceiverCompany), true, DataSourceUpdateMode.OnPropertyChanged);

            ReceiverAddress.DataBindings.Clear();
            ReceiverAddress.DataBindings.Add("EditValue", bsMod, mod.GetName(p => p.ReceiverAddress), true, DataSourceUpdateMode.OnPropertyChanged);

            ShipperCompany.Text = GLOB.Company.CompanyName;
            ShipperAddress.Text = GLOB.Company.CompanyInvoiceAddress;

            BindingSource bsDetails = new BindingSource();
            bsDetails.DataSource = mod.Details;

            gridControlServices.DataSource = bsDetails;
        }
        public static CheckResult ValidateSave(DSModel db, InvoiceModel model)
        {
            CheckResult res = new CheckResult(model);
            if (model.CompanyID == 0)
                res.AddError("Please choose a Company!", model.GetName(p => p.CompanyID));
            if (model.LocationID == 0)
                res.AddError("Please choose a Location!", model.GetName(p => p.LocationID));
            if (model.InvoicePeriodFrom == DateTime.MinValue)
                res.AddError("Please enter a date for Period From!", model.GetName(p => p.InvoicePeriodFrom));
            if (model.InvoicePeriodTo == DateTime.MinValue)
                res.AddError("Please enter a date for Period To!", model.GetName(p => p.InvoicePeriodTo));
            if (model.InvoicePeriodTo < model.InvoicePeriodFrom)
                res.AddError("The date for Pertiod To cannot be earlier than Period From!", model.GetName(p => p.InvoicePeriodTo));

            return res;
        }
        public CheckResult DeleteInvoice(InvoiceModel model)
        {
            try
            {
                using (var db = DB.GetContext())
                {
                    var check = InvoiceValidator.ValidateDelete(db, model);
                    if (check.Failed)
                        return check;

                    InvoiceRepository.DeleteInvoice(db, model);
                    db.SaveChanges();
                    return check;
                }
            }
            catch (Exception ex)
            {
                return new CheckResult(ex);
            }
        }
        public CheckResult SaveInvoice(InvoiceModel model)
        {
            try
            {
                using (var db = DB.GetContext())
                {
                    var check = InvoiceValidator.ValidateSave(db, model);
                    if (check.Failed)
                        return check;

                    KeyBinder key = new KeyBinder();
                    InvoiceRepository.SaveInvoice(db, key, model);
                    db.SaveChanges();
                    key.BindKeys();
                    return check;

                }
            }
            catch (Exception ex)
            {
                return new CheckResult(ex);
            }
        }
        public static InvoiceModel GetInvoice(DSModel db, uint invoiceID)
        {
            if (db == null)
                throw new ArgumentNullException("db");

            var poco = db.Invoices
                .Where(i => i.InvoiceID == invoiceID)
                .FirstOrDefault();

            if (poco == null)
                return null;

            InvoiceModel mod = new InvoiceModel();
            mod.InvoiceID = poco.InvoiceID;
            mod.CompanyID = poco.CompanyID;
            mod.LocationID = poco.LocationID;
            mod.InvoiceTypeID = poco.InvoiceTypeID;
            mod.InvoiceNumber = poco.InvoiceNumber;
            mod.InvoiceIssueDate = poco.InvoiceIssueDate;
            mod.InvoicePeriodFrom = poco.InvoicePeriodFrom;
            mod.InvoicePeriodTo = poco.InvoicePeriodTo;
            mod.InvoiceNote = poco.InvoiceNote;
            mod.LateCharge = poco.LateCharge;
            mod.LateChargeDays = poco.LateChargeDays;
            mod.IsConfirmed = poco.IsConfirmed;
            mod.UserID = poco.UserID;
            mod.LastUpdateTime = poco.LastUpdateTime;
            mod.Details = InvoiceRepository.GetDetails(db, invoiceID);

            mod.ReceiverCompany = poco.Company.CompanyName + " - " + poco.Location.LocationName;
            mod.ReceiverAddress = poco.Location.LocationAddress;

            mod.IsChanged = false;

            return mod;
        }
 public static CheckResult ValidateDelete(DSModel db, InvoiceModel model)
 {
     return new CheckResult(model);
 }
 public void PeekInvoiceNumber(InvoiceModel model)
 {
     using (var db = DB.GetContext())
     {
         model.InvoiceNumber = InvoiceRepository.PeekNumber(db, model);
     }
 }
        public static void SaveInvoice(DSModel db, KeyBinder key, InvoiceModel model)
        {
            if (db == null)
                throw new ArgumentNullException("db");
            if (key == null)
                throw new ArgumentNullException("key");
            if (model == null)
                throw new ArgumentNullException("model");

            model.UserID = GLOB.User.UserID;
            model.LastUpdateTime = DateTime.Now;
            if (model.InvoiceID == 0)
                InsertInvoice(db, key, model);
            else
                UpdateInvoice(db, key, model);
        }
        public static void CalculateInvoice(DSModel db, InvoiceModel invoice)
        {
            var details = db.ExecuteQuery<InvoiceDetailModel>("CALL InvoiceCalculate(@CompanyID, @LocationID, @PeriodFrom, @PeriodTo);",
                new MySqlParameter("CompanyID", invoice.CompanyID),
                new MySqlParameter("LocationID", invoice.LocationID),
                new MySqlParameter("PeriodFrom", invoice.InvoicePeriodFrom),
                new MySqlParameter("PeriodTo", invoice.InvoicePeriodTo));

            invoice.Details.Clear();
            invoice.Details.AddRange(details);
        }
        public static InvoiceModel CreateInvoice(DSModel db, uint companyID, uint locationID, DateTime periodFrom, DateTime periodTo)
        {
            InvoiceModel i = new InvoiceModel();
            i.CompanyID = companyID;
            i.LocationID = locationID;
            i.InvoiceIssueDate = DateTime.Now;
            i.InvoiceNumber = InvoiceRepository.PeekNumber(db, i);
            i.InvoicePeriodFrom = periodFrom;
            i.InvoicePeriodTo = periodTo;
            i.LateChargeDays = 15;
            InvoiceRepository.CalculateInvoice(db, i);


            return i;
        }
 public static string NextNumber(DSModel db, InvoiceModel model)
 {
     return db.ExecuteScalar<string>("SELECT NextInvoiceNumber(@CompID,@LocID,@InvYear);",
         new MySqlParameter("CompID", model.CompanyID),
         new MySqlParameter("LocID", model.LocationID),
         new MySqlParameter("InvYear", model.InvoiceIssueDate.Year));
 }
        public static void DeleteInvoice(DSModel db, InvoiceModel model)
        {
            if (db == null)
                throw new ArgumentNullException("db");
            if (model == null)
                throw new ArgumentNullException("model");

            if (model.InvoiceID != 0)
            {
                var poco = db.Invoices.Where(i => i.InvoiceID == model.InvoiceID).FirstOrDefault();
                if (poco != null)
                    db.Delete(poco);
            }
        }
        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);
            }
        }