Esempio n. 1
0
        public static string EditPurchases(BO_PurchaseInvoice pi)
        {
            if (pi.items.GroupBy(x => x.itemID).Where(x => x.Count() > 1).Count() == 1)
            {
                return("bad request");
            }
            using (AprosysAccountingEntities db = new AprosysAccountingEntities())
            {
                using (var transaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        int transType     = (int)Constants.TransactionTypes.Purchase;
                        var glPurchaseOld = db.Acc_GL.Where(x => x.InvoiceNo == pi.invoiceNo && x.IsActive == true).ToList();
                        glPurchaseOld.Where(x => x.CoaId != 0).ToList().ForEach(x => x.IsActive = false);
                        int transID  = glPurchaseOld.Where(x => x.CoaId != 0).Select(x => x.TranId.Value).FirstOrDefault();
                        var glParent = glPurchaseOld.Find(x => x.CoaId == 0);
                        glParent.Credit            = glParent.Debit = pi.netAmount;
                        glParent.VendorId          = pi.vendorID;
                        glParent.ActivityTimestamp = pi.purchaseDate;
                        glParent.UserId            = pi.empID;
                        glParent.Comments          = pi.comments;
                        glParent.ModifiedDate      = DateTime.Now;
                        glParent.ModifiedBy        = pi.empID;

                        foreach (var item in pi.items)
                        {
                            var currItem = glPurchaseOld.Find(x => x.ItemId == item.itemID && x.CoaId == 6);
                            if (currItem == null)
                            {
                                //New Stock Entry
                                var GLStock = new Acc_GL()
                                {
                                    TranId = transID, UserId = pi.empID, CoaId = 6, VendorId = pi.vendorID, ActivityTimestamp = pi.purchaseDate, TranTypeId = transType, InvoiceNo = pi.invoiceNo, IsActive = true, ItemId = item.itemID, Quantity = item.qty, QuantityBalance = item.qty, UnitPrice = item.unitPrice, Debit = item.amount, CreatedBy = pi.empID, CreatedDate = DateTime.Now, ModifiedBy = pi.empID, ModifiedDate = DateTime.Now
                                };
                                db.Acc_GL.Add(GLStock);
                            }
                            else
                            {
                                if (currItem.Quantity == currItem.QuantityBalance)
                                {
                                    currItem.UnitPrice = item.unitPrice;
                                }
                                if (item.qty > currItem.Quantity)
                                {
                                    currItem.QuantityBalance = item.qty - (currItem.Quantity - currItem.QuantityBalance);
                                }
                                currItem.Debit             = item.amount;
                                currItem.Quantity          = item.qty;
                                currItem.UserId            = pi.empID;
                                currItem.ActivityTimestamp = pi.purchaseDate;
                                currItem.IsActive          = true;
                                currItem.VendorId          = pi.vendorID;
                                currItem.ModifiedDate      = DateTime.Now;
                                currItem.ModifiedBy        = pi.empID;
                            }
                        }
                        db.SaveChanges();
                        if (pi.paid > 0)
                        {
                            //Paid Entry
                            var objPaid = glPurchaseOld.Find(x => x.CoaId == 11);
                            if (objPaid == null)
                            {
                                db.Acc_GL.Add(new Acc_GL()
                                {
                                    TranId = transID, CoaId = 11, VendorId = pi.vendorID, ActivityTimestamp = pi.purchaseDate, TranTypeId = transType, Credit = pi.paid, InvoiceNo = pi.invoiceNo, IsActive = true, CreatedBy = pi.empID, CreatedDate = DateTime.Now, ModifiedBy = pi.empID, ModifiedDate = DateTime.Now
                                });
                            }
                            else
                            {
                                objPaid.Credit            = pi.paid;
                                objPaid.VendorId          = pi.vendorID;
                                objPaid.ActivityTimestamp = pi.purchaseDate;
                                objPaid.UserId            = pi.empID;
                                objPaid.IsActive          = true;
                                objPaid.ModifiedDate      = DateTime.Now;
                                objPaid.ModifiedBy        = pi.empID;
                            }
                            db.SaveChanges();
                        }
                        decimal balance = pi.netAmount - pi.paid;
                        if (balance != 0)
                        {
                            //Acc Payable Entry
                            var objPayable = glPurchaseOld.Find(x => x.CoaId == 12);
                            if (objPayable == null)
                            {
                                db.Acc_GL.Add(new Acc_GL()
                                {
                                    TranId = transID, CoaId = 12, VendorId = pi.vendorID, ActivityTimestamp = pi.purchaseDate, TranTypeId = transType, Credit = balance, InvoiceNo = pi.invoiceNo, IsActive = true, CreatedBy = pi.empID, CreatedDate = DateTime.Now, ModifiedBy = pi.empID, ModifiedDate = DateTime.Now
                                });
                            }
                            else
                            {
                                objPayable.Credit            = balance;
                                objPayable.VendorId          = pi.vendorID;
                                objPayable.ActivityTimestamp = pi.purchaseDate;
                                objPayable.UserId            = pi.empID;
                                objPayable.IsActive          = true;
                                objPayable.ModifiedDate      = DateTime.Now;
                                objPayable.ModifiedBy        = pi.empID;
                            }
                            db.SaveChanges();
                        }
                        db.SaveChanges();
                        transaction.Commit();
                        return("success");
                    }
                    catch (Exception)
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }
Esempio n. 2
0
 public static string SavePurchase(BO_PurchaseInvoice pi)
 {
     if (pi.items.GroupBy(x => x.itemID).Where(x => x.Count() > 1).Count() == 1)
     {
         return("bad request");
     }
     using (AprosysAccountingEntities db = new AprosysAccountingEntities())
     {
         using (var transaction = db.Database.BeginTransaction())
         {
             try
             {
                 int transType = (int)Constants.TransactionTypes.Purchase;
                 pi.invoiceNo = Util.GetNextVoucher(transType);
                 //Parent Entry
                 var GLParent = new Acc_GL()
                 {
                     CoaId = 0, UserId = pi.empID, Comments = pi.comments, VendorId = pi.vendorID, ActivityTimestamp = pi.purchaseDate, TranTypeId = transType, Debit = pi.netAmount, Credit = pi.netAmount, InvoiceNo = pi.invoiceNo, IsActive = true, CreatedBy = pi.empID, CreatedDate = DateTime.Now, ModifiedBy = pi.empID, ModifiedDate = DateTime.Now
                 };
                 db.Acc_GL.Add(GLParent);
                 db.SaveChanges();
                 foreach (var item in pi.items)
                 {
                     //Stock Entry
                     var GLStock = new Acc_GL()
                     {
                         TranId = GLParent.GlId, UserId = pi.empID, CoaId = 6, VendorId = pi.vendorID, ActivityTimestamp = pi.purchaseDate, TranTypeId = transType, InvoiceNo = pi.invoiceNo, IsActive = true, ItemId = item.itemID, Quantity = item.qty, QuantityBalance = item.qty, UnitPrice = item.unitPrice, Debit = item.amount, CreatedBy = pi.empID, CreatedDate = DateTime.Now, ModifiedBy = pi.empID, ModifiedDate = DateTime.Now
                     };
                     db.Acc_GL.Add(GLStock);
                 }
                 if (pi.paid > 0)
                 {
                     //Paid Entry
                     var GLpaid = new Acc_GL()
                     {
                         TranId = GLParent.GlId, UserId = pi.empID, CoaId = 11, VendorId = pi.vendorID, ActivityTimestamp = pi.purchaseDate, TranTypeId = transType, InvoiceNo = pi.invoiceNo, IsActive = true, Credit = pi.paid, CreatedBy = pi.empID, CreatedDate = DateTime.Now, ModifiedBy = pi.empID, ModifiedDate = DateTime.Now
                     };
                     db.Acc_GL.Add(GLpaid);
                 }
                 decimal balance = pi.netAmount - pi.paid;
                 if (balance != 0)
                 {
                     //Acc Payable Entry
                     var GLpayable = new Acc_GL()
                     {
                         TranId = GLParent.GlId, UserId = pi.empID, CoaId = 12, VendorId = pi.vendorID, ActivityTimestamp = pi.purchaseDate, TranTypeId = transType, InvoiceNo = pi.invoiceNo, IsActive = true, Credit = balance, CreatedBy = pi.empID, CreatedDate = DateTime.Now, ModifiedBy = pi.empID, ModifiedDate = DateTime.Now
                     };
                     db.Acc_GL.Add(GLpayable);
                 }
                 db.SaveChanges();
                 transaction.Commit();
                 return("success");
             }
             catch (Exception)
             {
                 transaction.Rollback();
                 throw;
             }
         }
     }
 }