public static string SaveBonus(BO_BonusRevenue br, int empID, System.Data.Entity.DbContextTransaction trans = null, AprosysAccountingEntities context = null)
        {
            AprosysAccountingEntities db = context == null ? new AprosysAccountingEntities() : context;
            var transaction = trans == null?db.Database.BeginTransaction() : trans;

            try
            {
                var editVal = trans != null?db.Acc_GL.Where(x => x.IsActive == false && x.CoaId == 0).Select(x => new { createdBy = x.CreatedBy.Value, createdDate = x.CreatedDate.Value }).FirstOrDefault() : new { createdBy = empID, createdDate = DateTime.Now };
                if (br.TransactionId != null)
                {
                    var update = db.Acc_GL.Where(x =>

                                                 (x.TranId == br.TransactionId || x.GlId == br.TransactionId) && x.IsActive == true).ToList();
                    foreach (var rows in update)
                    {
                        rows.IsActive = false;
                    }
                    db.SaveChanges();
                }
                var GLParent = new Acc_GL()
                {
                    CoaId = 5, UserId = empID, Comments = br.Misc, ActivityTimestamp = br.activityDate, Debit = br.BonusAmount, Credit = br.BonusAmount, TranTypeId = (int)TransactionTypes.Bonus, IsActive = true, CreatedBy = editVal.createdBy, CreatedDate = editVal.createdDate, ModifiedBy = empID, ModifiedDate = DateTime.Now
                };
                db.Acc_GL.Add(GLParent);
                db.SaveChanges();
                var GLDebitEntry = new Acc_GL()
                {
                    TranId = GLParent.GlId, Comments = br.Misc, UserId = empID, CoaId = br.BankAccount != null?Convert.ToInt32(br.BankAccount) : (int?)null, ActivityTimestamp = br.activityDate, TranTypeId = (int)TransactionTypes.Bonus, IsActive = true, Debit = br.BonusAmount, CreatedBy = editVal.createdBy, CreatedDate = editVal.createdDate, ModifiedBy = empID, ModifiedDate = DateTime.Now
                };
                db.Acc_GL.Add(GLDebitEntry);
                var GLCreditEntry = new Acc_GL()
                {
                    TranId = GLParent.GlId, Comments = br.Misc, UserId = empID, CoaId = br.RevenueAccount != null?Convert.ToInt32(br.RevenueAccount) : (int?)null, ActivityTimestamp = br.activityDate, TranTypeId = (int)TransactionTypes.Bonus, IsActive = true, Credit = br.BonusAmount, CreatedBy = editVal.createdBy, CreatedDate = editVal.createdDate, ModifiedBy = empID, ModifiedDate = DateTime.Now
                };
                db.Acc_GL.Add(GLCreditEntry);
                db.SaveChanges();
                if (trans == null)
                {
                    transaction.Commit();
                    db.Dispose();
                    transaction.Dispose();
                }
                return("Success");
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                throw;
            }
        }
Exemple #2
0
        public static string DeleteSaleInvoice(string voucherNo, int empID, System.Data.Entity.DbContextTransaction trans = null, AprosysAccountingEntities context = null)
        {
            AprosysAccountingEntities db = context == null ? new AprosysAccountingEntities() : context;
            var transaction = trans == null?db.Database.BeginTransaction() : trans;

            try
            {
                var snv = db.Acc_GL.Where(x => x.InvoiceNo == voucherNo && x.IsActive == true
                                          &&
                                          x.TranTypeId == 2
                                          ).ToList();
                foreach (var item in snv.Where(x => x.CoaId == 6))
                {
                    var txLinks = db.Acc_GLTxLinks.Where(x => x.GLID == item.GlId && x.IsActive == true).ToList();
                    //Revert Stock to purchases
                    foreach (var txlinks in txLinks)
                    {
                        var pnv = db.Acc_GL.SingleOrDefault(x => x.GlId == txlinks.RelGLID);
                        pnv.QuantityBalance += txlinks.Quantity;
                        txlinks.IsActive     = false;
                    }
                }
                foreach (var item in snv)
                {
                    item.IsActive = false;
                    if (trans == null)
                    {
                        item.ModifiedBy = empID; item.ModifiedDate = DateTime.Now;
                    }
                }
                BL_CreditSales.DeletePartialPayments(db, voucherNo, empID);

                db.SaveChanges();

                if (trans == null)
                {
                    transaction.Commit();
                    db.Dispose();
                    transaction.Dispose();
                }

                return("Success");
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                throw;
            }
        }
Exemple #3
0
        public static string SaveSales(BO_SaleInvoice pi, System.Data.Entity.DbContextTransaction trans = null, AprosysAccountingEntities context = null)
        {
            //if (pi.items.GroupBy(x => x.itemID).Where(x => x.Count() > 1).Count() == 1) { return "bad request"; }
            //if transaction data is delted


            AprosysAccountingEntities db = context == null ? new AprosysAccountingEntities() : context;

            #region if Customer , Items And Services Deleted
            var _cust = db.Customers.Where(x => x.Id == pi.customerID && x.IsActive == false).FirstOrDefault();
            if (_cust != null)
            {
                throw new Exception("Customer is not Exist ");
            }

            List <int> itemids = pi.items.Select(x => x.itemID).ToList();
            var        _item   = db.Items.Where(x => itemids.Contains(x.Id) && x.IsActive == false).FirstOrDefault();
            if (_item != null)
            {
                throw new Exception("Items not Exist ");
            }
            List <int> serviceids = pi.items.Select(x => x.coaID).ToList();
            var        _service   = db.Acc_COA.Where(x => serviceids.Contains(x.CoaId) && x.IsActive == false).FirstOrDefault();
            if (_service != null)
            {
                throw new Exception("Services not Exist ");
            }
            #endregion


            var transaction = trans == null?db.Database.BeginTransaction() : trans;

            try
            {
                string InvoiceNumber;
                var    editVal = trans != null?db.Acc_GL.Where(x => x.IsActive == false && x.CoaId == 0).Select(x => new { createdBy = x.CreatedBy.Value, createdDate = x.CreatedDate.Value }).FirstOrDefault() : new { createdBy = pi.empID, createdDate = DateTime.Now };
                //Getting Purchased Stock for FIFO Base deduction
                var GLPurchases = db.Acc_GL.Where(x => (x.IsActive ?? false) && x.CoaId == 6 && x.QuantityBalance > 0 &&
                                                  (x.TranTypeId == 1 || x.TranTypeId == 7)).OrderBy(x => x.ActivityTimestamp).AsEnumerable();
                int transType = (int)Constants.TransactionTypes.Sales;
                pi.invoiceNo = trans == null?Util.GetNextVoucher(transType) : pi.invoiceNo;

                bool _isSalesCredit = pi.IsSalesCredit;
                //Parent Entry
                var GLParent = new Acc_GL()
                {
                    CoaId = 0, UserId = pi.empID, Comments = pi.comments, CustId = pi.customerID, ActivityTimestamp = pi.saleDate, Debit = pi.netAmount, Credit = pi.netAmount, TranTypeId = transType, InvoiceNo = pi.invoiceNo, IsActive = true, CreatedBy = editVal.createdBy, CreatedDate = editVal.createdDate, ModifiedBy = pi.empID, ModifiedDate = DateTime.Now, IsSalesCredit = _isSalesCredit, SalesPersonId = pi.salesPersonId
                };
                InvoiceNumber = GLParent.InvoiceNo;
                db.Acc_GL.Add(GLParent);
                db.SaveChanges();
                foreach (var item in pi.items)
                {
                    //In Case of Service Item
                    if (item.isServiceItem)
                    {
                        var GLServSales = new Acc_GL()
                        {
                            TranId = GLParent.GlId, UserId = pi.empID, TaxPercent = item.tax, Quantity = item.qty, CoaId = item.coaID, CustId = pi.customerID, ActivityTimestamp = pi.saleDate, TranTypeId = transType, InvoiceNo = pi.invoiceNo, IsActive = true, UnitPrice = item.unitPrice, Credit = item.amount - item.tax, IsPostpaid = true, CreatedBy = editVal.createdBy, CreatedDate = editVal.createdDate, ModifiedBy = pi.empID, ModifiedDate = DateTime.Now, IsSalesCredit = _isSalesCredit, SalesPersonId = pi.salesPersonId
                        };
                        db.Acc_GL.Add(GLServSales);
                        db.SaveChanges();
                        continue;
                    }
                    var qty            = item.qty;
                    var purchaseAmount = new List <decimal>();
                    #region --- FIFO base deduction ---
                    List <Acc_GLTxLinks> txLinks = new List <Acc_GLTxLinks>();
                    var itemPurchases            = GLPurchases.Where(x => x.ItemId == item.itemID).ToList();
                    foreach (var objpurchase in itemPurchases)
                    {
                        var qtytodeduct = 0m;
                        var bal         = qty - objpurchase.QuantityBalance;
                        if (bal == 0 || bal < 0)
                        {
                            qtytodeduct = qty;
                        }
                        else
                        {
                            qtytodeduct = objpurchase.QuantityBalance ?? 0;
                        }
                        objpurchase.QuantityBalance -= qtytodeduct;
                        Acc_GLTxLinks objTxLinks = new Acc_GLTxLinks()
                        {
                            UnitPrice = item.unitPrice, Credit = item.amount, TranTypeID = transType, Quantity = qtytodeduct, IsActive = true, ItemID = item.itemID, RelGLID = objpurchase.GlId
                        };
                        txLinks.Add(objTxLinks);
                        db.Acc_GLTxLinks.Add(objTxLinks);
                        qty = qty - qtytodeduct;
                        purchaseAmount.Add(objpurchase.UnitPrice ?? 0);
                        if (qty == 0)
                        {
                            break;
                        }
                    }
                    decimal avgPurchaseUnitPrice = purchaseAmount.Average();
                    //Stock Entry
                    var GLStock = new Acc_GL()
                    {
                        TranId = GLParent.GlId, UserId = pi.empID, CoaId = 6, CustId = pi.customerID, ActivityTimestamp = pi.saleDate, TranTypeId = transType, InvoiceNo = pi.invoiceNo, IsActive = true, ItemId = item.itemID, Quantity = -item.qty, QuantityBalance = item.qty, UnitPrice = avgPurchaseUnitPrice, Credit = avgPurchaseUnitPrice * item.qty, TaxPercent = item.tax, CreatedBy = editVal.createdBy, CreatedDate = editVal.createdDate, ModifiedBy = pi.empID, ModifiedDate = DateTime.Now, IsSalesCredit = _isSalesCredit, SalesPersonId = pi.salesPersonId
                    };
                    db.Acc_GL.Add(GLStock);
                    //COGS Entry
                    var GLCOGS = new Acc_GL()
                    {
                        TranId = GLParent.GlId, UserId = pi.empID, CoaId = 13, CustId = pi.customerID, ActivityTimestamp = pi.saleDate, TranTypeId = transType, InvoiceNo = pi.invoiceNo, IsActive = true, ItemId = item.itemID, Quantity = item.qty, UnitPrice = avgPurchaseUnitPrice, Debit = avgPurchaseUnitPrice * item.qty, TaxPercent = item.tax, CreatedBy = editVal.createdBy, CreatedDate = editVal.createdDate, ModifiedBy = pi.empID, ModifiedDate = DateTime.Now, IsSalesCredit = _isSalesCredit, SalesPersonId = pi.salesPersonId
                    };
                    db.Acc_GL.Add(GLCOGS);
                    //Sales Entry
                    var GLSales = new Acc_GL()
                    {
                        TranId = GLParent.GlId, UserId = pi.empID, CoaId = 14, CustId = pi.customerID, ActivityTimestamp = pi.saleDate, TranTypeId = transType, InvoiceNo = pi.invoiceNo, IsActive = true, ItemId = item.itemID, Quantity = item.qty, UnitPrice = item.unitPrice, Credit = item.amount - item.tax, TaxPercent = item.tax, CreatedBy = editVal.createdBy, CreatedDate = editVal.createdDate, ModifiedBy = pi.empID, ModifiedDate = DateTime.Now, IsSalesCredit = _isSalesCredit, SalesPersonId = pi.salesPersonId
                    };
                    db.Acc_GL.Add(GLSales);

                    db.SaveChanges();
                    txLinks.ForEach(x => x.GLID = GLStock.GlId);
                    #endregion
                }
                if (pi.IsSalesCredit)
                {
                    pi.paid = 0;
                }
                if (pi.paid > 0)
                {
                    //Paid Entry
                    var GLpaid = new Acc_GL()
                    {
                        TranId = GLParent.GlId, UserId = pi.empID, CoaId = 11, CustId = pi.customerID, ActivityTimestamp = pi.saleDate, TranTypeId = transType, InvoiceNo = pi.invoiceNo, IsActive = true, Debit = pi.paid, CreatedBy = editVal.createdBy, CreatedDate = editVal.createdDate, ModifiedBy = pi.empID, ModifiedDate = DateTime.Now, IsSalesCredit = _isSalesCredit, SalesPersonId = pi.salesPersonId
                    };
                    db.Acc_GL.Add(GLpaid);
                }
                decimal balance = pi.netAmount - pi.paid;
                if (balance != 0)
                {
                    //Acc Receivable Entry
                    var GLpayable = new Acc_GL()
                    {
                        TranId = GLParent.GlId, UserId = pi.empID, CoaId = 10, CustId = pi.customerID, ActivityTimestamp = pi.saleDate, TranTypeId = transType, InvoiceNo = pi.invoiceNo, IsActive = true, Debit = balance, CreatedBy = editVal.createdBy, CreatedDate = editVal.createdDate, ModifiedBy = pi.empID, ModifiedDate = DateTime.Now, IsSalesCredit = _isSalesCredit, SalesPersonId = pi.salesPersonId
                    };
                    db.Acc_GL.Add(GLpayable);
                }
                decimal tax = pi.items.Sum(x => x.tax ?? 0);
                if (tax > 0)
                {
                    //Tax Entry
                    var GLTax = new Acc_GL()
                    {
                        TranId = GLParent.GlId, UserId = pi.empID, CoaId = 99, CustId = pi.customerID, ActivityTimestamp = pi.saleDate, TranTypeId = transType, InvoiceNo = pi.invoiceNo, IsActive = true, Credit = tax, CreatedBy = editVal.createdBy, CreatedDate = editVal.createdDate, ModifiedBy = pi.empID, ModifiedDate = DateTime.Now, IsSalesCredit = _isSalesCredit, SalesPersonId = pi.salesPersonId
                    };
                    db.Acc_GL.Add(GLTax);
                }
                db.SaveChanges();
                if (trans == null)
                {
                    transaction.Commit();
                    db.Dispose();
                    transaction.Dispose();
                }
                return(InvoiceNumber);
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                throw;
            }
        }