public HttpResponseMessage LockReceivingReceipt(Entities.TrnReceivingReceipt objReceivingReceipt, 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("ReceivingReceiptDetail")
                                    select d;

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

                            if (receivingReceipt.Any())
                            {
                                int countInvalidPO = 0;
                                var invalidPOs     = from d in db.TrnReceivingReceiptItems
                                                     where d.RRId == receivingReceipt.FirstOrDefault().Id &&
                                                     d.TrnPurchaseOrder.IsLocked == false &&
                                                     d.TrnPurchaseOrder.IsCancelled == true
                                                     select d;

                                if (invalidPOs.Any())
                                {
                                    countInvalidPO = invalidPOs.Count();
                                }

                                if (!receivingReceipt.FirstOrDefault().IsLocked&& countInvalidPO == 0)
                                {
                                    String oldObject = auditTrail.GetObjectString(receivingReceipt.FirstOrDefault());

                                    Decimal amount = 0, WTAXAmount = 0;
                                    var     receivingReceiptItems = from d in db.TrnReceivingReceiptItems where d.RRId == Convert.ToInt32(id) select d;
                                    if (receivingReceiptItems.Any())
                                    {
                                        amount = receivingReceiptItems.Sum(d => d.Amount); WTAXAmount = receivingReceiptItems.Sum(d => d.WTAXAmount);
                                    }

                                    var lockReceivingReceipt      = receivingReceipt.FirstOrDefault();
                                    lockReceivingReceipt.RRDate            = Convert.ToDateTime(objReceivingReceipt.RRDate);
                                    lockReceivingReceipt.DocumentReference = objReceivingReceipt.DocumentReference;
                                    lockReceivingReceipt.SupplierId        = objReceivingReceipt.SupplierId;
                                    lockReceivingReceipt.TermId            = objReceivingReceipt.TermId;
                                    lockReceivingReceipt.Remarks           = objReceivingReceipt.Remarks;
                                    lockReceivingReceipt.ManualRRNumber    = objReceivingReceipt.ManualRRNumber;
                                    lockReceivingReceipt.Amount            = amount;
                                    lockReceivingReceipt.WTaxAmount        = WTAXAmount;
                                    lockReceivingReceipt.ReceivedById      = objReceivingReceipt.ReceivedById;
                                    lockReceivingReceipt.CheckedById       = objReceivingReceipt.CheckedById;
                                    lockReceivingReceipt.ApprovedById      = objReceivingReceipt.ApprovedById;
                                    lockReceivingReceipt.Status            = objReceivingReceipt.Status;
                                    lockReceivingReceipt.IsLocked          = true;
                                    lockReceivingReceipt.UpdatedById       = currentUserId;
                                    lockReceivingReceipt.UpdatedDateTime   = DateTime.Now;
                                    db.SubmitChanges();

                                    if (lockReceivingReceipt.IsLocked)
                                    {
                                        UpdatePurchaseOrderStatus(Convert.ToInt32(id));

                                        accountsPayable.UpdateAccountsPayable(Convert.ToInt32(id));
                                        inventory.InsertReceivingReceiptInventory(Convert.ToInt32(id));
                                        journal.InsertReceivingReceiptJournal(Convert.ToInt32(id));
                                    }

                                    String newObject = auditTrail.GetObjectString(receivingReceipt.FirstOrDefault());
                                    auditTrail.InsertAuditTrail(currentUser.FirstOrDefault().Id, GetType().Name, MethodBase.GetCurrentMethod().Name, oldObject, newObject);

                                    return(Request.CreateResponse(HttpStatusCode.OK));
                                }
                                else
                                {
                                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Locking Error. These receiving receipt details are already locked or PO is invalid.."));
                                }
                            }
                            else
                            {
                                return(Request.CreateResponse(HttpStatusCode.NotFound, "Data not found. These receiving receipt details are not found in the server."));
                            }
                        }
                        else
                        {
                            return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no rights to lock receiving receipt."));
                        }
                    }
                    else
                    {
                        return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no access for this receiving receipt 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."));
            }
        }
Esempio n. 2
0
        public HttpResponseMessage LockReceivingReceipt(Entities.TrnReceivingReceipt objReceivingReceipt, 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("ReceivingReceiptDetail")
                                    select d;

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

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

                                    var disbursementLines = from d in db.TrnDisbursementLines
                                                            where d.RRId == Convert.ToInt32(id) &&
                                                            d.TrnDisbursement.IsLocked == true
                                                            select d;

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

                                    var journalVoucherLines = from d in db.TrnJournalVoucherLines
                                                              where d.APRRId == Convert.ToInt32(id) &&
                                                              d.TrnJournalVoucher.IsLocked == true
                                                              select d;

                                    if (journalVoucherLines.Any())
                                    {
                                        Decimal debitAmount  = journalVoucherLines.Sum(d => d.DebitAmount);
                                        Decimal creditAmount = journalVoucherLines.Sum(d => d.CreditAmount);

                                        adjustmentAmount = creditAmount - debitAmount;
                                    }

                                    var lockReceivingReceipt = receivingReceipt.FirstOrDefault();
                                    lockReceivingReceipt.RRDate            = Convert.ToDateTime(objReceivingReceipt.RRDate);
                                    lockReceivingReceipt.DocumentReference = objReceivingReceipt.DocumentReference;
                                    lockReceivingReceipt.SupplierId        = objReceivingReceipt.SupplierId;
                                    lockReceivingReceipt.TermId            = objReceivingReceipt.TermId;
                                    lockReceivingReceipt.Remarks           = objReceivingReceipt.Remarks;
                                    lockReceivingReceipt.ManualRRNumber    = objReceivingReceipt.ManualRRNumber;
                                    lockReceivingReceipt.Amount            = GetReceivingReceiptAmount(Convert.ToInt32(id));
                                    lockReceivingReceipt.WTaxAmount        = 0;
                                    lockReceivingReceipt.PaidAmount        = paidAmount;
                                    lockReceivingReceipt.AdjustmentAmount  = adjustmentAmount;
                                    lockReceivingReceipt.BalanceAmount     = (GetReceivingReceiptAmount(Convert.ToInt32(id)) - paidAmount) + adjustmentAmount;
                                    lockReceivingReceipt.ReceivedById      = objReceivingReceipt.ReceivedById;
                                    lockReceivingReceipt.CheckedById       = objReceivingReceipt.CheckedById;
                                    lockReceivingReceipt.ApprovedById      = objReceivingReceipt.ApprovedById;
                                    lockReceivingReceipt.IsLocked          = true;
                                    lockReceivingReceipt.UpdatedById       = currentUserId;
                                    lockReceivingReceipt.UpdatedDateTime   = DateTime.Now;

                                    db.SubmitChanges();

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

                                    if (lockReceivingReceipt.IsLocked)
                                    {
                                        inventory.InsertReceivingReceiptInventory(Convert.ToInt32(id));
                                        journal.InsertReceivingReceiptJournal(Convert.ToInt32(id));
                                    }

                                    return(Request.CreateResponse(HttpStatusCode.OK));
                                }
                                else
                                {
                                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Locking Error. These receiving receipt details are already locked."));
                                }
                            }
                            else
                            {
                                return(Request.CreateResponse(HttpStatusCode.NotFound, "Data not found. These receiving receipt details are not found in the server."));
                            }
                        }
                        else
                        {
                            return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no rights to lock receiving receipt."));
                        }
                    }
                    else
                    {
                        return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no access for this receiving receipt 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 SaveReceivingReceipt(Entities.TrnReceivingReceipt objReceivingReceipt, 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 receivingReceipt = from d in db.TrnReceivingReceipts where d.Id == Convert.ToInt32(id) select d;
                    if (receivingReceipt.Any())
                    {
                        if (!receivingReceipt.FirstOrDefault().IsLocked)
                        {
                            String oldObject = auditTrail.GetObjectString(receivingReceipt.FirstOrDefault());

                            Decimal amount = 0, WTAXAmount = 0;
                            var     receivingReceiptItems = from d in db.TrnReceivingReceiptItems where d.RRId == Convert.ToInt32(id) select d;
                            if (receivingReceiptItems.Any())
                            {
                                amount = receivingReceiptItems.Sum(d => d.Amount); WTAXAmount = receivingReceiptItems.Sum(d => d.WTAXAmount);
                            }

                            var saveReceivingReceipt      = receivingReceipt.FirstOrDefault();
                            saveReceivingReceipt.RRDate            = Convert.ToDateTime(objReceivingReceipt.RRDate);
                            saveReceivingReceipt.DocumentReference = objReceivingReceipt.DocumentReference;
                            saveReceivingReceipt.SupplierId        = objReceivingReceipt.SupplierId;
                            saveReceivingReceipt.TermId            = objReceivingReceipt.TermId;
                            saveReceivingReceipt.Remarks           = objReceivingReceipt.Remarks;
                            saveReceivingReceipt.ManualRRNumber    = objReceivingReceipt.ManualRRNumber;
                            saveReceivingReceipt.Amount            = amount;
                            saveReceivingReceipt.WTaxAmount        = WTAXAmount;
                            saveReceivingReceipt.ReceivedById      = objReceivingReceipt.ReceivedById;
                            saveReceivingReceipt.CheckedById       = objReceivingReceipt.CheckedById;
                            saveReceivingReceipt.ApprovedById      = objReceivingReceipt.ApprovedById;
                            saveReceivingReceipt.Status            = objReceivingReceipt.Status;
                            saveReceivingReceipt.UpdatedById       = currentUserId;
                            saveReceivingReceipt.UpdatedDateTime   = DateTime.Now;
                            db.SubmitChanges();

                            String newObject = auditTrail.GetObjectString(receivingReceipt.FirstOrDefault());
                            auditTrail.InsertAuditTrail(currentUser.FirstOrDefault().Id, GetType().Name, MethodBase.GetCurrentMethod().Name, oldObject, newObject);

                            return(Request.CreateResponse(HttpStatusCode.OK));
                        }
                        else
                        {
                            return(Request.CreateResponse(HttpStatusCode.BadRequest, "Saving Error. These receiving receipt details are already locked."));
                        }
                    }
                    else
                    {
                        return(Request.CreateResponse(HttpStatusCode.NotFound, "Data not found. These receiving receipt details are not found in the server."));
                    }
                }
                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."));
            }
        }