public async Task <IActionResult> CreatePurchaseOrder(POHForDetailDto pOHForDetailDto, [FromQuery] bool isForSending)
        {
            if (pOHForDetailDto == null)
            {
                return(BadRequest("Empty Body"));
            }

            var pOHToCreate = _mapper.Map <PurchaseOrderHeader>(pOHForDetailDto);

            if (User.FindAll(ClaimTypes.Role).Any(a => a.Value == "Admin"))
            {
                pOHToCreate.isFromOutlet = pOHForDetailDto.isForOutlet;
            }
            else
            {
                pOHToCreate.isFromOutlet = User.FindAll(ClaimTypes.Role).Any(a => a.Value == "OutletManager") ? true : false;
            }

            pOHToCreate.Status = 0;
            // pOHToCreate.Supplier = await _repository.GetSupplier(pOHForDetailDto.SupplierId);

            await _repository.CreatePurchaseOrder(pOHToCreate);

            if (await _repository.SaveAll())
            {
                //sending mail
                var filterParams = GetFilterParams();

                var pOHFromRepository = await _repository.GetPurchaseOrder(pOHToCreate.Id, filterParams);

                if (isForSending)
                {
                    var htmlBody = GenerateMailHtml(pOHFromRepository.PONumber, User.FindAll(ClaimTypes.Name).FirstOrDefault().ToString(),
                                                    pOHFromRepository.Supplier.Name, pOHFromRepository.DeliveryMethod, pOHFromRepository.OrderDate,
                                                    pOHFromRepository.DeliveryDate, pOHFromRepository.PurchaseOrderDetail);

                    if (await SendMail("*****@*****.**", "*****@*****.**", "Purchase Order From Upland Bake house",
                                       htmlBody, "*****@*****.**", "hell123boy"))
                    {
                        pOHFromRepository.Status = 1;
                    }
                    else
                    {
                        var supFromRepo = pOHFromRepository.Supplier;
                        pOHFromRepository.Status = 1;
                        Notification noti = new Notification
                        {
                            Title   = "Purchase Order No " + pOHFromRepository.PONumber + " Failed To send",
                            Message = "purchase Order No "
                                      + pOHFromRepository.PONumber
                                      + " Failed To send , Supplier Detail are as Follows "
                                      + " Supplier Name  "
                                      + supFromRepo.Name
                                      + " Supplier Contact "
                                      + supFromRepo.ContactNumber
                                      + " Supplier email "
                                      + supFromRepo.Email
                                      + " Supplier Address "
                                      + supFromRepo.Address,
                            DateTime = DateTime.Now,
                            UserId   = pOHFromRepository.UserId,
                            Status   = 0
                        };

                        _repository.Add(noti);
                    }
                }

                if (isForSending == true && pOHFromRepository.Status == 0)
                {
                    return(Ok(new { error = "Failed to send" }));
                }

                await _repository.SaveAll();

                var pOHtoReturn = _mapper.Map <POHForDetailDto>(pOHToCreate);
                return(CreatedAtRoute(nameof(GetPurchaseOrder), new { pOHToCreate.Id }, pOHtoReturn));
            }

            return(BadRequest("Could not create Purchase Order"));
        }
        public async Task <IActionResult> UpdatePurchaseOrder(int id, POHForDetailDto pOHForDetailDto, [FromQuery] bool isForSending)
        {
            if (pOHForDetailDto == null)
            {
                return(BadRequest("Empty Body"));
            }

            var filterParams = GetFilterParams();

            var pOHFromRepository = await _repository.GetPurchaseOrder(id, filterParams);

            if (pOHFromRepository == null)
            {
                return(BadRequest("Purchase Order not available"));
            }

            var supFromRepo = await _repository.Get <Supplier>(pOHForDetailDto.SupplierId);

            var bPFromRepo = await _repository.Get <BusinessPlace>(pOHForDetailDto.BusinessPlaceId);


            if (User.FindAll(ClaimTypes.Role).Any(a => a.Value == "Admin"))
            {
                pOHFromRepository.isFromOutlet = pOHForDetailDto.isForOutlet;
            }
            else
            {
                pOHFromRepository.isFromOutlet = User.FindAll(ClaimTypes.Role).Any(a => a.Value == "OutletManager") ? true : false;
            }

            pOHFromRepository.UserId         = pOHForDetailDto.UserId;
            pOHFromRepository.Supplier       = supFromRepo;
            pOHFromRepository.BusinessPlace  = bPFromRepo;
            pOHFromRepository.DeliveryMethod = pOHForDetailDto.DeliveryMethod;
            pOHFromRepository.DeliveryDate   = DateTime.Parse(pOHForDetailDto.DeliveryDate);
            pOHFromRepository.OrderDate      = DateTime.Parse(pOHForDetailDto.OrderDate);
            pOHFromRepository.ModifiedDate   = pOHForDetailDto.ModifiedDate;
            pOHFromRepository.Status         = 0;

            foreach (var pod in pOHFromRepository.PurchaseOrderDetail)
            {
                _repository.Delete(pod);
            }

            pOHFromRepository.PurchaseOrderDetail.Clear();

            foreach (var pod in pOHForDetailDto.PODetail)
            {
                pOHFromRepository.PurchaseOrderDetail.Add(new PurchaseOrderDetail
                {
                    DueDate      = DateTime.Parse(pod.DueDate),
                    Item         = await _repository.Get <Item>(pod.ItemId),
                    OrderQty     = pod.OrderQty,
                    UnitPrice    = (decimal)pod.UnitPrice,
                    LineTotal    = (decimal)pod.LineTotal,
                    ModifiedTime = pod.ModifiedTime
                });
            }
            //sending mail
            if (isForSending)
            {
                var htmlBody = GenerateMailHtml(pOHFromRepository.PONumber, User.FindAll(ClaimTypes.Name).FirstOrDefault().ToString(),
                                                pOHFromRepository.Supplier.Name, pOHFromRepository.DeliveryMethod, pOHFromRepository.OrderDate,
                                                pOHFromRepository.DeliveryDate, pOHFromRepository.PurchaseOrderDetail);

                if (await SendMail("*****@*****.**", "*****@*****.**", "Purchase Order From Upland Bake house",
                                   htmlBody, "*****@*****.**", "hell123boy"))
                {
                    pOHFromRepository.Status = 1;
                }
                else
                {
                    pOHFromRepository.Status = 1;
                    Notification noti = new Notification
                    {
                        Title   = "Purchase Order No " + pOHFromRepository.PONumber + " Failed To send",
                        Message = "purchase Order No "
                                  + pOHFromRepository.PONumber
                                  + " Failed To send , Supplier Detail are as Follows "
                                  + " Supplier Name  "
                                  + supFromRepo.Name
                                  + " Supplier Contact "
                                  + supFromRepo.ContactNumber
                                  + " Supplier email "
                                  + supFromRepo.Email
                                  + " Supplier Address "
                                  + supFromRepo.Address,
                        DateTime = DateTime.Now,
                        UserId   = pOHFromRepository.UserId,
                        Status   = 0
                    };

                    _repository.Add(noti);
                }
            }
            else
            {
                pOHFromRepository.Status = 0;
            }

            if (await _repository.SaveAll())
            {
                if (isForSending == true && pOHFromRepository.Status == 0)
                {
                    return(Ok(new { error = "Failed to send" }));
                }
                else
                {
                    return(NoContent());
                }
            }


            throw new System.Exception($"Updating item {id} failed on save");
        }