Example #1
0
        private void dgMaintenance_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            var senderGrid      = (DataGridView)sender;
            var editColumnIndex = 0;

            if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
            {
                var selectedItem = senderGrid.Rows[e.RowIndex].DataBoundItem as MaintenanceResult;

                if (selectedItem != null)
                {
                    if (e.ColumnIndex == editColumnIndex)
                    {
                        if (selectedItem.MaintenanceId != null)
                        {
                            var frmMaintenanceDetail = new frmMaintenanceDetail(_DataContext, selectedItem.MaintenanceId.Value, false);
                            var dialogResult         = frmMaintenanceDetail.ShowDialog();

                            if (dialogResult == DialogResult.OK)
                            {
                                btnSearch.PerformClick();
                            }
                        }
                        else
                        {
                            var reqItem    = _DataContext.tblRequisitions.Where(a => a.id == selectedItem.RequisitionId).Single();
                            var configItem = _DataContext.BuildingMaintenanceConfigurationSet.Single(a => a.id == selectedItem.ConfigItemId.Value);

                            if (reqItem.SupplierId == null)
                            {
                                //lets find the supplier id


                                var frmSupplierLookup = new frmSupplierLookup(_DataContext, configItem.BuildingId);

                                var supplierResult = frmSupplierLookup.ShowDialog();
                                var supplier       = frmSupplierLookup.SelectedSupplier;

                                if (supplierResult == DialogResult.OK && supplier != null)
                                {
                                    var bankDetails = _DataContext.SupplierBuildingSet.Include(a => a.Bank).SingleOrDefault(a => a.BuildingId == configItem.BuildingId && a.SupplierId == supplier.id);
                                    if (bankDetails != null)
                                    {
                                        reqItem.SupplierId    = supplier.id;
                                        reqItem.Supplier      = supplier;
                                        reqItem.BankName      = bankDetails.Bank.Name;
                                        reqItem.BranchCode    = bankDetails.BranceCode;
                                        reqItem.BranchName    = bankDetails.BranchName;
                                        reqItem.AccountNumber = bankDetails.AccountNumber;
                                    }
                                    else
                                    {
                                        Controller.HandleError("Supplier banking details for this building is not configured.\n" +
                                                               "Please capture bank details for this building on the suppier detail screen.", "Validation Error");
                                        return;
                                    }
                                }
                                else
                                {
                                    Controller.HandleError("Supplier required for Maintenance. Please select a supplier.", "Validation Error");
                                    return;
                                }
                            }

                            var frmMaintenanceDetail = new frmMaintenanceDetail(_DataContext, reqItem, configItem, true);
                            var dialogResult         = frmMaintenanceDetail.ShowDialog();
                            if (dialogResult == DialogResult.OK)
                            {
                                _DataContext.SaveChanges();
                                btnSearch.PerformClick();
                            }
                            else
                            {
                                //reject changes
                                foreach (var entry in _DataContext.ChangeTracker.Entries())
                                {
                                    switch (entry.State)
                                    {
                                    case EntityState.Modified:
                                    case EntityState.Deleted:
                                        entry.State = EntityState.Modified;     //Revert changes made to deleted entity.
                                        entry.State = EntityState.Unchanged;
                                        break;

                                    case EntityState.Added:
                                        entry.State = EntityState.Detached;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (dtInvoiceDate.Value <= _minDate)
            {
                Controller.HandleError("Invoice Date required for Maintenance. Please select a date.", "Validation Error");
                return;
            }
            if (String.IsNullOrWhiteSpace(txtInvoiceNumber.Text))
            {
                Controller.HandleError("Invoice Number required for Maintenance. Please supply an invoice number.", "Validation Error");
                return;
            }
            if (_Documents.Count == 0)
            {
                Controller.HandleError("Invoice attachment required, please upload Invoice PDF", "Validation Error");
                return;
            }

            if (_Supplier == null)
            {
                Controller.HandleError("Supplier not selected, please select a supplier.", "Validation Error");
                return;
            }

            using (var context = SqlDataHandler.GetDataContext())
            {
                var building   = cmbBuilding.SelectedItem as Building;
                var buildingId = building.ID;

                var bankDetails = context.SupplierBuildingSet
                                  .Include(a => a.Bank)
                                  .SingleOrDefault(a => a.BuildingId == buildingId && a.SupplierId == _Supplier.id);

                if (bankDetails == null)
                {
                    Controller.HandleError("Supplier banking details for this building is not configured.\n" +
                                           "Please capture bank details for this building on the suppier detail screen.", "Validation Error");
                    return;
                }

                var item = new tblRequisition()
                {
                    trnDate = _Item.TransactionDate,
                    PastelLedgerAutoNumber = _Item.AutoNumber,
                    PastelDataPath         = _Item.DataPath,
                    account       = _Item.AccountType,
                    reference     = building.Abbr + " (" + _Item.Account + ")",
                    ledger        = _Item.AccountDesc,
                    amount        = _Item.Amount,
                    payreference  = txtPaymentRef.Text,
                    userID        = Controller.user.id,
                    building      = buildingId,
                    SupplierId    = _Supplier == null ? (int?)null : _Supplier.id,
                    InvoiceNumber = txtInvoiceNumber.Text,
                    InvoiceDate   = dtInvoiceDate.Value,
                    BankName      = _Supplier == null ? (string)null : bankDetails.Bank.Name,
                    BranchCode    = _Supplier == null ? (string)null : bankDetails.BranceCode,
                    BranchName    = _Supplier == null ? (string)null : bankDetails.BranchName,
                    AccountNumber = _Supplier == null ? (string)null : bankDetails.AccountNumber,
                    processed     = true
                };

                context.tblRequisitions.Add(item);
                foreach (var key in _Documents.Keys)
                {
                    context.RequisitionDocumentSet.Add(new RequisitionDocument()
                    {
                        Requisition = item,
                        FileData    = _Documents[key],
                        FileName    = Path.GetFileName(key),
                        IsInvoice   = true
                    });
                }

                var config = (from c in context.BuildingMaintenanceConfigurationSet.Include(a => a.Building)
                              where c.BuildingId == item.building &&
                              c.PastelAccountNumber == _Item.Account
                              select c).SingleOrDefault();

                if (config != null)
                {
                    if (item.SupplierId == null)
                    {
                        Controller.HandleError("Supplier required for Maintenance. Please select a supplier.", "Validation Error");
                        return;
                    }

                    //capture the maintenance as part of the same unit of work
                    var frmMaintenance = new frmMaintenanceDetail(context, item, config);
                    var dialogResult   = frmMaintenance.ShowDialog();

                    if (dialogResult == DialogResult.OK)
                    {
                        context.SaveChanges();
                    }
                }
                else
                {
                    context.SaveChanges();
                }
                _Data.Remove(_Item);
                ClearItem();
                BindDataGrid();
            }
        }