public HttpResponseMessage SaveTrnCommissionRequest(TrnCommissionRequest commisionrequest)
        {
            try
            {
                var TrnCommissionRequestData = from d in db.TrnCommissionRequests
                                               where d.Id == Convert.ToInt32(commisionrequest.Id) select d;

                if (TrnCommissionRequestData.Any())
                {
                    if (TrnCommissionRequestData.FirstOrDefault().IsLocked == false)
                    {
                        var currentUser = from d in db.MstUsers
                                          where d.AspNetId == User.Identity.GetUserId()
                                          select d;

                        if (currentUser.Any())
                        {
                            var UpdateTrnCommissionRequestData = TrnCommissionRequestData.FirstOrDefault();

                            UpdateTrnCommissionRequestData.CommissionRequestNumber = commisionrequest.CommissionRequestNumber;
                            UpdateTrnCommissionRequestData.CommissionRequestDate   = Convert.ToDateTime(commisionrequest.CommissionRequestDate);
                            UpdateTrnCommissionRequestData.BrokerId         = commisionrequest.BrokerId;
                            UpdateTrnCommissionRequestData.SoldUnitId       = commisionrequest.SoldUnitId;
                            UpdateTrnCommissionRequestData.CommissionNumber = commisionrequest.CommissionNumber;
                            UpdateTrnCommissionRequestData.Amount           = commisionrequest.Amount;
                            UpdateTrnCommissionRequestData.Remarks          = commisionrequest.Remarks;
                            UpdateTrnCommissionRequestData.PreparedBy       = commisionrequest.PreparedBy;
                            UpdateTrnCommissionRequestData.CheckedBy        = commisionrequest.CheckedBy;
                            UpdateTrnCommissionRequestData.ApprovedBy       = commisionrequest.ApprovedBy;
                            UpdateTrnCommissionRequestData.Status           = commisionrequest.Status;
                            UpdateTrnCommissionRequestData.UpdatedBy        = currentUser.FirstOrDefault().Id;
                            UpdateTrnCommissionRequestData.UpdatedDateTime  = DateTime.Now;

                            db.SubmitChanges();

                            return(Request.CreateResponse(HttpStatusCode.OK));
                        }
                        else
                        {
                            return(Request.CreateResponse(HttpStatusCode.BadRequest));
                        }
                    }
                    else
                    {
                        return(Request.CreateResponse(HttpStatusCode.BadRequest));
                    }
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.OK));
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }
        }
        public HttpResponseMessage UnlockTrnCommissionRequest(TrnCommissionRequest commissionRequest)
        {
            try
            {
                var TrnCommissionRequestData = from d in db.TrnCommissionRequests
                                               where d.Id == Convert.ToInt32(commissionRequest.Id)
                                               select d;

                if (TrnCommissionRequestData.Any())
                {
                    var currentUser = from d in db.MstUsers
                                      where d.AspNetId == User.Identity.GetUserId()
                                      select d;

                    if (currentUser.Any())
                    {
                        var UnLockCommissionRequesData = TrnCommissionRequestData.FirstOrDefault();

                        UnLockCommissionRequesData.IsLocked        = false;
                        UnLockCommissionRequesData.UpdatedBy       = currentUser.FirstOrDefault().Id;
                        UnLockCommissionRequesData.UpdatedDateTime = DateTime.Now;

                        db.SubmitChanges();

                        return(Request.CreateResponse(HttpStatusCode.OK));
                    }
                    else
                    {
                        return(Request.CreateResponse(HttpStatusCode.BadRequest));
                    }
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.OK));
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }
        }
        public Int32 PostTrnCommissionRequest(TrnCommissionRequest commissionRequest)
        {
            try
            {
                var currentUser = from d in db.MstUsers
                                  where d.AspNetId == User.Identity.GetUserId()
                                  select d;

                if (currentUser.Any())
                {
                    string commissionRequestNumber = "0000000001";
                    var    commissionRequests      = from d in db.TrnCommissionRequests.OrderByDescending(d => d.Id) select d;
                    if (commissionRequests.Any())
                    {
                        Int32 nextCommissionRequestNumber = Convert.ToInt32(commissionRequests.FirstOrDefault().CommissionRequestNumber) + 1;
                        commissionRequestNumber = padNumWithZero(nextCommissionRequestNumber, 10);
                    }

                    Int32 soldUnitId = 0;
                    Int32 brokerId   = 0;

                    var soldUnits = from d in db.TrnSoldUnits where d.IsLocked == true select d;
                    if (soldUnits.Any())
                    {
                        var soldUnit = soldUnits.FirstOrDefault();

                        soldUnitId = soldUnit.Id;
                        brokerId   = soldUnit.BrokerId;
                    }

                    if (soldUnitId > 0 && brokerId > 0)
                    {
                        Data.TrnCommissionRequest newTrnCommissionRequest = new Data.TrnCommissionRequest()
                        {
                            CommissionRequestNumber = commissionRequestNumber,
                            CommissionRequestDate   = Convert.ToDateTime(commissionRequest.CommissionRequestDate),
                            BrokerId         = brokerId,
                            SoldUnitId       = soldUnitId,
                            CommissionNumber = commissionRequest.CommissionNumber,
                            Amount           = commissionRequest.Amount,
                            Remarks          = commissionRequest.Remarks,
                            PreparedBy       = currentUser.FirstOrDefault().Id,
                            CheckedBy        = currentUser.FirstOrDefault().Id,
                            ApprovedBy       = currentUser.FirstOrDefault().Id,
                            Status           = commissionRequest.Status,
                            IsLocked         = commissionRequest.IsLocked,
                            CreatedBy        = currentUser.FirstOrDefault().Id,
                            CreatedDateTime  = DateTime.Now,
                            UpdatedBy        = currentUser.FirstOrDefault().Id,
                            UpdatedDateTime  = DateTime.Now
                        };

                        db.TrnCommissionRequests.InsertOnSubmit(newTrnCommissionRequest);
                        db.SubmitChanges();

                        return(newTrnCommissionRequest.Id);
                    }
                    else
                    {
                        return(0);
                    }
                }
                else
                {
                    return(0);
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return(0);
            }
        }