Example #1
0
        public HttpResponseMessage updateReceivingReceipt(String id, Models.TrnReceivingReceipt receivingReceipt)
        {
            try
            {
                var userId = (from d in db.MstUsers where d.UserId == User.Identity.GetUserId() select d.Id).SingleOrDefault();

                var receivingReceipts = from d in db.TrnReceivingReceipts where d.Id == Convert.ToInt32(id) select d;
                if (receivingReceipts.Any())
                {
                    //  get disbursement line for CVId
                    var disbursementLineCVId = from d in db.TrnDisbursementLines where d.RRId == Convert.ToInt32(id) select d;

                    Decimal PaidAmount = 0;

                    if (disbursementLineCVId.Any())
                    {
                        Boolean disbursementHeaderIsLocked = (from d in db.TrnDisbursements where d.Id == disbursementLineCVId.First().CVId select d.IsLocked).SingleOrDefault();

                        // get disbursement line for paid amaount
                        var disbursementLines = from d in db.TrnDisbursementLines where d.RRId == Convert.ToInt32(id) select d;
                        if (disbursementLines.Any())
                        {
                            if (disbursementHeaderIsLocked == true)
                            {
                                PaidAmount = disbursementLines.Sum(d => d.Amount);
                            }
                            else
                            {
                                PaidAmount = 0;
                            }
                        }
                        else
                        {
                            PaidAmount = 0;
                        }
                    }

                    var updatereceivingReceipt = receivingReceipts.FirstOrDefault();
                    updatereceivingReceipt.BranchId          = receivingReceipt.BranchId;
                    updatereceivingReceipt.RRDate            = Convert.ToDateTime(receivingReceipt.RRDate);
                    updatereceivingReceipt.RRNumber          = receivingReceipt.RRNumber;
                    updatereceivingReceipt.SupplierId        = receivingReceipt.SupplierId;
                    updatereceivingReceipt.TermId            = receivingReceipt.TermId;
                    updatereceivingReceipt.DocumentReference = receivingReceipt.DocumentReference;
                    updatereceivingReceipt.ManualRRNumber    = receivingReceipt.ManualRRNumber;
                    updatereceivingReceipt.Remarks           = receivingReceipt.Remarks;
                    updatereceivingReceipt.Amount            = getAmountReceivingReceiptItem(Convert.ToInt32(id));
                    updatereceivingReceipt.WTaxAmount        = 0;
                    updatereceivingReceipt.PaidAmount        = PaidAmount;
                    updatereceivingReceipt.AdjustmentAmount  = 0;
                    updatereceivingReceipt.BalanceAmount     = getAmountReceivingReceiptItem(Convert.ToInt32(id)) - PaidAmount;
                    updatereceivingReceipt.ReceivedById      = receivingReceipt.ReceivedById;
                    updatereceivingReceipt.PreparedById      = receivingReceipt.PreparedById;
                    updatereceivingReceipt.CheckedById       = receivingReceipt.CheckedById;
                    updatereceivingReceipt.ApprovedById      = receivingReceipt.ApprovedById;
                    updatereceivingReceipt.IsLocked          = true;
                    updatereceivingReceipt.UpdatedById       = userId;
                    updatereceivingReceipt.UpdatedDateTime   = DateTime.Now;

                    db.SubmitChanges();

                    if (updatereceivingReceipt.IsLocked == true)
                    {
                        inventory.InsertRRInventory(Convert.ToInt32(id));
                        journal.insertRRJournal(Convert.ToInt32(id));
                    }
                    else
                    {
                        inventory.deleteRRInventory(Convert.ToInt32(id));
                        journal.deleteRRJournal(Convert.ToInt32(id));
                    }

                    return(Request.CreateResponse(HttpStatusCode.OK));
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound));
                }
            }
            catch
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }
        }
        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;

                                    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 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  = 0;
                                    lockReceivingReceipt.BalanceAmount     = GetReceivingReceiptAmount(Convert.ToInt32(id)) - paidAmount;
                                    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.InsertRRInventory(Convert.ToInt32(id));
                                        journal.insertRRJournal(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."));
            }
        }