private void RemoveWork(BuildingComparer bc, Dictionary<string, object> block)
        {
            List<Dictionary<string, object>> works = FixType(block["works"]);

            block["works"] = works;

            for (int i = 0; i < works.Count; i++)
            {
                Dictionary<string, object> work = (Dictionary<string, object>)works[i];

                if ((int)work["wId"] != bc.WorkId)
                    continue;

                RemoveGroup(bc, work);

                if (((IList)work["groups"]).Count == 0)
                    works.RemoveAt(i);
                break;
            }
        }
 public void RemoveEntry(BuildingComparer bc)
 {
     RemoveProject(bc);
 }
        private void RemoveProject(BuildingComparer bc)
        {
            if (!_data.Contains("projects"))
                return;

            List<Dictionary<string, object>> projects = FixType(_data["projects"]);

            _data["projects"] = projects;

            for (int i = 0; i < projects.Count; i++)
            {
                Dictionary<string, object> project = (Dictionary<string, object>)projects[i];

                if ((int)project["pId"] != bc.ProjectId)
                    continue;

                RemoveBlock(bc, project);

                if (((IList)project["blocks"]).Count == 0)
                    projects.RemoveAt(i);
                break;
            }
        }
        private void RemoveSubitem(BuildingComparer bc, Dictionary<string, object> item)
        {
            List<Dictionary<string, object>> subitems = FixType(item["subitems"]);

            item["subitems"] = subitems;

            for (int i=0; i<subitems.Count; i++)
            {
                Dictionary<string, object> subitem = (Dictionary<string, object>)subitems[i];

                if ((int)subitem["sId"] != bc.SubItemId)
                    continue;

                subitems.RemoveAt(i);
                break;
            }
        }
        private void RemoveItem(BuildingComparer bc, Dictionary<string, object> group)
        {
            List<Dictionary<string, object>> items = FixType(group["items"]);

            group["items"] = items;

            for (int i = 0; i < items.Count; i++)
            {
                Dictionary<string, object> item = (Dictionary<string, object>)items[i];

                if ((int)item["iId"] != bc.ItemId)
                    continue;

                RemoveSubitem(bc, item);

                if (((IList)item["subitems"]).Count == 0)
                    items.RemoveAt(i);
                break;
            }
        }
        private void RemoveGroup(BuildingComparer bc, Dictionary<string, object> work)
        {
            List<Dictionary<string, object>> groups = FixType(work["groups"]);

            work["groups"] = groups;

            for (int i = 0; i < groups.Count; i++)
            {
                Dictionary<string, object> group = (Dictionary<string, object>)groups[i];

                if ((int)group["gId"] != bc.GroupId)
                    continue;

                RemoveItem(bc, group);

                if (((IList)group["items"]).Count == 0)
                    groups.RemoveAt(i);
                break;
            }
        }
        private void RemoveBlock(BuildingComparer bc, Dictionary<string, object> project)
        {
            List<Dictionary<string, object>> blocks = FixType(project["blocks"]);

            project["blocks"] = blocks;

            for (int i = 0; i < blocks.Count; i++)
            {
                Dictionary<string, object> block = (Dictionary<string, object>)blocks[i];

                if ((int)block["bId"] != bc.BlockId)
                    continue;

                RemoveWork(bc, block);

                if (((IList)block["works"]).Count == 0)
                    blocks.RemoveAt(i);
                break;
            }
        }
        private void PopulateDetail(Contract contract)
        {
            BuildingComparer current = new BuildingComparer();
            BuildingComparer previous = new BuildingComparer();
            string projectName = string.Empty;
            string blockName = string.Empty;
            string workName = string.Empty;
            string groupName = string.Empty;
            string arrow = Resources.BuildingMonitorResources.SeparatorLR;
            TableRow row = null;
            TableCell cell = null;

            foreach (ContractDetail detail in contract.Detail)
            {
                current.Set(detail.ProjectId, detail.BlockId, detail.WorkId, detail.GroupId, detail.ItemId, detail.SubItemId);

                if (!current.IsProject(previous))
                    projectName = Project.Create(detail.ProjectId).Name;

                if (!current.IsBlock(previous))
                    blockName = Block.Create(detail.ProjectId, detail.BlockId).Name;

                if (!current.IsWork(previous))
                    workName = Work.Create(detail.ProjectId, detail.BlockId, detail.WorkId).Name;

                if (!current.IsGroup(previous))
                {
                    groupName = Group.Create(detail.ProjectId, detail.BlockId, detail.WorkId, detail.GroupId).Name;
                    row = new TableRow();
                    row.CssClass = "bm-group-hd";
                    cell = new TableCell();
                    row.Cells.Add(cell);
                    cell.ColumnSpan = 4;
                    cell.Text = projectName + arrow + blockName + arrow + workName + arrow + groupName;
                    tblContractDetail.Rows.Add(row);
                }

                row = new TableRow();
                cell = new TableCell();
                row.Cells.Add(cell);
                cell.Text = "&nbsp;";
                cell = new TableCell();
                row.Cells.Add(cell);
                cell.Text = detail.Name;
                cell = new TableCell();
                row.Cells.Add(cell);
                cell.Text = Helpers.Formatter.Decimal(detail.Quantity) + " " + detail.Unit;
                cell.CssClass = "bm-number";
                cell = new TableCell();
                row.Cells.Add(cell);
                cell.Text = Helpers.Formatter.Decimal(detail.Price);
                cell.CssClass = "bm-number";
                tblContractDetail.Rows.Add(row);
                previous.CopyFrom(current);
            }

            row = new TableRow();
            row.TableSection = TableRowSection.TableFooter;

            cell = new TableCell();
            cell.ColumnSpan = 3;
            cell.CssClass = "bm-label-total";
            cell.Text = "Total: ";
            row.Cells.Add(cell);

            cell = new TableCell();
            cell.CssClass = "bm-total bm-number";
            cell.Text = Helpers.Formatter.Decimal(contract.Amount);
            row.Cells.Add(cell);

            tblContractDetail.Rows.Add(row);
        }
        private void PopulateDetail()
        {
            BuildingComparer prevBc = new BuildingComparer();
            BuildingComparer currBc = new BuildingComparer();
            int counter = 0;
            int rowCounter = 0;

            foreach(ContractDetail detail in _contract.Detail)
            {
                TableRow row = new TableRow();
                TableCell cell = new TableCell();

                currBc.Set(detail.ProjectId, detail.BlockId, detail.WorkId, detail.GroupId, detail.ItemId, detail.SubItemId);

                if (!currBc.IsGroup(prevBc))
                {
                    cell.Text = string.Format("{1} {0} {2} {0} {3} {0} {4}",
                        Resources.BuildingMonitorResources.SeparatorLR,
                        Project.Create(detail.ProjectId).Name,
                        Block.Create(detail.ProjectId, detail.BlockId).Name,
                        Work.Create(detail.ProjectId, detail.BlockId, detail.WorkId).Name,
                        Group.Create(detail.ProjectId, detail.BlockId, detail.WorkId, detail.GroupId).Name);
                    cell.ColumnSpan = 6;
                    row.CssClass = "bm-group-hd";
                    row.Cells.Add(cell);
                    prevBc.CopyFrom(currBc);
                    tblContractDetail.Rows.AddAt(tblContractDetail.Rows.Count - 1, row);

                    cell = new TableCell();
                    row = new TableRow();
                    counter = 1;
                }

                cell.CssClass = "bm-number";
                cell.Text = (++rowCounter).ToInvariantString();
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = detail.Name;
                row.TableSection = TableRowSection.TableBody;
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.CssClass = "bm-number";
                cell.Text = Helpers.Formatter.Decimal(detail.Quantity) + " " + detail.Unit;
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.CssClass = "bm-number";
                cell.Text = detail.InitialProgress.ToInvariantString() + "%";
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.CssClass = "bm-number";
                cell.Text = Helpers.Formatter.Decimal(detail.Price);
                row.Cells.Add(cell);

                row.CssClass = counter++ % 2 == 0 ? "alternate" : string.Empty;
                tblContractDetail.Rows.AddAt(tblContractDetail.Rows.Count - 1, row);
            }
        }