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);
        }
        // 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) {
                }
            }
        }