public void SavePurchaseOrder(int vendorId, OrderViewModel order, List <OrderDetailsViewModel> orderDetails)
        {
            Order       argOrder       = null;
            OrderDetail argOrderDetail = null;
            int         orderId        = order.OrderID;

            using (var context = new ERaceingSystemContext())
            {
                List <OrderDetail> orderDetailsOld = (from x in context.OrderDetails
                                                      where x.OrderID == orderId
                                                      select x).ToList();

                Order exists = (from x in context.Orders
                                where x.OrderID == orderId
                                select x).FirstOrDefault();
                if (exists == null)
                {
                    argOrder            = new Order();
                    argOrder.VendorID   = vendorId;
                    argOrder.EmployeeID = 20;
                    argOrder.SubTotal   = order.SubTotal;
                    argOrder.TaxGST     = order.Tax;
                    context.Orders.Add(argOrder);
                    context.SaveChanges();
                    orderId = argOrder.OrderID;
                }
                else
                {
                    foreach (OrderDetail item in orderDetailsOld)
                    {
                        context.OrderDetails.Remove(item);
                    }
                    argOrder            = exists;
                    argOrder.EmployeeID = 20;
                    argOrder.SubTotal   = order.SubTotal;
                    argOrder.TaxGST     = order.Tax;

                    context.SaveChanges();
                }



                foreach (OrderDetailsViewModel item in orderDetails)
                {
                    argOrderDetail               = new OrderDetail();
                    argOrderDetail.OrderID       = orderId;
                    argOrderDetail.ProductID     = item.ProductId;
                    argOrderDetail.Quantity      = item.Quantity;
                    argOrderDetail.OrderUnitSize = item.OrderUnitSize;
                    argOrderDetail.Cost          = item.UnitCost;
                    context.OrderDetails.Add(argOrderDetail);
                }

                context.SaveChanges();
            }
        }
Example #2
0
        public InvoiceViewModel Add_Racer(RosterViewModel roster, string employeeName)
        {
            using (var context = new ERaceingSystemContext())
            {
                decimal rentalFee = (from x in context.Cars
                                     where x.CarID == roster.CarID
                                     select x.CarClass.RaceRentalFee).FirstOrDefault();
                decimal subTotal = roster.RaceFee + rentalFee;
                decimal GST      = decimal.Multiply(subTotal, (decimal)0.05);

                int?employeeID = (from x in context.AspNetUsers
                                  where x.UserName == employeeName
                                  select x.EmployeeId).FirstOrDefault();
                Invoice invoice = new Invoice()
                {
                    EmployeeID  = (int)employeeID,
                    InvoiceDate = DateTime.Now,
                    SubTotal    = subTotal,
                    GST         = GST,
                    Total       = decimal.Add(subTotal, GST)
                };
                context.Invoices.Add(invoice);

                RaceDetail info = new RaceDetail()
                {
                    InvoiceID = invoice.InvoiceID,
                    RaceID    = roster.RaceID,
                    MemberID  = roster.MemberID,
                    RaceFee   = roster.RaceFee,
                    RentalFee = rentalFee,
                };
                if (roster.CarID != 0)
                {
                    var fee = (from x in context.Cars
                               where x.CarID == roster.CarID
                               select x.CarClass.RaceRentalFee).FirstOrDefault();
                    info.RentalFee = fee;
                    info.CarID     = roster.CarID;
                }
                context.RaceDetails.Add(info);

                context.SaveChanges();
                InvoiceViewModel createdInvoice = new InvoiceViewModel()
                {
                    InvoiceID  = invoice.InvoiceID,
                    EmployeeID = (int)employeeID,
                    Subtotal   = invoice.SubTotal,
                    GST        = invoice.GST,
                    Total      = decimal.Add(invoice.SubTotal, invoice.GST)
                };
                return(createdInvoice);
            }
        }
Example #3
0
        public void Edit_Results(List <ResultsViewModel> results)
        {
            int raceId = results[0].RaceID;

            using (var context = new ERaceingSystemContext())
            {
                List <RaceDetail> updatedResults = (from x in context.RaceDetails
                                                    where x.RaceID == raceId
                                                    select x).ToList();
                for (var index = 0; index < results.Count; index++)
                {
                    if (results[index].PenaltyID > 0)
                    {
                        if (results[index].PenaltyID == 4)
                        {
                            updatedResults[index].RunTime = null;
                        }
                    }
                    if (results[index].PenaltyID != 4)
                    {
                        updatedResults[index].RunTime = results[index].RunTime;
                    }

                    updatedResults[index].PenaltyID = results[index].PenaltyID;
                    if (results[index].PenaltyID == 0)
                    {
                        updatedResults[index].PenaltyID = null;
                    }
                }
                List <RaceDetail> placedResults = updatedResults.OrderBy(e => e.RunTime).ToList();

                int place = 1;
                foreach (RaceDetail result in placedResults)
                {
                    if (result.PenaltyID != 4)
                    {
                        result.Place = place;
                        place++;
                    }
                    else
                    {
                        result.Place = null;
                    }
                }

                context.SaveChanges();
            }
        }
        public int Purchase(InvoiceViewModel currentInvoice, string employeeName)
        {
            using (var context = new ERaceingSystemContext())
            {
                int?employeeID = (from x in context.AspNetUsers
                                  where x.UserName == employeeName
                                  select x.EmployeeId).FirstOrDefault();

                ICollection <InvoiceDetail> list = new List <InvoiceDetail>();

                foreach (var item in currentInvoice.Products)
                {
                    var result = (from x in context.Products
                                  where x.ProductID == item.ProductID
                                  select x).First();

                    result.QuantityOnHand = result.QuantityOnHand - item.Quantity;

                    var invoiceProducts = new InvoiceDetail
                    {
                        ProductID = item.ProductID,
                        Quantity  = item.Quantity,
                        Price     = item.Price
                    };
                    list.Add(invoiceProducts);
                }

                Invoice invoice = new Invoice
                {
                    InvoiceDate    = currentInvoice.InvoiceDate,
                    EmployeeID     = (int)employeeID,
                    SubTotal       = currentInvoice.Subtotal,
                    GST            = currentInvoice.GST,
                    Total          = currentInvoice.Total,
                    InvoiceDetails = list
                };
                context.Invoices.Add(invoice);
                context.SaveChanges();
                var invoiceId = invoice.InvoiceID;
                return(invoiceId);
            }
        }
Example #5
0
        public void Edit_Roster(RosterViewModel roster)
        {
            using (var context = new ERaceingSystemContext())
            {
                RaceDetail oldRaceDetail = (from x in context.RaceDetails
                                            where x.RaceDetailID == roster.RaceDetailID
                                            select x).FirstOrDefault();
                if (roster.CarID != 0)
                {
                    var fee = (from x in context.Cars
                               where x.CarID == roster.CarID
                               select x.CarClass.RaceRentalFee).SingleOrDefault();
                    oldRaceDetail.RentalFee = fee;
                }

                oldRaceDetail.CarID        = roster.CarID;
                oldRaceDetail.Comment      = roster.RaceDetailComment;
                oldRaceDetail.RefundReason = roster.RefundReason;
                oldRaceDetail.Refund       = roster.Refund;

                context.SaveChanges();
            }
        }
        public void DeletePurchaseOrder(int vendorId, OrderViewModel order, List <OrderDetailsViewModel> orderDetail)
        {
            int orderId = order.OrderID;

            using (var context = new ERaceingSystemContext())
            {
                List <OrderDetail> orderDetailsOld = (from x in context.OrderDetails
                                                      where x.OrderID == order.OrderID
                                                      select x).ToList();
                foreach (OrderDetail item in orderDetailsOld)
                {
                    context.OrderDetails.Remove(item);
                }
                Order oldOrder = (from x in context.Orders
                                  where x.OrderID == order.OrderID
                                  select x).FirstOrDefault();
                if (oldOrder != null)
                {
                    context.Orders.Remove(oldOrder);
                }

                context.SaveChanges();
            }
        }
        public int Refund(InvoiceViewModel currentInvoice, string employeeName)
        {
            using (var context = new ERaceingSystemContext())
            {
                ICollection <InvoiceDetail> list = new List <InvoiceDetail>();

                int?employeeID = (from x in context.AspNetUsers
                                  where x.UserName == employeeName
                                  select x.EmployeeId).FirstOrDefault();

                var originalInvoice = (from x in context.Invoices
                                       where x.InvoiceID == currentInvoice.InvoiceID
                                       select x).First();

                foreach (var item in currentInvoice.Products)
                {
                    var exist = (from x in context.StoreRefunds
                                 where x.OriginalInvoiceID == currentInvoice.InvoiceID &&
                                 x.ProductID == item.ProductID
                                 select x);

                    if (exist.Count() > 0)
                    {
                        throw new Exception("The item(s) were previously refunded. You can not refund an item again.");
                    }

                    var result = (from x in context.Products
                                  where x.ProductID == item.ProductID
                                  select x).First();

                    result.QuantityOnHand = result.QuantityOnHand + item.Quantity;

                    var invoiceProducts = new InvoiceDetail
                    {
                        ProductID = item.ProductID,
                        Quantity  = item.Quantity,
                        Price     = item.Price
                    };
                    list.Add(invoiceProducts);
                }

                Invoice refundInvoice = new Invoice
                {
                    InvoiceDate    = currentInvoice.InvoiceDate,
                    EmployeeID     = (int)employeeID,
                    SubTotal       = currentInvoice.Subtotal,
                    GST            = currentInvoice.GST,
                    Total          = currentInvoice.Total,
                    InvoiceDetails = list
                };

                foreach (var item in currentInvoice.Products)
                {
                    StoreRefund refund = new StoreRefund
                    {
                        ProductID         = item.ProductID,
                        OriginalInvoiceID = item.InvoiceID,
                        Reason            = item.RefundReason,
                        OriginalInvoice   = originalInvoice,
                        RefundInvoice     = refundInvoice
                    };
                    context.StoreRefunds.Add(refund);
                }
                ;
                context.SaveChanges();
                return(refundInvoice.InvoiceID);
            }
            //throw new NotImplementedException("Refund not implemented.");
        }