Exemplo n.º 1
0
        public TblStockInformation AddStockInformation(ERPContext context, TblInvoiceMasterReturn invoiceReturn, TblBranch _branch, TblProduct _product, decimal?qty, decimal?rate)
        {
            try
            {
                //using(ERPContext context=new ERPContext())
                //{
                var _stockInformation = new TblStockInformation
                {
                    BranchId      = _branch.BranchId,
                    BranchCode    = _branch.BranchCode,
                    ShiftId       = invoiceReturn.ShiftId,
                    VoucherNo     = invoiceReturn.VoucherNo,
                    VoucherTypeId = invoiceReturn.VoucherTypeId,
                    InvoiceNo     = invoiceReturn.InvoiceReturnNo,
                    ProductId     = _product.ProductId,
                    ProductCode   = _product.ProductCode,
                    OutwardQty    = qty,
                    Rate          = rate
                };

                context.TblStockInformation.Add(_stockInformation);
                if (context.SaveChanges() > 0)
                {
                    return(_stockInformation);
                }

                return(null);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 2
0
        public TblVoucherMaster AddVoucherMaster(ERPContext context, TblInvoiceMasterReturn invoiceReturn, TblBranch branch, decimal?voucherTypeId, string paymentType)
        {
            try
            {
                var _voucherMaster = new TblVoucherMaster
                {
                    BranchCode        = invoiceReturn.BranchCode,
                    BranchName        = branch.BranchName,
                    VoucherDate       = invoiceReturn.InvoiceDate,
                    VoucherTypeIdMain = voucherTypeId,
                    VoucherTypeIdSub  = 35,
                    VoucherNo         = invoiceReturn.InvoiceReturnNo,
                    Amount            = invoiceReturn.GrandTotal,
                    PaymentType       = paymentType,//accountLedger.CrOrD
                    Narration         = "Sales Invoice Return",
                    ServerDate        = DateTime.Now,
                    UserId            = invoiceReturn.UserId,
                    UserName          = invoiceReturn.UserName,
                    EmployeeId        = -1
                };

                context.TblVoucherMaster.Add(_voucherMaster);
                if (context.SaveChanges() > 0)
                {
                    return(_voucherMaster);
                }

                return(null);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 3
0
        public TblVoucherDetail AddVoucherDetails(ERPContext context, TblInvoiceMasterReturn invoiceReturn, TblBranch _branch, TblVoucherMaster _voucherMaster, TblAccountLedger _accountLedger, decimal?productRate, bool isFromInvoiceDetials = true)
        {
            try
            {
                //using(ERPContext context=new ERPContext())
                //{
                var _voucherDetail = new TblVoucherDetail
                {
                    VoucherMasterId   = _voucherMaster.VoucherMasterId,
                    VoucherDetailDate = _voucherMaster.VoucherDate,
                    BranchId          = _branch.BranchId,
                    BranchCode        = invoiceReturn.BranchCode,
                    BranchName        = invoiceReturn.BranchName
                };
                if (isFromInvoiceDetials)
                {
                    _voucherDetail.FromLedgerId   = invoiceReturn.LedgerId;
                    _voucherDetail.FromLedgerCode = invoiceReturn.LedgerCode;
                    _voucherDetail.FromLedgerName = invoiceReturn.LedgerName;
                }
                //To ledger  clarifiaction on selecion of product

                _voucherDetail.ToLedgerId      = _accountLedger.LedgerId;
                _voucherDetail.ToLedgerCode    = _accountLedger.LedgerCode;
                _voucherDetail.ToLedgerName    = _accountLedger.LedgerName;
                _voucherDetail.Amount          = productRate;
                _voucherDetail.TransactionType = _accountLedger.CrOrDr;
                _voucherDetail.CostCenter      = _accountLedger.BranchCode;
                _voucherDetail.ServerDate      = DateTime.Now;
                _voucherDetail.Narration       = $"Sales Invoice {_accountLedger.LedgerName} A /c: {_voucherDetail.TransactionType}";

                context.TblVoucherDetail.Add(_voucherDetail);
                if (context.SaveChanges() > 0)
                {
                    return(_voucherDetail);
                }


                return(null);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 4
0
        public TblInvoiceMasterReturn RegisterInvoiceReturns(string invoiceReturnNo, decimal invoiceMasterId, out string errorMessage)
        {
            try
            {
                errorMessage = string.Empty;
                string                        _invoiceMasterDetailsjson = string.Empty;
                TblTaxStructure               _taxStructure             = null;
                TblProduct                    _product                = null;
                TblInvoiceMasterReturn        invoiceMasterReturn     = null;
                List <TblInvoiceReturnDetail> invoiceReturnDetailList = null;
                TblInvoiceMaster              invoice        = GetInvoiceMaster(invoiceMasterId);
                List <TblInvoiceDetail>       invoiceDetails = GetInvoiceDetail(invoiceMasterId);


                if (invoice.IsSalesReturned.Value)
                {
                    errorMessage = $"Sales no:{invoice.InvoiceNo} is already return.";
                    return(null);
                }


                invoice.IsSalesReturned = true;
                invoice.IsManualEntry   = false;

                var _invoiceMasterjson = JsonConvert.SerializeObject(invoice);
                invoiceMasterReturn = (JsonConvert.DeserializeObject <TblInvoiceMasterReturn>(_invoiceMasterjson));
                //details section
                invoiceReturnDetailList = new List <TblInvoiceReturnDetail>();
                foreach (var invd in invoiceDetails)
                {
                    _invoiceMasterDetailsjson = JsonConvert.SerializeObject(invd);
                    invoiceReturnDetailList.Add(JsonConvert.DeserializeObject <TblInvoiceReturnDetail>(_invoiceMasterDetailsjson));
                }

                using ERPContext repo   = new ERPContext();
                using var dbTransaction = repo.Database.BeginTransaction();
                try
                {
                    //update invoice master table;
                    repo.TblInvoiceMaster.Update(invoice);
                    repo.SaveChanges();


                    //add voucher typedetails
                    var _invoiceHelper = new InvoiceHelper();
                    var _branch        = _invoiceHelper.GetBranches(invoice.BranchCode).ToArray().FirstOrDefault();

                    var _accountLedger = _invoiceHelper.GetAccountLedgers(invoice.LedgerCode).ToArray().FirstOrDefault();
                    var _vouchertType  = GetVoucherType(20).FirstOrDefault();

                    #region Add voucher master record
                    invoiceMasterReturn.InvoiceReturnNo = invoiceReturnNo;
                    var _voucherMaster = AddVoucherMaster(repo, invoiceMasterReturn, _branch, _vouchertType.VoucherTypeId, _accountLedger.CrOrDr);
                    #endregion

                    invoiceMasterReturn.VoucherNo         = _voucherMaster.VoucherMasterId.ToString();
                    invoiceMasterReturn.InvoiceReturnDate = DateTime.Now;
                    invoiceMasterReturn.ServerDateTime    = DateTime.Now;
                    repo.TblInvoiceMasterReturn.Add(invoiceMasterReturn);
                    repo.SaveChanges();

                    foreach (var invdtl in invoiceReturnDetailList)
                    {
                        _product       = _invoiceHelper.GetProducts(invdtl.ProductCode).FirstOrDefault();
                        _taxStructure  = _invoiceHelper.GetTaxStructure(invdtl.TaxStructureId);
                        _accountLedger = _invoiceHelper.GetAccountLedgersByLedgerId((decimal)_taxStructure.SalesAccount).FirstOrDefault();

                        #region Add voucher Details
                        var _voucherDetail = AddVoucherDetails(repo, invoiceMasterReturn, _branch, _voucherMaster, _accountLedger, invdtl.Rate);
                        #endregion

                        #region InvioceDetail

                        invdtl.InvoiceMasterReturnId = invoiceMasterReturn.InvoiceMasterReturnId;
                        invdtl.InvoiceReturnNo       = invoice.InvoiceNo;
                        invdtl.InvoiceReturnDate     = DateTime.Now;
                        invdtl.VoucherNo             = invoiceMasterReturn.VoucherNo;
                        invdtl.StateCode             = invoice.StateCode;
                        invdtl.ShiftId = invoice.ShiftId;
                        invdtl.UserId  = invoice.UserId;

                        invdtl.EmployeeId     = -1;
                        invdtl.ServerDateTime = DateTime.Now;

                        repo.TblInvoiceReturnDetail.Add(invdtl);
                        repo.SaveChanges();

                        #endregion

                        #region Add stock transaction  and Account Ledger Transaction
                        AddStockInformation(repo, invoiceMasterReturn, _branch, _product, invdtl.Qty > 0 ? invdtl.Qty : invdtl.FQty, invdtl.Rate);

                        AddAccountLedgerTransactions(repo, _voucherDetail, invoice.InvoiceDate);
                        #endregion
                    }

                    _accountLedger = _invoiceHelper.GetAccountLedgers(invoice.LedgerCode).ToArray().FirstOrDefault();
                    AddVoucherDetails(repo, invoiceMasterReturn, _branch, _voucherMaster, _accountLedger, invoice.GrandTotal, false);

                    //CHech weather igs or sg ,cg st
                    var _stateWiseGsts = _invoiceHelper.GetStateWiseGsts(invoice.StateCode).FirstOrDefault();
                    if (_stateWiseGsts.Igst == 1)
                    {
                        //Add IGST record
                        var _accAL = _invoiceHelper.GetAccountLedgers("243").ToArray().FirstOrDefault();
                        AddVoucherDetails(repo, invoiceMasterReturn, _branch, _voucherMaster, _accAL, invoice.TotalAmount, false);
                    }
                    else
                    {
                        // sgst
                        var _accAL = _invoiceHelper.GetAccountLedgers("240").ToArray().FirstOrDefault();
                        AddVoucherDetails(repo, invoiceMasterReturn, _branch, _voucherMaster, _accAL, invoice.TotalAmount, false);
                        // sgst
                        _accAL = _invoiceHelper.GetAccountLedgers("241").ToArray().FirstOrDefault();
                        AddVoucherDetails(repo, invoiceMasterReturn, _branch, _voucherMaster, _accAL, invoice.TotalAmount, false);
                    }

                    dbTransaction.Commit();
                    return(invoiceMasterReturn);
                }
                catch (Exception ex)
                {
                    dbTransaction.Rollback();
                    throw ex;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }