/// <summary> /// 计算产品的成本 /// </summary> /// <param name="strPRProduceCode"> 生产单编号 </param> /// <returns> 产品的成本 </returns> private decimal ComputeMaterialCost(string strPRProduceCode) { string strSql = null; DataTable dt = null; decimal decAvePrice; decimal decMaterialCost = 0; strSql = "Select InvenCode,UseQuantity From PRProduceItem Where PRProduceCode = '" + strPRProduceCode + "'"; try { dt = db.GetDataTable(strSql, "PRProduceItem"); foreach (DataRow dr in dt.Rows) { decAvePrice = commUse.GetAvePriceBySTGetMaterial(strPRProduceCode, dr["InvenCode"].ToString()); decMaterialCost += (decAvePrice * Convert.ToInt32(dr["UseQuantity"])); } } catch (Exception ex) { MessageBox.Show(ex.Message, "软件提示"); throw ex; } return(decMaterialCost); }
private void toolCheck_Click(object sender, EventArgs e) { List <string> strSqls = new List <string>(); SqlDataReader sdr = null; string strCode = null; string strSTGetMaterialSql = null; //表示提交STGetMaterial表的SQL语句 string strSTStockSql = null; //表示提交STStock表的SQL语句 string strPRProduceItemSql = null; //表示提交PRProduceItem表的SQL语句 string strSTGetCode = null; //单据编号 string strPRProduceCode = null; //生产单号 string strStoreCode = null; //仓库代码 string strInvenCode = null; //存货代码 string strIsFlag = null; //审核标记 int intQuantity; //退料单中的数量 decimal decUnitPrice; decimal decAvePrice; decimal decMoney; int intGetQuantity; //生产单子表的总计领用数量 int intStockQuantity; //库存剩余数量 strSTGetCode = dgvSTGetMaterialInfo["STGetCode", dgvSTGetMaterialInfo.CurrentCell.RowIndex].Value.ToString(); strPRProduceCode = dgvSTGetMaterialInfo["PRProduceCode", dgvSTGetMaterialInfo.CurrentCell.RowIndex].Value.ToString(); strStoreCode = dgvSTGetMaterialInfo["StoreCode", dgvSTGetMaterialInfo.CurrentCell.RowIndex].Value.ToString(); strInvenCode = dgvSTGetMaterialInfo["InvenCode", dgvSTGetMaterialInfo.CurrentCell.RowIndex].Value.ToString(); strIsFlag = dgvSTGetMaterialInfo["IsFlag", dgvSTGetMaterialInfo.CurrentCell.RowIndex].Value.ToString(); intQuantity = Convert.ToInt32(dgvSTGetMaterialInfo["Quantity", dgvSTGetMaterialInfo.CurrentCell.RowIndex].Value); if (strIsFlag == "1") { MessageBox.Show("该单据已审核,不许再次审核!", "软件提示"); return; } //处理库存 strCode = "SELECT Quantity,AvePrice,STMoney FROM STStock WHERE StoreCode = '" + strStoreCode + "' AND InvenCode = '" + strInvenCode + "'"; try { sdr = db.GetDataReader(strCode); sdr.Read(); if (!sdr.HasRows) { MessageBox.Show("该存货的库存未初始化,无法处理!", "软件提示"); sdr.Close(); return; } decUnitPrice = commUse.GetAvePriceBySTGetMaterial(strPRProduceCode, strInvenCode); //领料单中的平均单价 decMoney = sdr.GetDecimal(2) + Decimal.Round(decUnitPrice * intQuantity, 2); //总计金额 intStockQuantity = sdr.GetInt32(0) + intQuantity; //库存剩余数量 decAvePrice = Decimal.Round(decMoney / intStockQuantity, 2); //平均单价 strSTStockSql = "UPDATE STStock SET Quantity = " + intStockQuantity + ",STMoney = " + decMoney + ",AvePrice = " + decAvePrice + " "; strSTStockSql += "WHERE StoreCode = '" + strStoreCode + "' AND InvenCode = '" + strInvenCode + "'"; sdr.Close(); //生产单的子表 strCode = "SELECT GetQuantity FROM PRProduceItem WHERE PRProduceCode = '" + strPRProduceCode + "' AND InvenCode = '" + strInvenCode + "'"; sdr = db.GetDataReader(strCode); sdr.Read(); if (!sdr.HasRows) { MessageBox.Show("生产单数据异常,无法处理!", "软件提示"); sdr.Close(); return; } //剩余的退料量 if (sdr.GetInt32(0) < intQuantity) { MessageBox.Show("生产单数据异常(现存退料量小于本次退料量),无法处理!", "软件提示"); sdr.Close(); return; } intGetQuantity = sdr.GetInt32(0) - intQuantity; strPRProduceItemSql = "Update PRProduceItem Set GetQuantity = " + intGetQuantity + " Where PRProduceCode = '" + strPRProduceCode + "' AND InvenCode = '" + strInvenCode + "'"; sdr.Close(); //处理退料表 strSTGetMaterialSql = "UPDATE STGetMaterial SET UnitPrice = " + decUnitPrice + ",IsFlag = '1' WHERE STGetCode = '" + strSTGetCode + "'"; // strSqls.Add(strSTStockSql); strSqls.Add(strPRProduceItemSql); strSqls.Add(strSTGetMaterialSql); if (db.ExecDataBySqls(strSqls)) { MessageBox.Show("审核成功!", "软件提示"); } else { MessageBox.Show("审核失败!", "软件提示"); } } catch (Exception ex) { MessageBox.Show(ex.Message, "软件提示"); sdr.Close(); throw ex; } BindDataGridView(" Where BillType = '2'"); }