context: //
  // DO NOT REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  //
  // @Authors:
  //       timop
  //
  // Copyright 2004-2011 by OM International
  //
  // This file is part of OpenPetra.org.
  //
  // OpenPetra.org is free software: you can redistribute it and/or modify
  // it under the terms of the GNU General Public License as published by
  // the Free Software Foundation, either version 3 of the License, or
  // (at your option) any later version.
  //
  // OpenPetra.org is distributed in the hope that it will be useful,
  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  // GNU General Public License for more details.
  //
  // You should have received a copy of the GNU General Public License
  // along with OpenPetra.org.  If not, see <http://www.gnu.org/licenses/>.
  //
  using System;
  using System.Data;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Windows.Forms;
  
  using Ict.Common;
  using Ict.Common.Data;
  using Ict.Common.Verification;
  
  using Ict.Petra.Client.App.Core;
  using Ict.Petra.Client.App.Core.RemoteObjects;
  using Ict.Petra.Client.App.Gui;
  using Ict.Petra.Client.CommonForms;
  using Ict.Petra.Client.MFinance.Logic;
  using Ict.Petra.Client.MPartner.Gui;
  using Ict.Petra.Client.MReporting.Gui.MFinance;
  
  using Ict.Petra.Shared;
  using Ict.Petra.Shared.MFinance;
  using Ict.Petra.Shared.MFinance.Gift.Data;
  using Ict.Petra.Shared.MPartner;
  
  namespace Ict.Petra.Client.MFinance.Gui.Gift
  {
      public partial class TFrmGiftBatch : IFrmPetraEditManual
      {
          private Int32 FLedgerNumber;
          private Boolean FViewMode = false;
  
          private GiftBatchTDS FViewModeTDS;
          private int standardTabIndex = 0;
          private bool FNewDonorWarning = true;
          private bool FWarnAboutMissingIntlExchangeRate = false;
          private eGiftTabs FPreviouslySelectedTab = eGiftTabs.None;
  
          // changed gift records
          GiftBatchTDSAGiftDetailTable FGiftDetailTable = null;
  
          // Variables that are used to select a specific batch on startup
          private Int32 FInitialBatchNumber = -1;
          private Int32 FInitialBatchYear = -1;
          private Int32 FInitialBatchPeriod = -1;
  
          private Boolean FLatestSaveIncludedForex = false;
  
          /// ViewMode is a special mode where the whole window with all tabs is in a readonly mode
          public bool ViewMode {
              get
              {
                  return FViewMode;
              }
              set
              {
                  FViewMode = value;
              }
          }
  
          /// ViewModeTDS is for injection of the Datasets in the View Mode
          public GiftBatchTDS ViewModeTDS
          {
              get
              {
                  return FViewModeTDS;
              }
              set
              {
                  FViewModeTDS = value;
              }
          }
  
          /// <summary>
          /// Set this property if you want to load the screen with an initial Year/Batch/Journal
          /// </summary>
          public Int32 InitialBatchYear
          {
              set
              {
                  FInitialBatchYear = value;
              }
              get
              {
                  return FInitialBatchYear;
              }
          }
  
          /// <summary>
          /// Set this property if you want to load the screen with an initial Year/Batch/Journal
          /// </summary>
          public Int32 InitialBatchPeriod
          {
              set
              {
                  FInitialBatchPeriod = value;
              }
              get
              {
                  return FInitialBatchPeriod;
              }
          }
  
          /// <summary>
          /// Set this property if you want to load the screen with an initial Year/Batch/Journal
          /// </summary>
          public Int32 InitialBatchNumber
          {
              set
              {
                  FInitialBatchNumber = value;
              }
              get
              {
                  return FInitialBatchNumber;
              }
          }
  
          /* Be sure to leave the LedgerNumber property in this position in the code
           * AFTER the ones above.   Otherwise the code will not work that opens this
           * screen at a pre-defined batch or in ViewMode.*/
  
          /// <summary>
          /// use this ledger
          /// </summary>
          public Int32 LedgerNumber
          {
              set
              {
                  FLedgerNumber = value;
  
                  // setting the ledger number on the batch screen will automatically trigger loading the batches for the current year
                  ucoBatches.LedgerNumber = value;
  
                  this.Text += " - " + TFinanceControls.GetLedgerNumberAndName(FLedgerNumber);
  
                  //Enable below if want code to run before standard Save() is executed
                  FPetraUtilsObject.DataSavingStarted += new TDataSavingStartHandler(FPetraUtilsObject_DataSavingStarted);
              }
          }
  
          /// <summary>
          /// Show the user a message when a gift is entered for a new donor
          /// </summary>
          public bool NewDonorWarning
          {
              set
              {
                  FNewDonorWarning = value;
              }
          }
  
          /// <summary>
          /// show the actual data of the database after server has changed data
          /// </summary>
          public void RefreshAll(bool AShowStatusDialogOnLoad = true, bool AIsMessageRefresh = false)
          {
              ucoBatches.RefreshAllData(AShowStatusDialogOnLoad, AIsMessageRefresh);
          }
  
          private void FileSaveManual(object sender, EventArgs e)
          {
              SaveChangesManual();
          }
  
          /// <summary>
          /// Check for ExWorkers before saving
          /// </summary>
          /// <returns>True if Save is successful</returns>
          public bool SaveChangesManual()
          {
              return SaveChangesManual(TExtraGiftBatchChecks.GiftBatchAction.SAVING);
          }
  
          /// <summary>
          /// Check for ExWorkers before saving or cancelling
          /// </summary>
          /// <returns>True if Save is successful</returns>
          public bool SaveChangesManual(TExtraGiftBatchChecks.GiftBatchAction AAction)
          {
              GetDataFromControls();
  
              // first alert the user to any recipients who are Ex-Workers
              if (TExtraGiftBatchChecks.CanContinueWithAnyExWorkers(AAction, FMainDS, FPetraUtilsObject))
              {
                  return SaveChanges();
              }
  
              return false;
          }
  
          /// <summary>
          /// Save AND close the form
          /// </summary>
          /// <param name="sender"></param>
          /// <param name="e"></param>
          public void FileSaveClose(object sender, EventArgs e)
          {
              if (SaveChanges() == true)
              {
                  FPetraUtilsObject.CloseFormCheckRun = false;
                  Close();
              }
          }
  
          /// <summary>
          /// Checks to be made before saving and posting
          /// </summary>
          /// <param name="APostingGiftDetails">GiftDetails for the batch that is to be posted</param>
          /// <param name="ACancelledDueToExWorkerOrAnonDonor">True if batch posting has been cancelled by the user because of an Ex-Worker recipient
          /// or an anonymous donor for a gift that is not marked as confidential</param>
          /// <returns>True if Save is successful</returns>
          public bool SaveChangesForPosting(DataTable APostingGiftDetails, out bool ACancelledDueToExWorkerOrAnonDonor)
          {
              GetDataFromControls();
  
              // first alert the user to any recipients who are Ex-Workers
              ACancelledDueToExWorkerOrAnonDonor = !TExtraGiftBatchChecks.CanContinueWithAnyExWorkers(TExtraGiftBatchChecks.GiftBatchAction.POSTING,
                  FMainDS,
                  FPetraUtilsObject,
                  APostingGiftDetails);
  
              // if save is continuing then alert the user to any gift that are not marked confidential but have an anonymous donor
              if (!ACancelledDueToExWorkerOrAnonDonor)
              {
                  ACancelledDueToExWorkerOrAnonDonor = !TExtraGiftBatchChecks.CanContinueWithAnyAnonymousDonors(FMainDS);
              }
  
              if (!ACancelledDueToExWorkerOrAnonDonor)
              {
                  return SaveChanges();
              }
  
              return false;
          }
  
          // Before the dataset is saved, check for correlation between batch and transactions
          private void FPetraUtilsObject_DataSavingStarted(object Sender, EventArgs e)
          {
              ucoBatches.CheckBeforeSaving();
              ucoTransactions.CheckBeforeSaving();
  
              if (FNewDonorWarning)
              {
                  FPetraUtilsObject_DataSavingStarted_NewDonorWarning();
              }
          }
  
          // This manual method lets us peek at the data that is about to be saved...
          // The data has already been collected from the contols and validated and there is definitely something to save...
          private TSubmitChangesResult StoreManualCode(ref GiftBatchTDS SubmitDS, out TVerificationResultCollection VerificationResult)
          {
              FLatestSaveIncludedForex = false;
  
  
gt:             if (SubmitDS.AGiftBatch != null)