Example #1
0
        void resetAll()
        {
            grvLink.FocusedRowHandle = -1;
            decimal totalNC = 0;
            string  sql     = "update C_QuotationDetail set TotalNC = 0 where C_QuotationID = " + C_QuotationID;

            LibQLSX.ExcuteSQL(sql);
            using (WaitDialogForm fWait = new WaitDialogForm("Vui lòng chờ trong giây lát...", "Đang xử lý..."))
            {
                for (int i = 0; i < grvLink.RowCount; i++)
                {
                    int id = TextUtils.ToInt(grvLink.GetRowCellValue(i, colID));
                    if (id == 0)
                    {
                        return;
                    }

                    int costID            = TextUtils.ToInt(grvLink.GetRowCellValue(i, colC_CostID));
                    int quotationDetailID = TextUtils.ToInt(grvLink.GetRowCellValue(i, colC_QuotationDetailID));
                    C_QuotationDetailModel quotationDetail = (C_QuotationDetailModel)C_QuotationDetailBO.Instance.FindByPK(quotationDetailID);
                    C_CostModel            cost            = (C_CostModel)C_CostBO.Instance.FindByPK(costID);

                    C_CostProductGroupLinkModel group = (C_CostProductGroupLinkModel)C_CostProductGroupLinkBO.Instance.FindByExpression(new Expression("C_CostID", cost.ID)
                                                                                                                                        .And(new Expression("C_ProductGroupID", quotationDetail.C_ProductGroupID)))[0];

                    decimal pricePerDay = TextUtils.ToDecimal(grvLink.GetRowCellValue(i, colPricePerDay));
                    decimal qty         = TextUtils.ToDecimal(grvLink.GetRowCellValue(i, colQty));

                    C_CostQuotationItemLinkModel link = (C_CostQuotationItemLinkModel)C_CostQuotationItemLinkBO.Instance.FindByPK(id);
                    link.PersonNumber = group.PersonNumber;// TextUtils.ToDecimal(drs[0]["PersonNumber"]);
                    if (group.IsFix == 1)
                    {
                        link.NumberDay = group.NumberDay;// TextUtils.ToDecimal(drs[0]["NumberDay"]);
                    }
                    else
                    {
                        link.NumberDay = group.VtuPercent * quotationDetail.PriceVT / cost.Price;
                    }
                    link.TotalR = link.NumberDay * link.PersonNumber;
                    if (cost.ID == 61)//phòng thiết kế
                    {
                        link.TotalR *= (1 + 0.3M * (quotationDetail.Qty - 1)) / quotationDetail.Qty;
                    }
                    link.Price = link.TotalR * cost.Price;
                    C_CostQuotationItemLinkBO.Instance.Update(link);

                    quotationDetail.TotalNC += link.Price;
                    C_QuotationDetailBO.Instance.Update(quotationDetail);
                }
                IsSaved = true;
            }
            MessageBox.Show("Reset thành công.", TextUtils.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Example #2
0
        public static decimal CalculateCostPB(C_QuotationDetailModel item, C_QuotationModel thisQotation, decimal price)
        {
            decimal   totalPB            = 0;
            ArrayList arr                = C_CostBO.Instance.FindByExpression(new Expression("IsUse", 1).And(new Expression("IsWithProject", 0)).And(new Expression("IsDirectCost", 0)));
            DataTable dtCostProductGroup = LibQLSX.Select("select * from vC_CostProductGroupLink where [C_ProductGroupID] = " + item.C_ProductGroupID);

            foreach (var c in arr)
            {
                C_CostModel cost = (C_CostModel)c;
                DataRow[]   drs  = dtCostProductGroup.Select("C_CostID = " + cost.ID);
                if (drs.Length == 0)
                {
                    continue;
                }
                C_CostQuotationItemLinkModel link = new C_CostQuotationItemLinkModel();

                ArrayList arrLink = C_CostQuotationItemLinkBO.Instance.FindByExpression(new Expression("C_CostID", cost.ID).And(new Expression("C_QuotationDetailID", item.ID)));
                if (arrLink != null && arrLink.Count > 0)
                {
                    link = (C_CostQuotationItemLinkModel)arrLink[0];
                }

                link.C_CostID            = cost.ID;
                link.C_QuotationDetailID = item.ID;
                link.IsDirect            = cost.IsDirectCost;
                if (link.IsDirect == 0)
                {
                    decimal valuePercent = TextUtils.ToDecimal(drs[0]["ValuePercent"]);

                    link.Price = valuePercent * item.Qty * price;
                    if (cost.IsDeliveryTime == 1)
                    {
                        link.Price *= thisQotation.DeliveryTime / 288;
                    }

                    if (cost.ID == 72 || cost.ID == 73)
                    {
                        if (cost.ID != thisQotation.DepartmentId)
                        {
                            link.Price = 0;
                        }
                    }
                    totalPB += link.Price;
                }
            }
            return(totalPB);
        }
Example #3
0
        private void resetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int id = TextUtils.ToInt(grvLink.GetFocusedRowCellValue(colID));

            if (id == 0)
            {
                return;
            }

            int costID            = TextUtils.ToInt(grvLink.GetFocusedRowCellValue(colC_CostID));
            int quotationDetailID = TextUtils.ToInt(grvLink.GetFocusedRowCellValue(colC_QuotationDetailID));
            C_QuotationDetailModel quotationDetail = (C_QuotationDetailModel)C_QuotationDetailBO.Instance.FindByPK(quotationDetailID);
            C_CostModel            cost            = (C_CostModel)C_CostBO.Instance.FindByPK(costID);

            C_CostProductGroupLinkModel group = (C_CostProductGroupLinkModel)C_CostProductGroupLinkBO.Instance.FindByExpression(new Expression("C_CostID", cost.ID)
                                                                                                                                .And(new Expression("C_ProductGroupID", quotationDetail.C_ProductGroupID)))[0];

            decimal pricePerDay = TextUtils.ToDecimal(grvLink.GetFocusedRowCellValue(colPricePerDay));
            decimal qty         = TextUtils.ToDecimal(grvLink.GetFocusedRowCellValue(colQty));

            C_CostQuotationItemLinkModel link = (C_CostQuotationItemLinkModel)C_CostQuotationItemLinkBO.Instance.FindByPK(id);

            quotationDetail.TotalNC -= link.Price;
            link.PersonNumber        = group.PersonNumber;// TextUtils.ToDecimal(drs[0]["PersonNumber"]);
            if (group.IsFix == 1)
            {
                link.NumberDay = group.NumberDay;// TextUtils.ToDecimal(drs[0]["NumberDay"]);
            }
            else
            {
                link.NumberDay = group.VtuPercent * quotationDetail.PriceVT / cost.Price;
            }
            link.TotalR = link.NumberDay * link.PersonNumber;
            if (cost.ID == 61)//phòng thiết kế
            {
                link.TotalR *= (1 + 0.3M * (quotationDetail.Qty - 1)) / quotationDetail.Qty;
            }
            link.Price = link.TotalR * cost.Price;
            C_CostQuotationItemLinkBO.Instance.Update(link);

            quotationDetail.TotalNC += link.Price;
            C_QuotationDetailBO.Instance.Update(quotationDetail);
            loadData();
            MessageBox.Show("Reset thành công.", TextUtils.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
 private void grvData_KeyDown(object sender, KeyEventArgs e)
 {
     if (grvData.SelectedRowsCount < 1)
     {
         return;
     }
     if (e.KeyCode == Keys.Delete)
     {
         try
         {
             string moduleCode = TextUtils.ToString(grvData.GetFocusedRowCellValue(colModuleCode));
             int    id         = TextUtils.ToInt(grvData.GetFocusedRowCellValue(colDetailID));
             if (C_QuotationDetailBO.Instance.CheckExist("ParentID", id))
             {
                 MessageBox.Show("Bạn không thể xóa được thiết bị chính khi nó còn chứa các thiết bị con.", TextUtils.Caption, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                 return;
             }
             if (MessageBox.Show("Bạn có chắc muốn xóa thiết bị [" + moduleCode + "]?", TextUtils.Caption,
                                 MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
             {
                 C_QuotationDetailModel item = (C_QuotationDetailModel)C_QuotationDetailBO.Instance.FindByPK(id);
                 if (item.ParentID > 0)
                 {
                     C_QuotationDetailModel itemP = (C_QuotationDetailModel)C_QuotationDetailBO.Instance.FindByPK(item.ParentID);
                     itemP.PriceVTSX -= item.PriceVTSX;
                     itemP.PriceVTLD -= item.PriceVTLD;
                     itemP.PriceVTPS -= item.PriceVTPS;
                     itemP.PriceVTTN -= item.PriceVTTN;
                     itemP.PriceVT   -= item.PriceVT;
                     C_QuotationDetailBO.Instance.Update(itemP);
                 }
                 C_QuotationDetailBO.Instance.Delete(id);
                 grvData.DeleteSelectedRows();
                 if (item.ParentID == 0)
                 {
                     loadProduct();
                 }
             }
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.Message);
         }
     }
 }
        private void btnPhanBo_Click(object sender, EventArgs e)
        {
            bool isSaved = false;

            if (treeData.AllNodesCount == 0)
            {
                return;
            }

            for (int i = 0; i < treeData.AllNodesCount; i++)
            {
                int parentID = TextUtils.ToInt(treeData.GetNodeByVisibleIndex(i).GetValue(colParentID));
                if (parentID > 0)
                {
                    continue;
                }
                int     id       = TextUtils.ToInt(treeData.GetNodeByVisibleIndex(i).GetValue(colDetailID));
                decimal totalHD  = TextUtils.ToDecimal(treeData.GetNodeByVisibleIndex(i).GetValue(colPriceHD));
                decimal totalTPA = TextUtils.ToDecimal(treeData.GetNodeByVisibleIndex(i).GetValue(colPriceTPA));

                DataTable dtChild = LibQLSX.Select("select * from vC_QuotationDetail where ParentID = " + id);
                if (dtChild.Rows.Count > 0)
                {
                    foreach (DataRow row in dtChild.Rows)
                    {
                        int cID = TextUtils.ToInt(row["ID"]);
                        C_QuotationDetailModel child = (C_QuotationDetailModel)C_QuotationDetailBO.Instance.FindByPK(cID);
                        //child.PriceHD = totalHD * child.PriceTPA * child.QtyT / totalTPA;
                        child.PriceHD = totalHD * child.PriceTPA / totalTPA;
                        C_QuotationDetailBO.Instance.Update(child);
                    }
                }

                C_QuotationDetailModel parent = (C_QuotationDetailModel)C_QuotationDetailBO.Instance.FindByPK(id);
                parent.PriceHD = totalHD;
                C_QuotationDetailBO.Instance.Update(parent);
                isSaved = true;
            }
            if (isSaved)
            {
                loadQuotationItem();
            }
        }
Example #6
0
        public static void CalculatePriceTPA(C_QuotationDetailModel item, C_QuotationModel thisQotation, decimal costTSLN, decimal costVT, decimal percentPB)
        {
            //Công thức chung
            //Gb = VTU + CPDA.NC  + CPTKE + CPDA.TKHAI + (1-1/(1+%VAT))*Gb  + %PB*Gb(1-%KH) + 1.15*%KH*Gb + 1.15*tienMat- %PB*tienMat + %LN*Gb

            if (item.ParentID == 0 && item.C_ProductGroupID == 0)
            {
                return;
            }
            decimal nhanCongTrucTiep = item.TotalNC;
            decimal costCustomer     = thisQotation.TotalCustomer;
            decimal priceTPA         = 0;
            decimal tienMat          = thisQotation.CustomerCash * item.PriceVT / costVT;

            priceTPA = (item.PriceVT + nhanCongTrucTiep + (costCustomer * item.PriceVT / costVT) + tienMat * (1.15M - percentPB))
                       / Math.Abs((1 - (1 - 1 / (1 + item.VAT / 100)) - percentPB + thisQotation.CustomerPercent / 100 * (percentPB - 1.15M) - costTSLN / 100));

            item.PriceTPA = priceTPA;
            C_QuotationDetailBO.Instance.Update(item);
        }
        private void btnShowDirectCost_Click(object sender, EventArgs e)
        {
            int id = TextUtils.ToInt(treeData.FocusedNode.GetValue(colDetailID));

            if (id == 0)
            {
                return;
            }
            C_QuotationDetailModel item = (C_QuotationDetailModel)C_QuotationDetailBO.Instance.FindByPK(id);

            frmDirectCost frm = new frmDirectCost();

            frm.QuotationDetail = item;
            frm.C_QuotationID   = Quotation.ID;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                if (frm.IsSaved)
                {
                    btnSave_Click(null, null);
                }
            }
        }
 protected C_QuotationDetailFacade(C_QuotationDetailModel model) : base(model)
 {
 }
        void calculateCost(C_QuotationDetailModel item, decimal costVT)
        {
            if (item.C_ProductGroupID == 0)
            {
                C_CostQuotationItemLinkBO.Instance.DeleteByAttribute("C_QuotationDetailID", item.ID);
                item.TotalNC = 0;
                item.TotalPB = 0;
                C_QuotationDetailBO.Instance.Update(item);
            }
            else
            {
                //decimal priceThucThu = (item.PriceHD - Quotation.CustomerPercent / 100 * item.PriceHD - Quotation.CustomerCash * item.PriceVT / costVT);

                ArrayList arr = C_CostBO.Instance.FindByExpression(new Expression("IsUse", 1).And(new Expression("IsWithProject", 0)));
                DataTable dtCostProductGroup = LibQLSX.Select("select * from vC_CostProductGroupLink where [C_ProductGroupID] = " + item.C_ProductGroupID);
                decimal   totalNC            = 0;
                decimal   totalPB            = 0;
                foreach (var c in arr)
                {
                    C_CostModel cost = (C_CostModel)c;
                    DataRow[]   drs  = dtCostProductGroup.Select("C_CostID = " + cost.ID);
                    if (drs.Length == 0)
                    {
                        continue;
                    }
                    C_CostQuotationItemLinkModel link = new C_CostQuotationItemLinkModel();

                    ArrayList arrLink = C_CostQuotationItemLinkBO.Instance.FindByExpression(new Expression("C_CostID", cost.ID).And(new Expression("C_QuotationDetailID", item.ID)));
                    if (arrLink != null && arrLink.Count > 0)
                    {
                        link = (C_CostQuotationItemLinkModel)arrLink[0];
                    }

                    link.C_CostID            = cost.ID;
                    link.C_QuotationDetailID = item.ID;
                    link.IsDirect            = cost.IsDirectCost;
                    if (link.IsDirect == 1)
                    {
                        #region Chi phí nhân công
                        if (link.ID == 0)
                        {
                            if (cost.Price == 0)
                            {
                                link.Price = 0;
                            }
                            else
                            {
                                link.PersonNumber = TextUtils.ToDecimal(drs[0]["PersonNumber"]);
                                int isFix = TextUtils.ToInt(drs[0]["IsFix"]);
                                if (isFix == 1)
                                {
                                    link.NumberDay = TextUtils.ToDecimal(drs[0]["NumberDay"]);
                                }
                                else
                                {
                                    link.NumberDay = TextUtils.ToDecimal(drs[0]["VtuPercent"]) * item.PriceVT / cost.Price;
                                }
                                link.TotalR = link.NumberDay * link.PersonNumber;
                                if (cost.ID == 61)//phòng thiết kế
                                {
                                    link.TotalR = link.NumberDay * link.PersonNumber * (1 + 0.3M * (item.Qty - 1)) / item.Qty;
                                    //link.Price = (link.NumberDay * link.PersonNumber) * (1 + 0.3M * (item.Qty - 1)) / item.Qty * cost.Price;
                                }
                                link.Price = link.TotalR * cost.Price;
                            }
                        }

                        totalNC += link.Price;
                        #endregion
                    }
                    else
                    {
                        #region Chi phí phân bổ
                        if (item.PriceHD > 0)
                        {
                            decimal valuePercent = TextUtils.ToDecimal(drs[0]["ValuePercent"]);

                            link.Price = valuePercent / 100 * item.PriceReal;
                            if (cost.IsDeliveryTime == 1)
                            {
                                link.Price *= Quotation.DeliveryTime / 288;
                            }

                            if (cost.ID == 72 || cost.ID == 73)
                            {
                                if (cost.ID != Quotation.DepartmentId)
                                {
                                    link.Price = 0;
                                }
                            }
                            totalPB += link.Price;
                        }
                        #endregion
                    }

                    if (link.ID == 0)
                    {
                        C_CostQuotationItemLinkBO.Instance.Insert(link);
                    }
                    else
                    {
                        C_CostQuotationItemLinkBO.Instance.Update(link);
                    }

                    item.TotalNC = totalNC;
                    item.TotalPB = totalPB;
                    C_QuotationDetailBO.Instance.Update(item);
                }
            }
        }
        private void treeData_KeyDown(object sender, KeyEventArgs e)
        {
            if (treeData.FocusedNode == null)
            {
                return;
            }
            if (e.KeyCode == Keys.Delete)
            {
                if (Quotation.IsApproved)
                {
                    return;
                }
                try
                {
                    string moduleCode = TextUtils.ToString(treeData.FocusedNode.GetValue(colModuleCode));
                    int    id         = TextUtils.ToInt(treeData.FocusedNode.GetValue(colDetailID));
                    if (C_QuotationDetailBO.Instance.CheckExist("ParentID", id))
                    {
                        MessageBox.Show("Bạn không thể xóa được thiết bị chính khi nó còn chứa các thiết bị con.", TextUtils.Caption, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        return;
                    }
                    if (MessageBox.Show("Bạn có chắc muốn xóa thiết bị [" + moduleCode + "]?", TextUtils.Caption,
                                        MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        C_QuotationDetailModel item = (C_QuotationDetailModel)C_QuotationDetailBO.Instance.FindByPK(id);
                        if (item.ParentID > 0)
                        {
                            C_QuotationDetailModel itemP = (C_QuotationDetailModel)C_QuotationDetailBO.Instance.FindByPK(item.ParentID);
                            itemP.PriceVTSX -= item.PriceVTSX;
                            itemP.PriceVTLD -= item.PriceVTLD;
                            itemP.PriceVTPS -= item.PriceVTPS;
                            itemP.PriceVTTN -= item.PriceVTTN;
                            itemP.PriceVT   -= item.PriceVT;
                            itemP.PriceHD   -= item.PriceHD;
                            C_QuotationDetailBO.Instance.Update(itemP);
                        }

                        C_CostQuotationItemLinkBO.Instance.DeleteByAttribute("C_QuotationDetailID", id);
                        C_QuotationDetailBO.Instance.Delete(id);

                        treeData.DeleteSelectedNodes();
                        if (item.ParentID == 0)
                        {
                            loadProduct();
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

            if (e.KeyCode == (Keys.C))
            {
                try
                {
                    string text = TextUtils.ToString(treeData.FocusedNode.GetValue(treeData.FocusedColumn));
                    Clipboard.SetText(text);
                }
                catch (Exception)
                {
                }
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            bool _isSaved = false;

            if (treeData.AllNodesCount == 0)
            {
                return;
            }
            treeData.ExpandAll();
            treeData.FocusedNode = null;

            using (WaitDialogForm fWait = new WaitDialogForm("Vui lòng chờ trong giây lát...", "Đang xử lý..."))
            {
                for (int i = 0; i < treeData.AllNodesCount; i++)
                {
                    int id = TextUtils.ToInt(treeData.GetNodeByVisibleIndex(i).GetValue(colDetailID));
                    C_QuotationDetailModel item = (C_QuotationDetailModel)C_QuotationDetailBO.Instance.FindByPK(id);

                    item.ModuleCode = TextUtils.ToString(treeData.GetNodeByVisibleIndex(i).GetValue(colModuleCode));
                    item.ModuleName = TextUtils.ToString(treeData.GetNodeByVisibleIndex(i).GetValue(colModuleName));
                    item.PriceVTLD  = TextUtils.ToDecimal(treeData.GetNodeByVisibleIndex(i).GetValue(colPriceVTLD));
                    item.PriceVTPS  = TextUtils.ToDecimal(treeData.GetNodeByVisibleIndex(i).GetValue(colPriceVTPS));
                    item.PriceVTSX  = TextUtils.ToDecimal(treeData.GetNodeByVisibleIndex(i).GetValue(colPriceVTSX));
                    item.PriceVTTN  = TextUtils.ToDecimal(treeData.GetNodeByVisibleIndex(i).GetValue(colPriceVTTN));

                    item.PriceVT = item.PriceVTSX + item.PriceVTLD + item.PriceVTPS + item.PriceVTTN;

                    item.PriceHD = TextUtils.ToDecimal(treeData.GetNodeByVisibleIndex(i).GetValue(colPriceHD));

                    item.C_ProductGroupID = TextUtils.ToInt(treeData.GetNodeByVisibleIndex(i).GetValue(colProductGroupID));
                    item.VAT  = TextUtils.ToDecimal(treeData.GetNodeByVisibleIndex(i).GetValue(colVAT));
                    item.QtyT = TextUtils.ToDecimal(treeData.GetNodeByVisibleIndex(i).GetValue(colQtyT));

                    int parentID = TextUtils.ToInt(treeData.GetNodeByVisibleIndex(i).GetValue(colParentID));
                    if (parentID == 0)
                    {
                        item.Qty = item.QtyT;
                    }
                    else
                    {
                        C_QuotationDetailModel parent = (C_QuotationDetailModel)C_QuotationDetailBO.Instance.FindByPK(parentID);
                        item.Qty = item.QtyT * parent.Qty;
                    }

                    C_QuotationDetailBO.Instance.Update(item);
                }

                DataTable dtCostVT = LibQLSX.Select("select sum(Qty*PriceVT) from C_QuotationDetail where (ParentID > 0 or (ParentID = 0 and C_ProductGroupID > 0)) and [C_QuotationID] = " + Quotation.ID);
                decimal   costVT   = dtCostVT.Rows.Count > 0 ? TextUtils.ToDecimal(dtCostVT.Rows[0][0]) : 0;

                for (int i = 0; i < treeData.AllNodesCount; i++)
                {
                    int id = TextUtils.ToInt(treeData.GetNodeByVisibleIndex(i).GetValue(colDetailID));
                    C_QuotationDetailModel item = (C_QuotationDetailModel)C_QuotationDetailBO.Instance.FindByPK(id);

                    if (item.ParentID == 0 && item.C_ProductGroupID == 0)
                    {
                        continue;
                    }

                    DataTable dtProductGroup = LibQLSX.Select("select ProfitPercent, CustomerPercent from C_ProductGroup where ID = " + item.C_ProductGroupID);
                    decimal   profitPercent  = dtProductGroup.Rows.Count > 0 ? TextUtils.ToDecimal(dtProductGroup.Rows[0]["ProfitPercent"]) / 100 : 0;

                    decimal customerPercent = 0;
                    if (Quotation.IsVAT < 2)
                    {
                        customerPercent = dtProductGroup.Rows.Count > 0 ? TextUtils.ToDecimal(dtProductGroup.Rows[0]["CustomerPercent"]) / 100 : 0;
                    }

                    decimal percentPB        = LibQLSX.GetPercentCostPB(item.C_ProductGroupID, Quotation.DeliveryTime) / 100;
                    decimal priceTPA         = 0;
                    decimal nhanCongTrucTiep = item.TotalNC;
                    decimal costTrienKhai    = Quotation.TotalCustomer * item.PriceVT / costVT;
                    decimal tienMat          = Quotation.CustomerCash * item.PriceVT / costVT;
                    decimal percentKH        = Quotation.CustomerPercent / 100;

                    //Gsx = CP + CP * %LN
                    //Gsx = VT + NC + TrienKhai + %PB * Gsx + (VT + NC + TrienKhai + %PB * Gsx) * %LN
                    //Gsx = (VT+NC+TrienKhai)(1+%LN) + Gsx(%PB+%PB*%LN)
                    //Gsx - Gsx(%PB+%PB*%LN) = (VT+NC+TrienKhai)(1+%LN)
                    //Gsx = (VT+NC+TrienKhai)(1+%LN)/(1-%PB-%PB*%LN)
                    //decimal priceSX = (item.PriceVT + nhanCongTrucTiep + costTrienKhai) * (1 + profitPercent) / (1 - percentPB - percentPB * profitPercent);
                    //priceTPA = (priceSX + (1 + customerPercent) * tienMat) / (1 - (1 + customerPercent) * percentKH - (1 - 1 / (1 + item.VAT / 100)));

                    //Gb = VTU + CPDA.NC  + CPDA.TKHAI + (1-1/(1+%VAT))*Gb + (tienMat+%KH*Gb) + %PB*(Gb-(tienMat+percentKH*Gb)) + 0.15*(tienMat+%KH*Gb) + %LN*Gb
                    //Gb = (VTU + CPDA.NC  + CPDA.TKHAI +(1.15- %PB)* tienMat)/ (1/(1+%VAT) - %PB + %PB*%KH -1.15*%KH - %LN)

                    decimal priceTPA1 = (item.PriceVT + nhanCongTrucTiep + costTrienKhai + tienMat + customerPercent * tienMat - percentPB * tienMat - tienMat * profitPercent) //percentKH * item.PriceHD
                                        / (1 - (1 - 1 / (1 + item.VAT / 100)) - percentKH - percentPB - profitPercent + percentKH * percentPB + percentKH * profitPercent);

                    if (item.PriceHD == 0)
                    {
                        decimal priceTPA2 = priceTPA1 + (customerPercent * percentKH * priceTPA1)
                                            / (1 - (1 - 1 / (1 + item.VAT / 100)) - percentKH - percentPB - profitPercent + percentKH * percentPB + percentKH * profitPercent);
                        item.PriceHD = priceTPA2;
                    }

                    priceTPA = priceTPA1 + (customerPercent * percentKH * item.PriceHD)
                               / (1 - (1 - 1 / (1 + item.VAT / 100)) - percentKH - percentPB - profitPercent + percentKH * percentPB + percentKH * profitPercent);

                    item.PriceReal = item.PriceHD - (percentKH * item.PriceHD + tienMat);
                    item.TotalCP   = (item.PriceVT + nhanCongTrucTiep + costTrienKhai) + percentPB * item.PriceReal;
                    item.PriceSX   = item.TotalCP;
                    //decimal pricePBTPA = percentPB * (priceTPA - tienMat - percentKH * priceTPA);
                    //priceTPA += percentPB * item.PriceReal - pricePBTPA;

                    item.PriceTPA    = priceTPA;
                    item.TotalProfit = profitPercent * item.PriceReal;
                    C_QuotationDetailBO.Instance.Update(item);

                    calculateCost(item, costVT);
                }

                ArrayList listParent = C_QuotationDetailBO.Instance.FindByExpression(new Expression("ParentID", 0)
                                                                                     .And(new Expression("C_ProductGroupID", 0))
                                                                                     .And(new Expression("C_QuotationID", Quotation.ID)));

                if (listParent != null)
                {
                    foreach (var item in listParent)
                    {
                        C_QuotationDetailModel itemP = (C_QuotationDetailModel)item;
                        DataTable dtChild            = LibQLSX.Select("select Sum(QtyT*PriceVTLD),Sum(QtyT*PriceVTPS),Sum(QtyT*PriceVTSX),Sum(QtyT*PriceVTTN),Sum(QtyT*PriceTPA),Sum(QtyT*PriceHD) from C_QuotationDetail where ParentID = " + itemP.ID);

                        itemP.PriceVTLD = dtChild.Rows.Count > 0 ? TextUtils.ToDecimal(dtChild.Rows[0][0]) : itemP.PriceVTLD;
                        itemP.PriceVTPS = dtChild.Rows.Count > 0 ? TextUtils.ToDecimal(dtChild.Rows[0][1]) : itemP.PriceVTPS;
                        itemP.PriceVTSX = dtChild.Rows.Count > 0 ? TextUtils.ToDecimal(dtChild.Rows[0][2]) : itemP.PriceVTSX;
                        itemP.PriceVTTN = dtChild.Rows.Count > 0 ? TextUtils.ToDecimal(dtChild.Rows[0][3]) : itemP.PriceVTTN;
                        itemP.PriceTPA  = dtChild.Rows.Count > 0 ? TextUtils.ToDecimal(dtChild.Rows[0][4]) : itemP.PriceTPA;
                        itemP.PriceHD   = dtChild.Rows.Count > 0 ? TextUtils.ToDecimal(dtChild.Rows[0][5]) : itemP.PriceHD;
                        itemP.PriceVT   = itemP.PriceVTSX + itemP.PriceVTLD + itemP.PriceVTPS + itemP.PriceVTTN;

                        C_QuotationDetailBO.Instance.Update(itemP);
                    }
                }

                loadQuotationItem();
                _isSaved = true;
            }

            MessageBox.Show("Lưu trữ thành công.", TextUtils.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);

            if (_isSaved)
            {
                if (this.LoadDataChange != null)
                {
                    this.LoadDataChange(null, null);
                }
            }
        }
        private void btnAddModule_Click(object sender, EventArgs e)
        {
            #region Check
            if (txtModuleCode.Text.Trim() == "")
            {
                MessageBox.Show("Bạn phải thêm mã module!", TextUtils.Caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (txtModuleName.Text.Trim() == "")
            {
                MessageBox.Show("Bạn phải thêm tên module!", TextUtils.Caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (txtManufacture.Text.Trim() == "")
            {
                MessageBox.Show("Bạn phải thêm hãng!", TextUtils.Caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (txtOrigin.Text.Trim() == "")
            {
                MessageBox.Show("Bạn phải thêm xuất xứ!", TextUtils.Caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (TextUtils.ToDecimal(txtPriceVTSX.EditValue) == 0 && cboProduct.EditValue != null)
            {
                MessageBox.Show("Bạn phải điền giá vật tư sản xuất!", TextUtils.Caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (TextUtils.ToDecimal(txtQtyT.EditValue) == 0)
            {
                MessageBox.Show("Bạn phải điền số lượng!", TextUtils.Caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (cboProductGroup.EditValue == null && cboProduct.EditValue != null)
            {
                MessageBox.Show("Bạn phải chọn một nhóm sản phẩm!", TextUtils.Caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            #endregion

            C_QuotationDetailModel item = new C_QuotationDetailModel();

            item.C_QuotationID = Quotation.ID;
            item.ParentID      = TextUtils.ToInt(cboProduct.EditValue);
            if (item.ParentID > 0)
            {
                item.C_ProductGroupID = TextUtils.ToInt(cboProductGroup.EditValue);
                item.PriceVTSX        = TextUtils.ToDecimal(txtPriceVTSX.EditValue);
                item.PriceVTLD        = TextUtils.ToDecimal(txtPriceVTLD.EditValue);
                item.PriceVTPS        = TextUtils.ToDecimal(txtPriceVTPS.EditValue);
                item.PriceVTTN        = TextUtils.ToDecimal(txtPriceVTTN.EditValue);
                item.PriceVT          = item.PriceVTSX + item.PriceVTLD + item.PriceVTPS + item.PriceVTTN;
                item.QtyT             = TextUtils.ToDecimal(txtQtyT.EditValue);
                item.Qty = TextUtils.ToDecimal(txtQtyT.EditValue) * TextUtils.ToDecimal(grvCboProduct.GetFocusedRowCellValue(colPQty));
            }
            else
            {
                item.Qty = item.QtyT = TextUtils.ToDecimal(txtQtyT.EditValue);
            }

            item.Manufacture = txtManufacture.Text.Trim();
            item.ModuleCode  = item.ModuleCodeHD = txtModuleCode.Text.Trim().ToUpper();
            item.ModuleName  = item.ModuleNameHD = txtModuleName.Text.Trim();
            item.Origin      = txtOrigin.Text.Trim();

            item.VAT = TextUtils.ToDecimal(txtVAT.EditValue);

            item.ID = (int)C_QuotationDetailBO.Instance.Insert(item);

            if (item.ParentID > 0)
            {
                C_QuotationDetailModel itemP = (C_QuotationDetailModel)C_QuotationDetailBO.Instance.FindByPK(item.ParentID);
                itemP.PriceVTSX += item.PriceVTSX;
                itemP.PriceVTLD += item.PriceVTLD;
                itemP.PriceVTPS += item.PriceVTPS;
                itemP.PriceVTTN += item.PriceVTTN;
                itemP.PriceVT   += item.PriceVT;
                C_QuotationDetailBO.Instance.Update(itemP);

                //calculateCost(item);
            }

            MessageBox.Show("Lưu trữ thành công.", TextUtils.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);

            loadProduct();
            loadQuotationItem();

            txtPriceVTSX.EditValue = 0;
            txtQtyT.EditValue      = 1;
            txtModuleName.Text     = txtModuleCode.Text = "";
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            bool _isSaved = false;

            if (grvData.RowCount == 0)
            {
                return;
            }
            grvData.FocusedRowHandle = -1;

            using (WaitDialogForm fWait = new WaitDialogForm("Vui lòng chờ trong giây lát...", "Đang xử lý..."))
            {
                for (int i = 0; i < grvData.RowCount; i++)
                {
                    int id = TextUtils.ToInt(grvData.GetRowCellValue(i, colDetailID));
                    C_QuotationDetailModel item = (C_QuotationDetailModel)C_QuotationDetailBO.Instance.FindByPK(id);

                    item.PriceVTLD = TextUtils.ToDecimal(grvData.GetRowCellValue(i, colPriceVTLD));
                    item.PriceVTPS = TextUtils.ToDecimal(grvData.GetRowCellValue(i, colPriceVTPS));
                    item.PriceVTSX = TextUtils.ToDecimal(grvData.GetRowCellValue(i, colPriceVTSX));
                    item.PriceVTTN = TextUtils.ToDecimal(grvData.GetRowCellValue(i, colPriceVTTN));
                    item.PriceVT   = item.PriceVTSX + item.PriceVTLD + item.PriceVTPS + item.PriceVTTN;

                    item.PriceHD = TextUtils.ToDecimal(grvData.GetRowCellValue(i, colPriceHDnoVAT));

                    item.C_ProductGroupID = TextUtils.ToInt(grvData.GetRowCellValue(i, colProductGroupID));
                    item.VAT = TextUtils.ToDecimal(grvData.GetRowCellValue(i, colVAT));
                    item.Qty = TextUtils.ToDecimal(grvData.GetRowCellValue(i, colQty));

                    C_QuotationDetailBO.Instance.Update(item);

                    calculateCost(item);

                    //LibQLSX.CalculatePriceTPA(item, Quotation);
                }

                for (int i = 0; i < grvData.RowCount; i++)
                {
                    int id = TextUtils.ToInt(grvData.GetRowCellValue(i, colParentID));
                    if (id == 0)
                    {
                        continue;
                    }
                    C_QuotationDetailModel item = (C_QuotationDetailModel)C_QuotationDetailBO.Instance.FindByPK(id);
                    DataTable dtChildTPA        = LibQLSX.Select("select Sum(PriceTPA) from C_QuotationDetail where ParentID = " + id);
                    DataTable dtChildHD         = LibQLSX.Select("select Sum(PriceHD) from C_QuotationDetail where ParentID = " + id);

                    item.PriceTPA = dtChildTPA.Rows.Count > 0 ? TextUtils.ToDecimal(dtChildTPA.Rows[0][0]) : item.PriceTPA;
                    item.PriceHD  = dtChildHD.Rows.Count > 0 ? TextUtils.ToDecimal(dtChildHD.Rows[0][0]) : item.PriceHD;
                    C_QuotationDetailBO.Instance.Update(item);
                }

                loadQuotationItem();
                _isSaved = true;
            }

            MessageBox.Show("Lưu trữ thành công.", TextUtils.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);

            if (_isSaved)
            {
                if (this.LoadDataChange != null)
                {
                    this.LoadDataChange(null, null);
                }
            }
        }