Exemple #1
0
        /// <summary>
        /// Function to get particular values from PurchaseMaster table based on the parameter
        /// </summary>
        /// <param name="purchaseMasterId"></param>
        /// <returns></returns>
        public PurchaseMasterInfo PurchaseMasterView(Decimal purchaseMasterId)
        {
            PurchaseMasterInfo purchasemasterinfo = new PurchaseMasterInfo();
            SqlDataReader      sdrreader          = null;

            try
            {
                if (sqlcon.State == ConnectionState.Closed)
                {
                    sqlcon.Open();
                }
                SqlCommand sccmd = new SqlCommand("PurchaseMasterView", sqlcon);
                sccmd.CommandType = CommandType.StoredProcedure;
                SqlParameter sprmparam = new SqlParameter();
                sprmparam       = sccmd.Parameters.Add("@purchaseMasterId", SqlDbType.Decimal);
                sprmparam.Value = purchaseMasterId;
                sdrreader       = sccmd.ExecuteReader();
                while (sdrreader.Read())
                {
                    purchasemasterinfo.PurchaseMasterId        = decimal.Parse(sdrreader[0].ToString());
                    purchasemasterinfo.VoucherNo               = sdrreader[1].ToString();
                    purchasemasterinfo.InvoiceNo               = sdrreader[2].ToString();
                    purchasemasterinfo.SuffixPrefixId          = decimal.Parse(sdrreader[3].ToString());
                    purchasemasterinfo.VoucherTypeId           = decimal.Parse(sdrreader[4].ToString());
                    purchasemasterinfo.Date                    = DateTime.Parse(sdrreader[5].ToString());
                    purchasemasterinfo.LedgerId                = decimal.Parse(sdrreader[6].ToString());
                    purchasemasterinfo.VendorInvoiceNo         = sdrreader[7].ToString();
                    purchasemasterinfo.VendorInvoiceDate       = DateTime.Parse(sdrreader[8].ToString());
                    purchasemasterinfo.CreditPeriod            = sdrreader[9].ToString();
                    purchasemasterinfo.ExchangeRateId          = decimal.Parse(sdrreader[10].ToString());
                    purchasemasterinfo.Narration               = sdrreader[11].ToString();
                    purchasemasterinfo.PurchaseAccount         = decimal.Parse(sdrreader[12].ToString());
                    purchasemasterinfo.PurchaseOrderMasterId   = decimal.Parse(sdrreader[13].ToString());
                    purchasemasterinfo.MaterialReceiptMasterId = decimal.Parse(sdrreader[14].ToString());
                    purchasemasterinfo.AdditionalCost          = decimal.Parse(sdrreader[15].ToString());
                    purchasemasterinfo.TotalTax                = decimal.Parse(sdrreader[16].ToString());
                    purchasemasterinfo.BillDiscount            = decimal.Parse(sdrreader[17].ToString());
                    purchasemasterinfo.GrandTotal              = decimal.Parse(sdrreader[18].ToString());
                    purchasemasterinfo.TotalAmount             = decimal.Parse(sdrreader[19].ToString());
                    purchasemasterinfo.UserId                  = decimal.Parse(sdrreader[20].ToString());
                    purchasemasterinfo.LrNo                    = sdrreader[21].ToString();
                    purchasemasterinfo.TransportationCompany   = sdrreader[22].ToString();
                    purchasemasterinfo.FinancialYearId         = decimal.Parse(sdrreader[23].ToString());
                    purchasemasterinfo.ExtraDate               = DateTime.Parse(sdrreader[24].ToString());
                    purchasemasterinfo.Extra1                  = sdrreader[25].ToString();
                    purchasemasterinfo.Extra2                  = sdrreader[26].ToString();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                sdrreader.Close();
                sqlcon.Close();
            }
            return(purchasemasterinfo);
        }
Exemple #2
0
        /// <summary>
        /// Function to insert values to PurchaseMaster Table
        /// </summary>
        /// <param name="purchasemasterinfo"></param>
        /// <returns></returns>
        public decimal PurchaseMasterAdd(PurchaseMasterInfo purchasemasterinfo)
        {
            decimal decPurchaseMasterId = 0;

            try
            {
                if (sqlcon.State == ConnectionState.Closed)
                {
                    sqlcon.Open();
                }
                SqlCommand sccmd = new SqlCommand("PurchaseMasterAdd", sqlcon);
                sccmd.CommandType = CommandType.StoredProcedure;
                SqlParameter sprmparam = new SqlParameter();
                sprmparam           = sccmd.Parameters.Add("@voucherNo", SqlDbType.VarChar);
                sprmparam.Value     = purchasemasterinfo.VoucherNo;
                sprmparam           = sccmd.Parameters.Add("@invoiceNo", SqlDbType.VarChar);
                sprmparam.Value     = purchasemasterinfo.InvoiceNo;
                sprmparam           = sccmd.Parameters.Add("@suffixPrefixId", SqlDbType.Decimal);
                sprmparam.Value     = purchasemasterinfo.SuffixPrefixId;
                sprmparam           = sccmd.Parameters.Add("@voucherTypeId", SqlDbType.Decimal);
                sprmparam.Value     = purchasemasterinfo.VoucherTypeId;
                sprmparam           = sccmd.Parameters.Add("@date", SqlDbType.DateTime);
                sprmparam.Value     = purchasemasterinfo.Date;
                sprmparam           = sccmd.Parameters.Add("@ledgerId", SqlDbType.Decimal);
                sprmparam.Value     = purchasemasterinfo.LedgerId;
                sprmparam           = sccmd.Parameters.Add("@vendorInvoiceNo", SqlDbType.VarChar);
                sprmparam.Value     = purchasemasterinfo.VendorInvoiceNo;
                sprmparam           = sccmd.Parameters.Add("@vendorInvoiceDate", SqlDbType.DateTime);
                sprmparam.Value     = purchasemasterinfo.VendorInvoiceDate;
                sprmparam           = sccmd.Parameters.Add("@creditPeriod", SqlDbType.VarChar);
                sprmparam.Value     = purchasemasterinfo.CreditPeriod;
                sprmparam           = sccmd.Parameters.Add("@exchangeRateId", SqlDbType.Decimal);
                sprmparam.Value     = purchasemasterinfo.ExchangeRateId;
                sprmparam           = sccmd.Parameters.Add("@narration", SqlDbType.VarChar);
                sprmparam.Value     = purchasemasterinfo.Narration;
                sprmparam           = sccmd.Parameters.Add("@purchaseAccount", SqlDbType.Decimal);
                sprmparam.Value     = purchasemasterinfo.PurchaseAccount;
                sprmparam           = sccmd.Parameters.Add("@purchaseOrderMasterId", SqlDbType.Decimal);
                sprmparam.Value     = purchasemasterinfo.PurchaseOrderMasterId;
                sprmparam           = sccmd.Parameters.Add("@materialReceiptMasterId", SqlDbType.Decimal);
                sprmparam.Value     = purchasemasterinfo.MaterialReceiptMasterId;
                sprmparam           = sccmd.Parameters.Add("@additionalCost", SqlDbType.Decimal);
                sprmparam.Value     = purchasemasterinfo.AdditionalCost;
                sprmparam           = sccmd.Parameters.Add("@totalTax", SqlDbType.Decimal);
                sprmparam.Value     = purchasemasterinfo.TotalTax;
                sprmparam           = sccmd.Parameters.Add("@billDiscount", SqlDbType.Decimal);
                sprmparam.Value     = purchasemasterinfo.BillDiscount;
                sprmparam           = sccmd.Parameters.Add("@grandTotal", SqlDbType.Decimal);
                sprmparam.Value     = purchasemasterinfo.GrandTotal;
                sprmparam           = sccmd.Parameters.Add("@totalAmount", SqlDbType.Decimal);
                sprmparam.Value     = purchasemasterinfo.TotalAmount;
                sprmparam           = sccmd.Parameters.Add("@userId", SqlDbType.Decimal);
                sprmparam.Value     = purchasemasterinfo.UserId;
                sprmparam           = sccmd.Parameters.Add("@lrNo", SqlDbType.VarChar);
                sprmparam.Value     = purchasemasterinfo.LrNo;
                sprmparam           = sccmd.Parameters.Add("@transportationCompany", SqlDbType.VarChar);
                sprmparam.Value     = purchasemasterinfo.TransportationCompany;
                sprmparam           = sccmd.Parameters.Add("@financialYearId", SqlDbType.Decimal);
                sprmparam.Value     = purchasemasterinfo.FinancialYearId;
                sprmparam           = sccmd.Parameters.Add("@extraDate", SqlDbType.DateTime);
                sprmparam.Value     = purchasemasterinfo.ExtraDate;
                sprmparam           = sccmd.Parameters.Add("@extra1", SqlDbType.VarChar);
                sprmparam.Value     = purchasemasterinfo.Extra1;
                sprmparam           = sccmd.Parameters.Add("@extra2", SqlDbType.VarChar);
                sprmparam.Value     = purchasemasterinfo.Extra2;
                decPurchaseMasterId = Convert.ToDecimal(sccmd.ExecuteScalar());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                sqlcon.Close();
            }
            return(decPurchaseMasterId);
        }
        /// <summary>
        /// Function for Delete
        /// </summary>
        public void Delete()
        {
            try
            {
                StockPostingSP spStockPosting = new StockPostingSP();
                LedgerPostingSP spLedgerPosting = new LedgerPostingSP();
                PurchaseMasterSP SPPurchaseMaster = new PurchaseMasterSP();
                PurchaseReturnMasterSP SPPurchaseReturnMaster = new PurchaseReturnMasterSP();
                PurchaseMasterInfo infoPurchaseMaster = new PurchaseMasterInfo();
                PurchaseReturnMasterInfo infoPurchaseReturnMaster = new PurchaseReturnMasterInfo();
              
                infoPurchaseReturnMaster = SPPurchaseReturnMaster.PurchaseReturnMasterView(decPurchaseReturnMasterId);
                if (infoPurchaseReturnMaster.PurchaseMasterId == 0)
                {
                    spStockPosting.StockPostingDeleteByagainstVoucherTypeIdAndagainstVoucherNoAndVoucherNoAndVoucherType(0, "NA", infoPurchaseReturnMaster.VoucherNo, infoPurchaseReturnMaster.VoucherTypeId);
                }
                if (infoPurchaseReturnMaster.PurchaseMasterId != 0)
                {
                    infoPurchaseMaster = SPPurchaseMaster.PurchaseMasterView(infoPurchaseReturnMaster.PurchaseMasterId);
                    spStockPosting.StockPostingDeleteByagainstVoucherTypeIdAndagainstVoucherNoAndVoucherNoAndVoucherType(infoPurchaseMaster.VoucherTypeId, infoPurchaseMaster.VoucherNo, strVoucherNo, infoPurchaseReturnMaster.VoucherTypeId);
                }
                spLedgerPosting.LedgerPostingAndPartyBalanceDeleteByVoucherTypeIdAndLedgerIdAndVoucherNo(decPurchaseReturnVoucherTypeId, strVoucherNo, txtReturnNo.Text);
                SPPurchaseReturnMaster.PurchaseReturnMasterAndDetailsDelete(decPurchaseReturnMasterId);

                Messages.DeletedMessage();
                if (frmPurchaseReturnRegisterObj != null)
                {
                    this.Close();
                    frmPurchaseReturnRegisterObj.GridFill();
                }
                if (ObjPurchaseReturnReport != null)
                {
                    this.Close();
                    ObjPurchaseReturnReport.PurchaseReturnReportGridFill();
                }
                if (frmLedgerDetailsObj != null)
                {
                    this.Close();
                    frmLedgerDetailsObj.LedgerDetailsView();
                }
                if (objVoucherSearch != null)
                {
                    this.Close();
                    objVoucherSearch.GridFill();
                }
                if (frmDayBookObj != null)
                {
                    this.Close();
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("PR:50 " + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
 /// <summary>
 /// Call Corresponding Voucher to View details for updation in edit mode
 /// </summary>
 public void GotoPurchaseInvoice()
 {
     try
     {
         PurchaseMasterSP purchasemastersp = new PurchaseMasterSP();
         PurchaseMasterInfo purchasemasterinfo = new PurchaseMasterInfo();
         purchasemasterinfo = purchasemastersp.PurchaseMasterView(Convert.ToDecimal(dgvVoucherSearch.CurrentRow.Cells["Id"].Value.ToString()));
         if (purchasemasterinfo.PurchaseMasterId != 0)
         {
             frmPurchaseInvoice objfrmPurchaseInvoice = new frmPurchaseInvoice();
             frmPurchaseInvoice open = Application.OpenForms["frmPurchaseInvoice"] as frmPurchaseInvoice;
             if (open == null)
             {
                 objfrmPurchaseInvoice.WindowState = FormWindowState.Normal;
                 objfrmPurchaseInvoice.MdiParent = formMDI.MDIObj;
                 objfrmPurchaseInvoice.Show();
                 objfrmPurchaseInvoice.CallFromVoucherSerach(this, Convert.ToDecimal(dgvVoucherSearch.CurrentRow.Cells["Id"].Value.ToString()));
             }
             else
             {
                 open.MdiParent = formMDI.MDIObj;
                 if (open.WindowState == FormWindowState.Minimized)
                 {
                     open.WindowState = FormWindowState.Normal;
                 }
                 else
                 {
                     open.Activate();
                 }
               
                 open.CallFromVoucherSerach(this, Convert.ToDecimal(dgvVoucherSearch.CurrentRow.Cells["Id"].Value.ToString()));
                 open.BringToFront();
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("VS23:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
        /// <summary>
        /// Function for Save and Edit
        /// </summary>  
        public void SaveOrEdit()
        {
            try
            {
                PurchaseMasterSP SPPurchaseMaster = new PurchaseMasterSP();
                PurchaseReturnMasterSP SPPurchaseReturnMaster = new PurchaseReturnMasterSP();
                PurchaseReturnDetailsSP SPPurchaseReturnDetails = new PurchaseReturnDetailsSP();
                PurchaseReturnDetailsInfo infoPurchaseReturnDetails = new PurchaseReturnDetailsInfo();
                StockPostingInfo infoStockPosting = new StockPostingInfo();
                StockPostingSP spStockPosting = new StockPostingSP();
                UnitConvertionSP SPUnitConversion = new UnitConvertionSP();
                LedgerPostingSP spLedgerPosting = new LedgerPostingSP();
                LedgerPostingInfo infoLedgerPosting = new LedgerPostingInfo();
                ExchangeRateSP spExchangeRate = new ExchangeRateSP();
                PartyBalanceSP spPartyBalance = new PartyBalanceSP();
                PartyBalanceInfo infoPartyBalance = new PartyBalanceInfo();
                AccountLedgerInfo infoAccountLedger = new AccountLedgerInfo();
                AccountLedgerSP spAccountLedger = new AccountLedgerSP();
                PurchaseReturnBilltaxInfo infoPurchaseReturnBillTax = new PurchaseReturnBilltaxInfo();
                PurchaseReturnBilltaxSP spPurchaseReturnBillTax = new PurchaseReturnBilltaxSP();
                SettingsSP spSettings = new SettingsSP();
                UnitSP spUnit = new UnitSP();
                DataTable dtblPurchaseMasterViewById = new DataTable();
                string strAgainstVoucherNo = string.Empty;
                string strAgainstInvoiceNo = string.Empty;
                decimal decPurchaseReturnMasterIds = 0;
                decimal decPurchaseMasterId = 0;
                decimal decDiscount = 0;
                decimal decExchangeRate = 0;
                decimal decDis = 0;

                if (isAutomatic)
                {
                    if (strVoucherNo != string.Empty)
                    {
                        infoPurchaseReturnMaster.VoucherNo = strVoucherNo;
                    }
                    if (txtReturnNo.Text != string.Empty)
                    {
                        infoPurchaseReturnMaster.InvoiceNo = txtReturnNo.Text;
                    }
                }
                else
                {
                    infoPurchaseReturnMaster.VoucherNo = strVoucherNo;
                    infoPurchaseReturnMaster.InvoiceNo = txtReturnNo.Text;
                }
                if (decPurchaseReturnVoucherTypeId != 0)
                {
                    infoPurchaseReturnMaster.VoucherTypeId = decPurchaseReturnVoucherTypeId;
                }
                infoPurchaseReturnMaster.SuffixPrefixId = (decPurchaseReturnSuffixPrefixId != 0) ? decPurchaseReturnSuffixPrefixId : 0;
                infoPurchaseReturnMaster.LedgerId = Convert.ToDecimal(cmbCashOrParty.SelectedValue.ToString());
                infoPurchaseReturnMaster.PurchaseAccount = Convert.ToDecimal(cmbPurchaseAccount.SelectedValue.ToString());
                if (cmbInvoiceNo.SelectedValue != null && cmbInvoiceNo.Visible == true)
                {
                    infoPurchaseReturnMaster.PurchaseMasterId = Convert.ToDecimal((cmbInvoiceNo.SelectedValue.ToString()));
                    decPurchaseMasterId = Convert.ToDecimal((cmbInvoiceNo.SelectedValue.ToString()));
                }
                else
                {
                    infoPurchaseReturnMaster.PurchaseMasterId = 0;
                }
                infoPurchaseReturnMaster.ExchangeRateId = (cmbCurrency.SelectedValue != null) ? Convert.ToDecimal(cmbCurrency.SelectedValue.ToString()) : 0;
                infoPurchaseReturnMaster.Narration = txtNarration.Text.Trim();
                infoPurchaseReturnMaster.UserId = PublicVariables._decCurrentUserId;
                infoPurchaseReturnMaster.LrNo = txtLrlNo.Text.Trim();
                infoPurchaseReturnMaster.TransportationCompany = txtTransportationCompany.Text.Trim();
                infoPurchaseReturnMaster.Date = Convert.ToDateTime(txtDate.Text);
                infoPurchaseReturnMaster.TotalAmount = (txtTotalAmount.Text != string.Empty) ? Convert.ToDecimal(txtTotalAmount.Text) : 0;
                infoPurchaseReturnMaster.TotalTax = (lblTaxAmount.Text != string.Empty) ? Convert.ToDecimal(lblTaxAmount.Text) : 0;
                infoPurchaseReturnMaster.Discount = (txtBillDiscount.Text != string.Empty) ? Convert.ToDecimal(txtBillDiscount.Text) : 0;
                infoPurchaseReturnMaster.FinancialYearId = PublicVariables._decCurrentFinancialYearId;
                infoPurchaseReturnMaster.Extra1 = string.Empty;
                infoPurchaseReturnMaster.Extra2 = string.Empty;
                infoPurchaseReturnMaster.ExtraDate = DateTime.Now;
                infoPurchaseReturnMaster.GrandTotal = (txtGrandTotal.Text != string.Empty) ? Convert.ToDecimal(txtGrandTotal.Text) : 0;
                if (btnSave.Text == "Save")
                {
                    decPurchaseReturnMasterIds = SPPurchaseReturnMaster.PurchaseReturnMasterAddWithReturnIdentity(infoPurchaseReturnMaster);
                }
                else
                {
                    infoPurchaseReturnMaster.PurchaseReturnMasterId = decPurchaseReturnMasterId;
                    decExchangeRate = spExchangeRate.ExchangeRateViewByExchangeRateId(infoPurchaseReturnMaster.ExchangeRateId);
                    SPPurchaseReturnMaster.PurchaseReturnMasterEdit(infoPurchaseReturnMaster);
                    infoPurchaseMaster = SPPurchaseMaster.PurchaseMasterView(infoPurchaseReturnMaster.PurchaseMasterId);
                    spLedgerPosting.LedgerPostDelete(strVoucherNo, decPurchaseReturnVoucherTypeId);
                    spAccountLedger.PartyBalanceDeleteByVoucherTypeVoucherNoAndReferenceType(strVoucherNo, decPurchaseReturnVoucherTypeId);
                }

                infoLedgerPosting.Date = infoPurchaseReturnMaster.Date;
                infoLedgerPosting.VoucherTypeId = infoPurchaseReturnMaster.VoucherTypeId;
                infoLedgerPosting.VoucherNo = infoPurchaseReturnMaster.VoucherNo;
                infoLedgerPosting.ChequeNo = string.Empty;
                infoLedgerPosting.ChequeDate = DateTime.Now;
                infoLedgerPosting.YearId = PublicVariables._decCurrentFinancialYearId;
                infoLedgerPosting.InvoiceNo = infoPurchaseReturnMaster.InvoiceNo;
                infoLedgerPosting.Extra1 = string.Empty;
                infoLedgerPosting.Extra2 = string.Empty;

                infoLedgerPosting.LedgerId = infoPurchaseReturnMaster.PurchaseAccount;
                infoLedgerPosting.Debit = 0;
                if (btnSave.Text == "Save")
                {
                    infoLedgerPosting.Credit = TotalNetAmountCalculation() * spExchangeRate.ExchangeRateViewByExchangeRateId(Convert.ToDecimal(cmbCurrency.SelectedValue.ToString()));
                    infoLedgerPosting.ExtraDate = DateTime.Now;
                }
                else
                {
                    infoLedgerPosting.Credit = TotalNetAmountCalculation();
                }
                spLedgerPosting.LedgerPostingAdd(infoLedgerPosting);

                infoLedgerPosting.LedgerId = infoPurchaseReturnMaster.LedgerId;
                if (btnSave.Text == "Save")
                {
                    infoLedgerPosting.Debit = Convert.ToDecimal(txtGrandTotal.Text) * spExchangeRate.ExchangeRateViewByExchangeRateId(Convert.ToDecimal(cmbCurrency.SelectedValue.ToString()));
                    infoLedgerPosting.ExtraDate = DateTime.Now;
                }
                else
                {
                    infoLedgerPosting.Debit = Convert.ToDecimal(txtGrandTotal.Text);
                }
                infoLedgerPosting.Credit = 0;
                spLedgerPosting.LedgerPostingAdd(infoLedgerPosting);

                foreach (DataGridViewRow dgvrow in dgvPurchaseReturnTax.Rows)
                {
                    if (dgvrow.Cells["dgvtxtTaxId"].Value != null && dgvrow.Cells["dgvtxtTaxId"].Value.ToString() != string.Empty)
                    {
                        infoLedgerPosting.LedgerId = Convert.ToDecimal(dgvrow.Cells["dgvtxtledgerId"].Value.ToString());
                        infoLedgerPosting.Credit = (dgvrow.Cells["dgvtxtAmounts"].Value == null) ? 0 : Convert.ToDecimal(dgvrow.Cells["dgvtxtAmounts"].Value.ToString());
                        infoLedgerPosting.Debit = 0;
                        infoLedgerPosting.ExtraDate = DateTime.Now;
                        spLedgerPosting.LedgerPostingAdd(infoLedgerPosting);
                    }
                }

                if (txtBillDiscount.Text.Trim() != string.Empty)
                {
                    decDis = Convert.ToDecimal(txtBillDiscount.Text);
                }
                if (decDis >= 0)
                {
                    infoLedgerPosting.Debit = 0;
                    infoLedgerPosting.Credit = decDis;
                    infoLedgerPosting.LedgerId = 9;
                    infoLedgerPosting.ExtraDate = DateTime.Now;
                    spLedgerPosting.LedgerPostingAdd(infoLedgerPosting);
                }

                if (btnSave.Text == "Update")
                {
                    infoPurchaseReturnMaster = SPPurchaseReturnMaster.PurchaseReturnMasterView(decPurchaseReturnMasterId);
                    if (infoPurchaseReturnMaster.PurchaseMasterId == 0)
                    {
                        spStockPosting.StockPostingDeleteByagainstVoucherTypeIdAndagainstVoucherNoAndVoucherNoAndVoucherType(0, "NA", infoPurchaseReturnMaster.VoucherNo, infoPurchaseReturnMaster.VoucherTypeId);
                    }
                    if (infoPurchaseReturnMaster.PurchaseMasterId != 0)
                    {
                        infoPurchaseMaster = SPPurchaseMaster.PurchaseMasterView(infoPurchaseReturnMaster.PurchaseMasterId);
                        spStockPosting.StockPostingDeleteByagainstVoucherTypeIdAndagainstVoucherNoAndVoucherNoAndVoucherType(infoPurchaseMaster.VoucherTypeId, strInvoiceNo, strVoucherNo, infoPurchaseReturnMaster.VoucherTypeId);
                    }
                }

                foreach (DataGridViewRow dgvrow in dgvPurchaseReturn.Rows)
                {
                    if (dgvrow.Cells["dgvtxtProductId"].Value != null && dgvrow.Cells["dgvtxtProductId"].Value.ToString() != string.Empty)
                    {
                        infoPurchaseReturnDetails.ExtraDate = DateTime.Now;
                        infoPurchaseReturnDetails.Extra1 = string.Empty;
                        infoPurchaseReturnDetails.Extra2 = string.Empty;
                        infoPurchaseReturnDetails.PurchaseReturnMasterId = (btnSave.Text == "Save") ? decPurchaseReturnMasterIds : decPurchaseReturnMasterId;
                        infoPurchaseReturnDetails.ProductId = Convert.ToDecimal(Convert.ToString(dgvrow.Cells["dgvtxtproductId"].Value));
                        infoPurchaseReturnDetails.Qty = Convert.ToDecimal(Convert.ToString(dgvrow.Cells["dgvtxtqty"].Value));
                        infoPurchaseReturnDetails.Rate = Convert.ToDecimal(Convert.ToString(dgvrow.Cells["dgvtxtrate"].Value));
                        if (btnSave.Text == "Save")
                        {
                            infoPurchaseReturnDetails.UnitId = Convert.ToDecimal(dgvrow.Cells["dgvcmbUnit"].Value.ToString());
                            infoPurchaseReturnDetails.UnitConversionId = SPUnitConversion.UnitconversionIdViewByUnitIdAndProductId(infoPurchaseReturnDetails.UnitId, infoPurchaseReturnDetails.ProductId);
                        }
                        else
                        {
                            if (Convert.ToDecimal(dgvrow.Cells["dgvtxtPurchaseReturnDetailsId"].Value) == 0)
                            {
                                try
                                {
                                    infoPurchaseReturnDetails.UnitId = Convert.ToDecimal(dgvrow.Cells["dgvcmbUnit"].Value.ToString());
                                }
                                catch
                                {
                                    infoPurchaseReturnDetails.UnitId = spUnit.UnitIdByUnitName(Convert.ToString(dgvrow.Cells["dgvcmbUnit"].Value.ToString()));
                                }
                                infoPurchaseReturnDetails.UnitConversionId = SPUnitConversion.UnitconversionIdViewByUnitIdAndProductId(infoPurchaseReturnDetails.UnitId, infoPurchaseReturnDetails.ProductId);
                            }
                            else
                            {
                                try
                                {
                                    infoPurchaseReturnDetails.UnitId = Convert.ToDecimal(dgvrow.Cells["dgvcmbUnit"].Value.ToString());
                                }
                                catch
                                {
                                    infoPurchaseReturnDetails.UnitId = spUnit.UnitIdByUnitName(Convert.ToString(dgvrow.Cells["dgvcmbUnit"].Value.ToString()));
                                }
                            }
                            infoPurchaseReturnDetails.UnitConversionId = Convert.ToDecimal(dgvrow.Cells["dgvtxtUnitConversionId"].Value);
                        }
                        infoPurchaseReturnDetails.Discount = Convert.ToDecimal(Convert.ToString(dgvrow.Cells["dgvtxtdiscount"].Value));
                        if (dgvrow.Cells["dgvcmbTax"].Value != null && Convert.ToString(dgvrow.Cells["dgvcmbTax"].Value) != string.Empty && dgvrow.Cells["dgvcmbTax"].Value as string != "NA")
                        {
                            infoPurchaseReturnDetails.TaxId = Convert.ToDecimal(Convert.ToString(dgvrow.Cells["dgvcmbTax"].Value));
                            if (strTaxComboFill != string.Empty)
                            {
                                infoPurchaseReturnDetails.TaxAmount = Convert.ToDecimal(Convert.ToString(dgvrow.Cells["dgvtxtTaxAmount"].Value));
                            }
                        }
                        else
                        {
                            infoPurchaseReturnDetails.TaxId = 0;
                        }
                        if (dgvrow.Cells["dgvcmbBatch"].Value != null && Convert.ToString(dgvrow.Cells["dgvcmbBatch"].Value) != string.Empty)
                        {
                            infoPurchaseReturnDetails.BatchId = Convert.ToDecimal(dgvrow.Cells["dgvcmbBatch"].Value);
                        }
                        else
                        {
                            infoPurchaseReturnDetails.GodownId = 0;
                        }
                        if (dgvrow.Cells["dgvcmbGodown"].Value != null && Convert.ToString(dgvrow.Cells["dgvcmbGodown"].Value) != string.Empty)
                        {
                            infoPurchaseReturnDetails.GodownId = Convert.ToDecimal(dgvrow.Cells["dgvcmbGodown"].Value);
                        }
                        else
                        {
                            infoPurchaseReturnDetails.RackId = 0;
                        }
                        if (dgvrow.Cells["dgvcmbRack"].Value != null && Convert.ToString(dgvrow.Cells["dgvcmbRack"].Value) != string.Empty)
                        {
                            infoPurchaseReturnDetails.RackId = Convert.ToDecimal(dgvrow.Cells["dgvcmbRack"].Value);
                        }
                        infoPurchaseReturnDetails.GrossAmount = Convert.ToDecimal(dgvrow.Cells["dgvtxtgrossValue"].Value.ToString());
                        infoPurchaseReturnDetails.NetAmount = Convert.ToDecimal(Convert.ToString(dgvrow.Cells["dgvtxtNetAmount"].Value));
                        infoPurchaseReturnDetails.Amount = Convert.ToDecimal(Convert.ToString(dgvrow.Cells["dgvtxtAmount"].Value));
                        infoPurchaseReturnDetails.SlNo = Convert.ToInt32(Convert.ToString(dgvrow.Cells["dgvtxtSlNo"].Value));
                        infoPurchaseReturnDetails.PurchaseDetailsId = (dgvrow.Cells["dgvtxtPurchaseDetailsId"].Value != null) ? Convert.ToDecimal(Convert.ToString(dgvrow.Cells["dgvtxtPurchaseDetailsId"].Value)) : 0;
                        if (dgvrow.Cells["dgvtxtPurchaseReturnDetailsId"].Value != null)
                        {
                            if (dgvrow.Cells["dgvtxtPurchaseReturnDetailsId"].Value.ToString() == "0" || dgvrow.Cells["dgvtxtPurchaseReturnDetailsId"].Value.ToString() == string.Empty)
                            {
                                SPPurchaseReturnDetails.PurchaseReturnDetailsAddWithReturnIdentity(infoPurchaseReturnDetails);
                            }
                            else
                            {
                                infoPurchaseReturnDetails.PurchaseReturnDetailsId = Convert.ToDecimal(dgvrow.Cells["dgvtxtPurchaseReturnDetailsId"].Value.ToString());
                                SPPurchaseReturnDetails.PurchaseReturnDetailsEdit(infoPurchaseReturnDetails);
                            }
                        }
                        else
                        {
                            SPPurchaseReturnDetails.PurchaseReturnDetailsAddWithReturnIdentity(infoPurchaseReturnDetails);
                        }
                 
                        if (btnSave.Text == "Save")
                        {
                            infoPurchaseMaster = SPPurchaseMaster.PurchaseMasterView(infoPurchaseReturnMaster.PurchaseMasterId);
                        }
                        infoStockPosting.Date = infoPurchaseReturnMaster.Date;
                        infoStockPosting.ProductId = infoPurchaseReturnDetails.ProductId;
                        infoStockPosting.BatchId = infoPurchaseReturnDetails.BatchId;
                        infoStockPosting.UnitId = infoPurchaseReturnDetails.UnitId;
                        infoStockPosting.GodownId = infoPurchaseReturnDetails.GodownId;
                        infoStockPosting.RackId = infoPurchaseReturnDetails.RackId;
                        decimal decConversionId = SPUnitConversion.UnitConversionRateByUnitConversionId(infoPurchaseReturnDetails.UnitConversionId);
                        //infoStockPosting.OutwardQty = infoPurchaseReturnDetails.Qty / (decConversionId == 0 ? 1 : decConversionId);
                        infoStockPosting.OutwardQty = infoPurchaseReturnDetails.Qty;
                        infoStockPosting.InwardQty = 0;
                        infoStockPosting.Rate = infoPurchaseReturnDetails.Rate;
                        infoStockPosting.FinancialYearId = PublicVariables._decCurrentFinancialYearId;
                        infoStockPosting.Extra1 = string.Empty;
                        infoStockPosting.Extra2 = string.Empty;
                        if (infoPurchaseReturnDetails.PurchaseDetailsId != 0)
                        {
                            infoStockPosting.AgainstVoucherTypeId = infoPurchaseMaster.VoucherTypeId;
                            infoStockPosting.AgainstVoucherNo = infoPurchaseMaster.VoucherNo;
                            infoStockPosting.AgainstInvoiceNo = infoPurchaseMaster.InvoiceNo;
                            infoStockPosting.VoucherNo = strVoucherNo;
                            infoStockPosting.InvoiceNo = txtReturnNo.Text.Trim();
                            infoStockPosting.VoucherTypeId = decPurchaseReturnVoucherTypeId;
                            decAgainstVoucherTypeId = infoStockPosting.VoucherTypeId;
                        }
                        else
                        {
                            infoStockPosting.AgainstVoucherTypeId = 0;
                            infoStockPosting.AgainstVoucherNo = "NA";
                            infoStockPosting.AgainstInvoiceNo = "NA";
                            infoStockPosting.VoucherNo = infoPurchaseReturnMaster.VoucherNo;
                            infoStockPosting.InvoiceNo = infoPurchaseReturnMaster.InvoiceNo;
                            infoStockPosting.VoucherTypeId = decPurchaseReturnVoucherTypeId;
                            decAgainstVoucherTypeId = 0;
                        }
                        spStockPosting.StockPostingAdd(infoStockPosting);
                    }
                }
                if (btnSave.Text == "Update")
                {
                    removePurchaseReturnDetails();
                }
               
                infoAccountLedger = spAccountLedger.AccountLedgerView(infoPurchaseReturnMaster.LedgerId);
                if (infoAccountLedger.BillByBill == true)
                {
                    infoPartyBalance.Date = infoPurchaseReturnMaster.Date;
                    infoPartyBalance.LedgerId = infoPurchaseReturnMaster.LedgerId;
                    if (decAgainstVoucherTypeId != 0)
                    {
                        infoPartyBalance.VoucherTypeId = infoPurchaseMaster.VoucherTypeId;
                        infoPartyBalance.VoucherNo = infoPurchaseMaster.VoucherNo;
                        infoPartyBalance.InvoiceNo = infoPurchaseMaster.InvoiceNo;
                        infoPartyBalance.AgainstVoucherTypeId = infoPurchaseReturnMaster.VoucherTypeId;
                        infoPartyBalance.AgainstVoucherNo = infoPurchaseReturnMaster.VoucherNo;
                        infoPartyBalance.AgainstInvoiceNo = infoPurchaseReturnMaster.InvoiceNo;
                        infoPartyBalance.ReferenceType = "Against";
                    }
                    else
                    {
                        infoPartyBalance.VoucherTypeId = infoPurchaseReturnMaster.VoucherTypeId;
                        infoPartyBalance.VoucherNo = infoPurchaseReturnMaster.VoucherNo;
                        infoPartyBalance.InvoiceNo = infoPurchaseReturnMaster.InvoiceNo;
                        infoPartyBalance.AgainstVoucherTypeId = 0;
                        infoPartyBalance.AgainstVoucherNo = "NA";
                        infoPartyBalance.AgainstInvoiceNo = "NA";
                        infoPartyBalance.ReferenceType ="New";
                    }
                    infoPartyBalance.Debit = infoPurchaseReturnMaster.TotalAmount;
                    infoPartyBalance.Credit = 0;
                    infoPartyBalance.CreditPeriod = 0;
                    infoPartyBalance.ExchangeRateId = infoPurchaseReturnMaster.ExchangeRateId;
                    infoPartyBalance.FinancialYearId = PublicVariables._decCurrentFinancialYearId;
                    infoPartyBalance.Extra1 = string.Empty;
                    infoPartyBalance.Extra2 = string.Empty;
                    spPartyBalance.PartyBalanceAdd(infoPartyBalance);
                }
              
                foreach (DataGridViewRow item in dgvPurchaseReturnTax.Rows)
                {
                    if (item.Cells["dgvtxtTaxId"].Value != null)
                    {
                        infoPurchaseReturnBillTax.PurchaseReturnMasterId = (btnSave.Text == "Save") ? decPurchaseReturnMasterIds : decPurchaseReturnMasterIds;
                        infoPurchaseReturnBillTax.TaxId = Convert.ToDecimal(item.Cells["dgvtxtTaxId"].Value.ToString());
                        infoPurchaseReturnBillTax.TaxAmount = (item.Cells["dgvtxtAmounts"].Value == null) ? 0 : Convert.ToDecimal(item.Cells["dgvtxtAmounts"].Value.ToString());
                        infoPurchaseReturnBillTax.Extra1 = string.Empty;
                        infoPurchaseReturnBillTax.Extra2 = string.Empty;
                        spPurchaseReturnBillTax.PurchaseReturnBilltaxAdd(infoPurchaseReturnBillTax);
                    }
                }
                if (btnSave.Text == "Save")
                {
                    Messages.SavedMessage();
                    if (cbxPrintAfterSave.Checked)
                    {
                        if (spSettings.SettingsStatusCheck("Printer") == "Dot Matrix")
                        {
                            PrintForDotMatrix(decPurchaseReturnMasterIds);
                        }
                        else
                        {
                            Print(decPurchaseReturnMasterIds);
                        }
                    }
                    Clear();
                }
                else
                {
                    decDiscount = Convert.ToDecimal(txtBillDiscount.Text);
                    Messages.UpdatedMessage();
                    if (cbxPrintAfterSave.Checked)
                    {
                        if (spSettings.SettingsStatusCheck("Printer") == "Dot Matrix")
                        {
                            PrintForDotMatrix(decPurchaseReturnMasterId);
                        }
                        else
                        {
                            Print(decPurchaseReturnMasterId);
                        }
                    }
                    if (frmPurchaseReturnRegisterObj != null)
                    {
                        frmPurchaseReturnRegisterObj.GridFill();
                        frmPurchaseReturnRegisterObj.Enabled = true;
                    }

                    if (ObjPurchaseReturnReport != null)
                    {
                        ObjPurchaseReturnReport.PurchaseReturnReportGridFill();
                        ObjPurchaseReturnReport.Enabled = true;
                    }
                    if (frmLedgerDetailsObj != null)
                    {
                        frmLedgerDetailsObj.LedgerDetailsView();
                        frmLedgerDetailsObj.Enabled = true;
                    }
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("PR:49" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
 /// <summary>
 /// Function to insert values to PurchaseMaster Table
 /// </summary>
 /// <param name="purchasemasterinfo"></param>
 /// <returns></returns>
 public decimal PurchaseMasterAdd(PurchaseMasterInfo purchasemasterinfo)
 {
     decimal decPurchaseMasterId = 0;
     try
     {
         if (sqlcon.State == ConnectionState.Closed)
         {
             sqlcon.Open();
         }
         SqlCommand sccmd = new SqlCommand("PurchaseMasterAdd", sqlcon);
         sccmd.CommandType = CommandType.StoredProcedure;
         SqlParameter sprmparam = new SqlParameter();
         sprmparam = sccmd.Parameters.Add("@voucherNo", SqlDbType.VarChar);
         sprmparam.Value = purchasemasterinfo.VoucherNo;
         sprmparam = sccmd.Parameters.Add("@invoiceNo", SqlDbType.VarChar);
         sprmparam.Value = purchasemasterinfo.InvoiceNo;
         sprmparam = sccmd.Parameters.Add("@suffixPrefixId", SqlDbType.Decimal);
         sprmparam.Value = purchasemasterinfo.SuffixPrefixId;
         sprmparam = sccmd.Parameters.Add("@voucherTypeId", SqlDbType.Decimal);
         sprmparam.Value = purchasemasterinfo.VoucherTypeId;
         sprmparam = sccmd.Parameters.Add("@date", SqlDbType.DateTime);
         sprmparam.Value = purchasemasterinfo.Date;
         sprmparam = sccmd.Parameters.Add("@ledgerId", SqlDbType.Decimal);
         sprmparam.Value = purchasemasterinfo.LedgerId;
         sprmparam = sccmd.Parameters.Add("@vendorInvoiceNo", SqlDbType.VarChar);
         sprmparam.Value = purchasemasterinfo.VendorInvoiceNo;
         sprmparam = sccmd.Parameters.Add("@vendorInvoiceDate", SqlDbType.DateTime);
         sprmparam.Value = purchasemasterinfo.VendorInvoiceDate;
         sprmparam = sccmd.Parameters.Add("@creditPeriod", SqlDbType.VarChar);
         sprmparam.Value = purchasemasterinfo.CreditPeriod;
         sprmparam = sccmd.Parameters.Add("@exchangeRateId", SqlDbType.Decimal);
         sprmparam.Value = purchasemasterinfo.ExchangeRateId;
         sprmparam = sccmd.Parameters.Add("@narration", SqlDbType.VarChar);
         sprmparam.Value = purchasemasterinfo.Narration;
         sprmparam = sccmd.Parameters.Add("@purchaseAccount", SqlDbType.Decimal);
         sprmparam.Value = purchasemasterinfo.PurchaseAccount;
         sprmparam = sccmd.Parameters.Add("@purchaseOrderMasterId", SqlDbType.Decimal);
         sprmparam.Value = purchasemasterinfo.PurchaseOrderMasterId;
         sprmparam = sccmd.Parameters.Add("@materialReceiptMasterId", SqlDbType.Decimal);
         sprmparam.Value = purchasemasterinfo.MaterialReceiptMasterId;
         sprmparam = sccmd.Parameters.Add("@additionalCost", SqlDbType.Decimal);
         sprmparam.Value = purchasemasterinfo.AdditionalCost;
         sprmparam = sccmd.Parameters.Add("@totalTax", SqlDbType.Decimal);
         sprmparam.Value = purchasemasterinfo.TotalTax;
         sprmparam = sccmd.Parameters.Add("@billDiscount", SqlDbType.Decimal);
         sprmparam.Value = purchasemasterinfo.BillDiscount;
         sprmparam = sccmd.Parameters.Add("@grandTotal", SqlDbType.Decimal);
         sprmparam.Value = purchasemasterinfo.GrandTotal;
         sprmparam = sccmd.Parameters.Add("@totalAmount", SqlDbType.Decimal);
         sprmparam.Value = purchasemasterinfo.TotalAmount;
         sprmparam = sccmd.Parameters.Add("@userId", SqlDbType.Decimal);
         sprmparam.Value = purchasemasterinfo.UserId;
         sprmparam = sccmd.Parameters.Add("@lrNo", SqlDbType.VarChar);
         sprmparam.Value = purchasemasterinfo.LrNo;
         sprmparam = sccmd.Parameters.Add("@transportationCompany", SqlDbType.VarChar);
         sprmparam.Value = purchasemasterinfo.TransportationCompany;
         sprmparam = sccmd.Parameters.Add("@financialYearId", SqlDbType.Decimal);
         sprmparam.Value = purchasemasterinfo.FinancialYearId;
         sprmparam = sccmd.Parameters.Add("@extraDate", SqlDbType.DateTime);
         sprmparam.Value = purchasemasterinfo.ExtraDate;
         sprmparam = sccmd.Parameters.Add("@extra1", SqlDbType.VarChar);
         sprmparam.Value = purchasemasterinfo.Extra1;
         sprmparam = sccmd.Parameters.Add("@extra2", SqlDbType.VarChar);
         sprmparam.Value = purchasemasterinfo.Extra2;
         decPurchaseMasterId = Convert.ToDecimal(sccmd.ExecuteScalar());
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.ToString());
     }
     finally
     {
         sqlcon.Close();
     }
     return decPurchaseMasterId;
 }
 /// <summary>
 /// Function to get particular values from PurchaseMaster table based on the parameter
 /// </summary>
 /// <param name="purchaseMasterId"></param>
 /// <returns></returns>
 public PurchaseMasterInfo PurchaseMasterView(Decimal purchaseMasterId)
 {
     PurchaseMasterInfo purchasemasterinfo = new PurchaseMasterInfo();
     SqlDataReader sdrreader = null;
     try
     {
         if (sqlcon.State == ConnectionState.Closed)
         {
             sqlcon.Open();
         }
         SqlCommand sccmd = new SqlCommand("PurchaseMasterView", sqlcon);
         sccmd.CommandType = CommandType.StoredProcedure;
         SqlParameter sprmparam = new SqlParameter();
         sprmparam = sccmd.Parameters.Add("@purchaseMasterId", SqlDbType.Decimal);
         sprmparam.Value = purchaseMasterId;
         sdrreader = sccmd.ExecuteReader();
         while (sdrreader.Read())
         {
             purchasemasterinfo.PurchaseMasterId = decimal.Parse(sdrreader[0].ToString());
             purchasemasterinfo.VoucherNo = sdrreader[1].ToString();
             purchasemasterinfo.InvoiceNo = sdrreader[2].ToString();
             purchasemasterinfo.SuffixPrefixId = decimal.Parse(sdrreader[3].ToString());
             purchasemasterinfo.VoucherTypeId = decimal.Parse(sdrreader[4].ToString());
             purchasemasterinfo.Date = DateTime.Parse(sdrreader[5].ToString());
             purchasemasterinfo.LedgerId = decimal.Parse(sdrreader[6].ToString());
             purchasemasterinfo.VendorInvoiceNo = sdrreader[7].ToString();
             purchasemasterinfo.VendorInvoiceDate = DateTime.Parse(sdrreader[8].ToString());
             purchasemasterinfo.CreditPeriod = sdrreader[9].ToString();
             purchasemasterinfo.ExchangeRateId = decimal.Parse(sdrreader[10].ToString());
             purchasemasterinfo.Narration = sdrreader[11].ToString();
             purchasemasterinfo.PurchaseAccount = decimal.Parse(sdrreader[12].ToString());
             purchasemasterinfo.PurchaseOrderMasterId = decimal.Parse(sdrreader[13].ToString());
             purchasemasterinfo.MaterialReceiptMasterId = decimal.Parse(sdrreader[14].ToString());
             purchasemasterinfo.AdditionalCost = decimal.Parse(sdrreader[15].ToString());
             purchasemasterinfo.TotalTax = decimal.Parse(sdrreader[16].ToString());
             purchasemasterinfo.BillDiscount = decimal.Parse(sdrreader[17].ToString());
             purchasemasterinfo.GrandTotal = decimal.Parse(sdrreader[18].ToString());
             purchasemasterinfo.TotalAmount = decimal.Parse(sdrreader[19].ToString());
             purchasemasterinfo.UserId = decimal.Parse(sdrreader[20].ToString());
             purchasemasterinfo.LrNo = sdrreader[21].ToString();
             purchasemasterinfo.TransportationCompany = sdrreader[22].ToString();
             purchasemasterinfo.FinancialYearId = decimal.Parse(sdrreader[23].ToString());
             purchasemasterinfo.ExtraDate = DateTime.Parse(sdrreader[24].ToString());
             purchasemasterinfo.Extra1 = sdrreader[25].ToString();
             purchasemasterinfo.Extra2 = sdrreader[26].ToString();
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.ToString());
     }
     finally
     {
         sdrreader.Close();
         sqlcon.Close();
     }
     return purchasemasterinfo;
 }
 /// <summary>
 /// Function to fill the details while calling from register or report
 /// </summary>
 public void FillRegisterOrReport()
 {
     PurchaseMasterInfo infoPurchaseMaster = new PurchaseMasterInfo();
     PurchaseMasterSP spPurchaseMaster = new PurchaseMasterSP();
     PurchaseOrderMasterInfo infoPurchaseOrderMaster = new PurchaseOrderMasterInfo();
     PurchaseOrderMasterSP spPurchaseOrderMaster = new PurchaseOrderMasterSP();
     MaterialReceiptMasterInfo infoMaterialReceiptMaster = new MaterialReceiptMasterInfo();
     MaterialReceiptMasterSP spMaterialReceiptMaster = new MaterialReceiptMasterSP();
     VoucherTypeSP spVoucherType = new VoucherTypeSP();
     VoucherTypeInfo infoVoucherType = new VoucherTypeInfo();
     AccountLedgerSP spAccountLedger = new AccountLedgerSP();
     bool isPartyBalanceRef = false;
     try
     {
         isEditFill = true;
         btnSave.Text = "Update";
         btnDelete.Enabled = true;
         txtVoucherNo.ReadOnly = true;
         infoPurchaseMaster = spPurchaseMaster.PurchaseMasterView(decPurchaseMasterId);
         strVoucherNo = infoPurchaseMaster.VoucherNo;
         decPurchaseInvoiceVoucherTypeId = infoPurchaseMaster.VoucherTypeId;
         decPurchaseInvoiceSuffixPrefixId = infoPurchaseMaster.SuffixPrefixId;
         isAutomatic = spVoucherType.CheckMethodOfVoucherNumbering(decPurchaseInvoiceVoucherTypeId);
         infoVoucherType = spVoucherType.VoucherTypeView(decPurchaseInvoiceVoucherTypeId);
         this.Text = infoVoucherType.VoucherTypeName;
         if (isAutomatic)
         {
             txtVoucherDate.Focus();
         }
         else
         {
             txtVoucherNo.Focus();
         }
         txtVoucherNo.Text = infoPurchaseMaster.InvoiceNo;
         txtVendorInvoiceNo.Text = infoPurchaseMaster.VendorInvoiceNo;
         dtpVoucherDate.Value = infoPurchaseMaster.Date;
         dtpInvoiceDate.Value = infoPurchaseMaster.VendorInvoiceDate;
         cmbCashOrParty.SelectedValue = infoPurchaseMaster.LedgerId;
         if (infoPurchaseMaster.PurchaseOrderMasterId == 0 && infoPurchaseMaster.MaterialReceiptMasterId == 0)
         {
             cmbPurchaseMode.SelectedItem = "NA";
         }
         else if (infoPurchaseMaster.PurchaseOrderMasterId != 0 && infoPurchaseMaster.MaterialReceiptMasterId == 0)
         {
             cmbPurchaseMode.SelectedItem = "Against PurchaseOrder";
             infoPurchaseOrderMaster = spPurchaseOrderMaster.PurchaseOrderMasterView(infoPurchaseMaster.PurchaseOrderMasterId);
             cmbVoucherType.SelectedValue = infoPurchaseOrderMaster.VoucherTypeId;
             OrderComboFill();
             cmbOrderNo.SelectedValue = infoPurchaseMaster.PurchaseOrderMasterId;
         }
         else if (infoPurchaseMaster.PurchaseOrderMasterId == 0 && infoPurchaseMaster.MaterialReceiptMasterId != 0)
         {
             cmbPurchaseMode.SelectedItem = "Against MaterialReceipt";
             infoMaterialReceiptMaster = spMaterialReceiptMaster.MaterialReceiptMasterView(infoPurchaseMaster.MaterialReceiptMasterId);
             cmbVoucherType.SelectedValue = infoMaterialReceiptMaster.VoucherTypeId;
             OrderComboFill();
             cmbOrderNo.SelectedValue = infoPurchaseMaster.MaterialReceiptMasterId;
         }
         cmbPurchaseAccount.SelectedValue = infoPurchaseMaster.PurchaseAccount;
         txtCreditPeriod.Text = infoPurchaseMaster.CreditPeriod;
         cmbCurrency.SelectedValue = infoPurchaseMaster.ExchangeRateId;
         txtNarration.Text = infoPurchaseMaster.Narration;
         lblAdditionalCostAmount.Text = Math.Round(infoPurchaseMaster.AdditionalCost, PublicVariables._inNoOfDecimalPlaces).ToString();
         lblTaxAmount.Text = Math.Round(infoPurchaseMaster.TotalTax, PublicVariables._inNoOfDecimalPlaces).ToString();
         txtTotalAmount.Text = Math.Round(infoPurchaseMaster.TotalAmount, PublicVariables._inNoOfDecimalPlaces).ToString();
         txtBillDiscount.Text = Math.Round(infoPurchaseMaster.BillDiscount, PublicVariables._inNoOfDecimalPlaces).ToString();
         txtLRNo.Text = infoPurchaseMaster.LrNo;
         txtTransportationCompany.Text = infoPurchaseMaster.TransportationCompany;
         txtGrandTotal.Text = Math.Round(infoPurchaseMaster.GrandTotal, PublicVariables._inNoOfDecimalPlaces).ToString();
         PurchaseDetailsFill();
         TaxGridFill();
         AdditionalCostGridFill();
         isPartyBalanceRef = spAccountLedger.PartyBalanceAgainstReferenceCheck(strVoucherNo, decPurchaseInvoiceVoucherTypeId);
         if (isPartyBalanceRef)
         {
             cmbCashOrParty.Enabled = false;
         }
         else
         {
             cmbCashOrParty.Enabled = true;
         }
         isEditFill = false;
     }
     catch (Exception ex)
     {
         MessageBox.Show("PI55:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
 public void Delete()
 {
     PurchaseMasterSP spPurchaseMaster = new PurchaseMasterSP();
     PurchaseDetailsSP spPurchaseDetails = new PurchaseDetailsSP();
     PurchaseBillTaxSP spPurchaseBillTax = new PurchaseBillTaxSP();
     AdditionalCostSP spAdditionalCost = new AdditionalCostSP();
     AccountLedgerSP spAccountLedger = new AccountLedgerSP();
     StockPostingSP spStockPosting = new StockPostingSP();
     LedgerPostingSP spLedgerPosting = new LedgerPostingSP();
     PurchaseOrderMasterSP spPurchaseOrderMaster = new PurchaseOrderMasterSP();
     MaterialReceiptMasterSP spMaterialReceiptMaster = new MaterialReceiptMasterSP();
     PurchaseOrderMasterInfo infoPurchaseOrderMaster = new PurchaseOrderMasterInfo();
     MaterialReceiptMasterInfo infoMaterialReceiptMaster = new MaterialReceiptMasterInfo();
     PurchaseMasterInfo infoPurchaseMaster = new PurchaseMasterInfo();
     /*---------------------------------Deleting previous stock posting, Ledger posting, partybalanceposting---------------------------------------*/
     infoPurchaseMaster = spPurchaseMaster.PurchaseMasterView(decPurchaseMasterId);
     if (infoPurchaseMaster.MaterialReceiptMasterId != 0)
     {
         infoMaterialReceiptMaster = spMaterialReceiptMaster.MaterialReceiptMasterView(infoPurchaseMaster.MaterialReceiptMasterId);
         spStockPosting.StockPostingDeleteForSalesInvoiceAgainstDeliveryNote
             (infoPurchaseMaster.VoucherTypeId, infoPurchaseMaster.VoucherNo,
             infoMaterialReceiptMaster.VoucherNo, infoMaterialReceiptMaster.VoucherTypeId);
     }
     spStockPosting.StockPostingDeleteByagainstVoucherTypeIdAndagainstVoucherNoAndVoucherNoAndVoucherType
             (0, "NA", infoPurchaseMaster.VoucherNo, infoPurchaseMaster.VoucherTypeId);
     //-------------------------------------------------
     decimal decPurchaseDetailsId = 0;
     decimal decPurchaseOrderMasterId = 0;
     decimal decMaterialReceiptMasterId = 0;
     decimal decAdditionalCostId = 0;
     decimal decPurchaseBillTaxId = 0;
     int inRef = 0;
     bool isRef = false;
     try
     {
         foreach (DataGridViewRow dgvrow in dgvProductDetails.Rows)
         {
             if (dgvrow.Cells["dgvtxtPurchaseDetailsId"].Value != null)
             {
                 if (dgvrow.Cells["dgvtxtPurchaseDetailsId"].Value.ToString() != string.Empty && dgvrow.Cells["dgvtxtPurchaseDetailsId"].Value.ToString() != "0")
                 {
                     decPurchaseDetailsId = Convert.ToDecimal(dgvrow.Cells["dgvtxtPurchaseDetailsId"].Value.ToString());
                     inRef = spPurchaseMaster.PurchaseMasterReferenceCheck(decPurchaseMasterId, decPurchaseDetailsId);
                     if (inRef > 0 && !isRef)
                     {
                         isRef = true;
                     }
                 }
             }
         }
         if (!isRef)
         {
             isRef = spAccountLedger.PartyBalanceAgainstReferenceCheck(strVoucherNo, decPurchaseInvoiceVoucherTypeId);
             if (!isRef)
             {
                 spPurchaseMaster.PurchaseMasterDelete(decPurchaseMasterId);
                 spPurchaseDetails.PurchaseDetailsDeleteByPurchaseMasterId(decPurchaseMasterId);
                 foreach (DataGridViewRow dgvrow in dgvAdditionalCost.Rows)
                 {
                     if (dgvrow.Cells["dgvtxtAdditionalCostId"].Value != null)
                     {
                         if (dgvrow.Cells["dgvtxtAdditionalCostId"].Value.ToString() != string.Empty &&
                             dgvrow.Cells["dgvtxtAdditionalCostId"].Value.ToString() != "0")
                         {
                             decAdditionalCostId = Convert.ToDecimal(dgvrow.Cells["dgvtxtAdditionalCostId"].Value.ToString());
                             spAdditionalCost.AdditionalCostDelete(decAdditionalCostId);
                         }
                     }
                 }
                 if (dgvTax.Visible)
                 {
                     foreach (DataGridViewRow dgvrow in dgvTax.Rows)
                     {
                         if (dgvrow.Cells["dgvtxtPurchaseBillTaxId"].Value != null)
                         {
                             if (dgvrow.Cells["dgvtxtPurchaseBillTaxId"].Value.ToString() != string.Empty &&
                                 dgvrow.Cells["dgvtxtPurchaseBillTaxId"].Value.ToString() != "0")
                             {
                                 decPurchaseBillTaxId = Convert.ToDecimal(dgvrow.Cells["dgvtxtPurchaseBillTaxId"].Value.ToString());
                                 spPurchaseBillTax.PurchaseBillTaxDelete(decPurchaseBillTaxId);
                             }
                         }
                     }
                 }
                 if (cmbPurchaseMode.Text == "Against PurchaseOrder")
                 {
                     decPurchaseOrderMasterId = Convert.ToDecimal(cmbOrderNo.SelectedValue.ToString());
                     infoPurchaseOrderMaster = spPurchaseOrderMaster.PurchaseOrderMasterView(decPurchaseOrderMasterId);
                 }
                 else if (cmbPurchaseMode.Text == "Against MaterialReceipt")
                 {
                     decMaterialReceiptMasterId = Convert.ToDecimal(cmbOrderNo.SelectedValue.ToString());
                     infoMaterialReceiptMaster = spMaterialReceiptMaster.MaterialReceiptMasterView(decMaterialReceiptMasterId);
                 }
                 spLedgerPosting.LedgerPostDelete(strVoucherNo, decPurchaseInvoiceVoucherTypeId);
                 if (infoPurchaseOrderMaster.PurchaseOrderMasterId != 0)
                 {
                     spStockPosting.StockPostingDeleteByagainstVoucherTypeIdAndagainstVoucherNoAndVoucherNoAndVoucherType
                         (decPurchaseInvoiceVoucherTypeId, strVoucherNo,
                         infoPurchaseOrderMaster.VoucherNo, infoPurchaseOrderMaster.VoucherTypeId);
                 }
                 else if (infoMaterialReceiptMaster.MaterialReceiptMasterId != 0)
                 {
                     spStockPosting.StockPostingDeleteByagainstVoucherTypeIdAndagainstVoucherNoAndVoucherNoAndVoucherType
                         (decPurchaseInvoiceVoucherTypeId, strVoucherNo,
                         infoMaterialReceiptMaster.VoucherNo, infoMaterialReceiptMaster.VoucherTypeId);
                 }
                 spStockPosting.StockPostingDeleteByagainstVoucherTypeIdAndagainstVoucherNoAndVoucherNoAndVoucherType
                         (0, "NA", strVoucherNo, decPurchaseInvoiceVoucherTypeId);
                 spAccountLedger.PartyBalanceDeleteByVoucherTypeVoucherNoAndReferenceType(strVoucherNo, decPurchaseInvoiceVoucherTypeId);
                 Messages.DeletedMessage();
                 Clear();
                 this.Close();
             }
             else
             {
                 Messages.InformationMessage("Cannot delete purchase invoice because there is a payment voucher against this invoice");
             }
         }
         else
         {
             Messages.InformationMessage("Cannot delete purchase invoice because reference exists");
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("PI41:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
 /// <summary>
 /// Function to edit purchase invoice
 /// </summary>
 public void Edit()
 {
     PurchaseMasterInfo infoPurchaseMaster = new PurchaseMasterInfo();
     PurchaseMasterSP spPurchaseMaster = new PurchaseMasterSP();
     PurchaseDetailsInfo infoPurchaseDetails = new PurchaseDetailsInfo();
     PurchaseDetailsSP spPurchaseDetails = new PurchaseDetailsSP();
     MaterialReceiptMasterInfo infoMaterialReceiptMaster = new MaterialReceiptMasterInfo();
     MaterialReceiptMasterSP spMaterialReceiptMaster = new MaterialReceiptMasterSP();
     PurchaseOrderMasterInfo infoPurchaseOrderMaster = new PurchaseOrderMasterInfo();
     PurchaseOrderMasterSP spPurchaseOrderMaster = new PurchaseOrderMasterSP();
     StockPostingInfo infoStockPosting = new StockPostingInfo();
     StockPostingSP spStockPosting = new StockPostingSP();
     LedgerPostingInfo infoLedgerPosting = new LedgerPostingInfo();
     LedgerPostingSP spLedgerPosting = new LedgerPostingSP();
     PartyBalanceInfo infoPartyBalance = new PartyBalanceInfo();
     PartyBalanceSP spPartyBalance = new PartyBalanceSP();
     AdditionalCostInfo infoAdditionalCost = new AdditionalCostInfo();
     AdditionalCostSP spAdditionalCost = new AdditionalCostSP();
     PurchaseBillTaxInfo infoPurchaseBillTax = new PurchaseBillTaxInfo();
     PurchaseBillTaxSP spPurchaseBillTax = new PurchaseBillTaxSP();
     AccountLedgerInfo infoAccountLedger = new AccountLedgerInfo();
     AccountLedgerSP spAccountLedger = new AccountLedgerSP();
     UnitConvertionSP spUnitConvertion = new UnitConvertionSP();
     ExchangeRateSP spExchangeRate = new ExchangeRateSP();
     /*---------------------------------Deleting previous stock posting, Ledger posting, partybalanceposting---------------------------------------*/
     infoPurchaseMaster = spPurchaseMaster.PurchaseMasterView(decPurchaseMasterId);
     if (infoPurchaseMaster.MaterialReceiptMasterId != 0)
     {
         infoMaterialReceiptMaster = spMaterialReceiptMaster.MaterialReceiptMasterView(infoPurchaseMaster.MaterialReceiptMasterId);
         spStockPosting.StockPostingDeleteForSalesInvoiceAgainstDeliveryNote
             (infoPurchaseMaster.VoucherTypeId, infoPurchaseMaster.VoucherNo,
             infoMaterialReceiptMaster.VoucherNo, infoMaterialReceiptMaster.VoucherTypeId);
     }
     spStockPosting.StockPostingDeleteByagainstVoucherTypeIdAndagainstVoucherNoAndVoucherNoAndVoucherType
             (0, "NA", infoPurchaseMaster.VoucherNo, infoPurchaseMaster.VoucherTypeId);
     try
     {
         RemoveDelete();
         /*-----------------------------------------Purchase Master Edit----------------------------------------------------*/
         infoPurchaseMaster.AdditionalCost = Convert.ToDecimal(lblAdditionalCostAmount.Text);
         infoPurchaseMaster.BillDiscount = Convert.ToDecimal(txtBillDiscount.Text);
         infoPurchaseMaster.CreditPeriod = txtCreditPeriod.Text;
         infoPurchaseMaster.Date = Convert.ToDateTime(txtVoucherDate.Text);
         infoPurchaseMaster.ExchangeRateId = Convert.ToDecimal(cmbCurrency.SelectedValue.ToString());
         infoPurchaseMaster.FinancialYearId = PublicVariables._decCurrentFinancialYearId;
         infoPurchaseMaster.GrandTotal = Convert.ToDecimal(txtGrandTotal.Text);
         infoPurchaseMaster.InvoiceNo = txtVoucherNo.Text;
         if (isAutomatic)
         {
             infoPurchaseMaster.SuffixPrefixId = decPurchaseInvoiceSuffixPrefixId;
             infoPurchaseMaster.VoucherNo = strVoucherNo;
         }
         else
         {
             infoPurchaseMaster.SuffixPrefixId = 0;
             infoPurchaseMaster.VoucherNo = strVoucherNo;
         }
         infoPurchaseMaster.LedgerId = Convert.ToDecimal(cmbCashOrParty.SelectedValue.ToString());
         infoPurchaseMaster.LrNo = txtLRNo.Text;
         if (cmbPurchaseMode.Text == "Against MaterialReceipt")
         {
             infoPurchaseMaster.MaterialReceiptMasterId = Convert.ToDecimal(cmbOrderNo.SelectedValue.ToString());
         }
         else
         {
             infoPurchaseMaster.MaterialReceiptMasterId = 0;
         }
         infoPurchaseMaster.Narration = txtNarration.Text;
         infoPurchaseMaster.PurchaseAccount = Convert.ToDecimal(cmbPurchaseAccount.SelectedValue.ToString());
         if (cmbPurchaseMode.Text == "Against PurchaseOrder")
         {
             infoPurchaseMaster.PurchaseOrderMasterId = Convert.ToDecimal(cmbOrderNo.SelectedValue.ToString());
         }
         else
         {
             infoPurchaseMaster.PurchaseOrderMasterId = 0;
         }
         infoPurchaseMaster.TotalAmount = Convert.ToDecimal(txtTotalAmount.Text);
         infoPurchaseMaster.TotalTax = Convert.ToDecimal(lblTaxAmount.Text);
         infoPurchaseMaster.TransportationCompany = txtTransportationCompany.Text;
         infoPurchaseMaster.UserId = PublicVariables._decCurrentUserId;
         infoPurchaseMaster.VendorInvoiceDate = Convert.ToDateTime(txtInvoiceDate.Text);
         infoPurchaseMaster.VendorInvoiceNo = txtVendorInvoiceNo.Text;
         infoPurchaseMaster.VoucherTypeId = decPurchaseInvoiceVoucherTypeId;
         infoPurchaseMaster.Extra1 = string.Empty;
         infoPurchaseMaster.Extra2 = string.Empty;
         infoPurchaseMaster.ExtraDate = Convert.ToDateTime(DateTime.Now);
         infoPurchaseMaster.PurchaseMasterId = decPurchaseMasterId;
         spPurchaseMaster.PurchaseMasterEdit(infoPurchaseMaster);
         infoPurchaseOrderMaster = spPurchaseOrderMaster.PurchaseOrderMasterView(infoPurchaseMaster.PurchaseOrderMasterId);
         infoMaterialReceiptMaster = spMaterialReceiptMaster.MaterialReceiptMasterView(infoPurchaseMaster.MaterialReceiptMasterId);
         spLedgerPosting.LedgerPostDelete(strVoucherNo, decPurchaseInvoiceVoucherTypeId);
         spAccountLedger.PartyBalanceDeleteByVoucherTypeVoucherNoAndReferenceType(strVoucherNo, decPurchaseInvoiceVoucherTypeId);
         foreach (DataGridViewRow dgvrow in dgvProductDetails.Rows)
         {
             if (dgvrow.Cells["dgvtxtProductId"].Value != null)
             {
                 if (dgvrow.Cells["dgvtxtProductId"].Value.ToString() != string.Empty)
                 {
                     /*-----------------------------------------Purchase Details Add----------------------------------------------------*/
                     infoPurchaseDetails.Amount = Convert.ToDecimal(dgvrow.Cells["dgvtxtAmount"].Value.ToString());
                     infoPurchaseDetails.BatchId = Convert.ToDecimal(dgvrow.Cells["dgvcmbBatch"].Value.ToString());
                     infoPurchaseDetails.Discount = Convert.ToDecimal(dgvrow.Cells["dgvtxtDiscount"].Value.ToString());
                     infoPurchaseDetails.GodownId = Convert.ToDecimal(dgvrow.Cells["dgvcmbGodown"].Value.ToString());
                     infoPurchaseDetails.GrossAmount = Convert.ToDecimal(dgvrow.Cells["dgvtxtGrossValue"].Value.ToString());
                     infoPurchaseDetails.NetAmount = Convert.ToDecimal(dgvrow.Cells["dgvtxtNetValue"].Value.ToString());
                     infoPurchaseDetails.OrderDetailsId = Convert.ToDecimal(dgvrow.Cells["dgvtxtPurchaseOrderDetailsId"].Value.ToString());
                     infoPurchaseDetails.ProductId = Convert.ToDecimal(dgvrow.Cells["dgvtxtProductId"].Value.ToString());
                     infoPurchaseDetails.PurchaseMasterId = decPurchaseMasterId;
                     infoPurchaseDetails.Qty = Convert.ToDecimal(dgvrow.Cells["dgvtxtQuantity"].Value.ToString());
                     infoPurchaseDetails.RackId = Convert.ToDecimal(dgvrow.Cells["dgvcmbRack"].Value.ToString());
                     infoPurchaseDetails.Rate = Convert.ToDecimal(dgvrow.Cells["dgvtxtRate"].Value.ToString());
                     infoPurchaseDetails.ReceiptDetailsId = Convert.ToDecimal(dgvrow.Cells["dgvtxtMaterialReceiptDetailsId"].Value.ToString());
                     infoPurchaseDetails.SlNo = Convert.ToInt32(dgvrow.Cells["dgvtxtSlNo"].Value.ToString());
                     infoPurchaseDetails.TaxAmount = Convert.ToDecimal(dgvrow.Cells["dgvtxtTaxAmount"].Value.ToString());
                     infoPurchaseDetails.TaxId = Convert.ToDecimal(dgvrow.Cells["dgvcmbTax"].Value.ToString());
                     infoPurchaseDetails.UnitConversionId = Convert.ToDecimal(dgvrow.Cells["dgvtxtUnitConversionId"].Value.ToString());
                     infoPurchaseDetails.UnitId = Convert.ToDecimal(dgvrow.Cells["dgvcmbUnit"].Value.ToString());
                     infoPurchaseDetails.Extra1 = string.Empty;
                     infoPurchaseDetails.Extra2 = string.Empty;
                     infoPurchaseDetails.ExtraDate = Convert.ToDateTime(DateTime.Today);
                     if (dgvrow.Cells["dgvtxtPurchaseDetailsId"].Value != null)
                     {
                         if (dgvrow.Cells["dgvtxtPurchaseDetailsId"].Value.ToString() == "0" || dgvrow.Cells["dgvtxtPurchaseDetailsId"].Value.ToString() == string.Empty)
                         {
                             spPurchaseDetails.PurchaseDetailsAdd(infoPurchaseDetails);
                         }
                         else
                         {
                             infoPurchaseDetails.PurchaseDetailsId = Convert.ToDecimal(dgvrow.Cells["dgvtxtPurchaseDetailsId"].Value.ToString());
                             spPurchaseDetails.PurchaseDetailsEdit(infoPurchaseDetails);
                         }
                     }
                     else
                     {
                         spPurchaseDetails.PurchaseDetailsAdd(infoPurchaseDetails);
                     }
                     infoStockPosting.BatchId = infoPurchaseDetails.BatchId;
                     infoStockPosting.Date = infoPurchaseMaster.Date;
                     infoStockPosting.FinancialYearId = PublicVariables._decCurrentFinancialYearId;
                     infoStockPosting.GodownId = infoPurchaseDetails.GodownId;
                     infoStockPosting.InwardQty = infoPurchaseDetails.Qty; /// spUnitConvertion.UnitConversionRateByUnitConversionId(infoPurchaseDetails.UnitConversionId);
                     infoStockPosting.OutwardQty = 0;
                     infoStockPosting.ProductId = infoPurchaseDetails.ProductId;
                     infoStockPosting.RackId = infoPurchaseDetails.RackId;
                     infoStockPosting.Rate = infoPurchaseDetails.Rate;
                     infoStockPosting.UnitId = infoPurchaseDetails.UnitId;
                     if (infoPurchaseDetails.OrderDetailsId != 0)
                     {
                         infoStockPosting.InvoiceNo = infoPurchaseMaster.InvoiceNo;
                         infoStockPosting.VoucherNo = infoPurchaseMaster.VoucherNo;
                         infoStockPosting.VoucherTypeId = infoPurchaseMaster.VoucherTypeId;
                         infoStockPosting.AgainstInvoiceNo = "NA";
                         infoStockPosting.AgainstVoucherNo = "NA";
                         infoStockPosting.AgainstVoucherTypeId = 0;
                     }
                     else if (infoPurchaseDetails.ReceiptDetailsId != 0)
                     {
                         infoStockPosting.InvoiceNo = infoPurchaseMaster.InvoiceNo;
                         infoStockPosting.VoucherNo = infoPurchaseMaster.VoucherNo;
                         infoStockPosting.VoucherTypeId = infoPurchaseMaster.VoucherTypeId;
                         infoStockPosting.AgainstInvoiceNo = "NA";
                         infoStockPosting.AgainstVoucherNo = "NA";
                         infoStockPosting.AgainstVoucherTypeId = 0;
                     }
                     else if (infoPurchaseDetails.OrderDetailsId == 0 && infoPurchaseDetails.ReceiptDetailsId == 0)
                     {
                         infoStockPosting.InvoiceNo = infoPurchaseMaster.InvoiceNo;
                         infoStockPosting.VoucherNo = infoPurchaseMaster.VoucherNo;
                         infoStockPosting.VoucherTypeId = infoPurchaseMaster.VoucherTypeId;
                         infoStockPosting.AgainstInvoiceNo = "NA";
                         infoStockPosting.AgainstVoucherNo = "NA";
                         infoStockPosting.AgainstVoucherTypeId = 0;
                     }
                     infoStockPosting.Extra1 = string.Empty;
                     infoStockPosting.Extra2 = string.Empty;
                     infoStockPosting.ExtraDate = Convert.ToDateTime(DateTime.Today);
                     spStockPosting.StockPostingAdd(infoStockPosting);
                     if (infoPurchaseDetails.ReceiptDetailsId != 0)
                     {
                         infoStockPosting.InvoiceNo = infoMaterialReceiptMaster.InvoiceNo;
                         infoStockPosting.VoucherNo = infoMaterialReceiptMaster.VoucherNo;
                         infoStockPosting.VoucherTypeId = infoMaterialReceiptMaster.VoucherTypeId;
                         infoStockPosting.AgainstInvoiceNo = infoPurchaseMaster.InvoiceNo;
                         infoStockPosting.AgainstVoucherNo = infoPurchaseMaster.VoucherNo;
                         infoStockPosting.AgainstVoucherTypeId = infoPurchaseMaster.VoucherTypeId;
                         infoStockPosting.InwardQty = 0;
                         infoStockPosting.OutwardQty = infoPurchaseDetails.Qty; /// spUnitConvertion.UnitConversionRateByUnitConversionId(infoPurchaseDetails.UnitConversionId);
                         spStockPosting.StockPostingAdd(infoStockPosting);
                     }
                 }
             }
         }
         /*-----------------------------------------Ledger Posting----------------------------------------------------*/
         infoLedgerPosting.Credit = Convert.ToDecimal(txtGrandTotal.Text) * spExchangeRate.ExchangeRateViewByExchangeRateId(Convert.ToDecimal(cmbCurrency.SelectedValue.ToString()));
         infoLedgerPosting.Debit = 0;
         infoLedgerPosting.Date = Convert.ToDateTime(PublicVariables._dtCurrentDate);
         infoLedgerPosting.DetailsId = 0;
         infoLedgerPosting.InvoiceNo = infoPurchaseMaster.InvoiceNo;
         infoLedgerPosting.LedgerId = infoPurchaseMaster.LedgerId;
         infoLedgerPosting.VoucherNo = infoPurchaseMaster.VoucherNo;
         infoLedgerPosting.VoucherTypeId = infoPurchaseMaster.VoucherTypeId;
         infoLedgerPosting.YearId = PublicVariables._decCurrentFinancialYearId;
         infoLedgerPosting.ChequeDate = DateTime.Now;
         infoLedgerPosting.ChequeNo = string.Empty;
         infoLedgerPosting.Extra1 = string.Empty;
         infoLedgerPosting.Extra2 = string.Empty;
         infoLedgerPosting.ExtraDate = DateTime.Now;
         spLedgerPosting.LedgerPostingAdd(infoLedgerPosting);
         decimal DecBillDiscount = Convert.ToDecimal(txtBillDiscount.Text.Trim().ToString());
         if (DecBillDiscount > 0)
         {
             infoLedgerPosting.Credit = DecBillDiscount * spExchangeRate.ExchangeRateViewByExchangeRateId(Convert.ToDecimal(cmbCurrency.SelectedValue.ToString()));
             infoLedgerPosting.Debit = 0;
             infoLedgerPosting.Date = Convert.ToDateTime(PublicVariables._dtCurrentDate);
             infoLedgerPosting.DetailsId = 0;
             infoLedgerPosting.InvoiceNo = infoPurchaseMaster.InvoiceNo;
             infoLedgerPosting.LedgerId = 9;//ledger id of discount received ledger
             infoLedgerPosting.VoucherNo = infoPurchaseMaster.VoucherNo;
             infoLedgerPosting.VoucherTypeId = infoPurchaseMaster.VoucherTypeId;
             infoLedgerPosting.YearId = PublicVariables._decCurrentFinancialYearId;
             infoLedgerPosting.ChequeDate = DateTime.Now;
             infoLedgerPosting.ChequeNo = string.Empty;
             infoLedgerPosting.Extra1 = string.Empty;
             infoLedgerPosting.Extra2 = string.Empty;
             infoLedgerPosting.ExtraDate = DateTime.Now;
             spLedgerPosting.LedgerPostingAdd(infoLedgerPosting);
         }
         infoLedgerPosting.Credit = 0;
         infoLedgerPosting.Debit = TotalNetAmount();// * spExchangeRate.ExchangeRateViewByExchangeRateId(Convert.ToDecimal(cmbCurrency.SelectedValue.ToString()));
         infoLedgerPosting.Date = Convert.ToDateTime(PublicVariables._dtCurrentDate);
         infoLedgerPosting.DetailsId = 0;
         infoLedgerPosting.InvoiceNo = infoPurchaseMaster.InvoiceNo;
         infoLedgerPosting.LedgerId = infoPurchaseMaster.PurchaseAccount;
         infoLedgerPosting.VoucherNo = infoPurchaseMaster.VoucherNo;
         infoLedgerPosting.VoucherTypeId = infoPurchaseMaster.VoucherTypeId;
         infoLedgerPosting.YearId = PublicVariables._decCurrentFinancialYearId;
         infoLedgerPosting.ChequeDate = DateTime.Now;
         infoLedgerPosting.ChequeNo = string.Empty;
         infoLedgerPosting.Extra1 = string.Empty;
         infoLedgerPosting.Extra2 = string.Empty;
         infoLedgerPosting.ExtraDate = DateTime.Now;
         spLedgerPosting.LedgerPostingAdd(infoLedgerPosting);
         foreach (DataGridViewRow dgvrow in dgvAdditionalCost.Rows)
         {
             if (dgvrow.Cells["dgvcmbLedger"].Value != null)
             {
                 if (dgvrow.Cells["dgvcmbLedger"].Value.ToString() != string.Empty)
                 {
                     if (dgvrow.Cells["dgvtxtAdditionalCostAmount"].Value != null)
                     {
                         if (dgvrow.Cells["dgvtxtAdditionalCostAmount"].Value.ToString() != string.Empty)
                         {
                             /*-----------------------------------------Additional Cost Add----------------------------------------------------*/
                             infoAdditionalCost.Credit = 0;
                             infoAdditionalCost.Debit = Convert.ToDecimal(dgvrow.Cells["dgvtxtAdditionalCostAmount"].Value.ToString());
                             infoAdditionalCost.LedgerId = Convert.ToDecimal(dgvrow.Cells["dgvcmbLedger"].Value.ToString());
                             infoAdditionalCost.VoucherNo = infoPurchaseMaster.VoucherNo;
                             infoAdditionalCost.VoucherTypeId = infoPurchaseMaster.VoucherTypeId;
                             infoAdditionalCost.Extra1 = string.Empty;
                             infoAdditionalCost.Extra2 = string.Empty;
                             infoAdditionalCost.ExtraDate = DateTime.Now;
                             if (dgvrow.Cells["dgvtxtAdditionalCostId"].Value != null)
                             {
                                 if (dgvrow.Cells["dgvtxtAdditionalCostId"].Value.ToString() != string.Empty && dgvrow.Cells["dgvtxtAdditionalCostId"].Value.ToString() != string.Empty)
                                 {
                                     infoAdditionalCost.AdditionalCostId = Convert.ToDecimal(dgvrow.Cells["dgvtxtAdditionalCostId"].Value.ToString());
                                     spAdditionalCost.AdditionalCostEdit(infoAdditionalCost);
                                 }
                                 else
                                 {
                                     spAdditionalCost.AdditionalCostAdd(infoAdditionalCost);
                                 }
                             }
                             else
                             {
                                 spAdditionalCost.AdditionalCostAdd(infoAdditionalCost);
                             }
                             /*-----------------------------------------Additional Cost Ledger Posting----------------------------------------------------*/
                             infoLedgerPosting.Credit = 0;
                             infoLedgerPosting.Debit = infoAdditionalCost.Debit * spExchangeRate.ExchangeRateViewByExchangeRateId(Convert.ToDecimal(cmbCurrency.SelectedValue.ToString()));
                             infoLedgerPosting.Date = Convert.ToDateTime(PublicVariables._dtCurrentDate);
                             infoLedgerPosting.DetailsId = 0;
                             infoLedgerPosting.InvoiceNo = infoPurchaseMaster.InvoiceNo;
                             infoLedgerPosting.LedgerId = infoAdditionalCost.LedgerId;
                             infoLedgerPosting.VoucherNo = infoPurchaseMaster.VoucherNo;
                             infoLedgerPosting.VoucherTypeId = infoPurchaseMaster.VoucherTypeId;
                             infoLedgerPosting.YearId = PublicVariables._decCurrentFinancialYearId;
                             infoLedgerPosting.ChequeDate = DateTime.Now;
                             infoLedgerPosting.ChequeNo = string.Empty;
                             infoLedgerPosting.Extra1 = string.Empty;
                             infoLedgerPosting.Extra2 = string.Empty;
                             infoLedgerPosting.ExtraDate = DateTime.Now;
                             spLedgerPosting.LedgerPostingAdd(infoLedgerPosting);
                         }
                     }
                 }
             }
         }
         if (dgvTax.Visible)
         {
             foreach (DataGridViewRow dgvrow in dgvTax.Rows)
             {
                 if (dgvrow.Cells["dgvtxtTaxId"].Value != null)
                 {
                     if (dgvrow.Cells["dgvtxtTaxId"].Value.ToString() != string.Empty)
                     {
                         /*-----------------------------------------PurchaseBillTax Add----------------------------------------------------*/
                         infoPurchaseBillTax.PurchaseMasterId = decPurchaseMasterId;
                         infoPurchaseBillTax.TaxAmount = Convert.ToDecimal(dgvrow.Cells["dgvtxtTotalTax"].Value.ToString());
                         infoPurchaseBillTax.TaxId = Convert.ToDecimal(dgvrow.Cells["dgvtxtTaxId"].Value.ToString());
                         infoPurchaseBillTax.Extra1 = string.Empty;
                         infoPurchaseBillTax.Extra2 = string.Empty;
                         infoPurchaseBillTax.ExtraDate = DateTime.Now;
                         if (dgvrow.Cells["dgvtxtPurchaseBillTaxId"].Value != null)
                         {
                             if (dgvrow.Cells["dgvtxtPurchaseBillTaxId"].Value.ToString() != string.Empty && dgvrow.Cells["dgvtxtPurchaseBillTaxId"].Value.ToString() != "0")
                             {
                                 infoPurchaseBillTax.PurchaseBillTaxId = Convert.ToDecimal(dgvrow.Cells["dgvtxtPurchaseBillTaxId"].Value.ToString());
                                 spPurchaseBillTax.PurchaseBillTaxEdit(infoPurchaseBillTax);
                             }
                             else
                             {
                                 spPurchaseBillTax.PurchaseBillTaxAdd(infoPurchaseBillTax);
                             }
                         }
                         else
                         {
                             spPurchaseBillTax.PurchaseBillTaxAdd(infoPurchaseBillTax);
                         }
                         /*-----------------------------------------Tax Ledger Posting----------------------------------------------------*/
                         infoLedgerPosting.Credit = 0;
                         infoLedgerPosting.Debit = infoPurchaseBillTax.TaxAmount * spExchangeRate.ExchangeRateViewByExchangeRateId(Convert.ToDecimal(cmbCurrency.SelectedValue.ToString()));
                         infoLedgerPosting.Date = Convert.ToDateTime(PublicVariables._dtCurrentDate);
                         infoLedgerPosting.DetailsId = 0;
                         infoLedgerPosting.InvoiceNo = infoPurchaseMaster.InvoiceNo;
                         infoLedgerPosting.LedgerId = Convert.ToDecimal(dgvrow.Cells["dgvtxtLedgerId"].Value.ToString());
                         infoLedgerPosting.VoucherNo = infoPurchaseMaster.VoucherNo;
                         infoLedgerPosting.VoucherTypeId = infoPurchaseMaster.VoucherTypeId;
                         infoLedgerPosting.YearId = PublicVariables._decCurrentFinancialYearId;
                         infoLedgerPosting.ChequeDate = DateTime.Now;
                         infoLedgerPosting.ChequeNo = string.Empty;
                         infoLedgerPosting.Extra1 = string.Empty;
                         infoLedgerPosting.Extra2 = string.Empty;
                         infoLedgerPosting.ExtraDate = DateTime.Now;
                         spLedgerPosting.LedgerPostingAdd(infoLedgerPosting);
                     }
                 }
             }
         }
         /*-----------------------------------------PartyBalance Posting----------------------------------------------------*/
         infoAccountLedger = spAccountLedger.AccountLedgerView(infoPurchaseMaster.LedgerId);
         if (infoAccountLedger.BillByBill == true)
         {
             infoPartyBalance.Credit = Convert.ToDecimal(txtGrandTotal.Text);
             infoPartyBalance.Debit = 0;
             if (txtCreditPeriod.Text != string.Empty)
             {
                 infoPartyBalance.CreditPeriod = Convert.ToInt32(txtCreditPeriod.Text);
             }
             infoPartyBalance.Date = Convert.ToDateTime(txtVoucherDate.Text);
             infoPartyBalance.ExchangeRateId = infoPurchaseMaster.ExchangeRateId;
             infoPartyBalance.FinancialYearId = PublicVariables._decCurrentFinancialYearId;
             infoPartyBalance.LedgerId = infoPurchaseMaster.LedgerId;
             infoPartyBalance.ReferenceType = "New";
             infoPartyBalance.InvoiceNo = infoPurchaseMaster.InvoiceNo;
             infoPartyBalance.VoucherNo = infoPurchaseMaster.VoucherNo;
             infoPartyBalance.VoucherTypeId = infoPurchaseMaster.VoucherTypeId;
             infoPartyBalance.AgainstInvoiceNo = "NA";
             infoPartyBalance.AgainstVoucherNo = "NA";
             infoPartyBalance.AgainstVoucherTypeId = 0;
             infoPartyBalance.Extra1 = string.Empty;
             infoPartyBalance.Extra2 = string.Empty;
             infoPartyBalance.ExtraDate = DateTime.Now;
             spPartyBalance.PartyBalanceAdd(infoPartyBalance);
         }
         Messages.UpdatedMessage();
         if (cbxPrintAfterSave.Checked)
         {
             if (spSettings.SettingsStatusCheck("Printer") == "Dot Matrix")
             {
                 PrintForDotMatrix(decPurchaseMasterId);
             }
             else
             {
                 Print(decPurchaseMasterId);
             }
         }
         Clear();
         this.Close();
     }
     catch (Exception ex)
     {
         MessageBox.Show("PI39:" + ex.Message, "OpenMiracle", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }