protected void LoadExpDoc(BEXPORT_DOCUMETARYCOLLECTION expDoc)
        {
            if (expDoc != null)
            {
                //mainToolbar.FindItemByValue("btReview").Enabled = false;

                //var drow = dsDoc.Tables[0].Rows[0];

                #region Load Export Collection
                comboCollectionType.SelectedValue = expDoc.CollectionType;
                lblCollectionTypeName.Text = comboCollectionType.SelectedItem.Attributes["Description"];

                comboDrawerCusNo.SelectedValue = expDoc.DrawerCusNo;
                txtDrawerCusName.Text = expDoc.DrawerCusName;
                txtDrawerAddr1.Text = expDoc.DrawerAddr1;
                txtDrawerAddr2.Text = expDoc.DrawerAddr2;
                txtDrawerAddr3.Text = expDoc.DrawerAddr3;
                txtDrawerRefNo.Text = expDoc.DrawerRefNo;
                comboCollectingBankNo.SelectedValue = expDoc.CollectingBankNo;
                txtCollectingBankName.Text = expDoc.CollectingBankName;
                txtCollectingBankAddr1.Text = expDoc.CollectingBankAddr1;
                txtCollectingBankAddr2.Text = expDoc.CollectingBankAddr2;
                comboCollectingBankAcct.SelectedValue = expDoc.CollectingBankAcct;
                txtDraweeCusNo.Text = expDoc.DraweeCusNo;
                txtDraweeCusName.Text = expDoc.DraweeCusName;
                txtDraweeAddr1.Text = expDoc.DraweeAddr1;
                txtDraweeAddr2.Text = expDoc.DraweeAddr2;
                txtDraweeAddr3.Text = expDoc.DraweeAddr3;
                comboNostroCusNo.SelectedValue = expDoc.NostroCusNo;
                lblNostroCusName.Text = comboNostroCusNo.SelectedItem.Attributes["Description"];
                comboCreditCurrency.SelectedValue = expDoc.Currency;
                comboCurrency.SelectedValue = expDoc.Currency;
                loadNostroAccount();
                LoadCreditAccount();
                numAmount.Value = expDoc.Amount;
                txtTenor.Text = expDoc.Tenor;
                numReminderDays.Text = (expDoc.ReminderDays??0).ToString();

                comboCommodity.SelectedValue = expDoc.Commodity;
                txtCommodityName.Text = comboCommodity.SelectedItem.Attributes["Name2"];

                comboDocsCode1.SelectedValue = expDoc.DocsCode1;
                numNoOfOriginals1.Text = (expDoc.NoOfOriginals1??0).ToString();
                numNoOfCopies1.Text = (expDoc.NoOfCopies1??0).ToString();

                comboDocsCode2.SelectedValue = expDoc.DocsCode2;
                numNoOfOriginals2.Text = (expDoc.NoOfOriginals2 ?? 0).ToString();
                numNoOfCopies2.Text = (expDoc.NoOfCopies2 ?? 0).ToString();

                comboDocsCode3.SelectedValue = expDoc.DocsCode3;
                numNoOfOriginals3.Text = (expDoc.NoOfOriginals3 ?? 0).ToString();
                numNoOfCopies3.Text = (expDoc.NoOfCopies3 ?? 0).ToString();

                divDocsCode2.Visible = (expDoc.NoOfCopies2 ?? 0) > 0 || (expDoc.NoOfOriginals2 ?? 0) > 0;
                divDocsCode3.Visible = (expDoc.NoOfCopies3 ?? 0) > 0 || (expDoc.NoOfOriginals3 ?? 0) > 0;

                txtOtherDocs.Text = expDoc.OtherDocs;
                txtRemarks.Text = expDoc.Remarks;

                if (expDoc.DocsReceivedDate.HasValue && expDoc.DocsReceivedDate.Value != new DateTime(1900,1,1))
                {
                    dteDocsReceivedDate.SelectedDate = expDoc.DocsReceivedDate.Value;
                }
                if (expDoc.MaturityDate.HasValue && expDoc.MaturityDate.Value != new DateTime(1900, 1, 1))
                {
                    dteMaturityDate.SelectedDate = expDoc.MaturityDate.Value;
                }
                if (expDoc.TracerDate.HasValue && expDoc.TracerDate.Value != new DateTime(1900, 1, 1))
                {
                    dteTracerDate.SelectedDate = expDoc.TracerDate.Value;
                }

                comboCurrencyMt910.SelectedValue = comboCreditCurrency.SelectedValue;
                //bc.Commont.initRadComboBox(ref cboNostroAcct, "Code", "AccountNo", _entities.BSWIFTCODEs.Where(q => q.Currency.Equals(expDoc.Currency)).ToList());//
                //cboNostroAcct
                #endregion
            }
            else
            {
                comboCollectionType.SelectedValue = string.Empty;
                lblCollectionTypeName.Text = string.Empty;

                comboNostroCusNo.SelectedValue = string.Empty;
                txtDrawerCusName.Text = string.Empty;
                txtDrawerAddr1.Text = string.Empty;
                txtDrawerAddr2.Text = string.Empty;
                txtDrawerAddr3.Text = string.Empty;
                txtDrawerRefNo.Text = string.Empty;
                comboCollectingBankNo.SelectedValue = string.Empty;
                txtCollectingBankName.Text = string.Empty;
                txtCollectingBankAddr1.Text = string.Empty;
                txtCollectingBankAddr2.Text = string.Empty;
                comboCollectingBankAcct.SelectedValue = string.Empty;
                txtDraweeCusName.Text = string.Empty;
                txtDraweeAddr1.Text = string.Empty;
                txtDraweeAddr2.Text = string.Empty;
                txtDraweeAddr3.Text = string.Empty;
                comboNostroCusNo.SelectedValue = string.Empty;
                comboCurrency.SelectedValue = string.Empty;
                numAmount.Text = string.Empty;
                txtTenor.Text = "AT SIGHT";
                numReminderDays.Text = string.Empty;

                comboCommodity.SelectedValue = string.Empty;
                comboDocsCode1.SelectedValue = string.Empty;
                numNoOfOriginals1.Text = string.Empty;
                numNoOfCopies1.Text = string.Empty;

                comboDocsCode2.SelectedValue = string.Empty;
                numNoOfOriginals2.Text = string.Empty;
                numNoOfCopies2.Text = string.Empty;

                txtOtherDocs.Text = string.Empty;
                txtRemarks.Text = string.Empty;

                dteDocsReceivedDate.SelectedDate = null;
                dteMaturityDate.SelectedDate = null;
                dteTracerDate.SelectedDate = null;

            }

            //SetVisibilityByStatus(dsDoc);
        }
 private bool CheckStatusExportDocumentCollection(BEXPORT_DOCUMETARYCOLLECTION expDoc)
 {
     if (expDoc.Status != "AUT" || expDoc.Cancel_Status == "UNA" || (expDoc.Amend_Status != null && expDoc.Amend_Status != "AUT"))
     {
         lblError.Text = "Document was not authorized";
         return false;
     }
     if (expDoc.Cancel_Status == "AUT")
     {
         lblError.Text = "Document was canceled";
         return false;
     }
     if ((expDoc.PaymentFullFlag??0) == 1)
     {
         lblError.Text = "This document was paid full";
         return false;
     }
     return true;
 }
        protected void SaveDataAmend()
        {
            try
            {
                int AmendPreId = 0;
                string AmendNo = "";
                var Code = txtCode.Text.Trim();
                var objPreAmend = new BEXPORT_DOCUMETARYCOLLECTION();
                if (!String.IsNullOrEmpty(Code))
                {
                    if (Code.IndexOf('.') != -1)
                    {

                        var findTypeAmend = Code.Split('.');
                        if (findTypeAmend != null && findTypeAmend.Length > 0)
                        {
                            if (findTypeAmend.Length == 2)
                            {
                                var chkAmend = _entities.BEXPORT_DOCUMETARYCOLLECTION.Where(x => x.AmendNo == Code).FirstOrDefault();
                                //truong hop chua co
                                if (chkAmend == null)
                                {
                                    if (!String.IsNullOrEmpty(findTypeAmend[1]))
                                    {
                                        AmendPreId = int.Parse(findTypeAmend[1]) - 1;
                                    }
                                    if (AmendPreId > 0)
                                    {
                                        ///xet Amend truoc do
                                        ///
                                        AmendNo = findTypeAmend[0] + "." + AmendPreId;

                                        objPreAmend = _entities.BEXPORT_DOCUMETARYCOLLECTION.Where(x => x.AmendNo == AmendNo).FirstOrDefault();
                                        if (objPreAmend != null)
                                        {
                                            objPreAmend.ActiveRecordFlag = YesNo.NO;
                                        }
                                    }
                                    else
                                    {
                                        //nguoc lai update Status cho Amend goc
                                        AmendNo = findTypeAmend[0];
                                        objPreAmend = _entities.BEXPORT_DOCUMETARYCOLLECTION.Where(x => x.DocCollectCode == AmendNo).FirstOrDefault();
                                        if (objPreAmend != null)
                                        {
                                            objPreAmend.ActiveRecordFlag = YesNo.NO;
                                        }
                                    }
                                    _entities.SaveChanges();
                                    //them moi dong hien tai
                                    BEXPORT_DOCUMETARYCOLLECTION objInsert = new BEXPORT_DOCUMETARYCOLLECTION
                                    {
                                        DocCollectCode = findTypeAmend[0],
                                        DrawerCusNo = comboDrawerCusNo.SelectedValue,
                                        DrawerCusName = txtDrawerCusName.Text.Trim(),
                                        DrawerAddr1 = txtDrawerAddr1.Text.Trim(),
                                        DrawerAddr2 = txtDrawerAddr2.Text.Trim(),
                                        DrawerAddr3 = txtDrawerAddr3.Text.Trim(),
                                        DrawerRefNo = txtDrawerRefNo.Text.Trim(),
                                        CollectingBankNo = comboCollectingBankNo.Text,
                                        CollectingBankName = txtCollectingBankName.Text.Trim(),
                                        CollectingBankAddr1 = txtCollectingBankAddr1.Text.Trim(),
                                        CollectingBankAddr2 = txtCollectingBankAddr2.Text.Trim(),
                                        CollectingBankAddr3 = txtCollectingBankAddr3.Text.Trim(),
                                        CollectingBankAcct = comboCollectingBankAcct.SelectedValue,
                                        DraweeCusNo = txtDraweeCusNo.Text.Trim(),
                                        DraweeCusName = txtDraweeCusName.Text.Trim(),
                                        DraweeAddr1 = txtDraweeAddr1.Text.Trim(),
                                        DraweeAddr2 = txtDraweeAddr2.Text.Trim(),
                                        DraweeAddr3 = txtDraweeAddr3.Text.Trim(),
                                        DraweeAddr4 = txtDraweeAddr4.Text.Trim(),
                                        NostroCusNo = comboNostroCusNo.Text,
                                        Currency = comboCurrency.SelectedValue,
                                        Amount = numAmount.Value,
                                        DocsReceivedDate = dteDocsReceivedDate.SelectedDate,
                                        MaturityDate = dteMaturityDate.SelectedDate,
                                        Tenor = txtTenor.Text.Trim(),
                                        TracerDate = dteTracerDate.SelectedDate,
                                        Commodity = comboCommodity.SelectedValue,
                                        DocsCode1 = comboDocsCode1.SelectedValue,
                                        DocsCode2 = comboDocsCode2.SelectedValue,
                                        DocsCode3 = comboDocsCode3.SelectedValue,
                                        OtherDocs = txtOtherDocs.Text.Trim(),
                                        Remarks = txtRemarks.Text.Trim(),
                                        Remarks1 = txtRemarks1.Text.Trim(),
                                        Remarks2 = txtRemarks2.Text.Trim(),
                                        Remarks3 = txtRemarks3.Text.Trim(),
                                        CreateDate = DateTime.Now,
                                        CreateBy = UserId.ToString(),
                                        CollectionType = comboCollectionType.SelectedValue,
                                        CancelDate = dteCancelDate.SelectedDate,
                                        ContingentExpiryDate = dteContingentExpiryDate.SelectedDate,
                                        CancelRemark = txtCancelRemark.Text.Trim(),
                                        Accountofficer = comboAccountOfficer.SelectedValue,
                                        AcceptedDate = dtAcceptDate.SelectedDate,
                                        AcceptedRemarks = txtAcceptREmark.Text.Trim(),
                                        //Amend
                                        Amend_Status = "UNA",
                                        AmendBy = UserId.ToString(),
                                        AmendNo = txtCode.Text,
                                        RefAmendNo = findTypeAmend[0],
                                        ActiveRecordFlag=YesNo.YES,
                                        Status="AUT"

                                        //
                                    };
                                    if (objPreAmend != null)
                                    {

                                        objInsert.OldAmount = objPreAmend.Amount;
                                        objInsert.OldDocsReceivedDate = objPreAmend.OldDocsReceivedDate;
                                    }
                                    if (!String.IsNullOrEmpty(findTypeAmend[1]))
                                    {
                                        objInsert.AmendId = int.Parse(findTypeAmend[1]);
                                    }
                                    if (!String.IsNullOrEmpty(numReminderDays.Value.ToString()))
                                    {
                                        objInsert.ReminderDays = long.Parse(numReminderDays.Value.ToString());
                                    }
                                    if (!String.IsNullOrEmpty(numNoOfOriginals1.Value.ToString()))
                                    {
                                        objInsert.NoOfOriginals1 = int.Parse(numNoOfOriginals1.Value.ToString());
                                    }
                                    if (!String.IsNullOrEmpty(numNoOfCopies1.Value.ToString()))
                                    {
                                        objInsert.NoOfCopies1 = int.Parse(numNoOfCopies1.Value.ToString());
                                    }
                                    if (!String.IsNullOrEmpty(numNoOfOriginals2.Value.ToString()))
                                    {
                                        objInsert.NoOfOriginals2 = int.Parse(numNoOfOriginals2.Value.ToString());
                                    }
                                    if (!String.IsNullOrEmpty(numNoOfCopies2.Value.ToString()))
                                    {
                                        objInsert.NoOfCopies2 = int.Parse(numNoOfCopies2.Value.ToString());
                                    }
                                    if (!String.IsNullOrEmpty(numNoOfOriginals3.Value.ToString()))
                                    {
                                        objInsert.NoOfOriginals3 = int.Parse(numNoOfOriginals3.Value.ToString());
                                    }
                                    if (!String.IsNullOrEmpty(numNoOfCopies3.Value.ToString()))
                                    {
                                        objInsert.NoOfCopies3 = int.Parse(numNoOfCopies3.Value.ToString());
                                    }
                                    _entities.BEXPORT_DOCUMETARYCOLLECTION.Add(objInsert);
                                    _entities.SaveChanges();
                                }
                                else
                                {
                                    chkAmend.DocCollectCode = findTypeAmend[0];
                                    chkAmend.DrawerCusNo = comboDrawerCusNo.SelectedValue;
                                    chkAmend.DrawerCusName = txtDrawerCusName.Text.Trim();
                                    chkAmend.DrawerAddr1 = txtDrawerAddr1.Text.Trim();
                                    chkAmend.DrawerAddr2 = txtDrawerAddr2.Text.Trim();
                                    chkAmend.DrawerAddr3 = txtDrawerAddr3.Text.Trim();
                                    chkAmend.DrawerRefNo = txtDrawerRefNo.Text.Trim();
                                    chkAmend.CollectingBankNo = comboCollectingBankNo.Text;
                                    chkAmend.CollectingBankName = txtCollectingBankName.Text.Trim();
                                    chkAmend.CollectingBankAddr1 = txtCollectingBankAddr1.Text.Trim();
                                    chkAmend.CollectingBankAddr2 = txtCollectingBankAddr2.Text.Trim();
                                    chkAmend.CollectingBankAddr3 = txtCollectingBankAddr3.Text.Trim();
                                    chkAmend.CollectingBankAcct = comboCollectingBankAcct.SelectedValue;
                                    chkAmend.DraweeCusNo = txtDraweeCusNo.Text.Trim();
                                    chkAmend.DraweeCusName = txtDraweeCusName.Text.Trim();
                                    chkAmend.DraweeAddr1 = txtDraweeAddr1.Text.Trim();
                                    chkAmend.DraweeAddr2 = txtDraweeAddr2.Text.Trim();
                                    chkAmend.DraweeAddr3 = txtDraweeAddr3.Text.Trim();
                                    chkAmend.DraweeAddr4 = txtDraweeAddr4.Text.Trim();
                                    chkAmend.NostroCusNo = comboNostroCusNo.Text;
                                    chkAmend.Currency = comboCurrency.SelectedValue;
                                    chkAmend.Amount = numAmount.Value;
                                    chkAmend.DocsReceivedDate = dteDocsReceivedDate.SelectedDate;
                                    chkAmend.MaturityDate = dteMaturityDate.SelectedDate;
                                    chkAmend.Tenor = txtTenor.Text.Trim();
                                    chkAmend.TracerDate = dteTracerDate.SelectedDate;
                                    chkAmend.Commodity = comboCommodity.SelectedValue;
                                    chkAmend.DocsCode1 = comboDocsCode1.SelectedValue;
                                    chkAmend.DocsCode2 = comboDocsCode2.SelectedValue;
                                    chkAmend.DocsCode3 = comboDocsCode3.SelectedValue;
                                    chkAmend.OtherDocs = txtOtherDocs.Text.Trim();
                                    chkAmend.Remarks = txtRemarks.Text.Trim();
                                    chkAmend.Remarks1 = txtRemarks1.Text.Trim();
                                    chkAmend.Remarks2 = txtRemarks2.Text.Trim();
                                    chkAmend.Remarks3 = txtRemarks3.Text.Trim();
                                    chkAmend.UpdatedBy = UserId.ToString();
                                    chkAmend.UpdatedDate = DateTime.Now;
                                    chkAmend.CollectionType = comboCollectionType.SelectedValue;
                                    chkAmend.CancelDate = dteCancelDate.SelectedDate;
                                    chkAmend.ContingentExpiryDate = dteContingentExpiryDate.SelectedDate;
                                    chkAmend.CancelRemark = txtCancelRemark.Text.Trim();
                                    chkAmend.Accountofficer = comboAccountOfficer.SelectedValue;
                                    chkAmend.AcceptedDate = dtAcceptDate.SelectedDate;
                                    chkAmend.AcceptedRemarks = txtAcceptREmark.Text.Trim();
                                    //Amend
                                    chkAmend.Amend_Status = "UNA";
                                    chkAmend.AmendBy = UserId.ToString();
                                    chkAmend.AmendNo = txtCode.Text;
                                    chkAmend.RefAmendNo = findTypeAmend[0];
                                    chkAmend.Status = "AUT";
                                    if (!String.IsNullOrEmpty(findTypeAmend[1]))
                                    {
                                        chkAmend.AmendId = int.Parse(findTypeAmend[1]);
                                    }
                                    if (!String.IsNullOrEmpty(numReminderDays.Value.ToString()))
                                    {
                                        chkAmend.ReminderDays = long.Parse(numReminderDays.Value.ToString());
                                    }
                                    if (!String.IsNullOrEmpty(numNoOfOriginals1.Value.ToString()))
                                    {
                                        chkAmend.NoOfOriginals1 = int.Parse(numNoOfOriginals1.Value.ToString());
                                    }
                                    if (!String.IsNullOrEmpty(numNoOfCopies1.Value.ToString()))
                                    {
                                        chkAmend.NoOfCopies1 = int.Parse(numNoOfCopies1.Value.ToString());
                                    }
                                    if (!String.IsNullOrEmpty(numNoOfOriginals2.Value.ToString()))
                                    {
                                        chkAmend.NoOfOriginals2 = int.Parse(numNoOfOriginals2.Value.ToString());
                                    }
                                    if (!String.IsNullOrEmpty(numNoOfCopies2.Value.ToString()))
                                    {
                                        chkAmend.NoOfCopies2 = int.Parse(numNoOfCopies2.Value.ToString());
                                    }
                                    if (!String.IsNullOrEmpty(numNoOfOriginals3.Value.ToString()))
                                    {
                                        chkAmend.NoOfOriginals3 = int.Parse(numNoOfOriginals3.Value.ToString());
                                    }
                                    if (!String.IsNullOrEmpty(numNoOfCopies3.Value.ToString()))
                                    {
                                        chkAmend.NoOfCopies3 = int.Parse(numNoOfCopies3.Value.ToString());
                                    }
                                    _entities.SaveChanges();
                                }
                                //save phan Charge
                                var name = findTypeAmend[0];
                                var lstCharge = _entities.BEXPORT_DOCUMETARYCOLLECTIONCHARGES.Where(x => x.DocCollectCode == name && x.TabId == TabId).ToList();
                                //truong hop da co -->update
                                if (lstCharge != null && lstCharge.Count > 0)
                                {
                                    String[] chargeCode = { "EC.AMEND", "EC.CABLE", "EC.COURIER", "EC.OTHER" };
                                    var listChargeCode = chargeCode.ToList();
                                    foreach (var item in lstCharge)
                                    {
                                        if (item.Chargecode == "EC.AMEND")
                                        {
                                            listChargeCode.Remove(item.Chargecode);
                                            item.DocCollectCode = findTypeAmend[0];
                                            item.WaiveCharges = comboWaiveCharges.SelectedValue;
                                            item.Chargecode = tbChargeCode.SelectedValue;
                                            item.ChargeAcct = rcbChargeAcct.SelectedValue;
                                            item.ChargeCcy = rcbChargeCcy.SelectedValue;
                                            if (!String.IsNullOrEmpty(tbChargeAmt.Value.ToString()))
                                            {
                                                item.ChargeAmt = decimal.Parse(tbChargeAmt.Value.ToString());
                                            }
                                            item.PartyCharged = rcbPartyCharged.SelectedValue;
                                            item.OmortCharges = rcbOmortCharge.SelectedValue;
                                            item.ChargeStatus = rcbChargeStatus.SelectedValue;
                                            item.ChargeRemarks = tbChargeRemarks.Text.Trim();
                                            item.VATNo = tbVatNo.Text;
                                            item.TaxCode = lblTaxCode.Text;
                                            item.TaxAmt = lblTaxAmt.Text;
                                            item.Rowchages = "1";
                                            item.TabId = TabId;
                                            item.AmendNo = Code;
                                            if(!String.IsNullOrEmpty(findTypeAmend[1].ToString()))
                                            {
                                                item.AmendId = int.Parse(findTypeAmend[1].ToString());
                                            }
                                        }
                                        else if (item.Chargecode == "EC.CABLE")
                                        {
                                            listChargeCode.Remove(item.Chargecode);
                                            item.DocCollectCode = findTypeAmend[0];
                                            item.WaiveCharges = comboWaiveCharges.SelectedValue;
                                            item.Chargecode = tbChargeCode2.SelectedValue;
                                            item.ChargeAcct = rcbChargeAcct2.SelectedValue;
                                            item.ChargeCcy = rcbChargeCcy2.SelectedValue;
                                            if (!String.IsNullOrEmpty(tbChargeAmt2.Value.ToString()))
                                            {
                                                item.ChargeAmt = decimal.Parse(tbChargeAmt2.Value.ToString());
                                            }
                                            item.PartyCharged = rcbPartyCharged2.SelectedValue;
                                            item.OmortCharges = rcbOmortCharge2.SelectedValue;
                                            item.ChargeStatus = rcbChargeStatus2.SelectedValue;
                                            item.ChargeRemarks = tbChargeRemarks.Text.Trim();
                                            item.VATNo = tbVatNo.Text;
                                            item.TaxCode = lblTaxCode2.Text;
                                            item.TaxAmt = lblTaxAmt2.Text;
                                            item.Rowchages = "2";
                                            item.TabId = TabId;
                                            item.AmendNo = Code;
                                            if (!String.IsNullOrEmpty(findTypeAmend[1].ToString()))
                                            {
                                                item.AmendId = int.Parse(findTypeAmend[1].ToString());
                                            }
                                        }
                                        else if (item.Chargecode == "EC.COURIER")
                                        {
                                            listChargeCode.Remove(item.Chargecode);
                                            item.DocCollectCode = findTypeAmend[0];
                                            item.WaiveCharges = comboWaiveCharges.SelectedValue;
                                            item.Chargecode = tbChargeCode3.SelectedValue;
                                            item.ChargeAcct = rcbChargeAcct3.SelectedValue;
                                            item.ChargeCcy = rcbChargeCcy3.SelectedValue;
                                            if (!String.IsNullOrEmpty(tbChargeAmt3.Value.ToString()))
                                            {
                                                item.ChargeAmt = decimal.Parse(tbChargeAmt3.Value.ToString());
                                            }
                                            item.PartyCharged = rcbPartyCharged3.SelectedValue;
                                            item.OmortCharges = rcbOmortCharge3.SelectedValue;
                                            item.ChargeStatus = rcbChargeStatus3.SelectedValue;
                                            item.ChargeRemarks = tbChargeRemarks.Text.Trim();
                                            item.VATNo = tbVatNo.Text;
                                            item.TaxCode = lblTaxCode3.Text;
                                            item.TaxAmt = lblTaxAmt3.Text;
                                            item.Rowchages = "3";
                                            item.TabId = TabId;
                                            item.AmendNo = Code;
                                            if (!String.IsNullOrEmpty(findTypeAmend[1].ToString()))
                                            {
                                                item.AmendId = int.Parse(findTypeAmend[1].ToString());
                                            }
                                        }
                                        else if (item.Chargecode == "EC.OTHER")
                                        {
                                            listChargeCode.Remove(item.Chargecode);
                                            item.DocCollectCode = findTypeAmend[0];
                                            item.WaiveCharges = comboWaiveCharges.SelectedValue;
                                            item.Chargecode = tbChargeCode4.SelectedValue;
                                            item.ChargeAcct = rcbChargeAcct4.SelectedValue;
                                            item.ChargeCcy = rcbChargeCcy4.SelectedValue;
                                            if (!String.IsNullOrEmpty(tbChargeAmt4.Value.ToString()))
                                            {
                                                item.ChargeAmt = decimal.Parse(tbChargeAmt4.Value.ToString());
                                            }
                                            item.PartyCharged = rcbPartyCharged4.SelectedValue;
                                            item.OmortCharges = rcbOmortCharge4.SelectedValue;
                                            item.ChargeStatus = rcbChargeStatus4.SelectedValue;
                                            item.ChargeRemarks = tbChargeRemarks.Text.Trim();
                                            item.VATNo = tbVatNo.Text;
                                            item.TaxCode = lblTaxCode4.Text;
                                            item.TaxAmt = lblTaxAmt4.Text;
                                            item.Rowchages = "4";
                                            item.TabId = TabId;
                                        }
                                        _entities.SaveChanges();
                                    }

                                    //in case new charge code is added to existing item, add it to DB
                                    if (listChargeCode.Count > 0)
                                    {
                                        foreach (String item in listChargeCode)
                                        {
                                            var objCharge = addNewChagre(item, Code, findTypeAmend);
                                            _entities.BEXPORT_DOCUMETARYCOLLECTIONCHARGES.Add(objCharge);
                                            _entities.SaveChanges();
                                        }
                                    }
                                }

                                else
                                {

                                    var objCharge = addNewChagre("EC.AMEND", Code, findTypeAmend);
                                    _entities.BEXPORT_DOCUMETARYCOLLECTIONCHARGES.Add(objCharge);
                                    _entities.SaveChanges();

                                    objCharge = addNewChagre("EC.CABLE", Code, findTypeAmend);
                                    _entities.BEXPORT_DOCUMETARYCOLLECTIONCHARGES.Add(objCharge);
                                    _entities.SaveChanges();

                                    objCharge = addNewChagre("EC.COURIER", Code, findTypeAmend);
                                    _entities.BEXPORT_DOCUMETARYCOLLECTIONCHARGES.Add(objCharge);
                                    _entities.SaveChanges();

                                    objCharge = addNewChagre("EC.OTHER", Code, findTypeAmend);
                                    _entities.BEXPORT_DOCUMETARYCOLLECTIONCHARGES.Add(objCharge);
                                    _entities.SaveChanges();
                                }
                            }
                        }
                    }
                }
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dbEX)
            {
                Exception raise = dbEX;
                foreach (var validationError in dbEX.EntityValidationErrors)
                {
                    string message = string.Format("{0}:{1}", validationError.Entry.Entity.ToString(), validationError.ValidationErrors);
                    raise = new InvalidOperationException(message, raise);
                }
                throw raise;
            }
        }
        //load phan thong tin amend
        private void LoadExportDocAmend()
        {
            try
            {
                var Code = CodeId;
                if (Code != null)
                {
                    //nhap vao khong co dau .
                    if (Code.IndexOf('.') == -1)
                    {
                        var objAmend = new BEXPORT_DOCUMETARYCOLLECTION();
                        var lstDoc = _entities.BEXPORT_DOCUMETARYCOLLECTION.Where(x => (x.DocCollectCode == Code)).ToList();
                        if (lstDoc != null && lstDoc.Count > 0)
                        {

                            objAmend = lstDoc.Where(x => (x.ActiveRecordFlag == null || x.ActiveRecordFlag == YesNo.YES) && (x.Amend_Status == "UNA" || x.Amend_Status == "REV")).FirstOrDefault();
                            if (objAmend != null)
                            {
                                var objCharge = _entities.BEXPORT_DOCUMETARYCOLLECTIONCHARGES.Where(x => x.DocCollectCode == objAmend.DocCollectCode).ToList();
                                LoadDataForAmend(objAmend, objCharge);
                                txtCode.Text = objAmend.AmendNo;
                                _exportCollection = new BEXPORT_DOCUMETARYCOLLECTION();
                                _exportCollection = objAmend;
                            }
                            else
                            {
                                //truong hop chua co
                                var maxAmendId = lstDoc.Max(x => x.AmendId);
                                if (maxAmendId == null)
                                {
                                    maxAmendId = 1;
                                }
                                else
                                {
                                    maxAmendId = maxAmendId + 1;
                                }
                                var code = CodeId;
                                var ctnAmend = lstDoc.Where(x => (x.ActiveRecordFlag == null || x.ActiveRecordFlag == YesNo.YES)).FirstOrDefault();
                                if (ctnAmend != null)
                                {
                                    _exportCollection = new BEXPORT_DOCUMETARYCOLLECTION();
                                    _exportCollection = ctnAmend;

                                    var objCharge = new List<BEXPORT_DOCUMETARYCOLLECTIONCHARGES>();
                                    LoadDataForAmend(ctnAmend, objCharge);
                                }
                                RadToolBar1.FindItemByValue("btSave").Enabled = true;
                                txtCode.Text = code + "." + maxAmendId;
                                //
                            }
                        }
                    }
                    else
                    {
                        var lstDoc = _entities.BEXPORT_DOCUMETARYCOLLECTION.Where(x => x.AmendNo == Code).FirstOrDefault();
                        if (lstDoc != null)
                        {
                            var objCharge = _entities.BEXPORT_DOCUMETARYCOLLECTIONCHARGES.Where(x => x.DocCollectCode == lstDoc.DocCollectCode && x.AmendNo == Code).ToList();
                            LoadDataForAmend(lstDoc, objCharge);
                            txtCode.Text = lstDoc.AmendNo;
                            _exportCollection = new BEXPORT_DOCUMETARYCOLLECTION();
                            _exportCollection = lstDoc;
                        }
                        else
                        {
                            lblError.Text = "This Amend ID was not found";
                        }
                    }
                }
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dbEX)
            {
                Exception raise = dbEX;
                foreach (var validationError in dbEX.EntityValidationErrors)
                {
                    string message = string.Format("{0}:{1}", validationError.Entry.Entity.ToString(), validationError.ValidationErrors);
                    raise = new InvalidOperationException(message, raise);
                }
                lblError.Text = raise.Message;
                throw raise;
            }
        }
        protected void LoadDataForAmend(BEXPORT_DOCUMETARYCOLLECTION obj, List<BEXPORT_DOCUMETARYCOLLECTIONCHARGES> lstCharge)
        {
            try
            {
                if (obj != null)
                {
                    RadToolBar1.FindItemByValue("btReview").Enabled = false;

                    if (!String.IsNullOrEmpty(obj.OldAmount.ToString()))
                    {
                        AmountOld = double.Parse(obj.OldAmount.ToString());
                    }
                    if (!String.IsNullOrEmpty(obj.Amount.ToString()))
                    {
                        Amount = double.Parse(obj.Amount.ToString());
                    }

                    /*
                     * If this is first time Amend data, old amount will be 0
                     * In that case, show old amount as original amount
                     */
                    if (0 == AmountOld)
                    {
                        AmountOld = Amount;
                    }

                    // DocumentaryCollectionCancel
                    if (!string.IsNullOrEmpty(obj.CancelDate.ToString()) && obj.CancelDate.ToString().IndexOf("1/1/1900") == -1)
                    {
                        dteCancelDate.SelectedDate = DateTime.Parse(obj.CancelDate.ToString());
                    }
                    if (!string.IsNullOrEmpty(obj.ContingentExpiryDate.ToString()) && obj.ContingentExpiryDate.ToString().IndexOf("1/1/1900") == -1)
                    {
                        dteContingentExpiryDate.SelectedDate = DateTime.Parse(obj.ContingentExpiryDate.ToString());
                    }

                    if (string.IsNullOrEmpty(obj.Cancel_Status))
                    {
                        dteCancelDate.SelectedDate = DateTime.Now;
                        dteContingentExpiryDate.SelectedDate = DateTime.Now;
                    }

                    txtCancelRemark.Text = obj.CancelRemark;

                    // Outgoing Document Acception
                    if (!string.IsNullOrEmpty(obj.AcceptedDate.ToString()) && obj.AcceptedDate.ToString().IndexOf("1/1/1900") == -1)
                    {
                        dtAcceptDate.SelectedDate = DateTime.Parse(obj.AcceptedDate.ToString());
                    }

                    if (string.IsNullOrEmpty(obj.AcceptStatus))
                    {
                        dtAcceptDate.SelectedDate = DateTime.Now;
                    }

                    txtAcceptREmark.Text = obj.AcceptedRemarks;

                    ///////////////////////////////////////
                    // CC
                    if (obj.CollectionType == "CC")
                    {
                        comboCollectionType.Items.Clear();
                        comboCollectionType.DataValueField = "ID";
                        comboCollectionType.DataTextField = "ID";
                        comboCollectionType.DataSource = bd.SQLData.CreateGenerateDatas("DocumetaryCleanCollection_TabMain_CollectionType");
                        comboCollectionType.DataBind();
                        divCollectionType.Visible = false;
                        divDocsCode.Visible = false;
                        divDocsCode2.Visible = false;
                        divDocsCode3.Visible = false;

                    }
                    // end cc
                    comboCollectionType.SelectedValue = obj.CollectionType;
                    lblCollectionTypeName.Text = comboCollectionType.SelectedItem.Attributes["Description"];

                    comboAccountOfficer.SelectedValue = obj.Accountofficer;
                    comboDrawerCusNo.SelectedValue = obj.DrawerCusNo;
                    txtDrawerCusName.Text = obj.DrawerCusName;
                    txtDrawerAddr1.Text = obj.DrawerAddr1;
                    txtDrawerAddr2.Text = obj.DrawerAddr2;
                    txtDrawerAddr3.Text = obj.DrawerAddr3;
                    txtDrawerRefNo.Text = obj.DrawerRefNo;
                    comboCollectingBankNo.Text = obj.CollectingBankNo;
                    txtCollectingBankName.Text = obj.CollectingBankName;
                    txtCollectingBankAddr1.Text = obj.CollectingBankAddr1;
                    txtCollectingBankAddr2.Text = obj.CollectingBankAddr2;
                    comboCollectingBankAcct.SelectedValue = obj.CollectingBankAcct;
                    txtDraweeCusNo.Text = obj.DraweeCusNo;
                    txtDraweeCusName.Text = obj.DraweeCusName;
                    txtDraweeAddr1.Text = obj.DraweeAddr1;
                    txtDraweeAddr2.Text = obj.DraweeAddr2;
                    txtDraweeAddr3.Text = obj.DraweeAddr3;
                    txtDraweeAddr4.Text = obj.DraweeAddr4;
                    comboNostroCusNo.Text = obj.NostroCusNo;
                    loadNostroDes();
                    //lblNostroCusName.Text = comboNostroCusNo.SelectedItem.Attributes["Description"];
                    comboCurrency.SelectedValue = obj.Currency;
                    numAmount.Value = Amount;
                    lblAmount_New.Text = AmountOld.ToString("C");
                    txtTenor.Text = obj.Tenor;
                    numReminderDays.Text = obj.ReminderDays.ToString();

                    comboCommodity.SelectedValue = obj.Commodity;
                    //txtCommodityName.Text = comboCommodity.SelectedItem.Attributes["Name2;

                    comboDocsCode1.SelectedValue = obj.DocsCode1;
                    numNoOfOriginals1.Text = obj.NoOfOriginals1.ToString();
                    numNoOfCopies1.Text = obj.NoOfCopies1.ToString();

                    comboDocsCode2.SelectedValue = obj.DocsCode2;
                    numNoOfOriginals2.Text = obj.NoOfOriginals2.ToString();
                    numNoOfCopies2.Text = obj.NoOfCopies2.ToString();

                    comboDocsCode3.SelectedValue = obj.DocsCode3;
                    numNoOfOriginals3.Text = obj.NoOfOriginals3.ToString();
                    numNoOfCopies3.Text = obj.NoOfCopies3.ToString();

                    if ((!string.IsNullOrWhiteSpace(obj.NoOfOriginals2.ToString()) &&
                         int.Parse(obj.NoOfOriginals2.ToString()) > 0) ||
                        (!string.IsNullOrWhiteSpace(obj.NoOfCopies2.ToString()) &&
                         int.Parse(obj.NoOfCopies2.ToString()) > 0))
                    {
                        divDocsCode2.Visible = true;
                    }
                    if ((!string.IsNullOrWhiteSpace(obj.NoOfOriginals3.ToString()) &&
                         int.Parse(obj.NoOfOriginals3.ToString()) > 0) ||
                        (!string.IsNullOrWhiteSpace(obj.NoOfCopies3.ToString()) &&
                         int.Parse(obj.NoOfCopies3.ToString()) > 0))
                    {
                        divDocsCode3.Visible = true;
                    }

                    txtOtherDocs.Text = obj.OtherDocs;
                    txtRemarks.Text = obj.Remarks;
                    txtRemarks1.Text = obj.Remarks1;
                    txtRemarks2.Text = obj.Remarks2;
                    txtRemarks3.Text = obj.Remarks3;

                    if (!string.IsNullOrEmpty(obj.DocsReceivedDate.ToString()) && obj.DocsReceivedDate.ToString().IndexOf("1/1/1900") == -1)
                    {
                        dteDocsReceivedDate.SelectedDate = DateTime.Parse(obj.DocsReceivedDate.ToString());
                    }
                    if (!string.IsNullOrEmpty(obj.MaturityDate.ToString()) && obj.MaturityDate.ToString().IndexOf("1/1/1900") == -1)
                    {
                        dteMaturityDate.SelectedDate = DateTime.Parse(obj.MaturityDate.ToString());
                    }
                    if (!string.IsNullOrEmpty(obj.TracerDate.ToString()) && obj.TracerDate.ToString().IndexOf("1/1/1900") == -1)
                    {
                        dteTracerDate.SelectedDate = DateTime.Parse(obj.TracerDate.ToString());
                    }
                }
                else
                {
                    comboCollectionType.SelectedValue = string.Empty;
                    lblCollectionTypeName.Text = string.Empty;

                    comboNostroCusNo.Text = string.Empty;
                    txtDrawerCusName.Text = string.Empty;
                    txtDrawerAddr1.Text = string.Empty;
                    txtDrawerAddr2.Text = string.Empty;
                    txtDrawerAddr3.Text = string.Empty;
                    txtDrawerRefNo.Text = string.Empty;
                    comboCollectingBankNo.Text = string.Empty;
                    txtCollectingBankName.Text = string.Empty;
                    txtCollectingBankAddr1.Text = string.Empty;
                    txtCollectingBankAddr2.Text = string.Empty;
                    comboCollectingBankAcct.SelectedValue = string.Empty;
                    txtDraweeCusName.Text = string.Empty;
                    txtDraweeAddr1.Text = string.Empty;
                    txtDraweeAddr2.Text = string.Empty;
                    txtDraweeAddr3.Text = string.Empty;
                    txtDraweeAddr4.Text = string.Empty;
                    comboNostroCusNo.Text = string.Empty;
                    lblNostroCusName.Text = string.Empty;
                    comboCurrency.SelectedValue = string.Empty;
                    numAmount.Text = string.Empty;
                    txtTenor.Text = "AT SIGHT";
                    numReminderDays.Text = string.Empty;

                    comboCommodity.SelectedValue = string.Empty;
                    comboDocsCode1.SelectedValue = string.Empty;
                    numNoOfOriginals1.Text = string.Empty;
                    numNoOfCopies1.Text = string.Empty;

                    comboDocsCode2.SelectedValue = string.Empty;
                    numNoOfOriginals2.Text = string.Empty;
                    numNoOfCopies2.Text = string.Empty;

                    txtOtherDocs.Text = string.Empty;
                    txtRemarks.Text = string.Empty;
                    txtRemarks1.Text = string.Empty;
                    txtRemarks2.Text = string.Empty;
                    txtRemarks3.Text = string.Empty;

                    dteDocsReceivedDate.SelectedDate = null;
                    dteMaturityDate.SelectedDate = null;
                    dteTracerDate.SelectedDate = null;

                    Cal_TracerDate(false);
                }
                //khong load tab charge
                foreach (var item in lstCharge)
                {
                    if (item.Chargecode == "EC.AMEND")
                    {
                        comboWaiveCharges.SelectedValue = item.WaiveCharges;
                        rcbChargeAcct.SelectedValue = item.ChargeAcct;
                        //tbChargePeriod.Text = item.ChargePeriod;
                        rcbChargeCcy.SelectedValue = item.ChargeCcy;
                        if (!string.IsNullOrEmpty(rcbChargeCcy.SelectedValue))
                        {
                            LoadChargeAcct();
                        }
                        //tbExcheRate.Text = item.ExchRate;
                        tbChargeAmt.Text = item.ChargeAmt.ToString();
                        rcbPartyCharged.SelectedValue = item.PartyCharged;
                        rcbOmortCharge.SelectedValue = item.OmortCharges;
                        rcbChargeStatus.SelectedValue = item.ChargeStatus;
                        lblChargeStatus.Text = item.ChargeStatus;

                        tbChargeRemarks.Text = item.ChargeRemarks;
                        tbVatNo.Text = item.VATNo;
                        lblTaxCode.Text = item.TaxCode;
                        //lblTaxCcy.Text = item.TaxCcy;
                        lblTaxAmt.Text = item.TaxAmt;
                        tbChargeCode.SelectedValue = item.Chargecode;
                        ChargeAmount += ConvertStringToFloat(item.ChargeAmt.ToString());
                    }
                    else if (item.Chargecode == "EC.CABLE")
                    {
                        rcbChargeAcct2.SelectedValue = item.ChargeAcct;

                        rcbChargeCcy2.SelectedValue = item.ChargeCcy;
                        if (!string.IsNullOrEmpty(rcbChargeCcy2.SelectedValue))
                        {
                            LoadChargeAcct2();
                        }

                        tbChargeAmt2.Text = item.ChargeAmt.ToString();
                        rcbPartyCharged2.SelectedValue = item.PartyCharged;
                        rcbChargeStatus2.SelectedValue = item.ChargeStatus;
                        lblChargeStatus2.Text = item.ChargeStatus;

                        lblTaxCode2.Text = item.TaxCode;
                        lblTaxAmt2.Text = item.TaxAmt;

                        tbChargeCode2.SelectedValue = item.Chargecode;
                        ChargeAmount += ConvertStringToFloat(item.ChargeAmt.ToString());
                    }
                    else if (item.Chargecode == "EC.COURIER")
                    {
                        rcbChargeAcct3.SelectedValue = item.ChargeAcct;

                        rcbChargeCcy3.SelectedValue = item.ChargeCcy;
                        if (!string.IsNullOrEmpty(rcbChargeCcy3.SelectedValue))
                        {
                            LoadChargeAcct3();
                        }

                        tbChargeAmt3.Text = item.ChargeAmt.ToString();
                        rcbPartyCharged3.SelectedValue = item.PartyCharged;
                        //lblPartyCharged3.Text = item.PartyCharged;
                        rcbChargeStatus3.SelectedValue = item.ChargeStatus;
                        //lblChargeStatus3.Text = item.ChargeStatus;

                        lblTaxCode3.Text = item.TaxCode;
                        lblTaxAmt3.Text = item.TaxAmt;

                        tbChargeCode3.SelectedValue = item.Chargecode;
                        ChargeAmount += ConvertStringToFloat(item.ChargeAmt.ToString());
                    }
                    else if (item.Chargecode == "EC.OTHER")
                    {
                        rcbChargeAcct4.SelectedValue = item.ChargeAcct;

                        rcbChargeCcy4.SelectedValue = item.ChargeCcy;
                        if (!string.IsNullOrEmpty(rcbChargeCcy4.SelectedValue))
                        {
                            LoadChargeAcct4();
                        }

                        tbChargeAmt4.Text = item.ChargeAmt.ToString();
                        rcbPartyCharged4.SelectedValue = item.PartyCharged;
                        //lblPartyCharged4.Text = item.PartyCharged;
                        rcbChargeStatus4.SelectedValue = item.ChargeStatus;
                        //lblChargeStatus3.Text = item.ChargeStatus;

                        lblTaxCode4.Text = item.TaxCode;
                        lblTaxAmt4.Text = item.TaxAmt;

                        tbChargeCode4.SelectedValue = item.Chargecode;
                        ChargeAmount += ConvertStringToFloat(item.ChargeAmt.ToString());
                    }
                }
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dbEX)
            {
                Exception raise = dbEX;
                foreach (var validationError in dbEX.EntityValidationErrors)
                {
                    string message = string.Format("{0}:{1}", validationError.Entry.Entity.ToString(), validationError.ValidationErrors);
                    raise = new InvalidOperationException(message, raise);
                }
                lblError.Text = raise.Message;
                throw raise;
            }
            //SetVisibilityByStatus(dsDoc);
        }