public void EditRows()
        {
            var gridModel = new LeasingDatabase.Models.Grid.ComponentJqGridModel();
            var db        = new AuleaseEntities();

            var e = gridModel.OrdersGrid.GetEditData(); // Edit Row

            if (gridModel.OrdersGrid.AjaxCallBackMode == AjaxCallBackMode.EditRow)
            {
                int ComponentID = Convert.ToInt32(e.RowData["ComponentID"]);

                string _SerialNumber  = e.RowData["SerialNumber"].ToString().Trim();
                string _LeaseTag      = e.RowData["LeaseTag"].ToString().Trim();
                string _Note          = e.RowData["Note"].ToString().Trim();
                string _Make          = e.RowData["Make"].ToString().Trim();
                string _ComponentType = e.RowData["ComponentType"].ToString().Trim();
                string _Model         = e.RowData["Model"].ToString().Trim();

                // Update Component

                Component comp = db.Components.Where(n => n.Id == ComponentID).Single();
                comp.SerialNumber = _SerialNumber;
                comp.LeaseTag     = _LeaseTag;
                comp.Note         = _Note;
                comp.Make         = db.Makes.Where(n => n.Name == _Make).Single();
                comp.Type         = db.Types.Where(n => n.Name == _ComponentType).Single();
                Model model;
                if (db.Models.Any(n => n.Name == _Model))
                {
                    model = db.Models.Where(n => n.Name == _Model).FirstOrDefault();
                }
                else
                {
                    model = new Model()
                    {
                        Name = _Model, Make = db.Makes.Where(n => n.Name == _Make).Single()
                    };
                }
                comp.Model = model;


                // Update Leases

                DateTime _BeginDate      = Convert.ToDateTime(e.RowData["BeginDate"]);
                DateTime _EndDate        = Convert.ToDateTime(e.RowData["EndDate"]);
                string   _StatementName  = e.RowData["StatementName"].ToString().Trim();
                string   _ContractNumber = e.RowData["ContractNumber"].ToString().Trim();
                string   _Fund           = e.RowData["Fund"].ToString().Trim();
                string   _Org            = e.RowData["Org"].ToString().Trim();
                string   _Program        = e.RowData["Program"].ToString().Trim();
                string   _RateLevel      = e.RowData["RateLevel"].ToString().Trim();
                int      _Term           = Convert.ToInt32(e.RowData["Term"]);
                string   _GID            = e.RowData["GID"].ToString().Trim();

                Lease currentLease = comp.Leases.OrderByDescending(n => n.EndDate).First();

                Department department;
                if (db.Departments.Any(n => n.Fund == _Fund && n.Org == _Org && n.Program == _Program))
                {
                    department = db.Departments.Where(n => n.Fund == _Fund && n.Org == _Org && n.Program == _Program).FirstOrDefault();
                }
                else
                {
                    department = new Department {
                        Fund = _Fund, Org = _Org, Program = _Program
                    };
                }

                Overhead Overhead = db.Overheads.Where(n => n.RateLevel == _RateLevel && n.Term == _Term).OrderByDescending(n => n.BeginDate).FirstOrDefault();

                User newUser;

                if (db.Users.Any(n => n.GID == _GID))
                {
                    newUser = db.Users.Where(n => n.GID == _GID).Single();
                }
                else
                {
                    newUser          = new User();
                    newUser.GID      = _GID;
                    newUser.Location = new Location();
                }

                currentLease.BeginDate        = _BeginDate;
                currentLease.EndDate          = _EndDate;
                currentLease.StatementName    = _StatementName;
                currentLease.Timestamp        = DateTime.Now;
                currentLease.ContractNumber   = _ContractNumber;
                currentLease.Department       = department;
                currentLease.Overhead         = Overhead;
                currentLease.SystemGroup.User = newUser;

                db.SaveChanges();
            }
            if (gridModel.OrdersGrid.AjaxCallBackMode == AjaxCallBackMode.AddRow)
            {
                // No Adding through this table!
                throw new NotImplementedException();
            }
            if (gridModel.OrdersGrid.AjaxCallBackMode == AjaxCallBackMode.DeleteRow)
            {
                int           ComponentID   = Convert.ToInt32(e.RowData["ComponentID"]);
                Component     comp          = db.Components.Where(n => n.Id == ComponentID).Single();
                List <Lease>  leases        = comp.Leases.ToList();
                List <Charge> Charges       = leases.SelectMany(n => n.Charges).ToList();
                int           SystemGroupID = leases.Select(n => n.SystemGroupId).Single();
                int           OrderID       = leases.Select(n => n.SystemGroup).SingleOrDefault().OrderId;
                int?          POID          = leases.Select(n => n.SystemGroup).SingleOrDefault().POId.HasValue ? (int?)leases.Select(n => n.SystemGroup).Single().POId.Value : null;



                foreach (var charge in Charges)
                {
                    db.Entry(charge).State = EntityState.Deleted;
                }

                foreach (var lease in leases)
                {
                    db.Entry(lease).State = EntityState.Deleted;
                }

                List <Property> props = comp.Properties.ToList();
                foreach (var prop in props)
                {
                    comp.Properties.Remove(prop);
                }

                db.Entry(comp).State = EntityState.Deleted;
                db.SaveChanges();

                if (db.SystemGroups.Where(n => n.Id == SystemGroupID).SingleOrDefault().Leases.Count == 0)
                {
                    db.Entry(db.SystemGroups.Where(n => n.Id == SystemGroupID).SingleOrDefault()).State = EntityState.Deleted;
                }

                if (db.Orders.Where(n => n.Id == OrderID).Single().SystemGroups.Count == 0)
                {
                    db.Entry(db.Orders.Where(n => n.Id == OrderID).Single()).State = EntityState.Deleted;
                }

                if (POID != null && db.POes.Where(n => n.Id == POID).Single().SystemGroups.Count == 0)
                {
                    db.Entry(db.POes.Where(n => n.Id == POID).Single()).State = EntityState.Deleted;
                }

                db.SaveChanges();
            }
        }
Пример #2
0
        public ActionResult EditRows()
        {
            var gridModel = new LeasingDatabase.Models.Grid.OrdersJqGridModel();
            var db        = new AuleaseEntities();

            var e = gridModel.OrdersGrid.GetEditData();             // Edit Row

            if (gridModel.OrdersGrid.AjaxCallBackMode == AjaxCallBackMode.EditRow)
            {
                int   id    = Convert.ToInt32(e.RowData["Id"].ToString());
                Lease lease = db.Leases.Where(n => n.Id == id).Single();

                string validationMessage = ValidationEditRow(e);

                if (!String.IsNullOrWhiteSpace(validationMessage))
                {
                    return(gridModel.OrdersGrid.ShowEditValidationMessage(validationMessage));
                }

                string _Fund    = e.RowData["Fund"].ToString().Trim();
                string _Org     = e.RowData["Org"].ToString().Trim();
                string _Program = e.RowData["Program"].ToString().Trim();

                string   _ContractNumber = e.RowData["ContractNumber"].ToString().Trim();
                DateTime _BeginDate      = Convert.ToDateTime(e.RowData["BeginDate"].ToString());
                DateTime _EndDate        = Convert.ToDateTime(e.RowData["EndDate"].ToString());
                string   _StatementName  = e.RowData["StatementName"].ToString();

                decimal _MonthlyCharge = Convert.ToDecimal(e.RowData["MonthlyCharge"].ToString());


                Department dept = db.Departments.Where(n => n.Fund == _Fund && n.Org == _Org && n.Program == _Program).Single();
                lease.Department     = dept;
                lease.BeginDate      = _BeginDate;
                lease.EndDate        = _EndDate;
                lease.StatementName  = _StatementName;
                lease.ContractNumber = _ContractNumber;
                lease.MonthlyCharge  = _MonthlyCharge;

                db.SaveChanges();
            }
            if (gridModel.OrdersGrid.AjaxCallBackMode == AjaxCallBackMode.AddRow)
            {
                // Add Row
            }
            if (gridModel.OrdersGrid.AjaxCallBackMode == AjaxCallBackMode.DeleteRow)
            {
                int   id    = Convert.ToInt32(e.RowData["Id"].ToString());
                Lease lease = db.Leases.Where(n => n.Id == id).Single();

                Session["SN"] = lease.Component.SerialNumber;

                foreach (var charge in lease.Charges)
                {
                    charge.Leases.Remove(lease);
                }

                Component comp = lease.Component;
                comp.Leases.Remove(lease);
                SystemGroup group = lease.SystemGroup;
                group.Leases.Remove(lease);

                Department dept = lease.Department;
                dept.Leases.Remove(lease);

                db.Entry(lease).State = EntityState.Deleted;
                db.SaveChanges();
            }

            return(new RedirectResult(Url.Action("DataRequested", "Billing")));
        }
Пример #3
0
        public static void FillLeasingInfo(int SystemGroupID, string BegBillDate, string EndBillDate,
                                           string CPUCost, string MonitorCost, string Monitor2Cost,
                                           string InsuranceCost, string WarrantyCost, string ShippingCost)
        {
            AuleaseEntities db = new AuleaseEntities();

            SystemGroup system = db.SystemGroups.Where(n => n.Id == SystemGroupID).Single();

            //Remove existing charges before adding new ones
            foreach (var lease in system.Leases)
            {
                List <Charge> charges = lease.Charges.ToList();
                foreach (var charge in charges)
                {
                    lease.Charges.Remove(charge);
                }

                foreach (var charge in db.Charges.Where(n => n.Leases.FirstOrDefault().Id == lease.Id))
                {
                    db.Entry(charge).State = EntityState.Deleted;
                }
            }

            db.SaveChanges();


            //Add new charges
            int compCount = system.Leases.Count;
            List <Component> MainComps = system.Components.Where(n => n.Type.Name != "Monitor").ToList();
            List <Component> Monitors  = system.Components.Where(n => n.Type.Name == "Monitor").ToList();

            foreach (var comp in MainComps)
            {
                Lease currentLease = comp.CurrentLease;
                currentLease.BeginDate = Convert.ToDateTime(BegBillDate);
                currentLease.EndDate   = Convert.ToDateTime(EndBillDate);

                int Term = comp.Leases.FirstOrDefault().Overhead.Term + 1;                 // Add 1 for vendor rates

                VendorRate VendorRate = db.VendorRates.Where(n => n.Term == Term && n.Type.Name == comp.Type.Name).OrderByDescending(n => n.BeginDate).FirstOrDefault();
                Charge     MainCharge = new Charge {
                    Price = Convert.ToDecimal(CPUCost), VendorRate = VendorRate, Type = comp.Type, Tax = db.Taxes.OrderByDescending(n => n.Timestamp).First()
                };
                currentLease.Charges.Add(MainCharge);

                if (!String.IsNullOrWhiteSpace(InsuranceCost))
                {
                    VendorRate InsuranceVendorRate = db.VendorRates.Where(n => n.Term == Term && n.Type.Name == "Insurance").OrderByDescending(n => n.BeginDate).FirstOrDefault();
                    Charge     InsuranceCharge     = new Charge {
                        Price = Convert.ToDecimal(InsuranceCost), VendorRate = InsuranceVendorRate, Type = db.TypesInsurance
                    };
                    currentLease.Charges.Add(InsuranceCharge);
                }

                if (!String.IsNullOrWhiteSpace(WarrantyCost))
                {
                    VendorRate WarrantyVendorRate = db.VendorRates.Where(n => n.Term == Term && n.Type.Name == "Insurance").OrderByDescending(n => n.BeginDate).FirstOrDefault();
                    Charge     WarrantyCharge     = new Charge {
                        Price = Convert.ToDecimal(WarrantyCost) / compCount, VendorRate = WarrantyVendorRate, Type = db.TypesWarranty
                    };
                    currentLease.Charges.Add(WarrantyCharge);
                }

                if (!String.IsNullOrWhiteSpace(ShippingCost))
                {
                    VendorRate ShippingVendorRate = db.VendorRates.Where(n => n.Term == Term && n.Type.Name == "Insurance").OrderByDescending(n => n.BeginDate).FirstOrDefault();
                    Charge     ShippingCharge     = new Charge {
                        Price = Convert.ToDecimal(ShippingCost) / compCount, VendorRate = ShippingVendorRate, Type = db.TypesShipping
                    };
                    currentLease.Charges.Add(ShippingCharge);
                }
            }
            int i = 0;

            foreach (var comp in Monitors)
            {
                Lease currentLease = comp.CurrentLease;
                currentLease.BeginDate = Convert.ToDateTime(BegBillDate);
                currentLease.EndDate   = Convert.ToDateTime(EndBillDate);

                int Term = comp.Leases.FirstOrDefault().Overhead.Term + 1;                 // Add 1 for vendor rates

                VendorRate VendorRate = db.VendorRates.Where(n => n.Term == Term && n.Type.Name == comp.Type.Name).OrderByDescending(n => n.BeginDate).FirstOrDefault();

                Charge MainCharge;
                if (i == 0)
                {
                    MainCharge = new Charge {
                        Price = Convert.ToDecimal(MonitorCost), VendorRate = VendorRate, Type = comp.Type, Tax = db.Taxes.OrderByDescending(n => n.Timestamp).First()
                    };
                }
                else
                {
                    MainCharge = new Charge {
                        Price = Convert.ToDecimal(Monitor2Cost), VendorRate = VendorRate, Type = comp.Type, Tax = db.Taxes.OrderByDescending(n => n.Timestamp).First()
                    };
                }
                i++;
                currentLease.Charges.Add(MainCharge);

                if (!String.IsNullOrWhiteSpace(InsuranceCost) && MainComps.Count == 0)
                {
                    VendorRate InsuranceVendorRate = db.VendorRates.Where(n => n.Term == Term && n.Type.Name == "Insurance").OrderByDescending(n => n.BeginDate).FirstOrDefault();
                    Charge     InsuranceCharge     = new Charge {
                        Price = Convert.ToDecimal(InsuranceCost), VendorRate = InsuranceVendorRate, Type = db.TypesInsurance
                    };
                    currentLease.Charges.Add(InsuranceCharge);
                }

                if (!String.IsNullOrWhiteSpace(WarrantyCost))
                {
                    VendorRate WarrantyVendorRate = db.VendorRates.Where(n => n.Term == Term && n.Type.Name == "Insurance").OrderByDescending(n => n.BeginDate).FirstOrDefault();
                    Charge     WarrantyCharge     = new Charge {
                        Price = Convert.ToDecimal(WarrantyCost) / compCount, VendorRate = WarrantyVendorRate, Type = db.TypesWarranty
                    };
                    currentLease.Charges.Add(WarrantyCharge);
                }

                if (!String.IsNullOrWhiteSpace(ShippingCost))
                {
                    VendorRate ShippingVendorRate = db.VendorRates.Where(n => n.Term == Term && n.Type.Name == "Insurance").OrderByDescending(n => n.BeginDate).FirstOrDefault();
                    Charge     ShippingCharge     = new Charge {
                        Price = Convert.ToDecimal(ShippingCost) / compCount, VendorRate = ShippingVendorRate, Type = db.TypesShipping
                    };
                    currentLease.Charges.Add(ShippingCharge);
                }
            }

            db.SaveChanges();
        }