public async Task <IHttpActionResult> PutBranchProductPrice(int id, BranchProductPrice branchProductPrice)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != branchProductPrice.id)
            {
                return(BadRequest());
            }

            db.Entry(branchProductPrice).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BranchProductPriceExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public async Task <IHttpActionResult> GetBranchProductPrice(int id)
        {
            BranchProductPrice branchProductPrice = await db.BranchProductPrices.FindAsync(id);

            if (branchProductPrice == null)
            {
                return(NotFound());
            }

            return(Ok(branchProductPrice));
        }
        public async Task <IHttpActionResult> PostBranchProductPrice(BranchProductPrice branchProductPrice)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            db.BranchProductPrices.Add(branchProductPrice);
            await db.SaveChangesAsync();

            return(CreatedAtRoute("DefaultApi", new { id = branchProductPrice.id }, branchProductPrice));
        }
        public async Task <IHttpActionResult> DeleteBranchProductPrice(int id)
        {
            BranchProductPrice branchProductPrice = await db.BranchProductPrices.FindAsync(id);

            if (branchProductPrice == null)
            {
                return(NotFound());
            }

            db.BranchProductPrices.Remove(branchProductPrice);
            await db.SaveChangesAsync();

            return(Ok(branchProductPrice));
        }
Example #5
0
        public async Task <IHttpActionResult> PostOrder(OrderCreateDTO orderData)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            Branch branch = await db.Branches.FindAsync(orderData.branchId);

            if (branch == null)
            {
                return(BadRequest("Branch does not Exist"));
            }

            Customer customer = await db.Customers.FindAsync(branch.customerId);

            if (customer == null)
            {
                return(BadRequest("Customer does not Exist"));
            }

            if (customer.id != branch.customerId)
            {
                return(BadRequest("This branch does not belong to this customer"));
            }

            //ProductPrice price = await db.ProductPrices.Where(b => b.productId==product.id).SingleOrDefaultAsync();

            string   reg   = User.Identity.GetUserId();
            SalesRep sales = await db.SalesReps.Where(a => a.userid == reg).SingleAsync();

            TimeZoneInfo timeInfo = TimeZoneInfo.FindSystemTimeZoneById("South Africa Standard Time");
            DateTime     userTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, timeInfo);
            Order        order    = new Order
            {
                date       = userTime,
                customerId = branch.customerId,
                branchId   = orderData.branchId,
                status     = "pending",
                salesRepId = sales.id,
            };
            Order odr = db.Orders.Add(order);
            await db.SaveChangesAsync();

            double totalPrice = 0;
            List <OrderProduct> orderItems = new List <OrderProduct>();

            foreach (OrderItem orderItem in orderData.orderItem)
            {
                double  currentPrice = 0;
                Product product      = await db.Products.FindAsync(orderItem.productId);

                if (product == null)
                {
                    return(BadRequest("Product of id " + orderItem.productId + " does not Exist"));
                }

                BranchProductPrice priceBr = await db.BranchProductPrices.Where(b => b.productId == product.id && b.branchId == branch.id).SingleOrDefaultAsync();

                ProductPrice priceCus = await db.ProductPrices.Where(b => b.productId == product.id && b.customerId == customer.id).SingleOrDefaultAsync();


                if (priceBr != null)
                {
                    currentPrice = priceBr.amount * priceBr.case_size;
                    totalPrice  += currentPrice * orderItem.quantity;


                    orderItems.Add(new OrderProduct {
                        orderId = odr.id, price = currentPrice, productId = orderItem.productId, quantity = orderItem.quantity, sku = priceBr.sku
                    });
                }

                else if (priceCus != null)
                {
                    currentPrice = priceCus.amount * priceCus.case_size;
                    totalPrice  += currentPrice * orderItem.quantity;


                    orderItems.Add(new OrderProduct {
                        orderId = odr.id, price = currentPrice, productId = orderItem.productId, quantity = orderItem.quantity, sku = priceCus.sku
                    });
                }

                else
                {
                    return(BadRequest("This retailer does not have this product"));
                }


                //db.OrderProducts.
            }
            lock (Lock)
            {
                string year      = DateTime.Now.Year.ToString();
                string reference = "PO-" + year + "-";
                int    ordNum    = db.Orders.Count();
                if (ordNum == 0)
                {
                    ordNum    += 1;
                    reference += String.Format("{0:00000}", ordNum);
                }
                else
                {
                    ordNum     = db.Orders.Count(b => b.orderNumber.Substring(3, 4) == year);
                    ordNum    += 1;
                    reference += String.Format("{0:00000}", ordNum);
                }
                while (db.Orders.Count(d => d.orderNumber == reference) != 0)
                {
                    reference = "INV-" + year + "-" + String.Format("{0:00000}", ++ordNum);
                }


                odr.price           = totalPrice;
                odr.orderNumber     = reference;
                db.Entry(odr).State = EntityState.Modified;
                db.OrderProducts.AddRange(orderItems);
                db.SaveChanges();
            }


            return(Ok(odr.orderNumber));
        }