private void LoadHeadData()
        {
            TranHead entity = new TranHead();

            if (base.FormMode == ObjectState.Edit && !string.IsNullOrEmpty(base.FormKeyCode))
            {
                //entity.tran_head_id = Converts.ParseLong(base.FormKeyCode);
                //entity = ServiceProvider.TranHeadService.FindByKeys(entity, true);                

                //ddlReason.DataSource = ServiceProvider.ReasonService.GetReasonComboBoxDTOByDocumentTypeID(this._documentTypeID, entity.reason_id);
                //ddlReason.ValueMember = "Value";
                //ddlReason.DisplayMember = "Display";

                //ddlReason.SelectedValue = entity.reason_id.ToString();

                //WareHouse entityWarehouse = new WareHouse() { warehouse_id = entity.warehouse_id.Value };
                //ddlWarehouse.DataSource = ServiceProvider.WareHouseService.FindByActiveOrID(entityWarehouse);
                //ddlWarehouse.ValueMember = "Value";
                //ddlWarehouse.DisplayMember = "Display";

                //ddlWarehouse.SelectedValue = entity.warehouse_id.Value.ToString();

                //txtRemark.Text = entity.remark;
                //lblDocumentNo.Text = entity.transaction_no;
                //lblDocumentDate.Text = entity.transaction_date.ConvertDateToDisplay();
                //lblStatus.Text = (entity.transaction_status == TransactionStatus.IN.FinalCode) ? TransactionStatus.IN.FinalText : TransactionStatus.IN.NormalText;
                //this._documentStatus = entity.transaction_status;
            }
            else
            {
                ddlPeriod.DataSource = ServiceProvider.SaleOrderHeaderService.GetAllCancelOrder();
                ddlPeriod.ValueMember = "Value";
                ddlPeriod.DisplayMember = "Display";

                ddlReason.DataSource = ServiceProvider.ReasonService.GetReasonComboBoxDTOByDocumentTypeID(this._documentTypeID);
                ddlReason.ValueMember = "Value";
                ddlReason.DisplayMember = "Display";

                ddlWarehouse.DataSource = ServiceProvider.WareHouseService.FindByActiveOrID();
                ddlWarehouse.ValueMember = "Value";
                ddlWarehouse.DisplayMember = "Display";

                List<ComboBoxDTO> lstComboBox = new List<ComboBoxDTO>();
                lstComboBox.SetPleaseSelect();
                ddlMenu.DataSource = lstComboBox;
                ddlMenu.ValueMember = "Value";
                ddlMenu.DisplayMember = "Display";

                txtRemark.Text = string.Empty;
                lblDocumentNo.Text = GeneralMessage.AutoRunningDocumentNo;
                lblDocumentDate.Text = DateTime.Now.ConvertDateToDisplay();
                lblStatus.Text = TransactionStatus.IN.NormalText;
            }

            InitialDetailData();
            EnableModeHead();
            EnableModeDetail();
        }
        private void SaveLots(TranDetail entityDetail, TranHead entityTranHead)
        {
            //get in_material
            Material entityMaterial = new Material() { material_id = entityDetail.material_id };
            entityMaterial = ServiceProvider.MaterialService.FindByKeys(entityMaterial, false);

            //update into in_phy_lot
            PhyLot entityPhyLot = ServiceProvider.PhyLotService.GetPhyLot(entityDetail.material_id, entityDetail.warehouse_id_dest, entityDetail.lot_no.Value);
            entityPhyLot.bal_qty = (entityPhyLot.bal_qty + entityDetail.quantity.Value);
            entityPhyLot.expire_date = entityTranHead.transaction_date.AddDays(entityMaterial.shelf_life);
            ServiceProvider.PhyLotService.Update(entityPhyLot);

            //update into in_log_lot
            LogLot entityLogLot = ServiceProvider.LogLotService.GetLogLot(entityDetail.material_id, entityDetail.warehouse_id_dest);
            entityLogLot.bal_qty = (entityLogLot.bal_qty + entityDetail.quantity.Value);
            ServiceProvider.LogLotService.Update(entityLogLot);
        }
        private TranHead SaveTransactionHead(TranHead entity)
        {
            if (base.FormMode == ObjectState.Add)
            {
                //insert in_tran_head
                entity.transaction_no = ServiceProvider.DocumentTypeService.GetDocumentNumberByDocumentTypeCode(this._documentTypeCode, entity.transaction_date);
                entity.tran_head_id = ServiceProvider.TranHeadService.Insert<long>(entity);
            }
            else if (base.FormMode == ObjectState.Edit)
            {
                ServiceProvider.TranHeadService.Update(entity);
            }

            return entity;
        }
        private void SaveTransactionDetail(TranHead entityTranHead)
        {
            foreach (DataRow dr in this.dsTranDetail.Tables[0].Rows)
            {
                TranDetail entityDetail = new TranDetail();
                if (dr.RowState == DataRowState.Added)
                {
                    entityDetail.material_id = dr["material_id"].ToLong();
                    entityDetail.warehouse_id_dest = dr["warehouse_id_dest"].ToLong();
                    entityDetail.quantity = dr["Quantity"].ToDecimal();
                    entityDetail.remark = dr["Remark"].ToStringNullable();
                    entityDetail.lot_no = dr["Lot No."].ToDecimal();
                    entityDetail.tran_head_id = entityTranHead.tran_head_id;

                    ServiceProvider.TranDetailService.Insert(entityDetail);
                    SaveLots(entityDetail, entityTranHead);
                }
            }
        }
        private void ValidationHead(TranHead entity)
        {
            ValidationResults results = new ValidationResults();
            if (base.FormMode == ObjectState.Add)
            {
                if (ddlMenu.SelectedIndex == 0)
                {
                    ValidationResult result = new ValidationResult(string.Format(ErrorMessage.IsRequired, "Menu"), this, string.Empty, string.Empty, null);
                    results.AddResult(result);
                }
                if (entity.warehouse_id == 0)
                {
                    ValidationResult result = new ValidationResult(string.Format(ErrorMessage.IsRequired, "Warehose"), this, string.Empty, string.Empty, null);
                    results.AddResult(result);
                }
            }
            if (entity.reason_id == 0)
            {
                ValidationResult result = new ValidationResult(string.Format(ErrorMessage.IsRequired, "Reason"), this, string.Empty, string.Empty, null);
                results.AddResult(result);
            }

            if (!(this.dsTranDetail.Tables.Count > 0 && this.dsTranDetail.Tables[0].Rows.Count > 0))
            {
                ValidationResult result = new ValidationResult(string.Format(ErrorMessage.IsRequired, "Details"), this, string.Empty, string.Empty, null);
                results.AddResult(result);
            }
            if (results.Count > 0) { throw new ValidationException(results); }
        }
        private TranHead GetHeadData()
        {
            TranHead entity = new TranHead();
            if (base.FormMode == ObjectState.Edit)
            {
                entity.tran_head_id = base.FormKeyCode.ToLong();
                entity = ServiceProvider.TranHeadService.FindByKeys(entity, false);
            }

            //entity.or = txtOrderNo.Text;
            entity.transaction_date = DateTime.Now;
            entity.document_type_id = this._documentTypeID;
            entity.reason_id = Converts.ParseLong(ddlReason.SelectedValue.ToString());
            entity.warehouse_id = ddlWarehouse.SelectedValue.ToLong();

            entity.remark = txtRemark.Text;
            if (base.FormMode == ObjectState.Add)
            {
                entity.created_by = "SYSTEM";
                entity.created_date = DateTime.Now;
                entity.transaction_status = TransactionStatus.IN.NormalCode;
            }
            else if (base.FormMode == ObjectState.Edit)
            {
                entity.transaction_status = TransactionStatus.IN.FinalCode;
            }
            entity.updated_by = "SYSTEM";
            entity.updated_date = DateTime.Now;
            return entity;
        }
        private void LoadHeadData()
        {
            TranHead entity = new TranHead();

            if (base.FormMode == ObjectState.Edit && !string.IsNullOrEmpty(base.FormKeyCode))
            {
                entity.tran_head_id = Converts.ParseLong(base.FormKeyCode);
                entity = ServiceProvider.TranHeadService.FindByKeys(entity, true);

                txtReferenceNo.Text = entity.reference_no;

                ddlReason.DataSource = ServiceProvider.ReasonService.GetReasonComboBoxDTOByDocumentTypeID(this._documentTypeID, entity.reason_id);
                ddlReason.ValueMember = "Value";
                ddlReason.DisplayMember = "Display";

                ddlReason.SelectedValue = entity.reason_id.ToString();

                if (entity.supplier_id != null)
                {
                    rdoSupplier.Checked = true;

                    ddlSupplier.DataSource = ServiceProvider.SupplierService.FindByActiveOrID(new Supplier() { supplier_id = entity.supplier_id.Value });
                    ddlSupplier.ValueMember = "Value";
                    ddlSupplier.DisplayMember = "Display";

                    ddlSupplier.SelectedValue = entity.supplier_id.Value.ToString();

                    ddlWarehouse.DataSource = ServiceProvider.WareHouseService.FindByActiveOrID();
                    ddlWarehouse.ValueMember = "Value";
                    ddlWarehouse.DisplayMember = "Display";
                }
                else if (entity.warehouse_id != null)
                {
                    rdoWarehouse.Checked = true;

                    ddlSupplier.DataSource = ServiceProvider.SupplierService.FindByActiveOrID();
                    ddlSupplier.ValueMember = "Value";
                    ddlSupplier.DisplayMember = "Display";

                    ddlWarehouse.DataSource = ServiceProvider.WareHouseService.FindByActiveOrID(new WareHouse() { warehouse_id = entity.warehouse_id.Value });
                    ddlWarehouse.ValueMember = "Value";
                    ddlWarehouse.DisplayMember = "Display";

                    ddlWarehouse.SelectedValue = entity.warehouse_id.Value.ToString();
                }
                else if (!string.IsNullOrEmpty(entity.other_source))
                {
                    rdoOther.Checked = true;

                    ddlSupplier.DataSource = ServiceProvider.SupplierService.FindByActiveOrID();
                    ddlSupplier.ValueMember = "Value";
                    ddlSupplier.DisplayMember = "Display";

                    ddlWarehouse.DataSource = ServiceProvider.WareHouseService.FindByActiveOrID();
                    ddlWarehouse.ValueMember = "Value";
                    ddlWarehouse.DisplayMember = "Display";

                    txtOther.Text = entity.other_source;
                }

                txtRemark.Text = entity.remark;
                lblDocumentNo.Text = entity.transaction_no;
                lblDocumentDate.Text = entity.transaction_date.ConvertDateToDisplay();
                lblStatus.Text = (entity.transaction_status == TransactionStatus.IN.FinalCode) ? TransactionStatus.IN.FinalText : TransactionStatus.IN.NormalText;
                this._documentStatus = entity.transaction_status;
            }
            else
            {
                txtReferenceNo.Text = string.Empty;

                ddlReason.DataSource = ServiceProvider.ReasonService.GetReasonComboBoxDTOByDocumentTypeID(this._documentTypeID);
                ddlReason.ValueMember = "Value";
                ddlReason.DisplayMember = "Display";

                rdoSupplier.Checked = true;
                rdoWarehouse.Checked = false;
                rdoOther.Checked = false;

                ddlSupplier.DataSource = ServiceProvider.SupplierService.FindByActiveOrID();
                ddlSupplier.ValueMember = "Value";
                ddlSupplier.DisplayMember = "Display";

                ddlWarehouse.DataSource = ServiceProvider.WareHouseService.FindByActiveOrID();
                ddlWarehouse.ValueMember = "Value";
                ddlWarehouse.DisplayMember = "Display";

                txtRemark.Text = string.Empty;
                lblDocumentNo.Text = GeneralMessage.AutoRunningDocumentNo;
                lblDocumentDate.Text = DateTime.Now.ConvertDateToDisplay();
                lblStatus.Text = TransactionStatus.IN.NormalText;
            }

            InitialDetailData();
            EnableModeHead();
            EnableModeDetail();
        }
        private void ValidationHead(TranHead entity)
        {
            ValidationResults results = new ValidationResults();
            if (base.FormMode == ObjectState.Add)
            {
                if (string.IsNullOrEmpty(entity.reference_no))
                {
                    ValidationResult result = new ValidationResult(string.Format(ErrorMessage.IsRequired, "Reference No."), this, string.Empty, string.Empty, null);
                    results.AddResult(result);
                }
                else
                {
                    TranHead entityTranHead = ServiceProvider.TranHeadService.GetTransactionByReferenceNo(entity.reference_no);
                    if (entityTranHead != null && entityTranHead.tran_head_id != 0)
                    {
                        ValidationResult result = new ValidationResult(string.Format(ErrorMessage.IsDuplicate, "Reference No."), this, string.Empty, string.Empty, null);
                        results.AddResult(result);
                    }
                }
            }
            if (entity.reason_id == 0)
            {
                ValidationResult result = new ValidationResult(string.Format(ErrorMessage.IsRequired, "Reason"), this, string.Empty, string.Empty, null);
                results.AddResult(result);
            }
            if (rdoSupplier.Checked && entity.supplier_id == 0)
            {
                ValidationResult result = new ValidationResult(string.Format(ErrorMessage.IsRequired, "Source Supllier"), this, string.Empty, string.Empty, null);
                results.AddResult(result);
            }
            else if (rdoWarehouse.Checked && entity.warehouse_id == 0)
            {
                ValidationResult result = new ValidationResult(string.Format(ErrorMessage.IsRequired, "Source Warehouse"), this, string.Empty, string.Empty, null);
                results.AddResult(result);
            }
            else if (rdoOther.Checked && string.IsNullOrEmpty(entity.other_source))
            {
                ValidationResult result = new ValidationResult(string.Format(ErrorMessage.IsRequired, "Source other"), this, string.Empty, string.Empty, null);
                results.AddResult(result);
            }

            if (!(this.dsTranDetail.Tables.Count > 0 && this.dsTranDetail.Tables[0].Rows.Count > 0))
            {
                ValidationResult result = new ValidationResult(string.Format(ErrorMessage.IsRequired, "Details"), this, string.Empty, string.Empty, null);
                results.AddResult(result);
            }
            else
            {
                //foreach (DataRow dr in this.dsTranDetail.Tables[0].Rows)
                //{
                //    if (!string.IsNullOrEmpty(base.FormKeyCode))
                //    {
                //        dr["tran_head_id"] = base.FormKeyCode.ToLong();
                //    }
                //    TranDetail entityTranDetail = new TranDetail();
                //    entityTranDetail.material_id = dr["material_id"].ToLong();
                //    entityTranDetail.warehouse_id_dest = dr["warehouse_id_dest"].ToLong();
                //    entityTranDetail.quantity = dr["Quantity"].ToDecimal();
                //    entityTranDetail.remark = dr["Remark"].ToString();
                //    entityTranDetail.lot_no = dr["Lot No."].ToDecimal();
                //    this.ValidationDetail(entityTranDetail);
                //}
            }
            if (results.Count > 0) { throw new ValidationException(results); }
        }
        private void ValidationHead(TranHead entity)
        {
            ValidationResults results = new ValidationResults();
            if (entity.reason_id == 0)
            {
                ValidationResult result = new ValidationResult(string.Format(ErrorMessage.IsRequired, "Reason"), this, string.Empty, string.Empty, null);
                results.AddResult(result);
            }
            if (rdoWarehouse.Checked && entity.warehouse_id == 0)
            {
                ValidationResult result = new ValidationResult(string.Format(ErrorMessage.IsRequired, "Destination Warehouse"), this, string.Empty, string.Empty, null);
                results.AddResult(result);
            }
            else if (rdoOther.Checked && string.IsNullOrEmpty(entity.other_source))
            {
                ValidationResult result = new ValidationResult(string.Format(ErrorMessage.IsRequired, "Destination other"), this, string.Empty, string.Empty, null);
                results.AddResult(result);
            }

            if (!(this.dsTranDetail.Tables.Count > 0 && this.dsTranDetail.Tables[0].Rows.Count > 0))
            {
                ValidationResult result = new ValidationResult(string.Format(ErrorMessage.IsRequired, "Details"), this, string.Empty, string.Empty, null);
                results.AddResult(result);
            }
            if (results.Count > 0) { throw new ValidationException(results); }
        }