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

            if (base.FormMode == ObjectState.Edit && !string.IsNullOrEmpty(base.FormKeyCode))
            {
                entity.stock_head_id = Converts.ParseLong(base.FormKeyCode);
                entity = ServiceProvider.StockHeadService.FindByKeys(entity, true);
                Period periodEntity = new Period() { period_id = entity.period_id };
                periodEntity = ServiceProvider.PeriodService.FindByKeys(periodEntity, false);

                PeriodGroup periodGroupEntity = new PeriodGroup() { period_group_id = periodEntity.period_group_id };

                ddlPeriodGroup.DataSource = ServiceProvider.PeriodGroupService.FindByActiveOrID(periodGroupEntity);
                ddlPeriodGroup.ValueMember = "Value";
                ddlPeriodGroup.DisplayMember = "Display";

                ddlPeriodGroup.SelectedValue = periodGroupEntity.period_group_id.ToString();
                
                DataSet ds = ServiceProvider.PeriodService.FindDataSetByParentKey(periodEntity);
                List<ComboBoxDTO> lstComboboxDTO = new List<ComboBoxDTO>();
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    ComboBoxDTO dto = new ComboBoxDTO();
                    dto.Value = dr["period_id"].ToString();
                    dto.Display = dr["period_code"].ToString() + ":" + DateTime.Parse(dr["period_date"].ToString()).ConvertDateToDisplay();
                    lstComboboxDTO.Add(dto);

                }
                lstComboboxDTO.SetPleaseSelect();

                ddlPeriod.DataSource = lstComboboxDTO;
                ddlPeriod.ValueMember = "Value";
                ddlPeriod.DisplayMember = "Display";

                ddlPeriod.SelectedValue = entity.period_id.ToString();

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

                ddlWarehouse.SelectedValue = entity.warehouse_id.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
            {
                ddlPeriodGroup.DataSource = ServiceProvider.PeriodGroupService.FindByActiveOrID();
                ddlPeriodGroup.ValueMember = "Value";
                ddlPeriodGroup.DisplayMember = "Display";

                List<ComboBoxDTO> lstCombobox = new List<ComboBoxDTO>();
                lstCombobox.SetPleaseSelect();

                ddlPeriod.DataSource = lstCombobox;
                ddlPeriod.ValueMember = "Value";
                ddlPeriod.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.FinalText;
            }

            InitialDetailData();
            EnableModeHead();
            EnableModeDetail();
        }
        private void SaveLots(StockDetail entityDetail, StockHead entityStockHead)
        {
            //update into in_phy_lot
            PhyLot entityPhyLot = ServiceProvider.PhyLotService.GetPhyLot(entityDetail.material_id, entityStockHead.warehouse_id, entityDetail.lot_no, false);
            entityPhyLot.bal_qty = entityDetail.bal_qty;
            entityPhyLot.bf_qty = entityDetail.bf_qty;
            entityPhyLot.bf_date = entityDetail.bf_date;
            ServiceProvider.PhyLotService.Update(entityPhyLot);

            //update into in_log_lot
            LogLot entityLogLot = ServiceProvider.LogLotService.GetLogLot(entityDetail.material_id, entityStockHead.warehouse_id);
            entityLogLot.bal_qty = entityDetail.bal_qty;
            entityLogLot.bf_qty = entityDetail.bf_qty;
            entityLogLot.bf_date = entityDetail.bf_date;
            ServiceProvider.LogLotService.Update(entityLogLot);

            //Insert 
            BFLogical bfLogical = new BFLogical();
            bfLogical.period_id = entityStockHead.period_id;
            bfLogical.warehouse_id = entityStockHead.warehouse_id;
            bfLogical.material_id = entityDetail.material_id;
            bfLogical.bf_qty = entityDetail.bf_qty;
            bfLogical.bal_qty = entityDetail.bal_qty;
            ServiceProvider.BFLogicalService.Insert(bfLogical);

            BFPhysical bfPhysical = new BFPhysical();
            bfPhysical.period_id = entityStockHead.period_id;
            bfPhysical.warehouse_id = entityStockHead.warehouse_id;
            bfPhysical.material_id = entityDetail.material_id;
            bfPhysical.bf_qty = entityDetail.bf_qty;
            bfPhysical.bal_qty = entityDetail.bal_qty;
            bfPhysical.lot_no = entityDetail.lot_no;
            ServiceProvider.BFPhysicalService.Insert(bfPhysical);

        }
        private void SaveTransactionDetail(StockHead entityStockHead)
        {
            foreach (DataRow dr in this.dsStockDetail.Tables[0].Rows)
            {
                StockDetail entityDetail = new StockDetail();

                entityDetail.material_id = dr["material_id"].ToLong();                
                entityDetail.bal_qty = dr["Adjust"].ToDecimal();
                entityDetail.bf_qty = dr["Count"].ToDecimal();
                entityDetail.bf_date = DateTime.Now;
                entityDetail.lot_no = dr["Lot No."].ToDecimal();
                entityDetail.remark = dr["Remark"].ToStringNullable();
                entityDetail.stock_head_id = entityStockHead.stock_head_id;

                ServiceProvider.StockDetailService.Insert(entityDetail);
                SaveLots(entityDetail, entityStockHead);
            }
        }
        private StockHead SaveTransactionHead(StockHead entity)
        {
            if (base.FormMode == ObjectState.Add)
            {
                //insert in_tran_head
                entity.transaction_no = ServiceProvider.DocumentTypeService.GetDocumentNumberByDocumentTypeCode(this._documentTypeCode, entity.transaction_date);
                entity.stock_head_id = ServiceProvider.StockHeadService.Insert<long>(entity);
            }

            return entity;
        }
 private void ValidationHead(StockHead entity)
 {
     ValidationResults results = new ValidationResults();
     if (ddlPeriodGroup.SelectedIndex == 0)
     {
         ValidationResult result = new ValidationResult(string.Format(ErrorMessage.IsRequired, "Period Group"), this, string.Empty, string.Empty, null);
         results.AddResult(result);
     }
     if (entity.period_id == 0)
     {
         ValidationResult result = new ValidationResult(string.Format(ErrorMessage.IsRequired, "Period"), this, string.Empty, string.Empty, null);
         results.AddResult(result);
     }
     if (entity.warehouse_id == 0)
     {
         ValidationResult result = new ValidationResult(string.Format(ErrorMessage.IsRequired, "Warehouse"), this, string.Empty, string.Empty, null);
         results.AddResult(result);
     }
     if (results.Count > 0) { throw new ValidationException(results); }
 }
 private StockHead GetHeadData()
 {
     StockHead entity = new StockHead();
     entity.period_id = ddlPeriod.SelectedValue.ToLong();
     entity.warehouse_id = ddlWarehouse.SelectedValue.ToLong();
     entity.document_type_id = this._documentTypeID;
     entity.remark = txtRemark.Text;
     entity.created_by = "SYSTEM";
     entity.created_date = DateTime.Now;
     entity.transaction_date = DateTime.Now;
     entity.transaction_status = TransactionStatus.IN.FinalCode;
     entity.updated_by = "SYSTEM";
     entity.updated_date = DateTime.Now;
     return entity;
 }