private void gridOrderView_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (e != null && e.PrevFocusedRowHandle < -1)
             return;

            if (gridOrderView.GetFocusedDataRow() != null)
            {
                var dr = gridOrderView.GetFocusedDataRow();

                int OrderID = Convert.ToInt32(dr["ID"]);
                string sharps = Helpers.FormattingHelpers.GetNumberFormatting();
                Order = new PO(OrderID);
                RefreshInvoice();
                RefreshPoDetail();
                // Layout  ... Show the damn correct layout

                // Hide the empty layout
                LayoutEmptyDetails.Visibility = LayoutVisibility.Never;
                LayoutSelectedOrderDetail.Visibility = LayoutVisibility.Always;

                int Currency = 0x0409;
                if(!Order.IsColumnNull("LCID"))
                    Currency = Order.LCID;

                if(!string.IsNullOrEmpty(Order.s_PurchaseType))
                    PurchaseTypes = POType.GetAllPOTypes().Find(t => t.ID == Order.PurchaseType);
                string space = "";
                DataRow dar = gridInvoiceView.GetFocusedDataRow();
                HeaderGroup.Text = "PO No: " + dr["OrderNumber"].ToString();
               if (dar != null)
               {
                   HeaderGroup.Text = HeaderGroup.Text + space.PadRight(160) + "Invoice No: " + gridInvoiceView.GetFocusedDataRow()["STVOrInvoiceNo"];
               }

              // lblOrderType.Text = PurchaseTypes != null ?  string.Format("{0} : {1}" ,PurchaseTypes.Group , PurchaseTypes.Name) : "";
                lblPONumber.Text = Order.PONumber;
                lblTotalValue.Text = Order.TotalValue.ToString(Helpers.FormattingHelpers.GetCurrencyFormatByLCID(Currency));
                lblInsurance.Text = Order.Insurance.ToString(Helpers.FormattingHelpers.GetBirrFormatting());
                lblNBE.Text = Order.NBE.ToString(Helpers.FormattingHelpers.GetBirrFormatting());
                lblSupplier.Text = Order.Supplier.CompanyName;
                lblShipper.Text = String.IsNullOrEmpty(Order.ShippingSite) ? "-" : Order.ShippingSite;
                lblLetterNo.Text = String.IsNullOrEmpty(Order.LetterNo) ? "-" : Order.LetterNo;
                lblPOType.Text = dr["Name"].ToString();

                var PaymentType = new PaymentType();
                PaymentType.LoadByPrimaryKey(Order.PaymentTypeID);
                lblPaymentType.Text = String.IsNullOrEmpty(PaymentType.Name) ? "-" : PaymentType.Name ;

                var User = new User();
                User.LoadByPrimaryKey(Order.SavedbyUserID);
                lblPOBy.Text = String.IsNullOrEmpty(User.FullName) ? "-" : User.FullName;

                if (!Order.IsColumnNull("ModeID"))
                {
                    var Mode = new Mode();
                    Mode.LoadByPrimaryKey(Order.ModeID);
                    lblMode.Text = Mode.TypeName;
                }
                else lblMode.Text = "-";

                if (!Order.IsColumnNull("PurchaseOrderStatusID"))
                {
                    var poStatus = new PurchaseOrderStatus();
                    poStatus.LoadByPrimaryKey(Order.PurchaseOrderStatusID);
                    lblPOStatus.Text = poStatus.Name;
                }

                chkboxIsElectronic.Checked = Order.IsElectronic;
                lblOrderDate.Text = Order.PODate.ToShortDateString();
                lblSyncDate.Text = Order.ModifiedDate == DateTime.MinValue ? "NA" :  Order.ModifiedDate.ToString(CultureInfo.InvariantCulture);

                if (!Order.IsColumnNull("StoreID"))
                {
                    Activity act = new Activity();
                    act.LoadByPrimaryKey(Order.StoreID);
                    lblAccount.Text = act.AccountName;
                    lblSubAccount.Text = act.SubAccountName;
                    lblActivity.Text = act.Name;

                }
                else
                {
                    lblAccount.Text = lblSubAccount.Text = lblActivity.Text = "-";
                }

                lblRefNo.Text = String.IsNullOrEmpty(Order.RefNo) ? "-" : Order.RefNo;
              lblRemainingValue.Text = Order.Remaining.ToString(Helpers.FormattingHelpers.GetCurrencyFormatByLCID(Currency));
                if (BLL.Settings.UseNewUserManagement && this.HasPermission("Add-Invoice"))
                {
                    btnAddInvoice.Enabled = true;
                }
                else if (BLL.Settings.UseNewUserManagement)
                {
                    btnAddInvoice.Enabled = false;
                }

                btnAddInvoice.Enabled = btnEditOrder.Enabled =btnAddOrderDetail.Enabled = !Order.IsElectronic;

                if (Order.IsElectronic)
                {
                    grdPoDetail.Enabled = false;
                    gridInvoice.Enabled = false;
                }
                else
                {
                    grdPoDetail.Enabled = true ;
                    gridInvoice.Enabled = true;
                }

                //purchse order details
                grdPoDetail.DataSource = Order.PurchaseOrderDetail.DefaultView;
            }
            else
            {
                // Hide the empty layout
                LayoutEmptyDetails.Visibility = LayoutVisibility.Always;
                LayoutSelectedOrderDetail.Visibility = LayoutVisibility.Never;
            }
        }
        private void lkReceiptInvoice_EditValueChanged(object sender, EventArgs e)
        {
            ResetFields();

            if (lkReceiptInvoice.EditValue != null)
            {
                if ((standardRecType == StandardReceiptType.iGRVOnline || deliveryNoteType == DeliveryNoteType.Automatic) && lkReceiptInvoice.EditValue.Equals(-1))
                {
                    HandleReceiptTypeChange(true);
                    return;
                }

                if (standardRecType == StandardReceiptType.iGRV && !lkReceiptInvoice.EditValue.Equals(-1))
                {
                    //This means, the hub has chosen an invoice other than "Not Found" from the list of invoices.  Therefore, we want to change the receipt type to be iGRV-Online.
                    HandleReceiptTypeChange();
                }

                BLL.ReceiptInvoice rctInvoice = new ReceiptInvoice();
                rctInvoice.LoadByPrimaryKey(Convert.ToInt32(lkReceiptInvoice.EditValue));
                var po = new PO();
                po.LoadByPrimaryKey(rctInvoice.POID);

                if (po.IsElectronic == true)
                {
                    rdIsElectronic.Checked = true;
                    lblSyncDate.Text = rctInvoice.DateOfEntry.ToShortDateString();
                    txtPassCode.Enabled = true;
                    lcPassCode.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                }
                else
                {
                    rdIsElectronic.Checked = false;
                    lblSyncDate.Text = "-";
                    txtPassCode.Enabled = false;
                    lcPassCode.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                }

                lblOrderNo.Text = po.PONumber;
                lblOrderNum.Text = po.PONumber;

                if (!rctInvoice.IsColumnNull("DocumentTypeID"))
                {
                    var doctype = new BLL.DocumentType();
                    doctype.LoadByPrimaryKey(rctInvoice.DocumentTypeID);
                    lblDeliveryNote.Text = doctype.Name;
                    lblHeaderDoc.Text = doctype.Name;
                }
                else
                {
                    lblDeliveryNote.Text = "-";
                    lblHeaderDoc.Text = "-";
                }

                var mode = new Mode();
                mode.LoadByPrimaryKey(po.ModeID);
                lblMode.Text = mode.TypeName;
                lblAccount.Text = "-";
                lblSubAccount.Text = "-";
                lblActivity.Text = "-";
                lblReferenceNo.Text = po.RefNo;

                var payment = new PaymentType();
                payment.LoadByPrimaryKey(po.PaymentTypeID);
                lblPaymentType.Text = payment.Name;
                lblHeaderPayment.Text = payment.Name;

                //if (po.IsColumnNull("NBE"))
                //{
                //    lblNebService.Text = Convert.ToString(po.NBE);
                //}
                //else
                //{
                //    lblNebService.Text = "-";
                //}

                if (po.Remaining != -1)
                    lblRemainingValue.Text = po.Remaining.ToString();
                else
                    lblRemainingValue.Text = "-";

                if (po.TotalValue != -1)
                    lblTotalValue.Text = po.Remaining.ToString();
                else
                    lblTotalValue.Text = "-";

                if (po.Insurance != -1)
                    lblChargeInsurance.Text = po.Insurance.ToString();
                else
                    lblChargeInsurance.Text = "-";

                if (po.NBE != -1)
                    lblNebService.Text = po.NBE.ToString();
                else
                    lblNebService.Text = "-";

                lblOrderDate.Text = po.PODate.ToShortDateString();
                lblReceiptDate.Text = rctInvoice.DateOfEntry.ToShortDateString();

                var user = new User();
                user.LoadByPrimaryKey(po.SavedbyUserID);
                lblOrderBy.Text =  user.FullName;

                var sup = new Supplier();

                lkAccounts.EditValue = rctInvoice.ActivityID;
                var act = new Activity();
                act.LoadByPrimaryKey(rctInvoice.ActivityID);
                _supplierID = rctInvoice.GetSupplier();
                sup.LoadByPrimaryKey(_supplierID);

                lblSupplier.Text = sup.CompanyName;
                lblOrdSup.Text = sup.CompanyName;
                lblInvoiceNo.Text = rctInvoice.STVOrInvoiceNo;

                lblInvAccount.Text = act.AccountName ?? "-";
                lblInvActivity.Text = act.Name ?? "-";
                lblAct.Text = act.FullActivityName ?? "-";
                lblInvSubAccount.Text = act.SubAccountName ?? "-";
                lblInvMode.Text = act.ModeName ?? "-";
                lblInvTotalValue.Text = rctInvoice.TotalFOBValue.ToString("N");
                lblInsurancePolicy.Text = rctInvoice.InsurancePolicyNo == " "? rctInvoice.InsurancePolicyNo : "-";

                var poType = new POType();
                poType.LoadByPrimaryKey(po.PurchaseType);
                lblOrderType.Text = poType.Name ?? "-";
                lblPOType.Text = poType.Name ?? "-";

                var poStatus = new PurchaseOrderStatus();
                poStatus.LoadByPrimaryKey(po.PurchaseOrderStatusID);
                lblOrderStatus.Text = poStatus.Name ?? "-";

                var it = new InvoiceType();
                it.LoadByPrimaryKey(rctInvoice.InvoiceTypeID);
                lblInvType.Text = it.Name;
                lblInvDate.Text = rctInvoice.DateOfEntry.ToShortDateString();

                mode.LoadByPrimaryKey(rctInvoice.ActivityID);

                lblWayBillNo.Text = rctInvoice.WayBillNo;

                txtTransitTransferVoucherNo.Text = rctInvoice.TransitTransferNo;
                txtEditTransferNo.Text = rctInvoice.TransitTransferNo;

                lblInsurancePolicyNo.Text = rctInvoice.InsurancePolicyNo;
                BLL.PO order = new PO();
                order.LoadByPrimaryKey(rctInvoice.POID);
                lblRefNo.Text = order.RefNo;
                lblPoNo.Text = order.PONumber;

                DataTable relatedReceives = rctInvoice.GetRelatedReceives();

                hasPreviousReceive = false;

                if (relatedReceives.Rows.Count > 0)
                {
                    XtraMessageBox.Show(
                        "The Invoice you have selected has previous associated receives",
                        "Invoice Detail", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                        colSelectedManufactuerer.Visible = true;
                        colSelectedUnit.Visible = true;
                        colSelectionLineNo.Visible = true;
                        colSelectedReceiveQty.Visible = true;
                        colSelectedInvoiceQty.Visible = true;
                        hasPreviousReceive = true;
                        gridItemsChoice.Enabled = false;
                        colRemainingQty.VisibleIndex = 9;
                        lcItemChoiceGrid.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                        _dtSelectedTable = relatedReceives.Clone();
                        gridSelected.DataSource = _dtSelectedTable;

                        foreach (DataRow dr in relatedReceives.Rows)
                        {
                            SelectAnItem(dr);
                        }
                }
                else
                {
                    hasPreviousReceive = false;
                    colSelectedManufactuerer.Visible = false;
                    colSelectedUnit.Visible = false;
                    colSelectionLineNo.Visible = false;
                    colSelectedReceiveQty.Visible = false;
                    colSelectedInvoiceQty.Visible = false;
                    colRemainingQty.Visible = false;
                    gridItemsChoice.Enabled = true;
                    lcItemChoiceGrid.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                }

                if (lkAccounts.EditValue != null)
                {
                  //  lcInvoiceDetail.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                }

                if (standardRecType == StandardReceiptType.iGRVOnline || standardRecType == StandardReceiptType.GRV || deliveryNoteType == DeliveryNoteType.Automatic)
                {
                    if (!hasPreviousReceive) LoadItemsFromTransferredReceiptInvoiceDetail(rctInvoice.ID);
                    txtRefNo.Text = lkReceiptInvoice.Text;
                    colSelectedReceiveQty.Visible = true;
                    lcItemChoiceGrid.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    lkAccounts.Enabled = false;
                }
                else
                {
                    lkAccounts.Enabled = !BLL.Settings.IsCenter;
                }

                if(!BLL.Settings.IsCenter)
                {  //Passcode Textbox
                    var dataRowView = lkReceiptInvoice.GetSelectedDataRow() as DataRowView;
                    if (dataRowView != null)
                        _isElectronic = Convert.ToBoolean(dataRowView["IsElectronic"]);

                    lcPassCode.Visibility = (!_isNonElectronicReceiveOnly) && (_isElectronic) && (standardRecType == StandardReceiptType.iGRVOnline || deliveryNoteType == DeliveryNoteType.Automatic)
                                            ? LayoutVisibility.Always
                                            : LayoutVisibility.Never;
                    txtPassCode.Text = "HHMM";
                }

            }
        }
        private void ReceiptInvoiceDialog_Load(object sender, EventArgs e)
        {
            InitializeForm();
            LayoutAndBindingByAccess();
            LoadDecimalFormatings();
            ConstructTableColumns();
            gridViewInvoiceDetail.RowStyle += new RowStyleEventHandler(gridInvoiceDetail_RowStyle);
            lkInvoiceType.Properties.DataSource = BLL.Settings.IsCenter
                ? ReceiptInvoiceType.GetAllInvoiceTypes()
                : ReceiptInvoiceType.GetAllInvoiceTypeByCode("INT"); //Internal for Hubs

            lkInvoiceType.EditValue = BLL.Settings.IsCenter ? -1 : ReceiptInvoiceType.InvoiceType.INTERNAL;
            lkCurrencyLCID.Properties.DataSource = Helpers.FormattingHelpers.GetCurrencyList();
            var etb = Convert.ToInt32(Helpers.FormattingHelpers.GetCurrencyList().ToTable().Select("Symbol = 'ETB'")[0]["ID"]);
            lkCurrencyLCID.EditValue = etb; //ETB is the default value

            PO Order = new PO(_orderID);

            if (_orderID != 0)
            {

                Activity act = new Activity();
                act.LoadByPrimaryKey(Order.StoreID);
                lblAccount.Text = act.AccountName;
                lblAccountDetail.Text = act.AccountName;
                lblSubAccount.Text = act.SubAccountName;
                lblSubAccountDetail.Text = act.SubAccountName;
                lblActivity.Text = act.Name;
                lblActivityDetail.Text = act.Name;

                lblPOType.Text = POType.GetPOTypeNameByID(Order.PurchaseType);
                lblPOTypeDetail.Text = POType.GetPOTypeNameByID(Order.PurchaseType);
                lblPONumberDetail.Text = Order.PONumber.ToString();

                //lblInvoiceNoDetail.Text =

                var poStatus = new PurchaseOrderStatus();
                poStatus.LoadByPrimaryKey(Order.PurchaseOrderStatusID);
                lblOrderStatus.Text = poStatus.Name;

                var Mode = new Mode();
                Mode.LoadByPrimaryKey(Order.ModeID);
                lblMode.Text = Mode.TypeName;
                lblModeDetail.Text = Mode.TypeName;

                var receiptInvoice = new ReceiptInvoice();

                receiptInvoice.LoadForPO(_orderID);

                try
                {
                    lblInvoiceNoDetail.Text = !receiptInvoice.IsColumnNull("STVOrInvoiceNo") ? receiptInvoice.STVOrInvoiceNo : "-";
                }
                catch (Exception)
                {
                    lblInvoiceNoDetail.Text = "-";
                }
                var invoiceType = new ReceiptInvoiceType();
                HeaderGroup.Text = "PO No: " + Order.PONumber;
            }

            //documentType
            var po = new PO(_orderID);
            var documentTypes = BLL.DocumentType.GetDocumentTypesByPOType(po.PurchaseType);
            lkDocumentType.Properties.DataSource = documentTypes;

            lkDocumentType.EditValue = documentTypes.Rows.Count > 0 ? documentTypes.Rows[0]["DocumentTypeID"] : -1;

            if (_receiptInvoiceID != 0)
            {
                ReceiptInvoice rinvoice = new ReceiptInvoice(_receiptInvoiceID);
                //bind data to invoicedetail tab
                BindInvoiceDetail(rinvoice);

                lblOrderNumber.Text = rinvoice.PO.PONumber;

                lblSupplier.Text = rinvoice.PO.Supplier.IsActive ? rinvoice.PO.Supplier.CompanyName : "";
                lblRefNo.Text = rinvoice.PO.RefNo != "" ? rinvoice.PO.RefNo : "-";
                gridGrv.DataSource = rinvoice.Receipts.DefaultView;
                txtInsurance.Text = rinvoice.InsurancePolicyNo;
                txtInvoiceNumber.Text = rinvoice.STVOrInvoiceNo;
                txtTransitNumber.Text = rinvoice.TransitTransferNo;
                txtWayBill.Text = rinvoice.WayBillNo;
                dtInvoiceDate.EditValue = !rinvoice.IsColumnNull("DateofEntry")
                    ? rinvoice.DateOfEntry
                    : DateTimeHelper.ServerDateTime;
                txtTotalValue.EditValue = !rinvoice.IsColumnNull("TotalValue") ? rinvoice.TotalValue : 0;
                lkInvoiceType.EditValue = !rinvoice.IsColumnNull("InvoiceTypeID") ? rinvoice.InvoiceTypeID : 0;
                txtInvoiceAirFreight.EditValue = !rinvoice.IsColumnNull("AirFreight") ? rinvoice.AirFreight : 0;
                txtInvoiceSeaFreight.EditValue = !rinvoice.IsColumnNull("SeaFreight") ? rinvoice.SeaFreight : 0;
                txtInvoiceInlandFreight.EditValue = !rinvoice.IsColumnNull("InlandFreight") ? rinvoice.InlandFreight : 0;
                txtInvoiceCustomDutyTax.EditValue = !rinvoice.IsColumnNull("CustomDutyTax") ? rinvoice.CustomDutyTax : 0;
                txtInvoiceCBEServiceCharge.EditValue = !rinvoice.IsColumnNull("CBE") ? rinvoice.CBE : 0;
                txtExchangeRate.EditValue = !rinvoice.IsColumnNull("ExchangeRate") ? rinvoice.ExchangeRate : 1;
                lkCurrencyLCID.EditValue = _currency = !rinvoice.IsColumnNull("LCID") ? rinvoice.LCID : 0;
                lkDocumentType.EditValue = !rinvoice.IsColumnNull("DocumentTypeID") ? rinvoice.DocumentTypeID : -1;
                DisableEditDependingOnSetting();
                var activity = new Activity();
                activity.LoadByPrimaryKey(rinvoice.ActivityID);
                if (activity.IsHealthProgram())
                {
                    txtInvoiceCustomDutyTax.EditValue = 0;
                    txtInvoiceCustomDutyTax.Enabled = false;
                }

                // var receiptINvoice = new ReceiptInvoice();
                // receiptINvoice.LoadForPO(_orderID);
                lblInvoiceNoDetail.Text = rinvoice.STVOrInvoiceNo.ToString();
                lblInvoiceTypeDetail.Text = lkInvoiceType.Text;

            }
            else
            {
                PO PO = new PO(_orderID);
                lblOrderNumber.Text = PO.PONumber;

                BindInvoiceDetailByPO(PO.ID);

                lblSupplier.Text = PO.Supplier.IsActive ? PO.Supplier.CompanyName : "_";
                lkCurrencyLCID.EditValue = 1118; //ETB is the dafault
                lblRefNo.Text = PO.RefNo != "" ? PO.RefNo : "-";

                if (PO.StoreID != null)
                {
                    var activity = new Activity();
                    activity.LoadByPrimaryKey(po.StoreID);
                    if (activity.IsHealthProgram())
                    {
                        txtInvoiceCustomDutyTax.EditValue = 0;
                        txtInvoiceCustomDutyTax.Enabled = false;
                    }
                }
                layoutGoodsReceived.Enabled = false;
            }
            //gridViewInvoiceDetail.ValidateRow += GridViewInvoiceDetailOnValidateRow;
        }