public void SetValues(TallyDO obj)
		{
			if(obj == null) { return; }
			Hotkey = obj.Hotkey;
			Description = obj.Description;
			IndicatorValue = obj.IndicatorValue;
			IndicatorType = obj.IndicatorType;
		}
		public TallyDO(TallyDO obj) : this()
		{
		    SetValues(obj);
		}
        public void PullNewTallyTable(ComponentFileVM comp)
        {
            StartJob("Add New CountTree Records");
            List<TallyDO> compTallies = comp.Database.Read<TallyDO>("Tally", null);

            foreach (TallyDO tally in compTallies)
            {
                CheckWorkerStatus();
                TallyDO match = Master.ReadSingleRow<TallyDO>("Tally", "WHERE HotKey = ? AND Description = ?",
                    tally.Hotkey, tally.Description);

                if (match == null)
                {
                    match = new TallyDO(Master);
                    match.Hotkey = tally.Hotkey;
                    match.Description = tally.Description;
                    match.Save();
                }
            }
        }
        public DialogResult ShowDialog(SGType sg)
        {
            this.SampleGroup = sg;

            this._sgInfo_LBL.Text = this.SampleGroup.Code + "-" + this.SampleGroup.TallyMethod.ToString();
            if ((sg.TallyMethod & CruiseDAL.Enums.TallyMode.BySampleGroup) == CruiseDAL.Enums.TallyMode.BySampleGroup)
            {
                this._species_LBL.Visible = false;
                this._speciesSelect_CB.Visible = false;
                TallyDO t = this.Controller._cDal.From<TallyDO>()
                    .Join("CountTree", "USING (Tally_CN)")
                    .Where("SampleGroup_CN = ?")
                    .Read(sg.SampleGroup_CN).FirstOrDefault();
                if (t == null)
                {
                    t = new TallyDO(this.Controller._cDal);
                    t.Description = sg.Code;
                }
                this.Tally = t;
            }
            else if ((sg.TallyMethod & CruiseDAL.Enums.TallyMode.BySpecies) == CruiseDAL.Enums.TallyMode.BySpecies)
            {
                this._species_LBL.Visible = true;
                this._speciesSelect_CB.Visible = true;
                this._speciesSelect_CB.DisplayMember = "Species";
                this._speciesSelect_CB.ValueMember = "Tag";

                foreach (TreeDefaultValueDO tdv in sg.TreeDefaultValues)
                {
                    TallyDO t = this.Controller._cDal.From<TallyDO>()
                        .Join("CountTree", "USING (Tally_CN)")
                        .Where("SampleGroup_CN = ? and TreeDefaultValue_CN = ?")
                        .Read(sg.SampleGroup_CN, tdv.TreeDefaultValue_CN).FirstOrDefault();
                    if (t == null)
                    {
                        t = new TallyDO(this.Controller._cDal);
                        t.Description = tdv.Species;
                    }
                    tdv.Tag = t;
                }

                this._speciesSelect_CB.DataSource = sg.TreeDefaultValues;
            }
            else
            {
            }

            return this.ShowDialog();
        }
 private void _speciesSelect_CB_SelectedValueChanged(object sender, EventArgs e)
 {
     TallyDO tally = _speciesSelect_CB.SelectedValue as TallyDO;
     if (tally == null) { return; }
     this.Tally = tally;
 }
        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            if (this.DialogResult == DialogResult.Cancel)
            {
                return;
            }
            else if (this.DialogResult == DialogResult.OK)
            {
                this._BS_Tally.EndEdit();
                try
                {
                    Cursor.Current = Cursors.WaitCursor;
                    this.Controller._cDal.BeginTransaction();
                    if ((this.SampleGroup.TallyMethod & CruiseDAL.Enums.TallyMode.BySampleGroup) == CruiseDAL.Enums.TallyMode.BySampleGroup)
                    {
                        if (this.Tally.IsPersisted == false)
                        {
                            TallyDO t = this.Controller._cDal.From<TallyDO>()
                                .Where("Description = ? and Hotkey = ?")
                                .Read(this.Tally.Description
                                , this.Tally.Hotkey).FirstOrDefault();
                            if (t != null) { this.Tally = t; }
                            else
                            {
                                this.Tally.Save();
                            }
                        }

                        string createCountsCommand = String.Format(@"INSERT OR IGNORE INTO CountTree (CuttingUnit_CN, SampleGroup_CN,  CreatedBy)
                        Select CuttingUnitStratum.CuttingUnit_CN, SampleGroup.SampleGroup_CN,  '{0}' AS CreatedBy
                        From SampleGroup
                        INNER JOIN CuttingUnitStratum
                        ON SampleGroup.Stratum_CN = CuttingUnitStratum.Stratum_CN
                        WHERE SampleGroup.SampleGroup_CN = {1};", this.Controller._cDal.User, this.SampleGroup.SampleGroup_CN);
                        this.Controller._cDal.Execute(createCountsCommand);
                        string setTallyRefCommand = String.Format("UPDATE CountTree SET Tally_CN = {0} WHERE SampleGroup_CN = {1} AND ifnull(TreeDefaultValue_CN, 0) = 0",
                            this.Tally.Tally_CN, this.SampleGroup.SampleGroup_CN);
                        this.Controller._cDal.Execute(setTallyRefCommand);
                    }
                    else if ((this.SampleGroup.TallyMethod & CruiseDAL.Enums.TallyMode.BySpecies) == CruiseDAL.Enums.TallyMode.BySpecies)
                    {
                        string creatCountsBySpeciesCommand = String.Format(@"INSERT  OR IGNORE INTO CountTree (CuttingUnit_CN, SampleGroup_CN, TreeDefaultValue_CN, CreatedBy)
                        Select CuttingUnitStratum.CuttingUnit_CN, SampleGroup.SampleGroup_CN, SampleGroupTreeDefaultValue.TreeDefaultValue_CN, '{0}' AS CreatedBy
                        From SampleGroup
                        INNER JOIN CuttingUnitStratum
                        ON SampleGroup.Stratum_CN = CuttingUnitStratum.Stratum_CN
                        INNER JOIN SampleGroupTreeDefaultValue
                        ON SampleGroupTreeDefaultValue.SampleGroup_CN = SampleGroup.SampleGroup_CN
                        WHERE SampleGroup.SampleGroup_CN = {1};", this.Controller._cDal.User, this.SampleGroup.SampleGroup_CN);
                        this.Controller._cDal.Execute(creatCountsBySpeciesCommand);

                        foreach (TreeDefaultValueDO tdv in this.SampleGroup.TreeDefaultValues)
                        {
                            TallyDO tally = tdv.Tag as TallyDO;
                            if (tally == null)
                            {
                                e.Cancel = true;
                                return;
                            }

                            if (tally.IsPersisted == false)
                            {
                                TallyDO t = this.Controller._cDal.From<TallyDO>()
                                    .Where("Description = ? and Hotkey = ?")
                                    .Read(tally.Description, tally.Hotkey).FirstOrDefault();
                                if (t != null) { tally = t; }
                                else
                                {
                                    tally.Save();
                                }
                            }

                            string setTallyRefCommand = String.Format("UPDATE CountTree SET Tally_CN = {0} WHERE SampleGroup_CN = {1} AND TreeDefaultValue_CN = {2}",
                                tally.Tally_CN, this.SampleGroup.SampleGroup_CN, tdv.TreeDefaultValue_CN);
                            this.Controller._cDal.Execute(setTallyRefCommand);
                        }
                    }
                    this.Controller._cDal.CommitTransaction();
                }
                catch (Exception ex)
                {
                    this.Controller._cDal.RollbackTransaction();
                    this.Controller.HandleNonCriticalException(ex, "Some thing went wrong while saving");
                }
                finally
                {
                    Cursor.Current = Cursors.Default;
                }
            }
        }