public async Task <ActionResult <BillDetail> > PostBillDetail(BillDetail billDetail)
        {
            List <BillDetail> billDetails;
            EnterProduct      enterProduct = FindEnterProduct(billDetail.ProductId);

            using (SqlConnection connection = new SqlConnection(Connection))
            {
                billDetails = connection.Query <BillDetail>("select BillDetails.* from BillDetails inner join BookingDetails on BookingDetails.ID=BillDetails.BookingDetailId where BookingDetails.ID =" + billDetail.BookingDetailId).ToList();
            }
            for (int i = 0; i < billDetails.Count; i++)
            {
                if (billDetails[i].ProductId == billDetail.ProductId)
                {
                    if (FindQuantityProduct(billDetail.ProductId) > 0)
                    {
                        enterProduct.Quantity -= billDetail.Quantity;
                        _context.Entry(enterProduct).State = EntityState.Modified;
                        billDetails[i].Quantity           += billDetail.Quantity;
                        billDetails[i].Total = billDetails[i].Price * billDetails[i].Quantity;
                        _context.BillDetails.Update(billDetails[i]);
                        await _context.SaveChangesAsync();

                        return(NoContent());
                    }
                }
            }

            enterProduct.Quantity -= billDetail.Quantity;
            _context.Entry(enterProduct).State = EntityState.Modified;
            _context.BillDetails.Add(billDetail);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetBillDetail", new { id = billDetail.ID }, billDetail));
        }
Пример #2
0
        private EnterProduct FindEnterProductNew(int id)
        {
            EnterProduct enterProduct = new EnterProduct();
            string       sql          = "SELECT * FROM EnterProducts Where EnterProducts.IsDelete = 0 AND EnterProducts.UId =" + id;

            using (SqlConnection connection = new SqlConnection(Connection))
            {
                enterProduct = connection.Query <EnterProduct>(sql).FirstOrDefault();
            }
            return(enterProduct);
        }
Пример #3
0
        public async Task <ActionResult <EnterProduct> > PostEnterProduct(EnterProduct[] enterProducts)
        {
            for (int i = 0; i < enterProducts.Count(); i++)
            {
                EnterProduct enterProduct = enterProducts[i];
                enterProduct.CreateAt = DateTime.Now;
                enterProduct.Quantity = enterProduct.RemainingAmount;
                enterProduct.Product  = _context.Products.Find(enterProduct.ProductId);
                _context.EnterProducts.Add(enterProduct);
            }

            await _context.SaveChangesAsync();

            return(Ok(null));
        }
Пример #4
0
        public async Task <IActionResult> PutEnterProduct(int id, EnterProduct enterProduct)
        {
            EnterProduct enterProductOld = _context.EnterProducts.Find(id);

            if (enterProductOld.IsDelete)
            {
                EnterProduct enterProductNewUpdate = FindEnterProductNew(enterProduct.UId);
                int          idEnterProduct        = enterProduct.ID;
                enterProduct.ID          = 0;
                enterProductNewUpdate.ID = 0;

                enterProduct.CreateAt = enterProductOld.CreateAt;
                int idEnterProductNewUpdate = enterProductNewUpdate.ID;
                if (JSONEquals(enterProduct, enterProductNewUpdate))
                {
                    return(Ok(null));
                }
                else
                {
                    enterProductNewUpdate.ID = idEnterProductNewUpdate;
                    return(Ok(enterProductNewUpdate));
                }
            }
            else
            {
                if (enterProduct.UId == 0)
                {
                    enterProduct.UId = enterProduct.ID;
                }
                EnterProduct enterProductSave = enterProduct;
                enterProductSave.ID                   = 0;
                enterProductOld.IsDelete              = true;
                enterProduct.Quantity                 = enterProduct.Quantity + (enterProduct.RemainingAmount - enterProductOld.RemainingAmount);
                enterProductSave.CreateAt             = enterProductOld.CreateAt;
                _context.Entry(enterProductOld).State = EntityState.Modified;
                _context.EnterProducts.Add(enterProductSave);
                await _context.SaveChangesAsync();
            }


            return(Ok(null));
        }