using System;
using System.Data;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace JXC_MIS.UserTier.bps
{
	public class frmbps_GoodsBatch : JXC_MIS.UserTier.BaseForm.frmReceiptBase
	{

		private const int BATCHCODE_COl = 46;		//分批批次		列
		private const int BATCHSUM_COl = 37;		//分批前总数量即到货数量	列
		private const int BATCHNUM_COl = 47;		//分批此批数量	列
		private const int PAST_BATCHCODE_COl = 48;	//原始批号		列
		private const int REPORTCHECKCODE_COl = 50;	//送检号		列
		private const int CHECKOROTHER_COl = 51;	//送检  标记列	列

		#region 设计器生成的代码
		private System.Windows.Forms.ContextMenu ctmT_Menu;
		private System.Windows.Forms.MenuItem mitm_AddBatch;
		private System.Windows.Forms.MenuItem mitm_DelBatch;


		private System.ComponentModel.IContainer components = null;

		public frmbps_GoodsBatch()
		{
			// 该调用是 Windows 窗体设计器所必需的。
			InitializeComponent();

			// TODO: 在 InitializeComponent 调用后添加任何初始化
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.ctmT_Menu = new System.Windows.Forms.ContextMenu();
			this.mitm_AddBatch = new System.Windows.Forms.MenuItem();
			this.mitm_DelBatch = new System.Windows.Forms.MenuItem();
			this.SuspendLayout();
			// 
			// TxtTemp
			// 
			this.TxtTemp.Name = "TxtTemp";
			// 
			// sbr_statusBar
			// 
			this.sbr_statusBar.Name = "sbr_statusBar";
			this.sbr_statusBar.Size = new System.Drawing.Size(877, 26);
			// 
			// tbr_Toolbar
			// 
			this.tbr_Toolbar.Name = "tbr_Toolbar";
			this.tbr_Toolbar.Size = new System.Drawing.Size(877, 42);
			// 
			// t_Menu
			// 
			this.t_Menu.DropDownMenu = this.ctmT_Menu;
			// 
			// ToolMenu
			// 
			this.ToolMenu.Popup += new System.EventHandler(this.ToolMenu_Popup);
			// 
			// CboReceiptNo
			// 
			this.CboReceiptNo.Name = "CboReceiptNo";
			this.CboReceiptNo.Size = new System.Drawing.Size(150, 20);
			// 
			// cbo_Year
			// 
			this.cbo_Year.Name = "cbo_Year";
			this.cbo_Year.Size = new System.Drawing.Size(56, 20);
			// 
			// cbo_Month
			// 
			this.cbo_Month.Name = "cbo_Month";
			this.cbo_Month.Size = new System.Drawing.Size(44, 20);
			// 
			// grd_Detail
			// 
			this.grd_Detail.Name = "grd_Detail";
			// 
			// grd_Footer
			// 
			this.grd_Footer.Name = "grd_Footer";
			// 
			// grd_Other
			// 
			this.grd_Other.Name = "grd_Other";
			// 
			// btnDown
			// 
			this.btnDown.Name = "btnDown";
			// 
			// grd_Header
			// 
			this.grd_Header.Name = "grd_Header";
			// 
			// grd_Bottom
			// 
			this.grd_Bottom.Name = "grd_Bottom";
			// 
			// grd_Top
			// 
			this.grd_Top.Name = "grd_Top";
			// 
			// ctmT_Menu
			// 
			this.ctmT_Menu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.mitm_AddBatch,
																					  this.mitm_DelBatch});
			// 
			// mitm_AddBatch
			// 
			this.mitm_AddBatch.Index = 0;
			this.mitm_AddBatch.Text = "增加一批";
			this.mitm_AddBatch.Click += new System.EventHandler(this.mitm_AddBatch_Click);
			// 
			// mitm_DelBatch
			// 
			this.mitm_DelBatch.Index = 1;
			this.mitm_DelBatch.Text = "减少一批";
			this.mitm_DelBatch.Click += new System.EventHandler(this.mitm_DelBatch_Click);
			// 
			// frmbps_GoodsBatch
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(877, 505);
			this.Name = "frmbps_GoodsBatch";
			this.Load += new System.EventHandler(this.frmbps_GoodsBatch_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void frmbps_GoodsBatch_Load(object sender, System.EventArgs e)
		{
			this.ReceiptName="采购到货分批";
			this.InitInterface();		
			this.IRefertoForm = new JXC_MIS.UserTier.RefertoInput.frmGridRefertoSelect();	
		}

		private void ToolMenu_Popup(object sender, System.EventArgs e)
		{
		
		}

		//增加一批
		private void mitm_AddBatch_Click(object sender, System.EventArgs e)
		{
			int lngCols = grd_Detail.Cols;
			int lngRowIndex = grd_Detail.Row + 1;

			grd_Detail.RowInsert("",lngRowIndex);
			grd_Detail.Row = lngRowIndex;

			for(int i=0;i<lngCols;i++)
			{
				grd_Detail.SetCellText(lngRowIndex,i,grd_Detail.GetCellText(lngRowIndex-1,i));
			}

			grd_Detail.SetRowNo();


			//清除以下内容，重新填写
			grd_Detail.SetCellText(lngRowIndex,BATCHCODE_COl,"");		//分批批次
			grd_Detail.SetCellText(lngRowIndex,BATCHNUM_COl,"");		//数量
			grd_Detail.SetCellText(lngRowIndex,PAST_BATCHCODE_COl,"");	//原始批号
			grd_Detail.SetCellText(lngRowIndex,REPORTCHECKCODE_COl,"");	//送检号
		}

		//减少一批
		private void mitm_DelBatch_Click(object sender, System.EventArgs e)
		{
			int lngRowIndex = grd_Detail.Row;

			grd_Detail.RowRemove(lngRowIndex);

			grd_Detail.SetRowNo();
		}

		protected override bool ReceiptCheck()
		{
			//检测通过才能去保存
			bool blnReturn = base.ReceiptCheck ();
			
			//还要检测其它项目
			if(blnReturn)
			{

				if (!(CheckPastBatchCode() && CheckReportCheckCode()))
				{
					blnReturn = false;
				}

				//还要检测分批后的数量与分批前的总数量相等
				if(blnReturn && grd_Detail.GetCellText(grd_Detail.Rows-1,BATCHSUM_COl) != grd_Detail.GetCellText(grd_Detail.Rows-1,BATCHNUM_COl))
				{
					if (MessageBox.Show("分批后的数量和与分批前的总数量不相等，请检查后再保存！\n\r确实要保存吗？","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Information,MessageBoxDefaultButton.Button2) == DialogResult.No)
					{
						blnReturn = false;
					}
				}

			
			}

			return blnReturn;
		}

		private bool CheckPastBatchCode()
		{
			bool blnReturn = true;

			int lngRows = grd_Detail.Rows;
			for(int i=1 ; i<lngRows-1 ; i++)
			{
				if (grd_Detail.GetCellText(i, 1) != "" && grd_Detail.GetCellText(i, PAST_BATCHCODE_COl) == "")
				{
					MessageBox.Show("第" + i + "行原始批号不能为空！","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
					blnReturn = false;
					break;
				}																												  
			}

			return blnReturn;
		}

		private bool CheckReportCheckCode()
		{
			bool blnReturn = true;

			int lngRows = grd_Detail.Rows;
			for(int i=1 ; i<lngRows-1 ; i++)
			{
				if (grd_Detail.GetCellText(i, 1) != "" && grd_Detail.GetCellText(i, REPORTCHECKCODE_COl) == "" && grd_Detail.GetCellText(i, CHECKOROTHER_COl) == "送检")
				{
					MessageBox.Show("第" + i + "行报检号不能为空，请单击工具栏中菜单生成报检号！","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
					blnReturn = false;
					break;
				}																												  
			}

			return blnReturn;
		}

	}//End Class
}//End NameSpace


