Example #1
0
        private void btn_Convet_Click(object sender, EventArgs e)
        {
            try
            {
                List <string> Goods        = new List <string>();
                List <string> strOldGoods  = new List <string>();
                int           nKind        = 0; //计算有几种原物料
                int           nLen         = dataGridView1.Rows.Count;
                string        strStockName = "";
                string        strStockCode = "";

                for (int i = 0; i < nLen - 1; i++)
                {
                    object obj = dataGridView1.Rows[i].Cells["选择"].Value;
                    //if (Convert.ToString(obj) == "True" || Convert.ToString(obj) == "1")
                    //{
                    string strGoods = dataGridView1.Rows[i].Cells["物料"].Value.ToString();
                    string strQty   = dataGridView1.Rows[i].Cells["实用数量"].Value.ToString();
                    string strYLQty = dataGridView1.Rows[i].Cells["数量"].Value.ToString();
                    strStockCode = dataGridView1.Rows[i].Cells["仓库编码"].Value.ToString();
                    strStockName = dataGridView1.Rows[i].Cells["仓库名称"].Value.ToString();

                    //string strPc = dataGridView1.Rows[i].Cells["批次"].Value.ToString();
                    string strPrice = dataGridView1.Rows[i].Cells["价格"].Value.ToString();
                    string strName  = dataGridView1.Rows[i].Cells["物料名称"].Value.ToString();
                    string strUnit  = dataGridView1.Rows[i].Cells["单位"].Value.ToString();

                    if (Convert.ToDecimal(strQty) > Convert.ToDecimal(strYLQty))
                    {
                        lblTS.Text = "系统提示:物料:" + strName + "实用数量不能大于库存数量";
                        return;
                    }
                    if (Convert.ToDecimal(strQty) == 0 || Convert.ToDecimal(strQty) < 0)
                    {
                        lblTS.Text = "系统提示:物料:" + strName + "实用数量不能为0或者负数";
                        return;
                    }

                    if (Jud(strGoods, txtGoodsCode.Text))
                    {
                        if (strOldGoods.Contains(strGoods))
                        {
                        }
                        else
                        {
                            nKind = nKind + 1;
                            strOldGoods.Add(strGoods);
                        }
                        bool bRet = false;
                        for (int j = 0; j < Goods.Count; j++)
                        {
                            string   strTemp      = Goods[j].ToString();
                            string[] str          = strTemp.Split(',');
                            string   strTempGoods = str[0].ToString();
                            string   strTempPc    = str[2].ToString();
                            string   strTempQty   = str[3].ToString();
                            if (strTempGoods == strGoods)
                            {
                                decimal dQty = Convert.ToDecimal(strQty) + Convert.ToDecimal(strTempQty);
                                Goods[j] = strGoods + "," + dQty.ToString() + "," + strPrice + "," + strName + "," + strUnit;
                                bRet     = true;
                            }
                        }
                        if (bRet == false)
                        {
                            Goods.Add(strGoods + "," + strQty + "," + strPrice + "," + strName + "," + strUnit);
                        }
                        //}
                        else
                        {
                            lblTS.Text = "系统提示:选择的物料生成不了下一个物料,请核对";
                            return;
                        }
                    }
                }

                if (!Jud2(nKind, txtGoodsCode.Text))
                {
                    lblTS.Text = "系统提示:选择的转换前的物料不够";
                    return;
                }


                Mes_OrgResHeadBLL      OrgResHeadBLL      = new Mes_OrgResHeadBLL();
                Mes_OrgResDetailBLL    OrgResDetailBLL    = new Mes_OrgResDetailBLL();
                Mes_OrgResHeadEntity   OrgResHeadEntity   = new Mes_OrgResHeadEntity();
                Mes_OrgResDetailEntity OrgResDetailEntity = new Mes_OrgResDetailEntity();

                string strIn_No = "";

                MesMaterInHeadBLL MaterInHeadBLL = new MesMaterInHeadBLL();
                strIn_No = MaterInHeadBLL.GetDH("组装与拆分单");

                OrgResHeadEntity.O_OrgResNo = strIn_No;
                OrgResHeadEntity.O_OrderNo  = "";

                OrgResHeadEntity.O_CreateBy     = Globels.strUser;
                OrgResHeadEntity.O_CreateDate   = DateTime.Now;
                OrgResHeadEntity.O_OrderDate    = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                OrgResHeadEntity.O_Remark       = "";
                OrgResHeadEntity.O_Status       = 1;
                OrgResHeadEntity.O_StockCode    = strStockCode;
                OrgResHeadEntity.O_StockName    = strStockName;
                OrgResHeadEntity.O_WorkShopCode = Globels.strWorkShop;
                OrgResHeadEntity.O_WorkShopName = Globels.strWorkShopName;
                OrgResHeadEntity.O_Record       = "";
                OrgResHeadEntity.O_ProCode      = Globels.strProce;
                OrgResHeadEntity.O_TeamCode     = Globels.strTeam;
                OrgResHeadEntity.O_TeamName     = Globels.strTeamName;

                int     nRow      = OrgResHeadBLL.SaveEntity("", OrgResHeadEntity);
                decimal dSecPrice = 0;
                decimal dTotal    = 0;
                for (int i = 0; i < Goods.Count; i++)
                {
                    string[] strTemp = Goods[i].ToString().Split(',');
                    dTotal = dTotal + (Convert.ToDecimal(strTemp[2].ToString()) * Convert.ToDecimal(strTemp[1].ToString()));
                }
                dSecPrice = dTotal / Convert.ToDecimal(txtQty.Text);


                for (int i = 0; i < Goods.Count; i++)
                {
                    OrgResDetailEntity.O_OrgResNo     = strIn_No;
                    OrgResDetailEntity.O_SecGoodsCode = txtGoodsCode.Text;
                    OrgResDetailEntity.O_SecGoodsName = txtGoodsName.Text;
                    OrgResDetailEntity.O_SecPrice     = 0;
                    OrgResDetailEntity.O_SecQty       = Convert.ToDecimal(txtQty.Text);
                    OrgResDetailEntity.O_SecUnit      = m_strUnit;
                    OrgResDetailEntity.O_SecBatch     = txtPc.Text;

                    string[] strTemp = Goods[i].ToString().Split(',');

                    string strGoodsCode = strTemp[0].ToString();
                    //Mes_ConvertBLL ConvertBLL = new Mes_ConvertBLL();
                    //var Convert_rows = ConvertBLL.GetList_Mes_Convert(" where C_SecCode = '" + strGoodsCode + "'");
                    //string strC_GoodsCode = "";
                    //if (Convert_rows.Count > 0)
                    //{
                    //    strC_GoodsCode = Convert_rows[0].C_Code;
                    //}
                    MesInventoryBLL InventoryBLL  = new MesInventoryBLL();
                    var             Inventory_row = InventoryBLL.GetData(" where I_StockCode = '" + Globels.strStockCode + "' and I_GoodsCode = '" + strGoodsCode + "' and I_Qty > 0 order by I_Batch");
                    if (Inventory_row.Count > 0)
                    {
                        Decimal dTotalQty = Convert.ToDecimal(strTemp[1].ToString());
                        for (int j = 0; j < Inventory_row.Count; j++)
                        {
                            if (dTotalQty > 0)
                            {
                                if (dTotalQty < Inventory_row[j].I_Qty)
                                {
                                    OrgResDetailEntity.O_GoodsCode = strTemp[0].ToString();
                                    OrgResDetailEntity.O_GoodsName = strTemp[3].ToString();
                                    OrgResDetailEntity.O_Price     = Convert.ToDecimal(strTemp[2].ToString());
                                    OrgResDetailEntity.O_Qty       = dTotalQty;
                                    OrgResDetailEntity.O_Unit      = strTemp[4].ToString();
                                    OrgResDetailEntity.O_Batch     = Inventory_row[j].I_Batch;
                                    OrgResDetailEntity.O_SecPrice  = dSecPrice;
                                    nRow = OrgResDetailBLL.SaveEntity("", OrgResDetailEntity);
                                    //UpdateData(Inventory_row[j].ID, Inventory_row[j].I_Qty - dTotalQty);
                                    break;
                                }
                                else
                                {
                                    OrgResDetailEntity.O_GoodsCode = strTemp[0].ToString();
                                    OrgResDetailEntity.O_GoodsName = strTemp[3].ToString();
                                    OrgResDetailEntity.O_Price     = Convert.ToDecimal(strTemp[2].ToString());
                                    OrgResDetailEntity.O_Qty       = Inventory_row[j].I_Qty;
                                    OrgResDetailEntity.O_Unit      = strTemp[4].ToString();
                                    OrgResDetailEntity.O_Batch     = Inventory_row[j].I_Batch;
                                    OrgResDetailEntity.O_SecPrice  = dSecPrice;
                                    nRow      = OrgResDetailBLL.SaveEntity("", OrgResDetailEntity);
                                    dTotalQty = dTotalQty - Inventory_row[j].I_Qty;
                                    //DeleteData(Inventory_row[j].ID);
                                }
                            }
                        }
                    }

                    //OrgResDetailEntity.O_GoodsCode = strTemp[0].ToString();
                    //OrgResDetailEntity.O_GoodsName = strTemp[4].ToString();
                    //OrgResDetailEntity.O_Price = Convert.ToDecimal(strTemp[3].ToString());
                    //OrgResDetailEntity.O_Qty = Convert.ToDecimal(strTemp[1].ToString());
                    //OrgResDetailEntity.O_Unit = strTemp[5].ToString();
                    //OrgResDetailEntity.O_Batch = strTemp[2].ToString();
                    //OrgResDetailEntity.O_SecPrice = dSecPrice;
                    //nRow = OrgResDetailBLL.SaveEntity("", OrgResDetailEntity);
                }
                Upload(strIn_No);
                MessageBox.Show("保存成功");
                lblTS.Text = "";
                //UpdatePrice(strSecGoods, dSecPrice);  已经在存储过程中处理
                Delete();
                //Update();
                this.Close();
            }
            catch (Exception ex)
            {
                lblTS.Text = ex.ToString();
                return;
            }
        }
Example #2
0
        /// <summary>
        /// 保存实体数据
        /// </summary>
        /// <param name="keyValue">主键</param>
        /// <param name="entity">实体</param>
        /// <returns>返回值大于0:操作成功</returns>
        public int SaveEntity(string keyValue, Mes_OrgResHeadEntity entity)
        {
            try
            {
                var strSql    = new StringBuilder();
                var paramList = new List <SqlParameter>();
                if (string.IsNullOrEmpty(keyValue))
                {
                    strSql.Append("INSERT INTO Mes_OrgResHead(");
                    strSql.Append("ID,");
                    strSql.Append("O_OrgResNo,");
                    strSql.Append("O_Record,");
                    strSql.Append("O_ProCode,");
                    strSql.Append("O_StockCode,");
                    strSql.Append("O_StockName,");
                    strSql.Append("O_WorkShopCode,");
                    strSql.Append("O_WorkShopName,");
                    strSql.Append("O_OrderNo,");
                    strSql.Append("O_OrderDate,");
                    strSql.Append("O_Status,");
                    strSql.Append("O_CreateBy,");
                    strSql.Append("O_CreateDate,");
                    strSql.Append("O_TeamCode,");
                    strSql.Append("O_TeamName,");
                    strSql.Append("O_Remark");
                    strSql.Append(")");
                    strSql.Append(" VALUES (");
                    strSql.Append("@ID,");
                    strSql.Append("@O_OrgResNo,");
                    strSql.Append("@O_Record,");
                    strSql.Append("@O_ProCode,");
                    strSql.Append("@O_StockCode,");
                    strSql.Append("@O_StockName,");
                    strSql.Append("@O_WorkShopCode,");
                    strSql.Append("@O_WorkShopName,");
                    strSql.Append("@O_OrderNo,");
                    strSql.Append("@O_OrderDate,");
                    strSql.Append("@O_Status,");

                    strSql.Append("@O_CreateBy,");
                    strSql.Append("@O_CreateDate,");
                    strSql.Append("@O_TeamCode,");
                    strSql.Append("@O_TeamName,");
                    strSql.Append("@O_Remark");
                    strSql.Append(")");
                    paramList.Add(new SqlParameter("@ID", Guid.NewGuid().ToString()));
                }
                else
                {
                }

                paramList.Add(new SqlParameter("@O_OrgResNo", entity.O_OrgResNo));
                paramList.Add(new SqlParameter("@O_Record", entity.O_Record));
                paramList.Add(new SqlParameter("@O_ProCode", entity.O_ProCode));
                paramList.Add(new SqlParameter("@O_StockCode", entity.O_StockCode));
                paramList.Add(new SqlParameter("@O_StockName", entity.O_StockName));
                paramList.Add(new SqlParameter("@O_WorkShopCode", entity.O_WorkShopCode));
                paramList.Add(new SqlParameter("@O_WorkShopName", entity.O_WorkShopName));
                paramList.Add(new SqlParameter("@O_OrderNo", entity.O_OrderNo));
                paramList.Add(new SqlParameter("@O_OrderDate", entity.O_OrderDate));
                paramList.Add(new SqlParameter("@O_Status", entity.O_Status));
                paramList.Add(new SqlParameter("@O_CreateBy", entity.O_CreateBy));
                paramList.Add(new SqlParameter("@O_CreateDate", entity.O_CreateDate));
                paramList.Add(new SqlParameter("@O_TeamCode", entity.O_TeamCode));
                paramList.Add(new SqlParameter("@O_TeamName", entity.O_TeamName));
                paramList.Add(new SqlParameter("@O_Remark", entity.O_Remark));
                var result = db.ExecuteNonQuery(strSql.ToString(), paramList.ToArray());
                return(result);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public ActionResult SaveForm(string keyValue, string strEntity, string detailList)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                var entityTemp = orgResMangerIBLL.GetMes_OrgResHeadEntity(keyValue);
                if (entityTemp.O_Status == ErpEnums.MaterInStatusEnum.Audit)
                {
                    return(Fail("该单据已审核,不能修改!"));
                }
            }
            Mes_OrgResHeadEntity entity = strEntity.ToObject <Mes_OrgResHeadEntity>();
            var mes_OrgResDetailList    = detailList.ToObject <List <Mes_OrgResDetailEntity> >();

            if (mes_OrgResDetailList.Any(c => c.O_Qty <= 0 || c.O_SecQty <= 0))
            {
                return(Fail("数量只能是大于0的实数"));
            }

            var groupList = mes_OrgResDetailList.GroupBy(c => new { c.O_GoodsCode, c.O_GoodsName }).ToList();

            foreach (var t in groupList)
            {
                var stockQty = stock.GetOrgGoodsList(entity.O_StockCode, t.Key.O_GoodsCode).ToList().Sum(c => c.O_Qty);
                var factQty  = t.ToList().Sum(c => c.O_Qty);
                if (factQty > stockQty)
                {
                    return(Fail("当前【" + t.Key.O_GoodsName + "】录入数量大于总库存" + stockQty));
                }
            }

            #region  去
            //var  list = mes_OrgResDetailList.GroupBy(c => c.O_GoodsCode).ToList();
            //foreach (var item in list)
            //{
            //    var useNum = mes_OrgResDetailList.Where(c => c.O_GoodsCode == item.Key).ToList().Sum(c => c.O_Qty);
            //    var stock = new Mes_WorkShopScanBLL().GetMes_WorkShopScanEntity(item.Key,item,);
            //    if (useNum>stock.W_Qty)
            //    {
            //        return Fail("【"+stock.W_GoodsName+"】当前使用数量大于库存");
            //    }
            //}
            #endregion

            //按照先进先出原则生成单据
            var dicGoods   = new Dictionary <string, List <Mes_OrgResDetailEntity> >(); //记录批次库存
            var goods_list = new List <Mes_OrgResDetailEntity>();                       //记录物料批次数据
            Mes_OrgResDetailEntity reqGoods = null;
            for (var i = 0; i < mes_OrgResDetailList.Count; i++)
            {
                if (dicGoods.ContainsKey(mes_OrgResDetailList[i].O_GoodsCode))
                {
                    //根据 物料编码和仓库获取所有库存
                    var tempStock = dicGoods[mes_OrgResDetailList[i].O_GoodsCode];
                    for (var j = 0; j < tempStock.Count; j++) //需求20个,两个批次分别为5个,7个
                    {
                        if (mes_OrgResDetailList[i].O_Qty < tempStock[j].O_Qty)
                        {
                            //品种组装前物料
                            reqGoods             = new Mes_OrgResDetailEntity();
                            reqGoods.O_GoodsCode = tempStock[j].O_GoodsCode;
                            reqGoods.O_GoodsName = tempStock[j].O_GoodsName;
                            reqGoods.O_Batch     = tempStock[j].O_Batch;
                            reqGoods.O_Qty       = mes_OrgResDetailList[i].O_Qty;
                            reqGoods.O_Unit      = mes_OrgResDetailList[i].O_Unit;
                            reqGoods.O_Price     = mes_OrgResDetailList[i].O_Price;
                            //组装产出物
                            reqGoods.O_SecGoodsCode = mes_OrgResDetailList[i].O_SecGoodsCode;
                            reqGoods.O_SecGoodsName = mes_OrgResDetailList[i].O_SecGoodsName;
                            reqGoods.O_SecBatch     = mes_OrgResDetailList[i].O_SecBatch;
                            reqGoods.O_SecPrice     = mes_OrgResDetailList[i].O_SecPrice;
                            reqGoods.O_SecUnit      = mes_OrgResDetailList[i].O_SecUnit;
                            reqGoods.O_SecQty       = mes_OrgResDetailList[i].O_SecQty;

                            goods_list.Add(reqGoods);
                            tempStock[j].O_Qty = tempStock[j].O_Qty - mes_OrgResDetailList[i].O_Qty;
                            break; //数量足够 跳出循环
                        }
                        reqGoods = new Mes_OrgResDetailEntity();
                        var qty = tempStock[j].O_Qty; //取全部
                        //拼装组装前物料
                        reqGoods.O_Qty       = qty;
                        reqGoods.O_Price     = mes_OrgResDetailList[i].O_Price;
                        reqGoods.O_GoodsCode = tempStock[j].O_GoodsCode;
                        reqGoods.O_GoodsName = tempStock[j].O_GoodsName;
                        reqGoods.O_Batch     = tempStock[j].O_Batch;
                        reqGoods.O_Unit      = mes_OrgResDetailList[i].O_Unit;

                        //拼装组装后产物
                        reqGoods.O_SecGoodsCode = mes_OrgResDetailList[i].O_SecGoodsCode;
                        reqGoods.O_SecGoodsName = mes_OrgResDetailList[i].O_SecGoodsName;
                        reqGoods.O_SecBatch     = mes_OrgResDetailList[i].O_SecBatch;
                        reqGoods.O_SecPrice     = mes_OrgResDetailList[i].O_SecPrice;
                        reqGoods.O_SecUnit      = mes_OrgResDetailList[i].O_SecUnit;
                        reqGoods.O_SecQty       = mes_OrgResDetailList[i].O_SecQty;

                        goods_list.Add(reqGoods);
                        tempStock.RemoveAt(j);
                        j--;
                        mes_OrgResDetailList[i].O_Qty = mes_OrgResDetailList[i].O_Qty - qty;
                    }
                }
                else
                {
                    //根据 物料编码和仓库获取所有库存
                    var stockList = stock.GetOrgGoodsList(entity.O_StockCode, mes_OrgResDetailList[i].O_GoodsCode).ToList();
                    for (var j = 0; j < stockList.Count; j++) //需求20个,两个批次分别为5个,7个
                    {
                        if (mes_OrgResDetailList[i].O_Qty < stockList[j].O_Qty)
                        {
                            //品种组装前物料
                            reqGoods             = new Mes_OrgResDetailEntity();
                            reqGoods.O_GoodsCode = stockList[j].O_GoodsCode;
                            reqGoods.O_GoodsName = stockList[j].O_GoodsName;
                            reqGoods.O_Batch     = stockList[j].O_Batch;
                            reqGoods.O_Qty       = mes_OrgResDetailList[i].O_Qty;
                            reqGoods.O_Unit      = mes_OrgResDetailList[i].O_Unit;
                            reqGoods.O_Price     = mes_OrgResDetailList[i].O_Price;
                            //组装产出物
                            reqGoods.O_SecGoodsCode = mes_OrgResDetailList[i].O_SecGoodsCode;
                            reqGoods.O_SecGoodsName = mes_OrgResDetailList[i].O_SecGoodsName;
                            reqGoods.O_SecBatch     = mes_OrgResDetailList[i].O_SecBatch;
                            reqGoods.O_SecPrice     = mes_OrgResDetailList[i].O_SecPrice;
                            reqGoods.O_SecUnit      = mes_OrgResDetailList[i].O_SecUnit;
                            reqGoods.O_SecQty       = mes_OrgResDetailList[i].O_SecQty;

                            goods_list.Add(reqGoods);
                            stockList[j].O_Qty = stockList[j].O_Qty - mes_OrgResDetailList[i].O_Qty;
                            break; //数量足够 跳出循环
                        }
                        reqGoods = new Mes_OrgResDetailEntity();
                        var qty = stockList[j].O_Qty; //取全部
                        //拼装组装前物料
                        reqGoods.O_Qty       = qty;
                        reqGoods.O_Price     = mes_OrgResDetailList[i].O_Price;
                        reqGoods.O_GoodsCode = stockList[j].O_GoodsCode;
                        reqGoods.O_GoodsName = stockList[j].O_GoodsName;
                        reqGoods.O_Batch     = stockList[j].O_Batch;
                        reqGoods.O_Unit      = mes_OrgResDetailList[i].O_Unit;

                        //拼装组装后产物
                        reqGoods.O_SecGoodsCode = mes_OrgResDetailList[i].O_SecGoodsCode;
                        reqGoods.O_SecGoodsName = mes_OrgResDetailList[i].O_SecGoodsName;
                        reqGoods.O_SecBatch     = mes_OrgResDetailList[i].O_SecBatch;
                        reqGoods.O_SecPrice     = mes_OrgResDetailList[i].O_SecPrice;
                        reqGoods.O_SecUnit      = mes_OrgResDetailList[i].O_SecUnit;
                        reqGoods.O_SecQty       = mes_OrgResDetailList[i].O_SecQty;

                        goods_list.Add(reqGoods);
                        stockList.RemoveAt(j);
                        j--;
                        mes_OrgResDetailList[i].O_Qty = mes_OrgResDetailList[i].O_Qty - qty;
                    }
                    dicGoods[mes_OrgResDetailList[i].O_GoodsCode] = stockList; //记录剩余批次库存
                }
            }
            orgResMangerIBLL.SaveEntity(keyValue, entity, goods_list);
            return(Success("保存成功!"));
        }
Example #4
0
        private void btn_upload_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否要提交", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == System.Windows.Forms.DialogResult.Yes)
            {
                try
                {
                    if (cmbTeam.Text == "")
                    {
                        lblTS.Text = "系统提示:先选择班组";
                        return;
                    }
                    Mes_WorkShopScanBLL   WorkShopScanBLL  = new Mes_WorkShopScanBLL();
                    Mes_WorkShopWeightBLL WorkShopWeightBL = new Mes_WorkShopWeightBLL();

                    List <string> insertList  = new List <string>();
                    string        strSecGoods = "";
                    string        strSecPc    = "";
                    string        strSecName  = "";
                    string        strSecUnit  = "";
                    decimal       dSecQty     = 0;
                    int           nLen2       = dataGridView2.Rows.Count;
                    for (int i = 0; i < nLen2; i++)
                    {
                        object obj = dataGridView2.Rows[i].Cells["选择2"].Value;
                        if (Convert.ToString(obj) == "True" || Convert.ToString(obj) == "1")
                        {
                            strSecGoods = dataGridView2.Rows[i].Cells["物料2"].Value.ToString();
                            strSecPc    = dataGridView2.Rows[i].Cells["批次2"].Value.ToString();
                            strSecName  = dataGridView2.Rows[i].Cells["物料名称2"].Value.ToString();
                            strSecUnit  = dataGridView2.Rows[i].Cells["单位2"].Value.ToString();

                            string strSecQty = dataGridView2.Rows[i].Cells["数量2"].Value.ToString();
                            string strTemp   = dataGridView2.Rows[i].Cells["物料2"].Value.ToString() + "," + dataGridView2.Rows[i].Cells["批次2"].Value.ToString() + "," + dataGridView2.Rows[i].Cells["数量2"].Value.ToString();
                            if (insertList.Count > 0)
                            {
                                string[] str = insertList[0].ToString().Split(',');
                                if (str[0].ToString() == strSecGoods)
                                {
                                    if (str[1].ToString() == strSecPc)
                                    {
                                        dSecQty = dSecQty + Convert.ToDecimal(strSecQty);
                                    }
                                    else
                                    {
                                        lblTS.Text = "系统提示:生成的物料只允许同一物料,同一批次";
                                        return;
                                    }
                                }
                                else
                                {
                                    lblTS.Text = "系统提示:生成的物料只允许同一物料,同一批次";
                                    return;
                                }
                            }
                            else
                            {
                                dSecQty = dSecQty + Convert.ToDecimal(strSecQty);
                                insertList.Add(strTemp);
                            }
                        }
                    }

                    List <string> Goods       = new List <string>();
                    List <string> strOldGoods = new List <string>();
                    int           nKind       = 0; //计算有几种原物料
                    int           nLen        = dataGridView1.Rows.Count;
                    for (int i = 0; i < nLen; i++)
                    {
                        object obj = dataGridView1.Rows[i].Cells["选择"].Value;
                        if (Convert.ToString(obj) == "True" || Convert.ToString(obj) == "1")
                        {
                            string strGoods = dataGridView1.Rows[i].Cells["物料"].Value.ToString();
                            string strQty   = dataGridView1.Rows[i].Cells["实用数量"].Value.ToString();
                            string strYLQty = dataGridView1.Rows[i].Cells["数量"].Value.ToString();

                            string strPc    = dataGridView1.Rows[i].Cells["批次"].Value.ToString();
                            string strPrice = dataGridView1.Rows[i].Cells["价格"].Value.ToString();
                            string strName  = dataGridView1.Rows[i].Cells["物料名称"].Value.ToString();
                            string strUnit  = dataGridView1.Rows[i].Cells["单位"].Value.ToString();
                            if (Jud(strGoods, strSecGoods))
                            {
                                if (strOldGoods.Contains(strGoods))
                                {
                                }
                                else
                                {
                                    nKind = nKind + 1;
                                    strOldGoods.Add(strGoods);
                                }
                                bool bRet = false;
                                for (int j = 0; j < Goods.Count; j++)
                                {
                                    string   strTemp      = Goods[j].ToString();
                                    string[] str          = strTemp.Split(',');
                                    string   strTempGoods = str[0].ToString();
                                    string   strTempPc    = str[2].ToString();
                                    string   strTempQty   = str[3].ToString();
                                    if (strTempGoods == strGoods && strTempPc == strPc)
                                    {
                                        decimal dQty = Convert.ToDecimal(strQty) + Convert.ToDecimal(strTempQty);
                                        Goods[j] = strGoods + "," + dQty.ToString() + "," + strPc + "," + strPrice + "," + strName + "," + strUnit;
                                        bRet     = true;
                                    }
                                }
                                if (bRet == false)
                                {
                                    Goods.Add(strGoods + "," + strQty + "," + strPc + "," + strPrice + "," + strName + "," + strUnit);
                                }
                            }
                            else
                            {
                                lblTS.Text = "系统提示:选择的物料生成不了下一个物料,请核对";
                                return;
                            }
                        }
                    }

                    if (!Jud2(nKind, strSecGoods))
                    {
                        lblTS.Text = "系统提示:选择的转换前的物料不够";
                        return;
                    }

                    if (insertList.Count == 0 || Goods.Count == 0)
                    {
                        lblTS.Text = "系统提示:请选择物料";
                        return;
                    }
                    Mes_OrgResHeadBLL      OrgResHeadBLL      = new Mes_OrgResHeadBLL();
                    Mes_OrgResDetailBLL    OrgResDetailBLL    = new Mes_OrgResDetailBLL();
                    Mes_OrgResHeadEntity   OrgResHeadEntity   = new Mes_OrgResHeadEntity();
                    Mes_OrgResDetailEntity OrgResDetailEntity = new Mes_OrgResDetailEntity();

                    string strIn_No = "";

                    MesMaterInHeadBLL MaterInHeadBLL = new MesMaterInHeadBLL();
                    strIn_No = MaterInHeadBLL.GetDH("组装与拆分单");

                    OrgResHeadEntity.O_OrgResNo = strIn_No;
                    OrgResHeadEntity.O_OrderNo  = comOrderNo.Text;

                    OrgResHeadEntity.O_CreateBy     = Globels.strUser;
                    OrgResHeadEntity.O_CreateDate   = DateTime.Now;
                    OrgResHeadEntity.O_OrderDate    = txtOrderDate.Text;
                    OrgResHeadEntity.O_Remark       = "";
                    OrgResHeadEntity.O_Status       = 1;
                    OrgResHeadEntity.O_WorkShopCode = cmbWorkShop.Text;
                    OrgResHeadEntity.O_WorkShopName = cmbWorkShopName.Text;
                    OrgResHeadEntity.O_Record       = cmbRecord.Text;
                    OrgResHeadEntity.O_ProCode      = cmbProce.Text;
                    OrgResHeadEntity.O_TeamCode     = cmbTeam.Text;
                    OrgResHeadEntity.O_TeamName     = cmbTeamName.Text;

                    int     nRow      = OrgResHeadBLL.SaveEntity("", OrgResHeadEntity);
                    decimal dSecPrice = 0;
                    decimal dTotal    = 0;
                    for (int i = 0; i < Goods.Count; i++)
                    {
                        string[] strTemp = Goods[i].ToString().Split(',');
                        dTotal = dTotal + (Convert.ToDecimal(strTemp[3].ToString()) * Convert.ToDecimal(strTemp[1].ToString()));
                    }
                    dSecPrice = dTotal / dSecQty;


                    for (int i = 0; i < Goods.Count; i++)
                    {
                        OrgResDetailEntity.O_OrgResNo     = strIn_No;
                        OrgResDetailEntity.O_SecGoodsCode = strSecGoods;
                        OrgResDetailEntity.O_SecGoodsName = strSecName;
                        OrgResDetailEntity.O_SecPrice     = 0;
                        OrgResDetailEntity.O_SecQty       = dSecQty;
                        OrgResDetailEntity.O_SecUnit      = strSecUnit;
                        OrgResDetailEntity.O_SecBatch     = strSecPc;

                        string[] strTemp = Goods[i].ToString().Split(',');

                        string         strGoodsCode   = strTemp[0].ToString();
                        Mes_ConvertBLL ConvertBLL     = new Mes_ConvertBLL();
                        var            Convert_rows   = ConvertBLL.GetList_Mes_Convert(" where C_SecCode = '" + strGoodsCode + "'");
                        string         strC_GoodsCode = "";
                        if (Convert_rows.Count > 0)
                        {
                            strC_GoodsCode = Convert_rows[0].C_Code;
                        }

                        OrgResDetailEntity.O_GoodsCode = strTemp[0].ToString();
                        OrgResDetailEntity.O_GoodsName = strTemp[4].ToString();
                        OrgResDetailEntity.O_Price     = Convert.ToDecimal(strTemp[3].ToString());
                        OrgResDetailEntity.O_Qty       = Convert.ToDecimal(strTemp[1].ToString());
                        OrgResDetailEntity.O_Unit      = strTemp[5].ToString();
                        OrgResDetailEntity.O_Batch     = strTemp[2].ToString();
                        OrgResDetailEntity.O_SecPrice  = dSecPrice;
                        nRow = OrgResDetailBLL.SaveEntity("", OrgResDetailEntity);
                    }
                    Upload(strIn_No);
                    MessageBox.Show("保存成功");
                    lblTS.Text = "";
                    //UpdatePrice(strSecGoods, dSecPrice);  已经在存储过程中处理
                    Delete();
                    UpdateGoods();
                }
                catch (Exception ex)
                {
                    lblTS.Text = "系统提示:" + ex.ToString();
                }
            }
        }