示例#1
0
        public Int32 insertBranch(Models.MstBranch branch)
        {
            try
            {
                var userId = (from d in db.MstUsers where d.UserId == User.Identity.GetUserId() select d.Id).SingleOrDefault();

                var codeDefault = "101";
                var lastBranch  = from d in db.MstBranches.OrderByDescending(d => d.Id) select d;
                if (lastBranch.Any())
                {
                    codeDefault = lastBranch.FirstOrDefault().BranchCode;
                    var lastBranchByCompanyId = from d in db.MstBranches.OrderByDescending(d => d.Id) where d.CompanyId == branch.CompanyId select d;
                    if (lastBranchByCompanyId.Any())
                    {
                        codeDefault = lastBranchByCompanyId.FirstOrDefault().BranchCode;
                        var CVNumber = Convert.ToInt32(lastBranchByCompanyId.FirstOrDefault().BranchCode) + 001;
                        codeDefault = zeroFill(CVNumber, 3);
                    }
                    else
                    {
                        var branchCodeIncrement = Convert.ToInt32(lastBranch.FirstOrDefault().BranchCode) + 100;
                        var branchCode          = Math.Round(Convert.ToDouble(branchCodeIncrement) / 100, 0) * 100;
                        var CVNumber            = branchCode + 001;
                        codeDefault = zeroFill(Convert.ToInt32(CVNumber), 3);
                    }
                }

                Data.MstBranch newBranch = new Data.MstBranch();
                newBranch.CompanyId       = branch.CompanyId;
                newBranch.Branch          = branch.Branch;
                newBranch.BranchCode      = codeDefault;
                newBranch.Address         = branch.Address;
                newBranch.ContactNumber   = branch.ContactNumber;
                newBranch.TaxNumber       = branch.TaxNumber;
                newBranch.IsLocked        = true;
                newBranch.CreatedById     = userId;
                newBranch.CreatedDateTime = DateTime.Now;
                newBranch.UpdatedById     = userId;
                newBranch.UpdatedDateTime = DateTime.Now;

                db.MstBranches.InsertOnSubmit(newBranch);
                db.SubmitChanges();

                return(newBranch.Id);
            }
            catch
            {
                return(0);
            }
        }
        public HttpResponseMessage AddBranch(Entities.MstBranch objBranch, String companyId)
        {
            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("CompanyDetail")
                                    select d;

                    if (userForms.Any())
                    {
                        if (userForms.FirstOrDefault().CanAdd)
                        {
                            var company = from d in db.MstCompanies
                                          where d.Id == Convert.ToInt32(companyId)
                                          select d;

                            if (company.Any())
                            {
                                if (!company.FirstOrDefault().IsLocked)
                                {
                                    var defaultBranchCode = "101";
                                    var lastBranch        = from d in db.MstBranches.OrderByDescending(d => d.Id)
                                                            select d;

                                    if (lastBranch.Any())
                                    {
                                        defaultBranchCode = lastBranch.FirstOrDefault().BranchCode;

                                        var lastBranchByCompany = from d in db.MstBranches.OrderByDescending(d => d.Id)
                                                                  where d.CompanyId == Convert.ToInt32(companyId)
                                                                  select d;

                                        if (lastBranchByCompany.Any())
                                        {
                                            defaultBranchCode = lastBranchByCompany.FirstOrDefault().BranchCode;

                                            var CVNumber = Convert.ToInt32(lastBranchByCompany.FirstOrDefault().BranchCode) + 001;
                                            defaultBranchCode = FillLeadingZeroes(CVNumber, 3);
                                        }
                                        else
                                        {
                                            var branchCodeIncrement = Convert.ToInt32(lastBranch.FirstOrDefault().BranchCode) + 100;

                                            var branchCode = Math.Round(Convert.ToDouble(branchCodeIncrement) / 100, 0) * 100;
                                            var CVNumber   = branchCode + 001;
                                            defaultBranchCode = FillLeadingZeroes(Convert.ToInt32(CVNumber), 3);
                                        }
                                    }

                                    Data.MstBranch newBranch = new Data.MstBranch
                                    {
                                        CompanyId       = Convert.ToInt32(companyId),
                                        BranchCode      = defaultBranchCode,
                                        Branch          = objBranch.Branch,
                                        Address         = objBranch.Address,
                                        ContactNumber   = objBranch.ContactNumber,
                                        TaxNumber       = objBranch.TaxNumber,
                                        IsLocked        = true,
                                        CreatedById     = currentUserId,
                                        CreatedDateTime = DateTime.Now,
                                        UpdatedById     = currentUserId,
                                        UpdatedDateTime = DateTime.Now
                                    };

                                    db.MstBranches.InsertOnSubmit(newBranch);
                                    db.SubmitChanges();

                                    String newObject = at.GetObjectString(newBranch);
                                    at.InsertAuditTrail(currentUser.FirstOrDefault().Id, GetType().Name, MethodBase.GetCurrentMethod().Name, "NA", newObject);

                                    return(Request.CreateResponse(HttpStatusCode.OK));
                                }
                                else
                                {
                                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "You cannot add new branch if the current company detail is locked."));
                                }
                            }
                            else
                            {
                                return(Request.CreateResponse(HttpStatusCode.NotFound, "These current company details are not found in the server. Please add new company first before proceeding."));
                            }
                        }
                        else
                        {
                            return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no rights to add branch."));
                        }
                    }
                    else
                    {
                        return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no access for this company 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 Int32 insertBranch(Models.MstBranch branch)
        {
            try
            {
                var userId = (from d in db.MstUsers where d.UserId == User.Identity.GetUserId() select d.Id).SingleOrDefault();

                Data.MstBranch newBranch = new Data.MstBranch();
                newBranch.CompanyId = branch.CompanyId;
                newBranch.Branch = branch.Branch;
                newBranch.BranchCode = branch.BranchCode;
                newBranch.Address = branch.Address;
                newBranch.ContactNumber = branch.ContactNumber;
                newBranch.TaxNumber = branch.TaxNumber;
                newBranch.IsLocked = true;
                newBranch.CreatedById = userId;
                newBranch.CreatedDateTime = DateTime.Now;
                newBranch.UpdatedById = userId;
                newBranch.UpdatedDateTime = DateTime.Now;

                db.MstBranches.InsertOnSubmit(newBranch);
                db.SubmitChanges();

                return newBranch.Id;
            }
            catch
            {
                return 0;
            }
        }