public async void PasswordTesting()
        {
            NHibernateUserStore store = new NHibernateUserStore();
            var usr = await store.FindByIdAsync("ef9826e1-86b8-41c4-8716-a9e60123db20");

            await store.SetPasswordAsync(usr, "test2");
        }
        // POST api/<controller>
        public async Task Post(PrLinesViewModel value)
        {
            var nh   = new NHibernateUserStore();
            var nhps = new NHibernatePRStore();
            var nhcs = new NHibernateNonCatalogStore();
            var user = nh.FindByStampAsync(value.SecurityStamp);

            if (user != null)
            {
                var       nhis = new NHibernateItemStore();
                ItemModel item = null;
                NonCatalogItemHeadModel nonCatalog = null;
                if (value.itemType == "Catalog")
                {
                    item = await nhis.FindItemByIdAsync(value.Id);
                }
                else
                {
                    nonCatalog = await nhcs.GetNonCatalogAsync(value.Id);
                }
                var PrLines = new PRLinesModel {
                    Item       = item,
                    NonCatalog = nonCatalog,
                    Quantity   = value.Quantity,
                    CreatedBy  = user.Result
                };
                try {
                    await nhps.CreatePRLinesAsync(PrLines);
                } catch (Exception e) {
                    var a = e.Message;
                }
            }
        }
Пример #3
0
        // POST api/<controller>
        public async Task <string> Post(LoginModel Login)
        {
            NHibernateUserStore store = new NHibernateUserStore();

            try {
                var usr = await store.FindByNamePassAsync(Login.Username, Login.Password);

                //var usr = await store.FindByIdAsync("564267a0-ac19-4811-871c-a9d9011bdfe6");
                if (usr != null)
                {
                    var session = HttpContext.Current.Session;
                    session["UserId"] = usr.Id;
                    usr = await HibernateSession.SignInUser(usr, Login.RememberMe);

                    return("Ok");
                }
                else
                {
                    return("Incorrect User or Password");
                }
#pragma warning disable CS0168 // The variable 'e' is declared but never used
            } catch (Exception e) {
#pragma warning restore CS0168 // The variable 'e' is declared but never used
                return(null);
            }
        }
Пример #4
0
        public async Task <ActionResult> POApprover()
        {
            var nh       = new NHibernateUserStore();
            var response = new HttpResponseMessage(HttpStatusCode.OK);

            var user = (UserModel)HttpContext.Session[Config.GetAppSetting("SessionKey")];

            //UserModel user = null;
            if (user != null)
            {
                //user = (UserModel)task.GetType().GetProperty("Result").GetValue(task);
            }
            else if (CurrentUserSession.userSecurityStampCookie != null)
            {
                user = await nh.FindByStampAsync(CurrentUserSession.userSecurityStampCookie);

                HttpContext.Session[Config.GetAppSetting("SessionKey")] = user;
                var owinAuthentication = new OwinAuthenticationService(HttpContext);
                owinAuthentication.SignIn(user);
            }
            else
            {
                return(RedirectToAction("Login", "User"));
            }
            var nhps = new NHibernatePOStore();
            var pr   = await nhps.FindPOApprovalAsync(user.employee.position);

            return(View(pr));
        }
Пример #5
0
        public async void PasswordTesting()
        {
            NHibernateUserStore store = new NHibernateUserStore();
            var usr = await store.FindByNameAsync("*****@*****.**");

            await store.SetPasswordAsync(usr, "test2");
        }
        // POST api/<controller>
        public async Task <string> Post(RegistrationView value)
        {
            var result = "Success";
            NHibernateCompanyStore store = new NHibernateCompanyStore();
            NHibernateUserStore    user  = new NHibernateUserStore();
            PasswordHasher         ph    = new PasswordHasher();
            var passHash = ph.HashPassword(value.password);
            var emp      = new EmployeeModel()
            {
                FirstName  = value.FirstName,
                LastName   = value.LastName,
                Email      = value.Email,
                EmpNo      = value.EmpNo,
                Contact    = value.Contact,
                Gender     = value.Gender,
                Company    = await store.GetCompanyByIdAsync(value.CompanyId),
                Team       = await store.GetTeamByIdAsync(value.TeamId),
                position   = await store.GetPositionByIdAsync(value.Job_Id),
                Department = await store.GetDepartmentByIdAsync(value.Dept_Id)
            };
            //var employee=await store.RegisterEmployeeAsync(emp);
            var usr = new UserModel()
            {
                UserName     = value.Email,
                FirstName    = value.FirstName,
                LastName     = value.LastName,
                PasswordHash = passHash,
                employee     = emp
            };
            await user.CreateAsync(usr);

            return(result);
        }
Пример #7
0
        // GET: Confirmation
        public async Task <ActionResult> DriverConfirmation()
        {
            string id = null;

            var       nhs = new NHibernateUserStore();
            UserModel usr = null;

            if (HttpContext.Session["UserId"] != null)
            {
                id = HttpContext.Session["UserId"].ToString();
            }
            if (id == null)
            {
                usr = await nhs.FindByStampAsync(CurrentUserSession.userSecurityStampCookie);
            }
            else
            {
                usr = await nhs.FindByIdAsync(id);
            }
            ViewData["ApiServer"] = Config.GetApiServerURL();


            if (usr == null || !usr.IsAdmin)
            {
                RedirectToAction("Login", "Logins");
            }
            var hds     = new NHibernateDriverStore();
            var drivers = await hds.GetAllForValidationAsync();

            return(View(drivers));
        }
Пример #8
0
        public async Task <UserModel> Post(LoginModel login)
        {
            NHibernateUserStore store = new NHibernateUserStore();

            var usr = await store.FindByNamePassAsync(login.Username, login.Password);

            return(usr);
        }
        // POST api/<controller>
        public async Task <IList <PRApprovalViewModel> > Post(IList <PRApprovalViewModel> value)
        {
            var nh   = new NHibernateUserStore();
            var nhps = new NHibernatePRStore();
            List <PRApprovalViewModel> PRs = new List <PRApprovalViewModel>();



            foreach (var approved in value)
            {
                var user = nh.FindByStampAsync(approved.SecurityStamp);
                if (user != null)
                {
                    var approval = await nhps.FindPRAprovalAsync(approved.Id);

                    approval.Status = approved.Status;
                    switch (approval.Status)
                    {
                    case StatusType.Approved:
                        var PRApprovals = await nhps.FindPRAprovalAsync(approval.PRHeader);

                        approval.ApprovedBy   = user.Result;
                        approval.DateApproved = DateTime.UtcNow;
                        foreach (var PRApproval in PRApprovals)
                        {
                            if (PRApproval.Status == StatusType.Approved)
                            {
                                approval.PRHeader.Status = StatusType.Approved;
                            }
                            else
                            {
                                approval.PRHeader.Status = PRApproval.Status;
                                break;
                            }
                        }
                        break;

                    case StatusType.Rejected:
                        approval.PRHeader.Status = approval.Status;
                        break;

                    default:
                        break;
                    }
                    try {
                        await nhps.SaveOrUpdatePRApprovalAsync(approval);
                    } catch (Exception e) {
                    }
                    approved.RequisitionNo = approval.PRHeader.RequisitionNo;
                    PRs.Add(approved);
                }
            }


            return(PRs);
        }
 // POST api/<controller>
 public async Task Post(ChargeLocationViewModel value)
 {
     var nhus           = new NHibernateUserStore();
     var nhcs           = new NHibernateCompanyStore();
     var chargeLocation = new ChargeLocationModel {
         ChargeLocationCode = value.ChargeLocationCode,
         ChargeLocationName = value.ChargeLocationName,
         CreatedBy          = await nhus.FindByStampAsync(value.SecurityStamp)
     };
     await nhcs.AddChargeLocationAsync(chargeLocation);
 }
 // POST api/<controller>
 public async Task Post(CRCViewModel value)
 {
     var nhus = new NHibernateUserStore();
     var nhcs = new NHibernateCompanyStore();
     var CRC  = new CostRevenueCenterModel {
         CRCCode   = value.CRCCode,
         CRCName   = value.CRCName,
         CreatedBy = await nhus.FindByStampAsync(value.SecurityStamp)
     };
     await nhcs.AddCRCAsync(CRC);
 }
        public async void createUser()
        {
            NHibernateUserStore store = new NHibernateUserStore();
            var usr = new UserModel()
            {
                FirstName = "cons",
                LastName  = "mname",
                UserName  = "******"
            };

            CreateUser(usr);
            await store.SetPasswordAsync(usr, "test2");
        }
Пример #13
0
        public async Task <bool> RegisterUser(UserModel user)
        {
            NHibernateUserStore store = new NHibernateUserStore();
            var usr = await store.FindByNameAsync(user.UserName);

            if (usr == null)
            {
                await store.CreateAsync(user);

                return(true);
            }
            return(false);
        }
        // POST api/<controller>
        public async Task Post(IList <PrLinesViewModel> value)
        {
            var nh   = new NHibernateUserStore();
            var nhps = new NHibernatePRStore();

            foreach (var line in value)
            {
                var user = nh.FindByStampAsync(line.SecurityStamp);
                if (user != null)
                {
                    await nhps.DeletePRLineAsync(line.Id);
                }
            }
        }
        // POST api/<controller>
        public async Task <long> Post(POViewModel value)
        {
            const long PurchaserHead = 6;
            var        nhpos         = new NHibernatePOStore();
            var        nhus          = new NHibernateUserStore();
            var        company       = new NHibernateCompanyStore();
            var        po            = await nhpos.FindPOAByIdAsync(value.Id);

            var user = await nhus.FindByStampAsync(value.SecurityStamp);

            foreach (var line in value.Lines)
            {
                var nhps   = new NHibernatePRStore();
                var poLine = new POLinesModel()
                {
                    Quantity    = line.Quantity,
                    UOM         = line.UOM,
                    Description = line.Description,
                    UnitPrice   = line.UnitPrice,
                    PRLine      = await nhps.GetPRLineAsync(line.PRLineId),
                    Name        = line.Name,
                    CreatedBy   = user
                };
                po.Lines.Add(poLine);
            }
            po.Status = value.Status;
            if (value.Status == StatusType.ForApproval)
            {
                var approver = new POAprovalModel()
                {
                    Approver = await company.GetPositionByIdAsync(PurchaserHead),
                    Status   = StatusType.ForApproval
                };
                po.Approvals = new List <POAprovalModel>();
                po.Approvals.Add(approver);
                var costApprover = company.FindCostApprover(po.Amount);
                if (costApprover.Result != null)
                {
                    var approver2 = new POAprovalModel()
                    {
                        Approver = costApprover.Result.Approver,
                        Status   = StatusType.ForApproval
                    };
                    po.Approvals.Add(approver2);
                }
            }
            await nhpos.SaveOrUpdatePOHeaderAsync(po);

            return(value.Id);
        }
Пример #16
0
        // POST api/<controller>
        public async Task <string> Post(RegistrationSatusModel value)
        {
            string    id     = null;
            string    result = "Ok";
            var       nus    = new NHibernateUserStore();
            UserModel user   = null;

            if (HttpContext.Current.Session["UserId"] != null)
            {
                id = HttpContext.Current.Session["UserId"].ToString();
            }
            if (id == null)
            {
                user = await nus.FindByStampAsync(CurrentUserSession.userSecurityStampCookie);
            }
            else
            {
                user = await nus.FindByIdAsync(id);
            }

            var nds = new NHibernateDriverStore();

            var driver       = nds.FindByIdAsync(value.Id).Result;
            var existingUser = await nus.FindByNameAsync(driver.Email);

            driver.Status    = RegistrationStatus.Accepted;
            driver.UpdatedBy = user;
            if (existingUser != null)
            {
                existingUser.Driver = driver;
                await nus.UpdateAsync(existingUser);
            }
            else
            {
                var usr = new UserModel {
                    UserName      = driver.Email,
                    FirstName     = driver.FirstName,
                    LastName      = driver.LastName,
                    PasswordHash  = driver.Password,
                    SecurityStamp = Guid.NewGuid().ToString(),
                    Driver        = driver
                };
                await nus.CreateAsync(usr);
            }
            var emails = driver.Email;
            await Emailer.SendMessage(driver.Email + " Activated", emails, "Registration");

            return(result);
        }
        // POST api/<controller>
        public async Task <string> Post(PassengerModel value)
        {
            string         result    = "Ok";
            PasswordHasher ph        = new PasswordHasher();
            var            passHash  = ph.HashPassword(value.Password);
            var            hps       = new NHibernatePassengerStore();
            var            passenger = new PassengerModel {
                Email        = value.Email,
                FirstName    = value.FirstName,
                LastName     = value.LastName,
                MiddleName   = value.MiddleName,
                BirthDate    = value.BirthDate,
                Gender       = value.Gender,
                Town         = value.Town,
                Province     = value.Province,
                City         = value.City,
                MobileNumber = value.MobileNumber,
                Password     = passHash
            };
            var nhus         = new NHibernateUserStore();
            var existingUser = await nhus.FindByNameAsync(passenger.Email);

            try {
                if (existingUser != null)
                {
                    existingUser.Passenger = passenger;
                    await nhus.UpdateAsync(existingUser);
                }
                else
                {
                    var usr = new UserModel {
                        UserName      = passenger.Email,
                        FirstName     = passenger.FirstName,
                        LastName      = passenger.LastName,
                        PasswordHash  = passenger.Password,
                        SecurityStamp = Guid.NewGuid().ToString(),
                        Passenger     = passenger
                    };
                    await nhus.CreateAsync(usr);
                }
            } catch (Exception e) {
                result = e.Message;
            }
            return(result);
        }
Пример #18
0
 // POST api/<controller>
 public async Task Post(CompanyViewModel value)
 {
     NHibernateCompanyStore nhcs = new NHibernateCompanyStore();
     var nhus    = new NHibernateUserStore();
     var company = new CompanyModel()
     {
         CompanyCode = value.CompanyCode,
         CompanyName = value.CompanyName,
         Phone       = value.Phone,
         Address     = value.Address,
         Email       = value.Email,
         Logo        = value.Logo,
         SmallLogo   = value.SmallLogo,
         Tin         = value.Tin,
         CreatedBy   = await nhus.FindByStampAsync(value.SecurityStamp)
     };
     await nhcs.SaveOrUpdateCompany(company);
 }
Пример #19
0
        public static async void SignInUser(UserModel user, bool remeberMe)
        {
            //CurrentUserSession.userSession = user.Id;

            if (remeberMe)
            {
                if (user.SecurityStamp == null)
                {
                    user.SecurityStamp = Guid.NewGuid().ToString();
                    NHibernateUserStore hs = new NHibernateUserStore();
                    await hs.UpdateAsync(user);
                }
                CurrentUserSession.userSecurityStampCookie = user.SecurityStamp;
            }
            else
            {
                CurrentUserSession.removeSecurityStampCookie();
            }
        }
Пример #20
0
        // GET: PR
        public async Task <ActionResult> PRShop(string itemType = "Catalog")
        {
            NHibernateUserStore nh = new NHibernateUserStore();

            ViewData["ApiServer"] = Config.GetApiServerURL();

            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);

            //var task =  (Task)HttpContext.Session[Config.GetAppSetting("SessionKey")];
            var user = (UserModel)HttpContext.Session[Config.GetAppSetting("SessionKey")];

            //UserModel user=null;
            if (user != null)
            {
                //user =(UserModel) task.GetType().GetProperty("Result").GetValue(task);
            }
            else if (CurrentUserSession.userSecurityStampCookie != null)
            {
                user = await nh.FindByStampAsync(CurrentUserSession.userSecurityStampCookie);

                HttpContext.Session[Config.GetAppSetting("SessionKey")] = user;

                var owinAuthentication = new OwinAuthenticationService(HttpContext);
                owinAuthentication.SignIn(user);
            }
            else
            {
                return(RedirectToAction("Login", "User"));
            }
            var nhps  = new NHibernatePRStore();
            var lines = await nhps.PRLinesCreatedAsync(user);

            ViewData["cartCount"] = lines.Count;

            ViewData["itemType"]      = itemType;
            ViewData["ItemImagePath"] = Config.GetAppSetting("ItemImagePath");

            return(View(user));
        }
Пример #21
0
        public async Task <ActionResult> CreatePR(IList <PrLinesViewModel> value)
        {
            List <PRLinesModel> lines = new List <PRLinesModel>();
            var nhps = new NHibernatePRStore();
            var nh   = new NHibernateUserStore();
            var nhcs = new NHibernateCompanyStore();
            var user = (UserModel)HttpContext.Session[Config.GetAppSetting("SessionKey")];

            //UserModel user = null;
            if (user != null)
            {
                //user = (UserModel)task.GetType().GetProperty("Result").GetValue(task);
            }
            else if (CurrentUserSession.userSecurityStampCookie != null)
            {
                user = await nh.FindByStampAsync(CurrentUserSession.userSecurityStampCookie);

                HttpContext.Session[Config.GetAppSetting("SessionKey")] = user;
                var owinAuthentication = new OwinAuthenticationService(HttpContext);
                owinAuthentication.SignIn(user);
            }
            else
            {
                return(RedirectToAction("Login", "User"));
            }
            ViewData["SmallLogo"] = Config.GetCompanyLogo(user.employee.Company.SmallLogo);
            ViewData["Employee"]  = user.employee;
            ViewData["Section"]   = await nhcs.TeamEmployeeAsync(user.employee.Team);

            foreach (var item in value)
            {
                var line = await nhps.GetPRLineAsync(item.Id);

                lines.Add(line);
            }
            return(PartialView(lines));
        }
Пример #22
0
        // POST api/<controller>
        public async Task <string> Post(DriverModel value)
        {
            string                result   = "Ok";
            PasswordHasher        ph       = new PasswordHasher();
            var                   passHash = ph.HashPassword(value.Password);
            NHibernateDriverStore hds      = new NHibernateDriverStore();
            var                   driver   = new DriverModel {
                Email         = value.Email,
                FirstName     = value.FirstName,
                LastName      = value.LastName,
                MiddleName    = value.MiddleName,
                BirthDate     = value.BirthDate,
                Gender        = value.Gender,
                HomeAddress   = value.HomeAddress,
                Town          = value.Town,
                District      = value.District,
                Province      = value.Province,
                City          = value.City,
                MobileNumber  = value.MobileNumber,
                LicenseNumber = value.LicenseNumber,
                Password      = passHash,
                Status        = RegistrationStatus.ForValidation
            };

            try {
                await hds.CreateDriverAsync(driver);

                var nhus   = new NHibernateUserStore();
                var emails = await nhus.GetAllAdminEmailAsync();

                await Emailer.SendMessage(driver.Email + " For Activation", emails, "Registration");
            } catch (Exception e) {
                result = e.Message;
            }
            return(result);
        }
        // GET api/<controller>
        //public IEnumerable<string> Get() {
        //	return new string[] { "value1", "value2" };
        //}

        // GET api/<controller>/5
        public async Task <DriverProfile> Post(LoginModel Login)
        {
            NHibernateUserStore store = new NHibernateUserStore();
            var usr = await store.FindByNamePassAsync(Login.Username, Login.Password);

            if (usr.Driver != null)
            {
                string Origin = "", Destination = "", FranchiseNo = "", PlateNo = "";
                var    vehicle = (from x in usr.Driver.Vehicle.OfType <DriverVehicleModel>() where x.Status == StatusType.Active select x)
                                 .FirstOrDefault();

                if (vehicle != null)
                {
                    PlateNo     = vehicle.Vehicle.PlateNumber;
                    FranchiseNo = vehicle.Vehicle.FranchiseNo;
                    if (vehicle.Route != null)
                    {
                        Origin      = vehicle.Route.Origin;
                        Destination = vehicle.Route.Destination;
                    }
                }

                var driverProfile = new DriverProfile {
                    LastName    = usr.Driver.LastName,
                    FirstName   = usr.Driver.FirstName,
                    MiddleName  = usr.Driver.MiddleName,
                    PlateNo     = PlateNo,
                    FranchiseNo = FranchiseNo,
                    Origin      = Origin,
                    Destination = Destination,
                    Token       = usr.SecurityStamp
                };
                return(driverProfile);
            }
            return(null);
        }
        // POST api/<controller>
        public async Task Post(PRViewModel value)
        {
            var nh      = new NHibernateUserStore();
            var nhps    = new NHibernatePRStore();
            var session = HttpContext.Current.Session;
            //var sessionKey = Config.GetAppSetting("SessionKey").ToString();



            var user = nh.FindByStampAsync(value.SecurityStamp);

            if (user != null)
            {
                var nhcs      = new NHibernateCompanyStore();
                var utcDate   = value.DateNeeded.ToUniversalTime();
                var requestor = await nhcs.GetEmployeeAsync(value.RequestorId);

                DateTime dateNeeded;
                if (value.DateNeeded < DateTime.UtcNow)
                {
                    dateNeeded = DateTime.UtcNow.AddDays(6);
                }
                else
                {
                    dateNeeded = value.DateNeeded;
                }
                var prHeader = new PRHeaderModel()
                {
                    Status         = StatusType.Request,
                    Requestor      = requestor,
                    DeliveryAdress = value.DeliveryAdress,
                    NoteToBuyer    = value.NoteToBuyer,
                    DateNeeded     = dateNeeded,
                    CRC            = requestor.Team.CRC,
                    CreatedBy      = user.Result,
                    Lines          = new List <PRLinesModel>(),
                    Approvals      = new List <PRAprovalModel>(),
                };
                if (requestor.ImmediateLeader != null)
                {
                    var immedieateAprover = new PRAprovalModel()
                    {
                        Approver  = requestor.ImmediateLeader,
                        Status    = StatusType.Request,
                        CreatedBy = user.Result
                    };

                    prHeader.Approvals.Add(immedieateAprover);
                }
                if (requestor.Department != null)
                {
                    if (!prHeader.Approvals.Any(s => s.Approver.Id == requestor.Department.DepartmentLeader.Id))
                    {
                        var DepLeadAproval = new PRAprovalModel()
                        {
                            Approver  = requestor.Department.DepartmentLeader,
                            Status    = StatusType.Request,
                            CreatedBy = user.Result
                        };
                        prHeader.Approvals.Add(DepLeadAproval);
                    }
                }
                if (requestor.Company.Corfin != null && requestor.Company.Corfin.Id != requestor.position.Id)
                {
                    if (!prHeader.Approvals.Any(s => s.Approver.Id == requestor.Company.Corfin.Id))
                    {
                        var corfin = new PRAprovalModel()
                        {
                            Approver  = requestor.Company.Corfin,
                            Status    = StatusType.Request,
                            CreatedBy = user.Result
                        };
                        prHeader.Approvals.Add(corfin);
                    }
                }
                foreach (var line in value.Lines)
                {
                    var lin = await nhps.GetPRLineAsync(line.Id);

                    if (lin.Item != null)
                    {
                        if (lin.Item.SubCategory != null)
                        {
                            if (!prHeader.Approvals.Any(s => s.Approver.Id == lin.Item.SubCategory.Category.Approver.Id))
                            {
                                var ItemAproval = new PRAprovalModel()
                                {
                                    Approver  = lin.Item.SubCategory.Category.Approver,
                                    Status    = StatusType.Request,
                                    CreatedBy = user.Result
                                };
                                prHeader.Approvals.Add(ItemAproval);
                            }
                        }
                        lin.Description = lin.Item.Description;
                        lin.Supplier    = lin.Item.Supplier;
                        lin.UnitPrice   = lin.Item.Price;
                        lin.UOM         = lin.Item.UOM;
                    }
                    else
                    {
                        if (lin.NonCatalog.SubCategory != null)
                        {
                            if (!prHeader.Approvals.Any(s => s.Approver.Id == lin.NonCatalog.SubCategory.Category.Approver.Id))
                            {
                                var ItemAproval = new PRAprovalModel()
                                {
                                    Approver  = lin.NonCatalog.SubCategory.Category.Approver,
                                    Status    = StatusType.Request,
                                    CreatedBy = user.Result
                                };
                                prHeader.Approvals.Add(ItemAproval);
                            }
                        }
                        var item = lin.NonCatalog.Lines.Where(x => x.Selected == true && x.DeleteTime == null).SingleOrDefault();
                        lin.Description = item.Description;
                        lin.Supplier    = item.Supplier;
                        lin.UnitPrice   = item.Price;
                        lin.UOM         = item.UOM;
                    }

                    lin.TotalAmount  = lin.Quantity * lin.UnitPrice;
                    prHeader.Amount += lin.TotalAmount;
                    prHeader.Lines.Add(lin);
                }
                try {
                    await nhps.CreatePRHeaderAsync(prHeader);
                } catch (Exception e) {
                }
            }
        }
        public async Task <long> Post(NonCatalogViewModel value)
        {
            const long PurchaserHead = 6;

            var nhnch            = new NHibernateNonCatalogStore();
            var user             = new NHibernateUserStore();
            var nnc              = new NHibernateNonCatalogStore();
            var supplierStore    = new NHibernateISupplierStore();
            var subcategoryStore = new NHibernateCategoryStore();
            var company          = new NHibernateCompanyStore();
            var createdby        = await user.FindByStampAsync(value.SecurityStamp);

            var nonCatalog = await nnc.GetNonCatalogAsync(value.Id);

            if (nonCatalog == null)
            {
                nonCatalog = new NonCatalogItemHeadModel();
            }
            nonCatalog.Name        = value.Name;
            nonCatalog.Analysis    = value.Analysis;
            nonCatalog.SubCategory = await subcategoryStore.FindSubCategoryByIdAsync(value.SubCategoryId);

            nonCatalog.Requestor = await company.GetEmployeeAsync(value.RequestorId);

            nonCatalog.CreatedBy = createdby;
            nonCatalog.Approver  = await company.GetPositionByIdAsync(PurchaserHead);

            for (var line = 0; line < nonCatalog.Lines.Count; line++)
            {
                if (!value.Lines.Any(x => x.Id == nonCatalog.Lines.ElementAt(line).Id))
                {
                    nonCatalog.Lines.ElementAt(line).DeleteTime = DateTime.UtcNow;
                }
            }
            foreach (var line in value.Lines)
            {
                if (nonCatalog.Lines.Any(x => x.Id == line.Id) && line.Id != 0)
                {
                    continue;
                }
                var supplier = await supplierStore.FindSupplierByIdAsync(line.SupplierId);

                string tempSupplier = null;
                if (supplier == null)
                {
                    tempSupplier = line.TempSupplier;
                }

                var nonCatalogLine = new NonCatalogItemLinesModel {
                    Selected     = line.Selected,
                    Supplier     = supplier,
                    Price        = line.Price,
                    Description  = line.Description,
                    Quantity     = line.Quantity,
                    UOM          = line.UOM,
                    Discount     = line.Discount,
                    TotalAnount  = line.TotalAnount,
                    Availability = line.Availability,
                    Terms        = line.Terms,
                    Brand        = await supplierStore.FindBrandByIdAsync(line.BrandId),
                    CreatedBy    = createdby,
                    TempSupplier = tempSupplier
                };
                nonCatalog.Lines.Add(nonCatalogLine);
            }
            if (value.Approved)
            {
                nonCatalog.ApprovedBy   = createdby;
                nonCatalog.DateApproved = DateTime.UtcNow;
            }
            return(await nnc.CreateNonCatalogHeadAsync(nonCatalog));
        }
        // POST api/<controller>
        public async Task <string> Post(POViewModel value)
        {
            const long PurchaserHead = 6;
            var        nhss          = new NHibernateISupplierStore();
            var        nhcs          = new NHibernateCompanyStore();
            var        nhpos         = new NHibernatePOStore();
            var        nhus          = new NHibernateUserStore();
            var        company       = new NHibernateCompanyStore();
            var        requierDate   = value.RequiredDate < DateTime.UtcNow ? DateTime.UtcNow.AddDays(6) : value.RequiredDate;
            var        user          = await nhus.FindByStampAsync(value.SecurityStamp);

            var requestor = await nhcs.GetEmployeeAsync(value.RequestorId);

            var po = await nhpos.FindPOAByIdAsync(value.Id);

            if (po == null)
            {
                po = new POHeaderModel()
                {
                    Supplier       = await nhss.FindSupplierByIdAsync(value.SupplierId),
                    PaymentTerm    = value.PaymentTerm,
                    Requestor      = requestor,
                    DeliveryAdress = value.DeliveryAdress,
                    Status         = value.Status,
                    RequiredDate   = requierDate,
                    NoteToBuyer    = value.NoteToBuyer,
                    CreatedBy      = user,
                    Amount         = value.Amount,
                    CRC            = requestor.Team.CRC,
                    Lines          = new List <POLinesModel>()
                };
                foreach (var line in value.Lines)
                {
                    var nhps   = new NHibernatePRStore();
                    var poLine = new POLinesModel()
                    {
                        Quantity    = line.Quantity,
                        UOM         = line.UOM,
                        Description = line.Description,
                        UnitPrice   = line.UnitPrice,
                        PRLine      = await nhps.GetPRLineAsync(line.PRLineId),
                        Name        = line.Name,
                        CreatedBy   = user
                    };
                    po.Lines.Add(poLine);
                }
            }
            else
            {
                po.Requestor = await nhcs.GetEmployeeAsync(value.RequestorId);

                po.DeliveryAdress = value.DeliveryAdress;
                po.Amount         = value.Amount;
                for (var i = 0; i < po.Lines.Count; i++)
                {
                    var found = false;
                    var id    = po.Lines.ElementAt(i).Id;
                    foreach (var line in value.Lines)
                    {
                        if (line.Id == id)
                        {
                            found = true;
                            po.Lines.ElementAt(i).Quantity    = line.Quantity;
                            po.Lines.ElementAt(i).UOM         = line.UOM;
                            po.Lines.ElementAt(i).Description = line.Description;
                            po.Lines.ElementAt(i).UnitPrice   = line.UnitPrice;
                            break;
                        }
                    }
                    if (found == false)
                    {
                        po.Lines.ElementAt(i).DeleteTime = DateTime.UtcNow;
                    }
                }
            }
            if (value.Status == StatusType.ForApproval)
            {
                var approver = new POAprovalModel()
                {
                    Approver = await company.GetPositionByIdAsync(PurchaserHead),
                    Status   = StatusType.ForApproval
                };
                po.Approvals = new List <POAprovalModel>();
                po.Approvals.Add(approver);
                var costApprover = company.FindCostApprover(po.Amount);
                if (costApprover.Result != null)
                {
                    var approver2 = new POAprovalModel()
                    {
                        Approver = costApprover.Result.Approver,
                        Status   = StatusType.ForApproval
                    };
                    po.Approvals.Add(approver2);
                }
            }

            try {
                await nhpos.SaveOrUpdatePOHeaderAsync(po);
            } catch (Exception e) {
            }
            return(po.RequisitionNo);
        }