Esempio n. 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 UnlockReceivingReceipt(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().CanUnlock)
                        {
                            var receivingReceipt = from d in db.TrnReceivingReceipts
                                                   where d.Id == Convert.ToInt32(id)
                                                   select d;

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

                                    db.SubmitChanges();

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

                                    if (!unlockReceivingReceipt.IsLocked)
                                    {
                                        inventory.deleteRRInventory(Convert.ToInt32(id));
                                        journal.deleteRRJournal(Convert.ToInt32(id));
                                    }

                                    return(Request.CreateResponse(HttpStatusCode.OK));
                                }
                                else
                                {
                                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Unlocking Error. These receiving receipt details are already unlocked."));
                                }
                            }
                            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 unlock 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."));
            }
        }