Ejemplo n.º 1
0
        public int Do_RefundTransaction(List <RefundItem> list)
        {
            // Check refund for Oinvoice already there
            using (var context = new RaceContext())
            {
                int invoiceID = list[0].InvoiceID;
                if (context.StoreRefunds.Where(x => x.InvoiceID == invoiceID) != null)
                {
                    var totals = CalculateTotals(list);
                    // Invoice with negative values
                    Invoice invoice = new Invoice();
                    invoice.EmployeeID  = list[0].EmployeeID;
                    invoice.GST         = (decimal)totals.Item2;
                    invoice.SubTotal    = (decimal)totals.Item1;
                    invoice.Total       = (decimal)totals.Item3;
                    invoice.InvoiceDate = DateTime.Now;

                    context.Invoices.Add(invoice);

                    // Invoice details

                    foreach (RefundItem item in list)
                    {
                        InvoiceDetail temp   = new InvoiceDetail();
                        StoreRefund   retemp = new StoreRefund();
                        if (item.ItemToBeRefunded)
                        {
                            temp.InvoiceID = invoice.InvoiceID;
                            temp.ProductID = item.ProductID;
                            temp.Quantity  = item.ProductQuantity;
                            temp.Price     = (decimal)item.ProductPrice;

                            retemp.InvoiceID         = invoice.InvoiceID;
                            retemp.ProductID         = item.ProductID;
                            retemp.OriginalInvoiceID = item.InvoiceID;
                            retemp.Reason            = item.Reason;
                            context.StoreRefunds.Add(retemp);

                            Product prodTemp = context.Products.Where(x => x.ProductID == item.ProductID && x.CategoryID == item.CategoryID).Single();
                            prodTemp.QuantityOnHand      += item.ProductQuantity;
                            context.Entry(prodTemp).State = System.Data.Entity.EntityState.Modified;
                        }
                    }
                    context.SaveChanges();
                    return(invoice.InvoiceID);
                }
                else
                {
                    throw new BusinessRuleException("Refund Error", new List <string> {
                        "Refund already exsists"
                    });
                }
            }
        }
Ejemplo n.º 2
0
        public void CreateRefund(RefundRequired request)
        {
            using (var context = new eRaceContext())
            {
                foreach (var item in request.RequiredInvoice)
                {
                    var newinvoice = new Invoice
                    {
                        EmployeeID  = item.EmployeeID,
                        InvoiceDate = item.InvoiceDate,
                        SubTotal    = item.SubTotal,
                        GST         = item.GST,
                        Total       = item.Total
                    };
                    context.Invoices.Add(newinvoice);
                }

                foreach (var item in request.ReuquiredDetail)
                {
                    var newdetail = new InvoiceDetail
                    {
                        ProductID = item.ProductID,
                        Quantity  = item.Quantity
                    };

                    var product = (from x in context.Products
                                   where x.ProductID == item.ProductID
                                   select x).FirstOrDefault();
                    product.QuantityOnHand += newdetail.Quantity;
                    context.Entry(product).Property(y => y.QuantityOnHand).IsModified = true;
                }


                foreach (var item in request.RequiredStore)
                {
                    var newstore = new StoreRefund
                    {
                        OriginalInvoiceID = item.OriginalInvoiceID,
                        ProductID         = item.ProductID,
                        Reason            = item.Reason,
                    };
                    context.StoreRefunds.Add(newstore);
                }
                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.");
        }