public string GenerateInvoiceNo(string branchCode, out string errorMessage) { try { errorMessage = string.Empty; string suffix = string.Empty, prefix = string.Empty, billno = string.Empty; TblInvoiceMaster _invoiceMaster = null; using (Repository <TblInvoiceMaster> _repo = new Repository <TblInvoiceMaster>()) { _invoiceMaster = _repo.TblInvoiceMaster.Where(x => x.BranchCode == branchCode).OrderByDescending(x => x.InvoiceMasterId).FirstOrDefault(); if (_invoiceMaster != null) { var invSplit = _invoiceMaster.InvoiceNo.Split('-'); billno = $"{invSplit[0]}-{Convert.ToDecimal(invSplit[1])+1}-{invSplit[2]}"; } else { new Common.CommonHelper().GetSuffixPrefix(19, branchCode, out prefix, out suffix); billno = $"{prefix}-1-{suffix}"; } } if (string.IsNullOrEmpty(billno)) { errorMessage = "Invoice no not gererated please enter manully."; } return(billno); } catch (Exception ex) { throw ex; } }
public TblInvoiceMaster GetOne(string guid, bool IsNew) { if (IsNew == true) { using (AppDbContext db = new AppDbContext()) { TblInvoiceMaster n = new TblInvoiceMaster(); n.Guid = Guid.NewGuid(); n.Id = Max() + 1; n.Date = DateTime.Now.Date; return(n); } } else { using (AppDbContext db = new AppDbContext()) { Guid n = Guid.Parse(guid); TblInvoiceMaster query = db.TblInvoiceMaster.Include(x => x.tblCustomer).Include(x => x.tblInvoiceDetails.Select(c => c.tblProduct)) .Where(x => x.Guid == n).FirstOrDefault(); query.tblInvoiceDetails = query.tblInvoiceDetails.OrderBy(x => x.Id).ToList(); return(query); } } }
public void Update(TblInvoiceMaster TblInvoiceMaster) { using (AppDbContext db = new AppDbContext()) { using (var dbContextTransaction = db.Database.BeginTransaction()) { try { var original = db.TblInvoiceMaster.Where(x => x.Guid == TblInvoiceMaster.Guid).FirstOrDefault(); db.Entry(original).CurrentValues.SetValues(DataVerified(TblInvoiceMaster)); db.TblInvoiceDetail.RemoveRange(db.TblInvoiceDetail.Where(x => x.TblInvoiceMasterGuid == TblInvoiceMaster.Guid).ToList()); db.TblInvoiceDetail.AddRange(TblInvoiceMaster.tblInvoiceDetails); db.SaveChanges(); dbContextTransaction.Commit(); } catch (Exception) { dbContextTransaction.Rollback(); } } } }
public TblVoucherMaster AddVoucherMaster(ERPContext context, TblInvoiceMaster invoice, TblBranch branch, decimal?voucherTypeId, string paymentType) { try { //using (ERPContext context = new ERPContext()) //{ var _voucherMaster = new TblVoucherMaster(); _voucherMaster.BranchCode = invoice.BranchCode; _voucherMaster.BranchName = branch.BranchName; _voucherMaster.VoucherDate = invoice.InvoiceDate; _voucherMaster.VoucherTypeIdMain = voucherTypeId; _voucherMaster.VoucherTypeIdSub = 35; _voucherMaster.VoucherNo = invoice.InvoiceNo; _voucherMaster.Amount = invoice.GrandTotal; _voucherMaster.PaymentType = paymentType;//accountLedger.CrOrD _voucherMaster.Narration = "Sales Invoice"; _voucherMaster.ServerDate = DateTime.Now; _voucherMaster.UserId = invoice.UserId; _voucherMaster.UserName = invoice.UserName; _voucherMaster.EmployeeId = -1; context.TblVoucherMaster.Add(_voucherMaster); if (context.SaveChanges() > 0) { return(_voucherMaster); } #region comment //var _voucherMaster = new TblVoucherMaster(); //_voucherMaster.BranchCode = invoice.BranchCode; //_voucherMaster.BranchName = _branch.BranchName; //_voucherMaster.VoucherDate = invoice.InvoiceDate; //_voucherMaster.VoucherTypeIdMain = _vouchertType.VoucherTypeId; //_voucherMaster.VoucherTypeIdSub = 35; //_voucherMaster.VoucherNo = invoice.InvoiceNo; //_voucherMaster.Amount = invoice.GrandTotal; //_voucherMaster.PaymentType = _accountLedger.CrOrDr; //_voucherMaster.Narration = "Sales Invoice"; //_voucherMaster.ServerDate = DateTime.Now; //_voucherMaster.UserId = invoice.UserId; //_voucherMaster.UserName = invoice.UserName; //_voucherMaster.EmployeeId = -1; //repo.TblVoucherMaster.Add(_voucherMaster); //if(!(repo.SaveChanges() > 0)) //{ // dbTransaction.Rollback(); // return false; //} #endregion return(null); // } } catch (Exception ex) { throw ex; } }
public void Insert(TblInvoiceMaster TblInvoiceMaster) { using (AppDbContext db = new AppDbContext()) { db.TblInvoiceMaster.Add(DataVerified(TblInvoiceMaster)); db.TblInvoiceDetail.AddRange(TblInvoiceMaster.tblInvoiceDetails); db.SaveChanges(); } }
public TblInvoiceMaster DataVerified(TblInvoiceMaster TblInvoiceMaster) { TblInvoiceMaster.TblCustomerGuid = TblInvoiceMaster.tblCustomer.Guid; TblInvoiceMaster.tblCustomer = null; foreach (var item in TblInvoiceMaster.tblInvoiceDetails) { item.tblProduct = null; item.tblInvoiceMaster = null; } return(TblInvoiceMaster); }
public TblStockInformation AddStockInformation(ERPContext context, TblInvoiceMaster invoice, TblBranch _branch, TblProduct _product, decimal?qty, decimal?rate) { try { //using(ERPContext context=new ERPContext()) //{ var _stockInformation = new TblStockInformation(); _stockInformation.BranchId = _branch.BranchId; _stockInformation.BranchCode = _branch.BranchCode; _stockInformation.ShiftId = invoice.ShiftId; _stockInformation.VoucherNo = invoice.VoucherNo; _stockInformation.VoucherTypeId = invoice.VoucherTypeId; _stockInformation.InvoiceNo = invoice.InvoiceNo; _stockInformation.ProductId = _product.ProductId; _stockInformation.ProductCode = _product.ProductCode; _stockInformation.OutwardQty = qty; _stockInformation.Rate = rate; context.TblStockInformation.Add(_stockInformation); if (context.SaveChanges() > 0) { return(_stockInformation); } #region Comment //var _stockInformation = new TblStockInformation(); //_stockInformation.BranchId = _branch.BranchId; //_stockInformation.BranchCode = _branch.BranchCode; //_stockInformation.ShiftId = invoice.ShiftId; //_stockInformation.VoucherNo = invoice.VoucherNo; //_stockInformation.VoucherTypeId = invoice.VoucherTypeId; //_stockInformation.InvoiceNo = invoice.InvoiceNo; //_stockInformation.ProductId = _product.ProductId; //_stockInformation.ProductCode = _product.ProductCode; //_stockInformation.OutwardQty = invdtl.Qty > 0 ? invdtl.Qty : invdtl.FQty; //_stockInformation.Rate = invdtl.Rate; //repo.TblStockInformation.Add(_stockInformation); //repo.SaveChanges(); #endregion return(null); // } } catch (Exception ex) { throw ex; } }
public void Put([FromBody] TblInvoiceMaster invoiceMaster) { repository.Update(invoiceMaster); }
public void Post([FromBody] TblInvoiceMaster invoiceMaster) { repository.Insert(invoiceMaster); }
public TblVoucherDetail AddVoucherDetails(ERPContext context, TblInvoiceMaster invoice, TblBranch _branch, TblVoucherMaster _voucherMaster, TblAccountLedger _accountLedger, decimal?productRate, bool isFromInvoiceDetials = true) { try { //using(ERPContext context=new ERPContext()) //{ var _voucherDetail = new TblVoucherDetail(); _voucherDetail.VoucherMasterId = _voucherMaster.VoucherMasterId; _voucherDetail.VoucherDetailDate = _voucherMaster.VoucherDate; _voucherDetail.BranchId = _branch.BranchId; _voucherDetail.BranchCode = invoice.BranchCode; _voucherDetail.BranchName = invoice.BranchName; if (isFromInvoiceDetials) { _voucherDetail.FromLedgerId = invoice.LedgerId; _voucherDetail.FromLedgerCode = invoice.LedgerCode; _voucherDetail.FromLedgerName = invoice.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); } #region comment //var _voucherDetail = new TblVoucherDetail(); //_voucherDetail.VoucherMasterId = _voucherMaster.VoucherMasterId; //_voucherDetail.VoucherDetailDate = _voucherMaster.VoucherDate; //_voucherDetail.BranchId = _branch.BranchId; //_voucherDetail.BranchCode = invoice.BranchCode; //_voucherDetail.BranchName = invoice.BranchName; //_voucherDetail.FromLedgerId = invoice.LedgerId; //_voucherDetail.FromLedgerCode = invoice.LedgerCode; //_voucherDetail.FromLedgerName = invoice.LedgerName; ////To ledger clarifiaction on selecion of product //_voucherDetail.ToLedgerId = _accountLedger.LedgerId; //_voucherDetail.ToLedgerCode = _accountLedger.LedgerCode; //_voucherDetail.ToLedgerName = _accountLedger.LedgerName; //_voucherDetail.Amount = invdtl.Rate; //_voucherDetail.TransactionType = _accountLedger.CrOrDr; //_voucherDetail.CostCenter = _accountLedger.BranchCode; //_voucherDetail.ServerDate = DateTime.Now; //_voucherDetail.Narration = "Sales Invoice Product group A/c:" + _voucherDetail.TransactionType; //repo.TblVoucherDetail.Add(_voucherDetail); //repo.SaveChanges(); #endregion return(null); // } } catch (Exception ex) { throw ex; } }
/************************* Helper methods For invoice*******************************************/ public bool RegisterBill(TblInvoiceMaster invoice, List <TblInvoiceDetail> invoiceDetails) { try { decimal shifId = Convert.ToDecimal(new UserManagmentHelper().GetShiftId(invoice.UserId, null)); invoice.IsSalesReturned = false; invoice.IsManualEntry = false; TblTaxStructure _taxStructure = null; TblProduct _product = null; using (ERPContext repo = new ERPContext()) { using (var dbTransaction = repo.Database.BeginTransaction()) { try { //add voucher typedetails var _branch = GetBranches(invoice.BranchCode).ToArray().FirstOrDefault(); var _accountLedger = GetAccountLedgers(invoice.LedgerCode).ToArray().FirstOrDefault(); var _vouchertType = GetVoucherType(19).FirstOrDefault(); #region Add voucher master record var _voucherMaster = AddVoucherMaster(repo, invoice, _branch, _vouchertType.VoucherTypeId, _accountLedger.CrOrDr); #endregion invoice.LedgerId = _accountLedger.LedgerId; if (invoice.VehicleRegNo != null && invoice.MemberCode != null) { var vehicleId = GetVehicles(invoice.VehicleRegNo, Convert.ToString(invoice.MemberCode)).ToArray().FirstOrDefault(); invoice.VehicleId = vehicleId.VehicleId == null ? -1 : vehicleId.VehicleId; //vehicleId.VehicleId; } invoice.MemberCode = invoice.MemberCode == null?-1: invoice.MemberCode; invoice.ShiftId = shifId; invoice.VoucherNo = _voucherMaster.VoucherMasterId.ToString(); invoice.BranchName = _branch.BranchName; invoice.VoucherTypeId = 19; invoice.ServerDateTime = DateTime.Now; repo.TblInvoiceMaster.Add(invoice); repo.SaveChanges(); foreach (var invdtl in invoiceDetails) { _product = GetProducts(invdtl.ProductCode).FirstOrDefault(); _taxStructure = GetTaxStructure(Convert.ToDecimal(_product.TaxStructureCode)); _accountLedger = GetAccountLedgersByLedgerId((decimal)_taxStructure?.SalesAccount).FirstOrDefault(); #region Add voucher Details var _voucherDetail = AddVoucherDetails(repo, invoice, _branch, _voucherMaster, _accountLedger, invdtl.Rate); #endregion #region InvioceDetail invdtl.InvoiceMasterId = invoice.InvoiceMasterId; invdtl.VoucherNo = invoice.VoucherNo; invdtl.InvoiceNo = invoice.InvoiceNo; invdtl.StateCode = invoice.StateCode; invdtl.ShiftId = invoice.ShiftId; invdtl.UserId = invoice.UserId; invdtl.EmployeeId = -1; invdtl.ServerDateTime = DateTime.Now; invdtl.ShiftId = shifId; invdtl.PumpId = invdtl.PumpId ?? -1; invdtl.PumpNo = invdtl.PumpNo ?? -1; invdtl.SlipNo = invdtl.SlipNo ?? -1; repo.TblInvoiceDetail.Add(invdtl); repo.SaveChanges(); #endregion #region Add stock transaction and Account Ledger Transaction AddStockInformation(repo, invoice, _branch, _product, invdtl.Qty > 0 ? invdtl.Qty : invdtl.FQty, invdtl.Rate); AddAccountLedgerTransactions(repo, _voucherDetail, invoice.InvoiceDate); #endregion } _accountLedger = GetAccountLedgers(invoice.LedgerCode).ToArray().FirstOrDefault(); AddVoucherDetails(repo, invoice, _branch, _voucherMaster, _accountLedger, invoice.GrandTotal, false); //CHech weather igs or sg ,cg st var _stateWiseGsts = GetStateWiseGsts(invoice.StateCode).FirstOrDefault(); if (_stateWiseGsts.Igst == 1) { //Add IGST record var _accAL = GetAccountLedgers("243").ToArray().FirstOrDefault(); AddVoucherDetails(repo, invoice, _branch, _voucherMaster, _accAL, invoice.TotalAmount, false); } else { // sgst var _accAL = GetAccountLedgers("240").ToArray().FirstOrDefault(); AddVoucherDetails(repo, invoice, _branch, _voucherMaster, _accAL, invoice.TotalAmount, false); // sgst _accAL = GetAccountLedgers("241").ToArray().FirstOrDefault(); AddVoucherDetails(repo, invoice, _branch, _voucherMaster, _accAL, invoice.TotalAmount, false); } dbTransaction.Commit(); return(true); } catch (Exception ex) { dbTransaction.Rollback(); throw ex; } } } } catch (Exception ex) { throw ex; } }
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; } }