Example #1
0
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (!NP.ReqField(this.cbPrdOrder, "Please select Production Order first !!"))
            {
                return;
            }
            if (!NP.ReqField(this.txtQty, "Please enter Qty !!"))
            {
                return;
            }
            //if (decimal.Parse(lblPrdQty.Text.Trim()) < decimal.Parse(lblGrPrdQty.Text.Trim()) + decimal.Parse(txtQty.Text.Trim()))
            //{
            //    MessageBox.Show("Quantity must less than " + (decimal.Parse(lblPrdQty.Text.Trim()) - decimal.Parse(lblGrPrdQty.Text.Trim())).ToString(), "Warning !!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
            //    txtQty.Focus();
            //    txtQty.Text = (decimal.Parse(lblPrdQty.Text.Trim()) - decimal.Parse(lblGrPrdQty.Text.Trim())).ToString();
            //    return;
            //}
            if (NP.MSGB("Do you to Save GR Production ?") == DialogResult.Yes)
            {
                oConn = new SqlConnection(NP.ReadFileDB(NP_Cls.PathDB));
                if (oConn.State == ConnectionState.Open)
                {
                    oConn.Close();
                }
                oConn.Open(); SqlTransaction Tr;
                Tr = oConn.BeginTransaction();

                try
                {
                    SqlCommand cmdIns = new SqlCommand(); string strTmpBatch = NP_Cls._genBatch(this.lblMatCode.Text.Trim(), oConn, Tr);
                    NP_Cls.SqlInsert = "INSERT INTO t_StockMovement " +
                                       "(DocNumber, TranDate, MovementType,RefNumber, Remark, UserCreate, DateCreate,PlantCode,PlantName,BOMVersion,LocCode,LocName) " +
                                       "VALUES     (@GRPrdNumber,GETDATE(),@MovementType,@PrdONumber,@Remark,@UC,GETDATE(),@PlantCode,@PlantName,@BOMVersion,@LocCode,@LocName)";
                    cmdIns.Parameters.Add("@GRPrdNumber", SqlDbType.NVarChar, 12).Value = this.txtDocNo.Text.Trim();
                    cmdIns.Parameters.Add("@PrdONumber", SqlDbType.NVarChar, 12).Value  = this.cbPrdOrder.Text.Trim();
                    cmdIns.Parameters.Add("@MovementType", SqlDbType.NVarChar, 3).Value = this.lblMoveType.Text.Trim();
                    cmdIns.Parameters.Add("@GRPrdQty", SqlDbType.Decimal).Value         = Convert.ToDecimal(this.txtQty.Text.Trim());
                    cmdIns.Parameters.Add("@BatchNumber", SqlDbType.NVarChar, 10).Value = strTmpBatch;
                    cmdIns.Parameters.Add("@Remark", SqlDbType.NVarChar, 255).Value     = this.txtRemark.Text.Trim();
                    cmdIns.Parameters.Add("@UC", SqlDbType.NVarChar, 50).Value          = NP_Cls.strUsr;
                    cmdIns.Parameters.Add("@PlantCode", SqlDbType.NVarChar, 4).Value    = this.lblPlantCode.Text.Trim();
                    cmdIns.Parameters.Add("@PlantName", SqlDbType.NVarChar, 20).Value   = this.lblPlant.Text.Trim();
                    cmdIns.Parameters.Add("@LocCode", SqlDbType.NVarChar, 2).Value      = this.lblLocCode.Text.Trim();
                    cmdIns.Parameters.Add("@LocName", SqlDbType.NVarChar, 20).Value     = this.lblLoc.Text.Trim();
                    cmdIns.Parameters.Add("@BOMVersion", SqlDbType.Int).Value           = Convert.ToInt32(this.lblBomVer.Text.Trim());
                    cmdIns.Connection = oConn; cmdIns.CommandText = NP_Cls.SqlInsert; cmdIns.Transaction = Tr;
                    cmdIns.ExecuteNonQuery();

                    NP_Cls.SqlInsert = "INSERT INTO t_StockMovementDetail " +
                                       "(AutoID, DocNumber, RefNumber, MaterialCode, MaterialName, Quantity, BatchNumber, CurrentUser, LogDate,PlantCode,PlantName,LocCode,LocName,UnitCode,UnitName) " +
                                       "VALUES     (1, @GRPrdNumber,@PrdONumber,@MaterialCode, @MaterialName,@GRPrdQty,@BatchNumber, @UC, GETDATE(),@PlantCode,@PlantName,@LocCode,@LocName,@UnitCode,@UnitName)";
                    cmdIns.Parameters.Add("@MaterialCode", SqlDbType.NVarChar, 15).Value = this.lblMatCode.Text.Trim();
                    cmdIns.Parameters.Add("@MaterialName", SqlDbType.NVarChar, 60).Value = this.lblMaterialName.Text.Trim();
                    cmdIns.Parameters.Add("@UnitCode", SqlDbType.NVarChar, 3).Value      = this.lblUnitCode.Text.Trim();
                    cmdIns.Parameters.Add("@UnitName", SqlDbType.NVarChar, 20).Value     = this.lblUnitName.Text.Trim();
                    cmdIns.Connection = oConn; cmdIns.CommandText = NP_Cls.SqlInsert; cmdIns.Transaction = Tr;
                    cmdIns.ExecuteNonQuery();

                    NP_Cls.SqlInsert  = "INSERT INTO [dbo].[t_GRPrd] ([GRPrdNumber]  ,[PrdONumber]  ,[GRPrdDate]  ,[MovementType]  ,[GRPrdQty]  ,[BatchNumber]  ,[Remark]  ,[UserCreate]  ,[DateCreate]  )       VALUES  (@GRPrdNumber  ,@PrdONumber  ,GETDATE()  ,@MovementType  ,@GRPrdQty  ,@BatchNumber  ,@Remark  ,@UC  ,GetDate())";
                    cmdIns.Connection = oConn; cmdIns.CommandText = NP_Cls.SqlInsert; cmdIns.Transaction = Tr;
                    cmdIns.ExecuteNonQuery();

                    //TODO Save in Stock Overview // 16.11.10
                    string strTmp = "INSERT INTO t_StockOverview (MaterialCode, BatchNumber, UR, QI, Block, UserCreate, DateCreate, Cost,PlantCode,LocCode,OrigQty,UnitCode) VALUES (@MaterialCode,@BatchNumber,@UR,@QI,@Block,@UC, GETDATE(), @Cost,@PlantCode,@LocCode,@OrigQty,@UnitCode)";

                    //cmdIns.Parameters["@BatchNumber"].Value = strTmpBatch;
                    cmdIns.Parameters.Add("@UR", SqlDbType.Decimal).Value      = 0;
                    cmdIns.Parameters.Add("@QI", SqlDbType.Decimal).Value      = Convert.ToDecimal(this.txtQty.Text.Trim());
                    cmdIns.Parameters.Add("@Block", SqlDbType.Decimal).Value   = 0;
                    cmdIns.Parameters.Add("@Cost", SqlDbType.Decimal).Value    = getCost(oConn, Tr, this.lblMatCode.Text.Trim());
                    cmdIns.Parameters.Add("@OrigQty", SqlDbType.Decimal).Value = Convert.ToDecimal(this.txtQty.Text.Trim());
                    cmdIns.Connection = oConn; cmdIns.CommandText = strTmp; cmdIns.Transaction = Tr;
                    cmdIns.ExecuteNonQuery();



                    strTmp            = "UPDATE t_PrdOrder SET GrPrdQuantity = Isnull(GrPrdQuantity,0) + @QI WHERE (PrdONumber = @PrdONumber)";
                    cmdIns.Connection = oConn; cmdIns.CommandText = strTmp; cmdIns.Transaction = Tr;
                    cmdIns.ExecuteNonQuery();

                    if (decimal.Parse(lblPrdQty.Text.Trim()) <= decimal.Parse(lblGrPrdQty.Text.Trim()) + decimal.Parse(txtQty.Text.Trim()))
                    {
                        strTmp            = "UPDATE t_MRPTranOrder SET IsCompleted = 1 WHERE (MaterialHeader = @MaterialCode) AND (TranOrder = @PrdONumber)";
                        cmdIns.Connection = oConn; cmdIns.CommandText = strTmp; cmdIns.Transaction = Tr;
                        cmdIns.ExecuteNonQuery();

                        strTmp            = "UPDATE t_PrdOrder SET ISGRPrd = 1 WHERE (PrdONumber = @PrdONumber)";
                        cmdIns.Connection = oConn; cmdIns.CommandText = strTmp; cmdIns.Transaction = Tr;
                        cmdIns.ExecuteNonQuery();
                    }

                    Tr.Commit(); NP.MSGB(NP_Cls.NPMgsStyle.InfoType, "Save GR Production Completed !!");
                    Clear();

                    this.txtDocNo.Text = GetNumber();
                    BindMat();
                    this.cbPrdOrder.Text = string.Empty; this.cbPrdOrder.Select();
                }
                catch (Exception ex)
                {
                    if (oConn.State == ConnectionState.Open)
                    {
                        Tr.Rollback();
                    }
                    NP.MSGB(NP_Cls.NPMgsStyle.WarningType, "Add Detail : " + ex.Message); return;
                }
                finally
                {
                    if (oConn.State == ConnectionState.Open)
                    {
                        oConn.Close();
                    }
                }
            }
            else
            {
                return;
            }
        }