Example #1
0
        public HttpResponseMessage LockSalesInvoice(Entities.TrnSalesInvoice objSalesInvoice, String id)
        {
            try
            {
                var currentUser = from d in db.MstUsers
                                  where d.UserId == User.Identity.GetUserId()
                                  select d;

                if (currentUser.Any())
                {
                    var currentUserId = currentUser.FirstOrDefault().Id;

                    var userForms = from d in db.MstUserForms
                                    where d.UserId == currentUserId &&
                                    d.SysForm.FormName.Equals("SalesInvoiceDetail")
                                    select d;

                    if (userForms.Any())
                    {
                        if (userForms.FirstOrDefault().CanLock)
                        {
                            var salesInvoice = from d in db.TrnSalesInvoices
                                               where d.Id == Convert.ToInt32(id)
                                               select d;

                            if (salesInvoice.Any())
                            {
                                if (!salesInvoice.FirstOrDefault().IsLocked)
                                {
                                    Decimal paidAmount = 0;

                                    var collectionLines = from d in db.TrnCollectionLines
                                                          where d.SIId == Convert.ToInt32(id) &&
                                                          d.TrnCollection.IsLocked == true
                                                          select d;

                                    if (collectionLines.Any())
                                    {
                                        paidAmount = collectionLines.Sum(d => d.Amount);
                                    }

                                    var lockSalesInvoice = salesInvoice.FirstOrDefault();
                                    lockSalesInvoice.SIDate            = Convert.ToDateTime(objSalesInvoice.SIDate);
                                    lockSalesInvoice.CustomerId        = objSalesInvoice.CustomerId;
                                    lockSalesInvoice.TermId            = objSalesInvoice.TermId;
                                    lockSalesInvoice.DocumentReference = objSalesInvoice.DocumentReference;
                                    lockSalesInvoice.ManualSINumber    = objSalesInvoice.ManualSINumber;
                                    lockSalesInvoice.Remarks           = objSalesInvoice.Remarks;
                                    lockSalesInvoice.Amount            = GetSalesInvoiceAmount(Convert.ToInt32(id));
                                    lockSalesInvoice.PaidAmount        = paidAmount;
                                    lockSalesInvoice.AdjustmentAmount  = 0;
                                    lockSalesInvoice.BalanceAmount     = GetSalesInvoiceAmount(Convert.ToInt32(id)) - paidAmount;
                                    lockSalesInvoice.SoldById          = objSalesInvoice.SoldById;
                                    lockSalesInvoice.CheckedById       = objSalesInvoice.CheckedById;
                                    lockSalesInvoice.ApprovedById      = objSalesInvoice.ApprovedById;
                                    lockSalesInvoice.IsLocked          = true;
                                    lockSalesInvoice.UpdatedById       = currentUserId;
                                    lockSalesInvoice.UpdatedDateTime   = DateTime.Now;

                                    db.SubmitChanges();

                                    // =====================
                                    // Inventory and Journal
                                    // =====================
                                    Business.Inventory inventory = new Business.Inventory();
                                    Business.Journal   journal   = new Business.Journal();

                                    if (lockSalesInvoice.IsLocked)
                                    {
                                        inventory.InsertSIInventory(Convert.ToInt32(id));
                                        journal.insertSIJournal(Convert.ToInt32(id));
                                    }

                                    // ============================
                                    // Check for Negative Inventory
                                    // ============================
                                    Boolean foundNegativeQuantity = false;
                                    if (salesInvoice.FirstOrDefault().TrnSalesInvoiceItems.Any())
                                    {
                                        foreach (var salesInvoiceItem in salesInvoice.FirstOrDefault().TrnSalesInvoiceItems)
                                        {
                                            if (salesInvoiceItem.MstArticle.IsInventory)
                                            {
                                                var mstArticleInventory = from d in db.MstArticleInventories
                                                                          where d.TrnSalesInvoiceItems.Contains(salesInvoiceItem)
                                                                          select d;

                                                if (mstArticleInventory.Any())
                                                {
                                                    if (salesInvoiceItem.MstArticleInventory.Quantity < 0)
                                                    {
                                                        foundNegativeQuantity = true;
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    }

                                    if (foundNegativeQuantity)
                                    {
                                        inventory.deleteSIInventory(Convert.ToInt32(id));
                                        journal.deleteSIJournal(Convert.ToInt32(id));

                                        lockSalesInvoice.IsLocked = false;
                                        db.SubmitChanges();

                                        return(Request.CreateResponse(HttpStatusCode.BadRequest, "Negative Inventory Found!"));
                                    }
                                    else
                                    {
                                        return(Request.CreateResponse(HttpStatusCode.OK));
                                    }
                                }
                                else
                                {
                                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Locking Error. These sales invoice details are already locked."));
                                }
                            }
                            else
                            {
                                return(Request.CreateResponse(HttpStatusCode.NotFound, "Data not found. These sales invoice details are not found in the server."));
                            }
                        }
                        else
                        {
                            return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no rights to lock sales invoice."));
                        }
                    }
                    else
                    {
                        return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no access for this sales invoice page."));
                    }
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Theres no current user logged in."));
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, "Something's went wrong from the server."));
            }
        }
Example #2
0
        public HttpResponseMessage UnlockSalesInvoice(String id)
        {
            try
            {
                var currentUser = from d in db.MstUsers
                                  where d.UserId == User.Identity.GetUserId()
                                  select d;

                if (currentUser.Any())
                {
                    var currentUserId = currentUser.FirstOrDefault().Id;

                    var userForms = from d in db.MstUserForms
                                    where d.UserId == currentUserId &&
                                    d.SysForm.FormName.Equals("SalesInvoiceDetail")
                                    select d;

                    if (userForms.Any())
                    {
                        if (userForms.FirstOrDefault().CanUnlock)
                        {
                            var salesInvoice = from d in db.TrnSalesInvoices
                                               where d.Id == Convert.ToInt32(id)
                                               select d;

                            if (salesInvoice.Any())
                            {
                                if (salesInvoice.FirstOrDefault().IsLocked)
                                {
                                    var unlockSalesInvoice = salesInvoice.FirstOrDefault();
                                    unlockSalesInvoice.IsLocked        = false;
                                    unlockSalesInvoice.UpdatedById     = currentUserId;
                                    unlockSalesInvoice.UpdatedDateTime = DateTime.Now;

                                    db.SubmitChanges();

                                    // =====================
                                    // Inventory and Journal
                                    // =====================
                                    Business.Inventory inventory = new Business.Inventory();
                                    Business.Journal   journal   = new Business.Journal();

                                    if (!unlockSalesInvoice.IsLocked)
                                    {
                                        inventory.deleteSIInventory(Convert.ToInt32(id));
                                        journal.deleteSIJournal(Convert.ToInt32(id));
                                    }

                                    return(Request.CreateResponse(HttpStatusCode.OK));
                                }
                                else
                                {
                                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Unlocking Error. These sales invoice details are already unlocked."));
                                }
                            }
                            else
                            {
                                return(Request.CreateResponse(HttpStatusCode.NotFound, "Data not found. These sales invoice details are not found in the server."));
                            }
                        }
                        else
                        {
                            return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no rights to unlock sales invoice."));
                        }
                    }
                    else
                    {
                        return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no access for this sales invoice page."));
                    }
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Theres no current user logged in."));
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, "Something's went wrong from the server."));
            }
        }
        public HttpResponseMessage updateSalesInvoice(String id, Models.TrnSalesInvoice sales)
        {
            try
            {
                var userId        = (from d in db.MstUsers where d.UserId == User.Identity.GetUserId() select d.Id).SingleOrDefault();
                var salesInvoices = from d in db.TrnSalesInvoices where d.Id == Convert.ToInt32(id) select d;

                if (salesInvoices.Any())
                {
                    Decimal PaidAmount = 0;

                    var collectionLinesORId = from d in db.TrnCollectionLines where d.SIId == Convert.ToInt32(id) select d;
                    if (collectionLinesORId.Any())
                    {
                        Boolean collectionHeaderIsLocked = (from d in db.TrnCollections where d.Id == collectionLinesORId.First().ORId select d.IsLocked).SingleOrDefault();
                        var     collectionLines          = from d in db.TrnCollectionLines where d.SIId == Convert.ToInt32(id) select d;
                        if (collectionLines.Any())
                        {
                            if (collectionHeaderIsLocked == true)
                            {
                                PaidAmount = collectionLines.Sum(d => d.Amount);
                            }
                        }
                    }

                    var updateSalesInvoice = salesInvoices.FirstOrDefault();
                    updateSalesInvoice.BranchId          = sales.BranchId;
                    updateSalesInvoice.SINumber          = sales.SINumber;
                    updateSalesInvoice.SIDate            = Convert.ToDateTime(sales.SIDate);
                    updateSalesInvoice.CustomerId        = sales.CustomerId;
                    updateSalesInvoice.TermId            = sales.TermId;
                    updateSalesInvoice.DocumentReference = sales.DocumentReference;
                    updateSalesInvoice.ManualSINumber    = sales.ManualSINumber;
                    updateSalesInvoice.Remarks           = sales.Remarks;
                    updateSalesInvoice.Amount            = getAmountSalesInvoiceItem(Convert.ToInt32(id));
                    updateSalesInvoice.PaidAmount        = PaidAmount;
                    updateSalesInvoice.AdjustmentAmount  = 0;
                    updateSalesInvoice.BalanceAmount     = getAmountSalesInvoiceItem(Convert.ToInt32(id)) - PaidAmount;
                    updateSalesInvoice.SoldById          = sales.SoldById;
                    updateSalesInvoice.PreparedById      = sales.PreparedById;
                    updateSalesInvoice.CheckedById       = sales.CheckedById;
                    updateSalesInvoice.ApprovedById      = sales.ApprovedById;
                    updateSalesInvoice.IsLocked          = true;
                    updateSalesInvoice.UpdatedById       = userId;
                    updateSalesInvoice.UpdatedDateTime   = DateTime.Now;
                    db.SubmitChanges();

                    inventory.InsertSIInventory(Convert.ToInt32(id));
                    journal.insertSIJournal(Convert.ToInt32(id));

                    // Check for negative inventory
                    bool foundNegativeQuantity = false;
                    if (updateSalesInvoice.TrnSalesInvoiceItems.Any())
                    {
                        foreach (var salesInvoiceItem in updateSalesInvoice.TrnSalesInvoiceItems)
                        {
                            if (salesInvoiceItem.MstArticle.IsInventory)
                            {
                                var mstArticleInventory = from d in db.MstArticleInventories
                                                          where d.TrnSalesInvoiceItems.Contains(salesInvoiceItem)
                                                          select d;

                                if (mstArticleInventory.Any())
                                {
                                    if (salesInvoiceItem.MstArticleInventory.Quantity < 0)
                                    {
                                        foundNegativeQuantity = true;
                                        break;
                                    }
                                }
                            }
                        }
                    }

                    if (!foundNegativeQuantity)
                    {
                        return(Request.CreateResponse(HttpStatusCode.OK));
                    }
                    else
                    {
                        inventory.deleteSIInventory(Convert.ToInt32(id));
                        journal.deleteSIJournal(Convert.ToInt32(id));

                        updateSalesInvoice.IsLocked = false;
                        db.SubmitChanges();

                        return(Request.CreateResponse(HttpStatusCode.BadRequest, "Negative Inventory Found!"));
                    }
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound, "No Sales Invoice Found!"));
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return(Request.CreateResponse(HttpStatusCode.BadRequest, "Internal Server Error!"));
            }
        }